_wayland toplevel drag WIP

This commit is contained in:
2025-01-07 17:57:32 +02:00
parent 4204e7f571
commit 75c44122c6
15 changed files with 4771 additions and 62 deletions

View File

@@ -1,5 +1,7 @@
// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
#include <wayland/xdg-shell.h>
#include <wayland/xdg-shell.h>
#if PLATFORM_SDL && PLATFORM_LINUX
#include "Engine/Platform/Platform.h"
@@ -10,6 +12,7 @@
#include "Engine/Profiler/ProfilerCPU.h"
#include "Engine/Core/Log.h"
#include "Engine/Core/Collections/Array.h"
#include "Engine/Core/Collections/Dictionary.h"
#include "Engine/Engine/CommandLine.h"
#include "Engine/Platform/WindowsManager.h"
#include "Engine/Platform/Linux/IncludeX11.h"
@@ -18,16 +21,26 @@
#include <SDL3/SDL_system.h>
#include <SDL3/SDL_hints.h>
#include <wayland/xdg-toplevel-drag-v1.h>
// Wayland
void WaylandRegistryGlobal(void* data, wl_registry *registry, uint32 id, const char* interface, uint32 version);
void WaylandRegistryGlobalRemove(void* data, wl_registry *registry, uint32 id);
Dictionary<wl_surface*, SDLWindow*> SurfaceToWindowMap;
uint32 LastPointerSerial = 0;
wl_display* WaylandDisplay = nullptr;
wl_registry_listener WaylandRegistryListener = { WaylandRegistryGlobal, WaylandRegistryGlobalRemove };
xdg_toplevel_drag_manager_v1* DragManager = nullptr;
wl_seat* WaylandSeat = nullptr;
wl_data_device_manager* WaylandDataDeviceManager = nullptr;
xdg_wm_base* WaylandXdgWmBase = nullptr;
// X11
Delegate<void*> LinuxPlatform::xEventReceived;
// Missing Wayland features:
// - Application icon (xdg-toplevel-icon-v1) https://github.com/libsdl-org/SDL/pull/9584
// - Window positioning and position tracking
// - Color picker (xdg-desktop-portal?) https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Screenshot.html
// -
@@ -844,11 +857,54 @@ int X11ErrorHandler(X11::Display* display, X11::XErrorEvent* event)
return 0;
}
extern wl_data_device_listener WaylandDataDeviceListener;
wl_data_device* dataDevice;
bool SDLPlatform::InitPlatform()
{
if (LinuxPlatform::Init())
return true;
#if false
if (!CommandLine::Options.Headless && strcmp(SDL_GetCurrentVideoDriver(), "wayland") == 0)
{
WaylandDisplay = (wl_display*)SDL_GetPointerProperty(SDL_GetGlobalProperties(), SDL_PROP_GLOBAL_VIDEO_WAYLAND_WL_DISPLAY_POINTER, nullptr);
wl_registry* registry = wl_display_get_registry(WaylandDisplay);
wl_registry_add_listener(registry, &WaylandRegistryListener, nullptr);
wl_display_roundtrip(WaylandDisplay);
}
#else
bool waylandRequested = !CommandLine::Options.X11 || CommandLine::Options.Wayland;
if (!CommandLine::Options.Headless && waylandRequested)
{
// Ignore in X11 session
String waylandDisplayEnv;
if (!GetEnvironmentVariable(String("WAYLAND_DISPLAY"), waylandDisplayEnv))
{
WaylandDisplay = wl_display_connect(nullptr);
if (WaylandDisplay != nullptr)
{
// We need to manage the wl_display and create the wl_data_device
// before SDL so we can receive drag-and-drop related events from compositor.
SDL_SetPointerProperty(SDL_GetGlobalProperties(), SDL_PROP_GLOBAL_VIDEO_WAYLAND_WL_DISPLAY_POINTER, WaylandDisplay);
wl_registry* registry = wl_display_get_registry(WaylandDisplay);
wl_registry_add_listener(registry, &WaylandRegistryListener, nullptr);
wl_display_roundtrip(WaylandDisplay);
dataDevice = wl_data_device_manager_get_data_device(WaylandDataDeviceManager, WaylandSeat);
wl_data_device_add_listener(dataDevice, &WaylandDataDeviceListener, nullptr);
wl_display_roundtrip(WaylandDisplay);
}
}
}
#endif
return false;
}
@@ -903,6 +959,10 @@ bool SDLPlatform::InitPlatformX11(void* display)
return false;
}
void SDLPlatform::Exit()
{
}
void* SDLPlatform::GetXDisplay()
{
return xDisplay;
@@ -933,4 +993,161 @@ bool SDLPlatform::UsesX11()
return xDisplay != nullptr;
}
void WaylandPointer_Enter(void *data,
struct wl_pointer *wl_pointer,
uint32_t serial,
struct wl_surface *surface,
wl_fixed_t surface_x,
wl_fixed_t surface_y)
{
/*SDLWindow* window;
if (!SurfaceToWindowMap.TryGet(surface, window))
return;
LastPointerWindow = window;
LastPointerPosition = Int2(surface_x, surface_y);*/
//LOG(Info, "WaylandPointerEnter serial:{}", serial);
LastPointerSerial = serial;
}
void WaylandPointer_Leave(void *data,
struct wl_pointer *wl_pointer,
uint32_t serial,
struct wl_surface *surface)
{
//LastPointerWindow = nullptr;
//LOG(Info, "WaylandPointerLeave serial:{}", serial);
LastPointerSerial = serial;
}
void WaylandPointer_Motion(void *data,
struct wl_pointer *wl_pointer,
uint32_t time,
wl_fixed_t surface_x,
wl_fixed_t surface_y)
{
//LOG(Info, "WaylandPointerMotion time:{}", time);
//LastPointerPosition = Int2(surface_x, surface_y);
}
void WaylandPointer_Button(void* data, wl_pointer* wl_pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state)
{
//LOG(Info, "WaylandPointerButton serial:{}", serial);
// HACK: We store the serial for upcoming drag-and-drop action even though we are
// not really performing the action during this specific button press event.
// SDL receives the same event which actually starts the DnD process.
LastPointerSerial = serial;
}
void WaylandPointer_Axis(void *data,
struct wl_pointer *wl_pointer,
uint32_t time,
uint32_t axis,
wl_fixed_t value)
{
//LOG(Info, "WaylandPointerAxis time:{}", time);
}
void WaylandPointer_Frame(void *data,
struct wl_pointer *wl_pointer)
{
//LOG(Info, "WaylandPointerFrame");
}
void WaylandPointer_AxisSource(void *data,
struct wl_pointer *wl_pointer,
uint32_t axis_source)
{
//LOG(Info, "WaylandPointerAxisSource");
}
void WaylandPointer_AxisStop(void *data,
struct wl_pointer *wl_pointer,
uint32_t time,
uint32_t axis)
{
//LOG(Info, "WaylandPointerAxisStop time:{}", time);
}
void WaylandPointer_AxisDiscrete(void *data,
struct wl_pointer *wl_pointer,
uint32_t axis,
int32_t discrete)
{
//LOG(Info, "WaylandPointerAxisDiscrete");
}
void WaylandPointer_AxisValue120(void *data,
struct wl_pointer *wl_pointer,
uint32_t axis,
int32_t value120)
{
//LOG(Info, "WaylandPointerAxisValue120");
}
void WaylandPointer_AxisRelativeDirection(void *data,
struct wl_pointer *wl_pointer,
uint32_t axis,
uint32_t direction)
{
//LOG(Info, "WaylandPointerAxisRelativeDirection");
}
wl_pointer_listener WaylandPointerListener =
{
WaylandPointer_Enter,
WaylandPointer_Leave,
WaylandPointer_Motion,
WaylandPointer_Button,
WaylandPointer_Axis,
WaylandPointer_Frame,
WaylandPointer_AxisSource,
WaylandPointer_AxisStop,
WaylandPointer_AxisDiscrete,
WaylandPointer_AxisValue120,
WaylandPointer_AxisRelativeDirection
};
wl_pointer* WaylandPointer = nullptr;
void SeatCapabilities(void* data, wl_seat* seat, uint32 capabilities)
{
if ((capabilities & wl_seat_capability::WL_SEAT_CAPABILITY_POINTER) != 0)
{
WaylandPointer = wl_seat_get_pointer(seat);
wl_pointer_add_listener(WaylandPointer, &WaylandPointerListener, nullptr);
}
}
void SeatName(void* data, wl_seat* seat, const char* name)
{
}
wl_seat_listener SeatListener = { SeatCapabilities, SeatName };
void WaylandRegistryGlobal(void* data, wl_registry *registry, uint32 id, const char* interface, uint32 version)
{
StringAnsi interfaceStr(interface);
LOG(Info, "WaylandRegistryGlobal id: {}, interface: {}", id, String(interface));
if (interfaceStr == "xdg_toplevel_drag_manager_v1")
DragManager = (xdg_toplevel_drag_manager_v1*)wl_registry_bind(registry, id, &xdg_toplevel_drag_manager_v1_interface, 1U);
else if (interfaceStr == "wl_seat")
{
WaylandSeat = (wl_seat*)wl_registry_bind(registry, id, &wl_seat_interface, Math::Min(9U, version));
wl_seat_add_listener(WaylandSeat, &SeatListener, nullptr);
}
else if (interfaceStr == "wl_data_device_manager")
WaylandDataDeviceManager = (wl_data_device_manager*)wl_registry_bind(registry, id, &wl_data_device_manager_interface, Math::Min(3U, version));
else if (interfaceStr == "xdg_wm_base")
WaylandXdgWmBase = (xdg_wm_base*)wl_registry_bind(registry, id, &xdg_wm_base_interface, Math::Min(6U, version));
}
void WaylandRegistryGlobalRemove(void* data, wl_registry *registry, uint32 id)
{
LOG(Info, "WaylandRegistryGlobalRemove id:{}", id);
}
#endif