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 = Source 29 * strct = GSource 30 * realStrct= 31 * ctorStrct= 32 * clss = Source 33 * interf = 34 * class Code: No 35 * interface Code: No 36 * template for: 37 * extend = 38 * implements: 39 * prefixes: 40 * - g_source_ 41 * omit structs: 42 * omit prefixes: 43 * omit code: 44 * omit signals: 45 * imports: 46 * - glib.Str 47 * - glib.MainContext 48 * structWrap: 49 * - GMainContext* -> MainContext 50 * - GSource* -> Source 51 * module aliases: 52 * local aliases: 53 * overrides: 54 */ 55 56 module glib.Source; 57 58 public import gtkc.glibtypes; 59 60 private import gtkc.glib; 61 private import glib.ConstructionException; 62 63 64 private import glib.Str; 65 private import glib.MainContext; 66 67 68 69 70 /** 71 * The main event loop manages all the available sources of events for 72 * GLib and GTK+ applications. These events can come from any number of 73 * different types of sources such as file descriptors (plain files, 74 * pipes or sockets) and timeouts. New types of event sources can also 75 * be added using g_source_attach(). 76 * 77 * To allow multiple independent sets of sources to be handled in 78 * different threads, each source is associated with a GMainContext. 79 * A GMainContext can only be running in a single thread, but 80 * sources can be added to it and removed from it from other threads. 81 * 82 * Each event source is assigned a priority. The default priority, 83 * G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher priorities. 84 * Values greater than 0 denote lower priorities. Events from high priority 85 * sources are always processed before events from lower priority sources. 86 * 87 * Idle functions can also be added, and assigned a priority. These will 88 * be run whenever no events with a higher priority are ready to be processed. 89 * 90 * The GMainLoop data type represents a main event loop. A GMainLoop is 91 * created with g_main_loop_new(). After adding the initial event sources, 92 * g_main_loop_run() is called. This continuously checks for new events from 93 * each of the event sources and dispatches them. Finally, the processing of 94 * an event from one of the sources leads to a call to g_main_loop_quit() to 95 * exit the main loop, and g_main_loop_run() returns. 96 * 97 * It is possible to create new instances of GMainLoop recursively. 98 * This is often used in GTK+ applications when showing modal dialog 99 * boxes. Note that event sources are associated with a particular 100 * GMainContext, and will be checked and dispatched for all main 101 * loops associated with that GMainContext. 102 * 103 * GTK+ contains wrappers of some of these functions, e.g. gtk_main(), 104 * gtk_main_quit() and gtk_events_pending(). 105 * 106 * Creating new source types 107 * 108 * One of the unusual features of the GMainLoop functionality 109 * is that new types of event source can be created and used in 110 * addition to the builtin type of event source. A new event source 111 * type is used for handling GDK events. A new source type is created 112 * by deriving from the GSource structure. 113 * The derived type of source is represented by a structure that has 114 * the GSource structure as a first element, and other elements specific 115 * to the new source type. To create an instance of the new source type, 116 * call g_source_new() passing in the size of the derived structure and 117 * a table of functions. These GSourceFuncs determine the behavior of 118 * the new source type. 119 * 120 * New source types basically interact with the main context 121 * in two ways. Their prepare function in GSourceFuncs can set a timeout 122 * to determine the maximum amount of time that the main loop will sleep 123 * before checking the source again. In addition, or as well, the source 124 * can add file descriptors to the set that the main context checks using 125 * g_source_add_poll(). 126 * 127 * <hr> 128 * 129 * Customizing the main loop iteration 130 * 131 * Single iterations of a GMainContext can be run with 132 * g_main_context_iteration(). In some cases, more detailed control 133 * of exactly how the details of the main loop work is desired, for 134 * instance, when integrating the GMainLoop with an external main loop. 135 * In such cases, you can call the component functions of 136 * g_main_context_iteration() directly. These functions are 137 * g_main_context_prepare(), g_main_context_query(), 138 * g_main_context_check() and g_main_context_dispatch(). 139 * 140 * The operation of these functions can best be seen in terms 141 * of a state diagram, as shown in Figure 1, “States of a Main Context”. 142 * 143 * Figure 1. States of a Main Context 144 * 145 * On Unix, the GLib mainloop is incompatible with fork(). Any program 146 * using the mainloop must either exec() or exit() from the child 147 * without returning to the mainloop. 148 */ 149 public class Source 150 { 151 152 /** the main Gtk struct */ 153 protected GSource* gSource; 154 155 156 public GSource* getSourceStruct() 157 { 158 return gSource; 159 } 160 161 162 /** the main Gtk struct as a void* */ 163 protected void* getStruct() 164 { 165 return cast(void*)gSource; 166 } 167 168 /** 169 * Sets our main struct and passes it to the parent class 170 */ 171 public this (GSource* gSource) 172 { 173 this.gSource = gSource; 174 } 175 176 /** 177 */ 178 179 /** 180 * Creates a new GSource structure. The size is specified to 181 * allow creating structures derived from GSource that contain 182 * additional data. The size passed in must be at least 183 * sizeof (GSource). 184 * The source will not initially be associated with any GMainContext 185 * and must be added to one with g_source_attach() before it will be 186 * executed. 187 * Params: 188 * sourceFuncs = structure containing functions that implement 189 * the sources behavior. 190 * structSize = size of the GSource structure to create. 191 * Throws: ConstructionException GTK+ fails to create the object. 192 */ 193 public this (GSourceFuncs* sourceFuncs, uint structSize) 194 { 195 // GSource * g_source_new (GSourceFuncs *source_funcs, guint struct_size); 196 auto p = g_source_new(sourceFuncs, structSize); 197 if(p is null) 198 { 199 throw new ConstructionException("null returned by g_source_new(sourceFuncs, structSize)"); 200 } 201 this(cast(GSource*) p); 202 } 203 204 /** 205 * Increases the reference count on a source by one. 206 * Returns: source 207 */ 208 public Source doref() 209 { 210 // GSource * g_source_ref (GSource *source); 211 auto p = g_source_ref(gSource); 212 213 if(p is null) 214 { 215 return null; 216 } 217 218 return new Source(cast(GSource*) p); 219 } 220 221 /** 222 * Decreases the reference count of a source by one. If the 223 * resulting reference count is zero the source and associated 224 * memory will be destroyed. 225 */ 226 public void unref() 227 { 228 // void g_source_unref (GSource *source); 229 g_source_unref(gSource); 230 } 231 232 /** 233 * Sets the source functions (can be used to override 234 * default implementations) of an unattached source. 235 * Since 2.12 236 * Params: 237 * funcs = the new GSourceFuncs 238 */ 239 public void setFuncs(GSourceFuncs* funcs) 240 { 241 // void g_source_set_funcs (GSource *source, GSourceFuncs *funcs); 242 g_source_set_funcs(gSource, funcs); 243 } 244 245 /** 246 * Adds a GSource to a context so that it will be executed within 247 * that context. Remove it by calling g_source_destroy(). 248 * Params: 249 * context = a GMainContext (if NULL, the default context will be used). [allow-none] 250 * Returns: the ID (greater than 0) for the source within the GMainContext. 251 */ 252 public uint attach(MainContext context) 253 { 254 // guint g_source_attach (GSource *source, GMainContext *context); 255 return g_source_attach(gSource, (context is null) ? null : context.getMainContextStruct()); 256 } 257 258 /** 259 * Removes a source from its GMainContext, if any, and mark it as 260 * destroyed. The source cannot be subsequently added to another 261 * context. 262 */ 263 public void destroy() 264 { 265 // void g_source_destroy (GSource *source); 266 g_source_destroy(gSource); 267 } 268 269 /** 270 * Returns whether source has been destroyed. 271 * This is important when you operate upon your objects 272 * from within idle handlers, but may have freed the object 273 * before the dispatch of your idle handler. 274 * $(DDOC_COMMENT example) 275 * This will fail in a multi-threaded application if the 276 * widget is destroyed before the idle handler fires due 277 * to the use after free in the callback. A solution, to 278 * this particular problem, is to check to if the source 279 * has already been destroy within the callback. 280 * $(DDOC_COMMENT example) 281 * Since 2.12 282 * Returns: TRUE if the source has been destroyed 283 */ 284 public int isDestroyed() 285 { 286 // gboolean g_source_is_destroyed (GSource *source); 287 return g_source_is_destroyed(gSource); 288 } 289 290 /** 291 * Sets the priority of a source. While the main loop is being run, a 292 * source will be dispatched if it is ready to be dispatched and no 293 * sources at a higher (numerically smaller) priority are ready to be 294 * dispatched. 295 * Params: 296 * priority = the new priority. 297 */ 298 public void setPriority(int priority) 299 { 300 // void g_source_set_priority (GSource *source, gint priority); 301 g_source_set_priority(gSource, priority); 302 } 303 304 /** 305 * Gets the priority of a source. 306 * Returns: the priority of the source 307 */ 308 public int getPriority() 309 { 310 // gint g_source_get_priority (GSource *source); 311 return g_source_get_priority(gSource); 312 } 313 314 /** 315 * Sets whether a source can be called recursively. If can_recurse is 316 * TRUE, then while the source is being dispatched then this source 317 * will be processed normally. Otherwise, all processing of this 318 * source is blocked until the dispatch function returns. 319 * Params: 320 * canRecurse = whether recursion is allowed for this source 321 */ 322 public void setCanRecurse(int canRecurse) 323 { 324 // void g_source_set_can_recurse (GSource *source, gboolean can_recurse); 325 g_source_set_can_recurse(gSource, canRecurse); 326 } 327 328 /** 329 * Checks whether a source is allowed to be called recursively. 330 * see g_source_set_can_recurse(). 331 * Returns: whether recursion is allowed. 332 */ 333 public int getCanRecurse() 334 { 335 // gboolean g_source_get_can_recurse (GSource *source); 336 return g_source_get_can_recurse(gSource); 337 } 338 339 /** 340 * Returns the numeric ID for a particular source. The ID of a source 341 * is a positive integer which is unique within a particular main loop 342 * context. The reverse 343 * mapping from ID to source is done by g_main_context_find_source_by_id(). 344 * Returns: the ID (greater than 0) for the source 345 */ 346 public uint getId() 347 { 348 // guint g_source_get_id (GSource *source); 349 return g_source_get_id(gSource); 350 } 351 352 /** 353 * Gets a name for the source, used in debugging and profiling. 354 * The name may be NULL if it has never been set with 355 * g_source_set_name(). 356 * Since 2.26 357 * Returns: the name of the source 358 */ 359 public string getName() 360 { 361 // const char * g_source_get_name (GSource *source); 362 return Str.toString(g_source_get_name(gSource)); 363 } 364 365 /** 366 * Sets a name for the source, used in debugging and profiling. 367 * The name defaults to NULL. 368 * The source name should describe in a human-readable way 369 * what the source does. For example, "X11 event queue" 370 * or "GTK+ repaint idle handler" or whatever it is. 371 * It is permitted to call this function multiple times, but is not 372 * recommended due to the potential performance impact. For example, 373 * one could change the name in the "check" function of a GSourceFuncs 374 * to include details like the event type in the source name. 375 * Since 2.26 376 * Params: 377 * name = debug name for the source 378 */ 379 public void setName(string name) 380 { 381 // void g_source_set_name (GSource *source, const char *name); 382 g_source_set_name(gSource, Str.toStringz(name)); 383 } 384 385 /** 386 * Sets the name of a source using its ID. 387 * This is a convenience utility to set source names from the return 388 * value of g_idle_add(), g_timeout_add(), etc. 389 * Since 2.26 390 * Params: 391 * tag = a GSource ID 392 * name = debug name for the source 393 */ 394 public static void setNameById(uint tag, string name) 395 { 396 // void g_source_set_name_by_id (guint tag, const char *name); 397 g_source_set_name_by_id(tag, Str.toStringz(name)); 398 } 399 400 /** 401 * Gets the GMainContext with which the source is associated. 402 * You can call this on a source that has been destroyed, provided 403 * that the GMainContext it was attached to still exists (in which 404 * case it will return that GMainContext). In particular, you can 405 * always call this function on the source returned from 406 * g_main_current_source(). But calling this function on a source 407 * whose GMainContext has been destroyed is an error. 408 * Returns: the GMainContext with which the source is associated, or NULL if the context has not yet been added to a source. [transfer none][allow-none] 409 */ 410 public MainContext getContext() 411 { 412 // GMainContext * g_source_get_context (GSource *source); 413 auto p = g_source_get_context(gSource); 414 415 if(p is null) 416 { 417 return null; 418 } 419 420 return new MainContext(cast(GMainContext*) p); 421 } 422 423 /** 424 * Sets the callback function for a source. The callback for a source is 425 * called from the source's dispatch function. 426 * The exact type of func depends on the type of source; ie. you 427 * should not count on func being called with data as its first 428 * parameter. 429 * Typically, you won't use this function. Instead use functions specific 430 * to the type of source you are using. 431 * Params: 432 * func = a callback function 433 * data = the data to pass to callback function 434 * notify = a function to call when data is no longer in use, or NULL. [allow-none] 435 */ 436 public void setCallback(GSourceFunc func, void* data, GDestroyNotify notify) 437 { 438 // void g_source_set_callback (GSource *source, GSourceFunc func, gpointer data, GDestroyNotify notify); 439 g_source_set_callback(gSource, func, data, notify); 440 } 441 442 /** 443 * Sets the callback function storing the data as a refcounted callback 444 * "object". This is used internally. Note that calling 445 * g_source_set_callback_indirect() assumes 446 * an initial reference count on callback_data, and thus 447 * callback_funcs->unref will eventually be called once more 448 * than callback_funcs->ref. 449 * Params: 450 * callbackData = pointer to callback data "object" 451 * callbackFuncs = functions for reference counting callback_data 452 * and getting the callback and data 453 */ 454 public void setCallbackIndirect(void* callbackData, GSourceCallbackFuncs* callbackFuncs) 455 { 456 // void g_source_set_callback_indirect (GSource *source, gpointer callback_data, GSourceCallbackFuncs *callback_funcs); 457 g_source_set_callback_indirect(gSource, callbackData, callbackFuncs); 458 } 459 460 /** 461 * Sets a GSource to be dispatched when the given monotonic time is 462 * reached (or passed). If the monotonic time is in the past (as it 463 * always will be if ready_time is 0) then the source will be 464 * dispatched immediately. 465 * If ready_time is -1 then the source is never woken up on the basis 466 * of the passage of time. 467 * Dispatching the source does not reset the ready time. You should do 468 * so yourself, from the source dispatch function. 469 * Note that if you have a pair of sources where the ready time of one 470 * suggests that it will be delivered first but the priority for the 471 * other suggests that it would be delivered first, and the ready time 472 * for both sources is reached during the same main context iteration 473 * then the order of dispatch is undefined. 474 * Since 2.36 475 * Params: 476 * readyTime = the monotonic time at which the source will be ready, 477 * 0 for "immediately", -1 for "never" 478 */ 479 public void setReadyTime(long readyTime) 480 { 481 // void g_source_set_ready_time (GSource *source, gint64 ready_time); 482 g_source_set_ready_time(gSource, readyTime); 483 } 484 485 /** 486 * Gets the "ready time" of source, as set by 487 * g_source_set_ready_time(). 488 * Any time before the current monotonic time (including 0) is an 489 * indication that the source will fire immediately. 490 * Returns: the monotonic ready time, -1 for "never" 491 */ 492 public long getReadyTime() 493 { 494 // gint64 g_source_get_ready_time (GSource *source); 495 return g_source_get_ready_time(gSource); 496 } 497 498 /** 499 * Monitors fd for the IO events in events. 500 * The tag returned by this function can be used to remove or modify the 501 * monitoring of the fd using g_source_remove_unix_fd() or 502 * g_source_modify_unix_fd(). 503 * It is not necessary to remove the fd before destroying the source; it 504 * will be cleaned up automatically. 505 * As the name suggests, this function is not available on Windows. 506 * Since 2.36 507 * Params: 508 * fd = the fd to monitor 509 * events = an event mask 510 * Returns: an opaque tag 511 */ 512 public void* addUnixFd(int fd, GIOCondition events) 513 { 514 // gpointer g_source_add_unix_fd (GSource *source, gint fd, GIOCondition events); 515 return g_source_add_unix_fd(gSource, fd, events); 516 } 517 518 /** 519 * Reverses the effect of a previous call to g_source_add_unix_fd(). 520 * You only need to call this if you want to remove an fd from being 521 * watched while keeping the same source around. In the normal case you 522 * will just want to destroy the source. 523 * As the name suggests, this function is not available on Windows. 524 * Since 2.36 525 * Params: 526 * tag = the tag from g_source_add_unix_fd() 527 */ 528 public void removeUnixFd(void* tag) 529 { 530 // void g_source_remove_unix_fd (GSource *source, gpointer tag); 531 g_source_remove_unix_fd(gSource, tag); 532 } 533 534 /** 535 * Updates the event mask to watch for the fd identified by tag. 536 * tag is the tag returned from g_source_add_unix_fd(). 537 * If you want to remove a fd, don't set its event mask to zero. 538 * Instead, call g_source_remove_unix_fd(). 539 * As the name suggests, this function is not available on Windows. 540 * Since 2.36 541 * Params: 542 * tag = the tag from g_source_add_unix_fd() 543 * newEvents = the new event mask to watch 544 */ 545 public void modifyUnixFd(void* tag, GIOCondition newEvents) 546 { 547 // void g_source_modify_unix_fd (GSource *source, gpointer tag, GIOCondition new_events); 548 g_source_modify_unix_fd(gSource, tag, newEvents); 549 } 550 551 /** 552 * Queries the events reported for the fd corresponding to tag on 553 * source during the last poll. 554 * The return value of this function is only defined when the function 555 * is called from the check or dispatch functions for source. 556 * As the name suggests, this function is not available on Windows. 557 * Since 2.36 558 * Params: 559 * tag = the tag from g_source_add_unix_fd() 560 * Returns: the conditions reported on the fd 561 */ 562 public GIOCondition queryUnixFd(void* tag) 563 { 564 // GIOCondition g_source_query_unix_fd (GSource *source, gpointer tag); 565 return g_source_query_unix_fd(gSource, tag); 566 } 567 568 /** 569 * Adds a file descriptor to the set of file descriptors polled for 570 * this source. This is usually combined with g_source_new() to add an 571 * event source. The event source's check function will typically test 572 * the revents field in the GPollFD struct and return TRUE if events need 573 * to be processed. 574 * Using this API forces the linear scanning of event sources on each 575 * main loop iteration. Newly-written event sources should try to use 576 * g_source_add_unix_fd() instead of this API. 577 * Params: 578 * fd = a GPollFD structure holding information about a file 579 * descriptor to watch. 580 */ 581 public void addPoll(ref GPollFD fd) 582 { 583 // void g_source_add_poll (GSource *source, GPollFD *fd); 584 g_source_add_poll(gSource, &fd); 585 } 586 587 /** 588 * Removes a file descriptor from the set of file descriptors polled for 589 * this source. 590 * Params: 591 * fd = a GPollFD structure previously passed to g_source_add_poll(). 592 */ 593 public void removePoll(ref GPollFD fd) 594 { 595 // void g_source_remove_poll (GSource *source, GPollFD *fd); 596 g_source_remove_poll(gSource, &fd); 597 } 598 599 /** 600 * Adds child_source to source as a "polled" source; when source is 601 * added to a GMainContext, child_source will be automatically added 602 * with the same priority, when child_source is triggered, it will 603 * cause source to dispatch (in addition to calling its own 604 * callback), and when source is destroyed, it will destroy 605 * child_source as well. (source will also still be dispatched if 606 * its own prepare/check functions indicate that it is ready.) 607 * If you don't need child_source to do anything on its own when it 608 * triggers, you can call g_source_set_dummy_callback() on it to set a 609 * callback that does nothing (except return TRUE if appropriate). 610 * source will hold a reference on child_source while child_source 611 * is attached to it. 612 * Since 2.28 613 * Params: 614 * childSource = a second GSource that source should "poll" 615 */ 616 public void addChildSource(Source childSource) 617 { 618 // void g_source_add_child_source (GSource *source, GSource *child_source); 619 g_source_add_child_source(gSource, (childSource is null) ? null : childSource.getSourceStruct()); 620 } 621 622 /** 623 * Detaches child_source from source and destroys it. 624 * Since 2.28 625 * Params: 626 * childSource = a GSource previously passed to 627 * g_source_add_child_source(). 628 */ 629 public void removeChildSource(Source childSource) 630 { 631 // void g_source_remove_child_source (GSource *source, GSource *child_source); 632 g_source_remove_child_source(gSource, (childSource is null) ? null : childSource.getSourceStruct()); 633 } 634 635 /** 636 * Gets the time to be used when checking this source. The advantage of 637 * calling this function over calling g_get_monotonic_time() directly is 638 * that when checking multiple sources, GLib can cache a single value 639 * instead of having to repeatedly get the system monotonic time. 640 * The time here is the system monotonic time, if available, or some 641 * other reasonable alternative otherwise. See g_get_monotonic_time(). 642 * Since 2.28 643 * Returns: the monotonic time in microseconds 644 */ 645 public long getTime() 646 { 647 // gint64 g_source_get_time (GSource *source); 648 return g_source_get_time(gSource); 649 } 650 651 /** 652 * Warning 653 * g_source_get_current_time has been deprecated since version 2.28 and should not be used in newly-written code. use g_source_get_time() instead 654 * This function ignores source and is otherwise the same as 655 * g_get_current_time(). 656 * Params: 657 * timeval = GTimeVal structure in which to store current time. 658 */ 659 public void getCurrentTime(out GTimeVal timeval) 660 { 661 // void g_source_get_current_time (GSource *source, GTimeVal *timeval); 662 g_source_get_current_time(gSource, &timeval); 663 } 664 665 /** 666 * Removes the source with the given id from the default main context. 667 * The id of 668 * a GSource is given by g_source_get_id(), or will be returned by the 669 * functions g_source_attach(), g_idle_add(), g_idle_add_full(), 670 * g_timeout_add(), g_timeout_add_full(), g_child_watch_add(), 671 * g_child_watch_add_full(), g_io_add_watch(), and g_io_add_watch_full(). 672 * See also g_source_destroy(). You must use g_source_destroy() for sources 673 * added to a non-default main context. 674 * Params: 675 * tag = the ID of the source to remove. 676 * Returns: TRUE if the source was found and removed. 677 */ 678 public static int remove(uint tag) 679 { 680 // gboolean g_source_remove (guint tag); 681 return g_source_remove(tag); 682 } 683 684 /** 685 * Removes a source from the default main loop context given the 686 * source functions and user data. If multiple sources exist with the 687 * same source functions and user data, only one will be destroyed. 688 * Params: 689 * funcs = The source_funcs passed to g_source_new() 690 * userData = the user data for the callback 691 * Returns: TRUE if a source was found and removed. 692 */ 693 public static int removeByFuncsUserData(GSourceFuncs* funcs, void* userData) 694 { 695 // gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs, gpointer user_data); 696 return g_source_remove_by_funcs_user_data(funcs, userData); 697 } 698 699 /** 700 * Removes a source from the default main loop context given the user 701 * data for the callback. If multiple sources exist with the same user 702 * data, only one will be destroyed. 703 * Params: 704 * userData = the user_data for the callback. 705 * Returns: TRUE if a source was found and removed. 706 */ 707 public static int removeByUserData(void* userData) 708 { 709 // gboolean g_source_remove_by_user_data (gpointer user_data); 710 return g_source_remove_by_user_data(userData); 711 } 712 }