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