xremap 0.15.7

Dynamic key remap for X and Wayland
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
use crate::common::{
    fetch_events, get_random_device_name, get_virtual_device, key_press, key_release, wait_for_device,
    wait_for_grabbed, VirtualDeviceInfo,
};
use anyhow::{bail, Result};
use evdev::{Device, EventType, FetchEventsSynced, InputEvent, KeyCode as Key};
use std::cell::Cell;
use std::iter::repeat_with;
use std::path::PathBuf;
use std::process::{Child, Command, Stdio};
use std::time::{Duration, Instant};
use xremap::device::SEPARATOR;
use xremap::util::until;

pub enum InputDeviceFilter {
    NoFilter,
    RandomName,
    CustomFilter { filter: String },
}

pub struct XremapBuilder {
    nocapture_: bool,
    log_level_: String,
    allow_stdio_errors_: bool,
    // None means open a new input device
    custom_input_device_: InputDeviceFilter,
    ignore_device_: Option<String>,
    open_for_fetch_: bool,
    watch_: bool,
    watch_config_: bool,
    config_file: Option<String>,
}

impl XremapBuilder {
    fn new() -> Self {
        Self {
            nocapture_: false,
            log_level_: "debug".into(),
            allow_stdio_errors_: false,
            custom_input_device_: InputDeviceFilter::RandomName,
            ignore_device_: None,
            // If output from xremap isn't grabbed, the events
            // goes to the 'host', so disable with care.
            open_for_fetch_: true,
            watch_: false,
            watch_config_: false,
            config_file: None,
        }
    }

    pub fn nocapture(&mut self) -> &mut Self {
        self.nocapture_ = true;
        self
    }

    pub fn log_level(&mut self, log_level: impl Into<String>) -> &mut Self {
        self.log_level_ = log_level.into();
        self
    }

    pub fn allow_stdio_errors(&mut self, allow_stdio_errors: bool) -> &mut Self {
        self.allow_stdio_errors_ = allow_stdio_errors;
        self
    }

    pub fn input_device(&mut self, filter: InputDeviceFilter) -> &mut Self {
        self.custom_input_device_ = filter;
        self
    }

    pub fn ignore_device(&mut self, name: impl Into<String>) -> &mut Self {
        self.ignore_device_ = Some(name.into());
        self
    }

    pub fn not_open_for_fetch(&mut self) -> &mut Self {
        self.open_for_fetch_ = false;
        self
    }

    pub fn watch(&mut self, value: bool) -> &mut Self {
        self.watch_ = value;
        self
    }

    pub fn watch_config(&mut self, config: &str) -> Result<&mut Self> {
        // Custom config file is required, because static config file must not be changed.
        self.config(config)?;
        self.watch_config_ = true;
        Ok(self)
    }

    pub fn config(&mut self, config: &str) -> Result<&mut Self> {
        let filename =
            format!("xremap_config_{}.yml", repeat_with(fastrand::alphanumeric).take(10).collect::<String>());
        let config_file = std::env::temp_dir().join(filename);

        self.config_file = Some(config_file.to_string_lossy().into());

        std::fs::write(&config_file, config)?;

        Ok(self)
    }

    pub fn build(&mut self) -> anyhow::Result<XremapController> {
        XremapController::from_builder(self)
    }
}

#[derive(Debug)]
pub struct Output {
    pub stdout: String,
    pub stderr: String,
}

// Devices are managed tightly to avoid possible
// destructive events emitted to the 'host'.
pub struct XremapController {
    // Is None when xremap has been stopped.
    child: Cell<Option<Child>>,
    nocapture: bool,
    allow_stdio_errors: bool,
    // Input from xremap's perspective
    input_device: Option<VirtualDeviceInfo>,
    // Output from xremap's perspective
    output_device_name: String,
    output_device: Option<Device>,
    device_filter: Option<String>,
    config_file: String,
}

impl XremapController {
    pub fn builder() -> XremapBuilder {
        XremapBuilder::new()
    }

    pub fn new() -> anyhow::Result<Self> {
        XremapController::builder().build()
    }

