Enable no_std for sdl2-sys

This commit is contained in:
Cobrand
2019-03-23 15:04:53 +00:00
parent 0ebf34495f
commit 1d5f701d0f
40 changed files with 4472 additions and 4941 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ use sdl2::mouse::MouseButton;
use sdl2::keyboard::Keycode;
use sdl2::video::{Window, WindowContext};
use sdl2::render::{Canvas, Texture, TextureCreator};
use game_of_life::{SQUARE_SIZE, PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT};
use crate::game_of_life::{SQUARE_SIZE, PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT};
mod game_of_life {
pub const SQUARE_SIZE: u32 = 16;
+1 -1
View File
@@ -37,7 +37,7 @@ fn set_window_gradient(window: &mut Window, event_pump: &sdl2::EventPump, gradie
}
fn next_gradient(gradient: Gradient) -> Gradient {
use Gradient::*;
use crate::Gradient::*;
match gradient {
Red => Cyan,
Cyan => Green,
+4
View File
@@ -10,11 +10,15 @@ categories = ["rendering","external-ffi-bindings","game-engines","multimedia"]
license = "MIT"
links = "SDL2"
build = "build.rs"
edition = "2018"
[lib]
name = "sdl2_sys"
path = "src/lib.rs"
[dependencies]
libc = "^0.2"
[build-dependencies.bindgen]
version = "^0.42"
optional = true
+4 -1
View File
@@ -535,7 +535,10 @@ fn copy_pregenerated_bindings() {
// to be found by bindgen (should point to the include/ directories)
fn generate_bindings<S: AsRef<str> + ::std::fmt::Debug>(target: &str, host: &str, headers_paths: &[S]) {
let target_os = get_os_from_triple(target).unwrap();
let mut bindings = bindgen::Builder::default();
let mut bindings = bindgen::Builder::default()
// enable no_std-friendly output by only using core definitions
.use_core()
.ctypes_prefix("libc");
let mut image_bindings = bindgen::Builder::default();
+33
View File
@@ -0,0 +1,33 @@
#![no_std]
use sdl2_sys::*;
use core::ptr::null_mut;
fn main() {
unsafe {
let mut _window: *mut SDL_Window = null_mut();
let mut _surface: *mut SDL_Surface = null_mut();
if SDL_Init(SDL_INIT_VIDEO) < 0 {
panic!("failed to initialize sdl2 with video");
};
_window = SDL_CreateWindow(
b"hello_sdl2" as *const _ as *const i8,
SDL_WINDOWPOS_UNDEFINED_MASK as i32,
SDL_WINDOWPOS_UNDEFINED_MASK as i32,
640,
480,
SDL_WindowFlags::SDL_WINDOW_SHOWN as u32
);
if _window == null_mut() {
panic!("failed to create window");
}
_surface = SDL_GetWindowSurface(_window);
SDL_FillRect(_surface, null_mut(), SDL_MapRGB((*_surface).format, 0xFF, 0xFF, 0x00));
SDL_UpdateWindowSurface(_window);
SDL_Delay(5000);
SDL_DestroyWindow(_window);
SDL_Quit();
}
}
+4225 -4734
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -620,7 +620,7 @@ extern "C" {
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn gfxPrimitivesSetFont(fontdata: *const ::std::os::raw::c_void, cw: Uint32, ch: Uint32);
pub fn gfxPrimitivesSetFont(fontdata: *const ::libc::c_void, cw: Uint32, ch: Uint32);
}
extern "C" {
pub fn gfxPrimitivesSetFontRotation(rotation: Uint32);
+11 -11
View File
@@ -200,31 +200,31 @@ extern "C" {
pub fn Mix_SetPostMix(
mix_func: ::std::option::Option<
unsafe extern "C" fn(
udata: *mut ::std::os::raw::c_void,
udata: *mut ::libc::c_void,
stream: *mut Uint8,
len: ::std::os::raw::c_int,
),
>,
arg: *mut ::std::os::raw::c_void,
arg: *mut ::libc::c_void,
);
}
extern "C" {
pub fn Mix_HookMusic(
mix_func: ::std::option::Option<
unsafe extern "C" fn(
udata: *mut ::std::os::raw::c_void,
udata: *mut ::libc::c_void,
stream: *mut Uint8,
len: ::std::os::raw::c_int,
),
>,
arg: *mut ::std::os::raw::c_void,
arg: *mut ::libc::c_void,
);
}
extern "C" {
pub fn Mix_HookMusicFinished(music_finished: ::std::option::Option<unsafe extern "C" fn()>);
}
extern "C" {
pub fn Mix_GetMusicHookData() -> *mut ::std::os::raw::c_void;
pub fn Mix_GetMusicHookData() -> *mut ::libc::c_void;
}
extern "C" {
pub fn Mix_ChannelFinished(
@@ -236,20 +236,20 @@ extern "C" {
pub type Mix_EffectFunc_t = ::std::option::Option<
unsafe extern "C" fn(
chan: ::std::os::raw::c_int,
stream: *mut ::std::os::raw::c_void,
stream: *mut ::libc::c_void,
len: ::std::os::raw::c_int,
udata: *mut ::std::os::raw::c_void,
udata: *mut ::libc::c_void,
),
>;
pub type Mix_EffectDone_t = ::std::option::Option<
unsafe extern "C" fn(chan: ::std::os::raw::c_int, udata: *mut ::std::os::raw::c_void),
unsafe extern "C" fn(chan: ::std::os::raw::c_int, udata: *mut ::libc::c_void),
>;
extern "C" {
pub fn Mix_RegisterEffect(
chan: ::std::os::raw::c_int,
f: Mix_EffectFunc_t,
d: Mix_EffectDone_t,
arg: *mut ::std::os::raw::c_void,
arg: *mut ::libc::c_void,
) -> ::std::os::raw::c_int;
}
extern "C" {
@@ -454,10 +454,10 @@ extern "C" {
function: ::std::option::Option<
unsafe extern "C" fn(
arg1: *const ::std::os::raw::c_char,
arg2: *mut ::std::os::raw::c_void,
arg2: *mut ::libc::c_void,
) -> ::std::os::raw::c_int,
>,
data: *mut ::std::os::raw::c_void,
data: *mut ::libc::c_void,
) -> ::std::os::raw::c_int;
}
extern "C" {
+1
View File
@@ -7,6 +7,7 @@
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![no_std]
include!(concat!(env!("OUT_DIR"), "/sdl_bindings.rs"));
+7 -8
View File
@@ -54,19 +54,18 @@
use std::ffi::{CStr, CString};
use num::FromPrimitive;
use libc::{c_int, uint8_t, c_char};
use std::os::raw::c_void;
use libc::{c_int, c_void, uint8_t, c_char};
use std::ops::{Deref, DerefMut};
use std::path::Path;
use std::marker::PhantomData;
use std::mem;
use std::ptr;
use AudioSubsystem;
use get_error;
use rwops::RWops;
use crate::AudioSubsystem;
use crate::get_error;
use crate::rwops::RWops;
use sys;
use crate::sys;
impl AudioSubsystem {
/// Opens a new audio device given the desired parameters and callback.
@@ -281,7 +280,7 @@ pub struct AudioSpecWAV {
impl AudioSpecWAV {
/// Loads a WAVE from the file path.
pub fn load_wav<P: AsRef<Path>>(path: P) -> Result<AudioSpecWAV, String> {
let mut file = try!(RWops::from_file(path, "rb"));
let mut file = r#try!(RWops::from_file(path, "rb"));
AudioSpecWAV::load_wav_rw(&mut file)
}
@@ -330,7 +329,7 @@ where Self::Channel: AudioFormatNum + 'static
{
type Channel;
fn callback(&mut self, &mut [Self::Channel]);
fn callback(&mut self, _: &mut [Self::Channel]);
}
/// A phantom type for retrieving the `SDL_AudioFormat` of a given generic type.
+5 -5
View File
@@ -1,9 +1,9 @@
use std::ffi::{CString, CStr};
use std::os::raw::c_void;
use libc::c_void;
use libc::c_char;
use get_error;
use crate::get_error;
use sys;
use crate::sys;
/// Clipboard utility functions. Access with `VideoSubsystem::clipboard()`.
///
@@ -16,10 +16,10 @@ use sys;
/// video_subsystem.clipboard().set_clipboard_text("Hello World!").unwrap();
/// ```
pub struct ClipboardUtil {
_subsystem: ::VideoSubsystem
_subsystem: crate::VideoSubsystem
}
impl ::VideoSubsystem {
impl crate::VideoSubsystem {
#[inline]
pub fn clipboard(&self) -> ClipboardUtil {
ClipboardUtil {
+10 -10
View File
@@ -4,15 +4,15 @@ use std::ffi::{CString, CStr, NulError};
use std::fmt;
use std::io;
use std::path::Path;
use rwops::RWops;
use crate::rwops::RWops;
use GameControllerSubsystem;
use get_error;
use joystick;
use common::{validate_int, IntegerOrSdlError};
use crate::GameControllerSubsystem;
use crate::get_error;
use crate::joystick;
use crate::common::{validate_int, IntegerOrSdlError};
use std::mem::transmute;
use sys;
use crate::sys;
#[derive(Debug)]
pub enum AddMappingError {
@@ -73,8 +73,8 @@ impl GameControllerSubsystem {
/// Controller IDs are the same as joystick IDs and the maximum number can
/// be retrieved using the `SDL_NumJoysticks` function.
pub fn open(&self, joystick_index: u32) -> Result<GameController, IntegerOrSdlError> {
use common::IntegerOrSdlError::*;
let joystick_index = try!(validate_int(joystick_index, "joystick_index"));
use crate::common::IntegerOrSdlError::*;
let joystick_index = r#try!(validate_int(joystick_index, "joystick_index"));
let controller = unsafe { sys::SDL_GameControllerOpen(joystick_index) };
if controller.is_null() {
@@ -89,8 +89,8 @@ impl GameControllerSubsystem {
/// Return the name of the controller at index `joystick_index`.
pub fn name_for_index(&self, joystick_index: u32) -> Result<String, IntegerOrSdlError> {
use common::IntegerOrSdlError::*;
let joystick_index = try!(validate_int(joystick_index, "joystick_index"));
use crate::common::IntegerOrSdlError::*;
let joystick_index = r#try!(validate_int(joystick_index, "joystick_index"));
let c_str = unsafe { sys::SDL_GameControllerNameForIndex(joystick_index) };
if c_str.is_null() {
+1 -1
View File
@@ -1,4 +1,4 @@
use sys;
use crate::sys;
pub const CACHELINESIZE: u8 = 128;
+27 -27
View File
@@ -13,21 +13,21 @@ use std::marker::PhantomData;
use std::collections::HashMap;
use std::sync::Mutex;
use std::mem::transmute;
use std::os::raw::c_void;
use libc::c_void;
use controller;
use controller::{Axis, Button};
use joystick;
use joystick::HatState;
use keyboard;
use keyboard::Mod;
use keyboard::Keycode;
use mouse;
use mouse::{MouseButton, MouseState, MouseWheelDirection};
use keyboard::Scancode;
use get_error;
use crate::controller;
use crate::controller::{Axis, Button};
use crate::joystick;
use crate::joystick::HatState;
use crate::keyboard;
use crate::keyboard::Mod;
use crate::keyboard::Keycode;
use crate::mouse;
use crate::mouse::{MouseButton, MouseState, MouseWheelDirection};
use crate::keyboard::Scancode;
use crate::get_error;
use sys;
use crate::sys;
struct CustomEventTypeMaps {
sdl_id_to_type_id: HashMap<u32, ::std::any::TypeId>,
@@ -47,7 +47,7 @@ lazy_static! {
static ref CUSTOM_EVENT_TYPES : Mutex<CustomEventTypeMaps> = { Mutex::new(CustomEventTypeMaps::new()) };
}
impl ::EventSubsystem {
impl crate::EventSubsystem {
/// Removes all events in the event queue that match the specified event type.
pub fn flush_event(&self, event_type: EventType) {
unsafe { sys::SDL_FlushEvent(event_type as uint32_t) };
@@ -144,8 +144,8 @@ impl ::EventSubsystem {
/// window_id: 0,
/// type_: custom_event_type_id,
/// code: 456,
/// data1: 0x1234 as *mut ::std::os::raw::c_void,
/// data2: 0x5678 as *mut ::std::os::raw::c_void,
/// data1: 0x1234 as *mut libc::c_void,
/// data2: 0x5678 as *mut libc::c_void,
/// };
///
/// ev.push_event(event);
@@ -153,7 +153,7 @@ impl ::EventSubsystem {
/// ```
#[inline(always)]
pub unsafe fn register_event(&self) -> Result<u32, String> {
Ok(*try!(self.register_events(1)).first().unwrap())
Ok(*r#try!(self.register_events(1)).first().unwrap())
}
/// Registers custom SDL events.
@@ -185,7 +185,7 @@ impl ::EventSubsystem {
pub fn register_custom_event<T: ::std::any::Any>(&self)
-> Result<(), String> {
use ::std::any::TypeId;
let event_id = *try!(unsafe { self.register_events(1) }).first().unwrap();
let event_id = *r#try!(unsafe { self.register_events(1) }).first().unwrap();
let mut cet = CUSTOM_EVENT_TYPES.lock().unwrap();
let type_id = TypeId::of::<Box<T>>();
@@ -254,7 +254,7 @@ impl ::EventSubsystem {
data2: ::std::ptr::null_mut()
};
try!(self.push_event(event));
r#try!(self.push_event(event));
Ok(())
}
@@ -319,7 +319,7 @@ pub enum EventType {
impl FromPrimitive for EventType {
fn from_i64(n: i64) -> Option<EventType> {
use self::EventType::*;
use sys::SDL_EventType::*;
use crate::sys::SDL_EventType::*;
let n = n as u32;
Some( match unsafe { transmute(n) } {
@@ -1683,7 +1683,7 @@ unsafe fn wait_event_timeout(timeout: u32) -> Option<Event> {
else { None }
}
impl ::EventPump {
impl crate::EventPump {
/// Query if an event type is enabled.
pub fn is_event_enabled(&self, event_type: EventType) -> bool {
let result = unsafe { sys::SDL_EventState(event_type as u32, sys::SDL_QUERY) };
@@ -1770,18 +1770,18 @@ impl ::EventPump {
}
#[inline]
pub fn keyboard_state(&self) -> ::keyboard::KeyboardState {
::keyboard::KeyboardState::new(self)
pub fn keyboard_state(&self) -> crate::keyboard::KeyboardState {
crate::keyboard::KeyboardState::new(self)
}
#[inline]
pub fn mouse_state(&self) -> ::mouse::MouseState {
::mouse::MouseState::new(self)
pub fn mouse_state(&self) -> crate::mouse::MouseState {
crate::mouse::MouseState::new(self)
}
#[inline]
pub fn relative_mouse_state(&self) -> ::mouse::RelativeMouseState {
::mouse::RelativeMouseState::new(self)
pub fn relative_mouse_state(&self) -> crate::mouse::RelativeMouseState {
crate::mouse::RelativeMouseState::new(self)
}
}
+3 -3
View File
@@ -1,11 +1,11 @@
use std::error;
use std::ffi::{CStr, CString, NulError};
use std::fmt;
use std::os::raw::c_void;
use get_error;
use libc::c_void;
use crate::get_error;
use libc::c_char;
use sys;
use crate::sys;
pub fn base_path() -> Result<String, String> {
let result = unsafe {
+1 -1
View File
@@ -1,2 +1,2 @@
#![allow(unused)]
use sys;
use crate::sys;
+1 -1
View File
@@ -5,7 +5,7 @@ use std::ptr;
use std::ffi::CString;
use num::traits::ToPrimitive;
use libc::{c_int, c_char};
use std::os::raw::c_void;
use libc::c_void;
use render::Canvas;
use surface::Surface;
use pixels;
+6 -6
View File
@@ -1,15 +1,15 @@
//! Haptic Functions
use sys;
use crate::sys;
use HapticSubsystem;
use common::{validate_int, IntegerOrSdlError};
use get_error;
use crate::HapticSubsystem;
use crate::common::{validate_int, IntegerOrSdlError};
use crate::get_error;
impl HapticSubsystem {
/// Attempt to open the joystick at index `joystick_index` and return its haptic device.
pub fn open_from_joystick_id(&self, joystick_index: u32) -> Result<Haptic, IntegerOrSdlError> {
use common::IntegerOrSdlError::*;
let joystick_index = try!(validate_int(joystick_index, "joystick_index"));
use crate::common::IntegerOrSdlError::*;
let joystick_index = r#try!(validate_int(joystick_index, "joystick_index"));
let haptic = unsafe {
let joystick = sys::SDL_JoystickOpen(joystick_index);
+1 -1
View File
@@ -1,5 +1,5 @@
use std::ffi::{CString, CStr};
use sys;
use crate::sys;
use libc::c_char;
const VIDEO_MINIMIZE_ON_FOCUS_LOSS: &'static str = "SDL_VIDEO_MINIMIZE_ON_FOCUS_LOSS";
+21 -21
View File
@@ -1,12 +1,12 @@
use sys;
use crate::sys;
use JoystickSubsystem;
use get_error;
use clear_error;
use crate::JoystickSubsystem;
use crate::get_error;
use crate::clear_error;
use std::ffi::{CString, CStr, NulError};
use std::fmt::{Display, Formatter, Error};
use libc::c_char;
use common::{validate_int, IntegerOrSdlError};
use crate::common::{validate_int, IntegerOrSdlError};
impl JoystickSubsystem {
/// Retrieve the total number of attached joysticks *and* controllers identified by SDL.
@@ -23,8 +23,8 @@ impl JoystickSubsystem {
/// Attempt to open the joystick at index `joystick_index` and return it.
pub fn open(&self, joystick_index: u32)
-> Result<Joystick, IntegerOrSdlError> {
use common::IntegerOrSdlError::*;
let joystick_index = try!(validate_int(joystick_index, "joystick_index"));
use crate::common::IntegerOrSdlError::*;
let joystick_index = r#try!(validate_int(joystick_index, "joystick_index"));
let joystick = unsafe { sys::SDL_JoystickOpen(joystick_index) };
@@ -40,8 +40,8 @@ impl JoystickSubsystem {
/// Return the name of the joystick at index `joystick_index`.
pub fn name_for_index(&self, joystick_index: u32) -> Result<String, IntegerOrSdlError> {
use common::IntegerOrSdlError::*;
let joystick_index = try!(validate_int(joystick_index, "joystick_index"));
use crate::common::IntegerOrSdlError::*;
let joystick_index = r#try!(validate_int(joystick_index, "joystick_index"));
let c_str = unsafe { sys::SDL_JoystickNameForIndex(joystick_index) };
@@ -56,8 +56,8 @@ impl JoystickSubsystem {
/// Get the GUID for the joystick at index `joystick_index`
pub fn device_guid(&self, joystick_index: u32) -> Result<Guid, IntegerOrSdlError> {
use common::IntegerOrSdlError::*;
let joystick_index = try!(validate_int(joystick_index, "joystick_index"));
use crate::common::IntegerOrSdlError::*;
let joystick_index = r#try!(validate_int(joystick_index, "joystick_index"));
let raw = unsafe { sys::SDL_JoystickGetDeviceGUID(joystick_index) };
@@ -178,7 +178,7 @@ impl Joystick {
/// Retrieve the battery level of this joystick
pub fn power_level(&self) -> Result<PowerLevel, IntegerOrSdlError> {
use common::IntegerOrSdlError::*;
use crate::common::IntegerOrSdlError::*;
clear_error();
let result = unsafe { sys::SDL_JoystickCurrentPowerLevel(self.raw) };
@@ -214,14 +214,14 @@ impl Joystick {
///
/// The function will fail if the joystick doesn't have the provided axis.
pub fn axis(&self, axis: u32) -> Result<i16, IntegerOrSdlError> {
use common::IntegerOrSdlError::*;
use crate::common::IntegerOrSdlError::*;
// This interface is a bit messed up: 0 is a valid position
// but can also mean that an error occured. As far as I can
// tell the only way to know if an error happened is to see if
// get_error() returns a non-empty string.
clear_error();
let axis = try!(validate_int(axis, "axis"));
let axis = r#try!(validate_int(axis, "axis"));
let pos = unsafe { sys::SDL_JoystickGetAxis(self.raw, axis) };
if pos != 0 {
@@ -253,12 +253,12 @@ impl Joystick {
///
/// The function will fail if the joystick doesn't have the provided button.
pub fn button(&self, button: u32) -> Result<bool, IntegerOrSdlError> {
use common::IntegerOrSdlError::*;
use crate::common::IntegerOrSdlError::*;
// Same deal as axis, 0 can mean both unpressed or
// error...
clear_error();
let button = try!(validate_int(button, "button"));
let button = r#try!(validate_int(button, "button"));
let pressed = unsafe { sys::SDL_JoystickGetButton(self.raw, button) };
match pressed {
@@ -293,11 +293,11 @@ impl Joystick {
/// Return a pair `(dx, dy)` containing the difference in axis
/// position since the last poll
pub fn ball(&self, ball: u32) -> Result<(i32, i32), IntegerOrSdlError> {
use common::IntegerOrSdlError::*;
use crate::common::IntegerOrSdlError::*;
let mut dx = 0;
let mut dy = 0;
let ball = try!(validate_int(ball, "ball"));
let ball = r#try!(validate_int(ball, "ball"));
let result = unsafe { sys::SDL_JoystickGetBall(self.raw, ball, &mut dx, &mut dy) };
if result == 0 {
@@ -321,13 +321,13 @@ impl Joystick {
/// Return the position of `hat` for this joystick
pub fn hat(&self, hat: u32) -> Result<HatState, IntegerOrSdlError> {
use common::IntegerOrSdlError::*;
use crate::common::IntegerOrSdlError::*;
// Guess what? This function as well uses 0 to report an error
// but 0 is also a valid value (HatState::Centered). So we
// have to use the same hack as `axis`...
clear_error();
let hat = try!(validate_int(hat, "hat"));
let hat = r#try!(validate_int(hat, "hat"));
let result = unsafe { sys::SDL_JoystickGetHat(self.raw, hat) };
let state = HatState::from_raw(result as u8);
@@ -403,7 +403,7 @@ impl Eq for Guid {}
impl Guid {
/// Create a GUID from a string representation.
pub fn from_string(guid: &str) -> Result<Guid, NulError> {
let guid = try!(CString::new(guid));
let guid = r#try!(CString::new(guid));
let raw = unsafe { sys::SDL_JoystickGetGUIDFromString(guid.as_ptr() as *const c_char) };

Some files were not shown because too many files have changed in this diff Show More