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