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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
|
# SRDWM Platform Implementation Guide
## Overview
This document outlines the proper implementation approach for each platform, recognizing that **Wayland/XWayland is fundamentally different from X11** and requires completely different technologies and approaches.
## Platform Architecture Differences
### Linux: X11 vs Wayland
- **X11**: Traditional X11 window management with Xlib/XCB
- **Wayland**: Modern display protocol requiring wlroots or similar compositor framework
- **XWayland**: X11 applications running on Wayland (requires special handling)
### Windows vs macOS vs Linux
- **Windows**: Win32 API with global hooks and window subclassing
- **macOS**: Core Graphics/AppKit with accessibility APIs and event taps
- **Linux**: X11 or Wayland with different event systems
## Linux Implementation
### X11 Backend
```cpp
// X11-specific implementation using Xlib/XCB
class X11Platform : public Platform {
private:
Display* display_;
Window root_;
std::map<Window, Window*> window_map_;
public:
bool initialize() override {
display_ = XOpenDisplay(nullptr);
if (!display_) return false;
root_ = DefaultRootWindow(display_);
setup_event_handling();
return true;
}
void setup_event_handling() {
// X11 event masks and handlers
XSelectInput(display_, root_,
SubstructureRedirectMask | SubstructureNotifyMask |
KeyPressMask | KeyReleaseMask |
ButtonPressMask | ButtonReleaseMask |
PointerMotionMask);
}
bool poll_events(std::vector<Event>& events) override {
XEvent xevent;
while (XPending(display_)) {
XNextEvent(display_, &xevent);
convert_x11_event(xevent, events);
}
return true;
}
void convert_x11_event(const XEvent& xevent, std::vector<Event>& events) {
switch (xevent.type) {
case MapRequest:
handle_map_request(xevent.xmaprequest);
break;
case ConfigureRequest:
handle_configure_request(xevent.xconfigurerequest);
break;
case KeyPress:
handle_key_press(xevent.xkey);
break;
// ... other event types
}
}
};
```
### Wayland Backend (using wlroots)
```cpp
// Wayland implementation using wlroots
class WaylandPlatform : public Platform {
private:
struct wl_display* display_;
struct wlroots_backend* backend_;
struct wlroots_compositor* compositor_;
struct wlroots_output* output_;
struct wlroots_input_device* input_device_;
public:
bool initialize() override {
// Initialize wlroots backend
backend_ = wlroots_backend_create();
if (!backend_) return false;
// Create compositor
compositor_ = wlroots_compositor_create(backend_);
if (!compositor_) return false;
// Setup output and input
setup_output();
setup_input();
return true;
}
void setup_output() {
// Create and configure output
output_ = wlroots_output_create(compositor_);
wlroots_output_set_mode(output_, 1920, 1080, 60);
wlroots_output_commit(output_);
}
void setup_input() {
// Setup input devices
input_device_ = wlroots_input_device_create(compositor_);
wlroots_input_device_set_capabilities(input_device_,
WLROOTS_INPUT_DEVICE_CAP_KEYBOARD |
WLROOTS_INPUT_DEVICE_CAP_POINTER);
}
bool poll_events(std::vector<Event>& events) override {
// wlroots event loop
wlroots_backend_dispatch(backend_);
// Process wlroots events
struct wlroots_event* event;
while ((event = wlroots_backend_get_event(backend_))) {
convert_wlroots_event(event, events);
wlroots_event_destroy(event);
}
return true;
}
void convert_wlroots_event(struct wlroots_event* event, std::vector<Event>& events) {
switch (wlroots_event_get_type(event)) {
case WLROOTS_EVENT_NEW_SURFACE:
handle_new_surface(event);
break;
case WLROOTS_EVENT_SURFACE_COMMIT:
handle_surface_commit(event);
break;
case WLROOTS_EVENT_KEYBOARD_KEY:
handle_keyboard_key(event);
break;
// ... other event types
}
}
};
```
### XWayland Support
```cpp
// XWayland support for running X11 apps on Wayland
class XWaylandManager {
private:
struct wlroots_xwayland* xwayland_;
struct wlroots_xwayland_server* xwayland_server_;
public:
bool initialize(struct wlroots_compositor* compositor) {
// Create XWayland server
xwayland_server_ = wlroots_xwayland_server_create(compositor);
if (!xwayland_server_) return false;
// Setup XWayland
xwayland_ = wlroots_xwayland_create(xwayland_server_);
if (!xwayland_) return false;
return true;
}
void handle_xwayland_surface(struct wlroots_surface* surface) {
// Handle X11 windows running on Wayland
// These need special treatment for proper integration
}
};
```
## Windows Implementation
### Win32 API Integration
```cpp
// Windows implementation using Win32 API
class WindowsPlatform : public Platform {
private:
HINSTANCE h_instance_;
std::map<HWND, Window*> window_map_;
HHOOK keyboard_hook_;
HHOOK mouse_hook_;
public:
bool initialize() override {
h_instance_ = GetModuleHandle(nullptr);
// Register window class
if (!register_window_class()) return false;
// Setup global hooks
setup_global_hooks();
return true;
}
bool register_window_class() {
WNDCLASSEX wc = {};
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = window_proc;
wc.hInstance = h_instance_;
wc.lpszClassName = L"SRDWM_Window";
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
return RegisterClassEx(&wc) != 0;
}
void setup_global_hooks() {
// Global keyboard hook
keyboard_hook_ = SetWindowsHookEx(WH_KEYBOARD_LL,
keyboard_proc, h_instance_, 0);
// Global mouse hook
mouse_hook_ = SetWindowsHookEx(WH_MOUSE_LL,
mouse_proc, h_instance_, 0);
}
static LRESULT CALLBACK window_proc(HWND hwnd, UINT msg,
WPARAM wparam, LPARAM lparam) {
switch (msg) {
case WM_CREATE:
// Handle window creation
break;
case WM_DESTROY:
// Handle window destruction
break;
case WM_SIZE:
// Handle window resizing
break;
// ... other messages
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
static LRESULT CALLBACK keyboard_proc(int nCode, WPARAM wparam, LPARAM lparam) {
if (nCode >= 0) {
KBDLLHOOKSTRUCT* kbhs = (KBDLLHOOKSTRUCT*)lparam;
// Handle global keyboard events
handle_global_keyboard(wparam, kbhs);
}
return CallNextHookEx(nullptr, nCode, wparam, lparam);
}
static LRESULT CALLBACK mouse_proc(int nCode, WPARAM wparam, LPARAM lparam) {
if (nCode >= 0) {
MSLLHOOKSTRUCT* mhs = (MSLLHOOKSTRUCT*)lparam;
// Handle global mouse events
handle_global_mouse(wparam, mhs);
}
return CallNextHookEx(nullptr, nCode, wparam, lparam);
}
};
```
## macOS Implementation
### Core Graphics/AppKit Integration
```cpp
// macOS implementation using Core Graphics and AppKit
class MacOSPlatform : public Platform {
private:
CGEventTap event_tap_;
std::map<CGWindowID, Window*> window_map_;
public:
bool initialize() override {
// Request accessibility permissions
if (!request_accessibility_permissions()) return false;
// Setup event tap
setup_event_tap();
// Setup window monitoring
setup_window_monitoring();
return true;
}
bool request_accessibility_permissions() {
// Check if accessibility is enabled
const void* keys[] = { kAXTrustedCheckOptionPrompt };
const void* values[] = { kCFBooleanTrue };
CFDictionaryRef options = CFDictionaryCreate(
kCFAllocatorDefault, keys, values, 1, nullptr, nullptr);
bool trusted = AXIsProcessTrustedWithOptions(options);
CFRelease(options);
return trusted;
}
void setup_event_tap() {
// Create event tap for global events
event_tap_ = CGEventTapCreate(
kCGSessionEventTap,
kCGHeadInsertEventTap,
kCGEventTapOptionDefault,
CGEventMaskBit(kCGEventKeyDown) |
CGEventMaskBit(kCGEventKeyUp) |
CGEventMaskBit(kCGEventLeftMouseDown) |
CGEventMaskBit(kCGEventLeftMouseUp) |
CGEventMaskBit(kCGEventMouseMoved),
event_tap_callback,
this);
if (event_tap_) {
CFRunLoopSourceRef run_loop_source =
CFMachPortCreateRunLoopSource(kCFAllocatorDefault, event_tap_, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), run_loop_source, kCFRunLoopCommonModes);
CGEventTapEnable(event_tap_, true);
}
}
static CGEventRef event_tap_callback(CGEventTapProxy proxy, CGEventType type,
CGEventRef event, void* user_info) {
MacOSPlatform* platform = static_cast<MacOSPlatform*>(user_info);
return platform->handle_event_tap(proxy, type, event);
}
CGEventRef handle_event_tap(CGEventTapProxy proxy, CGEventType type, CGEventRef event) {
switch (type) {
case kCGEventKeyDown:
handle_key_event(event, true);
break;
case kCGEventKeyUp:
handle_key_event(event, false);
break;
case kCGEventLeftMouseDown:
handle_mouse_event(event, true);
break;
case kCGEventLeftMouseUp:
handle_mouse_event(event, false);
break;
case kCGEventMouseMoved:
handle_mouse_motion(event);
break;
}
return event;
}
void setup_window_monitoring() {
// Monitor window creation/destruction
CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly |
kCGWindowListExcludeDesktopElements,
kCGNullWindowID);
}
};
```
## Platform Detection and Selection
### Automatic Platform Detection
```cpp
// Platform factory with automatic detection
class PlatformFactory {
public:
static std::unique_ptr<Platform> create_platform() {
#ifdef _WIN32
return std::make_unique<WindowsPlatform>();
#elif defined(__APPLE__)
return std::make_unique<MacOSPlatform>();
#else
// Linux: detect X11 vs Wayland
return detect_linux_platform();
#endif
}
private:
static std::unique_ptr<Platform> detect_linux_platform() {
// Check environment variables
const char* wayland_display = std::getenv("WAYLAND_DISPLAY");
const char* xdg_session_type = std::getenv("XDG_SESSION_TYPE");
if (wayland_display || (xdg_session_type && strcmp(xdg_session_type, "wayland") == 0)) {
// Try Wayland first
auto wayland_platform = std::make_unique<WaylandPlatform>();
if (wayland_platform->initialize()) {
std::cout << "Using Wayland backend" << std::endl;
return wayland_platform;
}
std::cout << "Wayland initialization failed, falling back to X11" << std::endl;
}
// Fall back to X11
auto x11_platform = std::make_unique<X11Platform>();
if (x11_platform->initialize()) {
std::cout << "Using X11 backend" << std::endl;
return x11_platform;
}
std::cerr << "Failed to initialize any platform backend" << std::endl;
return nullptr;
}
};
```
## Dependencies and Build System
### CMake Configuration
```cmake
# Platform-specific dependencies
if(WIN32)
# Windows dependencies
find_package(PkgConfig REQUIRED)
set(PLATFORM_LIBS user32 gdi32)
elseif(APPLE)
# macOS dependencies
find_library(COCOA_LIBRARY Cocoa)
find_library(CARBON_LIBRARY Carbon)
find_library(IOKIT_LIBRARY IOKit)
set(PLATFORM_LIBS ${COCOA_LIBRARY} ${CARBON_LIBRARY} ${IOKIT_LIBRARY})
else()
# Linux dependencies
find_package(PkgConfig REQUIRED)
# X11 dependencies
pkg_check_modules(X11 REQUIRED x11 xcb xcb-keysyms)
# Wayland dependencies (optional)
pkg_check_modules(WAYLAND wayland-client wayland-cursor)
pkg_check_modules(WLROOTS wlroots)
if(WAYLAND_FOUND AND WLROOTS_FOUND)
add_definitions(-DWAYLAND_ENABLED)
set(PLATFORM_LIBS ${PLATFORM_LIBS} ${WAYLAND_LIBRARIES} ${WLROOTS_LIBRARIES})
endif()
set(PLATFORM_LIBS ${PLATFORM_LIBS} ${X11_LIBRARIES})
endif()
```
### Package Dependencies
```bash
# Ubuntu/Debian
sudo apt install libx11-dev libxcb1-dev libxcb-keysyms1-dev
sudo apt install libwayland-dev libwlroots-dev
# Arch Linux
sudo pacman -S xorg-server-devel wayland wlroots
# Fedora
sudo dnf install libX11-devel libxcb-devel wayland-devel wlroots-devel
```
## Event Handling Differences
### X11 Event System
```cpp
// X11 events are synchronous and direct
void X11Platform::handle_map_request(const XMapRequestEvent& event) {
Window* window = create_window(event.window);
if (window) {
window_map_[event.window] = window;
// X11 window is now managed
}
}
```
### Wayland Event System
```cpp
// Wayland events are asynchronous and callback-based
void WaylandPlatform::handle_new_surface(struct wlroots_event* event) {
struct wlroots_surface* surface = wlroots_event_get_surface(event);
// Create window for new surface
Window* window = create_window_from_surface(surface);
if (window) {
surface_window_map_[surface] = window;
}
}
```
### Windows Event System
```cpp
// Windows uses message-based event system
LRESULT WindowsPlatform::window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
switch (msg) {
case WM_CREATE:
// Window creation
break;
case WM_DESTROY:
// Window destruction
break;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
```
### macOS Event System
```cpp
// macOS uses event taps and accessibility APIs
CGEventRef MacOSPlatform::handle_event_tap(CGEventTapProxy proxy, CGEventType type, CGEventRef event) {
switch (type) {
case kCGEventKeyDown:
// Handle key press
break;
case kCGEventMouseMoved:
// Handle mouse movement
break;
}
return event;
}
```
## Window Management Differences
### X11 Window Management
```cpp
// X11: Direct window manipulation
void X11Platform::set_window_position(Window* window, int x, int y) {
XMoveWindow(display_, window->get_x11_handle(), x, y);
}
void X11Platform::set_window_size(Window* window, int width, int height) {
XResizeWindow(display_, window->get_x11_handle(), width, height);
}
```
### Wayland Window Management
```cpp
// Wayland: Surface-based management
void WaylandPlatform::set_window_position(Window* window, int x, int y) {
struct wlroots_surface* surface = window->get_wayland_surface();
wlroots_surface_set_position(surface, x, y);
}
void WaylandPlatform::set_window_size(Window* window, int width, int height) {
struct wlroots_surface* surface = window->get_wayland_surface();
wlroots_surface_set_size(surface, width, height);
}
```
### Windows Window Management
```cpp
// Windows: Win32 API calls
void WindowsPlatform::set_window_position(Window* window, int x, int y) {
HWND hwnd = window->get_win32_handle();
SetWindowPos(hwnd, nullptr, x, y, 0, 0,
SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}
void WindowsPlatform::set_window_size(Window* window, int width, int height) {
HWND hwnd = window->get_win32_handle();
SetWindowPos(hwnd, nullptr, 0, 0, width, height,
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
}
```
### macOS Window Management
```cpp
// macOS: Core Graphics API calls
void MacOSPlatform::set_window_position(Window* window, int x, int y) {
CGWindowID window_id = window->get_macos_window_id();
CGPoint position = CGPointMake(x, y);
// Use accessibility APIs to move window
AXUIElementRef element = AXUIElementCreateApplication(
window->get_macos_pid());
if (element) {
AXUIElementSetAttributeValue(element, kAXPositionAttribute, &position);
CFRelease(element);
}
}
```
## Testing and Validation
### Platform-Specific Testing
```cpp
// Test each platform independently
class PlatformTest {
public:
static void test_x11_platform() {
auto platform = std::make_unique<X11Platform>();
assert(platform->initialize());
// Test X11-specific functionality
}
static void test_wayland_platform() {
auto platform = std::make_unique<WaylandPlatform>();
assert(platform->initialize());
// Test Wayland-specific functionality
}
static void test_windows_platform() {
auto platform = std::make_unique<WindowsPlatform>();
assert(platform->initialize());
// Test Windows-specific functionality
}
static void test_macos_platform() {
auto platform = std::make_unique<MacOSPlatform>();
assert(platform->initialize());
// Test macOS-specific functionality
}
};
```
## Best Practices
### 1. **Platform Abstraction**
- Keep platform-specific code isolated
- Use common interfaces for cross-platform functionality
- Implement platform detection automatically
### 2. **Wayland vs X11**
- **Never mix X11 and Wayland APIs**
- Use wlroots for Wayland (don't implement from scratch)
- Handle XWayland as a special case within Wayland
### 3. **Event Handling**
- Respect each platform's event model
- Don't force synchronous behavior on asynchronous platforms
- Handle platform-specific quirks gracefully
### 4. **Window Management**
- Use platform-native APIs for best performance
- Don't try to emulate one platform's behavior on another
- Handle platform-specific window states properly
### 5. **Testing**
- Test each platform independently
- Use CI/CD with multiple platform targets
- Validate platform-specific features thoroughly
This implementation approach ensures that SRDWM works correctly on each platform while respecting the fundamental differences between X11, Wayland, Windows, and macOS.
|