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