Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 88 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
}
/// This for now is Android specific since the process can continue running but the display
/// is restarted. We support reinitializing the display.
fn set_or_replace_display(display: native::NativeDisplayData) {

Check warning on line 102 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (macos-latest, x86_64-apple-ios)

function `set_or_replace_display` is never used

Check warning on line 102 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (macos-latest, x86_64-apple-darwin)

function `set_or_replace_display` is never used

Check warning on line 102 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (macos-latest, aarch64-apple-ios)

function `set_or_replace_display` is never used

Check warning on line 102 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, x86_64-pc-windows-msvc)

function `set_or_replace_display` is never used

Check warning on line 102 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, x86_64-unknown-linux-gnu)

function `set_or_replace_display` is never used

Check warning on line 102 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, wasm32-unknown-unknown)

function `set_or_replace_display` is never used

Check warning on line 102 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, x86_64-pc-windows-gnu)

function `set_or_replace_display` is never used

Check warning on line 102 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, armv7-unknown-linux-gnueabihf)

function `set_or_replace_display` is never used

Check warning on line 102 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, aarch64-unknown-linux-gnu)

function `set_or_replace_display` is never used
if let Some(m) = NATIVE_DISPLAY.get() {
// Replace existing display
*m.lock().unwrap() = display;
Expand Down Expand Up @@ -211,9 +211,17 @@
/// TODO: implement window focus events
pub fn set_cursor_grab(grab: bool) {
let d = native_display().lock().unwrap();
d.native_requests
.send(native::Request::SetCursorGrab(grab))
.unwrap();
#[cfg(target_os = "android")]
{
(d.native_requests)(native::Request::SetCursorGrab(grab));
}

#[cfg(not(target_os = "android"))]
{
d.native_requests
.send(native::Request::SetCursorGrab(grab))
.unwrap();
}
}

/// With `conf.platform.blocking_event_loop`, `schedule_update` called from an
Expand All @@ -222,7 +230,13 @@
///
/// Does nothing without `conf.platform.blocking_event_loop`.
pub fn schedule_update() {
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(target_os = "android", not(target_arch = "wasm32")))]
{
let d = native_display().lock().unwrap();
(d.native_requests)(native::Request::ScheduleUpdate);
}

#[cfg(not(any(target_arch = "wasm32", target_os = "android")))]
{
let d = native_display().lock().unwrap();
d.native_requests
Expand All @@ -239,35 +253,70 @@
/// Show or hide the mouse cursor
pub fn show_mouse(shown: bool) {
let d = native_display().lock().unwrap();
d.native_requests
.send(native::Request::ShowMouse(shown))
.unwrap();
#[cfg(target_os = "android")]
{
(d.native_requests)(native::Request::ShowMouse(shown));
}

#[cfg(not(target_os = "android"))]
{
d.native_requests
.send(native::Request::ShowMouse(shown))
.unwrap();
}
}

/// Set the mouse cursor icon.
pub fn set_mouse_cursor(cursor_icon: CursorIcon) {
let d = native_display().lock().unwrap();
d.native_requests
.send(native::Request::SetMouseCursor(cursor_icon))
.unwrap();
#[cfg(target_os = "android")]
{
(d.native_requests)(native::Request::SetMouseCursor(cursor_icon));
}

#[cfg(not(target_os = "android"))]
{
d.native_requests
.send(native::Request::SetMouseCursor(cursor_icon))
.unwrap();
}
}

/// Set the application's window size.
pub fn set_window_size(new_width: u32, new_height: u32) {
let d = native_display().lock().unwrap();
d.native_requests
.send(native::Request::SetWindowSize {
#[cfg(target_os = "android")]
{
(d.native_requests)(native::Request::SetWindowSize {
new_width,
new_height,
})
.unwrap();
});
}

#[cfg(not(target_os = "android"))]
{
d.native_requests
.send(native::Request::SetWindowSize {
new_width,
new_height,
})
.unwrap();
}
}

pub fn set_window_position(new_x: u32, new_y: u32) {
let d = native_display().lock().unwrap();
d.native_requests
.send(native::Request::SetWindowPosition { new_x, new_y })
.unwrap();
#[cfg(target_os = "android")]
{
(d.native_requests)(native::Request::SetWindowPosition { new_x, new_y });
}

#[cfg(not(target_os = "android"))]
{
d.native_requests
.send(native::Request::SetWindowPosition { new_x, new_y })
.unwrap();
}
}

/// Get the position of the window.
Expand All @@ -280,9 +329,17 @@

pub fn set_fullscreen(fullscreen: bool) {
let d = native_display().lock().unwrap();
d.native_requests
.send(native::Request::SetFullscreen(fullscreen))
.unwrap();
#[cfg(target_os = "android")]
{
(d.native_requests)(native::Request::SetFullscreen(fullscreen));
}

#[cfg(not(target_os = "android"))]
{
d.native_requests
.send(native::Request::SetFullscreen(fullscreen))
.unwrap();
}
}

/// Get current OS clipboard value
Expand Down Expand Up @@ -313,9 +370,17 @@
/// Only works on Android right now.
pub fn show_keyboard(show: bool) {
let d = native_display().lock().unwrap();
d.native_requests
.send(native::Request::ShowKeyboard(show))
.unwrap();
#[cfg(target_os = "android")]
{
(d.native_requests)(native::Request::ShowKeyboard(show));
}

#[cfg(not(target_os = "android"))]
{
d.native_requests
.send(native::Request::ShowKeyboard(show))
.unwrap();
}
}

#[cfg(target_vendor = "apple")]
Expand Down
7 changes: 6 additions & 1 deletion src/native.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(dead_code)]

