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 121 122 123 124 125 126 127 128 129 130 131 132
|
/* 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. */
#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;
#if GLIB_MAJOR_VERSION >= 2
static GPollFunc save_poll_func = NULL;
#endif
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;
}
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);
#if GLIB_MAJOR_VERSION >= 2
save_poll_func = g_main_context_get_poll_func(g_main_context_default());
g_main_context_set_poll_func(g_main_context_default(), on_poll);
#else
g_main_set_poll_func(on_poll);
#endif
return oop_sys_source(sys);
}
void *oop_glib_return(void) {
if (&use_count == ret) return NULL;
return ret;
}
#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
void oop_glib_delete(void) {
assert(use_count > 0 && "oop_glib_delete() called too much");
if (0 != --use_count) return;
#if GLIB_MAJOR_VERSION >= 2
g_main_context_set_poll_func(g_main_context_default(), save_poll_func);
#elif defined(HAVE_POLL_H)
g_main_set_poll_func(real_poll);
#else
return;
#endif
oop_select_delete(sel);
oop_sys_delete(sys);
}
|