new function to check whether a main loop has been quitted. (g_main_new):
[platform/upstream/glib.git] / gmain.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * gmain.c: Main loop abstraction, timeouts, and idle functions
5  * Copyright 1998 Owen Taylor
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /* 
24  * MT safe
25  */
26
27 #include "glib.h"
28 #include <sys/time.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include "config.h"
32
33 /* Types */
34
35 typedef struct _GIdleData GIdleData;
36 typedef struct _GTimeoutData GTimeoutData;
37 typedef struct _GSource GSource;
38 typedef struct _GPollRec GPollRec;
39
40 typedef enum
41 {
42   G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
43   G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
44 } GSourceFlags;
45
46 struct _GSource
47 {
48   GHook hook;
49   gint priority;
50   gpointer source_data;
51 };
52
53 struct _GMainLoop
54 {
55   gboolean is_running;
56 };
57
58 struct _GIdleData
59 {
60   GSourceFunc callback;
61 };
62
63 struct _GTimeoutData
64 {
65   GTimeVal    expiration;
66   gint        interval;
67   GSourceFunc callback;
68 };
69
70 struct _GPollRec
71 {
72   gint priority;
73   GPollFD *fd;
74   GPollRec *next;
75 };
76
77 /* Forward declarations */
78
79 static void     g_main_poll               (gint      timeout,
80                                            gboolean  use_priority, 
81                                            gint      priority);
82 static void     g_main_add_unlocking_poll (gint      priority,
83                                            GPollFD  *fd);
84
85 static gboolean g_timeout_prepare      (gpointer  source_data, 
86                                         GTimeVal *current_time,
87                                         gint     *timeout);
88 static gboolean g_timeout_check        (gpointer  source_data,
89                                         GTimeVal *current_time);
90 static gboolean g_timeout_dispatch     (gpointer  source_data,
91                                         GTimeVal *current_time,
92                                         gpointer  user_data);
93 static gboolean g_idle_prepare         (gpointer  source_data, 
94                                         GTimeVal *current_time,
95                                         gint     *timeout);
96 static gboolean g_idle_check           (gpointer  source_data,
97                                         GTimeVal *current_time);
98 static gboolean g_idle_dispatch        (gpointer  source_data,
99                                         GTimeVal *current_time,
100                                         gpointer  user_data);
101
102 /* Data */
103
104 static GSList *pending_dispatches = NULL;
105 static GHookList source_list = { 0 };
106
107 /* The following lock is used for both the list of sources
108  * and the list of poll records
109  */
110 G_LOCK_DECLARE_STATIC (main_loop);
111
112 static GSourceFuncs timeout_funcs = {
113   g_timeout_prepare,
114   g_timeout_check,
115   g_timeout_dispatch,
116   (GDestroyNotify)g_free
117 };
118
119 static GSourceFuncs idle_funcs = {
120   g_idle_prepare,
121   g_idle_check,
122   g_idle_dispatch,
123   (GDestroyNotify)g_free
124 };
125
126 static GPollRec *poll_records = NULL;
127 static GPollRec *poll_free_list = NULL;
128 static GMemChunk *poll_chunk;
129 static guint n_poll_records = 0;
130
131 /* this pipe is used to wake up the main loop when a source is added.
132  */
133 static gint wake_up_pipe[2] = { -1, -1 };
134 static GPollFD wake_up_rec;
135 static gboolean poll_waiting = FALSE;
136
137 #ifdef HAVE_POLL
138 static GPollFunc poll_func = (GPollFunc) poll;
139 #else   /* !HAVE_POLL */
140
141 /* The following implementation of poll() comes from the GNU C Library.
142  * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
143  */
144
145 #include <string.h> /* for bzero on BSD systems */
146
147 #ifdef HAVE_SYS_SELECT_H
148 #include <sys/select.h>
149 #endif /* HAVE_SYS_SELECT_H_ */
150
151 #ifndef NO_FD_SET
152 #  define SELECT_MASK fd_set
153 #else   /* !NO_FD_SET */
154 #  ifndef _AIX
155 typedef long fd_mask;
156 #  endif
157 #  if defined(_IBMR2)
158 #    define SELECT_MASK void
159 #  else
160 #    define SELECT_MASK int
161 #  endif
162 #endif  /* !NO_FD_SET */
163
164 static gint 
165 g_poll (GPollFD *fds, guint nfds, gint timeout)
166 {
167   struct timeval tv;
168   SELECT_MASK rset, wset, xset;
169   GPollFD *f;
170   int ready;
171   int maxfd = 0;
172
173   FD_ZERO (&rset);
174   FD_ZERO (&wset);
175   FD_ZERO (&xset);
176
177   for (f = fds; f < &fds[nfds]; ++f)
178     if (f->fd >= 0)
179       {
180         if (f->events & G_IO_IN)
181           FD_SET (f->fd, &rset);
182         if (f->events & G_IO_OUT)
183           FD_SET (f->fd, &wset);
184         if (f->events & G_IO_PRI)
185           FD_SET (f->fd, &xset);
186         if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
187           maxfd = f->fd;
188       }
189
190   tv.tv_sec = timeout / 1000;
191   tv.tv_usec = (timeout % 1000) * 1000;
192
193   ready = select (maxfd + 1, &rset, &wset, &xset,
194                   timeout == -1 ? NULL : &tv);
195   if (ready > 0)
196     for (f = fds; f < &fds[nfds]; ++f)
197       {
198         f->revents = 0;
199         if (f->fd >= 0)
200           {
201             if (FD_ISSET (f->fd, &rset))
202               f->revents |= G_IO_IN;
203             if (FD_ISSET (f->fd, &wset))
204               f->revents |= G_IO_OUT;
205             if (FD_ISSET (f->fd, &xset))
206               f->revents |= G_IO_PRI;
207           }
208       }
209
210   return ready;
211 }
212 static GPollFunc poll_func = g_poll;
213 #endif  /* !HAVE_POLL */
214
215 /* Hooks for adding to the main loop */
216
217 /* Use knowledge of insert_sorted algorithm here to make
218  * sure we insert at the end of equal priority items
219  */
220 static gint
221 g_source_compare (GHook *a,
222                   GHook *b)
223 {
224   GSource *source_a = (GSource *)a;
225   GSource *source_b = (GSource *)b;
226
227   return (source_a->priority < source_b->priority) ? -1 : 1;
228 }
229
230 guint 
231 g_source_add (gint           priority,
232               gboolean       can_recurse,
233               GSourceFuncs  *funcs,
234               gpointer       source_data, 
235               gpointer       user_data,
236               GDestroyNotify notify)
237 {
238   guint return_val;
239   GSource *source;
240
241   G_LOCK (main_loop);
242
243   if (!source_list.is_setup)
244     g_hook_list_init (&source_list, sizeof(GSource));
245
246   source = (GSource *)g_hook_alloc (&source_list);
247   source->priority = priority;
248   source->source_data = source_data;
249   source->hook.func = funcs;
250   source->hook.data = user_data;
251   source->hook.destroy = notify;
252   
253   g_hook_insert_sorted (&source_list, 
254                         (GHook *)source, 
255                         g_source_compare);
256
257   if (can_recurse)
258     source->hook.flags |= G_SOURCE_CAN_RECURSE;
259
260   return_val = source->hook.hook_id;
261
262   /* Now wake up the main loop if it is waiting in the poll() */
263
264   if (poll_waiting)
265     {
266       poll_waiting = FALSE;
267       write (wake_up_pipe[1], "A", 1);
268     }
269
270   G_UNLOCK (main_loop);
271
272   return return_val;
273 }
274
275 void 
276 g_source_remove (guint tag)
277 {
278   GHook *hook;
279
280   G_LOCK (main_loop);
281
282   hook = g_hook_get (&source_list, tag);
283   if (hook)
284     {
285       GSource *source = (GSource *)hook;
286       
287       ((GSourceFuncs *) source->hook.func)->destroy (source->source_data);
288       g_hook_destroy_link (&source_list, hook);
289     }
290
291   G_UNLOCK (main_loop);
292 }
293
294 void 
295 g_source_remove_by_user_data (gpointer user_data)
296 {
297   GHook *hook;
298   
299   G_LOCK (main_loop);
300   
301   hook = g_hook_find_data (&source_list, TRUE, user_data);
302   if (hook)
303     {
304       GSource *source = (GSource *)hook;
305
306       ((GSourceFuncs *) source->hook.func)->destroy (source->source_data);
307       g_hook_destroy_link (&source_list, hook);
308     }
309
310   G_UNLOCK (main_loop);
311 }
312
313 static gboolean
314 g_source_find_source_data (GHook        *hook,
315                            gpointer      data)
316 {
317   GSource *source = (GSource *)hook;
318
319   return (source->source_data == data);
320 }
321
322 void 
323 g_source_remove_by_source_data (gpointer source_data)
324 {
325   GHook *hook;
326
327   G_LOCK (main_loop);
328
329   hook = g_hook_find (&source_list, TRUE, 
330                       g_source_find_source_data, source_data);
331   if (hook)
332     {
333       GSource *source = (GSource *)hook;
334
335       ((GSourceFuncs *) source->hook.func)->destroy (source->source_data);
336       g_hook_destroy_link (&source_list, hook);
337     }
338
339   G_UNLOCK (main_loop);
340 }
341
342 void
343 g_get_current_time (GTimeVal *result)
344 {
345   g_return_if_fail (result != NULL);
346
347   gettimeofday ((struct timeval *) result, NULL);
348 }
349
350 /* Running the main loop */
351
352 /* HOLDS: main_loop_lock */
353 static void
354 g_main_dispatch (GTimeVal *current_time)
355 {
356   while (pending_dispatches != NULL)
357     {
358       gboolean need_destroy;
359       GSource *source = pending_dispatches->data;
360       GSList *tmp_list;
361
362       tmp_list = pending_dispatches;
363       pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
364       g_slist_free_1 (tmp_list);
365
366       if (G_HOOK_IS_VALID (source))
367         {
368           gboolean was_in_call;
369           gpointer hook_data = source->hook.data;
370           gpointer source_data = source->source_data;
371           gboolean (*dispatch) (gpointer,
372                                 GTimeVal *,
373                                 gpointer);
374
375           dispatch = ((GSourceFuncs *) source->hook.func)->dispatch;
376           
377           was_in_call = G_HOOK_IN_CALL (source);
378           source->hook.flags |= G_HOOK_FLAG_IN_CALL;
379
380           G_UNLOCK (main_loop);
381           need_destroy = ! dispatch (source_data,
382                                      current_time,
383                                      hook_data);
384           G_LOCK (main_loop);
385
386           if (!was_in_call)
387             source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
388           
389           if (need_destroy && G_HOOK_IS_VALID (source))
390             {
391               ((GSourceFuncs *) source->hook.func)->destroy (source->source_data);
392               g_hook_destroy_link (&source_list, (GHook *) source);
393             }
394         }
395
396       g_hook_unref (&source_list, (GHook *)source);
397     }
398 }
399
400 /* g_main_iterate () runs a single iteration of the mainloop, or,
401  * if !dispatch checks to see if any sources need dispatching.
402  * basic algorithm for dispatch=TRUE:
403  *
404  * 1) while the list of currently pending sources is non-empty,
405  *    we call (*dispatch) on those that are !IN_CALL or can_recurse,
406  *    removing sources from the list after each returns.
407  *    the return value of (*dispatch) determines whether the source
408  *    itself is kept alive.
409  *
410  * 2) call (*prepare) for sources that are not yet SOURCE_READY and
411  *    are !IN_CALL or can_recurse. a return value of TRUE determines
412  *    that the source would like to be dispatched immediatedly, it
413  *    is then flagged as SOURCE_READY.
414  *
415  * 3) poll with the pollfds from all sources at the priority of the
416  *    first source flagged as SOURCE_READY. if there are any sources
417  *    flagged as SOURCE_READY, we use a timeout of 0 or the minimum
418  *    of all timouts otherwise.
419  *
420  * 4) for each source !IN_CALL or can_recurse, if SOURCE_READY or
421  *    (*check) returns true, add the source to the pending list.
422  *    once one source returns true, stop after checking all sources
423  *    at that priority.
424  *
425  * 5) while the list of currently pending sources is non-empty,
426  *    call (*dispatch) on each source, removing the source
427  *    after the call.
428  *
429  */
430 static gboolean
431 g_main_iterate (gboolean block,
432                 gboolean dispatch)
433 {
434   GHook *hook;
435   GTimeVal current_time;
436   gint n_ready = 0;
437   gint current_priority = 0;
438   gint timeout;
439   gboolean retval = FALSE;
440
441   g_return_val_if_fail (!block || dispatch, FALSE);
442
443   g_get_current_time (&current_time);
444
445   G_LOCK (main_loop);
446   
447   /* If recursing, finish up current dispatch, before starting over */
448   if (pending_dispatches)
449     {
450       if (dispatch)
451         g_main_dispatch (&current_time);
452       
453       G_UNLOCK (main_loop);
454
455       return TRUE;
456     }
457
458   /* Prepare all sources */
459
460   timeout = block ? -1 : 0;
461   
462   hook = g_hook_first_valid (&source_list, TRUE);
463   while (hook)
464     {
465       GSource *source = (GSource *)hook;
466       GHook *tmp;
467       gint source_timeout;
468
469       if ((n_ready > 0) && (source->priority > current_priority))
470         break;
471       if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
472         {
473           hook = g_hook_next_valid (hook, TRUE);
474           continue;
475         }
476
477       g_hook_ref (&source_list, hook);
478       
479       if (hook->flags & G_SOURCE_READY ||
480           ((GSourceFuncs *) hook->func)->prepare (source->source_data,
481                                                   &current_time,
482                                                   &source_timeout))
483         {
484           if (!dispatch)
485             {
486               hook->flags |= G_SOURCE_READY;
487               g_hook_unref (&source_list, hook);
488               G_UNLOCK (main_loop);
489
490               return TRUE;
491             }
492           else
493             {
494               hook->flags |= G_SOURCE_READY;
495               n_ready++;
496               current_priority = source->priority;
497               timeout = 0;
498             }
499         }
500       
501       if (source_timeout >= 0)
502         {
503           if (timeout < 0)
504             timeout = source_timeout;
505           else
506             timeout = MIN (timeout, source_timeout);
507         }
508
509       tmp = g_hook_next_valid (hook, TRUE);
510       
511       g_hook_unref (&source_list, hook);
512       hook = tmp;
513     }
514
515   /* poll(), if necessary */
516
517   g_main_poll (timeout, n_ready > 0, current_priority);
518
519   /* Check to see what sources need to be dispatched */
520
521   n_ready = 0;
522   
523   hook = g_hook_first_valid (&source_list, TRUE);
524   while (hook)
525     {
526       GSource *source = (GSource *)hook;
527       GHook *tmp;
528
529       if ((n_ready > 0) && (source->priority > current_priority))
530         break;
531       if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
532         {
533           hook = g_hook_next_valid (hook, TRUE);
534           continue;
535         }
536
537       g_hook_ref (&source_list, hook);
538
539       if (hook->flags & G_SOURCE_READY ||
540           ((GSourceFuncs *) hook->func)->check (source->source_data,
541                                                 &current_time))
542         {
543           if (dispatch)
544             {
545               hook->flags &= ~G_SOURCE_READY;
546               g_hook_ref (&source_list, hook);
547               pending_dispatches = g_slist_prepend (pending_dispatches, source);
548               current_priority = source->priority;
549               n_ready++;
550             }
551           else
552             {
553               g_hook_unref (&source_list, hook);
554               G_UNLOCK (main_loop);
555
556               return TRUE;
557             }
558         }
559       
560       tmp = g_hook_next_valid (hook, TRUE);
561       
562       g_hook_unref (&source_list, hook);
563       hook = tmp;
564     }
565
566   /* Now invoke the callbacks */
567
568   if (pending_dispatches)
569     {
570       pending_dispatches = g_slist_reverse (pending_dispatches);
571       g_main_dispatch (&current_time);
572       retval = TRUE;
573     }
574
575   G_UNLOCK (main_loop);
576
577   return retval;
578 }
579
580 /* See if any events are pending
581  */
582 gboolean 
583 g_main_pending ()
584 {
585   return g_main_iterate (FALSE, FALSE);
586 }
587
588 /* Run a single iteration of the mainloop. If block is FALSE,
589  * will never block
590  */
591 gboolean
592 g_main_iteration (gboolean block)
593 {
594   return g_main_iterate (block, TRUE);
595 }
596
597 GMainLoop*
598 g_main_new (gboolean is_running)
599 {
600   GMainLoop *loop;
601
602   loop = g_new0 (GMainLoop, 1);
603   loop->is_running = is_running != FALSE;
604
605   return loop;
606 }
607
608 void 
609 g_main_run (GMainLoop *loop)
610 {
611   g_return_if_fail (loop != NULL);
612
613   loop->is_running = TRUE;
614   while (loop->is_running)
615     g_main_iterate (TRUE, TRUE);
616 }
617
618 void 
619 g_main_quit (GMainLoop *loop)
620 {
621   g_return_if_fail (loop != NULL);
622
623   loop->is_running = FALSE;
624 }
625
626 void 
627 g_main_destroy (GMainLoop *loop)
628 {
629   g_return_if_fail (loop != NULL);
630
631   g_free (loop);
632 }
633
634 gboolean
635 g_main_is_running (GMainLoop *loop)
636 {
637   g_return_val_if_fail (loop != NULL, FALSE);
638
639   return loop->is_running;
640 }
641
642 /* HOLDS: main_loop_lock */
643 static void
644 g_main_poll (gint     timeout,
645              gboolean use_priority,
646              gint     priority)
647 {
648   GPollFD *fd_array = g_new (GPollFD, n_poll_records);
649   GPollRec *pollrec;
650
651   gint i;
652   gint npoll;
653
654   if (wake_up_pipe[0] < 0)
655     {
656       if (pipe (wake_up_pipe) < 0)
657         g_error ("Cannot create pipe main loop wake-up: %s\n",
658                  g_strerror (errno));
659
660       wake_up_rec.fd = wake_up_pipe[0];
661       wake_up_rec.events = G_IO_IN;
662       g_main_add_unlocking_poll (0, &wake_up_rec);
663     }
664   
665   pollrec = poll_records;
666   i = 0;
667   while (pollrec && (!use_priority || priority >= pollrec->priority))
668     {
669       fd_array[i].fd = pollrec->fd->fd;
670       fd_array[i].events = pollrec->fd->events;
671       fd_array[i].revents = 0;
672         
673       pollrec = pollrec->next;
674       i++;
675     }
676
677   poll_waiting = TRUE;
678   
679   G_UNLOCK (main_loop);
680   npoll = i;
681   (*poll_func) (fd_array, npoll, timeout);
682   G_LOCK (main_loop);
683
684   if (!poll_waiting)
685     {
686       gchar c;
687       read (wake_up_pipe[0], &c, 1);
688     }
689   else
690     poll_waiting = FALSE;
691
692   pollrec = poll_records;
693   i = 0;
694   while (i < npoll)
695     {
696       pollrec->fd->revents = fd_array[i].revents;
697       pollrec = pollrec->next;
698       i++;
699     }
700
701   g_free (fd_array);
702 }
703
704 void 
705 g_main_add_poll (GPollFD *fd,
706                  gint     priority)
707 {
708   G_LOCK (main_loop);
709   g_main_add_unlocking_poll (priority, fd);
710   G_UNLOCK (main_loop);
711 }
712
713 /* HOLDS: main_loop_lock */
714 static void 
715 g_main_add_unlocking_poll (gint     priority,
716                            GPollFD *fd)
717 {
718   GPollRec *lastrec, *pollrec, *newrec;
719
720   if (!poll_chunk)
721     poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
722
723   if (poll_free_list)
724     {
725       newrec = poll_free_list;
726       poll_free_list = newrec->next;
727     }
728   else
729     newrec = g_chunk_new (GPollRec, poll_chunk);
730
731   newrec->fd = fd;
732   newrec->priority = priority;
733
734   lastrec = NULL;
735   pollrec = poll_records;
736   while (pollrec && priority >= pollrec->priority)
737     {
738       lastrec = pollrec;
739       pollrec = pollrec->next;
740     }
741   
742   if (lastrec)
743     lastrec->next = newrec;
744   else
745     poll_records = newrec;
746
747   newrec->next = pollrec;
748
749   n_poll_records++;
750 }
751
752 void 
753 g_main_remove_poll (GPollFD *fd)
754 {
755   GPollRec *pollrec, *lastrec;
756
757   G_LOCK (main_loop);
758   
759   lastrec = NULL;
760   pollrec = poll_records;
761
762   while (pollrec)
763     {
764       if (pollrec->fd == fd)
765         {
766           if (lastrec != NULL)
767             lastrec->next = pollrec->next;
768           else
769             poll_records = pollrec->next;
770
771           pollrec->next = poll_free_list;
772           poll_free_list = pollrec;
773
774           n_poll_records--;
775           break;
776         }
777       lastrec = pollrec;
778       pollrec = pollrec->next;
779     }
780
781   G_UNLOCK (main_loop);
782 }
783
784 void 
785 g_main_set_poll_func (GPollFunc func)
786 {
787   if (func)
788     poll_func = func;
789   else
790 #ifdef HAVE_POLL
791     poll_func = (GPollFunc)poll;
792 #else
793     poll_func = (GPollFunc)g_poll;
794 #endif
795 }
796
797 /* Timeouts */
798
799 static gboolean 
800 g_timeout_prepare  (gpointer source_data, 
801                     GTimeVal *current_time,
802                     gint    *timeout)
803 {
804   glong msec;
805   GTimeoutData *data = source_data;
806
807   msec = (data->expiration.tv_sec  - current_time->tv_sec) * 1000 +
808          (data->expiration.tv_usec - current_time->tv_usec) / 1000;
809
810   *timeout = (msec <= 0) ? 0 : msec;
811
812   return (msec <= 0);
813 }
814
815 static gboolean 
816 g_timeout_check    (gpointer source_data,
817                     GTimeVal *current_time)
818 {
819   GTimeoutData *data = source_data;
820
821   return (data->expiration.tv_sec < current_time->tv_sec) ||
822          ((data->expiration.tv_sec == current_time->tv_sec) &&
823           (data->expiration.tv_usec <= current_time->tv_usec));
824 }
825
826 static gboolean
827 g_timeout_dispatch (gpointer source_data, 
828                     GTimeVal *current_time,
829                     gpointer user_data)
830 {
831   GTimeoutData *data = source_data;
832
833   if (data->callback(user_data))
834     {
835       data->expiration.tv_sec = current_time->tv_sec;
836       data->expiration.tv_usec = current_time->tv_usec + data->interval * 1000;
837       if (data->expiration.tv_usec >= 1000000)
838         {
839           data->expiration.tv_usec -= 1000000;
840           data->expiration.tv_sec++;
841         }
842       return TRUE;
843     }
844   else
845     return FALSE;
846 }
847
848 guint 
849 g_timeout_add_full (gint           priority,
850                     guint          interval, 
851                     GSourceFunc    function,
852                     gpointer       data,
853                     GDestroyNotify notify)
854 {
855   GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
856
857   timeout_data->interval = interval;
858   timeout_data->callback = function;
859   g_get_current_time (&timeout_data->expiration);
860
861   timeout_data->expiration.tv_usec += timeout_data->interval * 1000;
862   if (timeout_data->expiration.tv_usec >= 1000000)
863     {
864       timeout_data->expiration.tv_usec -= 1000000;
865       timeout_data->expiration.tv_sec++;
866     }
867
868   return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
869 }
870
871 guint 
872 g_timeout_add (guint32        interval,
873                GSourceFunc    function,
874                gpointer       data)
875 {
876   return g_timeout_add_full (0, interval, function, data, NULL);
877 }
878
879 /* Idle functions */
880
881 static gboolean 
882 g_idle_prepare  (gpointer source_data, 
883                  GTimeVal *current_time,
884                  gint     *timeout)
885 {
886   timeout = 0;
887   return TRUE;
888 }
889
890 static gboolean 
891 g_idle_check    (gpointer  source_data,
892                  GTimeVal *current_time)
893 {
894   return TRUE;
895 }
896
897 static gboolean
898 g_idle_dispatch (gpointer source_data, 
899                  GTimeVal *current_time,
900                  gpointer user_data)
901 {
902   GIdleData *data = source_data;
903
904   return (*data->callback)(user_data);
905 }
906
907 guint 
908 g_idle_add_full (gint          priority,
909                  GSourceFunc    function,
910                  gpointer       data,
911                  GDestroyNotify notify)
912 {
913   GIdleData *idle_data = g_new (GIdleData, 1);
914
915   idle_data->callback = function;
916
917   return g_source_add (priority, FALSE, &idle_funcs, idle_data, data, notify);
918 }
919
920 guint 
921 g_idle_add (GSourceFunc    function,
922             gpointer       data)
923 {
924   return g_idle_add_full (0, function, data, NULL);
925 }