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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
#include "wayland_platform.h"
#include <iostream>
#include <cstring>
#include <cstdint>
#ifndef USE_WAYLAND_STUB
#include <wayland-server-core.h>
extern "C" {
// Minimal wlroots declarations to avoid pulling C99-only headers into C++
struct wlr_backend;
struct wlr_renderer;
struct wlr_compositor;
struct wlr_seat;
struct wlr_xdg_shell;
// Logging
int wlr_log_init(int verbosity, void* callback);
// Backend
struct wlr_backend* wlr_backend_autocreate(struct wl_display* display, void* session);
bool wlr_backend_start(struct wlr_backend* backend);
void wlr_backend_destroy(struct wlr_backend* backend);
struct wlr_renderer* wlr_backend_get_renderer(struct wlr_backend* backend);
// Compositor and seat
struct wlr_compositor* wlr_compositor_create(struct wl_display* display, uint32_t version, struct wlr_renderer* renderer);
struct wlr_seat* wlr_seat_create(struct wl_display* display, const char* name);
// xdg-shell
struct wlr_xdg_shell* wlr_xdg_shell_create(struct wl_display* display, uint32_t version);
struct wlr_renderer* wlr_renderer_autocreate(struct wlr_backend* backend);
void wlr_renderer_init_wl_display(struct wlr_renderer* renderer, struct wl_display* display);
// Wayland display helpers (for C++ compilation)
struct wl_display* wl_display_create(void);
void wl_display_destroy(struct wl_display* display);
int wl_display_dispatch_pending(struct wl_display* display);
void wl_display_flush_clients(struct wl_display* display);
}
#endif
// Static member initialization
WaylandPlatform* WaylandPlatform::instance_ = nullptr;
WaylandPlatform::WaylandPlatform()
: display_(nullptr)
, registry_(nullptr)
, compositor_(nullptr)
, shm_(nullptr)
, seat_(nullptr)
, output_(nullptr)
, shell_(nullptr)
, backend_(nullptr)
, renderer_(nullptr)
, wlr_compositor_(nullptr)
, output_layout_(nullptr)
, cursor_(nullptr)
, xcursor_manager_(nullptr)
, wlr_seat_(nullptr)
, xdg_shell_(nullptr)
// , layer_shell_(nullptr)
, event_loop_running_(false) {
instance_ = this;
}
WaylandPlatform::~WaylandPlatform() {
shutdown();
if (instance_ == this) {
instance_ = nullptr;
}
}
bool WaylandPlatform::initialize() {
#ifndef USE_WAYLAND_STUB
std::cout << "Initializing Wayland platform (wlroots real backend)..." << std::endl;
// Create Wayland display
display_ = wl_display_create();
if (!display_) {
std::cerr << "Failed to create wl_display" << std::endl;
return false;
}
// Initialize wlroots logging (optional) - 2 corresponds to INFO
wlr_log_init(2, nullptr);
// Create backend (auto-detect e.g., DRM, Wayland, X11)
backend_ = wlr_backend_autocreate(display_, nullptr);
if (!backend_) {
std::cerr << "Failed to create wlr_backend" << std::endl;
return false;
}
// Create renderer and hook to display
renderer_ = wlr_renderer_autocreate(backend_);
if (!renderer_) {
std::cerr << "Failed to create wlr_renderer" << std::endl;
return false;
}
wlr_renderer_init_wl_display(renderer_, display_);
// Create compositor (wlroots 0.17 requires protocol version). Use version 6.
wlr_compositor_ = wlr_compositor_create(display_, 6, renderer_);
if (!wlr_compositor_) {
std::cerr << "Failed to create wlr_compositor" << std::endl;
return false;
}
// Create seat (input)
wlr_seat_ = wlr_seat_create(display_, "seat0");
if (!wlr_seat_) {
std::cerr << "Failed to create wlr_seat" << std::endl;
return false;
}
// Create xdg-shell (requires protocol version)
xdg_shell_ = wlr_xdg_shell_create(display_, 6);
if (!xdg_shell_) {
std::cerr << "Failed to create wlr_xdg_shell" << std::endl;
return false;
}
// Minimal bring-up: listeners will be wired in a follow-up
// Start backend
if (!wlr_backend_start(backend_)) {
std::cerr << "Failed to start wlr_backend" << std::endl;
return false;
}
decorations_enabled_ = true;
std::cout << "Wayland (wlroots) backend started." << std::endl;
return true;
#else
// Minimal Wayland backend initialization (no wlroots API calls).
std::cout << "Initializing Wayland platform (minimal stub)..." << std::endl;
decorations_enabled_ = true;
std::cout << "Wayland platform initialized (stub)." << std::endl;
return true;
#endif
}
void WaylandPlatform::shutdown() {
#ifndef USE_WAYLAND_STUB
std::cout << "Shutting down Wayland platform (wlroots)..." << std::endl;
if (xdg_shell_) { xdg_shell_ = nullptr; }
// Seat is tied to display lifecycle; skip explicit destroy to avoid ABI issues
wlr_seat_ = nullptr;
if (wlr_compositor_) { /* wlr_compositor is owned by display, no explicit destroy */ wlr_compositor_ = nullptr; }
// Renderer is owned by backend in this path; no explicit destroy
renderer_ = nullptr;
if (backend_) { wlr_backend_destroy(backend_); backend_ = nullptr; }
if (display_) { wl_display_destroy(display_); display_ = nullptr; }
#else
std::cout << "Shutting down Wayland platform (stub)..." << std::endl;
#endif
}
bool WaylandPlatform::setup_wlroots_backend() { std::cout << "wlroots backend (stub)" << std::endl; return true; }
bool WaylandPlatform::setup_compositor() { std::cout << "compositor (stub)" << std::endl; return true; }
// (removed unused stub-only setup_output/setup_input in real backend path)
bool WaylandPlatform::setup_shell_protocols() { std::cout << "shell protocols (stub)" << std::endl; return true; }
// bool WaylandPlatform::setup_xwayland() { return true; }
bool WaylandPlatform::poll_events(std::vector<Event>& events) {
#ifndef USE_WAYLAND_STUB
if (!display_ || !backend_) return false;
events.clear();
// Dispatch Wayland events; non-blocking to integrate with app loop
wl_display_dispatch_pending(display_);
wl_display_flush_clients(display_);
return true;
#else
events.clear();
return true;
#endif
}
void WaylandPlatform::process_event(const Event& event) {
// TODO: Implement event processing
}
std::unique_ptr<SRDWindow> WaylandPlatform::create_window(const std::string& title, int x, int y, int width, int height) {
// TODO: Implement window creation
std::cout << "Creating Wayland window: " << title << std::endl;
return nullptr;
}
void WaylandPlatform::destroy_window(SRDWindow* window) {
// TODO: Implement window destruction
std::cout << "Destroying Wayland window" << std::endl;
}
void WaylandPlatform::set_window_position(SRDWindow* window, int x, int y) {
// TODO: Implement window positioning
}
void WaylandPlatform::set_window_size(SRDWindow* window, int width, int height) {
// TODO: Implement window resizing
}
void WaylandPlatform::set_window_title(SRDWindow* window, const std::string& title) {
// TODO: Implement title setting
}
void WaylandPlatform::focus_window(SRDWindow* window) {
// TODO: Implement window focusing
}
void WaylandPlatform::minimize_window(SRDWindow* window) {
// TODO: Implement window minimization
}
void WaylandPlatform::maximize_window(SRDWindow* window) {
// TODO: Implement window maximization
}
void WaylandPlatform::close_window(SRDWindow* window) {
// TODO: Implement window closing
}
std::vector<Monitor> WaylandPlatform::get_monitors() {
// TODO: Implement monitor detection
return {};
}
Monitor WaylandPlatform::get_primary_monitor() {
// TODO: Implement primary monitor detection
return Monitor{};
}
void WaylandPlatform::grab_keyboard() {
// TODO: Implement keyboard grabbing
}
void WaylandPlatform::ungrab_keyboard() {
// TODO: Implement keyboard ungrab
}
void WaylandPlatform::grab_pointer() {
// TODO: Implement pointer grabbing
}
void WaylandPlatform::ungrab_pointer() {
// TODO: Implement pointer ungrab
}
// Static callback functions
void WaylandPlatform::registry_global_handler(void* data, struct wl_registry* registry,
uint32_t name, const char* interface, uint32_t version) {
(void)registry; (void)name; (void)interface; (void)version;
WaylandPlatform* platform = static_cast<WaylandPlatform*>(data);
platform->handle_registry_global(nullptr, 0, interface ? interface : "", 0);
}
void WaylandPlatform::registry_global_remove_handler(void* data, struct wl_registry* registry, uint32_t name) {
(void)registry; (void)name;
WaylandPlatform* platform = static_cast<WaylandPlatform*>(data);
platform->handle_registry_global_remove(nullptr, 0);
}
void WaylandPlatform::xdg_surface_new_handler(struct wl_listener* listener, void* data) {
(void)listener;
if (WaylandPlatform::instance_) {
WaylandPlatform::instance_->handle_xdg_surface_new(static_cast<struct wlr_xdg_surface*>(data));
}
}
void WaylandPlatform::xdg_surface_destroy_handler(struct wl_listener* listener, void* data) {
(void)listener;
if (WaylandPlatform::instance_) {
WaylandPlatform::instance_->handle_xdg_surface_destroy(static_cast<struct wlr_xdg_surface*>(data));
}
}
void WaylandPlatform::xdg_toplevel_new_handler(struct wl_listener* listener, void* data) {
(void)listener;
if (WaylandPlatform::instance_) {
WaylandPlatform::instance_->handle_xdg_toplevel_new(static_cast<struct wlr_xdg_toplevel*>(data));
}
}
void WaylandPlatform::xdg_toplevel_destroy_handler(struct wl_listener* listener, void* data) {
(void)listener;
if (WaylandPlatform::instance_) {
WaylandPlatform::instance_->handle_xdg_toplevel_destroy(static_cast<struct wlr_xdg_toplevel*>(data));
}
}
// (removed xwayland handlers; not declared while XWayland is disabled)
void WaylandPlatform::output_new_handler(struct wl_listener* listener, void* data) {
(void)listener;
if (WaylandPlatform::instance_) {
WaylandPlatform::instance_->handle_output_new(static_cast<struct wlr_output*>(data));
}
}
void WaylandPlatform::output_destroy_handler(struct wl_listener* listener, void* data) {
(void)listener;
if (WaylandPlatform::instance_) {
WaylandPlatform::instance_->handle_output_destroy(static_cast<struct wlr_output*>(data));
}
}
void WaylandPlatform::output_frame_handler(struct wl_listener* listener, void* data) {
(void)listener;
if (WaylandPlatform::instance_) {
WaylandPlatform::instance_->handle_output_frame(static_cast<struct wlr_output*>(data));
}
}
void WaylandPlatform::pointer_motion_handler(struct wl_listener* listener, void* data) {
(void)listener;
if (WaylandPlatform::instance_) {
WaylandPlatform::instance_->handle_pointer_motion(static_cast<struct wlr_pointer_motion_event*>(data));
}
}
void WaylandPlatform::pointer_button_handler(struct wl_listener* listener, void* data) {
(void)listener;
if (WaylandPlatform::instance_) {
WaylandPlatform::instance_->handle_pointer_button(static_cast<struct wlr_pointer_button_event*>(data));
}
}
void WaylandPlatform::pointer_axis_handler(struct wl_listener* listener, void* data) {
(void)listener;
if (WaylandPlatform::instance_) {
WaylandPlatform::instance_->handle_pointer_axis(static_cast<struct wlr_pointer_axis_event*>(data));
}
}
void WaylandPlatform::keyboard_key_handler(struct wl_listener* listener, void* data) {
(void)listener;
if (WaylandPlatform::instance_) {
WaylandPlatform::instance_->handle_keyboard_key(static_cast<struct wlr_keyboard_key_event*>(data));
}
}
// Event handling methods
void WaylandPlatform::handle_registry_global(struct wl_registry* registry, uint32_t name,
const char* interface, uint32_t version) {
(void)registry; (void)name; (void)version;
std::cout << "Registry global (stub): " << (interface ? interface : "?") << std::endl;
}
void WaylandPlatform::handle_registry_global_remove(struct wl_registry* registry, uint32_t name) {
(void)registry; (void)name;
}
void WaylandPlatform::handle_xdg_surface_new(struct wlr_xdg_surface* surface) {
std::cout << "New XDG surface" << std::endl;
manage_xdg_window(surface);
}
void WaylandPlatform::handle_xdg_surface_destroy(struct wlr_xdg_surface* surface) {
std::cout << "XDG surface destroyed" << std::endl;
}
void WaylandPlatform::handle_xdg_toplevel_new(struct wlr_xdg_toplevel* toplevel) {
std::cout << "New XDG toplevel" << std::endl;
}
void WaylandPlatform::handle_xdg_toplevel_destroy(struct wlr_xdg_toplevel* toplevel) {
std::cout << "XDG toplevel destroyed" << std::endl;
}
// void WaylandPlatform::handle_xwayland_surface_new(struct wlr_xwayland_surface* surface) {}
// void WaylandPlatform::handle_xwayland_surface_destroy(struct wlr_xwayland_surface* surface) {}
void WaylandPlatform::handle_output_new(struct wlr_output* output) {
std::cout << "New output" << std::endl;
handle_output_mode(output);
}
void WaylandPlatform::handle_output_destroy(struct wlr_output* output) {
std::cout << "Output destroyed" << std::endl;
}
void WaylandPlatform::handle_output_frame(struct wlr_output* output) {
// Handle output frame event
}
void WaylandPlatform::handle_pointer_motion(struct wlr_pointer_motion_event* event) {
// Handle pointer motion
}
void WaylandPlatform::handle_pointer_button(struct wlr_pointer_button_event* event) {
// Handle pointer button
}
void WaylandPlatform::handle_pointer_axis(struct wlr_pointer_axis_event* event) {
// Handle pointer axis
}
void WaylandPlatform::handle_keyboard_key(struct wlr_keyboard_key_event* event) {
// Handle keyboard key
}
void WaylandPlatform::manage_xdg_window(struct wlr_xdg_surface* surface) {
// TODO: Implement XDG window management
}
// void WaylandPlatform::manage_xwayland_window(struct wlr_xwayland_surface* surface) {}
void WaylandPlatform::unmanage_window(SRDWindow* window) {
// TODO: Implement window unmanagement
}
void WaylandPlatform::handle_output_mode(struct wlr_output* output) {
// TODO: Implement output mode handling
}
void WaylandPlatform::handle_output_scale(struct wlr_output* output) {
// TODO: Implement output scale handling
}
void WaylandPlatform::handle_key_event(uint32_t key, bool pressed) {
// TODO: Implement key event handling
}
void WaylandPlatform::handle_button_event(uint32_t button, bool pressed) {
// TODO: Implement button event handling
}
void WaylandPlatform::create_surface_window(struct wlr_surface* surface) {
// TODO: Implement surface window creation
}
void WaylandPlatform::destroy_surface_window(struct wlr_surface* surface) {
// TODO: Implement surface window destruction
}
void WaylandPlatform::update_surface_window(struct wlr_surface* surface) {
// TODO: Implement surface window update
}
void WaylandPlatform::convert_wlroots_event_to_srdwm_event(void* event_data, EventType type) {
// TODO: Implement event conversion
}
void WaylandPlatform::handle_wlroots_error(const std::string& error) {
std::cerr << "wlroots error: " << error << std::endl;
}
// SRDWindow decoration implementations
void WaylandPlatform::set_window_decorations(SRDWindow* window, bool enabled) {
// Temporary stub: wlroots xdg-decoration v1 helpers differ across versions.
// Keep internal state and log, skip calling decoration APIs.
std::cout << "WaylandPlatform: Set window decorations " << (enabled ? "enabled" : "disabled") << std::endl;
(void)window; // unused for now
decorations_enabled_ = enabled;
}
void WaylandPlatform::set_window_border_color(SRDWindow* window, int r, int g, int b) {
std::cout << "WaylandPlatform: Set border color RGB(" << r << "," << g << "," << b << ")" << std::endl;
if (!window || !renderer_) return;
// Store border color for this window
// In a full implementation, this would be stored in a window-specific data structure
// and applied during rendering
// For now, we'll just log the request
// TODO: Implement custom border rendering via wlroots rendering pipeline
std::cout << "Border color set for window " << window->getId()
<< ": RGB(" << r << "," << g << "," << b << ")" << std::endl;
}
void WaylandPlatform::set_window_border_width(SRDWindow* window, int width) {
std::cout << "WaylandPlatform: Set border width " << width << std::endl;
if (!window) return;
// TODO: Implement when wlroots rendering is set up
// This would involve drawing custom borders on surfaces
}
bool WaylandPlatform::get_window_decorations(SRDWindow* window) const {
if (!window) return false;
return decorations_enabled_;
}
void WaylandPlatform::setup_decoration_manager() {
// Temporary stub: skip creating xdg-decoration manager to avoid
// wlroots API/ABI differences across distro versions.
std::cout << "Decoration manager (stub) initialized" << std::endl;
}
void WaylandPlatform::handle_decoration_request(struct wlr_xdg_surface* surface, uint32_t mode) {
std::cout << "Handling decoration request, mode: " << mode << std::endl;
// TODO: Implement when zxdg-decoration protocol is available
// This would involve:
// 1. Checking if server-side decorations are enabled
// 2. Setting the decoration mode for the surface
// 3. Drawing decorations if needed
}
|