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 | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame^] | 308 | EGLint major, minor; |
| 309 | const char *extensions; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 310 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 311 | ec->base.base.device = strdup(udev_device_get_devnode(device)); |
| 312 | ec->drm_fd = open(ec->base.base.device, O_RDWR); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 313 | if (ec->drm_fd < 0) { |
| 314 | /* Probably permissions error */ |
| 315 | fprintf(stderr, "couldn't open %s, skipping\n", |
| 316 | udev_device_get_devnode(device)); |
| 317 | return -1; |
| 318 | } |
| 319 | |
Kristian Høgsberg | f252d6a | 2010-07-08 20:15:10 -0400 | [diff] [blame] | 320 | ec->base.display = eglGetDRMDisplayMESA(ec->drm_fd); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 321 | if (ec->base.display == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 322 | fprintf(stderr, "failed to create display\n"); |
| 323 | return -1; |
| 324 | } |
| 325 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 326 | if (!eglInitialize(ec->base.display, &major, &minor)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 327 | fprintf(stderr, "failed to initialize display\n"); |
| 328 | return -1; |
| 329 | } |
| 330 | |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame^] | 331 | extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS); |
| 332 | if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) { |
| 333 | fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | eglBindAPI(EGL_OPENGL_API); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 338 | ec->base.context = eglCreateContext(ec->base.display, |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame^] | 339 | NULL, EGL_NO_CONTEXT, NULL); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 340 | if (ec->base.context == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 341 | fprintf(stderr, "failed to create context\n"); |
| 342 | return -1; |
| 343 | } |
| 344 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 345 | if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE, |
| 346 | EGL_NO_SURFACE, ec->base.context)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 347 | fprintf(stderr, "failed to make context current\n"); |
| 348 | return -1; |
| 349 | } |
| 350 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static drmModeModeInfo builtin_1024x768 = { |
| 355 | 63500, /* clock */ |
| 356 | 1024, 1072, 1176, 1328, 0, |
| 357 | 768, 771, 775, 798, 0, |
| 358 | 59920, |
| 359 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC, |
| 360 | 0, |
| 361 | "1024x768" |
| 362 | }; |
| 363 | |
| 364 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 365 | create_output_for_connector(struct drm_compositor *ec, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 366 | drmModeRes *resources, |
| 367 | drmModeConnector *connector) |
| 368 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 369 | struct drm_output *output; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 370 | drmModeEncoder *encoder; |
| 371 | drmModeModeInfo *mode; |
| 372 | int i, ret; |
| 373 | EGLint handle, stride, attribs[] = { |
| 374 | EGL_WIDTH, 0, |
| 375 | EGL_HEIGHT, 0, |
| 376 | EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA, |
| 377 | EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA, |
| 378 | EGL_NONE |
| 379 | }; |
| 380 | |
| 381 | output = malloc(sizeof *output); |
| 382 | if (output == NULL) |
| 383 | return -1; |
| 384 | |
| 385 | if (connector->count_modes > 0) |
| 386 | mode = &connector->modes[0]; |
| 387 | else |
| 388 | mode = &builtin_1024x768; |
| 389 | |
| 390 | encoder = drmModeGetEncoder(ec->drm_fd, connector->encoders[0]); |
| 391 | if (encoder == NULL) { |
| 392 | fprintf(stderr, "No encoder for connector.\n"); |
| 393 | return -1; |
| 394 | } |
| 395 | |
| 396 | for (i = 0; i < resources->count_crtcs; i++) { |
| 397 | if (encoder->possible_crtcs & (1 << i)) |
| 398 | break; |
| 399 | } |
| 400 | if (i == resources->count_crtcs) { |
| 401 | fprintf(stderr, "No usable crtc for encoder.\n"); |
| 402 | return -1; |
| 403 | } |
| 404 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 405 | memset(output, 0, sizeof *output); |
| 406 | wlsc_output_init(&output->base, &ec->base, 0, 0, |
| 407 | mode->hdisplay, mode->vdisplay); |
| 408 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 409 | output->crtc_id = resources->crtcs[i]; |
| 410 | output->connector_id = connector->connector_id; |
| 411 | output->mode = *mode; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 412 | |
| 413 | drmModeFreeEncoder(encoder); |
| 414 | |
| 415 | glGenRenderbuffers(2, output->rbo); |
| 416 | for (i = 0; i < 2; i++) { |
| 417 | glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]); |
| 418 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 419 | attribs[1] = output->base.width; |
| 420 | attribs[3] = output->base.height; |
| 421 | output->image[i] = |
| 422 | eglCreateDRMImageMESA(ec->base.display, attribs); |
| 423 | glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, |
| 424 | output->image[i]); |
| 425 | eglExportDRMImageMESA(ec->base.display, output->image[i], |
| 426 | NULL, &handle, &stride); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 427 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 428 | ret = drmModeAddFB(ec->drm_fd, |
| 429 | output->base.width, output->base.height, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 430 | 32, 32, stride, handle, &output->fb_id[i]); |
| 431 | if (ret) { |
| 432 | fprintf(stderr, "failed to add fb %d: %m\n", i); |
| 433 | return -1; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | output->current = 0; |
| 438 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 439 | GL_COLOR_ATTACHMENT0, |
| 440 | GL_RENDERBUFFER, |
| 441 | output->rbo[output->current]); |
| 442 | ret = drmModeSetCrtc(ec->drm_fd, output->crtc_id, |
| 443 | output->fb_id[output->current ^ 1], 0, 0, |
| 444 | &output->connector_id, 1, &output->mode); |
| 445 | if (ret) { |
| 446 | fprintf(stderr, "failed to set mode: %m\n"); |
| 447 | return -1; |
| 448 | } |
| 449 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 450 | wl_list_insert(ec->base.output_list.prev, &output->base.link); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 456 | create_outputs(struct drm_compositor *ec) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 457 | { |
| 458 | drmModeConnector *connector; |
| 459 | drmModeRes *resources; |
| 460 | int i; |
| 461 | |
| 462 | resources = drmModeGetResources(ec->drm_fd); |
| 463 | if (!resources) { |
| 464 | fprintf(stderr, "drmModeGetResources failed\n"); |
| 465 | return -1; |
| 466 | } |
| 467 | |
| 468 | for (i = 0; i < resources->count_connectors; i++) { |
| 469 | connector = drmModeGetConnector(ec->drm_fd, resources->connectors[i]); |
| 470 | if (connector == NULL) |
| 471 | continue; |
| 472 | |
| 473 | if (connector->connection == DRM_MODE_CONNECTED && |
| 474 | (option_connector == 0 || |
| 475 | connector->connector_id == option_connector)) |
| 476 | if (create_output_for_connector(ec, resources, connector) < 0) |
| 477 | return -1; |
| 478 | |
| 479 | drmModeFreeConnector(connector); |
| 480 | } |
| 481 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 482 | if (wl_list_empty(&ec->base.output_list)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 483 | fprintf(stderr, "No currently active connector found.\n"); |
| 484 | return -1; |
| 485 | } |
| 486 | |
| 487 | drmModeFreeResources(resources); |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | static void on_enter_vt(int signal_number, void *data) |
| 493 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 494 | struct drm_compositor *ec = data; |
| 495 | struct drm_output *output; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 496 | int ret; |
| 497 | |
| 498 | ret = drmSetMaster(ec->drm_fd); |
| 499 | if (ret) { |
| 500 | fprintf(stderr, "failed to set drm master\n"); |
| 501 | kill(0, SIGTERM); |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | fprintf(stderr, "enter vt\n"); |
| 506 | |
| 507 | ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ); |
| 508 | ec->vt_active = 1; |
| 509 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 510 | wl_list_for_each(output, &ec->base.output_list, base.link) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 511 | ret = drmModeSetCrtc(ec->drm_fd, output->crtc_id, |
| 512 | output->fb_id[output->current ^ 1], 0, 0, |
| 513 | &output->connector_id, 1, &output->mode); |
| 514 | if (ret) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 515 | fprintf(stderr, |
| 516 | "failed to set mode for connector %d: %m\n", |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 517 | output->connector_id); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | static void on_leave_vt(int signal_number, void *data) |
| 522 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 523 | struct drm_compositor *ec = data; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 524 | int ret; |
| 525 | |
| 526 | ret = drmDropMaster(ec->drm_fd); |
| 527 | if (ret) { |
| 528 | fprintf(stderr, "failed to drop drm master\n"); |
| 529 | kill(0, SIGTERM); |
| 530 | return; |
| 531 | } |
| 532 | |
| 533 | ioctl (ec->tty_fd, VT_RELDISP, 1); |
| 534 | ec->vt_active = 0; |
| 535 | } |
| 536 | |
| 537 | static void |
| 538 | on_tty_input(int fd, uint32_t mask, void *data) |
| 539 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 540 | struct drm_compositor *ec = data; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 541 | |
| 542 | /* Ignore input to tty. We get keyboard events from evdev |
| 543 | */ |
| 544 | tcflush(ec->tty_fd, TCIFLUSH); |
| 545 | } |
| 546 | |
| 547 | static void on_term_signal(int signal_number, void *data) |
| 548 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 549 | struct drm_compositor *ec = data; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 550 | |
| 551 | if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0) |
| 552 | fprintf(stderr, "could not restore terminal to canonical mode\n"); |
| 553 | |
| 554 | exit(0); |
| 555 | } |
| 556 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 557 | 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] | 558 | { |
| 559 | struct termios raw_attributes; |
| 560 | struct vt_mode mode = { 0 }; |
| 561 | |
| 562 | ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY); |
| 563 | if (ec->tty_fd <= 0) { |
| 564 | fprintf(stderr, "failed to open active tty: %m\n"); |
| 565 | return -1; |
| 566 | } |
| 567 | |
| 568 | if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) { |
| 569 | fprintf(stderr, "could not get terminal attributes: %m\n"); |
| 570 | return -1; |
| 571 | } |
| 572 | |
| 573 | /* Ignore control characters and disable echo */ |
| 574 | raw_attributes = ec->terminal_attributes; |
| 575 | cfmakeraw(&raw_attributes); |
| 576 | |
| 577 | /* Fix up line endings to be normal (cfmakeraw hoses them) */ |
| 578 | raw_attributes.c_oflag |= OPOST | OCRNL; |
| 579 | |
| 580 | if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0) |
| 581 | fprintf(stderr, "could not put terminal into raw mode: %m\n"); |
| 582 | |
| 583 | ec->term_signal_source = |
| 584 | wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec); |
| 585 | |
| 586 | ec->tty_input_source = |
| 587 | wl_event_loop_add_fd(loop, ec->tty_fd, |
| 588 | WL_EVENT_READABLE, on_tty_input, ec); |
| 589 | |
| 590 | ec->vt_active = 1; |
| 591 | mode.mode = VT_PROCESS; |
| 592 | mode.relsig = SIGUSR1; |
| 593 | mode.acqsig = SIGUSR2; |
| 594 | if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) { |
| 595 | fprintf(stderr, "failed to take control of vt handling\n"); |
| 596 | } |
| 597 | |
| 598 | ec->leave_vt_source = |
| 599 | wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec); |
| 600 | ec->enter_vt_source = |
| 601 | wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec); |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 606 | struct wlsc_compositor * |
| 607 | drm_compositor_create(struct wl_display *display) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 608 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 609 | struct drm_compositor *ec; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 610 | struct udev_enumerate *e; |
| 611 | struct udev_list_entry *entry; |
| 612 | struct udev_device *device; |
| 613 | const char *path; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 614 | struct wl_event_loop *loop; |
| 615 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 616 | ec = malloc(sizeof *ec); |
| 617 | if (ec == NULL) |
| 618 | return NULL; |
| 619 | |
| 620 | memset(ec, 0, sizeof *ec); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 621 | ec->udev = udev_new(); |
| 622 | if (ec->udev == NULL) { |
| 623 | fprintf(stderr, "failed to initialize udev context\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 624 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 625 | } |
| 626 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 627 | e = udev_enumerate_new(ec->udev); |
| 628 | udev_enumerate_add_match_subsystem(e, "drm"); |
| 629 | udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1"); |
| 630 | udev_enumerate_scan_devices(e); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 631 | device = NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 632 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
| 633 | path = udev_list_entry_get_name(entry); |
| 634 | device = udev_device_new_from_syspath(ec->udev, path); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 635 | break; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 636 | } |
| 637 | udev_enumerate_unref(e); |
| 638 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 639 | if (device == NULL) { |
| 640 | fprintf(stderr, "no drm device found\n"); |
| 641 | return NULL; |
| 642 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 643 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 644 | if (init_egl(ec, device) < 0) { |
| 645 | fprintf(stderr, "failed to initialize egl\n"); |
| 646 | return NULL; |
| 647 | } |
| 648 | |
| 649 | /* 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] | 650 | if (wlsc_compositor_init(&ec->base, display) < 0) |
| 651 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 652 | |
| 653 | if (create_outputs(ec) < 0) { |
| 654 | fprintf(stderr, "failed to create output for %s\n", path); |
| 655 | return NULL; |
| 656 | } |
| 657 | |
| 658 | drm_input_create(ec); |
| 659 | |
| 660 | loop = wl_display_get_event_loop(ec->base.wl_display); |
| 661 | ec->drm_source = |
| 662 | wl_event_loop_add_fd(loop, ec->drm_fd, |
| 663 | WL_EVENT_READABLE, on_drm_input, ec); |
| 664 | setup_tty(ec, loop); |
| 665 | ec->base.present = drm_compositor_present; |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 666 | ec->base.focus = 1; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 667 | |
| 668 | return &ec->base; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 669 | } |