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