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" |
Kristian Høgsberg | fe831a7 | 2008-12-21 21:50:23 -0500 | [diff] [blame] | 49 | #include "wayland-protocol.h" |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 50 | #include "cairo-util.h" |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 51 | #include "wayland-system-compositor.h" |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 52 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 53 | #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) |
| 54 | |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 55 | struct wl_visual { |
| 56 | struct wl_object base; |
| 57 | }; |
| 58 | |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 59 | struct wlsc_input_device { |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 60 | struct wl_object base; |
| 61 | int32_t x, y; |
| 62 | struct egl_compositor *ec; |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 63 | struct egl_surface *pointer_surface; |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 64 | struct wl_list link; |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 65 | |
| 66 | int grab; |
| 67 | struct egl_surface *grab_surface; |
Kristian Høgsberg | a7700c8 | 2008-12-12 13:48:30 -0500 | [diff] [blame] | 68 | struct egl_surface *focus_surface; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 69 | }; |
| 70 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 71 | struct egl_compositor { |
| 72 | struct wl_compositor base; |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 73 | struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual; |
| 74 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 75 | EGLDisplay display; |
| 76 | EGLSurface surface; |
| 77 | EGLContext context; |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 78 | EGLConfig config; |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 79 | struct wl_display *wl_display; |
Kristian Høgsberg | 2c875bd | 2008-12-19 10:34:02 -0500 | [diff] [blame] | 80 | int width, height, stride; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 81 | struct egl_surface *background; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 82 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 83 | struct wl_list input_device_list; |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 84 | struct wl_list surface_list; |
| 85 | |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 86 | struct wl_event_source *term_signal_source; |
| 87 | |
| 88 | /* tty handling state */ |
| 89 | int tty_fd; |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 90 | uint32_t vt_active : 1; |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 91 | |
| 92 | struct termios terminal_attributes; |
| 93 | struct wl_event_source *tty_input_source; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 94 | struct wl_event_source *enter_vt_source; |
| 95 | struct wl_event_source *leave_vt_source; |
| 96 | |
Kristian Høgsberg | 2c875bd | 2008-12-19 10:34:02 -0500 | [diff] [blame] | 97 | /* Modesetting info. */ |
| 98 | struct drm_mode_modeinfo *mode; |
| 99 | uint32_t fb_id; |
| 100 | uint32_t crtc_id; |
| 101 | uint32_t connector_id; |
| 102 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 103 | /* Repaint state. */ |
| 104 | struct wl_event_source *timer_source; |
| 105 | int repaint_needed; |
| 106 | int repaint_on_timeout; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 107 | struct timespec previous_swap; |
| 108 | uint32_t current_frame; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 109 | }; |
| 110 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 111 | struct egl_surface { |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 112 | struct wl_surface base; |
| 113 | struct egl_compositor *compositor; |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 114 | struct wl_visual *visual; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 115 | GLuint texture; |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 116 | struct wl_map map; |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 117 | EGLSurface surface; |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 118 | int width, height; |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 119 | struct wl_list link; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 120 | }; |
| 121 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 122 | struct screenshooter { |
| 123 | struct wl_object base; |
| 124 | struct egl_compositor *ec; |
| 125 | }; |
| 126 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 127 | struct screenshooter_interface { |
| 128 | void (*shoot)(struct wl_client *client, struct screenshooter *shooter); |
| 129 | }; |
| 130 | |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 131 | static void |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 132 | screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter) |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 133 | { |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 134 | struct egl_compositor *ec = shooter->ec; |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 135 | GLuint stride; |
| 136 | static const char filename[] = "wayland-screenshot.png"; |
Kristian Høgsberg | 0ea4710 | 2008-12-14 15:53:13 -0500 | [diff] [blame] | 137 | GdkPixbuf *pixbuf; |
| 138 | GError *error = NULL; |
| 139 | void *data; |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 140 | |
| 141 | data = eglReadBuffer(ec->display, ec->surface, GL_FRONT_LEFT, &stride); |
Kristian Høgsberg | 0ea4710 | 2008-12-14 15:53:13 -0500 | [diff] [blame] | 142 | pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE, |
| 143 | 8, ec->width, ec->height, stride, |
| 144 | NULL, NULL); |
| 145 | gdk_pixbuf_save(pixbuf, filename, "png", &error, NULL); |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 146 | } |
| 147 | |
Kristian Høgsberg | fb6d68d | 2008-12-21 21:54:51 -0500 | [diff] [blame] | 148 | static const struct wl_message screenshooter_methods[] = { |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 149 | { "shoot", "", NULL } |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | static const struct wl_interface screenshooter_interface = { |
| 153 | "screenshooter", 1, |
| 154 | ARRAY_LENGTH(screenshooter_methods), |
| 155 | screenshooter_methods, |
| 156 | }; |
| 157 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 158 | struct screenshooter_interface screenshooter_implementation = { |
| 159 | screenshooter_shoot |
| 160 | }; |
| 161 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 162 | static struct screenshooter * |
| 163 | screenshooter_create(struct egl_compositor *ec) |
| 164 | { |
| 165 | struct screenshooter *shooter; |
| 166 | |
| 167 | shooter = malloc(sizeof *shooter); |
| 168 | if (shooter == NULL) |
| 169 | return NULL; |
| 170 | |
| 171 | shooter->base.interface = &screenshooter_interface; |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 172 | shooter->base.implementation = (void(**)(void)) &screenshooter_implementation; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 173 | shooter->ec = ec; |
| 174 | |
| 175 | return shooter; |
| 176 | }; |
| 177 | |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 178 | static struct egl_surface * |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 179 | egl_surface_create_from_cairo_surface(struct egl_compositor *ec, |
| 180 | cairo_surface_t *surface, |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 181 | int x, int y, int width, int height) |
| 182 | { |
| 183 | struct egl_surface *es; |
| 184 | int stride; |
| 185 | void *data; |
| 186 | |
| 187 | stride = cairo_image_surface_get_stride(surface); |
| 188 | data = cairo_image_surface_get_data(surface); |
| 189 | |
| 190 | es = malloc(sizeof *es); |
| 191 | if (es == NULL) |
| 192 | return NULL; |
| 193 | |
| 194 | glGenTextures(1, &es->texture); |
| 195 | glBindTexture(GL_TEXTURE_2D, es->texture); |
| 196 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
Kristian Høgsberg | 98fed0f | 2008-12-09 13:35:35 -0500 | [diff] [blame] | 197 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT); |
| 198 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 199 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 200 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, |
| 201 | GL_BGRA, GL_UNSIGNED_BYTE, data); |
| 202 | |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 203 | es->compositor = ec; |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 204 | es->map.x = x; |
| 205 | es->map.y = y; |
| 206 | es->map.width = width; |
| 207 | es->map.height = height; |
| 208 | es->surface = EGL_NO_SURFACE; |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 209 | es->visual = &ec->premultiplied_argb_visual; |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 210 | |
| 211 | return es; |
| 212 | } |
| 213 | |
| 214 | static void |
| 215 | egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec) |
| 216 | { |
| 217 | glDeleteTextures(1, &es->texture); |
| 218 | if (es->surface != EGL_NO_SURFACE) |
| 219 | eglDestroySurface(ec->display, es->surface); |
| 220 | free(es); |
| 221 | } |
| 222 | |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 223 | static void |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 224 | pointer_path(cairo_t *cr, int x, int y) |
| 225 | { |
| 226 | const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10; |
| 227 | const int width = 16, height = 16; |
| 228 | |
| 229 | cairo_move_to(cr, x, y); |
| 230 | cairo_line_to(cr, x + tx, y + ty); |
| 231 | cairo_line_to(cr, x + dx, y + dy); |
| 232 | cairo_line_to(cr, x + width - end, y + height); |
| 233 | cairo_line_to(cr, x + width, y + height - end); |
| 234 | cairo_line_to(cr, x + dy, y + dx); |
| 235 | cairo_line_to(cr, x + ty, y + tx); |
| 236 | cairo_close_path(cr); |
| 237 | } |
| 238 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 239 | static struct egl_surface * |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 240 | 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] | 241 | { |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 242 | struct egl_surface *es; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 243 | const int hotspot_x = 16, hotspot_y = 16; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 244 | cairo_surface_t *surface; |
| 245 | cairo_t *cr; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 246 | |
| 247 | surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 248 | width, height); |
| 249 | |
| 250 | cr = cairo_create(surface); |
| 251 | pointer_path(cr, hotspot_x + 5, hotspot_y + 4); |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 252 | cairo_set_line_width(cr, 2); |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 253 | cairo_set_source_rgb(cr, 0, 0, 0); |
| 254 | cairo_stroke_preserve(cr); |
| 255 | cairo_fill(cr); |
| 256 | blur_surface(surface, width); |
| 257 | |
| 258 | pointer_path(cr, hotspot_x, hotspot_y); |
| 259 | cairo_stroke_preserve(cr); |
| 260 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 261 | cairo_fill(cr); |
| 262 | cairo_destroy(cr); |
| 263 | |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 264 | es = egl_surface_create_from_cairo_surface(ec, |
| 265 | surface, |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 266 | x - hotspot_x, |
| 267 | y - hotspot_y, |
| 268 | width, height); |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 269 | |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 270 | cairo_surface_destroy(surface); |
| 271 | |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 272 | return es; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static struct egl_surface * |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 276 | background_create(struct egl_compositor *ec, |
| 277 | const char *filename, int width, int height) |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 278 | { |
| 279 | struct egl_surface *background; |
| 280 | GdkPixbuf *pixbuf; |
| 281 | GError *error = NULL; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 282 | void *data; |
Ray Strode | 18fd33c | 2008-12-18 21:05:20 -0500 | [diff] [blame] | 283 | GLenum format; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 284 | |
| 285 | background = malloc(sizeof *background); |
| 286 | if (background == NULL) |
| 287 | return NULL; |
| 288 | |
| 289 | g_type_init(); |
| 290 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 291 | pixbuf = gdk_pixbuf_new_from_file_at_scale(filename, |
| 292 | width, height, |
| 293 | FALSE, &error); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 294 | if (error != NULL) { |
| 295 | free(background); |
| 296 | return NULL; |
| 297 | } |
| 298 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 299 | data = gdk_pixbuf_get_pixels(pixbuf); |
| 300 | |
| 301 | glGenTextures(1, &background->texture); |
| 302 | glBindTexture(GL_TEXTURE_2D, background->texture); |
| 303 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
Kristian Høgsberg | 98fed0f | 2008-12-09 13:35:35 -0500 | [diff] [blame] | 304 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT); |
| 305 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 306 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
Ray Strode | 18fd33c | 2008-12-18 21:05:20 -0500 | [diff] [blame] | 307 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 308 | if (gdk_pixbuf_get_has_alpha(pixbuf)) |
Ray Strode | 18fd33c | 2008-12-18 21:05:20 -0500 | [diff] [blame] | 309 | format = GL_RGBA; |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 310 | else |
Ray Strode | 18fd33c | 2008-12-18 21:05:20 -0500 | [diff] [blame] | 311 | format = GL_RGB; |
Ray Strode | 18fd33c | 2008-12-18 21:05:20 -0500 | [diff] [blame] | 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 | fe831a7 | 2008-12-21 21:50:23 -0500 | [diff] [blame] | 699 | device->base.interface = &wl_input_device_interface; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 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 | fe831a7 | 2008-12-21 21:50:23 -0500 | [diff] [blame] | 703 | device->pointer_surface = |
| 704 | pointer_create(ec, 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 void |
| 887 | on_tty_input(int fd, uint32_t mask, void *data) |
| 888 | { |
| 889 | struct egl_compositor *ec = data; |
| 890 | |
| 891 | /* Ignore input to tty. We get keyboard events from evdev |
| 892 | */ |
| 893 | tcflush(ec->tty_fd, TCIFLUSH); |
| 894 | } |
| 895 | |
| 896 | static void on_term_signal(int signal_number, void *data) |
| 897 | { |
| 898 | struct egl_compositor *ec = data; |
| 899 | |
| 900 | if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0) |
| 901 | fprintf(stderr, "could not restore terminal to canonical mode\n"); |
| 902 | |
| 903 | exit(0); |
| 904 | } |
| 905 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 906 | static int setup_tty(struct egl_compositor *ec, struct wl_event_loop *loop) |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 907 | { |
| 908 | struct termios raw_attributes; |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 909 | struct vt_mode mode = { 0 }; |
| 910 | |
| 911 | ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY); |
| 912 | if (ec->tty_fd <= 0) { |
| 913 | fprintf(stderr, "failed to open active tty: %m\n"); |
| 914 | return -1; |
| 915 | } |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 916 | |
| 917 | if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) { |
| 918 | fprintf(stderr, "could not get terminal attributes: %m\n"); |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 919 | return -1; |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 920 | } |
| 921 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 922 | /* Ignore control characters and disable echo */ |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 923 | raw_attributes = ec->terminal_attributes; |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 924 | cfmakeraw(&raw_attributes); |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 925 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 926 | /* Fix up line endings to be normal (cfmakeraw hoses them) */ |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 927 | raw_attributes.c_oflag |= OPOST | OCRNL; |
| 928 | |
| 929 | if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0) |
| 930 | fprintf(stderr, "could not put terminal into raw mode: %m\n"); |
| 931 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 932 | ec->term_signal_source = |
| 933 | wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec); |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 934 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 935 | ec->tty_input_source = |
| 936 | wl_event_loop_add_fd(loop, ec->tty_fd, |
| 937 | WL_EVENT_READABLE, on_tty_input, ec); |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 938 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 939 | ec->vt_active = TRUE; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 940 | mode.mode = VT_PROCESS; |
| 941 | mode.relsig = SIGUSR1; |
| 942 | mode.acqsig = SIGUSR2; |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 943 | if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) { |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 944 | fprintf(stderr, "failed to take control of vt handling\n"); |
| 945 | } |
| 946 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 947 | ec->leave_vt_source = |
| 948 | wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec); |
| 949 | ec->enter_vt_source = |
| 950 | wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec); |
Kristian Høgsberg | fe831a7 | 2008-12-21 21:50:23 -0500 | [diff] [blame] | 951 | |
| 952 | return 0; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 953 | } |
| 954 | |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 955 | static struct egl_compositor * |
| 956 | egl_compositor_create(struct wl_display *display) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 957 | { |
Kristian Høgsberg | 15d0f8b | 2008-12-19 12:13:42 -0500 | [diff] [blame] | 958 | static const EGLint config_attribs[] = { |
| 959 | EGL_DEPTH_SIZE, 0, |
| 960 | EGL_STENCIL_SIZE, 0, |
| 961 | EGL_CONFIG_CAVEAT, EGL_NONE, |
| 962 | EGL_NONE |
| 963 | }; |
| 964 | |
| 965 | const static EGLint attribs[] = { |
| 966 | EGL_RENDER_BUFFER, EGL_BACK_BUFFER, |
| 967 | EGL_NONE |
| 968 | }; |
| 969 | |
Kristian Høgsberg | 443853c | 2008-11-25 12:12:05 -0500 | [diff] [blame] | 970 | EGLint major, minor; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 971 | struct egl_compositor *ec; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 972 | struct screenshooter *shooter; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 973 | uint32_t fb_name; |
Kristian Høgsberg | 2c875bd | 2008-12-19 10:34:02 -0500 | [diff] [blame] | 974 | int i; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 975 | struct wl_event_loop *loop; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 976 | |
| 977 | ec = malloc(sizeof *ec); |
| 978 | if (ec == NULL) |
| 979 | return NULL; |
| 980 | |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 981 | ec->wl_display = display; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 982 | |
Kristian Høgsberg | c508d93 | 2008-10-13 22:52:42 -0400 | [diff] [blame] | 983 | ec->display = eglCreateDisplayNative(gem_device, "i965"); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 984 | if (ec->display == NULL) { |
| 985 | fprintf(stderr, "failed to create display\n"); |
| 986 | return NULL; |
| 987 | } |
| 988 | |
| 989 | if (!eglInitialize(ec->display, &major, &minor)) { |
| 990 | fprintf(stderr, "failed to initialize display\n"); |
| 991 | return NULL; |
| 992 | } |
| 993 | |
Kristian Høgsberg | 15d0f8b | 2008-12-19 12:13:42 -0500 | [diff] [blame] | 994 | if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL)) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 995 | return NULL; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 996 | |
Kristian Høgsberg | 2c875bd | 2008-12-19 10:34:02 -0500 | [diff] [blame] | 997 | fb_name = create_frontbuffer(ec); |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 998 | ec->surface = eglCreateSurfaceForName(ec->display, ec->config, |
Kristian Høgsberg | 2c875bd | 2008-12-19 10:34:02 -0500 | [diff] [blame] | 999 | fb_name, ec->width, ec->height, ec->stride, attribs); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1000 | if (ec->surface == NULL) { |
| 1001 | fprintf(stderr, "failed to create surface\n"); |
| 1002 | return NULL; |
| 1003 | } |
| 1004 | |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 1005 | ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1006 | if (ec->context == NULL) { |
| 1007 | fprintf(stderr, "failed to create context\n"); |
| 1008 | return NULL; |
| 1009 | } |
| 1010 | |
| 1011 | if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) { |
| 1012 | fprintf(stderr, "failed to make context current\n"); |
| 1013 | return NULL; |
| 1014 | } |
| 1015 | |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 1016 | glViewport(0, 0, ec->width, ec->height); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1017 | glMatrixMode(GL_PROJECTION); |
| 1018 | glLoadIdentity(); |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 1019 | glOrtho(0, ec->width, ec->height, 0, 0, 1000.0); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1020 | glMatrixMode(GL_MODELVIEW); |
Kristian Høgsberg | 5b7f832 | 2008-12-18 12:08:19 -0500 | [diff] [blame] | 1021 | glClearColor(0, 0, 0.2, 1); |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 1022 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 1023 | wl_display_set_compositor(display, &ec->base, &compositor_interface); |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 1024 | add_visuals(ec); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 1025 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 1026 | wl_list_init(&ec->input_device_list); |
| 1027 | for (i = 0; option_input_devices[i]; i++) |
| 1028 | create_input_device(ec, option_input_devices[i]); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1029 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 1030 | wl_list_init(&ec->surface_list); |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 1031 | ec->background = background_create(ec, option_background, |
Kristian Høgsberg | d653126 | 2008-12-12 11:06:18 -0500 | [diff] [blame] | 1032 | ec->width, ec->height); |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 1033 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 1034 | shooter = screenshooter_create(ec); |
| 1035 | wl_display_add_object(display, &shooter->base); |
| 1036 | wl_display_add_global(display, &shooter->base); |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 1037 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 1038 | loop = wl_display_get_event_loop(ec->wl_display); |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 1039 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1040 | setup_tty(ec, loop); |
| 1041 | |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 1042 | ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 1043 | ec->repaint_needed = 0; |
| 1044 | ec->repaint_on_timeout = 0; |
| 1045 | |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 1046 | schedule_repaint(ec); |
| 1047 | |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1048 | return ec; |
| 1049 | } |
| 1050 | |
| 1051 | /* The plan here is to generate a random anonymous socket name and |
| 1052 | * advertise that through a service on the session dbus. |
| 1053 | */ |
| 1054 | static const char socket_name[] = "\0wayland"; |
| 1055 | |
| 1056 | int main(int argc, char *argv[]) |
| 1057 | { |
| 1058 | struct wl_display *display; |
| 1059 | struct egl_compositor *ec; |
Kristian Høgsberg | d653126 | 2008-12-12 11:06:18 -0500 | [diff] [blame] | 1060 | GError *error = NULL; |
| 1061 | GOptionContext *context; |
| 1062 | |
| 1063 | context = g_option_context_new(NULL); |
| 1064 | g_option_context_add_main_entries(context, option_entries, "Wayland"); |
| 1065 | if (!g_option_context_parse(context, &argc, &argv, &error)) { |
| 1066 | fprintf(stderr, "option parsing failed: %s\n", error->message); |
| 1067 | exit(EXIT_FAILURE); |
| 1068 | } |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1069 | |
| 1070 | display = wl_display_create(); |
| 1071 | |
| 1072 | ec = egl_compositor_create(display); |
Kristian Høgsberg | 841883b | 2008-12-05 11:19:56 -0500 | [diff] [blame] | 1073 | if (ec == NULL) { |
| 1074 | fprintf(stderr, "failed to create compositor\n"); |
| 1075 | exit(EXIT_FAILURE); |
| 1076 | } |
| 1077 | |
Kristian Høgsberg | dc0f355 | 2008-12-07 15:22:22 -0500 | [diff] [blame] | 1078 | if (wl_display_add_socket(display, socket_name, sizeof socket_name)) { |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1079 | fprintf(stderr, "failed to add socket: %m\n"); |
| 1080 | exit(EXIT_FAILURE); |
| 1081 | } |
| 1082 | |
| 1083 | wl_display_run(display); |
| 1084 | |
| 1085 | return 0; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1086 | } |