Handle NULL mime-type in drag::accept request
[profile/ivi/wayland.git] / 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/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 "compositor.h"
37
38 struct drm_compositor {
39         struct wlsc_compositor base;
40
41         struct udev *udev;
42         struct wl_event_source *drm_source;
43
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
56 struct 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
68 struct drm_input {
69         struct wlsc_input_device base;
70 };
71
72 struct evdev_input_device {
73         struct drm_input *master;
74         struct wl_event_source *source;
75         int tool, new_x, new_y;
76         int base_x, base_y;
77         int fd;
78 };
79
80 static void evdev_input_device_data(int fd, uint32_t mask, void *data)
81 {
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;
86         int x, y;
87         uint32_t time;
88
89         c = (struct drm_compositor *) device->master->base.ec;
90         if (!c->vt_active)
91                 return;
92
93         dx = 0;
94         dy = 0;
95         absolute_event = 0;
96         x = device->master->base.x;
97         y = device->master->base.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,
175                                               time, e->code, value);
176                                 break;
177
178                         default:
179                                 notify_key(&device->master->base,
180                                            time, e->code, value);
181                                 break;
182                         }
183                 }
184         }
185
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);
190 }
191
192 static struct evdev_input_device *
193 evdev_input_device_create(struct drm_input *master,
194                           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;
206         device->master = master;
207
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
228 static void
229 drm_input_create(struct drm_compositor *c)
230 {
231         struct drm_input *input;
232         struct udev_enumerate *e;
233         struct udev_list_entry *entry;
234         struct udev_device *device;
235         const char *path;
236
237         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
259 static void
260 drm_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
273                 drmModePageFlip(c->base.drm.fd, output->crtc_id,
274                                 output->fb_id[output->current ^ 1],
275                                 DRM_MODE_PAGE_FLIP_EVENT, output);
276         }       
277 }
278
279 static void
280 page_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
291 static void
292 on_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
302 static int
303 init_egl(struct drm_compositor *ec, struct udev_device *device)
304 {
305         EGLint major, minor;
306         const char *extensions, *filename;
307         int fd;
308         static const EGLint context_attribs[] = {
309                 EGL_CONTEXT_CLIENT_VERSION, 2,
310                 EGL_NONE
311         };
312
313         filename = udev_device_get_devnode(device);
314         fd = open(filename, O_RDWR);
315         if (fd < 0) {
316                 /* Probably permissions error */
317                 fprintf(stderr, "couldn't open %s, skipping\n",
318                         udev_device_get_devnode(device));
319                 return -1;
320         }
321
322         wlsc_drm_init(&ec->base, fd, filename);
323
324         ec->base.display = eglGetDRMDisplayMESA(ec->base.drm.fd);
325         if (ec->base.display == NULL) {
326                 fprintf(stderr, "failed to create display\n");
327                 return -1;
328         }
329
330         if (!eglInitialize(ec->base.display, &major, &minor)) {
331                 fprintf(stderr, "failed to initialize display\n");
332                 return -1;
333         }
334
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");
338                 return -1;
339         }
340
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");
346                 return -1;
347         }
348
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");
352                 return -1;
353         }
354
355         return 0;
356 }
357
358 static 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
368 static int
369 create_output_for_connector(struct drm_compositor *ec,
370                             drmModeRes *resources,
371                             drmModeConnector *connector)
372 {
373         struct drm_output *output;
374         drmModeEncoder *encoder;
375         drmModeModeInfo *mode;
376         int i, ret;
377         EGLint handle, stride, attribs[] = {
378                 EGL_WIDTH,              0,
379                 EGL_HEIGHT,             0,
380                 EGL_DRM_BUFFER_FORMAT_MESA,     EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
381                 EGL_DRM_BUFFER_USE_MESA,        EGL_DRM_BUFFER_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->base.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
409         memset(output, 0, sizeof *output);
410         wlsc_output_init(&output->base, &ec->base, 0, 0,
411                          mode->hdisplay, mode->vdisplay);
412
413         output->crtc_id = resources->crtcs[i];
414         output->connector_id = connector->connector_id;
415         output->mode = *mode;
416
417         drmModeFreeEncoder(encoder);
418
419         glGenRenderbuffers(2, output->rbo);
420         for (i = 0; i < 2; i++) {
421                 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
422
423                 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);
431
432                 ret = drmModeAddFB(ec->base.drm.fd,
433                                    output->base.width, output->base.height,
434                                    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->base.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
454         wl_list_insert(ec->base.output_list.prev, &output->base.link);
455
456         return 0;
457 }
458
459 static int
460 create_outputs(struct drm_compositor *ec)
461 {
462         drmModeConnector *connector;
463         drmModeRes *resources;
464         int i;
465
466         resources = drmModeGetResources(ec->base.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->base.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
486         if (wl_list_empty(&ec->base.output_list)) {
487                 fprintf(stderr, "No currently active connector found.\n");
488                 return -1;
489         }
490
491         drmModeFreeResources(resources);
492
493         return 0;
494 }
495
496 static void on_enter_vt(int signal_number, void *data)
497 {
498         struct drm_compositor *ec = data;
499         struct drm_output *output;
500         int ret;
501
502         ret = drmSetMaster(ec->base.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
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);
518                 if (ret)
519                         fprintf(stderr,
520                                 "failed to set mode for connector %d: %m\n",
521                                 output->connector_id);
522         }
523 }
524
525 static void on_leave_vt(int signal_number, void *data)
526 {
527         struct drm_compositor *ec = data;
528         int ret;
529
530         ret = drmDropMaster(ec->base.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
541 static void
542 on_tty_input(int fd, uint32_t mask, void *data)
543 {
544         struct drm_compositor *ec = data;
545
546         /* Ignore input to tty.  We get keyboard events from evdev
547          */
548         tcflush(ec->tty_fd, TCIFLUSH);
549 }
550
551 static void on_term_signal(int signal_number, void *data)
552 {
553         struct drm_compositor *ec = data;
554
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
561 static int setup_tty(struct drm_compositor *ec, struct wl_event_loop *loop)
562 {
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
610 static int
611 drm_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
618 struct wlsc_compositor *
619 drm_compositor_create(struct wl_display *display)
620 {
621         struct drm_compositor *ec;
622         struct udev_enumerate *e;
623         struct udev_list_entry *entry;
624         struct udev_device *device;
625         const char *path;
626         struct wl_event_loop *loop;
627
628         ec = malloc(sizeof *ec);
629         if (ec == NULL)
630                 return NULL;
631
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");
636                 return NULL;
637         }
638
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);
643         device = NULL;
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);
647                 break;
648         }
649         udev_enumerate_unref(e);
650
651         if (device == NULL) {
652                 fprintf(stderr, "no drm device found\n");
653                 return NULL;
654         }
655
656         ec->base.wl_display = display;
657         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 */
663         if (wlsc_compositor_init(&ec->base, display) < 0)
664                 return NULL;
665
666         if (create_outputs(ec) < 0) {
667                 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 =
675                 wl_event_loop_add_fd(loop, ec->base.drm.fd,
676                                      WL_EVENT_READABLE, on_drm_input, ec);
677         setup_tty(ec, loop);
678         ec->base.authenticate = drm_authenticate;
679         ec->base.present = drm_compositor_present;
680         ec->base.focus = 1;
681
682         return &ec->base;
683 }