enigo 0.6.1

Cross-platform (Linux, Windows, macOS & BSD) library to simulate keyboard and mouse events
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
use std::{
    convert::TryInto as _,
    env,
    num::Wrapping,
    os::{fd::AsFd, unix::net::UnixStream},
    path::PathBuf,
    time::Instant,
};

use log::{debug, error, trace, warn};
use wayland_client::{
    Connection, Dispatch, EventQueue, Proxy as _, QueueHandle, WEnum,
    protocol::{
        wl_keyboard::{self, WlKeyboard},
        wl_output::{self, Mode, WlOutput},
        wl_pointer::{self, WlPointer},
        wl_registry,
        wl_seat::{self, Capability},
    },
};
use wayland_protocols_misc::{
    zwp_input_method_v2::client::{zwp_input_method_manager_v2, zwp_input_method_v2},
    zwp_virtual_keyboard_v1::client::{zwp_virtual_keyboard_manager_v1, zwp_virtual_keyboard_v1},
};
use wayland_protocols_wlr::virtual_pointer::v1::client::{
    zwlr_virtual_pointer_manager_v1, zwlr_virtual_pointer_v1,
};
use xkbcommon::xkb;

use super::keymap2::Keymap2;
use crate::{
    Axis, Button, Coordinate, Direction, InputError, InputResult, Key, Keyboard, Mouse,
    NewConError, keycodes::ModifierBitflag,
};

pub type Keycode = u32;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
struct OutputInfo {
    width: i32,
    height: i32,
    transform: bool,
}

pub struct Con {
    event_queue: EventQueue<WaylandState>,
    state: WaylandState,
    base_time: std::time::Instant,
}

impl Con {
    /// Tries to establish a new Wayland connection
    ///
    /// # Errors
    /// TODO
    pub fn new(dpy_name: Option<&str>) -> Result<Self, NewConError> {
        // Setup Wayland connection
        let connection = Self::setup_connection(dpy_name)?;

        // Check to see if there was an error trying to connect
        if let Some(e) = connection.protocol_error() {
            error!(
                "unknown wayland initialization failure: {} {} {} {}",
                e.code, e.object_id, e.object_interface, e.message
            );
            return Err(NewConError::EstablishCon(
                "failed to connect to wayland. there was a protocol error",
            ));
        }

        let mut state = WaylandState::default();

        let mut event_queue = connection.new_event_queue();
        let qh = event_queue.handle();

        // Start registry
        let display = connection.display();
        let _ = display.get_registry(&qh, ()); // TODO: Check if we can drop the registry here

        // Receive the list of available globals
        event_queue
            .roundtrip(&mut state)
            .map_err(|_| NewConError::EstablishCon("Wayland roundtrip failed"))?;

        // Tell the compositor which globals the client wants to bind to
        event_queue
            .roundtrip(&mut state)
            .map_err(|_| NewConError::EstablishCon("Wayland roundtrip failed"))?;

        // Initialize the protocols (get the input_method, virtual_keyboard and
        // virtual_pointer)
        event_queue
            .roundtrip(&mut state)
            .map_err(|_| NewConError::EstablishCon("Wayland roundtrip failed"))?;

        // One extra, just to be sure
        event_queue
            .roundtrip(&mut state)
            .map_err(|_| NewConError::EstablishCon("Wayland roundtrip failed"))?;

        let mut connection = Self {
            event_queue,
            state,
            base_time: Instant::now(),
        };

        if connection.state.virtual_keyboard.is_some() {
            connection
                .update_keymap()
                .map_err(|_| NewConError::EstablishCon("Sending the initial keymap failed"))?;
        }

        connection.check_available_protocols()?;

        Ok(connection)
    }

    // Helper function for setting up the Wayland connection
    fn setup_connection(dyp_name: Option<&str>) -> Result<Connection, NewConError> {
        let connection = if let Some(dyp_name) = dyp_name {
            debug!("\x1b[93mtrying to establish a connection to: {dyp_name}\x1b[0m");
            let socket_path = env::var_os("XDG_RUNTIME_DIR").map(PathBuf::from).ok_or(
                NewConError::EstablishCon("Missing XDG_RUNTIME_DIR env variable"),
            )?;
            let stream = UnixStream::connect(socket_path.join(dyp_name))
                .map_err(|_| NewConError::EstablishCon("Failed to open Unix stream"))?;
            Connection::from_socket(stream)
        } else {
            debug!("\x1b[93mtrying to establish a connection to $WAYLAND_DISPLAY\x1b[0m");
            Connection::connect_to_env()
        };

        connection.map_err(|_| {
            error!("Failed to connect to Wayland. Try setting 'WAYLAND_DISPLAY=wayland-0'.");
            NewConError::EstablishCon("Wayland connection failed.")
        })
    }

