blob: 05ab02ac5e975001bd5448a7ad90cbd1f7bfba61 [file] [log] [blame]
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001/*
2 * Copyright © 2008-2010 Kristian Høgsberg
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <fcntl.h>
23#include <unistd.h>
24
25#include <signal.h>
26#include <linux/vt.h>
27#include <linux/input.h>
28
29#define GL_GLEXT_PROTOTYPES
30#define EGL_EGLEXT_PROTOTYPES
31#include <GLES2/gl2.h>
32#include <GLES2/gl2ext.h>
33#include <EGL/egl.h>
34#include <EGL/eglext.h>
35
36#include "wayland.h"
37#include "wayland-protocol.h"
38#include "compositor.h"
39
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040040struct drm_compositor {
41 struct wlsc_compositor base;
42
43 struct udev *udev;
44 struct wl_event_source *drm_source;
45 int drm_fd;
46
47 struct wl_event_source *term_signal_source;
48
49 /* tty handling state */
50 int tty_fd;
51 uint32_t vt_active : 1;
52
53 struct termios terminal_attributes;
54 struct wl_event_source *tty_input_source;
55 struct wl_event_source *enter_vt_source;
56 struct wl_event_source *leave_vt_source;
57};
58
59struct drm_output {
60 struct wlsc_output base;
61
62 drmModeModeInfo mode;
63 uint32_t crtc_id;
64 uint32_t connector_id;
65 GLuint rbo[2];
66 uint32_t fb_id[2];
67 EGLImageKHR image[2];
68 uint32_t current;
69};
70
71struct drm_input {
72 struct wlsc_input_device base;
73};
74
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040075struct evdev_input_device {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040076 struct drm_input *master;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040077 struct wl_event_source *source;
78 int tool, new_x, new_y;
79 int base_x, base_y;
80 int fd;
81};
82
83static void evdev_input_device_data(int fd, uint32_t mask, void *data)
84{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040085 struct drm_compositor *c;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040086 struct evdev_input_device *device = data;
87 struct input_event ev[8], *e, *end;
88 int len, value, dx, dy, absolute_event;
89 int x, y;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040090 uint32_t time;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040091
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040092 c = (struct drm_compositor *) device->master->base.ec;
93 if (!c->vt_active)
94 return;
95
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040096 dx = 0;
97 dy = 0;
98 absolute_event = 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040099 x = device->master->base.x;
100 y = device->master->base.y;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400101
102 len = read(fd, &ev, sizeof ev);
103 if (len < 0 || len % sizeof e[0] != 0) {
104 /* FIXME: handle error... reopen device? */;
105 return;
106 }
107
108 e = ev;
109 end = (void *) ev + len;
110 for (e = ev; e < end; e++) {
111 /* Get the signed value, earlier kernels had this as unsigned */
112 value = e->value;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400113 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400114
115 switch (e->type) {
116 case EV_REL:
117 switch (e->code) {
118 case REL_X:
119 dx += value;
120 break;
121
122 case REL_Y:
123 dy += value;
124 break;
125 }
126 break;
127
128 case EV_ABS:
129 absolute_event = 1;
130 switch (e->code) {
131 case ABS_X:
132 if (device->new_x) {
133 device->base_x = x - value;
134 device->new_x = 0;
135 }
136 x = device->base_x + value;
137 break;
138 case ABS_Y:
139 if (device->new_y) {
140 device->base_y = y - value;
141 device->new_y = 0;
142 }
143 y = device->base_y + value;
144 break;
145 }
146 break;
147
148 case EV_KEY:
149 if (value == 2)
150 break;
151
152 switch (e->code) {
153 case BTN_TOUCH:
154 case BTN_TOOL_PEN:
155 case BTN_TOOL_RUBBER:
156 case BTN_TOOL_BRUSH:
157 case BTN_TOOL_PENCIL:
158 case BTN_TOOL_AIRBRUSH:
159 case BTN_TOOL_FINGER:
160 case BTN_TOOL_MOUSE:
161 case BTN_TOOL_LENS:
162 if (device->tool == 0 && value) {
163 device->new_x = 1;
164 device->new_y = 1;
165 }
166 device->tool = value ? e->code : 0;
167 break;
168
169 case BTN_LEFT:
170 case BTN_RIGHT:
171 case BTN_MIDDLE:
172 case BTN_SIDE:
173 case BTN_EXTRA:
174 case BTN_FORWARD:
175 case BTN_BACK:
176 case BTN_TASK:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400177 notify_button(&device->master->base,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400178 time, e->code, value);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400179 break;
180
181 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400182 notify_key(&device->master->base,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400183 time, e->code, value);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400184 break;
185 }
186 }
187 }
188
189 if (dx != 0 || dy != 0)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400190 notify_motion(&device->master->base, time, x + dx, y + dy);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400191 if (absolute_event && device->tool)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400192 notify_motion(&device->master->base, time, x, y);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400193}
194
195static struct evdev_input_device *
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400196evdev_input_device_create(struct drm_input *master,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400197 struct wl_display *display, const char *path)
198{
199 struct evdev_input_device *device;
200 struct wl_event_loop *loop;
201
202 device = malloc(sizeof *device);
203 if (device == NULL)
204 return NULL;
205
206 device->tool = 1;
207 device->new_x = 1;
208 device->new_y = 1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400209 device->master = master;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400210
211 device->fd = open(path, O_RDONLY);
212 if (device->fd < 0) {
213 free(device);
214 fprintf(stderr, "couldn't create pointer for %s: %m\n", path);
215 return NULL;
216 }
217
218 loop = wl_display_get_event_loop(display);
219 device->source = wl_event_loop_add_fd(loop, device->fd,
220 WL_EVENT_READABLE,
221 evdev_input_device_data, device);
222 if (device->source == NULL) {
223 close(device->fd);
224 free(device);
225 return NULL;
226 }
227
228 return device;
229}
230
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400231static void
232drm_input_create(struct drm_compositor *c)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400233{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400234 struct drm_input *input;
235 struct udev_enumerate *e;
236 struct udev_list_entry *entry;
237 struct udev_device *device;
238 const char *path;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400239
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400240 input = malloc(sizeof *input);
241 if (input == NULL)
242 return;
243
244 memset(input, 0, sizeof *input);
245 wlsc_input_device_init(&input->base, &c->base);
246
247 e = udev_enumerate_new(c->udev);
248 udev_enumerate_add_match_subsystem(e, "input");
249 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
250 udev_enumerate_scan_devices(e);
251 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
252 path = udev_list_entry_get_name(entry);
253 device = udev_device_new_from_syspath(c->udev, path);
254 evdev_input_device_create(input, c->base.wl_display,
255 udev_device_get_devnode(device));
256 }
257 udev_enumerate_unref(e);
258
259 c->base.input_device = &input->base;
260}
261
262static void
263drm_compositor_present(struct wlsc_compositor *ec)
264{
265 struct drm_compositor *c = (struct drm_compositor *) ec;
266 struct drm_output *output;
267
268 wl_list_for_each(output, &ec->output_list, base.link) {
269 output->current ^= 1;
270
271 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
272 GL_COLOR_ATTACHMENT0,
273 GL_RENDERBUFFER,
274 output->rbo[output->current]);
275
276 drmModePageFlip(c->drm_fd, output->crtc_id,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400277 output->fb_id[output->current ^ 1],
278 DRM_MODE_PAGE_FLIP_EVENT, output);
279 }
280}
281
282static void
283page_flip_handler(int fd, unsigned int frame,
284 unsigned int sec, unsigned int usec, void *data)
285{
286 struct wlsc_output *output = data;
287 struct wlsc_compositor *compositor = output->compositor;
288 uint32_t msecs;
289
290 msecs = sec * 1000 + usec / 1000;
291 wlsc_compositor_finish_frame(compositor, msecs);
292}
293
294static void
295on_drm_input(int fd, uint32_t mask, void *data)
296{
297 drmEventContext evctx;
298
299 memset(&evctx, 0, sizeof evctx);
300 evctx.version = DRM_EVENT_CONTEXT_VERSION;
301 evctx.page_flip_handler = page_flip_handler;
302 drmHandleEvent(fd, &evctx);
303}
304
305static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400306init_egl(struct drm_compositor *ec, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400307{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400308 EGLint major, minor;
309 const char *extensions;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400310 static const EGLint context_attribs[] = {
311 EGL_CONTEXT_CLIENT_VERSION, 2,
312 EGL_NONE
313 };
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400314
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400315 ec->base.base.device = strdup(udev_device_get_devnode(device));
316 ec->drm_fd = open(ec->base.base.device, O_RDWR);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400317 if (ec->drm_fd < 0) {
318 /* Probably permissions error */
319 fprintf(stderr, "couldn't open %s, skipping\n",
320 udev_device_get_devnode(device));
321 return -1;
322 }
323
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -0400324 ec->base.display = eglGetDRMDisplayMESA(ec->drm_fd);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400325 if (ec->base.display == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400326 fprintf(stderr, "failed to create display\n");
327 return -1;
328 }
329
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400330 if (!eglInitialize(ec->base.display, &major, &minor)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400331 fprintf(stderr, "failed to initialize display\n");
332 return -1;
333 }
334
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400335 extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS);
336 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
337 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400338 return -1;
339 }
340
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400341 eglBindAPI(EGL_OPENGL_ES_API);
342 ec->base.context = eglCreateContext(ec->base.display, NULL,
343 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400344 if (ec->base.context == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400345 fprintf(stderr, "failed to create context\n");
346 return -1;
347 }
348
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400349 if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE,
350 EGL_NO_SURFACE, ec->base.context)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400351 fprintf(stderr, "failed to make context current\n");
352 return -1;
353 }
354
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400355 return 0;
356}
357
358static drmModeModeInfo builtin_1024x768 = {
359 63500, /* clock */
360 1024, 1072, 1176, 1328, 0,
361 768, 771, 775, 798, 0,
362 59920,
363 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
364 0,
365 "1024x768"
366};
367
368static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400369create_output_for_connector(struct drm_compositor *ec,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400370 drmModeRes *resources,
371 drmModeConnector *connector)
372{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400373 struct drm_output *output;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400374 drmModeEncoder *encoder;
375 drmModeModeInfo *mode;
376 int i, ret;
377 EGLint handle, stride, attribs[] = {
378 EGL_WIDTH, 0,
379 EGL_HEIGHT, 0,
380 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
381 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
382 EGL_NONE
383 };
384
385 output = malloc(sizeof *output);
386 if (output == NULL)
387 return -1;
388
389 if (connector->count_modes > 0)
390 mode = &connector->modes[0];
391 else
392 mode = &builtin_1024x768;
393
394 encoder = drmModeGetEncoder(ec->drm_fd, connector->encoders[0]);
395 if (encoder == NULL) {
396 fprintf(stderr, "No encoder for connector.\n");
397 return -1;
398 }
399
400 for (i = 0; i < resources->count_crtcs; i++) {
401 if (encoder->possible_crtcs & (1 << i))
402 break;
403 }
404 if (i == resources->count_crtcs) {
405 fprintf(stderr, "No usable crtc for encoder.\n");
406 return -1;
407 }
408
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400409 memset(output, 0, sizeof *output);
410 wlsc_output_init(&output->base, &ec->base, 0, 0,
411 mode->hdisplay, mode->vdisplay);
412
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400413 output->crtc_id = resources->crtcs[i];
414 output->connector_id = connector->connector_id;
415 output->mode = *mode;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400416
417 drmModeFreeEncoder(encoder);
418
419 glGenRenderbuffers(2, output->rbo);
420 for (i = 0; i < 2; i++) {
421 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
422
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400423 attribs[1] = output->base.width;
424 attribs[3] = output->base.height;
425 output->image[i] =
426 eglCreateDRMImageMESA(ec->base.display, attribs);
427 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
428 output->image[i]);
429 eglExportDRMImageMESA(ec->base.display, output->image[i],
430 NULL, &handle, &stride);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400431
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400432 ret = drmModeAddFB(ec->drm_fd,
433 output->base.width, output->base.height,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400434 32, 32, stride, handle, &output->fb_id[i]);
435 if (ret) {
436 fprintf(stderr, "failed to add fb %d: %m\n", i);
437 return -1;
438 }
439 }
440
441 output->current = 0;
442 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
443 GL_COLOR_ATTACHMENT0,
444 GL_RENDERBUFFER,
445 output->rbo[output->current]);
446 ret = drmModeSetCrtc(ec->drm_fd, output->crtc_id,
447 output->fb_id[output->current ^ 1], 0, 0,
448 &output->connector_id, 1, &output->mode);
449 if (ret) {
450 fprintf(stderr, "failed to set mode: %m\n");
451 return -1;
452 }
453
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400454 wl_list_insert(ec->base.output_list.prev, &output->base.link);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400455
456 return 0;
457}
458
459static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400460create_outputs(struct drm_compositor *ec)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400461{
462 drmModeConnector *connector;
463 drmModeRes *resources;
464 int i;
465
466 resources = drmModeGetResources(ec->drm_fd);
467 if (!resources) {
468 fprintf(stderr, "drmModeGetResources failed\n");
469 return -1;
470 }
471
472 for (i = 0; i < resources->count_connectors; i++) {
473 connector = drmModeGetConnector(ec->drm_fd, resources->connectors[i]);
474 if (connector == NULL)
475 continue;
476
477 if (connector->connection == DRM_MODE_CONNECTED &&
478 (option_connector == 0 ||
479 connector->connector_id == option_connector))
480 if (create_output_for_connector(ec, resources, connector) < 0)
481 return -1;
482
483 drmModeFreeConnector(connector);
484 }
485
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400486 if (wl_list_empty(&ec->base.output_list)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400487 fprintf(stderr, "No currently active connector found.\n");
488 return -1;
489 }
490
491 drmModeFreeResources(resources);
492
493 return 0;
494}
495
496static void on_enter_vt(int signal_number, void *data)
497{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400498 struct drm_compositor *ec = data;
499 struct drm_output *output;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400500 int ret;
501
502 ret = drmSetMaster(ec->drm_fd);
503 if (ret) {
504 fprintf(stderr, "failed to set drm master\n");
505 kill(0, SIGTERM);
506 return;
507 }
508
509 fprintf(stderr, "enter vt\n");
510
511 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
512 ec->vt_active = 1;
513
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400514 wl_list_for_each(output, &ec->base.output_list, base.link) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400515 ret = drmModeSetCrtc(ec->drm_fd, output->crtc_id,
516 output->fb_id[output->current ^ 1], 0, 0,
517 &output->connector_id, 1, &output->mode);
518 if (ret)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400519 fprintf(stderr,
520 "failed to set mode for connector %d: %m\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400521 output->connector_id);
522 }
523}
524
525static void on_leave_vt(int signal_number, void *data)
526{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400527 struct drm_compositor *ec = data;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400528 int ret;
529
530 ret = drmDropMaster(ec->drm_fd);
531 if (ret) {
532 fprintf(stderr, "failed to drop drm master\n");
533 kill(0, SIGTERM);
534 return;
535 }
536
537 ioctl (ec->tty_fd, VT_RELDISP, 1);
538 ec->vt_active = 0;
539}
540
541static void
542on_tty_input(int fd, uint32_t mask, void *data)
543{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400544 struct drm_compositor *ec = data;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400545
546 /* Ignore input to tty. We get keyboard events from evdev
547 */
548 tcflush(ec->tty_fd, TCIFLUSH);
549}
550
551static void on_term_signal(int signal_number, void *data)
552{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400553 struct drm_compositor *ec = data;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400554
555 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
556 fprintf(stderr, "could not restore terminal to canonical mode\n");
557
558 exit(0);
559}
560
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400561static int setup_tty(struct drm_compositor *ec, struct wl_event_loop *loop)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400562{
563 struct termios raw_attributes;
564 struct vt_mode mode = { 0 };
565
566 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
567 if (ec->tty_fd <= 0) {
568 fprintf(stderr, "failed to open active tty: %m\n");
569 return -1;
570 }
571
572 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
573 fprintf(stderr, "could not get terminal attributes: %m\n");
574 return -1;
575 }
576
577 /* Ignore control characters and disable echo */
578 raw_attributes = ec->terminal_attributes;
579 cfmakeraw(&raw_attributes);
580
581 /* Fix up line endings to be normal (cfmakeraw hoses them) */
582 raw_attributes.c_oflag |= OPOST | OCRNL;
583
584 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
585 fprintf(stderr, "could not put terminal into raw mode: %m\n");
586
587 ec->term_signal_source =
588 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
589
590 ec->tty_input_source =
591 wl_event_loop_add_fd(loop, ec->tty_fd,
592 WL_EVENT_READABLE, on_tty_input, ec);
593
594 ec->vt_active = 1;
595 mode.mode = VT_PROCESS;
596 mode.relsig = SIGUSR1;
597 mode.acqsig = SIGUSR2;
598 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
599 fprintf(stderr, "failed to take control of vt handling\n");
600 }
601
602 ec->leave_vt_source =
603 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
604 ec->enter_vt_source =
605 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
606
607 return 0;
608}
609
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400610struct wlsc_compositor *
611drm_compositor_create(struct wl_display *display)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400612{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400613 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400614 struct udev_enumerate *e;
615 struct udev_list_entry *entry;
616 struct udev_device *device;
617 const char *path;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400618 struct wl_event_loop *loop;
619
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400620 ec = malloc(sizeof *ec);
621 if (ec == NULL)
622 return NULL;
623
624 memset(ec, 0, sizeof *ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400625 ec->udev = udev_new();
626 if (ec->udev == NULL) {
627 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400628 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400629 }
630
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400631 e = udev_enumerate_new(ec->udev);
632 udev_enumerate_add_match_subsystem(e, "drm");
633 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
634 udev_enumerate_scan_devices(e);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400635 device = NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400636 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
637 path = udev_list_entry_get_name(entry);
638 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400639 break;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400640 }
641 udev_enumerate_unref(e);
642
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400643 if (device == NULL) {
644 fprintf(stderr, "no drm device found\n");
645 return NULL;
646 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400647
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400648 if (init_egl(ec, device) < 0) {
649 fprintf(stderr, "failed to initialize egl\n");
650 return NULL;
651 }
652
653 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400654 if (wlsc_compositor_init(&ec->base, display) < 0)
655 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400656
657 if (create_outputs(ec) < 0) {
658 fprintf(stderr, "failed to create output for %s\n", path);
659 return NULL;
660 }
661
662 drm_input_create(ec);
663
664 loop = wl_display_get_event_loop(ec->base.wl_display);
665 ec->drm_source =
666 wl_event_loop_add_fd(loop, ec->drm_fd,
667 WL_EVENT_READABLE, on_drm_input, ec);
668 setup_tty(ec, loop);
669 ec->base.present = drm_compositor_present;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400670 ec->base.focus = 1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400671
672 return &ec->base;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400673}