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