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