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