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