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