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 = Idle 29 * strct = 30 * realStrct= 31 * ctorStrct= 32 * clss = Idle 33 * interf = 34 * class Code: Yes 35 * interface Code: No 36 * template for: 37 * extend = 38 * implements: 39 * prefixes: 40 * - g_idle_ 41 * omit structs: 42 * omit prefixes: 43 * omit code: 44 * omit signals: 45 * imports: 46 * - glib.Source 47 * structWrap: 48 * - GSource* -> Source 49 * module aliases: 50 * local aliases: 51 * overrides: 52 */ 53 54 module glib.Idle; 55 56 public import gtkc.glibtypes; 57 58 private import gtkc.glib; 59 private import glib.ConstructionException; 60 61 62 private import glib.Source; 63 64 65 66 67 /** 68 * The main event loop manages all the available sources of events for 69 * GLib and GTK+ applications. These events can come from any number of 70 * different types of sources such as file descriptors (plain files, 71 * pipes or sockets) and timeouts. New types of event sources can also 72 * be added using g_source_attach(). 73 * 74 * To allow multiple independent sets of sources to be handled in 75 * different threads, each source is associated with a GMainContext. 76 * A GMainContext can only be running in a single thread, but 77 * sources can be added to it and removed from it from other threads. 78 * 79 * Each event source is assigned a priority. The default priority, 80 * G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher priorities. 81 * Values greater than 0 denote lower priorities. Events from high priority 82 * sources are always processed before events from lower priority sources. 83 * 84 * Idle functions can also be added, and assigned a priority. These will 85 * be run whenever no events with a higher priority are ready to be processed. 86 * 87 * The GMainLoop data type represents a main event loop. A GMainLoop is 88 * created with g_main_loop_new(). After adding the initial event sources, 89 * g_main_loop_run() is called. This continuously checks for new events from 90 * each of the event sources and dispatches them. Finally, the processing of 91 * an event from one of the sources leads to a call to g_main_loop_quit() to 92 * exit the main loop, and g_main_loop_run() returns. 93 * 94 * It is possible to create new instances of GMainLoop recursively. 95 * This is often used in GTK+ applications when showing modal dialog 96 * boxes. Note that event sources are associated with a particular 97 * GMainContext, and will be checked and dispatched for all main 98 * loops associated with that GMainContext. 99 * 100 * GTK+ contains wrappers of some of these functions, e.g. gtk_main(), 101 * gtk_main_quit() and gtk_events_pending(). 102 * 103 * Creating new source types 104 * 105 * One of the unusual features of the GMainLoop functionality 106 * is that new types of event source can be created and used in 107 * addition to the builtin type of event source. A new event source 108 * type is used for handling GDK events. A new source type is created 109 * by deriving from the GSource structure. 110 * The derived type of source is represented by a structure that has 111 * the GSource structure as a first element, and other elements specific 112 * to the new source type. To create an instance of the new source type, 113 * call g_source_new() passing in the size of the derived structure and 114 * a table of functions. These GSourceFuncs determine the behavior of 115 * the new source type. 116 * 117 * New source types basically interact with the main context 118 * in two ways. Their prepare function in GSourceFuncs can set a timeout 119 * to determine the maximum amount of time that the main loop will sleep 120 * before checking the source again. In addition, or as well, the source 121 * can add file descriptors to the set that the main context checks using 122 * g_source_add_poll(). 123 * 124 * <hr> 125 * 126 * Customizing the main loop iteration 127 * 128 * Single iterations of a GMainContext can be run with 129 * g_main_context_iteration(). In some cases, more detailed control 130 * of exactly how the details of the main loop work is desired, for 131 * instance, when integrating the GMainLoop with an external main loop. 132 * In such cases, you can call the component functions of 133 * g_main_context_iteration() directly. These functions are 134 * g_main_context_prepare(), g_main_context_query(), 135 * g_main_context_check() and g_main_context_dispatch(). 136 * 137 * The operation of these functions can best be seen in terms 138 * of a state diagram, as shown in Figure 1, “States of a Main Context”. 139 * 140 * Figure 1. States of a Main Context 141 * 142 * On Unix, the GLib mainloop is incompatible with fork(). Any program 143 * using the mainloop must either exec() or exit() from the child 144 * without returning to the mainloop. 145 */ 146 public class Idle 147 { 148 149 /** Holds all idle delegates */ 150 bool delegate()[] idleListeners; 151 /** our idle ID */ 152 uint idleID; 153 154 /** 155 * Creates a new idle cycle. 156 * Params: 157 * interval = the idle in milieconds 158 * dlg = the delegate to be executed 159 * fireNow = When true the delegate will be executed emmidiatly 160 */ 161 this(bool delegate() dlg, bool fireNow=false) 162 { 163 idleListeners ~= dlg; 164 idleID = g_idle_add(cast(GSourceFunc)&idleCallback, cast(void*)this); 165 if ( fireNow ) 166 { 167 if ( !dlg() ) 168 { 169 idleListeners.length = 0; 170 } 171 } 172 } 173 174 /** 175 * Creates a new idle cycle. 176 * Params: 177 * dlg = the delegate to be executed 178 * priority = Priority for the idle function 179 * fireNow = When true the delegate will be executed emmidiatly 180 */ 181 this(bool delegate() dlg, GPriority priority, bool fireNow=false) 182 { 183 idleListeners ~= dlg; 184 idleID = g_idle_add_full(priority, cast(GSourceFunc)&idleCallback, cast(void*)this, null); 185 if ( fireNow ) 186 { 187 if ( !dlg() ) 188 { 189 idleListeners.length = 0; 190 } 191 } 192 } 193 194 /** */ 195 public void stop() 196 { 197 if ( idleID > 0 ) 198 { 199 g_source_remove(idleID); 200 } 201 idleListeners.length = 0; 202 } 203 204 /** 205 * Removes the idle from gtk 206 */ 207 ~this() 208 { 209 stop(); 210 } 211 212 /** 213 * Adds a new delegate to this idle cycle 214 * Params: 215 * dlg = 216 * fireNow = 217 */ 218 public void addListener(bool delegate() dlg, bool fireNow=false) 219 { 220 idleListeners ~= dlg; 221 if ( fireNow ) 222 { 223 if ( !dlg() ) 224 { 225 idleListeners.length = idleListeners.length - 1; 226 } 227 } 228 } 229 230 /** 231 * The callback execution from glib 232 * Params: 233 * idle = 234 * Returns: 235 */ 236 extern(C) static bool idleCallback(Idle idle) 237 { 238 return idle.callAllListeners(); 239 } 240 241 /** 242 * Executes all delegates on the execution list 243 * Returns: 244 */ 245 private bool callAllListeners() 246 { 247 bool runAgain = false; 248 249 int i = 0; 250 251 while ( i<idleListeners.length ) 252 { 253 if ( !idleListeners[i]() ) 254 { 255 idleListeners = idleListeners[0..i] ~ idleListeners[i+1..idleListeners.length]; 256 } 257 else 258 { 259 runAgain = true; 260 ++i; 261 } 262 } 263 return runAgain; 264 } 265 266 /** 267 */ 268 269 /** 270 * Creates a new idle source. 271 * The source will not initially be associated with any GMainContext 272 * and must be added to one with g_source_attach() before it will be 273 * executed. Note that the default priority for idle sources is 274 * G_PRIORITY_DEFAULT_IDLE, as compared to other sources which 275 * have a default priority of G_PRIORITY_DEFAULT. 276 * Returns: the newly-created idle source 277 */ 278 public static Source sourceNew() 279 { 280 // GSource * g_idle_source_new (void); 281 auto p = g_idle_source_new(); 282 283 if(p is null) 284 { 285 return null; 286 } 287 288 return new Source(cast(GSource*) p); 289 } 290 291 /** 292 * Adds a function to be called whenever there are no higher priority 293 * events pending to the default main loop. The function is given the 294 * default idle priority, G_PRIORITY_DEFAULT_IDLE. If the function 295 * returns FALSE it is automatically removed from the list of event 296 * sources and will not be called again. 297 * This internally creates a main loop source using g_idle_source_new() 298 * and attaches it to the main loop context using g_source_attach(). 299 * You can do these steps manually if you need greater control. 300 * Params: 301 * data = data to pass to function. 302 * Returns: the ID (greater than 0) of the event source. 303 */ 304 public static uint add(GSourceFunc funct, void* data) 305 { 306 // guint g_idle_add (GSourceFunc function, gpointer data); 307 return g_idle_add(funct, data); 308 } 309 310 /** 311 * Adds a function to be called whenever there are no higher priority 312 * events pending. If the function returns FALSE it is automatically 313 * removed from the list of event sources and will not be called again. 314 * This internally creates a main loop source using g_idle_source_new() 315 * and attaches it to the main loop context using g_source_attach(). 316 * You can do these steps manually if you need greater control. 317 * Params: 318 * priority = the priority of the idle source. Typically this will be in the 319 * range between G_PRIORITY_DEFAULT_IDLE and G_PRIORITY_HIGH_IDLE. 320 * data = data to pass to function 321 * notify = function to call when the idle is removed, or NULL. [allow-none] 322 * Returns: the ID (greater than 0) of the event source. Rename to: g_idle_add 323 */ 324 public static uint addFull(int priority, GSourceFunc funct, void* data, GDestroyNotify notify) 325 { 326 // guint g_idle_add_full (gint priority, GSourceFunc function, gpointer data, GDestroyNotify notify); 327 return g_idle_add_full(priority, funct, data, notify); 328 } 329 330 /** 331 * Removes the idle function with the given data. 332 * Params: 333 * data = the data for the idle source's callback. 334 * Returns: TRUE if an idle source was found and removed. 335 */ 336 public static int removeByData(void* data) 337 { 338 // gboolean g_idle_remove_by_data (gpointer data); 339 return g_idle_remove_by_data(data); 340 } 341 }