Move grab state to struct wl_input_device
[profile/ivi/weston.git] / compositor / compositor-drm.c
1 /*
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/kd.h>
27 #include <linux/vt.h>
28 #include <linux/input.h>
29
30 #define GL_GLEXT_PROTOTYPES
31 #define EGL_EGLEXT_PROTOTYPES
32 #include <GLES2/gl2.h>
33 #include <GLES2/gl2ext.h>
34 #include <EGL/egl.h>
35 #include <EGL/eglext.h>
36
37 #include "compositor.h"
38
39 struct drm_compositor {
40         struct wlsc_compositor base;
41
42         struct udev *udev;
43         struct wl_event_source *drm_source;
44
45         /* tty handling state */
46         int tty_fd;
47         uint32_t vt_active : 1;
48
49         struct termios terminal_attributes;
50         struct wl_event_source *tty_input_source;
51         struct wl_event_source *enter_vt_source;
52         struct wl_event_source *leave_vt_source;
53 };
54
55 struct drm_output {
56         struct wlsc_output   base;
57
58         drmModeModeInfo mode;
59         uint32_t crtc_id;
60         uint32_t connector_id;
61         GLuint rbo[2];
62         uint32_t fb_id[2];
63         EGLImageKHR image[2];
64         uint32_t current;       
65 };
66
67 struct drm_input {
68         struct wlsc_input_device base;
69 };
70
71 struct evdev_input_device {
72         struct drm_input *master;
73         struct wl_event_source *source;
74         int tool, new_x, new_y;
75         int base_x, base_y;
76         int fd;
77 };
78
79 static void evdev_input_device_data(int fd, uint32_t mask, void *data)
80 {
81         struct drm_compositor *c;
82         struct evdev_input_device *device = data;
83         struct input_event ev[8], *e, *end;
84         int len, value, dx, dy, absolute_event;
85         int x, y;
86         uint32_t time;
87
88         c = (struct drm_compositor *)
89                 device->master->base.input_device.compositor;
90         if (!c->vt_active)
91                 return;
92
93         dx = 0;
94         dy = 0;
95         absolute_event = 0;
96         x = device->master->base.input_device.x;
97         y = device->master->base.input_device.y;
98
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;
110                 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
111
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:
174                                 notify_button(&device->master->base.input_device,
175                                               time, e->code, value);
176                                 break;
177
178                         default:
179                                 notify_key(&device->master->base.input_device,
180                                            time, e->code, value);
181                                 break;
182                         }
183                 }
184         }
185
186         if (dx != 0 || dy != 0)
187                 notify_motion(&device->master->base.input_device,
188                               time, x + dx, y + dy);
189         if (absolute_event && device->tool)
190                 notify_motion(&device->master->base.input_device, time, x, y);
191 }
192
193 static struct evdev_input_device *
194 evdev_input_device_create(struct drm_input *master,
195                           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;
207         device->master = master;
208
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
229 static void
230 drm_input_create(struct drm_compositor *c)
231 {
232         struct drm_input *input;
233         struct udev_enumerate *e;
234         struct udev_list_entry *entry;
235         struct udev_device *device;
236         const char *path;
237
238         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.input_device;
258 }
259
260 static void
261 drm_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->base.drm.fd, output->crtc_id,
275                                 output->fb_id[output->current ^ 1],
276                                 DRM_MODE_PAGE_FLIP_EVENT, output);
277         }       
278 }
279
280 static void
281 page_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
292 static void
293 on_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
303 static int
304 init_egl(struct drm_compositor *ec, struct udev_device *device)
305 {
306         EGLint major, minor;
307         const char *extensions, *filename;
308         int fd;
309         static const EGLint context_attribs[] = {
310                 EGL_CONTEXT_CLIENT_VERSION, 2,
311                 EGL_NONE
312         };
313
314         filename = udev_device_get_devnode(device);
315         fd = open(filename, O_RDWR);
316         if (fd < 0) {
317                 /* Probably permissions error */
318                 fprintf(stderr, "couldn't open %s, skipping\n",
319                         udev_device_get_devnode(device));
320                 return -1;
321         }
322
323         wlsc_drm_init(&ec->base, fd, filename);
324
325         ec->base.display = eglGetDRMDisplayMESA(ec->base.drm.fd);
326         if (ec->base.display == NULL) {
327                 fprintf(stderr, "failed to create display\n");
328                 return -1;
329         }
330
331         if (!eglInitialize(ec->base.display, &major, &minor)) {
332                 fprintf(stderr, "failed to initialize display\n");
333                 return -1;
334         }
335
336         extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS);
337         if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
338                 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
339                 return -1;
340         }
341
342         if (!eglBindAPI(EGL_OPENGL_ES_API)) {
343                 fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n");
344                 return -1;
345         }
346
347         ec->base.context = eglCreateContext(ec->base.display, NULL,
348                                             EGL_NO_CONTEXT, context_attribs);
349         if (ec->base.context == NULL) {
350                 fprintf(stderr, "failed to create context\n");
351                 return -1;
352         }
353
354         if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE,
355                             EGL_NO_SURFACE, ec->base.context)) {
356                 fprintf(stderr, "failed to make context current\n");
357                 return -1;
358         }
359
360         return 0;
361 }
362
363 static drmModeModeInfo builtin_1024x768 = {
364         63500,                  /* clock */
365         1024, 1072, 1176, 1328, 0,
366         768, 771, 775, 798, 0,
367         59920,
368         DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
369         0,
370         "1024x768"
371 };
372
373 static int
374 create_output_for_connector(struct drm_compositor *ec,
375                             drmModeRes *resources,
376                             drmModeConnector *connector)
377 {
378         struct drm_output *output;
379         drmModeEncoder *encoder;
380         drmModeModeInfo *mode;
381         int i, ret;
382         EGLint handle, stride, attribs[] = {
383                 EGL_WIDTH,              0,
384                 EGL_HEIGHT,             0,
385                 EGL_DRM_BUFFER_FORMAT_MESA,     EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
386                 EGL_DRM_BUFFER_USE_MESA,        EGL_DRM_BUFFER_USE_SCANOUT_MESA,
387                 EGL_NONE
388         };
389
390         output = malloc(sizeof *output);
391         if (output == NULL)
392                 return -1;
393
394         if (connector->count_modes > 0) 
395                 mode = &connector->modes[0];
396         else
397                 mode = &builtin_1024x768;
398
399         encoder = drmModeGetEncoder(ec->base.drm.fd, connector->encoders[0]);
400         if (encoder == NULL) {
401                 fprintf(stderr, "No encoder for connector.\n");
402                 return -1;
403         }
404
405         for (i = 0; i < resources->count_crtcs; i++) {
406                 if (encoder->possible_crtcs & (1 << i))
407                         break;
408         }
409         if (i == resources->count_crtcs) {
410                 fprintf(stderr, "No usable crtc for encoder.\n");
411                 return -1;
412         }
413
414         memset(output, 0, sizeof *output);
415         wlsc_output_init(&output->base, &ec->base, 0, 0,
416                          mode->hdisplay, mode->vdisplay);
417
418         output->crtc_id = resources->crtcs[i];
419         output->connector_id = connector->connector_id;
420         output->mode = *mode;
421
422         drmModeFreeEncoder(encoder);
423
424         glGenRenderbuffers(2, output->rbo);
425         for (i = 0; i < 2; i++) {
426                 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
427
428                 attribs[1] = output->base.width;
429                 attribs[3] = output->base.height;
430                 output->image[i] =
431                         eglCreateDRMImageMESA(ec->base.display, attribs);
432                 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
433                                                        output->image[i]);
434                 eglExportDRMImageMESA(ec->base.display, output->image[i],
435                                       NULL, &handle, &stride);
436
437                 ret = drmModeAddFB(ec->base.drm.fd,
438                                    output->base.width, output->base.height,
439                                    32, 32, stride, handle, &output->fb_id[i]);
440                 if (ret) {
441                         fprintf(stderr, "failed to add fb %d: %m\n", i);
442                         return -1;
443                 }
444         }
445
446         output->current = 0;
447         glFramebufferRenderbuffer(GL_FRAMEBUFFER,
448                                   GL_COLOR_ATTACHMENT0,
449                                   GL_RENDERBUFFER,
450                                   output->rbo[output->current]);
451         ret = drmModeSetCrtc(ec->base.drm.fd, output->crtc_id,
452                              output->fb_id[output->current ^ 1], 0, 0,
453                              &output->connector_id, 1, &output->mode);
454         if (ret) {
455                 fprintf(stderr, "failed to set mode: %m\n");
456                 return -1;
457         }
458
459         wl_list_insert(ec->base.output_list.prev, &output->base.link);
460
461         return 0;
462 }
463
464 static int
465 create_outputs(struct drm_compositor *ec, int option_connector)
466 {
467         drmModeConnector *connector;
468         drmModeRes *resources;
469         int i;
470
471         resources = drmModeGetResources(ec->base.drm.fd);
472         if (!resources) {
473                 fprintf(stderr, "drmModeGetResources failed\n");
474                 return -1;
475         }
476
477         for (i = 0; i < resources->count_connectors; i++) {
478                 connector = drmModeGetConnector(ec->base.drm.fd, resources->connectors[i]);
479                 if (connector == NULL)
480                         continue;
481
482                 if (connector->connection == DRM_MODE_CONNECTED &&
483                     (option_connector == 0 ||
484                      connector->connector_id == option_connector))
485                         if (create_output_for_connector(ec, resources, connector) < 0)
486                                 return -1;
487
488                 drmModeFreeConnector(connector);
489         }
490
491         if (wl_list_empty(&ec->base.output_list)) {
492                 fprintf(stderr, "No currently active connector found.\n");
493                 return -1;
494         }
495
496         drmModeFreeResources(resources);
497
498         return 0;
499 }
500
501 static void on_enter_vt(int signal_number, void *data)
502 {
503         struct drm_compositor *ec = data;
504         struct drm_output *output;
505         int ret;
506
507         ret = drmSetMaster(ec->base.drm.fd);
508         if (ret) {
509                 fprintf(stderr, "failed to set drm master\n");
510                 kill(0, SIGTERM);
511                 return;
512         }
513
514         fprintf(stderr, "enter vt\n");
515
516         ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
517         ret = ioctl(ec->tty_fd, KDSETMODE, KD_GRAPHICS);
518         if (ret)
519                 fprintf(stderr, "failed to set KD_GRAPHICS mode on console: %m\n");
520         ec->vt_active = 1;
521
522         wl_list_for_each(output, &ec->base.output_list, base.link) {
523                 ret = drmModeSetCrtc(ec->base.drm.fd, output->crtc_id,
524                                      output->fb_id[output->current ^ 1], 0, 0,
525                                      &output->connector_id, 1, &output->mode);
526                 if (ret)
527                         fprintf(stderr,
528                                 "failed to set mode for connector %d: %m\n",
529                                 output->connector_id);
530         }
531 }
532
533 static void on_leave_vt(int signal_number, void *data)
534 {
535         struct drm_compositor *ec = data;
536         int ret;
537
538         ret = drmDropMaster(ec->base.drm.fd);
539         if (ret) {
540                 fprintf(stderr, "failed to drop drm master\n");
541                 kill(0, SIGTERM);
542                 return;
543         }
544
545         ioctl (ec->tty_fd, VT_RELDISP, 1);
546         ret = ioctl(ec->tty_fd, KDSETMODE, KD_TEXT);
547         if (ret)
548                 fprintf(stderr, "failed to set KD_TEXT mode on console: %m\n");
549         ec->vt_active = 0;
550 }
551
552 static void
553 on_tty_input(int fd, uint32_t mask, void *data)
554 {
555         struct drm_compositor *ec = data;
556
557         /* Ignore input to tty.  We get keyboard events from evdev
558          */
559         tcflush(ec->tty_fd, TCIFLUSH);
560 }
561
562 static int setup_tty(struct drm_compositor *ec, struct wl_event_loop *loop)
563 {
564         struct termios raw_attributes;
565         struct vt_mode mode = { 0 };
566         int ret;
567
568         ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
569         if (ec->tty_fd <= 0) {
570                 fprintf(stderr, "failed to open active tty: %m\n");
571                 return -1;
572         }
573
574         if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
575                 fprintf(stderr, "could not get terminal attributes: %m\n");
576                 return -1;
577         }
578
579         /* Ignore control characters and disable echo */
580         raw_attributes = ec->terminal_attributes;
581         cfmakeraw(&raw_attributes);
582
583         /* Fix up line endings to be normal (cfmakeraw hoses them) */
584         raw_attributes.c_oflag |= OPOST | OCRNL;
585
586         if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
587                 fprintf(stderr, "could not put terminal into raw mode: %m\n");
588
589         ec->tty_input_source =
590                 wl_event_loop_add_fd(loop, ec->tty_fd,
591                                      WL_EVENT_READABLE, on_tty_input, ec);
592
593         ret = ioctl(ec->tty_fd, KDSETMODE, KD_GRAPHICS);
594         if (ret)
595                 fprintf(stderr, "failed to set KD_GRAPHICS mode on tty: %m\n");
596
597         ec->vt_active = 1;
598         mode.mode = VT_PROCESS;
599         mode.relsig = SIGUSR1;
600         mode.acqsig = SIGUSR2;
601         if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
602                 fprintf(stderr, "failed to take control of vt handling\n");
603         }
604
605         ec->leave_vt_source =
606                 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
607         ec->enter_vt_source =
608                 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
609
610         return 0;
611 }
612
613 static int
614 drm_authenticate(struct wlsc_compositor *c, uint32_t id)
615 {
616         struct drm_compositor *ec = (struct drm_compositor *) c;
617
618         return drmAuthMagic(ec->base.drm.fd, id);
619 }
620
621 static void
622 drm_destroy(struct wlsc_compositor *ec)
623 {
624         struct drm_compositor *d = (struct drm_compositor *) ec;
625
626         if (tcsetattr(d->tty_fd, TCSANOW, &d->terminal_attributes) < 0)
627                 fprintf(stderr,
628                         "could not restore terminal to canonical mode\n");
629
630         free(ec);
631 }
632
633 struct wlsc_compositor *
634 drm_compositor_create(struct wl_display *display, int connector)
635 {
636         struct drm_compositor *ec;
637         struct udev_enumerate *e;
638         struct udev_list_entry *entry;
639         struct udev_device *device;
640         const char *path;
641         struct wl_event_loop *loop;
642
643         ec = malloc(sizeof *ec);
644         if (ec == NULL)
645                 return NULL;
646
647         memset(ec, 0, sizeof *ec);
648         ec->udev = udev_new();
649         if (ec->udev == NULL) {
650                 fprintf(stderr, "failed to initialize udev context\n");
651                 return NULL;
652         }
653
654         e = udev_enumerate_new(ec->udev);
655         udev_enumerate_add_match_subsystem(e, "drm");
656         udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
657         udev_enumerate_scan_devices(e);
658         device = NULL;
659         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
660                 path = udev_list_entry_get_name(entry);
661                 device = udev_device_new_from_syspath(ec->udev, path);
662                 break;
663         }
664         udev_enumerate_unref(e);
665
666         if (device == NULL) {
667                 fprintf(stderr, "no drm device found\n");
668                 return NULL;
669         }
670
671         ec->base.wl_display = display;
672         if (init_egl(ec, device) < 0) {
673                 fprintf(stderr, "failed to initialize egl\n");
674                 return NULL;
675         }
676         
677         /* Can't init base class until we have a current egl context */
678         if (wlsc_compositor_init(&ec->base, display) < 0)
679                 return NULL;
680
681         if (create_outputs(ec, connector) < 0) {
682                 fprintf(stderr, "failed to create output for %s\n", path);
683                 return NULL;
684         }
685
686         drm_input_create(ec);
687
688         loop = wl_display_get_event_loop(ec->base.wl_display);
689         ec->drm_source =
690                 wl_event_loop_add_fd(loop, ec->base.drm.fd,
691                                      WL_EVENT_READABLE, on_drm_input, ec);
692         setup_tty(ec, loop);
693         ec->base.destroy = drm_destroy;
694         ec->base.authenticate = drm_authenticate;
695         ec->base.present = drm_compositor_present;
696         ec->base.focus = 1;
697
698         return &ec->base;
699 }