    fn from_builder(def: &XremapBuilder) -> anyhow::Result<Self> {
        let path = match std::env::var("CARGO_TARGET_DIR") {
            Ok(path) => PathBuf::from(path).join("debug/xremap"),
            Err(_) => PathBuf::from("target/debug/xremap"),
        };

        let mut command = Command::new(path);

        let output_device_name =
            format!("test output device {}", repeat_with(fastrand::alphanumeric).take(10).collect::<String>());

        let builder = command
            .env("RUST_LOG", &def.log_level_)
            .args(vec!["--output-device-name", &output_device_name]);

        let config_file = match &def.config_file {
            Some(config) => {
                println!("Using custom config: {:?}", config);
                builder.arg(config);
                config
            }
            None => {
                let config = "tests/common/config-test.yml";
                builder.arg(config);
                config
            }
        };

        if !def.nocapture_ {
            // Can remove these to get stdio from xremap
            // inline with the stdio from test cases.
            // That makes it easier to debug.
            // But some tests assert on the stdio so they will fail
            // when stdio isn't buffered.
            builder.stdout(Stdio::piped()).stderr(Stdio::piped());
        }

        let mut input_device: Option<VirtualDeviceInfo> = None;

        if def.watch_ {
            builder.arg("--watch");
        }

        if def.watch_config_ {
            builder.arg("--watch=config");
        }

        let device_filter = match &def.custom_input_device_ {
            InputDeviceFilter::NoFilter => {
                // When no device filter the test can't run
                // in parallel with other tests.
                None
            }
            InputDeviceFilter::RandomName => {
                // Use a unique device for xremap to grab
                // so test cases can run in parallel.
                let name = get_random_device_name();

                input_device = Some(get_virtual_device(&name)?);

                Some(name)
            }
            InputDeviceFilter::CustomFilter { filter } => Some(filter.clone()),
        };

        if let Some(device_filter) = &device_filter {
            builder.arg("--device").arg(device_filter);
        }

        if let Some(ignore_device) = &def.ignore_device_ {
            builder.arg("--ignore").arg(ignore_device);
        }

        let child = builder.spawn()?;

        let mut ctrl = Self {
            child: Cell::new(Some(child)),
            nocapture: def.nocapture_,
            allow_stdio_errors: def.allow_stdio_errors_,
            input_device,
            output_device_name,
            output_device: None,
            device_filter,
            config_file: config_file.to_string(),
        };

        match &ctrl.input_device {
            None => {
                println!("No input device configured for xremap.");
            }
            Some(input_device) => {
                wait_for_grabbed(&input_device.path)?;

                println!("Input device grabbed by xremap");
            }
        }

        // Default is to grab the device xremap opens to avoid
        // possibly destructive events to be send to the 'host'.
        if def.open_for_fetch_ {
            ctrl.open_output_device()?;
        }

        Ok(ctrl)
    }

