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