jay-config 1.11.0

Configuration crate for the Jay compositor
Documentation
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
//! This crate allows you to configure the Jay compositor.
//!
//! A minimal example configuration looks as follows:
//!
//! ```rust
//! use jay_config::{config, quit, reload};
//! use jay_config::input::get_default_seat;
//! use jay_config::keyboard::mods::ALT;
//! use jay_config::keyboard::syms::{SYM_q, SYM_r};
//!
//! fn configure() {
//!     let seat = get_default_seat();
//!     // Create a key binding to exit the compositor.
//!     seat.bind(ALT | SYM_q, || quit());
//!     // Reload the configuration.
//!     seat.bind(ALT | SYM_r, || reload());
//! }
//!
//! config!(configure);
//! ```
//!
//! You should configure your crate to be compiled as a shared library:
//!
//! ```toml
//! [lib]
//! crate-type = ["cdylib"]
//! ```
//!
//! After compiling it, copy the shared library to `$HOME/.config/jay/config.so` and restart
//! the compositor. It should then use your configuration file.
//!
//! Note that you do not have to restart the compositor every time you want to reload your
//! configuration afterwards. Instead, simply invoke the [`reload`] function via a shortcut.

#![allow(
    clippy::zero_prefixed_literal,
    clippy::manual_range_contains,
    clippy::uninlined_format_args,
    clippy::len_zero,
    clippy::single_char_pattern,
    clippy::single_char_add_str,
    clippy::single_match
)]
#![warn(unsafe_op_in_unsafe_fn)]

use {
    crate::{
        _private::{WorkspaceShowOpV1, WorkspaceShowOpV2, ipc::WorkspaceSource},
        input::{FallbackOutputMode, Seat},
        keyboard::ModifiedKeySym,
        video::Connector,
        window::Window,
    },
    serde::{Deserialize, Serialize},
    std::{
        fmt::{Debug, Display, Formatter},
        time::Duration,
    },
};

#[macro_use]
mod macros;
#[doc(hidden)]
pub mod _private;
pub mod client;
pub mod embedded;
pub mod exec;
pub mod input;
pub mod io;
pub mod keyboard;
pub mod logging;
pub mod status;
pub mod tasks;
pub mod theme;
pub mod timer;
pub mod video;
pub mod window;
pub mod workspace;
pub mod xwayland;

/// A planar direction.
#[derive(Serialize, Deserialize, Copy, Clone, Debug, Eq, PartialEq)]
pub enum Direction {
    Left,
    Down,
    Up,
    Right,
}

/// A planar axis.
#[derive(Serialize, Deserialize, Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum Axis {
    Horizontal,
    Vertical,
}

impl Axis {
    /// Returns the axis orthogonal to `self`.
    pub fn other(self) -> Self {
        match self {
            Self::Horizontal => Self::Vertical,
            Self::Vertical => Self::Horizontal,
        }
    }
}

/// Exits the compositor.
pub fn quit() {
    get!().quit()
}

/// Switches to a different VT.
pub fn switch_to_vt(n: u32) {
    get!().switch_to_vt(n)
}

/// Reloads the configuration.
///
/// If the configuration cannot be reloaded, this function has no effect.
pub fn reload() {
    get!().reload()
}

/// Returns whether this execution of the configuration function is due to a reload.
///
/// This can be used to decide whether the configuration should auto-start programs.
pub fn is_reload() -> bool {
    get!(false).is_reload()
}

/// Sets whether new workspaces are captured by default.
///
/// The default is `true`.
pub fn set_default_workspace_capture(capture: bool) {
    get!().set_default_workspace_capture(capture)
}

/// Returns whether new workspaces are captured by default.
pub fn get_default_workspace_capture() -> bool {
    get!(true).get_default_workspace_capture()
}

/// Toggles whether new workspaces are captured by default.
pub fn toggle_default_workspace_capture() {
    let get = get!();
    get.set_default_workspace_capture(!get.get_default_workspace_capture());
}

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

impl Workspace {
    /// Returns whether this workspace existed at the time `Seat::get_workspace` was called.
    pub fn exists(self) -> bool {
        self.0 != 0
    }

    /// Sets whether the workspaces is captured.
    ///
    /// The default is determined by `set_default_workspace_capture`.
    pub fn set_capture(self, capture: bool) {
        get!().set_workspace_capture(self, capture)
    }

    /// Returns whether the workspaces is captured.
    pub fn get_capture(self) -> bool {
        get!(true).get_workspace_capture(self)
    }

    /// Toggles whether the workspaces is captured.
    pub fn toggle_capture(self) {
        let get = get!();
        get.set_workspace_capture(self, !get.get_workspace_capture(self));
    }

    /// Moves this workspace to another output.
    ///
    /// This has no effect if the workspace is not currently being shown.
    pub fn move_to_output(self, output: Connector) {
        get!().move_to_output(WorkspaceSource::Explicit(self), output);
    }

