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