    fn check_available_protocols(&self) -> Result<(), NewConError> {
        debug!(
            "protocols available\nvirtual_keyboard: {}\ninput_method: {}\nvirtual_pointer: {}",
            self.state.virtual_keyboard.is_some(),
            self.state.input_method.is_some(),
            self.state.virtual_pointer.is_some(),
        );

        if self.state.virtual_keyboard.is_none()
            && self.state.input_method.is_none()
            && self.state.virtual_pointer.is_none()
        {
            return Err(NewConError::EstablishCon(
                "no protocol available to simulate input",
            ));
        }
        Ok(())
    }

    /// Get the duration since the Keymap was created
    fn get_time(&self) -> u32 {
        let duration = self.base_time.elapsed();
        let time = duration.as_millis();
        time.try_into().unwrap_or(u32::MAX)
    }

    /// Press/Release a keycode
    ///
    /// # Errors
    /// TODO
    fn send_key_event(&mut self, keycode: Keycode, direction: Direction) -> InputResult<()> {
        trace!("send_key_event(&mut self, keycode: {keycode}, direction: {direction:?})");
        let vk = self
            .state
            .virtual_keyboard
            .as_ref()
            .ok_or(InputError::Simulate("no way to enter key"))?;
        is_alive(vk)?;

        let time = self.get_time();
        let keycode = keycode - 8; // Adjust by 8 due to the xkb/xwayland requirements
        let direction_wayland = match direction {
            Direction::Press => 1,
            Direction::Release => 0,
            Direction::Click => {
                return Err(InputError::Simulate(
                    "impossible direction, this should never be possible. This function must never be called with Direction::Click",
                ));
            }
        };

        vk.key(time, keycode, direction_wayland);

        self.flush()?;
        Ok(())
    }

    /// Sends a modifier event with the updated bitflag of the modifiers to the
    /// compositor
    fn send_modifier_event(
        &mut self,
        depressed_mods_new: ModifierBitflag,
        latched_mods_new: ModifierBitflag,
        locked_mods_new: ModifierBitflag,
        effective_layout_new: u32,
    ) -> InputResult<()> {
        // Retrieve virtual keyboard or return an error early if None
        let vk = self
            .state
            .virtual_keyboard
            .as_ref()
            .ok_or(InputError::Simulate("no way to enter key"))?;

        // Check if virtual keyboard is still alive
        is_alive(vk)?;

        // Log the modifier event
        trace!(
            "vk.modifiers({depressed_mods_new}, {latched_mods_new}, {locked_mods_new}, {effective_layout_new})"
        );

        // Send the modifier event
        vk.modifiers(
            depressed_mods_new,
            latched_mods_new,
            locked_mods_new,
            effective_layout_new,
        );

        self.flush()?;

        Ok(())
    }

    /// Apply the current keymap
    ///
    /// # Errors
    /// TODO
    fn update_keymap(&mut self) -> InputResult<()> {
        debug!("update_keymap(&mut self)");
        let vk = self
            .state
            .virtual_keyboard
            .as_ref()
            .ok_or(InputError::Simulate("no way to apply keymap"))?;
        is_alive(vk)?;

        let keymap = match &self.state.seat_keymap {
            Some(keymap) => keymap,
            None => &Keymap2::default()
                .map_err(|()| InputError::Mapping("could not update the keymap".to_string()))?,
        };

        let (format, keymap_file, size) = keymap
            .format_file_size()
            .map_err(|()| InputError::Mapping("could not update the keymap".to_string()))?;
        vk.keymap(format, keymap_file.as_fd(), size);

        debug!("wait for response after keymap call");
        self.event_queue
            .roundtrip(&mut self.state)
            .map_err(|_| InputError::Simulate("Wayland roundtrip failed"))?;

        Ok(())
    }

