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