This commit is contained in:
Daniel Collin 2016-05-08 10:33:27 +02:00
parent 10de5cd84e
commit 3c2451c2ec

View file

@ -12,7 +12,7 @@ use {MenuItem, MenuItemHandle};
// use menu::Menu; // use menu::Menu;
use libc::{c_void, c_char, c_uchar}; use libc::{c_void, c_char, c_uchar};
use std::ffi::{CString}; use std::ffi::CString;
use std::ptr; use std::ptr;
use std::mem; use std::mem;
use std::os::raw; use std::os::raw;
@ -150,18 +150,23 @@ static KEY_MAPPINGS: [Key; 128] = [
/* 7f */ Key::Unknown, /* 7f */ Key::Unknown,
]; ];
#[link(name = "Cocoa", kind = "framework")] #[link(name = "Cocoa", kind = "framework")]
#[link(name = "Carbon", kind = "framework")] #[link(name = "Carbon", kind = "framework")]
extern { extern "C" {
fn mfb_open(name: *const c_char, width: u32, height: u32, flags: u32, scale: i32) -> *mut c_void; fn mfb_open(name: *const c_char,
width: u32,
height: u32,
flags: u32,
scale: i32)
-> *mut c_void;
fn mfb_close(window: *mut c_void); fn mfb_close(window: *mut c_void);
fn mfb_update(window: *mut c_void); fn mfb_update(window: *mut c_void);
fn mfb_update_with_buffer(window: *mut c_void, buffer: *const c_uchar); fn mfb_update_with_buffer(window: *mut c_void, buffer: *const c_uchar);
fn mfb_set_position(window: *mut c_void, x: i32, y: i32); fn mfb_set_position(window: *mut c_void, x: i32, y: i32);
fn mfb_set_key_callback(window: *mut c_void, target: *mut c_void, fn mfb_set_key_callback(window: *mut c_void,
cb: unsafe extern fn(*mut c_void, i32, i32), target: *mut c_void,
cb: unsafe extern fn(*mut c_void, u32)); cb: unsafe extern "C" fn(*mut c_void, i32, i32),
cb: unsafe extern "C" fn(*mut c_void, u32));
fn mfb_set_mouse_data(window_handle: *mut c_void, shared_data: *mut SharedData); fn mfb_set_mouse_data(window_handle: *mut c_void, shared_data: *mut SharedData);
fn mfb_should_close(window: *mut c_void) -> i32; fn mfb_should_close(window: *mut c_void) -> i32;
fn mfb_get_screen_size() -> u32; fn mfb_get_screen_size() -> u32;
@ -177,7 +182,8 @@ extern {
name: *const c_char, name: *const c_char,
enabled: bool, enabled: bool,
key: u32, key: u32,
modifier: u32) -> u64; modifier: u32)
-> u64;
fn mfb_remove_menu_item(menu: *mut c_void, item_handle: u64); fn mfb_remove_menu_item(menu: *mut c_void, item_handle: u64);
} }
@ -238,7 +244,11 @@ impl Window {
unsafe { unsafe {
let scale_factor = Self::get_scale_factor(width, height, opts.scale) as usize; let scale_factor = Self::get_scale_factor(width, height, opts.scale) as usize;
let handle = mfb_open(n.as_ptr(), width as u32, height as u32, window_flags::get_flags(opts), scale_factor as i32); let handle = mfb_open(n.as_ptr(),
width as u32,
height as u32,
window_flags::get_flags(opts),
scale_factor as i32);
if handle == ptr::null_mut() { if handle == ptr::null_mut() {
return Err(Error::WindowCreate("Unable to open Window".to_owned())); return Err(Error::WindowCreate("Unable to open Window".to_owned()));
@ -274,7 +284,10 @@ impl Window {
unsafe { unsafe {
mfb_update_with_buffer(self.window_handle, buffer.as_ptr() as *const u8); mfb_update_with_buffer(self.window_handle, buffer.as_ptr() as *const u8);
Self::set_mouse_data(self); Self::set_mouse_data(self);
mfb_set_key_callback(self.window_handle, mem::transmute(self), key_callback, char_callback); mfb_set_key_callback(self.window_handle,
mem::transmute(self),
key_callback,
char_callback);
} }
} }
@ -284,7 +297,10 @@ impl Window {
unsafe { unsafe {
mfb_update(self.window_handle); mfb_update(self.window_handle);
Self::set_mouse_data(self); Self::set_mouse_data(self);
mfb_set_key_callback(self.window_handle, mem::transmute(self), key_callback, char_callback); mfb_set_key_callback(self.window_handle,
mem::transmute(self),
key_callback,
char_callback);
} }
} }
@ -294,7 +310,8 @@ impl Window {
} }
pub fn get_size(&self) -> (usize, usize) { pub fn get_size(&self) -> (usize, usize) {
(self.shared_data.width as usize, self.shared_data.height as usize) (self.shared_data.width as usize,
self.shared_data.height as usize)
} }
pub fn get_scroll_wheel(&self) -> Option<(f32, f32)> { pub fn get_scroll_wheel(&self) -> Option<(f32, f32)> {
@ -321,7 +338,12 @@ impl Window {
let w = self.shared_data.width as f32; let w = self.shared_data.width as f32;
let h = self.shared_data.height as f32; let h = self.shared_data.height as f32;
mouse_handler::get_pos(mode, self.shared_data.mouse_x, self.shared_data.mouse_y, s, w, h) mouse_handler::get_pos(mode,
self.shared_data.mouse_x,
self.shared_data.mouse_y,
s,
w,
h)
} }
#[inline] #[inline]
@ -557,8 +579,11 @@ impl Menu {
let conv_key = Self::map_key_to_menu_key(item.key); let conv_key = Self::map_key_to_menu_key(item.key);
MenuItemHandle(mfb_add_menu_item(self.menu_handle, MenuItemHandle(mfb_add_menu_item(self.menu_handle,
item.id as i32, item_name.as_ptr(), item.id as i32,
item.enabled, conv_key, item.modifier)) item_name.as_ptr(),
item.enabled,
conv_key,
item.modifier))
} }
} }
@ -577,15 +602,11 @@ impl Drop for Window {
} }
} }
/* // impl Drop for Menu {
impl Drop for Menu { // fn drop(&mut self) {
fn drop(&mut self) { // unsafe {
unsafe { // mfb_destroy_menu(self.menu_handle);
mfb_destroy_menu(self.menu_handle); // }
} // }
} // }
} //
*/