1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 mainloop.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 See the COPYING file for a copy of the GNU General Public License.
25 #include <sys/select.h>
28 typedef struct _ObMainLoopTimer ObMainLoopTimer
;
29 typedef struct _ObMainLoopSignal ObMainLoopSignal
;
30 typedef struct _ObMainLoopSignalHandlerType ObMainLoopSignalHandlerType
;
31 typedef struct _ObMainLoopXHandlerType ObMainLoopXHandlerType
;
32 typedef struct _ObMainLoopFdHandlerType ObMainLoopFdHandlerType
;
34 /* this should be more than the number of possible signals on any
36 #define NUM_SIGNALS 99
38 /* all created ObMainLoops. Used by the signal handler to pass along signals */
39 static GSList
*all_loops
;
41 /* signals are global to all loops */
43 guint installed
; /* a ref count */
44 struct sigaction oldact
;
45 } all_signals
[NUM_SIGNALS
];
47 /* a set of all possible signals */
48 sigset_t all_signals_set
;
50 /* signals which cause a core dump, these can't be used for callbacks */
51 static gint core_signals
[] =
64 #define NUM_CORE_SIGNALS (sizeof(core_signals) / sizeof(core_signals[0]))
66 static void sighandler(gint sig
);
67 static void timer_dispatch(ObMainLoop
*loop
, GTimeVal
**wait
);
68 static void fd_handler_destroy(gpointer data
);
74 gboolean run
; /* do keep running */
75 gboolean running
; /* is still running */
79 gint fd_x
; /* The X fd is a special case! */
81 GHashTable
*fd_handlers
;
88 gboolean signal_fired
;
89 guint signals_fired
[NUM_SIGNALS
];
90 GSList
*signal_handlers
[NUM_SIGNALS
];
95 struct _ObMainLoopTimer
100 GDestroyNotify destroy
;
102 /* The timer needs to be freed */
104 /* The time the last fire should've been at */
106 /* When this timer will next trigger */
110 struct _ObMainLoopSignalHandlerType
115 ObMainLoopSignalHandler func
;
116 GDestroyNotify destroy
;
119 struct _ObMainLoopXHandlerType
123 ObMainLoopXHandler func
;
124 ObMainLoopXDoneHandler done_func
;
125 GDestroyNotify destroy
;
128 struct _ObMainLoopFdHandlerType
133 ObMainLoopFdHandler func
;
134 GDestroyNotify destroy
;
137 ObMainLoop
*ob_main_loop_new(Display
*display
)
141 loop
= g_new0(ObMainLoop
, 1);
142 loop
->display
= display
;
143 loop
->fd_x
= ConnectionNumber(display
);
144 FD_ZERO(&loop
->fd_set
);
145 FD_SET(loop
->fd_x
, &loop
->fd_set
);
146 loop
->fd_max
= loop
->fd_x
;
148 loop
->fd_handlers
= g_hash_table_new_full(g_int_hash
, g_int_equal
,
149 NULL
, fd_handler_destroy
);
151 g_get_current_time(&loop
->now
);
153 /* only do this if we're the first loop created */
156 struct sigaction action
;
159 /* initialize the all_signals_set */
160 sigfillset(&all_signals_set
);
162 sigemptyset(&sigset
);
163 action
.sa_handler
= sighandler
;
164 action
.sa_mask
= sigset
;
165 action
.sa_flags
= SA_NOCLDSTOP
;
167 /* grab all the signals that cause core dumps */
168 for (i
= 0; i
< NUM_CORE_SIGNALS
; ++i
) {
169 /* SIGABRT is curiously not grabbed here!! that's because when we
170 get one of the core_signals, we use abort() to dump the core.
171 And having the abort() only go back to our signal handler again
172 is less than optimal */
173 if (core_signals
[i
] != SIGABRT
) {
174 sigaction(core_signals
[i
], &action
,
175 &all_signals
[core_signals
[i
]].oldact
);
176 all_signals
[core_signals
[i
]].installed
++;
181 all_loops
= g_slist_prepend(all_loops
, loop
);
183 loop
->action_queue
= NULL
;
188 void ob_main_loop_destroy(ObMainLoop
*loop
)
194 g_assert(loop
->running
== FALSE
);
196 for (it
= loop
->x_handlers
; it
; it
= next
) {
197 ObMainLoopXHandlerType
*h
= it
->data
;
198 next
= g_slist_next(it
);
199 ob_main_loop_x_remove(loop
, h
->func
);
202 g_hash_table_destroy(loop
->fd_handlers
);
204 for (it
= loop
->timers
; it
; it
= g_slist_next(it
)) {
205 ObMainLoopTimer
*t
= it
->data
;
206 if (t
->destroy
) t
->destroy(t
->data
);
209 g_slist_free(loop
->timers
);
212 for (i
= 0; i
< NUM_SIGNALS
; ++i
)
213 for (it
= loop
->signal_handlers
[i
]; it
; it
= next
) {
214 ObMainLoopSignalHandlerType
*h
= it
->data
;
215 next
= g_slist_next(it
);
216 ob_main_loop_signal_remove(loop
, h
->func
);
219 all_loops
= g_slist_remove(all_loops
, loop
);
221 /* only do this if we're the last loop destroyed */
225 /* grab all the signals that cause core dumps */
226 for (i
= 0; i
< NUM_CORE_SIGNALS
; ++i
) {
227 if (all_signals
[core_signals
[i
]].installed
) {
228 sigaction(core_signals
[i
],
229 &all_signals
[core_signals
[i
]].oldact
, NULL
);
230 all_signals
[core_signals
[i
]].installed
--;
235 for (it
= loop
->action_queue
; it
; it
= g_slist_next(it
))
236 action_unref(it
->data
);
237 g_slist_free(loop
->action_queue
);
243 static void fd_handle_foreach(gpointer key
,
247 ObMainLoopFdHandlerType
*h
= value
;
250 if (FD_ISSET(h
->fd
, set
))
251 h
->func(h
->fd
, h
->data
);
254 void ob_main_loop_queue_action(ObMainLoop
*loop
, ObAction
*act
)
256 loop
->action_queue
= g_slist_append(loop
->action_queue
, action_copy(act
));
259 static void ob_main_loop_client_destroy(ObClient
*client
, gpointer data
)
261 ObMainLoop
*loop
= data
;
264 for (it
= loop
->action_queue
; it
; it
= g_slist_next(it
)) {
265 ObAction
*act
= it
->data
;
267 if (act
->data
.any
.c
== client
)
268 act
->data
.any
.c
= NULL
;
272 void ob_main_loop_run(ObMainLoop
*loop
)
275 struct timeval
*wait
;
281 loop
->running
= TRUE
;
283 client_add_destructor(ob_main_loop_client_destroy
, loop
);
286 if (loop
->signal_fired
) {
290 /* block signals so that we can do this without the data changing
292 sigprocmask(SIG_SETMASK
, &all_signals_set
, &oldset
);
294 for (i
= 0; i
< NUM_SIGNALS
; ++i
) {
295 while (loop
->signals_fired
[i
]) {
296 for (it
= loop
->signal_handlers
[i
];
297 it
; it
= g_slist_next(it
)) {
298 ObMainLoopSignalHandlerType
*h
= it
->data
;
301 loop
->signals_fired
[i
]--;
304 loop
->signal_fired
= FALSE
;
306 sigprocmask(SIG_SETMASK
, &oldset
, NULL
);
307 } else if (XPending(loop
->display
)) {
309 XNextEvent(loop
->display
, &e
);
311 for (it
= loop
->x_handlers
; it
; it
= g_slist_next(it
)) {
312 ObMainLoopXHandlerType
*h
= it
->data
;
313 h
->func(&e
, h
->data
);
315 } while (XPending(loop
->display
));
316 } else if (loop
->action_queue
) {
317 /* only fire off one action at a time, then go back for more
318 X events, since the action might cause some X events (like
322 act
= loop
->action_queue
->data
;
323 if (act
->data
.any
.client_action
== OB_CLIENT_ACTION_ALWAYS
&&
327 g_slist_delete_link(loop
->action_queue
,
332 } while (!act
&& loop
->action_queue
);
335 act
->func(&act
->data
);
337 g_slist_delete_link(loop
->action_queue
,
342 /* this only runs if there were no x events received */
344 for (it
= loop
->x_handlers
; it
; it
= g_slist_next(it
)) {
345 ObMainLoopXHandlerType
*h
= it
->data
;
347 h
->done_func(h
->data
);
350 timer_dispatch(loop
, (GTimeVal
**)&wait
);
352 selset
= loop
->fd_set
;
353 /* there is a small race condition here. if a signal occurs
354 between this if() and the select() then we will not process
355 the signal until 'wait' expires. possible solutions include
356 using GStaticMutex, and having the signal handler set 'wait'
358 if (!loop
->signal_fired
)
359 select(loop
->fd_max
+ 1, &selset
, NULL
, NULL
, wait
);
361 /* handle the X events with highest prioirity */
362 if (FD_ISSET(loop
->fd_x
, &selset
))
365 g_hash_table_foreach(loop
->fd_handlers
,
366 fd_handle_foreach
, &selset
);
370 client_remove_destructor(ob_main_loop_client_destroy
);
372 loop
->running
= FALSE
;
375 void ob_main_loop_exit(ObMainLoop
*loop
)
380 /*** XEVENT WATCHERS ***/
382 void ob_main_loop_x_add(ObMainLoop
*loop
,
383 ObMainLoopXHandler handler
,
384 ObMainLoopXDoneHandler done_handler
,
386 GDestroyNotify notify
)
388 ObMainLoopXHandlerType
*h
;
390 h
= g_new(ObMainLoopXHandlerType
, 1);
393 h
->done_func
= done_handler
;
396 loop
->x_handlers
= g_slist_prepend(loop
->x_handlers
, h
);
399 void ob_main_loop_x_remove(ObMainLoop
*loop
,
400 ObMainLoopXHandler handler
)
404 for (it
= loop
->x_handlers
; it
; it
= next
) {
405 ObMainLoopXHandlerType
*h
= it
->data
;
406 next
= g_slist_next(it
);
407 if (h
->func
== handler
) {
408 loop
->x_handlers
= g_slist_delete_link(loop
->x_handlers
, it
);
409 if (h
->destroy
) h
->destroy(h
->data
);
415 /*** SIGNAL WATCHERS ***/
417 static void sighandler(gint sig
)
422 g_return_if_fail(sig
< NUM_SIGNALS
);
424 for (i
= 0; i
< NUM_CORE_SIGNALS
; ++i
)
425 if (sig
== core_signals
[i
]) {
426 /* XXX special case for signals that default to core dump.
427 but throw some helpful output here... */
429 fprintf(stderr
, "Fuck yah. Core dump. (Signal=%d)\n", sig
);
431 /* die with a core dump */
435 for (it
= all_loops
; it
; it
= g_slist_next(it
)) {
436 ObMainLoop
*loop
= it
->data
;
437 loop
->signal_fired
= TRUE
;
438 loop
->signals_fired
[sig
]++;
442 void ob_main_loop_signal_add(ObMainLoop
*loop
,
444 ObMainLoopSignalHandler handler
,
446 GDestroyNotify notify
)
448 ObMainLoopSignalHandlerType
*h
;
450 g_return_if_fail(signal
< NUM_SIGNALS
);
452 h
= g_new(ObMainLoopSignalHandlerType
, 1);
458 loop
->signal_handlers
[h
->signal
] =
459 g_slist_prepend(loop
->signal_handlers
[h
->signal
], h
);
461 if (!all_signals
[signal
].installed
) {
462 struct sigaction action
;
465 sigemptyset(&sigset
);
466 action
.sa_handler
= sighandler
;
467 action
.sa_mask
= sigset
;
468 action
.sa_flags
= SA_NOCLDSTOP
;
470 sigaction(signal
, &action
, &all_signals
[signal
].oldact
);
473 all_signals
[signal
].installed
++;
476 void ob_main_loop_signal_remove(ObMainLoop
*loop
,
477 ObMainLoopSignalHandler handler
)
482 for (i
= 0; i
< NUM_SIGNALS
; ++i
) {
483 for (it
= loop
->signal_handlers
[i
]; it
; it
= next
) {
484 ObMainLoopSignalHandlerType
*h
= it
->data
;
486 next
= g_slist_next(it
);
488 if (h
->func
== handler
) {
489 g_assert(all_signals
[h
->signal
].installed
> 0);
491 all_signals
[h
->signal
].installed
--;
492 if (!all_signals
[h
->signal
].installed
) {
493 sigaction(h
->signal
, &all_signals
[h
->signal
].oldact
, NULL
);
496 loop
->signal_handlers
[i
] =
497 g_slist_delete_link(loop
->signal_handlers
[i
], it
);
498 if (h
->destroy
) h
->destroy(h
->data
);
507 /*** FILE DESCRIPTOR WATCHERS ***/
509 static void max_fd_func(gpointer key
, gpointer value
, gpointer data
)
511 ObMainLoop
*loop
= data
;
514 loop
->fd_max
= MAX(loop
->fd_max
, *(gint
*)key
);
517 static void calc_max_fd(ObMainLoop
*loop
)
519 loop
->fd_max
= loop
->fd_x
;
521 g_hash_table_foreach(loop
->fd_handlers
, max_fd_func
, loop
);
524 void ob_main_loop_fd_add(ObMainLoop
*loop
,
526 ObMainLoopFdHandler handler
,
528 GDestroyNotify notify
)
530 ObMainLoopFdHandlerType
*h
;
532 h
= g_new(ObMainLoopFdHandlerType
, 1);
539 g_hash_table_replace(loop
->fd_handlers
, &h
->fd
, h
);
540 FD_SET(h
->fd
, &loop
->fd_set
);
544 static void fd_handler_destroy(gpointer data
)
546 ObMainLoopFdHandlerType
*h
= data
;
548 FD_CLR(h
->fd
, &h
->loop
->fd_set
);
554 void ob_main_loop_fd_remove(ObMainLoop
*loop
,
557 g_hash_table_remove(loop
->fd_handlers
, &fd
);
562 #define NEAREST_TIMEOUT(loop) \
563 (((ObMainLoopTimer*)(loop)->timers->data)->timeout)
565 static long timecompare(GTimeVal
*a
, GTimeVal
*b
)
569 if ((r
= b
->tv_sec
- a
->tv_sec
)) return r
;
570 return b
->tv_usec
- a
->tv_usec
;
574 static void insert_timer(ObMainLoop
*loop
, ObMainLoopTimer
*ins
)
577 for (it
= loop
->timers
; it
; it
= g_slist_next(it
)) {
578 ObMainLoopTimer
*t
= it
->data
;
579 if (timecompare(&ins
->timeout
, &t
->timeout
) >= 0) {
580 loop
->timers
= g_slist_insert_before(loop
->timers
, it
, ins
);
584 if (it
== NULL
) /* didnt fit anywhere in the list */
585 loop
->timers
= g_slist_append(loop
->timers
, ins
);
588 void ob_main_loop_timeout_add(ObMainLoop
*loop
,
592 GDestroyNotify notify
)
594 ObMainLoopTimer
*t
= g_new(ObMainLoopTimer
, 1);
595 t
->delay
= microseconds
;
600 g_get_current_time(&loop
->now
);
601 t
->last
= t
->timeout
= loop
->now
;
602 g_time_val_add(&t
->timeout
, t
->delay
);
604 insert_timer(loop
, t
);
607 void ob_main_loop_timeout_remove(ObMainLoop
*loop
,
612 for (it
= loop
->timers
; it
; it
= g_slist_next(it
)) {
613 ObMainLoopTimer
*t
= it
->data
;
614 if (t
->func
== handler
)
619 void ob_main_loop_timeout_remove_data(ObMainLoop
*loop
,
625 for (it
= loop
->timers
; it
; it
= g_slist_next(it
)) {
626 ObMainLoopTimer
*t
= it
->data
;
627 if (t
->func
== handler
&& t
->data
== data
)
632 /* find the time to wait for the nearest timeout */
633 static gboolean
nearest_timeout_wait(ObMainLoop
*loop
, GTimeVal
*tm
)
635 if (loop
->timers
== NULL
)
638 tm
->tv_sec
= NEAREST_TIMEOUT(loop
).tv_sec
- loop
->now
.tv_sec
;
639 tm
->tv_usec
= NEAREST_TIMEOUT(loop
).tv_usec
- loop
->now
.tv_usec
;
641 while (tm
->tv_usec
< 0) {
642 tm
->tv_usec
+= G_USEC_PER_SEC
;
645 tm
->tv_sec
+= tm
->tv_usec
/ G_USEC_PER_SEC
;
646 tm
->tv_usec
%= G_USEC_PER_SEC
;
653 static void timer_dispatch(ObMainLoop
*loop
, GTimeVal
**wait
)
657 gboolean fired
= FALSE
;
659 g_get_current_time(&loop
->now
);
661 for (it
= loop
->timers
; it
; it
= next
) {
662 ObMainLoopTimer
*curr
;
664 next
= g_slist_next(it
);
668 /* since timer_stop doesn't actually free the timer, we have to do our
669 real freeing in here.
673 loop
->timers
= g_slist_delete_link(loop
->timers
, it
);
678 /* the queue is sorted, so if this timer shouldn't fire, none are
680 if (timecompare(&NEAREST_TIMEOUT(loop
), &loop
->now
) < 0)
683 /* we set the last fired time to delay msec after the previous firing,
684 then re-insert. timers maintain their order and may trigger more
685 than once if they've waited more than one delay's worth of time.
687 loop
->timers
= g_slist_delete_link(loop
->timers
, it
);
688 g_time_val_add(&curr
->last
, curr
->delay
);
689 if (curr
->func(curr
->data
)) {
690 g_time_val_add(&curr
->timeout
, curr
->delay
);
691 insert_timer(loop
, curr
);
694 curr
->destroy(curr
->data
);
702 /* if at least one timer fires, then don't wait on X events, as there
703 may already be some in the queue from the timer callbacks.
705 loop
->ret_wait
.tv_sec
= loop
->ret_wait
.tv_usec
= 0;
706 *wait
= &loop
->ret_wait
;
707 } else if (nearest_timeout_wait(loop
, &loop
->ret_wait
))
708 *wait
= &loop
->ret_wait
;