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