blob: 9ba4ea3a40f49a60642aeb40e07755a43f5b54a2 [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ø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øgsberg8e438622009-01-26 23:07:00 -0500852static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500853pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500854{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500855 struct wlsc_compositor *ec = device->ec;
856 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500857
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500858 if (device->grab > 0)
859 return device->grab_surface;
860
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500861 es = container_of(ec->surface_list.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500862 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500863 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500864 if (es->map.x <= device->x &&
865 device->x < es->map.x + es->map.width &&
866 es->map.y <= device->y &&
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500867 device->y < es->map.y + es->map.height) {
868 /* Transform to surface coordinates. */
869 *sx = (device->x - es->map.x) * es->width / es->map.width;
870 *sy = (device->y - es->map.y) * es->height / es->map.height;
871
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500872 return es;
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500873 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500874
875 es = container_of(es->link.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500876 struct wlsc_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500877
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500878 }
879
880 return NULL;
881}
882
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500883void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500884notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500885{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500886 struct wlsc_surface *es;
887 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500888 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500889 const int hotspot_x = 16, hotspot_y = 16;
890 int32_t sx, sy;
891
Ray Strodee96dcb82008-12-20 02:00:49 -0500892 if (!ec->vt_active)
893 return;
894
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500895 /* FIXME: We need some multi head love here. */
896 output = container_of(ec->output_list.next, struct wlsc_output, link);
897 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500898 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500899 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500900 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500901 if (x >= output->x + output->width)
902 x = output->x + output->width - 1;
903 if (y >= output->y + output->height)
904 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500905
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500906 device->x = x;
907 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500908 es = pick_surface(device, &sx, &sy);
909 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500910 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500911 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500912
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500913 device->pointer_surface->map.x = x - hotspot_x;
914 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500915
916 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500917}
918
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500919void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500920notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500921 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500922{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500923 struct wlsc_surface *es;
924 struct wlsc_compositor *ec = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500925 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500926
Ray Strodee96dcb82008-12-20 02:00:49 -0500927 if (!ec->vt_active)
928 return;
929
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500930 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500931 if (es) {
932 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500933 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500934
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500935 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500936 /* FIXME: We need callbacks when the surfaces
937 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500938 device->grab++;
939 device->grab_surface = es;
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500940 device->focus_surface = es;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500941 } else {
942 device->grab--;
943 }
944
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500945 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500946 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500947 WL_INPUT_BUTTON, button, state,
948 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500949
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500950 schedule_repaint(device->ec);
951 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500952}
953
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500954static void on_term_signal(int signal_number, void *data);
955
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500956static void
957update_surface_targets(struct wlsc_compositor *compositor, int primary)
958{
959 struct wlsc_surface *s;
960 int i;
961
962 i = 0;
963 s = container_of(compositor->surface_list.next,
964 struct wlsc_surface, link);
965 while (&s->link != &compositor->surface_list) {
966 if (i < primary) {
967 s->target.x = -400 + 500 * (i - primary);
968 s->target.y = 0;
969 s->target.z = -1500;
970 s->target_angle = M_PI / 4;
971 } else if (i == primary) {
972 s->target.x = 0;
973 s->target.y = 0;
974 s->target.z = -1000;
975 s->target_angle = 0;
976 compositor->primary = s;
977 } else {
978 s->target.x = 400 + 500 * (i - primary);
979 s->target.y = 0;
980 s->target.z = -1500;
981 s->target_angle = -M_PI / 4;
982 }
983 wl_list_remove(&s->animate.link);
984 wl_list_insert(compositor->animate_list.prev, &s->animate.link);
985 s = container_of(s->link.next,
986 struct wlsc_surface, link);
987 i++;
988 }
989
990 schedule_repaint(compositor);
991}
992
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500993void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500994notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500995 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500996{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500997 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500998 struct wlsc_surface *s;
Ray Strodee96dcb82008-12-20 02:00:49 -0500999
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001000 switch (key | ec->meta_state) {
1001 case KEY_EJECTCD | META_DOWN:
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001002 on_term_signal(SIGTERM, ec);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001003 return;
1004
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001005 case KEY_1 | META_DOWN:
1006 case KEY_2 | META_DOWN:
1007 case KEY_3 | META_DOWN:
1008 case KEY_4 | META_DOWN:
1009 case KEY_5 | META_DOWN:
1010 update_surface_targets(ec, key - KEY_1);
1011 if (device->grab == 0 && s != NULL)
1012 device->focus_surface = ec->primary;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001013 return;
1014
1015 case KEY_LEFTMETA:
1016 case KEY_RIGHTMETA:
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001017 case KEY_LEFTMETA | META_DOWN:
1018 case KEY_RIGHTMETA | META_DOWN:
1019 ec->meta_state = state ? META_DOWN : 0;
Kristian Høgsberg1febe012009-02-10 18:30:14 -05001020 if (state == 0 && ec->primary != NULL) {
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001021 ec->primary->target.z = 0;
1022 wl_list_remove(&ec->primary->animate.link);
1023 wl_list_insert(&ec->animate_list,
1024 &ec->primary->animate.link);
1025 schedule_repaint(ec);
1026 }
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001027 return;
1028 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001029
Ray Strodee96dcb82008-12-20 02:00:49 -05001030 if (!ec->vt_active)
1031 return;
1032
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -05001033 if (device->focus_surface != NULL)
1034 wl_surface_post_event(&device->focus_surface->base,
1035 &device->base,
1036 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001037}
1038
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001039struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001040evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001041 struct wl_display *display, const char *path);
1042
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001043static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001044create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001045{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001046 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001047
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001048 device = malloc(sizeof *device);
1049 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001050 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001051
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001052 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001053 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001054 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001055 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001056 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001057 device->x = 100;
1058 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001059 device->ec = ec;
1060
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001061 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001062
1063 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001064}
1065
1066void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001067wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001068{
1069 *x = device->x;
1070 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001071}
1072
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001073static void
1074post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001075{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001076 struct wlsc_output *output =
1077 container_of(global, struct wlsc_output, base);
1078
1079 wl_client_post_event(client, global,
1080 WL_OUTPUT_GEOMETRY,
1081 output->width, output->height);
1082}
1083
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001084static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001085init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001086{
1087 static const EGLint config_attribs[] = {
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001088 EGL_DEPTH_SIZE, 0,
1089 EGL_STENCIL_SIZE, 0,
1090 EGL_CONFIG_CAVEAT, EGL_NONE,
1091 EGL_RED_SIZE, 8,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001092 EGL_NONE
1093 };
1094
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001095 EGLint major, minor;
1096
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -05001097 ec->display = eglCreateDisplayNative(device);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001098 if (ec->display == NULL) {
1099 fprintf(stderr, "failed to create display\n");
1100 return -1;
1101 }
1102
1103 if (!eglInitialize(ec->display, &major, &minor)) {
1104 fprintf(stderr, "failed to initialize display\n");
1105 return -1;
1106 }
1107
1108 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
1109 return -1;
1110
1111 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
1112 if (ec->context == NULL) {
1113 fprintf(stderr, "failed to create context\n");
1114 return -1;
1115 }
1116 return 0;
1117}
1118
1119static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001120create_output(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001121{
1122 const static EGLint surface_attribs[] = {
1123 EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
1124 EGL_NONE
1125 };
1126
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001127 drmModeConnector *connector;
1128 drmModeRes *resources;
1129 drmModeEncoder *encoder;
1130 struct drm_mode_modeinfo *mode;
1131 struct drm_i915_gem_create create;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001132 struct drm_gem_flink flink;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001133 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001134 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001135
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001136 if (ec->display == NULL && init_egl(ec, device) < 0) {
1137 fprintf(stderr, "failed to initialize egl\n");
1138 return -1;
1139 }
1140
1141 output = malloc(sizeof *output);
1142 if (output == NULL)
1143 return -1;
1144
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001145 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001146 resources = drmModeGetResources(fd);
1147 if (!resources) {
1148 fprintf(stderr, "drmModeGetResources failed\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001149 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001150 }
1151
1152 for (i = 0; i < resources->count_connectors; i++) {
1153 connector = drmModeGetConnector(fd, resources->connectors[i]);
1154 if (connector == NULL)
1155 continue;
1156
1157 if (connector->connection == DRM_MODE_CONNECTED &&
1158 connector->count_modes > 0)
1159 break;
1160
1161 drmModeFreeConnector(connector);
1162 }
1163
1164 if (i == resources->count_connectors) {
1165 fprintf(stderr, "No currently active connector found.\n");
1166 return -1;
1167 }
1168
1169 mode = &connector->modes[0];
1170
1171 for (i = 0; i < resources->count_encoders; i++) {
1172 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
1173
1174 if (encoder == NULL)
1175 continue;
1176
1177 if (encoder->encoder_id == connector->encoder_id)
1178 break;
1179
1180 drmModeFreeEncoder(encoder);
1181 }
1182
1183 /* Mode size at 32 bpp */
1184 create.size = mode->hdisplay * mode->vdisplay * 4;
1185 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
1186 fprintf(stderr, "gem create failed: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001187 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001188 }
1189
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001190 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001191 32, 32, mode->hdisplay * 4, create.handle, &output->fb_id);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001192 if (ret) {
1193 fprintf(stderr, "failed to add fb: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001194 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001195 }
1196
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001197 ret = drmModeSetCrtc(fd, encoder->crtc_id, output->fb_id, 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001198 &connector->connector_id, 1, mode);
1199 if (ret) {
1200 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001201 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001202 }
1203
1204 flink.handle = create.handle;
1205 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
1206 fprintf(stderr, "gem flink failed: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001207 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001208 }
1209
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001210 output->compositor = ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001211 output->crtc_id = encoder->crtc_id;
1212 output->connector_id = connector->connector_id;
1213 output->mode = mode;
Kristian Høgsberge68d24c2009-01-15 11:44:35 -05001214 output->x = 0;
1215 output->y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001216 output->width = mode->hdisplay;
1217 output->height = mode->vdisplay;
1218 output->stride = mode->hdisplay * 4;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001219
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001220 output->surface = eglCreateSurfaceForName(ec->display, ec->config,
1221 flink.name,
1222 output->width, output->height,
1223 output->stride,
1224 surface_attribs);
1225 if (output->surface == NULL) {
1226 fprintf(stderr, "failed to create surface\n");
1227 return -1;
1228 }
1229
1230 output->base.interface = &wl_output_interface;
1231 wl_display_add_object(ec->wl_display, &output->base);
1232 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
1233 wl_list_insert(ec->output_list.prev, &output->link);
1234
1235 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
1236 fprintf(stderr, "failed to make context current\n");
1237 return -1;
1238 }
1239
1240 output->background = background_create(output, option_background);
1241
1242 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001243}
1244
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001245static const struct wl_interface visual_interface = {
1246 "visual", 1,
1247};
1248
1249static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001250add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001251{
1252 ec->argb_visual.base.interface = &visual_interface;
1253 ec->argb_visual.base.implementation = NULL;
1254 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001255 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001256
1257 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1258 ec->premultiplied_argb_visual.base.implementation = NULL;
1259 wl_display_add_object(ec->wl_display,
1260 &ec->premultiplied_argb_visual.base);
1261 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001262 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001263
1264 ec->rgb_visual.base.interface = &visual_interface;
1265 ec->rgb_visual.base.implementation = NULL;
1266 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001267 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1268}
1269
Ray Strode19ad6a92008-12-19 01:45:41 -05001270static void on_enter_vt(int signal_number, void *data)
1271{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001272 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001273 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001274 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -05001275
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001276 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001277 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001278
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001279 fd = eglGetDisplayFD(ec->display);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001280 output = container_of(ec->output_list.next, struct wlsc_output, link);
1281 while (&output->link != &ec->output_list) {
1282 ret = drmModeSetCrtc(fd, output->crtc_id, output->fb_id, 0, 0,
1283 &output->connector_id, 1, output->mode);
1284 if (ret)
1285 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1286 output->connector_id);
1287
1288 output = container_of(output->link.next,
1289 struct wlsc_output, link);
Ray Strode19ad6a92008-12-19 01:45:41 -05001290 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001291}
1292
1293static void on_leave_vt(int signal_number, void *data)
1294{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001295 struct wlsc_compositor *ec = data;
Ray Strode19ad6a92008-12-19 01:45:41 -05001296
1297 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001298 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001299}
1300
Ray Strode966aa112008-12-19 14:28:02 -05001301static void
1302on_tty_input(int fd, uint32_t mask, void *data)
1303{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001304 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001305
1306 /* Ignore input to tty. We get keyboard events from evdev
1307 */
1308 tcflush(ec->tty_fd, TCIFLUSH);
1309}
1310
1311static void on_term_signal(int signal_number, 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 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1316 fprintf(stderr, "could not restore terminal to canonical mode\n");
1317
1318 exit(0);
1319}
1320
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001321static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001322{
1323 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001324 struct vt_mode mode = { 0 };
1325
1326 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1327 if (ec->tty_fd <= 0) {
1328 fprintf(stderr, "failed to open active tty: %m\n");
1329 return -1;
1330 }
Ray Strode966aa112008-12-19 14:28:02 -05001331
1332 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1333 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001334 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001335 }
1336
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001337 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001338 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001339 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001340
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001341 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001342 raw_attributes.c_oflag |= OPOST | OCRNL;
1343
1344 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1345 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1346
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001347 ec->term_signal_source =
1348 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001349
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001350 ec->tty_input_source =
1351 wl_event_loop_add_fd(loop, ec->tty_fd,
1352 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001353
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001354 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001355 mode.mode = VT_PROCESS;
1356 mode.relsig = SIGUSR1;
1357 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001358 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001359 fprintf(stderr, "failed to take control of vt handling\n");
1360 }
1361
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001362 ec->leave_vt_source =
1363 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1364 ec->enter_vt_source =
1365 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001366
1367 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001368}
1369
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001370static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001371init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001372{
1373 struct udev_enumerate *e;
1374 struct udev_list_entry *entry;
1375 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001376 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001377 struct wlsc_input_device *input_device;
1378
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001379 ec->udev = udev_new();
1380 if (ec->udev == NULL) {
1381 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001382 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001383 }
1384
1385 input_device = create_input_device(ec);
1386
1387 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001388 udev_enumerate_add_match_subsystem(e, "input");
1389 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001390 udev_enumerate_scan_devices(e);
1391 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1392 path = udev_list_entry_get_name(entry);
1393 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001394 evdev_input_device_create(input_device, ec->wl_display,
1395 udev_device_get_devnode(device));
1396 }
1397 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001398
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001399 e = udev_enumerate_new(ec->udev);
1400 udev_enumerate_add_match_subsystem(e, "drm");
1401 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1402 udev_enumerate_scan_devices(e);
1403 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1404 path = udev_list_entry_get_name(entry);
1405 device = udev_device_new_from_syspath(ec->udev, path);
1406 if (create_output(ec, device) < 0) {
1407 fprintf(stderr, "failed to create output for %s\n", path);
1408 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001409 }
1410 }
1411 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001412
1413 /* Create the pointer surface now that we have a current EGL context. */
1414 input_device->pointer_surface =
1415 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1416
1417 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001418}
1419
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001420static struct wlsc_compositor *
1421wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001422{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001423 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001424 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001425 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001426
1427 ec = malloc(sizeof *ec);
1428 if (ec == NULL)
1429 return NULL;
1430
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001431 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001432 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001433
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001434 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001435
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001436 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001437
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001438 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001439 wl_list_init(&ec->input_device_list);
1440 wl_list_init(&ec->output_list);
1441 if (init_libudev(ec) < 0) {
1442 fprintf(stderr, "failed to initialize devices\n");
1443 return NULL;
1444 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001445
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001446 shooter = screenshooter_create(ec);
1447 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001448 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001449
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001450 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001451
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001452 setup_tty(ec, loop);
1453
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001454 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001455 schedule_repaint(ec);
1456
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001457 wl_list_init(&ec->animate_list);
1458
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001459 return ec;
1460}
1461
1462/* The plan here is to generate a random anonymous socket name and
1463 * advertise that through a service on the session dbus.
1464 */
1465static const char socket_name[] = "\0wayland";
1466
1467int main(int argc, char *argv[])
1468{
1469 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001470 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001471 GError *error = NULL;
1472 GOptionContext *context;
1473
1474 context = g_option_context_new(NULL);
1475 g_option_context_add_main_entries(context, option_entries, "Wayland");
1476 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1477 fprintf(stderr, "option parsing failed: %s\n", error->message);
1478 exit(EXIT_FAILURE);
1479 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001480
1481 display = wl_display_create();
1482
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001483 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001484 if (ec == NULL) {
1485 fprintf(stderr, "failed to create compositor\n");
1486 exit(EXIT_FAILURE);
1487 }
1488
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001489 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001490 fprintf(stderr, "failed to add socket: %m\n");
1491 exit(EXIT_FAILURE);
1492 }
1493
1494 wl_display_run(display);
1495
1496 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001497}