Skip to content

Commit

Permalink
Fix mouse input on outputs not located at 0,0
Browse files Browse the repository at this point in the history
Possibly addresses #21.
  • Loading branch information
Supreeeme committed Jun 30, 2024
1 parent 0a5ddda commit d3a46b7
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 48 deletions.
14 changes: 0 additions & 14 deletions src/clientside.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::server::{ObjectEvent, ObjectKey};
use std::os::unix::net::UnixStream;
use std::sync::mpsc;
use wayland_client::protocol::{
wl_buffer::WlBuffer, wl_callback::WlCallback, wl_compositor::WlCompositor,
wl_keyboard::WlKeyboard, wl_output::WlOutput, wl_pointer::WlPointer, wl_region::WlRegion,
Expand Down Expand Up @@ -156,19 +155,6 @@ impl Dispatch<WlCallback, server::wl_callback::WlCallback> for Globals {
}
}

impl Dispatch<WlOutput, mpsc::Sender<Event<WlOutput>>> for Globals {
fn event(
_: &mut Self,
_: &WlOutput,
event: <WlOutput as Proxy>::Event,
data: &mpsc::Sender<Event<WlOutput>>,
_: &Connection,
_: &QueueHandle<Self>,
) {
let _ = data.send(event);
}
}

macro_rules! push_events {
($type:ident) => {
impl Dispatch<$type, ObjectKey> for Globals {
Expand Down
26 changes: 25 additions & 1 deletion src/server/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,31 @@ where
});
}
}
global_dispatch_with_events!(WlOutput, client::wl_output::WlOutput);
impl<C: XConnection> GlobalDispatch<WlOutput, Global> for ServerState<C> {
fn bind(
state: &mut Self,
_: &DisplayHandle,
_: &wayland_server::Client,
resource: wayland_server::New<WlOutput>,
data: &Global,
data_init: &mut wayland_server::DataInit<'_, Self>,
) {
state.objects.insert_with_key(|key| {
let server = data_init.init(resource, key);
let client = state
.clientside
.global_list
.registry()
.bind::<client::wl_output::WlOutput, _, _>(
data.name,
server.version(),
&state.qh,
key,
);
Output::new(client, server).into()
});
}
}
global_dispatch_with_events!(WlDrmServer, WlDrmClient);

