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