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
extern crate x11;

use std::ptr;
pub use x11::xlib;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum KeyCode {
    Esc,
    Num1, Num2, Num3, Num4, Num5, Num6, Num7, Num8, Num9, Num0,
    Minus,
    Equals,
    Q,
    W,
    E,
    R,
    T,
    Y,
    U,
    I,
    O,
    P,
    LeftBracket,
    RightBracket,
    A,
    S,
    D,
    F,
    G,
    H,
    J,
    K,
    L,
    Semicolon,
    Apostrophe,
    Backtick,
    Backslash,
    Z,
    X,
    C,
    V,
    B,
    N,
    M,
    Comma,
    Period,
    Slash,
    Enter,
    Space,
    ShiftL,
    ShiftR,
    ControlL,
    ControlR,
    AltL,
    AltR,
    SuperL,
    SuperR,
    CapsLock,
    Tab,
    Backspace,
    F1,
    F2,
    F3,
    F4,
    F5,
    F6,
    F7,
    F8,
    F9,
    F10,
    F11,
    F12,
    Print,
    ScrollLock,
    Pause,
    Insert,
    Delete,
    Home,
    End,
    PageUp,
    PageDown,
    LeftArrow,
    RightArrow,
    UpArrow,
    DownArrow,
    NumLock,
    KpEnter,
    KpAdd,
    KpSubtract,
    KpMultiply,
    KpDivide,
    Kp0,
    Kp1,
    Kp2,
    Kp3,
    Kp4,
    Kp5,
    Kp6,
    Kp7,
    Kp8,
    Kp9,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MouseButton {
    Left,
    Middle,
    Right,
    ScrollUp,
    ScrollDown,
    ScrollLeft,
    ScrollRight,
    ExtraButton1,
    ExtraButton2,
}


impl KeyCode {
    /// Returns the X11 keycode associated with the `KeyCode` enum.
    pub fn to_x11_keycode(&self) -> u8 {
        match self {
            KeyCode::Esc => 9,
            KeyCode::Num1 => 10,
            KeyCode::Num2 => 11,
            KeyCode::Num3 => 12,
            KeyCode::Num4 => 13,
            KeyCode::Num5 => 14,
            KeyCode::Num6 => 15,
            KeyCode::Num7 => 16,
            KeyCode::Num8 => 17,
            KeyCode::Num9 => 18,
            KeyCode::Num0 => 19,
            KeyCode::Minus => 20,
            KeyCode::Equals => 21,
            KeyCode::Q => 24,
            KeyCode::W => 25,
            KeyCode::E => 26,
            KeyCode::R => 27,
            KeyCode::T => 28,
            KeyCode::Y => 29,
            KeyCode::U => 30,
            KeyCode::I => 31,
            KeyCode::O => 32,
            KeyCode::P => 33,
            KeyCode::LeftBracket => 34,
            KeyCode::RightBracket => 35,
            KeyCode::A => 38,
            KeyCode::S => 39,
            KeyCode::D => 40,
            KeyCode::F => 41,
            KeyCode::G => 42,
            KeyCode::H => 43,
            KeyCode::J => 44,
            KeyCode::K => 45,
            KeyCode::L => 46,
            KeyCode::Semicolon => 47,
            KeyCode::Apostrophe => 48,
            KeyCode::Backtick => 49,
            KeyCode::Backslash => 51,
            KeyCode::Z => 52,
            KeyCode::X => 53,
            KeyCode::C => 54,
            KeyCode::V => 55,
            KeyCode::B => 56,
            KeyCode::N => 57,
            KeyCode::M => 58,
            KeyCode::Comma => 59,
            KeyCode::Period => 60,
            KeyCode::Slash => 61,
            KeyCode::Enter => 36,
            KeyCode::Space => 65,
            KeyCode::ShiftL => 50,
            KeyCode::ShiftR => 62,
            KeyCode::ControlL => 37,
            KeyCode::ControlR => 105,
            KeyCode::AltL => 64,
            KeyCode::AltR => 108,
            KeyCode::SuperL => 133,
            KeyCode::SuperR => 134,
            KeyCode::CapsLock => 66,
            KeyCode::Tab => 23,
            KeyCode::Backspace => 22,
            KeyCode::F1 => 67,
            KeyCode::F2 => 68,
            KeyCode::F3 => 69,
            KeyCode::F4 => 70,
            KeyCode::F5 => 71,
            KeyCode::F6 => 72,
            KeyCode::F7 => 73,
            KeyCode::F8 => 74,
            KeyCode::F9 => 75,
            KeyCode::F10 => 76,
            KeyCode::F11 => 95,
            KeyCode::F12 => 96,
            KeyCode::Print => 107,
            KeyCode::ScrollLock => 78,
            KeyCode::Pause => 127,
            KeyCode::Insert => 118,
            KeyCode::Delete => 119,
            KeyCode::Home => 110,
            KeyCode::End => 115,
            KeyCode::PageUp => 112,
            KeyCode::PageDown => 117,
            KeyCode::LeftArrow => 113,
            KeyCode::RightArrow => 114,
            KeyCode::UpArrow => 111,
            KeyCode::DownArrow => 116,
            KeyCode::NumLock => 77,
            KeyCode::KpEnter => 104,
            KeyCode::KpAdd => 86,
            KeyCode::KpSubtract => 82,
            KeyCode::KpMultiply => 63,
            KeyCode::KpDivide => 106,
            KeyCode::Kp0 => 90,
            KeyCode::Kp1 => 87,
            KeyCode::Kp2 => 88,
            KeyCode::Kp3 => 89,
            KeyCode::Kp4 => 83,
            KeyCode::Kp5 => 84,
            KeyCode::Kp6 => 85,
            KeyCode::Kp7 => 79,
            KeyCode::Kp8 => 80,
            KeyCode::Kp9 => 81,
        }
    }
}
impl MouseButton {
    /// Returns the X11 button code associated with the `MouseButton` enum.
    pub fn to_x11_buttoncode(&self) -> u8 {
        match self {
            MouseButton::Left => 1,
            MouseButton::Middle => 2,
            MouseButton::Right => 3,
            MouseButton::ScrollUp => 4,
            MouseButton::ScrollDown => 5,
            MouseButton::ScrollLeft => 6,
            MouseButton::ScrollRight => 7,
            MouseButton::ExtraButton1 => 8,
            MouseButton::ExtraButton2 => 9,
        }
    }
}

pub fn getkey(kc: KeyCode) -> bool {
    unsafe {
        // Open the display
        let display = xlib::XOpenDisplay(ptr::null());
        if display.is_null() {
            eprintln!("Unable to open X display");
            return false;
        }
        // Allocate a 32-byte array to hold key states
        let mut keys: [u8; 32] = [0; 32];

        // Get the current key states
        xlib::XQueryKeymap(display, keys.as_mut_ptr() as *mut i8);

        // Get the min and max keycode for the keyboard
        let mut min_keycode: i32 = 0;
        let mut max_keycode: i32 = 0;
        xlib::XDisplayKeycodes(display, &mut min_keycode, &mut max_keycode);

        // Create a vector to store pressed keycodes
        let mut pressed_keys: Vec<u8> = Vec::new();

        // Check which keys are pressed
        for keycode in min_keycode..=max_keycode {
            let byte_index = (keycode / 8) as usize;
            let bit_index = (keycode % 8) as usize;

            if (keys[byte_index] & (1 << bit_index)) != 0 {
                pressed_keys.push(keycode as u8);
            }
        }
        // Close the display
        xlib::XCloseDisplay(display);
        pressed_keys.contains(&kc.to_x11_keycode())
    }
}

pub fn getmousebutton(button: MouseButton) -> bool {
    unsafe {
        // Open the display
        let display = xlib::XOpenDisplay(ptr::null());
        if display.is_null() {
            eprintln!("Unable to open X display");
            return false;
        }

        // Get the root window
        let root = xlib::XDefaultRootWindow(display);

        // Variables to hold the pointer position and mask state
        let mut root_return: xlib::Window = 0;
        let mut child_return: xlib::Window = 0;
        let mut root_x_return: i32 = 0;
        let mut root_y_return: i32 = 0;
        let mut win_x_return: i32 = 0;
        let mut win_y_return: i32 = 0;
        let mut mask_return: u32 = 0;

        // Query the pointer
        let status = xlib::XQueryPointer(
            display,
            root,
            &mut root_return,
            &mut child_return,
            &mut root_x_return,
            &mut root_y_return,
            &mut win_x_return,
            &mut win_y_return,
            &mut mask_return,
        );

        // Check if the query was successful
        if status == 0 {
            eprintln!("XQueryPointer failed");
            xlib::XCloseDisplay(display);
            return false;
        }

        // Create a vector to store pressed button codes
        let mut pressed_buttons: Vec<u8> = Vec::new();
        if mask_return & xlib::Button1Mask != 0 {
            pressed_buttons.push(1);
        }
        if mask_return & xlib::Button2Mask != 0 {
            pressed_buttons.push(2);
        }
        if mask_return & xlib::Button3Mask != 0 {
            pressed_buttons.push(3);
        }
        if mask_return & xlib::Button4Mask != 0 {
            pressed_buttons.push(4);
        }
        if mask_return & xlib::Button5Mask != 0 {
            pressed_buttons.push(5);
        }
        if mask_return & xlib::Button5Mask != 0 {
            pressed_buttons.push(6);
        }
        if mask_return & (1 << 8) != 0 { // Button 6
            pressed_buttons.push(6);
        }
        if mask_return & (1 << 9) != 0 { // Button 7
            pressed_buttons.push(7);
        }
        if mask_return & (1 << 10) != 0 { // Button 8
            pressed_buttons.push(8);
        }
        if mask_return & (1 << 11) != 0 { // Button 9
            pressed_buttons.push(9);
        }

        // Close the display
        xlib::XCloseDisplay(display);
        pressed_buttons.contains(&button.to_x11_buttoncode())
    }
}