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