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