jay_config/
input.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//! Tools for configuring input devices.

pub mod acceleration;
pub mod capability;

use {
    crate::{
        input::{acceleration::AccelProfile, capability::Capability},
        keyboard::{mods::Modifiers, Keymap},
        Axis, Direction, ModifiedKeySym, Workspace,
        _private::{ipc::WorkspaceSource, DEFAULT_SEAT_NAME},
        video::Connector,
    },
    serde::{Deserialize, Serialize},
    std::time::Duration,
};

/// An input device.
#[derive(Serialize, Deserialize, Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub struct InputDevice(pub u64);

impl InputDevice {
    /// Assigns the input device to a seat.
    pub fn set_seat(self, seat: Seat) {
        get!().set_seat(self, seat)
    }

    /// Sets the keymap of the device.
    ///
    /// This overrides the keymap set for the seat. The keymap becomes active when a key
    /// on the device is pressed.
    ///
    /// Setting the invalid keymap reverts to the seat keymap.
    pub fn set_keymap(self, keymap: Keymap) {
        get!().set_device_keymap(self, keymap)
    }

    /// Returns whether the device has the specified capability.
    pub fn has_capability(self, cap: Capability) -> bool {
        get!(false).has_capability(self, cap)
    }

    /// Sets the device to be left handed.
    ///
    /// This has the effect of swapping the left and right mouse button. See the libinput
    /// documentation for more details.
    pub fn set_left_handed(self, left_handed: bool) {
        get!().set_left_handed(self, left_handed);
    }

    /// Sets the acceleration profile of the device.
    ///
    /// This corresponds to the libinput setting of the same name.
    pub fn set_accel_profile(self, profile: AccelProfile) {
        get!().set_accel_profile(self, profile);
    }

    /// Sets the acceleration speed of the device.
    ///
    /// This corresponds to the libinput setting of the same name.
    pub fn set_accel_speed(self, speed: f64) {
        get!().set_accel_speed(self, speed);
    }

    /// Sets the transformation matrix of the device.
    ///
    /// This is not a libinput setting but a setting of the compositor. It currently affects
    /// relative mouse motions in that the matrix is applied to the motion. To reduce the mouse
    /// speed to 35%, use the following matrix:
    ///
    /// ```text
    /// [
    ///     [0.35, 1.0],
    ///     [1.0, 0.35],
    /// ]
    /// ```
    ///
    /// This might give you better results than using `set_accel_profile` and `set_accel_speed`.
    pub fn set_transform_matrix(self, matrix: [[f64; 2]; 2]) {
        get!().set_transform_matrix(self, matrix);
    }

    /// Sets the calibration matrix of the device.
    ///
    /// This corresponds to the libinput setting of the same name.
    pub fn set_calibration_matrix(self, matrix: [[f32; 3]; 2]) {
        get!().set_calibration_matrix(self, matrix);
    }

    /// Returns the name of the device.
    pub fn name(self) -> String {
        get!(String::new()).device_name(self)
    }

    /// Sets how many pixel to scroll per scroll wheel dedent.
    ///
    /// Default: `15.0`
    ///
    /// This setting has no effect on non-wheel input such as touchpads.
    ///
    /// Some mouse wheels support high-resolution scrolling without discrete steps. In
    /// this case a value proportional to this setting will be used.
    pub fn set_px_per_wheel_scroll(self, px: f64) {
        get!().set_px_per_wheel_scroll(self, px);
    }

    /// Sets whether tap-to-click is enabled for this device.
    ///
    /// See <https://wayland.freedesktop.org/libinput/doc/latest/tapping.html>
    pub fn set_tap_enabled(self, enabled: bool) {
        get!().set_input_tap_enabled(self, enabled);
    }

    /// Sets whether tap-and-drag is enabled for this device.
    ///
    /// See <https://wayland.freedesktop.org/libinput/doc/latest/tapping.html>
    pub fn set_drag_enabled(self, enabled: bool) {
        get!().set_input_drag_enabled(self, enabled);
    }

