Skip to content

A preview of the don't panic branch #701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ bitflags = "0.7"
libc = "0.2"
rand = "0.3"
lazy_static="0.2"
error-chain = "0.10"

[dependencies.num]
version = "0.1"
Expand Down
6 changes: 3 additions & 3 deletions src/sdl2/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,17 +346,17 @@ impl GameController {
}

/// Return the joystick id of this controller
pub fn instance_id(&self) -> i32 {
pub fn instance_id(&self) -> Result<i32, String> {
let result = unsafe {
let joystick = ll::SDL_GameControllerGetJoystick(self.raw);
::sys::joystick::SDL_JoystickInstanceID(joystick)
};

if result < 0 {
// Should only fail if the joystick is NULL.
panic!(get_error())
bail!(get_error())
} else {
result
Ok(result)
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/sdl2/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl ::EventSubsystem {
/// println!("{:?}", event);
/// }
/// ```
pub fn peek_events<B>(&self, max_amount: u32) -> B
pub fn peek_events<B>(&self, max_amount: u32) -> Result<B, String>
where B: FromIterator<Event>
{
unsafe {
Expand All @@ -99,13 +99,13 @@ impl ::EventSubsystem {

if result < 0 {
// The only error possible is "Couldn't lock event queue"
panic!(get_error());
bail!(get_error());
} else {
events.set_len(result as usize);

events.into_iter().map(|event_raw| {
Ok(events.into_iter().map(|event_raw| {
Event::from_ll(event_raw)
}).collect()
}).collect())
}
}
}
Expand Down Expand Up @@ -1678,12 +1678,12 @@ unsafe fn poll_event() -> Option<Event> {
else { None }
}

unsafe fn wait_event() -> Event {
unsafe fn wait_event() -> Result<Event, String> {
let mut raw = mem::uninitialized();
let success = ll::SDL_WaitEvent(&mut raw) == 1;

if success { Event::from_ll(raw) }
else { panic!(get_error()) }
if success { Ok(Event::from_ll(raw)) }
else { bail!(get_error()) }
}

unsafe fn wait_event_timeout(timeout: u32) -> Option<Event> {
Expand Down Expand Up @@ -1751,7 +1751,7 @@ impl ::EventPump {
}

/// Waits indefinitely for the next available event.
pub fn wait_event(&mut self) -> Event {
pub fn wait_event(&mut self) -> Result<Event, String> {
unsafe { wait_event() }
}

Expand Down Expand Up @@ -1815,8 +1815,8 @@ pub struct EventWaitIterator<'a> {
}

impl<'a> Iterator for EventWaitIterator<'a> {
type Item = Event;
fn next(&mut self) -> Option<Event> { unsafe { Some(wait_event()) } }
type Item = Result<Event, String>;
fn next(&mut self) -> Option<Result<Event,String>> { unsafe { Some(wait_event()) } }
}

/// An iterator that calls `EventPump::wait_event_timeout()`.
Expand Down
62 changes: 33 additions & 29 deletions src/sdl2/joystick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,40 +115,40 @@ impl Joystick {
unsafe { ll::SDL_JoystickGetAttached(self.raw) != 0 }
}

pub fn instance_id(&self) -> i32 {
pub fn instance_id(&self) -> Result<i32, String> {
let result = unsafe { ll::SDL_JoystickInstanceID(self.raw) };

if result < 0 {
// Should only fail if the joystick is NULL.
panic!(get_error())
bail!(get_error())
} else {
result
Ok(result)
}
}

/// Retreive the joystick's GUID
pub fn guid(&self) -> Guid {
pub fn guid(&self) -> Result<Guid, String> {
let raw = unsafe { ll::SDL_JoystickGetGUID(self.raw) };

let guid = Guid { raw: raw };

if guid.is_zero() {
// Should only fail if the joystick is NULL.
panic!(get_error())
bail!(get_error())
} else {
guid
Ok(guid)
}
}

/// Retreive the number of axes for this joystick
pub fn num_axes(&self) -> u32 {
pub fn num_axes(&self) -> Result<u32, String> {
let result = unsafe { ll::SDL_JoystickNumAxes(self.raw) };

if result < 0 {
// Should only fail if the joystick is NULL.
panic!(get_error())
bail!(get_error())
} else {
result as u32
Ok(result as u32)
}
}

Expand Down Expand Up @@ -180,14 +180,14 @@ impl Joystick {
}

/// Retreive the number of buttons for this joystick
pub fn num_buttons(&self) -> u32 {
pub fn num_buttons(&self) -> Result<u32, String> {
let result = unsafe { ll::SDL_JoystickNumButtons(self.raw) };

if result < 0 {
// Should only fail if the joystick is NULL.
panic!(get_error())
bail!(get_error())
} else {
result as u32
Ok(result as u32)
}
}

Expand Down Expand Up @@ -221,14 +221,14 @@ impl Joystick {
}

/// Retreive the number of balls for this joystick
pub fn num_balls(&self) -> u32 {
pub fn num_balls(&self) -> Result<u32, String> {
let result = unsafe { ll::SDL_JoystickNumBalls(self.raw) };

if result < 0 {
// Should only fail if the joystick is NULL.
panic!(get_error())
bail!(get_error())
} else {
result as u32
Ok(result as u32)
}
}

Expand All @@ -250,14 +250,14 @@ impl Joystick {
}

/// Retreive the number of balls for this joystick
pub fn num_hats(&self) -> u32 {
pub fn num_hats(&self) -> Result<u32, String> {
let result = unsafe { ll::SDL_JoystickNumHats(self.raw) };

if result < 0 {
// Should only fail if the joystick is NULL.
panic!(get_error())
bail!(get_error())
} else {
result as u32
Ok(result as u32)
}
}

Expand Down Expand Up @@ -365,18 +365,21 @@ impl Display for Guid {
/// This is represented in SDL2 as a bitfield but obviously not all
/// combinations make sense: 5 for instance would mean up and down at
/// the same time... To simplify things I turn it into an enum which
/// is how the SDL2 docs present it anyway (using macros).
/// is how the SDL2 docs present it anyway (using macros). The remaining
/// (unlikely) cases are encoded with the special constructor Unknown
/// which just holds the u8.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum HatState {
Centered = 0,
Up = 0x01,
Right = 0x02,
Down = 0x04,
Left = 0x08,
RightUp = 0x02 | 0x01,
RightDown = 0x02 | 0x04,
LeftUp = 0x08 | 0x01,
Leftdown = 0x08 | 0x04,
Centered,
Up,
Right,
Down,
Left,
RightUp,
RightDown,
LeftUp,
Leftdown,
Unknown(u8),
}

impl HatState {
Expand All @@ -391,7 +394,7 @@ impl HatState {
6 => HatState::RightDown,
9 => HatState::LeftUp,
12 => HatState::Leftdown,
_ => panic!("Unexpected hat position: {}", raw),
n => HatState::Unknown(n),
}
}

Expand All @@ -406,6 +409,7 @@ impl HatState {
HatState::RightDown => 6,
HatState::LeftUp => 9,
HatState::Leftdown => 12,
HatState::Unknown(n) => n,
}

}
Expand Down
3 changes: 3 additions & 0 deletions src/sdl2/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ extern crate lazy_static;
extern crate bitflags;
pub extern crate sdl2_sys as sys;

#[macro_use]
extern crate error_chain;

#[cfg(feature = "gfx")]
extern crate c_vec;

Expand Down
Loading