Add patches for rust-sdl crate
authorKostya Shishkov <kostya.shishkov@gmail.com>
Sat, 30 Nov 2019 10:23:41 +0000 (11:23 +0100)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Sat, 30 Nov 2019 10:23:41 +0000 (11:23 +0100)
sdl-patches/0001-remove-not-really-needed-dependencies.patch [new file with mode: 0644]
sdl-patches/0002-remove-obsolete-directives.patch [new file with mode: 0644]
sdl-patches/0003-audio-make-audio-callback-a-trait-instead-of-a-funct.patch [new file with mode: 0644]
sdl-patches/0004-video-add-YUV-overlay-support.patch [new file with mode: 0644]

diff --git a/sdl-patches/0001-remove-not-really-needed-dependencies.patch b/sdl-patches/0001-remove-not-really-needed-dependencies.patch
new file mode 100644 (file)
index 0000000..e1cf979
--- /dev/null
@@ -0,0 +1,151 @@
+From 4d9e740dfd80df976d976e21a7e33fd9f8b3c362 Mon Sep 17 00:00:00 2001
+From: Kostya Shishkov <kostya.shishkov@gmail.com>
+Date: Fri, 22 Nov 2019 17:47:54 +0100
+Subject: [PATCH 1/4] remove not really needed dependencies
+
+---
+ Cargo.toml       |    2 --
+ src/sdl/event.rs |   20 +++++++-------------
+ src/sdl/lib.rs   |    2 --
+ src/sdl/video.rs |    8 --------
+ 4 files changed, 7 insertions(+), 25 deletions(-)
+
+diff --git a/Cargo.toml b/Cargo.toml
+index b53c715..ddf8523 100644
+--- a/Cargo.toml
++++ b/Cargo.toml
+@@ -16,6 +16,4 @@ name = "sdl"
+ path = "src/sdl/lib.rs"
+ [dependencies]
+-num = "0.1.24"
+-rand = "0.3"
+ libc = "0.1"
+diff --git a/src/sdl/event.rs b/src/sdl/event.rs
+index 17e2cff..12812f8 100644
+--- a/src/sdl/event.rs
++++ b/src/sdl/event.rs
+@@ -1,7 +1,6 @@
+ use std::mem;
+ use libc::c_int;
+ use std::slice;
+-use num::FromPrimitive;
+ use std::ffi::CStr;
+ use std::str;
+@@ -512,7 +511,7 @@ pub enum Key {
+     Last,
+ }
+-impl FromPrimitive for Key {
++impl Key {
+     fn from_i64(n: i64) -> Option<Key> {
+         use self::Key::*;
+@@ -753,11 +752,11 @@ impl FromPrimitive for Key {
+         })
+     }
+-    fn from_u64(n: u64) -> Option<Key> { FromPrimitive::from_i64(n as i64) }
++    fn from_u64(n: u64) -> Option<Key> { Self::from_i64(n as i64) }
+ }
+ fn wrap_key(i: ll::SDLKey) -> Option<Key> {
+-    FromPrimitive::from_usize(i as usize)
++    Key::from_u64(i as u64)
+ }
+ #[derive(PartialEq, Eq, Copy, Clone)]
+@@ -829,7 +828,7 @@ pub enum Mouse {
+     WheelDown
+ }
+-impl FromPrimitive for Mouse {
++impl Mouse {
+     fn from_i64(n: i64) -> Option<Mouse> {
+         Some(match n {
+             1 => Mouse::Left,
+@@ -840,12 +839,10 @@ impl FromPrimitive for Mouse {
+             _ => return None,
+         })
+     }
+-
+-    fn from_u64(n: u64) -> Option<Mouse> { FromPrimitive::from_i64(n as i64) }
+ }
+ fn wrap_mouse(bitflags: u8) -> Option<Mouse> {
+-    FromPrimitive::from_u8(bitflags)
++    Mouse::from_i64(bitflags as i64)
+ }
+ #[derive(PartialEq, Eq, Copy, Clone)]
+@@ -903,7 +900,7 @@ fn wrap_event(raw: ll::SDL_Event) -> Event {
+         let ty = if ty.is_null() { return Event::None; }
+                  else { *ty };
+-        let ty : EventType = match FromPrimitive::from_usize(ty as usize) {
++        let ty : EventType = match EventType::from_u64(ty as u64) {
+             Some(ty) => ty,
+             None => return Event::None
+         };
+@@ -1022,9 +1019,6 @@ pub enum EventType {
+ impl EventType {
+     pub fn get_state(&self) -> bool { get_event_state(*self) }
+     pub fn set_state(&self, state: bool) { set_event_state(*self, state) }
+-}
+-
+-impl FromPrimitive for EventType {
+     fn from_i64(n: i64) -> Option<EventType> {
+         Some(match n as ll::SDL_EventType {
+             ll::SDL_NOEVENT => EventType::None,
+@@ -1048,7 +1042,7 @@ impl FromPrimitive for EventType {
+         })
+     }
+-    fn from_u64(n: u64) -> Option<EventType> { FromPrimitive::from_i64(n as i64) }
++    fn from_u64(n: u64) -> Option<EventType> { Self::from_i64(n as i64) }
+ }
+ pub fn pump_events() {
+diff --git a/src/sdl/lib.rs b/src/sdl/lib.rs
+index cf04157..0f2a910 100644
+--- a/src/sdl/lib.rs
++++ b/src/sdl/lib.rs
+@@ -1,8 +1,6 @@
+ #![allow(raw_pointer_derive)]
+ extern crate libc;
+-extern crate rand;
+-extern crate num;
+ pub use sdl::*;
+diff --git a/src/sdl/video.rs b/src/sdl/video.rs
+index 3f7020a..64084f8 100644
+--- a/src/sdl/video.rs
++++ b/src/sdl/video.rs
+@@ -1,7 +1,6 @@
+ use std::mem;
+ use libc::{c_int, c_float};
+ use std::ptr;
+-use rand::Rng;
+ use std::slice;
+ use std::ffi::CString;
+ use std::path::Path;
+@@ -305,13 +304,6 @@ pub enum Color {
+     RGBA(u8, u8, u8, u8)
+ }
+-impl ::rand::Rand for Color {
+-    fn rand<R: ::rand::Rng>(rng: &mut R) -> Color {
+-        if rng.gen() { RGBA(rng.gen(), rng.gen(), rng.gen(), rng.gen()) }
+-        else { RGB(rng.gen(), rng.gen(), rng.gen()) }
+-    }
+-}
+-
+ impl Color {
+     pub fn from_mapped(bit: u32, fmt: *const ll::SDL_PixelFormat) -> Color {
+         let mut r = 0;
+-- 
+1.7.9.5
+
diff --git a/sdl-patches/0002-remove-obsolete-directives.patch b/sdl-patches/0002-remove-obsolete-directives.patch
new file mode 100644 (file)
index 0000000..44c28e9
--- /dev/null
@@ -0,0 +1,35 @@
+From 03cecd007d1b5688af83f0c2dfcc0d94f04044a2 Mon Sep 17 00:00:00 2001
+From: Kostya Shishkov <kostya.shishkov@gmail.com>
+Date: Fri, 22 Nov 2019 17:49:15 +0100
+Subject: [PATCH 2/4] remove obsolete directives
+
+---
+ src/sdl/lib.rs   |    2 --
+ src/sdl/video.rs |    1 -
+ 2 files changed, 3 deletions(-)
+
+diff --git a/src/sdl/lib.rs b/src/sdl/lib.rs
+index 0f2a910..111ccb2 100644
+--- a/src/sdl/lib.rs
++++ b/src/sdl/lib.rs
+@@ -1,5 +1,3 @@
+-#![allow(raw_pointer_derive)]
+-
+ extern crate libc;
+ pub use sdl::*;
+diff --git a/src/sdl/video.rs b/src/sdl/video.rs
+index 64084f8..710a1d6 100644
+--- a/src/sdl/video.rs
++++ b/src/sdl/video.rs
+@@ -204,7 +204,6 @@ impl Drop for Surface {
+     }
+ }
+-#[allow(raw_pointer_derive)]
+ #[derive(PartialEq, Copy, Clone)]
+ pub struct Palette {
+     pub raw: *mut ll::SDL_Palette
+-- 
+1.7.9.5
+
diff --git a/sdl-patches/0003-audio-make-audio-callback-a-trait-instead-of-a-funct.patch b/sdl-patches/0003-audio-make-audio-callback-a-trait-instead-of-a-funct.patch
new file mode 100644 (file)
index 0000000..3b324b4
--- /dev/null
@@ -0,0 +1,61 @@
+From 733f792c6c9d6c850243e794551002358a0b74a5 Mon Sep 17 00:00:00 2001
+From: Kostya Shishkov <kostya.shishkov@gmail.com>
+Date: Wed, 27 Nov 2019 08:22:16 +0100
+Subject: [PATCH 3/4] audio: make audio callback a trait instead of a function
+
+---
+ src/sdl/audio.rs |   17 ++++++-----------
+ 1 file changed, 6 insertions(+), 11 deletions(-)
+
+diff --git a/src/sdl/audio.rs b/src/sdl/audio.rs
+index 18a140f..0c2f6cf 100644
+--- a/src/sdl/audio.rs
++++ b/src/sdl/audio.rs
+@@ -93,15 +93,16 @@ impl Channels {
+     pub fn count(self) -> c_int          { match self { Channels::Mono => 1, Channels::Stereo => 2 } }
+ }
+-pub type AudioCallback = fn(&mut [u8]);
++pub trait AudioCallback {
++    fn callback(&mut self, out: &mut [u8]);
++}
+-#[derive(Copy)]
+ pub struct DesiredAudioSpec {
+     pub freq: c_int,
+     pub format: AudioFormat,
+     pub channels: Channels,
+     pub samples: u16,
+-    pub callback: AudioCallback,
++    pub callback: Box<AudioCallback>,
+ }
+ impl DesiredAudioSpec {
+@@ -123,12 +124,6 @@ impl DesiredAudioSpec {
+     }
+ }
+-impl Clone for DesiredAudioSpec {
+-    fn clone(&self) -> DesiredAudioSpec {
+-        *self
+-    }
+-}
+-
+ #[derive(Copy, Clone)]
+ pub struct ObtainedAudioSpec {
+     pub freq: c_int,
+@@ -154,9 +149,9 @@ impl ObtainedAudioSpec {
+ extern fn native_callback(userdata: *const c_void, stream: *mut u8, len: c_int) {
+     unsafe {
+-        let callback: Box<AudioCallback> = transmute(userdata);
++        let mut callback: Box<Box<AudioCallback>> = transmute(userdata);
+         let buffer = transmute((stream, len as usize));
+-        (*callback)(buffer);
++        callback.callback(buffer);
+         forget(callback);   // Don't free the callback!
+     }
+ }
+-- 
+1.7.9.5
+
diff --git a/sdl-patches/0004-video-add-YUV-overlay-support.patch b/sdl-patches/0004-video-add-YUV-overlay-support.patch
new file mode 100644 (file)
index 0000000..74c7201
--- /dev/null
@@ -0,0 +1,180 @@
+From a7e89f88df1bff23df6314d592d8e375dfae4048 Mon Sep 17 00:00:00 2001
+From: Kostya Shishkov <kostya.shishkov@gmail.com>
+Date: Wed, 27 Nov 2019 08:22:47 +0100
+Subject: [PATCH 4/4] video: add YUV overlay support
+
+---
+ src/sdl/video.rs |  118 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
+ 1 file changed, 116 insertions(+), 2 deletions(-)
+
+diff --git a/src/sdl/video.rs b/src/sdl/video.rs
+index 710a1d6..9acfcb6 100644
+--- a/src/sdl/video.rs
++++ b/src/sdl/video.rs
+@@ -51,6 +51,26 @@ pub mod ll {
+         pub refcount: c_int
+     }
++    pub const SDL_YV12_OVERLAY: uint32_t = 0x32315659;
++    pub const SDL_IYUV_OVERLAY: uint32_t = 0x56555949;
++    pub const SDL_YUY2_OVERLAY: uint32_t = 0x32595559;
++    pub const SDL_UYVY_OVERLAY: uint32_t = 0x59565955;
++    pub const SDL_YVYU_OVERLAY: uint32_t = 0x55595659;
++
++    #[repr(C)]
++    #[derive(Copy, Clone)]
++    pub struct SDL_Overlay {
++        pub format: uint32_t,
++        pub w: c_int,
++        pub h: c_int,
++        pub planes: c_int,
++        pub pitches: *const uint16_t,
++        pub pixels: *const *mut uint8_t,
++        pub hwfuncs: *mut c_void,
++        pub hwdata: *mut c_void,
++        pub flags: uint32_t,
++    }
++
+     #[repr(C)]
+     #[derive(Copy, Clone)]
+     pub struct SDL_Color {
+@@ -109,7 +129,7 @@ pub mod ll {
+                                     Gmask: uint32_t,
+                                     Bmask: uint32_t,
+                                     Amask: uint32_t) -> *mut SDL_Surface;
+-        pub fn SDL_CreateRGBSurfaceFrom(pixels: *mut c_void,
++        pub fn SDL_CreateRGBSurfaceFrom(pixels: *const c_void,
+                                         width: c_int,
+                                         height: c_int,
+                                         depth: c_int,
+@@ -181,6 +201,14 @@ pub mod ll {
+         pub fn SDL_LoadBMP_RW(src: *mut SDL_RWops, freesrc: c_int) -> *mut SDL_Surface;
+         pub fn SDL_SaveBMP_RW(surface: *mut SDL_Surface, dst: *mut SDL_RWops, freedst: c_int) -> c_int;
+         pub fn SDL_GL_SwapBuffers();
++
++        pub fn SDL_CreateYUVOverlay(width: c_int, height: c_int, format: uint32_t, display: *mut SDL_Surface)
++                        -> *mut SDL_Overlay;
++        pub fn SDL_LockYUVOverlay(overlay: *mut SDL_Overlay) -> c_int;
++        pub fn SDL_UnlockYUVOverlay(overlay: *mut SDL_Overlay);
++        pub fn SDL_DisplayYUVOverlay(overlay: *mut SDL_Overlay,
++                             dstrect: *mut SDL_Rect) -> c_int;
++        pub fn SDL_FreeYUVOverlay(overlay: *mut SDL_Overlay);
+     }
+ }
+@@ -351,6 +379,7 @@ pub enum SurfaceFlag {
+     SWSurface = 0x00000000,
+     HWSurface = 0x00000001,
+     AsyncBlit = 0x00000004,
++    HWAccel   = 0x00000100,
+     SrcColorKey = 0x00001000,
+     SrcAlpha = 0x00010000,
+     RLEAccel = 0x00004000
+@@ -466,6 +495,15 @@ pub fn get_video_surface() -> Result<Surface, String> {
+     else { Ok(wrap_surface(raw, false)) }
+ }
++#[derive(PartialEq, Eq, Copy, Clone)]
++pub enum OverlayFormat {
++    YV12,
++    IYUV,
++    YUY2,
++    UYVY,
++    YVYU,
++}
++
+ // TODO: get_video_modes, get_video_driver_name
+ impl Surface {
+@@ -485,6 +523,38 @@ impl Surface {
+         }
+     }
++    pub fn new_from(pixels: &[u8], pitch: i32, width: isize, height: isize, bpp: isize,
++               rmask: u32, gmask: u32, bmask: u32, amask: u32) -> Result<Surface, String> {
++        unsafe {
++            let raw = ll::SDL_CreateRGBSurfaceFrom(pixels.as_ptr() as *const libc::c_void, width as c_int, height as c_int, bpp as c_int, pitch as c_int,
++                                               rmask, gmask, bmask, amask);
++
++            if raw.is_null() {
++                Err(get_error())
++            } else {
++                Ok(Surface { raw: raw, owned: true })
++            }
++        }
++    }
++
++    pub fn create_overlay(&self, width: isize, height: isize, format: OverlayFormat) -> Result<Overlay, String> {
++        unsafe {
++            let yuv_fmt = match format {
++                    OverlayFormat::YV12 => ll::SDL_YV12_OVERLAY,
++                    OverlayFormat::IYUV => ll::SDL_IYUV_OVERLAY,
++                    OverlayFormat::YUY2 => ll::SDL_YUY2_OVERLAY,
++                    OverlayFormat::UYVY => ll::SDL_UYVY_OVERLAY,
++                    OverlayFormat::YVYU => ll::SDL_YVYU_OVERLAY,
++                };
++            let raw = ll::SDL_CreateYUVOverlay(width as c_int, height as c_int, yuv_fmt, self.raw);
++            if raw.is_null() {
++                Err(get_error())
++            } else {
++                Ok(Overlay { raw: raw })
++            }
++        }
++    }
++
+     pub fn from_bmp(path: &Path) -> Result<Surface, String> {
+         let cpath = CString::new(path.to_str().unwrap()).unwrap();
+         let mode = CString::new("rb".as_bytes()).unwrap();
+@@ -742,4 +812,48 @@ pub fn swap_buffers() {
+ }
+-// TODO: YUV
++#[derive(PartialEq)]
++pub struct Overlay {
++    pub raw: *mut ll::SDL_Overlay,
++}
++
++impl Drop for Overlay {
++    fn drop(&mut self) {
++        unsafe {
++            ll::SDL_FreeYUVOverlay(self.raw);
++        }
++    }
++}
++
++impl Overlay {
++    pub fn display(&self, dest_rect: Option<Rect>) -> bool {
++        unsafe {
++            ll::SDL_DisplayYUVOverlay(self.raw, match dest_rect {
++                Some(ref rect) => mem::transmute(rect),
++                None => ptr::null_mut()
++            }) == 0
++        }
++    }
++
++    pub fn lock(&self) -> bool {
++        unsafe { ll::SDL_LockYUVOverlay(self.raw) == 0 }
++    }
++
++    pub fn unlock(&self) {
++        unsafe { ll::SDL_UnlockYUVOverlay(self.raw) }
++    }
++
++    pub unsafe fn get_pixel_ptr(&self, comp: usize) -> &mut [u8] {
++        let pitch = self.get_pitch(comp);
++        let len = if comp == 0 { pitch as usize * ((*self.raw).h as usize) } else
++                    { pitch as usize * (((*self.raw).h / 2) as usize) };
++        let ptr = *((*self.raw).pixels.add(comp));
++        let pixels: &mut [u8] = mem::transmute((ptr, len));
++
++        pixels
++    }
++
++    pub fn get_pitch(&self, comp: usize) -> usize {
++        unsafe { *((*self.raw).pitches.add(comp)) as usize }
++    }
++}
+-- 
+1.7.9.5
+