blob: 608ada913d5328eaa5a1550178249f8a81ab5c6e [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;
90
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040091 c = (struct drm_compositor *) device->master->base.ec;
92 if (!c->vt_active)
93 return;
94
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040095 dx = 0;
96 dy = 0;
97 absolute_event = 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040098 x = device->master->base.x;
99 y = device->master->base.y;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400100
101 len = read(fd, &ev, sizeof ev);
102 if (len < 0 || len % sizeof e[0] != 0) {
103 /* FIXME: handle error... reopen device? */;
104 return;
105 }
106
107 e = ev;
108 end = (void *) ev + len;
109 for (e = ev; e < end; e++) {
110 /* Get the signed value, earlier kernels had this as unsigned */
111 value = e->value;
112
113 switch (e->type) {
114 case EV_REL:
115 switch (e->code) {
116 case REL_X:
117 dx += value;
118 break;
119
120 case REL_Y:
121 dy += value;
122 break;
123 }
124 break;
125
126 case EV_ABS:
127 absolute_event = 1;
128 switch (e->code) {
129 case ABS_X:
130 if (device->new_x) {
131 device->base_x = x - value;
132 device->new_x = 0;
133 }
134 x = device->base_x + value;
135 break;
136 case ABS_Y:
137 if (device->new_y) {
138 device->base_y = y - value;
139 device->new_y = 0;
140 }
141 y = device->base_y + value;
142 break;
143 }
144 break;
145
146 case EV_KEY:
147 if (value == 2)
148 break;
149
150 switch (e->code) {
151 case BTN_TOUCH:
152 case BTN_TOOL_PEN:
153 case BTN_TOOL_RUBBER:
154 case BTN_TOOL_BRUSH:
155 case BTN_TOOL_PENCIL:
156 case BTN_TOOL_AIRBRUSH:
157 case BTN_TOOL_FINGER:
158 case BTN_TOOL_MOUSE:
159 case BTN_TOOL_LENS:
160 if (device->tool == 0 && value) {
161 device->new_x = 1;
162 device->new_y = 1;
163 }
164 device->tool = value ? e->code : 0;
165 break;
166
167 case BTN_LEFT:
168 case BTN_RIGHT:
169 case BTN_MIDDLE:
170 case BTN_SIDE:
171 case BTN_EXTRA:
172 case BTN_FORWARD:
173 case BTN_BACK:
174 case BTN_TASK:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400175 notify_button(&device->master->base,
176 e->code, value);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400177 break;
178
179 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400180 notify_key(&device->master->base,
181 e->code, value);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400182 break;
183 }
184 }
185 }
186
187 if (dx != 0 || dy != 0)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400188 notify_motion(&device->master->base, x + dx, y + dy);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400189 if (absolute_event && device->tool)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400190 notify_motion(&device->master->base, x, y);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400191}
192
193static struct evdev_input_device *
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400194evdev_input_device_create(struct drm_input *master,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400195 struct wl_display *display, const char *path)
196{
197 struct evdev_input_device *device;
198 struct wl_event_loop *loop;
199
200 device = malloc(sizeof *device);
201 if (device == NULL)
202 return NULL;
203
204 device->tool = 1;
205 device->new_x = 1;
206 device->new_y = 1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400207 device->master = master;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400208
209 device->fd = open(path, O_RDONLY);
210 if (device->fd < 0) {
211 free(device);
212 fprintf(stderr, "couldn't create pointer for %s: %m\n", path);
213 return NULL;
214 }
215
216 loop = wl_display_get_event_loop(display);
217 device->source = wl_event_loop_add_fd(loop, device->fd,
218 WL_EVENT_READABLE,
219 evdev_input_device_data, device);
220 if (device->source == NULL) {
221 close(device->fd);
222 free(device);
223 return NULL;
224 }
225
226 return device;
227}
228
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400229static void
230drm_input_create(struct drm_compositor *c)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400231{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400232 struct drm_input *input;
233 struct udev_enumerate *e;
234 struct udev_list_entry *entry;
235 struct udev_device *device;
236 const char *path;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400237
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400238 input = malloc(sizeof *input);
239 if (input == NULL)
240 return;
241
242 memset(input, 0, sizeof *input);
243 wlsc_input_device_init(&input->base, &c->base);
244
245 e = udev_enumerate_new(c->udev);
246 udev_enumerate_add_match_subsystem(e, "input");
247 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
248 udev_enumerate_scan_devices(e);
249 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
250 path = udev_list_entry_get_name(entry);
251 device = udev_device_new_from_syspath(c->udev, path);
252 evdev_input_device_create(input, c->base.wl_display,
253 udev_device_get_devnode(device));
254 }
255 udev_enumerate_unref(e);
256
257 c->base.input_device = &input->base;
258}
259
260static void
261drm_compositor_present(struct wlsc_compositor *ec)
262{
263 struct drm_compositor *c = (struct drm_compositor *) ec;
264 struct drm_output *output;
265
266 wl_list_for_each(output, &ec->output_list, base.link) {
267 output->current ^= 1;
268
269 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
270 GL_COLOR_ATTACHMENT0,
271 GL_RENDERBUFFER,
272 output->rbo[output->current]);
273
274 drmModePageFlip(c->drm_fd, output->crtc_id,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400275 output->fb_id[output->current ^ 1],
276 DRM_MODE_PAGE_FLIP_EVENT, output);
277 }
278}
279
280static void
281page_flip_handler(int fd, unsigned int frame,
282 unsigned int sec, unsigned int usec, void *data)
283{
284 struct wlsc_output *output = data;
285 struct wlsc_compositor *compositor = output->compositor;
286 uint32_t msecs;
287
288 msecs = sec * 1000 + usec / 1000;
289 wlsc_compositor_finish_frame(compositor, msecs);
290}
291
292static void
293on_drm_input(int fd, uint32_t mask, void *data)
294{
295 drmEventContext evctx;
296
297 memset(&evctx, 0, sizeof evctx);
298 evctx.version = DRM_EVENT_CONTEXT_VERSION;
299 evctx.page_flip_handler = page_flip_handler;
300 drmHandleEvent(fd, &evctx);
301}
302
303static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400304init_egl(struct drm_compositor *ec, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400305{
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400306 EGLint major, minor, count;
307 EGLConfig config;
308 PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
309
310 static const EGLint config_attribs[] = {
311 EGL_SURFACE_TYPE, 0,
312 EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT,
313 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
314 EGL_NONE
315 };
316
317 get_typed_display_mesa =
318 (PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
319 if (get_typed_display_mesa == NULL) {
320 fprintf(stderr, "eglGetTypedDisplayMESA() not found\n");
321 return -1;
322 }
323
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400324 ec->base.base.device = strdup(udev_device_get_devnode(device));
325 ec->drm_fd = open(ec->base.base.device, O_RDWR);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400326 if (ec->drm_fd < 0) {
327 /* Probably permissions error */
328 fprintf(stderr, "couldn't open %s, skipping\n",
329 udev_device_get_devnode(device));
330 return -1;
331 }
332
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400333 ec->base.display = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA,
334 (void *) ec->drm_fd);
335 if (ec->base.display == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400336 fprintf(stderr, "failed to create display\n");
337 return -1;
338 }
339
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400340 if (!eglInitialize(ec->base.display, &major, &minor)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400341 fprintf(stderr, "failed to initialize display\n");
342 return -1;
343 }
344
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400345 if (!eglChooseConfig(ec->base.display,
346 config_attribs, &config, 1, &count) ||
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400347 count == 0) {
348 fprintf(stderr, "eglChooseConfig() failed\n");
349 return -1;
350 }
351
352 eglBindAPI(EGL_OPENGL_API);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400353 ec->base.context = eglCreateContext(ec->base.display,
354 config, EGL_NO_CONTEXT, NULL);
355 if (ec->base.context == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400356 fprintf(stderr, "failed to create context\n");
357 return -1;
358 }
359
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400360 if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE,
361 EGL_NO_SURFACE, ec->base.context)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400362 fprintf(stderr, "failed to make context current\n");
363 return -1;
364 }
365
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400366 return 0;
367}
368
369static drmModeModeInfo builtin_1024x768 = {
370 63500, /* clock */
371 1024, 1072, 1176, 1328, 0,
372 768, 771, 775, 798, 0,
373 59920,
374 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
375 0,
376 "1024x768"
377};
378
379static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400380create_output_for_connector(struct drm_compositor *ec,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400381 drmModeRes *resources,
382 drmModeConnector *connector)
383{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400384 struct drm_output *output;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400385 drmModeEncoder *encoder;
386 drmModeModeInfo *mode;
387 int i, ret;
388 EGLint handle, stride, attribs[] = {
389 EGL_WIDTH, 0,
390 EGL_HEIGHT, 0,
391 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
392 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
393 EGL_NONE
394 };
395
396 output = malloc(sizeof *output);
397 if (output == NULL)
398 return -1;
399
400 if (connector->count_modes > 0)
401 mode = &connector->modes[0];
402 else
403 mode = &builtin_1024x768;
404
405 encoder = drmModeGetEncoder(ec->drm_fd, connector->encoders[0]);
406 if (encoder == NULL) {
407 fprintf(stderr, "No encoder for connector.\n");
408 return -1;
409 }
410
411 for (i = 0; i < resources->count_crtcs; i++) {
412 if (encoder->possible_crtcs & (1 << i))
413 break;
414 }
415 if (i == resources->count_crtcs) {
416 fprintf(stderr, "No usable crtc for encoder.\n");
417 return -1;
418 }
419
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400420 memset(output, 0, sizeof *output);
421 wlsc_output_init(&output->base, &ec->base, 0, 0,
422 mode->hdisplay, mode->vdisplay);
423
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400424 output->crtc_id = resources->crtcs[i];
425 output->connector_id = connector->connector_id;
426 output->mode = *mode;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400427
428 drmModeFreeEncoder(encoder);
429
430 glGenRenderbuffers(2, output->rbo);
431 for (i = 0; i < 2; i++) {
432 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
433
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400434 attribs[1] = output->base.width;
435 attribs[3] = output->base.height;
436 output->image[i] =
437 eglCreateDRMImageMESA(ec->base.display, attribs);
438 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
439 output->image[i]);
440 eglExportDRMImageMESA(ec->base.display, output->image[i],
441 NULL, &handle, &stride);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400442
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400443 ret = drmModeAddFB(ec->drm_fd,
444 output->base.width, output->base.height,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400445 32, 32, stride, handle, &output->fb_id[i]);
446 if (ret) {
447 fprintf(stderr, "failed to add fb %d: %m\n", i);
448 return -1;
449 }
450 }
451
452 output->current = 0;
453 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
454 GL_COLOR_ATTACHMENT0,
455 GL_RENDERBUFFER,
456 output->rbo[output->current]);
457 ret = drmModeSetCrtc(ec->drm_fd, output->crtc_id,
458 output->fb_id[output->current ^ 1], 0, 0,
459 &output->connector_id, 1, &output->mode);
460 if (ret) {
461 fprintf(stderr, "failed to set mode: %m\n");
462 return -1;
463 }
464
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400465 wl_list_insert(ec->base.output_list.prev, &output->base.link);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400466
467 return 0;
468}
469
470static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400471create_outputs(struct drm_compositor *ec)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400472{
473 drmModeConnector *connector;
474 drmModeRes *resources;
475 int i;
476
477 resources = drmModeGetResources(ec->drm_fd);
478 if (!resources) {
479 fprintf(stderr, "drmModeGetResources failed\n");
480 return -1;
481 }
482
483 for (i = 0; i < resources->count_connectors; i++) {
484 connector = drmModeGetConnector(ec->drm_fd, resources->connectors[i]);
485 if (connector == NULL)
486 continue;
487
488 if (connector->connection == DRM_MODE_CONNECTED &&
489 (option_connector == 0 ||
490 connector->connector_id == option_connector))
491 if (create_output_for_connector(ec, resources, connector) < 0)
492 return -1;
493
494 drmModeFreeConnector(connector);
495 }
496
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400497 if (wl_list_empty(&ec->base.output_list)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400498 fprintf(stderr, "No currently active connector found.\n");
499 return -1;
500 }
501
502 drmModeFreeResources(resources);
503
504 return 0;
505}
506
507static void on_enter_vt(int signal_number, void *data)
508{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400509 struct drm_compositor *ec = data;
510 struct drm_output *output;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400511 int ret;
512
513 ret = drmSetMaster(ec->drm_fd);
514 if (ret) {
515 fprintf(stderr, "failed to set drm master\n");
516 kill(0, SIGTERM);
517 return;
518 }
519
520 fprintf(stderr, "enter vt\n");
521
522 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
523 ec->vt_active = 1;
524
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400525 wl_list_for_each(output, &ec->base.output_list, base.link) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400526 ret = drmModeSetCrtc(ec->drm_fd, output->crtc_id,
527 output->fb_id[output->current ^ 1], 0, 0,
528 &output->connector_id, 1, &output->mode);
529 if (ret)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400530 fprintf(stderr,
531 "failed to set mode for connector %d: %m\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400532 output->connector_id);
533 }
534}
535
536static void on_leave_vt(int signal_number, void *data)
537{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400538 struct drm_compositor *ec = data;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400539 int ret;
540
541 ret = drmDropMaster(ec->drm_fd);
542 if (ret) {
543 fprintf(stderr, "failed to drop drm master\n");
544 kill(0, SIGTERM);
545 return;
546 }
547
548 ioctl (ec->tty_fd, VT_RELDISP, 1);
549 ec->vt_active = 0;
550}
551
552static void
553on_tty_input(int fd, uint32_t mask, void *data)
554{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400555 struct drm_compositor *ec = data;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400556
557 /* Ignore input to tty. We get keyboard events from evdev
558 */
559 tcflush(ec->tty_fd, TCIFLUSH);
560}
561
562static void on_term_signal(int signal_number, void *data)
563{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400564 struct drm_compositor *ec = data;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400565
566 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
567 fprintf(stderr, "could not restore terminal to canonical mode\n");
568
569 exit(0);
570}
571
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400572static int setup_tty(struct drm_compositor *ec, struct wl_event_loop *loop)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400573{
574 struct termios raw_attributes;
575 struct vt_mode mode = { 0 };
576
577 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
578 if (ec->tty_fd <= 0) {
579 fprintf(stderr, "failed to open active tty: %m\n");
580 return -1;
581 }
582
583 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
584 fprintf(stderr, "could not get terminal attributes: %m\n");
585 return -1;
586 }
587
588 /* Ignore control characters and disable echo */
589 raw_attributes = ec->terminal_attributes;
590 cfmakeraw(&raw_attributes);
591
592 /* Fix up line endings to be normal (cfmakeraw hoses them) */
593 raw_attributes.c_oflag |= OPOST | OCRNL;
594
595 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
596 fprintf(stderr, "could not put terminal into raw mode: %m\n");
597
598 ec->term_signal_source =
599 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
600
601 ec->tty_input_source =
602 wl_event_loop_add_fd(loop, ec->tty_fd,
603 WL_EVENT_READABLE, on_tty_input, ec);
604
605 ec->vt_active = 1;
606 mode.mode = VT_PROCESS;
607 mode.relsig = SIGUSR1;
608 mode.acqsig = SIGUSR2;
609 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
610 fprintf(stderr, "failed to take control of vt handling\n");
611 }
612
613 ec->leave_vt_source =
614 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
615 ec->enter_vt_source =
616 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
617
618 return 0;
619}
620
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400621struct wlsc_compositor *
622drm_compositor_create(struct wl_display *display)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400623{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400624 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400625 struct udev_enumerate *e;
626 struct udev_list_entry *entry;
627 struct udev_device *device;
628 const char *path;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400629 struct wl_event_loop *loop;
630
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400631 ec = malloc(sizeof *ec);
632 if (ec == NULL)
633 return NULL;
634
635 memset(ec, 0, sizeof *ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400636 ec->udev = udev_new();
637 if (ec->udev == NULL) {
638 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400639 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400640 }
641
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400642 e = udev_enumerate_new(ec->udev);
643 udev_enumerate_add_match_subsystem(e, "drm");
644 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
645 udev_enumerate_scan_devices(e);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400646 device = NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400647 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
648 path = udev_list_entry_get_name(entry);
649 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400650 break;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400651 }
652 udev_enumerate_unref(e);
653
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400654 if (device == NULL) {
655 fprintf(stderr, "no drm device found\n");
656 return NULL;
657 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400658
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400659 if (init_egl(ec, device) < 0) {
660 fprintf(stderr, "failed to initialize egl\n");
661 return NULL;
662 }
663
664 /* Can't init base class until we have a current egl context */
665 wlsc_compositor_init(&ec->base, display);
666
667 if (create_outputs(ec) < 0) {
668 fprintf(stderr, "failed to create output for %s\n", path);
669 return NULL;
670 }
671
672 drm_input_create(ec);
673
674 loop = wl_display_get_event_loop(ec->base.wl_display);
675 ec->drm_source =
676 wl_event_loop_add_fd(loop, ec->drm_fd,
677 WL_EVENT_READABLE, on_drm_input, ec);
678 setup_tty(ec, loop);
679 ec->base.present = drm_compositor_present;
680
681 return &ec->base;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400682}