    /// Sets whether drag lock is enabled for this device.
    ///
    /// See <https://wayland.freedesktop.org/libinput/doc/latest/tapping.html>
    pub fn set_drag_lock_enabled(self, enabled: bool) {
        get!().set_input_drag_lock_enabled(self, enabled);
    }

    /// Sets whether natural scrolling is enabled for this device.
    ///
    /// See <https://wayland.freedesktop.org/libinput/doc/latest/scrolling.html>
    pub fn set_natural_scrolling_enabled(self, enabled: bool) {
        get!().set_input_natural_scrolling_enabled(self, enabled);
    }

    /// Returns the syspath of this device.
    ///
    /// E.g. `/sys/devices/pci0000:00/0000:00:08.1/0000:14:00.4/usb5/5-1/5-1.1/5-1.1.3/5-1.1.3:1.0`.
    pub fn syspath(self) -> String {
        get!(String::new()).input_device_syspath(self)
    }

    /// Returns the devnode of this device.
    ///
    /// E.g. `/dev/input/event7`.
    pub fn devnode(self) -> String {
        get!(String::new()).input_device_devnode(self)
    }

    /// Sets a callback that will be run if this device triggers a switch event.
    pub fn on_switch_event<F: FnMut(SwitchEvent) + 'static>(self, f: F) {
        get!().on_switch_event(self, f)
    }

    /// Maps this input device to a connector.
    ///
    /// The connector should be connected.
    ///
    /// This should be used for touch screens and graphics tablets.
    pub fn set_connector(self, connector: Connector) {
        get!().set_input_device_connector(self, connector);
    }

    /// Removes the mapping of this device to a connector.
    pub fn remove_mapping(self) {
        get!().remove_input_mapping(self);
    }
}

/// A seat.
#[derive(Serialize, Deserialize, Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub struct Seat(pub u64);

impl Seat {
    pub const INVALID: Self = Self(0);

    /// Returns whether the seat is invalid.
    pub fn is_invalid(self) -> bool {
        self == Self::INVALID
    }

    #[doc(hidden)]
    pub fn raw(self) -> u64 {
        self.0
    }

    #[doc(hidden)]
    pub fn from_raw(raw: u64) -> Self {
        Self(raw)
    }

    /// Sets whether this seat's cursor uses the hardware cursor if available.
    ///
    /// Only one seat at a time can use the hardware cursor. Setting this to `true` for a
    /// seat automatically unsets it for all other seats.
    ///
    /// By default, the first created seat uses the hardware cursor.
    pub fn use_hardware_cursor(self, use_hardware_cursor: bool) {
        get!().set_use_hardware_cursor(self, use_hardware_cursor);
    }

    /// Sets the size of the cursor theme.
    ///
    /// Default: 16.
    pub fn set_cursor_size(self, size: i32) {
        get!().set_cursor_size(self, size)
    }

