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 | 06bc264 | 2010-12-01 09:50:16 -0500 | [diff] [blame] | 19 | #define _GNU_SOURCE |
| 20 | |
Kristian Høgsberg | a941022 | 2011-01-14 17:22:35 -0500 | [diff] [blame] | 21 | #include "config.h" |
| 22 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdint.h> |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 27 | #include <limits.h> |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 28 | #include <stdarg.h> |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 29 | #include <sys/ioctl.h> |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 30 | #include <fcntl.h> |
| 31 | #include <unistd.h> |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 32 | #include <gdk-pixbuf/gdk-pixbuf.h> |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 33 | #include <math.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 34 | #include <linux/input.h> |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 35 | #include <dlfcn.h> |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 36 | |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 37 | #include "wayland-server.h" |
Kristian Høgsberg | 8286302 | 2010-06-04 21:52:02 -0400 | [diff] [blame] | 38 | #include "compositor.h" |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 39 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 40 | /* The plan here is to generate a random anonymous socket name and |
| 41 | * advertise that through a service on the session dbus. |
| 42 | */ |
Kristian Høgsberg | 2bb3ebe | 2010-12-01 15:36:20 -0500 | [diff] [blame] | 43 | static const char *option_socket_name = NULL; |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 44 | static const char *option_background = "background.jpg"; |
| 45 | static const char *option_geometry = "1024x640"; |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 46 | static const char *option_shell = NULL; |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 47 | static int option_idle_time = 5; |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 48 | static int option_connector = 0; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 49 | |
| 50 | static const GOptionEntry option_entries[] = { |
| 51 | { "background", 'b', 0, G_OPTION_ARG_STRING, |
| 52 | &option_background, "Background image" }, |
Kristian Høgsberg | f5878fa | 2009-09-18 17:02:41 -0400 | [diff] [blame] | 53 | { "connector", 'c', 0, G_OPTION_ARG_INT, |
| 54 | &option_connector, "KMS connector" }, |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 55 | { "geometry", 'g', 0, G_OPTION_ARG_STRING, |
| 56 | &option_geometry, "Geometry" }, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 57 | { "socket", 's', 0, G_OPTION_ARG_STRING, |
| 58 | &option_socket_name, "Socket Name" }, |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 59 | { "idle-time", 'i', 0, G_OPTION_ARG_INT, |
| 60 | &option_idle_time, "Screensaver idle time" }, |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 61 | { "shell", 'i', 0, G_OPTION_ARG_STRING, |
| 62 | &option_shell, "Shell module" }, |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 63 | { NULL } |
| 64 | }; |
| 65 | |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 66 | static void |
| 67 | wlsc_matrix_init(struct wlsc_matrix *matrix) |
| 68 | { |
| 69 | static const struct wlsc_matrix identity = { |
| 70 | { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } |
| 71 | }; |
| 72 | |
| 73 | memcpy(matrix, &identity, sizeof identity); |
| 74 | } |
| 75 | |
| 76 | static void |
| 77 | wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n) |
| 78 | { |
| 79 | struct wlsc_matrix tmp; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 80 | const GLfloat *row, *column; |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 81 | div_t d; |
| 82 | int i, j; |
| 83 | |
| 84 | for (i = 0; i < 16; i++) { |
| 85 | tmp.d[i] = 0; |
| 86 | d = div(i, 4); |
| 87 | row = m->d + d.quot * 4; |
| 88 | column = n->d + d.rem; |
| 89 | for (j = 0; j < 4; j++) |
| 90 | tmp.d[i] += row[j] * column[j * 4]; |
| 91 | } |
| 92 | memcpy(m, &tmp, sizeof tmp); |
| 93 | } |
| 94 | |
| 95 | static void |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 96 | wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z) |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 97 | { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 98 | struct wlsc_matrix translate = { |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 99 | { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 } |
| 100 | }; |
| 101 | |
| 102 | wlsc_matrix_multiply(matrix, &translate); |
| 103 | } |
| 104 | |
| 105 | static void |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 106 | wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z) |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 107 | { |
| 108 | struct wlsc_matrix scale = { |
| 109 | { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 } |
| 110 | }; |
| 111 | |
| 112 | wlsc_matrix_multiply(matrix, &scale); |
| 113 | } |
| 114 | |
| 115 | static void |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 116 | wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v) |
| 117 | { |
| 118 | int i, j; |
Kristian Høgsberg | 0b8646b | 2010-06-08 15:29:14 -0400 | [diff] [blame] | 119 | struct wlsc_vector t; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 120 | |
| 121 | for (i = 0; i < 4; i++) { |
Kristian Høgsberg | 0b8646b | 2010-06-08 15:29:14 -0400 | [diff] [blame] | 122 | t.f[i] = 0; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 123 | for (j = 0; j < 4; j++) |
Kristian Høgsberg | 0b8646b | 2010-06-08 15:29:14 -0400 | [diff] [blame] | 124 | t.f[i] += v->f[j] * matrix->d[i + j * 4]; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 125 | } |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 126 | |
Kristian Høgsberg | 0b8646b | 2010-06-08 15:29:14 -0400 | [diff] [blame] | 127 | *v = t; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 130 | WL_EXPORT void |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 131 | wlsc_tweener_init(struct wlsc_tweener *tweener, |
| 132 | double k, double current, double target) |
| 133 | { |
| 134 | tweener->k = k; |
| 135 | tweener->current = current; |
| 136 | tweener->previous = current; |
| 137 | tweener->target = target; |
| 138 | } |
| 139 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 140 | WL_EXPORT void |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 141 | wlsc_tweener_update(struct wlsc_tweener *tweener, uint32_t msec) |
| 142 | { |
| 143 | double force, current, step; |
| 144 | |
| 145 | step = (msec - tweener->timestamp) / 100.0; |
| 146 | tweener->timestamp = msec; |
| 147 | |
| 148 | current = tweener->current; |
| 149 | force = tweener->k * (tweener->target - current) / 10.0 + |
| 150 | (tweener->previous - current); |
| 151 | |
| 152 | tweener->current = |
| 153 | current + (current - tweener->previous) + force * step * step; |
| 154 | tweener->previous = current; |
| 155 | |
| 156 | if (tweener->current >= 1.0) { |
| 157 | tweener->current = 1.0; |
| 158 | tweener->previous = 1.0; |
| 159 | } |
| 160 | |
| 161 | if (tweener->current <= 0.0) { |
| 162 | tweener->current = 0.0; |
| 163 | tweener->previous = 0.0; |
| 164 | } |
| 165 | } |
| 166 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 167 | WL_EXPORT int |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 168 | wlsc_tweener_done(struct wlsc_tweener *tweener) |
| 169 | { |
| 170 | return fabs(tweener->previous - tweener->target) < 0.0002 && |
| 171 | fabs(tweener->current - tweener->target) < 0.0002; |
| 172 | } |
| 173 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 174 | WL_EXPORT struct wlsc_surface * |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 175 | wlsc_surface_create(struct wlsc_compositor *compositor, |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 176 | int32_t x, int32_t y, int32_t width, int32_t height) |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 177 | { |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 178 | struct wlsc_surface *surface; |
| 179 | |
| 180 | surface = malloc(sizeof *surface); |
| 181 | if (surface == NULL) |
| 182 | return NULL; |
| 183 | |
Kristian Høgsberg | c551bd2 | 2010-12-06 16:43:16 -0500 | [diff] [blame] | 184 | wl_list_init(&surface->surface.destroy_listener_list); |
Kristian Høgsberg | f6b1471 | 2011-01-06 15:32:14 -0500 | [diff] [blame] | 185 | wl_list_init(&surface->link); |
Benjamin Franzke | bab41fb | 2011-03-07 18:04:59 +0100 | [diff] [blame] | 186 | wl_list_init(&surface->buffer_link); |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 187 | surface->map_type = WLSC_SURFACE_MAP_UNMAPPED; |
Kristian Høgsberg | c551bd2 | 2010-12-06 16:43:16 -0500 | [diff] [blame] | 188 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 189 | glGenTextures(1, &surface->texture); |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 190 | glBindTexture(GL_TEXTURE_2D, surface->texture); |
| 191 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 192 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 193 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 194 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 195 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 196 | surface->compositor = compositor; |
Kristian Høgsberg | c5d6be9 | 2011-01-14 16:22:37 -0500 | [diff] [blame] | 197 | surface->visual = NULL; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 198 | surface->image = EGL_NO_IMAGE_KHR; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 199 | surface->saved_texture = 0; |
Benjamin Franzke | bab41fb | 2011-03-07 18:04:59 +0100 | [diff] [blame] | 200 | surface->buffer = NULL; |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 201 | surface->x = x; |
| 202 | surface->y = y; |
| 203 | surface->width = width; |
| 204 | surface->height = height; |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 205 | wlsc_matrix_init(&surface->matrix); |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 206 | wlsc_matrix_scale(&surface->matrix, width, height, 1); |
| 207 | wlsc_matrix_translate(&surface->matrix, x, y, 0); |
| 208 | |
| 209 | wlsc_matrix_init(&surface->matrix_inv); |
| 210 | wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0); |
| 211 | wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 212 | |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 213 | return surface; |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 214 | } |
| 215 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 216 | WL_EXPORT void |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 217 | wlsc_surface_damage_rectangle(struct wlsc_surface *surface, |
| 218 | int32_t x, int32_t y, |
| 219 | int32_t width, int32_t height) |
| 220 | { |
| 221 | struct wlsc_compositor *compositor = surface->compositor; |
| 222 | |
| 223 | pixman_region32_union_rect(&compositor->damage_region, |
| 224 | &compositor->damage_region, |
| 225 | surface->x + x, surface->y + y, |
| 226 | width, height); |
| 227 | wlsc_compositor_schedule_repaint(compositor); |
| 228 | } |
| 229 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 230 | WL_EXPORT void |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 231 | wlsc_surface_damage(struct wlsc_surface *surface) |
| 232 | { |
| 233 | wlsc_surface_damage_rectangle(surface, 0, 0, |
| 234 | surface->width, surface->height); |
| 235 | } |
| 236 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 237 | WL_EXPORT uint32_t |
| 238 | wlsc_compositor_get_time(void) |
Kristian Høgsberg | 7132a9a | 2010-12-06 21:41:10 -0500 | [diff] [blame] | 239 | { |
| 240 | struct timeval tv; |
| 241 | |
| 242 | gettimeofday(&tv, NULL); |
| 243 | |
| 244 | return tv.tv_sec * 1000 + tv.tv_usec / 1000; |
| 245 | } |
| 246 | |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 247 | static void |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 248 | destroy_surface(struct wl_resource *resource, struct wl_client *client) |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 249 | { |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 250 | struct wlsc_surface *surface = |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 251 | container_of(resource, struct wlsc_surface, surface.resource); |
Kristian Høgsberg | c551bd2 | 2010-12-06 16:43:16 -0500 | [diff] [blame] | 252 | struct wl_listener *l, *next; |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame^] | 253 | struct wlsc_compositor *compositor = surface->compositor; |
Kristian Høgsberg | 4685fa3 | 2010-12-06 21:38:50 -0500 | [diff] [blame] | 254 | uint32_t time; |
Kristian Høgsberg | 4fa4873 | 2009-03-10 23:17:00 -0400 | [diff] [blame] | 255 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 256 | wlsc_surface_damage(surface); |
| 257 | |
Kristian Høgsberg | 3f8f39c | 2009-09-18 17:05:13 -0400 | [diff] [blame] | 258 | wl_list_remove(&surface->link); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 259 | if (surface->saved_texture == 0) |
| 260 | glDeleteTextures(1, &surface->texture); |
| 261 | else |
| 262 | glDeleteTextures(1, &surface->saved_texture); |
| 263 | |
Kristian Høgsberg | 3f8f39c | 2009-09-18 17:05:13 -0400 | [diff] [blame] | 264 | |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 265 | if (surface->image != EGL_NO_IMAGE_KHR) |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame^] | 266 | compositor->destroy_image(compositor->display, |
| 267 | surface->image); |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 268 | |
Benjamin Franzke | bab41fb | 2011-03-07 18:04:59 +0100 | [diff] [blame] | 269 | wl_list_remove(&surface->buffer_link); |
| 270 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 271 | time = wlsc_compositor_get_time(); |
Kristian Høgsberg | c551bd2 | 2010-12-06 16:43:16 -0500 | [diff] [blame] | 272 | wl_list_for_each_safe(l, next, |
| 273 | &surface->surface.destroy_listener_list, link) |
Kristian Høgsberg | 4685fa3 | 2010-12-06 21:38:50 -0500 | [diff] [blame] | 274 | l->func(l, &surface->surface, time); |
Kristian Høgsberg | 4fa4873 | 2009-03-10 23:17:00 -0400 | [diff] [blame] | 275 | |
Kristian Høgsberg | 4fa4873 | 2009-03-10 23:17:00 -0400 | [diff] [blame] | 276 | free(surface); |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 277 | } |
| 278 | |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 279 | uint32_t * |
| 280 | wlsc_load_image(const char *filename, int width, int height) |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 281 | { |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 282 | GdkPixbuf *pixbuf; |
| 283 | GError *error = NULL; |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 284 | int stride, i, n_channels; |
| 285 | unsigned char *pixels, *end, *argb_pixels, *s, *d; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 286 | |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 287 | pixbuf = gdk_pixbuf_new_from_file_at_scale(filename, |
| 288 | width, height, |
| 289 | FALSE, &error); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 290 | if (error != NULL) { |
| 291 | fprintf(stderr, "failed to load image: %s\n", error->message); |
| 292 | g_error_free(error); |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 293 | return NULL; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 294 | } |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 295 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 296 | stride = gdk_pixbuf_get_rowstride(pixbuf); |
| 297 | pixels = gdk_pixbuf_get_pixels(pixbuf); |
| 298 | n_channels = gdk_pixbuf_get_n_channels(pixbuf); |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 299 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 300 | argb_pixels = malloc (height * width * 4); |
| 301 | if (argb_pixels == NULL) { |
Darxus@chaosreigns.com | c4df99c | 2011-01-25 15:00:56 -0500 | [diff] [blame] | 302 | g_object_unref(pixbuf); |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 303 | return NULL; |
| 304 | } |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 305 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 306 | if (n_channels == 4) { |
| 307 | for (i = 0; i < height; i++) { |
| 308 | s = pixels + i * stride; |
| 309 | end = s + width * 4; |
| 310 | d = argb_pixels + i * width * 4; |
| 311 | while (s < end) { |
| 312 | unsigned int t; |
| 313 | |
| 314 | #define MULT(_d,c,a,t) \ |
| 315 | do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0) |
| 316 | |
| 317 | MULT(d[0], s[2], s[3], t); |
| 318 | MULT(d[1], s[1], s[3], t); |
| 319 | MULT(d[2], s[0], s[3], t); |
| 320 | d[3] = s[3]; |
| 321 | s += 4; |
| 322 | d += 4; |
| 323 | } |
| 324 | } |
| 325 | } else if (n_channels == 3) { |
| 326 | for (i = 0; i < height; i++) { |
| 327 | s = pixels + i * stride; |
| 328 | end = s + width * 3; |
| 329 | d = argb_pixels + i * width * 4; |
| 330 | while (s < end) { |
| 331 | d[0] = s[2]; |
| 332 | d[1] = s[1]; |
| 333 | d[2] = s[0]; |
| 334 | d[3] = 0xff; |
| 335 | s += 3; |
| 336 | d += 4; |
| 337 | } |
| 338 | } |
| 339 | } |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 340 | |
Darxus@chaosreigns.com | c4df99c | 2011-01-25 15:00:56 -0500 | [diff] [blame] | 341 | g_object_unref(pixbuf); |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 342 | |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 343 | return (uint32_t *) argb_pixels; |
| 344 | } |
| 345 | |
Benjamin Franzke | faa0a9d | 2011-02-21 16:24:53 +0100 | [diff] [blame] | 346 | static void |
| 347 | wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface) |
| 348 | { |
| 349 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 350 | struct wlsc_compositor *ec = es->compositor; |
Benjamin Franzke | 315b3dc | 2011-03-08 11:32:57 +0100 | [diff] [blame] | 351 | struct wl_list *surfaces_attached_to; |
Benjamin Franzke | faa0a9d | 2011-02-21 16:24:53 +0100 | [diff] [blame] | 352 | |
Benjamin Franzke | 315b3dc | 2011-03-08 11:32:57 +0100 | [diff] [blame] | 353 | if (es->saved_texture != 0) |
| 354 | es->texture = es->saved_texture; |
| 355 | |
| 356 | glBindTexture(GL_TEXTURE_2D, es->texture); |
| 357 | |
| 358 | if (wl_buffer_is_shm(buffer)) { |
| 359 | /* Unbind any EGLImage texture that may be bound, so we don't |
| 360 | * overwrite it.*/ |
| 361 | glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, |
| 362 | 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL); |
Benjamin Franzke | fab5ec1 | 2011-04-25 19:44:47 +0200 | [diff] [blame] | 363 | es->pitch = wl_shm_buffer_get_stride(buffer) / 4; |
Benjamin Franzke | 315b3dc | 2011-03-08 11:32:57 +0100 | [diff] [blame] | 364 | glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, |
Benjamin Franzke | fab5ec1 | 2011-04-25 19:44:47 +0200 | [diff] [blame] | 365 | es->pitch, buffer->height, 0, |
Benjamin Franzke | 315b3dc | 2011-03-08 11:32:57 +0100 | [diff] [blame] | 366 | GL_BGRA_EXT, GL_UNSIGNED_BYTE, |
| 367 | wl_shm_buffer_get_data(buffer)); |
| 368 | es->visual = buffer->visual; |
| 369 | |
| 370 | surfaces_attached_to = buffer->user_data; |
| 371 | |
| 372 | if (es->buffer) |
| 373 | wl_list_remove(&es->buffer_link); |
| 374 | wl_list_insert(surfaces_attached_to, &es->buffer_link); |
Benjamin Franzke | faa0a9d | 2011-02-21 16:24:53 +0100 | [diff] [blame] | 375 | } else { |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame^] | 376 | es->image = ec->create_image(ec->display, NULL, |
| 377 | EGL_WAYLAND_BUFFER_WL, |
| 378 | buffer, NULL); |
| 379 | |
| 380 | ec->image_target_texture_2d(GL_TEXTURE_2D, es->image); |
Benjamin Franzke | faa0a9d | 2011-02-21 16:24:53 +0100 | [diff] [blame] | 381 | es->visual = buffer->visual; |
Benjamin Franzke | fab5ec1 | 2011-04-25 19:44:47 +0200 | [diff] [blame] | 382 | es->pitch = es->width; |
Benjamin Franzke | faa0a9d | 2011-02-21 16:24:53 +0100 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 386 | static void |
| 387 | wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface) |
| 388 | { |
| 389 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame^] | 390 | struct wlsc_compositor *ec = es->compositor; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 391 | |
Benjamin Franzke | fab5ec1 | 2011-04-25 19:44:47 +0200 | [diff] [blame] | 392 | es->pitch = es->width; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 393 | es->image = sprite->image; |
| 394 | if (sprite->image != EGL_NO_IMAGE_KHR) { |
| 395 | glBindTexture(GL_TEXTURE_2D, es->texture); |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame^] | 396 | ec->image_target_texture_2d(GL_TEXTURE_2D, es->image); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 397 | } else { |
| 398 | if (es->saved_texture == 0) |
| 399 | es->saved_texture = es->texture; |
| 400 | es->texture = sprite->texture; |
| 401 | } |
| 402 | |
| 403 | es->visual = sprite->visual; |
| 404 | } |
| 405 | |
| 406 | enum sprite_usage { |
| 407 | SPRITE_USE_CURSOR = (1 << 0), |
| 408 | }; |
| 409 | |
| 410 | static struct wlsc_sprite * |
| 411 | create_sprite_from_png(struct wlsc_compositor *ec, |
| 412 | const char *filename, int width, int height, |
| 413 | uint32_t usage) |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 414 | { |
| 415 | uint32_t *pixels; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 416 | struct wlsc_sprite *sprite; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 417 | |
| 418 | pixels = wlsc_load_image(filename, width, height); |
Tim Wiederhake | ac5c5e7 | 2011-01-27 01:32:36 +0100 | [diff] [blame] | 419 | if(pixels == NULL) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 420 | return NULL; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 421 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 422 | sprite = malloc(sizeof *sprite); |
| 423 | if (sprite == NULL) { |
| 424 | free(pixels); |
| 425 | return NULL; |
| 426 | } |
| 427 | |
| 428 | sprite->visual = &ec->compositor.premultiplied_argb_visual; |
| 429 | sprite->width = width; |
| 430 | sprite->height = height; |
| 431 | sprite->image = EGL_NO_IMAGE_KHR; |
| 432 | |
| 433 | if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL) |
| 434 | sprite->image = ec->create_cursor_image(ec, width, height); |
| 435 | |
| 436 | glGenTextures(1, &sprite->texture); |
| 437 | glBindTexture(GL_TEXTURE_2D, sprite->texture); |
| 438 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 439 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 440 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 441 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 442 | |
| 443 | if (sprite->image != EGL_NO_IMAGE_KHR) { |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame^] | 444 | ec->image_target_texture_2d(GL_TEXTURE_2D, sprite->image); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 445 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, |
| 446 | GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels); |
| 447 | } else { |
| 448 | glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0, |
| 449 | GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels); |
| 450 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 451 | |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 452 | free(pixels); |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 453 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 454 | return sprite; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 455 | } |
| 456 | |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 457 | static const struct { |
| 458 | const char *filename; |
| 459 | int hotspot_x, hotspot_y; |
| 460 | } pointer_images[] = { |
Kristian Høgsberg | 4219a40 | 2010-08-16 16:43:03 -0400 | [diff] [blame] | 461 | { DATADIR "/wayland/bottom_left_corner.png", 6, 30 }, |
| 462 | { DATADIR "/wayland/bottom_right_corner.png", 28, 28 }, |
| 463 | { DATADIR "/wayland/bottom_side.png", 16, 20 }, |
| 464 | { DATADIR "/wayland/grabbing.png", 20, 17 }, |
| 465 | { DATADIR "/wayland/left_ptr.png", 10, 5 }, |
| 466 | { DATADIR "/wayland/left_side.png", 10, 20 }, |
| 467 | { DATADIR "/wayland/right_side.png", 30, 19 }, |
| 468 | { DATADIR "/wayland/top_left_corner.png", 8, 8 }, |
| 469 | { DATADIR "/wayland/top_right_corner.png", 26, 8 }, |
| 470 | { DATADIR "/wayland/top_side.png", 18, 8 }, |
| 471 | { DATADIR "/wayland/xterm.png", 15, 15 } |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 472 | }; |
| 473 | |
| 474 | static void |
| 475 | create_pointer_images(struct wlsc_compositor *ec) |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 476 | { |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 477 | int i, count; |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 478 | const int width = 32, height = 32; |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 479 | |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 480 | count = ARRAY_LENGTH(pointer_images); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 481 | ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites); |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 482 | for (i = 0; i < count; i++) { |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 483 | ec->pointer_sprites[i] = |
| 484 | create_sprite_from_png(ec, |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 485 | pointer_images[i].filename, |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 486 | width, height, |
| 487 | SPRITE_USE_CURSOR); |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 488 | } |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 489 | } |
| 490 | |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 491 | static struct wlsc_surface * |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 492 | background_create(struct wlsc_output *output, const char *filename) |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 493 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 494 | struct wlsc_surface *background; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 495 | struct wlsc_sprite *sprite; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 496 | |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 497 | background = wlsc_surface_create(output->compositor, |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 498 | output->x, output->y, |
| 499 | output->width, output->height); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 500 | if (background == NULL) |
| 501 | return NULL; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 502 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 503 | sprite = create_sprite_from_png(output->compositor, filename, |
| 504 | output->width, output->height, 0); |
| 505 | if (sprite == NULL) { |
Benjamin Franzke | d3b023e | 2011-01-15 12:34:49 +0100 | [diff] [blame] | 506 | free(background); |
| 507 | return NULL; |
| 508 | } |
Benjamin Franzke | faa0a9d | 2011-02-21 16:24:53 +0100 | [diff] [blame] | 509 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 510 | wlsc_sprite_attach(sprite, &background->surface); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 511 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 512 | return background; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | static void |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 516 | wlsc_surface_draw(struct wlsc_surface *es, |
| 517 | struct wlsc_output *output, pixman_region32_t *clip) |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 518 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 519 | struct wlsc_compositor *ec = es->compositor; |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 520 | GLfloat *v, inv_width, inv_height; |
| 521 | unsigned int *p; |
| 522 | pixman_region32_t repaint; |
| 523 | pixman_box32_t *rectangles; |
| 524 | int i, n; |
| 525 | |
| 526 | pixman_region32_init_rect(&repaint, |
| 527 | es->x, es->y, es->width, es->height); |
| 528 | pixman_region32_intersect(&repaint, &repaint, clip); |
| 529 | if (!pixman_region32_not_empty(&repaint)) |
| 530 | return; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 531 | |
Kristian Høgsberg | c5c510e | 2010-12-08 15:12:58 -0500 | [diff] [blame] | 532 | if (es->visual == &ec->compositor.argb_visual) { |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 533 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 534 | glEnable(GL_BLEND); |
Kristian Høgsberg | c5c510e | 2010-12-08 15:12:58 -0500 | [diff] [blame] | 535 | } else if (es->visual == &ec->compositor.premultiplied_argb_visual) { |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 536 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 537 | glEnable(GL_BLEND); |
| 538 | } else { |
| 539 | glDisable(GL_BLEND); |
| 540 | } |
| 541 | |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 542 | rectangles = pixman_region32_rectangles(&repaint, &n); |
| 543 | v = wl_array_add(&ec->vertices, n * 16 * sizeof *v); |
| 544 | p = wl_array_add(&ec->indices, n * 6 * sizeof *p); |
Benjamin Franzke | fab5ec1 | 2011-04-25 19:44:47 +0200 | [diff] [blame] | 545 | inv_width = 1.0 / es->pitch; |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 546 | inv_height = 1.0 / es->height; |
| 547 | for (i = 0; i < n; i++, v += 16, p += 6) { |
| 548 | v[ 0] = rectangles[i].x1; |
| 549 | v[ 1] = rectangles[i].y1; |
| 550 | v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width; |
| 551 | v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height; |
Kristian Høgsberg | fa4e2a7 | 2011-02-13 13:44:55 -0500 | [diff] [blame] | 552 | |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 553 | v[ 4] = rectangles[i].x1; |
| 554 | v[ 5] = rectangles[i].y2; |
| 555 | v[ 6] = v[ 2]; |
| 556 | v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height; |
Kristian Høgsberg | fa4e2a7 | 2011-02-13 13:44:55 -0500 | [diff] [blame] | 557 | |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 558 | v[ 8] = rectangles[i].x2; |
| 559 | v[ 9] = rectangles[i].y1; |
| 560 | v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width; |
| 561 | v[11] = v[ 3]; |
Kristian Høgsberg | fa4e2a7 | 2011-02-13 13:44:55 -0500 | [diff] [blame] | 562 | |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 563 | v[12] = rectangles[i].x2; |
| 564 | v[13] = rectangles[i].y2; |
| 565 | v[14] = v[10]; |
| 566 | v[15] = v[ 7]; |
| 567 | |
| 568 | p[0] = i * 4 + 0; |
| 569 | p[1] = i * 4 + 1; |
| 570 | p[2] = i * 4 + 2; |
| 571 | p[3] = i * 4 + 2; |
| 572 | p[4] = i * 4 + 1; |
| 573 | p[5] = i * 4 + 3; |
| 574 | } |
Kristian Høgsberg | fa4e2a7 | 2011-02-13 13:44:55 -0500 | [diff] [blame] | 575 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 576 | glBindTexture(GL_TEXTURE_2D, es->texture); |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 577 | v = ec->vertices.data; |
| 578 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]); |
| 579 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]); |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 580 | glEnableVertexAttribArray(0); |
| 581 | glEnableVertexAttribArray(1); |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 582 | glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data); |
| 583 | |
| 584 | ec->vertices.size = 0; |
| 585 | ec->indices.size = 0; |
| 586 | pixman_region32_fini(&repaint); |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | static void |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 590 | wlsc_surface_raise(struct wlsc_surface *surface) |
| 591 | { |
| 592 | struct wlsc_compositor *compositor = surface->compositor; |
| 593 | |
| 594 | wl_list_remove(&surface->link); |
Kristian Høgsberg | 747638b | 2010-07-12 17:06:06 -0400 | [diff] [blame] | 595 | wl_list_insert(&compositor->surface_list, &surface->link); |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 596 | } |
| 597 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 598 | WL_EXPORT void |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 599 | wlsc_surface_update_matrix(struct wlsc_surface *es) |
| 600 | { |
| 601 | wlsc_matrix_init(&es->matrix); |
| 602 | wlsc_matrix_scale(&es->matrix, es->width, es->height, 1); |
| 603 | wlsc_matrix_translate(&es->matrix, es->x, es->y, 0); |
| 604 | |
| 605 | wlsc_matrix_init(&es->matrix_inv); |
| 606 | wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0); |
| 607 | wlsc_matrix_scale(&es->matrix_inv, |
| 608 | 1.0 / es->width, 1.0 / es->height, 1); |
| 609 | } |
| 610 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 611 | WL_EXPORT void |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 612 | wlsc_compositor_damage_all(struct wlsc_compositor *compositor) |
| 613 | { |
| 614 | struct wlsc_output *output; |
| 615 | |
| 616 | wl_list_for_each(output, &compositor->output_list, link) |
| 617 | wlsc_output_damage(output); |
| 618 | } |
| 619 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 620 | WL_EXPORT void |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 621 | wlsc_output_finish_frame(struct wlsc_output *output, int msecs) |
Kristian Høgsberg | 01f941b | 2009-05-27 17:47:15 -0400 | [diff] [blame] | 622 | { |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 623 | struct wlsc_compositor *compositor = output->compositor; |
| 624 | struct wlsc_surface *es; |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 625 | struct wlsc_animation *animation, *next; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 626 | |
| 627 | wl_list_for_each(es, &compositor->surface_list, link) { |
| 628 | if (es->output == output) { |
| 629 | wl_display_post_frame(compositor->wl_display, |
| 630 | &es->surface, msecs); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | output->finished = 1; |
| 635 | |
Kristian Høgsberg | 5d312db | 2009-09-12 16:57:02 -0400 | [diff] [blame] | 636 | wl_event_source_timer_update(compositor->timer_source, 5); |
Kristian Høgsberg | f30c67e | 2011-02-06 12:58:44 -0500 | [diff] [blame] | 637 | compositor->repaint_on_timeout = 1; |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 638 | |
| 639 | wl_list_for_each_safe(animation, next, |
| 640 | &compositor->animation_list, link) |
| 641 | animation->frame(animation, output, msecs); |
Kristian Høgsberg | 01f941b | 2009-05-27 17:47:15 -0400 | [diff] [blame] | 642 | } |
| 643 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 644 | WL_EXPORT void |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 645 | wlsc_output_damage(struct wlsc_output *output) |
| 646 | { |
| 647 | struct wlsc_compositor *compositor = output->compositor; |
| 648 | |
| 649 | pixman_region32_union_rect(&compositor->damage_region, |
| 650 | &compositor->damage_region, |
| 651 | output->x, output->y, |
| 652 | output->width, output->height); |
| 653 | wlsc_compositor_schedule_repaint(compositor); |
| 654 | } |
| 655 | |
Kristian Høgsberg | 01f941b | 2009-05-27 17:47:15 -0400 | [diff] [blame] | 656 | static void |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 657 | fade_frame(struct wlsc_animation *animation, |
| 658 | struct wlsc_output *output, uint32_t msecs) |
| 659 | { |
| 660 | struct wlsc_compositor *compositor = |
| 661 | container_of(animation, |
| 662 | struct wlsc_compositor, fade.animation); |
| 663 | |
| 664 | wlsc_tweener_update(&compositor->fade.tweener, msecs); |
| 665 | if (wlsc_tweener_done(&compositor->fade.tweener)) { |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 666 | if (compositor->fade.tweener.current > 0.999) { |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 667 | compositor->state = WLSC_COMPOSITOR_SLEEPING; |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 668 | compositor->shell->lock(compositor->shell); |
| 669 | } |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 670 | compositor->fade.tweener.current = |
| 671 | compositor->fade.tweener.target; |
| 672 | wl_list_remove(&animation->link); |
| 673 | wl_list_init(&animation->link); |
| 674 | } |
| 675 | |
| 676 | wlsc_output_damage(output); |
| 677 | } |
| 678 | |
| 679 | static void |
| 680 | fade_output(struct wlsc_output *output, |
| 681 | GLfloat tint, pixman_region32_t *region) |
| 682 | { |
| 683 | struct wlsc_compositor *compositor = output->compositor; |
| 684 | struct wlsc_surface surface; |
| 685 | GLfloat color[4] = { 0.0, 0.0, 0.0, tint }; |
| 686 | |
| 687 | surface.compositor = compositor; |
| 688 | surface.x = output->x; |
| 689 | surface.y = output->y; |
| 690 | surface.width = output->width; |
| 691 | surface.height = output->height; |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 692 | surface.texture = GL_NONE; |
Kristian Høgsberg | c352ab0 | 2011-04-23 15:34:50 -0400 | [diff] [blame] | 693 | |
| 694 | if (tint <= 1.0) |
| 695 | surface.visual = |
| 696 | &compositor->compositor.premultiplied_argb_visual; |
| 697 | else |
| 698 | surface.visual = &compositor->compositor.rgb_visual; |
| 699 | |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 700 | glUseProgram(compositor->solid_shader.program); |
| 701 | glUniformMatrix4fv(compositor->solid_shader.proj_uniform, |
| 702 | 1, GL_FALSE, output->matrix.d); |
| 703 | glUniform4fv(compositor->solid_shader.color_uniform, 1, color); |
| 704 | wlsc_surface_draw(&surface, output, region); |
| 705 | } |
| 706 | |
| 707 | static void |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 708 | wlsc_output_repaint(struct wlsc_output *output) |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 709 | { |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 710 | struct wlsc_compositor *ec = output->compositor; |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 711 | struct wlsc_surface *es; |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 712 | struct wlsc_input_device *eid; |
Kristian Høgsberg | cfc6d27 | 2011-04-11 13:34:24 -0400 | [diff] [blame] | 713 | pixman_region32_t new_damage, total_damage, repaint; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 714 | int using_hardware_cursor = 1; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 715 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 716 | output->prepare_render(output); |
| 717 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 718 | glViewport(0, 0, output->width, output->height); |
Kristian Høgsberg | fdec236 | 2001-01-01 22:23:51 -0500 | [diff] [blame] | 719 | |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame] | 720 | glUseProgram(ec->texture_shader.program); |
| 721 | glUniformMatrix4fv(ec->texture_shader.proj_uniform, |
| 722 | 1, GL_FALSE, output->matrix.d); |
| 723 | glUniform1i(ec->texture_shader.tex_uniform, 0); |
Kristian Høgsberg | fa4e2a7 | 2011-02-13 13:44:55 -0500 | [diff] [blame] | 724 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 725 | pixman_region32_init(&new_damage); |
| 726 | pixman_region32_init(&total_damage); |
| 727 | pixman_region32_intersect_rect(&new_damage, |
| 728 | &ec->damage_region, |
| 729 | output->x, output->y, |
| 730 | output->width, output->height); |
| 731 | pixman_region32_subtract(&ec->damage_region, |
| 732 | &ec->damage_region, &new_damage); |
| 733 | pixman_region32_union(&total_damage, &new_damage, |
| 734 | &output->previous_damage_region); |
| 735 | pixman_region32_copy(&output->previous_damage_region, &new_damage); |
| 736 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 737 | if (ec->focus) |
| 738 | if (output->set_hardware_cursor(output, ec->input_device) < 0) |
| 739 | using_hardware_cursor = 0; |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 740 | if (ec->fade.tweener.current > 0.001) |
| 741 | using_hardware_cursor = 0; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 742 | |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 743 | es = container_of(ec->surface_list.next, struct wlsc_surface, link); |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 744 | if (es->fullscreen_output == output) { |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 745 | if (es->visual == &ec->compositor.rgb_visual && |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 746 | using_hardware_cursor) { |
| 747 | if (output->prepare_scanout_surface(output, es) == 0) { |
| 748 | /* We're drawing nothing now, |
| 749 | * draw the damaged regions later. */ |
| 750 | pixman_region32_union(&ec->damage_region, |
| 751 | &ec->damage_region, |
| 752 | &total_damage); |
| 753 | return; |
| 754 | } |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 755 | } |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 756 | |
| 757 | if (es->width < output->width || |
| 758 | es->height < output->height) |
| 759 | glClear(GL_COLOR_BUFFER_BIT); |
| 760 | wlsc_surface_draw(es, output, &total_damage); |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 761 | } else { |
Kristian Høgsberg | cfc6d27 | 2011-04-11 13:34:24 -0400 | [diff] [blame] | 762 | wl_list_for_each(es, &ec->surface_list, link) { |
| 763 | if (es->visual != &ec->compositor.rgb_visual) |
| 764 | continue; |
| 765 | |
| 766 | pixman_region32_init_rect(&repaint, |
| 767 | es->x, es->y, |
| 768 | es->width, es->height); |
| 769 | wlsc_surface_draw(es, output, &total_damage); |
| 770 | pixman_region32_subtract(&total_damage, |
| 771 | &total_damage, &repaint); |
| 772 | } |
| 773 | |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 774 | if (output->background) |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 775 | wlsc_surface_draw(output->background, |
| 776 | output, &total_damage); |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 777 | else |
Kristian Høgsberg | c352ab0 | 2011-04-23 15:34:50 -0400 | [diff] [blame] | 778 | fade_output(output, 1.0, &total_damage); |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 779 | |
Kristian Høgsberg | c352ab0 | 2011-04-23 15:34:50 -0400 | [diff] [blame] | 780 | glUseProgram(ec->texture_shader.program); |
Kristian Høgsberg | c9824dd | 2011-02-06 16:54:59 -0500 | [diff] [blame] | 781 | wl_list_for_each_reverse(es, &ec->surface_list, link) { |
Kristian Høgsberg | 547cadf | 2011-04-12 22:23:30 -0400 | [diff] [blame] | 782 | if (ec->overlay == es) |
Kristian Høgsberg | c9824dd | 2011-02-06 16:54:59 -0500 | [diff] [blame] | 783 | continue; |
| 784 | |
Kristian Høgsberg | cfc6d27 | 2011-04-11 13:34:24 -0400 | [diff] [blame] | 785 | if (es->visual == &ec->compositor.rgb_visual) { |
| 786 | pixman_region32_union_rect(&total_damage, |
| 787 | &total_damage, |
| 788 | es->x, es->y, |
| 789 | es->width, es->height); |
| 790 | continue; |
| 791 | } |
| 792 | |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 793 | wlsc_surface_draw(es, output, &total_damage); |
Kristian Høgsberg | c9824dd | 2011-02-06 16:54:59 -0500 | [diff] [blame] | 794 | } |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 795 | } |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 796 | |
Kristian Høgsberg | 547cadf | 2011-04-12 22:23:30 -0400 | [diff] [blame] | 797 | if (ec->overlay) |
| 798 | wlsc_surface_draw(ec->overlay, output, &total_damage); |
Kristian Høgsberg | c9824dd | 2011-02-06 16:54:59 -0500 | [diff] [blame] | 799 | |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 800 | if (ec->focus) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 801 | wl_list_for_each(eid, &ec->input_device_list, link) { |
| 802 | if (&eid->input_device != ec->input_device || |
| 803 | !using_hardware_cursor) |
| 804 | wlsc_surface_draw(eid->sprite, output, |
| 805 | &total_damage); |
| 806 | } |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 807 | |
| 808 | if (ec->fade.tweener.current > 0.001) |
| 809 | fade_output(output, ec->fade.tweener.current, &total_damage); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 810 | } |
| 811 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 812 | static int |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 813 | repaint(void *data) |
| 814 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 815 | struct wlsc_compositor *ec = data; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 816 | struct wlsc_output *output; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 817 | int repainted_all_outputs = 1; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 818 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 819 | wl_list_for_each(output, &ec->output_list, link) { |
| 820 | if (!output->repaint_needed) |
| 821 | continue; |
| 822 | |
| 823 | if (!output->finished) { |
| 824 | repainted_all_outputs = 0; |
| 825 | continue; |
| 826 | } |
| 827 | |
| 828 | wlsc_output_repaint(output); |
| 829 | output->finished = 0; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 830 | output->repaint_needed = 0; |
Kristian Høgsberg | 11e2828 | 2011-04-11 16:47:50 -0400 | [diff] [blame] | 831 | output->present(output); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 832 | } |
| 833 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 834 | if (repainted_all_outputs) |
| 835 | ec->repaint_on_timeout = 0; |
| 836 | else |
| 837 | wl_event_source_timer_update(ec->timer_source, 1); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 838 | |
| 839 | return 1; |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 840 | } |
| 841 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 842 | WL_EXPORT void |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 843 | wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor) |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 844 | { |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 845 | struct wlsc_output *output; |
| 846 | |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 847 | if (compositor->state == WLSC_COMPOSITOR_SLEEPING) |
| 848 | return; |
| 849 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 850 | wl_list_for_each(output, &compositor->output_list, link) |
| 851 | output->repaint_needed = 1; |
| 852 | |
Kristian Høgsberg | b0a167c | 2009-08-14 11:15:18 -0400 | [diff] [blame] | 853 | if (compositor->repaint_on_timeout) |
| 854 | return; |
| 855 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 856 | wl_event_source_timer_update(compositor->timer_source, 1); |
| 857 | compositor->repaint_on_timeout = 1; |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 858 | } |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 859 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 860 | WL_EXPORT void |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 861 | wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint) |
| 862 | { |
| 863 | int done; |
| 864 | |
| 865 | done = wlsc_tweener_done(&compositor->fade.tweener); |
| 866 | compositor->fade.tweener.target = tint; |
| 867 | if (wlsc_tweener_done(&compositor->fade.tweener)) |
| 868 | return; |
| 869 | |
| 870 | if (done) |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 871 | compositor->fade.tweener.timestamp = |
| 872 | wlsc_compositor_get_time(); |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 873 | |
| 874 | wlsc_compositor_damage_all(compositor); |
| 875 | if (wl_list_empty(&compositor->fade.animation.link)) |
| 876 | wl_list_insert(compositor->animation_list.prev, |
| 877 | &compositor->fade.animation.link); |
| 878 | } |
| 879 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 880 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 881 | surface_destroy(struct wl_client *client, |
| 882 | struct wl_surface *surface) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 883 | { |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 884 | wl_resource_destroy(&surface->resource, client); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 885 | } |
| 886 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 887 | WL_EXPORT void |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 888 | wlsc_surface_assign_output(struct wlsc_surface *es) |
| 889 | { |
| 890 | struct wlsc_compositor *ec = es->compositor; |
| 891 | struct wlsc_output *output; |
| 892 | |
| 893 | struct wlsc_output *tmp = es->output; |
| 894 | es->output = NULL; |
| 895 | |
| 896 | wl_list_for_each(output, &ec->output_list, link) { |
| 897 | if (output->x < es->x && es->x < output->x + output->width && |
| 898 | output->y < es->y && es->y < output->y + output->height) { |
| 899 | if (output != tmp) |
| 900 | printf("assiging surface %p to output %p\n", |
| 901 | es, output); |
| 902 | es->output = output; |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | if (es->output == NULL) { |
| 907 | printf("no output found\n"); |
| 908 | es->output = container_of(ec->output_list.next, |
| 909 | struct wlsc_output, link); |
| 910 | } |
| 911 | } |
| 912 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 913 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 914 | surface_attach(struct wl_client *client, |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 915 | struct wl_surface *surface, struct wl_buffer *buffer, |
| 916 | int32_t x, int32_t y) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 917 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 918 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 919 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 920 | /* FIXME: This damages the entire old surface, but we should |
| 921 | * really just damage the part that's no longer covered by the |
| 922 | * surface. Anything covered by the new surface will be |
| 923 | * damaged by the client. */ |
| 924 | wlsc_surface_damage(es); |
| 925 | |
Benjamin Franzke | faa0a9d | 2011-02-21 16:24:53 +0100 | [diff] [blame] | 926 | wlsc_buffer_attach(buffer, surface); |
| 927 | |
Kristian Høgsberg | 3d5bae0 | 2010-10-06 21:17:40 -0400 | [diff] [blame] | 928 | es->buffer = buffer; |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 929 | es->x += x; |
| 930 | es->y += y; |
| 931 | es->width = buffer->width; |
| 932 | es->height = buffer->height; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 933 | if (x != 0 || y != 0) |
| 934 | wlsc_surface_assign_output(es); |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 935 | wlsc_surface_update_matrix(es); |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 936 | |
| 937 | es->compositor->shell->attach(es->compositor->shell, es); |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 938 | } |
| 939 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 940 | static void |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 941 | surface_map_toplevel(struct wl_client *client, |
| 942 | struct wl_surface *surface) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 943 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 944 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 945 | struct wlsc_compositor *ec = es->compositor; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 946 | |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 947 | switch (es->map_type) { |
| 948 | case WLSC_SURFACE_MAP_UNMAPPED: |
| 949 | es->x = 10 + random() % 400; |
| 950 | es->y = 10 + random() % 400; |
| 951 | wlsc_surface_update_matrix(es); |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 952 | /* assign to first output */ |
| 953 | es->output = container_of(ec->output_list.next, |
| 954 | struct wlsc_output, link); |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 955 | wl_list_insert(&es->compositor->surface_list, &es->link); |
| 956 | break; |
| 957 | case WLSC_SURFACE_MAP_TOPLEVEL: |
Kristian Høgsberg | 8f66a57 | 2011-01-07 08:38:56 -0500 | [diff] [blame] | 958 | return; |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 959 | case WLSC_SURFACE_MAP_FULLSCREEN: |
| 960 | es->fullscreen_output = NULL; |
| 961 | es->x = es->saved_x; |
| 962 | es->y = es->saved_y; |
| 963 | wlsc_surface_update_matrix(es); |
| 964 | break; |
| 965 | default: |
| 966 | break; |
| 967 | } |
Kristian Høgsberg | 8f66a57 | 2011-01-07 08:38:56 -0500 | [diff] [blame] | 968 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 969 | wlsc_surface_damage(es); |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 970 | es->map_type = WLSC_SURFACE_MAP_TOPLEVEL; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 971 | } |
| 972 | |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 973 | static void |
Kristian Høgsberg | 8dc378f | 2011-01-21 18:02:24 -0500 | [diff] [blame] | 974 | surface_map_transient(struct wl_client *client, |
| 975 | struct wl_surface *surface, struct wl_surface *parent, |
| 976 | int x, int y, uint32_t flags) |
| 977 | { |
| 978 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 979 | struct wlsc_surface *pes = (struct wlsc_surface *) parent; |
| 980 | |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 981 | switch (es->map_type) { |
| 982 | case WLSC_SURFACE_MAP_UNMAPPED: |
| 983 | wl_list_insert(&es->compositor->surface_list, &es->link); |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 984 | /* assign to parents output */ |
| 985 | es->output = pes->output; |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 986 | break; |
| 987 | case WLSC_SURFACE_MAP_FULLSCREEN: |
| 988 | es->fullscreen_output = NULL; |
| 989 | break; |
| 990 | default: |
| 991 | break; |
| 992 | } |
Kristian Høgsberg | 8dc378f | 2011-01-21 18:02:24 -0500 | [diff] [blame] | 993 | |
| 994 | es->x = pes->x + x; |
| 995 | es->y = pes->y + y; |
| 996 | |
| 997 | wlsc_surface_update_matrix(es); |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 998 | wlsc_surface_damage(es); |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 999 | es->map_type = WLSC_SURFACE_MAP_TRANSIENT; |
| 1000 | } |
| 1001 | |
| 1002 | static void |
| 1003 | surface_map_fullscreen(struct wl_client *client, struct wl_surface *surface) |
| 1004 | { |
| 1005 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 1006 | struct wlsc_output *output; |
| 1007 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 1008 | /* FIXME: Fullscreen on first output */ |
| 1009 | /* FIXME: Handle output going away */ |
| 1010 | output = container_of(es->compositor->output_list.next, |
| 1011 | struct wlsc_output, link); |
| 1012 | |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 1013 | switch (es->map_type) { |
| 1014 | case WLSC_SURFACE_MAP_UNMAPPED: |
| 1015 | es->x = 10 + random() % 400; |
| 1016 | es->y = 10 + random() % 400; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 1017 | /* assign to first output */ |
| 1018 | es->output = output; |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 1019 | wl_list_insert(&es->compositor->surface_list, &es->link); |
| 1020 | break; |
| 1021 | case WLSC_SURFACE_MAP_FULLSCREEN: |
| 1022 | return; |
| 1023 | default: |
| 1024 | break; |
| 1025 | } |
| 1026 | |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 1027 | es->saved_x = es->x; |
| 1028 | es->saved_y = es->y; |
| 1029 | es->x = (output->width - es->width) / 2; |
| 1030 | es->y = (output->height - es->height) / 2; |
| 1031 | es->fullscreen_output = output; |
| 1032 | wlsc_surface_update_matrix(es); |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 1033 | wlsc_surface_damage(es); |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 1034 | es->map_type = WLSC_SURFACE_MAP_FULLSCREEN; |
Kristian Høgsberg | 8dc378f | 2011-01-21 18:02:24 -0500 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 1038 | surface_damage(struct wl_client *client, |
| 1039 | struct wl_surface *surface, |
| 1040 | 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] | 1041 | { |
Kristian Høgsberg | 9d69f8e | 2010-09-03 14:46:38 -0400 | [diff] [blame] | 1042 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 1043 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 1044 | wlsc_surface_damage_rectangle(es, x, y, width, height); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 1045 | } |
| 1046 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 1047 | const static struct wl_surface_interface surface_interface = { |
| 1048 | surface_destroy, |
| 1049 | surface_attach, |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 1050 | surface_map_toplevel, |
Kristian Høgsberg | 8dc378f | 2011-01-21 18:02:24 -0500 | [diff] [blame] | 1051 | surface_map_transient, |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 1052 | surface_map_fullscreen, |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 1053 | surface_damage |
| 1054 | }; |
| 1055 | |
| 1056 | static void |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1057 | wlsc_input_device_attach(struct wlsc_input_device *device, |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 1058 | int x, int y, int width, int height) |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 1059 | { |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 1060 | wlsc_surface_damage(device->sprite); |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 1061 | |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1062 | device->hotspot_x = x; |
| 1063 | device->hotspot_y = y; |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 1064 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1065 | device->sprite->x = device->input_device.x - device->hotspot_x; |
| 1066 | device->sprite->y = device->input_device.y - device->hotspot_y; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 1067 | device->sprite->width = width; |
| 1068 | device->sprite->height = height; |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 1069 | wlsc_surface_update_matrix(device->sprite); |
| 1070 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 1071 | wlsc_surface_damage(device->sprite); |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 1072 | } |
| 1073 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 1074 | static void |
| 1075 | wlsc_input_device_attach_buffer(struct wlsc_input_device *device, |
| 1076 | struct wl_buffer *buffer, int x, int y) |
| 1077 | { |
| 1078 | wlsc_buffer_attach(buffer, &device->sprite->surface); |
| 1079 | wlsc_input_device_attach(device, x, y, buffer->width, buffer->height); |
| 1080 | } |
| 1081 | |
| 1082 | static void |
| 1083 | wlsc_input_device_attach_sprite(struct wlsc_input_device *device, |
| 1084 | struct wlsc_sprite *sprite, int x, int y) |
| 1085 | { |
| 1086 | wlsc_sprite_attach(sprite, &device->sprite->surface); |
| 1087 | wlsc_input_device_attach(device, x, y, sprite->width, sprite->height); |
| 1088 | } |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1089 | |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 1090 | void |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1091 | wlsc_input_device_set_pointer_image(struct wlsc_input_device *device, |
| 1092 | enum wlsc_pointer_type type) |
| 1093 | { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1094 | struct wlsc_compositor *compositor = |
| 1095 | (struct wlsc_compositor *) device->input_device.compositor; |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1096 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 1097 | wlsc_input_device_attach_sprite(device, |
| 1098 | compositor->pointer_sprites[type], |
| 1099 | pointer_images[type].hotspot_x, |
| 1100 | pointer_images[type].hotspot_y); |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1101 | } |
| 1102 | |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 1103 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 1104 | compositor_create_surface(struct wl_client *client, |
| 1105 | struct wl_compositor *compositor, uint32_t id) |
| 1106 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1107 | struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor; |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 1108 | struct wlsc_surface *surface; |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 1109 | |
Kristian Høgsberg | c5d6be9 | 2011-01-14 16:22:37 -0500 | [diff] [blame] | 1110 | surface = wlsc_surface_create(ec, 0, 0, 0, 0); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 1111 | if (surface == NULL) { |
Kristian Høgsberg | 13b8ae4 | 2010-09-02 20:55:16 -0400 | [diff] [blame] | 1112 | wl_client_post_no_memory(client); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 1113 | return; |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 1114 | } |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 1115 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1116 | surface->surface.resource.destroy = destroy_surface; |
Kristian Høgsberg | f66d0f4 | 2010-09-02 20:27:16 -0400 | [diff] [blame] | 1117 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1118 | surface->surface.resource.object.id = id; |
| 1119 | surface->surface.resource.object.interface = &wl_surface_interface; |
| 1120 | surface->surface.resource.object.implementation = |
Kristian Høgsberg | f66d0f4 | 2010-09-02 20:27:16 -0400 | [diff] [blame] | 1121 | (void (**)(void)) &surface_interface; |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1122 | surface->surface.client = client; |
Kristian Høgsberg | f66d0f4 | 2010-09-02 20:27:16 -0400 | [diff] [blame] | 1123 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1124 | wl_client_add_resource(client, &surface->surface.resource); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 1125 | } |
| 1126 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 1127 | const static struct wl_compositor_interface compositor_interface = { |
| 1128 | compositor_create_surface, |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 1129 | }; |
| 1130 | |
Kristian Høgsberg | 7b6907f | 2009-02-14 17:47:55 -0500 | [diff] [blame] | 1131 | static void |
| 1132 | wlsc_surface_transform(struct wlsc_surface *surface, |
| 1133 | int32_t x, int32_t y, int32_t *sx, int32_t *sy) |
| 1134 | { |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1135 | struct wlsc_vector v = { { x, y, 0, 1 } }; |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1136 | |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1137 | wlsc_matrix_transform(&surface->matrix_inv, &v); |
Kristian Høgsberg | 0b8646b | 2010-06-08 15:29:14 -0400 | [diff] [blame] | 1138 | *sx = v.f[0] * surface->width; |
| 1139 | *sy = v.f[1] * surface->height; |
Kristian Høgsberg | 7b6907f | 2009-02-14 17:47:55 -0500 | [diff] [blame] | 1140 | } |
| 1141 | |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 1142 | struct wlsc_surface * |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1143 | pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy) |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 1144 | { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1145 | struct wlsc_compositor *ec = |
| 1146 | (struct wlsc_compositor *) device->compositor; |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1147 | struct wlsc_surface *es; |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 1148 | |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1149 | wl_list_for_each(es, &ec->surface_list, link) { |
| 1150 | wlsc_surface_transform(es, device->x, device->y, sx, sy); |
| 1151 | if (0 <= *sx && *sx < es->width && |
| 1152 | 0 <= *sy && *sy < es->height) |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 1153 | return es; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1154 | } |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 1155 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 1156 | return NULL; |
| 1157 | } |
| 1158 | |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 1159 | |
| 1160 | static void |
| 1161 | motion_grab_motion(struct wl_grab *grab, |
| 1162 | uint32_t time, int32_t x, int32_t y) |
| 1163 | { |
| 1164 | struct wlsc_input_device *device = |
| 1165 | (struct wlsc_input_device *) grab->input_device; |
| 1166 | struct wlsc_surface *es = |
| 1167 | (struct wlsc_surface *) device->input_device.pointer_focus; |
| 1168 | int32_t sx, sy; |
| 1169 | |
| 1170 | wlsc_surface_transform(es, x, y, &sx, &sy); |
| 1171 | wl_client_post_event(es->surface.client, |
| 1172 | &device->input_device.object, |
| 1173 | WL_INPUT_DEVICE_MOTION, |
| 1174 | time, x, y, sx, sy); |
| 1175 | } |
| 1176 | |
| 1177 | static void |
Kristian Høgsberg | b3fc757 | 2010-12-08 11:07:57 -0500 | [diff] [blame] | 1178 | motion_grab_button(struct wl_grab *grab, |
| 1179 | uint32_t time, int32_t button, int32_t state) |
| 1180 | { |
| 1181 | wl_client_post_event(grab->input_device->pointer_focus->client, |
| 1182 | &grab->input_device->object, |
| 1183 | WL_INPUT_DEVICE_BUTTON, |
| 1184 | time, button, state); |
| 1185 | } |
| 1186 | |
| 1187 | static void |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 1188 | motion_grab_end(struct wl_grab *grab, uint32_t time) |
| 1189 | { |
| 1190 | } |
| 1191 | |
| 1192 | static const struct wl_grab_interface motion_grab_interface = { |
| 1193 | motion_grab_motion, |
Kristian Høgsberg | b3fc757 | 2010-12-08 11:07:57 -0500 | [diff] [blame] | 1194 | motion_grab_button, |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 1195 | motion_grab_end |
| 1196 | }; |
| 1197 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 1198 | WL_EXPORT void |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1199 | wlsc_compositor_wake(struct wlsc_compositor *compositor) |
| 1200 | { |
| 1201 | if (compositor->idle_inhibit) |
| 1202 | return; |
| 1203 | |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 1204 | wlsc_compositor_fade(compositor, 0.0); |
| 1205 | compositor->state = WLSC_COMPOSITOR_ACTIVE; |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1206 | |
| 1207 | wl_event_source_timer_update(compositor->idle_source, |
| 1208 | option_idle_time * 1000); |
| 1209 | } |
| 1210 | |
| 1211 | static void |
| 1212 | wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor) |
| 1213 | { |
| 1214 | wlsc_compositor_wake(compositor); |
| 1215 | compositor->idle_inhibit++; |
| 1216 | } |
| 1217 | |
| 1218 | static void |
| 1219 | wlsc_compositor_idle_release(struct wlsc_compositor *compositor) |
| 1220 | { |
| 1221 | compositor->idle_inhibit--; |
| 1222 | wlsc_compositor_wake(compositor); |
| 1223 | } |
| 1224 | |
| 1225 | static int |
| 1226 | idle_handler(void *data) |
| 1227 | { |
| 1228 | struct wlsc_compositor *compositor = data; |
| 1229 | |
| 1230 | if (compositor->idle_inhibit) |
| 1231 | return 1; |
| 1232 | |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 1233 | wlsc_compositor_fade(compositor, 1.0); |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1234 | |
| 1235 | return 1; |
| 1236 | } |
| 1237 | |
| 1238 | void |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1239 | notify_motion(struct wl_input_device *device, uint32_t time, int x, int y) |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 1240 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1241 | struct wlsc_surface *es; |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1242 | struct wlsc_compositor *ec = |
| 1243 | (struct wlsc_compositor *) device->compositor; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1244 | struct wlsc_output *output; |
Kristian Høgsberg | 6d65d5f | 2010-12-07 13:30:18 -0500 | [diff] [blame] | 1245 | const struct wl_grab_interface *interface; |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1246 | struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 1247 | int32_t sx, sy; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1248 | int x_valid = 0, y_valid = 0; |
| 1249 | int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN; |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 1250 | |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1251 | wlsc_compositor_wake(ec); |
| 1252 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1253 | wl_list_for_each(output, &ec->output_list, link) { |
| 1254 | if (output->x <= x && x <= output->x + output->width) |
| 1255 | x_valid = 1; |
| 1256 | |
| 1257 | if (output->y <= y && y <= output->y + output->height) |
| 1258 | y_valid = 1; |
| 1259 | |
| 1260 | /* FIXME: calculate this only on output addition/deletion */ |
| 1261 | if (output->x < min_x) |
| 1262 | min_x = output->x; |
| 1263 | if (output->y < min_y) |
| 1264 | min_y = output->y; |
| 1265 | |
| 1266 | if (output->x + output->width > max_x) |
| 1267 | max_x = output->x + output->width; |
| 1268 | if (output->y + output->height > max_y) |
| 1269 | max_y = output->y + output->height; |
| 1270 | } |
| 1271 | |
| 1272 | if (!x_valid) { |
| 1273 | if (x < min_x) |
| 1274 | x = min_x; |
| 1275 | else if (x >= max_x) |
| 1276 | x = max_x; |
| 1277 | } |
| 1278 | if (!y_valid) { |
| 1279 | if (y < min_y) |
| 1280 | y = min_y; |
| 1281 | else if (y >= max_y) |
| 1282 | y = max_y; |
| 1283 | } |
Ray Strode | 90e701d | 2008-12-18 23:05:43 -0500 | [diff] [blame] | 1284 | |
Kristian Høgsberg | e3ef3e5 | 2008-12-21 19:30:01 -0500 | [diff] [blame] | 1285 | device->x = x; |
| 1286 | device->y = y; |
Kristian Høgsberg | db6c2f3 | 2009-02-22 21:51:24 -0500 | [diff] [blame] | 1287 | |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 1288 | if (device->grab) { |
| 1289 | interface = device->grab->interface; |
| 1290 | interface->motion(device->grab, time, x, y); |
| 1291 | } else { |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 1292 | es = pick_surface(device, &sx, &sy); |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1293 | wl_input_device_set_pointer_focus(device, |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1294 | &es->surface, |
Kristian Høgsberg | 2643707 | 2010-12-01 10:17:47 -0500 | [diff] [blame] | 1295 | time, x, y, sx, sy); |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 1296 | if (es) |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1297 | wl_client_post_event(es->surface.client, |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1298 | &device->object, |
Kristian Høgsberg | 50038e4 | 2010-09-07 21:08:59 -0400 | [diff] [blame] | 1299 | WL_INPUT_DEVICE_MOTION, |
| 1300 | time, x, y, sx, sy); |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 1301 | } |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 1302 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 1303 | wlsc_surface_damage(wd->sprite); |
| 1304 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1305 | wd->sprite->x = device->x - wd->hotspot_x; |
| 1306 | wd->sprite->y = device->y - wd->hotspot_y; |
| 1307 | wlsc_surface_update_matrix(wd->sprite); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1308 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 1309 | wlsc_surface_damage(wd->sprite); |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 1310 | } |
| 1311 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 1312 | WL_EXPORT void |
Kristian Høgsberg | c9824dd | 2011-02-06 16:54:59 -0500 | [diff] [blame] | 1313 | wlsc_surface_activate(struct wlsc_surface *surface, |
| 1314 | struct wlsc_input_device *device, uint32_t time) |
| 1315 | { |
| 1316 | wlsc_surface_raise(surface); |
| 1317 | if (device->selection) |
| 1318 | wlsc_selection_set_focus(device->selection, |
| 1319 | &surface->surface, time); |
| 1320 | |
| 1321 | wl_input_device_set_keyboard_focus(&device->input_device, |
| 1322 | &surface->surface, |
| 1323 | time); |
| 1324 | } |
| 1325 | |
Kristian Høgsberg | a8ec863 | 2011-04-12 17:22:49 -0400 | [diff] [blame] | 1326 | struct wlsc_binding { |
| 1327 | uint32_t key; |
| 1328 | uint32_t button; |
| 1329 | uint32_t modifier; |
| 1330 | wlsc_binding_handler_t handler; |
| 1331 | void *data; |
| 1332 | struct wl_list link; |
| 1333 | }; |
| 1334 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1335 | void |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1336 | notify_button(struct wl_input_device *device, |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 1337 | uint32_t time, int32_t button, int32_t state) |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 1338 | { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1339 | struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1340 | struct wlsc_compositor *compositor = |
| 1341 | (struct wlsc_compositor *) device->compositor; |
Kristian Høgsberg | a8ec863 | 2011-04-12 17:22:49 -0400 | [diff] [blame] | 1342 | struct wlsc_binding *b; |
| 1343 | struct wlsc_surface *surface = |
| 1344 | (struct wlsc_surface *) device->pointer_focus; |
Kristian Høgsberg | ea08115 | 2010-12-07 08:59:51 -0500 | [diff] [blame] | 1345 | |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1346 | if (state) |
| 1347 | wlsc_compositor_idle_inhibit(compositor); |
| 1348 | else |
| 1349 | wlsc_compositor_idle_release(compositor); |
| 1350 | |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 1351 | if (state && surface && device->grab == NULL) { |
Kristian Høgsberg | c9824dd | 2011-02-06 16:54:59 -0500 | [diff] [blame] | 1352 | wlsc_surface_activate(surface, wd, time); |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1353 | wl_input_device_start_grab(device, |
| 1354 | &device->motion_grab, |
| 1355 | button, time); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1356 | } |
Kristian Høgsberg | dff2e3c | 2010-12-07 09:02:09 -0500 | [diff] [blame] | 1357 | |
Kristian Høgsberg | a8ec863 | 2011-04-12 17:22:49 -0400 | [diff] [blame] | 1358 | wl_list_for_each(b, &compositor->binding_list, link) { |
| 1359 | if (b->button == button && |
| 1360 | b->modifier == wd->modifier_state && state) { |
| 1361 | b->handler(&wd->input_device, |
| 1362 | time, 0, button, state, b->data); |
| 1363 | break; |
| 1364 | } |
Benjamin Franzke | 5a2218a | 2011-02-01 16:30:31 +0100 | [diff] [blame] | 1365 | } |
Kristian Høgsberg | b3fc757 | 2010-12-08 11:07:57 -0500 | [diff] [blame] | 1366 | |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 1367 | if (device->grab) |
| 1368 | device->grab->interface->button(device->grab, time, |
| 1369 | button, state); |
Kristian Høgsberg | dff2e3c | 2010-12-07 09:02:09 -0500 | [diff] [blame] | 1370 | |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 1371 | if (!state && device->grab && device->grab_button == button) |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1372 | wl_input_device_end_grab(device, time); |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 1373 | } |
| 1374 | |
Kristian Høgsberg | c9824dd | 2011-02-06 16:54:59 -0500 | [diff] [blame] | 1375 | static void |
Kristian Høgsberg | a8ec863 | 2011-04-12 17:22:49 -0400 | [diff] [blame] | 1376 | terminate_binding(struct wl_input_device *device, uint32_t time, |
| 1377 | uint32_t key, uint32_t button, uint32_t state, void *data) |
Kristian Høgsberg | 3555d09 | 2011-04-11 13:58:13 -0400 | [diff] [blame] | 1378 | { |
| 1379 | struct wlsc_compositor *compositor = data; |
| 1380 | |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1381 | if (state) |
| 1382 | wl_display_terminate(compositor->wl_display); |
Kristian Høgsberg | 3555d09 | 2011-04-11 13:58:13 -0400 | [diff] [blame] | 1383 | } |
| 1384 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 1385 | WL_EXPORT struct wlsc_binding * |
Kristian Høgsberg | 3555d09 | 2011-04-11 13:58:13 -0400 | [diff] [blame] | 1386 | wlsc_compositor_add_binding(struct wlsc_compositor *compositor, |
Kristian Høgsberg | a8ec863 | 2011-04-12 17:22:49 -0400 | [diff] [blame] | 1387 | uint32_t key, uint32_t button, uint32_t modifier, |
Kristian Høgsberg | 3555d09 | 2011-04-11 13:58:13 -0400 | [diff] [blame] | 1388 | wlsc_binding_handler_t handler, void *data) |
| 1389 | { |
| 1390 | struct wlsc_binding *binding; |
| 1391 | |
| 1392 | binding = malloc(sizeof *binding); |
| 1393 | if (binding == NULL) |
| 1394 | return NULL; |
| 1395 | |
| 1396 | binding->key = key; |
Kristian Høgsberg | a8ec863 | 2011-04-12 17:22:49 -0400 | [diff] [blame] | 1397 | binding->button = button; |
Kristian Høgsberg | 3555d09 | 2011-04-11 13:58:13 -0400 | [diff] [blame] | 1398 | binding->modifier = modifier; |
| 1399 | binding->handler = handler; |
| 1400 | binding->data = data; |
| 1401 | wl_list_insert(compositor->binding_list.prev, &binding->link); |
| 1402 | |
| 1403 | return binding; |
| 1404 | } |
| 1405 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 1406 | WL_EXPORT void |
Kristian Høgsberg | 3555d09 | 2011-04-11 13:58:13 -0400 | [diff] [blame] | 1407 | wlsc_binding_destroy(struct wlsc_binding *binding) |
| 1408 | { |
| 1409 | wl_list_remove(&binding->link); |
| 1410 | free(binding); |
| 1411 | } |
| 1412 | |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1413 | static void |
| 1414 | update_modifier_state(struct wlsc_input_device *device, |
| 1415 | uint32_t key, uint32_t state) |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 1416 | { |
Kristian Høgsberg | 2cbedd1 | 2009-09-18 17:29:49 -0400 | [diff] [blame] | 1417 | uint32_t modifier; |
Kristian Høgsberg | ab909ae | 2001-01-01 22:24:24 -0500 | [diff] [blame] | 1418 | |
Kristian Høgsberg | 2cbedd1 | 2009-09-18 17:29:49 -0400 | [diff] [blame] | 1419 | switch (key) { |
| 1420 | case KEY_LEFTCTRL: |
| 1421 | case KEY_RIGHTCTRL: |
| 1422 | modifier = MODIFIER_CTRL; |
| 1423 | break; |
| 1424 | |
| 1425 | case KEY_LEFTALT: |
| 1426 | case KEY_RIGHTALT: |
| 1427 | modifier = MODIFIER_ALT; |
| 1428 | break; |
| 1429 | |
Kristian Høgsberg | 5b75f1b | 2010-08-04 23:21:41 -0400 | [diff] [blame] | 1430 | case KEY_LEFTMETA: |
| 1431 | case KEY_RIGHTMETA: |
| 1432 | modifier = MODIFIER_SUPER; |
| 1433 | break; |
| 1434 | |
Kristian Høgsberg | 2cbedd1 | 2009-09-18 17:29:49 -0400 | [diff] [blame] | 1435 | default: |
| 1436 | modifier = 0; |
| 1437 | break; |
| 1438 | } |
| 1439 | |
| 1440 | if (state) |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1441 | device->modifier_state |= modifier; |
Kristian Høgsberg | 2cbedd1 | 2009-09-18 17:29:49 -0400 | [diff] [blame] | 1442 | else |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1443 | device->modifier_state &= ~modifier; |
| 1444 | } |
Kristian Høgsberg | 2cbedd1 | 2009-09-18 17:29:49 -0400 | [diff] [blame] | 1445 | |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1446 | void |
| 1447 | notify_key(struct wl_input_device *device, |
| 1448 | uint32_t time, uint32_t key, uint32_t state) |
| 1449 | { |
| 1450 | struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
| 1451 | struct wlsc_compositor *compositor = |
| 1452 | (struct wlsc_compositor *) device->compositor; |
| 1453 | uint32_t *k, *end; |
| 1454 | struct wlsc_binding *b; |
| 1455 | |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1456 | if (state) |
| 1457 | wlsc_compositor_idle_inhibit(compositor); |
| 1458 | else |
| 1459 | wlsc_compositor_idle_release(compositor); |
| 1460 | |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1461 | wl_list_for_each(b, &compositor->binding_list, link) { |
| 1462 | if (b->key == key && |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1463 | b->modifier == wd->modifier_state) { |
Kristian Høgsberg | a8ec863 | 2011-04-12 17:22:49 -0400 | [diff] [blame] | 1464 | b->handler(&wd->input_device, |
| 1465 | time, key, 0, state, b->data); |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1466 | break; |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | update_modifier_state(wd, key, state); |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1471 | end = device->keys.data + device->keys.size; |
| 1472 | for (k = device->keys.data; k < end; k++) { |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 1473 | if (*k == key) |
| 1474 | *k = *--end; |
| 1475 | } |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1476 | device->keys.size = (void *) end - device->keys.data; |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 1477 | if (state) { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1478 | k = wl_array_add(&device->keys, sizeof *k); |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 1479 | *k = key; |
| 1480 | } |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 1481 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1482 | if (device->keyboard_focus != NULL) |
| 1483 | wl_client_post_event(device->keyboard_focus->client, |
| 1484 | &device->object, |
Kristian Høgsberg | 50038e4 | 2010-09-07 21:08:59 -0400 | [diff] [blame] | 1485 | WL_INPUT_DEVICE_KEY, time, key, state); |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 1486 | } |
| 1487 | |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 1488 | void |
| 1489 | notify_pointer_focus(struct wl_input_device *device, |
| 1490 | uint32_t time, struct wlsc_output *output, |
| 1491 | int32_t x, int32_t y) |
| 1492 | { |
| 1493 | struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
| 1494 | struct wlsc_compositor *compositor = |
| 1495 | (struct wlsc_compositor *) device->compositor; |
| 1496 | struct wlsc_surface *es; |
| 1497 | int32_t sx, sy; |
| 1498 | |
| 1499 | if (output) { |
| 1500 | device->x = x; |
| 1501 | device->y = y; |
| 1502 | es = pick_surface(device, &sx, &sy); |
| 1503 | wl_input_device_set_pointer_focus(device, |
| 1504 | &es->surface, |
| 1505 | time, x, y, sx, sy); |
| 1506 | |
| 1507 | compositor->focus = 1; |
| 1508 | |
| 1509 | wd->sprite->x = device->x - wd->hotspot_x; |
| 1510 | wd->sprite->y = device->y - wd->hotspot_y; |
| 1511 | wlsc_surface_update_matrix(wd->sprite); |
| 1512 | } else { |
| 1513 | wl_input_device_set_pointer_focus(device, NULL, |
| 1514 | time, 0, 0, 0, 0); |
| 1515 | compositor->focus = 0; |
| 1516 | } |
| 1517 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 1518 | wlsc_surface_damage(wd->sprite); |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 1519 | } |
| 1520 | |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1521 | void |
Kristian Høgsberg | f992b2f | 2011-01-28 15:53:07 -0500 | [diff] [blame] | 1522 | notify_keyboard_focus(struct wl_input_device *device, |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1523 | uint32_t time, struct wlsc_output *output, |
| 1524 | struct wl_array *keys) |
| 1525 | { |
Kristian Høgsberg | f992b2f | 2011-01-28 15:53:07 -0500 | [diff] [blame] | 1526 | struct wlsc_input_device *wd = |
| 1527 | (struct wlsc_input_device *) device; |
| 1528 | struct wlsc_compositor *compositor = |
| 1529 | (struct wlsc_compositor *) device->compositor; |
| 1530 | struct wlsc_surface *es; |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1531 | uint32_t *k, *end; |
Kristian Høgsberg | f992b2f | 2011-01-28 15:53:07 -0500 | [diff] [blame] | 1532 | |
| 1533 | if (!wl_list_empty(&compositor->surface_list)) |
| 1534 | es = container_of(compositor->surface_list.next, |
| 1535 | struct wlsc_surface, link); |
| 1536 | else |
| 1537 | es = NULL; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1538 | |
| 1539 | if (output) { |
Kristian Høgsberg | f992b2f | 2011-01-28 15:53:07 -0500 | [diff] [blame] | 1540 | wl_array_copy(&wd->input_device.keys, keys); |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1541 | wd->modifier_state = 0; |
| 1542 | end = device->keys.data + device->keys.size; |
| 1543 | for (k = device->keys.data; k < end; k++) { |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1544 | wlsc_compositor_idle_inhibit(compositor); |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1545 | update_modifier_state(wd, *k, 1); |
| 1546 | } |
| 1547 | |
Kristian Høgsberg | f992b2f | 2011-01-28 15:53:07 -0500 | [diff] [blame] | 1548 | wl_input_device_set_keyboard_focus(&wd->input_device, |
| 1549 | &es->surface, time); |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1550 | } else { |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1551 | end = device->keys.data + device->keys.size; |
| 1552 | for (k = device->keys.data; k < end; k++) |
| 1553 | wlsc_compositor_idle_release(compositor); |
| 1554 | |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1555 | wd->modifier_state = 0; |
Kristian Høgsberg | f992b2f | 2011-01-28 15:53:07 -0500 | [diff] [blame] | 1556 | wl_input_device_set_keyboard_focus(&wd->input_device, |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1557 | NULL, time); |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 1562 | static void |
Kristian Høgsberg | ab1862d | 2010-12-09 11:29:40 -0500 | [diff] [blame] | 1563 | input_device_attach(struct wl_client *client, |
| 1564 | struct wl_input_device *device_base, |
| 1565 | uint32_t time, |
| 1566 | struct wl_buffer *buffer, int32_t x, int32_t y) |
| 1567 | { |
| 1568 | struct wlsc_input_device *device = |
| 1569 | (struct wlsc_input_device *) device_base; |
| 1570 | |
| 1571 | if (time < device->input_device.pointer_focus_time) |
| 1572 | return; |
| 1573 | if (device->input_device.pointer_focus == NULL) |
| 1574 | return; |
Kristian Høgsberg | ab1862d | 2010-12-09 11:29:40 -0500 | [diff] [blame] | 1575 | if (device->input_device.pointer_focus->client != client) |
| 1576 | return; |
| 1577 | |
| 1578 | if (buffer == NULL) { |
| 1579 | wlsc_input_device_set_pointer_image(device, |
| 1580 | WLSC_POINTER_LEFT_PTR); |
| 1581 | return; |
| 1582 | } |
| 1583 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 1584 | wlsc_input_device_attach_buffer(device, buffer, x, y); |
Kristian Høgsberg | ab1862d | 2010-12-09 11:29:40 -0500 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | const static struct wl_input_device_interface input_device_interface = { |
| 1588 | input_device_attach, |
| 1589 | }; |
| 1590 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1591 | void |
| 1592 | wlsc_input_device_init(struct wlsc_input_device *device, |
| 1593 | struct wlsc_compositor *ec) |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 1594 | { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1595 | wl_input_device_init(&device->input_device, &ec->compositor); |
Kristian Høgsberg | 02ef1c1 | 2010-12-06 21:35:19 -0500 | [diff] [blame] | 1596 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1597 | device->input_device.object.interface = &wl_input_device_interface; |
| 1598 | device->input_device.object.implementation = |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 1599 | (void (**)(void)) &input_device_interface; |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1600 | wl_display_add_object(ec->wl_display, &device->input_device.object); |
| 1601 | wl_display_add_global(ec->wl_display, &device->input_device.object, NULL); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 1602 | |
Kristian Høgsberg | c5d6be9 | 2011-01-14 16:22:37 -0500 | [diff] [blame] | 1603 | device->sprite = wlsc_surface_create(ec, |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1604 | device->input_device.x, |
| 1605 | device->input_device.y, 32, 32); |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 1606 | device->hotspot_x = 16; |
| 1607 | device->hotspot_y = 16; |
Kristian Høgsberg | a8ec863 | 2011-04-12 17:22:49 -0400 | [diff] [blame] | 1608 | device->modifier_state = 0; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1609 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1610 | device->input_device.motion_grab.interface = &motion_grab_interface; |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 1611 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 1612 | wl_list_insert(ec->input_device_list.prev, &device->link); |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 1613 | |
| 1614 | wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1615 | } |
| 1616 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1617 | static void |
Kristian Høgsberg | 91342c6 | 2011-04-14 14:44:58 -0400 | [diff] [blame] | 1618 | wlsc_output_post_geometry(struct wl_client *client, |
| 1619 | struct wl_object *global, uint32_t version) |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1620 | { |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1621 | struct wlsc_output *output = |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1622 | container_of(global, struct wlsc_output, object); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1623 | |
| 1624 | wl_client_post_event(client, global, |
| 1625 | WL_OUTPUT_GEOMETRY, |
Kristian Høgsberg | f8fc08f | 2010-12-01 20:10:10 -0500 | [diff] [blame] | 1626 | output->x, output->y, |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1627 | output->width, output->height); |
| 1628 | } |
| 1629 | |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1630 | static const char vertex_shader[] = |
| 1631 | "uniform mat4 proj;\n" |
Kristian Høgsberg | fa4e2a7 | 2011-02-13 13:44:55 -0500 | [diff] [blame] | 1632 | "attribute vec2 position;\n" |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1633 | "attribute vec2 texcoord;\n" |
| 1634 | "varying vec2 v_texcoord;\n" |
| 1635 | "void main()\n" |
| 1636 | "{\n" |
Kristian Høgsberg | fa4e2a7 | 2011-02-13 13:44:55 -0500 | [diff] [blame] | 1637 | " gl_Position = proj * vec4(position, 0.0, 1.0);\n" |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1638 | " v_texcoord = texcoord;\n" |
| 1639 | "}\n"; |
| 1640 | |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame] | 1641 | static const char texture_fragment_shader[] = |
Kristian Høgsberg | dfce71d | 2010-12-07 20:19:10 -0500 | [diff] [blame] | 1642 | "precision mediump float;\n" |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1643 | "varying vec2 v_texcoord;\n" |
| 1644 | "uniform sampler2D tex;\n" |
| 1645 | "void main()\n" |
| 1646 | "{\n" |
| 1647 | " gl_FragColor = texture2D(tex, v_texcoord)\n;" |
| 1648 | "}\n"; |
| 1649 | |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 1650 | static const char solid_fragment_shader[] = |
| 1651 | "precision mediump float;\n" |
| 1652 | "varying vec2 v_texcoord;\n" |
| 1653 | "uniform vec4 color;\n" |
| 1654 | "void main()\n" |
| 1655 | "{\n" |
| 1656 | " gl_FragColor = color\n;" |
| 1657 | "}\n"; |
| 1658 | |
Kristian Høgsberg | a946821 | 2010-06-14 21:03:11 -0400 | [diff] [blame] | 1659 | static int |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame] | 1660 | compile_shader(GLenum type, const char *source) |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1661 | { |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame] | 1662 | GLuint s; |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1663 | char msg[512]; |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1664 | GLint status; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1665 | |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame] | 1666 | s = glCreateShader(type); |
| 1667 | glShaderSource(s, 1, &source, NULL); |
| 1668 | glCompileShader(s); |
| 1669 | glGetShaderiv(s, GL_COMPILE_STATUS, &status); |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1670 | if (!status) { |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame] | 1671 | glGetShaderInfoLog(s, sizeof msg, NULL, msg); |
| 1672 | fprintf(stderr, "shader info: %s\n", msg); |
| 1673 | return GL_NONE; |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1674 | } |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1675 | |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame] | 1676 | return s; |
| 1677 | } |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1678 | |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame] | 1679 | static int |
| 1680 | wlsc_shader_init(struct wlsc_shader *shader, |
| 1681 | const char *vertex_source, const char *fragment_source) |
| 1682 | { |
| 1683 | char msg[512]; |
| 1684 | GLint status; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1685 | |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame] | 1686 | shader->vertex_shader = |
| 1687 | compile_shader(GL_VERTEX_SHADER, vertex_source); |
| 1688 | shader->fragment_shader = |
| 1689 | compile_shader(GL_FRAGMENT_SHADER, fragment_source); |
| 1690 | |
| 1691 | shader->program = glCreateProgram(); |
| 1692 | glAttachShader(shader->program, shader->vertex_shader); |
| 1693 | glAttachShader(shader->program, shader->fragment_shader); |
| 1694 | glBindAttribLocation(shader->program, 0, "position"); |
| 1695 | glBindAttribLocation(shader->program, 1, "texcoord"); |
| 1696 | |
| 1697 | glLinkProgram(shader->program); |
| 1698 | glGetProgramiv(shader->program, GL_LINK_STATUS, &status); |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1699 | if (!status) { |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame] | 1700 | glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg); |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1701 | fprintf(stderr, "link info: %s\n", msg); |
| 1702 | return -1; |
| 1703 | } |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1704 | |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame] | 1705 | shader->proj_uniform = glGetUniformLocation(shader->program, "proj"); |
| 1706 | shader->tex_uniform = glGetUniformLocation(shader->program, "tex"); |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1707 | |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1708 | return 0; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1709 | } |
| 1710 | |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 1711 | static int |
| 1712 | init_solid_shader(struct wlsc_shader *shader, |
| 1713 | GLuint vertex_shader, const char *fragment_source) |
| 1714 | { |
| 1715 | GLint status; |
| 1716 | char msg[512]; |
| 1717 | |
| 1718 | shader->vertex_shader = vertex_shader; |
| 1719 | shader->fragment_shader = |
| 1720 | compile_shader(GL_FRAGMENT_SHADER, fragment_source); |
| 1721 | |
| 1722 | shader->program = glCreateProgram(); |
| 1723 | glAttachShader(shader->program, shader->vertex_shader); |
| 1724 | glAttachShader(shader->program, shader->fragment_shader); |
| 1725 | glBindAttribLocation(shader->program, 0, "position"); |
| 1726 | glBindAttribLocation(shader->program, 1, "texcoord"); |
| 1727 | |
| 1728 | glLinkProgram(shader->program); |
| 1729 | glGetProgramiv(shader->program, GL_LINK_STATUS, &status); |
| 1730 | if (!status) { |
| 1731 | glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg); |
| 1732 | fprintf(stderr, "link info: %s\n", msg); |
| 1733 | return -1; |
| 1734 | } |
| 1735 | |
| 1736 | shader->proj_uniform = glGetUniformLocation(shader->program, "proj"); |
| 1737 | shader->color_uniform = glGetUniformLocation(shader->program, "color"); |
| 1738 | |
| 1739 | return 0; |
| 1740 | } |
| 1741 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1742 | void |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1743 | wlsc_output_destroy(struct wlsc_output *output) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1744 | { |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1745 | destroy_surface(&output->background->surface.resource, NULL); |
| 1746 | } |
| 1747 | |
| 1748 | void |
| 1749 | wlsc_output_move(struct wlsc_output *output, int x, int y) |
| 1750 | { |
| 1751 | struct wlsc_compositor *c = output->compositor; |
Benjamin Franzke | 1b765ff | 2011-02-18 16:51:37 +0100 | [diff] [blame] | 1752 | int flip; |
| 1753 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1754 | output->x = x; |
| 1755 | output->y = y; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1756 | |
Benjamin Franzke | 264b3f9 | 2011-03-16 13:48:42 +0100 | [diff] [blame] | 1757 | if (output->background) { |
| 1758 | output->background->x = x; |
| 1759 | output->background->y = y; |
| 1760 | wlsc_surface_update_matrix(output->background); |
| 1761 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1762 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 1763 | pixman_region32_init(&output->previous_damage_region); |
| 1764 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1765 | wlsc_matrix_init(&output->matrix); |
| 1766 | wlsc_matrix_translate(&output->matrix, |
| 1767 | -output->x - output->width / 2.0, |
| 1768 | -output->y - output->height / 2.0, 0); |
Benjamin Franzke | 1b765ff | 2011-02-18 16:51:37 +0100 | [diff] [blame] | 1769 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1770 | flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1771 | wlsc_matrix_scale(&output->matrix, |
Benjamin Franzke | 1b765ff | 2011-02-18 16:51:37 +0100 | [diff] [blame] | 1772 | 2.0 / output->width, |
| 1773 | flip * 2.0 / output->height, 1); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1774 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1775 | pixman_region32_union_rect(&c->damage_region, |
| 1776 | &c->damage_region, |
| 1777 | x, y, output->width, output->height); |
| 1778 | } |
| 1779 | |
| 1780 | void |
| 1781 | wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c, |
| 1782 | int x, int y, int width, int height, uint32_t flags) |
| 1783 | { |
| 1784 | output->compositor = c; |
| 1785 | output->x = x; |
| 1786 | output->y = y; |
| 1787 | output->width = width; |
| 1788 | output->height = height; |
| 1789 | |
| 1790 | output->background = |
| 1791 | background_create(output, option_background); |
| 1792 | |
| 1793 | output->flags = flags; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 1794 | output->finished = 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1795 | wlsc_output_move(output, x, y); |
| 1796 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1797 | output->object.interface = &wl_output_interface; |
| 1798 | wl_display_add_object(c->wl_display, &output->object); |
| 1799 | wl_display_add_global(c->wl_display, &output->object, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1800 | wlsc_output_post_geometry); |
| 1801 | } |
| 1802 | |
Benjamin Franzke | 315b3dc | 2011-03-08 11:32:57 +0100 | [diff] [blame] | 1803 | static void |
| 1804 | shm_buffer_created(struct wl_buffer *buffer) |
| 1805 | { |
| 1806 | struct wl_list *surfaces_attached_to; |
| 1807 | |
| 1808 | surfaces_attached_to = malloc(sizeof *surfaces_attached_to); |
| 1809 | if (!surfaces_attached_to) { |
| 1810 | buffer->user_data = NULL; |
| 1811 | return; |
| 1812 | } |
| 1813 | |
| 1814 | wl_list_init(surfaces_attached_to); |
| 1815 | |
| 1816 | buffer->user_data = surfaces_attached_to; |
| 1817 | } |
| 1818 | |
| 1819 | static void |
| 1820 | shm_buffer_damaged(struct wl_buffer *buffer, |
| 1821 | int32_t x, int32_t y, int32_t width, int32_t height) |
| 1822 | { |
| 1823 | struct wl_list *surfaces_attached_to = buffer->user_data; |
| 1824 | struct wlsc_surface *es; |
Benjamin Franzke | fab5ec1 | 2011-04-25 19:44:47 +0200 | [diff] [blame] | 1825 | GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4; |
Benjamin Franzke | 315b3dc | 2011-03-08 11:32:57 +0100 | [diff] [blame] | 1826 | |
| 1827 | wl_list_for_each(es, surfaces_attached_to, buffer_link) { |
| 1828 | glBindTexture(GL_TEXTURE_2D, es->texture); |
| 1829 | glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, |
Benjamin Franzke | fab5ec1 | 2011-04-25 19:44:47 +0200 | [diff] [blame] | 1830 | tex_width, buffer->height, 0, |
Benjamin Franzke | 315b3dc | 2011-03-08 11:32:57 +0100 | [diff] [blame] | 1831 | GL_BGRA_EXT, GL_UNSIGNED_BYTE, |
| 1832 | wl_shm_buffer_get_data(buffer)); |
| 1833 | /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't |
| 1834 | * support any unpack attributes except GL_UNPACK_ALIGNMENT. */ |
| 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | static void |
| 1839 | shm_buffer_destroyed(struct wl_buffer *buffer) |
| 1840 | { |
| 1841 | struct wl_list *surfaces_attached_to = buffer->user_data; |
| 1842 | struct wlsc_surface *es, *next; |
| 1843 | |
| 1844 | wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) { |
| 1845 | es->buffer = NULL; |
| 1846 | wl_list_remove(&es->buffer_link); |
| 1847 | } |
| 1848 | |
| 1849 | free(surfaces_attached_to); |
| 1850 | } |
| 1851 | |
| 1852 | const static struct wl_shm_callbacks shm_callbacks = { |
| 1853 | shm_buffer_created, |
| 1854 | shm_buffer_damaged, |
| 1855 | shm_buffer_destroyed |
| 1856 | }; |
| 1857 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1858 | int |
| 1859 | wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display) |
| 1860 | { |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 1861 | struct wl_event_loop *loop; |
Kristian Høgsberg | d711d0c | 2011-01-14 17:28:21 -0500 | [diff] [blame] | 1862 | const char *extensions; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1863 | |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 1864 | ec->wl_display = display; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1865 | |
Kristian Høgsberg | c5c510e | 2010-12-08 15:12:58 -0500 | [diff] [blame] | 1866 | wl_compositor_init(&ec->compositor, &compositor_interface, display); |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 1867 | |
Benjamin Franzke | 315b3dc | 2011-03-08 11:32:57 +0100 | [diff] [blame] | 1868 | ec->shm = wl_shm_init(display, &shm_callbacks); |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame^] | 1869 | |
| 1870 | ec->image_target_texture_2d = |
| 1871 | (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES"); |
| 1872 | ec->image_target_renderbuffer_storage = (void *) |
| 1873 | eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES"); |
| 1874 | ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR"); |
| 1875 | ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR"); |
| 1876 | ec->bind_display = |
| 1877 | (void *) eglGetProcAddress("eglBindWaylandDisplayWL"); |
| 1878 | ec->unbind_display = |
| 1879 | (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL"); |
| 1880 | |
| 1881 | extensions = (const char *) glGetString(GL_EXTENSIONS); |
| 1882 | if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) { |
| 1883 | fprintf(stderr, |
| 1884 | "GL_EXT_texture_format_BGRA8888 not available\n"); |
| 1885 | return -1; |
| 1886 | } |
| 1887 | |
| 1888 | extensions = |
| 1889 | (const char *) eglQueryString(ec->display, EGL_EXTENSIONS); |
| 1890 | if (strstr(extensions, "EGL_WL_bind_wayland_display")) |
| 1891 | ec->has_bind_display = 1; |
| 1892 | if (ec->has_bind_display) |
| 1893 | ec->bind_display(ec->display, ec->wl_display); |
Kristian Høgsberg | 3d5bae0 | 2010-10-06 21:17:40 -0400 | [diff] [blame] | 1894 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 1895 | wl_list_init(&ec->surface_list); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1896 | wl_list_init(&ec->input_device_list); |
| 1897 | wl_list_init(&ec->output_list); |
Kristian Høgsberg | 3555d09 | 2011-04-11 13:58:13 -0400 | [diff] [blame] | 1898 | wl_list_init(&ec->binding_list); |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 1899 | wl_list_init(&ec->animation_list); |
| 1900 | wlsc_tweener_init(&ec->fade.tweener, 0.8, 0.0, 0.0); |
| 1901 | ec->fade.animation.frame = fade_frame; |
| 1902 | wl_list_init(&ec->fade.animation.link); |
Kristian Høgsberg | 3555d09 | 2011-04-11 13:58:13 -0400 | [diff] [blame] | 1903 | |
Kristian Høgsberg | a8ec863 | 2011-04-12 17:22:49 -0400 | [diff] [blame] | 1904 | wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0, |
Kristian Høgsberg | 3555d09 | 2011-04-11 13:58:13 -0400 | [diff] [blame] | 1905 | MODIFIER_CTRL | MODIFIER_ALT, |
| 1906 | terminate_binding, ec); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1907 | |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 1908 | create_pointer_images(ec); |
| 1909 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1910 | screenshooter_create(ec); |
| 1911 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1912 | glActiveTexture(GL_TEXTURE0); |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame] | 1913 | |
| 1914 | if (wlsc_shader_init(&ec->texture_shader, |
| 1915 | vertex_shader, texture_fragment_shader) < 0) |
Kristian Høgsberg | a946821 | 2010-06-14 21:03:11 -0400 | [diff] [blame] | 1916 | return -1; |
Kristian Høgsberg | cce1aec | 2011-04-22 15:38:14 -0400 | [diff] [blame] | 1917 | if (init_solid_shader(&ec->solid_shader, |
| 1918 | ec->texture_shader.vertex_shader, |
| 1919 | solid_fragment_shader) < 0) |
| 1920 | return -1; |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 1921 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 1922 | loop = wl_display_get_event_loop(ec->wl_display); |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1923 | ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec); |
| 1924 | wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000); |
| 1925 | |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 1926 | ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec); |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 1927 | pixman_region32_init(&ec->damage_region); |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 1928 | wlsc_compositor_schedule_repaint(ec); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 1929 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1930 | return 0; |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1931 | } |
| 1932 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1933 | static int on_term_signal(int signal_number, void *data) |
Kristian Høgsberg | 50dc698 | 2010-12-01 16:43:56 -0500 | [diff] [blame] | 1934 | { |
| 1935 | struct wlsc_compositor *ec = data; |
| 1936 | |
| 1937 | wl_display_terminate(ec->wl_display); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1938 | |
| 1939 | return 1; |
Kristian Høgsberg | 50dc698 | 2010-12-01 16:43:56 -0500 | [diff] [blame] | 1940 | } |
| 1941 | |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1942 | int main(int argc, char *argv[]) |
| 1943 | { |
| 1944 | struct wl_display *display; |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1945 | struct wlsc_compositor *ec; |
Kristian Høgsberg | 50dc698 | 2010-12-01 16:43:56 -0500 | [diff] [blame] | 1946 | struct wl_event_loop *loop; |
Kristian Høgsberg | d653126 | 2008-12-12 11:06:18 -0500 | [diff] [blame] | 1947 | GError *error = NULL; |
| 1948 | GOptionContext *context; |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 1949 | int width, height; |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 1950 | void *shell_module; |
| 1951 | int (*shell_init)(struct wlsc_compositor *ec); |
Kristian Høgsberg | d653126 | 2008-12-12 11:06:18 -0500 | [diff] [blame] | 1952 | |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 1953 | g_type_init(); /* GdkPixbuf needs this, it seems. */ |
| 1954 | |
Kristian Høgsberg | d653126 | 2008-12-12 11:06:18 -0500 | [diff] [blame] | 1955 | context = g_option_context_new(NULL); |
| 1956 | g_option_context_add_main_entries(context, option_entries, "Wayland"); |
| 1957 | if (!g_option_context_parse(context, &argc, &argv, &error)) { |
| 1958 | fprintf(stderr, "option parsing failed: %s\n", error->message); |
| 1959 | exit(EXIT_FAILURE); |
| 1960 | } |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 1961 | if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) { |
| 1962 | fprintf(stderr, "invalid geometry option: %s \n", |
| 1963 | option_geometry); |
| 1964 | exit(EXIT_FAILURE); |
| 1965 | } |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1966 | |
| 1967 | display = wl_display_create(); |
| 1968 | |
Kristian Høgsberg | a941022 | 2011-01-14 17:22:35 -0500 | [diff] [blame] | 1969 | ec = NULL; |
| 1970 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 1971 | shell_init = desktop_shell_init; |
| 1972 | if (option_shell) { |
| 1973 | shell_module = dlopen(option_shell, RTLD_LAZY); |
| 1974 | if (!shell_module) { |
| 1975 | fprintf(stderr, "failed to load shell module: %m\n"); |
| 1976 | exit(EXIT_FAILURE); |
| 1977 | } |
| 1978 | shell_init = dlsym(shell_module, "shell_init"); |
| 1979 | if (!shell_init) { |
| 1980 | fprintf(stderr, |
| 1981 | "failed to lookup shell init function: %m\n"); |
| 1982 | exit(EXIT_FAILURE); |
| 1983 | } |
| 1984 | } |
| 1985 | |
Kristian Høgsberg | a941022 | 2011-01-14 17:22:35 -0500 | [diff] [blame] | 1986 | #if BUILD_WAYLAND_COMPOSITOR |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1987 | if (getenv("WAYLAND_DISPLAY")) |
| 1988 | ec = wayland_compositor_create(display, width, height); |
Kristian Høgsberg | a941022 | 2011-01-14 17:22:35 -0500 | [diff] [blame] | 1989 | #endif |
| 1990 | |
| 1991 | #if BUILD_X11_COMPOSITOR |
| 1992 | if (ec == NULL && getenv("DISPLAY")) |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 1993 | ec = x11_compositor_create(display, width, height); |
Kristian Høgsberg | a941022 | 2011-01-14 17:22:35 -0500 | [diff] [blame] | 1994 | #endif |
| 1995 | |
Benjamin Franzke | 5d00709 | 2011-04-04 00:30:25 +0200 | [diff] [blame] | 1996 | #if BUILD_OPENWFD_COMPOSITOR |
| 1997 | if (ec == NULL && getenv("OPENWFD")) |
| 1998 | ec = wfd_compositor_create(display, option_connector); |
| 1999 | #endif |
| 2000 | |
Kristian Høgsberg | a941022 | 2011-01-14 17:22:35 -0500 | [diff] [blame] | 2001 | #if BUILD_DRM_COMPOSITOR |
| 2002 | if (ec == NULL) |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 2003 | ec = drm_compositor_create(display, option_connector); |
Kristian Høgsberg | a941022 | 2011-01-14 17:22:35 -0500 | [diff] [blame] | 2004 | #endif |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 2005 | |
Kristian Høgsberg | 841883b | 2008-12-05 11:19:56 -0500 | [diff] [blame] | 2006 | if (ec == NULL) { |
| 2007 | fprintf(stderr, "failed to create compositor\n"); |
| 2008 | exit(EXIT_FAILURE); |
| 2009 | } |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 2010 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 2011 | if (shell_init(ec) < 0) |
| 2012 | exit(EXIT_FAILURE); |
| 2013 | |
Kristian Høgsberg | 2bb3ebe | 2010-12-01 15:36:20 -0500 | [diff] [blame] | 2014 | if (wl_display_add_socket(display, option_socket_name)) { |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 2015 | fprintf(stderr, "failed to add socket: %m\n"); |
| 2016 | exit(EXIT_FAILURE); |
| 2017 | } |
| 2018 | |
Kristian Høgsberg | 50dc698 | 2010-12-01 16:43:56 -0500 | [diff] [blame] | 2019 | loop = wl_display_get_event_loop(ec->wl_display); |
| 2020 | wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec); |
| 2021 | wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec); |
| 2022 | |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 2023 | wl_display_run(display); |
| 2024 | |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame^] | 2025 | if (ec->has_bind_display) |
| 2026 | ec->unbind_display(ec->display, display); |
Kristian Høgsberg | 2bb3ebe | 2010-12-01 15:36:20 -0500 | [diff] [blame] | 2027 | wl_display_destroy(display); |
| 2028 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2029 | ec->destroy(ec); |
| 2030 | |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 2031 | return 0; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 2032 | } |