Skip to content

Commit

Permalink
Merge pull request #6 from tomaka/merge-glutin
Browse files Browse the repository at this point in the history
Merge glutin
  • Loading branch information
tomaka committed Mar 26, 2016
2 parents 7eeadde + d216d6b commit 6d7e3e7
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 103 deletions.
41 changes: 24 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "winit"
version = "0.4.8"
version = "0.4.9"
authors = ["The winit contributors, Pierre Krieger <pierre.krieger1708@gmail.com>"]
description = "Cross-platform window creation library."
keywords = ["windowing"]
Expand All @@ -18,26 +18,26 @@ shared_library = "0.1.0"
version = "0.1"

[target.i386-apple-ios.dependencies]
objc = "0.1.8"
objc = "0.2"

[target.x86_64-apple-ios.dependencies]
objc = "0.1.8"
objc = "0.2"

[target.aarch64-apple-ios.dependencies]
objc = "0.1.8"
objc = "0.2"

[target.armv7s-apple-ios.dependencies]
objc = "0.1.8"
objc = "0.2"

[target.armv7-apple-ios.dependencies]
objc = "0.1.8"
objc = "0.2"

[target.x86_64-apple-darwin.dependencies]
objc = "0.1.8"
objc = "0.2"
cgl = "0.1"
cocoa = "0.2"
cocoa = "0.3"
core-foundation = "0"
core-graphics = "0"
core-graphics = "0.3"

[target.i686-pc-windows-gnu.dependencies]
winapi = "0.2"
Expand Down Expand Up @@ -75,46 +75,53 @@ dwmapi-sys = "0.1"
wayland-client = { version = "0.5.4", features = ["egl", "dlopen"] }
wayland-kbd = "0.3.3"
wayland-window = "0.2.2"
x11-dl = "~2.3"
x11-dl = "~2.4"

[target.i586-unknown-linux-gnu.dependencies]
wayland-client = { version = "0.5.4", features = ["egl", "dlopen"] }
wayland-kbd = "0.3.3"
wayland-window = "0.2.2"
x11-dl = "~2.3"
x11-dl = "~2.4"

[target.x86_64-unknown-linux-gnu.dependencies]
wayland-client = { version = "0.5.4", features = ["egl", "dlopen"] }
wayland-kbd = "0.3.3"
wayland-window = "0.2.2"
x11-dl = "~2.3"
x11-dl = "~2.4"

[target.arm-unknown-linux-gnueabihf.dependencies]
wayland-client = { version = "0.5.4", features = ["egl", "dlopen"] }
wayland-kbd = "0.3.3"
wayland-window = "0.2.2"
x11-dl = "~2.3"
x11-dl = "~2.4"

[target.armv7-unknown-linux-gnueabihf.dependencies]
wayland-client = { version = "0.5.4", features = ["egl", "dlopen"] }
wayland-kbd = "0.3.3"
wayland-window = "0.2.2"
x11-dl = "~2.3"
x11-dl = "~2.4"

[target.aarch64-unknown-linux-gnu.dependencies]
wayland-client = { version = "0.5.4", features = ["egl", "dlopen"] }
wayland-kbd = "0.3.3"
wayland-window = "0.2.2"
x11-dl = "~2.3"
x11-dl = "~2.4"

[target.x86_64-unknown-dragonfly.dependencies]
wayland-client = { version = "0.5.4", features = ["egl", "dlopen"] }
wayland-kbd = "0.3.3"
wayland-window = "0.2.2"
x11-dl = "~2.3"
x11-dl = "~2.4"

[target.x86_64-unknown-freebsd.dependencies]
wayland-client = { version = "0.5.4", features = ["egl", "dlopen"] }
wayland-kbd = "0.3.3"
wayland-window = "0.2.2"
x11-dl = "~2.3"
x11-dl = "~2.4"

[target.x86_64-unknown-openbsd.dependencies]
osmesa-sys = "0.0.5"
wayland-client = { version = "0.5.4", features = ["egl", "dlopen"] }
wayland-kbd = "0.3.3"
wayland-window = "0.2.2"
x11-dl = "~2.4"
55 changes: 37 additions & 18 deletions src/api/cocoa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ use std::sync::Mutex;
use std::ascii::AsciiExt;
use std::ops::Deref;

