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