Files
addr2line
adler
aho_corasick
ansi_term
arraydeque
as_slice
atty
backtrace
base64
bincode_core
bitflags
byteorder
bytes
capnp
capnp_futures
capnp_rpc
cfg_if
chrono
clap
ctrlc
derivative
dlib
downcast_rs
enumflags2
enumflags2_derive
evdev_rs
evdev_sys
failure
failure_derive
flexi_logger
futures
futures_channel
futures_core
futures_executor
futures_io
futures_macro
futures_sink
futures_task
futures_util
async_await
future
io
lock
sink
stream
task
generic_array
getrandom
gimli
glob
hash32
heapless
hid_io_core
hid_io_protocol
hidapi
install_service
lazy_static
libc
libloading
libudev_sys
log
memchr
memmap
miniz_oxide
mio
nanoid
nix
num_cpus
num_enum
num_enum_derive
num_integer
num_traits
object
once_cell
open
pem
pin_project_lite
pin_utils
ppv_lite86
proc_macro2
proc_macro_hack
proc_macro_nested
quote
rand
rand_chacha
rand_core
rcgen
regex
regex_syntax
remove_dir_all
ring
rustc_demangle
rustls
scoped_tls
sct
serde
serde_derive
slab
smallvec
spin
stable_deref_trait
strsim
syn
synstructure
sys_info
tempfile
textwrap
thiserror
thiserror_impl
time
tokio
future
io
loom
macros
net
park
runtime
stream
sync
task
time
util
tokio_macros
tokio_rustls
tokio_util
typenum
udev
uhid_virt
uhidrs_sys
unicode_width
unicode_xid
untrusted
vec_map
wayland_client
wayland_commons
wayland_sys
webpki
which
x11
xcb
xkbcommon
yansi
yasna
zwp_virtual_keyboard
  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
/* Copyright (C) 2019-2020 by Jacob Alexander
 * Copyright (C) 2019 by Rowan Decker
 *
 * This file is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 */

use crate::module::displayserver::{DisplayOutput, DisplayOutputError};
use std::collections::HashMap;
use std::convert::TryInto;
use std::ffi::CString;
use std::os::raw::{c_int, c_uchar, c_void};
use std::process::Command;
use std::ptr::null;
use x11::xlib::*;
use x11::xtest::*;

// XXX (HaaTa): Not sure why we need an additional 50 ms for the sequence to stick and not
// to get overridden by the next sequence.
const KEY_SEQUENCE_END_DELAY_MS: u64 = 50;

pub struct XConnection {
    display: *mut x11::xlib::_XDisplay,
    charmap: HashMap<char, u32>,
    held: Vec<char>,
    last_event_before_delays: std::time::Instant, // Last instance event, only updated when enough time has passed to decrement pending delays
    pending_delays: i64,                          // Number of 1ms delays pending
}

impl Default for XConnection {
    fn default() -> Self {
        Self::new()
    }
}

impl XConnection {
    pub fn new() -> XConnection {
        unsafe {
            let display = XOpenDisplay(null());
            let charmap = HashMap::new();
            let held = Vec::new();
            let last_event_before_delays = std::time::Instant::now();
            let pending_delays = 0;
            XConnection {
                display,
                charmap,
                held,
                last_event_before_delays,
                pending_delays,
            }
        }
    }

    #[link(name = "X11")]
    pub fn find_keycode(&self, keysym: u64) -> (bool, Option<u32>) {
        let display = self.display;
        let mut keycode = None;
        let mut empty = false;

        unsafe {
            let mut keycode_low: c_int = 0;
            let mut keycode_high: c_int = 0;
            XDisplayKeycodes(display, &mut keycode_low, &mut keycode_high);

            let mut keysyms_per_keycode: c_int = 0;
            let keysyms = XGetKeyboardMapping(
                display,
                keycode_low as c_uchar,
                keycode_high - keycode_low,
                &mut keysyms_per_keycode,
            );

            for i in keycode_low..keycode_high {
                empty = true;
                for j in 0..keysyms_per_keycode {
                    let symindex = (i - keycode_low) * keysyms_per_keycode + j;
                    let s = *keysyms.offset(symindex as isize);

                    let c = XKeysymToString(s);
                    if c.as_ref().is_some() {
                        let v = std::ffi::CStr::from_ptr(c);
                        trace!("sym[{},{}] = {} ({:?})", i, j, s, v.to_str().unwrap_or(""));
                    } else {
                        trace!("sym[{},{}] = {}", i, j, s);
                    }

                    if s == keysym {
                        empty = false;
                        keycode = Some(i as u32);
                        break;
                    }
                    if s != 0 {
                        empty = false;
                        break;
                    }
                }

                if empty {
                    keycode = Some(i as u32);
                    break;
                }
            }

            XFree(keysyms as *mut c_void);
        }

        (empty, keycode)
    }

