#include "state/wayland.h" #include #include #include #include "xdg-shell-client-protocol.h" #define UNUSED(x) (void)(x) WaylandState *WaylandState::GetInstance() { if (!WaylandState::s_instance) WaylandState::s_instance = new WaylandState(); return WaylandState::s_instance; } void WaylandState::wm_base_handle_ping(void *data, struct xdg_wm_base *wm_base, uint32_t serial) { UNUSED(data); printf("INFO: ping method fired! sending pong...\n"); xdg_wm_base_pong(wm_base, serial); } void WaylandState::registry_handle_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) { UNUSED(data); auto state = reinterpret_cast(data); // printf("HANDLE_GLOBAL: new interface '%s', name = %d, version = %d\n", // interface, name, version); std::string interface_name(interface); if (interface_name == "wl_compositor") { state->m_compositor = reinterpret_cast( wl_registry_bind(registry, name, &wl_compositor_interface, version)); printf("[DEBUG] global_binding(%s): compositor %p\n", interface, state->m_compositor); } if (interface_name == "xdg_wm_base") { state->m_wm_base = reinterpret_cast( wl_registry_bind(registry, name, &xdg_wm_base_interface, version)); printf("[DEBUG] global_binding(%s): xdg_wm_base %p\n", interface, state->m_wm_base); if (state->m_wm_base == NULL) { fprintf(stderr, "ERROR: could not bind to xdg_wm_base\n"); } state->m_wm_base_listener.ping = wm_base_handle_ping; xdg_wm_base_add_listener(state->m_wm_base, &state->m_wm_base_listener, NULL); } if (interface_name == "wl_seat") { state->m_seat = reinterpret_cast( wl_registry_bind(registry, name, &wl_seat_interface, version)); printf("[DEBUG] global_binding(%s): wl_seat %p\n", interface, state->m_seat); if (state->m_seat == NULL) { fprintf(stderr, "ERROR: could not bind to wl_seat\n"); } state->m_seat_listener.capabilities = seat_handle_capabilities; state->m_seat_listener.name = seat_handle_name; wl_seat_add_listener(state->m_seat, &state->m_seat_listener, state); } if (interface_name == "wl_shm") { state->m_shm = reinterpret_cast( wl_registry_bind(registry, name, &wl_shm_interface, version)); printf("[DEBUG] global_binding(%s): wl_shm %p\n", interface, state->m_shm); if (state->m_shm == NULL) { fprintf(stderr, "ERROR: could not bind to wl_shm\n"); } } } void WaylandState::seat_handle_name(void *data, struct wl_seat *seat, const char *name) { UNUSED(seat); UNUSED(data); printf("[DEBUG] SEAT: name of the seat = %s\n", name); } void WaylandState::seat_handle_capabilities(void *data, struct wl_seat *seat, uint32_t capabilities) { auto state = reinterpret_cast(data); UNUSED(state); UNUSED(seat); if ((capabilities & WL_SEAT_CAPABILITY_KEYBOARD) > 0) { printf("[DEBUG] SEAT: keyboard is available\n"); } if ((capabilities & WL_SEAT_CAPABILITY_POINTER) > 0) { printf("[DEBUG] SEAT: pointer is available\n"); state->m_pointer = wl_seat_get_pointer(seat); printf("[DEBUG] SEAT: pointer is bound under %p address\n", state->m_pointer); } if ((capabilities & WL_SEAT_CAPABILITY_TOUCH) > 0) { printf("[DEBUG] SEAT: touch is available\n"); } } void WaylandState::registry_handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) { UNUSED(data); UNUSED(registry); UNUSED(name); } WaylandState::WaylandState() { m_display = wl_display_connect(NULL); if (m_display == NULL) { fprintf(stderr, "ERROR: could not connect to wl display\n"); exit(1); } struct wl_registry *registry = wl_display_get_registry(m_display); if (registry == NULL) { fprintf(stderr, "ERROR: could not connect to wl registry\n"); exit(1); } m_reg_listener.global = registry_handle_global; m_reg_listener.global_remove = registry_handle_global_remove; auto handle = wl_registry_add_listener(registry, &m_reg_listener, this); printf("[DEBUG] wl_registry listener bound: %d\n", handle); wl_display_roundtrip(m_display); } WaylandState::~WaylandState() { wl_display_flush(m_display); wl_display_roundtrip(m_display); if (m_wm_base) xdg_wm_base_destroy(m_wm_base); if (m_compositor) wl_compositor_destroy(m_compositor); wl_display_disconnect(m_display); } WaylandState *WaylandState::s_instance = nullptr;