blob: d2367489d822d8d9075eddbeb080e768ad12d6c7 [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;
84 struct wlsc_surface *pointer_surface;
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;
89 struct wlsc_surface *focus_surface;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050090};
91
Kristian Høgsberg8e438622009-01-26 23:07:00 -050092struct wlsc_compositor {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040093 struct wl_compositor base;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050094 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
95
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040096 EGLDisplay display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040097 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050098 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040099 struct wl_display *wl_display;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500100
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500101 struct wl_list output_list;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500102 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500103 struct wl_list surface_list;
104
Ray Strode966aa112008-12-19 14:28:02 -0500105 struct wl_event_source *term_signal_source;
106
107 /* tty handling state */
108 int tty_fd;
Ray Strodee96dcb82008-12-20 02:00:49 -0500109 uint32_t vt_active : 1;
Ray Strode966aa112008-12-19 14:28:02 -0500110
111 struct termios terminal_attributes;
112 struct wl_event_source *tty_input_source;
Ray Strode19ad6a92008-12-19 01:45:41 -0500113 struct wl_event_source *enter_vt_source;
114 struct wl_event_source *leave_vt_source;
115
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500116 struct udev *udev;
117
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500118 /* Repaint state. */
119 struct wl_event_source *timer_source;
120 int repaint_needed;
121 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500122 struct timespec previous_swap;
123 uint32_t current_frame;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500124
125 uint32_t meta_state;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500126 struct wl_list animate_list;
127 struct wlsc_surface *primary;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400128};
129
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500130#define META_DOWN 256
131
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500132struct wlsc_animate {
133 struct wl_list link;
134 void (*animate)(struct wlsc_animate *animate,
135 struct wlsc_compositor *compositor,
136 uint32_t frame, uint32_t msecs);
137};
138
139struct wlsc_vector {
140 GLdouble x, y, z;
141};
142
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500143struct wlsc_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500144 struct wl_surface base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500145 struct wlsc_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500146 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400147 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400148 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500149 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500150 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500151 struct wl_list link;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500152 struct wlsc_matrix matrix;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500153
154 struct wlsc_vector target, current, previous;
155 GLdouble target_angle, current_angle, previous_angle;
156 struct wlsc_animate animate;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400157};
158
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500159static const char *option_background = "background.jpg";
160
161static const GOptionEntry option_entries[] = {
162 { "background", 'b', 0, G_OPTION_ARG_STRING,
163 &option_background, "Background image" },
164 { NULL }
165};
166
167
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500168struct screenshooter {
169 struct wl_object base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500170 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500171};
172
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500173struct screenshooter_interface {
174 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
175};
176
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500177static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500178screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500179{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500180 struct wlsc_compositor *ec = shooter->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500181 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500182 char buffer[256];
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500183 GdkPixbuf *pixbuf, *normal;
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500184 GError *error = NULL;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500185 unsigned char *data;
186 int i, j;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500187
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500188 i = 0;
189 output = container_of(ec->output_list.next, struct wlsc_output, link);
190 while (&output->link != &ec->output_list) {
191 snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500192 data = malloc(output->width * output->height * 4);
193 if (data == NULL) {
194 fprintf(stderr, "couldn't allocate image buffer\n");
195 continue;
196 }
197
198 glReadBuffer(GL_FRONT);
199 glPixelStorei(GL_PACK_ALIGNMENT, 1);
200 glReadPixels(0, 0, output->width, output->height,
201 GL_RGBA, GL_UNSIGNED_BYTE, data);
202
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500203 /* FIXME: We should just use a RGB visual for the frontbuffer. */
204 for (j = 3; j < output->width * output->height * 4; j += 4)
205 data[j] = 0xff;
206
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500207 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500208 8, output->width, output->height, output->width * 4,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500209 NULL, NULL);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500210 normal = gdk_pixbuf_flip(pixbuf, FALSE);
211 gdk_pixbuf_save(normal, buffer, "png", &error, NULL);
212 gdk_pixbuf_unref(normal);
213 gdk_pixbuf_unref(pixbuf);
214 free(data);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500215
216 output = container_of(output->link.next,
217 struct wlsc_output, link);
218 }
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500219}
220
Kristian Høgsbergfb6d68d2008-12-21 21:54:51 -0500221static const struct wl_message screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500222 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500223};
224
225static const struct wl_interface screenshooter_interface = {
226 "screenshooter", 1,
227 ARRAY_LENGTH(screenshooter_methods),
228 screenshooter_methods,
229};
230
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500231struct screenshooter_interface screenshooter_implementation = {
232 screenshooter_shoot
233};
234
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500235static struct screenshooter *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500236screenshooter_create(struct wlsc_compositor *ec)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500237{
238 struct screenshooter *shooter;
239
240 shooter = malloc(sizeof *shooter);
241 if (shooter == NULL)
242 return NULL;
243
244 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500245 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500246 shooter->ec = ec;
247
248 return shooter;
249};
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500250
251static void
252wlsc_matrix_init(struct wlsc_matrix *matrix)
253{
254 static const struct wlsc_matrix identity = {
255 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
256 };
257
258 memcpy(matrix, &identity, sizeof identity);
259}
260
261static void
262wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
263{
264 struct wlsc_matrix tmp;
265 const GLdouble *row, *column;
266 div_t d;
267 int i, j;
268
269 for (i = 0; i < 16; i++) {
270 tmp.d[i] = 0;
271 d = div(i, 4);
272 row = m->d + d.quot * 4;
273 column = n->d + d.rem;
274 for (j = 0; j < 4; j++)
275 tmp.d[i] += row[j] * column[j * 4];
276 }
277 memcpy(m, &tmp, sizeof tmp);
278}
279
280static void
281wlsc_matrix_translate(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
282{
283 struct wlsc_matrix translate = {
284 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
285 };
286
287 wlsc_matrix_multiply(matrix, &translate);
288}
289
290static void
291wlsc_matrix_scale(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
292{
293 struct wlsc_matrix scale = {
294 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
295 };
296
297 wlsc_matrix_multiply(matrix, &scale);
298}
299
300static void
301wlsc_matrix_rotate(struct wlsc_matrix *matrix,
302 GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
303{
304 GLdouble c = cos(angle);
305 GLdouble s = sin(angle);
306 struct wlsc_matrix rotate = {
307 { x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
308 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) - x * s, 0,
309 x * z * (1 - c) + y * x, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
310 0, 0, 0, 1 }
311 };
312
313 wlsc_matrix_multiply(matrix, &rotate);
314}
315
316static void
317wlsc_surface_update_matrix(struct wlsc_surface *es)
318{
319 GLdouble tx, ty;
320
321 tx = es->map.x + es->map.width / 2;
322 ty = es->map.y + es->map.height / 2;
323
324 wlsc_matrix_init(&es->matrix);
325 wlsc_matrix_translate(&es->matrix, -tx, -ty, 0);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500326 wlsc_matrix_rotate(&es->matrix, es->current_angle, 0, 1, 0);
327 wlsc_matrix_translate(&es->matrix, tx + es->current.x,
328 ty + es->current.y, es->current.z);
329}
330
331static void
332wlsc_surface_init(struct wlsc_surface *surface,
333 struct wlsc_compositor *compositor, struct wl_visual *visual,
334 int32_t x, int32_t y, int32_t width, int32_t height)
335{
336 glGenTextures(1, &surface->texture);
337 surface->compositor = compositor;
338 surface->map.x = x;
339 surface->map.y = y;
340 surface->map.width = width;
341 surface->map.height = height;
342 surface->surface = EGL_NO_SURFACE;
343 surface->visual = visual;
344 surface->current.x = 0;
345 surface->current.y = 0;
346 surface->current.z = 0;
347 surface->current_angle = 0;
348 surface->target.x = 0;
349 surface->target.y = 0;
350 surface->target.z = 0;
351 surface->target_angle = 0;
352 surface->previous_angle = 0;
353
354 wlsc_surface_update_matrix(surface);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500355}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500356
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500357static struct wlsc_surface *
358wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500359 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500360 int x, int y, int width, int height)
361{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500362 struct wlsc_surface *es;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500363 int stride;
364 void *data;
365
366 stride = cairo_image_surface_get_stride(surface);
367 data = cairo_image_surface_get_data(surface);
368
369 es = malloc(sizeof *es);
370 if (es == NULL)
371 return NULL;
372
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500373 wlsc_surface_init(es, ec, &ec->premultiplied_argb_visual,
374 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500375 glBindTexture(GL_TEXTURE_2D, es->texture);
376 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500377 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
378 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
379 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500380 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
381 GL_BGRA, GL_UNSIGNED_BYTE, data);
382
Kristian Høgsberg54879822008-11-23 17:07:32 -0500383 return es;
384}
385
386static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500387wlsc_surface_destroy(struct wlsc_surface *es, struct wlsc_compositor *ec)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500388{
389 glDeleteTextures(1, &es->texture);
390 if (es->surface != EGL_NO_SURFACE)
391 eglDestroySurface(ec->display, es->surface);
392 free(es);
393}
394
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400395static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500396pointer_path(cairo_t *cr, int x, int y)
397{
398 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
399 const int width = 16, height = 16;
400
401 cairo_move_to(cr, x, y);
402 cairo_line_to(cr, x + tx, y + ty);
403 cairo_line_to(cr, x + dx, y + dy);
404 cairo_line_to(cr, x + width - end, y + height);
405 cairo_line_to(cr, x + width, y + height - end);
406 cairo_line_to(cr, x + dy, y + dx);
407 cairo_line_to(cr, x + ty, y + tx);
408 cairo_close_path(cr);
409}
410
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500411static struct wlsc_surface *
412pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500413{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500414 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500415 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500416 cairo_surface_t *surface;
417 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500418
419 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
420 width, height);
421
422 cr = cairo_create(surface);
423 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500424 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500425 cairo_set_source_rgb(cr, 0, 0, 0);
426 cairo_stroke_preserve(cr);
427 cairo_fill(cr);
428 blur_surface(surface, width);
429
430 pointer_path(cr, hotspot_x, hotspot_y);
431 cairo_stroke_preserve(cr);
432 cairo_set_source_rgb(cr, 1, 1, 1);
433 cairo_fill(cr);
434 cairo_destroy(cr);
435
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500436 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500437 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500438 x - hotspot_x,
439 y - hotspot_y,
440 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500441
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500442 cairo_surface_destroy(surface);
443
Kristian Høgsberg54879822008-11-23 17:07:32 -0500444 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500445}
446
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500447static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500448background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500449{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500450 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500451 GdkPixbuf *pixbuf;
452 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500453 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500454 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500455
456 background = malloc(sizeof *background);
457 if (background == NULL)
458 return NULL;
459
460 g_type_init();
461
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500462 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500463 output->width,
464 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500465 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500466 if (error != NULL) {
467 free(background);
468 return NULL;
469 }
470
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500471 data = gdk_pixbuf_get_pixels(pixbuf);
472
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500473 wlsc_surface_init(background, output->compositor,
474 &output->compositor->rgb_visual,
475 output->x, output->y, output->width, output->height);
476
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500477 glBindTexture(GL_TEXTURE_2D, background->texture);
478 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500479 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500480 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
481 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500482
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500483 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500484 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500485 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500486 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500487
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500488 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
489 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500490 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500491
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500492 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500493}
494
495static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500496wlsc_surface_draw(struct wlsc_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500497{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500498 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500499 GLint vertices[12];
500 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
501 GLuint indices[4] = { 0, 1, 2, 3 };
502
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500503 vertices[0] = es->map.x;
504 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500505 vertices[2] = 0;
506
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500507 vertices[3] = es->map.x;
508 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500509 vertices[5] = 0;
510
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500511 vertices[6] = es->map.x + es->map.width;
512 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500513 vertices[8] = 0;
514
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500515 vertices[9] = es->map.x + es->map.width;
516 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500517 vertices[11] = 0;
518
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500519 if (es->visual == &ec->argb_visual) {
520 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
521 glEnable(GL_BLEND);
522 } else if (es->visual == &ec->premultiplied_argb_visual) {
523 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
524 glEnable(GL_BLEND);
525 } else {
526 glDisable(GL_BLEND);
527 }
528
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500529 glPushMatrix();
530 glMultMatrixd(es->matrix.d);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500531 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500532 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500533 glEnableClientState(GL_VERTEX_ARRAY);
534 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
535 glVertexPointer(3, GL_INT, 0, vertices);
536 glTexCoordPointer(2, GL_INT, 0, tex_coords);
537 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500538 glPopMatrix();
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500539}
540
541static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500542wlsc_vector_add(struct wlsc_vector *v1, struct wlsc_vector *v2)
543{
544 v1->x += v2->x;
545 v1->y += v2->y;
546 v1->z += v2->z;
547}
548
549static void
550wlsc_vector_subtract(struct wlsc_vector *v1, struct wlsc_vector *v2)
551{
552 v1->x -= v2->x;
553 v1->y -= v2->y;
554 v1->z -= v2->z;
555}
556
557static void
558wlsc_vector_scalar(struct wlsc_vector *v1, GLdouble s)
559{
560 v1->x *= s;
561 v1->y *= s;
562 v1->z *= s;
563}
564
565static void
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500566repaint_output(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400567{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500568 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500569 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500570 struct wlsc_input_device *eid;
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500571 double s = 3000;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500572
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500573 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
574 fprintf(stderr, "failed to make context current\n");
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500575 return;
576 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500577
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500578 glViewport(0, 0, output->width, output->height);
579 glMatrixMode(GL_PROJECTION);
580 glLoadIdentity();
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500581 glFrustum(-output->width / s, output->width / s,
582 output->height / s, -output->height / s, 1, 2 * s);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500583 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500584 glLoadIdentity();
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500585 glClearColor(0, 0, 0.2, 1);
586
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500587 glTranslatef(-output->width / 2, -output->height / 2, -s / 2);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500588
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500589 if (output->background)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500590 wlsc_surface_draw(output->background);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500591 else
592 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400593
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500594 es = container_of(ec->surface_list.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500595 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500596 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500597 wlsc_surface_draw(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500598 es = container_of(es->link.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500599 struct wlsc_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400600 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400601
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500602 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500603 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500604 while (&eid->link != &ec->input_device_list) {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500605 wlsc_surface_draw(eid->pointer_surface);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500606
607 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500608 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500609 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500610
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500611 eglSwapBuffers(ec->display, output->surface);
612}
613
614static void
615repaint(void *data)
616{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500617 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500618 struct wlsc_output *output;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500619 struct wlsc_animate *animate, *next;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500620 struct timespec ts;
621 uint32_t msecs;
622
623 if (!ec->repaint_needed) {
624 ec->repaint_on_timeout = 0;
625 return;
626 }
627
628 output = container_of(ec->output_list.next, struct wlsc_output, link);
629 while (&output->link != &ec->output_list) {
630 repaint_output(output);
631 output = container_of(output->link.next,
632 struct wlsc_output, link);
633 }
634
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500635 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500636
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500637 clock_gettime(CLOCK_MONOTONIC, &ts);
638 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500639 wl_display_post_frame(ec->wl_display, &ec->base,
640 ec->current_frame, msecs);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500641
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500642 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500643 ec->repaint_on_timeout = 1;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500644
645 animate = container_of(ec->animate_list.next, struct wlsc_animate, link);
646 while (&animate->link != &ec->animate_list) {
647 next = container_of(animate->link.next,
648 struct wlsc_animate, link);
649 animate->animate(animate, ec, ec->current_frame, msecs);
650 animate = next;
651 }
652
653 ec->current_frame++;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400654}
655
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500656static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500657schedule_repaint(struct wlsc_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400658{
659 struct wl_event_loop *loop;
660
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500661 ec->repaint_needed = 1;
662 if (!ec->repaint_on_timeout) {
663 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500664 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500665 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400666}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500667
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500668static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500669surface_destroy(struct wl_client *client,
670 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400671{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500672 struct wlsc_surface *es = (struct wlsc_surface *) surface;
673 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500674
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500675 wl_list_remove(&es->link);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500676 wl_list_remove(&es->animate.link);
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500677 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400678
679 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400680}
681
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500682static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500683surface_attach(struct wl_client *client,
684 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500685 uint32_t width, uint32_t height, uint32_t stride,
686 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400687{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500688 struct wlsc_surface *es = (struct wlsc_surface *) surface;
689 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400690
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500691 if (es->surface != EGL_NO_SURFACE)
692 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400693
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500694 es->width = width;
695 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500696 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500697 name, width, height, stride, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500698 if (visual == &ec->argb_visual.base)
699 es->visual = &ec->argb_visual;
700 else if (visual == &ec->premultiplied_argb_visual.base)
701 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500702 else if (visual == &ec->rgb_visual.base)
703 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500704 else
705 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400706
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500707 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400708 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500709 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
710 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
711 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500712 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400713}
714
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500715static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500716surface_map(struct wl_client *client,
717 struct wl_surface *surface,
718 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400719{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500720 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400721
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500722 es->map.x = x;
723 es->map.y = y;
724 es->map.width = width;
725 es->map.height = height;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500726 wlsc_surface_update_matrix(es);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400727}
728
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500729static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500730surface_copy(struct wl_client *client,
731 struct wl_surface *surface,
732 int32_t dst_x, int32_t dst_y,
733 uint32_t name, uint32_t stride,
734 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500735{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500736 struct wlsc_surface *es = (struct wlsc_surface *) surface;
737 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500738 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500739
740 /* FIXME: glCopyPixels should work, but then we'll have to
741 * call eglMakeCurrent to set up the src and dest surfaces
742 * first. This seems cheaper, but maybe there's a better way
743 * to accomplish this. */
744
745 src = eglCreateSurfaceForName(ec->display, ec->config,
746 name, x + width, y + height, stride, NULL);
747
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500748 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500749 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500750 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500751}
752
753static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500754surface_damage(struct wl_client *client,
755 struct wl_surface *surface,
756 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500757{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500758 /* FIXME: This need to take a damage region, of course. */
759}
760
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500761const static struct wl_surface_interface surface_interface = {
762 surface_destroy,
763 surface_attach,
764 surface_map,
765 surface_copy,
766 surface_damage
767};
768
769static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500770animate_surface(struct wlsc_animate *animate,
771 struct wlsc_compositor *compositor,
772 uint32_t frame, uint32_t msecs)
773{
774 struct wlsc_surface *s;
775 double angle_force, angle;
776 struct wlsc_vector force, tmp;
777 double step = 0.3;
778 double friction = 1;
779 double spring = 0.2;
780
781 s = container_of(animate, struct wlsc_surface, animate);
782
783 angle = s->current_angle;
784 angle_force = (s->target_angle - angle) * spring +
785 (s->previous_angle - angle) * friction;
786
787 s->current_angle = angle + (angle - s->previous_angle) + angle_force * step;
788 s->previous_angle = angle;
789
790 force = s->target;
791 wlsc_vector_subtract(&force, &s->current);
792 wlsc_vector_scalar(&force, spring);
793 tmp = s->previous;
794 wlsc_vector_subtract(&tmp, &s->current);
795 wlsc_vector_scalar(&tmp, friction);
796 wlsc_vector_add(&force, &tmp);
797
798 wlsc_vector_scalar(&force, step);
799 wlsc_vector_add(&force, &s->current);
800 wlsc_vector_subtract(&force, &s->previous);
801 s->previous = s->current;
802 wlsc_vector_add(&s->current, &force);
803
804 wlsc_surface_update_matrix(s);
805
806 tmp = s->current;
807 wlsc_vector_subtract(&tmp, &s->target);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500808 if (tmp.x * tmp.x + tmp.y * tmp.y + tmp.z * tmp.z > 0.001) {
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500809 schedule_repaint(compositor);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500810 } else {
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500811 wl_list_remove(&s->animate.link);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500812 wl_list_init(&s->animate.link);
813 }
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500814}
815
816static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500817compositor_create_surface(struct wl_client *client,
818 struct wl_compositor *compositor, uint32_t id)
819{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500820 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
821 struct wlsc_surface *es;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500822
823 es = malloc(sizeof *es);
824 if (es == NULL)
825 /* FIXME: Send OOM event. */
826 return;
827
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500828 wlsc_surface_init(es, ec, NULL, 0, 0, 0, 0);
829 es->animate.animate = animate_surface;
830 wl_list_init(&es->animate.link);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500831
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500832 wl_list_insert(ec->surface_list.prev, &es->link);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500833 wl_client_add_surface(client, &es->base,
834 &surface_interface, id);
835}
836
837static void
838compositor_commit(struct wl_client *client,
839 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500840{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500841 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500842
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500843 schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500844 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500845}
846
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500847const static struct wl_compositor_interface compositor_interface = {
848 compositor_create_surface,
849 compositor_commit
850};
851
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500852static void
853wlsc_surface_transform(struct wlsc_surface *surface,
854 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
855{
856 /* Transform to surface coordinates. */
857 *sx = (x - surface->map.x) * surface->width / surface->map.width;
858 *sy = (y - surface->map.y) * surface->height / surface->map.height;
859}
860
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500861static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500862pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500863{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500864 struct wlsc_compositor *ec = device->ec;
865 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500866
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500867 if (device->grab > 0) {
868 wlsc_surface_transform(device->grab_surface,
869 device->x, device->y, sx, sy);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500870 return device->grab_surface;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500871 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500872
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500873 es = container_of(ec->surface_list.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500874 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500875 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500876 if (es->map.x <= device->x &&
877 device->x < es->map.x + es->map.width &&
878 es->map.y <= device->y &&
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500879 device->y < es->map.y + es->map.height) {
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500880 wlsc_surface_transform(es, device->x, device->y, sx, sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500881 return es;
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500882 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500883
884 es = container_of(es->link.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500885 struct wlsc_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500886
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500887 }
888
889 return NULL;
890}
891
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500892void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500893notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500894{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500895 struct wlsc_surface *es;
896 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500897 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500898 const int hotspot_x = 16, hotspot_y = 16;
899 int32_t sx, sy;
900
Ray Strodee96dcb82008-12-20 02:00:49 -0500901 if (!ec->vt_active)
902 return;
903
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500904 /* FIXME: We need some multi head love here. */
905 output = container_of(ec->output_list.next, struct wlsc_output, link);
906 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500907 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500908 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500909 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500910 if (x >= output->x + output->width)
911 x = output->x + output->width - 1;
912 if (y >= output->y + output->height)
913 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500914
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500915 device->x = x;
916 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500917 es = pick_surface(device, &sx, &sy);
918 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500919 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500920 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500921
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500922 device->pointer_surface->map.x = x - hotspot_x;
923 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500924
925 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500926}
927
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500928void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500929notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500930 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500931{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500932 struct wlsc_surface *es;
933 struct wlsc_compositor *ec = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500934 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500935
Ray Strodee96dcb82008-12-20 02:00:49 -0500936 if (!ec->vt_active)
937 return;
938
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500939 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500940 if (es) {
941 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500942 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500943
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500944 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500945 /* FIXME: We need callbacks when the surfaces
946 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500947 device->grab++;
948 device->grab_surface = es;
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500949 device->focus_surface = es;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500950 } else {
951 device->grab--;
952 }
953
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500954 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500955 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500956 WL_INPUT_BUTTON, button, state,
957 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500958
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500959 schedule_repaint(device->ec);
960 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500961}
962
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500963static void on_term_signal(int signal_number, void *data);
964
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500965static void
966update_surface_targets(struct wlsc_compositor *compositor, int primary)
967{
968 struct wlsc_surface *s;
969 int i;
970
971 i = 0;
972 s = container_of(compositor->surface_list.next,
973 struct wlsc_surface, link);
974 while (&s->link != &compositor->surface_list) {
975 if (i < primary) {
976 s->target.x = -400 + 500 * (i - primary);
977 s->target.y = 0;
978 s->target.z = -1500;
979 s->target_angle = M_PI / 4;
980 } else if (i == primary) {
981 s->target.x = 0;
982 s->target.y = 0;
983 s->target.z = -1000;
984 s->target_angle = 0;
985 compositor->primary = s;
986 } else {
987 s->target.x = 400 + 500 * (i - primary);
988 s->target.y = 0;
989 s->target.z = -1500;
990 s->target_angle = -M_PI / 4;
991 }
992 wl_list_remove(&s->animate.link);
993 wl_list_insert(compositor->animate_list.prev, &s->animate.link);
994 s = container_of(s->link.next,
995 struct wlsc_surface, link);
996 i++;
997 }
998
999 schedule_repaint(compositor);
1000}
1001
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001002void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001003notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001004 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001005{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001006 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001007 struct wlsc_surface *s;
Ray Strodee96dcb82008-12-20 02:00:49 -05001008
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001009 switch (key | ec->meta_state) {
1010 case KEY_EJECTCD | META_DOWN:
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001011 on_term_signal(SIGTERM, ec);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001012 return;
1013
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001014 case KEY_1 | META_DOWN:
1015 case KEY_2 | META_DOWN:
1016 case KEY_3 | META_DOWN:
1017 case KEY_4 | META_DOWN:
1018 case KEY_5 | META_DOWN:
1019 update_surface_targets(ec, key - KEY_1);
1020 if (device->grab == 0 && s != NULL)
1021 device->focus_surface = ec->primary;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001022 return;
1023
1024 case KEY_LEFTMETA:
1025 case KEY_RIGHTMETA:
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001026 case KEY_LEFTMETA | META_DOWN:
1027 case KEY_RIGHTMETA | META_DOWN:
1028 ec->meta_state = state ? META_DOWN : 0;
Kristian Høgsberg1febe012009-02-10 18:30:14 -05001029 if (state == 0 && ec->primary != NULL) {
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001030 ec->primary->target.z = 0;
1031 wl_list_remove(&ec->primary->animate.link);
1032 wl_list_insert(&ec->animate_list,
1033 &ec->primary->animate.link);
1034 schedule_repaint(ec);
1035 }
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001036 return;
1037 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001038
Ray Strodee96dcb82008-12-20 02:00:49 -05001039 if (!ec->vt_active)
1040 return;
1041
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -05001042 if (device->focus_surface != NULL)
1043 wl_surface_post_event(&device->focus_surface->base,
1044 &device->base,
1045 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001046}
1047
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001048struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001049evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001050 struct wl_display *display, const char *path);
1051
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001052static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001053create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001054{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001055 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001056
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001057 device = malloc(sizeof *device);
1058 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001059 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001060
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001061 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001062 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001063 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001064 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001065 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001066 device->x = 100;
1067 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001068 device->ec = ec;
1069
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001070 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001071
1072 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001073}
1074
1075void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001076wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001077{
1078 *x = device->x;
1079 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001080}
1081
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001082static void
1083post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001084{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001085 struct wlsc_output *output =
1086 container_of(global, struct wlsc_output, base);
1087
1088 wl_client_post_event(client, global,
1089 WL_OUTPUT_GEOMETRY,
1090 output->width, output->height);
1091}
1092
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001093static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001094init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001095{
1096 static const EGLint config_attribs[] = {
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001097 EGL_DEPTH_SIZE, 0,
1098 EGL_STENCIL_SIZE, 0,
1099 EGL_CONFIG_CAVEAT, EGL_NONE,
1100 EGL_RED_SIZE, 8,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001101 EGL_NONE
1102 };
1103
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001104 EGLint major, minor;
1105
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -05001106 ec->display = eglCreateDisplayNative(device);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001107 if (ec->display == NULL) {
1108 fprintf(stderr, "failed to create display\n");
1109 return -1;
1110 }
1111
1112 if (!eglInitialize(ec->display, &major, &minor)) {
1113 fprintf(stderr, "failed to initialize display\n");
1114 return -1;
1115 }
1116
1117 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
1118 return -1;
1119
1120 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
1121 if (ec->context == NULL) {
1122 fprintf(stderr, "failed to create context\n");
1123 return -1;
1124 }
1125 return 0;
1126}
1127
1128static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001129create_output(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001130{
1131 const static EGLint surface_attribs[] = {
1132 EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
1133 EGL_NONE
1134 };
1135
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001136 drmModeConnector *connector;
1137 drmModeRes *resources;
1138 drmModeEncoder *encoder;
Kristian Høgsberg41a10682009-02-15 22:37:03 -05001139 drmModeModeInfo *mode;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001140 struct drm_i915_gem_create create;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001141 struct drm_gem_flink flink;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001142 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001143 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001144
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001145 if (ec->display == NULL && init_egl(ec, device) < 0) {
1146 fprintf(stderr, "failed to initialize egl\n");
1147 return -1;
1148 }
1149
1150 output = malloc(sizeof *output);
1151 if (output == NULL)
1152 return -1;
1153
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001154 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001155 resources = drmModeGetResources(fd);
1156 if (!resources) {
1157 fprintf(stderr, "drmModeGetResources failed\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001158 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001159 }
1160
1161 for (i = 0; i < resources->count_connectors; i++) {
1162 connector = drmModeGetConnector(fd, resources->connectors[i]);
1163 if (connector == NULL)
1164 continue;
1165
1166 if (connector->connection == DRM_MODE_CONNECTED &&
1167 connector->count_modes > 0)
1168 break;
1169
1170 drmModeFreeConnector(connector);
1171 }
1172
1173 if (i == resources->count_connectors) {
1174 fprintf(stderr, "No currently active connector found.\n");
1175 return -1;
1176 }
1177
1178 mode = &connector->modes[0];
1179
1180 for (i = 0; i < resources->count_encoders; i++) {
1181 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
1182
1183 if (encoder == NULL)
1184 continue;
1185
1186 if (encoder->encoder_id == connector->encoder_id)
1187 break;
1188
1189 drmModeFreeEncoder(encoder);
1190 }
1191
1192 /* Mode size at 32 bpp */
1193 create.size = mode->hdisplay * mode->vdisplay * 4;
1194 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
1195 fprintf(stderr, "gem create failed: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001196 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001197 }
1198
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001199 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001200 32, 32, mode->hdisplay * 4, create.handle, &output->fb_id);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001201 if (ret) {
1202 fprintf(stderr, "failed to add fb: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001203 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001204 }
1205
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001206 ret = drmModeSetCrtc(fd, encoder->crtc_id, output->fb_id, 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001207 &connector->connector_id, 1, mode);
1208 if (ret) {
1209 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001210 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001211 }
1212
1213 flink.handle = create.handle;
1214 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
1215 fprintf(stderr, "gem flink failed: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001216 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001217 }
1218
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001219 output->compositor = ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001220 output->crtc_id = encoder->crtc_id;
1221 output->connector_id = connector->connector_id;
1222 output->mode = mode;
Kristian Høgsberge68d24c2009-01-15 11:44:35 -05001223 output->x = 0;
1224 output->y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001225 output->width = mode->hdisplay;
1226 output->height = mode->vdisplay;
1227 output->stride = mode->hdisplay * 4;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001228
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001229 output->surface = eglCreateSurfaceForName(ec->display, ec->config,
1230 flink.name,
1231 output->width, output->height,
1232 output->stride,
1233 surface_attribs);
1234 if (output->surface == NULL) {
1235 fprintf(stderr, "failed to create surface\n");
1236 return -1;
1237 }
1238
1239 output->base.interface = &wl_output_interface;
1240 wl_display_add_object(ec->wl_display, &output->base);
1241 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
1242 wl_list_insert(ec->output_list.prev, &output->link);
1243
1244 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
1245 fprintf(stderr, "failed to make context current\n");
1246 return -1;
1247 }
1248
1249 output->background = background_create(output, option_background);
1250
1251 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001252}
1253
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001254static const struct wl_interface visual_interface = {
1255 "visual", 1,
1256};
1257
1258static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001259add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001260{
1261 ec->argb_visual.base.interface = &visual_interface;
1262 ec->argb_visual.base.implementation = NULL;
1263 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001264 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001265
1266 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1267 ec->premultiplied_argb_visual.base.implementation = NULL;
1268 wl_display_add_object(ec->wl_display,
1269 &ec->premultiplied_argb_visual.base);
1270 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001271 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001272
1273 ec->rgb_visual.base.interface = &visual_interface;
1274 ec->rgb_visual.base.implementation = NULL;
1275 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001276 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1277}
1278
Ray Strode19ad6a92008-12-19 01:45:41 -05001279static void on_enter_vt(int signal_number, void *data)
1280{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001281 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001282 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001283 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -05001284
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001285 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001286 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001287
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001288 fd = eglGetDisplayFD(ec->display);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001289 output = container_of(ec->output_list.next, struct wlsc_output, link);
1290 while (&output->link != &ec->output_list) {
1291 ret = drmModeSetCrtc(fd, output->crtc_id, output->fb_id, 0, 0,
1292 &output->connector_id, 1, output->mode);
1293 if (ret)
1294 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1295 output->connector_id);
1296
1297 output = container_of(output->link.next,
1298 struct wlsc_output, link);
Ray Strode19ad6a92008-12-19 01:45:41 -05001299 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001300}
1301
1302static void on_leave_vt(int signal_number, void *data)
1303{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001304 struct wlsc_compositor *ec = data;
Ray Strode19ad6a92008-12-19 01:45:41 -05001305
1306 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001307 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001308}
1309
Ray Strode966aa112008-12-19 14:28:02 -05001310static void
1311on_tty_input(int fd, uint32_t mask, void *data)
1312{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001313 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001314
1315 /* Ignore input to tty. We get keyboard events from evdev
1316 */
1317 tcflush(ec->tty_fd, TCIFLUSH);
1318}
1319
1320static void on_term_signal(int signal_number, void *data)
1321{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001322 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001323
1324 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1325 fprintf(stderr, "could not restore terminal to canonical mode\n");
1326
1327 exit(0);
1328}
1329
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001330static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001331{
1332 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001333 struct vt_mode mode = { 0 };
1334
1335 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1336 if (ec->tty_fd <= 0) {
1337 fprintf(stderr, "failed to open active tty: %m\n");
1338 return -1;
1339 }
Ray Strode966aa112008-12-19 14:28:02 -05001340
1341 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1342 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001343 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001344 }
1345
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001346 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001347 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001348 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001349
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001350 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001351 raw_attributes.c_oflag |= OPOST | OCRNL;
1352
1353 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1354 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1355
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001356 ec->term_signal_source =
1357 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001358
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001359 ec->tty_input_source =
1360 wl_event_loop_add_fd(loop, ec->tty_fd,
1361 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001362
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001363 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001364 mode.mode = VT_PROCESS;
1365 mode.relsig = SIGUSR1;
1366 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001367 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001368 fprintf(stderr, "failed to take control of vt handling\n");
1369 }
1370
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001371 ec->leave_vt_source =
1372 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1373 ec->enter_vt_source =
1374 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001375
1376 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001377}
1378
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001379static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001380init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001381{
1382 struct udev_enumerate *e;
1383 struct udev_list_entry *entry;
1384 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001385 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001386 struct wlsc_input_device *input_device;
1387
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001388 ec->udev = udev_new();
1389 if (ec->udev == NULL) {
1390 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001391 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001392 }
1393
1394 input_device = create_input_device(ec);
1395
1396 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001397 udev_enumerate_add_match_subsystem(e, "input");
1398 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001399 udev_enumerate_scan_devices(e);
1400 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1401 path = udev_list_entry_get_name(entry);
1402 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001403 evdev_input_device_create(input_device, ec->wl_display,
1404 udev_device_get_devnode(device));
1405 }
1406 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001407
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001408 e = udev_enumerate_new(ec->udev);
1409 udev_enumerate_add_match_subsystem(e, "drm");
1410 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1411 udev_enumerate_scan_devices(e);
1412 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1413 path = udev_list_entry_get_name(entry);
1414 device = udev_device_new_from_syspath(ec->udev, path);
1415 if (create_output(ec, device) < 0) {
1416 fprintf(stderr, "failed to create output for %s\n", path);
1417 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001418 }
1419 }
1420 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001421
1422 /* Create the pointer surface now that we have a current EGL context. */
1423 input_device->pointer_surface =
1424 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1425
1426 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001427}
1428
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001429static struct wlsc_compositor *
1430wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001431{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001432 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001433 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001434 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001435
1436 ec = malloc(sizeof *ec);
1437 if (ec == NULL)
1438 return NULL;
1439
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001440 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001441 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001442
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001443 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001444
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001445 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001446
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001447 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001448 wl_list_init(&ec->input_device_list);
1449 wl_list_init(&ec->output_list);
1450 if (init_libudev(ec) < 0) {
1451 fprintf(stderr, "failed to initialize devices\n");
1452 return NULL;
1453 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001454
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001455 shooter = screenshooter_create(ec);
1456 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001457 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001458
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001459 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001460
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001461 setup_tty(ec, loop);
1462
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001463 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001464 schedule_repaint(ec);
1465
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001466 wl_list_init(&ec->animate_list);
1467
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001468 return ec;
1469}
1470
1471/* The plan here is to generate a random anonymous socket name and
1472 * advertise that through a service on the session dbus.
1473 */
1474static const char socket_name[] = "\0wayland";
1475
1476int main(int argc, char *argv[])
1477{
1478 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001479 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001480 GError *error = NULL;
1481 GOptionContext *context;
1482
1483 context = g_option_context_new(NULL);
1484 g_option_context_add_main_entries(context, option_entries, "Wayland");
1485 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1486 fprintf(stderr, "option parsing failed: %s\n", error->message);
1487 exit(EXIT_FAILURE);
1488 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001489
1490 display = wl_display_create();
1491
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001492 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001493 if (ec == NULL) {
1494 fprintf(stderr, "failed to create compositor\n");
1495 exit(EXIT_FAILURE);
1496 }
1497
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001498 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001499 fprintf(stderr, "failed to add socket: %m\n");
1500 exit(EXIT_FAILURE);
1501 }
1502
1503 wl_display_run(display);
1504
1505 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001506}