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