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
/* Copyright (C) 2019 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 std::collections::HashMap;
use std::ffi::CString;
use std::os::raw::{c_int, c_uchar, c_void};
use std::process::Command;
use std::ptr::null;
use std::{thread, time};
use x11::xlib::*;
use x11::xtest::*;

use crate::module::unicode::UnicodeOutput;

const KEY_DELAY_US: u64 = 60000;

pub struct XConnection {
    display: *mut x11::xlib::_XDisplay,
    charmap: HashMap<char, u32>,
    held: Vec<char>,
}

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();
            XConnection {
                display,
                charmap,
                held,
            }
        }
    }

    #[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);
        }
    }

    pub fn press_key(&self, keycode: u32, state: bool, time: x11::xlib::Time) {
        unsafe {
            XTestFakeKeyEvent(self.display, keycode, state as i32, time);
        }
    }

    pub fn map_sym(&mut self, c: char) -> Option<u32> {
        let keysym = 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);
        }
        info!("Unbinding all keys");
        for keycode in self.charmap.values() {
            self.unbind_key(*keycode);
        }
        unsafe {
            XCloseDisplay(self.display);
        }
    }
}

impl UnicodeOutput for XConnection {
    fn get_layout(&self) -> String {
        // 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("");
        layout.to_string()
    }

    fn set_layout(&self, layout: &str) {
        Command::new("setxkbmap").args(&[layout]).output().unwrap();
    }

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

        for c in string.chars() {
            if c == '\0' {
                continue;
            }
            if let Some(keycode) = self.get_sym(c) {
                info!("Type {} => {:x?}", keycode, c);
                keycodes.push(keycode);
            }
        }

        let time = x11::xlib::CurrentTime;
        for k in keycodes.iter() {
            self.press_key(*k, true, time);
            thread::sleep(time::Duration::from_micros(KEY_DELAY_US));
            self.press_key(*k, false, time);
            //thread::sleep(time::Duration::from_micros(KEY_DELAY_US));
        }

        unsafe {
            XFlush(self.display);
        }

        for c in string.chars() {
            self.unmap_sym(c);
        }
    }

    fn press_symbol(&mut self, c: char, press: bool) {
        if c == '\0' {
            return;
        }
        if let Some(keycode) = self.get_sym(c) {
            println!("Set {:?} ({}) = {}", c, keycode, press);
            self.press_key(keycode, press, x11::xlib::CurrentTime);

            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);
            }
        }
    }

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

    fn set_held(&mut self, string: &str) {
        let s: Vec<char> = string.chars().collect();
        for c in &self.held.clone() {
            if !s.contains(c) {
                self.press_symbol(*c, false);
            }
        }
        for c in &s {
            self.press_symbol(*c, true);
        }
    }
}