impl<C: XConnection> GlobalDispatch<XwaylandShellV1, ()> for ServerState<C> {
Expand Down
120 changes: 101 additions & 19 deletions src/server/event.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::*;
use log::{debug, trace, warn};
use std::collections::HashSet;
use std::os::fd::AsFd;
use wayland_client::{protocol as client, Proxy};
use wayland_protocols::{
Expand Down Expand Up @@ -97,7 +98,7 @@ macro_rules! simple_event_shunt {
}
}
)+
_ => log::warn!(concat!("unhandled", stringify!($event_type), ": {:?}"), $event)
_ => log::warn!(concat!("unhandled ", stringify!($event_type), ": {:?}"), $event)
}
}
}
Expand Down Expand Up @@ -150,16 +151,26 @@ impl SurfaceData {
simple_event_shunt! {
surface, event: client::wl_surface::Event => [
Enter { |output| {
let Some(object) = &state.get_object_from_client_object::<Output, _>(&output) else {
let key: ObjectKey = output.data().copied().unwrap();
let Some(object) = state.objects.get_mut(key) else {
return;
};
&object.server
let output: &mut Output = object.as_mut();
if let Some(win) = self.window {
if let Some(data) = state.windows.get(&win) {
output.add_surface(self, data.attrs.dims, state.connection.as_mut().unwrap());
}
}
&output.server
}},
Leave { |output| {
let Some(object) = &state.get_object_from_client_object::<Output, _>(&output) else {
let key: ObjectKey = output.data().copied().unwrap();
let Some(object) = state.objects.get_mut(key) else {
return;
};
&object.server
let output: &mut Output = object.as_mut();
output.surfaces.remove(&self.client);
&output.server
}},
PreferredBufferScale { factor }
]
Expand All @@ -179,32 +190,36 @@ impl SurfaceData {
if let Some(pending) = xdg.pending.take() {
let window = state.associated_windows[self.key];
let window = state.windows.get_mut(&window).unwrap();
let x = match pending.x {
Some(x) => x as i16,
None => 0,
};
let y = match pending.y {
Some(y) => y as i16,
None => 0,
};
let width = if pending.width > 0 {
pending.width as _
pending.width as u16
} else {
window.attrs.dims.width
};
let height = if pending.height > 0 {
pending.height as _
pending.height as u16
} else {
window.attrs.dims.height
};
debug!(
"configuring {:?}: {}x{}, {width}x{height}",
window.window, pending.x, pending.y
);
debug!("configuring {:?}: {x}x{y}, {width}x{height}", window.window);
connection.set_window_dims(
window.window,
PendingSurfaceState {
x: pending.x,
y: pending.y,
width: width as _,
height: height as _,
..pending
},
);
window.attrs.dims = WindowDims {
x: pending.x as _,
y: pending.y as _,
x,
y,
width,
height,
};
Expand Down Expand Up @@ -277,8 +292,8 @@ impl SurfaceData {
} => {
trace!("popup configure: {x}x{y}, {width}x{height}");
self.xdg_mut().unwrap().pending = Some(PendingSurfaceState {
x,
y,
x: Some(x),
y: Some(y),
width,
height,
});
Expand Down Expand Up @@ -618,11 +633,78 @@ impl HandleEvent for Touch {
}
}

pub type Output = GenericObject<WlOutput, client::wl_output::WlOutput>;
pub struct Output {
pub client: client::wl_output::WlOutput,
pub server: WlOutput,
surfaces: HashSet<client::wl_surface::WlSurface>,
x: i32,
y: i32,
}

impl Output {
pub fn new(client: client::wl_output::WlOutput, server: WlOutput) -> Self {
Self {
client,
server,
surfaces: HashSet::new(),
x: 0,
y: 0,
}
}

fn add_surface<C: XConnection>(
&mut self,
surface: &SurfaceData,
dims: WindowDims,
connection: &mut C,
) {
self.surfaces.insert(surface.client.clone());
let window = surface.window.unwrap();

debug!("moving surface to {}x{}", self.x, self.y);
connection.set_window_dims(
window,
PendingSurfaceState {
x: Some(self.x),
y: Some(self.y),
width: dims.width as _,
height: dims.height as _,
},
);
}
}
impl HandleEvent for Output {
type Event = client::wl_output::Event;

fn handle_event<C: XConnection>(&mut self, event: Self::Event, _: &mut ServerState<C>) {
fn handle_event<C: XConnection>(&mut self, event: Self::Event, state: &mut ServerState<C>) {
if let client::wl_output::Event::Geometry { x, y, .. } = event {
debug!("moving output to {x}x{y}");
self.x = x;
self.y = y;
self.surfaces.retain(|surface| {
let Some(data) = state.get_object_from_client_object::<SurfaceData, _>(surface)
else {
return false;
};

let window = data.window.as_ref().copied().unwrap();
let Some(win_data) = state.windows.get(&window) else {
return false;
};

state.connection.as_mut().unwrap().set_window_dims(
window,
PendingSurfaceState {
x: Some(x),
y: Some(y),
width: win_data.attrs.dims.width as _,
height: win_data.attrs.dims.height as _,
},
);
true
});
}

simple_event_shunt! {
self.server, event: client::wl_output::Event => [
Name { name },
Expand Down
4 changes: 2 additions & 2 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,8 +937,8 @@ impl<C: XConnection> ServerState<C> {

#[derive(Default, Debug)]
pub struct PendingSurfaceState {
pub x: i32,
pub y: i32,
pub x: Option<i32>,
pub y: Option<i32>,
pub width: i32,
pub height: i32,
}
Expand Down
6 changes: 4 additions & 2 deletions src/server/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ impl super::XConnection for FakeXConnection {
#[track_caller]
fn set_window_dims(&mut self, window: Window, state: super::PendingSurfaceState) {
self.window(window).dims = WindowDims {
x: state.x as _,
y: state.y as _,
x: state.x.unwrap_or(0) as _,
y: state.y.unwrap_or(0) as _,
width: state.width as _,
height: state.height as _,
};
Expand Down Expand Up @@ -699,6 +699,8 @@ fn pass_through_globals() {
use wayland_client::protocol::wl_output::WlOutput;

let mut f = TestFixture::new();
f.testwl.new_output(0, 0);
f.run();

const fn check<T: Proxy>() {}

Expand Down
19 changes: 13 additions & 6 deletions src/xstate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,14 +782,21 @@ impl super::XConnection for Arc<xcb::Connection> {

fn set_window_dims(&mut self, window: x::Window, dims: crate::server::PendingSurfaceState) {
trace!("reconfiguring window {window:?}");
let mut vals = vec![
x::ConfigWindow::Width(dims.width as _),
x::ConfigWindow::Height(dims.height as _),
];
if let Some(x) = dims.x {
vals.push(x::ConfigWindow::X(x));
}
if let Some(y) = dims.y {
vals.push(x::ConfigWindow::Y(y));
}
vals.sort();

unwrap_or_skip_bad_window!(self.send_and_check_request(&x::ConfigureWindow {
window,
value_list: &[
x::ConfigWindow::X(dims.x),
x::ConfigWindow::Y(dims.y),
x::ConfigWindow::Width(dims.width as _),
x::ConfigWindow::Height(dims.height as _),
],
value_list: &vals
}));
}

Expand Down
39 changes: 39 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,3 +870,42 @@ fn copy_from_wayland() {
assert_eq!(val, data);
}
}

// TODO: this test doesn't actually match real behavior for some reason...
#[test]
fn different_output_position() {
let mut f = Fixture::new();
let mut connection = Connection::new(&f.display);

let window = connection.new_window(connection.root, 0, 0, 200, 200, false);
connection.map_window(window);
f.wait_and_dispatch();
let surface = f
.testwl
.last_created_surface_id()
.expect("No surface created!");
f.configure_and_verify_new_toplevel(&mut connection, window, surface);

f.testwl.new_output(0, 0);
f.wait_and_dispatch();
let output = f.testwl.last_created_output();
f.testwl.move_surface_to_output(surface, output);
f.testwl.move_pointer_to(surface, 10.0, 10.0);
f.wait_and_dispatch();
let reply = connection.get_reply(&x::QueryPointer { window });
assert_eq!(reply.same_screen(), true);
assert_eq!(reply.win_x(), 10);
assert_eq!(reply.win_y(), 10);

f.testwl.new_output(100, 0);
f.wait_and_dispatch();
let output = f.testwl.last_created_output();
f.testwl.move_surface_to_output(surface, output);
f.testwl.move_pointer_to(surface, 150.0, 12.0);
f.wait_and_dispatch();
let reply = connection.get_reply(&x::QueryPointer { window });
println!("reply: {reply:?}");
assert_eq!(reply.same_screen(), true);
assert_eq!(reply.win_x(), 150);
assert_eq!(reply.win_y(), 12);
}
Loading

0 comments on commit d3a46b7

Please sign in to comment.