    pub fn lookup_sym(symbol: char) -> u64 {
        let hex: u32 = symbol.into();
        let s = format!("U{:x}", hex);
        let xs = CString::new(s).unwrap();
        unsafe { XStringToKeysym(xs.as_ptr()) }
    }

    pub fn bind_key(&self, keycode: u32, keysym: u64) {
        unsafe {
            // https://stackoverflow.com/a/44334103
            let mut keysyms = [keysym, keysym];
            XChangeKeyboardMapping(
                self.display,
                keycode as i32,
                keysyms.len() as i32,
                keysyms.as_mut_ptr(),
                1,
            );
            XSync(self.display, false as i32);
        }
    }

    pub fn unbind_key(&self, keycode: u32) {
        unsafe {
            let mut no_sym: u64 = 0;
            XChangeKeyboardMapping(self.display, keycode as i32, 1, &mut no_sym, 1);
        }
    }

    fn update_pending_delays(&mut self) {
        // Update pending delay (if enough time has pass, can be set to zero)
        let elapsed_ms: i64 = self
            .last_event_before_delays
            .elapsed()
            .as_millis()
            .try_into()
            .unwrap();
        if elapsed_ms > self.pending_delays {
            self.pending_delays = 0; // Safe to send the event immediately
        } else {
            self.pending_delays -= elapsed_ms as i64 - 1; // Add a 1ms delay
        }
    }

    /// Press/Release a specific keycode
    /// NOTE: This call does not update the pending delays and must be updated prior
    /// (unlike press_release_key())
    pub fn press_key(&mut self, keycode: u32, press: bool) {
        unsafe {
            XTestFakeKeyEvent(
                self.display,
                keycode,
                press as i32,
                self.pending_delays as u64,
            );
        }
    }

    /// Press then release a specific keycode
    /// Faster than individual calls to press_key as you don't need a delay between press and
    /// release of the same keycode.
    /// This function will automatically add delays as necessary using previously calculated
    /// delays.
    pub fn press_release_key(&mut self, keycode: u32) {
        self.update_pending_delays();

        unsafe {
            XTestFakeKeyEvent(
                self.display,
                keycode,
                true as i32,
                self.pending_delays as u64,
            );
            XTestFakeKeyEvent(
                self.display,
                keycode,
                false as i32,
                self.pending_delays as u64,
            );
        }
    }

    pub fn map_sym(&mut self, c: char) -> Option<u32> {
        // Special character lookup, otherwise normal lookup
        let keysym = match c {
            '\n' => x11::keysym::XK_Return as u64,
            '\t' => x11::keysym::XK_Tab as u64,
            _ => XConnection::lookup_sym(c),
        };
        let (unmapped, keycode) = self.find_keycode(keysym);
        if let Some(keycode) = keycode {
            if unmapped {
                self.bind_key(keycode, keysym);
                self.charmap.insert(c, keycode);
            }
            Some(keycode)
        } else {
            warn!("Could not find available keycode");
            None
        }
    }

    pub fn unmap_sym(&mut self, c: char) {
        if let Some(keycode) = self.charmap.get(&c) {
            self.unbind_key(*keycode);
            self.charmap.remove(&c);
        }
    }

    pub fn get_sym(&mut self, c: char) -> Option<u32> {
        if let Some(keycode) = self.charmap.get(&c) {
            Some(*keycode)
        } else {
            self.map_sym(c)
        }
    }
}

impl Drop for XConnection {
    fn drop(&mut self) {
        info!("Releasing all keys");
        for c in &self.held.clone() {
            self.press_symbol(*c, false).unwrap();
        }
        info!("Unbinding all keys");
        for keycode in self.charmap.values() {
            self.unbind_key(*keycode);
        }
        unsafe {
            XCloseDisplay(self.display);
        }
    }
}