    /// Creates a compositor-wide hotkey.
    ///
    /// The closure is invoked when the user presses the last key of the modified keysym.
    /// Note that the keysym is calculated without modifiers applied. To perform an action
    /// when `SHIFT+k` is pressed, use `SHIFT | SYM_k` not `SHIFT | SYM_K`.
    ///
    /// CapsLock and NumLock are ignored during modifier evaluation. Therefore, bindings
    /// containing these modifiers will never be invoked.
    pub fn bind<T: Into<ModifiedKeySym>, F: FnMut() + 'static>(self, mod_sym: T, f: F) {
        self.bind_masked(Modifiers(!0), mod_sym, f)
    }

    /// Creates a compositor-wide hotkey while ignoring some modifiers.
    ///
    /// This is similar to `bind` except that only the masked modifiers are considered.
    ///
    /// For example, if this function is invoked with `mod_mask = Modifiers::NONE` and
    /// `mod_sym = SYM_XF86AudioRaiseVolume`, then the callback will be invoked whenever
    /// `SYM_XF86AudioRaiseVolume` is pressed. Even if the user is simultaneously holding
    /// the shift key which would otherwise prevent the callback from taking effect.
    ///
    /// For example, if this function is invoked with `mod_mask = CTRL | SHIFT` and
    /// `mod_sym = CTRL | SYM_x`, then the callback will be invoked whenever the user
    /// presses `ctrl+x` without pressing the shift key. Even if the user is
    /// simultaneously holding the alt key.
    ///
    /// If `mod_sym` contains any modifiers, then these modifiers are automatically added
    /// to the mask. The synthetic `RELEASE` modifier is always added to the mask.
    pub fn bind_masked<T: Into<ModifiedKeySym>, F: FnMut() + 'static>(
        self,
        mod_mask: Modifiers,
        mod_sym: T,
        f: F,
    ) {
        get!().bind_masked(self, mod_mask, mod_sym.into(), f)
    }

    /// Registers a callback to be executed when the currently pressed key is released.
    ///
    /// This should only be called in callbacks for key-press binds.
    ///
    /// The callback will be executed once when the key is released regardless of any
    /// modifiers.
    pub fn latch<F: FnOnce() + 'static>(self, f: F) {
        get!().latch(self, f)
    }

    /// Unbinds a hotkey.
    pub fn unbind<T: Into<ModifiedKeySym>>(self, mod_sym: T) {
        get!().unbind(self, mod_sym.into())
    }

    /// Moves the keyboard focus of the seat in the specified direction.
    pub fn focus(self, direction: Direction) {
        get!().focus(self, direction)
    }

    /// Moves the focused window in the specified direction.
    pub fn move_(self, direction: Direction) {
        get!().move_(self, direction)
    }

    /// Sets the keymap of the seat.
    pub fn set_keymap(self, keymap: Keymap) {
        get!().seat_set_keymap(self, keymap)
    }

    /// Returns the repeat rate of the seat.
    ///
    /// The returned tuple is `(rate, delay)` where `rate` is the number of times keys repeat per second
    /// and `delay` is the time after the button press after which keys start repeating.
    pub fn repeat_rate(self) -> (i32, i32) {
        get!((25, 250)).seat_get_repeat_rate(self)
    }

    /// Sets the repeat rate of the seat.
    pub fn set_repeat_rate(self, rate: i32, delay: i32) {
        get!().seat_set_repeat_rate(self, rate, delay)
    }

    /// Returns whether the parent-container of the currently focused window is in mono-mode.
    pub fn mono(self) -> bool {
        get!(false).mono(self)
    }

    /// Sets whether the parent-container of the currently focused window is in mono-mode.
    pub fn set_mono(self, mono: bool) {
        get!().set_mono(self, mono)
    }

    /// Toggles whether the parent-container of the currently focused window is in mono-mode.
    pub fn toggle_mono(self) {
        self.set_mono(!self.mono());
    }

    /// Returns the split axis of the parent-container of the currently focused window.
    pub fn split(self) -> Axis {
        get!(Axis::Horizontal).split(self)
    }

    /// Sets the split axis of the parent-container of the currently focused window.
    pub fn set_split(self, axis: Axis) {
        get!().set_split(self, axis)
    }

    /// Toggles the split axis of the parent-container of the currently focused window.
    pub fn toggle_split(self) {
        self.set_split(self.split().other());
    }

    /// Returns the input devices assigned to this seat.
    pub fn input_devices(self) -> Vec<InputDevice> {
        get!().get_input_devices(Some(self))
    }

    /// Creates a new container with the specified split in place of the currently focused window.
    pub fn create_split(self, axis: Axis) {
        get!().create_split(self, axis);
    }

    /// Focuses the parent node of the currently focused window.
    pub fn focus_parent(self) {
        get!().focus_parent(self);
    }

    /// Requests the currently focused window to be closed.
    pub fn close(self) {
        get!().close(self);
    }

    /// Returns whether the currently focused window is floating.
    pub fn get_floating(self) -> bool {
        get!().get_floating(self)
    }
    /// Sets whether the currently focused window is floating.
    pub fn set_floating(self, floating: bool) {
        get!().set_floating(self, floating);
    }

    /// Toggles whether the currently focused window is floating.
    ///
    /// You can do the same by double-clicking on the header.
    pub fn toggle_floating(self) {
        get!().toggle_floating(self);
    }

    /// Returns the workspace that is currently active on the output that contains the seat's
    /// cursor.
    ///
    /// If no such workspace exists, `exists` returns `false` for the returned workspace.
    pub fn get_workspace(self) -> Workspace {
        get!(Workspace(0)).get_seat_workspace(self)
    }

    /// Shows the workspace and sets the keyboard focus of the seat to that workspace.
    ///
    /// If the workspace doesn't currently exist, it is created on the output that contains the
    /// seat's cursor.
    pub fn show_workspace(self, workspace: Workspace) {
        get!().show_workspace(self, workspace)
    }

    /// Moves the currently focused window to the workspace.
    pub fn set_workspace(self, workspace: Workspace) {
        get!().set_workspace(self, workspace)
    }

    /// Toggles whether the currently focused window is fullscreen.
    pub fn toggle_fullscreen(self) {
        let c = get!();
        c.set_fullscreen(self, !c.get_fullscreen(self));
    }
    /// Returns whether the currently focused window is fullscreen.
    pub fn fullscreen(self) -> bool {
        get!(false).get_fullscreen(self)
    }

    /// Sets whether the currently focused window is fullscreen.
    pub fn set_fullscreen(self, fullscreen: bool) {
        get!().set_fullscreen(self, fullscreen)
    }

    /// Disables the currently active pointer constraint on this seat.
    pub fn disable_pointer_constraint(self) {
        get!().disable_pointer_constraint(self)
    }

    /// Moves the currently focused workspace to another output.
    pub fn move_to_output(self, connector: Connector) {
        get!().move_to_output(WorkspaceSource::Seat(self), connector);
    }

    /// Set whether the current key event is forwarded to the focused client.
    ///
    /// This only has an effect if called from a keyboard shortcut.
    ///
    /// By default, release events are forwarded and press events are consumed. Note that
    /// consuming release events can cause clients to get stuck in the pressed state.
    pub fn set_forward(self, forward: bool) {
        get!().set_forward(self, forward);
    }

    /// This is a shorthand for `set_forward(true)`.
    pub fn forward(self) {
        self.set_forward(true)
    }

    /// This is a shorthand for `set_forward(false)`.
    pub fn consume(self) {
        self.set_forward(false)
    }

    /// Sets the focus-follows-mouse mode.
    pub fn set_focus_follows_mouse_mode(self, mode: FocusFollowsMouseMode) {
        get!().set_focus_follows_mouse_mode(self, mode);
    }

    /// Enables or disable window management mode.
    ///
    /// In window management mode, floating windows can be moved by pressing the left
    /// mouse button and all windows can be resize by pressing the right mouse button.
    pub fn set_window_management_enabled(self, enabled: bool) {
        get!().set_window_management_enabled(self, enabled);
    }

    /// Sets a key that enables window management mode while pressed.
    ///
    /// This is a shorthand for
    ///
    /// ```rust,ignore
    /// self.bind(mod_sym, move || {
    ///     self.set_window_management_enabled(true);
    ///     self.forward();
    ///     self.latch(move || {
    ///         self.set_window_management_enabled(false);
    ///     });
    /// });
    /// ```
    pub fn set_window_management_key<T: Into<ModifiedKeySym>>(self, mod_sym: T) {
        self.bind(mod_sym, move || {
            self.set_window_management_enabled(true);
            self.forward();
            self.latch(move || {
                self.set_window_management_enabled(false);
            });
        });
    }
}

