blob: 0ecbb7d5474a56e250ab5200ef947008857eb1b4 [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
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400128static struct wlsc_surface *
129wlsc_surface_create(struct wlsc_compositor *compositor,
130 struct wl_visual *visual,
131 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500132{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400133 struct wlsc_surface *surface;
134
135 surface = malloc(sizeof *surface);
136 if (surface == NULL)
137 return NULL;
138
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500139 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400140 glBindTexture(GL_TEXTURE_2D, surface->texture);
141 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
142 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
143 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
144 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
145
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500146 surface->compositor = compositor;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500147 surface->visual = visual;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400148 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400149 wlsc_matrix_scale(&surface->matrix, width, height, 1);
150 wlsc_matrix_translate(&surface->matrix, x, y, 0);
151
152 wlsc_matrix_init(&surface->matrix_inv);
153 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
154 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500155
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400156 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500157}
158
159static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400160destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500161{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400162 struct wlsc_surface *surface =
163 container_of(resource, struct wlsc_surface, base.base);
164 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500165 struct wlsc_listener *l;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400166
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400167 wl_list_remove(&surface->link);
168 glDeleteTextures(1, &surface->texture);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400169
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500170 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400171 l->func(l, surface);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400172
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400173 free(surface);
Kristian Høgsberg117d5132010-08-11 08:56:47 -0400174
175 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500176}
177
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400178static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500179pointer_path(cairo_t *cr, int x, int y)
180{
181 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
182 const int width = 16, height = 16;
183
184 cairo_move_to(cr, x, y);
185 cairo_line_to(cr, x + tx, y + ty);
186 cairo_line_to(cr, x + dx, y + dy);
187 cairo_line_to(cr, x + width - end, y + height);
188 cairo_line_to(cr, x + width, y + height - end);
189 cairo_line_to(cr, x + dy, y + dx);
190 cairo_line_to(cr, x + ty, y + tx);
191 cairo_close_path(cr);
192}
193
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500194static struct wlsc_surface *
195pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500196{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500197 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500198 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500199 cairo_surface_t *surface;
200 cairo_t *cr;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400201 int stride;
202 void *data;
203
204 EGLint image_attribs[] = {
205 EGL_WIDTH, 0,
206 EGL_HEIGHT, 0,
207 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
208 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
209 EGL_NONE
210 };
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500211
212 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
213 width, height);
214
215 cr = cairo_create(surface);
216 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500217 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500218 cairo_set_source_rgb(cr, 0, 0, 0);
219 cairo_stroke_preserve(cr);
220 cairo_fill(cr);
221 blur_surface(surface, width);
222
223 pointer_path(cr, hotspot_x, hotspot_y);
224 cairo_stroke_preserve(cr);
225 cairo_set_source_rgb(cr, 1, 1, 1);
226 cairo_fill(cr);
227 cairo_destroy(cr);
228
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400229 es = wlsc_surface_create(ec, &ec->premultiplied_argb_visual,
230 x, y, width, height);
231
232 stride = cairo_image_surface_get_stride(surface);
233 data = cairo_image_surface_get_data(surface);
234
235 image_attribs[1] = width;
236 image_attribs[3] = height;
237 ec->default_pointer_image =
238 eglCreateDRMImageMESA(ec->display, image_attribs);
239 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, ec->default_pointer_image);
240 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
241 GL_RGBA, GL_UNSIGNED_BYTE, data);
242
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500243 cairo_surface_destroy(surface);
244
Kristian Høgsberg54879822008-11-23 17:07:32 -0500245 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500246}
247
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400248static void
249wlsc_input_device_set_default_pointer_image(struct wlsc_input_device *device)
250{
251 struct wlsc_compositor *ec = device->ec;
252
253 glBindTexture(GL_TEXTURE_2D, device->sprite->texture);
254 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, ec->default_pointer_image);
255 device->sprite->visual = &ec->premultiplied_argb_visual;
256 device->hotspot_x = 16;
257 device->hotspot_y = 16;
258}
259
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500260static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500261background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500262{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500263 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500264 GdkPixbuf *pixbuf;
265 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500266 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500267 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500268
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400269 background = wlsc_surface_create(output->compositor,
270 &output->compositor->rgb_visual,
271 output->x, output->y,
272 output->width, output->height);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500273 if (background == NULL)
274 return NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500275
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500276 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500277 output->width,
278 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500279 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500280 if (error != NULL) {
281 free(background);
282 return NULL;
283 }
284
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500285 data = gdk_pixbuf_get_pixels(pixbuf);
286
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400287 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500288 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500289 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500290 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500291
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500292 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
293 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500294 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500295
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500296 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500297}
298
299static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400300wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500301{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500302 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400303 struct wlsc_matrix tmp;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500304
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400305 tmp = es->matrix;
306 wlsc_matrix_multiply(&tmp, &output->matrix);
307 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
308 glUniform1i(ec->tex_uniform, 0);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500309
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500310 if (es->visual == &ec->argb_visual) {
311 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
312 glEnable(GL_BLEND);
313 } else if (es->visual == &ec->premultiplied_argb_visual) {
314 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
315 glEnable(GL_BLEND);
316 } else {
317 glDisable(GL_BLEND);
318 }
319
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500320 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500321 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400322 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
323 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
324 5 * sizeof(GLfloat), NULL);
325 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
326 5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
327 glEnableVertexAttribArray(0);
328 glEnableVertexAttribArray(1);
329 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500330}
331
332static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400333wlsc_surface_raise(struct wlsc_surface *surface)
334{
335 struct wlsc_compositor *compositor = surface->compositor;
336
337 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400338 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400339}
340
341static void
342wlsc_surface_lower(struct wlsc_surface *surface)
343{
344 struct wlsc_compositor *compositor = surface->compositor;
345
346 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400347 wl_list_insert(compositor->surface_list.prev, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400348}
349
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400350static void
351wlsc_surface_update_matrix(struct wlsc_surface *es)
352{
353 wlsc_matrix_init(&es->matrix);
354 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
355 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
356
357 wlsc_matrix_init(&es->matrix_inv);
358 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
359 wlsc_matrix_scale(&es->matrix_inv,
360 1.0 / es->width, 1.0 / es->height, 1);
361}
362
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400363void
364wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400365{
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400366 wl_display_post_frame(compositor->wl_display,
367 &compositor->base,
368 compositor->current_frame, msecs);
369
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400370 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400371 compositor->current_frame++;
372}
373
374static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400375wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400376{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500377 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500378 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500379 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500380
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500381 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500382
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500383 if (output->background)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400384 wlsc_surface_draw(output->background, output);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500385 else
386 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400387
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400388 wl_list_for_each_reverse(es, &ec->surface_list, link)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400389 wlsc_surface_draw(es, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400390
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400391 if (ec->focus)
392 wl_list_for_each(eid, &ec->input_device_list, link)
393 wlsc_surface_draw(eid->sprite, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500394}
395
396static void
397repaint(void *data)
398{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500399 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500400 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500401
402 if (!ec->repaint_needed) {
403 ec->repaint_on_timeout = 0;
404 return;
405 }
406
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500407 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400408 wlsc_output_repaint(output);
409
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400410 ec->present(ec);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500411
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500412 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400413}
414
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400415void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400416wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400417{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400418 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400419 if (compositor->repaint_on_timeout)
420 return;
421
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400422 wl_event_source_timer_update(compositor->timer_source, 1);
423 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400424}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500425
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500426static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500427surface_destroy(struct wl_client *client,
428 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400429{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400430 wl_resource_destroy(&surface->base, client);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400431}
432
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500433static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500434surface_attach(struct wl_client *client,
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400435 struct wl_surface *surface, struct wl_buffer *buffer_base)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400436{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500437 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400438 struct wlsc_buffer *buffer = (struct wlsc_buffer *) buffer_base;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400439
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500440 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400441 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, buffer->image);
442 es->visual = buffer->visual;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400443}
444
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500445static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500446surface_map(struct wl_client *client,
447 struct wl_surface *surface,
448 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400449{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500450 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400451
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400452 es->x = x;
453 es->y = y;
454 es->width = width;
455 es->height = height;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400456
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400457 wlsc_surface_update_matrix(es);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400458}
459
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500460static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500461surface_damage(struct wl_client *client,
462 struct wl_surface *surface,
463 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500464{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500465 /* FIXME: This need to take a damage region, of course. */
466}
467
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500468const static struct wl_surface_interface surface_interface = {
469 surface_destroy,
470 surface_attach,
471 surface_map,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500472 surface_damage
473};
474
475static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400476shell_move(struct wl_client *client, struct wl_shell *shell,
477 struct wl_surface *surface,
478 struct wl_input_device *device, uint32_t time)
479{
480 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
481
482 if (wd->grab == WLSC_DEVICE_GRAB_MOTION &&
483 wd->grab_time == time &&
484 &wd->pointer_focus->base == surface) {
485 wd->grab = WLSC_DEVICE_GRAB_MOVE;
486 wd->grab_dx = wd->pointer_focus->x - wd->grab_x;
487 wd->grab_dy = wd->pointer_focus->y - wd->grab_y;
488 }
489}
490
491static void
492shell_resize(struct wl_client *client, struct wl_shell *shell,
493 struct wl_surface *surface,
494 struct wl_input_device *device, uint32_t time, uint32_t edges)
495{
496 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
497
498 if (edges == 0 || edges > 15 ||
499 (edges & 3) == 3 || (edges & 12) == 12)
500 return;
501
502 if (wd->grab == WLSC_DEVICE_GRAB_MOTION &&
503 wd->grab_time == time &&
504 &wd->pointer_focus->base == surface) {
505 wd->grab = edges;
506 wd->grab_dx = wd->pointer_focus->x - wd->grab_x;
507 wd->grab_dy = wd->pointer_focus->y - wd->grab_y;
508 wd->grab_width = wd->pointer_focus->width;
509 wd->grab_height = wd->pointer_focus->height;
510 }
511}
512
513const static struct wl_shell_interface shell_interface = {
514 shell_move,
515 shell_resize
516};
517
518static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500519compositor_create_surface(struct wl_client *client,
520 struct wl_compositor *compositor, uint32_t id)
521{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500522 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400523 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500524
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400525 surface = wlsc_surface_create(ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400526 if (surface == NULL) {
527 wl_client_post_event(client,
528 (struct wl_object *) ec->wl_display,
529 WL_DISPLAY_NO_MEMORY, 0);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500530 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400531 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -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
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400612 if (!surface)
613 wlsc_input_device_set_default_pointer_image(device);
614
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500615 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øgsberg83fc0612010-08-04 22:44:55 -0400646 int32_t sx, sy, width, height;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500647
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500648 /* FIXME: We need some multi head love here. */
649 output = container_of(ec->output_list.next, struct wlsc_output, link);
650 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500651 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500652 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500653 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500654 if (x >= output->x + output->width)
655 x = output->x + output->width - 1;
656 if (y >= output->y + output->height)
657 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500658
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500659 device->x = x;
660 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500661
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400662 switch (device->grab) {
663 case WLSC_DEVICE_GRAB_NONE:
664 case WLSC_DEVICE_GRAB_MOTION:
665 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400666
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400667 wlsc_input_device_set_pointer_focus(device, es,
668 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400669
670 if (es)
671 wl_surface_post_event(&es->base, &device->base,
672 WL_INPUT_DEVICE_MOTION,
673 time, x, y, sx, sy);
674 break;
675
676 case WLSC_DEVICE_GRAB_MOVE:
677 es = device->pointer_focus;
678 es->x = x + device->grab_dx;
679 es->y = y + device->grab_dy;;
680 wl_surface_post_event(&es->base, &ec->shell,
681 WL_SHELL_CONFIGURE,
682 time, device->grab,
683 &es->base, es->x, es->y,
684 es->width, es->height);
685
686 wlsc_surface_update_matrix(es);
687
688 break;
689
690 case WLSC_DEVICE_GRAB_RESIZE_TOP:
691 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
692 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
693 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
694 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
695 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
696 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
697 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
698 case WLSC_DEVICE_GRAB_RESIZE_MASK:
699 es = device->pointer_focus;
700
701 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_LEFT) {
702 sx = x + device->grab_dx;
703 width = device->grab_x - x + device->grab_width;
704 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_RIGHT) {
705 sx = device->grab_x + device->grab_dx;
706 width = x - device->grab_x + device->grab_width;
707 } else {
708 sx = device->grab_x + device->grab_dx;
709 width = device->grab_width;
710 }
711
712 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_TOP) {
713 sy = y + device->grab_dy;
714 height = device->grab_y - y + device->grab_height;
715 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_BOTTOM) {
716 sy = device->grab_y + device->grab_dy;
717 height = y - device->grab_y + device->grab_height;
718 } else {
719 sy = device->grab_y + device->grab_dy;
720 height = device->grab_height;
721 }
722
723 wl_surface_post_event(&es->base, &ec->shell,
724 WL_SHELL_CONFIGURE, time, device->grab,
725 &es->base, sx, sy, width, height);
726 break;
727 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500728
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400729 wlsc_matrix_init(&device->sprite->matrix);
730 wlsc_matrix_scale(&device->sprite->matrix, 64, 64, 1);
731 wlsc_matrix_translate(&device->sprite->matrix,
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400732 x - device->hotspot_x, y - device->hotspot_y, 0);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500733
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400734 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500735}
736
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500737void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500738notify_button(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400739 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500740{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400741 struct wlsc_surface *surface;
742 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500743
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400744 surface = device->pointer_focus;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400745 if (surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400746 if (state && device->grab == WLSC_DEVICE_GRAB_NONE) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400747 wlsc_surface_raise(surface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400748 device->grab = WLSC_DEVICE_GRAB_MOTION;
749 device->grab_time = time;
750 device->grab_x = device->x;
751 device->grab_y = device->y;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400752 wlsc_input_device_set_keyboard_focus(device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400753 surface, time);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500754 } else {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400755 device->grab = WLSC_DEVICE_GRAB_NONE;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500756 }
757
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400758 if (state && button == BTN_LEFT &&
759 device->grab == WLSC_DEVICE_GRAB_MOTION &&
760 (device->modifier_state & MODIFIER_SUPER))
761 shell_move(NULL, &compositor->shell,
762 &surface->base, device, time);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400763 else if (state && button == BTN_MIDDLE &&
764 device->grab == WLSC_DEVICE_GRAB_MOTION &&
765 (device->modifier_state & MODIFIER_SUPER))
766 shell_resize(NULL, &compositor->shell,
767 &surface->base, device, time,
768 WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT);
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400769 else
770 wl_surface_post_event(&surface->base, &device->base,
771 WL_INPUT_DEVICE_BUTTON,
772 time, button, state);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500773
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400774 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500775 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500776}
777
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500778void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500779notify_key(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400780 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500781{
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500782 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400783 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500784
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400785 switch (key | device->modifier_state) {
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400786 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400787 kill(0, SIGTERM);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500788 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500789 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500790
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400791 switch (key) {
792 case KEY_LEFTCTRL:
793 case KEY_RIGHTCTRL:
794 modifier = MODIFIER_CTRL;
795 break;
796
797 case KEY_LEFTALT:
798 case KEY_RIGHTALT:
799 modifier = MODIFIER_ALT;
800 break;
801
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400802 case KEY_LEFTMETA:
803 case KEY_RIGHTMETA:
804 modifier = MODIFIER_SUPER;
805 break;
806
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400807 default:
808 modifier = 0;
809 break;
810 }
811
812 if (state)
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400813 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400814 else
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400815 device->modifier_state &= ~modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400816
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500817 end = device->keys.data + device->keys.size;
818 for (k = device->keys.data; k < end; k++) {
819 if (*k == key)
820 *k = *--end;
821 }
822 device->keys.size = (void *) end - device->keys.data;
823 if (state) {
824 k = wl_array_add(&device->keys, sizeof *k);
825 *k = key;
826 }
Ray Strodee96dcb82008-12-20 02:00:49 -0500827
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500828 if (device->keyboard_focus != NULL)
829 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400830 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400831 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400832}
833
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400834static void
835input_device_attach(struct wl_client *client,
836 struct wl_input_device *device_base,
837 struct wl_buffer *buffer_base, int32_t x, int32_t y)
838{
839 struct wlsc_input_device *device =
840 (struct wlsc_input_device *) device_base;
841 struct wlsc_buffer *buffer = (struct wlsc_buffer *) buffer_base;
842
843 if (device->pointer_focus == NULL ||
844 device->pointer_focus->base.client != client)
845 return;
846
847 glBindTexture(GL_TEXTURE_2D, device->sprite->texture);
848 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, buffer->image);
849 device->sprite->visual = buffer->visual;
850 device->hotspot_x = x;
851 device->hotspot_y = y;
852}
853
854const static struct wl_input_device_interface input_device_interface = {
855 input_device_attach,
856};
857
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400858static uint32_t
859get_time(void)
860{
861 struct timeval tv;
862
863 gettimeofday(&tv, NULL);
864
865 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500866}
867
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400868static void
869handle_surface_destroy(struct wlsc_listener *listener,
870 struct wlsc_surface *surface)
871{
872 struct wlsc_input_device *device =
873 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400874 struct wlsc_surface *focus;
875 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400876 uint32_t time = get_time();
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400877
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400878 if (device->keyboard_focus == surface)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400879 wlsc_input_device_set_keyboard_focus(device, NULL, time);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400880 if (device->pointer_focus == surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400881 device->grab = WLSC_DEVICE_GRAB_NONE;
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400882 focus = pick_surface(device, &sx, &sy);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400883 wlsc_input_device_set_pointer_focus(device, focus, time,
884 device->x, device->y,
885 sx, sy);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400886 fprintf(stderr, "lost pointer focus surface, reverting to %p\n", focus);
887 }
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400888}
889
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400890void
891wlsc_input_device_init(struct wlsc_input_device *device,
892 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500893{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500894 device->base.interface = &wl_input_device_interface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400895 device->base.implementation =
896 (void (**)(void)) &input_device_interface;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500897 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500898 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400899
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500900 device->x = 100;
901 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500902 device->ec = ec;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400903 device->sprite = pointer_create(ec, device->x, device->y, 64, 64);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400904 device->hotspot_x = 16;
905 device->hotspot_y = 16;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500906
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400907 device->listener.func = handle_surface_destroy;
908 wl_list_insert(ec->surface_destroy_listener_list.prev,
909 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500910 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500911}
912
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500913static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400914wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500915{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500916 struct wlsc_output *output =
917 container_of(global, struct wlsc_output, base);
918
919 wl_client_post_event(client, global,
920 WL_OUTPUT_GEOMETRY,
921 output->width, output->height);
922}
923
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400924static const char vertex_shader[] =
925 "uniform mat4 proj;\n"
926 "attribute vec4 position;\n"
927 "attribute vec2 texcoord;\n"
928 "varying vec2 v_texcoord;\n"
929 "void main()\n"
930 "{\n"
931 " gl_Position = proj * position;\n"
932 " v_texcoord = texcoord;\n"
933 "}\n";
934
935static const char fragment_shader[] =
936 /* "precision mediump float;\n" */
937 "varying vec2 v_texcoord;\n"
938 "uniform sampler2D tex;\n"
939 "void main()\n"
940 "{\n"
941 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
942 "}\n";
943
Kristian Høgsberga9468212010-06-14 21:03:11 -0400944static int
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400945init_shaders(struct wlsc_compositor *ec)
946{
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400947 GLuint v, f, program;
948 const char *p;
949 char msg[512];
950 GLfloat vertices[4 * 5];
951 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400952
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400953 p = vertex_shader;
954 v = glCreateShader(GL_VERTEX_SHADER);
955 glShaderSource(v, 1, &p, NULL);
956 glCompileShader(v);
957 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
958 if (!status) {
959 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
960 fprintf(stderr, "vertex shader info: %s\n", msg);
961 return -1;
962 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400963
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400964 p = fragment_shader;
965 f = glCreateShader(GL_FRAGMENT_SHADER);
966 glShaderSource(f, 1, &p, NULL);
967 glCompileShader(f);
968 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
969 if (!status) {
970 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
971 fprintf(stderr, "fragment shader info: %s\n", msg);
972 return -1;
973 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400974
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400975 program = glCreateProgram();
976 glAttachShader(program, v);
977 glAttachShader(program, f);
978 glBindAttribLocation(program, 0, "position");
979 glBindAttribLocation(program, 1, "texcoord");
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400980
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400981 glLinkProgram(program);
982 glGetProgramiv(program, GL_LINK_STATUS, &status);
983 if (!status) {
984 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
985 fprintf(stderr, "link info: %s\n", msg);
986 return -1;
987 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400988
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400989 glUseProgram(program);
990 ec->proj_uniform = glGetUniformLocation(program, "proj");
991 ec->tex_uniform = glGetUniformLocation(program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400992
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400993 vertices[ 0] = 0.0;
994 vertices[ 1] = 0.0;
995 vertices[ 2] = 0.0;
996 vertices[ 3] = 0.0;
997 vertices[ 4] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400998
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400999 vertices[ 5] = 0.0;
1000 vertices[ 6] = 1.0;
1001 vertices[ 7] = 0.0;
1002 vertices[ 8] = 0.0;
1003 vertices[ 9] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001004
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001005 vertices[10] = 1.0;
1006 vertices[11] = 0.0;
1007 vertices[12] = 0.0;
1008 vertices[13] = 1.0;
1009 vertices[14] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001010
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001011 vertices[15] = 1.0;
1012 vertices[16] = 1.0;
1013 vertices[17] = 0.0;
1014 vertices[18] = 1.0;
1015 vertices[19] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001016
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001017 glGenBuffers(1, &ec->vbo);
1018 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
1019 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001020
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001021 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001022}
1023
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001024static const struct wl_interface visual_interface = {
1025 "visual", 1,
1026};
1027
1028static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001029add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001030{
1031 ec->argb_visual.base.interface = &visual_interface;
1032 ec->argb_visual.base.implementation = NULL;
1033 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001034 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001035
1036 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1037 ec->premultiplied_argb_visual.base.implementation = NULL;
1038 wl_display_add_object(ec->wl_display,
1039 &ec->premultiplied_argb_visual.base);
1040 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001041 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001042
1043 ec->rgb_visual.base.interface = &visual_interface;
1044 ec->rgb_visual.base.implementation = NULL;
1045 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001046 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1047}
1048
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001049void
1050wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1051 int x, int y, int width, int height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001052{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001053 output->compositor = c;
1054 output->x = x;
1055 output->y = y;
1056 output->width = width;
1057 output->height = height;
1058
1059 output->background =
1060 background_create(output, option_background);
1061
1062 wlsc_matrix_init(&output->matrix);
1063 wlsc_matrix_translate(&output->matrix,
1064 -output->x - output->width / 2.0,
1065 -output->y - output->height / 2.0, 0);
1066 wlsc_matrix_scale(&output->matrix,
1067 2.0 / output->width, 2.0 / output->height, 1);
1068
1069 output->base.interface = &wl_output_interface;
1070 wl_display_add_object(c->wl_display, &output->base);
1071 wl_display_add_global(c->wl_display, &output->base,
1072 wlsc_output_post_geometry);
1073}
1074
1075int
1076wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1077{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001078 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001079
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001080 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001081
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001082 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001083
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001084 ec->shell.interface = &wl_shell_interface;
1085 ec->shell.implementation = (void (**)(void)) &shell_interface;
1086 wl_display_add_object(display, &ec->shell);
1087 if (wl_display_add_global(display, &ec->shell, NULL))
1088 return -1;
1089
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001090 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001091
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001092 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001093 wl_list_init(&ec->input_device_list);
1094 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001095 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001096
1097 screenshooter_create(ec);
1098
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001099 glGenFramebuffers(1, &ec->fbo);
1100 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1101 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001102 if (init_shaders(ec) < 0)
1103 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001104
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001105 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001106 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001107 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001108
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001109 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001110}
1111
1112/* The plan here is to generate a random anonymous socket name and
1113 * advertise that through a service on the session dbus.
1114 */
1115static const char socket_name[] = "\0wayland";
1116
1117int main(int argc, char *argv[])
1118{
1119 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001120 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001121 GError *error = NULL;
1122 GOptionContext *context;
1123
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001124 g_type_init(); /* GdkPixbuf needs this, it seems. */
1125
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001126 context = g_option_context_new(NULL);
1127 g_option_context_add_main_entries(context, option_entries, "Wayland");
1128 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1129 fprintf(stderr, "option parsing failed: %s\n", error->message);
1130 exit(EXIT_FAILURE);
1131 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001132
1133 display = wl_display_create();
1134
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001135 if (getenv("DISPLAY"))
1136 ec = x11_compositor_create(display);
1137 else
1138 ec = drm_compositor_create(display);
1139
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001140 if (ec == NULL) {
1141 fprintf(stderr, "failed to create compositor\n");
1142 exit(EXIT_FAILURE);
1143 }
1144
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001145 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001146 fprintf(stderr, "failed to add socket: %m\n");
1147 exit(EXIT_FAILURE);
1148 }
1149
1150 wl_display_run(display);
1151
1152 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001153}