Skip to content

Commit

Permalink
Merge pull request #472 from AngryLawyer/arm-friendly
Browse files Browse the repository at this point in the history
Make various functions prefer c_char over native Rust u8/i8, for cross-platform compilation
  • Loading branch information
AngryLawyer committed Nov 27, 2015
2 parents 4c4a651 + 5de5784 commit 4a8764e
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name = "sdl2"
description = "SDL2 bindings for Rust"
repository = "https://github.com/AngryLawyer/rust-sdl2"
documentation = "http://angrylawyer.github.io/rust-sdl2/sdl2/"
version = "0.11.0"
version = "0.12.0"
license = "MIT"
authors = [ "Tony Aldridge <tony@angry-lawyer.com>" ]
keywords = ["SDL", "windowing", "graphics"]
Expand All @@ -16,7 +16,7 @@ path = "src/sdl2/lib.rs"

[dependencies]
num = "0.1"
bitflags = "0.3.2"
bitflags = "0.3"
libc = "0.2"
rand = "0.3"

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ download through Crates.io:

```toml
[dependencies]
sdl2 = "0.11"
sdl2 = "0.12"
```

Alternatively, pull it from GitHub
Expand Down
4 changes: 2 additions & 2 deletions src/sdl2/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
//! ```
use std::ffi::{CStr, CString};
use num::FromPrimitive;
use libc::{c_int, c_void, uint8_t};
use libc::{c_int, c_void, uint8_t, c_char};
use std::ops::{Deref, DerefMut};
use std::path::Path;
use std::marker::PhantomData;
Expand Down Expand Up @@ -480,7 +480,7 @@ impl<CB: AudioCallback> AudioDevice<CB> {
let device_ptr = device.map_or(null(), |s| s.as_ptr());

let iscapture_flag = 0;
let device_id = ll::SDL_OpenAudioDevice(device_ptr, iscapture_flag, &desired, &mut obtained, 0);
let device_id = ll::SDL_OpenAudioDevice(device_ptr as *const c_char, iscapture_flag, &desired, &mut obtained, 0);
match device_id {
0 => {
Err(get_error())
Expand Down
3 changes: 2 additions & 1 deletion src/sdl2/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ffi::{CString, CStr};
use libc::c_char;
use SdlResult;
use get_error;

Expand Down Expand Up @@ -31,7 +32,7 @@ impl ClipboardUtil {
pub fn set_clipboard_text(&self, text: &str) -> SdlResult<()> {
unsafe {
let text = CString::new(text).unwrap();
let result = ll::SDL_SetClipboardText(text.as_ptr());
let result = ll::SDL_SetClipboardText(text.as_ptr() as *const c_char);

if result == 0 {
Err(get_error())
Expand Down
6 changes: 3 additions & 3 deletions src/sdl2/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl GameControllerSubsystem {
pub fn add_mapping(&self, mapping: &str) -> SdlResult<MappingStatus> {
let mapping = try!(CString::new(mapping).unwrap_or_sdlresult());

let result = unsafe { ll::SDL_GameControllerAddMapping(mapping.as_ptr()) };
let result = unsafe { ll::SDL_GameControllerAddMapping(mapping.as_ptr() as *const c_char) };

match result {
1 => Ok(MappingStatus::Added),
Expand Down Expand Up @@ -112,7 +112,7 @@ impl Axis {
/// used by the game controller mapping strings.
pub fn from_string(axis: &str) -> Option<Axis> {
let id = match CString::new(axis) {
Ok(axis) => unsafe { ll::SDL_GameControllerGetAxisFromString(axis.as_ptr()) },
Ok(axis) => unsafe { ll::SDL_GameControllerGetAxisFromString(axis.as_ptr() as *const c_char) },
// string contains a nul byte - it won't match anything.
Err(_) => ll::SDL_CONTROLLER_AXIS_INVALID
};
Expand Down Expand Up @@ -169,7 +169,7 @@ impl Button {
/// used by the game controller mapping strings.
pub fn from_string(button: &str) -> Option<Button> {
let id = match CString::new(button) {
Ok(button) => unsafe { ll::SDL_GameControllerGetButtonFromString(button.as_ptr()) },
Ok(button) => unsafe { ll::SDL_GameControllerGetButtonFromString(button.as_ptr() as *const c_char) },
// string contains a nul byte - it won't match anything.
Err(_) => ll::SDL_CONTROLLER_BUTTON_INVALID
};
Expand Down
4 changes: 2 additions & 2 deletions src/sdl2/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ impl Event {

let text = String::from_utf8_lossy(
&event.text.iter()
.take_while(|&b| (*b) != 0i8)
.take_while(|&b| (*b) != 0)
.map(|&b| b as u8)
.collect::<Vec<u8>>()
).to_owned().into_owned();
Expand All @@ -679,7 +679,7 @@ impl Event {

let text = String::from_utf8_lossy(
&event.text.iter()
.take_while(|&b| (*b) != 0i8)
.take_while(|&b| (*b) != 0)
.map(|&b| b as u8)
.collect::<Vec<u8>>()
).to_owned().into_owned();
Expand Down
3 changes: 2 additions & 1 deletion src/sdl2/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::ffi::{CStr, CString};
use SdlResult;
use get_error;
use util::CStringExt;
use libc::c_char;

use sys::filesystem as ll;

Expand All @@ -22,7 +23,7 @@ pub fn pref_path(org: &str, app: &str) -> SdlResult<String> {
let result = unsafe {
let org = try!(CString::new(org).unwrap_or_sdlresult());
let app = try!(CString::new(app).unwrap_or_sdlresult());
let buf = ll::SDL_GetPrefPath(org.as_ptr(), app.as_ptr());
let buf = ll::SDL_GetPrefPath(org.as_ptr() as *const c_char, app.as_ptr() as *const c_char);
String::from_utf8_lossy(CStr::from_ptr(buf).to_bytes()).to_string()
};

Expand Down
7 changes: 4 additions & 3 deletions src/sdl2/hint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::ffi::{CString, CStr};
use sys::hint as ll;
use std::ptr;
use libc::c_char;

pub enum Hint {
Default,
Expand All @@ -12,7 +13,7 @@ pub fn set(name: &str, value: &str) -> bool{
let name = CString::new(name).unwrap();
let value = CString::new(value).unwrap();
unsafe {
ll::SDL_SetHint(name.as_ptr(), value.as_ptr()) == 1
ll::SDL_SetHint(name.as_ptr() as *const c_char, value.as_ptr() as *const c_char) == 1
}
}

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

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

if res == ptr::null_mut() {
None
Expand All @@ -43,6 +44,6 @@ pub fn set_with_priority(name: &str, value: &str, priority: Hint) -> bool {
};

unsafe {
ll::SDL_SetHintWithPriority(name.as_ptr(), value.as_ptr(), priority_val) == 1
ll::SDL_SetHintWithPriority(name.as_ptr() as *const c_char, value.as_ptr() as *const c_char, priority_val) == 1
}
}
2 changes: 1 addition & 1 deletion src/sdl2/joystick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ impl Guid {
pub fn from_string(guid: &str) -> Result<Guid, NulError> {
let guid = try!(CString::new(guid));

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

Ok(Guid { raw: raw })
}
Expand Down
3 changes: 2 additions & 1 deletion src/sdl2/keyboard/keycode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use num::{ToPrimitive, FromPrimitive};
use std::ffi::{CString, CStr};
use libc::c_char;

use sys::keycode as ll;

Expand Down Expand Up @@ -533,7 +534,7 @@ impl Keycode {
pub fn from_name(name: &str) -> Option<Keycode> {
unsafe {
match CString::new(name) {
Ok(name) => match ::sys::keyboard::SDL_GetKeyFromName(name.as_ptr()) {
Ok(name) => match ::sys::keyboard::SDL_GetKeyFromName(name.as_ptr() as *const c_char) {
ll::SDLK_UNKNOWN => None,
keycode_id => Some(FromPrimitive::from_isize(keycode_id as isize).unwrap())
},
Expand Down
3 changes: 2 additions & 1 deletion src/sdl2/keyboard/scancode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use num::{ToPrimitive, FromPrimitive};
use std::ffi::{CString, CStr};
use libc::c_char;

use sys::scancode as ll;

Expand Down Expand Up @@ -543,7 +544,7 @@ impl Scancode {
pub fn from_name(name: &str) -> Option<Scancode> {
unsafe {
match CString::new(name) {
Ok(name) => match ::sys::keyboard::SDL_GetScancodeFromName(name.as_ptr()) {
Ok(name) => match ::sys::keyboard::SDL_GetScancodeFromName(name.as_ptr() as *const c_char) {
ll::SDL_SCANCODE_UNKNOWN => None,
scancode_id => Some(FromPrimitive::from_isize(scancode_id as isize).unwrap())
},
Expand Down
5 changes: 3 additions & 2 deletions src/sdl2/messagebox.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::ffi::CString;
use std::ptr;
use libc::c_char;

use video::Window;
use get_error;
Expand All @@ -21,8 +22,8 @@ pub fn show_simple_message_box(flags: MessageBoxFlag, title: &str, message: &str
let title = CString::new(title).remove_nul();
let message = CString::new(message).remove_nul();
ll::SDL_ShowSimpleMessageBox(flags.bits(),
title.as_ptr(),
message.as_ptr(),
title.as_ptr() as *const c_char,
message.as_ptr() as *const c_char,
window.map_or(ptr::null_mut(), |win| win.raw()))
} == 0;

Expand Down
4 changes: 2 additions & 2 deletions src/sdl2/rwops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ffi::CString;
use std::io;
use std::path::Path;
use std::marker::PhantomData;
use libc::{c_void, c_int, size_t};
use libc::{c_void, c_int, size_t, c_char};
use get_error;
use SdlResult;

Expand All @@ -29,7 +29,7 @@ impl<'a> RWops<'a> {
let raw = unsafe {
let path_c = CString::new(path.as_ref().to_str().unwrap()).unwrap();
let mode_c = CString::new(mode).unwrap();
ll::SDL_RWFromFile(path_c.as_ptr(), mode_c.as_ptr())
ll::SDL_RWFromFile(path_c.as_ptr() as *const c_char, mode_c.as_ptr() as *const c_char)
};

if raw.is_null() {
Expand Down
3 changes: 2 additions & 1 deletion src/sdl2/sdl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::ffi::{CStr, CString};
use std::rc::Rc;
use std::fmt;
use std::error;
use libc::c_char;

use sys::sdl as ll;
use util::CStringExt;
Expand Down Expand Up @@ -315,7 +316,7 @@ pub fn get_error() -> ErrorMessage {

pub fn set_error(err: &str) {
let err = CString::new(err).remove_nul();
unsafe { ll::SDL_SetError(err.as_ptr()); }
unsafe { ll::SDL_SetError(err.as_ptr() as *const c_char); }
}

pub fn set_error_from_code(err: Error) {
Expand Down
12 changes: 6 additions & 6 deletions src/sdl2/video.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use libc::{c_int, c_float, uint32_t};
use libc::{c_int, c_float, uint32_t, c_char};
use std::ffi::{CStr, CString};
use std::mem;
use std::ops::{Deref, DerefMut};
Expand Down Expand Up @@ -601,7 +601,7 @@ impl VideoSubsystem {
unsafe {
// TODO: use OsStr::to_cstring() once it's stable
let path = CString::new(path.as_ref().to_str().unwrap()).unwrap();
if ll::SDL_GL_LoadLibrary(path.as_ptr()) == 0 {
if ll::SDL_GL_LoadLibrary(path.as_ptr() as *const c_char) == 0 {
Ok(())
} else {
Err(get_error())
Expand All @@ -622,15 +622,15 @@ impl VideoSubsystem {
/// This is useful for OpenGL wrappers such as [`gl-rs`](https://github.com/bjz/gl-rs).
pub fn gl_get_proc_address(&self, procname: &str) -> *const () {
match CString::new(procname) {
Ok(procname) => unsafe { ll::SDL_GL_GetProcAddress(procname.as_ptr()) as *const () },
Ok(procname) => unsafe { ll::SDL_GL_GetProcAddress(procname.as_ptr() as *const c_char) as *const () },
// string contains a nul byte - it won't match anything.
Err(_) => ptr::null()
}
}

pub fn gl_extension_supported(&self, extension: &str) -> bool {
match CString::new(extension) {
Ok(extension) => unsafe { ll::SDL_GL_ExtensionSupported(extension.as_ptr()) != 0 },
Ok(extension) => unsafe { ll::SDL_GL_ExtensionSupported(extension.as_ptr() as *const c_char) != 0 },
// string contains a nul byte - it won't match anything.
Err(_) => false
}
Expand Down Expand Up @@ -711,7 +711,7 @@ impl WindowBuilder {
let raw_height = self.height as c_int;

let raw = ll::SDL_CreateWindow(
self.title.as_ptr(),
self.title.as_ptr() as *const c_char,
unwrap_windowpos(self.x),
unwrap_windowpos(self.y),
raw_width,
Expand Down Expand Up @@ -938,7 +938,7 @@ impl WindowRef {

pub fn set_title(&mut self, title: &str) {
let title = CString::new(title).remove_nul();
unsafe { ll::SDL_SetWindowTitle(self.raw(), title.as_ptr()); }
unsafe { ll::SDL_SetWindowTitle(self.raw(), title.as_ptr() as *const c_char); }
}

pub fn title(&self) -> &str {
Expand Down

5 comments on commit 4a8764e

@MagaTailor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn! The fix for 1.6 does not work for 1.5 :( (which would've been affected by libc 0.2 update too)
For example:

/home/odroid/.cargo-1.5/registry/src/github.com-48ad6e4054423464/sdl2-0.12.0/src/sdl2/clipboard.rs:52:59: 52:62 error: mismatched types:
 expected `*const i8`,
    found `*const u8`
(expected i8,
    found u8) [E0308]
/home/odroid/.cargo-1.5/registry/src/github.com-48ad6e4054423464/sdl2-0.12.0/src/sdl2/clipboard.rs:52                 Ok(String::from_utf8_lossy(CStr::from_ptr(buf).to_bytes()).into_owned())

and so on, you know the drill.

An easy fix in some other crates seems to be as *const _

@MagaTailor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll give you an example where 1.5 still works with sdl2, (unlike 1.6):

sokoban-rs which still uses sdl2 0.8 and has no dependencies on libc 0.2 (just 0.1)

@AngryLawyer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a pop at it, unless you're feeling brave enough to do a PR :)

@MagaTailor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll see what I can do. If it compiles on both versions I'll create a PR.

@AngryLawyer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, cheers

Please sign in to comment.