blob: aec318cceb67d5e798954eb5efbd70e7a520180a [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05008 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050017 */
18
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040019#include <stdio.h>
20#include <string.h>
21#include <stdlib.h>
22#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050023#include <stdarg.h>
Ray Strode966aa112008-12-19 14:28:02 -050024#include <termios.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040025#include <sys/ioctl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040026#include <fcntl.h>
27#include <unistd.h>
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050028#include <cairo.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050029#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050030#include <math.h>
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -050031#include <linux/input.h>
Ray Strode19ad6a92008-12-19 01:45:41 -050032#include <linux/vt.h>
Kristian Høgsberg01f941b2009-05-27 17:47:15 -040033#include <xf86drm.h>
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -050034#include <xf86drmMode.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050035#include <time.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040036
Kristian Høgsberg890bc052008-12-30 14:31:33 -050037#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
38#include <libudev.h>
39
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040040#include <GL/gl.h>
41#include <eagle.h>
42
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050043#include "wayland.h"
Kristian Høgsbergfe831a72008-12-21 21:50:23 -050044#include "wayland-protocol.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050045#include "cairo-util.h"
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050046#include "wayland-system-compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050047
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040048#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
49
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050050struct wlsc_matrix {
51 GLdouble d[16];
52};
53
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050054struct wl_visual {
55 struct wl_object base;
56};
57
Kristian Høgsberg4fa48732009-03-10 23:17:00 -040058struct wlsc_surface;
59
60struct wlsc_listener {
61 struct wl_list link;
62 void (*func)(struct wlsc_listener *listener,
63 struct wlsc_surface *surface);
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;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -040072 int32_t x, y, width, height;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050073
Kristian Høgsberg41a10682009-02-15 22:37:03 -050074 drmModeModeInfo *mode;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050075 uint32_t crtc_id;
76 uint32_t connector_id;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -040077
78 uint32_t fb_id[2];
79 uint32_t current;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050080};
81
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050082struct wlsc_input_device {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050083 struct wl_object base;
84 int32_t x, y;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050085 struct wlsc_compositor *ec;
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -050086 struct wlsc_surface *sprite;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050087 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050088
89 int grab;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050090 struct wlsc_surface *grab_surface;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -050091 struct wlsc_surface *pointer_focus;
92 struct wlsc_surface *keyboard_focus;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050093 struct wl_array keys;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -040094
95 struct wlsc_listener listener;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050096};
97
Kristian Høgsberg8e438622009-01-26 23:07:00 -050098struct wlsc_compositor {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040099 struct wl_compositor base;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500100 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
101
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400102 EGLDisplay display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400103 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500104 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400105 struct wl_display *wl_display;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500106
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500107 struct wl_list output_list;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500108 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500109 struct wl_list surface_list;
110
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400111 struct wl_list surface_destroy_listener_list;
112
Ray Strode966aa112008-12-19 14:28:02 -0500113 struct wl_event_source *term_signal_source;
114
115 /* tty handling state */
116 int tty_fd;
Ray Strodee96dcb82008-12-20 02:00:49 -0500117 uint32_t vt_active : 1;
Ray Strode966aa112008-12-19 14:28:02 -0500118
119 struct termios terminal_attributes;
120 struct wl_event_source *tty_input_source;
Ray Strode19ad6a92008-12-19 01:45:41 -0500121 struct wl_event_source *enter_vt_source;
122 struct wl_event_source *leave_vt_source;
123
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500124 struct udev *udev;
125
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500126 /* Repaint state. */
127 struct wl_event_source *timer_source;
128 int repaint_needed;
129 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500130 struct timespec previous_swap;
131 uint32_t current_frame;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400132 struct wl_event_source *drm_source;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500133
134 uint32_t meta_state;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500135 struct wl_list animate_list;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400136
137 struct wlsc_session *primary;
138 struct wl_list session_list;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400139};
140
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500141#define META_DOWN 256
142
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400143struct wlsc_vector {
144 GLdouble x, y, z;
145};
146
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500147struct wlsc_animate {
148 struct wl_list link;
149 void (*animate)(struct wlsc_animate *animate,
150 struct wlsc_compositor *compositor,
151 uint32_t frame, uint32_t msecs);
152};
153
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400154struct wlsc_session {
155 struct wlsc_surface *surface;
156 struct wlsc_vector target, current, previous;
157 GLdouble target_angle, current_angle, previous_angle;
158 struct wlsc_animate animate;
159 struct wl_list link;
160 struct wlsc_listener listener;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500161};
162
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500163struct wlsc_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500164 struct wl_surface base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500165 struct wlsc_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500166 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400167 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400168 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500169 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500170 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500171 struct wl_list link;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500172 struct wlsc_matrix matrix;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500173
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400174 /* FIXME: This should be it's own object at some point. */
175 struct wlsc_session session;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400176};
177
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500178static const char *option_background = "background.jpg";
179
180static const GOptionEntry option_entries[] = {
181 { "background", 'b', 0, G_OPTION_ARG_STRING,
182 &option_background, "Background image" },
183 { NULL }
184};
185
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500186struct screenshooter {
187 struct wl_object base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500188 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500189};
190
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500191struct screenshooter_interface {
192 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
193};
194
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500195static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500196screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500197{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500198 struct wlsc_compositor *ec = shooter->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500199 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500200 char buffer[256];
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500201 GdkPixbuf *pixbuf, *normal;
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500202 GError *error = NULL;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500203 unsigned char *data;
204 int i, j;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500205
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500206 i = 0;
207 output = container_of(ec->output_list.next, struct wlsc_output, link);
208 while (&output->link != &ec->output_list) {
209 snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500210 data = malloc(output->width * output->height * 4);
211 if (data == NULL) {
212 fprintf(stderr, "couldn't allocate image buffer\n");
213 continue;
214 }
215
216 glReadBuffer(GL_FRONT);
217 glPixelStorei(GL_PACK_ALIGNMENT, 1);
218 glReadPixels(0, 0, output->width, output->height,
219 GL_RGBA, GL_UNSIGNED_BYTE, data);
220
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500221 /* FIXME: We should just use a RGB visual for the frontbuffer. */
222 for (j = 3; j < output->width * output->height * 4; j += 4)
223 data[j] = 0xff;
224
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500225 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500226 8, output->width, output->height, output->width * 4,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500227 NULL, NULL);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500228 normal = gdk_pixbuf_flip(pixbuf, FALSE);
229 gdk_pixbuf_save(normal, buffer, "png", &error, NULL);
230 gdk_pixbuf_unref(normal);
231 gdk_pixbuf_unref(pixbuf);
232 free(data);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500233
234 output = container_of(output->link.next,
235 struct wlsc_output, link);
236 }
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500237}
238
Kristian Høgsbergfb6d68d2008-12-21 21:54:51 -0500239static const struct wl_message screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500240 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500241};
242
243static const struct wl_interface screenshooter_interface = {
244 "screenshooter", 1,
245 ARRAY_LENGTH(screenshooter_methods),
246 screenshooter_methods,
247};
248
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500249struct screenshooter_interface screenshooter_implementation = {
250 screenshooter_shoot
251};
252
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500253static struct screenshooter *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500254screenshooter_create(struct wlsc_compositor *ec)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500255{
256 struct screenshooter *shooter;
257
258 shooter = malloc(sizeof *shooter);
259 if (shooter == NULL)
260 return NULL;
261
262 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500263 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500264 shooter->ec = ec;
265
266 return shooter;
267};
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500268
269static void
270wlsc_matrix_init(struct wlsc_matrix *matrix)
271{
272 static const struct wlsc_matrix identity = {
273 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
274 };
275
276 memcpy(matrix, &identity, sizeof identity);
277}
278
279static void
280wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
281{
282 struct wlsc_matrix tmp;
283 const GLdouble *row, *column;
284 div_t d;
285 int i, j;
286
287 for (i = 0; i < 16; i++) {
288 tmp.d[i] = 0;
289 d = div(i, 4);
290 row = m->d + d.quot * 4;
291 column = n->d + d.rem;
292 for (j = 0; j < 4; j++)
293 tmp.d[i] += row[j] * column[j * 4];
294 }
295 memcpy(m, &tmp, sizeof tmp);
296}
297
298static void
299wlsc_matrix_translate(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
300{
301 struct wlsc_matrix translate = {
302 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
303 };
304
305 wlsc_matrix_multiply(matrix, &translate);
306}
307
308static void
309wlsc_matrix_scale(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
310{
311 struct wlsc_matrix scale = {
312 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
313 };
314
315 wlsc_matrix_multiply(matrix, &scale);
316}
317
318static void
319wlsc_matrix_rotate(struct wlsc_matrix *matrix,
320 GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
321{
322 GLdouble c = cos(angle);
323 GLdouble s = sin(angle);
324 struct wlsc_matrix rotate = {
325 { x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
326 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) - x * s, 0,
327 x * z * (1 - c) + y * x, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
328 0, 0, 0, 1 }
329 };
330
331 wlsc_matrix_multiply(matrix, &rotate);
332}
333
334static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500335wlsc_surface_init(struct wlsc_surface *surface,
336 struct wlsc_compositor *compositor, struct wl_visual *visual,
337 int32_t x, int32_t y, int32_t width, int32_t height)
338{
339 glGenTextures(1, &surface->texture);
340 surface->compositor = compositor;
341 surface->map.x = x;
342 surface->map.y = y;
343 surface->map.width = width;
344 surface->map.height = height;
345 surface->surface = EGL_NO_SURFACE;
346 surface->visual = visual;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400347 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500348}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500349
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500350static struct wlsc_surface *
351wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500352 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500353 int x, int y, int width, int height)
354{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500355 struct wlsc_surface *es;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500356 int stride;
357 void *data;
358
359 stride = cairo_image_surface_get_stride(surface);
360 data = cairo_image_surface_get_data(surface);
361
362 es = malloc(sizeof *es);
363 if (es == NULL)
364 return NULL;
365
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500366 wlsc_surface_init(es, ec, &ec->premultiplied_argb_visual,
367 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500368 glBindTexture(GL_TEXTURE_2D, es->texture);
369 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500370 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
371 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
372 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500373 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
374 GL_BGRA, GL_UNSIGNED_BYTE, data);
375
Kristian Høgsberg54879822008-11-23 17:07:32 -0500376 return es;
377}
378
379static void
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400380wlsc_surface_destroy(struct wlsc_surface *surface,
381 struct wlsc_compositor *compositor)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500382{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400383 struct wlsc_listener *l, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400384
385 l = container_of(compositor->surface_destroy_listener_list.next,
386 struct wlsc_listener, link);
387 while (&l->link != &compositor->surface_destroy_listener_list) {
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400388 next = container_of(l->link.next, struct wlsc_listener, link);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400389 l->func(l, surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400390 l = next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400391 }
392
393 wl_list_remove(&surface->link);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400394
395 glDeleteTextures(1, &surface->texture);
396 if (surface->surface != EGL_NO_SURFACE)
397 eglDestroySurface(compositor->display, surface->surface);
398 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500399}
400
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400401static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500402pointer_path(cairo_t *cr, int x, int y)
403{
404 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
405 const int width = 16, height = 16;
406
407 cairo_move_to(cr, x, y);
408 cairo_line_to(cr, x + tx, y + ty);
409 cairo_line_to(cr, x + dx, y + dy);
410 cairo_line_to(cr, x + width - end, y + height);
411 cairo_line_to(cr, x + width, y + height - end);
412 cairo_line_to(cr, x + dy, y + dx);
413 cairo_line_to(cr, x + ty, y + tx);
414 cairo_close_path(cr);
415}
416
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500417static struct wlsc_surface *
418pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500419{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500420 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500421 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500422 cairo_surface_t *surface;
423 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500424
425 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
426 width, height);
427
428 cr = cairo_create(surface);
429 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500430 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500431 cairo_set_source_rgb(cr, 0, 0, 0);
432 cairo_stroke_preserve(cr);
433 cairo_fill(cr);
434 blur_surface(surface, width);
435
436 pointer_path(cr, hotspot_x, hotspot_y);
437 cairo_stroke_preserve(cr);
438 cairo_set_source_rgb(cr, 1, 1, 1);
439 cairo_fill(cr);
440 cairo_destroy(cr);
441
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500442 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500443 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500444 x - hotspot_x,
445 y - hotspot_y,
446 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500447
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500448 cairo_surface_destroy(surface);
449
Kristian Høgsberg54879822008-11-23 17:07:32 -0500450 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500451}
452
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500453static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500454background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500455{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500456 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500457 GdkPixbuf *pixbuf;
458 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500459 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500460 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500461
462 background = malloc(sizeof *background);
463 if (background == NULL)
464 return NULL;
465
466 g_type_init();
467
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500468 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500469 output->width,
470 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500471 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500472 if (error != NULL) {
473 free(background);
474 return NULL;
475 }
476
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500477 data = gdk_pixbuf_get_pixels(pixbuf);
478
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500479 wlsc_surface_init(background, output->compositor,
480 &output->compositor->rgb_visual,
481 output->x, output->y, output->width, output->height);
482
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500483 glBindTexture(GL_TEXTURE_2D, background->texture);
484 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500485 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500486 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
487 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500488
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500489 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500490 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500491 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500492 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500493
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500494 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
495 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500496 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500497
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500498 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500499}
500
501static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500502wlsc_surface_draw(struct wlsc_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500503{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500504 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500505 GLint vertices[12];
506 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
507 GLuint indices[4] = { 0, 1, 2, 3 };
508
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500509 vertices[0] = es->map.x;
510 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500511 vertices[2] = 0;
512
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500513 vertices[3] = es->map.x;
514 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500515 vertices[5] = 0;
516
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500517 vertices[6] = es->map.x + es->map.width;
518 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500519 vertices[8] = 0;
520
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500521 vertices[9] = es->map.x + es->map.width;
522 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500523 vertices[11] = 0;
524
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500525 if (es->visual == &ec->argb_visual) {
526 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
527 glEnable(GL_BLEND);
528 } else if (es->visual == &ec->premultiplied_argb_visual) {
529 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
530 glEnable(GL_BLEND);
531 } else {
532 glDisable(GL_BLEND);
533 }
534
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500535 glPushMatrix();
536 glMultMatrixd(es->matrix.d);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500537 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500538 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500539 glEnableClientState(GL_VERTEX_ARRAY);
540 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
541 glVertexPointer(3, GL_INT, 0, vertices);
542 glTexCoordPointer(2, GL_INT, 0, tex_coords);
543 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500544 glPopMatrix();
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500545}
546
547static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400548wlsc_surface_raise(struct wlsc_surface *surface)
549{
550 struct wlsc_compositor *compositor = surface->compositor;
551
552 wl_list_remove(&surface->link);
553 wl_list_insert(compositor->surface_list.prev, &surface->link);
554}
555
556static void
557wlsc_surface_lower(struct wlsc_surface *surface)
558{
559 struct wlsc_compositor *compositor = surface->compositor;
560
561 wl_list_remove(&surface->link);
562 wl_list_insert(&compositor->surface_list, &surface->link);
563}
564
565static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500566wlsc_vector_add(struct wlsc_vector *v1, struct wlsc_vector *v2)
567{
568 v1->x += v2->x;
569 v1->y += v2->y;
570 v1->z += v2->z;
571}
572
573static void
574wlsc_vector_subtract(struct wlsc_vector *v1, struct wlsc_vector *v2)
575{
576 v1->x -= v2->x;
577 v1->y -= v2->y;
578 v1->z -= v2->z;
579}
580
581static void
582wlsc_vector_scalar(struct wlsc_vector *v1, GLdouble s)
583{
584 v1->x *= s;
585 v1->y *= s;
586 v1->z *= s;
587}
588
589static void
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400590page_flip_handler(int fd, unsigned int frame,
591 unsigned int sec, unsigned int usec, void *data)
592{
593 struct wlsc_output *output = data;
594 struct wlsc_animate *animate, *next;
595 struct wlsc_compositor *compositor = output->compositor;
596 uint32_t msecs;
597
598 msecs = sec * 1000 + usec / 1000;
599 wl_display_post_frame(compositor->wl_display,
600 &compositor->base,
601 compositor->current_frame, msecs);
602
603 wl_event_source_timer_update(compositor->timer_source, 10);
604 compositor->repaint_on_timeout = 1;
605
606 animate = container_of(compositor->animate_list.next, struct wlsc_animate, link);
607 while (&animate->link != &compositor->animate_list) {
608 next = container_of(animate->link.next,
609 struct wlsc_animate, link);
610 animate->animate(animate, compositor, compositor->current_frame, msecs);
611 animate = next;
612 }
613
614 compositor->current_frame++;
615}
616
617static void
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500618repaint_output(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400619{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500620 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500621 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500622 struct wlsc_input_device *eid;
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500623 double s = 3000;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400624 int fd;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500625
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500626 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
627 fprintf(stderr, "failed to make context current\n");
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500628 return;
629 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500630
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500631 glViewport(0, 0, output->width, output->height);
632 glMatrixMode(GL_PROJECTION);
633 glLoadIdentity();
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500634 glFrustum(-output->width / s, output->width / s,
635 output->height / s, -output->height / s, 1, 2 * s);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500636 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500637 glLoadIdentity();
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400638 glClearColor(0, 0, 0, 1);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500639
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500640 glTranslatef(-output->width / 2, -output->height / 2, -s / 2);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500641
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500642 if (output->background)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500643 wlsc_surface_draw(output->background);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500644 else
645 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400646
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500647 es = container_of(ec->surface_list.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500648 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500649 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500650 wlsc_surface_draw(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500651 es = container_of(es->link.next,
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400652 struct wlsc_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400653 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400654
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500655 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500656 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500657 while (&eid->link != &ec->input_device_list) {
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500658 wlsc_surface_draw(eid->sprite);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500659
660 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500661 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500662 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500663
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400664 fd = eglGetDisplayFD(ec->display);
665 output->current ^= 1;
666 eglBindColorBuffer(ec->display, output->surface, output->current);
667 drmModePageFlip(fd, output->crtc_id, output->fb_id[output->current ^ 1], output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500668}
669
670static void
671repaint(void *data)
672{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500673 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500674 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500675
676 if (!ec->repaint_needed) {
677 ec->repaint_on_timeout = 0;
678 return;
679 }
680
681 output = container_of(ec->output_list.next, struct wlsc_output, link);
682 while (&output->link != &ec->output_list) {
683 repaint_output(output);
684 output = container_of(output->link.next,
685 struct wlsc_output, link);
686 }
687
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500688 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400689}
690
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500691static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400692wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400693{
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400694 struct wlsc_output *output;
695 int fd;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400696
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400697 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400698 if (compositor->repaint_on_timeout)
699 return;
700
701 fd = eglGetDisplayFD(compositor->display);
702 output = container_of(compositor->output_list.next,
703 struct wlsc_output, link);
704 while (&output->link != &compositor->output_list) {
705 drmModePageFlip(fd, output->crtc_id,
706 output->fb_id[output->current ^ 1], output);
707 output = container_of(output->link.next,
708 struct wlsc_output, link);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500709 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400710}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500711
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500712static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500713surface_destroy(struct wl_client *client,
714 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400715{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500716 struct wlsc_surface *es = (struct wlsc_surface *) surface;
717 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500718
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500719 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400720
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400721 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400722}
723
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500724static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500725surface_attach(struct wl_client *client,
726 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500727 uint32_t width, uint32_t height, uint32_t stride,
728 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400729{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500730 struct wlsc_surface *es = (struct wlsc_surface *) surface;
731 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400732
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500733 if (es->surface != EGL_NO_SURFACE)
734 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400735
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500736 es->width = width;
737 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500738 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500739 name, width, height, stride, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500740 if (visual == &ec->argb_visual.base)
741 es->visual = &ec->argb_visual;
742 else if (visual == &ec->premultiplied_argb_visual.base)
743 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500744 else if (visual == &ec->rgb_visual.base)
745 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500746 else
747 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400748
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500749 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400750 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500751 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
752 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
753 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500754 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400755}
756
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500757static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400758wlsc_session_update_matrix(struct wlsc_session *session);
759
760static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500761surface_map(struct wl_client *client,
762 struct wl_surface *surface,
763 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400764{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500765 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400766
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500767 es->map.x = x;
768 es->map.y = y;
769 es->map.width = width;
770 es->map.height = height;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400771
772 wlsc_session_update_matrix(&es->session);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400773}
774
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500775static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500776surface_copy(struct wl_client *client,
777 struct wl_surface *surface,
778 int32_t dst_x, int32_t dst_y,
779 uint32_t name, uint32_t stride,
780 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500781{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500782 struct wlsc_surface *es = (struct wlsc_surface *) surface;
783 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500784 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500785
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500786 src = eglCreateSurfaceForName(ec->display, ec->config,
787 name, x + width, y + height, stride, NULL);
788
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400789 eglMakeCurrent(ec->display, es->surface, src, ec->context);
790 glDrawBuffer(GL_FRONT);
791 glReadBuffer(GL_FRONT);
792 glRasterPos2d(0, 0);
793 fprintf(stderr, "copypixels\n");
794 glCopyPixels(x, y, width, height, GL_COLOR);
795
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500796 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500797}
798
799static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500800surface_damage(struct wl_client *client,
801 struct wl_surface *surface,
802 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500803{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500804 /* FIXME: This need to take a damage region, of course. */
805}
806
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500807const static struct wl_surface_interface surface_interface = {
808 surface_destroy,
809 surface_attach,
810 surface_map,
811 surface_copy,
812 surface_damage
813};
814
815static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400816wlsc_session_update_matrix(struct wlsc_session *session)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500817{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400818 GLdouble tx, ty;
819 struct wlsc_surface *s = session->surface;
820
821 tx = s->map.x + s->map.width / 2;
822 ty = s->map.y + s->map.height / 2;
823
824 wlsc_matrix_init(&s->matrix);
825 wlsc_matrix_translate(&s->matrix, -tx, -ty, 0);
826 wlsc_matrix_rotate(&s->matrix, session->current_angle, 0, 1, 0);
827 wlsc_matrix_translate(&s->matrix, tx + session->current.x,
828 ty + session->current.y, session->current.z);
829}
830
831static void
832wl_session_animate(struct wlsc_animate *animate,
833 struct wlsc_compositor *compositor,
834 uint32_t frame, uint32_t msecs)
835{
836 struct wlsc_session *session;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500837 struct wlsc_surface *s;
838 double angle_force, angle;
839 struct wlsc_vector force, tmp;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400840 double step = 0.2;
841 double friction = 1.5;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500842 double spring = 0.2;
843
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400844 session = container_of(animate, struct wlsc_session, animate);
845 s = session->surface;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500846
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400847 angle = session->current_angle;
848 angle_force = (session->target_angle - angle) * spring +
849 (session->previous_angle - angle) * friction;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500850
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400851 session->current_angle =
852 angle + (angle - session->previous_angle) + angle_force * step;
853 session->previous_angle = angle;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500854
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400855 force = session->target;
856 wlsc_vector_subtract(&force, &session->current);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500857 wlsc_vector_scalar(&force, spring);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400858 tmp = session->previous;
859 wlsc_vector_subtract(&tmp, &session->current);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500860 wlsc_vector_scalar(&tmp, friction);
861 wlsc_vector_add(&force, &tmp);
862
863 wlsc_vector_scalar(&force, step);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400864 wlsc_vector_add(&force, &session->current);
865 wlsc_vector_subtract(&force, &session->previous);
866 session->previous = session->current;
867 wlsc_vector_add(&session->current, &force);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500868
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400869 wlsc_session_update_matrix(session);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500870
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400871 tmp = session->current;
872 wlsc_vector_subtract(&tmp, &session->target);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500873 if (tmp.x * tmp.x + tmp.y * tmp.y + tmp.z * tmp.z > 0.001) {
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400874 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500875 } else {
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400876 wl_list_remove(&session->animate.link);
877 wl_list_init(&session->animate.link);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500878 }
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500879}
880
881static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400882wlsc_session_activate(struct wlsc_compositor *compositor, struct wlsc_session *session)
883{
884 struct wlsc_session *s;
885 int i;
886
887 compositor->primary = session;
888 if (wl_list_empty(&compositor->session_list))
889 return;
890
891 session->target.x = 0;
892 session->target.y = 0;
893 session->target.z = -600;
894 session->target_angle = 0;
895 wl_list_remove(&session->animate.link);
896 wl_list_insert(compositor->animate_list.prev, &session->animate.link);
897
898 i = 0;
899 s = container_of(session->link.prev,
900 struct wlsc_session, link);
901 while (&s->link != &compositor->session_list) {
902 s->target.x = -1000 - 500 * i;
903 s->target.y = 0;
904 s->target.z = -2500;
905 s->target_angle = M_PI / 4;
906 wl_list_remove(&s->animate.link);
907 wl_list_insert(compositor->animate_list.prev, &s->animate.link);
908 wlsc_surface_lower(s->surface);
909
910 s = container_of(s->link.prev,
911 struct wlsc_session, link);
912 i++;
913 }
914
915 i = 0;
916 s = container_of(session->link.next,
917 struct wlsc_session, link);
918 while (&s->link != &compositor->session_list) {
919 s->target.x = 1000 + 500 * i;
920 s->target.y = 0;
921 s->target.z = -2500;
922 s->target_angle = -M_PI / 4;
923 wl_list_remove(&s->animate.link);
924 wl_list_insert(compositor->animate_list.prev, &s->animate.link);
925 wlsc_surface_lower(s->surface);
926
927 s = container_of(s->link.next,
928 struct wlsc_session, link);
929 i++;
930 }
931
932 wlsc_compositor_schedule_repaint(compositor);
933}
934
935static void
936wlsc_session_handle_surface_destroy(struct wlsc_listener *listener,
937 struct wlsc_surface *surface)
938{
939 struct wlsc_session *session =
940 container_of(listener, struct wlsc_session, listener);
941 struct wlsc_compositor *compositor;
942 struct wlsc_session *primary;
943
944 if (session->surface != surface)
945 return;
946
947 compositor = session->surface->compositor;
948 if (compositor->primary == session) {
949 printf("destroy session %p [%p, %p]\n", session, session->link.prev, session->link.next);
950 if (session->link.next != &compositor->session_list)
951 primary = container_of(session->link.next,
952 struct wlsc_session, link);
953 else if (session->link.prev != &compositor->session_list)
954 primary = container_of(session->link.prev,
955 struct wlsc_session, link);
956 else
957 primary = NULL;
958 }
959
960 wl_list_remove(&session->animate.link);
961 wl_list_remove(&session->link);
962 wl_list_remove(&session->listener.link);
963
964 if (compositor->primary == session) {
965 printf("activating session %p\n", primary);
966 wlsc_session_activate(compositor, primary);
967 }
968}
969
970static void
971wlsc_session_init(struct wlsc_session *session, struct wlsc_surface *surface)
972{
973 struct wlsc_compositor *c = surface->compositor;
974
975 session->animate.animate = wl_session_animate;
976 wl_list_init(&session->animate.link);
977 wl_list_insert(c->session_list.prev, &session->link);
978
979 session->surface = surface;
980 session->current.x = 0;
981 session->current.y = 0;
982 session->current.z = 0;
983 session->current_angle = 0;
984 session->target.x = 0;
985 session->target.y = 0;
986 session->target.z = 0;
987 session->target_angle = 0;
988 session->previous_angle = 0;
989
990 wlsc_session_update_matrix(session);
991
992 session->listener.func = wlsc_session_handle_surface_destroy;
993 wl_list_insert(c->surface_destroy_listener_list.prev,
994 &session->listener.link);
995
996 printf("added session %p [%p, %p]\n", session, session->link.prev, session->link.next);
997 printf(" - prev [%p]\n", session->link.prev->prev);
998}
999
1000static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001001compositor_create_surface(struct wl_client *client,
1002 struct wl_compositor *compositor, uint32_t id)
1003{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001004 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001005 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001006
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001007 surface = malloc(sizeof *surface);
1008 if (surface == NULL)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001009 /* FIXME: Send OOM event. */
1010 return;
1011
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001012 wlsc_surface_init(surface, ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001013
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001014 wl_list_insert(ec->surface_list.prev, &surface->link);
1015 wl_client_add_surface(client, &surface->base,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001016 &surface_interface, id);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001017
1018 wlsc_session_init(&surface->session, surface);
1019 wlsc_session_activate(ec, &surface->session);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001020}
1021
1022static void
1023compositor_commit(struct wl_client *client,
1024 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001025{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001026 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001027
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001028 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001029 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001030}
1031
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001032const static struct wl_compositor_interface compositor_interface = {
1033 compositor_create_surface,
1034 compositor_commit
1035};
1036
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001037static void
1038wlsc_surface_transform(struct wlsc_surface *surface,
1039 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1040{
1041 /* Transform to surface coordinates. */
1042 *sx = (x - surface->map.x) * surface->width / surface->map.width;
1043 *sy = (y - surface->map.y) * surface->height / surface->map.height;
1044}
1045
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001046static void
1047wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
1048 struct wlsc_surface *surface)
1049{
1050 if (device->keyboard_focus == surface)
1051 return;
1052
1053 if (device->keyboard_focus &&
1054 (!surface || device->keyboard_focus->base.client != surface->base.client))
1055 wl_surface_post_event(&device->keyboard_focus->base,
1056 &device->base,
Kristian Høgsberg786ca0d2009-03-06 21:25:21 -05001057 WL_INPUT_KEYBOARD_FOCUS, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001058
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001059 if (surface)
1060 wl_surface_post_event(&surface->base,
1061 &device->base,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001062 WL_INPUT_KEYBOARD_FOCUS,
1063 &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001064
1065 device->keyboard_focus = surface;
1066}
1067
1068static void
1069wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
1070 struct wlsc_surface *surface)
1071{
1072 if (device->pointer_focus == surface)
1073 return;
1074
1075 if (device->pointer_focus &&
1076 (!surface || device->pointer_focus->base.client != surface->base.client))
1077 wl_surface_post_event(&device->pointer_focus->base,
1078 &device->base,
1079 WL_INPUT_POINTER_FOCUS, NULL);
1080 if (surface)
1081 wl_surface_post_event(&surface->base,
1082 &device->base,
1083 WL_INPUT_POINTER_FOCUS, &surface->base);
1084
1085 device->pointer_focus = surface;
1086}
1087
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001088static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -05001089pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001090{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001091 struct wlsc_compositor *ec = device->ec;
1092 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001093
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001094 if (device->grab > 0) {
1095 wlsc_surface_transform(device->grab_surface,
1096 device->x, device->y, sx, sy);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001097 return device->grab_surface;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001098 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001099
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001100 es = container_of(ec->surface_list.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001101 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001102 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001103 if (es->map.x <= device->x &&
1104 device->x < es->map.x + es->map.width &&
1105 es->map.y <= device->y &&
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -05001106 device->y < es->map.y + es->map.height) {
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001107 wlsc_surface_transform(es, device->x, device->y, sx, sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001108 return es;
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -05001109 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001110
1111 es = container_of(es->link.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001112 struct wlsc_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -05001113
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001114 }
1115
1116 return NULL;
1117}
1118
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001119void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001120notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001121{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001122 struct wlsc_surface *es;
1123 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001124 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001125 const int hotspot_x = 16, hotspot_y = 16;
1126 int32_t sx, sy;
1127
Ray Strodee96dcb82008-12-20 02:00:49 -05001128 if (!ec->vt_active)
1129 return;
1130
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001131 /* FIXME: We need some multi head love here. */
1132 output = container_of(ec->output_list.next, struct wlsc_output, link);
1133 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -05001134 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001135 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -05001136 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001137 if (x >= output->x + output->width)
1138 x = output->x + output->width - 1;
1139 if (y >= output->y + output->height)
1140 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -05001141
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -05001142 device->x = x;
1143 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -05001144 es = pick_surface(device, &sx, &sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001145
1146 wlsc_input_device_set_pointer_focus(device, es);
1147
Kristian Høgsberg7e972a52008-12-21 17:26:00 -05001148 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001149 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -05001150 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001151
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -05001152 device->sprite->map.x = x - hotspot_x;
1153 device->sprite->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001154
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001155 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001156}
1157
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001158void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001159notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001160 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001161{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001162 struct wlsc_surface *surface;
1163 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001164 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001165
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001166 if (!compositor->vt_active)
Ray Strodee96dcb82008-12-20 02:00:49 -05001167 return;
1168
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001169 surface = pick_surface(device, &sx, &sy);
1170 if (surface) {
1171 wlsc_surface_raise(surface);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -05001172
Kristian Høgsberg29573bc2008-12-11 23:27:27 -05001173 if (state) {
1174 device->grab++;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001175 device->grab_surface = surface;
1176 wlsc_input_device_set_keyboard_focus(device,
1177 surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -05001178 } else {
1179 device->grab--;
1180 }
1181
Kristian Høgsberg5a75c902008-12-10 13:16:50 -05001182 /* FIXME: Swallow click on raise? */
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001183 wl_surface_post_event(&surface->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001184 WL_INPUT_BUTTON, button, state,
1185 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001186
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001187 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001188 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001189}
1190
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001191static void on_term_signal(int signal_number, void *data);
1192
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001193void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001194notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001195 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001196{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001197 struct wlsc_compositor *compositor = device->ec;
1198 struct wlsc_session *s;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001199 uint32_t *k, *end;
1200
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001201 if (!compositor->vt_active)
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001202 return;
Ray Strodee96dcb82008-12-20 02:00:49 -05001203
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001204 switch (key | compositor->meta_state) {
Kristian Høgsberg45b7a3a2009-08-14 05:53:50 -04001205 case KEY_BACKSPACE | META_DOWN:
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001206 on_term_signal(SIGTERM, compositor);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001207 return;
1208
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001209 case KEY_LEFT | META_DOWN:
1210 if (!compositor->primary || !state)
1211 break;
1212 s = container_of(compositor->primary->link.prev,
1213 struct wlsc_session, link);
1214 if (&s->link != &compositor->session_list) {
1215 wlsc_session_activate(compositor, s);
1216 if (device->grab == 0)
1217 wlsc_input_device_set_keyboard_focus(device, s->surface);
1218 }
1219 return;
1220
1221
1222 case KEY_RIGHT | META_DOWN:
1223 if (!compositor->primary || !state)
1224 break;
1225 s = container_of(compositor->primary->link.next,
1226 struct wlsc_session, link);
1227 if (&s->link != &compositor->session_list) {
1228 wlsc_session_activate(compositor, s);
1229 if (device->grab == 0)
1230 wlsc_input_device_set_keyboard_focus(device, s->surface);
1231
1232 }
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001233 return;
1234
1235 case KEY_LEFTMETA:
1236 case KEY_RIGHTMETA:
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001237 case KEY_LEFTMETA | META_DOWN:
1238 case KEY_RIGHTMETA | META_DOWN:
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001239 compositor->meta_state = state ? META_DOWN : 0;
1240
1241 if (state == 0 && compositor->primary) {
1242 s = compositor->primary;
1243 s->target.z = 0;
1244 wl_list_remove(&s->animate.link);
1245 wl_list_insert(compositor->animate_list.prev, &s->animate.link);
1246 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001247 }
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001248
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001249 return;
1250 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001251
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001252 end = device->keys.data + device->keys.size;
1253 for (k = device->keys.data; k < end; k++) {
1254 if (*k == key)
1255 *k = *--end;
1256 }
1257 device->keys.size = (void *) end - device->keys.data;
1258 if (state) {
1259 k = wl_array_add(&device->keys, sizeof *k);
1260 *k = key;
1261 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001262
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001263 if (device->keyboard_focus != NULL)
1264 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -05001265 &device->base,
1266 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001267}
1268
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001269struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001270evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001271 struct wl_display *display, const char *path);
1272
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001273static void
1274handle_surface_destroy(struct wlsc_listener *listener,
1275 struct wlsc_surface *surface)
1276{
1277 struct wlsc_input_device *device =
1278 container_of(listener, struct wlsc_input_device, listener);
1279
1280 if (device->grab_surface == surface) {
1281 device->grab_surface = NULL;
1282 device->grab = 0;
1283 }
1284 if (device->keyboard_focus == surface)
1285 device->keyboard_focus = NULL;
1286 if (device->pointer_focus == surface)
1287 device->pointer_focus = NULL;
1288}
1289
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001290static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001291create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001292{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001293 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001294
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001295 device = malloc(sizeof *device);
1296 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001297 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001298
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001299 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001300 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001301 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001302 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001303 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001304 device->x = 100;
1305 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001306 device->ec = ec;
1307
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001308 device->listener.func = handle_surface_destroy;
1309 wl_list_insert(ec->surface_destroy_listener_list.prev,
1310 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001311 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001312
1313 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001314}
1315
1316void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001317wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001318{
1319 *x = device->x;
1320 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001321}
1322
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001323static void
1324post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001325{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001326 struct wlsc_output *output =
1327 container_of(global, struct wlsc_output, base);
1328
1329 wl_client_post_event(client, global,
1330 WL_OUTPUT_GEOMETRY,
1331 output->width, output->height);
1332}
1333
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001334static void
1335on_drm_input(int fd, uint32_t mask, void *data)
1336{
1337 drmEventContext evctx;
1338
1339 memset(&evctx, 0, sizeof evctx);
1340 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1341 evctx.page_flip_handler = page_flip_handler;
1342 drmHandleEvent(fd, &evctx);
1343}
1344
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001345static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001346init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001347{
1348 static const EGLint config_attribs[] = {
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001349 EGL_DEPTH_SIZE, 0,
1350 EGL_STENCIL_SIZE, 0,
1351 EGL_CONFIG_CAVEAT, EGL_NONE,
1352 EGL_RED_SIZE, 8,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001353 EGL_NONE
1354 };
1355
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001356 struct wl_event_loop *loop;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001357 EGLint major, minor;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001358 int fd;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001359
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -05001360 ec->display = eglCreateDisplayNative(device);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001361 if (ec->display == NULL) {
1362 fprintf(stderr, "failed to create display\n");
1363 return -1;
1364 }
1365
1366 if (!eglInitialize(ec->display, &major, &minor)) {
1367 fprintf(stderr, "failed to initialize display\n");
1368 return -1;
1369 }
1370
1371 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
1372 return -1;
1373
1374 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
1375 if (ec->context == NULL) {
1376 fprintf(stderr, "failed to create context\n");
1377 return -1;
1378 }
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001379
1380 loop = wl_display_get_event_loop(ec->wl_display);
1381 fd = eglGetDisplayFD(ec->display);
1382 ec->drm_source =
1383 wl_event_loop_add_fd(loop, fd,
1384 WL_EVENT_READABLE, on_drm_input, ec);
1385
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001386 return 0;
1387}
1388
1389static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001390create_output(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001391{
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001392 drmModeConnector *connector;
1393 drmModeRes *resources;
1394 drmModeEncoder *encoder;
Kristian Høgsberg41a10682009-02-15 22:37:03 -05001395 drmModeModeInfo *mode;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001396 struct wlsc_output *output;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001397 uint32_t name, handle, stride;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001398 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001399
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001400 if (ec->display == NULL && init_egl(ec, device) < 0) {
1401 fprintf(stderr, "failed to initialize egl\n");
1402 return -1;
1403 }
1404
1405 output = malloc(sizeof *output);
1406 if (output == NULL)
1407 return -1;
1408
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001409 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001410 resources = drmModeGetResources(fd);
1411 if (!resources) {
1412 fprintf(stderr, "drmModeGetResources failed\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001413 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001414 }
1415
1416 for (i = 0; i < resources->count_connectors; i++) {
1417 connector = drmModeGetConnector(fd, resources->connectors[i]);
1418 if (connector == NULL)
1419 continue;
1420
1421 if (connector->connection == DRM_MODE_CONNECTED &&
1422 connector->count_modes > 0)
1423 break;
1424
1425 drmModeFreeConnector(connector);
1426 }
1427
1428 if (i == resources->count_connectors) {
1429 fprintf(stderr, "No currently active connector found.\n");
1430 return -1;
1431 }
1432
1433 mode = &connector->modes[0];
1434
1435 for (i = 0; i < resources->count_encoders; i++) {
1436 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
1437
1438 if (encoder == NULL)
1439 continue;
1440
1441 if (encoder->encoder_id == connector->encoder_id)
1442 break;
1443
1444 drmModeFreeEncoder(encoder);
1445 }
1446
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001447 output->compositor = ec;
1448 output->crtc_id = encoder->crtc_id;
1449 output->connector_id = connector->connector_id;
1450 output->mode = mode;
1451 output->x = 0;
1452 output->y = 0;
1453 output->width = mode->hdisplay;
1454 output->height = mode->vdisplay;
1455
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001456 output->surface = eglCreateSurface(ec->display,
1457 ec->config,
1458 output->width,
1459 output->height,
1460 2, NULL);
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001461 if (output->surface == NULL) {
1462 fprintf(stderr, "failed to create surface\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001463 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001464 }
1465
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001466 for (i = 0; i < 2; i++) {
1467 eglGetColorBuffer(output->surface,
1468 i, &name, &handle, &stride);
1469
1470 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
1471 32, 32, stride, handle, &output->fb_id[i]);
1472 if (ret) {
1473 fprintf(stderr, "failed to add fb %d: %m\n", i);
1474 return -1;
1475 }
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001476 }
1477
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001478 output->current = 0;
1479 ret = drmModeSetCrtc(fd, encoder->crtc_id,
1480 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001481 &connector->connector_id, 1, mode);
1482 if (ret) {
1483 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001484 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001485 }
1486
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001487 output->base.interface = &wl_output_interface;
1488 wl_display_add_object(ec->wl_display, &output->base);
1489 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
1490 wl_list_insert(ec->output_list.prev, &output->link);
1491
1492 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
1493 fprintf(stderr, "failed to make context current\n");
1494 return -1;
1495 }
1496
1497 output->background = background_create(output, option_background);
1498
1499 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001500}
1501
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001502static const struct wl_interface visual_interface = {
1503 "visual", 1,
1504};
1505
1506static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001507add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001508{
1509 ec->argb_visual.base.interface = &visual_interface;
1510 ec->argb_visual.base.implementation = NULL;
1511 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001512 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001513
1514 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1515 ec->premultiplied_argb_visual.base.implementation = NULL;
1516 wl_display_add_object(ec->wl_display,
1517 &ec->premultiplied_argb_visual.base);
1518 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001519 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001520
1521 ec->rgb_visual.base.interface = &visual_interface;
1522 ec->rgb_visual.base.implementation = NULL;
1523 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001524 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1525}
1526
Ray Strode19ad6a92008-12-19 01:45:41 -05001527static void on_enter_vt(int signal_number, void *data)
1528{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001529 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001530 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001531 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -05001532
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001533 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001534 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001535
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001536 fd = eglGetDisplayFD(ec->display);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001537 output = container_of(ec->output_list.next, struct wlsc_output, link);
1538 while (&output->link != &ec->output_list) {
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001539 ret = drmModeSetCrtc(fd, output->crtc_id,
1540 output->fb_id[output->current], 0, 0,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001541 &output->connector_id, 1, output->mode);
1542 if (ret)
1543 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1544 output->connector_id);
1545
1546 output = container_of(output->link.next,
1547 struct wlsc_output, link);
Ray Strode19ad6a92008-12-19 01:45:41 -05001548 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001549}
1550
1551static void on_leave_vt(int signal_number, void *data)
1552{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001553 struct wlsc_compositor *ec = data;
Ray Strode19ad6a92008-12-19 01:45:41 -05001554
1555 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001556 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001557}
1558
Ray Strode966aa112008-12-19 14:28:02 -05001559static void
1560on_tty_input(int fd, uint32_t mask, void *data)
1561{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001562 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001563
1564 /* Ignore input to tty. We get keyboard events from evdev
1565 */
1566 tcflush(ec->tty_fd, TCIFLUSH);
1567}
1568
1569static void on_term_signal(int signal_number, void *data)
1570{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001571 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001572
1573 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1574 fprintf(stderr, "could not restore terminal to canonical mode\n");
1575
1576 exit(0);
1577}
1578
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001579static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001580{
1581 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001582 struct vt_mode mode = { 0 };
1583
1584 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1585 if (ec->tty_fd <= 0) {
1586 fprintf(stderr, "failed to open active tty: %m\n");
1587 return -1;
1588 }
Ray Strode966aa112008-12-19 14:28:02 -05001589
1590 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1591 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001592 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001593 }
1594
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001595 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001596 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001597 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001598
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001599 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001600 raw_attributes.c_oflag |= OPOST | OCRNL;
1601
1602 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1603 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1604
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001605 ec->term_signal_source =
1606 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001607
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001608 ec->tty_input_source =
1609 wl_event_loop_add_fd(loop, ec->tty_fd,
1610 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001611
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001612 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001613 mode.mode = VT_PROCESS;
1614 mode.relsig = SIGUSR1;
1615 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001616 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001617 fprintf(stderr, "failed to take control of vt handling\n");
1618 }
1619
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001620 ec->leave_vt_source =
1621 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1622 ec->enter_vt_source =
1623 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001624
1625 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001626}
1627
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001628static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001629init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001630{
1631 struct udev_enumerate *e;
1632 struct udev_list_entry *entry;
1633 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001634 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001635 struct wlsc_input_device *input_device;
1636
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001637 ec->udev = udev_new();
1638 if (ec->udev == NULL) {
1639 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001640 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001641 }
1642
1643 input_device = create_input_device(ec);
1644
1645 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001646 udev_enumerate_add_match_subsystem(e, "input");
1647 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001648 udev_enumerate_scan_devices(e);
1649 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1650 path = udev_list_entry_get_name(entry);
1651 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001652 evdev_input_device_create(input_device, ec->wl_display,
1653 udev_device_get_devnode(device));
1654 }
1655 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001656
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001657 e = udev_enumerate_new(ec->udev);
1658 udev_enumerate_add_match_subsystem(e, "drm");
1659 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1660 udev_enumerate_scan_devices(e);
1661 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1662 path = udev_list_entry_get_name(entry);
1663 device = udev_device_new_from_syspath(ec->udev, path);
1664 if (create_output(ec, device) < 0) {
1665 fprintf(stderr, "failed to create output for %s\n", path);
1666 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001667 }
1668 }
1669 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001670
1671 /* Create the pointer surface now that we have a current EGL context. */
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -05001672 input_device->sprite =
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001673 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1674
1675 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001676}
1677
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001678static struct wlsc_compositor *
1679wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001680{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001681 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001682 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001683 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001684
1685 ec = malloc(sizeof *ec);
1686 if (ec == NULL)
1687 return NULL;
1688
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001689 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001690 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001691
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001692 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001693
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001694 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001695
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001696 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001697 wl_list_init(&ec->input_device_list);
1698 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001699 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001700 if (init_libudev(ec) < 0) {
1701 fprintf(stderr, "failed to initialize devices\n");
1702 return NULL;
1703 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001704
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001705 shooter = screenshooter_create(ec);
1706 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001707 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001708
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001709 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001710
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001711 setup_tty(ec, loop);
1712
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001713 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001714 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001715
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001716 wl_list_init(&ec->animate_list);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001717 wl_list_init(&ec->session_list);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001718
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001719 return ec;
1720}
1721
1722/* The plan here is to generate a random anonymous socket name and
1723 * advertise that through a service on the session dbus.
1724 */
1725static const char socket_name[] = "\0wayland";
1726
1727int main(int argc, char *argv[])
1728{
1729 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001730 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001731 GError *error = NULL;
1732 GOptionContext *context;
1733
1734 context = g_option_context_new(NULL);
1735 g_option_context_add_main_entries(context, option_entries, "Wayland");
1736 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1737 fprintf(stderr, "option parsing failed: %s\n", error->message);
1738 exit(EXIT_FAILURE);
1739 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001740
1741 display = wl_display_create();
1742
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001743 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001744 if (ec == NULL) {
1745 fprintf(stderr, "failed to create compositor\n");
1746 exit(EXIT_FAILURE);
1747 }
1748
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001749 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001750 fprintf(stderr, "failed to add socket: %m\n");
1751 exit(EXIT_FAILURE);
1752 }
1753
1754 wl_display_run(display);
1755
1756 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001757}