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