    /// Flush the Wayland queue
    fn flush(&self) -> InputResult<()> {
        self.event_queue.flush().map_err(|e| {
            error!("{e:?}");
            InputError::Simulate("could not flush Wayland queue")
        })?;
        trace!("flushed event queue");
        Ok(())
    }
}

impl Drop for Con {
    // Destroy the Wayland objects we created
    fn drop(&mut self) {
        if let Some(vk) = self.state.virtual_keyboard.take() {
            vk.destroy();
        }
        if let Some(im) = self.state.input_method.take() {
            im.destroy();
        }
        if let Some(vp) = self.state.virtual_pointer.take() {
            vp.destroy();
        }

        if self.flush().is_err() {
            error!("could not flush wayland queue");
        }
        trace!("wayland objects were destroyed");

        let _ = self.event_queue.roundtrip(&mut self.state);
    }
}

#[derive(Default)]
/// Stores the manager for the various protocols
struct WaylandState {
    // interface name, global id, version
    globals: Vec<(String, u32, u32)>,
    outputs: Vec<(WlOutput, OutputInfo)>,
    keyboard_manager: Option<zwp_virtual_keyboard_manager_v1::ZwpVirtualKeyboardManagerV1>,
    virtual_keyboard: Option<zwp_virtual_keyboard_v1::ZwpVirtualKeyboardV1>,
    im_manager: Option<zwp_input_method_manager_v2::ZwpInputMethodManagerV2>,
    input_method: Option<zwp_input_method_v2::ZwpInputMethodV2>,
    im_serial: Wrapping<u32>,
    pointer_manager: Option<zwlr_virtual_pointer_manager_v1::ZwlrVirtualPointerManagerV1>,
    virtual_pointer: Option<zwlr_virtual_pointer_v1::ZwlrVirtualPointerV1>,
    seat: Option<wl_seat::WlSeat>,
    seat_keyboard: Option<WlKeyboard>,
    seat_keymap: Option<Keymap2>,
    seat_pointer: Option<WlPointer>,
}

