summaryrefslogtreecommitdiff
path: root/gmain.h
blob: 7f08c3dfbd07b04eca85d2af0b665d859fcf72ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* GLIB - Library of useful routines for C programming
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GLib Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
 */

#ifndef __G_MAIN_H__
#define __G_MAIN_H__

#include <gtypes.h>

G_BEGIN_DECLS

/* Main loop
 */
typedef struct _GTimeVal        GTimeVal;
typedef struct _GSourceFuncs    GSourceFuncs;
typedef struct _GMainLoop       GMainLoop;      /* Opaque */

struct _GTimeVal
{
  glong tv_sec;
  glong tv_usec;
};
struct _GSourceFuncs
{
  gboolean (*prepare)  (gpointer  source_data,
                        GTimeVal *current_time,
                        gint     *timeout,
                        gpointer  user_data);
  gboolean (*check)    (gpointer  source_data,
                        GTimeVal *current_time,
                        gpointer  user_data);
  gboolean (*dispatch) (gpointer  source_data,
                        GTimeVal *dispatch_time,
                        gpointer  user_data);
  GDestroyNotify destroy;
};

/* Standard priorities */

#define G_PRIORITY_HIGH            -100
#define G_PRIORITY_DEFAULT          0
#define G_PRIORITY_HIGH_IDLE        100
#define G_PRIORITY_DEFAULT_IDLE     200
#define G_PRIORITY_LOW              300

typedef gboolean (*GSourceFunc) (gpointer data);

/* Hooks for adding to the main loop */
guint    g_source_add                        (gint           priority,
                                              gboolean       can_recurse,
                                              GSourceFuncs  *funcs,
                                              gpointer       source_data,
                                              gpointer       user_data,
                                              GDestroyNotify notify);
gboolean g_source_remove                     (guint          tag);
gboolean g_source_remove_by_user_data        (gpointer       user_data);
gboolean g_source_remove_by_source_data      (gpointer       source_data);
gboolean g_source_remove_by_funcs_user_data  (GSourceFuncs  *funcs,
                                              gpointer       user_data);

void g_get_current_time                 (GTimeVal       *result);

/* Running the main loop */
GMainLoop*      g_main_new              (gboolean        is_running);
void            g_main_run              (GMainLoop      *loop);
void            g_main_quit             (GMainLoop      *loop);
void            g_main_destroy          (GMainLoop      *loop);
gboolean        g_main_is_running       (GMainLoop      *loop);

/* Run a single iteration of the mainloop. If block is FALSE,
 * will never block
 */
gboolean        g_main_iteration        (gboolean       may_block);

/* See if any events are pending */
gboolean        g_main_pending          (void);

/* Idles and timeouts */
guint           g_timeout_add_full      (gint           priority,
                                         guint          interval,
                                         GSourceFunc    function,
                                         gpointer       data,
                                         GDestroyNotify notify);
guint           g_timeout_add           (guint          interval,
                                         GSourceFunc    function,
                                         gpointer       data);
guint           g_idle_add              (GSourceFunc    function,
                                         gpointer       data);
guint           g_idle_add_full         (gint           priority,
                                         GSourceFunc    function,
                                         gpointer       data,
                                         GDestroyNotify destroy);
gboolean        g_idle_remove_by_data   (gpointer       data);

/* GPollFD
 *
 * System-specific IO and main loop calls
 *
 * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
 * descriptor as provided by the C runtime) that can be used by
 * MsgWaitForMultipleObjects. This does *not* include file handles
 * from CreateFile, SOCKETs, nor pipe handles. (But you can use
 * WSAEventSelect to signal events when a SOCKET is readable).
 *
 * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
 * indicate polling for messages. These message queue GPollFDs should
 * be added with the g_main_poll_win32_msg_add function.
 *
 * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
 * (GTK) programs, as GDK itself wants to read messages and convert them
 * to GDK events.
 *
 * So, unless you really know what you are doing, it's best not to try
 * to use the main loop polling stuff for your own needs on
 * Win32. It's really only written for the GIMP's needs so
 * far.
 */

typedef struct _GPollFD GPollFD;
typedef gint    (*GPollFunc)    (GPollFD *ufds,
                                 guint    nfsd,
                                 gint     timeout);
struct _GPollFD
{
  gint          fd;
  gushort       events;
  gushort       revents;
};

void        g_main_add_poll          (GPollFD    *fd,
                                      gint        priority);
void        g_main_remove_poll       (GPollFD    *fd);
void        g_main_set_poll_func     (GPollFunc   func);

#ifdef G_OS_WIN32

/* Useful on other platforms, too? */

GPollFunc   g_main_win32_get_poll_func (void);

#endif

G_END_DECLS

#endif /* __G_MAIN_H__ */