blob: 454d81e4348e7325a858469e4e3977279306c030 [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øgsberga1f3f602010-08-03 09:26:44 -040033#include "wayland-server-protocol.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050034#include "cairo-util.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040035#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050036
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040037const char *option_background = "background.jpg";
38int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050039
40static const GOptionEntry option_entries[] = {
41 { "background", 'b', 0, G_OPTION_ARG_STRING,
42 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -040043 { "connector", 'c', 0, G_OPTION_ARG_INT,
44 &option_connector, "KMS connector" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050045 { NULL }
46};
47
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050048static void
49wlsc_matrix_init(struct wlsc_matrix *matrix)
50{
51 static const struct wlsc_matrix identity = {
52 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
53 };
54
55 memcpy(matrix, &identity, sizeof identity);
56}
57
58static void
59wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
60{
61 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040062 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050063 div_t d;
64 int i, j;
65
66 for (i = 0; i < 16; i++) {
67 tmp.d[i] = 0;
68 d = div(i, 4);
69 row = m->d + d.quot * 4;
70 column = n->d + d.rem;
71 for (j = 0; j < 4; j++)
72 tmp.d[i] += row[j] * column[j * 4];
73 }
74 memcpy(m, &tmp, sizeof tmp);
75}
76
77static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040078wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050079{
Kristian Høgsberg27803c62010-06-06 22:23:21 -040080 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050081 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
82 };
83
84 wlsc_matrix_multiply(matrix, &translate);
85}
86
87static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040088wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050089{
90 struct wlsc_matrix scale = {
91 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
92 };
93
94 wlsc_matrix_multiply(matrix, &scale);
95}
96
97static void
98wlsc_matrix_rotate(struct wlsc_matrix *matrix,
Kristian Høgsberg27803c62010-06-06 22:23:21 -040099 GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500100{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400101 GLfloat c = cos(angle);
102 GLfloat s = sin(angle);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500103 struct wlsc_matrix rotate = {
104 { x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
Kristian Høgsbergfa548852010-03-08 17:16:01 -0500105 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) + x * s, 0,
106 x * z * (1 - c) + y * s, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500107 0, 0, 0, 1 }
108 };
109
110 wlsc_matrix_multiply(matrix, &rotate);
111}
112
113static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400114wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
115{
116 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400117 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400118
119 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400120 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400121 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400122 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400123 }
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400124
125 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400126}
127
128static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500129wlsc_surface_init(struct wlsc_surface *surface,
130 struct wlsc_compositor *compositor, struct wl_visual *visual,
131 int32_t x, int32_t y, int32_t width, int32_t height)
132{
133 glGenTextures(1, &surface->texture);
134 surface->compositor = compositor;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500135 surface->visual = visual;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400136 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400137 wlsc_matrix_scale(&surface->matrix, width, height, 1);
138 wlsc_matrix_translate(&surface->matrix, x, y, 0);
139
140 wlsc_matrix_init(&surface->matrix_inv);
141 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
142 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500143}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500144
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500145static struct wlsc_surface *
146wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500147 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500148 int x, int y, int width, int height)
149{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500150 struct wlsc_surface *es;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500151 int stride;
152 void *data;
153
154 stride = cairo_image_surface_get_stride(surface);
155 data = cairo_image_surface_get_data(surface);
156
157 es = malloc(sizeof *es);
158 if (es == NULL)
159 return NULL;
160
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500161 wlsc_surface_init(es, ec, &ec->premultiplied_argb_visual,
162 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500163 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400164 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
165 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500166 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
167 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500168 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400169 GL_RGBA, GL_UNSIGNED_BYTE, data);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500170
Kristian Høgsberg54879822008-11-23 17:07:32 -0500171 return es;
172}
173
174static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400175destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500176{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400177 struct wlsc_surface *surface =
178 container_of(resource, struct wlsc_surface, base.base);
179 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500180 struct wlsc_listener *l;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400181
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400182 wl_list_remove(&surface->link);
183 glDeleteTextures(1, &surface->texture);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400184
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500185 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400186 l->func(l, surface);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400187
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400188 free(surface);
Kristian Høgsberg117d5132010-08-11 08:56:47 -0400189
190 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500191}
192
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400193static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500194pointer_path(cairo_t *cr, int x, int y)
195{
196 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
197 const int width = 16, height = 16;
198
199 cairo_move_to(cr, x, y);
200 cairo_line_to(cr, x + tx, y + ty);
201 cairo_line_to(cr, x + dx, y + dy);
202 cairo_line_to(cr, x + width - end, y + height);
203 cairo_line_to(cr, x + width, y + height - end);
204 cairo_line_to(cr, x + dy, y + dx);
205 cairo_line_to(cr, x + ty, y + tx);
206 cairo_close_path(cr);
207}
208
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500209static struct wlsc_surface *
210pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500211{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500212 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500213 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500214 cairo_surface_t *surface;
215 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500216
217 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
218 width, height);
219
220 cr = cairo_create(surface);
221 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500222 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500223 cairo_set_source_rgb(cr, 0, 0, 0);
224 cairo_stroke_preserve(cr);
225 cairo_fill(cr);
226 blur_surface(surface, width);
227
228 pointer_path(cr, hotspot_x, hotspot_y);
229 cairo_stroke_preserve(cr);
230 cairo_set_source_rgb(cr, 1, 1, 1);
231 cairo_fill(cr);
232 cairo_destroy(cr);
233
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500234 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500235 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500236 x - hotspot_x,
237 y - hotspot_y,
238 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500239
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500240 cairo_surface_destroy(surface);
241
Kristian Høgsberg54879822008-11-23 17:07:32 -0500242 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500243}
244
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500245static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500246background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500247{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500248 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500249 GdkPixbuf *pixbuf;
250 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500251 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500252 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500253
254 background = malloc(sizeof *background);
255 if (background == NULL)
256 return NULL;
257
258 g_type_init();
259
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500260 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500261 output->width,
262 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500263 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500264 if (error != NULL) {
265 free(background);
266 return NULL;
267 }
268
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500269 data = gdk_pixbuf_get_pixels(pixbuf);
270
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500271 wlsc_surface_init(background, output->compositor,
272 &output->compositor->rgb_visual,
273 output->x, output->y, output->width, output->height);
274
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500275 glBindTexture(GL_TEXTURE_2D, background->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400276 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
277 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500278 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
279 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500280
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500281 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500282 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500283 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500284 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500285
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500286 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
287 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500288 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500289
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500290 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500291}
292
293static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400294wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500295{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500296 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400297 struct wlsc_matrix tmp;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500298
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400299 tmp = es->matrix;
300 wlsc_matrix_multiply(&tmp, &output->matrix);
301 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
302 glUniform1i(ec->tex_uniform, 0);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500303
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500304 if (es->visual == &ec->argb_visual) {
305 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
306 glEnable(GL_BLEND);
307 } else if (es->visual == &ec->premultiplied_argb_visual) {
308 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
309 glEnable(GL_BLEND);
310 } else {
311 glDisable(GL_BLEND);
312 }
313
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500314 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500315 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400316 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
317 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
318 5 * sizeof(GLfloat), NULL);
319 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
320 5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
321 glEnableVertexAttribArray(0);
322 glEnableVertexAttribArray(1);
323 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500324}
325
326static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400327wlsc_surface_raise(struct wlsc_surface *surface)
328{
329 struct wlsc_compositor *compositor = surface->compositor;
330
331 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400332 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400333}
334
335static void
336wlsc_surface_lower(struct wlsc_surface *surface)
337{
338 struct wlsc_compositor *compositor = surface->compositor;
339
340 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400341 wl_list_insert(compositor->surface_list.prev, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400342}
343
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400344static void
345wlsc_surface_update_matrix(struct wlsc_surface *es)
346{
347 wlsc_matrix_init(&es->matrix);
348 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
349 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
350
351 wlsc_matrix_init(&es->matrix_inv);
352 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
353 wlsc_matrix_scale(&es->matrix_inv,
354 1.0 / es->width, 1.0 / es->height, 1);
355}
356
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400357void
358wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400359{
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400360 wl_display_post_frame(compositor->wl_display,
361 &compositor->base,
362 compositor->current_frame, msecs);
363
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400364 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400365 compositor->current_frame++;
366}
367
368static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400369wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400370{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500371 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500372 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500373 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500374
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500375 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500376
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500377 if (output->background)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400378 wlsc_surface_draw(output->background, output);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500379 else
380 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400381
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400382 wl_list_for_each_reverse(es, &ec->surface_list, link)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400383 wlsc_surface_draw(es, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400384
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400385 if (ec->focus)
386 wl_list_for_each(eid, &ec->input_device_list, link)
387 wlsc_surface_draw(eid->sprite, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500388}
389
390static void
391repaint(void *data)
392{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500393 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500394 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500395
396 if (!ec->repaint_needed) {
397 ec->repaint_on_timeout = 0;
398 return;
399 }
400
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500401 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400402 wlsc_output_repaint(output);
403
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400404 ec->present(ec);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500405
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500406 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400407}
408
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400409void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400410wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400411{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400412 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400413 if (compositor->repaint_on_timeout)
414 return;
415
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400416 wl_event_source_timer_update(compositor->timer_source, 1);
417 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400418}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500419
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500420static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500421surface_destroy(struct wl_client *client,
422 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400423{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400424 wl_resource_destroy(&surface->base, client);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400425}
426
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500427static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500428surface_attach(struct wl_client *client,
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400429 struct wl_surface *surface, struct wl_buffer *buffer_base)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400430{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500431 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400432 struct wlsc_buffer *buffer = (struct wlsc_buffer *) buffer_base;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400433
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500434 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400435 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
436 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500437 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
438 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400439 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, buffer->image);
440 es->visual = buffer->visual;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400441}
442
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500443static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500444surface_map(struct wl_client *client,
445 struct wl_surface *surface,
446 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400447{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500448 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400449
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400450 es->x = x;
451 es->y = y;
452 es->width = width;
453 es->height = height;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400454
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400455 wlsc_surface_update_matrix(es);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400456}
457
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500458static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500459surface_damage(struct wl_client *client,
460 struct wl_surface *surface,
461 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500462{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500463 /* FIXME: This need to take a damage region, of course. */
464}
465
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500466const static struct wl_surface_interface surface_interface = {
467 surface_destroy,
468 surface_attach,
469 surface_map,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500470 surface_damage
471};
472
473static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400474shell_move(struct wl_client *client, struct wl_shell *shell,
475 struct wl_surface *surface,
476 struct wl_input_device *device, uint32_t time)
477{
478 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
479
480 if (wd->grab == WLSC_DEVICE_GRAB_MOTION &&
481 wd->grab_time == time &&
482 &wd->pointer_focus->base == surface) {
483 wd->grab = WLSC_DEVICE_GRAB_MOVE;
484 wd->grab_dx = wd->pointer_focus->x - wd->grab_x;
485 wd->grab_dy = wd->pointer_focus->y - wd->grab_y;
486 }
487}
488
489static void
490shell_resize(struct wl_client *client, struct wl_shell *shell,
491 struct wl_surface *surface,
492 struct wl_input_device *device, uint32_t time, uint32_t edges)
493{
494 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
495
496 if (edges == 0 || edges > 15 ||
497 (edges & 3) == 3 || (edges & 12) == 12)
498 return;
499
500 if (wd->grab == WLSC_DEVICE_GRAB_MOTION &&
501 wd->grab_time == time &&
502 &wd->pointer_focus->base == surface) {
503 wd->grab = edges;
504 wd->grab_dx = wd->pointer_focus->x - wd->grab_x;
505 wd->grab_dy = wd->pointer_focus->y - wd->grab_y;
506 wd->grab_width = wd->pointer_focus->width;
507 wd->grab_height = wd->pointer_focus->height;
508 }
509}
510
511const static struct wl_shell_interface shell_interface = {
512 shell_move,
513 shell_resize
514};
515
516static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500517compositor_create_surface(struct wl_client *client,
518 struct wl_compositor *compositor, uint32_t id)
519{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500520 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400521 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500522
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400523 surface = malloc(sizeof *surface);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400524 if (surface == NULL) {
525 wl_client_post_event(client,
526 (struct wl_object *) ec->wl_display,
527 WL_DISPLAY_NO_MEMORY, 0);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500528 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400529 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500530
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400531 wlsc_surface_init(surface, ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500532
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400533 wl_list_insert(ec->surface_list.prev, &surface->link);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400534 surface->base.base.destroy = destroy_surface;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400535 wl_client_add_surface(client, &surface->base,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500536 &surface_interface, id);
537}
538
539static void
540compositor_commit(struct wl_client *client,
541 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500542{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500543 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500544
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400545 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500546 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500547}
548
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500549const static struct wl_compositor_interface compositor_interface = {
550 compositor_create_surface,
551 compositor_commit
552};
553
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500554static void
555wlsc_surface_transform(struct wlsc_surface *surface,
556 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
557{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400558 struct wlsc_vector v = { { x, y, 0, 1 } };
559
560 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400561 *sx = v.f[0] * surface->width;
562 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500563}
564
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500565static void
566wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400567 struct wlsc_surface *surface,
568 uint32_t time)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500569{
570 if (device->keyboard_focus == surface)
571 return;
572
573 if (device->keyboard_focus &&
574 (!surface || device->keyboard_focus->base.client != surface->base.client))
575 wl_surface_post_event(&device->keyboard_focus->base,
576 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400577 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400578 time, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500579
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500580 if (surface)
581 wl_surface_post_event(&surface->base,
582 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400583 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400584 time, &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500585
586 device->keyboard_focus = surface;
587}
588
589static void
590wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400591 struct wlsc_surface *surface,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400592 uint32_t time,
593 int32_t x, int32_t y,
594 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500595{
596 if (device->pointer_focus == surface)
597 return;
598
599 if (device->pointer_focus &&
600 (!surface || device->pointer_focus->base.client != surface->base.client))
601 wl_surface_post_event(&device->pointer_focus->base,
602 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400603 WL_INPUT_DEVICE_POINTER_FOCUS,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400604 time, NULL, 0, 0, 0, 0);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500605 if (surface)
606 wl_surface_post_event(&surface->base,
607 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400608 WL_INPUT_DEVICE_POINTER_FOCUS,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400609 time, &surface->base,
610 x, y, sx, sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500611
612 device->pointer_focus = surface;
613}
614
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500615static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500616pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500617{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500618 struct wlsc_compositor *ec = device->ec;
619 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500620
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400621 if (device->grab != WLSC_DEVICE_GRAB_NONE) {
622 wlsc_surface_transform(device->pointer_focus,
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500623 device->x, device->y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400624 return device->pointer_focus;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500625 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500626
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400627 wl_list_for_each(es, &ec->surface_list, link) {
628 wlsc_surface_transform(es, device->x, device->y, sx, sy);
629 if (0 <= *sx && *sx < es->width &&
630 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500631 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400632 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500633
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500634 return NULL;
635}
636
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500637void
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400638notify_motion(struct wlsc_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500639{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500640 struct wlsc_surface *es;
641 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500642 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500643 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400644 int32_t sx, sy, width, height;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500645
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500646 /* FIXME: We need some multi head love here. */
647 output = container_of(ec->output_list.next, struct wlsc_output, link);
648 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500649 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500650 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500651 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500652 if (x >= output->x + output->width)
653 x = output->x + output->width - 1;
654 if (y >= output->y + output->height)
655 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500656
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500657 device->x = x;
658 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500659
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400660 switch (device->grab) {
661 case WLSC_DEVICE_GRAB_NONE:
662 case WLSC_DEVICE_GRAB_MOTION:
663 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400664
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400665 wlsc_input_device_set_pointer_focus(device, es,
666 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400667
668 if (es)
669 wl_surface_post_event(&es->base, &device->base,
670 WL_INPUT_DEVICE_MOTION,
671 time, x, y, sx, sy);
672 break;
673
674 case WLSC_DEVICE_GRAB_MOVE:
675 es = device->pointer_focus;
676 es->x = x + device->grab_dx;
677 es->y = y + device->grab_dy;;
678 wl_surface_post_event(&es->base, &ec->shell,
679 WL_SHELL_CONFIGURE,
680 time, device->grab,
681 &es->base, es->x, es->y,
682 es->width, es->height);
683
684 wlsc_surface_update_matrix(es);
685
686 break;
687
688 case WLSC_DEVICE_GRAB_RESIZE_TOP:
689 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
690 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
691 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
692 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
693 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
694 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
695 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
696 case WLSC_DEVICE_GRAB_RESIZE_MASK:
697 es = device->pointer_focus;
698
699 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_LEFT) {
700 sx = x + device->grab_dx;
701 width = device->grab_x - x + device->grab_width;
702 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_RIGHT) {
703 sx = device->grab_x + device->grab_dx;
704 width = x - device->grab_x + device->grab_width;
705 } else {
706 sx = device->grab_x + device->grab_dx;
707 width = device->grab_width;
708 }
709
710 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_TOP) {
711 sy = y + device->grab_dy;
712 height = device->grab_y - y + device->grab_height;
713 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_BOTTOM) {
714 sy = device->grab_y + device->grab_dy;
715 height = y - device->grab_y + device->grab_height;
716 } else {
717 sy = device->grab_y + device->grab_dy;
718 height = device->grab_height;
719 }
720
721 wl_surface_post_event(&es->base, &ec->shell,
722 WL_SHELL_CONFIGURE, time, device->grab,
723 &es->base, sx, sy, width, height);
724 break;
725 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500726
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400727 wlsc_matrix_init(&device->sprite->matrix);
728 wlsc_matrix_scale(&device->sprite->matrix, 64, 64, 1);
729 wlsc_matrix_translate(&device->sprite->matrix,
730 x - hotspot_x, y - hotspot_y, 0);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500731
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400732 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500733}
734
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500735void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500736notify_button(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400737 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500738{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400739 struct wlsc_surface *surface;
740 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500741
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400742 surface = device->pointer_focus;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400743 if (surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400744 if (state && device->grab == WLSC_DEVICE_GRAB_NONE) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400745 wlsc_surface_raise(surface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400746 device->grab = WLSC_DEVICE_GRAB_MOTION;
747 device->grab_time = time;
748 device->grab_x = device->x;
749 device->grab_y = device->y;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400750 wlsc_input_device_set_keyboard_focus(device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400751 surface, time);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500752 } else {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400753 device->grab = WLSC_DEVICE_GRAB_NONE;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500754 }
755
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400756 if (state && button == BTN_LEFT &&
757 device->grab == WLSC_DEVICE_GRAB_MOTION &&
758 (device->modifier_state & MODIFIER_SUPER))
759 shell_move(NULL, &compositor->shell,
760 &surface->base, device, time);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400761 else if (state && button == BTN_MIDDLE &&
762 device->grab == WLSC_DEVICE_GRAB_MOTION &&
763 (device->modifier_state & MODIFIER_SUPER))
764 shell_resize(NULL, &compositor->shell,
765 &surface->base, device, time,
766 WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT);
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400767 else
768 wl_surface_post_event(&surface->base, &device->base,
769 WL_INPUT_DEVICE_BUTTON,
770 time, button, state);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500771
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400772 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500773 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500774}
775
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500776void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500777notify_key(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400778 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500779{
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500780 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400781 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500782
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400783 switch (key | device->modifier_state) {
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400784 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400785 kill(0, SIGTERM);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500786 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500787 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500788
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400789 switch (key) {
790 case KEY_LEFTCTRL:
791 case KEY_RIGHTCTRL:
792 modifier = MODIFIER_CTRL;
793 break;
794
795 case KEY_LEFTALT:
796 case KEY_RIGHTALT:
797 modifier = MODIFIER_ALT;
798 break;
799
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400800 case KEY_LEFTMETA:
801 case KEY_RIGHTMETA:
802 modifier = MODIFIER_SUPER;
803 break;
804
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400805 default:
806 modifier = 0;
807 break;
808 }
809
810 if (state)
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400811 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400812 else
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400813 device->modifier_state &= ~modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400814
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500815 end = device->keys.data + device->keys.size;
816 for (k = device->keys.data; k < end; k++) {
817 if (*k == key)
818 *k = *--end;
819 }
820 device->keys.size = (void *) end - device->keys.data;
821 if (state) {
822 k = wl_array_add(&device->keys, sizeof *k);
823 *k = key;
824 }
Ray Strodee96dcb82008-12-20 02:00:49 -0500825
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500826 if (device->keyboard_focus != NULL)
827 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400828 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400829 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400830}
831
832static uint32_t
833get_time(void)
834{
835 struct timeval tv;
836
837 gettimeofday(&tv, NULL);
838
839 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500840}
841
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400842static void
843handle_surface_destroy(struct wlsc_listener *listener,
844 struct wlsc_surface *surface)
845{
846 struct wlsc_input_device *device =
847 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400848 struct wlsc_surface *focus;
849 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400850 uint32_t time = get_time();
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400851
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400852 if (device->keyboard_focus == surface)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400853 wlsc_input_device_set_keyboard_focus(device, NULL, time);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400854 if (device->pointer_focus == surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400855 device->grab = WLSC_DEVICE_GRAB_NONE;
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400856 focus = pick_surface(device, &sx, &sy);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400857 wlsc_input_device_set_pointer_focus(device, focus, time,
858 device->x, device->y,
859 sx, sy);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400860 fprintf(stderr, "lost pointer focus surface, reverting to %p\n", focus);
861 }
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400862}
863
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400864void
865wlsc_input_device_init(struct wlsc_input_device *device,
866 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500867{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500868 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500869 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500870 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500871 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400872
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500873 device->x = 100;
874 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500875 device->ec = ec;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400876 device->sprite = pointer_create(ec, device->x, device->y, 64, 64);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500877
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400878 device->listener.func = handle_surface_destroy;
879 wl_list_insert(ec->surface_destroy_listener_list.prev,
880 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500881 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500882}
883
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500884static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400885wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500886{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500887 struct wlsc_output *output =
888 container_of(global, struct wlsc_output, base);
889
890 wl_client_post_event(client, global,
891 WL_OUTPUT_GEOMETRY,
892 output->width, output->height);
893}
894
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400895static const char vertex_shader[] =
896 "uniform mat4 proj;\n"
897 "attribute vec4 position;\n"
898 "attribute vec2 texcoord;\n"
899 "varying vec2 v_texcoord;\n"
900 "void main()\n"
901 "{\n"
902 " gl_Position = proj * position;\n"
903 " v_texcoord = texcoord;\n"
904 "}\n";
905
906static const char fragment_shader[] =
907 /* "precision mediump float;\n" */
908 "varying vec2 v_texcoord;\n"
909 "uniform sampler2D tex;\n"
910 "void main()\n"
911 "{\n"
912 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
913 "}\n";
914
Kristian Høgsberga9468212010-06-14 21:03:11 -0400915static int
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400916init_shaders(struct wlsc_compositor *ec)
917{
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400918 GLuint v, f, program;
919 const char *p;
920 char msg[512];
921 GLfloat vertices[4 * 5];
922 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400923
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400924 p = vertex_shader;
925 v = glCreateShader(GL_VERTEX_SHADER);
926 glShaderSource(v, 1, &p, NULL);
927 glCompileShader(v);
928 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
929 if (!status) {
930 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
931 fprintf(stderr, "vertex shader info: %s\n", msg);
932 return -1;
933 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400934
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400935 p = fragment_shader;
936 f = glCreateShader(GL_FRAGMENT_SHADER);
937 glShaderSource(f, 1, &p, NULL);
938 glCompileShader(f);
939 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
940 if (!status) {
941 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
942 fprintf(stderr, "fragment shader info: %s\n", msg);
943 return -1;
944 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400945
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400946 program = glCreateProgram();
947 glAttachShader(program, v);
948 glAttachShader(program, f);
949 glBindAttribLocation(program, 0, "position");
950 glBindAttribLocation(program, 1, "texcoord");
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400951
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400952 glLinkProgram(program);
953 glGetProgramiv(program, GL_LINK_STATUS, &status);
954 if (!status) {
955 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
956 fprintf(stderr, "link info: %s\n", msg);
957 return -1;
958 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400959
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400960 glUseProgram(program);
961 ec->proj_uniform = glGetUniformLocation(program, "proj");
962 ec->tex_uniform = glGetUniformLocation(program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400963
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400964 vertices[ 0] = 0.0;
965 vertices[ 1] = 0.0;
966 vertices[ 2] = 0.0;
967 vertices[ 3] = 0.0;
968 vertices[ 4] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400969
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400970 vertices[ 5] = 0.0;
971 vertices[ 6] = 1.0;
972 vertices[ 7] = 0.0;
973 vertices[ 8] = 0.0;
974 vertices[ 9] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400975
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400976 vertices[10] = 1.0;
977 vertices[11] = 0.0;
978 vertices[12] = 0.0;
979 vertices[13] = 1.0;
980 vertices[14] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400981
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400982 vertices[15] = 1.0;
983 vertices[16] = 1.0;
984 vertices[17] = 0.0;
985 vertices[18] = 1.0;
986 vertices[19] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400987
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400988 glGenBuffers(1, &ec->vbo);
989 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
990 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
Kristian Høgsberga9468212010-06-14 21:03:11 -0400991
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400992 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400993}
994
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500995static const struct wl_interface visual_interface = {
996 "visual", 1,
997};
998
999static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001000add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001001{
1002 ec->argb_visual.base.interface = &visual_interface;
1003 ec->argb_visual.base.implementation = NULL;
1004 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001005 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001006
1007 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1008 ec->premultiplied_argb_visual.base.implementation = NULL;
1009 wl_display_add_object(ec->wl_display,
1010 &ec->premultiplied_argb_visual.base);
1011 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001012 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001013
1014 ec->rgb_visual.base.interface = &visual_interface;
1015 ec->rgb_visual.base.implementation = NULL;
1016 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001017 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1018}
1019
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001020void
1021wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1022 int x, int y, int width, int height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001023{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001024 output->compositor = c;
1025 output->x = x;
1026 output->y = y;
1027 output->width = width;
1028 output->height = height;
1029
1030 output->background =
1031 background_create(output, option_background);
1032
1033 wlsc_matrix_init(&output->matrix);
1034 wlsc_matrix_translate(&output->matrix,
1035 -output->x - output->width / 2.0,
1036 -output->y - output->height / 2.0, 0);
1037 wlsc_matrix_scale(&output->matrix,
1038 2.0 / output->width, 2.0 / output->height, 1);
1039
1040 output->base.interface = &wl_output_interface;
1041 wl_display_add_object(c->wl_display, &output->base);
1042 wl_display_add_global(c->wl_display, &output->base,
1043 wlsc_output_post_geometry);
1044}
1045
1046int
1047wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1048{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001049 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001050
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001051 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001052
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001053 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001054
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001055 ec->shell.interface = &wl_shell_interface;
1056 ec->shell.implementation = (void (**)(void)) &shell_interface;
1057 wl_display_add_object(display, &ec->shell);
1058 if (wl_display_add_global(display, &ec->shell, NULL))
1059 return -1;
1060
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001061 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001062
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001063 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001064 wl_list_init(&ec->input_device_list);
1065 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001066 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001067
1068 screenshooter_create(ec);
1069
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001070 glGenFramebuffers(1, &ec->fbo);
1071 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1072 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001073 if (init_shaders(ec) < 0)
1074 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001075
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001076 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001077 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001078 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001079
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001080 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001081}
1082
1083/* The plan here is to generate a random anonymous socket name and
1084 * advertise that through a service on the session dbus.
1085 */
1086static const char socket_name[] = "\0wayland";
1087
1088int main(int argc, char *argv[])
1089{
1090 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001091 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001092 GError *error = NULL;
1093 GOptionContext *context;
1094
1095 context = g_option_context_new(NULL);
1096 g_option_context_add_main_entries(context, option_entries, "Wayland");
1097 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1098 fprintf(stderr, "option parsing failed: %s\n", error->message);
1099 exit(EXIT_FAILURE);
1100 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001101
1102 display = wl_display_create();
1103
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001104 if (getenv("DISPLAY"))
1105 ec = x11_compositor_create(display);
1106 else
1107 ec = drm_compositor_create(display);
1108
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001109 if (ec == NULL) {
1110 fprintf(stderr, "failed to create compositor\n");
1111 exit(EXIT_FAILURE);
1112 }
1113
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001114 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001115 fprintf(stderr, "failed to add socket: %m\n");
1116 exit(EXIT_FAILURE);
1117 }
1118
1119 wl_display_run(display);
1120
1121 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001122}