Skip to content

Commit 4e579ea

Browse files
fixed multi window use case
1 parent 65b91fb commit 4e579ea

File tree

2 files changed

+284
-221
lines changed

2 files changed

+284
-221
lines changed

include/vsg/platform/wayland/Wayland_Window.h

Lines changed: 122 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,129 @@
44
#include <vsg/ui/KeyEvent.h>
55

66
#include <wayland-client.h>
7-
#include <wayland-egl.h>
87
#include <wayland-cursor.h>
8+
#include <wayland-egl.h>
99
#include <xkbcommon/xkbcommon.h>
1010

11+
#include <vector>
12+
#include <vsg/core/Objects.h>
1113
#include <vsg/platform/wayland/wayland-xdg-shell-client-protocol.h>
1214
#include <vsg/platform/wayland/xdg-decoration-client.h>
1315
#include <vsg/ui/PointerEvent.h>
1416
#include <vulkan/vulkan_wayland.h>
1517

1618
namespace vsgWayland
1719
{
20+
class Wayland_Window;
1821

19-
class KeyboardMap : public vsg::Object
22+
/*
23+
* This is similar to xcb surface, but we store the pointer to window
24+
* which will be used by wayland api to access window object
25+
*/
26+
class Wayland_surface : public vsg::Surface
2027
{
2128
public:
22-
KeyboardMap();
23-
24-
using KeycodeModifier = std::pair<uint32_t, uint32_t>;
25-
using KeycodeMap = std::map<KeycodeModifier, vsg::KeySymbol>;
26-
27-
void add(uint32_t keycode, uint32_t modifier, vsg::KeySymbol key);
28-
29-
void add(uint32_t keycode, std::initializer_list<std::pair<uint32_t, vsg::KeySymbol>> combinations);
30-
31-
vsg::KeySymbol getKeySymbol(uint32_t keycode, uint32_t modifier);
32-
vsg::KeyModifier getKeyModifier(vsg::KeySymbol keySym, uint32_t modifier, bool pressed);
29+
Wayland_surface(vsg::Instance* instance, wl_display* wlDisplay, wl_surface* wlSurface);
3330

3431
protected:
35-
KeycodeMap _keycodeMap;
36-
uint32_t _modifierMask;
32+
Wayland_Window* windowData;
3733
};
3834

39-
class Wayland_surface : public vsg::Surface
35+
/*
36+
* This is the object which holds wayland session infomration which is shared across all the windows
37+
* It uses global static object which will be referred by all the windows
38+
* Here we reuse vsg::Objects which will add the Wayland_Windows under it
39+
* the currentWindow member will track the actie window
40+
*/
41+
class WaylandRegistryState : public vsg::Inherit<vsg::Objects, WaylandRegistryState>
4042
{
43+
friend class Wayland_Window;
44+
4145
public:
42-
Wayland_surface(vsg::Instance* instance, wl_display* wlDisplay, wl_surface* wlSurface);
46+
WaylandRegistryState(const WaylandRegistryState&) = delete;
47+
WaylandRegistryState& operator=(const WaylandRegistryState&) = delete;
48+
static WaylandRegistryState* initOrSetState(Wayland_Window* window); // hook for initializing it or adding a new window to it
49+
~WaylandRegistryState();
50+
51+
/*
52+
* The below declerations are the listeners used by wayland, for some I used empty decleration
53+
* to avoid warnings, these can be implemented as per future need
54+
*/
55+
static void keymapEvent(void* data, wl_keyboard* wl_keyboard, uint32_t format, int32_t fd, uint32_t size);
56+
static void kbd_enter_event(void* data, struct wl_keyboard* wl_keyboard, uint32_t serial, struct wl_surface* surface, struct wl_array* keys);
57+
static void kbd_leave_event(void* data, struct wl_keyboard* wl_keyboard, uint32_t serial, struct wl_surface* surface){};
58+
static void kbd_key_event(void* data, struct wl_keyboard* wl_keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state);
59+
static void kbd_modifier_event(void* data, struct wl_keyboard* wl_keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group);
60+
static void kbd_repeat_event(void* data, wl_keyboard* wl_keyboard, int rate, int delay){};
61+
constexpr static struct wl_keyboard_listener wl_keyboard_listener = {
62+
.keymap = keymapEvent,
63+
.enter = kbd_enter_event,
64+
.leave = kbd_leave_event,
65+
.key = kbd_key_event,
66+
.modifiers = kbd_modifier_event,
67+
.repeat_info = kbd_repeat_event};
68+
69+
static void pointer_enter(void* data, struct wl_pointer* pointer, uint32_t serial, struct wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y);
70+
static void pointer_leave(void* data, struct wl_pointer* pointer, uint32_t serial, struct wl_surface* surface){};
71+
static void pointer_motion(void* data, struct wl_pointer* pointer, uint32_t time, wl_fixed_t x, wl_fixed_t y);
72+
static void pointer_button(void* data, struct wl_pointer* pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state);
73+
static void pointer_axis(void* data, struct wl_pointer* pointer, uint32_t time, uint32_t axis, wl_fixed_t value);
74+
static void pointer_frame(void* data, wl_pointer* wl_pointer){};
75+
static void pointer_axis_source(void* data, wl_pointer* wl_pointer, uint axis_source){};
76+
static void pointer_axis_stop(void* data, wl_pointer* wl_pointer, uint time, uint axis){};
77+
static void pointer_axis_discrete(void* data, wl_pointer* wl_pointer, uint axis, int discrete){};
78+
constexpr static struct wl_pointer_listener pointer_listener = {
79+
.enter = pointer_enter,
80+
.leave = pointer_leave,
81+
.motion = pointer_motion,
82+
.button = pointer_button,
83+
.axis = pointer_axis,
84+
.frame = pointer_frame,
85+
.axis_source = pointer_axis_source,
86+
.axis_stop = pointer_axis_stop,
87+
.axis_discrete = pointer_axis_discrete};
88+
89+
static void seat_capabilities(void* data, struct wl_seat* seat, uint32_t capabilities);
90+
static void seat_name(void* data, wl_seat* wl_seat, const char* name){};
91+
constexpr static struct wl_seat_listener seat_listener = {
92+
.capabilities = seat_capabilities,
93+
.name = seat_name};
94+
95+
static void registry_add_object(void* data, struct wl_registry* registry, uint32_t id, const char* interface, uint32_t version);
96+
static void registry_remove_object(void* data, struct wl_registry* registry, uint32_t id){};
97+
constexpr static struct wl_registry_listener registry_listener = {
98+
.global = registry_add_object,
99+
.global_remove = registry_remove_object};
100+
101+
protected:
102+
struct wl_display* _wlDisplay = nullptr;
103+
struct wl_registry* _wlRegistry = nullptr;
104+
struct wl_compositor* _wlCompositor = nullptr;
105+
struct wl_seat* _seat = nullptr;
106+
struct wl_shm* _shm = nullptr;
107+
struct xdg_wm_base* _xdgWmBase = nullptr;
108+
struct zxdg_decoration_manager_v1* _decorationManager = nullptr;
109+
struct wl_cursor_theme* _cursorTheme = nullptr;
110+
struct xkb_state* _xkb_state;
111+
struct xkb_context* _xkb_context;
112+
struct xkb_keymap* _xkb_keymap;
113+
struct wl_surface* _cursorSurface = nullptr;
114+
uint16_t maskButtons;
115+
Wayland_Window* currentWindow;
116+
static inline WaylandRegistryState* state;
117+
static inline bool initialized;
118+
119+
WaylandRegistryState(){}; //empty initializer needed for initializing from the initOrSetState
43120
};
44121

122+
/*
123+
* The Wayland_Window class can access members of WaylandStateRegistry class and vice versa
124+
*
125+
*/
45126
class Wayland_Window : public vsg::Inherit<vsg::Window, Wayland_Window>
46127
{
128+
friend class WaylandRegistryState;
129+
47130
public:
48131
Wayland_Window(vsg::ref_ptr<vsg::WindowTraits> traits);
49132
Wayland_Window() = delete;
@@ -62,102 +145,52 @@ namespace vsgWayland
62145
bool pollEvents(vsg::UIEvents& events) override;
63146

64147
void resize() override;
148+
65149
protected:
66150
~Wayland_Window();
67-
151+
static WaylandRegistryState registryStateRef;
68152
void _initSurface() override;
69153

70-
static void keymapEvent(void* data, wl_keyboard *wl_keyboard, uint32_t format, int32_t fd, uint32_t size);
71-
static void kbd_enter_event(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys);
72-
static void kbd_leave_event(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *surface);
73-
static void kbd_key_event(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state);
74-
static void kbd_modifier_event(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group);
75-
constexpr static struct wl_keyboard_listener wl_keyboard_listener = {
76-
.keymap = keymapEvent,
77-
.enter = kbd_enter_event,
78-
.leave = kbd_leave_event,
79-
.key = kbd_key_event,
80-
.modifiers = kbd_modifier_event
81-
};
82-
83-
static void xdg_surface_handle_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial);
154+
/*
155+
* The below declerations are the listeners used by wayland, for some I used empty decleration
156+
* to avoid warnings, these can be implemented as per future need
157+
*/
158+
static void xdg_surface_handle_configure(void* data, struct xdg_surface* xdg_surface, uint32_t serial);
84159
constexpr static struct xdg_surface_listener xdg_surface_listener = {
85160
.configure = xdg_surface_handle_configure,
86161
};
87162

88-
static void xdg_toplevel_handle_configure(void *data, struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height, struct wl_array *states);
89-
static void xdg_toplevel_handle_close(void *data, struct xdg_toplevel *xdg_toplevel);
163+
static void xdg_toplevel_handle_configure(void* data, struct xdg_toplevel* xdg_toplevel, int32_t width, int32_t height, struct wl_array* states);
164+
static void xdg_toplevel_handle_close(void* data, struct xdg_toplevel* xdg_toplevel);
165+
static void xdg_toplevel_handle_configure_bounds(void* data, struct xdg_toplevel* xdg_toplevel, int32_t width, int32_t height){};
90166
constexpr static struct xdg_toplevel_listener xdg_toplevel_listener = {
91167
.configure = xdg_toplevel_handle_configure,
92168
.close = xdg_toplevel_handle_close,
93-
};
94-
95-
static void xdg_wm_base_ping(void *data, struct xdg_wm_base *xdg_wm_base, uint32_t serial);
96-
constexpr static struct xdg_wm_base_listener xdg_wm_base_listener = {
97-
.ping = xdg_wm_base_ping
98-
};
99-
100-
static void pointer_enter (void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t surface_x, wl_fixed_t surface_y);
101-
static void pointer_leave (void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface);
102-
static void pointer_motion (void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t x, wl_fixed_t y);
103-
static void pointer_button (void *data, struct wl_pointer *pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state);
104-
static void pointer_axis (void *data, struct wl_pointer *pointer, uint32_t time, uint32_t axis, wl_fixed_t value);
105-
constexpr static struct wl_pointer_listener pointer_listener = {
106-
&pointer_enter,
107-
&pointer_leave,
108-
&pointer_motion,
109-
&pointer_button,
110-
&pointer_axis
111-
};
112-
113-
static void seat_capabilities (void *data, struct wl_seat *seat, uint32_t capabilities);
114-
constexpr static struct wl_seat_listener seat_listener = {
115-
&seat_capabilities
116-
};
169+
.configure_bounds = xdg_toplevel_handle_configure_bounds};
117170

118-
static void registry_add_object(void *data, struct wl_registry *registry, uint32_t id, const char *interface, uint32_t version);
119-
static void registry_remove_object(void *data, struct wl_registry *registry, uint32_t id);
120-
constexpr static struct wl_registry_listener registry_listener = {
121-
&registry_add_object,
122-
&registry_remove_object
123-
};
124-
125-
static void shell_surface_ping (void *data, struct wl_shell_surface *shell_surface, uint32_t serial);
126-
static void shell_surface_configure(void *data, struct wl_shell_surface *shell_surface, uint32_t edges, int32_t width, int32_t height);
127-
static void shell_surface_popup_done (void *data, struct wl_shell_surface *shell_surface);
171+
static void shell_surface_ping(void* data, struct wl_shell_surface* shell_surface, uint32_t serial);
172+
static void shell_surface_configure(void* data, struct wl_shell_surface* shell_surface, uint32_t edges, int32_t width, int32_t height);
173+
static void shell_surface_popup_done(void* data, struct wl_shell_surface* shell_surface);
128174
constexpr static struct wl_shell_surface_listener shell_surface_listener = {
129-
&shell_surface_ping,
130-
&shell_surface_configure,
131-
&shell_surface_popup_done
132-
};
175+
.ping = shell_surface_ping,
176+
.configure = shell_surface_configure,
177+
.popup_done = shell_surface_popup_done};
178+
static void xdg_wm_base_ping(void* data, struct xdg_wm_base* xdg_wm_base, uint32_t serial);
179+
constexpr static struct xdg_wm_base_listener xdg_wm_base_listener = {
180+
.ping = xdg_wm_base_ping};
133181

