*/
#include "obt/mainloop.h"
+#include "obt/display.h"
#include "obt/util.h"
#include <stdio.h>
static void sighandler(gint sig);
static void timer_dispatch(ObtMainLoop *loop, GTimeVal **wait);
static void fd_handler_destroy(gpointer data);
+static void calc_max_fd(ObtMainLoop *loop);
struct _ObtMainLoop
{
GDestroyNotify destroy;
};
-ObtMainLoop *obt_main_loop_new(Display *display)
+ObtMainLoop *obt_main_loop_new()
{
ObtMainLoop *loop;
loop = g_new0(ObtMainLoop, 1);
loop->ref = 1;
- loop->display = display;
- loop->fd_x = ConnectionNumber(display);
FD_ZERO(&loop->fd_set);
- FD_SET(loop->fd_x, &loop->fd_set);
- loop->fd_max = loop->fd_x;
+ loop->fd_x = -1;
+ loop->fd_max = -1;
loop->fd_handlers = g_hash_table_new_full(g_int_hash, g_int_equal,
NULL, fd_handler_destroy);
h->func = handler;
h->data = data;
h->destroy = notify;
+
+ if (!loop->x_handlers) {
+ g_assert(obt_display); /* is the display open? */
+
+ loop->display = obt_display;
+ loop->fd_x = ConnectionNumber(loop->display);
+ FD_SET(loop->fd_x, &loop->fd_set);
+ calc_max_fd(loop);
+ }
+
loop->x_handlers = g_slist_prepend(loop->x_handlers, h);
}
void obt_main_loop_x_remove(ObtMainLoop *loop,
- ObtMainLoopXHandler handler)
+ ObtMainLoopXHandler handler)
{
GSList *it, *next;
g_free(h);
}
}
+
+ if (!loop->x_handlers) {
+ FD_CLR(loop->fd_x, &loop->fd_set);
+ calc_max_fd(loop);
+ }
}
/*** SIGNAL WATCHERS ***/
gint fd)
{
g_hash_table_remove(loop->fd_handlers, &fd);
+ calc_max_fd(loop);
}
/*** TIMEOUTS ***/
exit(EXIT_SUCCESS);
}
- ob_main_loop = obt_main_loop_new(obt_display);
+ ob_main_loop = obt_main_loop_new();
/* set up signal handler */
obt_main_loop_signal_add(ob_main_loop, SIGUSR1, signal_handler, NULL,NULL);