Skip to content

Commit 385ba6b

Browse files
committed
wip
1 parent 70e7af6 commit 385ba6b

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

src/x11/visual_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl WindowVisualConfig {
6363

6464
// For this 32-bit depth to work, you also need to define a color map and set a border
6565
// pixel: https://cgit.freedesktop.org/xorg/xserver/tree/dix/window.c#n818
66-
pub fn create_color_map(
66+
fn create_color_map(
6767
connection: &XcbConnection, visual_id: Visualid,
6868
) -> Result<Colormap, Box<dyn Error>> {
6969
let colormap = connection.conn.generate_id()?;

src/x11/window.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::cell::Cell;
12
use std::error::Error;
23
use std::ffi::c_void;
34
use std::os::fd::AsRawFd;
@@ -101,11 +102,11 @@ struct WindowInner {
101102
window_id: XWindow,
102103
window_info: WindowInfo,
103104
visual_id: Visualid,
104-
mouse_cursor: MouseCursor,
105+
mouse_cursor: Cell<MouseCursor>,
105106

106107
frame_interval: Duration,
107108
event_loop_running: bool,
108-
close_requested: bool,
109+
close_requested: Cell<bool>,
109110

110111
new_physical_size: Option<PhySize>,
111112
parent_handle: Option<ParentHandle>,
@@ -115,7 +116,7 @@ struct WindowInner {
115116
}
116117

117118
pub struct Window<'a> {
118-
inner: &'a mut WindowInner,
119+
inner: &'a WindowInner,
119120
}
120121

121122
// Hack to allow sending a RawWindowHandle between threads. Do not make public
@@ -284,11 +285,11 @@ impl<'a> Window<'a> {
284285
window_id,
285286
window_info,
286287
visual_id: visual_info.visual_id,
287-
mouse_cursor: MouseCursor::default(),
288+
mouse_cursor: Cell::new(MouseCursor::default()),
288289

289290
frame_interval: Duration::from_millis(15),
290291
event_loop_running: false,
291-
close_requested: false,
292+
close_requested: Cell::new(false),
292293

293294
new_physical_size: None,
294295
parent_handle,
@@ -312,8 +313,8 @@ impl<'a> Window<'a> {
312313
Ok(())
313314
}
314315

315-
pub fn set_mouse_cursor(&mut self, mouse_cursor: MouseCursor) {
316-
if self.inner.mouse_cursor == mouse_cursor {
316+
pub fn set_mouse_cursor(&self, mouse_cursor: MouseCursor) {
317+
if self.inner.mouse_cursor.get() == mouse_cursor {
317318
return;
318319
}
319320

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

330-
self.inner.mouse_cursor = mouse_cursor;
331+
self.inner.mouse_cursor.set(mouse_cursor);
331332
}
332333

333334
pub fn close(&mut self) {
334-
self.inner.close_requested = true;
335+
self.inner.close_requested.set(true);
335336
}
336337

337338
pub fn has_focus(&mut self) -> bool {
@@ -444,14 +445,14 @@ impl WindowInner {
444445
if let Some(parent_handle) = &self.parent_handle {
445446
if parent_handle.parent_did_drop() {
446447
self.handle_must_close(handler);
447-
self.close_requested = false;
448+
self.close_requested.set(false);
448449
}
449450
}
450451

451452
// Check if the user has requested the window to close
452-
if self.close_requested {
453+
if self.close_requested.get() {
453454
self.handle_must_close(handler);
454-
self.close_requested = false;
455+
self.close_requested.set(false);
455456
}
456457
}
457458

src/x11/xcb_connection.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::cell::RefCell;
12
use std::collections::hash_map::{Entry, HashMap};
23
use std::error::Error;
34

@@ -30,7 +31,7 @@ pub struct XcbConnection {
3031
pub(crate) atoms: Atoms,
3132
pub(crate) resources: resource_manager::Database,
3233
pub(crate) cursor_handle: CursorHandle,
33-
pub(super) cursor_cache: HashMap<MouseCursor, u32>,
34+
pub(super) cursor_cache: RefCell<HashMap<MouseCursor, u32>>,
3435
}
3536

3637
impl XcbConnection {
@@ -56,7 +57,7 @@ impl XcbConnection {
5657
atoms,
5758
resources,
5859
cursor_handle,
59-
cursor_cache: HashMap::new(),
60+
cursor_cache: RefCell::new(HashMap::new()),
6061
})
6162
}
6263

@@ -103,8 +104,12 @@ impl XcbConnection {
103104
}
104105

105106
#[inline]
106-
pub fn get_cursor(&mut self, cursor: MouseCursor) -> Result<Cursor, Box<dyn Error>> {
107-
match self.cursor_cache.entry(cursor) {
107+
pub fn get_cursor(&self, cursor: MouseCursor) -> Result<Cursor, Box<dyn Error>> {
108+
// PANIC: this function is the only point where we access the cache, and we never call
109+
// external functions that may make a reentrant call to this function
110+
let mut cursor_cache = self.cursor_cache.borrow_mut();
111+
112+
match cursor_cache.entry(cursor) {
108113
Entry::Occupied(entry) => Ok(*entry.get()),
109114
Entry::Vacant(entry) => {
110115
let cursor =

0 commit comments

Comments
 (0)