Skip to content

Commit a8f0e33

Browse files
sapp_event no longer carries the file data.
A single FILES_DROPPED event is sent when one or more files are dropped. File count, paths and bytes can be requested with functions `dropped_file_count`, `dropped_file_path` and `dropped_file_bytes`.
1 parent cd61f72 commit a8f0e33

File tree

7 files changed

+182
-84
lines changed

7 files changed

+182
-84
lines changed

native/sapp-linux/src/lib.rs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ pub mod clipboard;
2323
pub use gl::*;
2424
pub use rand::*;
2525

26+
use std::path::PathBuf;
27+
2628
use crate::x::*;
2729

2830
pub type sapp_event_type = libc::c_uint;
2931
pub const sapp_event_type__SAPP_EVENTTYPE_FORCE_U32: sapp_event_type = 2147483647;
3032
pub const sapp_event_type__SAPP_EVENTTYPE_NUM: sapp_event_type = 23;
31-
pub const sapp_event_type_SAPP_EVENTTYPE_FILE_DROPPED: sapp_event_type = 22;
33+
pub const sapp_event_type_SAPP_EVENTTYPE_FILES_DROPPED: sapp_event_type = 22;
3234
pub const sapp_event_type_SAPP_EVENTTYPE_RAW_DEVICE: sapp_event_type = 21;
3335
pub const sapp_event_type_SAPP_EVENTTYPE_QUIT_REQUESTED: sapp_event_type = 20;
3436
pub const sapp_event_type_SAPP_EVENTTYPE_UPDATE_CURSOR: sapp_event_type = 19;
@@ -255,11 +257,13 @@ pub struct sapp_event {
255257
pub window_height: libc::c_int,
256258
pub framebuffer_width: libc::c_int,
257259
pub framebuffer_height: libc::c_int,
258-
pub file_path: Option<[u8; 4096]>,
259-
pub file_path_length: usize,
260-
pub file_buf: *mut u8,
261-
pub file_buf_length: usize,
262260
}
261+
262+
#[derive(Clone)]
263+
pub struct sapp_drop {
264+
pub file_paths: Vec<PathBuf>,
265+
}
266+
263267
#[derive(Copy, Clone)]
264268
#[repr(C)]
265269
pub struct sapp_desc {
@@ -294,8 +298,7 @@ pub struct sapp_desc {
294298
pub ios_keyboard_resizes_canvas: bool,
295299
pub gl_force_gles2: bool,
296300
}
297-
#[derive(Copy, Clone)]
298-
#[repr(C)]
301+
#[derive(Clone)]
299302
pub struct _sapp_state {
300303
pub valid: bool,
301304
pub window_width: libc::c_int,
@@ -324,6 +327,7 @@ pub struct _sapp_state {
324327
pub desc: sapp_desc,
325328
pub keycodes: [sapp_keycode; 512],
326329
pub xdnd: sapp_xdnd,
330+
pub drop: sapp_drop,
327331
}
328332

329333
/// opcode from XQueryExtension("XInputExtension")
@@ -2824,15 +2828,15 @@ pub unsafe extern "C" fn _sapp_x11_process_event(mut event: *mut XEvent) {
28242828
&mut buf) as usize;
28252829
let data = std::slice::from_raw_parts(buf as _, len);
28262830
let decoded = sapp_x11_percent_decode(&data);
2827-
for path in decoded.split("\r\n")
2831+
2832+
_sapp.drop.file_paths = decoded.split("\r\n")
28282833
.filter(|path| !path.is_empty())
2829-
.map(|path| path.trim_start_matches("file://")) {
2830-
let mut path_bytes = [0; 4096];
2831-
path_bytes[0..path.len()].copy_from_slice(path.as_bytes());
2834+
.map(|path| path.trim_start_matches("file://").to_string())
2835+
.map(PathBuf::from)
2836+
.collect();
28322837

2833-
_sapp_init_event(sapp_event_type_SAPP_EVENTTYPE_FILE_DROPPED);
2834-
_sapp.event.file_path = Some(path_bytes);
2835-
_sapp.event.file_path_length = path.len();
2838+
if !_sapp.drop.file_paths.is_empty() {
2839+
_sapp_init_event(sapp_event_type_SAPP_EVENTTYPE_FILES_DROPPED);
28362840
_sapp_call_event(&mut _sapp.event);
28372841
}
28382842
}
@@ -3257,10 +3261,6 @@ pub static mut _sapp: _sapp_state = _sapp_state {
32573261
window_height: 0,
32583262
framebuffer_width: 0,
32593263
framebuffer_height: 0,
3260-
file_path: None,
3261-
file_path_length: 0,
3262-
file_buf: std::ptr::null_mut(),
3263-
file_buf_length: 0,
32643264
},
32653265
desc: sapp_desc {
32663266
init_cb: None,
@@ -3309,6 +3309,9 @@ pub static mut _sapp: _sapp_state = _sapp_state {
33093309
type_list: 0,
33103310
text_uri_list: 0
33113311
}
3312+
},
3313+
drop: sapp_drop {
3314+
file_paths: vec![]
33123315
}
33133316
};
33143317
#[no_mangle]
@@ -3340,3 +3343,19 @@ fn sapp_x11_percent_decode(bytes: &[u8]) -> String {
33403343

33413344
String::from_utf8_lossy(&decoded).into_owned()
33423345
}
3346+
3347+
pub fn dropped_file_count() -> usize {
3348+
unsafe {
3349+
_sapp.drop.file_paths.len()
3350+
}
3351+
}
3352+
3353+
pub fn dropped_file_bytes(index: usize) -> Option<Vec<u8>> {
3354+
None
3355+
}
3356+
3357+
pub fn dropped_file_path(index: usize) -> Option<PathBuf> {
3358+
unsafe {
3359+
_sapp.drop.file_paths.get(index).cloned()
3360+
}
3361+
}

native/sapp-wasm/js/gl.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,8 @@ var importObject = {
12331233
window.ondrop = async function(e) {
12341234
e.preventDefault();
12351235

1236+
wasm_exports.on_files_dropped_start();
1237+
12361238
for (let file of e.dataTransfer.files) {
12371239
const nameLen = file.name.length;
12381240
const nameVec = wasm_exports.allocate_vec_u8(nameLen);
@@ -1247,6 +1249,8 @@ var importObject = {
12471249

12481250
wasm_exports.on_file_dropped(nameVec, nameLen, fileVec, fileLen);
12491251
}
1252+
1253+
wasm_exports.on_files_dropped_finish();
12501254
};
12511255

12521256
window.requestAnimationFrame(animation);

native/sapp-wasm/src/lib.rs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@ pub mod fs;
44
pub mod gl;
55
mod rand;
66

7+
use std::path::{Path, PathBuf};
78
pub use gl::*;
89
pub use rand::*;
910

11+
#[derive(Default)]
12+
struct DroppedFiles {
13+
paths: Vec<PathBuf>,
14+
bytes: Vec<Vec<u8>>,
15+
}
16+
1017
struct SappContext {
1118
desc: sapp_desc,
1219
clipboard: Option<String>,
20+
dropped_files: DroppedFiles,
1321
}
1422

1523
impl SappContext {
@@ -18,6 +26,10 @@ impl SappContext {
1826
SAPP_CONTEXT = Some(SappContext {
1927
desc,
2028
clipboard: None,
29+
dropped_files: DroppedFiles {
30+
paths: vec![],
31+
bytes: vec![]
32+
}
2133
});
2234
SAPP_CONTEXT
2335
.as_mut()
@@ -72,7 +84,7 @@ pub const sapp_event_type_SAPP_EVENTTYPE_RESUMED: sapp_event_type = 18;
7284
pub const sapp_event_type_SAPP_EVENTTYPE_UPDATE_CURSOR: sapp_event_type = 19;
7385
pub const sapp_event_type_SAPP_EVENTTYPE_QUIT_REQUESTED: sapp_event_type = 20;
7486
pub const sapp_event_type_SAPP_EVENTTYPE_RAW_DEVICE: sapp_event_type = 21;
75-
pub const sapp_event_type_SAPP_EVENTTYPE_FILE_DROPPED: sapp_event_type = 22;
87+
pub const sapp_event_type_SAPP_EVENTTYPE_FILES_DROPPED: sapp_event_type = 22;
7688
pub const sapp_event_type__SAPP_EVENTTYPE_NUM: sapp_event_type = 23;
7789
pub const sapp_event_type__SAPP_EVENTTYPE_FORCE_U32: sapp_event_type = 2147483647;
7890

@@ -243,10 +255,6 @@ pub struct sapp_event {
243255
pub window_height: ::std::os::raw::c_int,
244256
pub framebuffer_width: ::std::os::raw::c_int,
245257
pub framebuffer_height: ::std::os::raw::c_int,
246-
pub file_path: Option<[u8; 4096]>,
247-
pub file_path_length: usize,
248-
pub file_buf: *mut u8,
249-
pub file_buf_length: usize,
250258
}
251259

252260
#[repr(C)]
@@ -445,6 +453,24 @@ pub fn clipboard_set(data: &str) {
445453
unsafe { sapp_set_clipboard(data.as_ptr(), len) };
446454
}
447455

456+
pub fn dropped_file_count() -> usize {
457+
unsafe {
458+
sapp_context().dropped_files.bytes.len()
459+
}
460+
}
461+
462+
pub fn dropped_file_bytes(index: usize) -> Option<Vec<u8>> {
463+
unsafe {
464+
sapp_context().dropped_files.bytes.get(index).cloned()
465+
}
466+
}
467+
468+
pub fn dropped_file_path(index: usize) -> Option<PathBuf> {
469+
unsafe {
470+
sapp_context().dropped_files.paths.get(index).cloned()
471+
}
472+
}
473+
448474
#[no_mangle]
449475
pub extern "C" fn frame() {
450476
unsafe {
@@ -581,24 +607,30 @@ pub extern "C" fn touch(event_type: u32, id: u32, x: f32, y: f32) {
581607
}
582608

583609
#[no_mangle]
584-
pub extern "C" fn on_file_dropped(name: *mut u8, name_len: usize, bytes: *mut u8, bytes_len: usize) {
585-
let name = unsafe {
586-
String::from_raw_parts(name, name_len, name_len)
587-
};
588-
589-
let mut name_array = [0; 4096];
590-
let actual_len = name_array.len().min(name.len());
591-
name_array[0..actual_len].copy_from_slice((&name[0..actual_len]).as_bytes());
610+
pub extern "C" fn on_files_dropped_start() {
611+
unsafe {
612+
sapp_context().dropped_files = Default::default();
613+
}
614+
}
592615

616+
#[no_mangle]
617+
pub extern "C" fn on_files_dropped_finish() {
593618
let mut event: sapp_event = unsafe { std::mem::zeroed() };
594619

595-
event.type_ = sapp_event_type_SAPP_EVENTTYPE_FILE_DROPPED as u32;
596-
event.file_path = Some(name_array);
597-
event.file_path_length = actual_len;
598-
event.file_buf = bytes;
599-
event.file_buf_length = bytes_len;
620+
event.type_ = sapp_event_type_SAPP_EVENTTYPE_FILES_DROPPED as u32;
600621

601622
unsafe {
602623
sapp_context().event(event);
603624
}
604625
}
626+
627+
#[no_mangle]
628+
pub extern "C" fn on_file_dropped(path: *mut u8, path_len: usize, bytes: *mut u8, bytes_len: usize) {
629+
unsafe {
630+
let path = PathBuf::from(String::from_raw_parts(path, path_len, path_len));
631+
let bytes = Vec::from_raw_parts(bytes, bytes_len, bytes_len);
632+
633+
sapp_context().dropped_files.paths.push(path);
634+
sapp_context().dropped_files.bytes.push(bytes);
635+
}
636+
}

native/sapp-windows/src/lib.rs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ pub mod clipboard;
44
pub mod gl;
55
mod rand;
66

7+
use std::ffi::OsString;
8+
use std::os::windows::ffi::OsStringExt;
9+
use std::path::PathBuf;
710
pub use gl::*;
811
pub use rand::*;
912

@@ -61,7 +64,7 @@ use winapi::{
6164
pub type sapp_event_type = u32;
6265
pub const sapp_event_type__SAPP_EVENTTYPE_FORCE_U32: sapp_event_type = 2147483647;
6366
pub const sapp_event_type__SAPP_EVENTTYPE_NUM: sapp_event_type = 23;
64-
pub const sapp_event_type_SAPP_EVENTTYPE_FILE_DROPPED: sapp_event_type = 22;
67+
pub const sapp_event_type_SAPP_EVENTTYPE_FILES_DROPPED: sapp_event_type = 22;
6568
pub const sapp_event_type_SAPP_EVENTTYPE_RAW_DEVICE: sapp_event_type = 21;
6669
pub const sapp_event_type_SAPP_EVENTTYPE_QUIT_REQUESTED: sapp_event_type = 20;
6770
pub const sapp_event_type_SAPP_EVENTTYPE_UPDATE_CURSOR: sapp_event_type = 19;
@@ -261,10 +264,6 @@ pub struct sapp_event {
261264
pub window_height: i32,
262265
pub framebuffer_width: i32,
263266
pub framebuffer_height: i32,
264-
pub file_path: Option<[u16; MAX_PATH]>,
265-
pub file_path_length: usize,
266-
pub file_buf: *mut u8,
267-
pub file_buf_length: usize
268267
}
269268

270269
#[derive(Clone)]
@@ -274,6 +273,11 @@ pub struct sapp_icon {
274273
pub big: Vec<u8>,
275274
}
276275

276+
#[derive(Clone)]
277+
pub struct sapp_drop {
278+
pub file_paths: Vec<PathBuf>,
279+
}
280+
277281
#[derive(Clone)]
278282
pub struct sapp_desc {
279283
pub init_cb: Option<unsafe extern "C" fn() -> ()>,
@@ -335,6 +339,7 @@ pub struct _sapp_state {
335339
pub event: sapp_event,
336340
pub desc: sapp_desc,
337341
pub keycodes: [sapp_keycode; 512],
342+
pub drop: sapp_drop,
338343
}
339344

340345
static mut _sapp_win32_window_scale: f32 = 1.;
@@ -426,10 +431,6 @@ static mut _sapp: _sapp_state = _sapp_state {
426431
window_height: 0,
427432
framebuffer_width: 0,
428433
framebuffer_height: 0,
429-
file_path: None,
430-
file_path_length: 0,
431-
file_buf: std::ptr::null_mut(),
432-
file_buf_length: 0,
433434
},
434435
desc: sapp_desc {
435436
init_cb: None,
@@ -463,6 +464,9 @@ static mut _sapp: _sapp_state = _sapp_state {
463464
icon: None,
464465
},
465466
keycodes: [sapp_keycode_SAPP_KEYCODE_INVALID; 512],
467+
drop: sapp_drop {
468+
file_paths: Vec::new(),
469+
}
466470
};
467471

468472
#[no_mangle]
@@ -940,17 +944,23 @@ unsafe extern "system" fn win32_wndproc(
940944
let mut path: [u16; MAX_PATH] = std::mem::zeroed();
941945
let num_drops = DragQueryFileW(hdrop, 0xFFFFFFFF, std::ptr::null_mut(), 0);
942946

947+
let mut paths = Vec::with_capacity(num_drops as _);
948+
943949
for i in 0..num_drops {
944950
let path_len =
945951
DragQueryFileW(hdrop, i, path.as_mut_ptr(), MAX_PATH as u32) as usize;
946952
if path_len > 0 {
947-
_sapp_init_event(sapp_event_type_SAPP_EVENTTYPE_FILE_DROPPED);
948-
_sapp.event.file_path = Some(path);
949-
_sapp.event.file_path_length = path_len;
950-
_sapp_call_event(&_sapp.event);
953+
paths.push(PathBuf::from(OsString::from_wide(&path)));
951954
}
952955
}
953956

957+
_sapp.drop.file_paths = paths;
958+
959+
if !_sapp.drop.file_paths.is_empty() {
960+
_sapp_init_event(sapp_event_type_SAPP_EVENTTYPE_FILES_DROPPED);
961+
_sapp_call_event(&_sapp.event);
962+
}
963+
954964
DragFinish(hdrop);
955965
}
956966
_ => {}
@@ -1880,3 +1890,19 @@ pub unsafe fn sapp_run(desc: *const sapp_desc) -> i32 {
18801890
pub unsafe fn sapp_is_elapsed_timer_supported() -> bool {
18811891
return true;
18821892
}
1893+
1894+
pub fn dropped_file_count() -> usize {
1895+
unsafe {
1896+
_sapp.drop.file_paths.len()
1897+
}
1898+
}
1899+
1900+
pub fn dropped_file_bytes(_index: usize) -> Option<Vec<u8>> {
1901+
None
1902+
}
1903+
1904+
pub fn dropped_file_path(index: usize) -> Option<PathBuf> {
1905+
unsafe {
1906+
_sapp.drop.file_paths.get(index).cloned()
1907+
}
1908+
}

0 commit comments

Comments
 (0)