#[cfg(not(target_os = "android"))]
use std::sync::mpsc;

#[derive(Default)]
Expand All @@ -15,6 +16,9 @@ pub(crate) struct NativeDisplayData {
pub high_dpi: bool,
pub quit_requested: bool,
pub quit_ordered: bool,
#[cfg(target_os = "android")]
pub native_requests: Box<dyn Fn(Request) + Send>,
#[cfg(not(target_os = "android"))]
pub native_requests: mpsc::Sender<Request>,
pub clipboard: Box<dyn Clipboard>,
pub dropped_files: DroppedFiles,
Expand All @@ -36,7 +40,8 @@ impl NativeDisplayData {
pub fn new(
screen_width: i32,
screen_height: i32,
native_requests: mpsc::Sender<Request>,
#[cfg(target_os = "android")] native_requests: Box<dyn Fn(Request) + Send>,
#[cfg(not(target_os = "android"))] native_requests: mpsc::Sender<Request>,
clipboard: Box<dyn Clipboard>,
) -> NativeDisplayData {
NativeDisplayData {
Expand Down
27 changes: 9 additions & 18 deletions src/native/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ enum Message {
Pause,
Resume,
Destroy,
Request(crate::native::Request),
}
unsafe impl Send for Message {}

Expand Down Expand Up @@ -187,10 +188,7 @@ impl MainThreadState {
Message::SurfaceDestroyed => unsafe {
self.destroy_surface();
},
Message::SurfaceChanged {
width,
height,
} => {
Message::SurfaceChanged { width, height } => {
{
let mut d = crate::native_display().lock().unwrap();
d.screen_width = width as _;
Expand Down Expand Up @@ -248,6 +246,7 @@ impl MainThreadState {
self.quit = true;
self.event_handler.quit_requested_event()
}
Message::Request(req) => self.process_request(req),
}
}

Expand Down Expand Up @@ -393,7 +392,8 @@ where

let (tx, rx) = mpsc::channel();

MESSAGES_TX.with(move |messages_tx| *messages_tx.borrow_mut() = Some(tx));
let tx2 = tx.clone();
MESSAGES_TX.with(move |messages_tx| *messages_tx.borrow_mut() = Some(tx2));

thread::spawn(move || {
let mut libegl = LibEgl::try_load().expect("Cant load LibEGL");
Expand All @@ -404,20 +404,15 @@ where
// it is important to create GL context only after a first SurfaceChanged
let window = 'a: loop {
match rx.try_recv() {
Ok(Message::SurfaceCreated {
window,
}) => {
Ok(Message::SurfaceCreated { window }) => {
break 'a window;
}
_ => {}
}
};
let (screen_width, screen_height) = 'a: loop {
match rx.try_recv() {
Ok(Message::SurfaceChanged {
width,
height,
}) => {
Ok(Message::SurfaceChanged { width, height }) => {
break 'a (width as f32, height as f32);
}
_ => {}
Expand Down Expand Up @@ -451,12 +446,12 @@ where
panic!();
}

let (tx, requests_rx) = std::sync::mpsc::channel();
let clipboard = Box::new(AndroidClipboard::new());
let tx_fn = Box::new(move |req| tx.send(Message::Request(req)).unwrap());
crate::set_or_replace_display(NativeDisplayData {
high_dpi: conf.high_dpi,
blocking_event_loop: conf.platform.blocking_event_loop,
..NativeDisplayData::new(screen_width as _, screen_height as _, tx, clipboard)
..NativeDisplayData::new(screen_width as _, screen_height as _, tx_fn, clipboard)
});

let event_handler = f.0();
Expand All @@ -480,10 +475,6 @@ where
};

while !s.quit {
while let Ok(request) = requests_rx.try_recv() {
s.process_request(request);
}

let block_on_wait = conf.platform.blocking_event_loop && !s.update_requested;

if block_on_wait {
Expand Down
Loading