blob: 5524dbb075cf2ebc4d7b76eb1aa1631cbd8021c2 [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>
Ray Strode966aa112008-12-19 14:28:02 -050021#include <stdbool.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040022#include <stdlib.h>
23#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050024#include <stdarg.h>
Ray Strode966aa112008-12-19 14:28:02 -050025#include <termios.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040026#include <i915_drm.h>
27#include <sys/ioctl.h>
28#include <sys/mman.h>
29#include <fcntl.h>
30#include <unistd.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050031#include <signal.h>
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050032#include <cairo.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050033#include <gdk-pixbuf/gdk-pixbuf.h>
34#include <glib.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050035#include <sys/poll.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050036#include <png.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050037#include <math.h>
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -050038#include <linux/input.h>
Ray Strode19ad6a92008-12-19 01:45:41 -050039#include <linux/vt.h>
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -050040#include <xf86drmMode.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050041#include <time.h>
Kristian Høgsbergc492b482008-12-12 12:00:02 -050042#include <fnmatch.h>
43#include <dirent.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040044
Kristian Høgsberg890bc052008-12-30 14:31:33 -050045#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
46#include <libudev.h>
47
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040048#include <GL/gl.h>
49#include <eagle.h>
50
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050051#include "wayland.h"
Kristian Høgsbergfe831a72008-12-21 21:50:23 -050052#include "wayland-protocol.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050053#include "cairo-util.h"
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050054#include "wayland-system-compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050055
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040056#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
57
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050058struct wlsc_matrix {
59 GLdouble d[16];
60};
61
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050062struct wl_visual {
63 struct wl_object base;
64};
65
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050066struct wlsc_output {
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050067 struct wl_object base;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050068 struct wl_list link;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -050069 struct wlsc_compositor *compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050070 struct wlsc_surface *background;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050071 EGLSurface surface;
72 int32_t x, y, width, height, stride;
73
Kristian Høgsberg41a10682009-02-15 22:37:03 -050074 drmModeModeInfo *mode;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050075 uint32_t fb_id;
76 uint32_t crtc_id;
77 uint32_t connector_id;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050078};
79
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050080struct wlsc_input_device {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050081 struct wl_object base;
82 int32_t x, y;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050083 struct wlsc_compositor *ec;
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -050084 struct wlsc_surface *sprite;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050085 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050086
87 int grab;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050088 struct wlsc_surface *grab_surface;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -050089 struct wlsc_surface *pointer_focus;
90 struct wlsc_surface *keyboard_focus;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050091 struct wl_array keys;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050092};
93
Kristian Høgsberg8e438622009-01-26 23:07:00 -050094struct wlsc_compositor {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040095 struct wl_compositor base;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050096 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
97
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040098 EGLDisplay display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040099 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500100 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400101 struct wl_display *wl_display;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500102
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500103 struct wl_list output_list;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500104 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500105 struct wl_list surface_list;
106
Ray Strode966aa112008-12-19 14:28:02 -0500107 struct wl_event_source *term_signal_source;
108
109 /* tty handling state */
110 int tty_fd;
Ray Strodee96dcb82008-12-20 02:00:49 -0500111 uint32_t vt_active : 1;
Ray Strode966aa112008-12-19 14:28:02 -0500112
113 struct termios terminal_attributes;
114 struct wl_event_source *tty_input_source;
Ray Strode19ad6a92008-12-19 01:45:41 -0500115 struct wl_event_source *enter_vt_source;
116 struct wl_event_source *leave_vt_source;
117
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500118 struct udev *udev;
119
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500120 /* Repaint state. */
121 struct wl_event_source *timer_source;
122 int repaint_needed;
123 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500124 struct timespec previous_swap;
125 uint32_t current_frame;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500126
127 uint32_t meta_state;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500128 struct wl_list animate_list;
129 struct wlsc_surface *primary;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400130};
131
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500132#define META_DOWN 256
133
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500134struct wlsc_animate {
135 struct wl_list link;
136 void (*animate)(struct wlsc_animate *animate,
137 struct wlsc_compositor *compositor,
138 uint32_t frame, uint32_t msecs);
139};
140
141struct wlsc_vector {
142 GLdouble x, y, z;
143};
144
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500145struct wlsc_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500146 struct wl_surface base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500147 struct wlsc_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500148 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400149 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400150 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500151 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500152 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500153 struct wl_list link;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500154 struct wlsc_matrix matrix;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500155
156 struct wlsc_vector target, current, previous;
157 GLdouble target_angle, current_angle, previous_angle;
158 struct wlsc_animate animate;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400159};
160
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500161static const char *option_background = "background.jpg";
162
163static const GOptionEntry option_entries[] = {
164 { "background", 'b', 0, G_OPTION_ARG_STRING,
165 &option_background, "Background image" },
166 { NULL }
167};
168
169
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500170struct screenshooter {
171 struct wl_object base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500172 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500173};
174
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500175struct screenshooter_interface {
176 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
177};
178
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500179static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500180screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500181{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500182 struct wlsc_compositor *ec = shooter->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500183 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500184 char buffer[256];
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500185 GdkPixbuf *pixbuf, *normal;
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500186 GError *error = NULL;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500187 unsigned char *data;
188 int i, j;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500189
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500190 i = 0;
191 output = container_of(ec->output_list.next, struct wlsc_output, link);
192 while (&output->link != &ec->output_list) {
193 snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500194 data = malloc(output->width * output->height * 4);
195 if (data == NULL) {
196 fprintf(stderr, "couldn't allocate image buffer\n");
197 continue;
198 }
199
200 glReadBuffer(GL_FRONT);
201 glPixelStorei(GL_PACK_ALIGNMENT, 1);
202 glReadPixels(0, 0, output->width, output->height,
203 GL_RGBA, GL_UNSIGNED_BYTE, data);
204
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500205 /* FIXME: We should just use a RGB visual for the frontbuffer. */
206 for (j = 3; j < output->width * output->height * 4; j += 4)
207 data[j] = 0xff;
208
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500209 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500210 8, output->width, output->height, output->width * 4,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500211 NULL, NULL);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500212 normal = gdk_pixbuf_flip(pixbuf, FALSE);
213 gdk_pixbuf_save(normal, buffer, "png", &error, NULL);
214 gdk_pixbuf_unref(normal);
215 gdk_pixbuf_unref(pixbuf);
216 free(data);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500217
218 output = container_of(output->link.next,
219 struct wlsc_output, link);
220 }
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500221}
222
Kristian Høgsbergfb6d68d2008-12-21 21:54:51 -0500223static const struct wl_message screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500224 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500225};
226
227static const struct wl_interface screenshooter_interface = {
228 "screenshooter", 1,
229 ARRAY_LENGTH(screenshooter_methods),
230 screenshooter_methods,
231};
232
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500233struct screenshooter_interface screenshooter_implementation = {
234 screenshooter_shoot
235};
236
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500237static struct screenshooter *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500238screenshooter_create(struct wlsc_compositor *ec)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500239{
240 struct screenshooter *shooter;
241
242 shooter = malloc(sizeof *shooter);
243 if (shooter == NULL)
244 return NULL;
245
246 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500247 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500248 shooter->ec = ec;
249
250 return shooter;
251};
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500252
253static void
254wlsc_matrix_init(struct wlsc_matrix *matrix)
255{
256 static const struct wlsc_matrix identity = {
257 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
258 };
259
260 memcpy(matrix, &identity, sizeof identity);
261}
262
263static void
264wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
265{
266 struct wlsc_matrix tmp;
267 const GLdouble *row, *column;
268 div_t d;
269 int i, j;
270
271 for (i = 0; i < 16; i++) {
272 tmp.d[i] = 0;
273 d = div(i, 4);
274 row = m->d + d.quot * 4;
275 column = n->d + d.rem;
276 for (j = 0; j < 4; j++)
277 tmp.d[i] += row[j] * column[j * 4];
278 }
279 memcpy(m, &tmp, sizeof tmp);
280}
281
282static void
283wlsc_matrix_translate(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
284{
285 struct wlsc_matrix translate = {
286 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
287 };
288
289 wlsc_matrix_multiply(matrix, &translate);
290}
291
292static void
293wlsc_matrix_scale(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
294{
295 struct wlsc_matrix scale = {
296 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
297 };
298
299 wlsc_matrix_multiply(matrix, &scale);
300}
301
302static void
303wlsc_matrix_rotate(struct wlsc_matrix *matrix,
304 GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
305{
306 GLdouble c = cos(angle);
307 GLdouble s = sin(angle);
308 struct wlsc_matrix rotate = {
309 { x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
310 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) - x * s, 0,
311 x * z * (1 - c) + y * x, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
312 0, 0, 0, 1 }
313 };
314
315 wlsc_matrix_multiply(matrix, &rotate);
316}
317
318static void
319wlsc_surface_update_matrix(struct wlsc_surface *es)
320{
321 GLdouble tx, ty;
322
323 tx = es->map.x + es->map.width / 2;
324 ty = es->map.y + es->map.height / 2;
325
326 wlsc_matrix_init(&es->matrix);
327 wlsc_matrix_translate(&es->matrix, -tx, -ty, 0);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500328 wlsc_matrix_rotate(&es->matrix, es->current_angle, 0, 1, 0);
329 wlsc_matrix_translate(&es->matrix, tx + es->current.x,
330 ty + es->current.y, es->current.z);
331}
332
333static void
334wlsc_surface_init(struct wlsc_surface *surface,
335 struct wlsc_compositor *compositor, struct wl_visual *visual,
336 int32_t x, int32_t y, int32_t width, int32_t height)
337{
338 glGenTextures(1, &surface->texture);
339 surface->compositor = compositor;
340 surface->map.x = x;
341 surface->map.y = y;
342 surface->map.width = width;
343 surface->map.height = height;
344 surface->surface = EGL_NO_SURFACE;
345 surface->visual = visual;
346 surface->current.x = 0;
347 surface->current.y = 0;
348 surface->current.z = 0;
349 surface->current_angle = 0;
350 surface->target.x = 0;
351 surface->target.y = 0;
352 surface->target.z = 0;
353 surface->target_angle = 0;
354 surface->previous_angle = 0;
355
356 wlsc_surface_update_matrix(surface);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500357}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500358
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500359static struct wlsc_surface *
360wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500361 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500362 int x, int y, int width, int height)
363{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500364 struct wlsc_surface *es;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500365 int stride;
366 void *data;
367
368 stride = cairo_image_surface_get_stride(surface);
369 data = cairo_image_surface_get_data(surface);
370
371 es = malloc(sizeof *es);
372 if (es == NULL)
373 return NULL;
374
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500375 wlsc_surface_init(es, ec, &ec->premultiplied_argb_visual,
376 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500377 glBindTexture(GL_TEXTURE_2D, es->texture);
378 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500379 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
380 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
381 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500382 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
383 GL_BGRA, GL_UNSIGNED_BYTE, data);
384
Kristian Høgsberg54879822008-11-23 17:07:32 -0500385 return es;
386}
387
388static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500389wlsc_surface_destroy(struct wlsc_surface *es, struct wlsc_compositor *ec)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500390{
391 glDeleteTextures(1, &es->texture);
392 if (es->surface != EGL_NO_SURFACE)
393 eglDestroySurface(ec->display, es->surface);
394 free(es);
395}
396
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400397static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500398pointer_path(cairo_t *cr, int x, int y)
399{
400 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
401 const int width = 16, height = 16;
402
403 cairo_move_to(cr, x, y);
404 cairo_line_to(cr, x + tx, y + ty);
405 cairo_line_to(cr, x + dx, y + dy);
406 cairo_line_to(cr, x + width - end, y + height);
407 cairo_line_to(cr, x + width, y + height - end);
408 cairo_line_to(cr, x + dy, y + dx);
409 cairo_line_to(cr, x + ty, y + tx);
410 cairo_close_path(cr);
411}
412
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500413static struct wlsc_surface *
414pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500415{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500416 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500417 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500418 cairo_surface_t *surface;
419 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500420
421 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
422 width, height);
423
424 cr = cairo_create(surface);
425 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500426 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500427 cairo_set_source_rgb(cr, 0, 0, 0);
428 cairo_stroke_preserve(cr);
429 cairo_fill(cr);
430 blur_surface(surface, width);
431
432 pointer_path(cr, hotspot_x, hotspot_y);
433 cairo_stroke_preserve(cr);
434 cairo_set_source_rgb(cr, 1, 1, 1);
435 cairo_fill(cr);
436 cairo_destroy(cr);
437
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500438 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500439 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500440 x - hotspot_x,
441 y - hotspot_y,
442 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500443
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500444 cairo_surface_destroy(surface);
445
Kristian Høgsberg54879822008-11-23 17:07:32 -0500446 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500447}
448
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500449static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500450background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500451{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500452 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500453 GdkPixbuf *pixbuf;
454 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500455 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500456 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500457
458 background = malloc(sizeof *background);
459 if (background == NULL)
460 return NULL;
461
462 g_type_init();
463
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500464 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500465 output->width,
466 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500467 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500468 if (error != NULL) {
469 free(background);
470 return NULL;
471 }
472
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500473 data = gdk_pixbuf_get_pixels(pixbuf);
474
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500475 wlsc_surface_init(background, output->compositor,
476 &output->compositor->rgb_visual,
477 output->x, output->y, output->width, output->height);
478
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500479 glBindTexture(GL_TEXTURE_2D, background->texture);
480 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500481 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500482 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
483 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500484
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500485 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500486 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500487 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500488 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500489
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500490 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
491 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500492 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500493
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500494 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500495}
496
497static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500498wlsc_surface_draw(struct wlsc_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500499{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500500 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500501 GLint vertices[12];
502 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
503 GLuint indices[4] = { 0, 1, 2, 3 };
504
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500505 vertices[0] = es->map.x;
506 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500507 vertices[2] = 0;
508
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500509 vertices[3] = es->map.x;
510 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500511 vertices[5] = 0;
512
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500513 vertices[6] = es->map.x + es->map.width;
514 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500515 vertices[8] = 0;
516
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500517 vertices[9] = es->map.x + es->map.width;
518 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500519 vertices[11] = 0;
520
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500521 if (es->visual == &ec->argb_visual) {
522 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
523 glEnable(GL_BLEND);
524 } else if (es->visual == &ec->premultiplied_argb_visual) {
525 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
526 glEnable(GL_BLEND);
527 } else {
528 glDisable(GL_BLEND);
529 }
530
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500531 glPushMatrix();
532 glMultMatrixd(es->matrix.d);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500533 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500534 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500535 glEnableClientState(GL_VERTEX_ARRAY);
536 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
537 glVertexPointer(3, GL_INT, 0, vertices);
538 glTexCoordPointer(2, GL_INT, 0, tex_coords);
539 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500540 glPopMatrix();
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500541}
542
543static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500544wlsc_vector_add(struct wlsc_vector *v1, struct wlsc_vector *v2)
545{
546 v1->x += v2->x;
547 v1->y += v2->y;
548 v1->z += v2->z;
549}
550
551static void
552wlsc_vector_subtract(struct wlsc_vector *v1, struct wlsc_vector *v2)
553{
554 v1->x -= v2->x;
555 v1->y -= v2->y;
556 v1->z -= v2->z;
557}
558
559static void
560wlsc_vector_scalar(struct wlsc_vector *v1, GLdouble s)
561{
562 v1->x *= s;
563 v1->y *= s;
564 v1->z *= s;
565}
566
567static void
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500568repaint_output(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400569{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500570 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500571 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500572 struct wlsc_input_device *eid;
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500573 double s = 3000;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500574
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500575 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
576 fprintf(stderr, "failed to make context current\n");
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500577 return;
578 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500579
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500580 glViewport(0, 0, output->width, output->height);
581 glMatrixMode(GL_PROJECTION);
582 glLoadIdentity();
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500583 glFrustum(-output->width / s, output->width / s,
584 output->height / s, -output->height / s, 1, 2 * s);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500585 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500586 glLoadIdentity();
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500587 glClearColor(0, 0, 0.2, 1);
588
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500589 glTranslatef(-output->width / 2, -output->height / 2, -s / 2);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500590
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500591 if (output->background)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500592 wlsc_surface_draw(output->background);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500593 else
594 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400595
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500596 es = container_of(ec->surface_list.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500597 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500598 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500599 wlsc_surface_draw(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500600 es = container_of(es->link.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500601 struct wlsc_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400602 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400603
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500604 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500605 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500606 while (&eid->link != &ec->input_device_list) {
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500607 wlsc_surface_draw(eid->sprite);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500608
609 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500610 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500611 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500612
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500613 eglSwapBuffers(ec->display, output->surface);
614}
615
616static void
617repaint(void *data)
618{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500619 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500620 struct wlsc_output *output;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500621 struct wlsc_animate *animate, *next;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500622 struct timespec ts;
623 uint32_t msecs;
624
625 if (!ec->repaint_needed) {
626 ec->repaint_on_timeout = 0;
627 return;
628 }
629
630 output = container_of(ec->output_list.next, struct wlsc_output, link);
631 while (&output->link != &ec->output_list) {
632 repaint_output(output);
633 output = container_of(output->link.next,
634 struct wlsc_output, link);
635 }
636
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500637 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500638
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500639 clock_gettime(CLOCK_MONOTONIC, &ts);
640 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500641 wl_display_post_frame(ec->wl_display, &ec->base,
642 ec->current_frame, msecs);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500643
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500644 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500645 ec->repaint_on_timeout = 1;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500646
647 animate = container_of(ec->animate_list.next, struct wlsc_animate, link);
648 while (&animate->link != &ec->animate_list) {
649 next = container_of(animate->link.next,
650 struct wlsc_animate, link);
651 animate->animate(animate, ec, ec->current_frame, msecs);
652 animate = next;
653 }
654
655 ec->current_frame++;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400656}
657
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500658static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500659schedule_repaint(struct wlsc_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400660{
661 struct wl_event_loop *loop;
662
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500663 ec->repaint_needed = 1;
664 if (!ec->repaint_on_timeout) {
665 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500666 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500667 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400668}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500669
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500670static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500671surface_destroy(struct wl_client *client,
672 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400673{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500674 struct wlsc_surface *es = (struct wlsc_surface *) surface;
675 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500676
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500677 wl_list_remove(&es->link);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500678 wl_list_remove(&es->animate.link);
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500679 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400680
681 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400682}
683
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500684static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500685surface_attach(struct wl_client *client,
686 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500687 uint32_t width, uint32_t height, uint32_t stride,
688 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400689{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500690 struct wlsc_surface *es = (struct wlsc_surface *) surface;
691 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400692
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500693 if (es->surface != EGL_NO_SURFACE)
694 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400695
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500696 es->width = width;
697 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500698 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500699 name, width, height, stride, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500700 if (visual == &ec->argb_visual.base)
701 es->visual = &ec->argb_visual;
702 else if (visual == &ec->premultiplied_argb_visual.base)
703 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500704 else if (visual == &ec->rgb_visual.base)
705 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500706 else
707 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400708
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500709 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400710 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500711 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
712 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
713 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500714 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400715}
716
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500717static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500718surface_map(struct wl_client *client,
719 struct wl_surface *surface,
720 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400721{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500722 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400723
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500724 es->map.x = x;
725 es->map.y = y;
726 es->map.width = width;
727 es->map.height = height;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500728 wlsc_surface_update_matrix(es);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400729}
730
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500731static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500732surface_copy(struct wl_client *client,
733 struct wl_surface *surface,
734 int32_t dst_x, int32_t dst_y,
735 uint32_t name, uint32_t stride,
736 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500737{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500738 struct wlsc_surface *es = (struct wlsc_surface *) surface;
739 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500740 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500741
742 /* FIXME: glCopyPixels should work, but then we'll have to
743 * call eglMakeCurrent to set up the src and dest surfaces
744 * first. This seems cheaper, but maybe there's a better way
745 * to accomplish this. */
746
747 src = eglCreateSurfaceForName(ec->display, ec->config,
748 name, x + width, y + height, stride, NULL);
749
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500750 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500751 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500752 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500753}
754
755static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500756surface_damage(struct wl_client *client,
757 struct wl_surface *surface,
758 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500759{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500760 /* FIXME: This need to take a damage region, of course. */
761}
762
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500763const static struct wl_surface_interface surface_interface = {
764 surface_destroy,
765 surface_attach,
766 surface_map,
767 surface_copy,
768 surface_damage
769};
770
771static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500772animate_surface(struct wlsc_animate *animate,
773 struct wlsc_compositor *compositor,
774 uint32_t frame, uint32_t msecs)
775{
776 struct wlsc_surface *s;
777 double angle_force, angle;
778 struct wlsc_vector force, tmp;
779 double step = 0.3;
780 double friction = 1;
781 double spring = 0.2;
782
783 s = container_of(animate, struct wlsc_surface, animate);
784
785 angle = s->current_angle;
786 angle_force = (s->target_angle - angle) * spring +
787 (s->previous_angle - angle) * friction;
788
789 s->current_angle = angle + (angle - s->previous_angle) + angle_force * step;
790 s->previous_angle = angle;
791
792 force = s->target;
793 wlsc_vector_subtract(&force, &s->current);
794 wlsc_vector_scalar(&force, spring);
795 tmp = s->previous;
796 wlsc_vector_subtract(&tmp, &s->current);
797 wlsc_vector_scalar(&tmp, friction);
798 wlsc_vector_add(&force, &tmp);
799
800 wlsc_vector_scalar(&force, step);
801 wlsc_vector_add(&force, &s->current);
802 wlsc_vector_subtract(&force, &s->previous);
803 s->previous = s->current;
804 wlsc_vector_add(&s->current, &force);
805
806 wlsc_surface_update_matrix(s);
807
808 tmp = s->current;
809 wlsc_vector_subtract(&tmp, &s->target);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500810 if (tmp.x * tmp.x + tmp.y * tmp.y + tmp.z * tmp.z > 0.001) {
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500811 schedule_repaint(compositor);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500812 } else {
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500813 wl_list_remove(&s->animate.link);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500814 wl_list_init(&s->animate.link);
815 }
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500816}
817
818static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500819compositor_create_surface(struct wl_client *client,
820 struct wl_compositor *compositor, uint32_t id)
821{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500822 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
823 struct wlsc_surface *es;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500824
825 es = malloc(sizeof *es);
826 if (es == NULL)
827 /* FIXME: Send OOM event. */
828 return;
829
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500830 wlsc_surface_init(es, ec, NULL, 0, 0, 0, 0);
831 es->animate.animate = animate_surface;
832 wl_list_init(&es->animate.link);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500833
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500834 wl_list_insert(ec->surface_list.prev, &es->link);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500835 wl_client_add_surface(client, &es->base,
836 &surface_interface, id);
837}
838
839static void
840compositor_commit(struct wl_client *client,
841 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500842{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500843 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500844
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500845 schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500846 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500847}
848
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500849const static struct wl_compositor_interface compositor_interface = {
850 compositor_create_surface,
851 compositor_commit
852};
853
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500854static void
855wlsc_surface_transform(struct wlsc_surface *surface,
856 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
857{
858 /* Transform to surface coordinates. */
859 *sx = (x - surface->map.x) * surface->width / surface->map.width;
860 *sy = (y - surface->map.y) * surface->height / surface->map.height;
861}
862
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500863static void
864wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
865 struct wlsc_surface *surface)
866{
867 if (device->keyboard_focus == surface)
868 return;
869
870 if (device->keyboard_focus &&
871 (!surface || device->keyboard_focus->base.client != surface->base.client))
872 wl_surface_post_event(&device->keyboard_focus->base,
873 &device->base,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500874 WL_INPUT_KEYBOARD_FOCUS, NULL, NULL);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500875
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500876 if (surface)
877 wl_surface_post_event(&surface->base,
878 &device->base,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500879 WL_INPUT_KEYBOARD_FOCUS,
880 &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500881
882 device->keyboard_focus = surface;
883}
884
885static void
886wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
887 struct wlsc_surface *surface)
888{
889 if (device->pointer_focus == surface)
890 return;
891
892 if (device->pointer_focus &&
893 (!surface || device->pointer_focus->base.client != surface->base.client))
894 wl_surface_post_event(&device->pointer_focus->base,
895 &device->base,
896 WL_INPUT_POINTER_FOCUS, NULL);
897 if (surface)
898 wl_surface_post_event(&surface->base,
899 &device->base,
900 WL_INPUT_POINTER_FOCUS, &surface->base);
901
902 device->pointer_focus = surface;
903}
904
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500905static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500906pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500907{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500908 struct wlsc_compositor *ec = device->ec;
909 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500910
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500911 if (device->grab > 0) {
912 wlsc_surface_transform(device->grab_surface,
913 device->x, device->y, sx, sy);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500914 return device->grab_surface;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500915 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500916
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500917 es = container_of(ec->surface_list.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500918 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500919 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500920 if (es->map.x <= device->x &&
921 device->x < es->map.x + es->map.width &&
922 es->map.y <= device->y &&
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500923 device->y < es->map.y + es->map.height) {
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500924 wlsc_surface_transform(es, device->x, device->y, sx, sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500925 return es;
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500926 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500927
928 es = container_of(es->link.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500929 struct wlsc_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500930
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500931 }
932
933 return NULL;
934}
935
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500936void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500937notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500938{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500939 struct wlsc_surface *es;
940 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500941 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500942 const int hotspot_x = 16, hotspot_y = 16;
943 int32_t sx, sy;
944
Ray Strodee96dcb82008-12-20 02:00:49 -0500945 if (!ec->vt_active)
946 return;
947
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500948 /* FIXME: We need some multi head love here. */
949 output = container_of(ec->output_list.next, struct wlsc_output, link);
950 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500951 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500952 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500953 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500954 if (x >= output->x + output->width)
955 x = output->x + output->width - 1;
956 if (y >= output->y + output->height)
957 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500958
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500959 device->x = x;
960 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500961 es = pick_surface(device, &sx, &sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500962
963 wlsc_input_device_set_pointer_focus(device, es);
964
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500965 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500966 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500967 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500968
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500969 device->sprite->map.x = x - hotspot_x;
970 device->sprite->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500971
972 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500973}
974
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500975void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500976notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500977 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500978{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500979 struct wlsc_surface *es;
980 struct wlsc_compositor *ec = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500981 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500982
Ray Strodee96dcb82008-12-20 02:00:49 -0500983 if (!ec->vt_active)
984 return;
985
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500986 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500987 if (es) {
988 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500989 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500990
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500991 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500992 /* FIXME: We need callbacks when the surfaces
993 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500994 device->grab++;
995 device->grab_surface = es;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500996 wlsc_input_device_set_keyboard_focus(device, es);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500997 } else {
998 device->grab--;
999 }
1000
Kristian Høgsberg5a75c902008-12-10 13:16:50 -05001001 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001002 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001003 WL_INPUT_BUTTON, button, state,
1004 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001005
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001006 schedule_repaint(device->ec);
1007 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001008}
1009
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001010static void on_term_signal(int signal_number, void *data);
1011
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001012static void
1013update_surface_targets(struct wlsc_compositor *compositor, int primary)
1014{
1015 struct wlsc_surface *s;
1016 int i;
1017
1018 i = 0;
1019 s = container_of(compositor->surface_list.next,
1020 struct wlsc_surface, link);
1021 while (&s->link != &compositor->surface_list) {
1022 if (i < primary) {
1023 s->target.x = -400 + 500 * (i - primary);
1024 s->target.y = 0;
1025 s->target.z = -1500;
1026 s->target_angle = M_PI / 4;
1027 } else if (i == primary) {
1028 s->target.x = 0;
1029 s->target.y = 0;
1030 s->target.z = -1000;
1031 s->target_angle = 0;
1032 compositor->primary = s;
1033 } else {
1034 s->target.x = 400 + 500 * (i - primary);
1035 s->target.y = 0;
1036 s->target.z = -1500;
1037 s->target_angle = -M_PI / 4;
1038 }
1039 wl_list_remove(&s->animate.link);
1040 wl_list_insert(compositor->animate_list.prev, &s->animate.link);
1041 s = container_of(s->link.next,
1042 struct wlsc_surface, link);
1043 i++;
1044 }
1045
1046 schedule_repaint(compositor);
1047}
1048
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001049void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001050notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001051 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001052{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001053 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001054 uint32_t *k, *end;
1055
1056 if (!ec->vt_active)
1057 return;
Ray Strodee96dcb82008-12-20 02:00:49 -05001058
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001059 switch (key | ec->meta_state) {
1060 case KEY_EJECTCD | META_DOWN:
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001061 on_term_signal(SIGTERM, ec);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001062 return;
1063
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001064 case KEY_1 | META_DOWN:
1065 case KEY_2 | META_DOWN:
1066 case KEY_3 | META_DOWN:
1067 case KEY_4 | META_DOWN:
1068 case KEY_5 | META_DOWN:
1069 update_surface_targets(ec, key - KEY_1);
Kristian Høgsbergce5a9c12009-02-22 20:59:44 -05001070 if (device->grab == 0)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001071 wlsc_input_device_set_keyboard_focus(device, ec->primary);
Kristian Høgsbergce5a9c12009-02-22 20:59:44 -05001072 device->keyboard_focus = ec->primary;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001073 return;
1074
1075 case KEY_LEFTMETA:
1076 case KEY_RIGHTMETA:
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001077 case KEY_LEFTMETA | META_DOWN:
1078 case KEY_RIGHTMETA | META_DOWN:
1079 ec->meta_state = state ? META_DOWN : 0;
Kristian Høgsberg1febe012009-02-10 18:30:14 -05001080 if (state == 0 && ec->primary != NULL) {
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001081 ec->primary->target.z = 0;
1082 wl_list_remove(&ec->primary->animate.link);
1083 wl_list_insert(&ec->animate_list,
1084 &ec->primary->animate.link);
1085 schedule_repaint(ec);
1086 }
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001087 return;
1088 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001089
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001090 end = device->keys.data + device->keys.size;
1091 for (k = device->keys.data; k < end; k++) {
1092 if (*k == key)
1093 *k = *--end;
1094 }
1095 device->keys.size = (void *) end - device->keys.data;
1096 if (state) {
1097 k = wl_array_add(&device->keys, sizeof *k);
1098 *k = key;
1099 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001100
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001101 if (device->keyboard_focus != NULL)
1102 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -05001103 &device->base,
1104 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001105}
1106
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001107struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001108evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001109 struct wl_display *display, const char *path);
1110
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001111static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001112create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001113{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001114 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001115
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001116 device = malloc(sizeof *device);
1117 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001118 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001119
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001120 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001121 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001122 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001123 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001124 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001125 device->x = 100;
1126 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001127 device->ec = ec;
1128
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001129 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001130
1131 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001132}
1133
1134void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001135wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001136{
1137 *x = device->x;
1138 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001139}
1140
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001141static void
1142post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001143{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001144 struct wlsc_output *output =
1145 container_of(global, struct wlsc_output, base);
1146
1147 wl_client_post_event(client, global,
1148 WL_OUTPUT_GEOMETRY,
1149 output->width, output->height);
1150}
1151
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001152static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001153init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001154{
1155 static const EGLint config_attribs[] = {
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001156 EGL_DEPTH_SIZE, 0,
1157 EGL_STENCIL_SIZE, 0,
1158 EGL_CONFIG_CAVEAT, EGL_NONE,
1159 EGL_RED_SIZE, 8,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001160 EGL_NONE
1161 };
1162
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001163 EGLint major, minor;
1164
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -05001165 ec->display = eglCreateDisplayNative(device);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001166 if (ec->display == NULL) {
1167 fprintf(stderr, "failed to create display\n");
1168 return -1;
1169 }
1170
1171 if (!eglInitialize(ec->display, &major, &minor)) {
1172 fprintf(stderr, "failed to initialize display\n");
1173 return -1;
1174 }
1175
1176 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
1177 return -1;
1178
1179 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
1180 if (ec->context == NULL) {
1181 fprintf(stderr, "failed to create context\n");
1182 return -1;
1183 }
1184 return 0;
1185}
1186
1187static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001188create_output(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001189{
1190 const static EGLint surface_attribs[] = {
1191 EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
1192 EGL_NONE
1193 };
1194
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001195 drmModeConnector *connector;
1196 drmModeRes *resources;
1197 drmModeEncoder *encoder;
Kristian Høgsberg41a10682009-02-15 22:37:03 -05001198 drmModeModeInfo *mode;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001199 struct drm_i915_gem_create create;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001200 struct drm_gem_flink flink;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001201 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001202 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001203
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001204 if (ec->display == NULL && init_egl(ec, device) < 0) {
1205 fprintf(stderr, "failed to initialize egl\n");
1206 return -1;
1207 }
1208
1209 output = malloc(sizeof *output);
1210 if (output == NULL)
1211 return -1;
1212
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001213 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001214 resources = drmModeGetResources(fd);
1215 if (!resources) {
1216 fprintf(stderr, "drmModeGetResources failed\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001217 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001218 }
1219
1220 for (i = 0; i < resources->count_connectors; i++) {
1221 connector = drmModeGetConnector(fd, resources->connectors[i]);
1222 if (connector == NULL)
1223 continue;
1224
1225 if (connector->connection == DRM_MODE_CONNECTED &&
1226 connector->count_modes > 0)
1227 break;
1228
1229 drmModeFreeConnector(connector);
1230 }
1231
1232 if (i == resources->count_connectors) {
1233 fprintf(stderr, "No currently active connector found.\n");
1234 return -1;
1235 }
1236
1237 mode = &connector->modes[0];
1238
1239 for (i = 0; i < resources->count_encoders; i++) {
1240 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
1241
1242 if (encoder == NULL)
1243 continue;
1244
1245 if (encoder->encoder_id == connector->encoder_id)
1246 break;
1247
1248 drmModeFreeEncoder(encoder);
1249 }
1250
1251 /* Mode size at 32 bpp */
1252 create.size = mode->hdisplay * mode->vdisplay * 4;
1253 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
1254 fprintf(stderr, "gem create failed: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001255 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001256 }
1257
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001258 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001259 32, 32, mode->hdisplay * 4, create.handle, &output->fb_id);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001260 if (ret) {
1261 fprintf(stderr, "failed to add fb: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001262 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001263 }
1264
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001265 ret = drmModeSetCrtc(fd, encoder->crtc_id, output->fb_id, 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001266 &connector->connector_id, 1, mode);
1267 if (ret) {
1268 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001269 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001270 }
1271
1272 flink.handle = create.handle;
1273 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
1274 fprintf(stderr, "gem flink failed: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001275 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001276 }
1277
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001278 output->compositor = ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001279 output->crtc_id = encoder->crtc_id;
1280 output->connector_id = connector->connector_id;
1281 output->mode = mode;
Kristian Høgsberge68d24c2009-01-15 11:44:35 -05001282 output->x = 0;
1283 output->y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001284 output->width = mode->hdisplay;
1285 output->height = mode->vdisplay;
1286 output->stride = mode->hdisplay * 4;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001287
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001288 output->surface = eglCreateSurfaceForName(ec->display, ec->config,
1289 flink.name,
1290 output->width, output->height,
1291 output->stride,
1292 surface_attribs);
1293 if (output->surface == NULL) {
1294 fprintf(stderr, "failed to create surface\n");
1295 return -1;
1296 }
1297
1298 output->base.interface = &wl_output_interface;
1299 wl_display_add_object(ec->wl_display, &output->base);
1300 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
1301 wl_list_insert(ec->output_list.prev, &output->link);
1302
1303 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
1304 fprintf(stderr, "failed to make context current\n");
1305 return -1;
1306 }
1307
1308 output->background = background_create(output, option_background);
1309
1310 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001311}
1312
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001313static const struct wl_interface visual_interface = {
1314 "visual", 1,
1315};
1316
1317static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001318add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001319{
1320 ec->argb_visual.base.interface = &visual_interface;
1321 ec->argb_visual.base.implementation = NULL;
1322 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001323 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001324
1325 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1326 ec->premultiplied_argb_visual.base.implementation = NULL;
1327 wl_display_add_object(ec->wl_display,
1328 &ec->premultiplied_argb_visual.base);
1329 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001330 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001331
1332 ec->rgb_visual.base.interface = &visual_interface;
1333 ec->rgb_visual.base.implementation = NULL;
1334 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001335 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1336}
1337
Ray Strode19ad6a92008-12-19 01:45:41 -05001338static void on_enter_vt(int signal_number, void *data)
1339{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001340 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001341 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001342 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -05001343
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001344 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001345 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001346
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001347 fd = eglGetDisplayFD(ec->display);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001348 output = container_of(ec->output_list.next, struct wlsc_output, link);
1349 while (&output->link != &ec->output_list) {
1350 ret = drmModeSetCrtc(fd, output->crtc_id, output->fb_id, 0, 0,
1351 &output->connector_id, 1, output->mode);
1352 if (ret)
1353 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1354 output->connector_id);
1355
1356 output = container_of(output->link.next,
1357 struct wlsc_output, link);
Ray Strode19ad6a92008-12-19 01:45:41 -05001358 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001359}
1360
1361static void on_leave_vt(int signal_number, void *data)
1362{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001363 struct wlsc_compositor *ec = data;
Ray Strode19ad6a92008-12-19 01:45:41 -05001364
1365 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001366 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001367}
1368
Ray Strode966aa112008-12-19 14:28:02 -05001369static void
1370on_tty_input(int fd, uint32_t mask, void *data)
1371{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001372 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001373
1374 /* Ignore input to tty. We get keyboard events from evdev
1375 */
1376 tcflush(ec->tty_fd, TCIFLUSH);
1377}
1378
1379static void on_term_signal(int signal_number, void *data)
1380{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001381 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001382
1383 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1384 fprintf(stderr, "could not restore terminal to canonical mode\n");
1385
1386 exit(0);
1387}
1388
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001389static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001390{
1391 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001392 struct vt_mode mode = { 0 };
1393
1394 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1395 if (ec->tty_fd <= 0) {
1396 fprintf(stderr, "failed to open active tty: %m\n");
1397 return -1;
1398 }
Ray Strode966aa112008-12-19 14:28:02 -05001399
1400 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1401 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001402 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001403 }
1404
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001405 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001406 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001407 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001408
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001409 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001410 raw_attributes.c_oflag |= OPOST | OCRNL;
1411
1412 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1413 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1414
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001415 ec->term_signal_source =
1416 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001417
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001418 ec->tty_input_source =
1419 wl_event_loop_add_fd(loop, ec->tty_fd,
1420 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001421
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001422 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001423 mode.mode = VT_PROCESS;
1424 mode.relsig = SIGUSR1;
1425 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001426 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001427 fprintf(stderr, "failed to take control of vt handling\n");
1428 }
1429
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001430 ec->leave_vt_source =
1431 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1432 ec->enter_vt_source =
1433 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001434
1435 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001436}
1437
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001438static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001439init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001440{
1441 struct udev_enumerate *e;
1442 struct udev_list_entry *entry;
1443 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001444 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001445 struct wlsc_input_device *input_device;
1446
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001447 ec->udev = udev_new();
1448 if (ec->udev == NULL) {
1449 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001450 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001451 }
1452
1453 input_device = create_input_device(ec);
1454
1455 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001456 udev_enumerate_add_match_subsystem(e, "input");
1457 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001458 udev_enumerate_scan_devices(e);
1459 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1460 path = udev_list_entry_get_name(entry);
1461 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001462 evdev_input_device_create(input_device, ec->wl_display,
1463 udev_device_get_devnode(device));
1464 }
1465 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001466
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001467 e = udev_enumerate_new(ec->udev);
1468 udev_enumerate_add_match_subsystem(e, "drm");
1469 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1470 udev_enumerate_scan_devices(e);
1471 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1472 path = udev_list_entry_get_name(entry);
1473 device = udev_device_new_from_syspath(ec->udev, path);
1474 if (create_output(ec, device) < 0) {
1475 fprintf(stderr, "failed to create output for %s\n", path);
1476 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001477 }
1478 }
1479 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001480
1481 /* Create the pointer surface now that we have a current EGL context. */
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -05001482 input_device->sprite =
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001483 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1484
1485 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001486}
1487
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001488static struct wlsc_compositor *
1489wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001490{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001491 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001492 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001493 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001494
1495 ec = malloc(sizeof *ec);
1496 if (ec == NULL)
1497 return NULL;
1498
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001499 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001500 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001501
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001502 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001503
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001504 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001505
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001506 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001507 wl_list_init(&ec->input_device_list);
1508 wl_list_init(&ec->output_list);
1509 if (init_libudev(ec) < 0) {
1510 fprintf(stderr, "failed to initialize devices\n");
1511 return NULL;
1512 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001513
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001514 shooter = screenshooter_create(ec);
1515 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001516 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001517
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001518 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001519
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001520 setup_tty(ec, loop);
1521
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001522 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001523 schedule_repaint(ec);
1524
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001525 wl_list_init(&ec->animate_list);
1526
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001527 return ec;
1528}
1529
1530/* The plan here is to generate a random anonymous socket name and
1531 * advertise that through a service on the session dbus.
1532 */
1533static const char socket_name[] = "\0wayland";
1534
1535int main(int argc, char *argv[])
1536{
1537 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001538 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001539 GError *error = NULL;
1540 GOptionContext *context;
1541
1542 context = g_option_context_new(NULL);
1543 g_option_context_add_main_entries(context, option_entries, "Wayland");
1544 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1545 fprintf(stderr, "option parsing failed: %s\n", error->message);
1546 exit(EXIT_FAILURE);
1547 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001548
1549 display = wl_display_create();
1550
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001551 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001552 if (ec == NULL) {
1553 fprintf(stderr, "failed to create compositor\n");
1554 exit(EXIT_FAILURE);
1555 }
1556
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001557 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001558 fprintf(stderr, "failed to add socket: %m\n");
1559 exit(EXIT_FAILURE);
1560 }
1561
1562 wl_display_run(display);
1563
1564 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001565}