Skip to content

Commit

Permalink
Fix connection flushing, and add a few consistency checks
Browse files Browse the repository at this point in the history
  • Loading branch information
prokopyl committed Apr 19, 2024
1 parent ce63a7f commit 0065a4d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
7 changes: 6 additions & 1 deletion examples/render_femtovg/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ impl WindowHandler for FemtovgExample {
self.canvas.set_size(phy_size.width, phy_size.height, size.scale() as f32);
self.damaged = true;
}
Event::Mouse(MouseEvent::CursorMoved { position, .. }) => {
Event::Mouse(
MouseEvent::CursorMoved { position, .. }
| MouseEvent::DragEntered { position, .. }
| MouseEvent::DragMoved { position, .. }
| MouseEvent::DragDropped { position, .. },
) => {
self.current_mouse_position = position.to_physical(&self.current_size);
self.damaged = true;
}
Expand Down
16 changes: 13 additions & 3 deletions src/x11/drag_n_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use std::{
};

use percent_encoding::percent_decode;
use x11rb::connection::Connection;
use x11rb::protocol::xproto::Timestamp;
use x11rb::{
errors::ConnectionError,
protocol::xproto::{self, ConnectionExt},
Expand All @@ -31,6 +33,7 @@ pub(crate) struct DragNDrop {

// Populated by SelectionNotify event handler (triggered by XdndPosition event handler)
pub data: DropData,
pub data_requested_at: Option<Timestamp>,

pub logical_pos: Point,
}
Expand All @@ -42,6 +45,7 @@ impl DragNDrop {
type_list: None,
source_window: None,
data: DropData::None,
data_requested_at: None,
logical_pos: Point::new(0.0, 0.0),
}
}
Expand All @@ -51,6 +55,7 @@ impl DragNDrop {
self.type_list = None;
self.source_window = None;
self.data = DropData::None;
self.data_requested_at = None;
self.logical_pos = Point::new(0.0, 0.0);
}

Expand All @@ -70,9 +75,14 @@ impl DragNDrop {
type_: conn.atoms.XdndStatus as _,
};

conn.conn
.send_event(false, target_window, xproto::EventMask::NO_EVENT, event.serialize())
.map(|_| ())
conn.conn.send_event(
false,
target_window,
xproto::EventMask::NO_EVENT,
event.serialize(),
)?;

conn.conn.flush()
}

pub fn send_finished(
Expand Down
16 changes: 15 additions & 1 deletion src/x11/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ impl EventLoop {
// drag n drop
////
if event.type_ == self.window.xcb_connection.atoms.XdndEnter {
self.drag_n_drop.reset();
let data = event.data.as_data32();

let source_window = data[0] as XWindow;
Expand Down Expand Up @@ -278,7 +279,7 @@ impl EventLoop {
self.handler
.on_event(&mut crate::Window::new(Window { inner: &self.window }), ev);

if let DropData::None = &self.drag_n_drop.data {
if self.drag_n_drop.data_requested_at.is_none() {
let time = if version >= 1 {
data[3] as Timestamp
} else {
Expand All @@ -293,6 +294,9 @@ impl EventLoop {
&self.window.xcb_connection,
) {
// TODO: log warning
} else {
self.drag_n_drop.data_requested_at = Some(time);
self.window.xcb_connection.conn.flush().unwrap();
}
}

Expand Down Expand Up @@ -347,6 +351,16 @@ impl EventLoop {

XEvent::SelectionNotify(event) => {
if event.property == self.window.xcb_connection.atoms.XdndSelection {
let Some(requested_time) = self.drag_n_drop.data_requested_at else {
// eprintln!("Received DnD selection data, but we didn't request any. Weird. Skipping.");
return;
};

if event.time != requested_time {
// eprintln!("Received stale DnD selection data ({}), expected data is {requested_time}. Skipping.", event.time);
return;
}

if let Ok(mut data) = self
.drag_n_drop
.read_data(self.window.window_id, &self.window.xcb_connection)
Expand Down

0 comments on commit 0065a4d

Please sign in to comment.