Skip to content

Commit f45ef41

Browse files
authored
Merge pull request #544 from icefoxen/push-nonuser-event
Starting to implement Event::to_ll for all event types.
2 parents 0c37d28 + a8bbc96 commit f45ef41

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

sdl2-sys/src/event.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ pub const SDL_DISABLE: SDL_State = 0;
1717
pub const SDL_ENABLE: SDL_State = 1;
1818
pub const SDL_QUERY: SDL_State = -1;
1919

20+
pub type SDL_KeyState = uint8_t;
21+
pub const SDL_RELEASED: SDL_KeyState = 0;
22+
pub const SDL_PRESSED: SDL_KeyState = 1;
23+
2024
pub type SDL_SysWMmsg = c_void;
2125

2226
pub type SDL_EventType = c_uint;

src/sdl2/event.rs

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::ffi::CStr;
66
use std::mem;
77
use libc::{c_int, c_void, uint32_t};
88
use num::FromPrimitive;
9+
use num::ToPrimitive;
910
use std::ptr;
1011
use std::borrow::ToOwned;
1112
use std::iter::FromIterator;
@@ -27,6 +28,9 @@ use keyboard::Scancode;
2728
use get_error;
2829

2930
use sys::event as ll;
31+
use sys::scancode;
32+
use sys::keycode;
33+
use sys::keyboard as syskeyboard;
3034

3135
struct CustomEventTypeMaps {
3236
sdl_id_to_type_id: HashMap<u32, ::std::any::TypeId>,
@@ -417,6 +421,27 @@ impl WindowEventId {
417421
_ => WindowEventId::None
418422
}
419423
}
424+
425+
fn to_ll(&self) -> u8 {
426+
match *self {
427+
WindowEventId::Shown => 1,
428+
WindowEventId::Hidden => 2,
429+
WindowEventId::Exposed => 3,
430+
WindowEventId::Moved => 4,
431+
WindowEventId::Resized => 5,
432+
WindowEventId::SizeChanged => 6,
433+
WindowEventId::Minimized => 7,
434+
WindowEventId::Maximized => 8,
435+
WindowEventId::Restored => 9,
436+
WindowEventId::Enter => 10,
437+
WindowEventId::Leave => 11,
438+
WindowEventId::FocusGained => 12,
439+
WindowEventId::FocusLost => 13,
440+
WindowEventId::Close => 14,
441+
WindowEventId::None => 0,
442+
}
443+
}
444+
420445
}
421446

422447
#[derive(Clone, PartialEq)]
@@ -709,6 +734,26 @@ impl ::std::fmt::Debug for Event {
709734
}
710735
}
711736

