Skip to content

Commit 3a6ef83

Browse files
authored
Merge pull request #2 from AngryLawyer/master
updating to master
2 parents 819c0ff + f45ef41 commit 3a6ef83

File tree

4 files changed

+220
-0
lines changed

4 files changed

+220
-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

src/sdl2/pixels.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,18 @@ impl PixelFormatEnum {
324324
}
325325
}
326326

327+
impl From<PixelFormat> for PixelFormatEnum {
328+
fn from(pf: PixelFormat) -> PixelFormatEnum {
329+
unsafe {
330+
let ref sdl_pf = *pf.raw;
331+
match PixelFormatEnum::from_u64(sdl_pf.format as u64) {
332+
Some(pfe) => pfe,
333+
None => panic!("Unknown pixel format: {:?}", sdl_pf.format)
334+
}
335+
}
336+
}
337+
}
338+
327339
impl FromPrimitive for PixelFormatEnum {
328340
fn from_i64(n: i64) -> Option<PixelFormatEnum> {
329341
use self::PixelFormatEnum::*;
@@ -371,3 +383,46 @@ impl FromPrimitive for PixelFormatEnum {
371383

372384
fn from_u64(n: u64) -> Option<PixelFormatEnum> { FromPrimitive::from_i64(n as i64) }
373385
}
386+
387+
388+
// Just test a round-trip conversion from PixelFormat to
389+
// PixelFormatEnum and back.
390+
#[test]
391+
fn test_pixel_format_enum() {
392+
let pixel_formats = vec![
393+
PixelFormatEnum::RGB332,
394+
PixelFormatEnum::RGB444, PixelFormatEnum::RGB555,
395+
PixelFormatEnum::BGR555, PixelFormatEnum::ARGB4444,
396+
PixelFormatEnum::RGBA4444, PixelFormatEnum::ABGR4444,
397+
PixelFormatEnum::BGRA4444, PixelFormatEnum::ARGB1555,
398+
PixelFormatEnum::RGBA5551, PixelFormatEnum::ABGR1555,
399+
PixelFormatEnum::BGRA5551, PixelFormatEnum::RGB565,
400+
PixelFormatEnum::BGR565,
401+
PixelFormatEnum::RGB24, PixelFormatEnum::BGR24,
402+
PixelFormatEnum::RGB888, PixelFormatEnum::RGBX8888,
403+
PixelFormatEnum::BGR888, PixelFormatEnum::BGRX8888,
404+
PixelFormatEnum::ARGB8888, PixelFormatEnum::RGBA8888,
405+
PixelFormatEnum::ABGR8888, PixelFormatEnum::BGRA8888,
406+
PixelFormatEnum::ARGB2101010,
407+
PixelFormatEnum::YV12, PixelFormatEnum::IYUV,
408+
PixelFormatEnum::YUY2, PixelFormatEnum::UYVY,
409+
PixelFormatEnum::YVYU,
410+
PixelFormatEnum::Index8,
411+
// These don't seem to be supported;
412+
// the round-trip
413+
//PixelFormatEnum::Unknown, PixelFormatEnum::Index1LSB,
414+
//PixelFormatEnum::Index1MSB, PixelFormatEnum::Index4LSB,
415+
//PixelFormatEnum::Index4MSB
416+
];
417+
418+
419+
let _sdl_context = ::sdl::init().unwrap();
420+
for format in pixel_formats {
421+
// If we don't support making a surface of a specific format,
422+
// that's fine, just keep going the best we can.
423+
if let Ok(surf) = super::surface::Surface::new(1, 1, format) {
424+
let surf_format = surf.pixel_format();
425+
assert_eq!(PixelFormatEnum::from(surf_format), format);
426+
}
427+
}
428+
}

src/sdl2/surface.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ impl SurfaceRef {
224224
}
225225
}
226226

227+
pub fn pixel_format_enum(&self) -> pixels::PixelFormatEnum {
228+
pixels::PixelFormatEnum::from(self.pixel_format())
229+
}
230+
227231
/// Locks a surface so that the pixels can be directly accessed safely.
228232
pub fn with_lock<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
229233
unsafe {

0 commit comments

Comments
 (0)