File: glib.c

package info (click to toggle)
liboop 0.8-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 636 kB
  • ctags: 435
  • sloc: sh: 7,375; ansic: 2,426; makefile: 111
file content (120 lines) | stat: -rw-r--r-- 2,778 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* glib.c, liboop, copyright 1999 Dan Egnor

   This is free software; you can redistribute it and/or modify it under the
   terms of the GNU Lesser General Public License, version 2.1 or later.
   See the file COPYING for details. */

#ifdef HAVE_GLIB

#include "glib.h"
#include "oop-glib.h"
#include "oop.h"

#include <assert.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>

#ifdef HAVE_POLL_H
#include <poll.h>
#endif

static int use_count = 0;
static oop_source_sys *sys;
static oop_adapter_select *sel;

static fd_set read_set,write_set,except_set;
static int count;
static void *ret = NULL;

static void *on_select(
	oop_adapter_select *s,int num,fd_set *r,fd_set *w,fd_set *x,
	struct timeval now,void *unused) 
{
	read_set = *r;
	write_set = *w;
	except_set = *x;
	count = num;
	return &use_count;
}

static gint on_poll(GPollFD *array,guint num,gint timeout) {
	struct timeval tv;
	int i;

	FD_ZERO(&read_set);
	FD_ZERO(&write_set);
	FD_ZERO(&except_set);
	count = 0;
	for (i = 0; i < num; ++i) {
		if (array[i].events & G_IO_IN)
			FD_SET(array[i].fd,&read_set);
		if (array[i].events & G_IO_OUT)
			FD_SET(array[i].fd,&write_set);
		if (array[i].events & G_IO_PRI)
			FD_SET(array[i].fd,&except_set);
		/* {G_IO_,POLL}{ERR,HUP,INVAL} don't correspond to anything
		   in select(2), and aren't `normal' events anyway. */
		if (array[i].fd >= count)
			count = 1 + array[i].fd;
	}

	tv.tv_sec = timeout / 1000;
	tv.tv_usec = timeout % 1000;

	oop_select_set(sel,count,&read_set,&write_set,&except_set,
		       timeout < 0 ? NULL : &tv);
	ret = oop_sys_run(sys);

	if (&use_count != ret) {
		/* I really wish I could: g_main_quit(glib_loop); */
		return -1;
		/* but they even ignore the error return... sigh. */
	}

	for (i = 0; i < num; ++i) {
		if (FD_ISSET(array[i].fd,&read_set))
			array[i].revents |= G_IO_IN;
		if (FD_ISSET(array[i].fd,&write_set))
			array[i].revents |= G_IO_OUT;
		if (FD_ISSET(array[i].fd,&except_set))
			array[i].revents |= G_IO_PRI;
	}

	return count;
}

#ifdef HAVE_POLL_H
static gint real_poll(GPollFD *array,guint num,gint timeout) {
	assert(sizeof(GPollFD) == sizeof(struct pollfd));
	return poll((struct pollfd *) array,num,timeout);
}
#endif

oop_source *oop_glib_new(void) {
	if (use_count++) return oop_sys_source(sys);

	sys = oop_sys_new();
	sel = oop_select_new(oop_sys_source(sys),on_select,NULL);
	g_main_set_poll_func(on_poll);
	return oop_sys_source(sys);
}

void *oop_glib_return(void) {
	if (&use_count == ret) return NULL;
	return ret;
}

#ifdef HAVE_POLL_H
void oop_glib_delete(void) {
	assert(use_count > 0 && "oop_glib_delete() called too much");
	if (0 != --use_count) return;

	oop_select_delete(sel);
	oop_sys_delete(sys);
	g_main_set_poll_func(real_poll);
}
#endif

#endif