use events::Event::{Awakened, MouseInput, MouseMoved, ReceivedCharacter, KeyboardInput, MouseWheel, Closed, Focused};
use events::ElementState::{Pressed, Released};
use events::MouseButton;
use events;
use events::Event::{Awakened, MouseInput, MouseMoved, ReceivedCharacter, KeyboardInput};
use events::Event::{MouseWheel, Closed, Focused, TouchpadPressure};
use events::{self, MouseButton, TouchPhase};

pub use self::monitor::{MonitorId, get_available_monitors, get_primary_monitor};

Expand Down Expand Up @@ -65,11 +65,12 @@ struct WindowDelegate {
impl WindowDelegate {
/// Get the delegate class, initiailizing it neccessary
fn class() -> *const Class {
use std::os::raw::c_void;
use std::sync::{Once, ONCE_INIT};

extern fn window_should_close(this: &Object, _: Sel, _: id) -> BOOL {
unsafe {
let state: *mut libc::c_void = *this.get_ivar("glutinState");
let state: *mut c_void = *this.get_ivar("glutinState");
let state = state as *mut DelegateState;
(*state).pending_events.lock().unwrap().push_back(Closed);
}
Expand All @@ -78,7 +79,7 @@ impl WindowDelegate {

extern fn window_did_resize(this: &Object, _: Sel, _: id) {
unsafe {
let state: *mut libc::c_void = *this.get_ivar("glutinState");
let state: *mut c_void = *this.get_ivar("glutinState");
let state = &mut *(state as *mut DelegateState);

// need to notify context before (?) event
Expand All @@ -98,15 +99,15 @@ impl WindowDelegate {
// TODO: center the cursor if the window had mouse grab when it
// lost focus

let state: *mut libc::c_void = *this.get_ivar("glutinState");
let state: *mut c_void = *this.get_ivar("glutinState");
let state = state as *mut DelegateState;
(*state).pending_events.lock().unwrap().push_back(Focused(true));
}
}

extern fn window_did_resign_key(this: &Object, _: Sel, _: id) {
unsafe {
let state: *mut libc::c_void = *this.get_ivar("glutinState");
let state: *mut c_void = *this.get_ivar("glutinState");
let state = state as *mut DelegateState;
(*state).pending_events.lock().unwrap().push_back(Focused(false));
}
Expand All @@ -118,7 +119,7 @@ impl WindowDelegate {
INIT.call_once(|| unsafe {
// Create new NSWindowDelegate
let superclass = Class::get("NSObject").unwrap();
let mut decl = ClassDecl::new(superclass, "GlutinWindowDelegate").unwrap();
let mut decl = ClassDecl::new("GlutinWindowDelegate", superclass).unwrap();

// Add callback methods
decl.add_method(sel!(windowShouldClose:),
Expand All @@ -132,7 +133,7 @@ impl WindowDelegate {
window_did_resign_key as extern fn(&Object, Sel, id));

// Store internal state as user data
decl.add_ivar::<*mut libc::c_void>("glutinState");
decl.add_ivar::<*mut c_void>("glutinState");

delegate_class = decl.register();
});
Expand All @@ -149,7 +150,7 @@ impl WindowDelegate {
unsafe {
let delegate = IdRef::new(msg_send![WindowDelegate::class(), new]);

(&mut **delegate).set_ivar("glutinState", state_ptr as *mut libc::c_void);
(&mut **delegate).set_ivar("glutinState", state_ptr as *mut ::std::os::raw::c_void);
let _: () = msg_send![*state.window, setDelegate:*delegate];

WindowDelegate { state: state, _this: delegate }
Expand Down Expand Up @@ -189,7 +190,7 @@ impl WindowProxy {
NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_(
nil, NSApplicationDefined, NSPoint::new(0.0, 0.0), NSEventModifierFlags::empty(),
0.0, 0, nil, NSApplicationActivatedEventType, 0, 0);
NSApp().postEvent_atStart_(event, YES);
NSApp().postEvent_atStart_(event, NO);
pool.drain();
}
}
Expand All @@ -209,12 +210,16 @@ impl<'a> Iterator for PollEventsIterator<'a> {

let event: Option<Event>;
unsafe {
let pool = NSAutoreleasePool::new(nil);

let nsevent = NSApp().nextEventMatchingMask_untilDate_inMode_dequeue_(
NSAnyEventMask.bits(),
NSAnyEventMask.bits() | NSEventMaskPressure.bits(),
NSDate::distantPast(nil),
NSDefaultRunLoopMode,
YES);
event = NSEventToEvent(self.window, nsevent);

let _: () = msg_send![pool, release];
}
event
}
Expand All @@ -234,12 +239,16 @@ impl<'a> Iterator for WaitEventsIterator<'a> {

let event: Option<Event>;
unsafe {
let pool = NSAutoreleasePool::new(nil);

let nsevent = NSApp().nextEventMatchingMask_untilDate_inMode_dequeue_(
NSAnyEventMask.bits(),
NSAnyEventMask.bits() | NSEventMaskPressure.bits(),
NSDate::distantFuture(nil),
NSDefaultRunLoopMode,
YES);
event = NSEventToEvent(self.window, nsevent);

let _: () = msg_send![pool, release];
}

if event.is_none() {
Expand Down Expand Up @@ -357,7 +366,9 @@ impl Window {

let masks = if screen.is_some() || attrs.transparent {
// Fullscreen or transparent window
NSBorderlessWindowMask as NSUInteger
NSBorderlessWindowMask as NSUInteger |
NSResizableWindowMask as NSUInteger |
NSTitledWindowMask as NSUInteger
} else if attrs.decorations {
// Classic opaque window with titlebar
NSClosableWindowMask as NSUInteger |
Expand Down Expand Up @@ -557,8 +568,8 @@ impl Window {
let sel = Sel::register(cursor_name);
let cls = Class::get("NSCursor").unwrap();
unsafe {
use objc::MessageArguments;
let cursor: id = ().send(cls as *const _ as id, sel);
use objc::Message;
let cursor: id = cls.send_message(sel, ()).unwrap();
let _: () = msg_send![cursor, set];
}
}
Expand Down Expand Up @@ -654,7 +665,7 @@ impl Clone for IdRef {
unsafe fn NSEventToEvent(window: &Window, nsevent: id) -> Option<Event> {
if nsevent == nil { return None; }

let event_type = msg_send![nsevent, type];
let event_type = nsevent.eventType();
NSApp().sendEvent_(if let NSKeyDown = event_type { nil } else { nsevent });

match event_type {
Expand Down Expand Up @@ -735,7 +746,15 @@ unsafe fn NSEventToEvent(window: &Window, nsevent: id) -> Option<Event> {
LineDelta(scale_factor * nsevent.scrollingDeltaX() as f32,
scale_factor * nsevent.scrollingDeltaY() as f32)
};
Some(MouseWheel(delta))
let phase = match nsevent.phase() {
NSEventPhaseMayBegin | NSEventPhaseBegan => TouchPhase::Started,
NSEventPhaseEnded => TouchPhase::Ended,
_ => TouchPhase::Moved,
};
Some(MouseWheel(delta, phase))
},
NSEventTypePressure => {
Some(TouchpadPressure(nsevent.pressure(), nsevent.stage()))
},
_ => { None },
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/dlopen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"))]
#![cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))]
#![allow(dead_code)]

use std::os::raw::{c_void, c_char, c_int};
Expand Down
6 changes: 4 additions & 2 deletions src/api/wayland/events.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashSet;

use TouchPhase;
use Event as GlutinEvent;
use ElementState;
use MouseButton;
Expand Down Expand Up @@ -98,7 +99,8 @@ pub fn translate_event(
WlPointerAxis::HorizontalScroll => {
MouseScrollDelta::PixelDelta(0.0, amplitude as f32)
}
}
},
TouchPhase::Moved
), surface))
} else {
None
Expand All @@ -107,4 +109,4 @@ pub fn translate_event(
},
_ => None
}
}
}
2 changes: 1 addition & 1 deletion src/api/wayland/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"))]
#![cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))]

pub use self::monitor::{MonitorId, get_available_monitors, get_primary_monitor};
pub use self::window::{PollEventsIterator, WaitEventsIterator, Window, WindowProxy};
Expand Down
3 changes: 2 additions & 1 deletion src/api/win32/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,13 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
winapi::WM_MOUSEWHEEL => {
use events::Event::MouseWheel;
use events::MouseScrollDelta::LineDelta;
use events::TouchPhase;

let value = (wparam >> 16) as i16;
let value = value as i32;
let value = value as f32 / winapi::WHEEL_DELTA as f32;

send_event(window, MouseWheel(LineDelta(0.0, value)));
send_event(window, MouseWheel(LineDelta(0.0, value), TouchPhase::Moved));

0
},
Expand Down
Loading

0 comments on commit 6d7e3e7

Please sign in to comment.