blob: 8f50aef875ac79bd7a0c8b85dcaca0aae7011109 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05004 * 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øgsbergffd710e2008-12-02 15:15:01 -05008 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05009 * 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øgsbergffd710e2008-12-02 15:15:01 -050017 */
18
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040019#include <stdio.h>
20#include <string.h>
21#include <stdlib.h>
22#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050023#include <stdarg.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040024#include <sys/ioctl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040025#include <fcntl.h>
26#include <unistd.h>
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050027#include <cairo.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050028#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050029#include <math.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050030#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040031#include <linux/input.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050032
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050033#include "wayland.h"
Kristian Høgsberga1f3f602010-08-03 09:26:44 -040034#include "wayland-server-protocol.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050035#include "cairo-util.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040036#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050037
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040038const char *option_background = "background.jpg";
39int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050040
41static const GOptionEntry option_entries[] = {
42 { "background", 'b', 0, G_OPTION_ARG_STRING,
43 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -040044 { "connector", 'c', 0, G_OPTION_ARG_INT,
45 &option_connector, "KMS connector" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050046 { NULL }
47};
48
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050049static void
50wlsc_matrix_init(struct wlsc_matrix *matrix)
51{
52 static const struct wlsc_matrix identity = {
53 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
54 };
55
56 memcpy(matrix, &identity, sizeof identity);
57}
58
59static void
60wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
61{
62 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040063 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050064 div_t d;
65 int i, j;
66
67 for (i = 0; i < 16; i++) {
68 tmp.d[i] = 0;
69 d = div(i, 4);
70 row = m->d + d.quot * 4;
71 column = n->d + d.rem;
72 for (j = 0; j < 4; j++)
73 tmp.d[i] += row[j] * column[j * 4];
74 }
75 memcpy(m, &tmp, sizeof tmp);
76}
77
78static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040079wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050080{
Kristian Høgsberg27803c62010-06-06 22:23:21 -040081 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050082 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
83 };
84
85 wlsc_matrix_multiply(matrix, &translate);
86}
87
88static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040089wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050090{
91 struct wlsc_matrix scale = {
92 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
93 };
94
95 wlsc_matrix_multiply(matrix, &scale);
96}
97
98static void
99wlsc_matrix_rotate(struct wlsc_matrix *matrix,
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400100 GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500101{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400102 GLfloat c = cos(angle);
103 GLfloat s = sin(angle);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500104 struct wlsc_matrix rotate = {
105 { x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
Kristian Høgsbergfa548852010-03-08 17:16:01 -0500106 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) + x * s, 0,
107 x * z * (1 - c) + y * s, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500108 0, 0, 0, 1 }
109 };
110
111 wlsc_matrix_multiply(matrix, &rotate);
112}
113
114static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400115wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
116{
117 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400118 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400119
120 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400121 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400122 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400123 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400124 }
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400125
126 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400127}
128
129static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500130wlsc_surface_init(struct wlsc_surface *surface,
131 struct wlsc_compositor *compositor, struct wl_visual *visual,
132 int32_t x, int32_t y, int32_t width, int32_t height)
133{
134 glGenTextures(1, &surface->texture);
135 surface->compositor = compositor;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500136 surface->visual = visual;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400137 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400138 wlsc_matrix_scale(&surface->matrix, width, height, 1);
139 wlsc_matrix_translate(&surface->matrix, x, y, 0);
140
141 wlsc_matrix_init(&surface->matrix_inv);
142 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
143 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500144}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500145
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500146static struct wlsc_surface *
147wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500148 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500149 int x, int y, int width, int height)
150{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500151 struct wlsc_surface *es;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500152 int stride;
153 void *data;
154
155 stride = cairo_image_surface_get_stride(surface);
156 data = cairo_image_surface_get_data(surface);
157
158 es = malloc(sizeof *es);
159 if (es == NULL)
160 return NULL;
161
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500162 wlsc_surface_init(es, ec, &ec->premultiplied_argb_visual,
163 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500164 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400165 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
166 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500167 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
168 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500169 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400170 GL_RGBA, GL_UNSIGNED_BYTE, data);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500171
Kristian Høgsberg54879822008-11-23 17:07:32 -0500172 return es;
173}
174
175static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400176destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500177{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400178 struct wlsc_surface *surface =
179 container_of(resource, struct wlsc_surface, base.base);
180 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500181 struct wlsc_listener *l;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400182
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400183 wl_list_remove(&surface->link);
184 glDeleteTextures(1, &surface->texture);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400185
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500186 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400187 l->func(l, surface);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400188
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400189 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500190}
191
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400192static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500193pointer_path(cairo_t *cr, int x, int y)
194{
195 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
196 const int width = 16, height = 16;
197
198 cairo_move_to(cr, x, y);
199 cairo_line_to(cr, x + tx, y + ty);
200 cairo_line_to(cr, x + dx, y + dy);
201 cairo_line_to(cr, x + width - end, y + height);
202 cairo_line_to(cr, x + width, y + height - end);
203 cairo_line_to(cr, x + dy, y + dx);
204 cairo_line_to(cr, x + ty, y + tx);
205 cairo_close_path(cr);
206}
207
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500208static struct wlsc_surface *
209pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500210{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500211 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500212 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500213 cairo_surface_t *surface;
214 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500215
216 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
217 width, height);
218
219 cr = cairo_create(surface);
220 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500221 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500222 cairo_set_source_rgb(cr, 0, 0, 0);
223 cairo_stroke_preserve(cr);
224 cairo_fill(cr);
225 blur_surface(surface, width);
226
227 pointer_path(cr, hotspot_x, hotspot_y);
228 cairo_stroke_preserve(cr);
229 cairo_set_source_rgb(cr, 1, 1, 1);
230 cairo_fill(cr);
231 cairo_destroy(cr);
232
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500233 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500234 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500235 x - hotspot_x,
236 y - hotspot_y,
237 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500238
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500239 cairo_surface_destroy(surface);
240
Kristian Høgsberg54879822008-11-23 17:07:32 -0500241 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500242}
243
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500244static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500245background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500246{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500247 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500248 GdkPixbuf *pixbuf;
249 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500250 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500251 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500252
253 background = malloc(sizeof *background);
254 if (background == NULL)
255 return NULL;
256
257 g_type_init();
258
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500259 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500260 output->width,
261 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500262 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500263 if (error != NULL) {
264 free(background);
265 return NULL;
266 }
267
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500268 data = gdk_pixbuf_get_pixels(pixbuf);
269
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500270 wlsc_surface_init(background, output->compositor,
271 &output->compositor->rgb_visual,
272 output->x, output->y, output->width, output->height);
273
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500274 glBindTexture(GL_TEXTURE_2D, background->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400275 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
276 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500277 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
278 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500279
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500280 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500281 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500282 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500283 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500284
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500285 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
286 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500287 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500288
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500289 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500290}
291
292static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400293wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500294{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500295 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400296 struct wlsc_matrix tmp;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500297
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400298 tmp = es->matrix;
299 wlsc_matrix_multiply(&tmp, &output->matrix);
300 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
301 glUniform1i(ec->tex_uniform, 0);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500302
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500303 if (es->visual == &ec->argb_visual) {
304 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
305 glEnable(GL_BLEND);
306 } else if (es->visual == &ec->premultiplied_argb_visual) {
307 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
308 glEnable(GL_BLEND);
309 } else {
310 glDisable(GL_BLEND);
311 }
312
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500313 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500314 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400315 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
316 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
317 5 * sizeof(GLfloat), NULL);
318 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
319 5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
320 glEnableVertexAttribArray(0);
321 glEnableVertexAttribArray(1);
322 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500323}
324
325static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400326wlsc_surface_raise(struct wlsc_surface *surface)
327{
328 struct wlsc_compositor *compositor = surface->compositor;
329
330 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400331 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400332}
333
334static void
335wlsc_surface_lower(struct wlsc_surface *surface)
336{
337 struct wlsc_compositor *compositor = surface->compositor;
338
339 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400340 wl_list_insert(compositor->surface_list.prev, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400341}
342
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400343static void
344wlsc_surface_update_matrix(struct wlsc_surface *es)
345{
346 wlsc_matrix_init(&es->matrix);
347 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
348 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
349
350 wlsc_matrix_init(&es->matrix_inv);
351 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
352 wlsc_matrix_scale(&es->matrix_inv,
353 1.0 / es->width, 1.0 / es->height, 1);
354}
355
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400356void
357wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400358{
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400359 wl_display_post_frame(compositor->wl_display,
360 &compositor->base,
361 compositor->current_frame, msecs);
362
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400363 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400364 compositor->current_frame++;
365}
366
367static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400368wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400369{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500370 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500371 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500372 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500373
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500374 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500375
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500376 if (output->background)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400377 wlsc_surface_draw(output->background, output);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500378 else
379 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400380
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400381 wl_list_for_each_reverse(es, &ec->surface_list, link)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400382 wlsc_surface_draw(es, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400383
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400384 if (ec->focus)
385 wl_list_for_each(eid, &ec->input_device_list, link)
386 wlsc_surface_draw(eid->sprite, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500387}
388
389static void
390repaint(void *data)
391{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500392 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500393 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500394
395 if (!ec->repaint_needed) {
396 ec->repaint_on_timeout = 0;
397 return;
398 }
399
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500400 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400401 wlsc_output_repaint(output);
402
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400403 ec->present(ec);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500404
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500405 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400406}
407
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400408void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400409wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400410{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400411 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400412 if (compositor->repaint_on_timeout)
413 return;
414
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400415 wl_event_source_timer_update(compositor->timer_source, 1);
416 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400417}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500418
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500419static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500420surface_destroy(struct wl_client *client,
421 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400422{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500423 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500424
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400425 wl_resource_destroy(&surface->base, client);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400426
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400427 wlsc_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400428}
429
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500430static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500431surface_attach(struct wl_client *client,
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400432 struct wl_surface *surface, struct wl_buffer *buffer_base)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400433{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500434 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400435 struct wlsc_buffer *buffer = (struct wlsc_buffer *) buffer_base;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400436
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500437 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400438 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
439 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500440 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
441 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400442 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, buffer->image);
443 es->visual = buffer->visual;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400444}
445
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500446static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500447surface_map(struct wl_client *client,
448 struct wl_surface *surface,
449 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400450{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500451 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400452
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400453 es->x = x;
454 es->y = y;
455 es->width = width;
456 es->height = height;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400457
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400458 wlsc_surface_update_matrix(es);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400459}
460
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500461static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500462surface_damage(struct wl_client *client,
463 struct wl_surface *surface,
464 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500465{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500466 /* FIXME: This need to take a damage region, of course. */
467}
468
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500469const static struct wl_surface_interface surface_interface = {
470 surface_destroy,
471 surface_attach,
472 surface_map,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500473 surface_damage
474};
475
476static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400477shell_move(struct wl_client *client, struct wl_shell *shell,
478 struct wl_surface *surface,
479 struct wl_input_device *device, uint32_t time)
480{
481 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
482
483 if (wd->grab == WLSC_DEVICE_GRAB_MOTION &&
484 wd->grab_time == time &&
485 &wd->pointer_focus->base == surface) {
486 wd->grab = WLSC_DEVICE_GRAB_MOVE;
487 wd->grab_dx = wd->pointer_focus->x - wd->grab_x;
488 wd->grab_dy = wd->pointer_focus->y - wd->grab_y;
489 }
490}
491
492static void
493shell_resize(struct wl_client *client, struct wl_shell *shell,
494 struct wl_surface *surface,
495 struct wl_input_device *device, uint32_t time, uint32_t edges)
496{
497 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
498
499 if (edges == 0 || edges > 15 ||
500 (edges & 3) == 3 || (edges & 12) == 12)
501 return;
502
503 if (wd->grab == WLSC_DEVICE_GRAB_MOTION &&
504 wd->grab_time == time &&
505 &wd->pointer_focus->base == surface) {
506 wd->grab = edges;
507 wd->grab_dx = wd->pointer_focus->x - wd->grab_x;
508 wd->grab_dy = wd->pointer_focus->y - wd->grab_y;
509 wd->grab_width = wd->pointer_focus->width;
510 wd->grab_height = wd->pointer_focus->height;
511 }
512}
513
514const static struct wl_shell_interface shell_interface = {
515 shell_move,
516 shell_resize
517};
518
519static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500520compositor_create_surface(struct wl_client *client,
521 struct wl_compositor *compositor, uint32_t id)
522{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500523 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400524 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500525
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400526 surface = malloc(sizeof *surface);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400527 if (surface == NULL) {
528 wl_client_post_event(client,
529 (struct wl_object *) ec->wl_display,
530 WL_DISPLAY_NO_MEMORY, 0);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500531 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400532 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500533
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400534 wlsc_surface_init(surface, ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500535
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400536 wl_list_insert(ec->surface_list.prev, &surface->link);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400537 surface->base.base.destroy = destroy_surface;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400538 wl_client_add_surface(client, &surface->base,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500539 &surface_interface, id);
540}
541
542static void
543compositor_commit(struct wl_client *client,
544 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500545{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500546 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500547
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400548 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500549 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500550}
551
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500552const static struct wl_compositor_interface compositor_interface = {
553 compositor_create_surface,
554 compositor_commit
555};
556
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500557static void
558wlsc_surface_transform(struct wlsc_surface *surface,
559 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
560{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400561 struct wlsc_vector v = { { x, y, 0, 1 } };
562
563 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400564 *sx = v.f[0] * surface->width;
565 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500566}
567
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500568static void
569wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400570 struct wlsc_surface *surface,
571 uint32_t time)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500572{
573 if (device->keyboard_focus == surface)
574 return;
575
576 if (device->keyboard_focus &&
577 (!surface || device->keyboard_focus->base.client != surface->base.client))
578 wl_surface_post_event(&device->keyboard_focus->base,
579 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400580 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400581 time, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500582
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500583 if (surface)
584 wl_surface_post_event(&surface->base,
585 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400586 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400587 time, &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500588
589 device->keyboard_focus = surface;
590}
591
592static void
593wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400594 struct wlsc_surface *surface,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400595 uint32_t time,
596 int32_t x, int32_t y,
597 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500598{
599 if (device->pointer_focus == surface)
600 return;
601
602 if (device->pointer_focus &&
603 (!surface || device->pointer_focus->base.client != surface->base.client))
604 wl_surface_post_event(&device->pointer_focus->base,
605 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400606 WL_INPUT_DEVICE_POINTER_FOCUS,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400607 time, NULL, 0, 0, 0, 0);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500608 if (surface)
609 wl_surface_post_event(&surface->base,
610 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400611 WL_INPUT_DEVICE_POINTER_FOCUS,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400612 time, &surface->base,
613 x, y, sx, sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500614
615 device->pointer_focus = surface;
616}
617
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500618static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500619pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500620{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500621 struct wlsc_compositor *ec = device->ec;
622 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500623
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400624 if (device->grab != WLSC_DEVICE_GRAB_NONE) {
625 wlsc_surface_transform(device->pointer_focus,
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500626 device->x, device->y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400627 return device->pointer_focus;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500628 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500629
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400630 wl_list_for_each(es, &ec->surface_list, link) {
631 wlsc_surface_transform(es, device->x, device->y, sx, sy);
632 if (0 <= *sx && *sx < es->width &&
633 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500634 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400635 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500636
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500637 return NULL;
638}
639
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500640void
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400641notify_motion(struct wlsc_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500642{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500643 struct wlsc_surface *es;
644 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500645 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500646 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400647 int32_t sx, sy, width, height;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500648
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500649 /* FIXME: We need some multi head love here. */
650 output = container_of(ec->output_list.next, struct wlsc_output, link);
651 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500652 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500653 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500654 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500655 if (x >= output->x + output->width)
656 x = output->x + output->width - 1;
657 if (y >= output->y + output->height)
658 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500659
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500660 device->x = x;
661 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500662
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400663 switch (device->grab) {
664 case WLSC_DEVICE_GRAB_NONE:
665 case WLSC_DEVICE_GRAB_MOTION:
666 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400667
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400668 wlsc_input_device_set_pointer_focus(device, es,
669 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400670
671 if (es)
672 wl_surface_post_event(&es->base, &device->base,
673 WL_INPUT_DEVICE_MOTION,
674 time, x, y, sx, sy);
675 break;
676
677 case WLSC_DEVICE_GRAB_MOVE:
678 es = device->pointer_focus;
679 es->x = x + device->grab_dx;
680 es->y = y + device->grab_dy;;
681 wl_surface_post_event(&es->base, &ec->shell,
682 WL_SHELL_CONFIGURE,
683 time, device->grab,
684 &es->base, es->x, es->y,
685 es->width, es->height);
686
687 wlsc_surface_update_matrix(es);
688
689 break;
690
691 case WLSC_DEVICE_GRAB_RESIZE_TOP:
692 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
693 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
694 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
695 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
696 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
697 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
698 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
699 case WLSC_DEVICE_GRAB_RESIZE_MASK:
700 es = device->pointer_focus;
701
702 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_LEFT) {
703 sx = x + device->grab_dx;
704 width = device->grab_x - x + device->grab_width;
705 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_RIGHT) {
706 sx = device->grab_x + device->grab_dx;
707 width = x - device->grab_x + device->grab_width;
708 } else {
709 sx = device->grab_x + device->grab_dx;
710 width = device->grab_width;
711 }
712
713 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_TOP) {
714 sy = y + device->grab_dy;
715 height = device->grab_y - y + device->grab_height;
716 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_BOTTOM) {
717 sy = device->grab_y + device->grab_dy;
718 height = y - device->grab_y + device->grab_height;
719 } else {
720 sy = device->grab_y + device->grab_dy;
721 height = device->grab_height;
722 }
723
724 wl_surface_post_event(&es->base, &ec->shell,
725 WL_SHELL_CONFIGURE, time, device->grab,
726 &es->base, sx, sy, width, height);
727 break;
728 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500729
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400730 wlsc_matrix_init(&device->sprite->matrix);
731 wlsc_matrix_scale(&device->sprite->matrix, 64, 64, 1);
732 wlsc_matrix_translate(&device->sprite->matrix,
733 x - hotspot_x, y - hotspot_y, 0);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500734
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400735 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500736}
737
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500738void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500739notify_button(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400740 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500741{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400742 struct wlsc_surface *surface;
743 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500744
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400745 surface = device->pointer_focus;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400746 if (surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400747 if (state && device->grab == WLSC_DEVICE_GRAB_NONE) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400748 wlsc_surface_raise(surface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400749 device->grab = WLSC_DEVICE_GRAB_MOTION;
750 device->grab_time = time;
751 device->grab_x = device->x;
752 device->grab_y = device->y;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400753 wlsc_input_device_set_keyboard_focus(device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400754 surface, time);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500755 } else {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400756 device->grab = WLSC_DEVICE_GRAB_NONE;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500757 }
758
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400759 if (state && button == BTN_LEFT &&
760 device->grab == WLSC_DEVICE_GRAB_MOTION &&
761 (device->modifier_state & MODIFIER_SUPER))
762 shell_move(NULL, &compositor->shell,
763 &surface->base, device, time);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400764 else if (state && button == BTN_MIDDLE &&
765 device->grab == WLSC_DEVICE_GRAB_MOTION &&
766 (device->modifier_state & MODIFIER_SUPER))
767 shell_resize(NULL, &compositor->shell,
768 &surface->base, device, time,
769 WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT);
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400770 else
771 wl_surface_post_event(&surface->base, &device->base,
772 WL_INPUT_DEVICE_BUTTON,
773 time, button, state);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500774
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400775 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500776 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500777}
778
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500779void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500780notify_key(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400781 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500782{
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500783 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400784 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500785
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400786 switch (key | device->modifier_state) {
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400787 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400788 kill(0, SIGTERM);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500789 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500790 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500791
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400792 switch (key) {
793 case KEY_LEFTCTRL:
794 case KEY_RIGHTCTRL:
795 modifier = MODIFIER_CTRL;
796 break;
797
798 case KEY_LEFTALT:
799 case KEY_RIGHTALT:
800 modifier = MODIFIER_ALT;
801 break;
802
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400803 case KEY_LEFTMETA:
804 case KEY_RIGHTMETA:
805 modifier = MODIFIER_SUPER;
806 break;
807
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400808 default:
809 modifier = 0;
810 break;
811 }
812
813 if (state)
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400814 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400815 else
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400816 device->modifier_state &= ~modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400817
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500818 end = device->keys.data + device->keys.size;
819 for (k = device->keys.data; k < end; k++) {
820 if (*k == key)
821 *k = *--end;
822 }
823 device->keys.size = (void *) end - device->keys.data;
824 if (state) {
825 k = wl_array_add(&device->keys, sizeof *k);
826 *k = key;
827 }
Ray Strodee96dcb82008-12-20 02:00:49 -0500828
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500829 if (device->keyboard_focus != NULL)
830 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400831 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400832 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400833}
834
835static uint32_t
836get_time(void)
837{
838 struct timeval tv;
839
840 gettimeofday(&tv, NULL);
841
842 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500843}
844
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400845static void
846handle_surface_destroy(struct wlsc_listener *listener,
847 struct wlsc_surface *surface)
848{
849 struct wlsc_input_device *device =
850 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400851 struct wlsc_surface *focus;
852 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400853 uint32_t time = get_time();
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400854
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400855 if (device->keyboard_focus == surface)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400856 wlsc_input_device_set_keyboard_focus(device, NULL, time);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400857 if (device->pointer_focus == surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400858 device->grab = WLSC_DEVICE_GRAB_NONE;
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400859 focus = pick_surface(device, &sx, &sy);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400860 wlsc_input_device_set_pointer_focus(device, focus, time,
861 device->x, device->y,
862 sx, sy);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400863 fprintf(stderr, "lost pointer focus surface, reverting to %p\n", focus);
864 }
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400865}
866
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400867void
868wlsc_input_device_init(struct wlsc_input_device *device,
869 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500870{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500871 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500872 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500873 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500874 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400875
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500876 device->x = 100;
877 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500878 device->ec = ec;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400879 device->sprite = pointer_create(ec, device->x, device->y, 64, 64);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500880
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400881 device->listener.func = handle_surface_destroy;
882 wl_list_insert(ec->surface_destroy_listener_list.prev,
883 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500884 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500885}
886
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500887static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400888wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500889{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500890 struct wlsc_output *output =
891 container_of(global, struct wlsc_output, base);
892
893 wl_client_post_event(client, global,
894 WL_OUTPUT_GEOMETRY,
895 output->width, output->height);
896}
897
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400898static const char vertex_shader[] =
899 "uniform mat4 proj;\n"
900 "attribute vec4 position;\n"
901 "attribute vec2 texcoord;\n"
902 "varying vec2 v_texcoord;\n"
903 "void main()\n"
904 "{\n"
905 " gl_Position = proj * position;\n"
906 " v_texcoord = texcoord;\n"
907 "}\n";
908
909static const char fragment_shader[] =
910 /* "precision mediump float;\n" */
911 "varying vec2 v_texcoord;\n"
912 "uniform sampler2D tex;\n"
913 "void main()\n"
914 "{\n"
915 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
916 "}\n";
917
Kristian Høgsberga9468212010-06-14 21:03:11 -0400918static int
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400919init_shaders(struct wlsc_compositor *ec)
920{
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400921 GLuint v, f, program;
922 const char *p;
923 char msg[512];
924 GLfloat vertices[4 * 5];
925 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400926
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400927 p = vertex_shader;
928 v = glCreateShader(GL_VERTEX_SHADER);
929 glShaderSource(v, 1, &p, NULL);
930 glCompileShader(v);
931 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
932 if (!status) {
933 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
934 fprintf(stderr, "vertex shader info: %s\n", msg);
935 return -1;
936 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400937
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400938 p = fragment_shader;
939 f = glCreateShader(GL_FRAGMENT_SHADER);
940 glShaderSource(f, 1, &p, NULL);
941 glCompileShader(f);
942 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
943 if (!status) {
944 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
945 fprintf(stderr, "fragment shader info: %s\n", msg);
946 return -1;
947 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400948
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400949 program = glCreateProgram();
950 glAttachShader(program, v);
951 glAttachShader(program, f);
952 glBindAttribLocation(program, 0, "position");
953 glBindAttribLocation(program, 1, "texcoord");
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400954
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400955 glLinkProgram(program);
956 glGetProgramiv(program, GL_LINK_STATUS, &status);
957 if (!status) {
958 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
959 fprintf(stderr, "link info: %s\n", msg);
960 return -1;
961 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400962
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400963 glUseProgram(program);
964 ec->proj_uniform = glGetUniformLocation(program, "proj");
965 ec->tex_uniform = glGetUniformLocation(program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400966
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400967 vertices[ 0] = 0.0;
968 vertices[ 1] = 0.0;
969 vertices[ 2] = 0.0;
970 vertices[ 3] = 0.0;
971 vertices[ 4] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400972
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400973 vertices[ 5] = 0.0;
974 vertices[ 6] = 1.0;
975 vertices[ 7] = 0.0;
976 vertices[ 8] = 0.0;
977 vertices[ 9] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400978
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400979 vertices[10] = 1.0;
980 vertices[11] = 0.0;
981 vertices[12] = 0.0;
982 vertices[13] = 1.0;
983 vertices[14] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400984
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400985 vertices[15] = 1.0;
986 vertices[16] = 1.0;
987 vertices[17] = 0.0;
988 vertices[18] = 1.0;
989 vertices[19] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400990
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400991 glGenBuffers(1, &ec->vbo);
992 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
993 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
Kristian Høgsberga9468212010-06-14 21:03:11 -0400994
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400995 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400996}
997
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500998static const struct wl_interface visual_interface = {
999 "visual", 1,
1000};
1001
1002static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001003add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001004{
1005 ec->argb_visual.base.interface = &visual_interface;
1006 ec->argb_visual.base.implementation = NULL;
1007 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001008 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001009
1010 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1011 ec->premultiplied_argb_visual.base.implementation = NULL;
1012 wl_display_add_object(ec->wl_display,
1013 &ec->premultiplied_argb_visual.base);
1014 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001015 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001016
1017 ec->rgb_visual.base.interface = &visual_interface;
1018 ec->rgb_visual.base.implementation = NULL;
1019 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001020 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1021}
1022
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001023void
1024wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1025 int x, int y, int width, int height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001026{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001027 output->compositor = c;
1028 output->x = x;
1029 output->y = y;
1030 output->width = width;
1031 output->height = height;
1032
1033 output->background =
1034 background_create(output, option_background);
1035
1036 wlsc_matrix_init(&output->matrix);
1037 wlsc_matrix_translate(&output->matrix,
1038 -output->x - output->width / 2.0,
1039 -output->y - output->height / 2.0, 0);
1040 wlsc_matrix_scale(&output->matrix,
1041 2.0 / output->width, 2.0 / output->height, 1);
1042
1043 output->base.interface = &wl_output_interface;
1044 wl_display_add_object(c->wl_display, &output->base);
1045 wl_display_add_global(c->wl_display, &output->base,
1046 wlsc_output_post_geometry);
1047}
1048
1049int
1050wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1051{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001052 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001053
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001054 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001055
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001056 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001057
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001058 ec->shell.interface = &wl_shell_interface;
1059 ec->shell.implementation = (void (**)(void)) &shell_interface;
1060 wl_display_add_object(display, &ec->shell);
1061 if (wl_display_add_global(display, &ec->shell, NULL))
1062 return -1;
1063
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001064 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001065
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001066 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001067 wl_list_init(&ec->input_device_list);
1068 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001069 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001070
1071 screenshooter_create(ec);
1072
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001073 glGenFramebuffers(1, &ec->fbo);
1074 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1075 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001076 if (init_shaders(ec) < 0)
1077 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001078
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001079 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001080 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001081 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001082
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001083 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001084}
1085
1086/* The plan here is to generate a random anonymous socket name and
1087 * advertise that through a service on the session dbus.
1088 */
1089static const char socket_name[] = "\0wayland";
1090
1091int main(int argc, char *argv[])
1092{
1093 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001094 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001095 GError *error = NULL;
1096 GOptionContext *context;
1097
1098 context = g_option_context_new(NULL);
1099 g_option_context_add_main_entries(context, option_entries, "Wayland");
1100 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1101 fprintf(stderr, "option parsing failed: %s\n", error->message);
1102 exit(EXIT_FAILURE);
1103 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001104
1105 display = wl_display_create();
1106
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001107 if (getenv("DISPLAY"))
1108 ec = x11_compositor_create(display);
1109 else
1110 ec = drm_compositor_create(display);
1111
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001112 if (ec == NULL) {
1113 fprintf(stderr, "failed to create compositor\n");
1114 exit(EXIT_FAILURE);
1115 }
1116
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001117 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001118 fprintf(stderr, "failed to add socket: %m\n");
1119 exit(EXIT_FAILURE);
1120 }
1121
1122 wl_display_run(display);
1123
1124 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001125}