Split out evdev handling to its own file
[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 static void
68 drm_compositor_present(struct wlsc_compositor *ec)
69 {
70         struct drm_compositor *c = (struct drm_compositor *) ec;
71         struct drm_output *output;
72
73         wl_list_for_each(output, &ec->output_list, base.link) {
74                 output->current ^= 1;
75
76                 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
77                                           GL_COLOR_ATTACHMENT0,
78                                           GL_RENDERBUFFER,
79                                           output->rbo[output->current]);
80
81                 drmModePageFlip(c->base.drm.fd, output->crtc_id,
82                                 output->fb_id[output->current ^ 1],
83                                 DRM_MODE_PAGE_FLIP_EVENT, output);
84         }       
85 }
86
87 static void
88 page_flip_handler(int fd, unsigned int frame,
89                   unsigned int sec, unsigned int usec, void *data)
90 {
91         struct wlsc_output *output = data;
92         struct wlsc_compositor *compositor = output->compositor;
93         uint32_t msecs;
94
95         msecs = sec * 1000 + usec / 1000;
96         wlsc_compositor_finish_frame(compositor, msecs);
97 }
98
99 static void
100 on_drm_input(int fd, uint32_t mask, void *data)
101 {
102         drmEventContext evctx;
103
104         memset(&evctx, 0, sizeof evctx);
105         evctx.version = DRM_EVENT_CONTEXT_VERSION;
106         evctx.page_flip_handler = page_flip_handler;
107         drmHandleEvent(fd, &evctx);
108 }
109
110 static int
111 init_egl(struct drm_compositor *ec, struct udev_device *device)
112 {
113         EGLint major, minor;
114         const char *extensions, *filename;
115         int fd;
116         static const EGLint context_attribs[] = {
117                 EGL_CONTEXT_CLIENT_VERSION, 2,
118                 EGL_NONE
119         };
120
121         filename = udev_device_get_devnode(device);
122         fd = open(filename, O_RDWR);
123         if (fd < 0) {
124                 /* Probably permissions error */
125                 fprintf(stderr, "couldn't open %s, skipping\n",
126                         udev_device_get_devnode(device));
127                 return -1;
128         }
129
130         wlsc_drm_init(&ec->base, fd, filename);
131
132         ec->base.display = eglGetDRMDisplayMESA(ec->base.drm.fd);
133         if (ec->base.display == NULL) {
134                 fprintf(stderr, "failed to create display\n");
135                 return -1;
136         }
137
138         if (!eglInitialize(ec->base.display, &major, &minor)) {
139                 fprintf(stderr, "failed to initialize display\n");
140                 return -1;
141         }
142
143         extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS);
144         if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
145                 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
146                 return -1;
147         }
148
149         if (!eglBindAPI(EGL_OPENGL_ES_API)) {
150                 fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n");
151                 return -1;
152         }
153
154         ec->base.context = eglCreateContext(ec->base.display, NULL,
155                                             EGL_NO_CONTEXT, context_attribs);
156         if (ec->base.context == NULL) {
157                 fprintf(stderr, "failed to create context\n");
158                 return -1;
159         }
160
161         if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE,
162                             EGL_NO_SURFACE, ec->base.context)) {
163                 fprintf(stderr, "failed to make context current\n");
164                 return -1;
165         }
166
167         return 0;
168 }
169
170 static drmModeModeInfo builtin_1024x768 = {
171         63500,                  /* clock */
172         1024, 1072, 1176, 1328, 0,
173         768, 771, 775, 798, 0,
174         59920,
175         DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
176         0,
177         "1024x768"
178 };
179
180 static int
181 create_output_for_connector(struct drm_compositor *ec,
182                             drmModeRes *resources,
183                             drmModeConnector *connector)
184 {
185         struct drm_output *output;
186         drmModeEncoder *encoder;
187         drmModeModeInfo *mode;
188         int i, ret;
189         EGLint handle, stride, attribs[] = {
190                 EGL_WIDTH,              0,
191                 EGL_HEIGHT,             0,
192                 EGL_DRM_BUFFER_FORMAT_MESA,     EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
193                 EGL_DRM_BUFFER_USE_MESA,        EGL_DRM_BUFFER_USE_SCANOUT_MESA,
194                 EGL_NONE
195         };
196
197         output = malloc(sizeof *output);
198         if (output == NULL)
199                 return -1;
200
201         if (connector->count_modes > 0) 
202                 mode = &connector->modes[0];
203         else
204                 mode = &builtin_1024x768;
205
206         encoder = drmModeGetEncoder(ec->base.drm.fd, connector->encoders[0]);
207         if (encoder == NULL) {
208                 fprintf(stderr, "No encoder for connector.\n");
209                 return -1;
210         }
211
212         for (i = 0; i < resources->count_crtcs; i++) {
213                 if (encoder->possible_crtcs & (1 << i))
214                         break;
215         }
216         if (i == resources->count_crtcs) {
217                 fprintf(stderr, "No usable crtc for encoder.\n");
218                 return -1;
219         }
220
221         memset(output, 0, sizeof *output);
222         wlsc_output_init(&output->base, &ec->base, 0, 0,
223                          mode->hdisplay, mode->vdisplay);
224
225         output->crtc_id = resources->crtcs[i];
226         output->connector_id = connector->connector_id;
227         output->mode = *mode;
228
229         drmModeFreeEncoder(encoder);
230
231         glGenRenderbuffers(2, output->rbo);
232         for (i = 0; i < 2; i++) {
233                 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
234
235                 attribs[1] = output->base.width;
236                 attribs[3] = output->base.height;
237                 output->image[i] =
238                         eglCreateDRMImageMESA(ec->base.display, attribs);
239                 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
240                                                        output->image[i]);
241                 eglExportDRMImageMESA(ec->base.display, output->image[i],
242                                       NULL, &handle, &stride);
243
244                 ret = drmModeAddFB(ec->base.drm.fd,
245                                    output->base.width, output->base.height,
246                                    32, 32, stride, handle, &output->fb_id[i]);
247                 if (ret) {
248                         fprintf(stderr, "failed to add fb %d: %m\n", i);
249                         return -1;
250                 }
251         }
252
253         output->current = 0;
254         glFramebufferRenderbuffer(GL_FRAMEBUFFER,
255                                   GL_COLOR_ATTACHMENT0,
256                                   GL_RENDERBUFFER,
257                                   output->rbo[output->current]);
258         ret = drmModeSetCrtc(ec->base.drm.fd, output->crtc_id,
259                              output->fb_id[output->current ^ 1], 0, 0,
260                              &output->connector_id, 1, &output->mode);
261         if (ret) {
262                 fprintf(stderr, "failed to set mode: %m\n");
263                 return -1;
264         }
265
266         wl_list_insert(ec->base.output_list.prev, &output->base.link);
267
268         return 0;
269 }
270
271 static int
272 create_outputs(struct drm_compositor *ec, int option_connector)
273 {
274         drmModeConnector *connector;
275         drmModeRes *resources;
276         int i;
277
278         resources = drmModeGetResources(ec->base.drm.fd);
279         if (!resources) {
280                 fprintf(stderr, "drmModeGetResources failed\n");
281                 return -1;
282         }
283
284         for (i = 0; i < resources->count_connectors; i++) {
285                 connector = drmModeGetConnector(ec->base.drm.fd, resources->connectors[i]);
286                 if (connector == NULL)
287                         continue;
288
289                 if (connector->connection == DRM_MODE_CONNECTED &&
290                     (option_connector == 0 ||
291                      connector->connector_id == option_connector))
292                         if (create_output_for_connector(ec, resources, connector) < 0)
293                                 return -1;
294
295                 drmModeFreeConnector(connector);
296         }
297
298         if (wl_list_empty(&ec->base.output_list)) {
299                 fprintf(stderr, "No currently active connector found.\n");
300                 return -1;
301         }
302
303         drmModeFreeResources(resources);
304
305         return 0;
306 }
307
308 static void on_enter_vt(int signal_number, void *data)
309 {
310         struct drm_compositor *ec = data;
311         struct drm_output *output;
312         int ret;
313
314         ret = drmSetMaster(ec->base.drm.fd);
315         if (ret) {
316                 fprintf(stderr, "failed to set drm master\n");
317                 kill(0, SIGTERM);
318                 return;
319         }
320
321         fprintf(stderr, "enter vt\n");
322
323         ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
324         ret = ioctl(ec->tty_fd, KDSETMODE, KD_GRAPHICS);
325         if (ret)
326                 fprintf(stderr, "failed to set KD_GRAPHICS mode on console: %m\n");
327         ec->vt_active = 1;
328
329         wl_list_for_each(output, &ec->base.output_list, base.link) {
330                 ret = drmModeSetCrtc(ec->base.drm.fd, output->crtc_id,
331                                      output->fb_id[output->current ^ 1], 0, 0,
332                                      &output->connector_id, 1, &output->mode);
333                 if (ret)
334                         fprintf(stderr,
335                                 "failed to set mode for connector %d: %m\n",
336                                 output->connector_id);
337         }
338 }
339
340 static void on_leave_vt(int signal_number, void *data)
341 {
342         struct drm_compositor *ec = data;
343         int ret;
344
345         ret = drmDropMaster(ec->base.drm.fd);
346         if (ret) {
347                 fprintf(stderr, "failed to drop drm master\n");
348                 kill(0, SIGTERM);
349                 return;
350         }
351
352         ioctl (ec->tty_fd, VT_RELDISP, 1);
353         ret = ioctl(ec->tty_fd, KDSETMODE, KD_TEXT);
354         if (ret)
355                 fprintf(stderr, "failed to set KD_TEXT mode on console: %m\n");
356         ec->vt_active = 0;
357 }
358
359 static void
360 on_tty_input(int fd, uint32_t mask, void *data)
361 {
362         struct drm_compositor *ec = data;
363
364         /* Ignore input to tty.  We get keyboard events from evdev
365          */
366         tcflush(ec->tty_fd, TCIFLUSH);
367 }
368
369 static int setup_tty(struct drm_compositor *ec, struct wl_event_loop *loop)
370 {
371         struct termios raw_attributes;
372         struct vt_mode mode = { 0 };
373         int ret;
374
375         ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
376         if (ec->tty_fd <= 0) {
377                 fprintf(stderr, "failed to open active tty: %m\n");
378                 return -1;
379         }
380
381         if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
382                 fprintf(stderr, "could not get terminal attributes: %m\n");
383                 return -1;
384         }
385
386         /* Ignore control characters and disable echo */
387         raw_attributes = ec->terminal_attributes;
388         cfmakeraw(&raw_attributes);
389
390         /* Fix up line endings to be normal (cfmakeraw hoses them) */
391         raw_attributes.c_oflag |= OPOST | OCRNL;
392
393         if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
394                 fprintf(stderr, "could not put terminal into raw mode: %m\n");
395
396         ec->tty_input_source =
397                 wl_event_loop_add_fd(loop, ec->tty_fd,
398                                      WL_EVENT_READABLE, on_tty_input, ec);
399
400         ret = ioctl(ec->tty_fd, KDSETMODE, KD_GRAPHICS);
401         if (ret)
402                 fprintf(stderr, "failed to set KD_GRAPHICS mode on tty: %m\n");
403
404         ec->vt_active = 1;
405         mode.mode = VT_PROCESS;
406         mode.relsig = SIGUSR1;
407         mode.acqsig = SIGUSR2;
408         if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
409                 fprintf(stderr, "failed to take control of vt handling\n");
410         }
411
412         ec->leave_vt_source =
413                 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
414         ec->enter_vt_source =
415                 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
416
417         return 0;
418 }
419
420 static int
421 drm_authenticate(struct wlsc_compositor *c, uint32_t id)
422 {
423         struct drm_compositor *ec = (struct drm_compositor *) c;
424
425         return drmAuthMagic(ec->base.drm.fd, id);
426 }
427
428 static void
429 drm_destroy(struct wlsc_compositor *ec)
430 {
431         struct drm_compositor *d = (struct drm_compositor *) ec;
432
433         if (tcsetattr(d->tty_fd, TCSANOW, &d->terminal_attributes) < 0)
434                 fprintf(stderr,
435                         "could not restore terminal to canonical mode\n");
436
437         free(ec);
438 }
439
440 struct wlsc_compositor *
441 drm_compositor_create(struct wl_display *display, int connector)
442 {
443         struct drm_compositor *ec;
444         struct udev_enumerate *e;
445         struct udev_list_entry *entry;
446         struct udev_device *device;
447         const char *path;
448         struct wl_event_loop *loop;
449
450         ec = malloc(sizeof *ec);
451         if (ec == NULL)
452                 return NULL;
453
454         memset(ec, 0, sizeof *ec);
455         ec->udev = udev_new();
456         if (ec->udev == NULL) {
457                 fprintf(stderr, "failed to initialize udev context\n");
458                 return NULL;
459         }
460
461         e = udev_enumerate_new(ec->udev);
462         udev_enumerate_add_match_subsystem(e, "drm");
463         udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
464         udev_enumerate_scan_devices(e);
465         device = NULL;
466         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
467                 path = udev_list_entry_get_name(entry);
468                 device = udev_device_new_from_syspath(ec->udev, path);
469                 break;
470         }
471         udev_enumerate_unref(e);
472
473         if (device == NULL) {
474                 fprintf(stderr, "no drm device found\n");
475                 return NULL;
476         }
477
478         ec->base.wl_display = display;
479         if (init_egl(ec, device) < 0) {
480                 fprintf(stderr, "failed to initialize egl\n");
481                 return NULL;
482         }
483         
484         /* Can't init base class until we have a current egl context */
485         if (wlsc_compositor_init(&ec->base, display) < 0)
486                 return NULL;
487
488         if (create_outputs(ec, connector) < 0) {
489                 fprintf(stderr, "failed to create output for %s\n", path);
490                 return NULL;
491         }
492
493         evdev_input_add_devices(&ec->base, ec->udev);
494
495         loop = wl_display_get_event_loop(ec->base.wl_display);
496         ec->drm_source =
497                 wl_event_loop_add_fd(loop, ec->base.drm.fd,
498                                      WL_EVENT_READABLE, on_drm_input, ec);
499         setup_tty(ec, loop);
500         ec->base.destroy = drm_destroy;
501         ec->base.authenticate = drm_authenticate;
502         ec->base.present = drm_compositor_present;
503         ec->base.focus = 1;
504
505         return &ec->base;
506 }