Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

532 lignes
19 KiB

  1. /* gmain.h - the GLib Main loop
  2. * Copyright (C) 1998-2000 Red Hat, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. */
  19. #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  20. #error "Only <glib.h> can be included directly."
  21. #endif
  22. #ifndef __G_MAIN_H__
  23. #define __G_MAIN_H__
  24. #include <glib/gpoll.h>
  25. #include <glib/gslist.h>
  26. #include <glib/gthread.h>
  27. G_BEGIN_DECLS
  28. /**
  29. * GMainContext:
  30. *
  31. * The <structname>GMainContext</structname> struct is an opaque data
  32. * type representing a set of sources to be handled in a main loop.
  33. */
  34. typedef struct _GMainContext GMainContext;
  35. /**
  36. * GMainLoop:
  37. *
  38. * The <structname>GMainLoop</structname> struct is an opaque data type
  39. * representing the main event loop of a GLib or GTK+ application.
  40. */
  41. typedef struct _GMainLoop GMainLoop;
  42. /**
  43. * GSource:
  44. *
  45. * The <structname>GSource</structname> struct is an opaque data type
  46. * representing an event source.
  47. */
  48. typedef struct _GSource GSource;
  49. /**
  50. * GSourceCallbackFuncs:
  51. * @ref: Called when a reference is added to the callback object
  52. * @unref: Called when a reference to the callback object is dropped
  53. * @get: Called to extract the callback function and data from the
  54. * callback object.
  55. * The <structname>GSourceCallbackFuncs</structname> struct contains
  56. * functions for managing callback objects.
  57. */
  58. typedef struct _GSourceCallbackFuncs GSourceCallbackFuncs;
  59. /**
  60. * GSourceFuncs:
  61. * @prepare: Called before all the file descriptors are polled. If the
  62. * source can determine that it is ready here (without waiting for the
  63. * results of the poll() call) it should return %TRUE. It can also return
  64. * a @timeout_ value which should be the maximum timeout (in milliseconds)
  65. * which should be passed to the poll() call. The actual timeout used will
  66. * be -1 if all sources returned -1, or it will be the minimum of all the
  67. * @timeout_ values returned which were >= 0.
  68. * @check: Called after all the file descriptors are polled. The source
  69. * should return %TRUE if it is ready to be dispatched. Note that some
  70. * time may have passed since the previous prepare function was called,
  71. * so the source should be checked again here.
  72. * @dispatch: Called to dispatch the event source, after it has returned
  73. * %TRUE in either its @prepare or its @check function. The @dispatch
  74. * function is passed in a callback function and data. The callback
  75. * function may be %NULL if the source was never connected to a callback
  76. * using g_source_set_callback(). The @dispatch function should call the
  77. * callback function with @user_data and whatever additional parameters
  78. * are needed for this type of event source.
  79. * @finalize: Called when the source is finalized.
  80. * @closure_callback:
  81. * @closure_marshal:
  82. *
  83. * The <structname>GSourceFuncs</structname> struct contains a table of
  84. * functions used to handle event sources in a generic manner.
  85. *
  86. * For idle sources, the prepare and check functions always return %TRUE
  87. * to indicate that the source is always ready to be processed. The prepare
  88. * function also returns a timeout value of 0 to ensure that the poll() call
  89. * doesn't block (since that would be time wasted which could have been spent
  90. * running the idle function).
  91. *
  92. * For timeout sources, the prepare and check functions both return %TRUE
  93. * if the timeout interval has expired. The prepare function also returns
  94. * a timeout value to ensure that the poll() call doesn't block too long
  95. * and miss the next timeout.
  96. *
  97. * For file descriptor sources, the prepare function typically returns %FALSE,
  98. * since it must wait until poll() has been called before it knows whether
  99. * any events need to be processed. It sets the returned timeout to -1 to
  100. * indicate that it doesn't mind how long the poll() call blocks. In the
  101. * check function, it tests the results of the poll() call to see if the
  102. * required condition has been met, and returns %TRUE if so.
  103. */
  104. typedef struct _GSourceFuncs GSourceFuncs;
  105. /**
  106. * GPid:
  107. *
  108. * A type which is used to hold a process identification.
  109. *
  110. * On UNIX, processes are identified by a process id (an integer),
  111. * while Windows uses process handles (which are pointers).
  112. */
  113. typedef gboolean (*GSourceFunc) (gpointer data);
  114. /**
  115. * GChildWatchFunc:
  116. * @pid: the process id of the child process
  117. * @status: Status information about the child process,
  118. * see waitpid(2) for more information about this field
  119. * @data: user data passed to g_child_watch_add()
  120. *
  121. * The type of functions to be called when a child exists.
  122. */
  123. typedef void (*GChildWatchFunc) (GPid pid,
  124. gint status,
  125. gpointer data);
  126. struct _GSource
  127. {
  128. /*< private >*/
  129. gpointer callback_data;
  130. GSourceCallbackFuncs *callback_funcs;
  131. GSourceFuncs *source_funcs;
  132. guint ref_count;
  133. GMainContext *context;
  134. gint priority;
  135. guint flags;
  136. guint source_id;
  137. GSList *poll_fds;
  138. GSource *prev;
  139. GSource *next;
  140. char *name;
  141. gpointer reserved2;
  142. };
  143. struct _GSourceCallbackFuncs
  144. {
  145. void (*ref) (gpointer cb_data);
  146. void (*unref) (gpointer cb_data);
  147. void (*get) (gpointer cb_data,
  148. GSource *source,
  149. GSourceFunc *func,
  150. gpointer *data);
  151. };
  152. typedef void (*GSourceDummyMarshal) (void);
  153. struct _GSourceFuncs
  154. {
  155. gboolean (*prepare) (GSource *source,
  156. gint *timeout_);
  157. gboolean (*check) (GSource *source);
  158. gboolean (*dispatch) (GSource *source,
  159. GSourceFunc callback,
  160. gpointer user_data);
  161. void (*finalize) (GSource *source); /* Can be NULL */
  162. /* For use by g_source_set_closure */
  163. GSourceFunc closure_callback;
  164. GSourceDummyMarshal closure_marshal; /* Really is of type GClosureMarshal */
  165. };
  166. /* Standard priorities */
  167. /**
  168. * G_PRIORITY_HIGH:
  169. *
  170. * Use this for high priority event sources.
  171. *
  172. * It is not used within GLib or GTK+.
  173. */
  174. #define G_PRIORITY_HIGH -100
  175. /**
  176. * G_PRIORITY_DEFAULT:
  177. *
  178. * Use this for default priority event sources.
  179. *
  180. * In GLib this priority is used when adding timeout functions
  181. * with g_timeout_add(). In GDK this priority is used for events
  182. * from the X server.
  183. */
  184. #define G_PRIORITY_DEFAULT 0
  185. /**
  186. * G_PRIORITY_HIGH_IDLE:
  187. *
  188. * Use this for high priority idle functions.
  189. *
  190. * GTK+ uses #G_PRIORITY_HIGH_IDLE + 10 for resizing operations,
  191. * and #G_PRIORITY_HIGH_IDLE + 20 for redrawing operations. (This is
  192. * done to ensure that any pending resizes are processed before any
  193. * pending redraws, so that widgets are not redrawn twice unnecessarily.)
  194. */
  195. #define G_PRIORITY_HIGH_IDLE 100
  196. /**
  197. * G_PRIORITY_DEFAULT_IDLE:
  198. *
  199. * Use this for default priority idle functions.
  200. *
  201. * In GLib this priority is used when adding idle functions with
  202. * g_idle_add().
  203. */
  204. #define G_PRIORITY_DEFAULT_IDLE 200
  205. /**
  206. * G_PRIORITY_LOW:
  207. *
  208. * Use this for very low priority background tasks.
  209. *
  210. * It is not used within GLib or GTK+.
  211. */
  212. #define G_PRIORITY_LOW 300
  213. /* GMainContext: */
  214. GMainContext *g_main_context_new (void);
  215. GMainContext *g_main_context_ref (GMainContext *context);
  216. void g_main_context_unref (GMainContext *context);
  217. GMainContext *g_main_context_default (void);
  218. gboolean g_main_context_iteration (GMainContext *context,
  219. gboolean may_block);
  220. gboolean g_main_context_pending (GMainContext *context);
  221. /* For implementation of legacy interfaces
  222. */
  223. GSource *g_main_context_find_source_by_id (GMainContext *context,
  224. guint source_id);
  225. GSource *g_main_context_find_source_by_user_data (GMainContext *context,
  226. gpointer user_data);
  227. GSource *g_main_context_find_source_by_funcs_user_data (GMainContext *context,
  228. GSourceFuncs *funcs,
  229. gpointer user_data);
  230. /* Low level functions for implementing custom main loops.
  231. */
  232. void g_main_context_wakeup (GMainContext *context);
  233. gboolean g_main_context_acquire (GMainContext *context);
  234. void g_main_context_release (GMainContext *context);
  235. gboolean g_main_context_is_owner (GMainContext *context);
  236. gboolean g_main_context_wait (GMainContext *context,
  237. GCond *cond,
  238. GMutex *mutex);
  239. gboolean g_main_context_prepare (GMainContext *context,
  240. gint *priority);
  241. gint g_main_context_query (GMainContext *context,
  242. gint max_priority,
  243. gint *timeout_,
  244. GPollFD *fds,
  245. gint n_fds);
  246. gint g_main_context_check (GMainContext *context,
  247. gint max_priority,
  248. GPollFD *fds,
  249. gint n_fds);
  250. void g_main_context_dispatch (GMainContext *context);
  251. void g_main_context_set_poll_func (GMainContext *context,
  252. GPollFunc func);
  253. GPollFunc g_main_context_get_poll_func (GMainContext *context);
  254. /* Low level functions for use by source implementations
  255. */
  256. void g_main_context_add_poll (GMainContext *context,
  257. GPollFD *fd,
  258. gint priority);
  259. void g_main_context_remove_poll (GMainContext *context,
  260. GPollFD *fd);
  261. gint g_main_depth (void);
  262. GSource *g_main_current_source (void);
  263. /* GMainContexts for other threads
  264. */
  265. void g_main_context_push_thread_default (GMainContext *context);
  266. void g_main_context_pop_thread_default (GMainContext *context);
  267. GMainContext *g_main_context_get_thread_default (void);
  268. /* GMainLoop: */
  269. GMainLoop *g_main_loop_new (GMainContext *context,
  270. gboolean is_running);
  271. void g_main_loop_run (GMainLoop *loop);
  272. void g_main_loop_quit (GMainLoop *loop);
  273. GMainLoop *g_main_loop_ref (GMainLoop *loop);
  274. void g_main_loop_unref (GMainLoop *loop);
  275. gboolean g_main_loop_is_running (GMainLoop *loop);
  276. GMainContext *g_main_loop_get_context (GMainLoop *loop);
  277. /* GSource: */
  278. GSource *g_source_new (GSourceFuncs *source_funcs,
  279. guint struct_size);
  280. GSource *g_source_ref (GSource *source);
  281. void g_source_unref (GSource *source);
  282. guint g_source_attach (GSource *source,
  283. GMainContext *context);
  284. void g_source_destroy (GSource *source);
  285. void g_source_set_priority (GSource *source,
  286. gint priority);
  287. gint g_source_get_priority (GSource *source);
  288. void g_source_set_can_recurse (GSource *source,
  289. gboolean can_recurse);
  290. gboolean g_source_get_can_recurse (GSource *source);
  291. guint g_source_get_id (GSource *source);
  292. GMainContext *g_source_get_context (GSource *source);
  293. void g_source_set_callback (GSource *source,
  294. GSourceFunc func,
  295. gpointer data,
  296. GDestroyNotify notify);
  297. void g_source_set_funcs (GSource *source,
  298. GSourceFuncs *funcs);
  299. gboolean g_source_is_destroyed (GSource *source);
  300. void g_source_set_name (GSource *source,
  301. const char *name);
  302. G_CONST_RETURN char* g_source_get_name (GSource *source);
  303. void g_source_set_name_by_id (guint tag,
  304. const char *name);
  305. /* Used to implement g_source_connect_closure and internally*/
  306. void g_source_set_callback_indirect (GSource *source,
  307. gpointer callback_data,
  308. GSourceCallbackFuncs *callback_funcs);
  309. void g_source_add_poll (GSource *source,
  310. GPollFD *fd);
  311. void g_source_remove_poll (GSource *source,
  312. GPollFD *fd);
  313. void g_source_get_current_time (GSource *source,
  314. GTimeVal *timeval);
  315. /* void g_source_connect_closure (GSource *source,
  316. GClosure *closure);
  317. */
  318. /* Specific source types
  319. */
  320. GSource *g_idle_source_new (void);
  321. GSource *g_child_watch_source_new (GPid pid);
  322. GSource *g_timeout_source_new (guint interval);
  323. GSource *g_timeout_source_new_seconds (guint interval);
  324. /* Miscellaneous functions
  325. */
  326. void g_get_current_time (GTimeVal *result);
  327. /* ============== Compat main loop stuff ================== */
  328. #ifndef G_DISABLE_DEPRECATED
  329. /**
  330. * g_main_new:
  331. * @is_running: set to %TRUE to indicate that the loop is running. This
  332. * is not very important since calling g_main_run() will set this
  333. * to %TRUE anyway.
  334. *
  335. * Creates a new #GMainLoop for th default main context.
  336. *
  337. * Returns: a new #GMainLoop
  338. *
  339. * Deprecated: 2.2: Use g_main_loop_new() instead
  340. */
  341. #define g_main_new(is_running) g_main_loop_new (NULL, is_running)
  342. /**
  343. * g_main_run:
  344. * @loop: a #GMainLoop
  345. *
  346. * Runs a main loop until it stops running.
  347. *
  348. * Deprecated: 2.2: Use g_main_loop_run() instead
  349. */
  350. #define g_main_run(loop) g_main_loop_run(loop)
  351. /**
  352. * g_main_quit:
  353. * @loop: a #GMainLoop
  354. *
  355. * Stops the #GMainLoop.
  356. * If g_main_run() was called to run the #GMainLoop, it will now return.
  357. *
  358. * Deprecated: 2.2: Use g_main_loop_quit() instead
  359. */
  360. #define g_main_quit(loop) g_main_loop_quit(loop)
  361. /**
  362. * g_main_destroy:
  363. * @loop: a #GMainLoop
  364. *
  365. * Frees the memory allocated for the #GMainLoop.
  366. *
  367. * Deprecated: 2.2: Use g_main_loop_unref() instead
  368. */
  369. #define g_main_destroy(loop) g_main_loop_unref(loop)
  370. /**
  371. * g_main_is_running:
  372. * @loop: a #GMainLoop
  373. *
  374. * Checks if the main loop is running.
  375. *
  376. * Returns: %TRUE if the main loop is running
  377. *
  378. * Deprecated: 2.2: Use g_main_loop_is_running() instead
  379. */
  380. #define g_main_is_running(loop) g_main_loop_is_running(loop)
  381. /**
  382. * g_main_iteration:
  383. * @may_block: set to %TRUE if it should block (i.e. wait) until an event
  384. * source becomes ready. It will return after an event source has been
  385. * processed. If set to %FALSE it will return immediately if no event
  386. * source is ready to be processed.
  387. *
  388. * Runs a single iteration for the default #GMainContext.
  389. *
  390. * Returns: %TRUE if more events are pending.
  391. *
  392. * Deprecated: 2.2: Use g_main_context_iteration() instead.
  393. */
  394. #define g_main_iteration(may_block) g_main_context_iteration (NULL, may_block)
  395. /**
  396. * g_main_pending:
  397. *
  398. * Checks if any events are pending for the default #GMainContext
  399. * (i.e. ready to be processed).
  400. *
  401. * Returns: %TRUE if any events are pending.
  402. *
  403. * Deprected: 2.2: Use g_main_context_pending() instead.
  404. */
  405. #define g_main_pending() g_main_context_pending (NULL)
  406. /**
  407. * g_main_set_poll_func:
  408. * @func: the function to call to poll all file descriptors
  409. *
  410. * Sets the function to use for the handle polling of file descriptors
  411. * for the default main context.
  412. *
  413. * Deprecated: 2.2: Use g_main_context_set_poll_func() again
  414. */
  415. #define g_main_set_poll_func(func) g_main_context_set_poll_func (NULL, func)
  416. #endif /* G_DISABLE_DEPRECATED */
  417. /* Source manipulation by ID */
  418. gboolean g_source_remove (guint tag);
  419. gboolean g_source_remove_by_user_data (gpointer user_data);
  420. gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
  421. gpointer user_data);
  422. /* Idles, child watchers and timeouts */
  423. guint g_timeout_add_full (gint priority,
  424. guint interval,
  425. GSourceFunc function,
  426. gpointer data,
  427. GDestroyNotify notify);
  428. guint g_timeout_add (guint interval,
  429. GSourceFunc function,
  430. gpointer data);
  431. guint g_timeout_add_seconds_full (gint priority,
  432. guint interval,
  433. GSourceFunc function,
  434. gpointer data,
  435. GDestroyNotify notify);
  436. guint g_timeout_add_seconds (guint interval,
  437. GSourceFunc function,
  438. gpointer data);
  439. guint g_child_watch_add_full (gint priority,
  440. GPid pid,
  441. GChildWatchFunc function,
  442. gpointer data,
  443. GDestroyNotify notify);
  444. guint g_child_watch_add (GPid pid,
  445. GChildWatchFunc function,
  446. gpointer data);
  447. guint g_idle_add (GSourceFunc function,
  448. gpointer data);
  449. guint g_idle_add_full (gint priority,
  450. GSourceFunc function,
  451. gpointer data,
  452. GDestroyNotify notify);
  453. gboolean g_idle_remove_by_data (gpointer data);
  454. /* Hook for GClosure / GSource integration. Don't touch */
  455. GLIB_VAR GSourceFuncs g_timeout_funcs;
  456. GLIB_VAR GSourceFuncs g_child_watch_funcs;
  457. GLIB_VAR GSourceFuncs g_idle_funcs;
  458. G_END_DECLS
  459. #endif /* __G_MAIN_H__ */