Skip to content

Commit

Permalink
Merge pull request #258 from kazimuth/master
Browse files Browse the repository at this point in the history
Fix #257
  • Loading branch information
brendanzab committed Dec 24, 2014
2 parents b0fc90e + a628878 commit 9415016
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
22 changes: 15 additions & 7 deletions src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ macro_rules! callback(
CALLBACK_KEY.with(|cb| {
*cb.borrow_mut() = boxed_cb.take();
});
($ext_set)(Some(callback));
($ext_set)(Some(callback as extern "C" fn($($ext_arg: $ext_arg_ty),*)));
}

pub fn unset() {
Expand Down Expand Up @@ -110,16 +110,24 @@ unsafe fn get_sender<'a>(window: &'a *mut ffi::GLFWwindow) -> &'a Sender<(f64, W
mem::transmute(ffi::glfwGetWindowUserPointer(*window))
}

// Note that this macro creates a static function pointer rather than a plain function.
// This makes it more ergonomic to embed in an Option; see set_window_callback! in lib.rs
macro_rules! window_callback(
(fn $name:ident () => $event:ident) => (
pub extern "C" fn $name(window: *mut ffi::GLFWwindow) {
unsafe { get_sender(&window).send((ffi::glfwGetTime() as f64, WindowEvent::$event)); }
}
pub static $name: (extern "C" fn(window: *mut ffi::GLFWwindow)) = {
extern "C" fn actual_callback(window: *mut ffi::GLFWwindow) {
unsafe { get_sender(&window).send((ffi::glfwGetTime() as f64, WindowEvent::$event));}
}
actual_callback
};
);
(fn $name:ident ($($ext_arg:ident: $ext_arg_ty:ty),*) => $event:ident($($arg_conv:expr),*)) => (
pub extern "C" fn $name(window: *mut ffi::GLFWwindow $(, $ext_arg: $ext_arg_ty)*) {
unsafe { get_sender(&window).send((ffi::glfwGetTime() as f64, WindowEvent::$event($($arg_conv),*))); }
}
pub static $name: (extern "C" fn(window: *mut ffi::GLFWwindow $(, $ext_arg: $ext_arg_ty)*)) = {
extern "C" fn actual_callback(window: *mut ffi::GLFWwindow $(, $ext_arg: $ext_arg_ty)*) {
unsafe { get_sender(&window).send((ffi::glfwGetTime() as f64, WindowEvent::$event($($arg_conv),*))); }
}
actual_callback
};
);
);

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ pub fn fail_on_errors(_: Error, description: String, _: &()) {

/// A callback that triggers a task failure when an error is encountered.
pub static FAIL_ON_ERRORS: Option<ErrorCallback<()>> =
Some(Callback { f: fail_on_errors, data: () });
Some(Callback { f: fail_on_errors as fn(Error, String, &()), data: () });

/// The function to be used with the `LOG_ERRORS` callback.
pub fn log_errors(_: Error, description: String, _: &()) {
Expand All @@ -311,7 +311,7 @@ pub fn log_errors(_: Error, description: String, _: &()) {
/// A callback that logs each error as it is encountered without triggering a
/// task failure.
pub static LOG_ERRORS: Option<ErrorCallback<()>> =
Some(Callback { f: log_errors, data: () });
Some(Callback { f: log_errors as fn(Error, String, &()), data: () });

/// Cursor modes.
#[repr(i32)]
Expand Down

0 comments on commit 9415016

Please sign in to comment.