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.Timeout; 26 27 private import glib.Source; 28 private import glib.c.functions; 29 public import glib.c.types; 30 public import gtkc.glibtypes; 31 32 33 /** */ 34 public class Timeout 35 { 36 /** Holds all idle delegates */ 37 private bool delegate()[] timeoutListeners; 38 /** Our timeout ID */ 39 private uint timeoutID; 40 41 42 /** 43 * Creates a new timeout cycle with the default priority, GPriority.DEFAULT. 44 * 45 * Note that timeout functions may be delayed, due to the processing of other 46 * event sources. Thus they should not be relied on for precise timing. 47 * After each call to the timeout function, the time of the next timeout is 48 * recalculated based on the current time and the given interval 49 * (it does not try to 'catch up' time lost in delays). 50 * Params: 51 * interval = the timeout in milieconds 52 * delegate() = the delegate to be executed 53 * fireNow = When true the delegate will be executed emmidiatly 54 */ 55 this(uint interval, bool delegate() dlg, bool fireNow=false) 56 { 57 if ( fireNow && !dlg() ) 58 return; 59 60 timeoutListeners ~= dlg; 61 timeoutID = g_timeout_add_full(GPriority.DEFAULT, interval, cast(GSourceFunc)&timeoutCallback, cast(void*)this, cast(GDestroyNotify)&destroyTimeoutNotify); 62 } 63 64 /** 65 * Creates a new timeout cycle. 66 * Params: 67 * interval = the timeout in milieconds 68 * delegate() = the delegate to be executed 69 * priority = Priority for the timeout function 70 * fireNow = When true the delegate will be executed emmidiatly 71 */ 72 this(uint interval, bool delegate() dlg, GPriority priority, bool fireNow=false) 73 { 74 if ( fireNow && !dlg() ) 75 return; 76 77 timeoutListeners ~= dlg; 78 timeoutID = g_timeout_add_full(priority, interval, cast(GSourceFunc)&timeoutCallback, cast(void*)this, cast(GDestroyNotify)&destroyTimeoutNotify); 79 } 80 81 /** 82 * Creates a new timeout cycle with the default priority, GPriority.DEFAULT. 83 * Params: 84 * delegate() = the delegate to be executed 85 * seconds = interval in seconds. 86 * fireNow = When true the delegate will be executed emmidiatly 87 */ 88 this(bool delegate() dlg, uint seconds, bool fireNow=false) 89 { 90 if ( fireNow && !dlg() ) 91 return; 92 93 timeoutListeners ~= dlg; 94 timeoutID = g_timeout_add_seconds_full(GPriority.DEFAULT, seconds, cast(GSourceFunc)&timeoutCallback, cast(void*)this, cast(GDestroyNotify)&destroyTimeoutNotify); 95 } 96 97 /** 98 * Creates a new timeout cycle. 99 * Params: 100 * delegate() = the delegate to be executed 101 * seconds = interval in seconds. 102 * priority = Priority for the timeout function 103 * fireNow = When true the delegate will be executed emmidiatly 104 */ 105 this(bool delegate() dlg, uint seconds, GPriority priority, bool fireNow=false) 106 { 107 if ( fireNow && !dlg() ) 108 return; 109 110 timeoutListeners ~= dlg; 111 timeoutID = g_timeout_add_seconds_full(priority, seconds, cast(GSourceFunc)&timeoutCallback, cast(void*)this, cast(GDestroyNotify)&destroyTimeoutNotify); 112 } 113 114 /** Removes the timeout from gtk */ 115 public void stop() 116 { 117 if ( timeoutID > 0 ) 118 { 119 g_source_remove(timeoutID); 120 } 121 } 122 123 /** 124 * Removes the timeout from gtk 125 */ 126 ~this() 127 { 128 stop(); 129 } 130 131 /** 132 * Adds a new delegate to this timeout cycle 133 * Params: 134 * dlg = 135 * fireNow = 136 */ 137 public void addListener(bool delegate() dlg, bool fireNow=false) 138 { 139 if ( fireNow && !dlg() ) 140 return; 141 142 timeoutListeners ~= dlg; 143 } 144 145 /** 146 * The callback execution from glib 147 * Params: 148 * timeout = 149 * Returns: 150 */ 151 extern(C) static bool timeoutCallback(Timeout timeout) 152 { 153 bool runAgain = false; 154 int i = 0; 155 156 while ( i<timeout.timeoutListeners.length ) 157 { 158 if ( !timeout.timeoutListeners[i]() ) 159 { 160 timeout.timeoutListeners = timeout.timeoutListeners[0..i] ~ timeout.timeoutListeners[i+1..$]; 161 } 162 else 163 { 164 runAgain = true; 165 ++i; 166 } 167 } 168 169 return runAgain; 170 } 171 172 /* 173 * Reset the timeout object when it's destroyed on the GTK side. 174 */ 175 extern(C) static void destroyTimeoutNotify(Timeout timeout) 176 { 177 timeout.timeoutListeners.length = 0; 178 timeout.timeoutID = 0; 179 } 180 181 /** 182 */ 183 184 /** 185 * Sets a function to be called at regular intervals, with the default 186 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly 187 * until it returns %FALSE, at which point the timeout is automatically 188 * destroyed and the function will not be called again. The first call 189 * to the function will be at the end of the first @interval. 190 * 191 * Note that timeout functions may be delayed, due to the processing of other 192 * event sources. Thus they should not be relied on for precise timing. 193 * After each call to the timeout function, the time of the next 194 * timeout is recalculated based on the current time and the given interval 195 * (it does not try to 'catch up' time lost in delays). 196 * 197 * See [memory management of sources][mainloop-memory-management] for details 198 * on how to handle the return value and memory management of @data. 199 * 200 * If you want to have a timer in the "seconds" range and do not care 201 * about the exact time of the first call of the timer, use the 202 * g_timeout_add_seconds() function; this function allows for more 203 * optimizations and more efficient system power usage. 204 * 205 * This internally creates a main loop source using g_timeout_source_new() 206 * and attaches it to the global #GMainContext using g_source_attach(), so 207 * the callback will be invoked in whichever thread is running that main 208 * context. You can do these steps manually if you need greater control or to 209 * use a custom main context. 210 * 211 * The interval given is in terms of monotonic time, not wall clock 212 * time. See g_get_monotonic_time(). 213 * 214 * Params: 215 * interval = the time between calls to the function, in milliseconds 216 * (1/1000ths of a second) 217 * funct = function to call 218 * data = data to pass to @function 219 * 220 * Returns: the ID (greater than 0) of the event source. 221 */ 222 public static uint add(uint interval, GSourceFunc funct, void* data) 223 { 224 return g_timeout_add(interval, funct, data); 225 } 226 227 /** 228 * Sets a function to be called at regular intervals, with the given 229 * priority. The function is called repeatedly until it returns 230 * %FALSE, at which point the timeout is automatically destroyed and 231 * the function will not be called again. The @notify function is 232 * called when the timeout is destroyed. The first call to the 233 * function will be at the end of the first @interval. 234 * 235 * Note that timeout functions may be delayed, due to the processing of other 236 * event sources. Thus they should not be relied on for precise timing. 237 * After each call to the timeout function, the time of the next 238 * timeout is recalculated based on the current time and the given interval 239 * (it does not try to 'catch up' time lost in delays). 240 * 241 * See [memory management of sources][mainloop-memory-management] for details 242 * on how to handle the return value and memory management of @data. 243 * 244 * This internally creates a main loop source using g_timeout_source_new() 245 * and attaches it to the global #GMainContext using g_source_attach(), so 246 * the callback will be invoked in whichever thread is running that main 247 * context. You can do these steps manually if you need greater control or to 248 * use a custom main context. 249 * 250 * The interval given in terms of monotonic time, not wall clock time. 251 * See g_get_monotonic_time(). 252 * 253 * Params: 254 * priority = the priority of the timeout source. Typically this will be in 255 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH. 256 * interval = the time between calls to the function, in milliseconds 257 * (1/1000ths of a second) 258 * funct = function to call 259 * data = data to pass to @function 260 * notify = function to call when the timeout is removed, or %NULL 261 * 262 * Returns: the ID (greater than 0) of the event source. 263 */ 264 public static uint addFull(int priority, uint interval, GSourceFunc funct, void* data, GDestroyNotify notify) 265 { 266 return g_timeout_add_full(priority, interval, funct, data, notify); 267 } 268 269 /** 270 * Sets a function to be called at regular intervals with the default 271 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly until 272 * it returns %FALSE, at which point the timeout is automatically destroyed 273 * and the function will not be called again. 274 * 275 * This internally creates a main loop source using 276 * g_timeout_source_new_seconds() and attaches it to the main loop context 277 * using g_source_attach(). You can do these steps manually if you need 278 * greater control. Also see g_timeout_add_seconds_full(). 279 * 280 * Note that the first call of the timer may not be precise for timeouts 281 * of one second. If you need finer precision and have such a timeout, 282 * you may want to use g_timeout_add() instead. 283 * 284 * See [memory management of sources][mainloop-memory-management] for details 285 * on how to handle the return value and memory management of @data. 286 * 287 * The interval given is in terms of monotonic time, not wall clock 288 * time. See g_get_monotonic_time(). 289 * 290 * Params: 291 * interval = the time between calls to the function, in seconds 292 * funct = function to call 293 * data = data to pass to @function 294 * 295 * Returns: the ID (greater than 0) of the event source. 296 * 297 * Since: 2.14 298 */ 299 public static uint addSeconds(uint interval, GSourceFunc funct, void* data) 300 { 301 return g_timeout_add_seconds(interval, funct, data); 302 } 303 304 /** 305 * Sets a function to be called at regular intervals, with @priority. 306 * The function is called repeatedly until it returns %FALSE, at which 307 * point the timeout is automatically destroyed and the function will 308 * not be called again. 309 * 310 * Unlike g_timeout_add(), this function operates at whole second granularity. 311 * The initial starting point of the timer is determined by the implementation 312 * and the implementation is expected to group multiple timers together so that 313 * they fire all at the same time. 314 * To allow this grouping, the @interval to the first timer is rounded 315 * and can deviate up to one second from the specified interval. 316 * Subsequent timer iterations will generally run at the specified interval. 317 * 318 * Note that timeout functions may be delayed, due to the processing of other 319 * event sources. Thus they should not be relied on for precise timing. 320 * After each call to the timeout function, the time of the next 321 * timeout is recalculated based on the current time and the given @interval 322 * 323 * See [memory management of sources][mainloop-memory-management] for details 324 * on how to handle the return value and memory management of @data. 325 * 326 * If you want timing more precise than whole seconds, use g_timeout_add() 327 * instead. 328 * 329 * The grouping of timers to fire at the same time results in a more power 330 * and CPU efficient behavior so if your timer is in multiples of seconds 331 * and you don't require the first timer exactly one second from now, the 332 * use of g_timeout_add_seconds() is preferred over g_timeout_add(). 333 * 334 * This internally creates a main loop source using 335 * g_timeout_source_new_seconds() and attaches it to the main loop context 336 * using g_source_attach(). You can do these steps manually if you need 337 * greater control. 338 * 339 * The interval given is in terms of monotonic time, not wall clock 340 * time. See g_get_monotonic_time(). 341 * 342 * Params: 343 * priority = the priority of the timeout source. Typically this will be in 344 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH. 345 * interval = the time between calls to the function, in seconds 346 * funct = function to call 347 * data = data to pass to @function 348 * notify = function to call when the timeout is removed, or %NULL 349 * 350 * Returns: the ID (greater than 0) of the event source. 351 * 352 * Since: 2.14 353 */ 354 public static uint addSecondsFull(int priority, uint interval, GSourceFunc funct, void* data, GDestroyNotify notify) 355 { 356 return g_timeout_add_seconds_full(priority, interval, funct, data, notify); 357 } 358 359 /** 360 * Creates a new timeout source. 361 * 362 * The source will not initially be associated with any #GMainContext 363 * and must be added to one with g_source_attach() before it will be 364 * executed. 365 * 366 * The interval given is in terms of monotonic time, not wall clock 367 * time. See g_get_monotonic_time(). 368 * 369 * Params: 370 * interval = the timeout interval in milliseconds. 371 * 372 * Returns: the newly-created timeout source 373 */ 374 public static Source sourceNew(uint interval) 375 { 376 auto p = g_timeout_source_new(interval); 377 378 if(p is null) 379 { 380 return null; 381 } 382 383 return new Source(cast(GSource*) p, true); 384 } 385 386 /** 387 * Creates a new timeout source. 388 * 389 * The source will not initially be associated with any #GMainContext 390 * and must be added to one with g_source_attach() before it will be 391 * executed. 392 * 393 * The scheduling granularity/accuracy of this timeout source will be 394 * in seconds. 395 * 396 * The interval given in terms of monotonic time, not wall clock time. 397 * See g_get_monotonic_time(). 398 * 399 * Params: 400 * interval = the timeout interval in seconds 401 * 402 * Returns: the newly-created timeout source 403 * 404 * Since: 2.14 405 */ 406 public static Source sourceNewSeconds(uint interval) 407 { 408 auto p = g_timeout_source_new_seconds(interval); 409 410 if(p is null) 411 { 412 return null; 413 } 414 415 return new Source(cast(GSource*) p, true); 416 } 417 }