impl Dispatch<wl_registry::WlRegistry, ()> for WaylandState {
    #[allow(clippy::too_many_lines)]
    fn event(
        state: &mut Self,
        registry: &wl_registry::WlRegistry,
        event: wl_registry::Event,
        (): &(),
        _: &Connection,
        qh: &QueueHandle<Self>,
    ) {
        match event {
            // Store global to later bind to them
            wl_registry::Event::Global {
                name,
                interface,
                version,
            } => {
                debug!("Global announced: {interface} (name: {name}, version: {version})");
                match &interface[..] {
                    "wl_seat" => {
                        let seat = registry.bind::<wl_seat::WlSeat, _, _>(name, version, qh, ());
                        // We don't know if the seat or the im_manager is created first and we need
                        // both to get the input_method
                        if let Some(im_manager) = &state.im_manager {
                            if state.input_method.is_none() {
                                let input_method = im_manager.get_input_method(&seat, qh, ());
                                state.input_method = Some(input_method);
                                state.im_serial = Wrapping(0u32);
                            }
                        }
                        // We don't know if the seat or the keyboard_manager is created first and we
                        // need both to get the virtual_keyboard
                        if let Some(keyboard_manager) = &state.keyboard_manager {
                            if state.virtual_keyboard.is_none() {
                                let virtual_keyboard =
                                    keyboard_manager.create_virtual_keyboard(&seat, qh, ());
                                state.virtual_keyboard = Some(virtual_keyboard);
                            }
                        }
                        // We don't know if the seat or the pointer_manager is created first and we
                        // need both to get the virtual_pointer
                        if let Some(pointer_manager) = &state.pointer_manager {
                            if state.virtual_pointer.is_none() {
                                let virtual_pointer =
                                    pointer_manager.create_virtual_pointer(Some(&seat), qh, ());
                                state.virtual_pointer = Some(virtual_pointer);
                            }
                        }

                        state.seat = Some(seat);
                    }
                    "wl_output" => {
                        let wl_output =
                            registry.bind::<wl_output::WlOutput, _, _>(name, version, qh, ());
                        state.outputs.push((wl_output, OutputInfo::default()));
                    }
                    "zwp_input_method_manager_v2" => {
                        let im_manager = registry
                            .bind::<zwp_input_method_manager_v2::ZwpInputMethodManagerV2, _, _>(
                            name,
                            version,
                            qh,
                            (),
                        );
                        // We don't know if the seat or the im_manager is created first and we need
                        // both to get the input_method
                        if let Some(seat) = &state.seat {
                            if state.input_method.is_none() {
                                let input_method = im_manager.get_input_method(seat, qh, ());
                                state.input_method = Some(input_method);
                                state.im_serial = Wrapping(0u32);
                            }
                        }
                        state.im_manager = Some(im_manager);
                    }
                    "zwp_virtual_keyboard_manager_v1" => {
                        let keyboard_manager = registry
                        .bind::<zwp_virtual_keyboard_manager_v1::ZwpVirtualKeyboardManagerV1, _, _>(
                        name,
                        version,
                        qh,
                        (),
                    );
                        // We don't know if the seat or the keyboard_manager is created first and we
                        // need both to get the virtual_keyboard
                        if let Some(seat) = &state.seat {
                            if state.virtual_keyboard.is_none() {
                                let virtual_keyboard =
                                    keyboard_manager.create_virtual_keyboard(seat, qh, ());
                                state.virtual_keyboard = Some(virtual_keyboard);
                            }
                        }
                        state.keyboard_manager = Some(keyboard_manager);
                    }
                    "zwlr_virtual_pointer_manager_v1" => {
                        let pointer_manager = registry
                        .bind::<zwlr_virtual_pointer_manager_v1::ZwlrVirtualPointerManagerV1, _, _>(
                        name,
                        version,
                        qh,
                        (),
                    );
                        // We don't know if the seat or the pointer_manager is created first and we
                        // need both to get the virtual_pointer
                        if let Some(seat) = &state.seat {
                            if state.virtual_pointer.is_none() {
                                let virtual_pointer =
                                    pointer_manager.create_virtual_pointer(Some(seat), qh, ());
                                state.virtual_pointer = Some(virtual_pointer);
                            }
                        }
                        state.pointer_manager = Some(pointer_manager);
                    }
                    _ => {}
                }
                state.globals.push((interface, name, version));
            }
            // Remove global from store when it becomes unavailable
            wl_registry::Event::GlobalRemove { name } => {
                debug!("Global removed: {name}");
                let Some((idx, (interface, name, _))) = state
                    .globals
                    .iter()
                    .enumerate()
                    .find(|(_, (_, n, _))| *n == name)
                else {
                    return;
                };

                match &interface[..] {
                    "wl_seat" => {
                        state.virtual_keyboard = None;
                        state.keyboard_manager = None;
                        state.input_method = None;
                        state.im_manager = None;
                        state.virtual_pointer = None;
                        state.pointer_manager = None;
                        state.seat_keyboard = None;
                        state.seat_keymap = None;
                        state.seat_pointer = None;
                        state.seat = None;
                    }
                    "wl_output" => {
                        state
                            .outputs
                            .retain(|(output, _)| output.id().protocol_id() != *name);
                    }
                    "zwp_input_method_manager_v2" => {
                        state.input_method = None;
                        state.im_manager = None;
                    }
                    "zwp_virtual_keyboard_manager_v1" => {
                        state.virtual_keyboard = None;
                        state.keyboard_manager = None;
                    }
                    "zwlr_virtual_pointer_manager_v1" => {
                        state.pointer_manager = None;
                        state.virtual_pointer = None;
                    }
                    _ => {}
                }
                state.globals.remove(idx);
            }
            ev => warn!("WlRegistry received unknown event:\n{ev:?}"),
        }
    }
}

impl Dispatch<zwp_virtual_keyboard_manager_v1::ZwpVirtualKeyboardManagerV1, ()> for WaylandState {
    fn event(
        _state: &mut Self,
        _manager: &zwp_virtual_keyboard_manager_v1::ZwpVirtualKeyboardManagerV1,
        event: zwp_virtual_keyboard_manager_v1::Event,
        (): &(),
        _: &Connection,
        _qh: &QueueHandle<Self>,
    ) {
        warn!("ZwpVirtualKeyboardManagerV1 received unknown event:\n{event:?}");
    }
}

impl Dispatch<zwp_virtual_keyboard_v1::ZwpVirtualKeyboardV1, ()> for WaylandState {
    fn event(
        _state: &mut Self,
        _vk: &zwp_virtual_keyboard_v1::ZwpVirtualKeyboardV1,
        event: zwp_virtual_keyboard_v1::Event,
        (): &(),
        _: &Connection,
        _qh: &QueueHandle<Self>,
    ) {
        warn!("ZwpVirtualKeyboardV1 received unknown event:\n{event:?}");
    }
}