737+
/// Helper function to make converting scancodes
738+
/// and keycodes to primitive SDL_Keysym types.
739+
fn mk_keysym(scancode: Option<Scancode>,
740+
keycode: Option<Keycode>,
741+
keymod: Mod) -> syskeyboard::SDL_Keysym {
742+
let scancode = scancode
743+
.map(|sc| sc.to_u32().unwrap_or(0u32))
744+
.unwrap_or(scancode::SDL_SCANCODE_UNKNOWN);
745+
let keycode = keycode
746+
.map(|kc| kc.to_i32().unwrap_or(0i32))
747+
.unwrap_or(keycode::SDLK_UNKNOWN);
748+
let keymod = keymod.bits() as u16;
749+
syskeyboard::SDL_Keysym {
750+
scancode: scancode,
751+
sym: keycode,
752+
_mod: keymod,
753+
unused: 0,
754+
}
755+
}
756+
712757
// TODO: Remove this when from_utf8 is updated in Rust
713758
impl Event {
714759
fn to_ll(self) -> Option<ll::SDL_Event> {
@@ -728,6 +773,118 @@ impl Event {
728773
}
729774
Some(ret)
730775
},
776+
777+
Event::Quit{timestamp} => {
778+
let event = ll::SDL_QuitEvent {
779+
type_: ll::SDL_QUIT,
780+
timestamp: timestamp,
781+
};
782+
unsafe {
783+
ptr::copy(&event, &mut ret as *mut ll::SDL_Event as *mut ll::SDL_QuitEvent, 1);
784+
}
785+
Some(ret)
786+
},
787+
788+
Event::Window{
789+
timestamp,
790+
window_id,
791+
win_event_id,
792+
data1,
793+
data2,
794+
} => {
795+
let event = ll::SDL_WindowEvent {
796+
type_: ll::SDL_WINDOWEVENT,
797+
timestamp: timestamp,
798+
windowID: window_id,
799+
event: win_event_id.to_ll(),
800+
padding1: 0,
801+
padding2: 0,
802+
padding3: 0,
803+
data1: data1,
804+
data2: data2,
805+
};
806+
unsafe {
807+
ptr::copy(&event, &mut ret as *mut ll::SDL_Event as *mut ll::SDL_WindowEvent, 1);
808+
}
809+
Some(ret)
810+
},
811+
812+
Event::KeyDown{
813+
timestamp,
814+
window_id,
815+
keycode,
816+
scancode,
817+
keymod,
818+
repeat,
819+
} => {
820+
let keysym = mk_keysym(scancode, keycode, keymod);
821+
let event = ll::SDL_KeyboardEvent{
822+
type_: ll::SDL_KEYDOWN,
823+
timestamp: timestamp,
824+
windowID: window_id,
825+
state: ll::SDL_PRESSED,
826+
repeat: repeat as u8,
827+
padding2: 0,
828+
padding3: 0,
829+
keysym: keysym,
830+
};
831+
unsafe {
832+
ptr::copy(&event, &mut ret as *mut ll::SDL_Event as *mut ll::SDL_KeyboardEvent, 1);
833+
}
834+
Some(ret)
835+
},
836+
Event::KeyUp {
837+
timestamp,
838+
window_id,
839+
keycode,
840+
scancode,
841+
keymod,
842+
repeat,
843+
} => {
844+
let keysym = mk_keysym(scancode, keycode, keymod);
845+
let event = ll::SDL_KeyboardEvent{
846+
type_: ll::SDL_KEYUP,
847+
timestamp: timestamp,
848+
windowID: window_id,
849+
state: ll::SDL_RELEASED,
850+
repeat: repeat as u8,
851+
padding2: 0,
852+
padding3: 0,
853+
keysym: keysym,
854+
};
855+
unsafe {
856+
ptr::copy(&event, &mut ret as *mut ll::SDL_Event as *mut ll::SDL_KeyboardEvent, 1);
857+
}
858+
Some(ret)
859+
},
860+
Event::TextEditing{..} |
861+
Event::TextInput{..} |
862+
Event::MouseMotion{..} |
863+
Event::MouseButtonDown{..} |
864+
Event::MouseButtonUp{..} |
865+
Event::MouseWheel{..} |
866+
Event::JoyAxisMotion{..} |
867+
Event::JoyBallMotion{..} |
868+
Event::JoyHatMotion{..} |
869+
Event::JoyButtonDown{..} |
870+
Event::JoyButtonUp{..} |
871+
Event::JoyDeviceAdded{..} |
872+
Event::JoyDeviceRemoved{..} |
873+
Event::ControllerAxisMotion{..} |
874+
Event::ControllerButtonDown{..} |
875+
Event::ControllerButtonUp{..} |
876+
Event::ControllerDeviceAdded{..} |
877+
Event::ControllerDeviceRemoved{..} |
878+
Event::ControllerDeviceRemapped{..} |
879+
Event::FingerDown{..} |
880+
Event::FingerUp{..} |
881+
Event::FingerMotion{..} |
882+
Event::DollarGesture{..} |
883+
Event::DollarRecord{..} |
884+
Event::MultiGesture{..} |
885+
Event::ClipboardUpdate{..} |
886+
Event::DropFile{..} |
887+
Event::Unknown{..} |
731888
_ => {
732889
// don't know how to convert!
733890
None

0 commit comments

Comments
 (0)