|
| 1 | +// Copyright 2013-2015, The Rust-GNOME Project Developers. |
| 2 | +// See the COPYRIGHT file at the top-level directory of this distribution. |
| 3 | +// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT> |
| 4 | + |
| 5 | +//! GSourceFunc functions |
| 6 | +
|
| 7 | +use std::cell::RefCell; |
| 8 | +use std::ops::DerefMut; |
| 9 | +use std::mem::transmute; |
| 10 | +use ffi::{gboolean, gpointer, g_idle_add_full, g_timeout_add_full, g_timeout_add_seconds_full}; |
| 11 | +use translate::ToGlib; |
| 12 | + |
| 13 | +pub struct Continue(pub bool); |
| 14 | + |
| 15 | +impl ToGlib for Continue { |
| 16 | + type GlibType = gboolean; |
| 17 | + |
| 18 | + #[inline] |
| 19 | + fn to_glib(&self) -> gboolean { |
| 20 | + self.0.to_glib() |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +// Box::into_raw stability workaround |
| 26 | +unsafe fn into_raw<T>(b: Box<T>) -> *mut T { transmute(b) } |
| 27 | + |
| 28 | +extern "C" fn trampoline(func: &RefCell<Box<FnMut() -> Continue + 'static>>) -> gboolean { |
| 29 | + func.borrow_mut().deref_mut()().to_glib() |
| 30 | +} |
| 31 | + |
| 32 | +extern "C" fn destroy_closure(ptr: gpointer) { |
| 33 | + unsafe { |
| 34 | + // Box::from_raw API stability workaround |
| 35 | + let ptr = ptr as *mut RefCell<Box<FnMut() -> Continue + 'static>>; |
| 36 | + let _: Box<RefCell<Box<FnMut() -> Continue + 'static>>> = transmute(ptr); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +const G_PRIORITY_DEFAULT: i32 = 0; |
| 41 | +const G_PRIORITY_DEFAULT_IDLE: i32 = 200; |
| 42 | + |
| 43 | + |
| 44 | +/// Adds a function to be called whenever there are no higher priority events pending to the default main loop. |
| 45 | +/// The function is given the default idle priority, G_PRIORITY_DEFAULT_IDLE. |
| 46 | +/// If the function returns FALSE it is automatically removed from |
| 47 | +/// the list of event sources and will not be called again. |
| 48 | +/// |
| 49 | +/// This internally creates a main loop source using g_idle_source_new() |
| 50 | +/// and attaches it to the global GMainContext using g_source_attach(), |
| 51 | +/// so the callback will be invoked in whichever thread is running that main context. |
| 52 | +/// You can do these steps manually if you need greater control or to use a custom main context. |
| 53 | +pub fn idle_add<F>(func: F) -> u32 |
| 54 | + where F: FnMut() -> Continue + 'static { |
| 55 | + let f: Box<RefCell<Box<FnMut() -> Continue + 'static>>> = Box::new(RefCell::new(Box::new(func))); |
| 56 | + unsafe { |
| 57 | + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, transmute(trampoline), |
| 58 | + into_raw(f) as gpointer, destroy_closure) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/// Sets a function to be called at regular intervals, with the default priority, G_PRIORITY_DEFAULT. |
| 63 | +/// The function is called repeatedly until it returns FALSE, at which point the timeout is |
| 64 | +/// automatically destroyed and the function will not be called again. The first call to the |
| 65 | +/// function will be at the end of the first interval . |
| 66 | +/// |
| 67 | +/// Note that timeout functions may be delayed, due to the processing of other event sources. Thus |
| 68 | +/// they should not be relied on for precise timing. After each call to the timeout function, the |
| 69 | +/// time of the next timeout is recalculated based on the current time and the given interval (it |
| 70 | +/// does not try to 'catch up' time lost in delays). |
| 71 | +/// |
| 72 | +/// If you want to have a timer in the "seconds" range and do not care about the exact time of the |
| 73 | +/// first call of the timer, use the g_timeout_add_seconds() function; this function allows for more |
| 74 | +/// optimizations and more efficient system power usage. |
| 75 | +/// |
| 76 | +/// This internally creates a main loop source using g_timeout_source_new() and attaches it to the |
| 77 | +/// global GMainContext using g_source_attach(), so the callback will be invoked in whichever thread |
| 78 | +/// is running that main context. You can do these steps manually if you need greater control or to |
| 79 | +/// use a custom main context. |
| 80 | +/// |
| 81 | +/// The interval given is in terms of monotonic time, not wall clock time. See g_get_monotonic_time(). |
| 82 | +pub fn timeout_add<F>(interval: u32, func: F) -> u32 |
| 83 | + where F: FnMut() -> Continue + 'static { |
| 84 | + let f: Box<RefCell<Box<FnMut() -> Continue + 'static>>> = Box::new(RefCell::new(Box::new(func))); |
| 85 | + unsafe { |
| 86 | + g_timeout_add_full(G_PRIORITY_DEFAULT, interval, transmute(trampoline), |
| 87 | + into_raw(f) as gpointer, destroy_closure) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/// Sets a function to be called at regular intervals with the default priority, G_PRIORITY_DEFAULT. |
| 92 | +/// The function is called repeatedly until it returns FALSE, at which point the timeout is automatically |
| 93 | +/// destroyed and the function will not be called again. |
| 94 | +/// |
| 95 | +/// This internally creates a main loop source using g_timeout_source_new_seconds() and attaches it to |
| 96 | +/// the main loop context using g_source_attach(). You can do these steps manually if you need greater |
| 97 | +/// control. Also see g_timeout_add_seconds_full(). |
| 98 | +/// |
| 99 | +/// Note that the first call of the timer may not be precise for timeouts of one second. If you need |
| 100 | +/// finer precision and have such a timeout, you may want to use g_timeout_add() instead. |
| 101 | +/// |
| 102 | +/// The interval given is in terms of monotonic time, not wall clock time. See g_get_monotonic_time(). |
| 103 | +pub fn timeout_add_seconds<F>(interval: u32, func: F) -> u32 |
| 104 | + where F: FnMut() -> Continue + 'static { |
| 105 | + let f: Box<RefCell<Box<FnMut() -> Continue + 'static>>> = Box::new(RefCell::new(Box::new(func))); |
| 106 | + unsafe { |
| 107 | + g_timeout_add_seconds_full(G_PRIORITY_DEFAULT, interval, transmute(trampoline), |
| 108 | + into_raw(f) as gpointer, destroy_closure) |
| 109 | + } |
| 110 | +} |
0 commit comments