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
|
/*
* Copyright © 2010 Benjamin Franzke
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stddef.h>
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include "wayland-client.h"
#define GL_GLEXT_PROTOTYPES
#define EGL_EGLEXT_PROTOTYPES
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include "compositor.h"
struct wayland_compositor {
struct wlsc_compositor base;
struct {
struct wl_display *display;
struct wl_compositor *compositor;
struct wl_shell *shell;
struct wl_drm *drm;
struct wl_output *output;
struct {
int32_t x, y, width, height;
} screen_allocation;
char *device_name;
int authenticated;
struct wl_event_source *wl_source;
uint32_t event_mask;
} parent;
struct wl_list window_list;
struct wl_list input_list;
};
struct wayland_output {
struct wlsc_output base;
struct {
struct wl_surface *surface;
struct wl_buffer *buffer[2];
} parent;
EGLImageKHR image[2];
GLuint rbo[2];
uint32_t fb_id[2];
uint32_t current;
};
struct wayland_input {
struct wayland_compositor *compositor;
struct wl_input_device *input_device;
struct wl_list link;
};
static int
wayland_input_create(struct wayland_compositor *c)
{
struct wlsc_input_device *input;
input = malloc(sizeof *input);
if (input == NULL)
return -1;
memset(input, 0, sizeof *input);
wlsc_input_device_init(input, &c->base);
c->base.input_device = &input->input_device;
return 0;
}
static int
wayland_compositor_init_egl(struct wayland_compositor *c)
{
EGLint major, minor;
const char *extensions;
drm_magic_t magic;
int fd;
static const EGLint context_attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
fd = open(c->parent.device_name, O_RDWR);
if (fd < 0) {
fprintf(stderr, "drm open failed: %m\n");
return -1;
}
wlsc_drm_init(&c->base, fd, c->parent.device_name);
if (drmGetMagic(fd, &magic)) {
fprintf(stderr, "DRI2: failed to get drm magic");
return -1;
}
wl_drm_authenticate(c->parent.drm, magic);
wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
while (!c->parent.authenticated)
wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
c->base.display = eglGetDRMDisplayMESA(fd);
if (c->base.display == NULL) {
fprintf(stderr, "failed to create display\n");
return -1;
}
if (!eglInitialize(c->base.display, &major, &minor)) {
fprintf(stderr, "failed to initialize display\n");
return -1;
}
extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
return -1;
}
if (!eglBindAPI(EGL_OPENGL_ES_API)) {
fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
return -1;
}
c->base.context = eglCreateContext(c->base.display, NULL,
EGL_NO_CONTEXT, context_attribs);
if (c->base.context == NULL) {
fprintf(stderr, "failed to create context\n");
return -1;
}
if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
EGL_NO_SURFACE, c->base.context)) {
fprintf(stderr, "failed to make context current\n");
return -1;
}
return 0;
}
static void
frame_callback(void *data, uint32_t time)
{
struct wayland_compositor *c = (struct wayland_compositor *) data;
wlsc_compositor_finish_frame(&c->base, time);
}
static void
wayland_compositor_present(struct wlsc_compositor *base)
{
struct wayland_compositor *c = (struct wayland_compositor *) base;
struct wayland_output *output;
glFlush();
wl_list_for_each(output, &base->output_list, base.link) {
output->current ^= 1;
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_RENDERBUFFER,
output->rbo[output->current]);
wl_surface_attach(output->parent.surface,
output->parent.buffer[output->current ^ 1]);
wl_surface_damage(output->parent.surface, 0, 0,
output->base.width, output->base.height);
}
wl_display_frame_callback(c->parent.display, frame_callback, c);
}
static int
wayland_authenticate(struct wlsc_compositor *ec, uint32_t id)
{
struct wayland_compositor *c = (struct wayland_compositor *) ec;
wl_drm_authenticate(c->parent.drm, id);
/* FIXME: recv drm_authenticated event from parent? */
wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
return 0;
}
static int
wayland_compositor_create_output(struct wayland_compositor *c,
int width, int height)
{
struct wayland_output *output;
struct wl_visual *visual;
int i;
EGLint name, stride, attribs[] = {
EGL_WIDTH, 0,
EGL_HEIGHT, 0,
EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
EGL_NONE
};
output = malloc(sizeof *output);
if (output == NULL)
return -1;
memset(output, 0, sizeof *output);
wlsc_output_init(&output->base, &c->base, 0, 0, width, height);
output->parent.surface =
wl_compositor_create_surface(c->parent.compositor);
wl_surface_set_user_data(output->parent.surface, output);
glGenRenderbuffers(2, output->rbo);
visual = wl_display_get_premultiplied_argb_visual(c->parent.display);
for (i = 0; i < 2; i++) {
glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
attribs[1] = width;
attribs[3] = height;
output->image[i] =
eglCreateDRMImageMESA(c->base.display, attribs);
glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
output->image[i]);
eglExportDRMImageMESA(c->base.display, output->image[i],
&name, NULL, &stride);
output->parent.buffer[i] =
wl_drm_create_buffer(c->parent.drm, name,
width, height,
stride, visual);
}
output->current = 0;
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_RENDERBUFFER,
output->rbo[output->current]);
wl_surface_attach(output->parent.surface,
output->parent.buffer[output->current]);
wl_surface_map(output->parent.surface,
output->base.x, output->base.y,
output->base.width, output->base.height);
glClearColor(0, 0, 0, 0.5);
wl_list_insert(c->base.output_list.prev, &output->base.link);
return 0;
}
static struct wl_buffer *
create_invisible_pointer(struct wayland_compositor *c)
{
struct wlsc_drm_buffer *wlsc_buffer;
struct wl_buffer *buffer;
int name, stride;
struct wl_visual *visual;
GLuint texture;
const int width = 1, height = 1;
const GLubyte data[] = { 0, 0, 0, 0 };
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
visual = wl_display_get_premultiplied_argb_visual(c->parent.display);
wlsc_buffer = wlsc_drm_buffer_create(&c->base, width, height, visual);
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, wlsc_buffer->image);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
GL_RGBA, GL_UNSIGNED_BYTE, data);
eglExportDRMImageMESA(c->base.display, wlsc_buffer->image,
&name, NULL, &stride);
buffer = wl_drm_create_buffer(c->parent.drm, name,
width, height,
stride, visual);
return buffer;
}
/* Events received from the wayland-server this compositor is client of: */
/* parent output interface */
static void
display_handle_geometry(void *data,
struct wl_output *output,
int32_t x, int32_t y,
int32_t width, int32_t height)
{
struct wayland_compositor *c = data;
c->parent.screen_allocation.x = x;
c->parent.screen_allocation.y = y;
c->parent.screen_allocation.width = width;
c->parent.screen_allocation.height = height;
}
static const struct wl_output_listener output_listener = {
display_handle_geometry,
};
/* parent shell interface */
static void
handle_configure(void *data, struct wl_shell *shell,
uint32_t time, uint32_t edges,
struct wl_surface *surface,
int32_t x, int32_t y, int32_t width, int32_t height)
{
#if 0
struct output *output = wl_surface_get_user_data(surface);
/* FIXME: add resize? */
#endif
}
static const struct wl_shell_listener shell_listener = {
handle_configure,
};
/* parent drm interface */
static void
drm_handle_device(void *data, struct wl_drm *drm, const char *device)
{
struct wayland_compositor *c = data;
c->parent.device_name = strdup(device);
}
static void drm_handle_authenticated(void *data, struct wl_drm *drm)
{
struct wayland_compositor *c = data;
c->parent.authenticated = 1;
}
static const struct wl_drm_listener drm_listener = {
drm_handle_device,
drm_handle_authenticated
};
/* parent input interface */
static void
input_handle_motion(void *data, struct wl_input_device *input_device,
uint32_t time,
int32_t x, int32_t y, int32_t sx, int32_t sy)
{
struct wayland_input *input = data;
struct wayland_compositor *c = input->compositor;
notify_motion(c->base.input_device, time, sx, sy);
}
static void
input_handle_button(void *data,
struct wl_input_device *input_device,
uint32_t time, uint32_t button, uint32_t state)
{
struct wayland_input *input = data;
struct wayland_compositor *c = input->compositor;
notify_button(c->base.input_device, time, button, state);
}
static void
input_handle_key(void *data, struct wl_input_device *input_device,
uint32_t time, uint32_t key, uint32_t state)
{
struct wayland_input *input = data;
struct wayland_compositor *c = input->compositor;
notify_key(c->base.input_device, time, key, state);
}
static void
input_handle_pointer_focus(void *data,
struct wl_input_device *input_device,
uint32_t time, struct wl_surface *surface,
int32_t x, int32_t y, int32_t sx, int32_t sy)
{
struct wayland_input *input = data;
struct wayland_compositor *c = input->compositor;
static struct wl_buffer *pntr_buffer = NULL;
if (surface) {
c->base.focus = 1;
/* FIXME: extend protocol to allow hiding the cursor? */
if (pntr_buffer == NULL)
pntr_buffer = create_invisible_pointer(c);
wl_input_device_attach(input_device, time, pntr_buffer, x, y);
} else {
/* FIXME. hide our own pointer */
c->base.focus = 0;
}
}
static void
input_handle_keyboard_focus(void *data,
struct wl_input_device *input_device,
uint32_t time,
struct wl_surface *surface,
struct wl_array *keys)
{
/* FIXME: sth to be done here? */
}
static const struct wl_input_device_listener input_device_listener = {
input_handle_motion,
input_handle_button,
input_handle_key,
input_handle_pointer_focus,
input_handle_keyboard_focus,
};
static void
display_add_input(struct wayland_compositor *c, uint32_t id)
{
struct wayland_input *input;
input = malloc(sizeof *input);
if (input == NULL)
return;
memset(input, 0, sizeof *input);
input->compositor = c;
input->input_device = wl_input_device_create(c->parent.display, id);
wl_list_insert(c->input_list.prev, &input->link);
wl_input_device_add_listener(input->input_device,
&input_device_listener, input);
wl_input_device_set_user_data(input->input_device, input);
}
static void
display_handle_global(struct wl_display *display, uint32_t id,
const char *interface, uint32_t version, void *data)
{
struct wayland_compositor *c = data;
if (strcmp(interface, "compositor") == 0) {
c->parent.compositor = wl_compositor_create(display, id);
} else if (strcmp(interface, "output") == 0) {
c->parent.output = wl_output_create(display, id);
wl_output_add_listener(c->parent.output, &output_listener, c);
} else if (strcmp(interface, "input_device") == 0) {
display_add_input(c, id);
} else if (strcmp(interface, "shell") == 0) {
c->parent.shell = wl_shell_create(display, id);
wl_shell_add_listener(c->parent.shell, &shell_listener, c);
} else if (strcmp(interface, "drm") == 0) {
c->parent.drm = wl_drm_create(display, id);
wl_drm_add_listener(c->parent.drm, &drm_listener, c);
}
}
static int
update_event_mask(uint32_t mask, void *data)
{
struct wayland_compositor *c = data;
c->parent.event_mask = mask;
if (c->parent.wl_source)
wl_event_source_fd_update(c->parent.wl_source, mask);
return 0;
}
static void
wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
{
struct wayland_compositor *c = data;
if (mask & WL_EVENT_READABLE)
wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
if (mask & WL_EVENT_WRITEABLE)
wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
}
static void
wayland_destroy(struct wlsc_compositor *ec)
{
free(ec);
}
struct wlsc_compositor *
wayland_compositor_create(struct wl_display *display, int width, int height)
{
struct wayland_compositor *c;
struct wl_event_loop *loop;
int fd;
c = malloc(sizeof *c);
if (c == NULL)
return NULL;
memset(c, 0, sizeof *c);
c->parent.display = wl_display_connect(NULL);
if (c->parent.display == NULL) {
fprintf(stderr, "failed to create display: %m\n");
return NULL;
}
wl_list_init(&c->input_list);
wl_display_add_global_listener(c->parent.display,
display_handle_global, c);
wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
c->base.wl_display = display;
if (wayland_compositor_init_egl(c) < 0)
return NULL;
/* Can't init base class until we have a current egl context */
if (wlsc_compositor_init(&c->base, display) < 0)
return NULL;
if (wayland_compositor_create_output(c, width, height) < 0)
return NULL;
if (wayland_input_create(c) < 0)
return NULL;
loop = wl_display_get_event_loop(c->base.wl_display);
fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
c->parent.wl_source =
wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
wayland_compositor_handle_event, c);
if (c->parent.wl_source == NULL)
return NULL;
c->base.destroy = wayland_destroy;
c->base.authenticate = wayland_authenticate;
c->base.present = wayland_compositor_present;
return &c->base;
}
|