# WaylandEventWatcher
[WaylandEventWatcher](https://source.chromium.org/chromium/chromium/src/+/main:ui/events/platform/wayland/wayland_event_watcher.h;l=36;drc=26f9f647eaadfd89a11c757cad807a51a1d66644) watches Wayland file descriptor and get notified when they can read events.
WaylandEventWatchers dispatches an event and trigger input objects such as WaylandPointer or WaylandKeyboard.
Its implementation is whether [WaylndEventWatcherGlib](https://source.chromium.org/chromium/chromium/src/+/main:ui/events/platform/wayland/wayland_event_watcher_glib.h;drc=26f9f647eaadfd89a11c757cad807a51a1d66644) or [WaylandEventWatcherFdWatch](https://source.chromium.org/chromium/chromium/src/+/main:ui/events/platform/wayland/wayland_event_watcher_fdwatch.h) depending on the platform.
If `use_glib` is set to true, it uses WaylandEventWatcherGlib. Otherwise, WaylandEventWatcherFdWatch is used.
## How does it work
Let's see WaylandEventWatcherFdWatch version.
WaylandEventWatcher receives `wl_display` and `wl_event_queue` on the construction.
When WaylandConnection is established, it [StartProcessingEvents](https://source.chromium.org/chromium/chromium/src/+/main:remoting/host/linux/wayland_connection.cc;l=25;drc=eef4762779d05708d5dfc7d5fe4ea16288069a35).
Then [StartWatchingFD](https://source.chromium.org/chromium/chromium/src/+/main:ui/events/platform/wayland/wayland_event_watcher_fdwatch.cc;l=21;drc=eef4762779d05708d5dfc7d5fe4ea16288069a35).
Before starting reading FD, [WlDisplyPrepareToRead](https://source.chromium.org/chromium/chromium/src/+/main:ui/events/platform/wayland/wayland_event_watcher.cc;l=141;drc=eef4762779d05708d5dfc7d5fe4ea16288069a35) is called and make sure to prepare.
`wl_display_repare_reqe_queue` checks the event queue event list to be empty and increment the `reader_count` of display to memorize the added reader.
It `wl_display_flush` to make sure the event is flushed, then it becomes ready.
WatchFileDescriptor is triggered t this point to keep receiving events.
## RoundTripQueue
[RoundTripQueue](https://source.chromium.org/chromium/chromium/src/+/main:ui/events/platform/wayland/wayland_event_watcher.cc;l=115;drc=eef4762779d05708d5dfc7d5fe4ea16288069a35) is used to block the UI thread until all pending requests are processed by the server.
This calls [`wl_display_roundtrip_queue`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/wayland/src/src/wayland-client.c;l=1351;drc=eef4762779d05708d5dfc7d5fe4ea16288069a35) protocol.
[WaylandConnection::RountTripQueue](https://source.chromium.org/chromium/chromium/src/+/main:ui/ozone/platform/wayland/host/wayland_connection.cc;l=255;drc=eef4762779d05708d5dfc7d5fe4ea16288069a35) eventually comes to WaylandEventWatcher.
This is called, for example, by [WaylandConnection::Initialize](https://source.chromium.org/chromium/chromium/src/+/main:ui/ozone/platform/wayland/host/wayland_connection.cc;l=227;drc=eef4762779d05708d5dfc7d5fe4ea16288069a35).
By blocking threads using RoundTripQueue, it waits until output_manager becomes ready.