Wayland refeactoring.

This commit is contained in:
Bartosz Taudul 2024-01-30 18:13:28 +01:00
parent 1354205db8
commit a28c115c5c
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3
14 changed files with 1209 additions and 753 deletions

View File

@ -10,7 +10,7 @@ IMAGE := $(PROJECT)-$(BUILD)
FILTER := ../../../nfd/nfd_win.cpp ../../src/BackendGlfw.cpp ../../src/imgui/imgui_impl_glfw.cpp
include ../../../common/src-from-vcxproj.mk
SRC += ../../src/BackendWayland.cpp
SRC += ../../src/BackendWayland.cpp ../../src/WaylandDisplay.cpp ../../src/WaylandPointer.cpp ../../src/WaylandKeyboard.cpp ../../src/WaylandWindow.cpp ../../src/WaylandOutput.cpp
SRC2 += ../../src/wayland/xdg-shell.c ../../src/wayland/xdg-activation.c ../../src/wayland/xdg-decoration.c
ifdef TRACY_NO_FILESELECTOR

View File

@ -1,708 +1,50 @@
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <EGL/egl.h> // must be here to avoid redefinition of khronos_int64_t
#include "imgui/imgui_impl_opengl3.h"
#include "imgui/imgui_impl_opengl3_loader.h"
#include <chrono>
#include <linux/input-event-codes.h>
#include <functional>
#include <memory>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <unordered_map>
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-compose.h>
#include <wayland-client.h>
#include <wayland-cursor.h>
#include <wayland-egl.h>
#include "wayland/xdg-activation.h"
#include "wayland/xdg-decoration.h"
#include "wayland/xdg-shell.h"
#include "../../server/TracyImGui.hpp"
#include "Backend.hpp"
#include "RunQueue.hpp"
#include "WaylandDisplay.hpp"
#include "WaylandWindow.hpp"
constexpr ImGuiKey s_keyTable[] = {
/* 0 */ ImGuiKey_None,
/* 1 */ ImGuiKey_Escape,
/* 2 */ ImGuiKey_1,
/* 3 */ ImGuiKey_2,
/* 4 */ ImGuiKey_3,
/* 5 */ ImGuiKey_4,
/* 6 */ ImGuiKey_5,
/* 7 */ ImGuiKey_6,
/* 8 */ ImGuiKey_7,
/* 9 */ ImGuiKey_8,
/* 10 */ ImGuiKey_9,
/* 11 */ ImGuiKey_0,
/* 12 */ ImGuiKey_Minus,
/* 13 */ ImGuiKey_Equal,
/* 14 */ ImGuiKey_Backspace,
/* 15 */ ImGuiKey_Tab,
/* 16 */ ImGuiKey_Q,
/* 17 */ ImGuiKey_W,
/* 18 */ ImGuiKey_E,
/* 19 */ ImGuiKey_R,
/* 20 */ ImGuiKey_T,
/* 21 */ ImGuiKey_Y,
/* 22 */ ImGuiKey_U,
/* 23 */ ImGuiKey_I,
/* 24 */ ImGuiKey_O,
/* 25 */ ImGuiKey_P,
/* 26 */ ImGuiKey_LeftBracket,
/* 27 */ ImGuiKey_RightBracket,
/* 28 */ ImGuiKey_Enter,
/* 29 */ ImGuiKey_LeftCtrl,
/* 30 */ ImGuiKey_A,
/* 31 */ ImGuiKey_S,
/* 32 */ ImGuiKey_D,
/* 33 */ ImGuiKey_F,
/* 34 */ ImGuiKey_G,
/* 35 */ ImGuiKey_H,
/* 36 */ ImGuiKey_J,
/* 37 */ ImGuiKey_K,
/* 38 */ ImGuiKey_L,
/* 39 */ ImGuiKey_Semicolon,
/* 40 */ ImGuiKey_Apostrophe,
/* 41 */ ImGuiKey_GraveAccent,
/* 42 */ ImGuiKey_LeftShift,
/* 43 */ ImGuiKey_Backslash,
/* 44 */ ImGuiKey_Z,
/* 45 */ ImGuiKey_X,
/* 46 */ ImGuiKey_C,
/* 47 */ ImGuiKey_V,
/* 48 */ ImGuiKey_B,
/* 49 */ ImGuiKey_N,
/* 50 */ ImGuiKey_M,
/* 51 */ ImGuiKey_Comma,
/* 52 */ ImGuiKey_Period,
/* 53 */ ImGuiKey_Slash,
/* 54 */ ImGuiKey_RightShift,
/* 55 */ ImGuiKey_KeypadMultiply,
/* 56 */ ImGuiKey_LeftAlt,
/* 57 */ ImGuiKey_Space,
/* 58 */ ImGuiKey_CapsLock,
/* 59 */ ImGuiKey_F1,
/* 60 */ ImGuiKey_F2,
/* 61 */ ImGuiKey_F3,
/* 62 */ ImGuiKey_F4,
/* 63 */ ImGuiKey_F5,
/* 64 */ ImGuiKey_F6,
/* 65 */ ImGuiKey_F7,
/* 66 */ ImGuiKey_F8,
/* 67 */ ImGuiKey_F9,
/* 68 */ ImGuiKey_F10,
/* 69 */ ImGuiKey_NumLock,
/* 70 */ ImGuiKey_ScrollLock,
/* 71 */ ImGuiKey_Keypad7,
/* 72 */ ImGuiKey_Keypad8,
/* 73 */ ImGuiKey_Keypad9,
/* 74 */ ImGuiKey_KeypadSubtract,
/* 75 */ ImGuiKey_Keypad4,
/* 76 */ ImGuiKey_Keypad5,
/* 77 */ ImGuiKey_Keypad6,
/* 78 */ ImGuiKey_KeypadAdd,
/* 79 */ ImGuiKey_Keypad1,
/* 80 */ ImGuiKey_Keypad2,
/* 81 */ ImGuiKey_Keypad3,
/* 82 */ ImGuiKey_Keypad0,
/* 83 */ ImGuiKey_KeypadDecimal,
/* 84 */ ImGuiKey_RightAlt,
/* 85 */ ImGuiKey_None,
/* 86 */ ImGuiKey_Backslash,
/* 87 */ ImGuiKey_F11,
/* 88 */ ImGuiKey_F12,
/* 89 */ ImGuiKey_None,
/* 90 */ ImGuiKey_None,
/* 91 */ ImGuiKey_None,
/* 92 */ ImGuiKey_None,
/* 93 */ ImGuiKey_None,
/* 94 */ ImGuiKey_None,
/* 95 */ ImGuiKey_None,
/* 96 */ ImGuiKey_KeypadEnter,
/* 97 */ ImGuiKey_RightCtrl,
/* 98 */ ImGuiKey_KeypadDivide,
/* 99 */ ImGuiKey_PrintScreen,
/* 100 */ ImGuiKey_RightAlt,
/* 101 */ ImGuiKey_None,
/* 102 */ ImGuiKey_Home,
/* 103 */ ImGuiKey_UpArrow,
/* 104 */ ImGuiKey_PageUp,
/* 105 */ ImGuiKey_LeftArrow,
/* 106 */ ImGuiKey_RightArrow,
/* 107 */ ImGuiKey_End,
/* 108 */ ImGuiKey_DownArrow,
/* 109 */ ImGuiKey_PageDown,
/* 110 */ ImGuiKey_Insert,
/* 111 */ ImGuiKey_Delete,
/* 112 */ ImGuiKey_None,
/* 113 */ ImGuiKey_None,
/* 114 */ ImGuiKey_None,
/* 115 */ ImGuiKey_None,
/* 116 */ ImGuiKey_None,
/* 117 */ ImGuiKey_KeypadEqual,
/* 118 */ ImGuiKey_None,
/* 119 */ ImGuiKey_Pause,
/* 120 */ ImGuiKey_None,
/* 121 */ ImGuiKey_KeypadDecimal,
/* 122 */ ImGuiKey_None,
/* 123 */ ImGuiKey_None,
/* 124 */ ImGuiKey_None,
/* 125 */ ImGuiKey_LeftSuper,
/* 126 */ ImGuiKey_RightSuper,
/* 127 */ ImGuiKey_Menu,
};
static std::function<void()> s_redraw;
static RunQueue* s_mainThreadTasks;
static struct wl_display* s_dpy;
static struct wl_compositor* s_comp;
static struct wl_surface* s_surf;
static struct wl_egl_window* s_eglWin;
static struct wl_shm* s_shm;
static struct xdg_wm_base* s_wm;
static EGLDisplay s_eglDpy;
static EGLContext s_eglCtx;
static EGLSurface s_eglSurf;
static struct xdg_surface* s_xdgSurf;
static struct xdg_toplevel* s_toplevel;
static struct wl_seat* s_seat;
static struct wl_pointer* s_pointer;
static struct wl_cursor_theme* s_cursorTheme;
static struct wl_surface* s_cursorSurf;
static int32_t s_cursorX, s_cursorY;
static struct xdg_activation_v1* s_activation;
static struct xdg_activation_token_v1* s_actToken;
static struct zxdg_decoration_manager_v1* s_decoration;
static struct zxdg_toplevel_decoration_v1* s_tldec;
static struct wl_keyboard* s_keyboard;
static struct xkb_context* s_xkbCtx;
static struct xkb_keymap* s_xkbKeymap;
static struct xkb_state* s_xkbState;
static struct xkb_compose_table* s_xkbComposeTable;
static struct xkb_compose_state* s_xkbComposeState;
static xkb_mod_index_t s_xkbCtrl, s_xkbAlt, s_xkbShift, s_xkbSuper;
struct Output
namespace
{
int32_t scale;
wl_output* obj;
};
static std::unordered_map<uint32_t, std::unique_ptr<Output>> s_output;
static int s_maxScale = 1;
static int s_prevScale = 1;
std::function<void()> s_redraw;
RunQueue* s_mainThreadTasks;
static bool s_running = true;
static int s_w, s_h;
static bool s_maximized;
static uint64_t s_time;
int32_t s_scale = 1;
static wl_fixed_t s_wheelAxisX, s_wheelAxisY;
static bool s_wheel;
bool s_running = true;
uint64_t s_time;
static void PointerEnter( void*, struct wl_pointer* pointer, uint32_t serial, struct wl_surface* surf, wl_fixed_t sx, wl_fixed_t sy )
{
wl_pointer_set_cursor( pointer, serial, s_cursorSurf, s_cursorX, s_cursorY );
ImGuiIO& io = ImGui::GetIO();
io.AddMousePosEvent( wl_fixed_to_double( sx * s_maxScale ), wl_fixed_to_double( sy * s_maxScale ) );
}
static void PointerLeave( void*, struct wl_pointer* pointer, uint32_t serial, struct wl_surface* surf )
{
ImGuiIO& io = ImGui::GetIO();
io.AddMousePosEvent( -FLT_MAX, -FLT_MAX );
}
static void PointerMotion( void*, struct wl_pointer* pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy )
{
ImGuiIO& io = ImGui::GetIO();
io.AddMousePosEvent( wl_fixed_to_double( sx * s_maxScale ), wl_fixed_to_double( sy * s_maxScale ) );
}
static void PointerButton( void*, struct wl_pointer* pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state )
{
int b;
switch( button )
{
case BTN_LEFT: b = 0; break;
case BTN_MIDDLE: b = 2; break;
case BTN_RIGHT: b = 1; break;
default: return;
}
ImGuiIO& io = ImGui::GetIO();
io.AddMouseButtonEvent( b, state == WL_POINTER_BUTTON_STATE_PRESSED );
}
static void PointerAxis( void*, struct wl_pointer* pointer, uint32_t time, uint32_t axis, wl_fixed_t value )
{
s_wheel = true;
if( axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL )
{
s_wheelAxisX -= value;
}
else
{
s_wheelAxisY -= value;
}
}
static void PointerAxisSource( void*, struct wl_pointer* pointer, uint32_t source )
{
}
static void PointerAxisStop( void*, struct wl_pointer* pointer, uint32_t time, uint32_t axis )
{
}
static void PointerAxisDiscrete( void*, struct wl_pointer* pointer, uint32_t axis, int32_t type )
{
}
static void PointerFrame( void*, struct wl_pointer* pointer )
{
if( s_wheel )
{
s_wheel = false;
s_wheelAxisX /= 8;
s_wheelAxisY /= 8;
ImGuiIO& io = ImGui::GetIO();
io.AddMouseWheelEvent( wl_fixed_to_double( s_wheelAxisX ), wl_fixed_to_double( s_wheelAxisY ) );
s_wheelAxisX = s_wheelAxisY = 0;
}
}
constexpr struct wl_pointer_listener pointerListener = {
.enter = PointerEnter,
.leave = PointerLeave,
.motion = PointerMotion,
.button = PointerButton,
.axis = PointerAxis,
.frame = PointerFrame,
.axis_source = PointerAxisSource,
.axis_stop = PointerAxisStop,
.axis_discrete = PointerAxisDiscrete
};
static void KeyboardKeymap( void*, struct wl_keyboard* kbd, uint32_t format, int32_t fd, uint32_t size )
{
if( format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1 )
{
close( fd );
return;
}
auto map = (char*)mmap( nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0 );
close( fd );
if( map == MAP_FAILED ) return;
if( s_xkbKeymap ) xkb_keymap_unref( s_xkbKeymap );
s_xkbKeymap = xkb_keymap_new_from_string( s_xkbCtx, map, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS );
munmap( map, size );
if( !s_xkbKeymap ) return;
if( s_xkbState ) xkb_state_unref( s_xkbState );
s_xkbState = xkb_state_new( s_xkbKeymap );
const char* locale = getenv( "LC_ALL" );
if( !locale )
{
locale = getenv( "LC_CTYPE" );
if( !locale )
{
locale = getenv( "LANG" );
if( !locale )
{
locale = "C";
}
}
}
if( s_xkbComposeTable ) xkb_compose_table_unref( s_xkbComposeTable );
s_xkbComposeTable = xkb_compose_table_new_from_locale( s_xkbCtx, locale, XKB_COMPOSE_COMPILE_NO_FLAGS );
if( s_xkbComposeState ) xkb_compose_state_unref( s_xkbComposeState );
s_xkbComposeState = xkb_compose_state_new( s_xkbComposeTable, XKB_COMPOSE_STATE_NO_FLAGS );
s_xkbCtrl = xkb_keymap_mod_get_index( s_xkbKeymap, "Control" );
s_xkbAlt = xkb_keymap_mod_get_index( s_xkbKeymap, "Mod1" );
s_xkbShift = xkb_keymap_mod_get_index( s_xkbKeymap, "Shift" );
s_xkbSuper = xkb_keymap_mod_get_index( s_xkbKeymap, "Mod4" );
}
static void KeyboardEnter( void*, struct wl_keyboard* kbd, uint32_t serial, struct wl_surface* surf, struct wl_array* keys )
{
ImGui::GetIO().AddFocusEvent( true );
}
static void KeyboardLeave( void*, struct wl_keyboard* kbd, uint32_t serial, struct wl_surface* surf )
{
ImGui::GetIO().AddFocusEvent( false );
}
static xkb_keysym_t Compose( const xkb_keysym_t sym )
{
if( sym == XKB_KEY_NoSymbol ) return sym;
if( xkb_compose_state_feed( s_xkbComposeState, sym ) != XKB_COMPOSE_FEED_ACCEPTED ) return sym;
switch( xkb_compose_state_get_status( s_xkbComposeState ) )
{
case XKB_COMPOSE_COMPOSED:
return xkb_compose_state_get_one_sym( s_xkbComposeState );
case XKB_COMPOSE_COMPOSING:
case XKB_COMPOSE_CANCELLED:
return XKB_KEY_NoSymbol;
case XKB_COMPOSE_NOTHING:
default:
return sym;
}
}
static void KeyboardKey( void*, struct wl_keyboard* kbd, uint32_t serial, uint32_t time, uint32_t key, uint32_t state )
{
auto& io = ImGui::GetIO();
if( key < ( sizeof( s_keyTable ) / sizeof( *s_keyTable ) ) )
{
io.AddKeyEvent( s_keyTable[key], state == WL_KEYBOARD_KEY_STATE_PRESSED );
}
if( state == WL_KEYBOARD_KEY_STATE_PRESSED )
{
const xkb_keysym_t* keysyms;
if( xkb_state_key_get_syms( s_xkbState, key + 8, &keysyms ) == 1 )
{
const auto sym = Compose( keysyms[0] );
char txt[8];
if( xkb_keysym_to_utf8( sym, txt, sizeof( txt ) ) > 0 )
{
ImGui::GetIO().AddInputCharactersUTF8( txt );
}
}
}
}
static void KeyboardModifiers( void*, struct wl_keyboard* kbd, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group )
{
xkb_state_update_mask( s_xkbState, mods_depressed, mods_latched, mods_locked, 0, 0, group );
auto& io = ImGui::GetIO();
io.AddKeyEvent( ImGuiMod_Ctrl, xkb_state_mod_index_is_active( s_xkbState, s_xkbCtrl, XKB_STATE_MODS_EFFECTIVE ) );
io.AddKeyEvent( ImGuiMod_Shift, xkb_state_mod_index_is_active( s_xkbState, s_xkbShift, XKB_STATE_MODS_EFFECTIVE ) );
io.AddKeyEvent( ImGuiMod_Alt, xkb_state_mod_index_is_active( s_xkbState, s_xkbAlt, XKB_STATE_MODS_EFFECTIVE ) );
io.AddKeyEvent( ImGuiMod_Super, xkb_state_mod_index_is_active( s_xkbState, s_xkbSuper, XKB_STATE_MODS_EFFECTIVE ) );
}
static void KeyboardRepeatInfo( void*, struct wl_keyboard* kbd, int32_t rate, int32_t delay )
{
}
constexpr struct wl_keyboard_listener keyboardListener = {
.keymap = KeyboardKeymap,
.enter = KeyboardEnter,
.leave = KeyboardLeave,
.key = KeyboardKey,
.modifiers = KeyboardModifiers,
.repeat_info = KeyboardRepeatInfo
};
static void SeatCapabilities( void*, struct wl_seat* seat, uint32_t caps )
{
const bool hasPointer = caps & WL_SEAT_CAPABILITY_POINTER;
const bool hasKeyboard = caps & WL_SEAT_CAPABILITY_KEYBOARD;
if( hasPointer && !s_pointer )
{
s_pointer = wl_seat_get_pointer( s_seat );
wl_pointer_add_listener( s_pointer, &pointerListener, nullptr );
}
else if( !hasPointer && s_pointer )
{
wl_pointer_release( s_pointer );
s_pointer = nullptr;
}
if( hasKeyboard && !s_keyboard )
{
s_keyboard = wl_seat_get_keyboard( s_seat );
wl_keyboard_add_listener( s_keyboard, &keyboardListener, nullptr );
}
else if( !hasKeyboard && s_keyboard )
{
wl_keyboard_release( s_keyboard );
s_keyboard = nullptr;
}
}
static void SeatName( void*, struct wl_seat* seat, const char* name )
{
}
constexpr struct wl_seat_listener seatListener = {
.capabilities = SeatCapabilities,
.name = SeatName
};
static void WmPing( void*, struct xdg_wm_base* shell, uint32_t serial )
{
xdg_wm_base_pong( shell, serial );
}
constexpr struct xdg_wm_base_listener wmListener = {
.ping = WmPing
};
static void OutputGeometry( void*, struct wl_output* output, int32_t x, int32_t y, int32_t phys_w, int32_t phys_h, int32_t subpixel, const char* make, const char* model, int32_t transform )
{
}
static void OutputMode( void*, struct wl_output* output, uint32_t flags, int32_t w, int32_t h, int32_t refresh )
{
}
static void OutputDone( void*, struct wl_output* output )
{
int max = 1;
for( auto& out : s_output )
{
if( out.second->scale > max ) max = out.second->scale;
}
s_maxScale = max;
}
static void OutputScale( void* data, struct wl_output* output, int32_t scale )
{
auto out = (Output*)data;
out->scale = scale;
}
constexpr struct wl_output_listener outputListener = {
.geometry = OutputGeometry,
.mode = OutputMode,
.done = OutputDone,
.scale = OutputScale
};
static void DecorationConfigure( void*, struct zxdg_toplevel_decoration_v1* tldec, uint32_t mode )
{
}
constexpr struct zxdg_toplevel_decoration_v1_listener decorationListener = {
.configure = DecorationConfigure
};
static void RegistryGlobal( void*, struct wl_registry* reg, uint32_t name, const char* interface, uint32_t version )
{
if( strcmp( interface, wl_compositor_interface.name ) == 0 )
{
s_comp = (wl_compositor*)wl_registry_bind( reg, name, &wl_compositor_interface, 4 );
}
else if( strcmp( interface, wl_shm_interface.name ) == 0 )
{
s_shm = (wl_shm*)wl_registry_bind( reg, name, &wl_shm_interface, 1 );
}
else if( strcmp( interface, xdg_wm_base_interface.name ) == 0 )
{
s_wm = (xdg_wm_base*)wl_registry_bind( reg, name, &xdg_wm_base_interface, 1 );
xdg_wm_base_add_listener( s_wm, &wmListener, nullptr );
}
else if( strcmp( interface, wl_seat_interface.name ) == 0 )
{
s_seat = (wl_seat*)wl_registry_bind( reg, name, &wl_seat_interface, 5 );
wl_seat_add_listener( s_seat, &seatListener, nullptr );
}
else if( strcmp( interface, xdg_activation_v1_interface.name ) == 0 )
{
s_activation = (xdg_activation_v1*)wl_registry_bind( reg, name, &xdg_activation_v1_interface, 1 );
}
else if( strcmp( interface, wl_output_interface.name ) == 0 )
{
auto output = (wl_output*)wl_registry_bind( reg, name, &wl_output_interface, 2 );
auto ptr = std::make_unique<Output>( Output { 1, output } );
wl_output_add_listener( output, &outputListener, ptr.get() );
s_output.emplace( name, std::move( ptr ) );
}
else if( strcmp( interface, zxdg_decoration_manager_v1_interface.name ) == 0 )
{
s_decoration = (zxdg_decoration_manager_v1*)wl_registry_bind( reg, name, &zxdg_decoration_manager_v1_interface, 1 );
}
}
static void RegistryGlobalRemove( void*, struct wl_registry* reg, uint32_t name )
{
auto it = s_output.find( name );
if( it == s_output.end() ) return;
wl_output_destroy( it->second->obj );
s_output.erase( it );
}
constexpr struct wl_registry_listener registryListener = {
.global = RegistryGlobal,
.global_remove = RegistryGlobalRemove
};
static void XdgSurfaceConfigure( void*, struct xdg_surface* surf, uint32_t serial )
{
tracy::s_wasActive = true;
xdg_surface_ack_configure( surf, serial );
}
constexpr struct xdg_surface_listener xdgSurfaceListener = {
.configure = XdgSurfaceConfigure
};
static void XdgToplevelConfigure( void*, struct xdg_toplevel* toplevel, int32_t width, int32_t height, struct wl_array* states )
{
if( width == 0 || height == 0 ) return;
bool max = false;
auto data = (uint32_t*)states->data;
for( size_t i = 0; i < states->size / sizeof(uint32_t); i++ )
{
if( data[i] == XDG_TOPLEVEL_STATE_MAXIMIZED )
{
max = true;
break;
}
}
s_maximized = max;
width *= s_maxScale;
height *= s_maxScale;
if( s_w != width || s_h != height )
{
s_w = width;
s_h = height;
wl_egl_window_resize( s_eglWin, width, height, 0, 0 );
wl_surface_commit( s_surf );
}
}
static void XdgToplevelClose( void*, struct xdg_toplevel* toplevel )
{
s_running = false;
}
constexpr struct xdg_toplevel_listener toplevelListener = {
.configure = XdgToplevelConfigure,
.close = XdgToplevelClose
};
static void SetupCursor()
{
auto env_xcursor_theme = getenv( "XCURSOR_THEME" );
auto env_xcursor_size = getenv( "XCURSOR_SIZE" );
int size = env_xcursor_size ? atoi( env_xcursor_size ) : 24;
size *= s_maxScale;
if( s_cursorSurf ) wl_surface_destroy( s_cursorSurf );
if( s_cursorTheme ) wl_cursor_theme_destroy( s_cursorTheme );
s_cursorTheme = wl_cursor_theme_load( env_xcursor_theme, size, s_shm );
auto cursor = wl_cursor_theme_get_cursor( s_cursorTheme, "left_ptr" );
s_cursorSurf = wl_compositor_create_surface( s_comp );
if( s_maxScale != 1 ) wl_surface_set_buffer_scale( s_cursorSurf, s_maxScale );
wl_surface_attach( s_cursorSurf, wl_cursor_image_get_buffer( cursor->images[0] ), 0, 0 );
wl_surface_commit( s_cursorSurf );
s_cursorX = cursor->images[0]->hotspot_x / s_maxScale;
s_cursorY = cursor->images[0]->hotspot_y / s_maxScale;
std::unique_ptr<WaylandDisplay> s_display;
std::unique_ptr<WaylandWindow> s_window;
}
Backend::Backend( const char* title, const std::function<void()>& redraw, RunQueue* mainThreadTasks )
{
s_redraw = redraw;
s_mainThreadTasks = mainThreadTasks;
s_w = m_winPos.w;
s_h = m_winPos.h;
s_maximized = m_winPos.maximize;
s_dpy = wl_display_connect( nullptr );
if( !s_dpy ) { fprintf( stderr, "Cannot establish wayland display connection!\n" ); exit( 1 ); }
wl_registry_add_listener( wl_display_get_registry( s_dpy ), &registryListener, nullptr );
s_xkbCtx = xkb_context_new( XKB_CONTEXT_NO_FLAGS );
wl_display_roundtrip( s_dpy );
if( !s_comp ) { fprintf( stderr, "No wayland compositor!\n" ); exit( 1 ); }
if( !s_shm ) { fprintf( stderr, "No wayland shared memory!\n" ); exit( 1 ); }
if( !s_wm ) { fprintf( stderr, "No wayland window manager!\n" ); exit( 1 ); }
if( !s_seat ) { fprintf( stderr, "No wayland seat!\n" ); exit( 1 ); }
s_surf = wl_compositor_create_surface( s_comp );
s_eglWin = wl_egl_window_create( s_surf, m_winPos.w, m_winPos.h );
s_xdgSurf = xdg_wm_base_get_xdg_surface( s_wm, s_surf );
xdg_surface_add_listener( s_xdgSurf, &xdgSurfaceListener, nullptr );
SetupCursor();
constexpr EGLint eglConfigAttrib[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_NONE
};
s_eglDpy = eglGetPlatformDisplay( EGL_PLATFORM_WAYLAND_KHR, s_dpy, nullptr );
EGLBoolean res;
res = eglInitialize( s_eglDpy, nullptr, nullptr );
if( res != EGL_TRUE ) { fprintf( stderr, "Cannot initialize EGL!\n" ); exit( 1 ); }
EGLint count;
EGLConfig eglConfig;
res = eglChooseConfig( s_eglDpy, eglConfigAttrib, &eglConfig, 1, &count );
if( res != EGL_TRUE || count != 1 ) { fprintf( stderr, "No suitable EGL config found!\n" ); exit( 1 ); }
res = eglBindAPI( EGL_OPENGL_API );
if( res != EGL_TRUE ) { fprintf( stderr, "Cannot use OpenGL through EGL!\n" ); exit( 1 ); }
s_eglSurf = eglCreatePlatformWindowSurface( s_eglDpy, eglConfig, s_eglWin, nullptr );
constexpr EGLint eglCtxAttrib[] = {
EGL_CONTEXT_MAJOR_VERSION, 3,
EGL_CONTEXT_MINOR_VERSION, 2,
EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
EGL_NONE
};
s_eglCtx = eglCreateContext( s_eglDpy, eglConfig, EGL_NO_CONTEXT, eglCtxAttrib );
if( !s_eglCtx ) { fprintf( stderr, "Cannot create OpenGL 3.2 Core Profile context!\n" ); exit( 1 ); }
res = eglMakeCurrent( s_eglDpy, s_eglSurf, s_eglSurf, s_eglCtx );
if( res != EGL_TRUE ) { fprintf( stderr, "Cannot make EGL context current!\n" ); exit( 1 ); }
s_display = std::make_unique<WaylandDisplay>( s_scale, []( wl_pointer* pointer, uint32_t serial ) {
int32_t x, y;
auto surface = s_window->GetCursor( x, y );
wl_pointer_set_cursor( pointer, serial, surface, x, y );
} );
s_window = std::make_unique<WaylandWindow>( WaylandWindowParams {
.display = *s_display,
.title = title,
.winPos = m_winPos,
.running = s_running,
.scale = s_scale,
} );
ImGui_ImplOpenGL3_Init( "#version 150" );
wl_display_roundtrip( s_dpy );
s_toplevel = xdg_surface_get_toplevel( s_xdgSurf );
xdg_toplevel_add_listener( s_toplevel, &toplevelListener, nullptr );
xdg_toplevel_set_title( s_toplevel, title );
xdg_toplevel_set_app_id( s_toplevel, "tracy" );
if( s_decoration )
{
s_tldec = zxdg_decoration_manager_v1_get_toplevel_decoration( s_decoration, s_toplevel );
zxdg_toplevel_decoration_v1_add_listener( s_tldec, &decorationListener, nullptr );
zxdg_toplevel_decoration_v1_set_mode( s_tldec, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE );
wl_display_roundtrip( s_dpy );
}
ImGuiIO& io = ImGui::GetIO();
io.BackendPlatformName = "wayland (tracy profiler)";
s_time = std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::high_resolution_clock::now().time_since_epoch() ).count();
@ -710,91 +52,36 @@ Backend::Backend( const char* title, const std::function<void()>& redraw, RunQue
Backend::~Backend()
{
if( s_tldec ) zxdg_toplevel_decoration_v1_destroy( s_tldec );
if( s_decoration ) zxdg_decoration_manager_v1_destroy( s_decoration );
if( s_actToken ) xdg_activation_token_v1_destroy( s_actToken );
if( s_activation ) xdg_activation_v1_destroy( s_activation );
if( s_keyboard ) wl_keyboard_destroy( s_keyboard );
if( s_pointer ) wl_pointer_destroy( s_pointer );
eglMakeCurrent( s_eglDpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
eglDestroySurface( s_eglDpy, s_eglSurf );
eglDestroyContext( s_eglDpy, s_eglCtx );
eglTerminate( s_eglDpy );
xdg_toplevel_destroy( s_toplevel );
wl_surface_destroy( s_cursorSurf );
wl_cursor_theme_destroy( s_cursorTheme );
xdg_surface_destroy( s_xdgSurf );
wl_egl_window_destroy( s_eglWin );
wl_surface_destroy( s_surf );
for( auto& v : s_output ) wl_output_destroy( v.second->obj );
s_output.clear();
wl_seat_destroy( s_seat );
xdg_wm_base_destroy( s_wm );
wl_shm_destroy( s_shm );
wl_compositor_destroy( s_comp );
if( s_xkbComposeState ) xkb_compose_state_unref( s_xkbComposeState );
if( s_xkbComposeTable ) xkb_compose_table_unref( s_xkbComposeTable );
if( s_xkbState ) xkb_state_unref( s_xkbState );
if( s_xkbKeymap ) xkb_keymap_unref( s_xkbKeymap );
xkb_context_unref( s_xkbCtx );
wl_display_disconnect( s_dpy );
ImGui_ImplOpenGL3_Shutdown();
s_window.reset();
s_display.reset();
}
void Backend::Show()
{
wl_surface_commit( s_surf );
s_window->Show();
}
void Backend::Run()
{
while( s_running && wl_display_dispatch( s_dpy ) != -1 )
while( s_running && wl_display_dispatch( s_display->GetDisplay() ) != -1 )
{
s_redraw();
s_mainThreadTasks->Run();
s_redraw();
}
}
static void TokenDone( void*, xdg_activation_token_v1* token, const char* str )
{
xdg_activation_v1_activate( s_activation, str, s_surf );
xdg_activation_token_v1_destroy( token );
s_actToken = nullptr;
}
constexpr struct xdg_activation_token_v1_listener tokenListener = {
.done = TokenDone
};
void Backend::Attention()
{
if( !s_activation ) return;
if( s_actToken ) return;
s_actToken = xdg_activation_v1_get_activation_token( s_activation );
xdg_activation_token_v1_set_surface( s_actToken, s_surf );
xdg_activation_token_v1_commit( s_actToken );
xdg_activation_token_v1_add_listener( s_actToken, &tokenListener, nullptr );
s_window->Attention();
}
void Backend::NewFrame( int& w, int& h )
{
if( s_prevScale != s_maxScale )
{
SetupCursor();
wl_surface_set_buffer_scale( s_surf, s_maxScale );
s_prevScale = s_maxScale;
}
m_winPos.maximize = s_maximized;
if( !s_maximized )
{
m_winPos.w = s_w;
m_winPos.h = s_h;
}
w = s_w;
h = s_h;
s_window->NewFrame();
w = s_window->Width();
h = s_window->Height();
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2( w, h );
@ -812,12 +99,12 @@ void Backend::EndFrame()
const ImVec4 clear_color = ImColor( 114, 144, 154 );
ImGui::Render();
glViewport( 0, 0, s_w, s_h );
glViewport( 0, 0, s_window->Width(), s_window->Height() );
glClearColor( clear_color.x, clear_color.y, clear_color.z, clear_color.w );
glClear( GL_COLOR_BUFFER_BIT );
ImGui_ImplOpenGL3_RenderDrawData( ImGui::GetDrawData() );
eglSwapBuffers( s_eglDpy, s_eglSurf );
s_window->Present();
}
void Backend::SetIcon( uint8_t* data, int w, int h )
@ -826,10 +113,10 @@ void Backend::SetIcon( uint8_t* data, int w, int h )
void Backend::SetTitle( const char* title )
{
xdg_toplevel_set_title( s_toplevel, title );
s_window->SetTitle( title );
}
float Backend::GetDpiScale()
{
return s_maxScale;
return s_scale;
}

View File

@ -0,0 +1,155 @@
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "WaylandDisplay.hpp"
#include "WaylandMethod.hpp"
#include "WaylandOutput.hpp"
#include "WaylandRegistry.hpp"
namespace {
void Check( bool condition, const char* msg )
{
if( !condition )
{
fprintf( stderr, "%s\n", msg );
abort();
}
}
}
WaylandDisplay::WaylandDisplay( int32_t& scale, std::function<void(wl_pointer*, uint32_t)> setCursor )
: m_dpy( wl_display_connect( nullptr ) )
, m_decorationManager( nullptr )
, m_activation( nullptr )
, m_scale( scale )
, m_setCursor( std::move( setCursor ) )
{
Check( m_dpy, "Failed to connect to Wayland display" );
static constexpr wl_registry_listener listener = {
.global = Method( RegistryGlobal ),
.global_remove = Method( RegistryGlobalRemove ),
};
wl_registry_add_listener( wl_display_get_registry( m_dpy ), &listener, this );
wl_display_roundtrip( m_dpy );
Check( m_compositor, "Wayland compositor not found" );
Check( m_shm, "Wayland shared memory not found" );
Check( m_wmBase, "Wayland window manager not found" );
Check( m_seat, "Wayland seat not found" );
}
WaylandDisplay::~WaylandDisplay()
{
if( m_decorationManager ) zxdg_decoration_manager_v1_destroy( m_decorationManager );
if( m_activation ) xdg_activation_v1_destroy( m_activation );
m_outputs.clear();
m_keyboard.reset();
m_pointer.reset();
wl_seat_destroy( m_seat );
xdg_wm_base_destroy( m_wmBase );
wl_shm_destroy( m_shm );
wl_compositor_destroy( m_compositor );
wl_display_disconnect( m_dpy );
}
WaylandOutput* WaylandDisplay::GetOutput( wl_output* output, uint32_t& id )
{
auto it = std::find_if( m_outputs.begin(), m_outputs.end(), [output]( const auto& pair ) { return pair.second->Output() == output; } );
if( it == m_outputs.end() ) return nullptr;
id = it->first;
return it->second.get();
}
WaylandOutput* WaylandDisplay::GetOutput( uint32_t id )
{
auto it = m_outputs.find( id );
if( it == m_outputs.end() ) return nullptr;
return it->second.get();
}
void WaylandDisplay::RegistryGlobal( wl_registry* reg, uint32_t name, const char* interface, uint32_t version )
{
if( strcmp( interface, wl_compositor_interface.name ) == 0 )
{
m_compositor = RegistryBind( wl_compositor, 3, 4 );
}
else if ( strcmp( interface, wl_shm_interface.name ) == 0 )
{
m_shm = RegistryBind( wl_shm );
}
else if( strcmp( interface, xdg_wm_base_interface.name ) == 0 )
{
static constexpr xdg_wm_base_listener listener = {
.ping = Method( XdgWmPing )
};
m_wmBase = RegistryBind( xdg_wm_base );
xdg_wm_base_add_listener( m_wmBase, &listener, this );
}
else if( strcmp( interface, wl_seat_interface.name ) == 0 )
{
static constexpr wl_seat_listener listener = {
.capabilities = Method( SeatCapabilities ),
.name = Method( SeatName )
};
m_seat = RegistryBind( wl_seat, 5, 9 );
wl_seat_add_listener( m_seat, &listener, this );
}
else if( strcmp( interface, wl_output_interface.name ) == 0 )
{
auto output = RegistryBind( wl_output, 3, 4 );
m_outputs.emplace( name, std::make_unique<WaylandOutput>( output ) );
}
else if( strcmp( interface, zxdg_decoration_manager_v1_interface.name ) == 0 )
{
m_decorationManager = RegistryBind( zxdg_decoration_manager_v1 );
}
else if( strcmp( interface, xdg_activation_v1_interface.name ) == 0 )
{
m_activation = RegistryBind( xdg_activation_v1 );
}
}
void WaylandDisplay::RegistryGlobalRemove( wl_registry* reg, uint32_t name )
{
auto it = m_outputs.find( name );
if( it != m_outputs.end() ) m_outputs.erase( it );
}
void WaylandDisplay::XdgWmPing( xdg_wm_base* shell, uint32_t serial )
{
xdg_wm_base_pong( shell, serial );
}
void WaylandDisplay::SeatCapabilities( wl_seat* seat, uint32_t caps )
{
const bool hasPointer = caps & WL_SEAT_CAPABILITY_POINTER;
const bool hasKeyboard = caps & WL_SEAT_CAPABILITY_KEYBOARD;
if( hasPointer && !m_pointer )
{
m_pointer = std::make_unique<WaylandPointer>( wl_seat_get_pointer( seat ), m_scale, m_setCursor );
}
else if( !hasPointer && m_pointer )
{
m_pointer.reset();
}
if( hasKeyboard && !m_keyboard )
{
m_keyboard = std::make_unique<WaylandKeyboard>( wl_seat_get_keyboard( seat ) );
}
else if( !hasKeyboard && m_keyboard )
{
m_keyboard.reset();
}
}
void WaylandDisplay::SeatName( wl_seat* seat, const char* name )
{
}

View File

@ -0,0 +1,61 @@
#ifndef __WAYLANDDISPLAY_HPP__
#define __WAYLANDDISPLAY_HPP__
#include <functional>
#include <memory>
#include <wayland-client.h>
#include <unordered_map>
#include "wayland/xdg-activation.h"
#include "wayland/xdg-decoration.h"
#include "wayland/xdg-shell.h"
#include "WaylandKeyboard.hpp"
#include "WaylandOutput.hpp"
#include "WaylandPointer.hpp"
class WaylandDisplay
{
public:
WaylandDisplay( int32_t& scale, std::function<void(wl_pointer*, uint32_t)> setCursor );
~WaylandDisplay();
[[nodiscard]] wl_display* GetDisplay() const { return m_dpy; }
[[nodiscard]] wl_compositor* GetCompositor() const { return m_compositor; }
[[nodiscard]] wl_shm* GetShm() const { return m_shm; }
[[nodiscard]] xdg_wm_base* GetWmBase() const { return m_wmBase; }
[[nodiscard]] zxdg_decoration_manager_v1* GetDecorationManager() const { return m_decorationManager; }
[[nodiscard]] xdg_activation_v1* GetActivation() const { return m_activation; }
[[nodiscard]] wl_pointer* GetPointer() const { return m_pointer ? m_pointer->GetPointer() : nullptr; }
[[nodiscard]] WaylandOutput* GetOutput( wl_output* output, uint32_t& id );
[[nodiscard]] WaylandOutput* GetOutput( uint32_t id );
private:
void RegistryGlobal( wl_registry* reg, uint32_t name, const char* interface, uint32_t version );
void RegistryGlobalRemove( wl_registry* reg, uint32_t name );
void XdgWmPing( xdg_wm_base* shell, uint32_t serial );
void SeatCapabilities( wl_seat* seat, uint32_t caps );
void SeatName( wl_seat* seat, const char* name );
wl_display* m_dpy;
wl_compositor* m_compositor;
wl_shm* m_shm;
xdg_wm_base* m_wmBase;
wl_seat* m_seat;
zxdg_decoration_manager_v1* m_decorationManager;
xdg_activation_v1* m_activation;
std::unique_ptr<WaylandKeyboard> m_keyboard;
std::unique_ptr<WaylandPointer> m_pointer;
std::unordered_map<uint32_t, std::unique_ptr<WaylandOutput>> m_outputs;
int32_t& m_scale;
std::function<void(wl_pointer*, uint32_t)> m_setCursor;
};
#endif

View File

@ -0,0 +1,282 @@
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
#include "../../imgui/imgui.h"
#include "WaylandKeyboard.hpp"
#include "WaylandMethod.hpp"
constexpr ImGuiKey s_keyTable[] = {
/* 0 */ ImGuiKey_None,
/* 1 */ ImGuiKey_Escape,
/* 2 */ ImGuiKey_1,
/* 3 */ ImGuiKey_2,
/* 4 */ ImGuiKey_3,
/* 5 */ ImGuiKey_4,
/* 6 */ ImGuiKey_5,
/* 7 */ ImGuiKey_6,
/* 8 */ ImGuiKey_7,
/* 9 */ ImGuiKey_8,
/* 10 */ ImGuiKey_9,
/* 11 */ ImGuiKey_0,
/* 12 */ ImGuiKey_Minus,
/* 13 */ ImGuiKey_Equal,
/* 14 */ ImGuiKey_Backspace,
/* 15 */ ImGuiKey_Tab,
/* 16 */ ImGuiKey_Q,
/* 17 */ ImGuiKey_W,
/* 18 */ ImGuiKey_E,
/* 19 */ ImGuiKey_R,
/* 20 */ ImGuiKey_T,
/* 21 */ ImGuiKey_Y,
/* 22 */ ImGuiKey_U,
/* 23 */ ImGuiKey_I,
/* 24 */ ImGuiKey_O,
/* 25 */ ImGuiKey_P,
/* 26 */ ImGuiKey_LeftBracket,
/* 27 */ ImGuiKey_RightBracket,
/* 28 */ ImGuiKey_Enter,
/* 29 */ ImGuiKey_LeftCtrl,
/* 30 */ ImGuiKey_A,
/* 31 */ ImGuiKey_S,
/* 32 */ ImGuiKey_D,
/* 33 */ ImGuiKey_F,
/* 34 */ ImGuiKey_G,
/* 35 */ ImGuiKey_H,
/* 36 */ ImGuiKey_J,
/* 37 */ ImGuiKey_K,
/* 38 */ ImGuiKey_L,
/* 39 */ ImGuiKey_Semicolon,
/* 40 */ ImGuiKey_Apostrophe,
/* 41 */ ImGuiKey_GraveAccent,
/* 42 */ ImGuiKey_LeftShift,
/* 43 */ ImGuiKey_Backslash,
/* 44 */ ImGuiKey_Z,
/* 45 */ ImGuiKey_X,
/* 46 */ ImGuiKey_C,
/* 47 */ ImGuiKey_V,
/* 48 */ ImGuiKey_B,
/* 49 */ ImGuiKey_N,
/* 50 */ ImGuiKey_M,
/* 51 */ ImGuiKey_Comma,
/* 52 */ ImGuiKey_Period,
/* 53 */ ImGuiKey_Slash,
/* 54 */ ImGuiKey_RightShift,
/* 55 */ ImGuiKey_KeypadMultiply,
/* 56 */ ImGuiKey_LeftAlt,
/* 57 */ ImGuiKey_Space,
/* 58 */ ImGuiKey_CapsLock,
/* 59 */ ImGuiKey_F1,
/* 60 */ ImGuiKey_F2,
/* 61 */ ImGuiKey_F3,
/* 62 */ ImGuiKey_F4,
/* 63 */ ImGuiKey_F5,
/* 64 */ ImGuiKey_F6,
/* 65 */ ImGuiKey_F7,
/* 66 */ ImGuiKey_F8,
/* 67 */ ImGuiKey_F9,
/* 68 */ ImGuiKey_F10,
/* 69 */ ImGuiKey_NumLock,
/* 70 */ ImGuiKey_ScrollLock,
/* 71 */ ImGuiKey_Keypad7,
/* 72 */ ImGuiKey_Keypad8,
/* 73 */ ImGuiKey_Keypad9,
/* 74 */ ImGuiKey_KeypadSubtract,
/* 75 */ ImGuiKey_Keypad4,
/* 76 */ ImGuiKey_Keypad5,
/* 77 */ ImGuiKey_Keypad6,
/* 78 */ ImGuiKey_KeypadAdd,
/* 79 */ ImGuiKey_Keypad1,
/* 80 */ ImGuiKey_Keypad2,
/* 81 */ ImGuiKey_Keypad3,
/* 82 */ ImGuiKey_Keypad0,
/* 83 */ ImGuiKey_KeypadDecimal,
/* 84 */ ImGuiKey_RightAlt,
/* 85 */ ImGuiKey_None,
/* 86 */ ImGuiKey_Backslash,
/* 87 */ ImGuiKey_F11,
/* 88 */ ImGuiKey_F12,
/* 89 */ ImGuiKey_None,
/* 90 */ ImGuiKey_None,
/* 91 */ ImGuiKey_None,
/* 92 */ ImGuiKey_None,
/* 93 */ ImGuiKey_None,
/* 94 */ ImGuiKey_None,
/* 95 */ ImGuiKey_None,
/* 96 */ ImGuiKey_KeypadEnter,
/* 97 */ ImGuiKey_RightCtrl,
/* 98 */ ImGuiKey_KeypadDivide,
/* 99 */ ImGuiKey_PrintScreen,
/* 100 */ ImGuiKey_RightAlt,
/* 101 */ ImGuiKey_None,
/* 102 */ ImGuiKey_Home,
/* 103 */ ImGuiKey_UpArrow,
/* 104 */ ImGuiKey_PageUp,
/* 105 */ ImGuiKey_LeftArrow,
/* 106 */ ImGuiKey_RightArrow,
/* 107 */ ImGuiKey_End,
/* 108 */ ImGuiKey_DownArrow,
/* 109 */ ImGuiKey_PageDown,
/* 110 */ ImGuiKey_Insert,
/* 111 */ ImGuiKey_Delete,
/* 112 */ ImGuiKey_None,
/* 113 */ ImGuiKey_None,
/* 114 */ ImGuiKey_None,
/* 115 */ ImGuiKey_None,
/* 116 */ ImGuiKey_None,
/* 117 */ ImGuiKey_KeypadEqual,
/* 118 */ ImGuiKey_None,
/* 119 */ ImGuiKey_Pause,
/* 120 */ ImGuiKey_None,
/* 121 */ ImGuiKey_KeypadDecimal,
/* 122 */ ImGuiKey_None,
/* 123 */ ImGuiKey_None,
/* 124 */ ImGuiKey_None,
/* 125 */ ImGuiKey_LeftSuper,
/* 126 */ ImGuiKey_RightSuper,
/* 127 */ ImGuiKey_Menu,
};
WaylandKeyboard::WaylandKeyboard( wl_keyboard* keyboard )
: m_keyboard( keyboard )
, m_keymap( nullptr )
, m_state( nullptr )
, m_composeTable( nullptr )
, m_composeState( nullptr )
{
m_xkbCtx = xkb_context_new( XKB_CONTEXT_NO_FLAGS );
static constexpr wl_keyboard_listener listener = {
.keymap = Method( Keymap ),
.enter = Method( Enter ),
.leave = Method( Leave ),
.key = Method( Key ),
.modifiers = Method( Modifiers ),
.repeat_info = Method( RepeatInfo ),
};
wl_keyboard_add_listener( m_keyboard, &listener, this );
}
WaylandKeyboard::~WaylandKeyboard()
{
if( m_composeState ) xkb_compose_state_unref( m_composeState );
if( m_composeTable ) xkb_compose_table_unref( m_composeTable );
if( m_state ) xkb_state_unref( m_state );
if( m_keymap ) xkb_keymap_unref( m_keymap );
xkb_context_unref( m_xkbCtx );
wl_keyboard_destroy( m_keyboard );
}
void WaylandKeyboard::Keymap( wl_keyboard* kbd, uint32_t format, int32_t fd, uint32_t size )
{
if( format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1 )
{
close( fd );
return;
}
auto map = (char*)mmap( nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0 );
close( fd );
if( map == MAP_FAILED ) return;
if( m_keymap ) xkb_keymap_unref( m_keymap );
m_keymap = xkb_keymap_new_from_string( m_xkbCtx, map, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS );
munmap( map, size );
if( !m_keymap ) return;
if( m_state ) xkb_state_unref( m_state );
m_state = xkb_state_new( m_keymap );
const char* locale = getenv( "LC_ALL" );
if( !locale )
{
locale = getenv( "LC_CTYPE" );
if( !locale )
{
locale = getenv( "LANG" );
if( !locale )
{
locale = "C";
}
}
}
if( m_composeTable ) xkb_compose_table_unref( m_composeTable );
m_composeTable = xkb_compose_table_new_from_locale( m_xkbCtx, locale, XKB_COMPOSE_COMPILE_NO_FLAGS );
if( m_composeState ) xkb_compose_state_unref( m_composeState );
m_composeState = xkb_compose_state_new( m_composeTable, XKB_COMPOSE_STATE_NO_FLAGS );
m_modCtrl = xkb_keymap_mod_get_index( m_keymap, XKB_MOD_NAME_CTRL );
m_modAlt = xkb_keymap_mod_get_index( m_keymap, XKB_MOD_NAME_ALT );
m_modShift = xkb_keymap_mod_get_index( m_keymap, XKB_MOD_NAME_SHIFT );
m_modSuper = xkb_keymap_mod_get_index( m_keymap, XKB_MOD_NAME_LOGO );
}
void WaylandKeyboard::Enter( wl_keyboard* kbd, uint32_t serial, wl_surface* surf, wl_array* keys )
{
ImGui::GetIO().AddFocusEvent( true );
}
void WaylandKeyboard::Leave( wl_keyboard* kbd, uint32_t serial, wl_surface* surf )
{
ImGui::GetIO().AddFocusEvent( false );
}
void WaylandKeyboard::Key( wl_keyboard* kbd, uint32_t serial, uint32_t time, uint32_t key, uint32_t state )
{
auto& io = ImGui::GetIO();
if( key < ( sizeof( s_keyTable ) / sizeof( *s_keyTable ) ) )
{
io.AddKeyEvent( s_keyTable[key], state == WL_KEYBOARD_KEY_STATE_PRESSED );
}
if( state == WL_KEYBOARD_KEY_STATE_PRESSED )
{
const xkb_keysym_t* keysyms;
if( xkb_state_key_get_syms( m_state, key + 8, &keysyms ) == 1 )
{
const auto sym = Compose( keysyms[0] );
char txt[8];
if( xkb_keysym_to_utf8( sym, txt, sizeof( txt ) ) > 0 )
{
ImGui::GetIO().AddInputCharactersUTF8( txt );
}
}
}
}
void WaylandKeyboard::Modifiers( wl_keyboard* kbd, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group )
{
xkb_state_update_mask( m_state, mods_depressed, mods_latched, mods_locked, 0, 0, group );
auto& io = ImGui::GetIO();
io.AddKeyEvent( ImGuiMod_Ctrl, xkb_state_mod_index_is_active( m_state, m_modCtrl, XKB_STATE_MODS_EFFECTIVE ) );
io.AddKeyEvent( ImGuiMod_Shift, xkb_state_mod_index_is_active( m_state, m_modShift, XKB_STATE_MODS_EFFECTIVE ) );
io.AddKeyEvent( ImGuiMod_Alt, xkb_state_mod_index_is_active( m_state, m_modAlt, XKB_STATE_MODS_EFFECTIVE ) );
io.AddKeyEvent( ImGuiMod_Super, xkb_state_mod_index_is_active( m_state, m_modSuper, XKB_STATE_MODS_EFFECTIVE ) );
}
void WaylandKeyboard::RepeatInfo( wl_keyboard* kbd, int32_t rate, int32_t delay )
{
}
xkb_keysym_t WaylandKeyboard::Compose( xkb_keysym_t sym )
{
if( sym == XKB_KEY_NoSymbol ) return sym;
if( xkb_compose_state_feed( m_composeState, sym ) != XKB_COMPOSE_FEED_ACCEPTED ) return sym;
switch( xkb_compose_state_get_status( m_composeState ) )
{
case XKB_COMPOSE_COMPOSED:
return xkb_compose_state_get_one_sym( m_composeState );
case XKB_COMPOSE_COMPOSING:
case XKB_COMPOSE_CANCELLED:
return XKB_KEY_NoSymbol;
case XKB_COMPOSE_NOTHING:
default:
return sym;
}
}

View File

@ -0,0 +1,38 @@
#ifndef __WAYLANDKEYBOARD_HPP__
#define __WAYLANDKEYBOARD_HPP__
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-compose.h>
#include <wayland-client.h>
class WaylandKeyboard
{
public:
WaylandKeyboard( wl_keyboard* keyboard );
~WaylandKeyboard();
private:
void Keymap( wl_keyboard* kbd, uint32_t format, int32_t fd, uint32_t size );
void Enter( wl_keyboard* kbd, uint32_t serial, wl_surface* surf, wl_array* keys );
void Leave( wl_keyboard* kbd, uint32_t serial, wl_surface* surf );
void Key( wl_keyboard* kbd, uint32_t serial, uint32_t time, uint32_t key, uint32_t state );
void Modifiers( wl_keyboard* kbd, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group );
void RepeatInfo( wl_keyboard* kbd, int32_t rate, int32_t delay );
xkb_keysym_t Compose( xkb_keysym_t sym );
wl_keyboard* m_keyboard;
xkb_context* m_xkbCtx;
xkb_keymap* m_keymap;
xkb_state* m_state;
xkb_compose_table* m_composeTable;
xkb_compose_state* m_composeState;
xkb_mod_index_t m_modCtrl;
xkb_mod_index_t m_modAlt;
xkb_mod_index_t m_modShift;
xkb_mod_index_t m_modSuper;
};
#endif

View File

@ -0,0 +1,8 @@
#ifndef __WAYLANDMETHOD_HPP__
#define __WAYLANDMETHOD_HPP__
#include <assert.h>
#define Method( func ) [](void* ptr, auto... args) { assert( ptr ); ((decltype(this))ptr)->func( args... ); }
#endif

View File

@ -0,0 +1,48 @@
#include "WaylandMethod.hpp"
#include "WaylandOutput.hpp"
WaylandOutput::WaylandOutput( wl_output* output )
: m_output( output )
, m_scale( 1 )
{
static constexpr wl_output_listener listener = {
.geometry = Method( Geometry ),
.mode = Method( Mode ),
.done = Method( Done ),
.scale = Method( Scale ),
.name = Method( Name ),
.description = Method( Description )
};
wl_output_add_listener( m_output, &listener, this );
}
WaylandOutput::~WaylandOutput()
{
wl_output_destroy( m_output );
}
void WaylandOutput::Geometry( wl_output* output, int32_t x, int32_t y, int32_t phys_w, int32_t phys_h, int32_t subpixel, const char* make, const char* model, int32_t transform )
{
}
void WaylandOutput::Mode( wl_output* output, uint32_t flags, int32_t width, int32_t height, int32_t refresh )
{
}
void WaylandOutput::Done( wl_output* output )
{
}
void WaylandOutput::Scale( wl_output* output, int32_t scale )
{
m_scale = scale;
}
void WaylandOutput::Name( wl_output* output, const char* name )
{
}
void WaylandOutput::Description( wl_output* output, const char* description )
{
}

View File

@ -0,0 +1,27 @@
#ifndef __WAYLANDOUTPUT_HPP__
#define __WAYLANDOUTPUT_HPP__
#include <wayland-client.h>
class WaylandOutput
{
public:
WaylandOutput( wl_output* output );
~WaylandOutput();
int32_t Scale() const { return m_scale; }
wl_output* Output() const { return m_output; }
private:
void Geometry( wl_output* output, int32_t x, int32_t y, int32_t phys_w, int32_t phys_h, int32_t subpixel, const char* make, const char* model, int32_t transform );
void Mode( wl_output* output, uint32_t flags, int32_t width, int32_t height, int32_t refresh );
void Done( wl_output* output );
void Scale( wl_output* output, int32_t scale );
void Name( wl_output* output, const char* name );
void Description( wl_output* output, const char* description );
wl_output* m_output;
int32_t m_scale;
};
#endif

View File

@ -0,0 +1,115 @@
#include <linux/input-event-codes.h>
#include "../../imgui/imgui.h"
#include "WaylandMethod.hpp"
#include "WaylandPointer.hpp"
WaylandPointer::WaylandPointer( wl_pointer* pointer, int32_t& scale, std::function<void(wl_pointer*, uint32_t)> setCursor )
: m_pointer( pointer )
, m_wheel( false )
, m_wheelX( 0 )
, m_wheelY( 0 )
, m_scale( scale )
, m_setCursor( std::move( setCursor ) )
{
static constexpr wl_pointer_listener listener = {
.enter = Method( Enter ),
.leave = Method( Leave ),
.motion = Method( Motion ),
.button = Method( Button ),
.axis = Method( Axis ),
.frame = Method( Frame ),
.axis_source = Method( AxisSource ),
.axis_stop = Method( AxisStop ),
.axis_discrete = Method( AxisDiscrete ),
.axis_value120 = Method( AxisValue120 ),
.axis_relative_direction = Method( AxisRelativeDirection )
};
wl_pointer_add_listener( m_pointer, &listener, this );
}
WaylandPointer::~WaylandPointer()
{
wl_pointer_destroy( m_pointer );
}
void WaylandPointer::Enter( wl_pointer* pointer, uint32_t serial, wl_surface* surf, wl_fixed_t sx, wl_fixed_t sy )
{
m_setCursor( pointer, serial );
ImGuiIO& io = ImGui::GetIO();
io.AddMousePosEvent( wl_fixed_to_double( sx * m_scale ), wl_fixed_to_double( sy * m_scale ) );
}
void WaylandPointer::Leave( wl_pointer* pointer, uint32_t serial, wl_surface* surf )
{
ImGuiIO& io = ImGui::GetIO();
io.AddMousePosEvent( -FLT_MAX, -FLT_MAX );
}
void WaylandPointer::Motion( wl_pointer* pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy )
{
ImGuiIO& io = ImGui::GetIO();
io.AddMousePosEvent( wl_fixed_to_double( sx * m_scale ), wl_fixed_to_double( sy * m_scale ) );
}
void WaylandPointer::Button( wl_pointer* pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state )
{
int b;
switch( button )
{
case BTN_LEFT: b = 0; break;
case BTN_MIDDLE: b = 2; break;
case BTN_RIGHT: b = 1; break;
default: return;
}
ImGuiIO& io = ImGui::GetIO();
io.AddMouseButtonEvent( b, state == WL_POINTER_BUTTON_STATE_PRESSED );
}
void WaylandPointer::Axis( wl_pointer* pointer, uint32_t time, uint32_t axis, wl_fixed_t value )
{
m_wheel = true;
if( axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL )
{
m_wheelX -= value;
}
else
{
m_wheelY -= value;
}
}
void WaylandPointer::Frame( wl_pointer* pointer )
{
if( m_wheel )
{
m_wheel = false;
m_wheelX /= 8;
m_wheelY /= 8;
ImGuiIO& io = ImGui::GetIO();
io.AddMouseWheelEvent( wl_fixed_to_double( m_wheelX ), wl_fixed_to_double( m_wheelY ) );
m_wheelX = m_wheelY = 0;
}
}
void WaylandPointer::AxisSource( wl_pointer* pointer, uint32_t source )
{
}
void WaylandPointer::AxisStop( wl_pointer* pointer, uint32_t time, uint32_t axis )
{
}
void WaylandPointer::AxisDiscrete( wl_pointer* pointer, uint32_t axis, int32_t discrete )
{
}
void WaylandPointer::AxisValue120( wl_pointer* pointer, uint32_t axis, int32_t value120 )
{
}
void WaylandPointer::AxisRelativeDirection( wl_pointer* pointer, uint32_t axis, uint32_t direction )
{
}

View File

@ -0,0 +1,38 @@
#ifndef __WAYLANDPOINTER_HPP__
#define __WAYLANDPOINTER_HPP__
#include <functional>
#include <wayland-client.h>
class WaylandPointer
{
public:
WaylandPointer( wl_pointer* pointer, int32_t& scale, std::function<void(wl_pointer*, uint32_t)> setCursor );
~WaylandPointer();
[[nodiscard]] wl_pointer* GetPointer() const { return m_pointer; }
private:
void Enter( wl_pointer* pointer, uint32_t serial, wl_surface* surf, wl_fixed_t sx, wl_fixed_t sy );
void Leave( wl_pointer* pointer, uint32_t serial, wl_surface* surf );
void Motion( wl_pointer* pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy );
void Button( wl_pointer* pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state );
void Axis( wl_pointer* pointer, uint32_t time, uint32_t axis, wl_fixed_t value );
void Frame( wl_pointer* pointer );
void AxisSource( wl_pointer* pointer, uint32_t source );
void AxisStop( wl_pointer* pointer, uint32_t time, uint32_t axis );
void AxisDiscrete( wl_pointer* pointer, uint32_t axis, int32_t discrete );
void AxisValue120( wl_pointer* pointer, uint32_t axis, int32_t value120 );
void AxisRelativeDirection( wl_pointer* pointer, uint32_t axis, uint32_t direction );
wl_pointer* m_pointer;
bool m_wheel;
wl_fixed_t m_wheelX;
wl_fixed_t m_wheelY;
int32_t& m_scale;
std::function<void(wl_pointer*, uint32_t)> m_setCursor;
};
#endif

View File

@ -0,0 +1,23 @@
#ifndef __WAYLANDREGISTRY_HPP__
#define __WAYLANDREGISTRY_HPP__
#include <stdio.h>
#include <stdlib.h>
#include <wayland-client.h>
template<typename T>
static inline T* RegistryBindImpl( wl_registry* reg, uint32_t name, const char* interfaceName, const wl_interface* interface, uint32_t version, uint32_t versionMin = 1, uint32_t versionMax = 1 )
{
if( version < versionMin )
{
printf( "Wayland interface %s version %u is too old (minimum required is %u)\n", interfaceName, version, versionMin );
abort();
}
if( version > versionMax ) version = versionMax;
return (T*)wl_registry_bind( reg, name, interface, version );
}
// Two optional parameters: versionMin and versionMax.
#define RegistryBind( type, ... ) RegistryBindImpl<type>( reg, name, interface, &type ## _interface, version, ##__VA_ARGS__ )
#endif

View File

@ -0,0 +1,288 @@
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <stdio.h>
#include <stdlib.h>
#include <wayland-cursor.h>
#include "../../server/TracyImGui.hpp"
#include "WaylandMethod.hpp"
#include "WaylandWindow.hpp"
namespace {
void Check( bool condition, const char* msg )
{
if( !condition )
{
fprintf( stderr, "%s\n", msg );
abort();
}
}
}
WaylandWindow::WaylandWindow( const WaylandWindowParams& p )
: m_surface( wl_compositor_create_surface( p.display.GetCompositor() ) )
, m_eglWindow( wl_egl_window_create( m_surface, p.winPos.w, p.winPos.h ) )
, m_xdgSurface( xdg_wm_base_get_xdg_surface( p.display.GetWmBase(), m_surface ) )
, m_xdgToplevel( xdg_surface_get_toplevel( m_xdgSurface ) )
, m_xdgToplevelDecoration( nullptr )
, m_cursorTheme( nullptr )
, m_cursorSurf( nullptr )
, m_display( p.display )
, m_winPos( p.winPos )
, m_running( p.running )
, m_scale( p.scale )
, m_prevScale( p.scale )
, m_width( p.winPos.w )
, m_height( p.winPos.h )
, m_activationToken( nullptr )
{
static constexpr wl_surface_listener surfaceListener = {
.enter = Method( Enter ),
.leave = Method( Leave )
};
wl_surface_add_listener( m_surface, &surfaceListener, this );
static constexpr xdg_surface_listener xdgSurfaceListener = {
.configure = Method( XdgSurfaceConfigure ),
};
xdg_surface_add_listener( m_xdgSurface, &xdgSurfaceListener, this );
static constexpr xdg_toplevel_listener toplevelListener = {
.configure = Method( XdgToplevelConfigure ),
.close = Method( XdgToplevelClose )
};
xdg_toplevel_add_listener( m_xdgToplevel, &toplevelListener, this );
xdg_toplevel_set_title( m_xdgToplevel, p.title );
xdg_toplevel_set_app_id( m_xdgToplevel, "tracy" );
if( p.display.GetDecorationManager() )
{
m_xdgToplevelDecoration = zxdg_decoration_manager_v1_get_toplevel_decoration( p.display.GetDecorationManager(), m_xdgToplevel );
static constexpr zxdg_toplevel_decoration_v1_listener decorationListener = {
.configure = Method( DecorationConfigure )
};
zxdg_toplevel_decoration_v1_add_listener( m_xdgToplevelDecoration, &decorationListener, this );
zxdg_toplevel_decoration_v1_set_mode( m_xdgToplevelDecoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE );
}
constexpr EGLint eglConfigAttrib[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_NONE
};
m_eglDpy = eglGetPlatformDisplay( EGL_PLATFORM_WAYLAND_KHR, p.display.GetDisplay(), nullptr );
auto res = eglInitialize( m_eglDpy, nullptr, nullptr );
Check( res == EGL_TRUE, "Cannot initialize EGL!" );
EGLint count;
EGLConfig eglConfig;
res = eglChooseConfig( m_eglDpy, eglConfigAttrib, &eglConfig, 1, &count );
Check( res == EGL_TRUE && count == 1, "No suitable EGL config found!" );
res = eglBindAPI( EGL_OPENGL_API );
Check( res == EGL_TRUE, "Cannot use OpenGL through EGL!" );
m_eglSurf = eglCreatePlatformWindowSurface( m_eglDpy, eglConfig, m_eglWindow, nullptr );
constexpr EGLint eglCtxAttrib[] = {
EGL_CONTEXT_MAJOR_VERSION, 3,
EGL_CONTEXT_MINOR_VERSION, 2,
EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
EGL_NONE
};
m_eglCtx = eglCreateContext( m_eglDpy, eglConfig, EGL_NO_CONTEXT, eglCtxAttrib );
Check( m_eglCtx != EGL_NO_CONTEXT, "Cannot create OpenGL 3.2 Core Profile context!" );
res = eglMakeCurrent( m_eglDpy, m_eglSurf, m_eglSurf, m_eglCtx );
Check( res == EGL_TRUE, "Cannot make EGL context current!" );
SetupCursor();
}
WaylandWindow::~WaylandWindow()
{
if( m_activationToken ) xdg_activation_token_v1_destroy( m_activationToken );
eglMakeCurrent( m_eglDpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
eglDestroySurface( m_eglDpy, m_eglSurf );
eglDestroyContext( m_eglDpy, m_eglCtx );
eglTerminate( m_eglDpy );
if( m_xdgToplevelDecoration ) zxdg_toplevel_decoration_v1_destroy( m_xdgToplevelDecoration );
xdg_toplevel_destroy( m_xdgToplevel );
xdg_surface_destroy( m_xdgSurface );
wl_egl_window_destroy( m_eglWindow );
wl_surface_destroy( m_surface );
wl_surface_destroy( m_cursorSurf );
wl_cursor_theme_destroy( m_cursorTheme );
}
void WaylandWindow::SetTitle( const char* title )
{
xdg_toplevel_set_title( m_xdgToplevel, title );
}
void WaylandWindow::Show()
{
wl_surface_commit( m_surface );
}
void WaylandWindow::Attention()
{
if( !m_display.GetActivation() ) return;
if( m_activationToken ) return;
static constexpr xdg_activation_token_v1_listener listener = {
.done = Method( TokenDone )
};
m_activationToken = xdg_activation_v1_get_activation_token( m_display.GetActivation() );
xdg_activation_token_v1_set_surface( m_activationToken, m_surface );
xdg_activation_token_v1_commit( m_activationToken );
xdg_activation_token_v1_add_listener( m_activationToken, &listener, this );
}
void WaylandWindow::NewFrame()
{
if( m_prevScale != m_scale )
{
m_prevScale = m_scale;
SetupCursor();
wl_surface_set_buffer_scale( m_surface, m_scale );
wl_surface_commit( m_surface );
}
if( !m_winPos.maximize )
{
m_winPos.w = m_width;
m_winPos.h = m_height;
}
}
void WaylandWindow::Present()
{
eglSwapBuffers( m_eglDpy, m_eglSurf );
}
wl_surface* WaylandWindow::GetCursor( int32_t& x, int32_t& y ) const
{
x = m_cursorX;
y = m_cursorY;
return m_cursorSurf;
}
void WaylandWindow::Enter( struct wl_surface* surface, struct wl_output* output )
{
uint32_t id;
auto out = m_display.GetOutput( output, id );
if( !out ) return;
m_outputs.emplace( id );
const auto outScale = out->Scale();
if( outScale > m_scale ) m_scale = outScale;
}
void WaylandWindow::Leave( struct wl_surface* surface, struct wl_output* output )
{
uint32_t id;
auto out = m_display.GetOutput( output, id );
assert( out );
m_outputs.erase( id );
if( out->Scale() == m_scale )
{
int32_t scale = 1;
for( auto& id : m_outputs )
{
auto out = m_display.GetOutput( id );
if( out->Scale() > scale ) scale = out->Scale();
}
if( scale != m_scale ) m_scale = scale;
}
}
void WaylandWindow::XdgSurfaceConfigure( struct xdg_surface *xdg_surface, uint32_t serial )
{
tracy::s_wasActive = true;
xdg_surface_ack_configure( xdg_surface, serial );
}
void WaylandWindow::XdgToplevelConfigure( struct xdg_toplevel* toplevel, int32_t width, int32_t height, struct wl_array* states )
{
if( width == 0 || height == 0 ) return;
bool max = false;
auto data = (uint32_t*)states->data;
for( size_t i = 0; i < states->size / sizeof(uint32_t); i++ )
{
if( data[i] == XDG_TOPLEVEL_STATE_MAXIMIZED )
{
max = true;
break;
}
}
m_winPos.maximize = max;
width *= m_scale;
height *= m_scale;
if( m_width != width || m_height != height )
{
m_width = width;
m_height = height;
wl_egl_window_resize( m_eglWindow, width, height, 0, 0 );
wl_surface_commit( m_surface );
}
}
void WaylandWindow::XdgToplevelClose( struct xdg_toplevel* toplevel )
{
m_running = false;
}
void WaylandWindow::DecorationConfigure( zxdg_toplevel_decoration_v1* tldec, uint32_t mode )
{
}
void WaylandWindow::TokenDone( xdg_activation_token_v1* activationToken, const char* token )
{
xdg_activation_v1_activate( m_display.GetActivation(), token, m_surface );
xdg_activation_token_v1_destroy( activationToken );
m_activationToken = nullptr;
}
void WaylandWindow::SetupCursor()
{
auto env_xcursor_theme = getenv( "XCURSOR_THEME" );
auto env_xcursor_size = getenv( "XCURSOR_SIZE" );
int size = env_xcursor_size ? atoi( env_xcursor_size ) : 24;
size *= m_scale;
if( m_cursorSurf ) wl_surface_destroy( m_cursorSurf );
if( m_cursorTheme ) wl_cursor_theme_destroy( m_cursorTheme );
m_cursorTheme = wl_cursor_theme_load( env_xcursor_theme, size, m_display.GetShm() );
auto cursor = wl_cursor_theme_get_cursor( m_cursorTheme, "left_ptr" );
m_cursorSurf = wl_compositor_create_surface( m_display.GetCompositor() );
if( m_scale != 1 ) wl_surface_set_buffer_scale( m_cursorSurf, m_scale );
wl_surface_attach( m_cursorSurf, wl_cursor_image_get_buffer( cursor->images[0] ), 0, 0 );
wl_surface_commit( m_cursorSurf );
m_cursorX = cursor->images[0]->hotspot_x / m_scale;
m_cursorY = cursor->images[0]->hotspot_y / m_scale;
auto pointer = m_display.GetPointer();
if( pointer ) wl_pointer_set_cursor( pointer, 0, m_cursorSurf, m_cursorX, m_cursorY );
}

View File

@ -0,0 +1,86 @@
#ifndef __WAYLANDWINDOW_HPP__
#define __WAYLANDWINDOW_HPP__
#include <EGL/egl.h>
#include <functional>
#include <stdint.h>
#include <unordered_set>
#include <wayland-client.h>
#include <wayland-egl.h>
#include "wayland/xdg-activation.h"
#include "wayland/xdg-decoration.h"
#include "wayland/xdg-shell.h"
#include "WaylandDisplay.hpp"
#include "WindowPosition.hpp"
struct WaylandWindowParams
{
WaylandDisplay& display;
const char* title;
WindowPosition& winPos;
bool& running;
int32_t& scale;
};
class WaylandWindow
{
public:
WaylandWindow( const WaylandWindowParams& p );
~WaylandWindow();
void SetTitle( const char* title );
void Show();
void Attention();
void NewFrame();
void Present();
[[nodiscard]] int Width() const { return m_width; }
[[nodiscard]] int Height() const { return m_height; }
[[nodiscard]] wl_surface* GetCursor( int32_t& x, int32_t& y ) const;
private:
void Enter( struct wl_surface* surface, struct wl_output* output );
void Leave( struct wl_surface* surface, struct wl_output* output );
void XdgSurfaceConfigure( struct xdg_surface *xdg_surface, uint32_t serial );
void XdgToplevelConfigure( struct xdg_toplevel* toplevel, int32_t width, int32_t height, struct wl_array* states );
void XdgToplevelClose( struct xdg_toplevel* toplevel );
void DecorationConfigure( zxdg_toplevel_decoration_v1* tldec, uint32_t mode );
void TokenDone( xdg_activation_token_v1* activationToken, const char* token );
void SetupCursor();
wl_surface* m_surface;
wl_egl_window* m_eglWindow;
xdg_surface* m_xdgSurface;
xdg_toplevel* m_xdgToplevel;
zxdg_toplevel_decoration_v1* m_xdgToplevelDecoration;
EGLDisplay m_eglDpy;
EGLContext m_eglCtx;
EGLSurface m_eglSurf;
struct wl_cursor_theme* m_cursorTheme;
struct wl_surface* m_cursorSurf;
int32_t m_cursorX, m_cursorY;
WaylandDisplay& m_display;
WindowPosition& m_winPos;
bool& m_running;
int32_t& m_scale;
int32_t m_prevScale;
int m_width, m_height;
std::unordered_set<uint32_t> m_outputs;
xdg_activation_token_v1* m_activationToken;
};
#endif