impl Dispatch<zwp_input_method_manager_v2::ZwpInputMethodManagerV2, ()> for WaylandState {
    fn event(
        _state: &mut Self,
        _manager: &zwp_input_method_manager_v2::ZwpInputMethodManagerV2,
        event: zwp_input_method_manager_v2::Event,
        (): &(),
        _: &Connection,
        _qh: &QueueHandle<Self>,
    ) {
        warn!("ZwpInputMethodManagerV2 received unknown event:\n{event:?}");
    }
}
impl Dispatch<zwp_input_method_v2::ZwpInputMethodV2, ()> for WaylandState {
    fn event(
        state: &mut Self,
        _vk: &zwp_input_method_v2::ZwpInputMethodV2,
        event: zwp_input_method_v2::Event,
        (): &(),
        _: &Connection,
        _qh: &QueueHandle<Self>,
    ) {
        match event {
            zwp_input_method_v2::Event::Done => {
                debug!("ZwpInputMethodV2 received event:\nzwp_input_method_v2::Event::Done");
                state.im_serial += Wrapping(1u32);
            }
            zwp_input_method_v2::Event::Activate
            | zwp_input_method_v2::Event::Deactivate
            | zwp_input_method_v2::Event::SurroundingText {
                text: _,
                cursor: _,
                anchor: _,
            }
            | zwp_input_method_v2::Event::TextChangeCause { cause: _ }
            | zwp_input_method_v2::Event::ContentType {
                hint: _,
                purpose: _,
            } => {
                trace!("ZwpInputMethodV2 received irrelevant event:\n{event:?}");
            }
            zwp_input_method_v2::Event::Unavailable => {
                warn!("ZwpInputMethodV2 received event:\nzwp_input_method_v2::Event::Unavailable");
                warn!(
                    "It is not possible to enter text anymore! The characters will now be simulated by individual key presses instead"
                );
                state.input_method = None;
                state.im_manager = None;
            }
            _ => warn!("ZwpInputMethodV2 received unknown event:\n{event:?}"),
        }
    }
}

impl Dispatch<wl_seat::WlSeat, ()> for WaylandState {
    fn event(
        state: &mut Self,
        seat: &wl_seat::WlSeat,
        event: wl_seat::Event,
        (): &(),
        _con: &Connection,
        qh: &QueueHandle<Self>,
    ) {
        match event {
            wl_seat::Event::Capabilities { capabilities } => {
                debug!("WlSeat received event:\n{event:?}");
                let wayland_client::WEnum::Value(capabilities) = capabilities else {
                    warn!("Unknown value for the capabilities of the wl_seat: {capabilities:?}");
                    return;
                };

                // Create a WlKeyboard if the seat has the capability
                if state.seat_keyboard.is_none() && capabilities.contains(Capability::Keyboard) {
                    let seat_keyboard = seat.get_keyboard(qh, ());
                    state.seat_keyboard = Some(seat_keyboard);
                }

                // Create a WlPointer if the seat has the capability
                if state.seat_pointer.is_none() && capabilities.contains(Capability::Pointer) {
                    let seat_pointer = seat.get_pointer(qh, ());
                    state.seat_pointer = Some(seat_pointer);
                }
            }
            wl_seat::Event::Name { name: _ } => {
                trace!("WlSeat received irrelevant event:\n{event:?}");
            }
            _ => warn!("WlSeat received unknown event:\n{event:?}"),
        }
    }
}

