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