Skip to content

Commit

Permalink
Don't pass pointer button events to client when moving or resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
JCapucho authored and Drakulix committed Aug 23, 2024
1 parent 3c24934 commit 6669548
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 50 deletions.
133 changes: 84 additions & 49 deletions src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ use std::{
any::Any,
borrow::Cow,
cell::RefCell,
collections::HashSet,
os::unix::process::CommandExt,
thread,
time::{Duration, Instant},
Expand All @@ -87,6 +88,8 @@ pub mod gestures;

#[derive(Default)]
pub struct SupressedKeys(RefCell<Vec<(Keycode, Option<RegistrationToken>)>>);
#[derive(Default)]
pub struct SupressedButtons(RefCell<HashSet<u32>>);
#[derive(Default, Debug)]
pub struct ModifiersShortcutQueue(RefCell<Option<shortcuts::Binding>>);

Expand Down Expand Up @@ -116,6 +119,16 @@ impl SupressedKeys {
}
}

impl SupressedButtons {
fn add(&self, button: u32) {
self.0.borrow_mut().insert(button);
}

fn remove(&self, button: u32) -> bool {
self.0.borrow_mut().remove(&button)
}
}

impl ModifiersShortcutQueue {
pub fn set(&self, binding: shortcuts::Binding) {
let mut set = self.0.borrow_mut();
Expand Down Expand Up @@ -773,6 +786,7 @@ impl State {

let serial = SERIAL_COUNTER.next_serial();
let button = event.button_code();
let mut pass_event = !seat.supressed_buttons().remove(button);
if event.state() == ButtonState::Pressed {
// change the keyboard focus unless the pointer is grabbed
// We test for any matching surface type here but always use the root
Expand Down Expand Up @@ -850,35 +864,45 @@ impl State {
target.toplevel().map(Cow::into_owned)
{
let seat_clone = seat.clone();
let button = PointerButtonEvent::button(&event);
self.common.event_loop_handle.insert_idle(
move |state| {
fn dispatch_grab<G: PointerGrab<State> + 'static>(
grab: Option<(G, smithay::input::pointer::Focus)>,
seat: Seat<State>,
serial: Serial,
state: &mut State)
{
if let Some((target, focus)) = grab {
seat
.modifiers_shortcut_queue()
.clear();

seat
.get_pointer()
.unwrap()
.set_grab(
state, target, serial,
focus,
);
}
}
let mouse_button =
PointerButtonEvent::button(&event);

let mut supress_button = || {
// If the logo is held then the pointer event is
// aimed at the compositor and shouldn't be passed
// to the application.
pass_event = false;
seat.supressed_buttons().add(button);
};

fn dispatch_grab<
G: PointerGrab<State> + 'static,
>(
grab: Option<(
G,
smithay::input::pointer::Focus,
)>,
seat: Seat<State>,
serial: Serial,
state: &mut State,
) {
if let Some((target, focus)) = grab {
seat.modifiers_shortcut_queue().clear();

seat.get_pointer()
.unwrap()
.set_grab(state, target, serial, focus);
}
}

let mut shell =
state.common.shell.write().unwrap();
if let Some(button) = button {
match button {
smithay::backend::input::MouseButton::Left => {
if let Some(mouse_button) = mouse_button {
match mouse_button {
smithay::backend::input::MouseButton::Left => {
supress_button();
self.common.event_loop_handle.insert_idle(
move |state| {
let mut shell =
state.common.shell.write().unwrap();
let res = shell.move_request(
&surface,
&seat_clone,
Expand All @@ -888,12 +912,19 @@ impl State {
&state.common.config,
&state.common.event_loop_handle,
&state.common.xdg_activation_state,
false
false,
);
drop(shell);
dispatch_grab(res, seat_clone, serial, state);
},
smithay::backend::input::MouseButton::Right => {
}
);
},
smithay::backend::input::MouseButton::Right => {
supress_button();
self.common.event_loop_handle.insert_idle(
move |state| {
let mut shell =
state.common.shell.write().unwrap();
let Some(target_elem) = shell.element_for_surface(&surface) else { return };
let Some(geom) = shell
.space_for(target_elem)
Expand All @@ -912,17 +943,16 @@ impl State {
&seat_clone,
serial,
edge,
false
false,
);
drop(shell);
dispatch_grab(res, seat_clone, serial, state);
},
_ => {},
}

}
},
);
}
);
},
_ => {},
}
}
}
}
under = Some(target);
Expand Down Expand Up @@ -971,20 +1001,25 @@ impl State {
.set_overview_mode(None, self.common.event_loop_handle.clone());
}
}

std::mem::drop(shell);
};

let ptr = seat.get_pointer().unwrap();
ptr.button(
self,
&ButtonEvent {
button,
state: event.state(),
serial,
time: event.time_msec(),
},
);
ptr.frame(self);
if pass_event {
ptr.button(
self,
&ButtonEvent {
button,
state: event.state(),
serial,
time: event.time_msec(),
},
);
ptr.frame(self);
} else if event.state() == ButtonState::Released {
ptr.unset_grab(self, serial, event.time_msec())
}
}
}
InputEvent::PointerAxis { event, .. } => {
Expand Down
8 changes: 7 additions & 1 deletion src/shell/seats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{any::Any, cell::RefCell, collections::HashMap, sync::Mutex, time::Dura
use crate::{
backend::render::cursor::{CursorShape, CursorState},
config::{xkb_config_to_wl, Config},
input::{ModifiersShortcutQueue, SupressedKeys},
input::{ModifiersShortcutQueue, SupressedButtons, SupressedKeys},
state::State,
};
use smithay::{
Expand Down Expand Up @@ -169,6 +169,7 @@ pub fn create_seat(
userdata.insert_if_missing_threadsafe(SeatId::default);
userdata.insert_if_missing(Devices::default);
userdata.insert_if_missing(SupressedKeys::default);
userdata.insert_if_missing(SupressedButtons::default);
userdata.insert_if_missing(ModifiersShortcutQueue::default);
userdata.insert_if_missing_threadsafe(SeatMoveGrabState::default);
userdata.insert_if_missing_threadsafe(SeatMenuGrabState::default);
Expand Down Expand Up @@ -215,6 +216,7 @@ pub trait SeatExt {
fn set_active_output(&self, output: &Output);
fn devices(&self) -> &Devices;
fn supressed_keys(&self) -> &SupressedKeys;
fn supressed_buttons(&self) -> &SupressedButtons;
fn modifiers_shortcut_queue(&self) -> &ModifiersShortcutQueue;

fn cursor_geometry(
Expand Down Expand Up @@ -254,6 +256,10 @@ impl SeatExt for Seat<State> {
self.user_data().get::<SupressedKeys>().unwrap()
}

fn supressed_buttons(&self) -> &SupressedButtons {
self.user_data().get::<SupressedButtons>().unwrap()
}

fn modifiers_shortcut_queue(&self) -> &ModifiersShortcutQueue {
self.user_data().get::<ModifiersShortcutQueue>().unwrap()
}
Expand Down

0 comments on commit 6669548

Please sign in to comment.