impl Dispatch<wl_keyboard::WlKeyboard, ()> for WaylandState {
    fn event(
        state: &mut Self,
        _seat: &wl_keyboard::WlKeyboard,
        event: wl_keyboard::Event,
        (): &(),
        _: &Connection,
        _qh: &QueueHandle<Self>,
    ) {
        match event {
            wl_keyboard::Event::Keymap { format, fd, size } => {
                debug!(
                    "WlKeyboard received event:\nwl_keyboard::Event::Keymap {{ {format:?}, {fd:?}, {size} }}"
                );

                // Get the received format
                let format = if let WEnum::Value(format) = format {
                    format as xkb::KeymapFormat
                } else {
                    error!("invalid format received! resetting the keymap");
                    state.seat_keymap = None;
                    return;
                };

                if let Some(keymap) = &mut state.seat_keymap {
                    if keymap.update(format, fd, size).is_err() {
                        state.seat_keymap = None;
                    }
                } else {
                    let context = xkb::Context::new(xkb::CONTEXT_NO_FLAGS);
                    state.seat_keymap = Keymap2::new_from_fd(context, format, fd, size).ok();
                }
            }
            wl_keyboard::Event::Modifiers {
                serial: _,
                mods_depressed: depressed_mods,
                mods_latched: latched_mods,
                mods_locked: locked_mods,
                group: depressed_layout,
            } => {
                if let Some(keymap) = &mut state.seat_keymap {
                    // Wayland doesn't differentiates between depressed, latched and locked
                    keymap.update_modifiers(
                        depressed_mods,
                        latched_mods,
                        locked_mods,
                        depressed_layout,
                        0,
                        0,
                    );
                    debug!("modifiers updated");
                }
            }
            // On Wayland the clients only get notified about pressed keys or modifiers if they have
            // the focus. We cannot assume that is the case, so the received events don't reflect
            // the full picture and we cannot use them to keep track of the state of the keyboard
            wl_keyboard::Event::Enter { .. }
            | wl_keyboard::Event::Leave { .. }
            | wl_keyboard::Event::Key { .. }
            | wl_keyboard::Event::RepeatInfo { .. } => {
                debug!("WlKeyboard received irrelevant event:\n{event:?}");
            }
            _ => warn!("WlKeyboard received unknown event:\n{event:?}"),
        }
    }
}

impl Dispatch<wl_pointer::WlPointer, ()> for WaylandState {
    fn event(
        _state: &mut Self,
        _seat: &wl_pointer::WlPointer,
        event: wl_pointer::Event,
        (): &(),
        _: &Connection,
        _qh: &QueueHandle<Self>,
    ) {
        warn!("WlPointer received unknown event:\n{event:?}");
    }
}

impl Dispatch<wl_output::WlOutput, ()> for WaylandState {
    fn event(
        state: &mut Self,
        output: &wl_output::WlOutput,
        event: wl_output::Event,
        (): &(),
        _: &Connection,
        _qh: &QueueHandle<Self>,
    ) {
        match event {
            wl_output::Event::Geometry { transform, .. } => {
                debug!("WlOutput received event:\n{event:?}");
                // The width and height need to get switched if the transform changes them
                // TODO: Check if this really is needed
                if transform == WEnum::Value(wl_output::Transform::_90)
                    || transform == WEnum::Value(wl_output::Transform::_270)
                    || transform == WEnum::Value(wl_output::Transform::Flipped90)
                    || transform == WEnum::Value(wl_output::Transform::Flipped270)
                {
                    if let Some((_, output_data)) =
                        state.outputs.iter_mut().find(|(o, _)| o == output)
                    {
                        output_data.transform = true;
                    }
                }
            }
            wl_output::Event::Mode {
                flags,
                width,
                height,
                refresh: _,
            } => {
                debug!("WlOutput received event:\n{event:?}");
                if flags == WEnum::Value(Mode::Current) {
                    if let Some((_, output_data)) =
                        state.outputs.iter_mut().find(|(o, _)| o == output)
                    {
                        output_data.width = width;
                        output_data.height = height;
                    }
                }
            }
            // TODO: Check if Scale is relevant
            wl_output::Event::Done
            | wl_output::Event::Scale { factor: _ }
            | wl_output::Event::Name { name: _ }
            | wl_output::Event::Description { description: _ } => {
                trace!("WlOutput received irrelevant event:\n{event:?}");
            }
            _ => warn!("WlOutput received unknown event:\n{event:?}"),
        }
    }
}

impl Dispatch<zwlr_virtual_pointer_manager_v1::ZwlrVirtualPointerManagerV1, ()> for WaylandState {
    fn event(
        _state: &mut Self,
        _manager: &zwlr_virtual_pointer_manager_v1::ZwlrVirtualPointerManagerV1,
        event: zwlr_virtual_pointer_manager_v1::Event,
        (): &(),
        _: &Connection,
        _qh: &QueueHandle<Self>,
    ) {
        warn!("ZwlrVirtualPointerManagerV1 received unknown event:\n{event:?}");
    }
}

