jay-toml-config 0.9.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
mod context;
pub mod error;
mod extractor;
mod keysyms;
mod parser;
mod parsers;
mod spanned;
mod value;

use {
    crate::{
        config::{
            context::Context,
            parsers::config::{ConfigParser, ConfigParserError},
        },
        toml::{self},
    },
    ahash::AHashMap,
    jay_config::{
        input::{acceleration::AccelProfile, SwitchEvent},
        keyboard::{mods::Modifiers, Keymap, ModifiedKeySym},
        logging::LogLevel,
        status::MessageFormat,
        theme::Color,
        video::{Format, GfxApi, TearingMode, Transform, VrrMode},
        xwayland::XScalingMode,
        Axis, Direction, Workspace,
    },
    std::{
        error::Error,
        fmt::{Display, Formatter},
        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,
    ToggleFullscreen,
    ToggleMono,
    ToggleSplit,
    Forward(bool),
    EnableWindowManagement(bool),
}

#[derive(Debug, Clone)]
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,
    },
    SimpleCommand {
        cmd: SimpleCommand,
    },
    SwitchToVt {
        num: u32,
    },
    UnsetEnv {
        env: Vec<String>,
    },
    MoveToOutput {
        workspace: Option<Workspace>,
        output: OutputMatch,
    },
    SetRepeatRate {
        rate: RepeatRate,
    },
}

#[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 font: Option<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(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>,
}

#[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 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,
}

#[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 Xwayland {
    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 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 log_level: Option<LogLevel>,
    pub theme: Theme,
    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>,
}

#[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], handle_error: F) -> Option<Config>
where
    F: FnOnce(&dyn Error),
{
    let cx = Context {
        input,
        used: Default::default(),
    };
    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, |_| ()).unwrap();
}