    pub fn get_input_device_name<'a>(&'a mut self) -> &'a Option<String> {
        &self.device_filter
    }

    pub fn get_config_file<'a>(&'a mut self) -> &'a str {
        &self.config_file
    }

    pub fn open_input_device(&mut self, name: impl Into<String>) -> anyhow::Result<()> {
        if self.input_device.is_some() {
            bail!("Input device already opened.")
        }

        println!("Preparing new input device for xremap, that is already running.");

        let dev_info = get_virtual_device(name)?;

        wait_for_grabbed(&dev_info.path)?;

        println!("Input device grabbed by xremap");

        self.input_device = Some(dev_info);

        Ok(())
    }

    pub fn close_input_device(&mut self) -> anyhow::Result<()> {
        if self.input_device.is_none() {
            bail!("Input device not opened.")
        }

        self.input_device = None;
        println!("Input device closed");

        Ok(())
    }

    pub fn open_output_device(&mut self) -> anyhow::Result<()> {
        if self.output_device.is_some() {
            bail!("Output device already opened.")
        }

        let (_, mut device) = wait_for_device(&self.output_device_name)?;

        device.grab()?;

        println!("Output device from xremap grabbed");

        self.output_device = Some(device);

        Ok(())
    }

    pub fn emit_events(&mut self, events: &[InputEvent]) -> anyhow::Result<()> {
        let input_device = self.input_device.as_mut().expect("Input device is not opened");

        let mut probe_device = Device::open(&input_device.path)?;

        if probe_device.grab().is_ok() {
            // Emitting events here would go to the 'host'
            // because xremap has not grabbed the device.
            probe_device.ungrab()?;
            bail!("Input device not grabbed.");
        };

        Ok(input_device.device.emit(events)?)
    }

    pub fn fetch_events(&mut self) -> anyhow::Result<FetchEventsSynced<'_>> {
        fetch_events(self.output_device.as_mut().expect("Output device is not opened"))
    }

    pub fn fetch_until_key(&mut self, key: Key) -> anyhow::Result<Vec<InputEvent>> {
        let start = Instant::now();

        let mut done = false;
        let mut result: Vec<InputEvent> = vec![];

        loop {
            if done {
                break;
            }

            if Instant::now().duration_since(start) > Duration::from_secs(1) {
                break;
            }

            let events = self.fetch_events()?;

            for event in events {
                if event.event_type() == EventType::KEY && event.code() == key.0 && event.value() == 0 {
                    done = true;
                }

                result.push(event);
            }
        }

        Ok(result)
    }

    pub fn fetch(&mut self) -> anyhow::Result<Vec<InputEvent>> {
        // Sync key
        self.emit_events(&vec![key_press(Key::KEY_MOVE), key_release(Key::KEY_MOVE)])?;

        // Fetch until sync key.
        Ok(self.fetch_until_key(Key::KEY_MOVE)?)
    }

    pub fn kill_for_output(&mut self) -> anyhow::Result<Output> {
        self.raw_kill()?;
        self.wait_for_output()
    }

    pub fn wait_for_output(&self) -> anyhow::Result<Output> {
        if self.nocapture {
            bail!("Can't get output when configured for nocapture.");
        }

        let child = self.child.take().expect("Output is already fetched.");

        self.wait_for_output_inner(child)
    }

    /// Expects xremap to stop by itself.
    fn wait_for_output_inner(&self, mut child: Child) -> anyhow::Result<Output> {
        println!("Waiting for xremap to exit");

        let is_stopped = until(
            || child.try_wait().is_ok_and(|val| !val.is_none()),
            Duration::from_secs(1),
            "Timed out waiting for xremap exit",
        );

        if is_stopped.is_err() {
            child.kill()?;
            println!("Xremap killed");
        };

        let res = child.wait_with_output()?;

        println!("Xremap stopped");

        let stdout = String::from_utf8(res.stdout)?;
        let stderr = String::from_utf8(res.stderr)?;

        if self.nocapture {
            // No output to print because of nocapture.
            assert_eq!("", stdout);
            assert_eq!("", stderr);
        } else {
            // To make debugging easier always print output.
            println!("{SEPARATOR}");
            println!("stdout:\n{stdout}");
            println!("{SEPARATOR}");
            println!("stderr:\n{stderr}");
            println!("{SEPARATOR}");
        }

        if !self.allow_stdio_errors {
            Self::check_stdio(&stdout)?;
            Self::check_stdio(&stderr)?;
        }

        match is_stopped {
            Ok(_) => Ok(Output { stdout, stderr }),
            Err(e) => Err(e),
        }
    }

    fn check_stdio(stdio: &str) -> anyhow::Result<()> {
        // Ignore an error from evdev that goes straight to stderr, open PR:https://github.com/emberian/evdev/pull/172
        let stdio = stdio.replace("Failed to ungrab device: No such device (os error 19)", "");
        let stdio = stdio.to_ascii_lowercase();
        if stdio.contains("fail")
            || stdio.contains("fatal")
            || stdio.contains("error")
            || stdio.contains("panic")
            || stdio.contains("warn")
        {
            bail!("Stdio contained an error message")
        }
        Ok(())
    }

    fn raw_kill(&self) -> anyhow::Result<()> {
        let mut child = self.child.take().expect("Output is already fetched.");

        let result = child.kill();

        println!("Xremap killed");

        self.child.set(Some(child));

        Ok(result?)
    }

    // Kill and ignore stdio
    pub fn kill(&self) -> anyhow::Result<()> {
        if let Some(mut child) = self.child.take() {
            if child.try_wait()?.is_none() {
                child.kill()?;
                println!("Xremap killed");
            } else {
                println!("Xremap already stopped when attempting to kill.");
            }

            let _ = self.wait_for_output_inner(child)?;
        } else {
            println!("Some sort of shutdown has already been requested.");
        }

        Ok(())
    }
}

// Ensures stdio is printed in case test cases fail unexpectedly.
// It also ensures xremap is stopped before the output_device
// is ungrabbed so events don't go to the 'host'.
impl Drop for XremapController {
    fn drop(&mut self) {
        println!("XremapController dropped");
        self.child.take().map(|child| {
            match self.wait_for_output_inner(child) {
                Ok(_) => {
                    // Has already been printed.
                }
                Err(err) => {
                    println!("While dropping xremap command: {err:?}");
                }
            }
        });
    }
}