    /// Returns the root container of this workspace.
    ///
    /// If no such container exists, [`Window::exists`] returns false.
    pub fn window(self) -> Window {
        get!(Window(0)).get_workspace_window(self)
    }

    /// Returns the connector that contains this workspace.
    ///
    /// If no such connector exists, [`Connector::exists`] returns false.
    pub fn connector(self) -> Connector {
        get!(Connector(0)).get_workspace_connector(self)
    }

    /// Creates an operation to show this workspace.
    ///
    /// Does nothing until [`WorkspaceShowOp::exec`] is called.
    pub fn show(self) -> WorkspaceShowOp {
        WorkspaceShowOp {
            v1: WorkspaceShowOpV1 {
                workspace: self,
                connector: None,
                move_to_connector: false,
                seat: None,
                fallback_output_mode: None,
                focus: true,
            },
            v2: Default::default(),
        }
    }

    /// Returns the kind of this workspace.
    pub fn kind(self) -> WorkspaceKind {
        get!(WorkspaceKind::Normal).get_workspace_kind(self)
    }

    /// Hides this workspace.
    ///
    /// This has no effect for normal workspaces.
    pub fn hide(self) {
        get!().hide_workspace(self);
    }
}

/// Returns the workspace with the given name.
///
/// Workspaces are identified by their name. Calling this function alone does not create the
/// workspace if it doesn't already exist.
///
/// Workspaces and overlays share the same namespace. If an overlay with the same name
/// already exists, this request changes the pending kind of the workspace from `Overlay`
/// to `Normal`.
pub fn get_workspace(name: &str) -> Workspace {
    get!(Workspace(0)).get_workspace(name)
}

/// An operation to show a workspace.
///
/// Create this using [`Workspace::show`].
#[must_use]
pub struct WorkspaceShowOp {
    v1: WorkspaceShowOpV1,
    v2: WorkspaceShowOpV2,
}

impl WorkspaceShowOp {
    /// Runs this operation.
    pub fn exec(self) {
        get!().show_workspace_3(self);
    }

    /// The connector on which to show the workspace.
    ///
    /// If the workspace does not already exist, it will be shown on this connector.
    /// Otherwise, see [`WorkspaceShowOp::move_to_connector`].
    ///
    /// By default, this workspace is determined via the [`WorkspaceShowOp::seat`].
    pub fn connector(mut self, c: Connector) -> Self {
        self.v1.connector = Some(c);
        self
    }

    /// Whether to move the workspace to the target connector if it already exists.
    ///
    /// The default is `false`.
    pub fn move_to_connector(mut self, move_to_connector: bool) -> Self {
        self.v1.move_to_connector = move_to_connector;
        self
    }

    /// The reference seat.
    ///
    /// If no connector was explicitly set, this seat will be used to determine the target
    /// output.
    pub fn seat(mut self, s: Seat) -> Self {
        self.v1.seat = Some(s);
        self
    }

    /// The fallback output mode to use when the target output is determined via the
    /// [WorkspaceShowOp::seat].
    ///
    /// The default is determined via [`Seat::set_fallback_output_mode`].
    pub fn fallback_output_mode(mut self, mode: FallbackOutputMode) -> Self {
        self.v1.fallback_output_mode = Some(mode);
        self
    }

    /// Whether the workspace should grab the focus of the [`WorkspaceShowOp::seat`].
    ///
    /// The default is `true`.
    pub fn focus(mut self, focus: bool) -> Self {
        self.v1.focus = focus;
        self
    }

    /// Whether this operation should hide the workspace if it is already visible.
    ///
    /// This has no effect for normal workspaces.
    pub fn toggle(mut self, toggle: bool) -> Self {
        self.v2.toggle = Some(toggle);
        self
    }
}

/// The kind of a workspace.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum WorkspaceKind {
    /// A normal workspace.
    Normal,
    /// An overlay workspace.
    Overlay,
}

/// Returns the overlay with the given name.
///
/// Overlays are identified by their name. Calling this function alone does not create the
/// overlay if it doesn't already exist.
///
/// Workspaces and overlays share the same namespace. If a workspace with the same name
/// already exists, this request changes the pending kind of the workspace from `Normal`
/// to `Overlay`.
pub fn get_overlay(name: &str) -> Workspace {
    get!(Workspace(0)).get_overlay(name)
}

/// Hides all overlays.
pub fn hide_overlays() {
    get!().hide_overlays();
}

/// A PCI ID.
///
/// PCI IDs can be used to identify a hardware component. See the Debian [documentation][pci].
///
/// [pci]: https://wiki.debian.org/HowToIdentifyADevice/PCI
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Hash, Eq, PartialEq, Default)]
pub struct PciId {
    pub vendor: u32,
    pub model: u32,
}

impl Display for PciId {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:04x}:{:04x}", self.vendor, self.model)
    }
}

/// Sets the callback to be called when the display goes idle.
pub fn on_idle<F: FnMut() + 'static>(f: F) {
    get!().on_idle(f)
}

