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