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