Kristian Høgsberg | ffd710e | 2008-12-02 15:15:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 Kristian Høgsberg |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 5 | * documentation for any purpose is hereby granted without fee, provided that |
| 6 | * the above copyright notice appear in all copies and that both that copyright |
| 7 | * notice and this permission notice appear in supporting documentation, and |
| 8 | * that the name of the copyright holders not be used in advertising or |
| 9 | * publicity pertaining to distribution of the software without specific, |
| 10 | * written prior permission. The copyright holders make no representations |
| 11 | * about the suitability of this software for any purpose. It is provided "as |
| 12 | * is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 15 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 16 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 17 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 18 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 19 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 20 | * OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdint.h> |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 27 | #include <stdarg.h> |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 28 | #include <i915_drm.h> |
| 29 | #include <sys/ioctl.h> |
| 30 | #include <sys/mman.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <unistd.h> |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 33 | #include <signal.h> |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 34 | #include <cairo.h> |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 35 | #include <gdk-pixbuf/gdk-pixbuf.h> |
| 36 | #include <glib.h> |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 37 | #include <sys/poll.h> |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 38 | #include <png.h> |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 39 | #include <math.h> |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 40 | #include <linux/input.h> |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 41 | #include <xf86drmMode.h> |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 42 | #include <time.h> |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 43 | #include <fnmatch.h> |
| 44 | #include <dirent.h> |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 45 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 46 | #include <GL/gl.h> |
| 47 | #include <eagle.h> |
| 48 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 49 | #include "wayland.h" |
| 50 | #include "cairo-util.h" |
| 51 | #include "egl-compositor.h" |
| 52 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 53 | #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) |
| 54 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 55 | struct egl_input_device { |
| 56 | struct wl_object base; |
| 57 | int32_t x, y; |
| 58 | struct egl_compositor *ec; |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 59 | struct egl_surface *pointer_surface; |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 60 | struct wl_list link; |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 61 | |
| 62 | int grab; |
| 63 | struct egl_surface *grab_surface; |
Kristian Høgsberg | a7700c8 | 2008-12-12 13:48:30 -0500 | [diff] [blame^] | 64 | struct egl_surface *focus_surface; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 65 | }; |
| 66 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 67 | struct egl_compositor { |
| 68 | struct wl_compositor base; |
| 69 | EGLDisplay display; |
| 70 | EGLSurface surface; |
| 71 | EGLContext context; |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 72 | EGLConfig config; |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 73 | struct wl_display *wl_display; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 74 | int gem_fd; |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 75 | int width, height; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 76 | struct egl_surface *background; |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 77 | struct egl_surface *overlay; |
Kristian Høgsberg | 5c1e6ec | 2008-11-25 13:51:36 -0500 | [diff] [blame] | 78 | double overlay_y, overlay_target, overlay_previous; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 79 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 80 | struct wl_list input_device_list; |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 81 | struct wl_list surface_list; |
| 82 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 83 | /* Repaint state. */ |
| 84 | struct wl_event_source *timer_source; |
| 85 | int repaint_needed; |
| 86 | int repaint_on_timeout; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 87 | struct timespec previous_swap; |
| 88 | uint32_t current_frame; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 89 | }; |
| 90 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 91 | struct egl_surface { |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 92 | GLuint texture; |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 93 | struct wl_map map; |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 94 | EGLSurface surface; |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 95 | struct wl_surface *wl_surface; |
| 96 | int width, height; |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 97 | struct wl_list link; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 98 | }; |
| 99 | |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 100 | static void |
| 101 | die(const char *msg, ...) |
| 102 | { |
| 103 | va_list ap; |
| 104 | |
| 105 | va_start (ap, msg); |
| 106 | vfprintf(stderr, msg, ap); |
| 107 | va_end (ap); |
| 108 | |
| 109 | exit(EXIT_FAILURE); |
| 110 | } |
| 111 | |
| 112 | static void |
| 113 | stdio_write_func (png_structp png, png_bytep data, png_size_t size) |
| 114 | { |
| 115 | FILE *fp; |
| 116 | size_t ret; |
| 117 | |
| 118 | fp = png_get_io_ptr (png); |
| 119 | while (size) { |
| 120 | ret = fwrite (data, 1, size, fp); |
| 121 | size -= ret; |
| 122 | data += ret; |
| 123 | if (size && ferror (fp)) |
| 124 | die("write: %m\n"); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static void |
| 129 | png_simple_output_flush_fn (png_structp png_ptr) |
| 130 | { |
| 131 | } |
| 132 | |
| 133 | static void |
| 134 | png_simple_error_callback (png_structp png, |
| 135 | png_const_charp error_msg) |
| 136 | { |
| 137 | die("png error: %s\n", error_msg); |
| 138 | } |
| 139 | |
| 140 | static void |
| 141 | png_simple_warning_callback (png_structp png, |
| 142 | png_const_charp error_msg) |
| 143 | { |
| 144 | fprintf(stderr, "png warning: %s\n", error_msg); |
| 145 | } |
| 146 | |
| 147 | static void |
| 148 | convert_pixels(png_structp png, png_row_infop row_info, png_bytep data) |
| 149 | { |
| 150 | unsigned int i; |
| 151 | |
| 152 | for (i = 0; i < row_info->rowbytes; i += 4) { |
| 153 | uint8_t *b = &data[i]; |
| 154 | uint32_t pixel; |
| 155 | |
| 156 | memcpy (&pixel, b, sizeof (uint32_t)); |
| 157 | b[0] = (pixel & 0xff0000) >> 16; |
| 158 | b[1] = (pixel & 0x00ff00) >> 8; |
| 159 | b[2] = (pixel & 0x0000ff) >> 0; |
| 160 | b[3] = 0; |
| 161 | } |
| 162 | } |
| 163 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 164 | struct screenshooter { |
| 165 | struct wl_object base; |
| 166 | struct egl_compositor *ec; |
| 167 | }; |
| 168 | |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 169 | static void |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 170 | screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter) |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 171 | { |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 172 | struct egl_compositor *ec = shooter->ec; |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 173 | png_struct *png; |
| 174 | png_info *info; |
| 175 | png_byte **volatile rows = NULL; |
| 176 | png_color_16 white; |
| 177 | int depth, i; |
| 178 | FILE *fp; |
| 179 | uint8_t *data; |
| 180 | GLuint stride; |
| 181 | static const char filename[] = "wayland-screenshot.png"; |
| 182 | |
| 183 | data = eglReadBuffer(ec->display, ec->surface, GL_FRONT_LEFT, &stride); |
| 184 | if (data == NULL) |
| 185 | die("eglReadBuffer failed\n"); |
| 186 | rows = malloc(ec->height * sizeof rows[0]); |
| 187 | if (rows == NULL) |
| 188 | die("malloc failed\n"); |
| 189 | |
| 190 | for (i = 0; i < ec->height; i++) |
| 191 | rows[i] = (png_byte *) data + i * stride; |
| 192 | |
| 193 | png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, |
| 194 | png_simple_error_callback, |
| 195 | png_simple_warning_callback); |
| 196 | if (png == NULL) |
| 197 | die("png_create_write_struct failed\n"); |
| 198 | |
| 199 | info = png_create_info_struct(png); |
| 200 | if (info == NULL) |
| 201 | die("png_create_info_struct failed\n"); |
| 202 | |
| 203 | fp = fopen(filename, "w"); |
| 204 | if (fp == NULL) |
| 205 | die("fopen failed: %m\n"); |
| 206 | |
| 207 | png_set_write_fn(png, fp, stdio_write_func, png_simple_output_flush_fn); |
| 208 | |
| 209 | depth = 8; |
| 210 | png_set_IHDR(png, info, |
| 211 | ec->width, |
| 212 | ec->height, depth, |
| 213 | PNG_COLOR_TYPE_RGB, |
| 214 | PNG_INTERLACE_NONE, |
| 215 | PNG_COMPRESSION_TYPE_DEFAULT, |
| 216 | PNG_FILTER_TYPE_DEFAULT); |
| 217 | |
| 218 | white.gray = (1 << depth) - 1; |
| 219 | white.red = white.blue = white.green = white.gray; |
| 220 | png_set_bKGD(png, info, &white); |
| 221 | png_write_info (png, info); |
| 222 | png_set_write_user_transform_fn(png, convert_pixels); |
| 223 | |
| 224 | png_set_filler(png, 0, PNG_FILLER_AFTER); |
| 225 | png_write_image(png, rows); |
| 226 | png_write_end(png, info); |
| 227 | |
| 228 | png_destroy_write_struct(&png, &info); |
| 229 | fclose(fp); |
| 230 | free(rows); |
| 231 | free(data); |
| 232 | } |
| 233 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 234 | static const struct wl_method screenshooter_methods[] = { |
Kristian Høgsberg | 73f4e76 | 2008-12-08 14:07:33 -0500 | [diff] [blame] | 235 | { "shoot", screenshooter_shoot, "", NULL } |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | static const struct wl_interface screenshooter_interface = { |
| 239 | "screenshooter", 1, |
| 240 | ARRAY_LENGTH(screenshooter_methods), |
| 241 | screenshooter_methods, |
| 242 | }; |
| 243 | |
| 244 | static struct screenshooter * |
| 245 | screenshooter_create(struct egl_compositor *ec) |
| 246 | { |
| 247 | struct screenshooter *shooter; |
| 248 | |
| 249 | shooter = malloc(sizeof *shooter); |
| 250 | if (shooter == NULL) |
| 251 | return NULL; |
| 252 | |
| 253 | shooter->base.interface = &screenshooter_interface; |
| 254 | shooter->ec = ec; |
| 255 | |
| 256 | return shooter; |
| 257 | }; |
| 258 | |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 259 | static struct egl_surface * |
| 260 | egl_surface_create_from_cairo_surface(cairo_surface_t *surface, |
| 261 | int x, int y, int width, int height) |
| 262 | { |
| 263 | struct egl_surface *es; |
| 264 | int stride; |
| 265 | void *data; |
| 266 | |
| 267 | stride = cairo_image_surface_get_stride(surface); |
| 268 | data = cairo_image_surface_get_data(surface); |
| 269 | |
| 270 | es = malloc(sizeof *es); |
| 271 | if (es == NULL) |
| 272 | return NULL; |
| 273 | |
| 274 | glGenTextures(1, &es->texture); |
| 275 | glBindTexture(GL_TEXTURE_2D, es->texture); |
| 276 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
Kristian Høgsberg | 98fed0f | 2008-12-09 13:35:35 -0500 | [diff] [blame] | 277 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT); |
| 278 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 279 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 280 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, |
| 281 | GL_BGRA, GL_UNSIGNED_BYTE, data); |
| 282 | |
| 283 | es->map.x = x; |
| 284 | es->map.y = y; |
| 285 | es->map.width = width; |
| 286 | es->map.height = height; |
| 287 | es->surface = EGL_NO_SURFACE; |
| 288 | |
| 289 | return es; |
| 290 | } |
| 291 | |
| 292 | static void |
| 293 | egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec) |
| 294 | { |
| 295 | glDeleteTextures(1, &es->texture); |
| 296 | if (es->surface != EGL_NO_SURFACE) |
| 297 | eglDestroySurface(ec->display, es->surface); |
| 298 | free(es); |
| 299 | } |
| 300 | |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 301 | static void |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 302 | pointer_path(cairo_t *cr, int x, int y) |
| 303 | { |
| 304 | const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10; |
| 305 | const int width = 16, height = 16; |
| 306 | |
| 307 | cairo_move_to(cr, x, y); |
| 308 | cairo_line_to(cr, x + tx, y + ty); |
| 309 | cairo_line_to(cr, x + dx, y + dy); |
| 310 | cairo_line_to(cr, x + width - end, y + height); |
| 311 | cairo_line_to(cr, x + width, y + height - end); |
| 312 | cairo_line_to(cr, x + dy, y + dx); |
| 313 | cairo_line_to(cr, x + ty, y + tx); |
| 314 | cairo_close_path(cr); |
| 315 | } |
| 316 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 317 | static struct egl_surface * |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 318 | pointer_create(int x, int y, int width, int height) |
| 319 | { |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 320 | struct egl_surface *es; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 321 | const int hotspot_x = 16, hotspot_y = 16; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 322 | cairo_surface_t *surface; |
| 323 | cairo_t *cr; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 324 | |
| 325 | surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 326 | width, height); |
| 327 | |
| 328 | cr = cairo_create(surface); |
| 329 | pointer_path(cr, hotspot_x + 5, hotspot_y + 4); |
| 330 | cairo_set_line_width (cr, 2); |
| 331 | cairo_set_source_rgb(cr, 0, 0, 0); |
| 332 | cairo_stroke_preserve(cr); |
| 333 | cairo_fill(cr); |
| 334 | blur_surface(surface, width); |
| 335 | |
| 336 | pointer_path(cr, hotspot_x, hotspot_y); |
| 337 | cairo_stroke_preserve(cr); |
| 338 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 339 | cairo_fill(cr); |
| 340 | cairo_destroy(cr); |
| 341 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 342 | es = egl_surface_create_from_cairo_surface(surface, |
| 343 | x - hotspot_x, |
| 344 | y - hotspot_y, |
| 345 | width, height); |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 346 | |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 347 | cairo_surface_destroy(surface); |
| 348 | |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 349 | return es; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static struct egl_surface * |
| 353 | background_create(const char *filename, int width, int height) |
| 354 | { |
| 355 | struct egl_surface *background; |
| 356 | GdkPixbuf *pixbuf; |
| 357 | GError *error = NULL; |
| 358 | int pixbuf_width, pixbuf_height; |
| 359 | void *data; |
| 360 | |
| 361 | background = malloc(sizeof *background); |
| 362 | if (background == NULL) |
| 363 | return NULL; |
| 364 | |
| 365 | g_type_init(); |
| 366 | |
| 367 | pixbuf = gdk_pixbuf_new_from_file(filename, &error); |
| 368 | if (error != NULL) { |
| 369 | free(background); |
| 370 | return NULL; |
| 371 | } |
| 372 | |
| 373 | pixbuf_width = gdk_pixbuf_get_width(pixbuf); |
| 374 | pixbuf_height = gdk_pixbuf_get_height(pixbuf); |
| 375 | data = gdk_pixbuf_get_pixels(pixbuf); |
| 376 | |
| 377 | glGenTextures(1, &background->texture); |
| 378 | glBindTexture(GL_TEXTURE_2D, background->texture); |
| 379 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
Kristian Høgsberg | 98fed0f | 2008-12-09 13:35:35 -0500 | [diff] [blame] | 380 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT); |
| 381 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 382 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 383 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixbuf_width, pixbuf_height, 0, |
| 384 | GL_BGR, GL_UNSIGNED_BYTE, data); |
| 385 | |
| 386 | background->map.x = 0; |
| 387 | background->map.y = 0; |
| 388 | background->map.width = width; |
| 389 | background->map.height = height; |
| 390 | background->surface = EGL_NO_SURFACE; |
| 391 | |
| 392 | return background; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static void |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 396 | rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius) |
| 397 | { |
| 398 | cairo_move_to(cr, x0, y0 + radius); |
| 399 | cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2); |
| 400 | cairo_line_to(cr, x1 - radius, y0); |
| 401 | cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI); |
| 402 | cairo_line_to(cr, x1, y1 - radius); |
| 403 | cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2); |
| 404 | cairo_line_to(cr, x0 + radius, y1); |
| 405 | cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI); |
| 406 | cairo_close_path(cr); |
| 407 | } |
| 408 | |
| 409 | static void |
| 410 | draw_button(cairo_t *cr, int x, int y, int width, int height, const char *text) |
| 411 | { |
| 412 | cairo_pattern_t *gradient; |
| 413 | cairo_text_extents_t extents; |
| 414 | double bright = 0.15, dim = 0.02; |
| 415 | int radius = 10; |
| 416 | |
| 417 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 418 | cairo_set_line_width (cr, 2); |
| 419 | rounded_rect(cr, x, y, x + width, y + height, radius); |
| 420 | cairo_set_source_rgb(cr, dim, dim, dim); |
| 421 | cairo_stroke(cr); |
| 422 | rounded_rect(cr, x + 2, y + 2, x + width, y + height, radius); |
| 423 | cairo_set_source_rgb(cr, 0.1, 0.1, 0.1); |
| 424 | cairo_stroke(cr); |
| 425 | |
| 426 | rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1); |
| 427 | cairo_set_source_rgb(cr, bright, bright, bright); |
| 428 | cairo_stroke(cr); |
| 429 | rounded_rect(cr, x + 3, y + 3, x + width - 1, y + height - 1, radius - 1); |
| 430 | cairo_set_source_rgb(cr, dim, dim, dim); |
| 431 | cairo_stroke(cr); |
| 432 | |
| 433 | rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1); |
| 434 | gradient = cairo_pattern_create_linear (0, y, 0, y + height); |
| 435 | cairo_pattern_add_color_stop_rgb(gradient, 0, 0.15, 0.15, 0.15); |
| 436 | cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.08, 0.08, 0.08); |
| 437 | cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.07, 0.07, 0.07); |
| 438 | cairo_pattern_add_color_stop_rgb(gradient, 1, 0.1, 0.1, 0.1); |
| 439 | cairo_set_source(cr, gradient); |
| 440 | cairo_fill(cr); |
| 441 | |
| 442 | cairo_set_font_size(cr, 16); |
| 443 | cairo_text_extents(cr, text, &extents); |
| 444 | cairo_move_to(cr, x + (width - extents.width) / 2, y + (height - extents.height) / 2 - extents.y_bearing); |
| 445 | cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); |
| 446 | cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); |
| 447 | cairo_set_line_width (cr, 4); |
| 448 | cairo_text_path(cr, text); |
| 449 | cairo_set_source_rgb(cr, 0.0, 0.0, 0.0); |
| 450 | cairo_stroke_preserve(cr); |
| 451 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 452 | cairo_fill(cr); |
| 453 | } |
| 454 | |
| 455 | static struct egl_surface * |
| 456 | overlay_create(int x, int y, int width, int height) |
| 457 | { |
| 458 | struct egl_surface *es; |
| 459 | cairo_surface_t *surface; |
| 460 | cairo_t *cr; |
| 461 | int total_width, button_x, button_y; |
| 462 | const int button_width = 150; |
| 463 | const int button_height = 40; |
| 464 | const int spacing = 50; |
| 465 | |
| 466 | surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 467 | width, height); |
| 468 | |
| 469 | cr = cairo_create(surface); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 470 | cairo_set_source_rgba(cr, 0.1, 0.1, 0.1, 0.8); |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 471 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 472 | cairo_paint(cr); |
| 473 | |
| 474 | total_width = button_width * 2 + spacing; |
| 475 | button_x = (width - total_width) / 2; |
| 476 | button_y = height - button_height - 20; |
| 477 | draw_button(cr, button_x, button_y, button_width, button_height, "Previous"); |
| 478 | button_x += button_width + spacing; |
| 479 | draw_button(cr, button_x, button_y, button_width, button_height, "Next"); |
| 480 | |
| 481 | cairo_destroy(cr); |
| 482 | |
| 483 | es = egl_surface_create_from_cairo_surface(surface, x, y, width, height); |
| 484 | |
| 485 | cairo_surface_destroy(surface); |
| 486 | |
| 487 | return es; |
| 488 | } |
| 489 | |
| 490 | static void |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 491 | draw_surface(struct egl_surface *es) |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 492 | { |
| 493 | GLint vertices[12]; |
| 494 | GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 }; |
| 495 | GLuint indices[4] = { 0, 1, 2, 3 }; |
| 496 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 497 | vertices[0] = es->map.x; |
| 498 | vertices[1] = es->map.y; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 499 | vertices[2] = 0; |
| 500 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 501 | vertices[3] = es->map.x; |
| 502 | vertices[4] = es->map.y + es->map.height; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 503 | vertices[5] = 0; |
| 504 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 505 | vertices[6] = es->map.x + es->map.width; |
| 506 | vertices[7] = es->map.y; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 507 | vertices[8] = 0; |
| 508 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 509 | vertices[9] = es->map.x + es->map.width; |
| 510 | vertices[10] = es->map.y + es->map.height; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 511 | vertices[11] = 0; |
| 512 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 513 | glBindTexture(GL_TEXTURE_2D, es->texture); |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 514 | glEnable(GL_TEXTURE_2D); |
| 515 | glEnable(GL_BLEND); |
| 516 | /* Assume pre-multiplied alpha for now, this probably |
| 517 | * needs to be a wayland visual type of thing. */ |
| 518 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 519 | |
| 520 | glEnableClientState(GL_VERTEX_ARRAY); |
| 521 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 522 | glVertexPointer(3, GL_INT, 0, vertices); |
| 523 | glTexCoordPointer(2, GL_INT, 0, tex_coords); |
| 524 | glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices); |
| 525 | } |
| 526 | |
| 527 | static void |
Kristian Høgsberg | 9af92b3 | 2008-11-24 01:12:46 -0500 | [diff] [blame] | 528 | schedule_repaint(struct egl_compositor *ec); |
| 529 | |
| 530 | static void |
Kristian Høgsberg | 5c1e6ec | 2008-11-25 13:51:36 -0500 | [diff] [blame] | 531 | animate_overlay(struct egl_compositor *ec) |
| 532 | { |
| 533 | double force, y; |
| 534 | int32_t top, bottom; |
Kristian Høgsberg | ffb7406 | 2008-11-25 18:10:39 -0500 | [diff] [blame] | 535 | #if 1 |
| 536 | double bounce = 0.0; |
| 537 | double friction = 1.0; |
| 538 | double spring = 0.2; |
| 539 | #else |
| 540 | double bounce = 0.2; |
| 541 | double friction = 0.04; |
| 542 | double spring = 0.09; |
| 543 | #endif |
Kristian Høgsberg | 5c1e6ec | 2008-11-25 13:51:36 -0500 | [diff] [blame] | 544 | |
| 545 | y = ec->overlay_y; |
Kristian Høgsberg | ffb7406 | 2008-11-25 18:10:39 -0500 | [diff] [blame] | 546 | force = (ec->overlay_target - ec->overlay_y) * spring + |
| 547 | (ec->overlay_previous - y) * friction; |
Kristian Høgsberg | 5c1e6ec | 2008-11-25 13:51:36 -0500 | [diff] [blame] | 548 | |
| 549 | ec->overlay_y = y + (y - ec->overlay_previous) + force; |
| 550 | ec->overlay_previous = y; |
| 551 | |
| 552 | top = ec->height - ec->overlay->map.height; |
| 553 | bottom = ec->height; |
| 554 | if (ec->overlay_y >= bottom) { |
| 555 | ec->overlay_y = bottom; |
| 556 | ec->overlay_previous = bottom; |
| 557 | } |
| 558 | |
| 559 | if (ec->overlay_y <= top) { |
Kristian Høgsberg | ffb7406 | 2008-11-25 18:10:39 -0500 | [diff] [blame] | 560 | ec->overlay_y = top + bounce * (top - ec->overlay_y); |
| 561 | ec->overlay_previous = |
| 562 | top + bounce * (top - ec->overlay_previous); |
Kristian Høgsberg | 5c1e6ec | 2008-11-25 13:51:36 -0500 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | ec->overlay->map.y = ec->overlay_y + 0.5; |
Kristian Høgsberg | ffb7406 | 2008-11-25 18:10:39 -0500 | [diff] [blame] | 566 | |
Kristian Høgsberg | 73c3058 | 2008-11-25 22:45:46 -0500 | [diff] [blame] | 567 | if (fabs(y - ec->overlay_target) > 0.2 || |
| 568 | fabs(ec->overlay_y - ec->overlay_target) > 0.2) |
Kristian Høgsberg | 5c1e6ec | 2008-11-25 13:51:36 -0500 | [diff] [blame] | 569 | schedule_repaint(ec); |
| 570 | } |
| 571 | |
| 572 | static void |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 573 | repaint(void *data) |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 574 | { |
| 575 | struct egl_compositor *ec = data; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 576 | struct egl_surface *es; |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 577 | struct egl_input_device *eid; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 578 | struct timespec ts; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 579 | uint32_t msecs; |
| 580 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 581 | if (!ec->repaint_needed) { |
| 582 | ec->repaint_on_timeout = 0; |
| 583 | return; |
| 584 | } |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 585 | |
| 586 | draw_surface(ec->background); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 587 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 588 | es = container_of(ec->surface_list.next, |
| 589 | struct egl_surface, link); |
| 590 | while (&es->link != &ec->surface_list) { |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 591 | draw_surface(es); |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 592 | |
| 593 | es = container_of(es->link.next, |
| 594 | struct egl_surface, link); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 595 | } |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 596 | |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 597 | draw_surface(ec->overlay); |
| 598 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 599 | eid = container_of(ec->input_device_list.next, |
| 600 | struct egl_input_device, link); |
| 601 | while (&eid->link != &ec->input_device_list) { |
| 602 | draw_surface(eid->pointer_surface); |
| 603 | |
| 604 | eid = container_of(eid->link.next, |
| 605 | struct egl_input_device, link); |
| 606 | } |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 607 | |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 608 | eglSwapBuffers(ec->display, ec->surface); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 609 | ec->repaint_needed = 0; |
Kristian Høgsberg | 9af92b3 | 2008-11-24 01:12:46 -0500 | [diff] [blame] | 610 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 611 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 612 | msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000); |
| 613 | wl_display_post_frame(ec->wl_display, ec->current_frame, msecs); |
| 614 | ec->current_frame++; |
| 615 | |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 616 | wl_event_source_timer_update(ec->timer_source, 10); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 617 | ec->repaint_on_timeout = 1; |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 618 | |
Kristian Høgsberg | 5c1e6ec | 2008-11-25 13:51:36 -0500 | [diff] [blame] | 619 | animate_overlay(ec); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 620 | } |
| 621 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 622 | static void |
| 623 | schedule_repaint(struct egl_compositor *ec) |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 624 | { |
| 625 | struct wl_event_loop *loop; |
| 626 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 627 | ec->repaint_needed = 1; |
| 628 | if (!ec->repaint_on_timeout) { |
| 629 | loop = wl_display_get_event_loop(ec->wl_display); |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 630 | wl_event_loop_add_idle(loop, repaint, ec); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 631 | } |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 632 | } |
| 633 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 634 | static void |
| 635 | notify_surface_create(struct wl_compositor *compositor, |
| 636 | struct wl_surface *surface) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 637 | { |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 638 | struct egl_compositor *ec = (struct egl_compositor *) compositor; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 639 | struct egl_surface *es; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 640 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 641 | es = malloc(sizeof *es); |
| 642 | if (es == NULL) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 643 | return; |
| 644 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 645 | es->surface = EGL_NO_SURFACE; |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 646 | es->wl_surface = surface; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 647 | wl_surface_set_data(surface, es); |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 648 | wl_list_insert(ec->surface_list.prev, &es->link); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 649 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 650 | glGenTextures(1, &es->texture); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 651 | } |
| 652 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 653 | static void |
| 654 | notify_surface_destroy(struct wl_compositor *compositor, |
| 655 | struct wl_surface *surface) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 656 | { |
| 657 | struct egl_compositor *ec = (struct egl_compositor *) compositor; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 658 | struct egl_surface *es; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 659 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 660 | es = wl_surface_get_data(surface); |
| 661 | if (es == NULL) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 662 | return; |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 663 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 664 | wl_list_remove(&es->link); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 665 | egl_surface_destroy(es, ec); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 666 | |
| 667 | schedule_repaint(ec); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 668 | } |
| 669 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 670 | static void |
| 671 | notify_surface_attach(struct wl_compositor *compositor, |
| 672 | struct wl_surface *surface, uint32_t name, |
| 673 | uint32_t width, uint32_t height, uint32_t stride) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 674 | { |
| 675 | struct egl_compositor *ec = (struct egl_compositor *) compositor; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 676 | struct egl_surface *es; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 677 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 678 | es = wl_surface_get_data(surface); |
| 679 | if (es == NULL) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 680 | return; |
| 681 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 682 | if (es->surface != EGL_NO_SURFACE) |
| 683 | eglDestroySurface(ec->display, es->surface); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 684 | |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 685 | es->width = width; |
| 686 | es->height = height; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 687 | es->surface = eglCreateSurfaceForName(ec->display, ec->config, |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 688 | name, width, height, stride, NULL); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 689 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 690 | glBindTexture(GL_TEXTURE_2D, es->texture); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 691 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
Kristian Høgsberg | 98fed0f | 2008-12-09 13:35:35 -0500 | [diff] [blame] | 692 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT); |
| 693 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 694 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 695 | eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D); |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 696 | } |
| 697 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 698 | static void |
| 699 | notify_surface_map(struct wl_compositor *compositor, |
| 700 | struct wl_surface *surface, struct wl_map *map) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 701 | { |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 702 | struct egl_surface *es; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 703 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 704 | es = wl_surface_get_data(surface); |
| 705 | if (es == NULL) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 706 | return; |
| 707 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 708 | es->map = *map; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 709 | } |
| 710 | |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 711 | static void |
| 712 | notify_surface_copy(struct wl_compositor *compositor, |
| 713 | struct wl_surface *surface, |
| 714 | int32_t dst_x, int32_t dst_y, |
| 715 | uint32_t name, uint32_t stride, |
| 716 | int32_t x, int32_t y, int32_t width, int32_t height) |
| 717 | { |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 718 | struct egl_compositor *ec = (struct egl_compositor *) compositor; |
| 719 | EGLSurface src; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 720 | struct egl_surface *es; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 721 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 722 | es = wl_surface_get_data(surface); |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 723 | |
| 724 | /* FIXME: glCopyPixels should work, but then we'll have to |
| 725 | * call eglMakeCurrent to set up the src and dest surfaces |
| 726 | * first. This seems cheaper, but maybe there's a better way |
| 727 | * to accomplish this. */ |
| 728 | |
| 729 | src = eglCreateSurfaceForName(ec->display, ec->config, |
| 730 | name, x + width, y + height, stride, NULL); |
| 731 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 732 | eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y, |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 733 | src, GL_FRONT_LEFT, x, y, width, height); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 734 | eglDestroySurface(ec->display, src); |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | static void |
| 738 | notify_surface_damage(struct wl_compositor *compositor, |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 739 | struct wl_surface *surface, |
| 740 | int32_t x, int32_t y, int32_t width, int32_t height) |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 741 | { |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 742 | /* FIXME: This need to take a damage region, of course. */ |
| 743 | } |
| 744 | |
| 745 | static uint32_t |
| 746 | notify_commit(struct wl_compositor *compositor) |
| 747 | { |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 748 | struct egl_compositor *ec = (struct egl_compositor *) compositor; |
| 749 | |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 750 | schedule_repaint(ec); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 751 | |
| 752 | return ec->current_frame; |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 753 | } |
| 754 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 755 | static struct egl_surface * |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 756 | pick_surface(struct egl_input_device *device) |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 757 | { |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 758 | struct egl_compositor *ec = device->ec; |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 759 | struct egl_surface *es; |
| 760 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 761 | if (device->grab > 0) |
| 762 | return device->grab_surface; |
| 763 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 764 | es = container_of(ec->surface_list.prev, |
| 765 | struct egl_surface, link); |
| 766 | while (&es->link != &ec->surface_list) { |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 767 | if (es->map.x <= device->x && |
| 768 | device->x < es->map.x + es->map.width && |
| 769 | es->map.y <= device->y && |
| 770 | device->y < es->map.y + es->map.height) |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 771 | return es; |
| 772 | |
| 773 | es = container_of(es->link.prev, |
| 774 | struct egl_surface, link); |
| 775 | } |
| 776 | |
| 777 | return NULL; |
| 778 | } |
| 779 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 780 | void |
| 781 | notify_motion(struct egl_input_device *device, int x, int y) |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 782 | { |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 783 | struct egl_surface *es; |
| 784 | const int hotspot_x = 16, hotspot_y = 16; |
| 785 | int32_t sx, sy; |
| 786 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 787 | es = pick_surface(device); |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 788 | |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 789 | if (es) { |
| 790 | sx = (x - es->map.x) * es->width / es->map.width; |
| 791 | sy = (y - es->map.y) * es->height / es->map.height; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 792 | wl_surface_post_event(es->wl_surface, &device->base, |
Kristian Høgsberg | 5a75c90 | 2008-12-10 13:16:50 -0500 | [diff] [blame] | 793 | WL_INPUT_MOTION, x, y, sx, sy); |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 794 | } |
| 795 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 796 | device->x = x; |
| 797 | device->y = y; |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 798 | device->pointer_surface->map.x = x - hotspot_x; |
| 799 | device->pointer_surface->map.y = y - hotspot_y; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 800 | |
| 801 | schedule_repaint(device->ec); |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 802 | } |
| 803 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 804 | void |
| 805 | notify_button(struct egl_input_device *device, |
| 806 | int32_t button, int32_t state) |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 807 | { |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 808 | struct egl_surface *es; |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 809 | int32_t sx, sy; |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 810 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 811 | es = pick_surface(device); |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 812 | if (es) { |
| 813 | wl_list_remove(&es->link); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 814 | wl_list_insert(device->ec->surface_list.prev, &es->link); |
Kristian Høgsberg | 5a75c90 | 2008-12-10 13:16:50 -0500 | [diff] [blame] | 815 | |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 816 | if (state) { |
Kristian Høgsberg | a7700c8 | 2008-12-12 13:48:30 -0500 | [diff] [blame^] | 817 | /* FIXME: We need callbacks when the surfaces |
| 818 | * we reference here go away. */ |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 819 | device->grab++; |
| 820 | device->grab_surface = es; |
Kristian Høgsberg | a7700c8 | 2008-12-12 13:48:30 -0500 | [diff] [blame^] | 821 | device->focus_surface = es; |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 822 | } else { |
| 823 | device->grab--; |
| 824 | } |
| 825 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 826 | sx = (device->x - es->map.x) * es->width / es->map.width; |
| 827 | sy = (device->y - es->map.y) * es->height / es->map.height; |
| 828 | |
Kristian Høgsberg | 5a75c90 | 2008-12-10 13:16:50 -0500 | [diff] [blame] | 829 | /* FIXME: Swallow click on raise? */ |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 830 | wl_surface_post_event(es->wl_surface, &device->base, |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 831 | WL_INPUT_BUTTON, button, state, |
| 832 | device->x, device->y, sx, sy); |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 833 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 834 | schedule_repaint(device->ec); |
| 835 | } |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 836 | } |
| 837 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 838 | void |
| 839 | notify_key(struct egl_input_device *device, |
| 840 | uint32_t key, uint32_t state) |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 841 | { |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 842 | struct egl_compositor *ec = device->ec; |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 843 | |
Kristian Høgsberg | 9af92b3 | 2008-11-24 01:12:46 -0500 | [diff] [blame] | 844 | if (key == KEY_ESC && state == 1) { |
| 845 | if (ec->overlay_target == ec->height) |
| 846 | ec->overlay_target -= 200; |
| 847 | else |
| 848 | ec->overlay_target += 200; |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 849 | schedule_repaint(ec); |
Kristian Høgsberg | 7fdff04 | 2008-12-10 13:25:00 -0500 | [diff] [blame] | 850 | } else if (!wl_list_empty(&ec->surface_list)) { |
Kristian Høgsberg | a7700c8 | 2008-12-12 13:48:30 -0500 | [diff] [blame^] | 851 | if (device->focus_surface != NULL) |
| 852 | wl_surface_post_event(device->focus_surface->wl_surface, |
| 853 | &device->base, |
| 854 | WL_INPUT_KEY, key, state); |
Kristian Høgsberg | 9af92b3 | 2008-11-24 01:12:46 -0500 | [diff] [blame] | 855 | } |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 856 | } |
| 857 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 858 | static const struct wl_compositor_interface interface = { |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 859 | notify_surface_create, |
| 860 | notify_surface_destroy, |
| 861 | notify_surface_attach, |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 862 | notify_surface_map, |
| 863 | notify_surface_copy, |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 864 | notify_surface_damage, |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 865 | notify_commit, |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 866 | }; |
| 867 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 868 | struct evdev_input_device * |
| 869 | evdev_input_device_create(struct egl_input_device *device, |
| 870 | struct wl_display *display, const char *path); |
| 871 | |
| 872 | void |
| 873 | egl_device_get_position(struct egl_input_device *device, int32_t *x, int32_t *y); |
| 874 | |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 875 | static void |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 876 | create_input_device(struct egl_compositor *ec, const char *glob) |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 877 | { |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 878 | struct egl_input_device *device; |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 879 | struct dirent *de; |
| 880 | char path[PATH_MAX]; |
| 881 | const char *by_path_dir = "/dev/input/by-path"; |
| 882 | DIR *dir; |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 883 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 884 | device = malloc(sizeof *device); |
| 885 | if (device == NULL) |
| 886 | return; |
| 887 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 888 | memset(device, 0, sizeof *device); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 889 | device->base.interface = wl_input_device_get_interface(); |
| 890 | wl_display_add_object(ec->wl_display, &device->base); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 891 | device->x = 100; |
| 892 | device->y = 100; |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 893 | device->pointer_surface = pointer_create(device->x, device->y, 64, 64); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 894 | device->ec = ec; |
| 895 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 896 | dir = opendir(by_path_dir); |
| 897 | if (dir == NULL) { |
| 898 | fprintf(stderr, "couldn't read dir %s\n", by_path_dir); |
| 899 | return; |
| 900 | } |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 901 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 902 | while (de = readdir(dir), de != NULL) { |
| 903 | if (fnmatch(glob, de->d_name, 0)) |
| 904 | continue; |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 905 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 906 | snprintf(path, sizeof path, "%s/%s", by_path_dir, de->d_name); |
| 907 | evdev_input_device_create(device, ec->wl_display, path); |
| 908 | } |
| 909 | closedir(dir); |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 910 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 911 | wl_list_insert(ec->input_device_list.prev, &device->link); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | void |
| 915 | egl_device_get_position(struct egl_input_device *device, int32_t *x, int32_t *y) |
| 916 | { |
| 917 | *x = device->x; |
| 918 | *y = device->y; |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 919 | } |
| 920 | |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 921 | static uint32_t |
| 922 | create_frontbuffer(int fd, int *width, int *height, int *stride) |
| 923 | { |
| 924 | drmModeConnector *connector; |
| 925 | drmModeRes *resources; |
| 926 | drmModeEncoder *encoder; |
| 927 | struct drm_mode_modeinfo *mode; |
| 928 | struct drm_i915_gem_create create; |
| 929 | struct drm_i915_gem_pin pin; |
| 930 | struct drm_gem_flink flink; |
| 931 | unsigned int fb_id; |
| 932 | int i, ret; |
| 933 | |
| 934 | resources = drmModeGetResources(fd); |
| 935 | if (!resources) { |
| 936 | fprintf(stderr, "drmModeGetResources failed\n"); |
| 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | for (i = 0; i < resources->count_connectors; i++) { |
| 941 | connector = drmModeGetConnector(fd, resources->connectors[i]); |
| 942 | if (connector == NULL) |
| 943 | continue; |
| 944 | |
| 945 | if (connector->connection == DRM_MODE_CONNECTED && |
| 946 | connector->count_modes > 0) |
| 947 | break; |
| 948 | |
| 949 | drmModeFreeConnector(connector); |
| 950 | } |
| 951 | |
| 952 | if (i == resources->count_connectors) { |
| 953 | fprintf(stderr, "No currently active connector found.\n"); |
| 954 | return -1; |
| 955 | } |
| 956 | |
| 957 | mode = &connector->modes[0]; |
| 958 | |
| 959 | for (i = 0; i < resources->count_encoders; i++) { |
| 960 | encoder = drmModeGetEncoder(fd, resources->encoders[i]); |
| 961 | |
| 962 | if (encoder == NULL) |
| 963 | continue; |
| 964 | |
| 965 | if (encoder->encoder_id == connector->encoder_id) |
| 966 | break; |
| 967 | |
| 968 | drmModeFreeEncoder(encoder); |
| 969 | } |
| 970 | |
| 971 | /* Mode size at 32 bpp */ |
| 972 | create.size = mode->hdisplay * mode->vdisplay * 4; |
| 973 | if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) { |
| 974 | fprintf(stderr, "gem create failed: %m\n"); |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | pin.handle = create.handle; |
| 979 | pin.alignment = 4096; |
| 980 | if (ioctl(fd, DRM_IOCTL_I915_GEM_PIN, &pin)) { |
| 981 | fprintf(stderr, "failed to pin buffer: %m\n"); |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay, |
| 986 | 32, 32, mode->hdisplay * 4, create.handle, &fb_id); |
| 987 | if (ret) { |
| 988 | fprintf(stderr, "failed to add fb: %m\n"); |
| 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | ret = drmModeSetCrtc(fd, encoder->crtc_id, fb_id, 0, 0, |
| 993 | &connector->connector_id, 1, mode); |
| 994 | if (ret) { |
| 995 | fprintf(stderr, "failed to set mode: %m\n"); |
| 996 | return 0; |
| 997 | } |
| 998 | |
| 999 | flink.handle = create.handle; |
| 1000 | if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) { |
| 1001 | fprintf(stderr, "gem flink failed: %m\n"); |
| 1002 | return 0; |
| 1003 | } |
| 1004 | |
| 1005 | *width = mode->hdisplay; |
| 1006 | *height = mode->vdisplay; |
| 1007 | *stride = mode->hdisplay * 4; |
| 1008 | |
| 1009 | return flink.name; |
| 1010 | } |
| 1011 | |
Kristian Høgsberg | 443853c | 2008-11-25 12:12:05 -0500 | [diff] [blame] | 1012 | static int |
| 1013 | pick_config(struct egl_compositor *ec) |
| 1014 | { |
| 1015 | EGLConfig configs[100]; |
| 1016 | EGLint value, count; |
| 1017 | int i; |
| 1018 | |
| 1019 | if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) { |
| 1020 | fprintf(stderr, "failed to get configs\n"); |
| 1021 | return -1; |
| 1022 | } |
| 1023 | |
| 1024 | ec->config = EGL_NO_CONFIG; |
| 1025 | for (i = 0; i < count; i++) { |
| 1026 | eglGetConfigAttrib(ec->display, |
| 1027 | configs[i], |
| 1028 | EGL_DEPTH_SIZE, |
| 1029 | &value); |
| 1030 | if (value > 0) { |
| 1031 | fprintf(stderr, "config %d has depth size %d\n", i, value); |
| 1032 | continue; |
| 1033 | } |
| 1034 | |
| 1035 | eglGetConfigAttrib(ec->display, |
| 1036 | configs[i], |
| 1037 | EGL_STENCIL_SIZE, |
| 1038 | &value); |
| 1039 | if (value > 0) { |
| 1040 | fprintf(stderr, "config %d has stencil size %d\n", i, value); |
| 1041 | continue; |
| 1042 | } |
| 1043 | |
| 1044 | eglGetConfigAttrib(ec->display, |
| 1045 | configs[i], |
| 1046 | EGL_CONFIG_CAVEAT, |
| 1047 | &value); |
| 1048 | if (value != EGL_NONE) { |
| 1049 | fprintf(stderr, "config %d has caveat %d\n", i, value); |
| 1050 | continue; |
| 1051 | } |
| 1052 | |
| 1053 | ec->config = configs[i]; |
| 1054 | break; |
| 1055 | } |
| 1056 | |
| 1057 | if (ec->config == EGL_NO_CONFIG) { |
| 1058 | fprintf(stderr, "found no config without depth or stencil buffers\n"); |
| 1059 | return -1; |
| 1060 | } |
| 1061 | |
| 1062 | return 0; |
| 1063 | } |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1064 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1065 | static const char gem_device[] = "/dev/dri/card0"; |
| 1066 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 1067 | static const char *macbook_air_default_input_device[] = { |
| 1068 | "pci-0000:00:1d.0-usb-0:2:1*event*" |
| 1069 | }; |
Kristian Høgsberg | d653126 | 2008-12-12 11:06:18 -0500 | [diff] [blame] | 1070 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 1071 | static const char *option_background = "background.jpg"; |
| 1072 | static const char **option_input_devices = macbook_air_default_input_device; |
| 1073 | |
| 1074 | static const GOptionEntry option_entries[] = { |
| 1075 | { "background", 'b', 0, G_OPTION_ARG_STRING, |
| 1076 | &option_background, "Background image" }, |
| 1077 | { "input-device", 'i', 0, G_OPTION_ARG_STRING_ARRAY, |
| 1078 | &option_input_devices, "Input device glob" }, |
Kristian Høgsberg | d653126 | 2008-12-12 11:06:18 -0500 | [diff] [blame] | 1079 | { NULL } |
| 1080 | }; |
| 1081 | |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1082 | static struct egl_compositor * |
| 1083 | egl_compositor_create(struct wl_display *display) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1084 | { |
Kristian Høgsberg | 443853c | 2008-11-25 12:12:05 -0500 | [diff] [blame] | 1085 | EGLint major, minor; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1086 | struct egl_compositor *ec; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 1087 | struct screenshooter *shooter; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1088 | uint32_t fb_name; |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 1089 | int i, stride; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 1090 | struct wl_event_loop *loop; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1091 | const static EGLint attribs[] = |
| 1092 | { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE }; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1093 | |
| 1094 | ec = malloc(sizeof *ec); |
| 1095 | if (ec == NULL) |
| 1096 | return NULL; |
| 1097 | |
| 1098 | ec->base.interface = &interface; |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 1099 | ec->wl_display = display; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1100 | |
Kristian Høgsberg | c508d93 | 2008-10-13 22:52:42 -0400 | [diff] [blame] | 1101 | ec->display = eglCreateDisplayNative(gem_device, "i965"); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1102 | if (ec->display == NULL) { |
| 1103 | fprintf(stderr, "failed to create display\n"); |
| 1104 | return NULL; |
| 1105 | } |
| 1106 | |
| 1107 | if (!eglInitialize(ec->display, &major, &minor)) { |
| 1108 | fprintf(stderr, "failed to initialize display\n"); |
| 1109 | return NULL; |
| 1110 | } |
| 1111 | |
Kristian Høgsberg | 443853c | 2008-11-25 12:12:05 -0500 | [diff] [blame] | 1112 | if (pick_config(ec)) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1113 | return NULL; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1114 | |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1115 | fb_name = create_frontbuffer(eglGetDisplayFD(ec->display), |
| 1116 | &ec->width, &ec->height, &stride); |
| 1117 | ec->surface = eglCreateSurfaceForName(ec->display, ec->config, |
| 1118 | fb_name, ec->width, ec->height, stride, attribs); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1119 | if (ec->surface == NULL) { |
| 1120 | fprintf(stderr, "failed to create surface\n"); |
| 1121 | return NULL; |
| 1122 | } |
| 1123 | |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 1124 | ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1125 | if (ec->context == NULL) { |
| 1126 | fprintf(stderr, "failed to create context\n"); |
| 1127 | return NULL; |
| 1128 | } |
| 1129 | |
| 1130 | if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) { |
| 1131 | fprintf(stderr, "failed to make context current\n"); |
| 1132 | return NULL; |
| 1133 | } |
| 1134 | |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 1135 | glViewport(0, 0, ec->width, ec->height); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1136 | glMatrixMode(GL_PROJECTION); |
| 1137 | glLoadIdentity(); |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 1138 | glOrtho(0, ec->width, ec->height, 0, 0, 1000.0); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1139 | glMatrixMode(GL_MODELVIEW); |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 1140 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 1141 | wl_list_init(&ec->input_device_list); |
| 1142 | for (i = 0; option_input_devices[i]; i++) |
| 1143 | create_input_device(ec, option_input_devices[i]); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1144 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 1145 | wl_list_init(&ec->surface_list); |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 1146 | ec->background = background_create(option_background, |
Kristian Høgsberg | d653126 | 2008-12-12 11:06:18 -0500 | [diff] [blame] | 1147 | ec->width, ec->height); |
Kristian Høgsberg | 9af92b3 | 2008-11-24 01:12:46 -0500 | [diff] [blame] | 1148 | ec->overlay = overlay_create(0, ec->height, ec->width, 200); |
Kristian Høgsberg | 9f88b18 | 2008-12-08 13:52:08 -0500 | [diff] [blame] | 1149 | ec->overlay_y = ec->height; |
Kristian Høgsberg | 9af92b3 | 2008-11-24 01:12:46 -0500 | [diff] [blame] | 1150 | ec->overlay_target = ec->height; |
| 1151 | ec->overlay_previous = ec->height; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 1152 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1153 | ec->gem_fd = open(gem_device, O_RDWR); |
| 1154 | if (ec->gem_fd < 0) { |
| 1155 | fprintf(stderr, "failed to open drm device\n"); |
| 1156 | return NULL; |
| 1157 | } |
| 1158 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 1159 | shooter = screenshooter_create(ec); |
| 1160 | wl_display_add_object(display, &shooter->base); |
| 1161 | wl_display_add_global(display, &shooter->base); |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 1162 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 1163 | loop = wl_display_get_event_loop(ec->wl_display); |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 1164 | ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 1165 | ec->repaint_needed = 0; |
| 1166 | ec->repaint_on_timeout = 0; |
| 1167 | |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 1168 | schedule_repaint(ec); |
| 1169 | |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1170 | return ec; |
| 1171 | } |
| 1172 | |
| 1173 | /* The plan here is to generate a random anonymous socket name and |
| 1174 | * advertise that through a service on the session dbus. |
| 1175 | */ |
| 1176 | static const char socket_name[] = "\0wayland"; |
| 1177 | |
| 1178 | int main(int argc, char *argv[]) |
| 1179 | { |
| 1180 | struct wl_display *display; |
| 1181 | struct egl_compositor *ec; |
Kristian Høgsberg | d653126 | 2008-12-12 11:06:18 -0500 | [diff] [blame] | 1182 | GError *error = NULL; |
| 1183 | GOptionContext *context; |
| 1184 | |
| 1185 | context = g_option_context_new(NULL); |
| 1186 | g_option_context_add_main_entries(context, option_entries, "Wayland"); |
| 1187 | if (!g_option_context_parse(context, &argc, &argv, &error)) { |
| 1188 | fprintf(stderr, "option parsing failed: %s\n", error->message); |
| 1189 | exit(EXIT_FAILURE); |
| 1190 | } |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1191 | |
| 1192 | display = wl_display_create(); |
| 1193 | |
| 1194 | ec = egl_compositor_create(display); |
Kristian Høgsberg | 841883b | 2008-12-05 11:19:56 -0500 | [diff] [blame] | 1195 | if (ec == NULL) { |
| 1196 | fprintf(stderr, "failed to create compositor\n"); |
| 1197 | exit(EXIT_FAILURE); |
| 1198 | } |
| 1199 | |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1200 | wl_display_set_compositor(display, &ec->base); |
| 1201 | |
Kristian Høgsberg | dc0f355 | 2008-12-07 15:22:22 -0500 | [diff] [blame] | 1202 | if (wl_display_add_socket(display, socket_name, sizeof socket_name)) { |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1203 | fprintf(stderr, "failed to add socket: %m\n"); |
| 1204 | exit(EXIT_FAILURE); |
| 1205 | } |
| 1206 | |
| 1207 | wl_display_run(display); |
| 1208 | |
| 1209 | return 0; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1210 | } |