1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 /* 25 * Conversion parameters: 26 * inFile = 27 * outPack = glib 28 * outFile = MainContext 29 * strct = GMainContext 30 * realStrct= 31 * ctorStrct= 32 * clss = MainContext 33 * interf = 34 * class Code: No 35 * interface Code: No 36 * template for: 37 * extend = 38 * implements: 39 * prefixes: 40 * - g_main_context_ 41 * omit structs: 42 * omit prefixes: 43 * omit code: 44 * omit signals: 45 * imports: 46 * - glib.Source 47 * - gthread.Cond 48 * - gthread.Mutex 49 * structWrap: 50 * - GCond* -> Cond 51 * - GMainContext* -> MainContext 52 * - GMutex* -> Mutex 53 * - GSource* -> Source 54 * module aliases: 55 * local aliases: 56 * overrides: 57 */ 58 59 module glib.MainContext; 60 61 public import gtkc.glibtypes; 62 63 private import gtkc.glib; 64 private import glib.ConstructionException; 65 66 private import glib.Source; 67 private import gthread.Cond; 68 private import gthread.Mutex; 69 70 71 72 /** 73 * The main event loop manages all the available sources of events for 74 * GLib and GTK+ applications. These events can come from any number of 75 * different types of sources such as file descriptors (plain files, 76 * pipes or sockets) and timeouts. New types of event sources can also 77 * be added using g_source_attach(). 78 * 79 * To allow multiple independent sets of sources to be handled in 80 * different threads, each source is associated with a GMainContext. 81 * A GMainContext can only be running in a single thread, but 82 * sources can be added to it and removed from it from other threads. 83 * 84 * Each event source is assigned a priority. The default priority, 85 * G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher priorities. 86 * Values greater than 0 denote lower priorities. Events from high priority 87 * sources are always processed before events from lower priority sources. 88 * 89 * Idle functions can also be added, and assigned a priority. These will 90 * be run whenever no events with a higher priority are ready to be processed. 91 * 92 * The GMainLoop data type represents a main event loop. A GMainLoop is 93 * created with g_main_loop_new(). After adding the initial event sources, 94 * g_main_loop_run() is called. This continuously checks for new events from 95 * each of the event sources and dispatches them. Finally, the processing of 96 * an event from one of the sources leads to a call to g_main_loop_quit() to 97 * exit the main loop, and g_main_loop_run() returns. 98 * 99 * It is possible to create new instances of GMainLoop recursively. 100 * This is often used in GTK+ applications when showing modal dialog 101 * boxes. Note that event sources are associated with a particular 102 * GMainContext, and will be checked and dispatched for all main 103 * loops associated with that GMainContext. 104 * 105 * GTK+ contains wrappers of some of these functions, e.g. gtk_main(), 106 * gtk_main_quit() and gtk_events_pending(). 107 * 108 * Creating new source types 109 * 110 * One of the unusual features of the GMainLoop functionality 111 * is that new types of event source can be created and used in 112 * addition to the builtin type of event source. A new event source 113 * type is used for handling GDK events. A new source type is created 114 * by deriving from the GSource structure. 115 * The derived type of source is represented by a structure that has 116 * the GSource structure as a first element, and other elements specific 117 * to the new source type. To create an instance of the new source type, 118 * call g_source_new() passing in the size of the derived structure and 119 * a table of functions. These GSourceFuncs determine the behavior of 120 * the new source type. 121 * 122 * New source types basically interact with the main context 123 * in two ways. Their prepare function in GSourceFuncs can set a timeout 124 * to determine the maximum amount of time that the main loop will sleep 125 * before checking the source again. In addition, or as well, the source 126 * can add file descriptors to the set that the main context checks using 127 * g_source_add_poll(). 128 * 129 * <hr> 130 * 131 * Customizing the main loop iteration 132 * 133 * Single iterations of a GMainContext can be run with 134 * g_main_context_iteration(). In some cases, more detailed control 135 * of exactly how the details of the main loop work is desired, for 136 * instance, when integrating the GMainLoop with an external main loop. 137 * In such cases, you can call the component functions of 138 * g_main_context_iteration() directly. These functions are 139 * g_main_context_prepare(), g_main_context_query(), 140 * g_main_context_check() and g_main_context_dispatch(). 141 * 142 * The operation of these functions can best be seen in terms 143 * of a state diagram, as shown in Figure 1, “States of a Main Context”. 144 * 145 * Figure 1. States of a Main Context 146 * 147 * On Unix, the GLib mainloop is incompatible with fork(). Any program 148 * using the mainloop must either exec() or exit() from the child 149 * without returning to the mainloop. 150 */ 151 public class MainContext 152 { 153 154 /** the main Gtk struct */ 155 protected GMainContext* gMainContext; 156 157 158 /** Get the main Gtk struct */ 159 public GMainContext* getMainContextStruct() 160 { 161 return gMainContext; 162 } 163 164 165 /** the main Gtk struct as a void* */ 166 protected void* getStruct() 167 { 168 return cast(void*)gMainContext; 169 } 170 171 /** 172 * Sets our main struct and passes it to the parent class 173 */ 174 public this (GMainContext* gMainContext) 175 { 176 this.gMainContext = gMainContext; 177 } 178 179 /** 180 */ 181 182 /** 183 * Creates a new GMainContext structure. 184 * Throws: ConstructionException GTK+ fails to create the object. 185 */ 186 public this () 187 { 188 // GMainContext * g_main_context_new (void); 189 auto p = g_main_context_new(); 190 if(p is null) 191 { 192 throw new ConstructionException("null returned by g_main_context_new()"); 193 } 194 this(cast(GMainContext*) p); 195 } 196 197 /** 198 * Increases the reference count on a GMainContext object by one. 199 * Returns: the context that was passed in (since 2.6) 200 */ 201 public MainContext doref() 202 { 203 // GMainContext * g_main_context_ref (GMainContext *context); 204 auto p = g_main_context_ref(gMainContext); 205 206 if(p is null) 207 { 208 return null; 209 } 210 211 return new MainContext(cast(GMainContext*) p); 212 } 213 214 /** 215 * Decreases the reference count on a GMainContext object by one. If 216 * the result is zero, free the context and free all associated memory. 217 */ 218 public void unref() 219 { 220 // void g_main_context_unref (GMainContext *context); 221 g_main_context_unref(gMainContext); 222 } 223 224 /** 225 * Returns the global default main context. This is the main context 226 * used for main loop functions when a main loop is not explicitly 227 * specified, and corresponds to the "main" main loop. See also 228 * g_main_context_get_thread_default(). 229 * Returns: the global default main context. [transfer none] 230 */ 231 public static MainContext defaulx() 232 { 233 // GMainContext * g_main_context_default (void); 234 auto p = g_main_context_default(); 235 236 if(p is null) 237 { 238 return null; 239 } 240 241 return new MainContext(cast(GMainContext*) p); 242 } 243 244 /** 245 * Runs a single iteration for the given main loop. This involves 246 * checking to see if any event sources are ready to be processed, 247 * then if no events sources are ready and may_block is TRUE, waiting 248 * for a source to become ready, then dispatching the highest priority 249 * events sources that are ready. Otherwise, if may_block is FALSE 250 * sources are not waited to become ready, only those highest priority 251 * events sources will be dispatched (if any), that are ready at this 252 * given moment without further waiting. 253 * Note that even when may_block is TRUE, it is still possible for 254 * g_main_context_iteration() to return FALSE, since the wait may 255 * be interrupted for other reasons than an event source becoming ready. 256 * Params: 257 * mayBlock = whether the call may block. 258 * Returns: TRUE if events were dispatched. 259 */ 260 public int iteration(int mayBlock) 261 { 262 // gboolean g_main_context_iteration (GMainContext *context, gboolean may_block); 263 return g_main_context_iteration(gMainContext, mayBlock); 264 } 265 266 /** 267 * Checks if any sources have pending events for the given context. 268 * Returns: TRUE if events are pending. 269 */ 270 public int pending() 271 { 272 // gboolean g_main_context_pending (GMainContext *context); 273 return g_main_context_pending(gMainContext); 274 } 275 276 /** 277 * Finds a GSource given a pair of context and ID. 278 * Params: 279 * sourceId = the source ID, as returned by g_source_get_id(). 280 * Returns: the GSource if found, otherwise, NULL. [transfer none] 281 */ 282 public Source findSourceById(uint sourceId) 283 { 284 // GSource * g_main_context_find_source_by_id (GMainContext *context, guint source_id); 285 auto p = g_main_context_find_source_by_id(gMainContext, sourceId); 286 287 if(p is null) 288 { 289 return null; 290 } 291 292 return new Source(cast(GSource*) p); 293 } 294 295 /** 296 * Finds a source with the given user data for the callback. If 297 * multiple sources exist with the same user data, the first 298 * one found will be returned. 299 * Params: 300 * userData = the user_data for the callback. 301 * Returns: the source, if one was found, otherwise NULL. [transfer none] 302 */ 303 public Source findSourceByUserData(void* userData) 304 { 305 // GSource * g_main_context_find_source_by_user_data (GMainContext *context, gpointer user_data); 306 auto p = g_main_context_find_source_by_user_data(gMainContext, userData); 307 308 if(p is null) 309 { 310 return null; 311 } 312 313 return new Source(cast(GSource*) p); 314 } 315 316 /** 317 * Finds a source with the given source functions and user data. If 318 * multiple sources exist with the same source function and user data, 319 * the first one found will be returned. 320 * Params: 321 * funcs = the source_funcs passed to g_source_new(). 322 * userData = the user data from the callback. 323 * Returns: the source, if one was found, otherwise NULL. [transfer none] 324 */ 325 public Source findSourceByFuncsUserData(GSourceFuncs* funcs, void* userData) 326 { 327 // GSource * g_main_context_find_source_by_funcs_user_data (GMainContext *context, GSourceFuncs *funcs, gpointer user_data); 328 auto p = g_main_context_find_source_by_funcs_user_data(gMainContext, funcs, userData); 329 330 if(p is null) 331 { 332 return null; 333 } 334 335 return new Source(cast(GSource*) p); 336 } 337 338 /** 339 * If context is currently blocking in g_main_context_iteration() 340 * waiting for a source to become ready, cause it to stop blocking 341 * and return. Otherwise, cause the next invocation of 342 * g_main_context_iteration() to return without blocking. 343 * This API is useful for low-level control over GMainContext; for 344 * example, integrating it with main loop implementations such as 345 * GMainLoop. 346 * Another related use for this function is when implementing a main 347 */ 348 public void wakeup() 349 { 350 // void g_main_context_wakeup (GMainContext *context); 351 g_main_context_wakeup(gMainContext); 352 } 353 354 /** 355 * Tries to become the owner of the specified context. 356 * If some other thread is the owner of the context, 357 * returns FALSE immediately. Ownership is properly 358 * recursive: the owner can require ownership again 359 * and will release ownership when g_main_context_release() 360 * is called as many times as g_main_context_acquire(). 361 * You must be the owner of a context before you 362 * can call g_main_context_prepare(), g_main_context_query(), 363 * g_main_context_check(), g_main_context_dispatch(). 364 * Returns: TRUE if the operation succeeded, and this thread is now the owner of context. 365 */ 366 public int acquire() 367 { 368 // gboolean g_main_context_acquire (GMainContext *context); 369 return g_main_context_acquire(gMainContext); 370 } 371 372 /** 373 * Releases ownership of a context previously acquired by this thread 374 * with g_main_context_acquire(). If the context was acquired multiple 375 * times, the ownership will be released only when g_main_context_release() 376 * is called as many times as it was acquired. 377 */ 378 public void release() 379 { 380 // void g_main_context_release (GMainContext *context); 381 g_main_context_release(gMainContext); 382 } 383 384 /** 385 * Determines whether this thread holds the (recursive) 386 * ownership of this GMainContext. This is useful to 387 * know before waiting on another thread that may be 388 * blocking to get ownership of context. 389 * Since 2.10 390 * Returns: TRUE if current thread is owner of context. 391 */ 392 public int isOwner() 393 { 394 // gboolean g_main_context_is_owner (GMainContext *context); 395 return g_main_context_is_owner(gMainContext); 396 } 397 398 /** 399 * Tries to become the owner of the specified context, 400 * as with g_main_context_acquire(). But if another thread 401 * is the owner, atomically drop mutex and wait on cond until 402 * that owner releases ownership or until cond is signaled, then 403 * try again (once) to become the owner. 404 * Params: 405 * cond = a condition variable 406 * mutex = a mutex, currently held 407 * Returns: TRUE if the operation succeeded, and this thread is now the owner of context. 408 */ 409 public int wait(Cond cond, Mutex mutex) 410 { 411 // gboolean g_main_context_wait (GMainContext *context, GCond *cond, GMutex *mutex); 412 return g_main_context_wait(gMainContext, (cond is null) ? null : cond.getCondStruct(), (mutex is null) ? null : mutex.getMutexStruct()); 413 } 414 415 /** 416 * Prepares to poll sources within a main loop. The resulting information 417 * for polling is determined by calling g_main_context_query(). 418 * Params: 419 * priority = location to store priority of highest priority 420 * source already ready. 421 * Returns: TRUE if some source is ready to be dispatched prior to polling. 422 */ 423 public int prepare(out int priority) 424 { 425 // gboolean g_main_context_prepare (GMainContext *context, gint *priority); 426 return g_main_context_prepare(gMainContext, &priority); 427 } 428 429 /** 430 * Determines information necessary to poll this main loop. 431 * Params: 432 * maxPriority = maximum priority source to check 433 * timeout = location to store timeout to be used in polling. [out] 434 * fds = location to 435 * store GPollFD records that need to be polled. [out caller-allocates][array length=n_fds] 436 * Returns: the number of records actually stored in fds, or, if more than n_fds records need to be stored, the number of records that need to be stored. 437 */ 438 public int query(int maxPriority, out int timeout, GPollFD[] fds) 439 { 440 // gint g_main_context_query (GMainContext *context, gint max_priority, gint *timeout_, GPollFD *fds, gint n_fds); 441 return g_main_context_query(gMainContext, maxPriority, &timeout, fds.ptr, cast(int) fds.length); 442 } 443 444 /** 445 * Passes the results of polling back to the main loop. 446 * Params: 447 * maxPriority = the maximum numerical priority of sources to check 448 * fds = array of GPollFD's that was passed to 449 * the last call to g_main_context_query(). [array length=n_fds] 450 * Returns: TRUE if some sources are ready to be dispatched. 451 */ 452 public int check(int maxPriority, GPollFD[] fds) 453 { 454 // gint g_main_context_check (GMainContext *context, gint max_priority, GPollFD *fds, gint n_fds); 455 return g_main_context_check(gMainContext, maxPriority, fds.ptr, cast(int) fds.length); 456 } 457 458 /** 459 * Dispatches all pending sources. 460 */ 461 public void dispatch() 462 { 463 // void g_main_context_dispatch (GMainContext *context); 464 g_main_context_dispatch(gMainContext); 465 } 466 467 /** 468 * Sets the function to use to handle polling of file descriptors. It 469 * will be used instead of the poll() system call 470 * (or GLib's replacement function, which is used where 471 * poll() isn't available). 472 * This function could possibly be used to integrate the GLib event 473 * loop with an external event loop. 474 * Params: 475 * func = the function to call to poll all file descriptors 476 */ 477 public void setPollFunc(GPollFunc func) 478 { 479 // void g_main_context_set_poll_func (GMainContext *context, GPollFunc func); 480 g_main_context_set_poll_func(gMainContext, func); 481 } 482 483 /** 484 * Gets the poll function set by g_main_context_set_poll_func(). 485 * Returns: the poll function 486 */ 487 public GPollFunc getPollFunc() 488 { 489 // GPollFunc g_main_context_get_poll_func (GMainContext *context); 490 return g_main_context_get_poll_func(gMainContext); 491 } 492 493 /** 494 * Adds a file descriptor to the set of file descriptors polled for 495 * this context. This will very seldom be used directly. Instead 496 * a typical event source will use g_source_add_unix_fd() instead. 497 * Params: 498 * fd = a GPollFD structure holding information about a file 499 * descriptor to watch. 500 * priority = the priority for this file descriptor which should be 501 * the same as the priority used for g_source_attach() to ensure that the 502 * file descriptor is polled whenever the results may be needed. 503 */ 504 public void addPoll(ref GPollFD fd, int priority) 505 { 506 // void g_main_context_add_poll (GMainContext *context, GPollFD *fd, gint priority); 507 g_main_context_add_poll(gMainContext, &fd, priority); 508 } 509 510 /** 511 * Removes file descriptor from the set of file descriptors to be 512 * polled for a particular context. 513 * Params: 514 * fd = a GPollFD descriptor previously added with g_main_context_add_poll() 515 */ 516 public void removePoll(ref GPollFD fd) 517 { 518 // void g_main_context_remove_poll (GMainContext *context, GPollFD *fd); 519 g_main_context_remove_poll(gMainContext, &fd); 520 } 521 522 /** 523 * Invokes a function in such a way that context is owned during the 524 * invocation of function. 525 * If context is NULL then the global default main context — as 526 * returned by g_main_context_default() — is used. 527 * If context is owned by the current thread, function is called 528 * directly. Otherwise, if context is the thread-default main context 529 * of the current thread and g_main_context_acquire() succeeds, then 530 * function is called and g_main_context_release() is called 531 * afterwards. 532 * In any other case, an idle source is created to call function and 533 * that source is attached to context (presumably to be run in another 534 * thread). The idle source is attached with G_PRIORITY_DEFAULT 535 * priority. If you want a different priority, use 536 * g_main_context_invoke_full(). 537 * Note that, as with normal idle functions, function should probably 538 * return FALSE. If it returns TRUE, it will be continuously run in a 539 * loop (and may prevent this call from returning). 540 * Since 2.28 541 * Params: 542 * data = data to pass to function 543 */ 544 public void invoke(GSourceFunc funct, void* data) 545 { 546 // void g_main_context_invoke (GMainContext *context, GSourceFunc function, gpointer data); 547 g_main_context_invoke(gMainContext, funct, data); 548 } 549 550 /** 551 * Invokes a function in such a way that context is owned during the 552 * invocation of function. 553 * This function is the same as g_main_context_invoke() except that it 554 * lets you specify the priority incase function ends up being 555 * scheduled as an idle and also lets you give a GDestroyNotify for data. 556 * notify should not assume that it is called from any particular 557 * thread or with any particular context acquired. 558 * Since 2.28 559 * Params: 560 * priority = the priority at which to run function 561 * data = data to pass to function 562 * notify = a function to call when data is no longer in use, or NULL. [allow-none] 563 */ 564 public void invokeFull(int priority, GSourceFunc funct, void* data, GDestroyNotify notify) 565 { 566 // void g_main_context_invoke_full (GMainContext *context, gint priority, GSourceFunc function, gpointer data, GDestroyNotify notify); 567 g_main_context_invoke_full(gMainContext, priority, funct, data, notify); 568 } 569 570 /** 571 * Gets the thread-default GMainContext for this thread. Asynchronous 572 * operations that want to be able to be run in contexts other than 573 * the default one should call this method or 574 * g_main_context_ref_thread_default() to get a GMainContext to add 575 * their GSources to. (Note that even in single-threaded 576 * programs applications may sometimes want to temporarily push a 577 * non-default context, so it is not safe to assume that this will 578 * always return NULL if you are running in the default thread.) 579 * If you need to hold a reference on the context, use 580 * g_main_context_ref_thread_default() instead. 581 * Since 2.22 582 * Returns: the thread-default GMainContext, or NULL if the thread-default context is the global default context. [transfer none] 583 */ 584 public static MainContext getThreadDefault() 585 { 586 // GMainContext * g_main_context_get_thread_default (void); 587 auto p = g_main_context_get_thread_default(); 588 589 if(p is null) 590 { 591 return null; 592 } 593 594 return new MainContext(cast(GMainContext*) p); 595 } 596 597 /** 598 * Gets the thread-default GMainContext for this thread, as with 599 * g_main_context_get_thread_default(), but also adds a reference to 600 * it with g_main_context_ref(). In addition, unlike 601 * g_main_context_get_thread_default(), if the thread-default context 602 * is the global default context, this will return that GMainContext 603 * (with a ref added to it) rather than returning NULL. 604 * Since 2.32 605 * Returns: the thread-default GMainContext. Unref with g_main_context_unref() when you are done with it. [transfer full] 606 */ 607 public static MainContext refThreadDefault() 608 { 609 // GMainContext * g_main_context_ref_thread_default (void); 610 auto p = g_main_context_ref_thread_default(); 611 612 if(p is null) 613 { 614 return null; 615 } 616 617 return new MainContext(cast(GMainContext*) p); 618 } 619 620 /** 621 * Acquires context and sets it as the thread-default context for the 622 * current thread. This will cause certain asynchronous operations 623 * (such as most gio-based I/O) which are 624 * started in this thread to run under context and deliver their 625 * results to its main loop, rather than running under the global 626 * default context in the main thread. Note that calling this function 627 * changes the context returned by 628 * g_main_context_get_thread_default(), not the 629 * one returned by g_main_context_default(), so it does not affect the 630 * context used by functions like g_idle_add(). 631 * Normally you would call this function shortly after creating a new 632 * thread, passing it a GMainContext which will be run by a 633 * GMainLoop in that thread, to set a new default context for all 634 * async operations in that thread. (In this case, you don't need to 635 * ever call g_main_context_pop_thread_default().) In some cases 636 * however, you may want to schedule a single operation in a 637 * non-default context, or temporarily use a non-default context in 638 * the main thread. In that case, you can wrap the call to the 639 * asynchronous operation inside a 640 * g_main_context_push_thread_default() / 641 * g_main_context_pop_thread_default() pair, but it is up to you to 642 * ensure that no other asynchronous operations accidentally get 643 * started while the non-default context is active. 644 * Beware that libraries that predate this function may not correctly 645 * handle being used from a thread with a thread-default context. Eg, 646 * see g_file_supports_thread_contexts(). 647 * Since 2.22 648 */ 649 public void pushThreadDefault() 650 { 651 // void g_main_context_push_thread_default (GMainContext *context); 652 g_main_context_push_thread_default(gMainContext); 653 } 654 655 /** 656 * Pops context off the thread-default context stack (verifying that 657 * it was on the top of the stack). 658 * Since 2.22 659 */ 660 public void popThreadDefault() 661 { 662 // void g_main_context_pop_thread_default (GMainContext *context); 663 g_main_context_pop_thread_default(gMainContext); 664 } 665 }