Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 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 "wayland.h" |
| 37 | #include "wayland-protocol.h" |
| 38 | #include "compositor.h" |
| 39 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 40 | struct drm_compositor { |
| 41 | struct wlsc_compositor base; |
| 42 | |
| 43 | struct udev *udev; |
| 44 | struct wl_event_source *drm_source; |
| 45 | int drm_fd; |
| 46 | |
| 47 | struct wl_event_source *term_signal_source; |
| 48 | |
| 49 | /* tty handling state */ |
| 50 | int tty_fd; |
| 51 | uint32_t vt_active : 1; |
| 52 | |
| 53 | struct termios terminal_attributes; |
| 54 | struct wl_event_source *tty_input_source; |
| 55 | struct wl_event_source *enter_vt_source; |
| 56 | struct wl_event_source *leave_vt_source; |
| 57 | }; |
| 58 | |
| 59 | struct drm_output { |
| 60 | struct wlsc_output base; |
| 61 | |
| 62 | drmModeModeInfo mode; |
| 63 | uint32_t crtc_id; |
| 64 | uint32_t connector_id; |
| 65 | GLuint rbo[2]; |
| 66 | uint32_t fb_id[2]; |
| 67 | EGLImageKHR image[2]; |
| 68 | uint32_t current; |
| 69 | }; |
| 70 | |
| 71 | struct drm_input { |
| 72 | struct wlsc_input_device base; |
| 73 | }; |
| 74 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 75 | struct evdev_input_device { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 76 | struct drm_input *master; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 77 | struct wl_event_source *source; |
| 78 | int tool, new_x, new_y; |
| 79 | int base_x, base_y; |
| 80 | int fd; |
| 81 | }; |
| 82 | |
| 83 | static void evdev_input_device_data(int fd, uint32_t mask, void *data) |
| 84 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 85 | struct drm_compositor *c; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 86 | struct evdev_input_device *device = data; |
| 87 | struct input_event ev[8], *e, *end; |
| 88 | int len, value, dx, dy, absolute_event; |
| 89 | int x, y; |
| 90 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 91 | c = (struct drm_compositor *) device->master->base.ec; |
| 92 | if (!c->vt_active) |
| 93 | return; |
| 94 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 95 | dx = 0; |
| 96 | dy = 0; |
| 97 | absolute_event = 0; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 98 | x = device->master->base.x; |
| 99 | y = device->master->base.y; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 100 | |
| 101 | len = read(fd, &ev, sizeof ev); |
| 102 | if (len < 0 || len % sizeof e[0] != 0) { |
| 103 | /* FIXME: handle error... reopen device? */; |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | e = ev; |
| 108 | end = (void *) ev + len; |
| 109 | for (e = ev; e < end; e++) { |
| 110 | /* Get the signed value, earlier kernels had this as unsigned */ |
| 111 | value = e->value; |
| 112 | |
| 113 | switch (e->type) { |
| 114 | case EV_REL: |
| 115 | switch (e->code) { |
| 116 | case REL_X: |
| 117 | dx += value; |
| 118 | break; |
| 119 | |
| 120 | case REL_Y: |
| 121 | dy += value; |
| 122 | break; |
| 123 | } |
| 124 | break; |
| 125 | |
| 126 | case EV_ABS: |
| 127 | absolute_event = 1; |
| 128 | switch (e->code) { |
| 129 | case ABS_X: |
| 130 | if (device->new_x) { |
| 131 | device->base_x = x - value; |
| 132 | device->new_x = 0; |
| 133 | } |
| 134 | x = device->base_x + value; |
| 135 | break; |
| 136 | case ABS_Y: |
| 137 | if (device->new_y) { |
| 138 | device->base_y = y - value; |
| 139 | device->new_y = 0; |
| 140 | } |
| 141 | y = device->base_y + value; |
| 142 | break; |
| 143 | } |
| 144 | break; |
| 145 | |
| 146 | case EV_KEY: |
| 147 | if (value == 2) |
| 148 | break; |
| 149 | |
| 150 | switch (e->code) { |
| 151 | case BTN_TOUCH: |
| 152 | case BTN_TOOL_PEN: |
| 153 | case BTN_TOOL_RUBBER: |
| 154 | case BTN_TOOL_BRUSH: |
| 155 | case BTN_TOOL_PENCIL: |
| 156 | case BTN_TOOL_AIRBRUSH: |
| 157 | case BTN_TOOL_FINGER: |
| 158 | case BTN_TOOL_MOUSE: |
| 159 | case BTN_TOOL_LENS: |
| 160 | if (device->tool == 0 && value) { |
| 161 | device->new_x = 1; |
| 162 | device->new_y = 1; |
| 163 | } |
| 164 | device->tool = value ? e->code : 0; |
| 165 | break; |
| 166 | |
| 167 | case BTN_LEFT: |
| 168 | case BTN_RIGHT: |
| 169 | case BTN_MIDDLE: |
| 170 | case BTN_SIDE: |
| 171 | case BTN_EXTRA: |
| 172 | case BTN_FORWARD: |
| 173 | case BTN_BACK: |
| 174 | case BTN_TASK: |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 175 | notify_button(&device->master->base, |
| 176 | e->code, value); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 177 | break; |
| 178 | |
| 179 | default: |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 180 | notify_key(&device->master->base, |
| 181 | e->code, value); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 182 | break; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if (dx != 0 || dy != 0) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 188 | notify_motion(&device->master->base, x + dx, y + dy); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 189 | if (absolute_event && device->tool) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 190 | notify_motion(&device->master->base, x, y); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static struct evdev_input_device * |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 194 | evdev_input_device_create(struct drm_input *master, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 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; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 207 | device->master = master; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 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 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 229 | static void |
| 230 | drm_input_create(struct drm_compositor *c) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 231 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 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; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 237 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 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; |
| 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->drm_fd, output->crtc_id, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 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 |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 304 | init_egl(struct drm_compositor *ec, struct udev_device *device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 305 | { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 306 | EGLint major, minor, count; |
| 307 | EGLConfig config; |
| 308 | PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa; |
| 309 | |
| 310 | static const EGLint config_attribs[] = { |
| 311 | EGL_SURFACE_TYPE, 0, |
| 312 | EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT, |
| 313 | EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, |
| 314 | EGL_NONE |
| 315 | }; |
| 316 | |
| 317 | get_typed_display_mesa = |
| 318 | (PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA"); |
| 319 | if (get_typed_display_mesa == NULL) { |
| 320 | fprintf(stderr, "eglGetTypedDisplayMESA() not found\n"); |
| 321 | return -1; |
| 322 | } |
| 323 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 324 | ec->base.base.device = strdup(udev_device_get_devnode(device)); |
| 325 | ec->drm_fd = open(ec->base.base.device, O_RDWR); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 326 | if (ec->drm_fd < 0) { |
| 327 | /* Probably permissions error */ |
| 328 | fprintf(stderr, "couldn't open %s, skipping\n", |
| 329 | udev_device_get_devnode(device)); |
| 330 | return -1; |
| 331 | } |
| 332 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 333 | ec->base.display = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA, |
| 334 | (void *) ec->drm_fd); |
| 335 | if (ec->base.display == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 336 | fprintf(stderr, "failed to create display\n"); |
| 337 | return -1; |
| 338 | } |
| 339 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 340 | if (!eglInitialize(ec->base.display, &major, &minor)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 341 | fprintf(stderr, "failed to initialize display\n"); |
| 342 | return -1; |
| 343 | } |
| 344 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 345 | if (!eglChooseConfig(ec->base.display, |
| 346 | config_attribs, &config, 1, &count) || |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 347 | count == 0) { |
| 348 | fprintf(stderr, "eglChooseConfig() failed\n"); |
| 349 | return -1; |
| 350 | } |
| 351 | |
| 352 | eglBindAPI(EGL_OPENGL_API); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 353 | ec->base.context = eglCreateContext(ec->base.display, |
| 354 | config, EGL_NO_CONTEXT, NULL); |
| 355 | if (ec->base.context == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 356 | fprintf(stderr, "failed to create context\n"); |
| 357 | return -1; |
| 358 | } |
| 359 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 360 | if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE, |
| 361 | EGL_NO_SURFACE, ec->base.context)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 362 | fprintf(stderr, "failed to make context current\n"); |
| 363 | return -1; |
| 364 | } |
| 365 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static drmModeModeInfo builtin_1024x768 = { |
| 370 | 63500, /* clock */ |
| 371 | 1024, 1072, 1176, 1328, 0, |
| 372 | 768, 771, 775, 798, 0, |
| 373 | 59920, |
| 374 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC, |
| 375 | 0, |
| 376 | "1024x768" |
| 377 | }; |
| 378 | |
| 379 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 380 | create_output_for_connector(struct drm_compositor *ec, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 381 | drmModeRes *resources, |
| 382 | drmModeConnector *connector) |
| 383 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 384 | struct drm_output *output; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 385 | drmModeEncoder *encoder; |
| 386 | drmModeModeInfo *mode; |
| 387 | int i, ret; |
| 388 | EGLint handle, stride, attribs[] = { |
| 389 | EGL_WIDTH, 0, |
| 390 | EGL_HEIGHT, 0, |
| 391 | EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA, |
| 392 | EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA, |
| 393 | EGL_NONE |
| 394 | }; |
| 395 | |
| 396 | output = malloc(sizeof *output); |
| 397 | if (output == NULL) |
| 398 | return -1; |
| 399 | |
| 400 | if (connector->count_modes > 0) |
| 401 | mode = &connector->modes[0]; |
| 402 | else |
| 403 | mode = &builtin_1024x768; |
| 404 | |
| 405 | encoder = drmModeGetEncoder(ec->drm_fd, connector->encoders[0]); |
| 406 | if (encoder == NULL) { |
| 407 | fprintf(stderr, "No encoder for connector.\n"); |
| 408 | return -1; |
| 409 | } |
| 410 | |
| 411 | for (i = 0; i < resources->count_crtcs; i++) { |
| 412 | if (encoder->possible_crtcs & (1 << i)) |
| 413 | break; |
| 414 | } |
| 415 | if (i == resources->count_crtcs) { |
| 416 | fprintf(stderr, "No usable crtc for encoder.\n"); |
| 417 | return -1; |
| 418 | } |
| 419 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 420 | memset(output, 0, sizeof *output); |
| 421 | wlsc_output_init(&output->base, &ec->base, 0, 0, |
| 422 | mode->hdisplay, mode->vdisplay); |
| 423 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 424 | output->crtc_id = resources->crtcs[i]; |
| 425 | output->connector_id = connector->connector_id; |
| 426 | output->mode = *mode; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 427 | |
| 428 | drmModeFreeEncoder(encoder); |
| 429 | |
| 430 | glGenRenderbuffers(2, output->rbo); |
| 431 | for (i = 0; i < 2; i++) { |
| 432 | glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]); |
| 433 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 434 | attribs[1] = output->base.width; |
| 435 | attribs[3] = output->base.height; |
| 436 | output->image[i] = |
| 437 | eglCreateDRMImageMESA(ec->base.display, attribs); |
| 438 | glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, |
| 439 | output->image[i]); |
| 440 | eglExportDRMImageMESA(ec->base.display, output->image[i], |
| 441 | NULL, &handle, &stride); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 442 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 443 | ret = drmModeAddFB(ec->drm_fd, |
| 444 | output->base.width, output->base.height, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 445 | 32, 32, stride, handle, &output->fb_id[i]); |
| 446 | if (ret) { |
| 447 | fprintf(stderr, "failed to add fb %d: %m\n", i); |
| 448 | return -1; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | output->current = 0; |
| 453 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 454 | GL_COLOR_ATTACHMENT0, |
| 455 | GL_RENDERBUFFER, |
| 456 | output->rbo[output->current]); |
| 457 | ret = drmModeSetCrtc(ec->drm_fd, output->crtc_id, |
| 458 | output->fb_id[output->current ^ 1], 0, 0, |
| 459 | &output->connector_id, 1, &output->mode); |
| 460 | if (ret) { |
| 461 | fprintf(stderr, "failed to set mode: %m\n"); |
| 462 | return -1; |
| 463 | } |
| 464 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 465 | wl_list_insert(ec->base.output_list.prev, &output->base.link); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 471 | create_outputs(struct drm_compositor *ec) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 472 | { |
| 473 | drmModeConnector *connector; |
| 474 | drmModeRes *resources; |
| 475 | int i; |
| 476 | |
| 477 | resources = drmModeGetResources(ec->drm_fd); |
| 478 | if (!resources) { |
| 479 | fprintf(stderr, "drmModeGetResources failed\n"); |
| 480 | return -1; |
| 481 | } |
| 482 | |
| 483 | for (i = 0; i < resources->count_connectors; i++) { |
| 484 | connector = drmModeGetConnector(ec->drm_fd, resources->connectors[i]); |
| 485 | if (connector == NULL) |
| 486 | continue; |
| 487 | |
| 488 | if (connector->connection == DRM_MODE_CONNECTED && |
| 489 | (option_connector == 0 || |
| 490 | connector->connector_id == option_connector)) |
| 491 | if (create_output_for_connector(ec, resources, connector) < 0) |
| 492 | return -1; |
| 493 | |
| 494 | drmModeFreeConnector(connector); |
| 495 | } |
| 496 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 497 | if (wl_list_empty(&ec->base.output_list)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 498 | fprintf(stderr, "No currently active connector found.\n"); |
| 499 | return -1; |
| 500 | } |
| 501 | |
| 502 | drmModeFreeResources(resources); |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static void on_enter_vt(int signal_number, void *data) |
| 508 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 509 | struct drm_compositor *ec = data; |
| 510 | struct drm_output *output; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 511 | int ret; |
| 512 | |
| 513 | ret = drmSetMaster(ec->drm_fd); |
| 514 | if (ret) { |
| 515 | fprintf(stderr, "failed to set drm master\n"); |
| 516 | kill(0, SIGTERM); |
| 517 | return; |
| 518 | } |
| 519 | |
| 520 | fprintf(stderr, "enter vt\n"); |
| 521 | |
| 522 | ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ); |
| 523 | ec->vt_active = 1; |
| 524 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 525 | wl_list_for_each(output, &ec->base.output_list, base.link) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 526 | ret = drmModeSetCrtc(ec->drm_fd, output->crtc_id, |
| 527 | output->fb_id[output->current ^ 1], 0, 0, |
| 528 | &output->connector_id, 1, &output->mode); |
| 529 | if (ret) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 530 | fprintf(stderr, |
| 531 | "failed to set mode for connector %d: %m\n", |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 532 | output->connector_id); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | static void on_leave_vt(int signal_number, void *data) |
| 537 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 538 | struct drm_compositor *ec = data; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 539 | int ret; |
| 540 | |
| 541 | ret = drmDropMaster(ec->drm_fd); |
| 542 | if (ret) { |
| 543 | fprintf(stderr, "failed to drop drm master\n"); |
| 544 | kill(0, SIGTERM); |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | ioctl (ec->tty_fd, VT_RELDISP, 1); |
| 549 | ec->vt_active = 0; |
| 550 | } |
| 551 | |
| 552 | static void |
| 553 | on_tty_input(int fd, uint32_t mask, void *data) |
| 554 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 555 | struct drm_compositor *ec = data; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 556 | |
| 557 | /* Ignore input to tty. We get keyboard events from evdev |
| 558 | */ |
| 559 | tcflush(ec->tty_fd, TCIFLUSH); |
| 560 | } |
| 561 | |
| 562 | static void on_term_signal(int signal_number, void *data) |
| 563 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 564 | struct drm_compositor *ec = data; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 565 | |
| 566 | if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0) |
| 567 | fprintf(stderr, "could not restore terminal to canonical mode\n"); |
| 568 | |
| 569 | exit(0); |
| 570 | } |
| 571 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 572 | static int setup_tty(struct drm_compositor *ec, struct wl_event_loop *loop) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 573 | { |
| 574 | struct termios raw_attributes; |
| 575 | struct vt_mode mode = { 0 }; |
| 576 | |
| 577 | ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY); |
| 578 | if (ec->tty_fd <= 0) { |
| 579 | fprintf(stderr, "failed to open active tty: %m\n"); |
| 580 | return -1; |
| 581 | } |
| 582 | |
| 583 | if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) { |
| 584 | fprintf(stderr, "could not get terminal attributes: %m\n"); |
| 585 | return -1; |
| 586 | } |
| 587 | |
| 588 | /* Ignore control characters and disable echo */ |
| 589 | raw_attributes = ec->terminal_attributes; |
| 590 | cfmakeraw(&raw_attributes); |
| 591 | |
| 592 | /* Fix up line endings to be normal (cfmakeraw hoses them) */ |
| 593 | raw_attributes.c_oflag |= OPOST | OCRNL; |
| 594 | |
| 595 | if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0) |
| 596 | fprintf(stderr, "could not put terminal into raw mode: %m\n"); |
| 597 | |
| 598 | ec->term_signal_source = |
| 599 | wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec); |
| 600 | |
| 601 | ec->tty_input_source = |
| 602 | wl_event_loop_add_fd(loop, ec->tty_fd, |
| 603 | WL_EVENT_READABLE, on_tty_input, ec); |
| 604 | |
| 605 | ec->vt_active = 1; |
| 606 | mode.mode = VT_PROCESS; |
| 607 | mode.relsig = SIGUSR1; |
| 608 | mode.acqsig = SIGUSR2; |
| 609 | if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) { |
| 610 | fprintf(stderr, "failed to take control of vt handling\n"); |
| 611 | } |
| 612 | |
| 613 | ec->leave_vt_source = |
| 614 | wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec); |
| 615 | ec->enter_vt_source = |
| 616 | wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec); |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 621 | struct wlsc_compositor * |
| 622 | drm_compositor_create(struct wl_display *display) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 623 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 624 | struct drm_compositor *ec; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 625 | struct udev_enumerate *e; |
| 626 | struct udev_list_entry *entry; |
| 627 | struct udev_device *device; |
| 628 | const char *path; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 629 | struct wl_event_loop *loop; |
| 630 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 631 | ec = malloc(sizeof *ec); |
| 632 | if (ec == NULL) |
| 633 | return NULL; |
| 634 | |
| 635 | memset(ec, 0, sizeof *ec); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 636 | ec->udev = udev_new(); |
| 637 | if (ec->udev == NULL) { |
| 638 | fprintf(stderr, "failed to initialize udev context\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 639 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 640 | } |
| 641 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 642 | e = udev_enumerate_new(ec->udev); |
| 643 | udev_enumerate_add_match_subsystem(e, "drm"); |
| 644 | udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1"); |
| 645 | udev_enumerate_scan_devices(e); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 646 | device = NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 647 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
| 648 | path = udev_list_entry_get_name(entry); |
| 649 | device = udev_device_new_from_syspath(ec->udev, path); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 650 | break; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 651 | } |
| 652 | udev_enumerate_unref(e); |
| 653 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 654 | if (device == NULL) { |
| 655 | fprintf(stderr, "no drm device found\n"); |
| 656 | return NULL; |
| 657 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 658 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame^] | 659 | if (init_egl(ec, device) < 0) { |
| 660 | fprintf(stderr, "failed to initialize egl\n"); |
| 661 | return NULL; |
| 662 | } |
| 663 | |
| 664 | /* Can't init base class until we have a current egl context */ |
| 665 | wlsc_compositor_init(&ec->base, display); |
| 666 | |
| 667 | if (create_outputs(ec) < 0) { |
| 668 | fprintf(stderr, "failed to create output for %s\n", path); |
| 669 | return NULL; |
| 670 | } |
| 671 | |
| 672 | drm_input_create(ec); |
| 673 | |
| 674 | loop = wl_display_get_event_loop(ec->base.wl_display); |
| 675 | ec->drm_source = |
| 676 | wl_event_loop_add_fd(loop, ec->drm_fd, |
| 677 | WL_EVENT_READABLE, on_drm_input, ec); |
| 678 | setup_tty(ec, loop); |
| 679 | ec->base.present = drm_compositor_present; |
| 680 | |
| 681 | return &ec->base; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 682 | } |