Skip to content
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

More updates #18

Merged
merged 4 commits into from
Oct 16, 2024
Merged
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
10 changes: 5 additions & 5 deletions src/sdl3/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ impl ClipboardUtil {
pub fn set_clipboard_text(&self, text: &str) -> Result<(), String> {
unsafe {
let text = CString::new(text).unwrap();
let result = sys::SDL_SetClipboardText(text.as_ptr() as *const c_char);
let result = sys::clipboard::SDL_SetClipboardText(text.as_ptr() as *const c_char);

if result != 0 {
if result {
Err(get_error())
} else {
Ok(())
Expand All @@ -46,20 +46,20 @@ impl ClipboardUtil {
#[doc(alias = "SDL_GetClipboardText")]
pub fn clipboard_text(&self) -> Result<String, String> {
unsafe {
let buf = sys::SDL_GetClipboardText();
let buf = sys::clipboard::SDL_GetClipboardText();

if buf.is_null() {
Err(get_error())
} else {
let s = CStr::from_ptr(buf as *const _).to_str().unwrap().to_owned();
sys::SDL_free(buf as *mut c_void);
sys::stdinc::SDL_free(buf as *mut c_void);
Ok(s)
}
}
}

#[doc(alias = "SDL_HasClipboardText")]
pub fn has_clipboard_text(&self) -> bool {
unsafe { sys::SDL_HasClipboardText() == sys::SDL_bool::SDL_TRUE }
unsafe { sys::clipboard::SDL_HasClipboardText() == true }
}
}
8 changes: 4 additions & 4 deletions src/sdl3/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,7 @@ impl Event {
type_: SDL_EventType::SDL_EVENT_GAMEPAD_AXIS_MOTION as u32,
timestamp,
which,
axis: axisval as u8,
axis: axisval.into(),
value,
padding1: 0,
padding2: 0,
Expand Down Expand Up @@ -1374,7 +1374,7 @@ impl Event {
which,
// This conversion turns an i32 into a u8; signed-to-unsigned conversions
// are a bit of a code smell, but that appears to be how SDL defines it.
button: buttonval as u8,
button: buttonval .into(),
state: sys::events::SDL_Event::SDL_PRESSED as u8,
padding1: 0,
padding2: 0,
Expand All @@ -1399,7 +1399,7 @@ impl Event {
type_: SDL_EventType::SDL_EVENT_GAMEPAD_BUTTON_UP as u32,
timestamp,
which,
button: buttonval as u8,
button: buttonval .into(),
state: sys::events::SDL_Event::SDL_RELEASED as u8,
padding1: 0,
padding2: 0,
Expand Down Expand Up @@ -3119,7 +3119,7 @@ impl<'a, CB: EventWatchCallback + 'a> EventWatch<'a, CB> {
}

fn filter(&self) -> SDL_EventFilter {
Some(event_callback_marshall::<CB> as _)
Some(event_callback_marshall::<CB>.into())
}

fn callback(&mut self) -> *mut c_void {
Expand Down
6 changes: 3 additions & 3 deletions src/sdl3/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use crate::sys;
#[doc(alias = "SDL_GetBasePath")]
pub fn base_path() -> Result<String, String> {
let result = unsafe {
let buf = sys::SDL_GetBasePath();
let buf = sys::filesystem::SDL_GetBasePath();
let s = CStr::from_ptr(buf as *const _).to_str().unwrap().to_owned();
sys::SDL_free(buf as *mut c_void);
sys::stdinc::SDL_free(buf as *mut c_void);
s
};

Expand Down Expand Up @@ -70,7 +70,7 @@ pub fn pref_path(org_name: &str, app_name: &str) -> Result<String, PrefPathError
Err(err) => return Err(InvalidApplicationName(err)),
};
let buf =
sys::SDL_GetPrefPath(org.as_ptr() as *const c_char, app.as_ptr() as *const c_char);
sys::filesystem::SDL_GetPrefPath(org.as_ptr() as *const c_char, app.as_ptr() as *const c_char);
CStr::from_ptr(buf as *const _).to_str().unwrap().to_owned()
};

Expand Down
324 changes: 152 additions & 172 deletions src/sdl3/gamepad.rs

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions src/sdl3/guid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use std::ffi::{CStr, CString, NulError};
use std::fmt::{Display, Error, Formatter};
use libc::c_char;

/// Wrapper around a `SDL_GUID`, a globally unique identifier
/// for a joystick.
#[derive(Copy, Clone)]
pub struct Guid {
pub raw: sys::guid::SDL_GUID,
}

impl PartialEq for Guid {
fn eq(&self, other: &Guid) -> bool {
self.raw.data == other.raw.data
}
}

impl Eq for Guid {}

impl Guid {
/// Create a GUID from a string representation.
#[doc(alias = "SDL_StringToGUID")]
pub fn from_string(guid: &str) -> Result<Guid, NulError> {
let guid = CString::new(guid)?;

let raw = unsafe { sys::guid::SDL_StringToGUID(guid.as_ptr() as *const c_char) };

Ok(Guid { raw })
}

/// Return `true` if GUID is full 0s
pub fn is_zero(&self) -> bool {
for &i in &self.raw.data {
if i != 0 {
return false;
}
}

true
}

/// Return a String representation of GUID
#[doc(alias = "SDL_GUIDToString")]
pub fn string(&self) -> String {
// Doc says "buf should supply at least 33bytes". I took that
// to mean that 33bytes should be enough in all cases, but
// maybe I'm wrong?
let mut buf = [0; 33];

let len = buf.len() as i32;
let c_str = buf.as_mut_ptr();

unsafe {
sys::guid::SDL_GUIDToString(self.raw, c_str, len);
}

// The buffer should always be NUL terminated (the
// documentation doesn't explicitly say it but I checked the
// code)
if c_str.is_null() {
String::new()
} else {
unsafe {
CStr::from_ptr(c_str as *const _)
.to_str()
.unwrap()
.to_string()
}
}
}

/// Return a copy of the internal GUID
pub fn raw(self) -> sys::joystick::SDL_GUID {
self.raw
}
}

impl Display for Guid {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "{}", self.string())
}
}
14 changes: 7 additions & 7 deletions src/sdl3/haptic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ impl HapticSubsystem {
use crate::common::IntegerOrSdlError::*;

let haptic = unsafe {
let joystick = sys::SDL_OpenJoystick(joystick_index);
sys::SDL_HapticOpenFromJoystick(joystick)
let joystick = sys::haptic::SDL_OpenJoystick(joystick_index);
sys::haptic::SDL_OpenHapticFromJoystick(joystick)
};

if haptic.is_null() {
Err(SdlError(get_error()))
} else {
unsafe { sys::SDL_HapticRumbleInit(haptic) };
unsafe { sys::haptic::SDL_InitHapticRumble(haptic) };
Ok(Haptic {
subsystem: self.clone(),
raw: haptic,
Expand All @@ -31,7 +31,7 @@ impl HapticSubsystem {
/// Wrapper around the `SDL_Haptic` object
pub struct Haptic {
subsystem: HapticSubsystem,
raw: *mut sys::SDL_Haptic,
raw: *mut sys::haptic::SDL_Haptic,
}

impl Haptic {
Expand All @@ -43,19 +43,19 @@ impl Haptic {

/// Run a simple rumble effect on the haptic device.
pub fn rumble_play(&mut self, strength: f32, duration: u32) {
unsafe { sys::SDL_HapticRumblePlay(self.raw, strength, duration) };
unsafe { sys::haptic::SDL_PlayHapticRumble(self.raw, strength, duration) };
}

/// Stop the simple rumble on the haptic device.
#[doc(alias = "SDL_HapticRumbleStop")]
pub fn rumble_stop(&mut self) {
unsafe { sys::SDL_HapticRumbleStop(self.raw) };
unsafe { sys::haptic::SDL_StopHapticRumble(self.raw) };
}
}

impl Drop for Haptic {
#[doc(alias = "SDL_HapticClose")]
fn drop(&mut self) {
unsafe { sys::SDL_HapticClose(self.raw) }
unsafe { sys::haptic::SDL_CloseHaptic(self.raw) }
}
}
26 changes: 13 additions & 13 deletions src/sdl3/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum Hint {
///
/// # Example
/// ```rust,no_run
/// sdl2::hint::set_video_minimize_on_focus_loss(false);
/// sdl3::hint::set_video_minimize_on_focus_loss(false);
/// ```
///
/// * `value`: `true` to enable minimizing of the Window if it loses key focus when in fullscreen mode,
Expand All @@ -36,7 +36,7 @@ pub fn set_video_minimize_on_focus_loss(value: bool) -> bool {
///
/// # Example
/// ```rust,no_run
/// sdl2::hint::set_video_minimize_on_focus_loss_with_priority(false, &sdl2::hint::Hint::Override);
/// sdl3::hint::set_video_minimize_on_focus_loss_with_priority(false, &sdl3::hint::Hint::Override);
/// ```
///
/// * `value`: `true` to enable minimizing of the Window if it loses key focus when in fullscreen mode,
Expand All @@ -62,10 +62,10 @@ pub fn set_video_minimize_on_focus_loss_with_priority(value: bool, priority: &Hi
///
/// # Example
/// ```rust,no_run
/// assert_eq!(sdl2::hint::get_video_minimize_on_focus_loss(), true);
/// assert_eq!(sdl3::hint::get_video_minimize_on_focus_loss(), true);
///
/// sdl2::hint::set_video_minimize_on_focus_loss(false);
/// assert_eq!(sdl2::hint::get_video_minimize_on_focus_loss(), false);
/// sdl3::hint::set_video_minimize_on_focus_loss(false);
/// assert_eq!(sdl3::hint::get_video_minimize_on_focus_loss(), false);
/// ```
pub fn get_video_minimize_on_focus_loss() -> bool {
match get(VIDEO_MINIMIZE_ON_FOCUS_LOSS) {
Expand All @@ -82,10 +82,10 @@ pub fn set(name: &str, value: &str) -> bool {
let name = CString::new(name).unwrap();
let value = CString::new(value).unwrap();
unsafe {
sys::SDL_SetHint(
sys::hints::SDL_SetHint(
name.as_ptr() as *const c_char,
value.as_ptr() as *const c_char,
) == sys::SDL_bool::SDL_TRUE
) == true
}
}

Expand All @@ -96,7 +96,7 @@ pub fn get(name: &str) -> Option<String> {
let name = CString::new(name).unwrap();

unsafe {
let res = sys::SDL_GetHint(name.as_ptr() as *const c_char);
let res = sys::hints::SDL_GetHint(name.as_ptr() as *const c_char);

if res.is_null() {
None
Expand All @@ -116,16 +116,16 @@ pub fn set_with_priority(name: &str, value: &str, priority: &Hint) -> bool {
let value = CString::new(value).unwrap();

let priority_val = match *priority {
Hint::Normal => sys::SDL_HintPriority::SDL_HINT_NORMAL,
Hint::Override => sys::SDL_HintPriority::SDL_HINT_OVERRIDE,
Hint::Default => sys::SDL_HintPriority::SDL_HINT_DEFAULT,
Hint::Normal => sys::hints::SDL_HintPriority::SDL_HINT_NORMAL,
Hint::Override => sys::hints::SDL_HintPriority::SDL_HINT_OVERRIDE,
Hint::Default => sys::hints::SDL_HintPriority::SDL_HINT_DEFAULT,
};

unsafe {
sys::SDL_SetHintWithPriority(
sys::hints::SDL_SetHintWithPriority(
name.as_ptr() as *const c_char,
value.as_ptr() as *const c_char,
priority_val,
) == sys::SDL_bool::SDL_TRUE
) == true
}
}
Loading
Loading