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 * If you want to have a timer in the "seconds" range and do not care 218 * about the exact time of the first call of the timer, use the 219 * g_timeout_add_seconds() function; this function allows for more 220 * optimizations and more efficient system power usage. 221 * 222 * This internally creates a main loop source using g_timeout_source_new() 223 * and attaches it to the main loop context using g_source_attach(). You can 224 * do these steps manually if you need greater control. 225 * 226 * The interval given is in terms of monotonic time, not wall clock 227 * time. See g_get_monotonic_time(). 228 * 229 * Params: 230 * interval = the time between calls to the function, in milliseconds 231 * (1/1000ths of a second) 232 * funct = function to call 233 * data = data to pass to @function 234 * 235 * Return: the ID (greater than 0) of the event source. 236 */ 237 public static uint add(uint interval, GSourceFunc funct, void* data) 238 { 239 return g_timeout_add(interval, funct, data); 240 } 241 242 /** 243 * Sets a function to be called at regular intervals, with the given 244 * priority. The function is called repeatedly until it returns 245 * %FALSE, at which point the timeout is automatically destroyed and 246 * the function will not be called again. The @notify function is 247 * called when the timeout is destroyed. The first call to the 248 * function will be at the end of the first @interval. 249 * 250 * Note that timeout functions may be delayed, due to the processing of other 251 * event sources. Thus they should not be relied on for precise timing. 252 * After each call to the timeout function, the time of the next 253 * timeout is recalculated based on the current time and the given interval 254 * (it does not try to 'catch up' time lost in delays). 255 * 256 * This internally creates a main loop source using g_timeout_source_new() 257 * and attaches it to the main loop context using g_source_attach(). You can 258 * do these steps manually if you need greater control. 259 * 260 * The interval given in terms of monotonic time, not wall clock time. 261 * See g_get_monotonic_time(). 262 * 263 * Params: 264 * priority = the priority of the timeout source. Typically this will be in 265 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH. 266 * interval = the time between calls to the function, in milliseconds 267 * (1/1000ths of a second) 268 * funct = function to call 269 * data = data to pass to @function 270 * notify = function to call when the timeout is removed, or %NULL 271 * 272 * Return: the ID (greater than 0) of the event source. 273 */ 274 public static uint addFull(int priority, uint interval, GSourceFunc funct, void* data, GDestroyNotify notify) 275 { 276 return g_timeout_add_full(priority, interval, funct, data, notify); 277 } 278 279 /** 280 * Sets a function to be called at regular intervals with the default 281 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly until 282 * it returns %FALSE, at which point the timeout is automatically destroyed 283 * and the function will not be called again. 284 * 285 * This internally creates a main loop source using 286 * g_timeout_source_new_seconds() and attaches it to the main loop context 287 * using g_source_attach(). You can do these steps manually if you need 288 * greater control. Also see g_timeout_add_seconds_full(). 289 * 290 * Note that the first call of the timer may not be precise for timeouts 291 * of one second. If you need finer precision and have such a timeout, 292 * you may want to use g_timeout_add() instead. 293 * 294 * The interval given is in terms of monotonic time, not wall clock 295 * time. See g_get_monotonic_time(). 296 * 297 * Params: 298 * interval = the time between calls to the function, in seconds 299 * funct = function to call 300 * data = data to pass to @function 301 * 302 * Return: the ID (greater than 0) of the event source. 303 * 304 * Since: 2.14 305 */ 306 public static uint addSeconds(uint interval, GSourceFunc funct, void* data) 307 { 308 return g_timeout_add_seconds(interval, funct, data); 309 } 310 311 /** 312 * Sets a function to be called at regular intervals, with @priority. 313 * The function is called repeatedly until it returns %FALSE, at which 314 * point the timeout is automatically destroyed and the function will 315 * not be called again. 316 * 317 * Unlike g_timeout_add(), this function operates at whole second granularity. 318 * The initial starting point of the timer is determined by the implementation 319 * and the implementation is expected to group multiple timers together so that 320 * they fire all at the same time. 321 * To allow this grouping, the @interval to the first timer is rounded 322 * and can deviate up to one second from the specified interval. 323 * Subsequent timer iterations will generally run at the specified interval. 324 * 325 * Note that timeout functions may be delayed, due to the processing of other 326 * event sources. Thus they should not be relied on for precise timing. 327 * After each call to the timeout function, the time of the next 328 * timeout is recalculated based on the current time and the given @interval 329 * 330 * If you want timing more precise than whole seconds, use g_timeout_add() 331 * instead. 332 * 333 * The grouping of timers to fire at the same time results in a more power 334 * and CPU efficient behavior so if your timer is in multiples of seconds 335 * and you don't require the first timer exactly one second from now, the 336 * use of g_timeout_add_seconds() is preferred over g_timeout_add(). 337 * 338 * This internally creates a main loop source using 339 * g_timeout_source_new_seconds() and attaches it to the main loop context 340 * using g_source_attach(). You can do these steps manually if you need 341 * greater control. 342 * 343 * The interval given is in terms of monotonic time, not wall clock 344 * time. See g_get_monotonic_time(). 345 * 346 * Params: 347 * priority = the priority of the timeout source. Typically this will be in 348 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH. 349 * interval = the time between calls to the function, in seconds 350 * funct = function to call 351 * data = data to pass to @function 352 * notify = function to call when the timeout is removed, or %NULL 353 * 354 * Return: the ID (greater than 0) of the event source. 355 * 356 * Since: 2.14 357 */ 358 public static uint addSecondsFull(int priority, uint interval, GSourceFunc funct, void* data, GDestroyNotify notify) 359 { 360 return g_timeout_add_seconds_full(priority, interval, funct, data, notify); 361 } 362 363 /** 364 * Creates a new timeout source. 365 * 366 * The source will not initially be associated with any #GMainContext 367 * and must be added to one with g_source_attach() before it will be 368 * executed. 369 * 370 * The interval given is in terms of monotonic time, not wall clock 371 * time. See g_get_monotonic_time(). 372 * 373 * Params: 374 * interval = the timeout interval in milliseconds. 375 * 376 * Return: the newly-created timeout source 377 */ 378 public static Source sourceNew(uint interval) 379 { 380 auto p = g_timeout_source_new(interval); 381 382 if(p is null) 383 { 384 return null; 385 } 386 387 return new Source(cast(GSource*) p); 388 } 389 390 /** 391 * Creates a new timeout source. 392 * 393 * The source will not initially be associated with any #GMainContext 394 * and must be added to one with g_source_attach() before it will be 395 * executed. 396 * 397 * The scheduling granularity/accuracy of this timeout source will be 398 * in seconds. 399 * 400 * The interval given in terms of monotonic time, not wall clock time. 401 * See g_get_monotonic_time(). 402 * 403 * Params: 404 * interval = the timeout interval in seconds 405 * 406 * Return: the newly-created timeout source 407 * 408 * Since: 2.14 409 */ 410 public static Source sourceNewSeconds(uint interval) 411 { 412 auto p = g_timeout_source_new_seconds(interval); 413 414 if(p is null) 415 { 416 return null; 417 } 418 419 return new Source(cast(GSource*) p); 420 } 421 }