aboutsummaryrefslogtreecommitdiff
path: root/src/window/wayland.c
blob: 20c5c7a6d03288582c65a2f7ed847b7dd66112fb (plain)
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
#include "../../../include/mgl/window/wayland.h"
#include "../../../include/mgl/mgl.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include <wayland-client.h>
#include <wayland-egl.h>
#include "xdg-shell-client-protocol.h"

static void mgl_window_wayland_deinit(mgl_window *self);

typedef struct {
    struct wl_egl_window *window;
    struct wl_registry *registry;
    struct wl_surface *surface;
    struct wl_compositor *compositor;
    struct wl_shell *shell;
    struct wl_shell_surface *shell_surface;

    struct xdg_wm_base *xdg_wm_base;
    struct xdg_surface *xdg_surface;
    struct xdg_toplevel *xdg_toplevel;

    mgl_graphics graphics;
} mgl_window_wayland;

static void registry_add_object(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) {
    (void)version;
    mgl_window *self = data;
    mgl_window_wayland *impl = self->impl;
    if(strcmp(interface, wl_compositor_interface.name) == 0) {
        if(impl->compositor) {
            wl_compositor_destroy(impl->compositor);
            impl->compositor = NULL;
        }
        impl->compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 1);
    } else if(strcmp(interface, wl_output_interface.name) == 0) {
        if(version < 4) {
            fprintf(stderr, "gsr warning: wl output interface version is < 4, expected >= 4 to capture a monitor\n");
            return;
        }

        if(self->num_monitors == MGL_MAX_MONITORS) {
            fprintf(stderr, "gsr warning: reached maximum outputs (%d), ignoring output %u\n", MGL_MAX_MONITORS, name);
            return;
        }

        // gsr_wayland_output *gsr_output = &window_wayland->outputs[window_wayland->num_outputs];
        // ++self->num_monitors;
        // *gsr_output = (gsr_wayland_output) {
        //     .wl_name = name,
        //     .output = wl_registry_bind(registry, name, &wl_output_interface, 4),
        //     .pos = { .x = 0, .y = 0 },
        //     .size = { .x = 0, .y = 0 },
        //     .transform = 0,
        //     .name = NULL,
        // };
        // wl_output_add_listener(gsr_output->output, &output_listener, gsr_output);
    } else if(strcmp(interface, wl_shell_interface.name) == 0) {
        if(impl->shell) {
            wl_shell_destroy(impl->shell);
            impl->shell = NULL;
        }
        impl->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
    } else if(strcmp(interface, xdg_wm_base_interface.name) == 0) {
        if(impl->xdg_wm_base) {
            xdg_wm_base_destroy(impl->xdg_wm_base);
            impl->xdg_wm_base = NULL;
        }
        impl->xdg_wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1);
    }
}

static void registry_remove_object(void *data, struct wl_registry *registry, uint32_t name) {
    (void)data;
    (void)registry;
    (void)name;
    // TODO: Remove output
}

static struct wl_registry_listener registry_listener = {
    .global = registry_add_object,
    .global_remove = registry_remove_object,
};

static void shell_surface_ping(void *data, struct wl_shell_surface *shell_surface, uint32_t serial) {
    wl_shell_surface_pong(shell_surface, serial);
}

static void shell_surface_configure(void *data, struct wl_shell_surface *shell_surface, uint32_t edges, int32_t width, int32_t height) {
    mgl_window *self = data;
    mgl_window_wayland *impl = self->impl;
    wl_egl_window_resize(impl->window, width, height, 0, 0);
}

void shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface) {
    
}

static struct wl_shell_surface_listener shell_surface_listener = {
    .ping = shell_surface_ping,
    .configure = shell_surface_configure,
    .popup_done = shell_surface_popup_done,
};

static void xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial) {
    xdg_wm_base_pong(shell, serial);
}