impl Dispatch<zwlr_virtual_pointer_v1::ZwlrVirtualPointerV1, ()> for WaylandState {
    fn event(
        _state: &mut Self,
        _vk: &zwlr_virtual_pointer_v1::ZwlrVirtualPointerV1,
        event: zwlr_virtual_pointer_v1::Event,
        (): &(),
        _: &Connection,
        _qh: &QueueHandle<Self>,
    ) {
        warn!("ZwlrVirtualPointerV1 received unknown event:\n{event:?}");
    }
}

impl Drop for WaylandState {
    // Destroy the manager for the protocols we used
    fn drop(&mut self) {
        if let Some(im_mgr) = self.im_manager.as_ref() {
            im_mgr.destroy();
        }
        if let Some(pointer_mgr) = self.pointer_manager.as_ref() {
            pointer_mgr.destroy();
        }
    }
}

impl Keyboard for Con {
    fn fast_text(&mut self, text: &str) -> InputResult<Option<()>> {
        // Process all previous events so that the serial number is correct
        self.event_queue
            .roundtrip(&mut self.state)
            .map_err(|_| InputError::Simulate("The roundtrip on Wayland failed"))?;

        let Some(im) = self.state.input_method.as_mut() else {
            return Ok(None);
        };

        is_alive(im)?;
        trace!("fast text input with imput_method protocol");

        im.commit_string(text.to_string());
        im.commit(self.state.im_serial.0);

        self.flush()?;

        Ok(Some(()))
    }

    fn key(&mut self, key: Key, direction: Direction) -> InputResult<()> {
        let keymap = self
            .state
            .seat_keymap
            .as_mut()
            .ok_or(InputError::Simulate("no keymap available"))?;

        let keycode = if let Some(keycode) = keymap.key_to_keycode(key) {
            keycode
        } else {
            debug!("keycode for key {key:?} was not found");

            let mapping_res = keymap.map_key(key);
            let keycode = match mapping_res {
                Err(InputError::Mapping(_)) => {
                    // Unmap and retry
                    keymap.unmap_everything()?;
                    keymap.map_key(key)?
                }

                Ok(keycode) => keycode,
                _ => return Err(InputError::Mapping("unable to map the key".to_string())),
            };

            // Apply the new keymap if there were any changes
            self.update_keymap()?;
            keycode
        };
        self.raw(keycode, direction)
    }

    fn raw(&mut self, keycode: u16, direction: Direction) -> InputResult<()> {
        if direction == Direction::Click || direction == Direction::Press {
            // Update keymap state
            if let Some((
                depressed_mods_new,
                latched_mods_new,
                locked_mods_new,
                effective_layout_new,
            )) = self
                .state
                .seat_keymap
                .as_mut()
                .ok_or(InputError::Simulate("no keymap available"))?
                .update_key(xkb::Keycode::new(keycode.into()), xkb::KeyDirection::Down)
            {
                trace!("it is a modifier");
                self.send_modifier_event(
                    depressed_mods_new,
                    latched_mods_new,
                    locked_mods_new,
                    effective_layout_new,
                )?;
            } else {
                self.send_key_event(keycode.into(), Direction::Press)?;
            }
        }
        if direction == Direction::Click || direction == Direction::Release {
            // Update keymap state
            if let Some((
                depressed_mods_new,
                latched_mods_new,
                locked_mods_new,
                effective_layout_new,
            )) = self
                .state
                .seat_keymap
                .as_mut()
                .ok_or(InputError::Simulate("no keymap available"))?
                .update_key(xkb::Keycode::new(keycode.into()), xkb::KeyDirection::Up)
            {
                trace!("it is a modifier");
                self.send_modifier_event(
                    depressed_mods_new,
                    latched_mods_new,
                    locked_mods_new,
                    effective_layout_new,
                )?;
            } else {
                self.send_key_event(keycode.into(), Direction::Release)?;
            }
        }
        Ok(())
    }
}

