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