Skip to content
This repository was archived by the owner on Jun 8, 2021. It is now read-only.

Commit 2149905

Browse files
committed
Merge idle and timouet funcs into source module, cleanup
1 parent 9bdfe69 commit 2149905

File tree

5 files changed

+114
-112
lines changed

5 files changed

+114
-112
lines changed

glib-sys/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,9 @@ extern "C" {
349349
pub fn g_timeout_source_new () -> *mut GSource;
350350
pub fn g_timeout_source_new_seconds (interval: c_uint) -> *mut GSource;
351351
//pub fn g_timeout_add (interval: c_uint, function: GSourceFunc, data: gpointer) -> c_uint;
352-
pub fn g_timeout_add (interval: c_uint, function: gpointer, data: gpointer) -> c_uint;
353-
//pub fn g_timeout_add_full ();
352+
pub fn g_timeout_add_full (priority: c_int, interval: c_uint, function: GSourceFunc, data: gpointer, notify: GDestroyNotify) -> c_uint;
354353
//pub fn g_timeout_add_seconds (interval: c_uint, function: GSourceFunc, data: gpointer) -> c_uint;
355-
pub fn g_timeout_add_seconds (interval: c_uint, function: gpointer, data: gpointer) -> c_uint;
356-
//pub fn g_timeout_add_seconds_full ();
354+
pub fn g_timeout_add_seconds_full (priority: c_int, interval: c_uint, function: GSourceFunc, data: gpointer, notify: GDestroyNotify) -> c_uint;
357355
pub fn g_idle_source_new () -> *mut GSource;
358356
// pub fn g_idle_add (function: GSourceFunc, data: gpointer) -> c_uint;
359357
pub fn g_idle_add_full (priority: c_int, function: GSourceFunc, data: gpointer, notify: GDestroyNotify) -> c_uint;

src/idle_func.rs

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ pub use self::slist::{SList, SElem};
1919
pub use self::glib_container::GlibContainer;
2020
pub use self::error::{Error};
2121
pub use self::permission::Permission;
22-
pub use self::timeout_func::timeout;
23-
pub use self::idle_func::idle;
22+
pub use self::source::{Continue, idle_add, timeout_add, timeout_add_seconds};
2423
pub use self::traits::FFIGObject;
2524
pub use self::value::{Value, ValuePublic};
2625
pub use types::Type;
@@ -33,8 +32,7 @@ pub mod glib_container;
3332
mod error;
3433
mod permission;
3534
pub mod signal;
36-
pub mod timeout_func;
37-
pub mod idle_func;
35+
pub mod source;
3836
pub mod traits;
3937
pub mod translate;
4038
mod value;

src/source.rs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
}

src/timeout_func.rs

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)