/// A focus-follows-mouse mode.
#[derive(Serialize, Deserialize, Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum FocusFollowsMouseMode {
    /// When the mouse moves and enters a toplevel, that toplevel gets the keyboard focus.
    True,
    /// The keyboard focus changes only when clicking on a window or the previously
    /// focused window becomes invisible.
    False,
}

/// Returns all seats.
pub fn get_seats() -> Vec<Seat> {
    get!().seats()
}

/// Returns all input devices.
pub fn input_devices() -> Vec<InputDevice> {
    get!().get_input_devices(None)
}

/// Returns or creates a seat.
///
/// Seats are identified by their name. If no seat with the name exists, a new seat will be created.
///
/// NOTE: You should prefer [`get_default_seat`] instead. Most applications cannot handle more than
/// one seat and will only process input from one of the seats.
pub fn get_seat(name: &str) -> Seat {
    get!(Seat(0)).get_seat(name)
}

/// Returns or creates the default seat.
///
/// This is equivalent to `get_seat("default")`.
pub fn get_default_seat() -> Seat {
    get_seat(DEFAULT_SEAT_NAME)
}

/// Sets a closure to run when a new seat has been created.
pub fn on_new_seat<F: FnMut(Seat) + 'static>(f: F) {
    get!().on_new_seat(f)
}

