Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
prokopyl committed Mar 27, 2024
1 parent 70e7af6 commit 385ba6b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/x11/visual_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl WindowVisualConfig {

// For this 32-bit depth to work, you also need to define a color map and set a border
// pixel: https://cgit.freedesktop.org/xorg/xserver/tree/dix/window.c#n818
pub fn create_color_map(
fn create_color_map(
connection: &XcbConnection, visual_id: Visualid,
) -> Result<Colormap, Box<dyn Error>> {
let colormap = connection.conn.generate_id()?;
Expand Down
25 changes: 13 additions & 12 deletions src/x11/window.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cell::Cell;
use std::error::Error;
use std::ffi::c_void;
use std::os::fd::AsRawFd;
Expand Down Expand Up @@ -101,11 +102,11 @@ struct WindowInner {
window_id: XWindow,
window_info: WindowInfo,
visual_id: Visualid,
mouse_cursor: MouseCursor,
mouse_cursor: Cell<MouseCursor>,

frame_interval: Duration,
event_loop_running: bool,
close_requested: bool,
close_requested: Cell<bool>,

new_physical_size: Option<PhySize>,
parent_handle: Option<ParentHandle>,
Expand All @@ -115,7 +116,7 @@ struct WindowInner {
}

pub struct Window<'a> {
inner: &'a mut WindowInner,
inner: &'a WindowInner,
}

// Hack to allow sending a RawWindowHandle between threads. Do not make public
Expand Down Expand Up @@ -284,11 +285,11 @@ impl<'a> Window<'a> {
window_id,
window_info,
visual_id: visual_info.visual_id,
mouse_cursor: MouseCursor::default(),
mouse_cursor: Cell::new(MouseCursor::default()),

frame_interval: Duration::from_millis(15),
event_loop_running: false,
close_requested: false,
close_requested: Cell::new(false),

new_physical_size: None,
parent_handle,
Expand All @@ -312,8 +313,8 @@ impl<'a> Window<'a> {
Ok(())
}

pub fn set_mouse_cursor(&mut self, mouse_cursor: MouseCursor) {
if self.inner.mouse_cursor == mouse_cursor {
pub fn set_mouse_cursor(&self, mouse_cursor: MouseCursor) {
if self.inner.mouse_cursor.get() == mouse_cursor {
return;
}

Expand All @@ -327,11 +328,11 @@ impl<'a> Window<'a> {
let _ = self.inner.xcb_connection.conn.flush();
}

self.inner.mouse_cursor = mouse_cursor;
self.inner.mouse_cursor.set(mouse_cursor);
}

pub fn close(&mut self) {
self.inner.close_requested = true;
self.inner.close_requested.set(true);
}

pub fn has_focus(&mut self) -> bool {
Expand Down Expand Up @@ -444,14 +445,14 @@ impl WindowInner {
if let Some(parent_handle) = &self.parent_handle {
if parent_handle.parent_did_drop() {
self.handle_must_close(handler);
self.close_requested = false;
self.close_requested.set(false);
}
}

// Check if the user has requested the window to close
if self.close_requested {
if self.close_requested.get() {
self.handle_must_close(handler);
self.close_requested = false;
self.close_requested.set(false);
}
}

Expand Down
13 changes: 9 additions & 4 deletions src/x11/xcb_connection.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cell::RefCell;
use std::collections::hash_map::{Entry, HashMap};
use std::error::Error;

Expand Down Expand Up @@ -30,7 +31,7 @@ pub struct XcbConnection {
pub(crate) atoms: Atoms,
pub(crate) resources: resource_manager::Database,
pub(crate) cursor_handle: CursorHandle,
pub(super) cursor_cache: HashMap<MouseCursor, u32>,
pub(super) cursor_cache: RefCell<HashMap<MouseCursor, u32>>,
}

impl XcbConnection {
Expand All @@ -56,7 +57,7 @@ impl XcbConnection {
atoms,
resources,
cursor_handle,
cursor_cache: HashMap::new(),
cursor_cache: RefCell::new(HashMap::new()),
})
}

Expand Down Expand Up @@ -103,8 +104,12 @@ impl XcbConnection {
}

#[inline]
pub fn get_cursor(&mut self, cursor: MouseCursor) -> Result<Cursor, Box<dyn Error>> {
match self.cursor_cache.entry(cursor) {
pub fn get_cursor(&self, cursor: MouseCursor) -> Result<Cursor, Box<dyn Error>> {
// PANIC: this function is the only point where we access the cache, and we never call
// external functions that may make a reentrant call to this function
let mut cursor_cache = self.cursor_cache.borrow_mut();

match cursor_cache.entry(cursor) {
Entry::Occupied(entry) => Ok(*entry.get()),
Entry::Vacant(entry) => {
let cursor =
Expand Down

0 comments on commit 385ba6b

Please sign in to comment.