Merge pull request #472 from AngryLawyer/arm-friendly

Make various functions prefer c_char over native Rust u8/i8, for cross-platform compilation
This commit is contained in:
Tony Aldridge
2015-11-27 16:45:23 +00:00
15 changed files with 36 additions and 29 deletions
+2 -2
View File
@@ -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"]
@@ -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"
+1 -1
View File
@@ -95,7 +95,7 @@ download through Crates.io:
```toml
[dependencies]
sdl2 = "0.11"
sdl2 = "0.12"
```
Alternatively, pull it from GitHub
+2 -2
View File
@@ -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;
@@ -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())
+2 -1
View File
@@ -1,4 +1,5 @@
use std::ffi::{CString, CStr};
use libc::c_char;
use SdlResult;
use get_error;
@@ -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())
+3 -3
View File
@@ -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),
@@ -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
};
@@ -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
};
+2 -2
View File
@@ -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();
@@ -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();
+2 -1
View File
@@ -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;
@@ -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()
};
+4 -3
View File
@@ -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,
@@ -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
}
}
@@ -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
@@ -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
}
}
+1 -1
View File
@@ -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 })
}
+2 -1
View File
@@ -1,5 +1,6 @@
use num::{ToPrimitive, FromPrimitive};
use std::ffi::{CString, CStr};
use libc::c_char;
use sys::keycode as ll;
@@ -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())
},
+2 -1
View File
@@ -1,5 +1,6 @@
use num::{ToPrimitive, FromPrimitive};
use std::ffi::{CString, CStr};
use libc::c_char;
use sys::scancode as ll;
@@ -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())
},
+3 -2
View File
@@ -1,5 +1,6 @@
use std::ffi::CString;
use std::ptr;
use libc::c_char;
use video::Window;
use get_error;
@@ -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;
+2 -2
View File
@@ -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;
@@ -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() {
+2 -1
View File
@@ -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;
@@ -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) {
+6 -6
View File
@@ -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};
@@ -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())
@@ -622,7 +622,7 @@ 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()
}
@@ -630,7 +630,7 @@ impl VideoSubsystem {
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
}
@@ -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,
@@ -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 {