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