static const struct xdg_wm_base_listener wm_base_listener = {
    .ping = xdg_wm_base_ping,
};

static void xdg_surface_configure(void *data, struct xdg_surface *surface, uint32_t serial) {
	xdg_surface_ack_configure(surface, serial);
    // TODO:
	//window->wait_for_configure = false;
}

static const struct xdg_surface_listener xdg_surface_listener = {
	.configure = xdg_surface_configure,
};

static void xdg_toplevel_configure(void *data, struct xdg_toplevel *toplevel, int32_t width, int32_t height, struct wl_array *states) {
    mgl_window *self = data;
    mgl_window_wayland *impl = self->impl;
    // TODO:
    wl_egl_window_resize(impl->window, width, height, 0, 0);
	// struct window *window = data;
	// uint32_t *p;

	// window->fullscreen = 0;
	// window->maximized = 0;
	// wl_array_for_each(p, states) {
	// 	uint32_t state = *p;
	// 	switch (state) {
	// 	case XDG_TOPLEVEL_STATE_FULLSCREEN:
	// 		window->fullscreen = 1;
	// 		break;
	// 	case XDG_TOPLEVEL_STATE_MAXIMIZED:
	// 		window->maximized = 1;
	// 		break;
	// 	}
	// }

	// if (width > 0 && height > 0) {
	// 	if (!window->fullscreen && !window->maximized) {
	// 		window->window_size.width = width;
	// 		window->window_size.height = height;
	// 	}
	// 	window->geometry.width = width;
	// 	window->geometry.height = height;
	// } else if (!window->fullscreen && !window->maximized) {
	// 	window->geometry = window->window_size;
	// }

	// if (window->native)
	// 	wl_egl_window_resize(window->native,
	// 			     window->geometry.width,
	// 			     window->geometry.height, 0, 0);
}

static void xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel) {
    // TODO:
	//running = 0;
}

static const struct xdg_toplevel_listener xdg_toplevel_listener = {
	.configure = xdg_toplevel_configure,
	.close = xdg_toplevel_close,
};


// TODO: params and existing_window
bool mgl_wayland_setup_window(mgl_window *self, const char *title, const mgl_window_create_params *params, mgl_window_handle existing_window) {
    mgl_window_wayland *impl = self->impl;
    mgl_context *context = mgl_get_context();

    mgl_vec2i window_size = params ? params->size : (mgl_vec2i){ 0, 0 };
    if(window_size.x <= 0 || window_size.y <= 0) {
        window_size.x = 640;
        window_size.y = 480;
    }
    self->size = window_size;

    impl->registry = wl_display_get_registry(context->connection); /* TODO: Error checking */
    wl_registry_add_listener(impl->registry, &registry_listener, self); /* TODO: Error checking */

    /* Fetch globals */
    wl_display_roundtrip(context->connection);

    /* Fetch wl_output */
    //wl_display_roundtrip(context->connection);

    if(!impl->compositor) {
        fprintf(stderr, "mgl error: mgl_wayland_setup_window: failed to find compositor\n");
        return false;
    }

    if(!impl->xdg_wm_base && !impl->shell) {
        fprintf(stderr, "mgl error: mgl_wayland_setup_window: failed to find shell\n");
        return false;
    }

    impl->surface = wl_compositor_create_surface(impl->compositor);
    if(!impl->surface) {
        fprintf(stderr, "mgl error: mgl_wayland_setup_window: failed to create surface\n");
        return false;
    }

    if(impl->xdg_wm_base) {
        // TODO: Error check, cleanup
        xdg_wm_base_add_listener(impl->xdg_wm_base, &wm_base_listener, self);

        impl->xdg_surface = xdg_wm_base_get_xdg_surface(impl->xdg_wm_base, impl->surface);
        xdg_surface_add_listener(impl->xdg_surface, &xdg_surface_listener, self);

        impl->xdg_toplevel = xdg_surface_get_toplevel(impl->xdg_surface);
        xdg_toplevel_add_listener(impl->xdg_toplevel, &xdg_toplevel_listener, self);

        xdg_toplevel_set_title(impl->xdg_toplevel, title);
    } else {
        // TODO: Error check, cleanup
        impl->shell_surface = wl_shell_get_shell_surface(impl->shell, impl->surface);
        wl_shell_surface_add_listener(impl->shell_surface, &shell_surface_listener, self);
        wl_shell_surface_set_toplevel(impl->shell_surface);
        wl_shell_surface_set_title(impl->shell_surface, title);
    }

    wl_surface_commit(impl->surface);

    // TODO: Error check
    struct wl_region *region = wl_compositor_create_region(impl->compositor);
    wl_region_add(region, 0, 0, self->size.x, self->size.y);
    wl_surface_set_opaque_region(impl->surface, region);
    wl_region_destroy(region);

    impl->window = wl_egl_window_create(impl->surface, self->size.x, self->size.y);
    if(!impl->window) {
        fprintf(stderr, "mgl error: mgl_wayland_setup_window: failed to create the Wayland window\n");
        return false;
    }

    //mgl_window_x11_on_resize(self, self->size.x, self->size.y);
    //mgl_window_x11_on_move(self, window_pos.x, window_pos.y);
    mgl_view view;
    view.position = (mgl_vec2i){ 0, 0 };
    view.size = self->size;
    mgl_window_set_view(self, &view);
    mgl_window_set_scissor(self, &(mgl_scissor){ .position = { 0, 0 }, .size = self->size });

    self->open = true;
    self->focused = false;
    return true;
}

