jay-toml-config 0.12.0

Internal dependency of 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
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
mod context;
pub mod error;
mod extractor;
mod keycodes;
mod parser;
mod parsers;
mod spanned;
mod value;

pub use crate::config::parsers::input_mode::InputMode;
use {
    crate::{
        config::{
            context::Context,
            parsers::{
                color_management::ColorManagement,
                config::{ConfigParser, ConfigParserError},
                float::Float,
                focus_history::FocusHistory,
            },
        },
        toml::{self},
    },
    ahash::AHashMap,
    jay_config::{
        Axis, Direction, Workspace,
        client::ClientCapabilities,
        input::{
            FallbackOutputMode, LayerDirection, SwitchEvent, Timeline, acceleration::AccelProfile,
            clickmethod::ClickMethod,
        },
        keyboard::{Keymap, ModifiedKeySym, mods::Modifiers, syms::KeySym},
        logging::LogLevel,
        status::MessageFormat,
        theme::{BarPosition, Color},
        video::{BlendSpace, ColorSpace, Eotf, Format, GfxApi, TearingMode, Transform, VrrMode},
        window::{ContentType, TileState, WindowType},
        workspace::WorkspaceDisplayOrder,
        xwayland::XScalingMode,
    },
    std::{
        cell::RefCell,
        error::Error,
        fmt::{Display, Formatter},
        rc::Rc,
        time::Duration,
    },
    thiserror::Error,
    toml::toml_parser,
};

#[derive(Debug, Copy, Clone)]
pub enum SimpleCommand {
    Close,
    DisablePointerConstraint,
    Focus(Direction),
    FocusParent,
    Move(Direction),
    None,
    Quit,
    ReloadConfigSo,
    ReloadConfigToml,
    Split(Axis),
    ToggleFloating,
    SetFloating(bool),
    ToggleFullscreen,
    SetFullscreen(bool),
    ToggleMono,
    SetMono(bool),
    ToggleSplit,
    SetSplit(Axis),
    Forward(bool),
    EnableWindowManagement(bool),
    SetFloatAboveFullscreen(bool),
    ToggleFloatAboveFullscreen,
    SetFloatPinned(bool),
    ToggleFloatPinned,
    KillClient,
    ShowBar(bool),
    ToggleBar,
    ShowTitles(bool),
    ToggleTitles,
    FocusHistory(Timeline),
    FocusLayerRel(LayerDirection),
    FocusTiles,
    CreateMark,
    JumpToMark,
    PopMode(bool),
    EnableSimpleIm(bool),
    ToggleSimpleImEnabled,
    ReloadSimpleIm,
    EnableUnicodeInput,
    OpenControlCenter,
}

#[derive(Debug, Clone)]
#[expect(clippy::enum_variant_names)]
pub enum Action {
    ConfigureConnector {
        con: ConfigConnector,
    },
    ConfigureDirectScanout {
        enabled: bool,
    },
    ConfigureDrmDevice {
        dev: ConfigDrmDevice,
    },
    ConfigureIdle {
        idle: Option<Duration>,
        grace_period: Option<Duration>,
    },
    ConfigureInput {
        input: Box<Input>,
    },
    ConfigureOutput {
        out: Output,
    },
    Exec {
        exec: Exec,
    },
    MoveToWorkspace {
        name: String,
    },
    Multi {
        actions: Vec<Action>,
    },
    SetEnv {
        env: Vec<(String, String)>,
    },
    SetGfxApi {
        api: GfxApi,
    },
    SetKeymap {
        map: ConfigKeymap,
    },
    SetLogLevel {
        level: LogLevel,
    },
    SetRenderDevice {
        dev: Box<DrmDeviceMatch>,
    },
    SetStatus {
        status: Option<Status>,
    },
    SetTheme {
        theme: Box<Theme>,
    },
    ShowWorkspace {
        name: String,
        output: Option<OutputMatch>,
    },
    SimpleCommand {
        cmd: SimpleCommand,
    },
    SwitchToVt {
        num: u32,
    },
    UnsetEnv {
        env: Vec<String>,
    },
    MoveToOutput {
        workspace: Option<Workspace>,
        output: Option<OutputMatch>,
        direction: Option<Direction>,
    },
    SetRepeatRate {
        rate: RepeatRate,
    },
    DefineAction {
        name: String,
        action: Box<Action>,
    },
    UndefineAction {
        name: String,
    },
    NamedAction {
        name: String,
    },
    CreateMark(u32),
    JumpToMark(u32),
    CopyMark(u32, u32),
    SetMode {
        name: String,
        latch: bool,
    },
}

