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