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.
30 #include <sys/types.h>
32 #ifdef HAVE_SYS_TIME_H
34 #endif /* HAVE_SYS_TIME_H */
35 #ifdef GLIB_HAVE_SYS_POLL_H
36 # include <sys/poll.h>
37 # undef events /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
38 # undef revents /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
39 #endif /* GLIB_HAVE_SYS_POLL_H */
42 #endif /* HAVE_UNISTD_H */
48 #endif /* NATIVE_WIN32 */
57 typedef struct _GTimeoutData GTimeoutData;
58 typedef struct _GSource GSource;
59 typedef struct _GPollRec GPollRec;
63 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
64 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
93 /* Forward declarations */
95 static gint g_source_compare (GHook *a,
97 static void g_source_destroy_func (GHookList *hook_list,
99 static void g_main_poll (gint timeout,
100 gboolean use_priority,
102 static void g_main_add_poll_unlocked (gint priority,
105 static gboolean g_timeout_prepare (gpointer source_data,
106 GTimeVal *current_time,
108 static gboolean g_timeout_check (gpointer source_data,
109 GTimeVal *current_time);
110 static gboolean g_timeout_dispatch (gpointer source_data,
111 GTimeVal *current_time,
113 static gboolean g_idle_prepare (gpointer source_data,
114 GTimeVal *current_time,
116 static gboolean g_idle_check (gpointer source_data,
117 GTimeVal *current_time);
118 static gboolean g_idle_dispatch (gpointer source_data,
119 GTimeVal *current_time,
124 static GSList *pending_dispatches = NULL;
125 static GHookList source_list = { 0 };
126 static gint in_check_or_prepare = 0;
128 /* The following lock is used for both the list of sources
129 * and the list of poll records
131 G_LOCK_DECLARE_STATIC (main_loop);
133 static GSourceFuncs timeout_funcs =
141 static GSourceFuncs idle_funcs =
149 static GPollRec *poll_records = NULL;
150 static GPollRec *poll_free_list = NULL;
151 static GMemChunk *poll_chunk;
152 static guint n_poll_records = 0;
154 #ifdef G_THREADS_ENABLED
156 /* this pipe is used to wake up the main loop when a source is added.
158 static gint wake_up_pipe[2] = { -1, -1 };
159 #else /* NATIVE_WIN32 */
160 static HANDLE wake_up_semaphore = NULL;
161 #endif /* NATIVE_WIN32 */
162 static GPollFD wake_up_rec;
163 static gboolean poll_waiting = FALSE;
164 #endif /* G_THREADS_ENABLED */
167 static GPollFunc poll_func = (GPollFunc) poll;
168 #else /* !HAVE_POLL */
172 g_poll (GPollFD *fds, guint nfds, gint timeout)
174 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
183 for (f = fds; f < &fds[nfds]; ++f)
186 if (f->events & G_IO_IN)
187 if (f->fd == G_WIN32_MSG_HANDLE)
191 /* g_print ("g_poll: waiting for handle %#x\n", f->fd); */
192 handles[nhandles++] = (HANDLE) f->fd;
201 /* Waiting for messages, and maybe events */
204 if (timeout == INFINITE)
206 /* Waiting just for messages, infinite timeout
207 * -> Use PeekMessage, then WaitMessage
209 /* g_print ("WaitMessage, PeekMessage\n"); */
210 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
211 ready = WAIT_OBJECT_0;
212 else if (!WaitMessage ())
213 g_warning ("g_poll: WaitMessage failed");
214 ready = WAIT_OBJECT_0;
216 else if (timeout == 0)
218 /* Waiting just for messages, zero timeout
221 /* g_print ("PeekMessage\n"); */
222 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
223 ready = WAIT_OBJECT_0;
225 ready = WAIT_TIMEOUT;
229 /* Waiting just for messages, some timeout
230 * -> First try PeekMessage, then set a timer, wait for message,
231 * kill timer, use PeekMessage
233 /* g_print ("PeekMessage\n"); */
234 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
235 ready = WAIT_OBJECT_0;
236 else if ((timer = SetTimer (NULL, 0, timeout, NULL)) == 0)
237 g_warning ("g_poll: SetTimer failed");
240 /* g_print ("WaitMessage\n"); */
242 KillTimer (NULL, timer);
243 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
244 ready = WAIT_OBJECT_0;
246 ready = WAIT_TIMEOUT;
252 /* Wait for either message or event
253 * -> Use MsgWaitForMultipleObjects
255 /* g_print ("MsgWaitForMultipleObjects(%d, %d)\n", nhandles, timeout); */
256 ready = MsgWaitForMultipleObjects (nhandles, handles, FALSE,
257 timeout, QS_ALLINPUT);
258 /* g_print("=%d\n", ready); */
259 if (ready == WAIT_FAILED)
260 g_warning ("g_poll: MsgWaitForMultipleObjects failed");
263 else if (nhandles == 0)
265 /* Wait for nothing (huh?) */
270 /* Wait for just events
271 * -> Use WaitForMultipleObjects
273 /* g_print ("WaitForMultipleObjects(%d, %d)\n", nhandles, timeout); */
274 ready = WaitForMultipleObjects (nhandles, handles, FALSE, timeout);
275 /* g_print("=%d\n", ready); */
276 if (ready == WAIT_FAILED)
277 g_warning ("g_poll: WaitForMultipleObjects failed");
280 for (f = fds; f < &fds[nfds]; ++f)
283 if (ready == WAIT_FAILED)
285 else if (poll_msgs >= 0 && ready == WAIT_OBJECT_0 + nhandles)
287 fds[poll_msgs].revents |= G_IO_IN;
289 else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
290 for (f = fds; f < &fds[nfds]; ++f)
292 if ((f->events & G_IO_IN)
293 && f->fd == (gint) handles[ready - WAIT_OBJECT_0])
295 f->revents |= G_IO_IN;
296 /* g_print ("event %#x\n", f->fd); */
297 ResetEvent ((HANDLE) f->fd);
301 if (ready == WAIT_TIMEOUT)
307 #else /* !NATIVE_WIN32 */
309 /* The following implementation of poll() comes from the GNU C Library.
310 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
313 #include <string.h> /* for bzero on BSD systems */
315 #ifdef HAVE_SYS_SELECT_H
316 #include <sys/select.h>
317 #endif /* HAVE_SYS_SELECT_H_ */
320 # define SELECT_MASK fd_set
321 #else /* !NO_FD_SET */
323 typedef long fd_mask;
326 # define SELECT_MASK void
328 # define SELECT_MASK int
329 # endif /* !_IBMR2 */
330 #endif /* !NO_FD_SET */
333 g_poll (GPollFD *fds,
338 SELECT_MASK rset, wset, xset;
347 for (f = fds; f < &fds[nfds]; ++f)
350 if (f->events & G_IO_IN)
351 FD_SET (f->fd, &rset);
352 if (f->events & G_IO_OUT)
353 FD_SET (f->fd, &wset);
354 if (f->events & G_IO_PRI)
355 FD_SET (f->fd, &xset);
356 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
360 tv.tv_sec = timeout / 1000;
361 tv.tv_usec = (timeout % 1000) * 1000;
363 ready = select (maxfd + 1, &rset, &wset, &xset,
364 timeout == -1 ? NULL : &tv);
366 for (f = fds; f < &fds[nfds]; ++f)
371 if (FD_ISSET (f->fd, &rset))
372 f->revents |= G_IO_IN;
373 if (FD_ISSET (f->fd, &wset))
374 f->revents |= G_IO_OUT;
375 if (FD_ISSET (f->fd, &xset))
376 f->revents |= G_IO_PRI;
383 #endif /* !NATIVE_WIN32 */
385 static GPollFunc poll_func = g_poll;
386 #endif /* !HAVE_POLL */
388 /* Hooks for adding to the main loop */
390 /* Use knowledge of insert_sorted algorithm here to make
391 * sure we insert at the end of equal priority items
394 g_source_compare (GHook *a,
397 GSource *source_a = (GSource *)a;
398 GSource *source_b = (GSource *)b;
400 return (source_a->priority < source_b->priority) ? -1 : 1;
403 /* HOLDS: main_loop_lock */
405 g_source_destroy_func (GHookList *hook_list,
408 GSource *source = (GSource*) hook;
409 GDestroyNotify destroy;
411 G_UNLOCK (main_loop);
413 destroy = hook->destroy;
415 destroy (hook->data);
417 destroy = ((GSourceFuncs*) hook->func)->destroy;
419 destroy (source->source_data);
425 g_source_noop (GHookList *hook_list,
431 g_source_add (gint priority,
432 gboolean can_recurse,
434 gpointer source_data,
436 GDestroyNotify notify)
443 if (!source_list.is_setup)
444 g_hook_list_init (&source_list, sizeof(GSource));
446 source_list.hook_destroy = g_source_noop;
447 source_list.hook_free = g_source_destroy_func;
449 source = (GSource *)g_hook_alloc (&source_list);
450 source->priority = priority;
451 source->source_data = source_data;
452 source->hook.func = funcs;
453 source->hook.data = user_data;
454 source->hook.destroy = notify;
456 g_hook_insert_sorted (&source_list,
461 source->hook.flags |= G_SOURCE_CAN_RECURSE;
463 return_val = source->hook.hook_id;
465 #ifdef G_THREADS_ENABLED
466 /* Now wake up the main loop if it is waiting in the poll() */
470 poll_waiting = FALSE;
472 write (wake_up_pipe[1], "A", 1);
474 ReleaseSemaphore (wake_up_semaphore, 1, NULL);
478 G_UNLOCK (main_loop);
484 g_source_remove (guint tag)
488 g_return_val_if_fail (tag > 0, FALSE);
492 hook = g_hook_get (&source_list, tag);
494 g_hook_destroy_link (&source_list, hook);
496 G_UNLOCK (main_loop);
502 g_source_remove_by_user_data (gpointer user_data)
508 hook = g_hook_find_data (&source_list, TRUE, user_data);
510 g_hook_destroy_link (&source_list, hook);
512 G_UNLOCK (main_loop);
518 g_source_find_source_data (GHook *hook,
521 GSource *source = (GSource *)hook;
523 return (source->source_data == data);
527 g_source_remove_by_source_data (gpointer source_data)
533 hook = g_hook_find (&source_list, TRUE,
534 g_source_find_source_data, source_data);
536 g_hook_destroy_link (&source_list, hook);
538 G_UNLOCK (main_loop);
544 g_source_find_funcs_user_data (GHook *hook,
549 return hook->func == d[0] && hook->data == d[1];
553 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
559 g_return_val_if_fail (funcs != NULL, FALSE);
566 hook = g_hook_find (&source_list, TRUE,
567 g_source_find_funcs_user_data, d);
569 g_hook_destroy_link (&source_list, hook);
571 G_UNLOCK (main_loop);
577 g_get_current_time (GTimeVal *result)
581 g_return_if_fail (result != NULL);
583 /*this is required on alpha, there the timeval structs are int's
584 not longs and a cast only would fail horribly*/
585 gettimeofday (&r, NULL);
586 result->tv_sec = r.tv_sec;
587 result->tv_usec = r.tv_usec;
589 /* Avoid calling time() except for the first time.
590 * GetTickCount() should be pretty fast and low-level?
591 * I could also use ftime() but it seems unnecessarily overheady.
593 static DWORD start_tick = 0;
594 static time_t start_time;
598 g_return_if_fail (result != NULL);
602 start_tick = GetTickCount ();
606 tick = GetTickCount ();
608 result->tv_sec = (tick - start_tick) / 1000 + start_time;
609 result->tv_usec = ((tick - start_tick) % 1000) * 1000;
613 /* Running the main loop */
615 /* HOLDS: main_loop_lock */
617 g_main_dispatch (GTimeVal *current_time)
619 while (pending_dispatches != NULL)
621 gboolean need_destroy;
622 GSource *source = pending_dispatches->data;
625 tmp_list = pending_dispatches;
626 pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
627 g_slist_free_1 (tmp_list);
629 if (G_HOOK_IS_VALID (source))
631 gboolean was_in_call;
632 gpointer hook_data = source->hook.data;
633 gpointer source_data = source->source_data;
634 gboolean (*dispatch) (gpointer,
638 dispatch = ((GSourceFuncs *) source->hook.func)->dispatch;
640 was_in_call = G_HOOK_IN_CALL (source);
641 source->hook.flags |= G_HOOK_FLAG_IN_CALL;
643 G_UNLOCK (main_loop);
644 need_destroy = ! dispatch (source_data,
650 source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
652 if (need_destroy && G_HOOK_IS_VALID (source))
653 g_hook_destroy_link (&source_list, (GHook *) source);
656 g_hook_unref (&source_list, (GHook*) source);
660 /* g_main_iterate () runs a single iteration of the mainloop, or,
661 * if !dispatch checks to see if any sources need dispatching.
662 * basic algorithm for dispatch=TRUE:
664 * 1) while the list of currently pending sources is non-empty,
665 * we call (*dispatch) on those that are !IN_CALL or can_recurse,
666 * removing sources from the list after each returns.
667 * the return value of (*dispatch) determines whether the source
668 * itself is kept alive.
670 * 2) call (*prepare) for sources that are not yet SOURCE_READY and
671 * are !IN_CALL or can_recurse. a return value of TRUE determines
672 * that the source would like to be dispatched immediatedly, it
673 * is then flagged as SOURCE_READY.
675 * 3) poll with the pollfds from all sources at the priority of the
676 * first source flagged as SOURCE_READY. if there are any sources
677 * flagged as SOURCE_READY, we use a timeout of 0 or the minimum
678 * of all timouts otherwise.
680 * 4) for each source !IN_CALL or can_recurse, if SOURCE_READY or
681 * (*check) returns true, add the source to the pending list.
682 * once one source returns true, stop after checking all sources
685 * 5) while the list of currently pending sources is non-empty,
686 * call (*dispatch) on each source, removing the source
691 g_main_iterate (gboolean block,
695 GTimeVal current_time ={ 0, 0 };
697 gint current_priority = 0;
699 gboolean retval = FALSE;
701 g_return_val_if_fail (!block || dispatch, FALSE);
703 g_get_current_time (¤t_time);
707 /* If recursing, finish up current dispatch, before starting over */
708 if (pending_dispatches)
711 g_main_dispatch (¤t_time);
713 G_UNLOCK (main_loop);
718 /* Prepare all sources */
720 timeout = block ? -1 : 0;
722 hook = g_hook_first_valid (&source_list, TRUE);
725 GSource *source = (GSource *)hook;
726 gint source_timeout = -1;
728 if ((n_ready > 0) && (source->priority > current_priority))
730 g_hook_unref (&source_list, hook);
733 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
735 hook = g_hook_next_valid (&source_list, hook, TRUE);
739 in_check_or_prepare++;
740 if (hook->flags & G_SOURCE_READY ||
741 ((GSourceFuncs *) hook->func)->prepare (source->source_data,
745 in_check_or_prepare--;
748 hook->flags |= G_SOURCE_READY;
749 g_hook_unref (&source_list, hook);
750 G_UNLOCK (main_loop);
756 hook->flags |= G_SOURCE_READY;
758 current_priority = source->priority;
763 in_check_or_prepare--;
765 if (source_timeout >= 0)
768 timeout = source_timeout;
770 timeout = MIN (timeout, source_timeout);
773 hook = g_hook_next_valid (&source_list, hook, TRUE);
776 /* poll(), if necessary */
778 g_main_poll (timeout, n_ready > 0, current_priority);
780 /* Check to see what sources need to be dispatched */
784 hook = g_hook_first_valid (&source_list, TRUE);
787 GSource *source = (GSource *)hook;
789 if ((n_ready > 0) && (source->priority > current_priority))
791 g_hook_unref (&source_list, hook);
794 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
796 hook = g_hook_next_valid (&source_list, hook, TRUE);
800 in_check_or_prepare++;
801 if (hook->flags & G_SOURCE_READY ||
802 ((GSourceFuncs *) hook->func)->check (source->source_data,
805 in_check_or_prepare--;
808 hook->flags &= ~G_SOURCE_READY;
809 g_hook_ref (&source_list, hook);
810 pending_dispatches = g_slist_prepend (pending_dispatches, source);
811 current_priority = source->priority;
816 g_hook_unref (&source_list, hook);
817 G_UNLOCK (main_loop);
823 in_check_or_prepare--;
825 hook = g_hook_next_valid (&source_list, hook, TRUE);
828 /* Now invoke the callbacks */
830 if (pending_dispatches)
832 pending_dispatches = g_slist_reverse (pending_dispatches);
833 g_main_dispatch (¤t_time);
837 G_UNLOCK (main_loop);
842 /* See if any events are pending
845 g_main_pending (void)
847 return in_check_or_prepare ? FALSE : g_main_iterate (FALSE, FALSE);
850 /* Run a single iteration of the mainloop. If block is FALSE,
854 g_main_iteration (gboolean block)
856 if (in_check_or_prepare)
858 g_warning ("g_main_iteration(): called recursively from within a source's check() or "
859 "prepare() member, iteration not possible");
863 return g_main_iterate (block, TRUE);
867 g_main_new (gboolean is_running)
871 loop = g_new0 (GMainLoop, 1);
872 loop->is_running = is_running != FALSE;
878 g_main_run (GMainLoop *loop)
880 g_return_if_fail (loop != NULL);
882 if (in_check_or_prepare)
884 g_warning ("g_main_run(): called recursively from within a source's check() or "
885 "prepare() member, iteration not possible");
889 loop->is_running = TRUE;
890 while (loop->is_running)
891 g_main_iterate (TRUE, TRUE);
895 g_main_quit (GMainLoop *loop)
897 g_return_if_fail (loop != NULL);
899 loop->is_running = FALSE;
903 g_main_destroy (GMainLoop *loop)
905 g_return_if_fail (loop != NULL);
911 g_main_is_running (GMainLoop *loop)
913 g_return_val_if_fail (loop != NULL, FALSE);
915 return loop->is_running;
918 /* HOLDS: main_loop_lock */
920 g_main_poll (gint timeout,
921 gboolean use_priority,
929 #ifdef G_THREADS_ENABLED
931 if (wake_up_pipe[0] < 0)
933 if (pipe (wake_up_pipe) < 0)
934 g_error ("Cannot create pipe main loop wake-up: %s\n",
937 wake_up_rec.fd = wake_up_pipe[0];
938 wake_up_rec.events = G_IO_IN;
939 g_main_add_poll_unlocked (0, &wake_up_rec);
942 if (wake_up_semaphore == NULL)
944 if ((wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL)) == NULL)
945 g_error ("Cannot create wake-up semaphore: %d", GetLastError ());
946 wake_up_rec.fd = (gint) wake_up_semaphore;
947 wake_up_rec.events = G_IO_IN;
948 g_main_add_poll_unlocked (0, &wake_up_rec);
952 fd_array = g_new (GPollFD, n_poll_records);
954 pollrec = poll_records;
956 while (pollrec && (!use_priority || priority >= pollrec->priority))
958 fd_array[i].fd = pollrec->fd->fd;
959 fd_array[i].events = pollrec->fd->events;
960 fd_array[i].revents = 0;
962 pollrec = pollrec->next;
965 #ifdef G_THREADS_ENABLED
968 G_UNLOCK (main_loop);
970 (*poll_func) (fd_array, npoll, timeout);
973 #ifdef G_THREADS_ENABLED
978 read (wake_up_pipe[0], &c, 1);
982 poll_waiting = FALSE;
985 pollrec = poll_records;
989 pollrec->fd->revents = fd_array[i].revents;
990 pollrec = pollrec->next;
998 g_main_add_poll (GPollFD *fd,
1002 g_main_add_poll_unlocked (priority, fd);
1003 G_UNLOCK (main_loop);
1006 /* HOLDS: main_loop_lock */
1008 g_main_add_poll_unlocked (gint priority,
1011 GPollRec *lastrec, *pollrec, *newrec;
1014 poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
1018 newrec = poll_free_list;
1019 poll_free_list = newrec->next;
1022 newrec = g_chunk_new (GPollRec, poll_chunk);
1025 newrec->priority = priority;
1028 pollrec = poll_records;
1029 while (pollrec && priority >= pollrec->priority)
1032 pollrec = pollrec->next;
1036 lastrec->next = newrec;
1038 poll_records = newrec;
1040 newrec->next = pollrec;
1046 g_main_remove_poll (GPollFD *fd)
1048 GPollRec *pollrec, *lastrec;
1053 pollrec = poll_records;
1057 if (pollrec->fd == fd)
1059 if (lastrec != NULL)
1060 lastrec->next = pollrec->next;
1062 poll_records = pollrec->next;
1064 pollrec->next = poll_free_list;
1065 poll_free_list = pollrec;
1071 pollrec = pollrec->next;
1074 G_UNLOCK (main_loop);
1078 g_main_set_poll_func (GPollFunc func)
1084 poll_func = (GPollFunc) poll;
1086 poll_func = (GPollFunc) g_poll;
1093 g_timeout_prepare (gpointer source_data,
1094 GTimeVal *current_time,
1098 GTimeoutData *data = source_data;
1100 msec = (data->expiration.tv_sec - current_time->tv_sec) * 1000 +
1101 (data->expiration.tv_usec - current_time->tv_usec) / 1000;
1103 *timeout = (msec <= 0) ? 0 : msec;
1109 g_timeout_check (gpointer source_data,
1110 GTimeVal *current_time)
1112 GTimeoutData *data = source_data;
1114 return (data->expiration.tv_sec < current_time->tv_sec) ||
1115 ((data->expiration.tv_sec == current_time->tv_sec) &&
1116 (data->expiration.tv_usec <= current_time->tv_usec));
1120 g_timeout_dispatch (gpointer source_data,
1121 GTimeVal *current_time,
1124 GTimeoutData *data = source_data;
1126 if (data->callback (user_data))
1128 guint seconds = data->interval / 1000;
1129 guint msecs = data->interval - seconds * 1000;
1131 data->expiration.tv_sec = current_time->tv_sec + seconds;
1132 data->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
1133 if (data->expiration.tv_usec >= 1000000)
1135 data->expiration.tv_usec -= 1000000;
1136 data->expiration.tv_sec++;
1145 g_timeout_add_full (gint priority,
1147 GSourceFunc function,
1149 GDestroyNotify notify)
1153 GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
1155 timeout_data->interval = interval;
1156 timeout_data->callback = function;
1157 g_get_current_time (&timeout_data->expiration);
1159 seconds = timeout_data->interval / 1000;
1160 msecs = timeout_data->interval - seconds * 1000;
1162 timeout_data->expiration.tv_sec += seconds;
1163 timeout_data->expiration.tv_usec += msecs * 1000;
1164 if (timeout_data->expiration.tv_usec >= 1000000)
1166 timeout_data->expiration.tv_usec -= 1000000;
1167 timeout_data->expiration.tv_sec++;
1170 return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
1174 g_timeout_add (guint32 interval,
1175 GSourceFunc function,
1178 return g_timeout_add_full (G_PRIORITY_DEFAULT,
1179 interval, function, data, NULL);
1182 /* Idle functions */
1185 g_idle_prepare (gpointer source_data,
1186 GTimeVal *current_time,
1194 g_idle_check (gpointer source_data,
1195 GTimeVal *current_time)
1201 g_idle_dispatch (gpointer source_data,
1202 GTimeVal *current_time,
1205 GSourceFunc func = source_data;
1207 return func (user_data);
1211 g_idle_add_full (gint priority,
1212 GSourceFunc function,
1214 GDestroyNotify notify)
1216 g_return_val_if_fail (function != NULL, 0);
1218 return g_source_add (priority, FALSE, &idle_funcs, function, data, notify);
1222 g_idle_add (GSourceFunc function,
1225 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
1229 g_idle_remove_by_data (gpointer data)
1231 return g_source_remove_by_funcs_user_data (&idle_funcs, data);