impl DisplayOutput for XConnection {
    fn get_layout(&self) -> Result<String, DisplayOutputError> {
        // TODO: Better solution. https://unix.stackexchange.com/a/422493

        let result = Command::new("setxkbmap")
            .args(&["-query"])
            .output()
            .expect("Failed to exec setxkbmap");
        let output = String::from_utf8_lossy(&result.stdout);
        let mut map = output
            .lines()
            .map(|l| l.split(':'))
            .map(|mut kv| (kv.next().unwrap_or(""), kv.next().unwrap_or("")));
        let layout = map
            .find(|(k, _): &(&str, &str)| *k == "layout")
            .map(|(_, v)| v.trim())
            .unwrap_or("");
        Ok(layout.to_string())
    }

    fn set_layout(&self, layout: &str) -> Result<(), DisplayOutputError> {
        Command::new("setxkbmap").args(&[layout]).output().unwrap();
        Ok(())
    }

    fn type_string(&mut self, string: &str) -> Result<(), DisplayOutputError> {
        let mut keycodes = Vec::with_capacity(string.len());

        // Make sure we have a keysym for every key
        for c in string.chars() {
            if c == '\0' {
                continue;
            }
            if let Some(keycode) = self.get_sym(c) {
                debug!("Type {} => {:x?}", keycode, c);
                keycodes.push(keycode);
            } else {
                error!("Could not allocate a keysym for unicode '{}'", c);
                return Err(DisplayOutputError::AllocationFailed(c));
            }
        }

        // Send keypresses in chunks
        // We have to increase the delay for each chunk (1ms intervals) as we can queue up events
        // much quicker than 1ms.
        // e.g. The tall moose
        // Instance 1   Press: 'The tall' # Stops at the 2nd space
        // Instance 1 Release: 'The tall'
        // Instance 2   Press: ' mo'      # Double o so we have to stop again
        // Instance 2 Release: ' mo'
        // Instance 3   Press: 'ose'
        // Instance 3 Release: 'ose'
        let mut keysym_queue = vec![];
        for k in keycodes.iter() {
            if !keysym_queue.contains(k) {
                keysym_queue.push(*k)
            } else {
                // Press/Release
                for qk in keysym_queue {
                    self.press_release_key(qk);
                }

                // Clear queue and insert the keysym we weren't able to add
                keysym_queue = vec![*k];
            }
        }

        // Handle remaining queue
        // Press/Release
        for qk in keysym_queue {
            self.press_release_key(qk);
        }

        unsafe {
            XFlush(self.display);
        }

        // Cleanup any symbols we had to map (just in case we need to remap new symbols another
        // time)
        for c in string.chars() {
            self.unmap_sym(c);
        }

        // Make sure to sleep the length of the delay to make sure we don't call this API again too
        // quickly and interfere with these keypresses.
        std::thread::sleep(std::time::Duration::from_millis(
            KEY_SEQUENCE_END_DELAY_MS + self.pending_delays as u64,
        ));

        Ok(())
    }

    fn press_symbol(&mut self, c: char, press: bool) -> Result<(), DisplayOutputError> {
        // Nothing to do
        if c == '\0' {
            return Ok(());
        }
        if let Some(keycode) = self.get_sym(c) {
            debug!("Set {:?} ({}) = {}", c, keycode, press);
            self.press_key(keycode, press);

            if press {
                self.held.push(c);
            } else {
                self.unmap_sym(c);
                self.held
                    .iter()
                    .position(|&x| x == c)
                    .map(|e| self.held.remove(e));
            }
            unsafe {
                XFlush(self.display);
            }
        }

        Ok(())
    }

    fn get_held(&mut self) -> Result<Vec<char>, DisplayOutputError> {
        Ok(self.held.clone())
    }

    fn set_held(&mut self, string: &str) -> Result<(), DisplayOutputError> {
        let s: Vec<char> = string.chars().collect();

        // This is a single instance, so update the pending delays first
        self.update_pending_delays();

        for c in &self.held.clone() {
            if !s.contains(c) {
                self.press_symbol(*c, false)?;
            }
        }
        for c in &s {
            self.press_symbol(*c, true)?;
        }

        // Make sure to sleep the length of the delay to make sure we don't call this API again too
        // quickly and interfere with these keypresses.
        std::thread::sleep(std::time::Duration::from_millis(
            KEY_SEQUENCE_END_DELAY_MS + self.pending_delays as u64,
        ));

        Ok(())
    }
}