#[derive(Debug, Clone, Default)]
pub struct Theme {
    pub attention_requested_bg_color: Option<Color>,
    pub bg_color: Option<Color>,
    pub bar_bg_color: Option<Color>,
    pub bar_status_text_color: Option<Color>,
    pub border_color: Option<Color>,
    pub captured_focused_title_bg_color: Option<Color>,
    pub captured_unfocused_title_bg_color: Option<Color>,
    pub focused_inactive_title_bg_color: Option<Color>,
    pub focused_inactive_title_text_color: Option<Color>,
    pub focused_title_bg_color: Option<Color>,
    pub focused_title_text_color: Option<Color>,
    pub separator_color: Option<Color>,
    pub unfocused_title_bg_color: Option<Color>,
    pub unfocused_title_text_color: Option<Color>,
    pub highlight_color: Option<Color>,
    pub border_width: Option<i32>,
    pub title_height: Option<i32>,
    pub bar_height: Option<i32>,
    pub font: Option<String>,
    pub title_font: Option<String>,
    pub bar_font: Option<String>,
    pub bar_position: Option<BarPosition>,
    pub bar_separator_width: Option<i32>,
}

#[derive(Debug, Clone, Default)]
pub struct Egui {
    pub proportional_fonts: Option<Vec<String>>,
    pub monospace_fonts: Option<Vec<String>>,
}

#[derive(Debug, Clone)]
pub struct Status {
    pub format: MessageFormat,
    pub exec: Exec,
    pub separator: Option<String>,
}

#[derive(Debug, Clone, Default)]
pub struct UiDrag {
    pub enabled: Option<bool>,
    pub threshold: Option<i32>,
}

#[derive(Debug, Clone)]
pub enum OutputMatch {
    Any(Vec<OutputMatch>),
    All {
        name: Option<String>,
        connector: Option<String>,
        serial_number: Option<String>,
        manufacturer: Option<String>,
        model: Option<String>,
    },
}

#[derive(Default, Debug, Clone)]
pub struct GenericMatch<Match> {
    pub name: Option<String>,
    pub not: Option<Box<Match>>,
    pub all: Option<Vec<Match>>,
    pub any: Option<Vec<Match>>,
    pub exactly: Option<MatchExactly<Match>>,
}

#[derive(Debug, Clone)]
pub struct MatchExactly<Match> {
    pub num: usize,
    pub list: Vec<Match>,
}

#[derive(Debug, Clone)]
pub struct ClientRule {
    pub name: Option<String>,
    pub match_: ClientMatch,
    pub action: Option<Action>,
    pub latch: Option<Action>,
    pub capabilities: Option<ClientCapabilities>,
    pub bounding_capabilities: Option<ClientCapabilities>,
}

#[derive(Default, Debug, Clone)]
pub struct ClientMatch {
    pub generic: GenericMatch<Self>,
    pub sandbox_engine: Option<String>,
    pub sandbox_engine_regex: Option<String>,
    pub sandbox_app_id: Option<String>,
    pub sandbox_app_id_regex: Option<String>,
    pub sandbox_instance_id: Option<String>,
    pub sandbox_instance_id_regex: Option<String>,
    pub sandboxed: Option<bool>,
    pub uid: Option<i32>,
    pub pid: Option<i32>,
    pub is_xwayland: Option<bool>,
    pub comm: Option<String>,
    pub comm_regex: Option<String>,
    pub exe: Option<String>,
    pub exe_regex: Option<String>,
    pub tag: Option<String>,
    pub tag_regex: Option<String>,
}

#[derive(Debug, Clone)]
pub struct WindowRule {
    pub name: Option<String>,
    pub match_: WindowMatch,
    pub action: Option<Action>,
    pub latch: Option<Action>,
    pub auto_focus: Option<bool>,
    pub initial_tile_state: Option<TileState>,
}

#[derive(Default, Debug, Clone)]
pub struct WindowMatch {
    pub generic: GenericMatch<Self>,
    pub types: Option<WindowType>,
    pub client: Option<ClientMatch>,
    pub title: Option<String>,
    pub title_regex: Option<String>,
    pub app_id: Option<String>,
    pub app_id_regex: Option<String>,
    pub floating: Option<bool>,
    pub visible: Option<bool>,
    pub urgent: Option<bool>,
    pub focused: Option<bool>,
    pub fullscreen: Option<bool>,
    pub just_mapped: Option<bool>,
    pub tag: Option<String>,
    pub tag_regex: Option<String>,
    pub x_class: Option<String>,
    pub x_class_regex: Option<String>,
    pub x_instance: Option<String>,
    pub x_instance_regex: Option<String>,
    pub x_role: Option<String>,
    pub x_role_regex: Option<String>,
    pub workspace: Option<String>,
    pub workspace_regex: Option<String>,
    pub content_types: Option<ContentType>,
}