/// Sets the callback to be called when all devices have been enumerated.
///
/// This callback is only invoked once during the lifetime of the compositor. This is a
/// good place to select the DRM device used for rendering.
pub fn on_devices_enumerated<F: FnOnce() + 'static>(f: F) {
    get!().on_devices_enumerated(f)
}

/// Returns the Jay config directory.
pub fn config_dir() -> String {
    get!().config_dir()
}

/// Returns all visible workspaces.
pub fn workspaces() -> Vec<Workspace> {
    get!().workspaces()
}

/// Configures the idle timeout.
///
/// `None` disables the timeout.
///
/// The default is 10 minutes.
pub fn set_idle(timeout: Option<Duration>) {
    get!().set_idle(timeout.unwrap_or_default())
}

/// Configures the idle grace period.
///
/// The grace period starts after the idle timeout expires. During the grace period, the
/// screen goes black but the displays are not yet disabled and the idle callback (set
/// with [`on_idle`]) is not yet called. This is a purely visual effect to inform the user
/// that the machine will soon go idle.
///
/// The default is 5 seconds.
pub fn set_idle_grace_period(timeout: Duration) {
    get!().set_idle_grace_period(timeout)
}

/// Enables or disables explicit sync.
///
/// Calling this after the compositor has started has no effect.
///
/// The default is `true`.
pub fn set_explicit_sync_enabled(enabled: bool) {
    get!().set_explicit_sync_enabled(enabled);
}

/// Enables or disables dragging of tiles and workspaces.
///
/// The default is `true`.
pub fn set_ui_drag_enabled(enabled: bool) {
    get!().set_ui_drag_enabled(enabled);
}

/// Sets the distance at which ui dragging starts.
///
/// The default is `10`.
pub fn set_ui_drag_threshold(threshold: i32) {
    get!().set_ui_drag_threshold(threshold);
}

/// Enables or disables the color-management protocol.
///
/// The default is `false`.
///
/// Affected applications must be restarted for this to take effect.
pub fn set_color_management_enabled(enabled: bool) {
    get!().set_color_management_enabled(enabled);
}

/// Enables or disables the session-management protocol.
///
/// The default is `true`.
///
/// Affected applications must be restarted for this to take effect.
pub fn set_session_management_enabled(enabled: bool) {
    get!().set_session_management_enabled(enabled);
}

/// Sets whether floating windows are shown above fullscreen windows.
///
/// The default is `false`.
pub fn set_float_above_fullscreen(above: bool) {
    get!().set_float_above_fullscreen(above);
}

/// Gets whether floating windows are shown above fullscreen windows.
pub fn get_float_above_fullscreen() -> bool {
    get!().get_float_above_fullscreen()
}

/// Toggles whether floating windows are shown above fullscreen windows.
///
/// The default is `false`.
pub fn toggle_float_above_fullscreen() {
    set_float_above_fullscreen(!get_float_above_fullscreen())
}

/// Sets whether floating windows always show a pin icon.
///
/// Clicking on the pin icon toggles the pin mode. See [`Seat::toggle_float_pinned`].
///
/// The icon is always shown if the window is pinned. This setting only affects unpinned
/// windows.
pub fn set_show_float_pin_icon(show: bool) {
    get!().set_show_float_pin_icon(show);
}

/// Sets whether the built-in bar is shown.
///
/// The default is `true`.
pub fn set_show_bar(show: bool) {
    get!().set_show_bar(show)
}

/// Returns whether the built-in bar is shown.
pub fn get_show_bar() -> bool {
    get!(true).get_show_bar()
}

/// Toggles whether the built-in bar is shown.
pub fn toggle_show_bar() {
    let get = get!();
    get.set_show_bar(!get.get_show_bar());
}

/// Sets whether title bars on windows are shown.
///
/// The default is `true`.
pub fn set_show_titles(show: bool) {
    get!().set_show_titles(show)
}

/// Returns whether title bars on windows are shown.
pub fn get_show_titles() -> bool {
    get!(true).get_show_titles()
}

/// Toggles whether title bars on windows are shown.
pub fn toggle_show_titles() {
    let get = get!();
    get.set_show_titles(!get.get_show_titles());
}

/// Sets a callback to run when this config is unloaded.
///
/// Only one callback can be set at a time. If another callback is already set, it will be
/// dropped without being run.
///
/// This function can be used to terminate threads and clear reference cycles.
pub fn on_unload(f: impl FnOnce() + 'static) {
    get!().on_unload(f);
}

/// Enables or disables middle-click pasting.
///
/// This has no effect on applications that are already running.
///
/// The default is `true`.
#[doc(alias("primary-selection", "primary_selection"))]
pub fn set_middle_click_paste_enabled(enabled: bool) {
    get!().set_middle_click_paste_enabled(enabled);
}

/// Opens the control center.
pub fn open_control_center() {
    get!().open_control_center();
}