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 */
64 typedef struct _GTimeoutData GTimeoutData;
65 typedef struct _GSource GSource;
66 typedef struct _GPollRec GPollRec;
70 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
71 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
100 /* Forward declarations */
102 static gint g_source_compare (GHook *a,
104 static void g_source_destroy_func (GHookList *hook_list,
106 static void g_main_poll (gint timeout,
107 gboolean use_priority,
109 static void g_main_add_poll_unlocked (gint priority,
112 static gboolean g_timeout_prepare (gpointer source_data,
113 GTimeVal *current_time,
115 static gboolean g_timeout_check (gpointer source_data,
116 GTimeVal *current_time);
117 static gboolean g_timeout_dispatch (gpointer source_data,
118 GTimeVal *current_time,
120 static gboolean g_idle_prepare (gpointer source_data,
121 GTimeVal *current_time,
123 static gboolean g_idle_check (gpointer source_data,
124 GTimeVal *current_time);
125 static gboolean g_idle_dispatch (gpointer source_data,
126 GTimeVal *current_time,
131 static GSList *pending_dispatches = NULL;
132 static GHookList source_list = { 0 };
133 static gint in_check_or_prepare = 0;
135 /* The following lock is used for both the list of sources
136 * and the list of poll records
138 G_LOCK_DEFINE_STATIC (main_loop);
140 static GSourceFuncs timeout_funcs =
148 static GSourceFuncs idle_funcs =
156 static GPollRec *poll_records = NULL;
157 static GPollRec *poll_free_list = NULL;
158 static GMemChunk *poll_chunk;
159 static guint n_poll_records = 0;
161 #ifdef G_THREADS_ENABLED
163 /* this pipe is used to wake up the main loop when a source is added.
165 static gint wake_up_pipe[2] = { -1, -1 };
166 #else /* NATIVE_WIN32 */
167 static HANDLE wake_up_semaphore = NULL;
168 #endif /* NATIVE_WIN32 */
169 static GPollFD wake_up_rec;
170 static gboolean poll_waiting = FALSE;
171 #endif /* G_THREADS_ENABLED */
174 static GPollFunc poll_func = (GPollFunc) poll;
175 #else /* !HAVE_POLL */
179 g_poll (GPollFD *fds, guint nfds, gint timeout)
181 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
190 for (f = fds; f < &fds[nfds]; ++f)
193 if (f->events & G_IO_IN)
194 if (f->fd == G_WIN32_MSG_HANDLE)
198 /* g_print ("g_poll: waiting for handle %#x\n", f->fd); */
199 handles[nhandles++] = (HANDLE) f->fd;
208 /* Waiting for messages, and maybe events */
211 if (timeout == INFINITE)
213 /* Waiting just for messages, infinite timeout
214 * -> Use PeekMessage, then WaitMessage
216 /* g_print ("WaitMessage, PeekMessage\n"); */
217 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
218 ready = WAIT_OBJECT_0;
219 else if (!WaitMessage ())
220 g_warning ("g_poll: WaitMessage failed");
221 ready = WAIT_OBJECT_0;
223 else if (timeout == 0)
225 /* Waiting just for messages, zero timeout
228 /* g_print ("PeekMessage\n"); */
229 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
230 ready = WAIT_OBJECT_0;
232 ready = WAIT_TIMEOUT;
236 /* Waiting just for messages, some timeout
237 * -> First try PeekMessage, then set a timer, wait for message,
238 * kill timer, use PeekMessage
240 /* g_print ("PeekMessage\n"); */
241 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
242 ready = WAIT_OBJECT_0;
243 else if ((timer = SetTimer (NULL, 0, timeout, NULL)) == 0)
244 g_warning ("g_poll: SetTimer failed");
247 /* g_print ("WaitMessage\n"); */
249 KillTimer (NULL, timer);
250 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
251 ready = WAIT_OBJECT_0;
253 ready = WAIT_TIMEOUT;
259 /* Wait for either message or event
260 * -> Use MsgWaitForMultipleObjects
262 /* g_print ("MsgWaitForMultipleObjects(%d, %d)\n", nhandles, timeout); */
263 ready = MsgWaitForMultipleObjects (nhandles, handles, FALSE,
264 timeout, QS_ALLINPUT);
265 /* g_print("=%d\n", ready); */
266 if (ready == WAIT_FAILED)
267 g_warning ("g_poll: MsgWaitForMultipleObjects failed");
270 else if (nhandles == 0)
272 /* Wait for nothing (huh?) */
277 /* Wait for just events
278 * -> Use WaitForMultipleObjects
280 /* g_print ("WaitForMultipleObjects(%d, %d)\n", nhandles, timeout); */
281 ready = WaitForMultipleObjects (nhandles, handles, FALSE, timeout);
282 /* g_print("=%d\n", ready); */
283 if (ready == WAIT_FAILED)
284 g_warning ("g_poll: WaitForMultipleObjects failed");
287 for (f = fds; f < &fds[nfds]; ++f)
290 if (ready == WAIT_FAILED)
292 else if (poll_msgs >= 0 && ready == WAIT_OBJECT_0 + nhandles)
294 fds[poll_msgs].revents |= G_IO_IN;
296 else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
297 for (f = fds; f < &fds[nfds]; ++f)
299 if ((f->events & G_IO_IN)
300 && f->fd == (gint) handles[ready - WAIT_OBJECT_0])
302 f->revents |= G_IO_IN;
303 /* g_print ("event %#x\n", f->fd); */
304 ResetEvent ((HANDLE) f->fd);
308 if (ready == WAIT_TIMEOUT)
314 #else /* !NATIVE_WIN32 */
316 /* The following implementation of poll() comes from the GNU C Library.
317 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
320 #include <string.h> /* for bzero on BSD systems */
322 #ifdef HAVE_SYS_SELECT_H
323 #include <sys/select.h>
324 #endif /* HAVE_SYS_SELECT_H_ */
327 # define SELECT_MASK fd_set
328 #else /* !NO_FD_SET */
330 typedef long fd_mask;
333 # define SELECT_MASK void
335 # define SELECT_MASK int
336 # endif /* !_IBMR2 */
337 #endif /* !NO_FD_SET */
340 g_poll (GPollFD *fds,
345 SELECT_MASK rset, wset, xset;
354 for (f = fds; f < &fds[nfds]; ++f)
357 if (f->events & G_IO_IN)
358 FD_SET (f->fd, &rset);
359 if (f->events & G_IO_OUT)
360 FD_SET (f->fd, &wset);
361 if (f->events & G_IO_PRI)
362 FD_SET (f->fd, &xset);
363 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
367 tv.tv_sec = timeout / 1000;
368 tv.tv_usec = (timeout % 1000) * 1000;
370 ready = select (maxfd + 1, &rset, &wset, &xset,
371 timeout == -1 ? NULL : &tv);
373 for (f = fds; f < &fds[nfds]; ++f)
378 if (FD_ISSET (f->fd, &rset))
379 f->revents |= G_IO_IN;
380 if (FD_ISSET (f->fd, &wset))
381 f->revents |= G_IO_OUT;
382 if (FD_ISSET (f->fd, &xset))
383 f->revents |= G_IO_PRI;
390 #endif /* !NATIVE_WIN32 */
392 static GPollFunc poll_func = g_poll;
393 #endif /* !HAVE_POLL */
395 /* Hooks for adding to the main loop */
397 /* Use knowledge of insert_sorted algorithm here to make
398 * sure we insert at the end of equal priority items
401 g_source_compare (GHook *a,
404 GSource *source_a = (GSource *)a;
405 GSource *source_b = (GSource *)b;
407 return (source_a->priority < source_b->priority) ? -1 : 1;
410 /* HOLDS: main_loop lock */
412 g_source_destroy_func (GHookList *hook_list,
415 GSource *source = (GSource*) hook;
416 GDestroyNotify destroy;
418 G_UNLOCK (main_loop);
420 destroy = hook->destroy;
422 destroy (hook->data);
424 destroy = ((GSourceFuncs*) hook->func)->destroy;
426 destroy (source->source_data);
432 g_source_add (gint priority,
433 gboolean can_recurse,
435 gpointer source_data,
437 GDestroyNotify notify)
444 if (!source_list.is_setup)
446 g_hook_list_init (&source_list, sizeof (GSource));
448 source_list.hook_destroy = G_HOOK_DEFERRED_DESTROY;
449 source_list.hook_free = g_source_destroy_func;
452 source = (GSource*) g_hook_alloc (&source_list);
453 source->priority = priority;
454 source->source_data = source_data;
455 source->hook.func = funcs;
456 source->hook.data = user_data;
457 source->hook.destroy = notify;
459 g_hook_insert_sorted (&source_list,
464 source->hook.flags |= G_SOURCE_CAN_RECURSE;
466 return_val = source->hook.hook_id;
468 #ifdef G_THREADS_ENABLED
469 /* Now wake up the main loop if it is waiting in the poll() */
473 poll_waiting = FALSE;
475 write (wake_up_pipe[1], "A", 1);
477 ReleaseSemaphore (wake_up_semaphore, 1, NULL);
481 G_UNLOCK (main_loop);
487 g_source_remove (guint tag)
491 g_return_val_if_fail (tag > 0, FALSE);
495 hook = g_hook_get (&source_list, tag);
497 g_hook_destroy_link (&source_list, hook);
499 G_UNLOCK (main_loop);
505 g_source_remove_by_user_data (gpointer user_data)
511 hook = g_hook_find_data (&source_list, TRUE, user_data);
513 g_hook_destroy_link (&source_list, hook);
515 G_UNLOCK (main_loop);
521 g_source_find_source_data (GHook *hook,
524 GSource *source = (GSource *)hook;
526 return (source->source_data == data);
530 g_source_remove_by_source_data (gpointer source_data)
536 hook = g_hook_find (&source_list, TRUE,
537 g_source_find_source_data, source_data);
539 g_hook_destroy_link (&source_list, hook);
541 G_UNLOCK (main_loop);
547 g_source_find_funcs_user_data (GHook *hook,
552 return hook->func == d[0] && hook->data == d[1];
556 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
562 g_return_val_if_fail (funcs != NULL, FALSE);
569 hook = g_hook_find (&source_list, TRUE,
570 g_source_find_funcs_user_data, d);
572 g_hook_destroy_link (&source_list, hook);
574 G_UNLOCK (main_loop);
580 g_get_current_time (GTimeVal *result)
584 g_return_if_fail (result != NULL);
586 /*this is required on alpha, there the timeval structs are int's
587 not longs and a cast only would fail horribly*/
588 gettimeofday (&r, NULL);
589 result->tv_sec = r.tv_sec;
590 result->tv_usec = r.tv_usec;
592 /* Avoid calling time() except for the first time.
593 * GetTickCount() should be pretty fast and low-level?
594 * I could also use ftime() but it seems unnecessarily overheady.
596 static DWORD start_tick = 0;
597 static time_t start_time;
601 g_return_if_fail (result != NULL);
605 start_tick = GetTickCount ();
609 tick = GetTickCount ();
611 result->tv_sec = (tick - start_tick) / 1000 + start_time;
612 result->tv_usec = ((tick - start_tick) % 1000) * 1000;
616 /* Running the main loop */
618 /* HOLDS: main_loop_lock */
620 g_main_dispatch (GTimeVal *current_time)
622 while (pending_dispatches != NULL)
624 gboolean need_destroy;
625 GSource *source = pending_dispatches->data;
628 tmp_list = pending_dispatches;
629 pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
630 g_slist_free_1 (tmp_list);
632 if (G_HOOK_IS_VALID (source))
634 gboolean was_in_call;
635 gpointer hook_data = source->hook.data;
636 gpointer source_data = source->source_data;
637 gboolean (*dispatch) (gpointer,
641 dispatch = ((GSourceFuncs *) source->hook.func)->dispatch;
643 was_in_call = G_HOOK_IN_CALL (source);
644 source->hook.flags |= G_HOOK_FLAG_IN_CALL;
646 G_UNLOCK (main_loop);
647 need_destroy = ! dispatch (source_data,
653 source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
655 if (need_destroy && G_HOOK_IS_VALID (source))
656 g_hook_destroy_link (&source_list, (GHook *) source);
659 g_hook_unref (&source_list, (GHook*) source);
663 /* g_main_iterate () runs a single iteration of the mainloop, or,
664 * if !dispatch checks to see if any sources need dispatching.
665 * basic algorithm for dispatch=TRUE:
667 * 1) while the list of currently pending sources is non-empty,
668 * we call (*dispatch) on those that are !IN_CALL or can_recurse,
669 * removing sources from the list after each returns.
670 * the return value of (*dispatch) determines whether the source
671 * itself is kept alive.
673 * 2) call (*prepare) for sources that are not yet SOURCE_READY and
674 * are !IN_CALL or can_recurse. a return value of TRUE determines
675 * that the source would like to be dispatched immediatedly, it
676 * is then flagged as SOURCE_READY.
678 * 3) poll with the pollfds from all sources at the priority of the
679 * first source flagged as SOURCE_READY. if there are any sources
680 * flagged as SOURCE_READY, we use a timeout of 0 or the minimum
681 * of all timouts otherwise.
683 * 4) for each source !IN_CALL or can_recurse, if SOURCE_READY or
684 * (*check) returns true, add the source to the pending list.
685 * once one source returns true, stop after checking all sources
688 * 5) while the list of currently pending sources is non-empty,
689 * call (*dispatch) on each source, removing the source
694 g_main_iterate (gboolean block,
698 GTimeVal current_time ={ 0, 0 };
700 gint current_priority = 0;
702 gboolean retval = FALSE;
704 g_return_val_if_fail (!block || dispatch, FALSE);
706 g_get_current_time (¤t_time);
710 /* If recursing, finish up current dispatch, before starting over */
711 if (pending_dispatches)
714 g_main_dispatch (¤t_time);
716 G_UNLOCK (main_loop);
721 /* Prepare all sources */
723 timeout = block ? -1 : 0;
725 hook = g_hook_first_valid (&source_list, TRUE);
728 GSource *source = (GSource *)hook;
729 gint source_timeout = -1;
731 if ((n_ready > 0) && (source->priority > current_priority))
733 g_hook_unref (&source_list, hook);
736 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
738 hook = g_hook_next_valid (&source_list, hook, TRUE);
742 if (!(hook->flags & G_SOURCE_READY))
744 gboolean (*prepare) (gpointer source_data,
745 GTimeVal *current_time,
748 prepare = ((GSourceFuncs *) hook->func)->prepare;
749 in_check_or_prepare++;
750 G_UNLOCK (main_loop);
752 if ((*prepare) (source->source_data, ¤t_time, &source_timeout))
753 hook->flags |= G_SOURCE_READY;
756 in_check_or_prepare--;
759 if (hook->flags & G_SOURCE_READY)
763 g_hook_unref (&source_list, hook);
764 G_UNLOCK (main_loop);
771 current_priority = source->priority;
776 if (source_timeout >= 0)
779 timeout = source_timeout;
781 timeout = MIN (timeout, source_timeout);
784 hook = g_hook_next_valid (&source_list, hook, TRUE);
787 /* poll(), if necessary */
789 g_main_poll (timeout, n_ready > 0, current_priority);
791 /* Check to see what sources need to be dispatched */
795 hook = g_hook_first_valid (&source_list, TRUE);
798 GSource *source = (GSource *)hook;
800 if ((n_ready > 0) && (source->priority > current_priority))
802 g_hook_unref (&source_list, hook);
805 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
807 hook = g_hook_next_valid (&source_list, hook, TRUE);
811 if (!(hook->flags & G_SOURCE_READY))
813 gboolean (*check) (gpointer source_data,
814 GTimeVal *current_time);
816 check = ((GSourceFuncs *) hook->func)->check;
817 in_check_or_prepare++;
818 G_UNLOCK (main_loop);
820 if ((*check) (source->source_data, ¤t_time))
821 hook->flags |= G_SOURCE_READY;
824 in_check_or_prepare--;
827 if (hook->flags & G_SOURCE_READY)
831 hook->flags &= ~G_SOURCE_READY;
832 g_hook_ref (&source_list, hook);
833 pending_dispatches = g_slist_prepend (pending_dispatches, source);
834 current_priority = source->priority;
839 g_hook_unref (&source_list, hook);
840 G_UNLOCK (main_loop);
846 hook = g_hook_next_valid (&source_list, hook, TRUE);
849 /* Now invoke the callbacks */
851 if (pending_dispatches)
853 pending_dispatches = g_slist_reverse (pending_dispatches);
854 g_main_dispatch (¤t_time);
858 G_UNLOCK (main_loop);
863 /* See if any events are pending
866 g_main_pending (void)
868 return in_check_or_prepare ? FALSE : g_main_iterate (FALSE, FALSE);
871 /* Run a single iteration of the mainloop. If block is FALSE,
875 g_main_iteration (gboolean block)
877 if (in_check_or_prepare)
879 g_warning ("g_main_iteration(): called recursively from within a source's check() or "
880 "prepare() member or from a second thread, iteration not possible");
884 return g_main_iterate (block, TRUE);
888 g_main_new (gboolean is_running)
892 loop = g_new0 (GMainLoop, 1);
893 loop->is_running = is_running != FALSE;
899 g_main_run (GMainLoop *loop)
901 g_return_if_fail (loop != NULL);
903 if (in_check_or_prepare)
905 g_warning ("g_main_run(): called recursively from within a source's check() or "
906 "prepare() member or from a second thread, iteration not possible");
910 loop->is_running = TRUE;
911 while (loop->is_running)
912 g_main_iterate (TRUE, TRUE);
916 g_main_quit (GMainLoop *loop)
918 g_return_if_fail (loop != NULL);
920 loop->is_running = FALSE;
924 g_main_destroy (GMainLoop *loop)
926 g_return_if_fail (loop != NULL);
932 g_main_is_running (GMainLoop *loop)
934 g_return_val_if_fail (loop != NULL, FALSE);
936 return loop->is_running;
939 /* HOLDS: main_loop_lock */
941 g_main_poll (gint timeout,
942 gboolean use_priority,
950 #ifdef G_THREADS_ENABLED
952 if (wake_up_pipe[0] < 0)
954 if (pipe (wake_up_pipe) < 0)
955 g_error ("Cannot create pipe main loop wake-up: %s\n",
958 wake_up_rec.fd = wake_up_pipe[0];
959 wake_up_rec.events = G_IO_IN;
960 g_main_add_poll_unlocked (0, &wake_up_rec);
963 if (wake_up_semaphore == NULL)
965 if ((wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL)) == NULL)
966 g_error ("Cannot create wake-up semaphore: %d", GetLastError ());
967 wake_up_rec.fd = (gint) wake_up_semaphore;
968 wake_up_rec.events = G_IO_IN;
969 g_main_add_poll_unlocked (0, &wake_up_rec);
973 fd_array = g_new (GPollFD, n_poll_records);
975 pollrec = poll_records;
977 while (pollrec && (!use_priority || priority >= pollrec->priority))
979 fd_array[i].fd = pollrec->fd->fd;
980 fd_array[i].events = pollrec->fd->events;
981 fd_array[i].revents = 0;
983 pollrec = pollrec->next;
986 #ifdef G_THREADS_ENABLED
989 G_UNLOCK (main_loop);
991 (*poll_func) (fd_array, npoll, timeout);
994 #ifdef G_THREADS_ENABLED
999 read (wake_up_pipe[0], &c, 1);
1003 poll_waiting = FALSE;
1006 pollrec = poll_records;
1010 pollrec->fd->revents = fd_array[i].revents;
1011 pollrec = pollrec->next;
1019 g_main_add_poll (GPollFD *fd,
1023 g_main_add_poll_unlocked (priority, fd);
1024 G_UNLOCK (main_loop);
1027 /* HOLDS: main_loop_lock */
1029 g_main_add_poll_unlocked (gint priority,
1032 GPollRec *lastrec, *pollrec, *newrec;
1035 poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
1039 newrec = poll_free_list;
1040 poll_free_list = newrec->next;
1043 newrec = g_chunk_new (GPollRec, poll_chunk);
1046 newrec->priority = priority;
1049 pollrec = poll_records;
1050 while (pollrec && priority >= pollrec->priority)
1053 pollrec = pollrec->next;
1057 lastrec->next = newrec;
1059 poll_records = newrec;
1061 newrec->next = pollrec;
1067 g_main_remove_poll (GPollFD *fd)
1069 GPollRec *pollrec, *lastrec;
1074 pollrec = poll_records;
1078 if (pollrec->fd == fd)
1080 if (lastrec != NULL)
1081 lastrec->next = pollrec->next;
1083 poll_records = pollrec->next;
1085 pollrec->next = poll_free_list;
1086 poll_free_list = pollrec;
1092 pollrec = pollrec->next;
1095 G_UNLOCK (main_loop);
1099 g_main_set_poll_func (GPollFunc func)
1105 poll_func = (GPollFunc) poll;
1107 poll_func = (GPollFunc) g_poll;
1114 g_timeout_prepare (gpointer source_data,
1115 GTimeVal *current_time,
1119 GTimeoutData *data = source_data;
1121 msec = (data->expiration.tv_sec - current_time->tv_sec) * 1000 +
1122 (data->expiration.tv_usec - current_time->tv_usec) / 1000;
1124 *timeout = (msec <= 0) ? 0 : msec;
1130 g_timeout_check (gpointer source_data,
1131 GTimeVal *current_time)
1133 GTimeoutData *data = source_data;
1135 return (data->expiration.tv_sec < current_time->tv_sec) ||
1136 ((data->expiration.tv_sec == current_time->tv_sec) &&
1137 (data->expiration.tv_usec <= current_time->tv_usec));
1141 g_timeout_dispatch (gpointer source_data,
1142 GTimeVal *current_time,
1145 GTimeoutData *data = source_data;
1147 if (data->callback (user_data))
1149 guint seconds = data->interval / 1000;
1150 guint msecs = data->interval - seconds * 1000;
1152 data->expiration.tv_sec = current_time->tv_sec + seconds;
1153 data->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
1154 if (data->expiration.tv_usec >= 1000000)
1156 data->expiration.tv_usec -= 1000000;
1157 data->expiration.tv_sec++;
1166 g_timeout_add_full (gint priority,
1168 GSourceFunc function,
1170 GDestroyNotify notify)
1174 GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
1176 timeout_data->interval = interval;
1177 timeout_data->callback = function;
1178 g_get_current_time (&timeout_data->expiration);
1180 seconds = timeout_data->interval / 1000;
1181 msecs = timeout_data->interval - seconds * 1000;
1183 timeout_data->expiration.tv_sec += seconds;
1184 timeout_data->expiration.tv_usec += msecs * 1000;
1185 if (timeout_data->expiration.tv_usec >= 1000000)
1187 timeout_data->expiration.tv_usec -= 1000000;
1188 timeout_data->expiration.tv_sec++;
1191 return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
1195 g_timeout_add (guint32 interval,
1196 GSourceFunc function,
1199 return g_timeout_add_full (G_PRIORITY_DEFAULT,
1200 interval, function, data, NULL);
1203 /* Idle functions */
1206 g_idle_prepare (gpointer source_data,
1207 GTimeVal *current_time,
1215 g_idle_check (gpointer source_data,
1216 GTimeVal *current_time)
1222 g_idle_dispatch (gpointer source_data,
1223 GTimeVal *current_time,
1226 GSourceFunc func = source_data;
1228 return func (user_data);
1232 g_idle_add_full (gint priority,
1233 GSourceFunc function,
1235 GDestroyNotify notify)
1237 g_return_val_if_fail (function != NULL, 0);
1239 return g_source_add (priority, FALSE, &idle_funcs, function, data, notify);
1243 g_idle_add (GSourceFunc function,
1246 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
1250 g_idle_remove_by_data (gpointer data)
1252 return g_source_remove_by_funcs_user_data (&idle_funcs, data);