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