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