Commit d0c63e2b authored by Florian Blasius's avatar Florian Blasius 🤘
Browse files

implement drop event for sdl2

parent d0a1b0ec
Pipeline #8304 passed with stage
in 1 minute and 46 seconds
......@@ -13,6 +13,7 @@ pub const EVENT_RESIZE: i64 = 8;
pub const EVENT_SCREEN: i64 = 9;
pub const EVENT_CLIPBOARD: i64 = 10;
pub const EVENT_MOUSE_RELATIVE: i64 = 11;
pub const EVENT_DROP: i64 = 12;
/// An optional event
#[derive(Copy, Clone, Debug)]
......@@ -39,6 +40,8 @@ pub enum EventOption {
Screen(ScreenEvent),
/// A clipboard event
Clipboard(ClipboardEvent),
/// A drop file / text event (available on linux, windows and macOS)
Drop(DropEvent),
/// An unknown event
Unknown(Event),
/// No event
......@@ -71,7 +74,9 @@ impl Event {
EVENT_NONE => EventOption::None,
EVENT_KEY => EventOption::Key(KeyEvent::from_event(self)),
EVENT_MOUSE => EventOption::Mouse(MouseEvent::from_event(self)),
EVENT_MOUSE_RELATIVE => EventOption::MouseRelative(MouseRelativeEvent::from_event(self)),
EVENT_MOUSE_RELATIVE => {
EventOption::MouseRelative(MouseRelativeEvent::from_event(self))
}
EVENT_BUTTON => EventOption::Button(ButtonEvent::from_event(self)),
EVENT_SCROLL => EventOption::Scroll(ScrollEvent::from_event(self)),
EVENT_QUIT => EventOption::Quit(QuitEvent::from_event(self)),
......@@ -80,6 +85,7 @@ impl Event {
EVENT_RESIZE => EventOption::Resize(ResizeEvent::from_event(self)),
EVENT_SCREEN => EventOption::Screen(ScreenEvent::from_event(self)),
EVENT_CLIPBOARD => EventOption::Clipboard(ClipboardEvent::from_event(self)),
EVENT_DROP => EventOption::Drop(DropEvent::from_event(self)),
_ => EventOption::Unknown(self),
}
}
......@@ -511,3 +517,28 @@ impl ClipboardEvent {
}
}
}
pub const DROP_FILE: u8 = 0;
pub const DROP_TEXT: u8 = 1;
/// A drop file event.
#[derive(Copy, Clone, Debug)]
pub struct DropEvent {
pub kind: u8,
}
impl DropEvent {
pub fn to_event(&self) -> Event {
Event {
code: EVENT_DROP,
a: self.kind as i64,
b: 0,
}
}
pub fn from_event(event: Event) -> DropEvent {
DropEvent {
kind: event.a as u8,
}
}
}
......@@ -52,7 +52,7 @@ pub struct Window {
/// The title of the window
t: String,
/// True if the window should not wait for events
async: bool,
window_async: bool,
/// True if the window can be resized
resizable: bool,
/// Drawing mode
......@@ -112,12 +112,12 @@ impl Window {
) -> Option<Self> {
let mut flag_str = String::new();
let mut async = false;
let mut window_async = false;
let mut resizable = false;
for &flag in flags.iter() {
match flag {
WindowFlag::Async => {
async = true;
window_async = true;
flag_str.push('a');
}
WindowFlag::Back => flag_str.push('b'),
......@@ -136,19 +136,23 @@ impl Window {
"orbital:{}/{}/{}/{}/{}/{}",
flag_str, x, y, w, h, title
)) {
if let Ok(address) = unsafe { syscall::fmap(file.as_raw_fd() as usize, &syscall::Map {
offset: 0,
size: (w * h * 4) as usize,
flags: syscall::PROT_READ | syscall::PROT_WRITE,
}) }
{
if let Ok(address) = unsafe {
syscall::fmap(
file.as_raw_fd() as usize,
&syscall::Map {
offset: 0,
size: (w * h * 4) as usize,
flags: syscall::PROT_READ | syscall::PROT_WRITE,
},
)
} {
Some(Window {
x: x,
y: y,
w: w,
h: h,
t: title.to_string(),
async: async,
window_async,
resizable: resizable,
mode: Cell::new(Mode::Blend),
file: file,
......@@ -182,6 +186,11 @@ impl Window {
}
}
/// Not yet available on Redox OS.
pub fn pop_drop_content(&self) -> Option<String> {
None
}
// TODO: Replace with smarter mechanism, maybe a move event?
pub fn sync_path(&mut self) {
let mut buf: [u8; 4096] = [0; 4096];
......@@ -254,12 +263,15 @@ impl Window {
.write(&format!("S,{},{}", width, height).as_bytes());
self.sync_path();
unsafe {
let address = syscall::fmap(self.file.as_raw_fd() as usize, &syscall::Map {
offset: 0,
size: (self.w * self.h * 4) as usize,
flags: syscall::PROT_READ | syscall::PROT_WRITE,
})
.expect("orbclient: failed to map memory in resize");
let address = syscall::fmap(
self.file.as_raw_fd() as usize,
&syscall::Map {
offset: 0,
size: (self.w * self.h * 4) as usize,
flags: syscall::PROT_READ | syscall::PROT_WRITE,
},
)
.expect("orbclient: failed to map memory in resize");
self.data =
slice::from_raw_parts_mut(address as *mut Color, (self.w * self.h) as usize);
}
......@@ -296,7 +308,7 @@ impl Window {
};
match self.file.read(bytes) {
Ok(0) => {
if !self.async && iter.extra.is_none() && iter.count == 0 {
if !self.window_async && iter.extra.is_none() && iter.count == 0 {
thread::yield_now();
} else {
break 'blocking;
......@@ -319,7 +331,7 @@ impl Window {
self.set_size(w, h);
}
}
if !self.async {
if !self.window_async {
// Synchronous windows are blocking, can't attempt another read
break 'blocking;
}
......
extern crate sdl2;
use std::cell::Cell;
use std::cell::{Cell, RefCell};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::{mem, ptr, slice};
......@@ -50,13 +50,15 @@ pub struct Window {
/// The title of the window
t: String,
/// True if the window should not wait for events
async: bool,
window_async: bool,
/// Drawing mode
mode: Cell<Mode>,
/// The inner renderer
inner: sdl2::render::WindowCanvas,
/// Mouse in relative mode
mouse_relative: bool,
/// Content of the last drop (file | text) operation
drop_content: RefCell<Option<String>>,
}
impl Renderer for Window {
......@@ -126,7 +128,7 @@ impl Window {
//Insure that init has been called
unsafe { init() };
let mut async = false;
let mut window_async = false;
//TODO: Use z-order
let mut _back = false;
let mut _front = false;
......@@ -138,7 +140,7 @@ impl Window {
let mut _unclosable = false;
for &flag in flags.iter() {
match flag {
WindowFlag::Async => async = true,
WindowFlag::Async => window_async = true,
WindowFlag::Back => _back = true,
WindowFlag::Front => _front = true,
WindowFlag::Borderless => borderless = true,
......@@ -173,10 +175,11 @@ impl Window {
w: w,
h: h,
t: title.to_string(),
async: async,
window_async,
mode: Cell::new(Mode::Blend),
inner: window.into_canvas().software().build().unwrap(),
mouse_relative: false,
drop_content: RefCell::new(None),
}),
Err(_) => None,
}
......@@ -193,6 +196,14 @@ impl Window {
.unwrap();
}
/// Pops the content of the last drop event from the window.
pub fn pop_drop_content(&self) -> Option<String> {
let result = self.drop_content.borrow().clone();
*self.drop_content.borrow_mut() = None;
result
}
pub fn sync_path(&mut self) {
let window = self.inner.window();
let pos = window.position();
......@@ -433,6 +444,14 @@ impl Window {
);
}
}
sdl2::event::Event::DropFile { filename, .. } => {
*self.drop_content.borrow_mut() = Some(filename);
events.push(DropEvent { kind: DROP_FILE }.to_event())
}
sdl2::event::Event::DropText { filename, .. } => {
*self.drop_content.borrow_mut() = Some(filename);
events.push(DropEvent { kind: DROP_TEXT }.to_event())
}
sdl2::event::Event::KeyUp { scancode, .. } => {
if let Some(code) = self.convert_scancode(scancode, shift) {
events.push(
......@@ -460,7 +479,7 @@ impl Window {
count: 0,
};
if !self.async {
if !self.window_async {
let event = unsafe { &mut *EVENT_PUMP }.wait_event();
if let sdl2::event::Event::Window { .. } = event {
self.sync_path();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment