This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaylandRegistry.hpp
239 lines (202 loc) · 5.51 KB
/
WaylandRegistry.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#ifndef _WAYLANDREGISTRY_H
#define _WAYLANDREGISTRY_H
#include <iostream>
#include <wayland-client.h>
#include "xdg-shell-client-protocol.h"
#include "wlr-layer-shell-unstable-v1.h"
class WaylandRegistry
{
private:
WaylandRegistry();
public:
struct wl_display *display;
struct wl_registry *registry;
WaylandRegistry(struct wl_display *display);
virtual void global(void *data, struct wl_registry *wl_registry,
uint32_t name, const char *interface, uint32_t version);
void round_trip()
{
wl_display_dispatch(this->display);
wl_display_roundtrip(this->display);
}
virtual ~WaylandRegistry()
{
if (this->registry != nullptr)
{
// std::cout << "Removing wl_registry\n";
wl_registry_destroy(this->registry);
}
}
};
template <class T>
class WaylandRegistryWrapper
{
public:
T value;
T getValue()
{
return value;
}
void setValue(T value)
{
this->value = value;
}
};
class WaylandRegistryGeneric
{
public:
struct wl_display *display;
struct wl_registry *registry;
WaylandRegistryGeneric()
{
}
};
class WaylandXDGRegistry : public WaylandRegistry
{
private:
WaylandXDGRegistry();
public:
WaylandXDGRegistry(struct wl_display *display) : WaylandRegistry(display){};
struct wl_compositor *wl_compositor = nullptr;
struct wl_shm *wl_shm = nullptr;
struct wl_seat *wl_seat = nullptr;
struct xdg_wm_base *xdg_wm_base = nullptr;
struct zwlr_layer_shell_v1 *zwlr_layer_shell_v1 = nullptr;
virtual void global(void *data, struct wl_registry *wl_registry,
uint32_t name, const char *interface, uint32_t version);
virtual ~WaylandXDGRegistry()
{
// std::cout << "Removing wl_compositor xdg_wm_base zwlr_layer_shell_v1\n";
if (this->wl_compositor != nullptr)
{
wl_compositor_destroy(this->wl_compositor);
}
if (this->wl_seat != nullptr)
{
wl_seat_destroy(this->wl_seat);
}
if (this->xdg_wm_base != nullptr)
{
xdg_wm_base_destroy(this->xdg_wm_base);
}
if (this->zwlr_layer_shell_v1 != nullptr)
{
zwlr_layer_shell_v1_destroy(this->zwlr_layer_shell_v1);
}
}
};
class WaylandXDG
{
public:
WaylandXDGRegistry *registry;
WaylandXDG(struct wl_display *display);
~WaylandXDG()
{
if (this->registry != nullptr)
{
delete registry;
}
}
};
class WaylandSurface
{
public:
struct wl_surface *wl_surface;
WaylandSurface(WaylandXDGRegistry *registry)
{
this->wl_surface = wl_compositor_create_surface(registry->wl_compositor);
}
~WaylandSurface()
{
if (this->wl_surface != nullptr)
{
// std::cout << "Removing wl_surface\n";
wl_surface_destroy(this->wl_surface);
}
}
};
class WaylandLayerShellSurface
{
public:
WaylandLayerShellSurface(WaylandXDGRegistry);
};
class WaylandXDGSurface : public WaylandSurface
{
public:
struct xdg_surface *xdg_surface;
WaylandXDGSurface(WaylandXDGRegistry *registry);
struct xdg_toplevel *get_toplevel()
{
return xdg_surface_get_toplevel(this->xdg_surface);
}
void configure(struct xdg_surface *xdg_surface, uint32_t serial)
{
xdg_surface_ack_configure(xdg_surface, serial);
}
~WaylandXDGSurface()
{
if (this->xdg_surface != nullptr)
{
// std::cout << "Removing xdg_surface\n";
xdg_surface_destroy(this->xdg_surface);
}
}
};
class WaylandXDGSurfaceToplevel : public WaylandXDGSurface
{
public:
struct xdg_toplevel *xdg_toplevel;
int32_t width;
int32_t height;
bool resized;
WaylandXDGSurfaceToplevel(WaylandXDGRegistry *registry);
void set_title(const char *str)
{
xdg_toplevel_set_title(this->xdg_toplevel, str);
}
bool CheckIfWlArrayHasValue(struct wl_array *wl_array, uint32_t value)
{
// wl_array_for_each has a bug in upstream. It tries to assign void* to
// uint32_t *, which is not allowed in C++. Explicit cast should be
// performed. In other words, one just cannot assign void * to other pointer
// type implicitly in C++ as in C. We can't modify wayland-util.h, because
// it is fetched with gclient sync. Thus, use own loop.
uint32_t *data = reinterpret_cast<uint32_t *>(wl_array->data);
size_t array_size = wl_array->size / sizeof(uint32_t);
for (size_t i = 0; i < array_size; i++)
{
if (data[i] == value)
return true;
}
return false;
}
void configure(struct xdg_toplevel *xdg_toplevel,
int32_t width,
int32_t height,
struct wl_array *states)
{
printf("XDGTOPLEVEL configure: %dx%d\n", width, height);
if (CheckIfWlArrayHasValue(states, XDG_TOPLEVEL_SHOW_WINDOW_MENU))
{
if (width > 0 && height > 0)
{
this->width = width;
this->height = height;
this->resized = true;
}
}
}
void commit()
{
wl_surface_commit(this->wl_surface);
}
~WaylandXDGSurfaceToplevel()
{
if (this->xdg_toplevel != nullptr)
{
// std::cout << "Removing xdg_toplevel\n";
xdg_toplevel_destroy(this->xdg_toplevel);
}
}
};
#endif