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