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