#[derive(Debug, Clone)]
pub enum DrmDeviceMatch {
    Any(Vec<DrmDeviceMatch>),
    All {
        name: Option<String>,
        syspath: Option<String>,
        vendor: Option<u32>,
        vendor_name: Option<String>,
        model: Option<u32>,
        model_name: Option<String>,
        devnode: Option<String>,
    },
}

#[derive(Debug, Clone)]
pub struct Mode {
    pub width: i32,
    pub height: i32,
    pub refresh_rate: Option<f64>,
}

impl Display for Mode {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} x {}", self.width, self.height)?;
        if let Some(rr) = self.refresh_rate {
            write!(f, " @ {rr}")?;
        }
        Ok(())
    }
}

#[derive(Debug, Clone)]
pub struct Output {
    pub name: Option<String>,
    pub match_: OutputMatch,
    pub x: Option<i32>,
    pub y: Option<i32>,
    pub scale: Option<f64>,
    pub transform: Option<Transform>,
    pub mode: Option<Mode>,
    pub vrr: Option<Vrr>,
    pub tearing: Option<Tearing>,
    pub format: Option<Format>,
    pub color_space: Option<ColorSpace>,
    pub eotf: Option<Eotf>,
    pub brightness: Option<Option<f64>>,
    pub blend_space: Option<BlendSpace>,
    pub use_native_gamut: Option<bool>,
}

#[derive(Debug, Clone)]
pub enum ConnectorMatch {
    Any(Vec<ConnectorMatch>),
    All { connector: Option<String> },
}

#[derive(Debug, Clone)]
pub enum InputMatch {
    Any(Vec<InputMatch>),
    All {
        tag: Option<String>,
        name: Option<String>,
        syspath: Option<String>,
        devnode: Option<String>,
        is_keyboard: Option<bool>,
        is_pointer: Option<bool>,
        is_touch: Option<bool>,
        is_tablet_tool: Option<bool>,
        is_tablet_pad: Option<bool>,
        is_gesture: Option<bool>,
        is_switch: Option<bool>,
    },
}

#[derive(Debug, Clone)]
pub struct Input {
    pub tag: Option<String>,
    pub match_: InputMatch,
    pub accel_profile: Option<AccelProfile>,
    pub accel_speed: Option<f64>,
    pub tap_enabled: Option<bool>,
    pub tap_drag_enabled: Option<bool>,
    pub tap_drag_lock_enabled: Option<bool>,
    pub left_handed: Option<bool>,
    pub natural_scrolling: Option<bool>,
    pub click_method: Option<ClickMethod>,
    pub middle_button_emulation: Option<bool>,
    pub px_per_wheel_scroll: Option<f64>,
    pub transform_matrix: Option<[[f64; 2]; 2]>,
    pub keymap: Option<ConfigKeymap>,
    pub switch_actions: AHashMap<SwitchEvent, Action>,
    pub output: Option<Option<OutputMatch>>,
    pub calibration_matrix: Option<[[f32; 3]; 2]>,
}

#[derive(Debug, Clone)]
pub struct Exec {
    pub prog: String,
    pub args: Vec<String>,
    pub envs: Vec<(String, String)>,
    pub privileged: bool,
    pub tag: Option<String>,
}

#[derive(Debug, Clone)]
pub struct ConfigConnector {
    pub match_: ConnectorMatch,
    pub enabled: bool,
}

#[derive(Debug, Clone)]
pub struct ConfigDrmDevice {
    pub name: Option<String>,
    pub match_: DrmDeviceMatch,
    pub gfx_api: Option<GfxApi>,
    pub direct_scanout_enabled: Option<bool>,
    pub flip_margin_ms: Option<f64>,
}

#[derive(Debug, Clone)]
pub enum ConfigKeymap {
    Named(String),
    Literal(Keymap),
    Defined { name: String, map: Keymap },
}

#[derive(Debug, Clone)]
pub struct RepeatRate {
    pub rate: i32,
    pub delay: i32,
}

#[derive(Debug, Clone)]
pub struct Vrr {
    pub mode: Option<VrrMode>,
    pub cursor_hz: Option<f64>,
}

#[derive(Debug, Clone)]
pub struct SimpleIm {
    pub enabled: Option<bool>,
}

#[derive(Debug, Clone)]
pub struct Xwayland {
    pub enabled: Option<bool>,
    pub scaling_mode: Option<XScalingMode>,
}

