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