X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=openbox%2Fmainloop.c;h=b2921207b45ab716b4263c433f2d41ed0624b662;hb=07126124c9242a5a20883c3145464730e50afe94;hp=9df84d34b1ccedc571c2e2ddf462d0a0446b53ae;hpb=87342f85eb75079486e5bbc410f6002ee443dd6a;p=chaz%2Fopenbox diff --git a/openbox/mainloop.c b/openbox/mainloop.c index 9df84d34..b2921207 100644 --- a/openbox/mainloop.c +++ b/openbox/mainloop.c @@ -1,4 +1,24 @@ +/* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*- + + mainloop.c for the Openbox window manager + Copyright (c) 2006 Mikael Magnusson + Copyright (c) 2003-2007 Dana Jansens + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + See the COPYING file for a copy of the GNU General Public License. +*/ + #include "mainloop.h" +#include "event.h" #include #include @@ -19,13 +39,13 @@ typedef struct _ObMainLoopFdHandlerType ObMainLoopFdHandlerType; static GSList *all_loops; /* signals are global to all loops */ -struct { +static struct { guint installed; /* a ref count */ struct sigaction oldact; } all_signals[NUM_SIGNALS]; /* a set of all possible signals */ -sigset_t all_signals_set; +static sigset_t all_signals_set; /* signals which cause a core dump, these can't be used for callbacks */ static gint core_signals[] = @@ -75,6 +95,7 @@ struct _ObMainLoopTimer gulong delay; GSourceFunc func; gpointer data; + GEqualFunc equal; GDestroyNotify destroy; /* The timer needs to be freed */ @@ -83,6 +104,10 @@ struct _ObMainLoopTimer GTimeVal last; /* When this timer will next trigger */ GTimeVal timeout; + + /* Only allow a timer's function to fire once per run through the list, + so that it doesn't get locked in there forever */ + gboolean fired; }; struct _ObMainLoopSignalHandlerType @@ -195,8 +220,6 @@ void ob_main_loop_destroy(ObMainLoop *loop) /* only do this if we're the last loop destroyed */ if (!all_loops) { - guint i; - /* grab all the signals that cause core dumps */ for (i = 0; i < NUM_CORE_SIGNALS; ++i) { if (all_signals[core_signals[i]].installed) { @@ -244,7 +267,7 @@ void ob_main_loop_run(ObMainLoop *loop) for (i = 0; i < NUM_SIGNALS; ++i) { while (loop->signals_fired[i]) { for (it = loop->signal_handlers[i]; - it; it = g_slist_next(it)) { + it; it = g_slist_next(it)) { ObMainLoopSignalHandlerType *h = it->data; h->func(i, h->data); } @@ -262,11 +285,12 @@ void ob_main_loop_run(ObMainLoop *loop) ObMainLoopXHandlerType *h = it->data; h->func(&e, h->data); } - } while (XPending(loop->display)); + } while (XPending(loop->display) && loop->run); } else { /* this only runs if there were no x events received */ timer_dispatch(loop, (GTimeVal**)&wait); + selset = loop->fd_set; /* there is a small race condition here. if a signal occurs between this if() and the select() then we will not process @@ -340,7 +364,8 @@ static void sighandler(gint sig) /* XXX special case for signals that default to core dump. but throw some helpful output here... */ - fprintf(stderr, "Fuck yah. Core dump. (Signal=%d)\n", sig); + fprintf(stderr, "How are you gentlemen? All your base are" + " belong to us. (Openbox received signal %d)\n", sig); /* die with a core dump */ abort(); @@ -476,39 +501,42 @@ void ob_main_loop_fd_remove(ObMainLoop *loop, #define NEAREST_TIMEOUT(loop) \ (((ObMainLoopTimer*)(loop)->timers->data)->timeout) -static long timecompare(GTimeVal *a, GTimeVal *b) +static glong timecompare(GTimeVal *a, GTimeVal *b) { - long r; - - if ((r = b->tv_sec - a->tv_sec)) return r; - return b->tv_usec - a->tv_usec; - + glong r; + if ((r = a->tv_sec - b->tv_sec)) return r; + return a->tv_usec - b->tv_usec; } static void insert_timer(ObMainLoop *loop, ObMainLoopTimer *ins) { GSList *it; for (it = loop->timers; it; it = g_slist_next(it)) { - ObMainLoopTimer *t = it->data; + ObMainLoopTimer *t = it->data; if (timecompare(&ins->timeout, &t->timeout) <= 0) { - loop->timers = g_slist_insert_before(loop->timers, it, ins); - break; - } + loop->timers = g_slist_insert_before(loop->timers, it, ins); + break; + } } if (it == NULL) /* didnt fit anywhere in the list */ - loop->timers = g_slist_append(loop->timers, ins); + loop->timers = g_slist_append(loop->timers, ins); } void ob_main_loop_timeout_add(ObMainLoop *loop, gulong microseconds, GSourceFunc handler, gpointer data, + GEqualFunc cmp, GDestroyNotify notify) { ObMainLoopTimer *t = g_new(ObMainLoopTimer, 1); + + g_assert(microseconds > 0); /* if it's 0 it'll cause an infinite loop */ + t->delay = microseconds; t->func = handler; t->data = data; + t->equal = cmp; t->destroy = notify; t->del_me = FALSE; g_get_current_time(&loop->now); @@ -525,9 +553,22 @@ void ob_main_loop_timeout_remove(ObMainLoop *loop, for (it = loop->timers; it; it = g_slist_next(it)) { ObMainLoopTimer *t = it->data; - if (t->func == handler) { + if (t->func == handler) t->del_me = TRUE; - break; + } +} + +void ob_main_loop_timeout_remove_data(ObMainLoop *loop, GSourceFunc handler, + gpointer data, gboolean cancel_dest) +{ + GSList *it; + + for (it = loop->timers; it; it = g_slist_next(it)) { + ObMainLoopTimer *t = it->data; + if (t->func == handler && t->equal(t->data, data)) { + t->del_me = TRUE; + if (cancel_dest) + t->destroy = NULL; } } } @@ -555,48 +596,66 @@ static gboolean nearest_timeout_wait(ObMainLoop *loop, GTimeVal *tm) static void timer_dispatch(ObMainLoop *loop, GTimeVal **wait) { + GSList *it, *next; + + gboolean fired = FALSE; + g_get_current_time(&loop->now); - while (loop->timers != NULL) { - ObMainLoopTimer *curr = loop->timers->data; /* get the top element */ - /* since timer_stop doesn't actually free the timer, we have to do our - real freeing in here. - */ - if (curr->del_me) { + for (it = loop->timers; it; it = next) { + ObMainLoopTimer *curr; + + next = g_slist_next(it); + + curr = it->data; + + /* since timer_stop doesn't actually free the timer, we have to do our + real freeing in here. + */ + if (curr->del_me) { /* delete the top */ - loop->timers = g_slist_delete_link(loop->timers, loop->timers); - g_free(curr); - continue; - } - - /* the queue is sorted, so if this timer shouldn't fire, none are - ready */ - if (timecompare(&NEAREST_TIMEOUT(loop), &loop->now) <= 0) - break; - - /* we set the last fired time to delay msec after the previous firing, - then re-insert. timers maintain their order and may trigger more - than once if they've waited more than one delay's worth of time. - */ - loop->timers = g_slist_delete_link(loop->timers, loop->timers); - g_time_val_add(&curr->last, curr->delay); - if (curr->func(curr->data)) { + loop->timers = g_slist_delete_link(loop->timers, it); + if (curr->destroy) + curr->destroy(curr->data); + g_free(curr); + continue; + } + + /* the queue is sorted, so if this timer shouldn't fire, none are + ready */ + if (timecompare(&NEAREST_TIMEOUT(loop), &loop->now) > 0) + break; + + /* we set the last fired time to delay msec after the previous firing, + then re-insert. timers maintain their order and may trigger more + than once if they've waited more than one delay's worth of time. + */ + loop->timers = g_slist_delete_link(loop->timers, it); + g_time_val_add(&curr->last, curr->delay); + if (curr->func(curr->data)) { g_time_val_add(&curr->timeout, curr->delay); insert_timer(loop, curr); - } else if (curr->destroy) { - curr->destroy(curr->data); + } else { + if (curr->destroy) + curr->destroy(curr->data); + g_free(curr); } - /* if at least one timer fires, then don't wait on X events, as there - may already be some in the queue from the timer callbacks. - */ - loop->ret_wait.tv_sec = loop->ret_wait.tv_usec = 0; - *wait = &loop->ret_wait; - return; + /* the timer queue has been shuffled, start from the beginning + (which is the next one to fire) */ + next = loop->timers; + + fired = TRUE; } - if (nearest_timeout_wait(loop, &loop->ret_wait)) - *wait = &loop->ret_wait; + if (fired) { + /* if at least one timer fires, then don't wait on X events, as there + may already be some in the queue from the timer callbacks. + */ + loop->ret_wait.tv_sec = loop->ret_wait.tv_usec = 0; + *wait = &loop->ret_wait; + } else if (nearest_timeout_wait(loop, &loop->ret_wait)) + *wait = &loop->ret_wait; else - *wait = NULL; + *wait = NULL; }