blob: 1358c582592cfb3a8b07d71d9772f4894d7cacb8 [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>
Ray Strode966aa112008-12-19 14:28:02 -050024#include <termios.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040025#include <sys/ioctl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040026#include <fcntl.h>
27#include <unistd.h>
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050028#include <cairo.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050029#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050030#include <math.h>
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -050031#include <linux/input.h>
Ray Strode19ad6a92008-12-19 01:45:41 -050032#include <linux/vt.h>
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -050033#include <xf86drmMode.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050034#include <time.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040035
Kristian Høgsberg890bc052008-12-30 14:31:33 -050036#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
37#include <libudev.h>
38
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040039#include <GL/gl.h>
40#include <eagle.h>
41
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050042#include "wayland.h"
Kristian Høgsbergfe831a72008-12-21 21:50:23 -050043#include "wayland-protocol.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050044#include "cairo-util.h"
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050045#include "wayland-system-compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050046
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040047#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
48
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050049struct wlsc_matrix {
50 GLdouble d[16];
51};
52
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050053struct wl_visual {
54 struct wl_object base;
55};
56
Kristian Høgsberg4fa48732009-03-10 23:17:00 -040057struct wlsc_surface;
58
59struct wlsc_listener {
60 struct wl_list link;
61 void (*func)(struct wlsc_listener *listener,
62 struct wlsc_surface *surface);
63};
64
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050065struct wlsc_output {
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050066 struct wl_object base;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050067 struct wl_list link;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -050068 struct wlsc_compositor *compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050069 struct wlsc_surface *background;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050070 EGLSurface surface;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -040071 int32_t x, y, width, height;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050072
Kristian Høgsberg41a10682009-02-15 22:37:03 -050073 drmModeModeInfo *mode;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050074 uint32_t fb_id;
75 uint32_t crtc_id;
76 uint32_t connector_id;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050077};
78
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050079struct wlsc_input_device {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050080 struct wl_object base;
81 int32_t x, y;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050082 struct wlsc_compositor *ec;
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -050083 struct wlsc_surface *sprite;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050084 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050085
86 int grab;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050087 struct wlsc_surface *grab_surface;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -050088 struct wlsc_surface *pointer_focus;
89 struct wlsc_surface *keyboard_focus;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050090 struct wl_array keys;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -040091
92 struct wlsc_listener listener;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050093};
94
Kristian Høgsberg8e438622009-01-26 23:07:00 -050095struct wlsc_compositor {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040096 struct wl_compositor base;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050097 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
98
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040099 EGLDisplay display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400100 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500101 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400102 struct wl_display *wl_display;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500103
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500104 struct wl_list output_list;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500105 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500106 struct wl_list surface_list;
107
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400108 struct wl_list surface_destroy_listener_list;
109
Ray Strode966aa112008-12-19 14:28:02 -0500110 struct wl_event_source *term_signal_source;
111
112 /* tty handling state */
113 int tty_fd;
Ray Strodee96dcb82008-12-20 02:00:49 -0500114 uint32_t vt_active : 1;
Ray Strode966aa112008-12-19 14:28:02 -0500115
116 struct termios terminal_attributes;
117 struct wl_event_source *tty_input_source;
Ray Strode19ad6a92008-12-19 01:45:41 -0500118 struct wl_event_source *enter_vt_source;
119 struct wl_event_source *leave_vt_source;
120
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500121 struct udev *udev;
122
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500123 /* Repaint state. */
124 struct wl_event_source *timer_source;
125 int repaint_needed;
126 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500127 struct timespec previous_swap;
128 uint32_t current_frame;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500129
130 uint32_t meta_state;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500131 struct wl_list animate_list;
132 struct wlsc_surface *primary;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400133};
134
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500135#define META_DOWN 256
136
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500137struct wlsc_animate {
138 struct wl_list link;
139 void (*animate)(struct wlsc_animate *animate,
140 struct wlsc_compositor *compositor,
141 uint32_t frame, uint32_t msecs);
142};
143
144struct wlsc_vector {
145 GLdouble x, y, z;
146};
147
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500148struct wlsc_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500149 struct wl_surface base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500150 struct wlsc_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500151 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400152 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400153 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500154 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500155 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500156 struct wl_list link;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500157 struct wlsc_matrix matrix;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500158
159 struct wlsc_vector target, current, previous;
160 GLdouble target_angle, current_angle, previous_angle;
161 struct wlsc_animate animate;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400162};
163
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500164static const char *option_background = "background.jpg";
165
166static const GOptionEntry option_entries[] = {
167 { "background", 'b', 0, G_OPTION_ARG_STRING,
168 &option_background, "Background image" },
169 { NULL }
170};
171
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500172struct screenshooter {
173 struct wl_object base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500174 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500175};
176
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500177struct screenshooter_interface {
178 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
179};
180
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500181static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500182screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500183{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500184 struct wlsc_compositor *ec = shooter->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500185 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500186 char buffer[256];
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500187 GdkPixbuf *pixbuf, *normal;
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500188 GError *error = NULL;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500189 unsigned char *data;
190 int i, j;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500191
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500192 i = 0;
193 output = container_of(ec->output_list.next, struct wlsc_output, link);
194 while (&output->link != &ec->output_list) {
195 snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500196 data = malloc(output->width * output->height * 4);
197 if (data == NULL) {
198 fprintf(stderr, "couldn't allocate image buffer\n");
199 continue;
200 }
201
202 glReadBuffer(GL_FRONT);
203 glPixelStorei(GL_PACK_ALIGNMENT, 1);
204 glReadPixels(0, 0, output->width, output->height,
205 GL_RGBA, GL_UNSIGNED_BYTE, data);
206
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500207 /* FIXME: We should just use a RGB visual for the frontbuffer. */
208 for (j = 3; j < output->width * output->height * 4; j += 4)
209 data[j] = 0xff;
210
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500211 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500212 8, output->width, output->height, output->width * 4,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500213 NULL, NULL);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500214 normal = gdk_pixbuf_flip(pixbuf, FALSE);
215 gdk_pixbuf_save(normal, buffer, "png", &error, NULL);
216 gdk_pixbuf_unref(normal);
217 gdk_pixbuf_unref(pixbuf);
218 free(data);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500219
220 output = container_of(output->link.next,
221 struct wlsc_output, link);
222 }
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500223}
224
Kristian Høgsbergfb6d68d2008-12-21 21:54:51 -0500225static const struct wl_message screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500226 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500227};
228
229static const struct wl_interface screenshooter_interface = {
230 "screenshooter", 1,
231 ARRAY_LENGTH(screenshooter_methods),
232 screenshooter_methods,
233};
234
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500235struct screenshooter_interface screenshooter_implementation = {
236 screenshooter_shoot
237};
238
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500239static struct screenshooter *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500240screenshooter_create(struct wlsc_compositor *ec)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500241{
242 struct screenshooter *shooter;
243
244 shooter = malloc(sizeof *shooter);
245 if (shooter == NULL)
246 return NULL;
247
248 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500249 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500250 shooter->ec = ec;
251
252 return shooter;
253};
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500254
255static void
256wlsc_matrix_init(struct wlsc_matrix *matrix)
257{
258 static const struct wlsc_matrix identity = {
259 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
260 };
261
262 memcpy(matrix, &identity, sizeof identity);
263}
264
265static void
266wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
267{
268 struct wlsc_matrix tmp;
269 const GLdouble *row, *column;
270 div_t d;
271 int i, j;
272
273 for (i = 0; i < 16; i++) {
274 tmp.d[i] = 0;
275 d = div(i, 4);
276 row = m->d + d.quot * 4;
277 column = n->d + d.rem;
278 for (j = 0; j < 4; j++)
279 tmp.d[i] += row[j] * column[j * 4];
280 }
281 memcpy(m, &tmp, sizeof tmp);
282}
283
284static void
285wlsc_matrix_translate(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
286{
287 struct wlsc_matrix translate = {
288 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
289 };
290
291 wlsc_matrix_multiply(matrix, &translate);
292}
293
294static void
295wlsc_matrix_scale(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
296{
297 struct wlsc_matrix scale = {
298 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
299 };
300
301 wlsc_matrix_multiply(matrix, &scale);
302}
303
304static void
305wlsc_matrix_rotate(struct wlsc_matrix *matrix,
306 GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
307{
308 GLdouble c = cos(angle);
309 GLdouble s = sin(angle);
310 struct wlsc_matrix rotate = {
311 { x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
312 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) - x * s, 0,
313 x * z * (1 - c) + y * x, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
314 0, 0, 0, 1 }
315 };
316
317 wlsc_matrix_multiply(matrix, &rotate);
318}
319
320static void
321wlsc_surface_update_matrix(struct wlsc_surface *es)
322{
323 GLdouble tx, ty;
324
325 tx = es->map.x + es->map.width / 2;
326 ty = es->map.y + es->map.height / 2;
327
328 wlsc_matrix_init(&es->matrix);
329 wlsc_matrix_translate(&es->matrix, -tx, -ty, 0);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500330 wlsc_matrix_rotate(&es->matrix, es->current_angle, 0, 1, 0);
331 wlsc_matrix_translate(&es->matrix, tx + es->current.x,
332 ty + es->current.y, es->current.z);
333}
334
335static void
336wlsc_surface_init(struct wlsc_surface *surface,
337 struct wlsc_compositor *compositor, struct wl_visual *visual,
338 int32_t x, int32_t y, int32_t width, int32_t height)
339{
340 glGenTextures(1, &surface->texture);
341 surface->compositor = compositor;
342 surface->map.x = x;
343 surface->map.y = y;
344 surface->map.width = width;
345 surface->map.height = height;
346 surface->surface = EGL_NO_SURFACE;
347 surface->visual = visual;
348 surface->current.x = 0;
349 surface->current.y = 0;
350 surface->current.z = 0;
351 surface->current_angle = 0;
352 surface->target.x = 0;
353 surface->target.y = 0;
354 surface->target.z = 0;
355 surface->target_angle = 0;
356 surface->previous_angle = 0;
357
358 wlsc_surface_update_matrix(surface);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500359}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500360
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500361static struct wlsc_surface *
362wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500363 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500364 int x, int y, int width, int height)
365{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500366 struct wlsc_surface *es;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500367 int stride;
368 void *data;
369
370 stride = cairo_image_surface_get_stride(surface);
371 data = cairo_image_surface_get_data(surface);
372
373 es = malloc(sizeof *es);
374 if (es == NULL)
375 return NULL;
376
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500377 wlsc_surface_init(es, ec, &ec->premultiplied_argb_visual,
378 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500379 glBindTexture(GL_TEXTURE_2D, es->texture);
380 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500381 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
382 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
383 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500384 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
385 GL_BGRA, GL_UNSIGNED_BYTE, data);
386
Kristian Høgsberg54879822008-11-23 17:07:32 -0500387 return es;
388}
389
390static void
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400391wlsc_surface_destroy(struct wlsc_surface *surface,
392 struct wlsc_compositor *compositor)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500393{
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400394 struct wlsc_listener *l;
395
396 l = container_of(compositor->surface_destroy_listener_list.next,
397 struct wlsc_listener, link);
398 while (&l->link != &compositor->surface_destroy_listener_list) {
399 l->func(l, surface);
400 l = container_of(l->link.next, struct wlsc_listener, link);
401 }
402
403 wl_list_remove(&surface->link);
404 wl_list_remove(&surface->animate.link);
405
406 glDeleteTextures(1, &surface->texture);
407 if (surface->surface != EGL_NO_SURFACE)
408 eglDestroySurface(compositor->display, surface->surface);
409 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500410}
411
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400412static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500413pointer_path(cairo_t *cr, int x, int y)
414{
415 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
416 const int width = 16, height = 16;
417
418 cairo_move_to(cr, x, y);
419 cairo_line_to(cr, x + tx, y + ty);
420 cairo_line_to(cr, x + dx, y + dy);
421 cairo_line_to(cr, x + width - end, y + height);
422 cairo_line_to(cr, x + width, y + height - end);
423 cairo_line_to(cr, x + dy, y + dx);
424 cairo_line_to(cr, x + ty, y + tx);
425 cairo_close_path(cr);
426}
427
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500428static struct wlsc_surface *
429pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500430{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500431 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500432 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500433 cairo_surface_t *surface;
434 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500435
436 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
437 width, height);
438
439 cr = cairo_create(surface);
440 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500441 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500442 cairo_set_source_rgb(cr, 0, 0, 0);
443 cairo_stroke_preserve(cr);
444 cairo_fill(cr);
445 blur_surface(surface, width);
446
447 pointer_path(cr, hotspot_x, hotspot_y);
448 cairo_stroke_preserve(cr);
449 cairo_set_source_rgb(cr, 1, 1, 1);
450 cairo_fill(cr);
451 cairo_destroy(cr);
452
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500453 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500454 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500455 x - hotspot_x,
456 y - hotspot_y,
457 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500458
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500459 cairo_surface_destroy(surface);
460
Kristian Høgsberg54879822008-11-23 17:07:32 -0500461 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500462}
463
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500464static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500465background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500466{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500467 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500468 GdkPixbuf *pixbuf;
469 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500470 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500471 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500472
473 background = malloc(sizeof *background);
474 if (background == NULL)
475 return NULL;
476
477 g_type_init();
478
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500479 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500480 output->width,
481 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500482 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500483 if (error != NULL) {
484 free(background);
485 return NULL;
486 }
487
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500488 data = gdk_pixbuf_get_pixels(pixbuf);
489
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500490 wlsc_surface_init(background, output->compositor,
491 &output->compositor->rgb_visual,
492 output->x, output->y, output->width, output->height);
493
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500494 glBindTexture(GL_TEXTURE_2D, background->texture);
495 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500496 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500497 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
498 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500499
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500500 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500501 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500502 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500503 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500504
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500505 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
506 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500507 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500508
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500509 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500510}
511
512static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500513wlsc_surface_draw(struct wlsc_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500514{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500515 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500516 GLint vertices[12];
517 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
518 GLuint indices[4] = { 0, 1, 2, 3 };
519
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500520 vertices[0] = es->map.x;
521 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500522 vertices[2] = 0;
523
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500524 vertices[3] = es->map.x;
525 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500526 vertices[5] = 0;
527
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500528 vertices[6] = es->map.x + es->map.width;
529 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500530 vertices[8] = 0;
531
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500532 vertices[9] = es->map.x + es->map.width;
533 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500534 vertices[11] = 0;
535
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500536 if (es->visual == &ec->argb_visual) {
537 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
538 glEnable(GL_BLEND);
539 } else if (es->visual == &ec->premultiplied_argb_visual) {
540 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
541 glEnable(GL_BLEND);
542 } else {
543 glDisable(GL_BLEND);
544 }
545
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500546 glPushMatrix();
547 glMultMatrixd(es->matrix.d);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500548 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500549 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500550 glEnableClientState(GL_VERTEX_ARRAY);
551 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
552 glVertexPointer(3, GL_INT, 0, vertices);
553 glTexCoordPointer(2, GL_INT, 0, tex_coords);
554 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500555 glPopMatrix();
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500556}
557
558static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500559wlsc_vector_add(struct wlsc_vector *v1, struct wlsc_vector *v2)
560{
561 v1->x += v2->x;
562 v1->y += v2->y;
563 v1->z += v2->z;
564}
565
566static void
567wlsc_vector_subtract(struct wlsc_vector *v1, struct wlsc_vector *v2)
568{
569 v1->x -= v2->x;
570 v1->y -= v2->y;
571 v1->z -= v2->z;
572}
573
574static void
575wlsc_vector_scalar(struct wlsc_vector *v1, GLdouble s)
576{
577 v1->x *= s;
578 v1->y *= s;
579 v1->z *= s;
580}
581
582static void
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500583repaint_output(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400584{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500585 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500586 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500587 struct wlsc_input_device *eid;
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500588 double s = 3000;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500589
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500590 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
591 fprintf(stderr, "failed to make context current\n");
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500592 return;
593 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500594
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500595 glViewport(0, 0, output->width, output->height);
596 glMatrixMode(GL_PROJECTION);
597 glLoadIdentity();
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500598 glFrustum(-output->width / s, output->width / s,
599 output->height / s, -output->height / s, 1, 2 * s);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500600 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500601 glLoadIdentity();
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500602 glClearColor(0, 0, 0.2, 1);
603
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500604 glTranslatef(-output->width / 2, -output->height / 2, -s / 2);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500605
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500606 if (output->background)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500607 wlsc_surface_draw(output->background);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500608 else
609 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400610
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500611 es = container_of(ec->surface_list.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500612 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500613 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500614 wlsc_surface_draw(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500615 es = container_of(es->link.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500616 struct wlsc_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400617 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400618
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500619 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500620 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500621 while (&eid->link != &ec->input_device_list) {
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500622 wlsc_surface_draw(eid->sprite);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500623
624 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500625 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500626 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500627
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500628 eglSwapBuffers(ec->display, output->surface);
629}
630
631static void
632repaint(void *data)
633{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500634 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500635 struct wlsc_output *output;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500636 struct wlsc_animate *animate, *next;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500637 struct timespec ts;
638 uint32_t msecs;
639
640 if (!ec->repaint_needed) {
641 ec->repaint_on_timeout = 0;
642 return;
643 }
644
645 output = container_of(ec->output_list.next, struct wlsc_output, link);
646 while (&output->link != &ec->output_list) {
647 repaint_output(output);
648 output = container_of(output->link.next,
649 struct wlsc_output, link);
650 }
651
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500652 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500653
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500654 clock_gettime(CLOCK_MONOTONIC, &ts);
655 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500656 wl_display_post_frame(ec->wl_display, &ec->base,
657 ec->current_frame, msecs);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500658
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500659 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500660 ec->repaint_on_timeout = 1;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500661
662 animate = container_of(ec->animate_list.next, struct wlsc_animate, link);
663 while (&animate->link != &ec->animate_list) {
664 next = container_of(animate->link.next,
665 struct wlsc_animate, link);
666 animate->animate(animate, ec, ec->current_frame, msecs);
667 animate = next;
668 }
669
670 ec->current_frame++;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400671}
672
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500673static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500674schedule_repaint(struct wlsc_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400675{
676 struct wl_event_loop *loop;
677
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500678 ec->repaint_needed = 1;
679 if (!ec->repaint_on_timeout) {
680 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500681 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500682 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400683}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500684
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500685static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500686surface_destroy(struct wl_client *client,
687 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400688{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500689 struct wlsc_surface *es = (struct wlsc_surface *) surface;
690 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500691
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500692 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400693
694 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400695}
696
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500697static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500698surface_attach(struct wl_client *client,
699 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500700 uint32_t width, uint32_t height, uint32_t stride,
701 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400702{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500703 struct wlsc_surface *es = (struct wlsc_surface *) surface;
704 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400705
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500706 if (es->surface != EGL_NO_SURFACE)
707 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400708
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500709 es->width = width;
710 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500711 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500712 name, width, height, stride, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500713 if (visual == &ec->argb_visual.base)
714 es->visual = &ec->argb_visual;
715 else if (visual == &ec->premultiplied_argb_visual.base)
716 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500717 else if (visual == &ec->rgb_visual.base)
718 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500719 else
720 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400721
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500722 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400723 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500724 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
725 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
726 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500727 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400728}
729
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500730static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500731surface_map(struct wl_client *client,
732 struct wl_surface *surface,
733 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400734{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500735 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400736
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500737 es->map.x = x;
738 es->map.y = y;
739 es->map.width = width;
740 es->map.height = height;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500741 wlsc_surface_update_matrix(es);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400742}
743
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500744static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500745surface_copy(struct wl_client *client,
746 struct wl_surface *surface,
747 int32_t dst_x, int32_t dst_y,
748 uint32_t name, uint32_t stride,
749 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500750{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500751 struct wlsc_surface *es = (struct wlsc_surface *) surface;
752 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500753 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500754
755 /* FIXME: glCopyPixels should work, but then we'll have to
756 * call eglMakeCurrent to set up the src and dest surfaces
757 * first. This seems cheaper, but maybe there's a better way
758 * to accomplish this. */
759
760 src = eglCreateSurfaceForName(ec->display, ec->config,
761 name, x + width, y + height, stride, NULL);
762
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500763 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500764 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500765 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500766}
767
768static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500769surface_damage(struct wl_client *client,
770 struct wl_surface *surface,
771 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500772{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500773 /* FIXME: This need to take a damage region, of course. */
774}
775
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500776const static struct wl_surface_interface surface_interface = {
777 surface_destroy,
778 surface_attach,
779 surface_map,
780 surface_copy,
781 surface_damage
782};
783
784static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500785animate_surface(struct wlsc_animate *animate,
786 struct wlsc_compositor *compositor,
787 uint32_t frame, uint32_t msecs)
788{
789 struct wlsc_surface *s;
790 double angle_force, angle;
791 struct wlsc_vector force, tmp;
792 double step = 0.3;
793 double friction = 1;
794 double spring = 0.2;
795
796 s = container_of(animate, struct wlsc_surface, animate);
797
798 angle = s->current_angle;
799 angle_force = (s->target_angle - angle) * spring +
800 (s->previous_angle - angle) * friction;
801
802 s->current_angle = angle + (angle - s->previous_angle) + angle_force * step;
803 s->previous_angle = angle;
804
805 force = s->target;
806 wlsc_vector_subtract(&force, &s->current);
807 wlsc_vector_scalar(&force, spring);
808 tmp = s->previous;
809 wlsc_vector_subtract(&tmp, &s->current);
810 wlsc_vector_scalar(&tmp, friction);
811 wlsc_vector_add(&force, &tmp);
812
813 wlsc_vector_scalar(&force, step);
814 wlsc_vector_add(&force, &s->current);
815 wlsc_vector_subtract(&force, &s->previous);
816 s->previous = s->current;
817 wlsc_vector_add(&s->current, &force);
818
819 wlsc_surface_update_matrix(s);
820
821 tmp = s->current;
822 wlsc_vector_subtract(&tmp, &s->target);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500823 if (tmp.x * tmp.x + tmp.y * tmp.y + tmp.z * tmp.z > 0.001) {
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500824 schedule_repaint(compositor);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500825 } else {
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500826 wl_list_remove(&s->animate.link);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500827 wl_list_init(&s->animate.link);
828 }
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500829}
830
831static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500832compositor_create_surface(struct wl_client *client,
833 struct wl_compositor *compositor, uint32_t id)
834{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500835 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
836 struct wlsc_surface *es;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500837
838 es = malloc(sizeof *es);
839 if (es == NULL)
840 /* FIXME: Send OOM event. */
841 return;
842
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500843 wlsc_surface_init(es, ec, NULL, 0, 0, 0, 0);
844 es->animate.animate = animate_surface;
845 wl_list_init(&es->animate.link);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500846
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500847 wl_list_insert(ec->surface_list.prev, &es->link);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500848 wl_client_add_surface(client, &es->base,
849 &surface_interface, id);
850}
851
852static void
853compositor_commit(struct wl_client *client,
854 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500855{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500856 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500857
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500858 schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500859 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500860}
861
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500862const static struct wl_compositor_interface compositor_interface = {
863 compositor_create_surface,
864 compositor_commit
865};
866
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500867static void
868wlsc_surface_transform(struct wlsc_surface *surface,
869 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
870{
871 /* Transform to surface coordinates. */
872 *sx = (x - surface->map.x) * surface->width / surface->map.width;
873 *sy = (y - surface->map.y) * surface->height / surface->map.height;
874}
875
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500876static void
877wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
878 struct wlsc_surface *surface)
879{
880 if (device->keyboard_focus == surface)
881 return;
882
883 if (device->keyboard_focus &&
884 (!surface || device->keyboard_focus->base.client != surface->base.client))
885 wl_surface_post_event(&device->keyboard_focus->base,
886 &device->base,
Kristian Høgsberg786ca0d2009-03-06 21:25:21 -0500887 WL_INPUT_KEYBOARD_FOCUS, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500888
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500889 if (surface)
890 wl_surface_post_event(&surface->base,
891 &device->base,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500892 WL_INPUT_KEYBOARD_FOCUS,
893 &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500894
895 device->keyboard_focus = surface;
896}
897
898static void
899wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
900 struct wlsc_surface *surface)
901{
902 if (device->pointer_focus == surface)
903 return;
904
905 if (device->pointer_focus &&
906 (!surface || device->pointer_focus->base.client != surface->base.client))
907 wl_surface_post_event(&device->pointer_focus->base,
908 &device->base,
909 WL_INPUT_POINTER_FOCUS, NULL);
910 if (surface)
911 wl_surface_post_event(&surface->base,
912 &device->base,
913 WL_INPUT_POINTER_FOCUS, &surface->base);
914
915 device->pointer_focus = surface;
916}
917
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500918static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500919pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500920{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500921 struct wlsc_compositor *ec = device->ec;
922 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500923
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500924 if (device->grab > 0) {
925 wlsc_surface_transform(device->grab_surface,
926 device->x, device->y, sx, sy);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500927 return device->grab_surface;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500928 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500929
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500930 es = container_of(ec->surface_list.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500931 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500932 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500933 if (es->map.x <= device->x &&
934 device->x < es->map.x + es->map.width &&
935 es->map.y <= device->y &&
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500936 device->y < es->map.y + es->map.height) {
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500937 wlsc_surface_transform(es, device->x, device->y, sx, sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500938 return es;
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500939 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500940
941 es = container_of(es->link.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500942 struct wlsc_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500943
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500944 }
945
946 return NULL;
947}
948
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500949void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500950notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500951{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500952 struct wlsc_surface *es;
953 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500954 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500955 const int hotspot_x = 16, hotspot_y = 16;
956 int32_t sx, sy;
957
Ray Strodee96dcb82008-12-20 02:00:49 -0500958 if (!ec->vt_active)
959 return;
960
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500961 /* FIXME: We need some multi head love here. */
962 output = container_of(ec->output_list.next, struct wlsc_output, link);
963 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500964 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500965 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500966 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500967 if (x >= output->x + output->width)
968 x = output->x + output->width - 1;
969 if (y >= output->y + output->height)
970 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500971
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500972 device->x = x;
973 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500974 es = pick_surface(device, &sx, &sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500975
976 wlsc_input_device_set_pointer_focus(device, es);
977
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500978 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500979 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500980 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500981
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500982 device->sprite->map.x = x - hotspot_x;
983 device->sprite->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500984
985 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500986}
987
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500988void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500989notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500990 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500991{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500992 struct wlsc_surface *es;
993 struct wlsc_compositor *ec = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500994 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500995
Ray Strodee96dcb82008-12-20 02:00:49 -0500996 if (!ec->vt_active)
997 return;
998
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500999 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001000 if (es) {
1001 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001002 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -05001003
Kristian Høgsberg29573bc2008-12-11 23:27:27 -05001004 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -05001005 /* FIXME: We need callbacks when the surfaces
1006 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -05001007 device->grab++;
1008 device->grab_surface = es;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001009 wlsc_input_device_set_keyboard_focus(device, es);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -05001010 } else {
1011 device->grab--;
1012 }
1013
Kristian Høgsberg5a75c902008-12-10 13:16:50 -05001014 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001015 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001016 WL_INPUT_BUTTON, button, state,
1017 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001018
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001019 schedule_repaint(device->ec);
1020 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001021}
1022
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001023static void on_term_signal(int signal_number, void *data);
1024
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001025static void
1026update_surface_targets(struct wlsc_compositor *compositor, int primary)
1027{
1028 struct wlsc_surface *s;
1029 int i;
1030
1031 i = 0;
1032 s = container_of(compositor->surface_list.next,
1033 struct wlsc_surface, link);
1034 while (&s->link != &compositor->surface_list) {
1035 if (i < primary) {
1036 s->target.x = -400 + 500 * (i - primary);
1037 s->target.y = 0;
1038 s->target.z = -1500;
1039 s->target_angle = M_PI / 4;
1040 } else if (i == primary) {
1041 s->target.x = 0;
1042 s->target.y = 0;
1043 s->target.z = -1000;
1044 s->target_angle = 0;
1045 compositor->primary = s;
1046 } else {
1047 s->target.x = 400 + 500 * (i - primary);
1048 s->target.y = 0;
1049 s->target.z = -1500;
1050 s->target_angle = -M_PI / 4;
1051 }
1052 wl_list_remove(&s->animate.link);
1053 wl_list_insert(compositor->animate_list.prev, &s->animate.link);
1054 s = container_of(s->link.next,
1055 struct wlsc_surface, link);
1056 i++;
1057 }
1058
1059 schedule_repaint(compositor);
1060}
1061
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001062void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001063notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001064 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001065{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001066 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001067 uint32_t *k, *end;
1068
1069 if (!ec->vt_active)
1070 return;
Ray Strodee96dcb82008-12-20 02:00:49 -05001071
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001072 switch (key | ec->meta_state) {
1073 case KEY_EJECTCD | META_DOWN:
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001074 on_term_signal(SIGTERM, ec);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001075 return;
1076
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001077 case KEY_1 | META_DOWN:
1078 case KEY_2 | META_DOWN:
1079 case KEY_3 | META_DOWN:
1080 case KEY_4 | META_DOWN:
1081 case KEY_5 | META_DOWN:
1082 update_surface_targets(ec, key - KEY_1);
Kristian Høgsbergce5a9c12009-02-22 20:59:44 -05001083 if (device->grab == 0)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001084 wlsc_input_device_set_keyboard_focus(device, ec->primary);
Kristian Høgsbergce5a9c12009-02-22 20:59:44 -05001085 device->keyboard_focus = ec->primary;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001086 return;
1087
1088 case KEY_LEFTMETA:
1089 case KEY_RIGHTMETA:
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001090 case KEY_LEFTMETA | META_DOWN:
1091 case KEY_RIGHTMETA | META_DOWN:
1092 ec->meta_state = state ? META_DOWN : 0;
Kristian Høgsberg1febe012009-02-10 18:30:14 -05001093 if (state == 0 && ec->primary != NULL) {
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001094 ec->primary->target.z = 0;
1095 wl_list_remove(&ec->primary->animate.link);
1096 wl_list_insert(&ec->animate_list,
1097 &ec->primary->animate.link);
1098 schedule_repaint(ec);
1099 }
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001100 return;
1101 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001102
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001103 end = device->keys.data + device->keys.size;
1104 for (k = device->keys.data; k < end; k++) {
1105 if (*k == key)
1106 *k = *--end;
1107 }
1108 device->keys.size = (void *) end - device->keys.data;
1109 if (state) {
1110 k = wl_array_add(&device->keys, sizeof *k);
1111 *k = key;
1112 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001113
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001114 if (device->keyboard_focus != NULL)
1115 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -05001116 &device->base,
1117 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001118}
1119
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001120struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001121evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001122 struct wl_display *display, const char *path);
1123
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001124static void
1125handle_surface_destroy(struct wlsc_listener *listener,
1126 struct wlsc_surface *surface)
1127{
1128 struct wlsc_input_device *device =
1129 container_of(listener, struct wlsc_input_device, listener);
1130
1131 if (device->grab_surface == surface) {
1132 device->grab_surface = NULL;
1133 device->grab = 0;
1134 }
1135 if (device->keyboard_focus == surface)
1136 device->keyboard_focus = NULL;
1137 if (device->pointer_focus == surface)
1138 device->pointer_focus = NULL;
1139}
1140
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001141static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001142create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001143{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001144 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001145
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001146 device = malloc(sizeof *device);
1147 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001148 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001149
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001150 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001151 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001152 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001153 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001154 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001155 device->x = 100;
1156 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001157 device->ec = ec;
1158
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001159 device->listener.func = handle_surface_destroy;
1160 wl_list_insert(ec->surface_destroy_listener_list.prev,
1161 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001162 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001163
1164 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001165}
1166
1167void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001168wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001169{
1170 *x = device->x;
1171 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001172}
1173
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001174static void
1175post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001176{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001177 struct wlsc_output *output =
1178 container_of(global, struct wlsc_output, base);
1179
1180 wl_client_post_event(client, global,
1181 WL_OUTPUT_GEOMETRY,
1182 output->width, output->height);
1183}
1184
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001185static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001186init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001187{
1188 static const EGLint config_attribs[] = {
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001189 EGL_DEPTH_SIZE, 0,
1190 EGL_STENCIL_SIZE, 0,
1191 EGL_CONFIG_CAVEAT, EGL_NONE,
1192 EGL_RED_SIZE, 8,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001193 EGL_NONE
1194 };
1195
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001196 EGLint major, minor;
1197
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -05001198 ec->display = eglCreateDisplayNative(device);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001199 if (ec->display == NULL) {
1200 fprintf(stderr, "failed to create display\n");
1201 return -1;
1202 }
1203
1204 if (!eglInitialize(ec->display, &major, &minor)) {
1205 fprintf(stderr, "failed to initialize display\n");
1206 return -1;
1207 }
1208
1209 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
1210 return -1;
1211
1212 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
1213 if (ec->context == NULL) {
1214 fprintf(stderr, "failed to create context\n");
1215 return -1;
1216 }
1217 return 0;
1218}
1219
1220static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001221create_output(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001222{
1223 const static EGLint surface_attribs[] = {
1224 EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
1225 EGL_NONE
1226 };
1227
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001228 drmModeConnector *connector;
1229 drmModeRes *resources;
1230 drmModeEncoder *encoder;
Kristian Høgsberg41a10682009-02-15 22:37:03 -05001231 drmModeModeInfo *mode;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001232 struct wlsc_output *output;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001233 uint32_t name, handle, stride;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001234 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001235
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001236 if (ec->display == NULL && init_egl(ec, device) < 0) {
1237 fprintf(stderr, "failed to initialize egl\n");
1238 return -1;
1239 }
1240
1241 output = malloc(sizeof *output);
1242 if (output == NULL)
1243 return -1;
1244
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001245 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001246 resources = drmModeGetResources(fd);
1247 if (!resources) {
1248 fprintf(stderr, "drmModeGetResources failed\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001249 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001250 }
1251
1252 for (i = 0; i < resources->count_connectors; i++) {
1253 connector = drmModeGetConnector(fd, resources->connectors[i]);
1254 if (connector == NULL)
1255 continue;
1256
1257 if (connector->connection == DRM_MODE_CONNECTED &&
1258 connector->count_modes > 0)
1259 break;
1260
1261 drmModeFreeConnector(connector);
1262 }
1263
1264 if (i == resources->count_connectors) {
1265 fprintf(stderr, "No currently active connector found.\n");
1266 return -1;
1267 }
1268
1269 mode = &connector->modes[0];
1270
1271 for (i = 0; i < resources->count_encoders; i++) {
1272 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
1273
1274 if (encoder == NULL)
1275 continue;
1276
1277 if (encoder->encoder_id == connector->encoder_id)
1278 break;
1279
1280 drmModeFreeEncoder(encoder);
1281 }
1282
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001283 output->compositor = ec;
1284 output->crtc_id = encoder->crtc_id;
1285 output->connector_id = connector->connector_id;
1286 output->mode = mode;
1287 output->x = 0;
1288 output->y = 0;
1289 output->width = mode->hdisplay;
1290 output->height = mode->vdisplay;
1291
1292 output->surface = eglCreateSurfaceForName(ec->display,
1293 ec->config,
1294 0,
1295 output->width,
1296 output->height,
1297 0, surface_attribs);
1298 if (output->surface == NULL) {
1299 fprintf(stderr, "failed to create surface\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001300 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001301 }
1302
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001303 eglGetNativeBuffer(output->surface,
1304 GL_FRONT_LEFT, &name, &handle, &stride);
1305 ret = drmModeAddFB(fd, output->width, output->height,
1306 32, 32, stride, handle, &output->fb_id);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001307 if (ret) {
1308 fprintf(stderr, "failed to add fb: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001309 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001310 }
1311
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001312 ret = drmModeSetCrtc(fd, encoder->crtc_id, output->fb_id, 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001313 &connector->connector_id, 1, mode);
1314 if (ret) {
1315 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001316 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001317 }
1318
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001319 output->base.interface = &wl_output_interface;
1320 wl_display_add_object(ec->wl_display, &output->base);
1321 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
1322 wl_list_insert(ec->output_list.prev, &output->link);
1323
1324 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
1325 fprintf(stderr, "failed to make context current\n");
1326 return -1;
1327 }
1328
1329 output->background = background_create(output, option_background);
1330
1331 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001332}
1333
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001334static const struct wl_interface visual_interface = {
1335 "visual", 1,
1336};
1337
1338static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001339add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001340{
1341 ec->argb_visual.base.interface = &visual_interface;
1342 ec->argb_visual.base.implementation = NULL;
1343 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001344 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001345
1346 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1347 ec->premultiplied_argb_visual.base.implementation = NULL;
1348 wl_display_add_object(ec->wl_display,
1349 &ec->premultiplied_argb_visual.base);
1350 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001351 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001352
1353 ec->rgb_visual.base.interface = &visual_interface;
1354 ec->rgb_visual.base.implementation = NULL;
1355 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001356 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1357}
1358
Ray Strode19ad6a92008-12-19 01:45:41 -05001359static void on_enter_vt(int signal_number, void *data)
1360{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001361 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001362 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001363 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -05001364
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001365 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001366 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001367
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001368 fd = eglGetDisplayFD(ec->display);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001369 output = container_of(ec->output_list.next, struct wlsc_output, link);
1370 while (&output->link != &ec->output_list) {
1371 ret = drmModeSetCrtc(fd, output->crtc_id, output->fb_id, 0, 0,
1372 &output->connector_id, 1, output->mode);
1373 if (ret)
1374 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1375 output->connector_id);
1376
1377 output = container_of(output->link.next,
1378 struct wlsc_output, link);
Ray Strode19ad6a92008-12-19 01:45:41 -05001379 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001380}
1381
1382static void on_leave_vt(int signal_number, void *data)
1383{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001384 struct wlsc_compositor *ec = data;
Ray Strode19ad6a92008-12-19 01:45:41 -05001385
1386 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001387 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001388}
1389
Ray Strode966aa112008-12-19 14:28:02 -05001390static void
1391on_tty_input(int fd, uint32_t mask, void *data)
1392{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001393 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001394
1395 /* Ignore input to tty. We get keyboard events from evdev
1396 */
1397 tcflush(ec->tty_fd, TCIFLUSH);
1398}
1399
1400static void on_term_signal(int signal_number, void *data)
1401{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001402 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001403
1404 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1405 fprintf(stderr, "could not restore terminal to canonical mode\n");
1406
1407 exit(0);
1408}
1409
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001410static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001411{
1412 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001413 struct vt_mode mode = { 0 };
1414
1415 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1416 if (ec->tty_fd <= 0) {
1417 fprintf(stderr, "failed to open active tty: %m\n");
1418 return -1;
1419 }
Ray Strode966aa112008-12-19 14:28:02 -05001420
1421 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1422 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001423 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001424 }
1425
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001426 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001427 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001428 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001429
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001430 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001431 raw_attributes.c_oflag |= OPOST | OCRNL;
1432
1433 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1434 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1435
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001436 ec->term_signal_source =
1437 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001438
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001439 ec->tty_input_source =
1440 wl_event_loop_add_fd(loop, ec->tty_fd,
1441 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001442
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001443 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001444 mode.mode = VT_PROCESS;
1445 mode.relsig = SIGUSR1;
1446 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001447 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001448 fprintf(stderr, "failed to take control of vt handling\n");
1449 }
1450
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001451 ec->leave_vt_source =
1452 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1453 ec->enter_vt_source =
1454 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001455
1456 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001457}
1458
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001459static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001460init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001461{
1462 struct udev_enumerate *e;
1463 struct udev_list_entry *entry;
1464 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001465 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001466 struct wlsc_input_device *input_device;
1467
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001468 ec->udev = udev_new();
1469 if (ec->udev == NULL) {
1470 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001471 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001472 }
1473
1474 input_device = create_input_device(ec);
1475
1476 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001477 udev_enumerate_add_match_subsystem(e, "input");
1478 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001479 udev_enumerate_scan_devices(e);
1480 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1481 path = udev_list_entry_get_name(entry);
1482 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001483 evdev_input_device_create(input_device, ec->wl_display,
1484 udev_device_get_devnode(device));
1485 }
1486 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001487
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001488 e = udev_enumerate_new(ec->udev);
1489 udev_enumerate_add_match_subsystem(e, "drm");
1490 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1491 udev_enumerate_scan_devices(e);
1492 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1493 path = udev_list_entry_get_name(entry);
1494 device = udev_device_new_from_syspath(ec->udev, path);
1495 if (create_output(ec, device) < 0) {
1496 fprintf(stderr, "failed to create output for %s\n", path);
1497 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001498 }
1499 }
1500 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001501
1502 /* Create the pointer surface now that we have a current EGL context. */
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -05001503 input_device->sprite =
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001504 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1505
1506 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001507}
1508
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001509static struct wlsc_compositor *
1510wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001511{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001512 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001513 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001514 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001515
1516 ec = malloc(sizeof *ec);
1517 if (ec == NULL)
1518 return NULL;
1519
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001520 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001521 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001522
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001523 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001524
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001525 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001526
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001527 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001528 wl_list_init(&ec->input_device_list);
1529 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001530 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001531 if (init_libudev(ec) < 0) {
1532 fprintf(stderr, "failed to initialize devices\n");
1533 return NULL;
1534 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001535
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001536 shooter = screenshooter_create(ec);
1537 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001538 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001539
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001540 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001541
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001542 setup_tty(ec, loop);
1543
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001544 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001545 schedule_repaint(ec);
1546
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001547 wl_list_init(&ec->animate_list);
1548
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001549 return ec;
1550}
1551
1552/* The plan here is to generate a random anonymous socket name and
1553 * advertise that through a service on the session dbus.
1554 */
1555static const char socket_name[] = "\0wayland";
1556
1557int main(int argc, char *argv[])
1558{
1559 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001560 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001561 GError *error = NULL;
1562 GOptionContext *context;
1563
1564 context = g_option_context_new(NULL);
1565 g_option_context_add_main_entries(context, option_entries, "Wayland");
1566 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1567 fprintf(stderr, "option parsing failed: %s\n", error->message);
1568 exit(EXIT_FAILURE);
1569 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001570
1571 display = wl_display_create();
1572
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001573 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001574 if (ec == NULL) {
1575 fprintf(stderr, "failed to create compositor\n");
1576 exit(EXIT_FAILURE);
1577 }
1578
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001579 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001580 fprintf(stderr, "failed to add socket: %m\n");
1581 exit(EXIT_FAILURE);
1582 }
1583
1584 wl_display_run(display);
1585
1586 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001587}