Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement tablet mode (disabling keyboard and mouse/touch pad) #238

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions niri-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ pub enum Action {
ChangeVt(i32),
Suspend,
PowerOffMonitors,
ToggleTabletMode,
ToggleDebugTint,
Spawn(#[knuffel(arguments)] Vec<String>),
#[knuffel(skip)]
Expand Down Expand Up @@ -845,6 +846,7 @@ impl From<niri_ipc::Action> for Action {
niri_ipc::Action::MoveWorkspaceToMonitorDown => Self::MoveWorkspaceToMonitorDown,
niri_ipc::Action::MoveWorkspaceToMonitorUp => Self::MoveWorkspaceToMonitorUp,
niri_ipc::Action::ToggleDebugTint => Self::ToggleDebugTint,
niri_ipc::Action::ToggleTabletMode => Self::ToggleTabletMode,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions niri-ipc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ pub enum Action {
MoveWorkspaceToMonitorUp,
/// Toggle a debug tint on windows.
ToggleDebugTint,
/// Toggle tablet mode (disable keyboard and touchpad)
ToggleTabletMode,
}

/// Change in window or column size.
Expand Down
40 changes: 38 additions & 2 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use smithay::backend::input::{
AbsolutePositionEvent, Axis, AxisSource, ButtonState, Device, DeviceCapability, Event,
GestureBeginEvent, GestureEndEvent, GesturePinchUpdateEvent as _, GestureSwipeUpdateEvent as _,
InputBackend, InputEvent, KeyState, KeyboardKeyEvent, PointerAxisEvent, PointerButtonEvent,
PointerMotionEvent, ProximityState, TabletToolButtonEvent, TabletToolEvent,
PointerMotionEvent, ProximityState, SwitchToggleEvent, TabletToolButtonEvent, TabletToolEvent,
TabletToolProximityEvent, TabletToolTipEvent, TabletToolTipState, TouchEvent,
};
use smithay::backend::libinput::LibinputInputBackend;
Expand Down Expand Up @@ -109,7 +109,7 @@ impl State {
TouchUp { event } => self.on_touch_up::<I>(event),
TouchCancel { event } => self.on_touch_cancel::<I>(event),
TouchFrame { event } => self.on_touch_frame::<I>(event),
SwitchToggle { .. } => (),
SwitchToggle { event } => self.on_switch_toggle::<I>(event),
Special(_) => (),
}

Expand Down Expand Up @@ -278,6 +278,7 @@ impl State {
*mods,
&this.niri.screenshot_ui,
this.niri.config.borrow().input.disable_power_key_handling,
this.niri.tablet_mode,
)
},
) else {
Expand Down Expand Up @@ -332,6 +333,11 @@ impl State {
self.backend.toggle_debug_tint();
self.niri.queue_redraw_all();
}
Action::ToggleTabletMode => {
self.niri.tablet_mode = !self.niri.tablet_mode;
// FIXME: redraw only outputs overlapping the cursor.
self.niri.queue_redraw_all();
}
Action::Spawn(command) => {
spawn(command);
}
Expand Down Expand Up @@ -697,6 +703,10 @@ impl State {
}

fn on_pointer_motion<I: InputBackend>(&mut self, event: I::PointerMotionEvent) {
if self.niri.tablet_mode {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking, maybe handle all of these one level above? I.e. before dispatching in process_input_event(), do:

if self.niri.tablet_mode && matches!(event, ...) {
    return;
}

return;
}

// We need an output to be able to move the pointer.
if self.niri.global_space.outputs().next().is_none() {
return;
Expand Down Expand Up @@ -932,6 +942,10 @@ impl State {
}

fn on_pointer_button<I: InputBackend>(&mut self, event: I::PointerButtonEvent) {
if self.niri.tablet_mode {
return;
}

let pointer = self.niri.seat.get_pointer().unwrap();

let serial = SERIAL_COUNTER.next_serial();
Expand Down Expand Up @@ -997,6 +1011,9 @@ impl State {
}

fn on_pointer_axis<I: InputBackend>(&mut self, event: I::PointerAxisEvent) {
if self.niri.tablet_mode {
return;
}
let source = event.source();

let horizontal_amount = event
Expand Down Expand Up @@ -1538,6 +1555,14 @@ impl State {
};
handle.cancel(self);
}

fn on_switch_toggle<B: InputBackend>(&mut self, event: B::SwitchToggleEvent) {
if event.switch() == Some(smithay::backend::input::Switch::TabletMode) {
self.niri.tablet_mode = event.state() == smithay::backend::input::SwitchState::On;
// FIXME: redraw only outputs overlapping the cursor.
self.niri.queue_redraw_all();
}
}
}

/// Check whether the key should be intercepted and mark intercepted
Expand All @@ -1555,6 +1580,7 @@ fn should_intercept_key(
mods: ModifiersState,
screenshot_ui: &ScreenshotUi,
disable_power_key_handling: bool,
tablet_mode: bool,
) -> FilterResult<Option<Action>> {
// Actions are only triggered on presses, release of the key
// shouldn't try to intercept anything unless we have marked
Expand All @@ -1572,6 +1598,13 @@ fn should_intercept_key(
disable_power_key_handling,
);

if tablet_mode {
if final_action != Some(Action::ToggleTabletMode) {
suppressed_keys.insert(key_code);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this need to remove on key release?

return FilterResult::Intercept(None);
}
}

// Allow only a subset of compositor actions while the screenshot UI is open, since the user
// cannot see the screen.
if screenshot_ui.is_open() {
Expand Down Expand Up @@ -1842,6 +1875,7 @@ mod tests {

let screenshot_ui = ScreenshotUi::new();
let disable_power_key_handling = false;
let tablet_mode = false;

// The key_code we pick is arbitrary, the only thing
// that matters is that they are different between cases.
Expand All @@ -1859,6 +1893,7 @@ mod tests {
mods,
&screenshot_ui,
disable_power_key_handling,
tablet_mode,
)
};

Expand All @@ -1875,6 +1910,7 @@ mod tests {
mods,
&screenshot_ui,
disable_power_key_handling,
tablet_mode,
)
};

Expand Down
6 changes: 6 additions & 0 deletions src/niri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ pub struct Niri {
pub gesture_swipe_3f_cumulative: Option<(f64, f64)>,

pub lock_state: LockState,
pub tablet_mode: bool,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub tablet_mode: bool,
// Tablet mode for convertibles: disable regular pointer and keyboard input.
pub tablet_mode: bool,


pub screenshot_ui: ScreenshotUi,
pub config_error_notification: ConfigErrorNotification,
Expand Down Expand Up @@ -1096,6 +1097,7 @@ impl Niri {
gesture_swipe_3f_cumulative: None,

lock_state: LockState::Unlocked,
tablet_mode: false,

screenshot_ui,
config_error_notification,
Expand Down Expand Up @@ -1736,6 +1738,10 @@ impl Niri {
renderer: &mut R,
output: &Output,
) -> Vec<OutputRenderElements<R>> {
if self.tablet_mode && self.tablet_cursor_location.is_none() {
return vec![];
}

let _span = tracy_client::span!("Niri::pointer_element");
let output_scale = output.current_scale();
let output_pos = self.global_space.output_geometry(output).unwrap().loc;
Expand Down