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