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