#[derive(Debug, Clone)]
pub struct Tearing {
    pub mode: Option<TearingMode>,
}

#[derive(Debug, Clone, Default)]
pub struct Libei {
    pub enable_socket: Option<bool>,
}

#[derive(Debug, Clone)]
pub struct Shortcut {
    pub mask: Modifiers,
    pub keysym: ModifiedKeySym,
    pub action: Action,
    pub latch: Option<Action>,
}

#[derive(Debug, Clone)]
pub struct NamedAction {
    pub name: Rc<String>,
    pub action: Action,
}

#[derive(Debug, Clone)]
pub struct Config {
    pub keymap: Option<ConfigKeymap>,
    pub repeat_rate: Option<RepeatRate>,
    pub shortcuts: Vec<Shortcut>,
    pub on_graphics_initialized: Option<Action>,
    pub on_idle: Option<Action>,
    pub status: Option<Status>,
    pub connectors: Vec<ConfigConnector>,
    pub outputs: Vec<Output>,
    pub workspace_capture: bool,
    pub env: Vec<(String, String)>,
    pub on_startup: Option<Action>,
    pub keymaps: Vec<ConfigKeymap>,
    pub auto_reload: Option<bool>,
    pub log_level: Option<LogLevel>,
    pub theme: Theme,
    pub egui: Egui,
    pub gfx_api: Option<GfxApi>,
    pub direct_scanout_enabled: Option<bool>,
    pub drm_devices: Vec<ConfigDrmDevice>,
    pub render_device: Option<DrmDeviceMatch>,
    pub inputs: Vec<Input>,
    pub idle: Option<Duration>,
    pub grace_period: Option<Duration>,
    pub explicit_sync_enabled: Option<bool>,
    pub focus_follows_mouse: bool,
    pub window_management_key: Option<ModifiedKeySym>,
    pub vrr: Option<Vrr>,
    pub tearing: Option<Tearing>,
    pub libei: Libei,
    pub ui_drag: UiDrag,
    pub xwayland: Option<Xwayland>,
    pub color_management: Option<ColorManagement>,
    pub float: Option<Float>,
    pub named_actions: Vec<NamedAction>,
    pub max_action_depth: u64,
    pub client_rules: Vec<ClientRule>,
    pub window_rules: Vec<WindowRule>,
    pub pointer_revert_key: Option<KeySym>,
    pub use_hardware_cursor: Option<bool>,
    pub show_bar: Option<bool>,
    pub show_titles: Option<bool>,
    pub focus_history: Option<FocusHistory>,
    pub middle_click_paste: Option<bool>,
    pub input_modes: AHashMap<String, InputMode>,
    pub workspace_display_order: Option<WorkspaceDisplayOrder>,
    pub simple_im: Option<SimpleIm>,
    pub fallback_output_mode: Option<FallbackOutputMode>,
}

#[derive(Debug, Error)]
pub enum ConfigError {
    #[error("Could not parse the toml document")]
    Toml(#[from] toml_parser::ParserError),
    #[error("Could not interpret the toml as a config document")]
    Parser(#[from] ConfigParserError),
}

pub fn parse_config<F>(
    input: &[u8],
    mark_names: &RefCell<AHashMap<String, u32>>,
    handle_error: F,
) -> Option<Config>
where
    F: FnOnce(&dyn Error),
{
    let cx = Context {
        input,
        used: Default::default(),
        mark_names,
    };
    macro_rules! fatal {
        ($e:expr) => {{
            let e = ConfigError::from($e.value);
            let e = cx.error2($e.span, e);
            handle_error(&e);
            return None;
        }};
    }
    let toml = match toml_parser::parse(input, &cx) {
        Ok(t) => t,
        Err(e) => fatal!(e),
    };
    let config = match toml.parse(&mut ConfigParser(&cx)) {
        Ok(c) => c,
        Err(e) => fatal!(e),
    };
    let used = cx.used.take();
    macro_rules! check_defined {
        ($name:expr, $used:ident, $defined:ident) => {
            for spanned in &used.$used {
                if !used.$defined.contains(spanned) {
                    log::warn!(
                        "{} {} used but not defined: {}",
                        $name,
                        spanned.value,
                        cx.error3(spanned.span),
                    );
                }
            }
        };
    }
    check_defined!("Keymap", keymaps, defined_keymaps);
    check_defined!("DRM device", drm_devices, defined_drm_devices);
    check_defined!("Output", outputs, defined_outputs);
    check_defined!("Input", inputs, defined_inputs);
    Some(config)
}

#[test]
fn default_config_parses() {
    let input = include_bytes!("default-config.toml");
    parse_config(input, &Default::default(), |_| ()).unwrap();
}