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