static mgl_window_handle mgl_window_wayland_get_system_handle(const mgl_window *self) {
    const mgl_window_wayland *impl = self->impl;
    return (mgl_window_handle)impl->window;
}

static void mgl_window_wayland_close(mgl_window *self) {
    mgl_window_wayland *impl = self->impl;

    if(impl->window) {
        wl_egl_window_destroy(impl->window);
        impl->window = NULL;
    }

    if(impl->surface) {
        wl_surface_destroy(impl->surface);
        impl->surface = NULL;
    }

    self->open = false;
}

static bool mgl_window_wayland_poll_event(mgl_window *self, mgl_event *event) {
    mgl_context *context = mgl_get_context();
    struct wl_display *display = context->connection;
    const bool events_available = wl_display_dispatch_pending(display) > 0;
    //wl_display_flush(display);
    /* TODO: Return event here, pop from circular buffer */
    return events_available;
}

static void mgl_window_wayland_swap_buffers(mgl_window *self) {
    mgl_window_wayland *impl = self->impl;
    mgl_graphics_swap_buffers(&impl->graphics, (mgl_window_handle)impl->window);
}

static void mgl_window_wayland_set_visible(mgl_window *self, bool visible) {
    fprintf(stderr, "TODO: Implement\n");
}

static bool mgl_window_wayland_is_key_pressed(const mgl_window *self, mgl_key key) {
    (void)self;
    if(key < 0 || key >= __MGL_NUM_KEYS__)
        return false;
    
    fprintf(stderr, "TODO: Implement\n");
    return false;
}

static bool mgl_window_wayland_is_mouse_button_pressed(const mgl_window *self, mgl_mouse_button button) {
    (void)self;
    if(button < 0 || button >= __MGL_NUM_MOUSE_BUTTONS__)
        return false;

    fprintf(stderr, "TODO: Implement\n");
    return false;
}

static void mgl_window_wayland_set_title(mgl_window *self, const char *title) {
    fprintf(stderr, "TODO: Implement\n");
}

static void mgl_window_wayland_set_cursor_visible(mgl_window *self, bool visible) {
    fprintf(stderr, "TODO: Implement\n");
}

