blob: 2018e5c084e28c319bc9c317edf66265da8aadfc [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
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040036#include "compositor.h"
37
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040038struct drm_compositor {
39 struct wlsc_compositor base;
40
41 struct udev *udev;
42 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040043
44 struct wl_event_source *term_signal_source;
45
46 /* tty handling state */
47 int tty_fd;
48 uint32_t vt_active : 1;
49
50 struct termios terminal_attributes;
51 struct wl_event_source *tty_input_source;
52 struct wl_event_source *enter_vt_source;
53 struct wl_event_source *leave_vt_source;
54};
55
56struct drm_output {
57 struct wlsc_output base;
58
59 drmModeModeInfo mode;
60 uint32_t crtc_id;
61 uint32_t connector_id;
62 GLuint rbo[2];
63 uint32_t fb_id[2];
64 EGLImageKHR image[2];
65 uint32_t current;
66};
67
68struct drm_input {
69 struct wlsc_input_device base;
70};
71
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040072struct evdev_input_device {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040073 struct drm_input *master;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040074 struct wl_event_source *source;
75 int tool, new_x, new_y;
76 int base_x, base_y;
77 int fd;
78};
79
80static void evdev_input_device_data(int fd, uint32_t mask, void *data)
81{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040082 struct drm_compositor *c;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040083 struct evdev_input_device *device = data;
84 struct input_event ev[8], *e, *end;
85 int len, value, dx, dy, absolute_event;
86 int x, y;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040087 uint32_t time;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040088
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040089 c = (struct drm_compositor *) device->master->base.ec;
90 if (!c->vt_active)
91 return;
92
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040093 dx = 0;
94 dy = 0;
95 absolute_event = 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040096 x = device->master->base.x;
97 y = device->master->base.y;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040098
99 len = read(fd, &ev, sizeof ev);
100 if (len < 0 || len % sizeof e[0] != 0) {
101 /* FIXME: handle error... reopen device? */;
102 return;
103 }
104
105 e = ev;
106 end = (void *) ev + len;
107 for (e = ev; e < end; e++) {
108 /* Get the signed value, earlier kernels had this as unsigned */
109 value = e->value;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400110 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400111
112 switch (e->type) {
113 case EV_REL:
114 switch (e->code) {
115 case REL_X:
116 dx += value;
117 break;
118
119 case REL_Y:
120 dy += value;
121 break;
122 }
123 break;
124
125 case EV_ABS:
126 absolute_event = 1;
127 switch (e->code) {
128 case ABS_X:
129 if (device->new_x) {
130 device->base_x = x - value;
131 device->new_x = 0;
132 }
133 x = device->base_x + value;
134 break;
135 case ABS_Y:
136 if (device->new_y) {
137 device->base_y = y - value;
138 device->new_y = 0;
139 }
140 y = device->base_y + value;
141 break;
142 }
143 break;
144
145 case EV_KEY:
146 if (value == 2)
147 break;
148
149 switch (e->code) {
150 case BTN_TOUCH:
151 case BTN_TOOL_PEN:
152 case BTN_TOOL_RUBBER:
153 case BTN_TOOL_BRUSH:
154 case BTN_TOOL_PENCIL:
155 case BTN_TOOL_AIRBRUSH:
156 case BTN_TOOL_FINGER:
157 case BTN_TOOL_MOUSE:
158 case BTN_TOOL_LENS:
159 if (device->tool == 0 && value) {
160 device->new_x = 1;
161 device->new_y = 1;
162 }
163 device->tool = value ? e->code : 0;
164 break;
165
166 case BTN_LEFT:
167 case BTN_RIGHT:
168 case BTN_MIDDLE:
169 case BTN_SIDE:
170 case BTN_EXTRA:
171 case BTN_FORWARD:
172 case BTN_BACK:
173 case BTN_TASK:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400174 notify_button(&device->master->base,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400175 time, e->code, value);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400176 break;
177
178 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400179 notify_key(&device->master->base,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400180 time, e->code, value);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400181 break;
182 }
183 }
184 }
185
186 if (dx != 0 || dy != 0)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400187 notify_motion(&device->master->base, time, x + dx, y + dy);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400188 if (absolute_event && device->tool)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400189 notify_motion(&device->master->base, time, x, y);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400190}
191
192static struct evdev_input_device *
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400193evdev_input_device_create(struct drm_input *master,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400194 struct wl_display *display, const char *path)
195{
196 struct evdev_input_device *device;
197 struct wl_event_loop *loop;
198
199 device = malloc(sizeof *device);
200 if (device == NULL)
201 return NULL;
202
203 device->tool = 1;
204 device->new_x = 1;
205 device->new_y = 1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400206 device->master = master;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400207
208 device->fd = open(path, O_RDONLY);
209 if (device->fd < 0) {
210 free(device);
211 fprintf(stderr, "couldn't create pointer for %s: %m\n", path);
212 return NULL;
213 }
214
215 loop = wl_display_get_event_loop(display);
216 device->source = wl_event_loop_add_fd(loop, device->fd,
217 WL_EVENT_READABLE,
218 evdev_input_device_data, device);
219 if (device->source == NULL) {
220 close(device->fd);
221 free(device);
222 return NULL;
223 }
224
225 return device;
226}
227
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400228static void
229drm_input_create(struct drm_compositor *c)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400230{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400231 struct drm_input *input;
232 struct udev_enumerate *e;
233 struct udev_list_entry *entry;
234 struct udev_device *device;
235 const char *path;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400236
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400237 input = malloc(sizeof *input);
238 if (input == NULL)
239 return;
240
241 memset(input, 0, sizeof *input);
242 wlsc_input_device_init(&input->base, &c->base);
243
244 e = udev_enumerate_new(c->udev);
245 udev_enumerate_add_match_subsystem(e, "input");
246 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
247 udev_enumerate_scan_devices(e);
248 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
249 path = udev_list_entry_get_name(entry);
250 device = udev_device_new_from_syspath(c->udev, path);
251 evdev_input_device_create(input, c->base.wl_display,
252 udev_device_get_devnode(device));
253 }
254 udev_enumerate_unref(e);
255
256 c->base.input_device = &input->base;
257}
258
259static void
260drm_compositor_present(struct wlsc_compositor *ec)
261{
262 struct drm_compositor *c = (struct drm_compositor *) ec;
263 struct drm_output *output;
264
265 wl_list_for_each(output, &ec->output_list, base.link) {
266 output->current ^= 1;
267
268 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
269 GL_COLOR_ATTACHMENT0,
270 GL_RENDERBUFFER,
271 output->rbo[output->current]);
272
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400273 drmModePageFlip(c->base.drm.fd, output->crtc_id,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400274 output->fb_id[output->current ^ 1],
275 DRM_MODE_PAGE_FLIP_EVENT, output);
276 }
277}
278
279static void
280page_flip_handler(int fd, unsigned int frame,
281 unsigned int sec, unsigned int usec, void *data)
282{
283 struct wlsc_output *output = data;
284 struct wlsc_compositor *compositor = output->compositor;
285 uint32_t msecs;
286
287 msecs = sec * 1000 + usec / 1000;
288 wlsc_compositor_finish_frame(compositor, msecs);
289}
290
291static void
292on_drm_input(int fd, uint32_t mask, void *data)
293{
294 drmEventContext evctx;
295
296 memset(&evctx, 0, sizeof evctx);
297 evctx.version = DRM_EVENT_CONTEXT_VERSION;
298 evctx.page_flip_handler = page_flip_handler;
299 drmHandleEvent(fd, &evctx);
300}
301
302static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400303init_egl(struct drm_compositor *ec, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400304{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400305 EGLint major, minor;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400306 const char *extensions, *filename;
307 int fd;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400308 static const EGLint context_attribs[] = {
309 EGL_CONTEXT_CLIENT_VERSION, 2,
310 EGL_NONE
311 };
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400312
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400313 filename = udev_device_get_devnode(device);
314 fd = open(filename, O_RDWR);
315 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400316 /* Probably permissions error */
317 fprintf(stderr, "couldn't open %s, skipping\n",
318 udev_device_get_devnode(device));
319 return -1;
320 }
321
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400322 wlsc_drm_init(&ec->base, fd, filename);
323
324 ec->base.display = eglGetDRMDisplayMESA(ec->base.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,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400380 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
381 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400382 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
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400394 encoder = drmModeGetEncoder(ec->base.drm.fd, connector->encoders[0]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400395 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øgsberg5fcd0aa2010-08-09 14:43:33 -0400432 ret = drmModeAddFB(ec->base.drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400433 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]);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400446 ret = drmModeSetCrtc(ec->base.drm.fd, output->crtc_id,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400447 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øgsberg61a82512010-10-26 11:26:44 -0400460create_outputs(struct drm_compositor *ec, int option_connector)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400461{
462 drmModeConnector *connector;
463 drmModeRes *resources;
464 int i;
465
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400466 resources = drmModeGetResources(ec->base.drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400467 if (!resources) {
468 fprintf(stderr, "drmModeGetResources failed\n");
469 return -1;
470 }
471
472 for (i = 0; i < resources->count_connectors; i++) {
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400473 connector = drmModeGetConnector(ec->base.drm.fd, resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400474 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
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400502 ret = drmSetMaster(ec->base.drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400503 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øgsberg5fcd0aa2010-08-09 14:43:33 -0400515 ret = drmModeSetCrtc(ec->base.drm.fd, output->crtc_id,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400516 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
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400530 ret = drmDropMaster(ec->base.drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400531 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øgsberg640609a2010-08-09 22:11:47 -0400610static int
611drm_authenticate(struct wlsc_compositor *c, uint32_t id)
612{
613 struct drm_compositor *ec = (struct drm_compositor *) c;
614
615 return drmAuthMagic(ec->base.drm.fd, id);
616}
617
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400618struct wlsc_compositor *
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400619drm_compositor_create(struct wl_display *display, int connector)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400620{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400621 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400622 struct udev_enumerate *e;
623 struct udev_list_entry *entry;
624 struct udev_device *device;
625 const char *path;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400626 struct wl_event_loop *loop;
627
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400628 ec = malloc(sizeof *ec);
629 if (ec == NULL)
630 return NULL;
631
632 memset(ec, 0, sizeof *ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400633 ec->udev = udev_new();
634 if (ec->udev == NULL) {
635 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400636 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400637 }
638
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400639 e = udev_enumerate_new(ec->udev);
640 udev_enumerate_add_match_subsystem(e, "drm");
641 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
642 udev_enumerate_scan_devices(e);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400643 device = NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400644 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
645 path = udev_list_entry_get_name(entry);
646 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400647 break;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400648 }
649 udev_enumerate_unref(e);
650
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400651 if (device == NULL) {
652 fprintf(stderr, "no drm device found\n");
653 return NULL;
654 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400655
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400656 ec->base.wl_display = display;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400657 if (init_egl(ec, device) < 0) {
658 fprintf(stderr, "failed to initialize egl\n");
659 return NULL;
660 }
661
662 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400663 if (wlsc_compositor_init(&ec->base, display) < 0)
664 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400665
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400666 if (create_outputs(ec, connector) < 0) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400667 fprintf(stderr, "failed to create output for %s\n", path);
668 return NULL;
669 }
670
671 drm_input_create(ec);
672
673 loop = wl_display_get_event_loop(ec->base.wl_display);
674 ec->drm_source =
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400675 wl_event_loop_add_fd(loop, ec->base.drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400676 WL_EVENT_READABLE, on_drm_input, ec);
677 setup_tty(ec, loop);
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400678 ec->base.authenticate = drm_authenticate;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400679 ec->base.present = drm_compositor_present;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400680 ec->base.focus = 1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400681
682 return &ec->base;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400683}