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> |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 27 | #include <stdarg.h> |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 28 | #include <sys/ioctl.h> |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 29 | #include <fcntl.h> |
| 30 | #include <unistd.h> |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 31 | #include <gdk-pixbuf/gdk-pixbuf.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 | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 34 | |
Kristian Høgsberg | a1f3f60 | 2010-08-03 09:26:44 -0400 | [diff] [blame] | 35 | #include "wayland-server-protocol.h" |
Kristian Høgsberg | 8286302 | 2010-06-04 21:52:02 -0400 | [diff] [blame] | 36 | #include "compositor.h" |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 37 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 38 | /* The plan here is to generate a random anonymous socket name and |
| 39 | * advertise that through a service on the session dbus. |
| 40 | */ |
Kristian Høgsberg | 2bb3ebe | 2010-12-01 15:36:20 -0500 | [diff] [blame] | 41 | static const char *option_socket_name = NULL; |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 42 | static const char *option_background = "background.jpg"; |
| 43 | static const char *option_geometry = "1024x640"; |
| 44 | static int option_connector = 0; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 45 | |
| 46 | static const GOptionEntry option_entries[] = { |
| 47 | { "background", 'b', 0, G_OPTION_ARG_STRING, |
| 48 | &option_background, "Background image" }, |
Kristian Høgsberg | f5878fa | 2009-09-18 17:02:41 -0400 | [diff] [blame] | 49 | { "connector", 'c', 0, G_OPTION_ARG_INT, |
| 50 | &option_connector, "KMS connector" }, |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 51 | { "geometry", 'g', 0, G_OPTION_ARG_STRING, |
| 52 | &option_geometry, "Geometry" }, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 53 | { "socket", 's', 0, G_OPTION_ARG_STRING, |
| 54 | &option_socket_name, "Socket Name" }, |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 55 | { NULL } |
| 56 | }; |
| 57 | |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 58 | static void |
| 59 | wlsc_matrix_init(struct wlsc_matrix *matrix) |
| 60 | { |
| 61 | static const struct wlsc_matrix identity = { |
| 62 | { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } |
| 63 | }; |
| 64 | |
| 65 | memcpy(matrix, &identity, sizeof identity); |
| 66 | } |
| 67 | |
| 68 | static void |
| 69 | wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n) |
| 70 | { |
| 71 | struct wlsc_matrix tmp; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 72 | const GLfloat *row, *column; |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 73 | div_t d; |
| 74 | int i, j; |
| 75 | |
| 76 | for (i = 0; i < 16; i++) { |
| 77 | tmp.d[i] = 0; |
| 78 | d = div(i, 4); |
| 79 | row = m->d + d.quot * 4; |
| 80 | column = n->d + d.rem; |
| 81 | for (j = 0; j < 4; j++) |
| 82 | tmp.d[i] += row[j] * column[j * 4]; |
| 83 | } |
| 84 | memcpy(m, &tmp, sizeof tmp); |
| 85 | } |
| 86 | |
| 87 | static void |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 88 | 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] | 89 | { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 90 | struct wlsc_matrix translate = { |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 91 | { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 } |
| 92 | }; |
| 93 | |
| 94 | wlsc_matrix_multiply(matrix, &translate); |
| 95 | } |
| 96 | |
| 97 | static void |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 98 | 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] | 99 | { |
| 100 | struct wlsc_matrix scale = { |
| 101 | { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 } |
| 102 | }; |
| 103 | |
| 104 | wlsc_matrix_multiply(matrix, &scale); |
| 105 | } |
| 106 | |
| 107 | static void |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 108 | wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v) |
| 109 | { |
| 110 | int i, j; |
Kristian Høgsberg | 0b8646b | 2010-06-08 15:29:14 -0400 | [diff] [blame] | 111 | struct wlsc_vector t; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 112 | |
| 113 | for (i = 0; i < 4; i++) { |
Kristian Høgsberg | 0b8646b | 2010-06-08 15:29:14 -0400 | [diff] [blame] | 114 | t.f[i] = 0; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 115 | for (j = 0; j < 4; j++) |
Kristian Høgsberg | 0b8646b | 2010-06-08 15:29:14 -0400 | [diff] [blame] | 116 | t.f[i] += v->f[j] * matrix->d[i + j * 4]; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 117 | } |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 118 | |
Kristian Høgsberg | 0b8646b | 2010-06-08 15:29:14 -0400 | [diff] [blame] | 119 | *v = t; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 120 | } |
| 121 | |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 122 | static struct wlsc_surface * |
| 123 | wlsc_surface_create(struct wlsc_compositor *compositor, |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 124 | 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] | 125 | { |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 126 | struct wlsc_surface *surface; |
| 127 | |
| 128 | surface = malloc(sizeof *surface); |
| 129 | if (surface == NULL) |
| 130 | return NULL; |
| 131 | |
Kristian Høgsberg | c551bd2 | 2010-12-06 16:43:16 -0500 | [diff] [blame] | 132 | wl_list_init(&surface->surface.destroy_listener_list); |
Kristian Høgsberg | f6b1471 | 2011-01-06 15:32:14 -0500 | [diff] [blame] | 133 | wl_list_init(&surface->link); |
Kristian Høgsberg | 8f66a57 | 2011-01-07 08:38:56 -0500 | [diff] [blame] | 134 | surface->mapped = 0; |
Kristian Høgsberg | c551bd2 | 2010-12-06 16:43:16 -0500 | [diff] [blame] | 135 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 136 | glGenTextures(1, &surface->texture); |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 137 | glBindTexture(GL_TEXTURE_2D, surface->texture); |
| 138 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 139 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 140 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 141 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 142 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 143 | surface->compositor = compositor; |
Kristian Høgsberg | c5d6be9 | 2011-01-14 16:22:37 -0500 | [diff] [blame] | 144 | surface->visual = NULL; |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 145 | surface->x = x; |
| 146 | surface->y = y; |
| 147 | surface->width = width; |
| 148 | surface->height = height; |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 149 | wlsc_matrix_init(&surface->matrix); |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 150 | wlsc_matrix_scale(&surface->matrix, width, height, 1); |
| 151 | wlsc_matrix_translate(&surface->matrix, x, y, 0); |
| 152 | |
| 153 | wlsc_matrix_init(&surface->matrix_inv); |
| 154 | wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0); |
| 155 | wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 156 | |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 157 | return surface; |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 158 | } |
| 159 | |
Kristian Høgsberg | 7132a9a | 2010-12-06 21:41:10 -0500 | [diff] [blame] | 160 | static uint32_t |
| 161 | get_time(void) |
| 162 | { |
| 163 | struct timeval tv; |
| 164 | |
| 165 | gettimeofday(&tv, NULL); |
| 166 | |
| 167 | return tv.tv_sec * 1000 + tv.tv_usec / 1000; |
| 168 | } |
| 169 | |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 170 | static void |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 171 | destroy_surface(struct wl_resource *resource, struct wl_client *client) |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 172 | { |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 173 | struct wlsc_surface *surface = |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 174 | container_of(resource, struct wlsc_surface, surface.resource); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 175 | struct wlsc_compositor *compositor = surface->compositor; |
Kristian Høgsberg | c551bd2 | 2010-12-06 16:43:16 -0500 | [diff] [blame] | 176 | struct wl_listener *l, *next; |
Kristian Høgsberg | 4685fa3 | 2010-12-06 21:38:50 -0500 | [diff] [blame] | 177 | uint32_t time; |
Kristian Høgsberg | 4fa4873 | 2009-03-10 23:17:00 -0400 | [diff] [blame] | 178 | |
Kristian Høgsberg | 3f8f39c | 2009-09-18 17:05:13 -0400 | [diff] [blame] | 179 | wl_list_remove(&surface->link); |
| 180 | glDeleteTextures(1, &surface->texture); |
Kristian Høgsberg | 3f8f39c | 2009-09-18 17:05:13 -0400 | [diff] [blame] | 181 | |
Kristian Høgsberg | 7132a9a | 2010-12-06 21:41:10 -0500 | [diff] [blame] | 182 | time = get_time(); |
Kristian Høgsberg | c551bd2 | 2010-12-06 16:43:16 -0500 | [diff] [blame] | 183 | wl_list_for_each_safe(l, next, |
| 184 | &surface->surface.destroy_listener_list, link) |
Kristian Høgsberg | 4685fa3 | 2010-12-06 21:38:50 -0500 | [diff] [blame] | 185 | l->func(l, &surface->surface, time); |
Kristian Høgsberg | 4fa4873 | 2009-03-10 23:17:00 -0400 | [diff] [blame] | 186 | |
Kristian Høgsberg | 4fa4873 | 2009-03-10 23:17:00 -0400 | [diff] [blame] | 187 | free(surface); |
Kristian Høgsberg | 117d513 | 2010-08-11 08:56:47 -0400 | [diff] [blame] | 188 | |
| 189 | wlsc_compositor_schedule_repaint(compositor); |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 190 | } |
| 191 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 192 | static struct wl_buffer * |
| 193 | create_buffer_from_png(struct wlsc_compositor *ec, |
| 194 | const char *filename, int width, int height) |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 195 | { |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 196 | GdkPixbuf *pixbuf; |
| 197 | GError *error = NULL; |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 198 | int stride, i, n_channels; |
| 199 | unsigned char *pixels, *end, *argb_pixels, *s, *d; |
| 200 | struct wl_buffer *buffer; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 201 | |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 202 | pixbuf = gdk_pixbuf_new_from_file_at_scale(filename, |
| 203 | width, height, |
| 204 | FALSE, &error); |
| 205 | if (error != NULL) |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 206 | return NULL; |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 207 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 208 | stride = gdk_pixbuf_get_rowstride(pixbuf); |
| 209 | pixels = gdk_pixbuf_get_pixels(pixbuf); |
| 210 | n_channels = gdk_pixbuf_get_n_channels(pixbuf); |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 211 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 212 | argb_pixels = malloc (height * width * 4); |
| 213 | if (argb_pixels == NULL) { |
| 214 | gdk_pixbuf_unref(pixbuf); |
| 215 | return NULL; |
| 216 | } |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 217 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 218 | if (n_channels == 4) { |
| 219 | for (i = 0; i < height; i++) { |
| 220 | s = pixels + i * stride; |
| 221 | end = s + width * 4; |
| 222 | d = argb_pixels + i * width * 4; |
| 223 | while (s < end) { |
| 224 | unsigned int t; |
| 225 | |
| 226 | #define MULT(_d,c,a,t) \ |
| 227 | do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0) |
| 228 | |
| 229 | MULT(d[0], s[2], s[3], t); |
| 230 | MULT(d[1], s[1], s[3], t); |
| 231 | MULT(d[2], s[0], s[3], t); |
| 232 | d[3] = s[3]; |
| 233 | s += 4; |
| 234 | d += 4; |
| 235 | } |
| 236 | } |
| 237 | } else if (n_channels == 3) { |
| 238 | for (i = 0; i < height; i++) { |
| 239 | s = pixels + i * stride; |
| 240 | end = s + width * 3; |
| 241 | d = argb_pixels + i * width * 4; |
| 242 | while (s < end) { |
| 243 | d[0] = s[2]; |
| 244 | d[1] = s[1]; |
| 245 | d[2] = s[0]; |
| 246 | d[3] = 0xff; |
| 247 | s += 3; |
| 248 | d += 4; |
| 249 | } |
| 250 | } |
| 251 | } |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 252 | |
| 253 | gdk_pixbuf_unref(pixbuf); |
| 254 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 255 | buffer = ec->create_buffer(ec, width, height, |
| 256 | &ec->compositor.premultiplied_argb_visual, |
| 257 | argb_pixels); |
| 258 | |
| 259 | free(argb_pixels); |
| 260 | |
| 261 | return buffer; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 262 | } |
| 263 | |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 264 | static const struct { |
| 265 | const char *filename; |
| 266 | int hotspot_x, hotspot_y; |
| 267 | } pointer_images[] = { |
Kristian Høgsberg | 4219a40 | 2010-08-16 16:43:03 -0400 | [diff] [blame] | 268 | { DATADIR "/wayland/bottom_left_corner.png", 6, 30 }, |
| 269 | { DATADIR "/wayland/bottom_right_corner.png", 28, 28 }, |
| 270 | { DATADIR "/wayland/bottom_side.png", 16, 20 }, |
| 271 | { DATADIR "/wayland/grabbing.png", 20, 17 }, |
| 272 | { DATADIR "/wayland/left_ptr.png", 10, 5 }, |
| 273 | { DATADIR "/wayland/left_side.png", 10, 20 }, |
| 274 | { DATADIR "/wayland/right_side.png", 30, 19 }, |
| 275 | { DATADIR "/wayland/top_left_corner.png", 8, 8 }, |
| 276 | { DATADIR "/wayland/top_right_corner.png", 26, 8 }, |
| 277 | { DATADIR "/wayland/top_side.png", 18, 8 }, |
| 278 | { DATADIR "/wayland/xterm.png", 15, 15 } |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | static void |
| 282 | create_pointer_images(struct wlsc_compositor *ec) |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 283 | { |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 284 | int i, count; |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 285 | const int width = 32, height = 32; |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 286 | |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 287 | count = ARRAY_LENGTH(pointer_images); |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 288 | ec->pointer_buffers = malloc(count * sizeof *ec->pointer_buffers); |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 289 | for (i = 0; i < count; i++) { |
Kristian Høgsberg | 3d5bae0 | 2010-10-06 21:17:40 -0400 | [diff] [blame] | 290 | ec->pointer_buffers[i] = |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 291 | create_buffer_from_png(ec, |
| 292 | pointer_images[i].filename, |
| 293 | width, height); |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 294 | } |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 295 | } |
| 296 | |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 297 | static struct wlsc_surface * |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 298 | background_create(struct wlsc_output *output, const char *filename) |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 299 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 300 | struct wlsc_surface *background; |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 301 | struct wl_buffer *buffer; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 302 | |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 303 | background = wlsc_surface_create(output->compositor, |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 304 | output->x, output->y, |
| 305 | output->width, output->height); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 306 | if (background == NULL) |
| 307 | return NULL; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 308 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 309 | buffer = create_buffer_from_png(output->compositor, |
| 310 | filename, |
| 311 | output->width, output->height); |
Benjamin Franzke | d3b023e | 2011-01-15 12:34:49 +0100 | [diff] [blame^] | 312 | if (buffer == NULL) { |
| 313 | free(background); |
| 314 | return NULL; |
| 315 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 316 | buffer->attach(buffer, &background->surface); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 317 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 318 | return background; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static void |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 322 | wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output) |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 323 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 324 | struct wlsc_compositor *ec = es->compositor; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 325 | struct wlsc_matrix tmp; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 326 | |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 327 | tmp = es->matrix; |
| 328 | wlsc_matrix_multiply(&tmp, &output->matrix); |
| 329 | glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d); |
| 330 | glUniform1i(ec->tex_uniform, 0); |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 331 | |
Kristian Høgsberg | c5c510e | 2010-12-08 15:12:58 -0500 | [diff] [blame] | 332 | if (es->visual == &ec->compositor.argb_visual) { |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 333 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 334 | glEnable(GL_BLEND); |
Kristian Høgsberg | c5c510e | 2010-12-08 15:12:58 -0500 | [diff] [blame] | 335 | } else if (es->visual == &ec->compositor.premultiplied_argb_visual) { |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 336 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 337 | glEnable(GL_BLEND); |
| 338 | } else { |
| 339 | glDisable(GL_BLEND); |
| 340 | } |
| 341 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 342 | glBindTexture(GL_TEXTURE_2D, es->texture); |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 343 | glBindBuffer(GL_ARRAY_BUFFER, ec->vbo); |
| 344 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, |
| 345 | 5 * sizeof(GLfloat), NULL); |
| 346 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, |
| 347 | 5 * sizeof(GLfloat), (GLfloat *) 0 + 3); |
| 348 | glEnableVertexAttribArray(0); |
| 349 | glEnableVertexAttribArray(1); |
| 350 | glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static void |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 354 | wlsc_surface_raise(struct wlsc_surface *surface) |
| 355 | { |
| 356 | struct wlsc_compositor *compositor = surface->compositor; |
| 357 | |
| 358 | wl_list_remove(&surface->link); |
Kristian Høgsberg | 747638b | 2010-07-12 17:06:06 -0400 | [diff] [blame] | 359 | wl_list_insert(&compositor->surface_list, &surface->link); |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static void |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 363 | wlsc_surface_update_matrix(struct wlsc_surface *es) |
| 364 | { |
| 365 | wlsc_matrix_init(&es->matrix); |
| 366 | wlsc_matrix_scale(&es->matrix, es->width, es->height, 1); |
| 367 | wlsc_matrix_translate(&es->matrix, es->x, es->y, 0); |
| 368 | |
| 369 | wlsc_matrix_init(&es->matrix_inv); |
| 370 | wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0); |
| 371 | wlsc_matrix_scale(&es->matrix_inv, |
| 372 | 1.0 / es->width, 1.0 / es->height, 1); |
| 373 | } |
| 374 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 375 | void |
| 376 | wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs) |
Kristian Høgsberg | 01f941b | 2009-05-27 17:47:15 -0400 | [diff] [blame] | 377 | { |
Kristian Høgsberg | 9d69f8e | 2010-09-03 14:46:38 -0400 | [diff] [blame] | 378 | wl_display_post_frame(compositor->wl_display, msecs); |
Kristian Høgsberg | 5d312db | 2009-09-12 16:57:02 -0400 | [diff] [blame] | 379 | wl_event_source_timer_update(compositor->timer_source, 5); |
Kristian Høgsberg | 01f941b | 2009-05-27 17:47:15 -0400 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static void |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 383 | wlsc_output_repaint(struct wlsc_output *output) |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 384 | { |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 385 | struct wlsc_compositor *ec = output->compositor; |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 386 | struct wlsc_surface *es; |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 387 | struct wlsc_input_device *eid; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 388 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 389 | glViewport(0, 0, output->width, output->height); |
Kristian Høgsberg | fdec236 | 2001-01-01 22:23:51 -0500 | [diff] [blame] | 390 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 391 | if (output->background) |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 392 | wlsc_surface_draw(output->background, output); |
Kristian Høgsberg | 5b7f832 | 2008-12-18 12:08:19 -0500 | [diff] [blame] | 393 | else |
| 394 | glClear(GL_COLOR_BUFFER_BIT); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 395 | |
Kristian Høgsberg | 747638b | 2010-07-12 17:06:06 -0400 | [diff] [blame] | 396 | wl_list_for_each_reverse(es, &ec->surface_list, link) |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 397 | wlsc_surface_draw(es, output); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 398 | |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 399 | if (ec->focus) |
| 400 | wl_list_for_each(eid, &ec->input_device_list, link) |
| 401 | wlsc_surface_draw(eid->sprite, output); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | static void |
| 405 | repaint(void *data) |
| 406 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 407 | struct wlsc_compositor *ec = data; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 408 | struct wlsc_output *output; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 409 | |
| 410 | if (!ec->repaint_needed) { |
| 411 | ec->repaint_on_timeout = 0; |
| 412 | return; |
| 413 | } |
| 414 | |
Kristian Høgsberg | a5db589 | 2010-02-26 10:28:44 -0500 | [diff] [blame] | 415 | wl_list_for_each(output, &ec->output_list, link) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 416 | wlsc_output_repaint(output); |
| 417 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 418 | ec->present(ec); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 419 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 420 | ec->repaint_needed = 0; |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 421 | } |
| 422 | |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 423 | void |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 424 | wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor) |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 425 | { |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 426 | compositor->repaint_needed = 1; |
Kristian Høgsberg | b0a167c | 2009-08-14 11:15:18 -0400 | [diff] [blame] | 427 | if (compositor->repaint_on_timeout) |
| 428 | return; |
| 429 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 430 | wl_event_source_timer_update(compositor->timer_source, 1); |
| 431 | compositor->repaint_on_timeout = 1; |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 432 | } |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 433 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 434 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 435 | surface_destroy(struct wl_client *client, |
| 436 | struct wl_surface *surface) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 437 | { |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 438 | wl_resource_destroy(&surface->resource, client); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 439 | } |
| 440 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 441 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 442 | surface_attach(struct wl_client *client, |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 443 | struct wl_surface *surface, struct wl_buffer *buffer, |
| 444 | int32_t x, int32_t y) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 445 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 446 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 447 | |
Kristian Høgsberg | 3d5bae0 | 2010-10-06 21:17:40 -0400 | [diff] [blame] | 448 | buffer->attach(buffer, surface); |
| 449 | es->buffer = buffer; |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 450 | es->x += x; |
| 451 | es->y += y; |
| 452 | es->width = buffer->width; |
| 453 | es->height = buffer->height; |
| 454 | wlsc_surface_update_matrix(es); |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 455 | } |
| 456 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 457 | static void |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 458 | surface_map_toplevel(struct wl_client *client, |
| 459 | struct wl_surface *surface) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 460 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 461 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 462 | |
Kristian Høgsberg | 8f66a57 | 2011-01-07 08:38:56 -0500 | [diff] [blame] | 463 | if (es->mapped) |
| 464 | return; |
| 465 | |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 466 | es->x = 10 + random() % 400; |
| 467 | es->y = 10 + random() % 400; |
Kristian Høgsberg | 0b8646b | 2010-06-08 15:29:14 -0400 | [diff] [blame] | 468 | |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 469 | wlsc_surface_update_matrix(es); |
Kristian Høgsberg | 052d17d | 2011-01-07 16:13:00 -0500 | [diff] [blame] | 470 | wl_list_insert(&es->compositor->surface_list, &es->link); |
Kristian Høgsberg | 9d69f8e | 2010-09-03 14:46:38 -0400 | [diff] [blame] | 471 | wlsc_compositor_schedule_repaint(es->compositor); |
Kristian Høgsberg | 8f66a57 | 2011-01-07 08:38:56 -0500 | [diff] [blame] | 472 | es->mapped = 1; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 473 | } |
| 474 | |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 475 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 476 | surface_damage(struct wl_client *client, |
| 477 | struct wl_surface *surface, |
| 478 | 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] | 479 | { |
Kristian Høgsberg | 9d69f8e | 2010-09-03 14:46:38 -0400 | [diff] [blame] | 480 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 481 | |
Kristian Høgsberg | 3d5bae0 | 2010-10-06 21:17:40 -0400 | [diff] [blame] | 482 | es->buffer->damage(es->buffer, surface, x, y, width, height); |
Kristian Høgsberg | 9d69f8e | 2010-09-03 14:46:38 -0400 | [diff] [blame] | 483 | wlsc_compositor_schedule_repaint(es->compositor); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 484 | } |
| 485 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 486 | const static struct wl_surface_interface surface_interface = { |
| 487 | surface_destroy, |
| 488 | surface_attach, |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 489 | surface_map_toplevel, |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 490 | surface_damage |
| 491 | }; |
| 492 | |
| 493 | static void |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 494 | wlsc_input_device_attach(struct wlsc_input_device *device, |
Kristian Høgsberg | 3d5bae0 | 2010-10-06 21:17:40 -0400 | [diff] [blame] | 495 | struct wl_buffer *buffer, int x, int y) |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 496 | { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 497 | struct wlsc_compositor *ec = |
| 498 | (struct wlsc_compositor *) device->input_device.compositor; |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 499 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 500 | buffer->attach(buffer, &device->sprite->surface); |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 501 | device->hotspot_x = x; |
| 502 | device->hotspot_y = y; |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 503 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 504 | device->sprite->x = device->input_device.x - device->hotspot_x; |
| 505 | device->sprite->y = device->input_device.y - device->hotspot_y; |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 506 | device->sprite->width = buffer->width; |
| 507 | device->sprite->height = buffer->height; |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 508 | wlsc_surface_update_matrix(device->sprite); |
| 509 | |
| 510 | wlsc_compositor_schedule_repaint(ec); |
| 511 | } |
| 512 | |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 513 | |
| 514 | static void |
| 515 | wlsc_input_device_set_pointer_image(struct wlsc_input_device *device, |
| 516 | enum wlsc_pointer_type type) |
| 517 | { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 518 | struct wlsc_compositor *compositor = |
| 519 | (struct wlsc_compositor *) device->input_device.compositor; |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 520 | |
| 521 | wlsc_input_device_attach(device, |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 522 | compositor->pointer_buffers[type], |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 523 | pointer_images[type].hotspot_x, |
| 524 | pointer_images[type].hotspot_y); |
| 525 | } |
| 526 | |
Kristian Høgsberg | 6d65d5f | 2010-12-07 13:30:18 -0500 | [diff] [blame] | 527 | struct wlsc_move_grab { |
| 528 | struct wl_grab grab; |
| 529 | int32_t dx, dy; |
| 530 | }; |
| 531 | |
| 532 | static void |
| 533 | move_grab_motion(struct wl_grab *grab, |
| 534 | uint32_t time, int32_t x, int32_t y) |
| 535 | { |
| 536 | struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab; |
Kristian Høgsberg | 6d65d5f | 2010-12-07 13:30:18 -0500 | [diff] [blame] | 537 | struct wlsc_surface *es = |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 538 | (struct wlsc_surface *) grab->input_device->pointer_focus; |
Kristian Høgsberg | 6d65d5f | 2010-12-07 13:30:18 -0500 | [diff] [blame] | 539 | |
| 540 | es->x = x + move->dx; |
| 541 | es->y = y + move->dy; |
Kristian Høgsberg | 6d65d5f | 2010-12-07 13:30:18 -0500 | [diff] [blame] | 542 | wlsc_surface_update_matrix(es); |
| 543 | } |
| 544 | |
| 545 | static void |
Kristian Høgsberg | b3fc757 | 2010-12-08 11:07:57 -0500 | [diff] [blame] | 546 | move_grab_button(struct wl_grab *grab, |
| 547 | uint32_t time, int32_t button, int32_t state) |
| 548 | { |
| 549 | } |
| 550 | |
| 551 | static void |
Kristian Høgsberg | 287343a | 2010-12-07 14:58:57 -0500 | [diff] [blame] | 552 | move_grab_end(struct wl_grab *grab, uint32_t time) |
Kristian Høgsberg | 6d65d5f | 2010-12-07 13:30:18 -0500 | [diff] [blame] | 553 | { |
| 554 | free(grab); |
| 555 | } |
| 556 | |
| 557 | static const struct wl_grab_interface move_grab_interface = { |
| 558 | move_grab_motion, |
Kristian Høgsberg | b3fc757 | 2010-12-08 11:07:57 -0500 | [diff] [blame] | 559 | move_grab_button, |
Kristian Høgsberg | 6d65d5f | 2010-12-07 13:30:18 -0500 | [diff] [blame] | 560 | move_grab_end |
| 561 | }; |
| 562 | |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 563 | static void |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 564 | shell_move(struct wl_client *client, struct wl_shell *shell, |
| 565 | struct wl_surface *surface, |
| 566 | struct wl_input_device *device, uint32_t time) |
| 567 | { |
| 568 | struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
Kristian Høgsberg | 6d65d5f | 2010-12-07 13:30:18 -0500 | [diff] [blame] | 569 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 570 | struct wlsc_move_grab *move; |
| 571 | |
| 572 | move = malloc(sizeof *move); |
| 573 | if (!move) { |
| 574 | wl_client_post_no_memory(client); |
| 575 | return; |
| 576 | } |
| 577 | |
| 578 | move->grab.interface = &move_grab_interface; |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 579 | move->dx = es->x - wd->input_device.grab_x; |
| 580 | move->dy = es->y - wd->input_device.grab_y; |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 581 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 582 | if (wl_input_device_update_grab(&wd->input_device, |
| 583 | &move->grab, surface, time) < 0) |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 584 | return; |
| 585 | |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 586 | wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING); |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 587 | } |
| 588 | |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 589 | struct wlsc_resize_grab { |
| 590 | struct wl_grab grab; |
| 591 | uint32_t edges; |
| 592 | int32_t dx, dy, width, height; |
| 593 | }; |
| 594 | |
| 595 | static void |
| 596 | resize_grab_motion(struct wl_grab *grab, |
| 597 | uint32_t time, int32_t x, int32_t y) |
| 598 | { |
| 599 | struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab; |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 600 | struct wl_input_device *device = grab->input_device; |
| 601 | struct wlsc_compositor *ec = |
| 602 | (struct wlsc_compositor *) device->compositor; |
| 603 | struct wl_surface *surface = device->pointer_focus; |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 604 | int32_t width, height; |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 605 | |
Kristian Høgsberg | f1ef15c | 2011-01-06 10:00:23 -0500 | [diff] [blame] | 606 | if (resize->edges & WL_GRAB_RESIZE_LEFT) { |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 607 | width = device->grab_x - x + resize->width; |
Kristian Høgsberg | f1ef15c | 2011-01-06 10:00:23 -0500 | [diff] [blame] | 608 | } else if (resize->edges & WL_GRAB_RESIZE_RIGHT) { |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 609 | width = x - device->grab_x + resize->width; |
| 610 | } else { |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 611 | width = resize->width; |
| 612 | } |
| 613 | |
Kristian Høgsberg | f1ef15c | 2011-01-06 10:00:23 -0500 | [diff] [blame] | 614 | if (resize->edges & WL_GRAB_RESIZE_TOP) { |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 615 | height = device->grab_y - y + resize->height; |
Kristian Høgsberg | f1ef15c | 2011-01-06 10:00:23 -0500 | [diff] [blame] | 616 | } else if (resize->edges & WL_GRAB_RESIZE_BOTTOM) { |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 617 | height = y - device->grab_y + resize->height; |
| 618 | } else { |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 619 | height = resize->height; |
| 620 | } |
| 621 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 622 | wl_client_post_event(surface->client, &ec->shell.object, |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 623 | WL_SHELL_CONFIGURE, time, resize->edges, |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 624 | surface, width, height); |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static void |
Kristian Høgsberg | b3fc757 | 2010-12-08 11:07:57 -0500 | [diff] [blame] | 628 | resize_grab_button(struct wl_grab *grab, |
| 629 | uint32_t time, int32_t button, int32_t state) |
| 630 | { |
| 631 | } |
| 632 | |
| 633 | static void |
Kristian Høgsberg | 287343a | 2010-12-07 14:58:57 -0500 | [diff] [blame] | 634 | resize_grab_end(struct wl_grab *grab, uint32_t time) |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 635 | { |
| 636 | free(grab); |
| 637 | } |
| 638 | |
Kristian Høgsberg | 6d65d5f | 2010-12-07 13:30:18 -0500 | [diff] [blame] | 639 | static const struct wl_grab_interface resize_grab_interface = { |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 640 | resize_grab_motion, |
Kristian Høgsberg | b3fc757 | 2010-12-08 11:07:57 -0500 | [diff] [blame] | 641 | resize_grab_button, |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 642 | resize_grab_end |
| 643 | }; |
| 644 | |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 645 | static void |
| 646 | shell_resize(struct wl_client *client, struct wl_shell *shell, |
| 647 | struct wl_surface *surface, |
| 648 | struct wl_input_device *device, uint32_t time, uint32_t edges) |
| 649 | { |
| 650 | struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 651 | struct wlsc_resize_grab *resize; |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 652 | enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR; |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 653 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 654 | |
| 655 | resize = malloc(sizeof *resize); |
| 656 | if (!resize) { |
| 657 | wl_client_post_no_memory(client); |
| 658 | return; |
| 659 | } |
| 660 | |
| 661 | resize->grab.interface = &resize_grab_interface; |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 662 | resize->edges = edges; |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 663 | resize->dx = es->x - wd->input_device.grab_x; |
| 664 | resize->dy = es->y - wd->input_device.grab_y; |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 665 | resize->width = es->width; |
| 666 | resize->height = es->height; |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 667 | |
| 668 | if (edges == 0 || edges > 15 || |
| 669 | (edges & 3) == 3 || (edges & 12) == 12) |
| 670 | return; |
| 671 | |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 672 | switch (edges) { |
Kristian Høgsberg | f1ef15c | 2011-01-06 10:00:23 -0500 | [diff] [blame] | 673 | case WL_GRAB_RESIZE_TOP: |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 674 | pointer = WLSC_POINTER_TOP; |
| 675 | break; |
Kristian Høgsberg | f1ef15c | 2011-01-06 10:00:23 -0500 | [diff] [blame] | 676 | case WL_GRAB_RESIZE_BOTTOM: |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 677 | pointer = WLSC_POINTER_BOTTOM; |
| 678 | break; |
Kristian Høgsberg | f1ef15c | 2011-01-06 10:00:23 -0500 | [diff] [blame] | 679 | case WL_GRAB_RESIZE_LEFT: |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 680 | pointer = WLSC_POINTER_LEFT; |
| 681 | break; |
Kristian Høgsberg | f1ef15c | 2011-01-06 10:00:23 -0500 | [diff] [blame] | 682 | case WL_GRAB_RESIZE_TOP_LEFT: |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 683 | pointer = WLSC_POINTER_TOP_LEFT; |
| 684 | break; |
Kristian Høgsberg | f1ef15c | 2011-01-06 10:00:23 -0500 | [diff] [blame] | 685 | case WL_GRAB_RESIZE_BOTTOM_LEFT: |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 686 | pointer = WLSC_POINTER_BOTTOM_LEFT; |
| 687 | break; |
Kristian Høgsberg | f1ef15c | 2011-01-06 10:00:23 -0500 | [diff] [blame] | 688 | case WL_GRAB_RESIZE_RIGHT: |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 689 | pointer = WLSC_POINTER_RIGHT; |
| 690 | break; |
Kristian Høgsberg | f1ef15c | 2011-01-06 10:00:23 -0500 | [diff] [blame] | 691 | case WL_GRAB_RESIZE_TOP_RIGHT: |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 692 | pointer = WLSC_POINTER_TOP_RIGHT; |
| 693 | break; |
Kristian Høgsberg | f1ef15c | 2011-01-06 10:00:23 -0500 | [diff] [blame] | 694 | case WL_GRAB_RESIZE_BOTTOM_RIGHT: |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 695 | pointer = WLSC_POINTER_BOTTOM_RIGHT; |
| 696 | break; |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 697 | } |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 698 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 699 | if (wl_input_device_update_grab(&wd->input_device, |
| 700 | &resize->grab, surface, time) < 0) |
Kristian Høgsberg | ea08115 | 2010-12-07 08:59:51 -0500 | [diff] [blame] | 701 | return; |
| 702 | |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 703 | wlsc_input_device_set_pointer_image(wd, pointer); |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 704 | } |
| 705 | |
Kristian Høgsberg | 3d46534 | 2010-11-22 13:58:46 -0500 | [diff] [blame] | 706 | static void |
| 707 | wl_drag_set_pointer_focus(struct wl_drag *drag, |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 708 | struct wl_surface *surface, uint32_t time, |
Kristian Høgsberg | 3d46534 | 2010-11-22 13:58:46 -0500 | [diff] [blame] | 709 | int32_t x, int32_t y, int32_t sx, int32_t sy); |
| 710 | |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 711 | static void |
| 712 | destroy_drag(struct wl_resource *resource, struct wl_client *client) |
| 713 | { |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 714 | struct wl_drag *drag = |
| 715 | container_of(resource, struct wl_drag, resource); |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 716 | |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 717 | wl_list_remove(&drag->drag_focus_listener.link); |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 718 | if (drag->grab.input_device) |
| 719 | wl_input_device_end_grab(drag->grab.input_device, get_time()); |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 720 | |
| 721 | free(drag); |
| 722 | } |
| 723 | |
| 724 | const static struct wl_drag_interface drag_interface; |
| 725 | |
| 726 | static void |
Kristian Høgsberg | c551bd2 | 2010-12-06 16:43:16 -0500 | [diff] [blame] | 727 | drag_handle_surface_destroy(struct wl_listener *listener, |
Kristian Høgsberg | 4685fa3 | 2010-12-06 21:38:50 -0500 | [diff] [blame] | 728 | struct wl_surface *surface, uint32_t time) |
Kristian Høgsberg | 3d46534 | 2010-11-22 13:58:46 -0500 | [diff] [blame] | 729 | { |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 730 | struct wl_drag *drag = |
| 731 | container_of(listener, struct wl_drag, drag_focus_listener); |
Kristian Høgsberg | 3d46534 | 2010-11-22 13:58:46 -0500 | [diff] [blame] | 732 | |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 733 | if (drag->drag_focus == surface) |
| 734 | wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0); |
Kristian Høgsberg | 3d46534 | 2010-11-22 13:58:46 -0500 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | static void |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 738 | shell_create_drag(struct wl_client *client, |
| 739 | struct wl_shell *shell, uint32_t id) |
| 740 | { |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 741 | struct wl_drag *drag; |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 742 | |
| 743 | drag = malloc(sizeof *drag); |
| 744 | if (drag == NULL) { |
Kristian Høgsberg | 13b8ae4 | 2010-09-02 20:55:16 -0400 | [diff] [blame] | 745 | wl_client_post_no_memory(client); |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 746 | return; |
| 747 | } |
| 748 | |
| 749 | memset(drag, 0, sizeof *drag); |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 750 | drag->resource.object.id = id; |
| 751 | drag->resource.object.interface = &wl_drag_interface; |
| 752 | drag->resource.object.implementation = |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 753 | (void (**)(void)) &drag_interface; |
| 754 | |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 755 | drag->resource.destroy = destroy_drag; |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 756 | |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 757 | drag->drag_focus_listener.func = drag_handle_surface_destroy; |
| 758 | wl_list_init(&drag->drag_focus_listener.link); |
Kristian Høgsberg | 3d46534 | 2010-11-22 13:58:46 -0500 | [diff] [blame] | 759 | |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 760 | wl_client_add_resource(client, &drag->resource); |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 761 | } |
| 762 | |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 763 | const static struct wl_shell_interface shell_interface = { |
| 764 | shell_move, |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 765 | shell_resize, |
| 766 | shell_create_drag |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 767 | }; |
| 768 | |
| 769 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 770 | compositor_create_surface(struct wl_client *client, |
| 771 | struct wl_compositor *compositor, uint32_t id) |
| 772 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 773 | struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor; |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 774 | struct wlsc_surface *surface; |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 775 | |
Kristian Høgsberg | c5d6be9 | 2011-01-14 16:22:37 -0500 | [diff] [blame] | 776 | surface = wlsc_surface_create(ec, 0, 0, 0, 0); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 777 | if (surface == NULL) { |
Kristian Høgsberg | 13b8ae4 | 2010-09-02 20:55:16 -0400 | [diff] [blame] | 778 | wl_client_post_no_memory(client); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 779 | return; |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 780 | } |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 781 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 782 | surface->surface.resource.destroy = destroy_surface; |
Kristian Høgsberg | f66d0f4 | 2010-09-02 20:27:16 -0400 | [diff] [blame] | 783 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 784 | surface->surface.resource.object.id = id; |
| 785 | surface->surface.resource.object.interface = &wl_surface_interface; |
| 786 | surface->surface.resource.object.implementation = |
Kristian Høgsberg | f66d0f4 | 2010-09-02 20:27:16 -0400 | [diff] [blame] | 787 | (void (**)(void)) &surface_interface; |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 788 | surface->surface.client = client; |
Kristian Høgsberg | f66d0f4 | 2010-09-02 20:27:16 -0400 | [diff] [blame] | 789 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 790 | wl_client_add_resource(client, &surface->surface.resource); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 791 | } |
| 792 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 793 | const static struct wl_compositor_interface compositor_interface = { |
| 794 | compositor_create_surface, |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 795 | }; |
| 796 | |
Kristian Høgsberg | 7b6907f | 2009-02-14 17:47:55 -0500 | [diff] [blame] | 797 | static void |
| 798 | wlsc_surface_transform(struct wlsc_surface *surface, |
| 799 | int32_t x, int32_t y, int32_t *sx, int32_t *sy) |
| 800 | { |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 801 | struct wlsc_vector v = { { x, y, 0, 1 } }; |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 802 | |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 803 | wlsc_matrix_transform(&surface->matrix_inv, &v); |
Kristian Høgsberg | 0b8646b | 2010-06-08 15:29:14 -0400 | [diff] [blame] | 804 | *sx = v.f[0] * surface->width; |
| 805 | *sy = v.f[1] * surface->height; |
Kristian Høgsberg | 7b6907f | 2009-02-14 17:47:55 -0500 | [diff] [blame] | 806 | } |
| 807 | |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 808 | static struct wlsc_surface * |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 809 | 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] | 810 | { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 811 | struct wlsc_compositor *ec = |
| 812 | (struct wlsc_compositor *) device->compositor; |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 813 | struct wlsc_surface *es; |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 814 | |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 815 | wl_list_for_each(es, &ec->surface_list, link) { |
| 816 | wlsc_surface_transform(es, device->x, device->y, sx, sy); |
| 817 | if (0 <= *sx && *sx < es->width && |
| 818 | 0 <= *sy && *sy < es->height) |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 819 | return es; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 820 | } |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 821 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 822 | return NULL; |
| 823 | } |
| 824 | |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 825 | |
| 826 | static void |
| 827 | motion_grab_motion(struct wl_grab *grab, |
| 828 | uint32_t time, int32_t x, int32_t y) |
| 829 | { |
| 830 | struct wlsc_input_device *device = |
| 831 | (struct wlsc_input_device *) grab->input_device; |
| 832 | struct wlsc_surface *es = |
| 833 | (struct wlsc_surface *) device->input_device.pointer_focus; |
| 834 | int32_t sx, sy; |
| 835 | |
| 836 | wlsc_surface_transform(es, x, y, &sx, &sy); |
| 837 | wl_client_post_event(es->surface.client, |
| 838 | &device->input_device.object, |
| 839 | WL_INPUT_DEVICE_MOTION, |
| 840 | time, x, y, sx, sy); |
| 841 | } |
| 842 | |
| 843 | static void |
Kristian Høgsberg | b3fc757 | 2010-12-08 11:07:57 -0500 | [diff] [blame] | 844 | motion_grab_button(struct wl_grab *grab, |
| 845 | uint32_t time, int32_t button, int32_t state) |
| 846 | { |
| 847 | wl_client_post_event(grab->input_device->pointer_focus->client, |
| 848 | &grab->input_device->object, |
| 849 | WL_INPUT_DEVICE_BUTTON, |
| 850 | time, button, state); |
| 851 | } |
| 852 | |
| 853 | static void |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 854 | motion_grab_end(struct wl_grab *grab, uint32_t time) |
| 855 | { |
| 856 | } |
| 857 | |
| 858 | static const struct wl_grab_interface motion_grab_interface = { |
| 859 | motion_grab_motion, |
Kristian Høgsberg | b3fc757 | 2010-12-08 11:07:57 -0500 | [diff] [blame] | 860 | motion_grab_button, |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 861 | motion_grab_end |
| 862 | }; |
| 863 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 864 | void |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 865 | 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] | 866 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 867 | struct wlsc_surface *es; |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 868 | struct wlsc_compositor *ec = |
| 869 | (struct wlsc_compositor *) device->compositor; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 870 | struct wlsc_output *output; |
Kristian Høgsberg | 6d65d5f | 2010-12-07 13:30:18 -0500 | [diff] [blame] | 871 | const struct wl_grab_interface *interface; |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 872 | struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 873 | int32_t sx, sy; |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 874 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 875 | /* FIXME: We need some multi head love here. */ |
| 876 | output = container_of(ec->output_list.next, struct wlsc_output, link); |
| 877 | if (x < output->x) |
Ray Strode | 90e701d | 2008-12-18 23:05:43 -0500 | [diff] [blame] | 878 | x = 0; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 879 | if (y < output->y) |
Ray Strode | 90e701d | 2008-12-18 23:05:43 -0500 | [diff] [blame] | 880 | y = 0; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 881 | if (x >= output->x + output->width) |
| 882 | x = output->x + output->width - 1; |
| 883 | if (y >= output->y + output->height) |
| 884 | y = output->y + output->height - 1; |
Ray Strode | 90e701d | 2008-12-18 23:05:43 -0500 | [diff] [blame] | 885 | |
Kristian Høgsberg | e3ef3e5 | 2008-12-21 19:30:01 -0500 | [diff] [blame] | 886 | device->x = x; |
| 887 | device->y = y; |
Kristian Høgsberg | db6c2f3 | 2009-02-22 21:51:24 -0500 | [diff] [blame] | 888 | |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 889 | if (device->grab) { |
| 890 | interface = device->grab->interface; |
| 891 | interface->motion(device->grab, time, x, y); |
| 892 | } else { |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 893 | es = pick_surface(device, &sx, &sy); |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 894 | wl_input_device_set_pointer_focus(device, |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 895 | &es->surface, |
Kristian Høgsberg | 2643707 | 2010-12-01 10:17:47 -0500 | [diff] [blame] | 896 | time, x, y, sx, sy); |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 897 | if (es) |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 898 | wl_client_post_event(es->surface.client, |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 899 | &device->object, |
Kristian Høgsberg | 50038e4 | 2010-09-07 21:08:59 -0400 | [diff] [blame] | 900 | WL_INPUT_DEVICE_MOTION, |
| 901 | time, x, y, sx, sy); |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 902 | } |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 903 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 904 | wd->sprite->x = device->x - wd->hotspot_x; |
| 905 | wd->sprite->y = device->y - wd->hotspot_y; |
| 906 | wlsc_surface_update_matrix(wd->sprite); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 907 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 908 | wlsc_compositor_schedule_repaint(ec); |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 909 | } |
| 910 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 911 | void |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 912 | notify_button(struct wl_input_device *device, |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 913 | uint32_t time, int32_t button, int32_t state) |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 914 | { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 915 | struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 916 | struct wlsc_surface *surface; |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 917 | struct wlsc_compositor *compositor = |
| 918 | (struct wlsc_compositor *) device->compositor; |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 919 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 920 | surface = (struct wlsc_surface *) device->pointer_focus; |
Kristian Høgsberg | dff2e3c | 2010-12-07 09:02:09 -0500 | [diff] [blame] | 921 | if (!surface) |
| 922 | return; |
Kristian Høgsberg | ea08115 | 2010-12-07 08:59:51 -0500 | [diff] [blame] | 923 | |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 924 | if (state && device->grab == NULL) { |
Kristian Høgsberg | dff2e3c | 2010-12-07 09:02:09 -0500 | [diff] [blame] | 925 | wlsc_surface_raise(surface); |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 926 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 927 | wl_input_device_start_grab(device, |
| 928 | &device->motion_grab, |
| 929 | button, time); |
| 930 | wl_input_device_set_keyboard_focus(device, |
Kristian Høgsberg | dff2e3c | 2010-12-07 09:02:09 -0500 | [diff] [blame] | 931 | &surface->surface, |
| 932 | time); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 933 | } |
Kristian Høgsberg | dff2e3c | 2010-12-07 09:02:09 -0500 | [diff] [blame] | 934 | |
| 935 | if (state && button == BTN_LEFT && |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 936 | (wd->modifier_state & MODIFIER_SUPER)) |
Kristian Høgsberg | dff2e3c | 2010-12-07 09:02:09 -0500 | [diff] [blame] | 937 | shell_move(NULL, |
| 938 | (struct wl_shell *) &compositor->shell, |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 939 | &surface->surface, device, time); |
Kristian Høgsberg | dff2e3c | 2010-12-07 09:02:09 -0500 | [diff] [blame] | 940 | else if (state && button == BTN_MIDDLE && |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 941 | (wd->modifier_state & MODIFIER_SUPER)) |
Kristian Høgsberg | dff2e3c | 2010-12-07 09:02:09 -0500 | [diff] [blame] | 942 | shell_resize(NULL, |
| 943 | (struct wl_shell *) &compositor->shell, |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 944 | &surface->surface, device, time, |
Kristian Høgsberg | f1ef15c | 2011-01-06 10:00:23 -0500 | [diff] [blame] | 945 | WL_GRAB_RESIZE_BOTTOM_RIGHT); |
Kristian Høgsberg | b3fc757 | 2010-12-08 11:07:57 -0500 | [diff] [blame] | 946 | |
| 947 | device->grab->interface->button(device->grab, time, button, state); |
Kristian Høgsberg | dff2e3c | 2010-12-07 09:02:09 -0500 | [diff] [blame] | 948 | |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 949 | if (!state && device->grab && device->grab_button == button) |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 950 | wl_input_device_end_grab(device, time); |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 951 | } |
| 952 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 953 | void |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 954 | notify_key(struct wl_input_device *device, |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 955 | uint32_t time, uint32_t key, uint32_t state) |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 956 | { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 957 | struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
| 958 | struct wlsc_compositor *compositor = |
| 959 | (struct wlsc_compositor *) device->compositor; |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 960 | uint32_t *k, *end; |
Kristian Høgsberg | 2cbedd1 | 2009-09-18 17:29:49 -0400 | [diff] [blame] | 961 | uint32_t modifier; |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 962 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 963 | switch (key | wd->modifier_state) { |
Kristian Høgsberg | 2cbedd1 | 2009-09-18 17:29:49 -0400 | [diff] [blame] | 964 | case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT: |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 965 | wl_display_terminate(compositor->wl_display); |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 966 | return; |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 967 | } |
Kristian Høgsberg | ab909ae | 2001-01-01 22:24:24 -0500 | [diff] [blame] | 968 | |
Kristian Høgsberg | 2cbedd1 | 2009-09-18 17:29:49 -0400 | [diff] [blame] | 969 | switch (key) { |
| 970 | case KEY_LEFTCTRL: |
| 971 | case KEY_RIGHTCTRL: |
| 972 | modifier = MODIFIER_CTRL; |
| 973 | break; |
| 974 | |
| 975 | case KEY_LEFTALT: |
| 976 | case KEY_RIGHTALT: |
| 977 | modifier = MODIFIER_ALT; |
| 978 | break; |
| 979 | |
Kristian Høgsberg | 5b75f1b | 2010-08-04 23:21:41 -0400 | [diff] [blame] | 980 | case KEY_LEFTMETA: |
| 981 | case KEY_RIGHTMETA: |
| 982 | modifier = MODIFIER_SUPER; |
| 983 | break; |
| 984 | |
Kristian Høgsberg | 2cbedd1 | 2009-09-18 17:29:49 -0400 | [diff] [blame] | 985 | default: |
| 986 | modifier = 0; |
| 987 | break; |
| 988 | } |
| 989 | |
| 990 | if (state) |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 991 | wd->modifier_state |= modifier; |
Kristian Høgsberg | 2cbedd1 | 2009-09-18 17:29:49 -0400 | [diff] [blame] | 992 | else |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 993 | wd->modifier_state &= ~modifier; |
Kristian Høgsberg | 2cbedd1 | 2009-09-18 17:29:49 -0400 | [diff] [blame] | 994 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 995 | end = device->keys.data + device->keys.size; |
| 996 | for (k = device->keys.data; k < end; k++) { |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 997 | if (*k == key) |
| 998 | *k = *--end; |
| 999 | } |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1000 | device->keys.size = (void *) end - device->keys.data; |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 1001 | if (state) { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1002 | k = wl_array_add(&device->keys, sizeof *k); |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 1003 | *k = key; |
| 1004 | } |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 1005 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1006 | if (device->keyboard_focus != NULL) |
| 1007 | wl_client_post_event(device->keyboard_focus->client, |
| 1008 | &device->object, |
Kristian Høgsberg | 50038e4 | 2010-09-07 21:08:59 -0400 | [diff] [blame] | 1009 | WL_INPUT_DEVICE_KEY, time, key, state); |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 1010 | } |
| 1011 | |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 1012 | static void |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1013 | wl_drag_set_pointer_focus(struct wl_drag *drag, |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 1014 | struct wl_surface *surface, uint32_t time, |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1015 | int32_t x, int32_t y, int32_t sx, int32_t sy) |
| 1016 | { |
Kristian Høgsberg | 506e20e | 2010-08-19 17:26:02 -0400 | [diff] [blame] | 1017 | char **p, **end; |
| 1018 | |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 1019 | if (drag->drag_focus == surface) |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1020 | return; |
| 1021 | |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 1022 | if (drag->drag_focus && |
| 1023 | (!surface || drag->drag_focus->client != surface->client)) |
| 1024 | wl_client_post_event(drag->drag_focus->client, |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1025 | &drag->drag_offer.object, |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1026 | WL_DRAG_OFFER_POINTER_FOCUS, |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1027 | time, NULL, 0, 0, 0, 0); |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1028 | |
| 1029 | if (surface && |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 1030 | (!drag->drag_focus || |
| 1031 | drag->drag_focus->client != surface->client)) { |
| 1032 | wl_client_post_global(surface->client, |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1033 | &drag->drag_offer.object); |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1034 | |
Kristian Høgsberg | 506e20e | 2010-08-19 17:26:02 -0400 | [diff] [blame] | 1035 | end = drag->types.data + drag->types.size; |
| 1036 | for (p = drag->types.data; p < end; p++) |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 1037 | wl_client_post_event(surface->client, |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1038 | &drag->drag_offer.object, |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1039 | WL_DRAG_OFFER_OFFER, *p); |
Kristian Høgsberg | 506e20e | 2010-08-19 17:26:02 -0400 | [diff] [blame] | 1040 | } |
| 1041 | |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1042 | if (surface) { |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 1043 | wl_client_post_event(surface->client, |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1044 | &drag->drag_offer.object, |
Kristian Høgsberg | 50038e4 | 2010-09-07 21:08:59 -0400 | [diff] [blame] | 1045 | WL_DRAG_OFFER_POINTER_FOCUS, |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 1046 | time, surface, |
Kristian Høgsberg | 50038e4 | 2010-09-07 21:08:59 -0400 | [diff] [blame] | 1047 | x, y, sx, sy); |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1048 | |
| 1049 | } |
Kristian Høgsberg | 506e20e | 2010-08-19 17:26:02 -0400 | [diff] [blame] | 1050 | |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 1051 | drag->drag_focus = surface; |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1052 | drag->pointer_focus_time = time; |
Kristian Høgsberg | 4eb5360 | 2010-08-27 20:29:56 -0400 | [diff] [blame] | 1053 | drag->target = NULL; |
Kristian Høgsberg | 3d76e65 | 2010-12-06 17:33:11 -0500 | [diff] [blame] | 1054 | |
| 1055 | wl_list_remove(&drag->drag_focus_listener.link); |
| 1056 | if (surface) |
| 1057 | wl_list_insert(surface->destroy_listener_list.prev, |
| 1058 | &drag->drag_focus_listener.link); |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | static void |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1062 | drag_offer_accept(struct wl_client *client, |
| 1063 | struct wl_drag_offer *offer, uint32_t time, const char *type) |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1064 | { |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1065 | struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer); |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1066 | char **p, **end; |
| 1067 | |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1068 | /* If the client responds to drag pointer_focus or motion |
| 1069 | * events after the pointer has left the surface, we just |
| 1070 | * discard the accept requests. The drag source just won't |
| 1071 | * get the corresponding 'target' events and eventually the |
| 1072 | * next surface/root will start sending events. */ |
| 1073 | if (time < drag->pointer_focus_time) |
| 1074 | return; |
| 1075 | |
| 1076 | drag->target = client; |
| 1077 | drag->type = NULL; |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1078 | end = drag->types.data + drag->types.size; |
| 1079 | for (p = drag->types.data; p < end; p++) |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1080 | if (type && strcmp(*p, type) == 0) |
| 1081 | drag->type = *p; |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1082 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1083 | wl_client_post_event(drag->source->client, &drag->resource.object, |
Kristian Høgsberg | 50038e4 | 2010-09-07 21:08:59 -0400 | [diff] [blame] | 1084 | WL_DRAG_TARGET, drag->type); |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | static void |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1088 | drag_offer_receive(struct wl_client *client, |
| 1089 | struct wl_drag_offer *offer, int fd) |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1090 | { |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1091 | struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer); |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1092 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1093 | wl_client_post_event(drag->source->client, &drag->resource.object, |
Kristian Høgsberg | 50038e4 | 2010-09-07 21:08:59 -0400 | [diff] [blame] | 1094 | WL_DRAG_FINISH, fd); |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1095 | close(fd); |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1096 | } |
| 1097 | |
Kristian Høgsberg | d44bc8b | 2010-11-30 15:10:26 -0500 | [diff] [blame] | 1098 | static void |
| 1099 | drag_offer_reject(struct wl_client *client, struct wl_drag_offer *offer) |
| 1100 | { |
| 1101 | struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer); |
| 1102 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1103 | wl_client_post_event(drag->source->client, &drag->resource.object, |
Kristian Høgsberg | d44bc8b | 2010-11-30 15:10:26 -0500 | [diff] [blame] | 1104 | WL_DRAG_REJECT); |
| 1105 | } |
| 1106 | |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1107 | static const struct wl_drag_offer_interface drag_offer_interface = { |
| 1108 | drag_offer_accept, |
Kristian Høgsberg | d44bc8b | 2010-11-30 15:10:26 -0500 | [diff] [blame] | 1109 | drag_offer_receive, |
| 1110 | drag_offer_reject |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1111 | }; |
| 1112 | |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1113 | static void |
| 1114 | drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type) |
| 1115 | { |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1116 | char **p; |
| 1117 | |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1118 | p = wl_array_add(&drag->types, sizeof *p); |
| 1119 | if (p) |
| 1120 | *p = strdup(type); |
| 1121 | if (!p || !*p) |
Kristian Høgsberg | 13b8ae4 | 2010-09-02 20:55:16 -0400 | [diff] [blame] | 1122 | wl_client_post_no_memory(client); |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | static void |
Kristian Høgsberg | 287343a | 2010-12-07 14:58:57 -0500 | [diff] [blame] | 1126 | drag_grab_motion(struct wl_grab *grab, |
| 1127 | uint32_t time, int32_t x, int32_t y) |
| 1128 | { |
| 1129 | struct wl_drag *drag = container_of(grab, struct wl_drag, grab); |
Kristian Høgsberg | 287343a | 2010-12-07 14:58:57 -0500 | [diff] [blame] | 1130 | struct wlsc_surface *es; |
| 1131 | int32_t sx, sy; |
| 1132 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1133 | es = pick_surface(grab->input_device, &sx, &sy); |
Kristian Høgsberg | 287343a | 2010-12-07 14:58:57 -0500 | [diff] [blame] | 1134 | wl_drag_set_pointer_focus(drag, &es->surface, time, x, y, sx, sy); |
| 1135 | if (es) |
| 1136 | wl_client_post_event(es->surface.client, |
| 1137 | &drag->drag_offer.object, |
| 1138 | WL_DRAG_OFFER_MOTION, |
| 1139 | time, x, y, sx, sy); |
| 1140 | } |
| 1141 | |
| 1142 | static void |
Kristian Høgsberg | b3fc757 | 2010-12-08 11:07:57 -0500 | [diff] [blame] | 1143 | drag_grab_button(struct wl_grab *grab, |
| 1144 | uint32_t time, int32_t button, int32_t state) |
| 1145 | { |
| 1146 | } |
| 1147 | |
| 1148 | static void |
Kristian Høgsberg | 287343a | 2010-12-07 14:58:57 -0500 | [diff] [blame] | 1149 | drag_grab_end(struct wl_grab *grab, uint32_t time) |
| 1150 | { |
| 1151 | struct wl_drag *drag = container_of(grab, struct wl_drag, grab); |
| 1152 | |
| 1153 | if (drag->target) |
| 1154 | wl_client_post_event(drag->target, |
| 1155 | &drag->drag_offer.object, |
| 1156 | WL_DRAG_OFFER_DROP); |
| 1157 | |
| 1158 | wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0); |
| 1159 | } |
| 1160 | |
| 1161 | static const struct wl_grab_interface drag_grab_interface = { |
| 1162 | drag_grab_motion, |
Kristian Høgsberg | b3fc757 | 2010-12-08 11:07:57 -0500 | [diff] [blame] | 1163 | drag_grab_button, |
Kristian Høgsberg | 287343a | 2010-12-07 14:58:57 -0500 | [diff] [blame] | 1164 | drag_grab_end |
| 1165 | }; |
| 1166 | |
| 1167 | static void |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1168 | drag_activate(struct wl_client *client, |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1169 | struct wl_drag *drag, |
| 1170 | struct wl_surface *surface, |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1171 | struct wl_input_device *device, uint32_t time) |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1172 | { |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1173 | struct wl_display *display = wl_client_get_display (client); |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1174 | struct wlsc_surface *target; |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1175 | int32_t sx, sy; |
| 1176 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1177 | if (wl_input_device_update_grab(device, |
| 1178 | &drag->grab, surface, time) < 0) |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1179 | return; |
| 1180 | |
Kristian Høgsberg | 287343a | 2010-12-07 14:58:57 -0500 | [diff] [blame] | 1181 | drag->grab.interface = &drag_grab_interface; |
Kristian Høgsberg | 287343a | 2010-12-07 14:58:57 -0500 | [diff] [blame] | 1182 | |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1183 | drag->source = surface; |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1184 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1185 | drag->drag_offer.object.interface = &wl_drag_offer_interface; |
| 1186 | drag->drag_offer.object.implementation = |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1187 | (void (**)(void)) &drag_offer_interface; |
| 1188 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1189 | wl_display_add_object(display, &drag->drag_offer.object); |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1190 | |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 1191 | target = pick_surface(device, &sx, &sy); |
Kristian Høgsberg | 287343a | 2010-12-07 14:58:57 -0500 | [diff] [blame] | 1192 | wl_drag_set_pointer_focus(drag, &target->surface, time, |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1193 | device->x, device->y, sx, sy); |
| 1194 | } |
| 1195 | |
| 1196 | static void |
Kristian Høgsberg | 287343a | 2010-12-07 14:58:57 -0500 | [diff] [blame] | 1197 | drag_destroy(struct wl_client *client, struct wl_drag *drag) |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1198 | { |
Kristian Høgsberg | 287343a | 2010-12-07 14:58:57 -0500 | [diff] [blame] | 1199 | wl_resource_destroy(&drag->resource, client); |
Kristian Høgsberg | 4eb5360 | 2010-08-27 20:29:56 -0400 | [diff] [blame] | 1200 | } |
| 1201 | |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1202 | static const struct wl_drag_interface drag_interface = { |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1203 | drag_offer, |
| 1204 | drag_activate, |
Kristian Høgsberg | 287343a | 2010-12-07 14:58:57 -0500 | [diff] [blame] | 1205 | drag_destroy, |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 1206 | }; |
| 1207 | |
Kristian Høgsberg | ab1862d | 2010-12-09 11:29:40 -0500 | [diff] [blame] | 1208 | static void |
| 1209 | input_device_attach(struct wl_client *client, |
| 1210 | struct wl_input_device *device_base, |
| 1211 | uint32_t time, |
| 1212 | struct wl_buffer *buffer, int32_t x, int32_t y) |
| 1213 | { |
| 1214 | struct wlsc_input_device *device = |
| 1215 | (struct wlsc_input_device *) device_base; |
| 1216 | |
| 1217 | if (time < device->input_device.pointer_focus_time) |
| 1218 | return; |
| 1219 | if (device->input_device.pointer_focus == NULL) |
| 1220 | return; |
| 1221 | |
| 1222 | if (device->input_device.pointer_focus->client != client) |
| 1223 | return; |
| 1224 | |
| 1225 | if (buffer == NULL) { |
| 1226 | wlsc_input_device_set_pointer_image(device, |
| 1227 | WLSC_POINTER_LEFT_PTR); |
| 1228 | return; |
| 1229 | } |
| 1230 | |
| 1231 | wlsc_input_device_attach(device, buffer, x, y); |
| 1232 | } |
| 1233 | |
| 1234 | const static struct wl_input_device_interface input_device_interface = { |
| 1235 | input_device_attach, |
| 1236 | }; |
| 1237 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1238 | void |
| 1239 | wlsc_input_device_init(struct wlsc_input_device *device, |
| 1240 | struct wlsc_compositor *ec) |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 1241 | { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1242 | wl_input_device_init(&device->input_device, &ec->compositor); |
Kristian Høgsberg | 02ef1c1 | 2010-12-06 21:35:19 -0500 | [diff] [blame] | 1243 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1244 | device->input_device.object.interface = &wl_input_device_interface; |
| 1245 | device->input_device.object.implementation = |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 1246 | (void (**)(void)) &input_device_interface; |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1247 | wl_display_add_object(ec->wl_display, &device->input_device.object); |
| 1248 | 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] | 1249 | |
Kristian Høgsberg | c5d6be9 | 2011-01-14 16:22:37 -0500 | [diff] [blame] | 1250 | device->sprite = wlsc_surface_create(ec, |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1251 | device->input_device.x, |
| 1252 | device->input_device.y, 32, 32); |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 1253 | device->hotspot_x = 16; |
| 1254 | device->hotspot_y = 16; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1255 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1256 | device->input_device.motion_grab.interface = &motion_grab_interface; |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 1257 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 1258 | wl_list_insert(ec->input_device_list.prev, &device->link); |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 1259 | |
| 1260 | wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1261 | } |
| 1262 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1263 | static void |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1264 | wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global) |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1265 | { |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1266 | struct wlsc_output *output = |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1267 | container_of(global, struct wlsc_output, object); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1268 | |
| 1269 | wl_client_post_event(client, global, |
| 1270 | WL_OUTPUT_GEOMETRY, |
Kristian Høgsberg | f8fc08f | 2010-12-01 20:10:10 -0500 | [diff] [blame] | 1271 | output->x, output->y, |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1272 | output->width, output->height); |
| 1273 | } |
| 1274 | |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1275 | static const char vertex_shader[] = |
| 1276 | "uniform mat4 proj;\n" |
| 1277 | "attribute vec4 position;\n" |
| 1278 | "attribute vec2 texcoord;\n" |
| 1279 | "varying vec2 v_texcoord;\n" |
| 1280 | "void main()\n" |
| 1281 | "{\n" |
| 1282 | " gl_Position = proj * position;\n" |
| 1283 | " v_texcoord = texcoord;\n" |
| 1284 | "}\n"; |
| 1285 | |
| 1286 | static const char fragment_shader[] = |
Kristian Høgsberg | dfce71d | 2010-12-07 20:19:10 -0500 | [diff] [blame] | 1287 | "precision mediump float;\n" |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1288 | "varying vec2 v_texcoord;\n" |
| 1289 | "uniform sampler2D tex;\n" |
| 1290 | "void main()\n" |
| 1291 | "{\n" |
| 1292 | " gl_FragColor = texture2D(tex, v_texcoord)\n;" |
| 1293 | "}\n"; |
| 1294 | |
Kristian Høgsberg | a946821 | 2010-06-14 21:03:11 -0400 | [diff] [blame] | 1295 | static int |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1296 | init_shaders(struct wlsc_compositor *ec) |
| 1297 | { |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1298 | GLuint v, f, program; |
| 1299 | const char *p; |
| 1300 | char msg[512]; |
| 1301 | GLfloat vertices[4 * 5]; |
| 1302 | GLint status; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1303 | |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1304 | p = vertex_shader; |
| 1305 | v = glCreateShader(GL_VERTEX_SHADER); |
| 1306 | glShaderSource(v, 1, &p, NULL); |
| 1307 | glCompileShader(v); |
| 1308 | glGetShaderiv(v, GL_COMPILE_STATUS, &status); |
| 1309 | if (!status) { |
| 1310 | glGetShaderInfoLog(v, sizeof msg, NULL, msg); |
| 1311 | fprintf(stderr, "vertex shader info: %s\n", msg); |
| 1312 | return -1; |
| 1313 | } |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1314 | |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1315 | p = fragment_shader; |
| 1316 | f = glCreateShader(GL_FRAGMENT_SHADER); |
| 1317 | glShaderSource(f, 1, &p, NULL); |
| 1318 | glCompileShader(f); |
| 1319 | glGetShaderiv(f, GL_COMPILE_STATUS, &status); |
| 1320 | if (!status) { |
| 1321 | glGetShaderInfoLog(f, sizeof msg, NULL, msg); |
| 1322 | fprintf(stderr, "fragment shader info: %s\n", msg); |
| 1323 | return -1; |
| 1324 | } |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1325 | |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1326 | program = glCreateProgram(); |
| 1327 | glAttachShader(program, v); |
| 1328 | glAttachShader(program, f); |
| 1329 | glBindAttribLocation(program, 0, "position"); |
| 1330 | glBindAttribLocation(program, 1, "texcoord"); |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1331 | |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1332 | glLinkProgram(program); |
| 1333 | glGetProgramiv(program, GL_LINK_STATUS, &status); |
| 1334 | if (!status) { |
| 1335 | glGetProgramInfoLog(program, sizeof msg, NULL, msg); |
| 1336 | fprintf(stderr, "link info: %s\n", msg); |
| 1337 | return -1; |
| 1338 | } |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1339 | |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1340 | glUseProgram(program); |
| 1341 | ec->proj_uniform = glGetUniformLocation(program, "proj"); |
| 1342 | ec->tex_uniform = glGetUniformLocation(program, "tex"); |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1343 | |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1344 | vertices[ 0] = 0.0; |
| 1345 | vertices[ 1] = 0.0; |
| 1346 | vertices[ 2] = 0.0; |
| 1347 | vertices[ 3] = 0.0; |
| 1348 | vertices[ 4] = 0.0; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1349 | |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1350 | vertices[ 5] = 0.0; |
| 1351 | vertices[ 6] = 1.0; |
| 1352 | vertices[ 7] = 0.0; |
| 1353 | vertices[ 8] = 0.0; |
| 1354 | vertices[ 9] = 1.0; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1355 | |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1356 | vertices[10] = 1.0; |
| 1357 | vertices[11] = 0.0; |
| 1358 | vertices[12] = 0.0; |
| 1359 | vertices[13] = 1.0; |
| 1360 | vertices[14] = 0.0; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1361 | |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1362 | vertices[15] = 1.0; |
| 1363 | vertices[16] = 1.0; |
| 1364 | vertices[17] = 0.0; |
| 1365 | vertices[18] = 1.0; |
| 1366 | vertices[19] = 1.0; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1367 | |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1368 | glGenBuffers(1, &ec->vbo); |
| 1369 | glBindBuffer(GL_ARRAY_BUFFER, ec->vbo); |
| 1370 | glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW); |
Kristian Høgsberg | a946821 | 2010-06-14 21:03:11 -0400 | [diff] [blame] | 1371 | |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1372 | return 0; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1373 | } |
| 1374 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1375 | void |
| 1376 | wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c, |
| 1377 | int x, int y, int width, int height) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1378 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1379 | output->compositor = c; |
| 1380 | output->x = x; |
| 1381 | output->y = y; |
| 1382 | output->width = width; |
| 1383 | output->height = height; |
| 1384 | |
| 1385 | output->background = |
| 1386 | background_create(output, option_background); |
| 1387 | |
| 1388 | wlsc_matrix_init(&output->matrix); |
| 1389 | wlsc_matrix_translate(&output->matrix, |
| 1390 | -output->x - output->width / 2.0, |
| 1391 | -output->y - output->height / 2.0, 0); |
| 1392 | wlsc_matrix_scale(&output->matrix, |
| 1393 | 2.0 / output->width, 2.0 / output->height, 1); |
| 1394 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1395 | output->object.interface = &wl_output_interface; |
| 1396 | wl_display_add_object(c->wl_display, &output->object); |
| 1397 | wl_display_add_global(c->wl_display, &output->object, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1398 | wlsc_output_post_geometry); |
| 1399 | } |
| 1400 | |
| 1401 | int |
| 1402 | wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display) |
| 1403 | { |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 1404 | struct wl_event_loop *loop; |
Kristian Høgsberg | d711d0c | 2011-01-14 17:28:21 -0500 | [diff] [blame] | 1405 | const char *extensions; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1406 | |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 1407 | ec->wl_display = display; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1408 | |
Kristian Høgsberg | c5c510e | 2010-12-08 15:12:58 -0500 | [diff] [blame] | 1409 | wl_compositor_init(&ec->compositor, &compositor_interface, display); |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 1410 | |
Kristian Høgsberg | 3d5bae0 | 2010-10-06 21:17:40 -0400 | [diff] [blame] | 1411 | wlsc_shm_init(ec); |
| 1412 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1413 | ec->shell.object.interface = &wl_shell_interface; |
| 1414 | ec->shell.object.implementation = (void (**)(void)) &shell_interface; |
| 1415 | wl_display_add_object(display, &ec->shell.object); |
| 1416 | if (wl_display_add_global(display, &ec->shell.object, NULL)) |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 1417 | return -1; |
| 1418 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 1419 | wl_list_init(&ec->surface_list); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1420 | wl_list_init(&ec->input_device_list); |
| 1421 | wl_list_init(&ec->output_list); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1422 | |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 1423 | create_pointer_images(ec); |
| 1424 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1425 | screenshooter_create(ec); |
| 1426 | |
Kristian Høgsberg | d711d0c | 2011-01-14 17:28:21 -0500 | [diff] [blame] | 1427 | extensions = (const char *) glGetString(GL_EXTENSIONS); |
| 1428 | if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) { |
| 1429 | fprintf(stderr, |
| 1430 | "GL_EXT_texture_format_BGRA8888 not available\n"); |
| 1431 | return -1; |
| 1432 | } |
| 1433 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1434 | glGenFramebuffers(1, &ec->fbo); |
| 1435 | glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo); |
| 1436 | glActiveTexture(GL_TEXTURE0); |
Kristian Høgsberg | a946821 | 2010-06-14 21:03:11 -0400 | [diff] [blame] | 1437 | if (init_shaders(ec) < 0) |
| 1438 | return -1; |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 1439 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 1440 | loop = wl_display_get_event_loop(ec->wl_display); |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 1441 | ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec); |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 1442 | wlsc_compositor_schedule_repaint(ec); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 1443 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1444 | return 0; |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1445 | } |
| 1446 | |
Kristian Høgsberg | 50dc698 | 2010-12-01 16:43:56 -0500 | [diff] [blame] | 1447 | static void on_term_signal(int signal_number, void *data) |
| 1448 | { |
| 1449 | struct wlsc_compositor *ec = data; |
| 1450 | |
| 1451 | wl_display_terminate(ec->wl_display); |
| 1452 | } |
| 1453 | |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1454 | int main(int argc, char *argv[]) |
| 1455 | { |
| 1456 | struct wl_display *display; |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1457 | struct wlsc_compositor *ec; |
Kristian Høgsberg | 50dc698 | 2010-12-01 16:43:56 -0500 | [diff] [blame] | 1458 | struct wl_event_loop *loop; |
Kristian Høgsberg | d653126 | 2008-12-12 11:06:18 -0500 | [diff] [blame] | 1459 | GError *error = NULL; |
| 1460 | GOptionContext *context; |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 1461 | int width, height; |
Kristian Høgsberg | d653126 | 2008-12-12 11:06:18 -0500 | [diff] [blame] | 1462 | |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 1463 | g_type_init(); /* GdkPixbuf needs this, it seems. */ |
| 1464 | |
Kristian Høgsberg | d653126 | 2008-12-12 11:06:18 -0500 | [diff] [blame] | 1465 | context = g_option_context_new(NULL); |
| 1466 | g_option_context_add_main_entries(context, option_entries, "Wayland"); |
| 1467 | if (!g_option_context_parse(context, &argc, &argv, &error)) { |
| 1468 | fprintf(stderr, "option parsing failed: %s\n", error->message); |
| 1469 | exit(EXIT_FAILURE); |
| 1470 | } |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 1471 | if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) { |
| 1472 | fprintf(stderr, "invalid geometry option: %s \n", |
| 1473 | option_geometry); |
| 1474 | exit(EXIT_FAILURE); |
| 1475 | } |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1476 | |
| 1477 | display = wl_display_create(); |
| 1478 | |
Kristian Høgsberg | a941022 | 2011-01-14 17:22:35 -0500 | [diff] [blame] | 1479 | ec = NULL; |
| 1480 | |
| 1481 | #if BUILD_WAYLAND_COMPOSITOR |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1482 | if (getenv("WAYLAND_DISPLAY")) |
| 1483 | ec = wayland_compositor_create(display, width, height); |
Kristian Høgsberg | a941022 | 2011-01-14 17:22:35 -0500 | [diff] [blame] | 1484 | #endif |
| 1485 | |
| 1486 | #if BUILD_X11_COMPOSITOR |
| 1487 | if (ec == NULL && getenv("DISPLAY")) |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 1488 | ec = x11_compositor_create(display, width, height); |
Kristian Høgsberg | a941022 | 2011-01-14 17:22:35 -0500 | [diff] [blame] | 1489 | #endif |
| 1490 | |
| 1491 | #if BUILD_DRM_COMPOSITOR |
| 1492 | if (ec == NULL) |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 1493 | ec = drm_compositor_create(display, option_connector); |
Kristian Høgsberg | a941022 | 2011-01-14 17:22:35 -0500 | [diff] [blame] | 1494 | #endif |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1495 | |
Kristian Høgsberg | 841883b | 2008-12-05 11:19:56 -0500 | [diff] [blame] | 1496 | if (ec == NULL) { |
| 1497 | fprintf(stderr, "failed to create compositor\n"); |
| 1498 | exit(EXIT_FAILURE); |
| 1499 | } |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 1500 | |
Kristian Høgsberg | 2bb3ebe | 2010-12-01 15:36:20 -0500 | [diff] [blame] | 1501 | if (wl_display_add_socket(display, option_socket_name)) { |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1502 | fprintf(stderr, "failed to add socket: %m\n"); |
| 1503 | exit(EXIT_FAILURE); |
| 1504 | } |
| 1505 | |
Kristian Høgsberg | 50dc698 | 2010-12-01 16:43:56 -0500 | [diff] [blame] | 1506 | loop = wl_display_get_event_loop(ec->wl_display); |
| 1507 | wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec); |
| 1508 | wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec); |
| 1509 | |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1510 | wl_display_run(display); |
| 1511 | |
Kristian Høgsberg | 2bb3ebe | 2010-12-01 15:36:20 -0500 | [diff] [blame] | 1512 | wl_display_destroy(display); |
| 1513 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1514 | ec->destroy(ec); |
| 1515 | |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1516 | return 0; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1517 | } |