impl Mouse for Con {
    fn button(&mut self, button: Button, direction: Direction) -> InputResult<()> {
        let vp = self
            .state
            .virtual_pointer
            .as_ref()
            .ok_or(InputError::Simulate("no way to enter button"))?;

        // Do nothing if one of the mouse scroll buttons was released
        // Releasing one of the scroll mouse buttons has no effect
        if direction == Direction::Release
            && matches!(
                button,
                Button::ScrollDown | Button::ScrollUp | Button::ScrollRight | Button::ScrollLeft
            )
        {
            return Ok(());
        }

        let button = match button {
            // Taken from /linux/input-event-codes.h
            Button::Left => 0x110,
            Button::Right => 0x111,
            Button::Back => 0x116,
            Button::Forward => 0x115,
            Button::Middle => 0x112,
            Button::ScrollDown => return self.scroll(1, Axis::Vertical),
            Button::ScrollUp => return self.scroll(-1, Axis::Vertical),
            Button::ScrollRight => return self.scroll(1, Axis::Horizontal),
            Button::ScrollLeft => return self.scroll(-1, Axis::Horizontal),
        };

        if direction == Direction::Press || direction == Direction::Click {
            let time = self.get_time();
            trace!("vp.button({time}, {button}, wl_pointer::ButtonState::Pressed)");
            vp.button(time, button, wl_pointer::ButtonState::Pressed);
            vp.frame(); // TODO: Check if this is needed
        }

        if direction == Direction::Release || direction == Direction::Click {
            let time = self.get_time();
            trace!("vp.button({time}, {button}, wl_pointer::ButtonState::Released)");
            vp.button(time, button, wl_pointer::ButtonState::Released);
            vp.frame(); // TODO: Check if this is needed
        }

        self.flush()
    }

    fn move_mouse(&mut self, x: i32, y: i32, coordinate: Coordinate) -> InputResult<()> {
        let vp = self
            .state
            .virtual_pointer
            .as_ref()
            .ok_or(InputError::Simulate("no way to move the mouse"))?;

        let time = self.get_time();
        match coordinate {
            Coordinate::Rel => {
                trace!("vp.motion({time}, {x}, {y})");
                vp.motion(time, x as f64, y as f64);
            }
            Coordinate::Abs => {
                let (x_extend, y_extend) = self.main_display()?;
                let x_extend: u32 = x_extend
                    .try_into()
                    .map_err(|_| InputError::InvalidInput("x_extend cannot be negative"))?;
                let y_extend: u32 = y_extend
                    .try_into()
                    .map_err(|_| InputError::InvalidInput("y_extend cannot be negative"))?;
                let x: u32 = x.try_into().map_err(|_| {
                    InputError::InvalidInput("the absolute coordinates cannot be negative")
                })?;
                let y: u32 = y.try_into().map_err(|_| {
                    InputError::InvalidInput("the absolute coordinates cannot be negative")
                })?;

                trace!("vp.motion_absolute({time}, {x}, {y}, {x_extend}, {y_extend})");
                vp.motion_absolute(time, x, y, x_extend, y_extend);
            }
        }
        vp.frame(); // TODO: Check if this is needed

        self.flush()
    }

    fn scroll(&mut self, length: i32, axis: Axis) -> InputResult<()> {
        let vp = self
            .state
            .virtual_pointer
            .as_ref()
            .ok_or(InputError::Simulate("no way to scroll"))?;

        // TODO: Check what the value of length should be
        // TODO: Check if it would be better to use .axis_discrete here
        let time = self.get_time();
        let axis = match axis {
            Axis::Horizontal => wl_pointer::Axis::HorizontalScroll,
            Axis::Vertical => wl_pointer::Axis::VerticalScroll,
        };
        trace!("vp.axis(time, axis, length.into())");
        vp.axis(time, axis, length.into());
        vp.frame(); // TODO: Check if this is needed

        self.flush()
    }

    fn main_display(&self) -> InputResult<(i32, i32)> {
        // TODO: The assumption here is that the output we store in the first position
        // is the main display. This likely can be wrong
        match self.state.outputs.first() {
            // Switch width and height if the output was transformed
            Some((_, output_info)) if output_info.transform => {
                Ok((output_info.height, output_info.width))
            }
            Some((_, output_info)) => Ok((output_info.width, output_info.height)),
            None => Err(InputError::Simulate("No screens available")),
        }
    }

    fn location(&self) -> InputResult<(i32, i32)> {
        // TODO Implement this
        error!(
            "You tried to get the mouse location. I don't know how this is possible under Wayland. Let me know if there is a new protocol"
        );
        Err(InputError::Simulate("Not implemented yet"))
    }
}

fn is_alive<P: wayland_client::Proxy>(proxy: &P) -> InputResult<()> {
    if proxy.is_alive() {
        Ok(())
    } else {
        Err(InputError::Simulate("wayland proxy is dead"))
    }
}