static void mgl_window_wayland_set_vsync_enabled(mgl_window *self, bool enabled) {
    mgl_window_wayland *impl = self->impl;
    self->vsync_enabled = enabled;
    if(!mgl_graphics_set_swap_interval(&impl->graphics, (mgl_window_handle)impl->window, self->vsync_enabled))
        fprintf(stderr, "mgl warning: mgl_window_wayland_set_vsync_enabled: failed to enable vsync\n");
}

static bool mgl_window_wayland_is_vsync_enabled(const mgl_window *self) {
    return self->vsync_enabled;
}

static void mgl_window_wayland_set_fullscreen(mgl_window *self, bool fullscreen) {
    mgl_window_wayland *impl = self->impl;
    // TODO: The last argument is the monitor we want to fullscreen on. Use this!
    if(impl->xdg_wm_base) {
        if(fullscreen)
            xdg_toplevel_set_fullscreen(impl->xdg_toplevel, NULL);
        else
            xdg_toplevel_unset_fullscreen(impl->xdg_toplevel);
    } else {
        if(fullscreen)
            wl_shell_surface_set_fullscreen(impl->shell_surface, WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, 0, NULL);
        else
            fprintf(stderr, "TODO: Implement\n");
    }
}

static bool mgl_window_wayland_is_fullscreen(const mgl_window *self) {
    fprintf(stderr, "TODO: Implement\n");
    return false;
}

static void mgl_window_wayland_set_position(mgl_window *self, mgl_vec2i position) {
    fprintf(stderr, "TODO: Implement\n");
}

static void mgl_window_wayland_set_size(mgl_window *self, mgl_vec2i size) {
    fprintf(stderr, "TODO: Implement\n");
}

static void mgl_window_wayland_set_size_limits(mgl_window *self, mgl_vec2i minimum, mgl_vec2i maximum) {
    fprintf(stderr, "TODO: Implement\n");
}

static void mgl_window_wayland_set_clipboard(mgl_window *self, const char *str, size_t size) {
    fprintf(stderr, "TODO: Implement\n");
}

static bool mgl_window_wayland_get_clipboard(mgl_window *self, mgl_clipboard_callback callback, void *userdata, uint32_t clipboard_types) {
    fprintf(stderr, "TODO: Implement\n");
    return false;
}

static void mgl_window_wayland_set_key_repeat_enabled(mgl_window *self, bool enabled) {
    self->key_repeat_enabled = enabled;
    // TODO: Implement
}

static void mgl_window_wayland_flush(mgl_window *self) {
    (void)self;
    mgl_context *context = mgl_get_context();
    struct wl_display *display = context->connection;
    wl_display_flush(display);
}

static void* mgl_window_wayland_get_egl_display(mgl_window *self) {
    mgl_window_wayland *impl = self->impl;
    if(impl->graphics.graphics_api == MGL_GRAPHICS_API_EGL)
        return mgl_graphics_get_display(&impl->graphics);
    else
        return NULL;
}

static void* mgl_window_wayland_get_egl_context(mgl_window *self) {
    mgl_window_wayland *impl = self->impl;
    if(impl->graphics.graphics_api == MGL_GRAPHICS_API_EGL)
        return mgl_graphics_get_context(&impl->graphics);
    else
        return NULL;
}

static void mgl_window_wayland_for_each_active_monitor_output(mgl_window *self, mgl_active_monitor_callback callback, void *userdata) {
    fprintf(stderr, "TODO: Implement\n");
}