/// Sets a closure to run when a new input device has been added.
pub fn on_new_input_device<F: FnMut(InputDevice) + 'static>(f: F) {
    get!().on_new_input_device(f)
}

/// Sets a closure to run when an input device has been removed.
pub fn on_input_device_removed<F: FnMut(InputDevice) + 'static>(f: F) {
    get!().on_input_device_removed(f)
}

/// Sets the maximum time between two clicks to be registered as a double click by the
/// compositor.
///
/// This only affects interactions with the compositor UI and has no effect on
/// applications.
///
/// The default is 400 ms.
pub fn set_double_click_time(duration: Duration) {
    let usec = duration.as_micros().min(u64::MAX as u128);
    get!().set_double_click_interval(usec as u64)
}

/// Sets the maximum distance between two clicks to be registered as a double click by the
/// compositor.
///
/// This only affects interactions with the compositor UI and has no effect on
/// applications.
///
/// Setting a negative distance disables double clicks.
///
/// The default is 5.
pub fn set_double_click_distance(distance: i32) {
    get!().set_double_click_distance(distance)
}

/// Disables the creation of a default seat.
///
/// Unless this function is called at startup of the compositor, a seat called `default`
/// will automatically be created.
///
/// When a new input device is attached and a seat called `default` exists, the input
/// device is initially attached to this seat.
pub fn disable_default_seat() {
    get!().disable_default_seat();
}

/// An event generated by a switch.
#[derive(Serialize, Deserialize, Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum SwitchEvent {
    /// The lid of the device (usually a laptop) has been opened.
    ///
    /// This is the default state.
    LidOpened,
    /// The lid of the device (usually a laptop) has been closed.
    ///
    /// If the device is already in this state when the device is discovered, a synthetic
    /// event of this kind is generated.
    LidClosed,
    /// The device has been converted from tablet to laptop mode.
    ///
    /// This is the default state.
    ConvertedToLaptop,
    /// The device has been converted from laptop to tablet mode.
    ///
    /// If the device is already in this state when the device is discovered, a synthetic
    /// event of this kind is generated.
    ConvertedToTablet,
}

/// Enables or disables the unauthenticated libei socket.
///
/// Even if the socket is disabled, application can still request access via the portal.
///
/// The default is `false`.
pub fn set_libei_socket_enabled(enabled: bool) {
    get!().set_ei_socket_enabled(enabled);
}