134-
struct wl_display* _wlDisplay = nullptr;
135-
struct wl_registry* _wlRegistry = nullptr;
136-
struct wl_compositor* _wlCompositor = nullptr;
137-
struct wl_seat* _seat = nullptr;
138-
struct wl_shm* _shm = nullptr;
139-
struct wl_cursor_theme* _cursorTheme = nullptr;
140-
struct wl_surface* _cursorSurface = nullptr;
141-
struct zxdg_decoration_manager_v1* _decorationManager = nullptr;
142-
struct xdg_wm_base* _xdgWmBase = nullptr;
143-
//struct wl_subcompositor *subcompositor;
144182
struct wl_surface* _wlSurface = nullptr;
145183
struct xdg_surface* _xdgSurface = nullptr;
146184
struct xdg_toplevel* _xdgToplevel = nullptr;
147185
struct wl_surface* _currentSurface = nullptr;
148186
struct zxdg_toplevel_decoration_v1* _topDecoration = nullptr;
149-
struct xkb_state *_xkb_state;
150-
struct xkb_context *_xkb_context;
151-
struct xkb_keymap *_xkb_keymap;
152-
187+
WaylandRegistryState* _state;
153188
int _width = 256;
154189
int _height = 256;
155190
int cursor_x = -1;
156191
int cursor_y = -1;
157192
bool _resize = false;
158-
uint16_t maskButtons;
159-
160-
vsg::ref_ptr<KeyboardMap> _keyboard;
193+
std::string _windowTitle;
161194
};
162195
} // namespace vsgWayland
163196

0 commit comments

Comments
 (0)