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