2 * Copyright © 2008-2010 Kristian Høgsberg
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.
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.
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.
27 #include <linux/input.h>
29 #define GL_GLEXT_PROTOTYPES
30 #define EGL_EGLEXT_PROTOTYPES
31 #include <GLES2/gl2.h>
32 #include <GLES2/gl2ext.h>
34 #include <EGL/eglext.h>
36 #include "compositor.h"
38 struct drm_compositor {
39 struct wlsc_compositor base;
42 struct wl_event_source *drm_source;
44 struct wl_event_source *term_signal_source;
46 /* tty handling state */
48 uint32_t vt_active : 1;
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;
57 struct wlsc_output base;
61 uint32_t connector_id;
69 struct wlsc_input_device base;
72 struct evdev_input_device {
73 struct drm_input *master;
74 struct wl_event_source *source;
75 int tool, new_x, new_y;
80 static void evdev_input_device_data(int fd, uint32_t mask, void *data)
82 struct drm_compositor *c;
83 struct evdev_input_device *device = data;
84 struct input_event ev[8], *e, *end;
85 int len, value, dx, dy, absolute_event;
89 c = (struct drm_compositor *) device->master->base.ec;
96 x = device->master->base.x;
97 y = device->master->base.y;
99 len = read(fd, &ev, sizeof ev);
100 if (len < 0 || len % sizeof e[0] != 0) {
101 /* FIXME: handle error... reopen device? */;
106 end = (void *) ev + len;
107 for (e = ev; e < end; e++) {
108 /* Get the signed value, earlier kernels had this as unsigned */
110 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
130 device->base_x = x - value;
133 x = device->base_x + value;
137 device->base_y = y - value;
140 y = device->base_y + value;
152 case BTN_TOOL_RUBBER:
154 case BTN_TOOL_PENCIL:
155 case BTN_TOOL_AIRBRUSH:
156 case BTN_TOOL_FINGER:
159 if (device->tool == 0 && value) {
163 device->tool = value ? e->code : 0;
174 notify_button(&device->master->base,
175 time, e->code, value);
179 notify_key(&device->master->base,
180 time, e->code, value);
186 if (dx != 0 || dy != 0)
187 notify_motion(&device->master->base, time, x + dx, y + dy);
188 if (absolute_event && device->tool)
189 notify_motion(&device->master->base, time, x, y);
192 static struct evdev_input_device *
193 evdev_input_device_create(struct drm_input *master,
194 struct wl_display *display, const char *path)
196 struct evdev_input_device *device;
197 struct wl_event_loop *loop;
199 device = malloc(sizeof *device);
206 device->master = master;
208 device->fd = open(path, O_RDONLY);
209 if (device->fd < 0) {
211 fprintf(stderr, "couldn't create pointer for %s: %m\n", path);
215 loop = wl_display_get_event_loop(display);
216 device->source = wl_event_loop_add_fd(loop, device->fd,
218 evdev_input_device_data, device);
219 if (device->source == NULL) {
229 drm_input_create(struct drm_compositor *c)
231 struct drm_input *input;
232 struct udev_enumerate *e;
233 struct udev_list_entry *entry;
234 struct udev_device *device;
237 input = malloc(sizeof *input);
241 memset(input, 0, sizeof *input);
242 wlsc_input_device_init(&input->base, &c->base);
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));
254 udev_enumerate_unref(e);
256 c->base.input_device = &input->base;
260 drm_compositor_present(struct wlsc_compositor *ec)
262 struct drm_compositor *c = (struct drm_compositor *) ec;
263 struct drm_output *output;
265 wl_list_for_each(output, &ec->output_list, base.link) {
266 output->current ^= 1;
268 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
269 GL_COLOR_ATTACHMENT0,
271 output->rbo[output->current]);
273 drmModePageFlip(c->base.drm.fd, output->crtc_id,
274 output->fb_id[output->current ^ 1],
275 DRM_MODE_PAGE_FLIP_EVENT, output);
280 page_flip_handler(int fd, unsigned int frame,
281 unsigned int sec, unsigned int usec, void *data)
283 struct wlsc_output *output = data;
284 struct wlsc_compositor *compositor = output->compositor;
287 msecs = sec * 1000 + usec / 1000;
288 wlsc_compositor_finish_frame(compositor, msecs);
292 on_drm_input(int fd, uint32_t mask, void *data)
294 drmEventContext evctx;
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);
303 init_egl(struct drm_compositor *ec, struct udev_device *device)
306 const char *extensions, *filename;
308 static const EGLint context_attribs[] = {
309 EGL_CONTEXT_CLIENT_VERSION, 2,
313 filename = udev_device_get_devnode(device);
314 fd = open(filename, O_RDWR);
316 /* Probably permissions error */
317 fprintf(stderr, "couldn't open %s, skipping\n",
318 udev_device_get_devnode(device));
322 wlsc_drm_init(&ec->base, fd, filename);
324 ec->base.display = eglGetDRMDisplayMESA(ec->base.drm.fd);
325 if (ec->base.display == NULL) {
326 fprintf(stderr, "failed to create display\n");
330 if (!eglInitialize(ec->base.display, &major, &minor)) {
331 fprintf(stderr, "failed to initialize display\n");
335 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");
341 eglBindAPI(EGL_OPENGL_ES_API);
342 ec->base.context = eglCreateContext(ec->base.display, NULL,
343 EGL_NO_CONTEXT, context_attribs);
344 if (ec->base.context == NULL) {
345 fprintf(stderr, "failed to create context\n");
349 if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE,
350 EGL_NO_SURFACE, ec->base.context)) {
351 fprintf(stderr, "failed to make context current\n");
358 static drmModeModeInfo builtin_1024x768 = {
360 1024, 1072, 1176, 1328, 0,
361 768, 771, 775, 798, 0,
363 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
369 create_output_for_connector(struct drm_compositor *ec,
370 drmModeRes *resources,
371 drmModeConnector *connector)
373 struct drm_output *output;
374 drmModeEncoder *encoder;
375 drmModeModeInfo *mode;
377 EGLint handle, stride, attribs[] = {
380 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
381 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
385 output = malloc(sizeof *output);
389 if (connector->count_modes > 0)
390 mode = &connector->modes[0];
392 mode = &builtin_1024x768;
394 encoder = drmModeGetEncoder(ec->base.drm.fd, connector->encoders[0]);
395 if (encoder == NULL) {
396 fprintf(stderr, "No encoder for connector.\n");
400 for (i = 0; i < resources->count_crtcs; i++) {
401 if (encoder->possible_crtcs & (1 << i))
404 if (i == resources->count_crtcs) {
405 fprintf(stderr, "No usable crtc for encoder.\n");
409 memset(output, 0, sizeof *output);
410 wlsc_output_init(&output->base, &ec->base, 0, 0,
411 mode->hdisplay, mode->vdisplay);
413 output->crtc_id = resources->crtcs[i];
414 output->connector_id = connector->connector_id;
415 output->mode = *mode;
417 drmModeFreeEncoder(encoder);
419 glGenRenderbuffers(2, output->rbo);
420 for (i = 0; i < 2; i++) {
421 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
423 attribs[1] = output->base.width;
424 attribs[3] = output->base.height;
426 eglCreateDRMImageMESA(ec->base.display, attribs);
427 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
429 eglExportDRMImageMESA(ec->base.display, output->image[i],
430 NULL, &handle, &stride);
432 ret = drmModeAddFB(ec->base.drm.fd,
433 output->base.width, output->base.height,
434 32, 32, stride, handle, &output->fb_id[i]);
436 fprintf(stderr, "failed to add fb %d: %m\n", i);
442 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
443 GL_COLOR_ATTACHMENT0,
445 output->rbo[output->current]);
446 ret = drmModeSetCrtc(ec->base.drm.fd, output->crtc_id,
447 output->fb_id[output->current ^ 1], 0, 0,
448 &output->connector_id, 1, &output->mode);
450 fprintf(stderr, "failed to set mode: %m\n");
454 wl_list_insert(ec->base.output_list.prev, &output->base.link);
460 create_outputs(struct drm_compositor *ec)
462 drmModeConnector *connector;
463 drmModeRes *resources;
466 resources = drmModeGetResources(ec->base.drm.fd);
468 fprintf(stderr, "drmModeGetResources failed\n");
472 for (i = 0; i < resources->count_connectors; i++) {
473 connector = drmModeGetConnector(ec->base.drm.fd, resources->connectors[i]);
474 if (connector == NULL)
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)
483 drmModeFreeConnector(connector);
486 if (wl_list_empty(&ec->base.output_list)) {
487 fprintf(stderr, "No currently active connector found.\n");
491 drmModeFreeResources(resources);
496 static void on_enter_vt(int signal_number, void *data)
498 struct drm_compositor *ec = data;
499 struct drm_output *output;
502 ret = drmSetMaster(ec->base.drm.fd);
504 fprintf(stderr, "failed to set drm master\n");
509 fprintf(stderr, "enter vt\n");
511 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
514 wl_list_for_each(output, &ec->base.output_list, base.link) {
515 ret = drmModeSetCrtc(ec->base.drm.fd, output->crtc_id,
516 output->fb_id[output->current ^ 1], 0, 0,
517 &output->connector_id, 1, &output->mode);
520 "failed to set mode for connector %d: %m\n",
521 output->connector_id);
525 static void on_leave_vt(int signal_number, void *data)
527 struct drm_compositor *ec = data;
530 ret = drmDropMaster(ec->base.drm.fd);
532 fprintf(stderr, "failed to drop drm master\n");
537 ioctl (ec->tty_fd, VT_RELDISP, 1);
542 on_tty_input(int fd, uint32_t mask, void *data)
544 struct drm_compositor *ec = data;
546 /* Ignore input to tty. We get keyboard events from evdev
548 tcflush(ec->tty_fd, TCIFLUSH);
551 static void on_term_signal(int signal_number, void *data)
553 struct drm_compositor *ec = data;
555 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
556 fprintf(stderr, "could not restore terminal to canonical mode\n");
561 static int setup_tty(struct drm_compositor *ec, struct wl_event_loop *loop)
563 struct termios raw_attributes;
564 struct vt_mode mode = { 0 };
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");
572 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
573 fprintf(stderr, "could not get terminal attributes: %m\n");
577 /* Ignore control characters and disable echo */
578 raw_attributes = ec->terminal_attributes;
579 cfmakeraw(&raw_attributes);
581 /* Fix up line endings to be normal (cfmakeraw hoses them) */
582 raw_attributes.c_oflag |= OPOST | OCRNL;
584 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
585 fprintf(stderr, "could not put terminal into raw mode: %m\n");
587 ec->term_signal_source =
588 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
590 ec->tty_input_source =
591 wl_event_loop_add_fd(loop, ec->tty_fd,
592 WL_EVENT_READABLE, on_tty_input, ec);
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");
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);
611 drm_authenticate(struct wlsc_compositor *c, uint32_t id)
613 struct drm_compositor *ec = (struct drm_compositor *) c;
615 return drmAuthMagic(ec->base.drm.fd, id);
618 struct wlsc_compositor *
619 drm_compositor_create(struct wl_display *display)
621 struct drm_compositor *ec;
622 struct udev_enumerate *e;
623 struct udev_list_entry *entry;
624 struct udev_device *device;
626 struct wl_event_loop *loop;
628 ec = malloc(sizeof *ec);
632 memset(ec, 0, sizeof *ec);
633 ec->udev = udev_new();
634 if (ec->udev == NULL) {
635 fprintf(stderr, "failed to initialize udev context\n");
639 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);
644 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);
649 udev_enumerate_unref(e);
651 if (device == NULL) {
652 fprintf(stderr, "no drm device found\n");
656 ec->base.wl_display = display;
657 if (init_egl(ec, device) < 0) {
658 fprintf(stderr, "failed to initialize egl\n");
662 /* Can't init base class until we have a current egl context */
663 if (wlsc_compositor_init(&ec->base, display) < 0)
666 if (create_outputs(ec) < 0) {
667 fprintf(stderr, "failed to create output for %s\n", path);
671 drm_input_create(ec);
673 loop = wl_display_get_event_loop(ec->base.wl_display);
675 wl_event_loop_add_fd(loop, ec->base.drm.fd,
676 WL_EVENT_READABLE, on_drm_input, ec);
678 ec->base.authenticate = drm_authenticate;
679 ec->base.present = drm_compositor_present;