blob: 2aaa774ab16535e9e0478681bbd84cdcf297e221 [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
74 struct drm_mode_modeinfo *mode;
75 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øgsberg8e438622009-01-26 23:07:00 -0500676 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400677
678 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400679}
680
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500681static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500682surface_attach(struct wl_client *client,
683 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500684 uint32_t width, uint32_t height, uint32_t stride,
685 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400686{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500687 struct wlsc_surface *es = (struct wlsc_surface *) surface;
688 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400689
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500690 if (es->surface != EGL_NO_SURFACE)
691 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400692
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500693 es->width = width;
694 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500695 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500696 name, width, height, stride, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500697 if (visual == &ec->argb_visual.base)
698 es->visual = &ec->argb_visual;
699 else if (visual == &ec->premultiplied_argb_visual.base)
700 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500701 else if (visual == &ec->rgb_visual.base)
702 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500703 else
704 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400705
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500706 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400707 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500708 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
709 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
710 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500711 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400712}
713
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500714static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500715surface_map(struct wl_client *client,
716 struct wl_surface *surface,
717 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400718{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500719 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400720
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500721 es->map.x = x;
722 es->map.y = y;
723 es->map.width = width;
724 es->map.height = height;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500725 wlsc_surface_update_matrix(es);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400726}
727
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500728static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500729surface_copy(struct wl_client *client,
730 struct wl_surface *surface,
731 int32_t dst_x, int32_t dst_y,
732 uint32_t name, uint32_t stride,
733 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500734{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500735 struct wlsc_surface *es = (struct wlsc_surface *) surface;
736 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500737 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500738
739 /* FIXME: glCopyPixels should work, but then we'll have to
740 * call eglMakeCurrent to set up the src and dest surfaces
741 * first. This seems cheaper, but maybe there's a better way
742 * to accomplish this. */
743
744 src = eglCreateSurfaceForName(ec->display, ec->config,
745 name, x + width, y + height, stride, NULL);
746
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500747 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500748 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500749 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500750}
751
752static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500753surface_damage(struct wl_client *client,
754 struct wl_surface *surface,
755 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500756{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500757 /* FIXME: This need to take a damage region, of course. */
758}
759
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500760const static struct wl_surface_interface surface_interface = {
761 surface_destroy,
762 surface_attach,
763 surface_map,
764 surface_copy,
765 surface_damage
766};
767
768static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500769animate_surface(struct wlsc_animate *animate,
770 struct wlsc_compositor *compositor,
771 uint32_t frame, uint32_t msecs)
772{
773 struct wlsc_surface *s;
774 double angle_force, angle;
775 struct wlsc_vector force, tmp;
776 double step = 0.3;
777 double friction = 1;
778 double spring = 0.2;
779
780 s = container_of(animate, struct wlsc_surface, animate);
781
782 angle = s->current_angle;
783 angle_force = (s->target_angle - angle) * spring +
784 (s->previous_angle - angle) * friction;
785
786 s->current_angle = angle + (angle - s->previous_angle) + angle_force * step;
787 s->previous_angle = angle;
788
789 force = s->target;
790 wlsc_vector_subtract(&force, &s->current);
791 wlsc_vector_scalar(&force, spring);
792 tmp = s->previous;
793 wlsc_vector_subtract(&tmp, &s->current);
794 wlsc_vector_scalar(&tmp, friction);
795 wlsc_vector_add(&force, &tmp);
796
797 wlsc_vector_scalar(&force, step);
798 wlsc_vector_add(&force, &s->current);
799 wlsc_vector_subtract(&force, &s->previous);
800 s->previous = s->current;
801 wlsc_vector_add(&s->current, &force);
802
803 wlsc_surface_update_matrix(s);
804
805 tmp = s->current;
806 wlsc_vector_subtract(&tmp, &s->target);
807 if (tmp.x * tmp.x + tmp.y * tmp.y + tmp.z * tmp.z > 0.001)
808 schedule_repaint(compositor);
809 else
810 wl_list_remove(&s->animate.link);
811}
812
813static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500814compositor_create_surface(struct wl_client *client,
815 struct wl_compositor *compositor, uint32_t id)
816{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500817 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
818 struct wlsc_surface *es;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500819
820 es = malloc(sizeof *es);
821 if (es == NULL)
822 /* FIXME: Send OOM event. */
823 return;
824
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500825 wlsc_surface_init(es, ec, NULL, 0, 0, 0, 0);
826 es->animate.animate = animate_surface;
827 wl_list_init(&es->animate.link);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500828
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500829 wl_list_insert(ec->surface_list.prev, &es->link);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500830 wl_client_add_surface(client, &es->base,
831 &surface_interface, id);
832}
833
834static void
835compositor_commit(struct wl_client *client,
836 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500837{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500838 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500839
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500840 schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500841 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500842}
843
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500844const static struct wl_compositor_interface compositor_interface = {
845 compositor_create_surface,
846 compositor_commit
847};
848
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500849static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500850pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500851{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500852 struct wlsc_compositor *ec = device->ec;
853 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500854
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500855 if (device->grab > 0)
856 return device->grab_surface;
857
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500858 es = container_of(ec->surface_list.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500859 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500860 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500861 if (es->map.x <= device->x &&
862 device->x < es->map.x + es->map.width &&
863 es->map.y <= device->y &&
864 device->y < es->map.y + es->map.height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500865 return es;
866
867 es = container_of(es->link.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500868 struct wlsc_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500869
870 /* Transform to surface coordinates. */
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500871 *sx = (device->x - es->map.x) * es->width / es->map.width;
872 *sy = (device->y - es->map.y) * es->height / es->map.height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500873 }
874
875 return NULL;
876}
877
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500878void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500879notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500880{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500881 struct wlsc_surface *es;
882 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500883 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500884 const int hotspot_x = 16, hotspot_y = 16;
885 int32_t sx, sy;
886
Ray Strodee96dcb82008-12-20 02:00:49 -0500887 if (!ec->vt_active)
888 return;
889
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500890 /* FIXME: We need some multi head love here. */
891 output = container_of(ec->output_list.next, struct wlsc_output, link);
892 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500893 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500894 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500895 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500896 if (x >= output->x + output->width)
897 x = output->x + output->width - 1;
898 if (y >= output->y + output->height)
899 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500900
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500901 device->x = x;
902 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500903 es = pick_surface(device, &sx, &sy);
904 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500905 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500906 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500907
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500908 device->pointer_surface->map.x = x - hotspot_x;
909 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500910
911 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500912}
913
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500914void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500915notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500916 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500917{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500918 struct wlsc_surface *es;
919 struct wlsc_compositor *ec = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500920 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500921
Ray Strodee96dcb82008-12-20 02:00:49 -0500922 if (!ec->vt_active)
923 return;
924
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500925 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500926 if (es) {
927 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500928 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500929
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500930 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500931 /* FIXME: We need callbacks when the surfaces
932 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500933 device->grab++;
934 device->grab_surface = es;
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500935 device->focus_surface = es;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500936 } else {
937 device->grab--;
938 }
939
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500940 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500941 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500942 WL_INPUT_BUTTON, button, state,
943 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500944
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500945 schedule_repaint(device->ec);
946 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500947}
948
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500949static void on_term_signal(int signal_number, void *data);
950
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500951static void
952update_surface_targets(struct wlsc_compositor *compositor, int primary)
953{
954 struct wlsc_surface *s;
955 int i;
956
957 i = 0;
958 s = container_of(compositor->surface_list.next,
959 struct wlsc_surface, link);
960 while (&s->link != &compositor->surface_list) {
961 if (i < primary) {
962 s->target.x = -400 + 500 * (i - primary);
963 s->target.y = 0;
964 s->target.z = -1500;
965 s->target_angle = M_PI / 4;
966 } else if (i == primary) {
967 s->target.x = 0;
968 s->target.y = 0;
969 s->target.z = -1000;
970 s->target_angle = 0;
971 compositor->primary = s;
972 } else {
973 s->target.x = 400 + 500 * (i - primary);
974 s->target.y = 0;
975 s->target.z = -1500;
976 s->target_angle = -M_PI / 4;
977 }
978 wl_list_remove(&s->animate.link);
979 wl_list_insert(compositor->animate_list.prev, &s->animate.link);
980 s = container_of(s->link.next,
981 struct wlsc_surface, link);
982 i++;
983 }
984
985 schedule_repaint(compositor);
986}
987
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500988void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500989notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500990 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500991{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500992 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500993 struct wlsc_surface *s;
Ray Strodee96dcb82008-12-20 02:00:49 -0500994
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500995 switch (key | ec->meta_state) {
996 case KEY_EJECTCD | META_DOWN:
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500997 on_term_signal(SIGTERM, ec);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500998 return;
999
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001000 case KEY_1 | META_DOWN:
1001 case KEY_2 | META_DOWN:
1002 case KEY_3 | META_DOWN:
1003 case KEY_4 | META_DOWN:
1004 case KEY_5 | META_DOWN:
1005 update_surface_targets(ec, key - KEY_1);
1006 if (device->grab == 0 && s != NULL)
1007 device->focus_surface = ec->primary;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001008 return;
1009
1010 case KEY_LEFTMETA:
1011 case KEY_RIGHTMETA:
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001012 case KEY_LEFTMETA | META_DOWN:
1013 case KEY_RIGHTMETA | META_DOWN:
1014 ec->meta_state = state ? META_DOWN : 0;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001015 if (state == 0) {
1016 ec->primary->target.z = 0;
1017 wl_list_remove(&ec->primary->animate.link);
1018 wl_list_insert(&ec->animate_list,
1019 &ec->primary->animate.link);
1020 schedule_repaint(ec);
1021 }
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001022 return;
1023 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001024
Ray Strodee96dcb82008-12-20 02:00:49 -05001025 if (!ec->vt_active)
1026 return;
1027
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -05001028 if (device->focus_surface != NULL)
1029 wl_surface_post_event(&device->focus_surface->base,
1030 &device->base,
1031 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001032}
1033
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001034struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001035evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001036 struct wl_display *display, const char *path);
1037
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001038static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001039create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001040{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001041 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001042
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001043 device = malloc(sizeof *device);
1044 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001045 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001046
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001047 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001048 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001049 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001050 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001051 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001052 device->x = 100;
1053 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001054 device->ec = ec;
1055
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001056 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001057
1058 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001059}
1060
1061void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001062wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001063{
1064 *x = device->x;
1065 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001066}
1067
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001068static void
1069post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001070{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001071 struct wlsc_output *output =
1072 container_of(global, struct wlsc_output, base);
1073
1074 wl_client_post_event(client, global,
1075 WL_OUTPUT_GEOMETRY,
1076 output->width, output->height);
1077}
1078
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001079static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001080init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001081{
1082 static const EGLint config_attribs[] = {
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001083 EGL_DEPTH_SIZE, 0,
1084 EGL_STENCIL_SIZE, 0,
1085 EGL_CONFIG_CAVEAT, EGL_NONE,
1086 EGL_RED_SIZE, 8,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001087 EGL_NONE
1088 };
1089
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001090 EGLint major, minor;
1091
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -05001092 ec->display = eglCreateDisplayNative(device);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001093 if (ec->display == NULL) {
1094 fprintf(stderr, "failed to create display\n");
1095 return -1;
1096 }
1097
1098 if (!eglInitialize(ec->display, &major, &minor)) {
1099 fprintf(stderr, "failed to initialize display\n");
1100 return -1;
1101 }
1102
1103 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
1104 return -1;
1105
1106 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
1107 if (ec->context == NULL) {
1108 fprintf(stderr, "failed to create context\n");
1109 return -1;
1110 }
1111 return 0;
1112}
1113
1114static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001115create_output(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001116{
1117 const static EGLint surface_attribs[] = {
1118 EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
1119 EGL_NONE
1120 };
1121
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001122 drmModeConnector *connector;
1123 drmModeRes *resources;
1124 drmModeEncoder *encoder;
1125 struct drm_mode_modeinfo *mode;
1126 struct drm_i915_gem_create create;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001127 struct drm_gem_flink flink;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001128 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001129 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001130
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001131 if (ec->display == NULL && init_egl(ec, device) < 0) {
1132 fprintf(stderr, "failed to initialize egl\n");
1133 return -1;
1134 }
1135
1136 output = malloc(sizeof *output);
1137 if (output == NULL)
1138 return -1;
1139
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001140 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001141 resources = drmModeGetResources(fd);
1142 if (!resources) {
1143 fprintf(stderr, "drmModeGetResources failed\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001144 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001145 }
1146
1147 for (i = 0; i < resources->count_connectors; i++) {
1148 connector = drmModeGetConnector(fd, resources->connectors[i]);
1149 if (connector == NULL)
1150 continue;
1151
1152 if (connector->connection == DRM_MODE_CONNECTED &&
1153 connector->count_modes > 0)
1154 break;
1155
1156 drmModeFreeConnector(connector);
1157 }
1158
1159 if (i == resources->count_connectors) {
1160 fprintf(stderr, "No currently active connector found.\n");
1161 return -1;
1162 }
1163
1164 mode = &connector->modes[0];
1165
1166 for (i = 0; i < resources->count_encoders; i++) {
1167 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
1168
1169 if (encoder == NULL)
1170 continue;
1171
1172 if (encoder->encoder_id == connector->encoder_id)
1173 break;
1174
1175 drmModeFreeEncoder(encoder);
1176 }
1177
1178 /* Mode size at 32 bpp */
1179 create.size = mode->hdisplay * mode->vdisplay * 4;
1180 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
1181 fprintf(stderr, "gem create failed: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001182 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001183 }
1184
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001185 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001186 32, 32, mode->hdisplay * 4, create.handle, &output->fb_id);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001187 if (ret) {
1188 fprintf(stderr, "failed to add fb: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001189 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001190 }
1191
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001192 ret = drmModeSetCrtc(fd, encoder->crtc_id, output->fb_id, 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001193 &connector->connector_id, 1, mode);
1194 if (ret) {
1195 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001196 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001197 }
1198
1199 flink.handle = create.handle;
1200 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
1201 fprintf(stderr, "gem flink failed: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001202 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001203 }
1204
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001205 output->compositor = ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001206 output->crtc_id = encoder->crtc_id;
1207 output->connector_id = connector->connector_id;
1208 output->mode = mode;
Kristian Høgsberge68d24c2009-01-15 11:44:35 -05001209 output->x = 0;
1210 output->y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001211 output->width = mode->hdisplay;
1212 output->height = mode->vdisplay;
1213 output->stride = mode->hdisplay * 4;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001214
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001215 output->surface = eglCreateSurfaceForName(ec->display, ec->config,
1216 flink.name,
1217 output->width, output->height,
1218 output->stride,
1219 surface_attribs);
1220 if (output->surface == NULL) {
1221 fprintf(stderr, "failed to create surface\n");
1222 return -1;
1223 }
1224
1225 output->base.interface = &wl_output_interface;
1226 wl_display_add_object(ec->wl_display, &output->base);
1227 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
1228 wl_list_insert(ec->output_list.prev, &output->link);
1229
1230 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
1231 fprintf(stderr, "failed to make context current\n");
1232 return -1;
1233 }
1234
1235 output->background = background_create(output, option_background);
1236
1237 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001238}
1239
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001240static const struct wl_interface visual_interface = {
1241 "visual", 1,
1242};
1243
1244static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001245add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001246{
1247 ec->argb_visual.base.interface = &visual_interface;
1248 ec->argb_visual.base.implementation = NULL;
1249 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001250 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001251
1252 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1253 ec->premultiplied_argb_visual.base.implementation = NULL;
1254 wl_display_add_object(ec->wl_display,
1255 &ec->premultiplied_argb_visual.base);
1256 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001257 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001258
1259 ec->rgb_visual.base.interface = &visual_interface;
1260 ec->rgb_visual.base.implementation = NULL;
1261 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001262 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1263}
1264
Ray Strode19ad6a92008-12-19 01:45:41 -05001265static void on_enter_vt(int signal_number, void *data)
1266{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001267 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001268 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001269 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -05001270
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001271 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001272 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001273
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001274 fd = eglGetDisplayFD(ec->display);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001275 output = container_of(ec->output_list.next, struct wlsc_output, link);
1276 while (&output->link != &ec->output_list) {
1277 ret = drmModeSetCrtc(fd, output->crtc_id, output->fb_id, 0, 0,
1278 &output->connector_id, 1, output->mode);
1279 if (ret)
1280 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1281 output->connector_id);
1282
1283 output = container_of(output->link.next,
1284 struct wlsc_output, link);
Ray Strode19ad6a92008-12-19 01:45:41 -05001285 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001286}
1287
1288static void on_leave_vt(int signal_number, void *data)
1289{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001290 struct wlsc_compositor *ec = data;
Ray Strode19ad6a92008-12-19 01:45:41 -05001291
1292 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001293 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001294}
1295
Ray Strode966aa112008-12-19 14:28:02 -05001296static void
1297on_tty_input(int fd, uint32_t mask, void *data)
1298{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001299 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001300
1301 /* Ignore input to tty. We get keyboard events from evdev
1302 */
1303 tcflush(ec->tty_fd, TCIFLUSH);
1304}
1305
1306static void on_term_signal(int signal_number, void *data)
1307{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001308 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001309
1310 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1311 fprintf(stderr, "could not restore terminal to canonical mode\n");
1312
1313 exit(0);
1314}
1315
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001316static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001317{
1318 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001319 struct vt_mode mode = { 0 };
1320
1321 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1322 if (ec->tty_fd <= 0) {
1323 fprintf(stderr, "failed to open active tty: %m\n");
1324 return -1;
1325 }
Ray Strode966aa112008-12-19 14:28:02 -05001326
1327 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1328 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001329 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001330 }
1331
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001332 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001333 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001334 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001335
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001336 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001337 raw_attributes.c_oflag |= OPOST | OCRNL;
1338
1339 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1340 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1341
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001342 ec->term_signal_source =
1343 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001344
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001345 ec->tty_input_source =
1346 wl_event_loop_add_fd(loop, ec->tty_fd,
1347 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001348
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001349 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001350 mode.mode = VT_PROCESS;
1351 mode.relsig = SIGUSR1;
1352 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001353 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001354 fprintf(stderr, "failed to take control of vt handling\n");
1355 }
1356
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001357 ec->leave_vt_source =
1358 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1359 ec->enter_vt_source =
1360 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001361
1362 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001363}
1364
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001365static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001366init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001367{
1368 struct udev_enumerate *e;
1369 struct udev_list_entry *entry;
1370 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001371 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001372 struct wlsc_input_device *input_device;
1373
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001374 ec->udev = udev_new();
1375 if (ec->udev == NULL) {
1376 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001377 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001378 }
1379
1380 input_device = create_input_device(ec);
1381
1382 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001383 udev_enumerate_add_match_subsystem(e, "input");
1384 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001385 udev_enumerate_scan_devices(e);
1386 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1387 path = udev_list_entry_get_name(entry);
1388 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001389 evdev_input_device_create(input_device, ec->wl_display,
1390 udev_device_get_devnode(device));
1391 }
1392 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001393
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001394 e = udev_enumerate_new(ec->udev);
1395 udev_enumerate_add_match_subsystem(e, "drm");
1396 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1397 udev_enumerate_scan_devices(e);
1398 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1399 path = udev_list_entry_get_name(entry);
1400 device = udev_device_new_from_syspath(ec->udev, path);
1401 if (create_output(ec, device) < 0) {
1402 fprintf(stderr, "failed to create output for %s\n", path);
1403 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001404 }
1405 }
1406 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001407
1408 /* Create the pointer surface now that we have a current EGL context. */
1409 input_device->pointer_surface =
1410 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1411
1412 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001413}
1414
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001415static struct wlsc_compositor *
1416wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001417{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001418 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001419 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001420 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001421
1422 ec = malloc(sizeof *ec);
1423 if (ec == NULL)
1424 return NULL;
1425
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001426 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001427 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001428
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001429 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001430
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001431 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001432
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001433 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001434 wl_list_init(&ec->input_device_list);
1435 wl_list_init(&ec->output_list);
1436 if (init_libudev(ec) < 0) {
1437 fprintf(stderr, "failed to initialize devices\n");
1438 return NULL;
1439 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001440
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001441 shooter = screenshooter_create(ec);
1442 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001443 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001444
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001445 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001446
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001447 setup_tty(ec, loop);
1448
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001449 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001450 schedule_repaint(ec);
1451
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001452 wl_list_init(&ec->animate_list);
1453
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001454 return ec;
1455}
1456
1457/* The plan here is to generate a random anonymous socket name and
1458 * advertise that through a service on the session dbus.
1459 */
1460static const char socket_name[] = "\0wayland";
1461
1462int main(int argc, char *argv[])
1463{
1464 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001465 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001466 GError *error = NULL;
1467 GOptionContext *context;
1468
1469 context = g_option_context_new(NULL);
1470 g_option_context_add_main_entries(context, option_entries, "Wayland");
1471 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1472 fprintf(stderr, "option parsing failed: %s\n", error->message);
1473 exit(EXIT_FAILURE);
1474 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001475
1476 display = wl_display_create();
1477
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001478 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001479 if (ec == NULL) {
1480 fprintf(stderr, "failed to create compositor\n");
1481 exit(EXIT_FAILURE);
1482 }
1483
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001484 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001485 fprintf(stderr, "failed to add socket: %m\n");
1486 exit(EXIT_FAILURE);
1487 }
1488
1489 wl_display_run(display);
1490
1491 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001492}