bool mgl_window_wayland_init(mgl_window *self, const char *title, const mgl_window_create_params *params, mgl_window_handle existing_window) {
    mgl_window_wayland *impl = calloc(1, sizeof(mgl_window_wayland));
    if(!impl)
        return false;

    self->get_system_handle = mgl_window_wayland_get_system_handle;
    self->deinit = mgl_window_wayland_deinit;
    self->close = mgl_window_wayland_close;
    self->poll_event = mgl_window_wayland_poll_event;
    self->swap_buffers = mgl_window_wayland_swap_buffers;
    self->set_visible = mgl_window_wayland_set_visible;
    self->is_key_pressed = mgl_window_wayland_is_key_pressed;
    self->is_mouse_button_pressed = mgl_window_wayland_is_mouse_button_pressed;
    self->set_title = mgl_window_wayland_set_title;
    self->set_cursor_visible = mgl_window_wayland_set_cursor_visible;
    self->set_vsync_enabled = mgl_window_wayland_set_vsync_enabled;
    self->is_vsync_enabled = mgl_window_wayland_is_vsync_enabled;
    self->set_fullscreen = mgl_window_wayland_set_fullscreen;
    self->is_fullscreen = mgl_window_wayland_is_fullscreen;
    self->set_position = mgl_window_wayland_set_position;
    self->set_size = mgl_window_wayland_set_size;
    self->set_size_limits = mgl_window_wayland_set_size_limits;
    self->set_clipboard = mgl_window_wayland_set_clipboard;
    self->get_clipboard = mgl_window_wayland_get_clipboard;
    self->set_key_repeat_enabled = mgl_window_wayland_set_key_repeat_enabled;
    self->flush = mgl_window_wayland_flush;
    self->get_egl_display = mgl_window_wayland_get_egl_display;
    self->get_egl_context = mgl_window_wayland_get_egl_context;
    self->for_each_active_monitor_output = mgl_window_wayland_for_each_active_monitor_output;
    self->impl = impl;

    if(!mgl_wayland_setup_window(self, title, params, existing_window)) {
        mgl_window_wayland_deinit(self);
        return false;
    }

    assert(!params || params->graphics_api == MGL_GRAPHICS_API_EGL);
    const bool alpha = params && params->support_alpha;
    if(!mgl_graphics_init(&impl->graphics, &(mgl_graphics_create_params){ .graphics_api = MGL_GRAPHICS_API_EGL, .alpha = alpha })) {
        mgl_window_wayland_deinit(self);
        return false;
    }

    if(!mgl_graphics_make_context_current(&impl->graphics, impl->window)) {
        fprintf(stderr, "mgl error: mgl_window_wayland_init: failed to make window context current\n");
        mgl_window_wayland_deinit(self);
        return false;
    }

    self->vsync_enabled = true;
    mgl_graphics_set_swap_interval(&impl->graphics, (mgl_window_handle)impl->window, self->vsync_enabled);

    mgl_context *context = mgl_get_context();
    context->current_window = self;
    return true;
}

void mgl_window_wayland_deinit(mgl_window *self) {
    mgl_window_wayland *impl = self->impl;
    if(!impl)
        return;

    mgl_graphics_deinit(&impl->graphics);
    mgl_window_wayland_close(self);

    if(impl->xdg_toplevel) {
        xdg_toplevel_destroy(impl->xdg_toplevel);
        impl->xdg_toplevel = NULL;
    }

    if(impl->xdg_surface) {
        xdg_surface_destroy(impl->xdg_surface);
        impl->xdg_surface = NULL;
    }

    if(impl->shell_surface) {
        wl_shell_surface_destroy(impl->shell_surface);
        impl->shell_surface = NULL;
    }

    if(impl->shell) {
        wl_shell_destroy(impl->shell);
        impl->shell = NULL;
    }

    if(impl->xdg_wm_base) {
        xdg_wm_base_destroy(impl->xdg_wm_base);
        impl->xdg_wm_base = NULL;
    }

    if(impl->compositor) {
        wl_compositor_destroy(impl->compositor);
        impl->compositor = NULL;
    }

    if(impl->registry) {
        wl_registry_destroy(impl->registry);
        impl->registry = NULL;
    }

    mgl_context *context = mgl_get_context();
    if(context->current_window == self)
        context->current_window = NULL;

    free(self->impl);
    self->impl = NULL;
}