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