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