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