1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gmain.c: Main loop abstraction, timeouts, and idle functions
5 * Copyright 1998 Owen Taylor
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GLib Team and others 1997-1999. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
37 #include <sys/types.h>
39 #ifdef HAVE_SYS_TIME_H
41 #endif /* HAVE_SYS_TIME_H */
42 #ifdef GLIB_HAVE_SYS_POLL_H
43 # include <sys/poll.h>
44 # undef events /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
45 # undef revents /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
46 #endif /* GLIB_HAVE_SYS_POLL_H */
49 #endif /* HAVE_UNISTD_H */
55 #endif /* NATIVE_WIN32 */
59 typedef struct _GTimeoutData GTimeoutData;
60 typedef struct _GSource GSource;
61 typedef struct _GPollRec GPollRec;
65 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
66 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
95 /* Forward declarations */
97 static gint g_source_compare (GHook *a,
99 static void g_source_destroy_func (GHookList *hook_list,
101 static void g_main_poll (gint timeout,
102 gboolean use_priority,
104 static void g_main_add_poll_unlocked (gint priority,
107 static gboolean g_timeout_prepare (gpointer source_data,
108 GTimeVal *current_time,
110 static gboolean g_timeout_check (gpointer source_data,
111 GTimeVal *current_time);
112 static gboolean g_timeout_dispatch (gpointer source_data,
113 GTimeVal *current_time,
115 static gboolean g_idle_prepare (gpointer source_data,
116 GTimeVal *current_time,
118 static gboolean g_idle_check (gpointer source_data,
119 GTimeVal *current_time);
120 static gboolean g_idle_dispatch (gpointer source_data,
121 GTimeVal *current_time,
126 static GSList *pending_dispatches = NULL;
127 static GHookList source_list = { 0 };
128 static gint in_check_or_prepare = 0;
130 /* The following lock is used for both the list of sources
131 * and the list of poll records
133 G_LOCK_DEFINE_STATIC (main_loop);
135 static GSourceFuncs timeout_funcs =
143 static GSourceFuncs idle_funcs =
151 static GPollRec *poll_records = NULL;
152 static GPollRec *poll_free_list = NULL;
153 static GMemChunk *poll_chunk;
154 static guint n_poll_records = 0;
156 #ifdef G_THREADS_ENABLED
158 /* this pipe is used to wake up the main loop when a source is added.
160 static gint wake_up_pipe[2] = { -1, -1 };
161 #else /* NATIVE_WIN32 */
162 static HANDLE wake_up_semaphore = NULL;
163 #endif /* NATIVE_WIN32 */
164 static GPollFD wake_up_rec;
165 static gboolean poll_waiting = FALSE;
166 #endif /* G_THREADS_ENABLED */
169 static GPollFunc poll_func = (GPollFunc) poll;
170 #else /* !HAVE_POLL */
174 g_poll (GPollFD *fds, guint nfds, gint timeout)
176 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
185 for (f = fds; f < &fds[nfds]; ++f)
188 if (f->events & G_IO_IN)
189 if (f->fd == G_WIN32_MSG_HANDLE)
193 /* g_print ("g_poll: waiting for handle %#x\n", f->fd); */
194 handles[nhandles++] = (HANDLE) f->fd;
203 /* Waiting for messages, and maybe events */
206 if (timeout == INFINITE)
208 /* Waiting just for messages, infinite timeout
209 * -> Use PeekMessage, then WaitMessage
211 /* g_print ("WaitMessage, PeekMessage\n"); */
212 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
213 ready = WAIT_OBJECT_0;
214 else if (!WaitMessage ())
215 g_warning ("g_poll: WaitMessage failed");
216 ready = WAIT_OBJECT_0;
218 else if (timeout == 0)
220 /* Waiting just for messages, zero timeout
223 /* g_print ("PeekMessage\n"); */
224 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
225 ready = WAIT_OBJECT_0;
227 ready = WAIT_TIMEOUT;
231 /* Waiting just for messages, some timeout
232 * -> First try PeekMessage, then set a timer, wait for message,
233 * kill timer, use PeekMessage
235 /* g_print ("PeekMessage\n"); */
236 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
237 ready = WAIT_OBJECT_0;
238 else if ((timer = SetTimer (NULL, 0, timeout, NULL)) == 0)
239 g_warning ("g_poll: SetTimer failed");
242 /* g_print ("WaitMessage\n"); */
244 KillTimer (NULL, timer);
245 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
246 ready = WAIT_OBJECT_0;
248 ready = WAIT_TIMEOUT;
254 /* Wait for either message or event
255 * -> Use MsgWaitForMultipleObjects
257 /* g_print ("MsgWaitForMultipleObjects(%d, %d)\n", nhandles, timeout); */
258 ready = MsgWaitForMultipleObjects (nhandles, handles, FALSE,
259 timeout, QS_ALLINPUT);
260 /* g_print("=%d\n", ready); */
261 if (ready == WAIT_FAILED)
262 g_warning ("g_poll: MsgWaitForMultipleObjects failed");
265 else if (nhandles == 0)
267 /* Wait for nothing (huh?) */
272 /* Wait for just events
273 * -> Use WaitForMultipleObjects
275 /* g_print ("WaitForMultipleObjects(%d, %d)\n", nhandles, timeout); */
276 ready = WaitForMultipleObjects (nhandles, handles, FALSE, timeout);
277 /* g_print("=%d\n", ready); */
278 if (ready == WAIT_FAILED)
279 g_warning ("g_poll: WaitForMultipleObjects failed");
282 for (f = fds; f < &fds[nfds]; ++f)
285 if (ready == WAIT_FAILED)
287 else if (poll_msgs >= 0 && ready == WAIT_OBJECT_0 + nhandles)
289 fds[poll_msgs].revents |= G_IO_IN;
291 else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
292 for (f = fds; f < &fds[nfds]; ++f)
294 if ((f->events & G_IO_IN)
295 && f->fd == (gint) handles[ready - WAIT_OBJECT_0])
297 f->revents |= G_IO_IN;
298 /* g_print ("event %#x\n", f->fd); */
299 ResetEvent ((HANDLE) f->fd);
303 if (ready == WAIT_TIMEOUT)
309 #else /* !NATIVE_WIN32 */
311 /* The following implementation of poll() comes from the GNU C Library.
312 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
315 #include <string.h> /* for bzero on BSD systems */
317 #ifdef HAVE_SYS_SELECT_H
318 #include <sys/select.h>
319 #endif /* HAVE_SYS_SELECT_H */
322 # define SELECT_MASK fd_set
323 #else /* !NO_FD_SET */
325 # define SELECT_MASK void
327 # define SELECT_MASK int
328 # endif /* !_IBMR2 */
329 #endif /* !NO_FD_SET */
332 g_poll (GPollFD *fds,
337 SELECT_MASK rset, wset, xset;
346 for (f = fds; f < &fds[nfds]; ++f)
349 if (f->events & G_IO_IN)
350 FD_SET (f->fd, &rset);
351 if (f->events & G_IO_OUT)
352 FD_SET (f->fd, &wset);
353 if (f->events & G_IO_PRI)
354 FD_SET (f->fd, &xset);
355 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
359 tv.tv_sec = timeout / 1000;
360 tv.tv_usec = (timeout % 1000) * 1000;
362 ready = select (maxfd + 1, &rset, &wset, &xset,
363 timeout == -1 ? NULL : &tv);
365 for (f = fds; f < &fds[nfds]; ++f)
370 if (FD_ISSET (f->fd, &rset))
371 f->revents |= G_IO_IN;
372 if (FD_ISSET (f->fd, &wset))
373 f->revents |= G_IO_OUT;
374 if (FD_ISSET (f->fd, &xset))
375 f->revents |= G_IO_PRI;
382 #endif /* !NATIVE_WIN32 */
384 static GPollFunc poll_func = g_poll;
385 #endif /* !HAVE_POLL */
387 /* Hooks for adding to the main loop */
389 /* Use knowledge of insert_sorted algorithm here to make
390 * sure we insert at the end of equal priority items
393 g_source_compare (GHook *a,
396 GSource *source_a = (GSource *)a;
397 GSource *source_b = (GSource *)b;
399 return (source_a->priority < source_b->priority) ? -1 : 1;
402 /* HOLDS: main_loop lock */
404 g_source_destroy_func (GHookList *hook_list,
407 GSource *source = (GSource*) hook;
408 GDestroyNotify destroy;
410 G_UNLOCK (main_loop);
412 destroy = hook->destroy;
414 destroy (hook->data);
416 destroy = ((GSourceFuncs*) hook->func)->destroy;
418 destroy (source->source_data);
424 g_source_add (gint priority,
425 gboolean can_recurse,
427 gpointer source_data,
429 GDestroyNotify notify)
436 if (!source_list.is_setup)
438 g_hook_list_init (&source_list, sizeof (GSource));
440 source_list.hook_destroy = G_HOOK_DEFERRED_DESTROY;
441 source_list.hook_free = g_source_destroy_func;
444 source = (GSource*) g_hook_alloc (&source_list);
445 source->priority = priority;
446 source->source_data = source_data;
447 source->hook.func = funcs;
448 source->hook.data = user_data;
449 source->hook.destroy = notify;
451 g_hook_insert_sorted (&source_list,
456 source->hook.flags |= G_SOURCE_CAN_RECURSE;
458 return_val = source->hook.hook_id;
460 #ifdef G_THREADS_ENABLED
461 /* Now wake up the main loop if it is waiting in the poll() */
465 poll_waiting = FALSE;
467 write (wake_up_pipe[1], "A", 1);
469 ReleaseSemaphore (wake_up_semaphore, 1, NULL);
473 G_UNLOCK (main_loop);
479 g_source_remove (guint tag)
483 g_return_val_if_fail (tag > 0, FALSE);
487 hook = g_hook_get (&source_list, tag);
489 g_hook_destroy_link (&source_list, hook);
491 G_UNLOCK (main_loop);
497 g_source_remove_by_user_data (gpointer user_data)
503 hook = g_hook_find_data (&source_list, TRUE, user_data);
505 g_hook_destroy_link (&source_list, hook);
507 G_UNLOCK (main_loop);
513 g_source_find_source_data (GHook *hook,
516 GSource *source = (GSource *)hook;
518 return (source->source_data == data);
522 g_source_remove_by_source_data (gpointer source_data)
528 hook = g_hook_find (&source_list, TRUE,
529 g_source_find_source_data, source_data);
531 g_hook_destroy_link (&source_list, hook);
533 G_UNLOCK (main_loop);
539 g_source_find_funcs_user_data (GHook *hook,
544 return hook->func == d[0] && hook->data == d[1];
548 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
554 g_return_val_if_fail (funcs != NULL, FALSE);
561 hook = g_hook_find (&source_list, TRUE,
562 g_source_find_funcs_user_data, d);
564 g_hook_destroy_link (&source_list, hook);
566 G_UNLOCK (main_loop);
572 g_get_current_time (GTimeVal *result)
576 g_return_if_fail (result != NULL);
578 /*this is required on alpha, there the timeval structs are int's
579 not longs and a cast only would fail horribly*/
580 gettimeofday (&r, NULL);
581 result->tv_sec = r.tv_sec;
582 result->tv_usec = r.tv_usec;
584 /* Avoid calling time() except for the first time.
585 * GetTickCount() should be pretty fast and low-level?
586 * I could also use ftime() but it seems unnecessarily overheady.
588 static DWORD start_tick = 0;
589 static time_t start_time;
593 g_return_if_fail (result != NULL);
597 start_tick = GetTickCount ();
601 tick = GetTickCount ();
603 result->tv_sec = (tick - start_tick) / 1000 + start_time;
604 result->tv_usec = ((tick - start_tick) % 1000) * 1000;
608 /* Running the main loop */
610 /* HOLDS: main_loop_lock */
612 g_main_dispatch (GTimeVal *current_time)
614 while (pending_dispatches != NULL)
616 gboolean need_destroy;
617 GSource *source = pending_dispatches->data;
620 tmp_list = pending_dispatches;
621 pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
622 g_slist_free_1 (tmp_list);
624 if (G_HOOK_IS_VALID (source))
626 gboolean was_in_call;
627 gpointer hook_data = source->hook.data;
628 gpointer source_data = source->source_data;
629 gboolean (*dispatch) (gpointer,
633 dispatch = ((GSourceFuncs *) source->hook.func)->dispatch;
635 was_in_call = G_HOOK_IN_CALL (source);
636 source->hook.flags |= G_HOOK_FLAG_IN_CALL;
638 G_UNLOCK (main_loop);
639 need_destroy = ! dispatch (source_data,
645 source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
647 if (need_destroy && G_HOOK_IS_VALID (source))
648 g_hook_destroy_link (&source_list, (GHook *) source);
651 g_hook_unref (&source_list, (GHook*) source);
655 /* g_main_iterate () runs a single iteration of the mainloop, or,
656 * if !dispatch checks to see if any sources need dispatching.
657 * basic algorithm for dispatch=TRUE:
659 * 1) while the list of currently pending sources is non-empty,
660 * we call (*dispatch) on those that are !IN_CALL or can_recurse,
661 * removing sources from the list after each returns.
662 * the return value of (*dispatch) determines whether the source
663 * itself is kept alive.
665 * 2) call (*prepare) for sources that are not yet SOURCE_READY and
666 * are !IN_CALL or can_recurse. a return value of TRUE determines
667 * that the source would like to be dispatched immediatedly, it
668 * is then flagged as SOURCE_READY.
670 * 3) poll with the pollfds from all sources at the priority of the
671 * first source flagged as SOURCE_READY. if there are any sources
672 * flagged as SOURCE_READY, we use a timeout of 0 or the minimum
673 * of all timouts otherwise.
675 * 4) for each source !IN_CALL or can_recurse, if SOURCE_READY or
676 * (*check) returns true, add the source to the pending list.
677 * once one source returns true, stop after checking all sources
680 * 5) while the list of currently pending sources is non-empty,
681 * call (*dispatch) on each source, removing the source
686 g_main_iterate (gboolean block,
690 GTimeVal current_time ={ 0, 0 };
692 gint current_priority = 0;
694 gboolean retval = FALSE;
696 g_return_val_if_fail (!block || dispatch, FALSE);
698 g_get_current_time (¤t_time);
702 /* If recursing, finish up current dispatch, before starting over */
703 if (pending_dispatches)
706 g_main_dispatch (¤t_time);
708 G_UNLOCK (main_loop);
713 /* Prepare all sources */
715 timeout = block ? -1 : 0;
717 hook = g_hook_first_valid (&source_list, TRUE);
720 GSource *source = (GSource *)hook;
721 gint source_timeout = -1;
723 if ((n_ready > 0) && (source->priority > current_priority))
725 g_hook_unref (&source_list, hook);
728 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
730 hook = g_hook_next_valid (&source_list, hook, TRUE);
734 if (!(hook->flags & G_SOURCE_READY))
736 gboolean (*prepare) (gpointer source_data,
737 GTimeVal *current_time,
740 prepare = ((GSourceFuncs *) hook->func)->prepare;
741 in_check_or_prepare++;
742 G_UNLOCK (main_loop);
744 if ((*prepare) (source->source_data, ¤t_time, &source_timeout))
745 hook->flags |= G_SOURCE_READY;
748 in_check_or_prepare--;
751 if (hook->flags & G_SOURCE_READY)
755 g_hook_unref (&source_list, hook);
756 G_UNLOCK (main_loop);
763 current_priority = source->priority;
768 if (source_timeout >= 0)
771 timeout = source_timeout;
773 timeout = MIN (timeout, source_timeout);
776 hook = g_hook_next_valid (&source_list, hook, TRUE);
779 /* poll(), if necessary */
781 g_main_poll (timeout, n_ready > 0, current_priority);
783 /* Check to see what sources need to be dispatched */
787 hook = g_hook_first_valid (&source_list, TRUE);
790 GSource *source = (GSource *)hook;
792 if ((n_ready > 0) && (source->priority > current_priority))
794 g_hook_unref (&source_list, hook);
797 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
799 hook = g_hook_next_valid (&source_list, hook, TRUE);
803 if (!(hook->flags & G_SOURCE_READY))
805 gboolean (*check) (gpointer source_data,
806 GTimeVal *current_time);
808 check = ((GSourceFuncs *) hook->func)->check;
809 in_check_or_prepare++;
810 G_UNLOCK (main_loop);
812 if ((*check) (source->source_data, ¤t_time))
813 hook->flags |= G_SOURCE_READY;
816 in_check_or_prepare--;
819 if (hook->flags & G_SOURCE_READY)
823 hook->flags &= ~G_SOURCE_READY;
824 g_hook_ref (&source_list, hook);
825 pending_dispatches = g_slist_prepend (pending_dispatches, source);
826 current_priority = source->priority;
831 g_hook_unref (&source_list, hook);
832 G_UNLOCK (main_loop);
838 hook = g_hook_next_valid (&source_list, hook, TRUE);
841 /* Now invoke the callbacks */
843 if (pending_dispatches)
845 pending_dispatches = g_slist_reverse (pending_dispatches);
846 g_main_dispatch (¤t_time);
850 G_UNLOCK (main_loop);
855 /* See if any events are pending
858 g_main_pending (void)
860 return in_check_or_prepare ? FALSE : g_main_iterate (FALSE, FALSE);
863 /* Run a single iteration of the mainloop. If block is FALSE,
867 g_main_iteration (gboolean block)
869 if (in_check_or_prepare)
871 g_warning ("g_main_iteration(): called recursively from within a source's check() or "
872 "prepare() member or from a second thread, iteration not possible");
876 return g_main_iterate (block, TRUE);
880 g_main_new (gboolean is_running)
884 loop = g_new0 (GMainLoop, 1);
885 loop->is_running = is_running != FALSE;
891 g_main_run (GMainLoop *loop)
893 g_return_if_fail (loop != NULL);
895 if (in_check_or_prepare)
897 g_warning ("g_main_run(): called recursively from within a source's check() or "
898 "prepare() member or from a second thread, iteration not possible");
902 loop->is_running = TRUE;
903 while (loop->is_running)
904 g_main_iterate (TRUE, TRUE);
908 g_main_quit (GMainLoop *loop)
910 g_return_if_fail (loop != NULL);
912 loop->is_running = FALSE;
916 g_main_destroy (GMainLoop *loop)
918 g_return_if_fail (loop != NULL);
924 g_main_is_running (GMainLoop *loop)
926 g_return_val_if_fail (loop != NULL, FALSE);
928 return loop->is_running;
931 /* HOLDS: main_loop_lock */
933 g_main_poll (gint timeout,
934 gboolean use_priority,
942 #ifdef G_THREADS_ENABLED
944 if (wake_up_pipe[0] < 0)
946 if (pipe (wake_up_pipe) < 0)
947 g_error ("Cannot create pipe main loop wake-up: %s\n",
950 wake_up_rec.fd = wake_up_pipe[0];
951 wake_up_rec.events = G_IO_IN;
952 g_main_add_poll_unlocked (0, &wake_up_rec);
955 if (wake_up_semaphore == NULL)
957 if ((wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL)) == NULL)
958 g_error ("Cannot create wake-up semaphore: %d", GetLastError ());
959 wake_up_rec.fd = (gint) wake_up_semaphore;
960 wake_up_rec.events = G_IO_IN;
961 g_main_add_poll_unlocked (0, &wake_up_rec);
965 fd_array = g_new (GPollFD, n_poll_records);
967 pollrec = poll_records;
969 while (pollrec && (!use_priority || priority >= pollrec->priority))
971 fd_array[i].fd = pollrec->fd->fd;
972 fd_array[i].events = pollrec->fd->events;
973 fd_array[i].revents = 0;
975 pollrec = pollrec->next;
978 #ifdef G_THREADS_ENABLED
981 G_UNLOCK (main_loop);
983 (*poll_func) (fd_array, npoll, timeout);
986 #ifdef G_THREADS_ENABLED
991 read (wake_up_pipe[0], &c, 1);
995 poll_waiting = FALSE;
998 pollrec = poll_records;
1002 pollrec->fd->revents = fd_array[i].revents;
1003 pollrec = pollrec->next;
1011 g_main_add_poll (GPollFD *fd,
1015 g_main_add_poll_unlocked (priority, fd);
1016 G_UNLOCK (main_loop);
1019 /* HOLDS: main_loop_lock */
1021 g_main_add_poll_unlocked (gint priority,
1024 GPollRec *lastrec, *pollrec, *newrec;
1027 poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
1031 newrec = poll_free_list;
1032 poll_free_list = newrec->next;
1035 newrec = g_chunk_new (GPollRec, poll_chunk);
1038 newrec->priority = priority;
1041 pollrec = poll_records;
1042 while (pollrec && priority >= pollrec->priority)
1045 pollrec = pollrec->next;
1049 lastrec->next = newrec;
1051 poll_records = newrec;
1053 newrec->next = pollrec;
1059 g_main_remove_poll (GPollFD *fd)
1061 GPollRec *pollrec, *lastrec;
1066 pollrec = poll_records;
1070 if (pollrec->fd == fd)
1072 if (lastrec != NULL)
1073 lastrec->next = pollrec->next;
1075 poll_records = pollrec->next;
1077 pollrec->next = poll_free_list;
1078 poll_free_list = pollrec;
1084 pollrec = pollrec->next;
1087 G_UNLOCK (main_loop);
1091 g_main_set_poll_func (GPollFunc func)
1097 poll_func = (GPollFunc) poll;
1099 poll_func = (GPollFunc) g_poll;
1106 g_timeout_prepare (gpointer source_data,
1107 GTimeVal *current_time,
1111 GTimeoutData *data = source_data;
1113 msec = (data->expiration.tv_sec - current_time->tv_sec) * 1000 +
1114 (data->expiration.tv_usec - current_time->tv_usec) / 1000;
1116 *timeout = (msec <= 0) ? 0 : msec;
1122 g_timeout_check (gpointer source_data,
1123 GTimeVal *current_time)
1125 GTimeoutData *data = source_data;
1127 return (data->expiration.tv_sec < current_time->tv_sec) ||
1128 ((data->expiration.tv_sec == current_time->tv_sec) &&
1129 (data->expiration.tv_usec <= current_time->tv_usec));
1133 g_timeout_dispatch (gpointer source_data,
1134 GTimeVal *current_time,
1137 GTimeoutData *data = source_data;
1139 if (data->callback (user_data))
1141 guint seconds = data->interval / 1000;
1142 guint msecs = data->interval - seconds * 1000;
1144 data->expiration.tv_sec = current_time->tv_sec + seconds;
1145 data->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
1146 if (data->expiration.tv_usec >= 1000000)
1148 data->expiration.tv_usec -= 1000000;
1149 data->expiration.tv_sec++;
1158 g_timeout_add_full (gint priority,
1160 GSourceFunc function,
1162 GDestroyNotify notify)
1166 GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
1168 timeout_data->interval = interval;
1169 timeout_data->callback = function;
1170 g_get_current_time (&timeout_data->expiration);
1172 seconds = timeout_data->interval / 1000;
1173 msecs = timeout_data->interval - seconds * 1000;
1175 timeout_data->expiration.tv_sec += seconds;
1176 timeout_data->expiration.tv_usec += msecs * 1000;
1177 if (timeout_data->expiration.tv_usec >= 1000000)
1179 timeout_data->expiration.tv_usec -= 1000000;
1180 timeout_data->expiration.tv_sec++;
1183 return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
1187 g_timeout_add (guint32 interval,
1188 GSourceFunc function,
1191 return g_timeout_add_full (G_PRIORITY_DEFAULT,
1192 interval, function, data, NULL);
1195 /* Idle functions */
1198 g_idle_prepare (gpointer source_data,
1199 GTimeVal *current_time,
1207 g_idle_check (gpointer source_data,
1208 GTimeVal *current_time)
1214 g_idle_dispatch (gpointer source_data,
1215 GTimeVal *current_time,
1218 GSourceFunc func = source_data;
1220 return func (user_data);
1224 g_idle_add_full (gint priority,
1225 GSourceFunc function,
1227 GDestroyNotify notify)
1229 g_return_val_if_fail (function != NULL, 0);
1231 return g_source_add (priority, FALSE, &idle_funcs, function, data, notify);
1235 g_idle_add (GSourceFunc function,
1238 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
1242 g_idle_remove_by_data (gpointer data)
1244 return g_source_remove_by_funcs_user_data (&idle_funcs, data);