blob: c135aea04881ac51d9da04dee7fd51f89a466ec0 [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øgsberg8e438622009-01-26 23:07:00 -0500652 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{
694 struct wl_event_loop *loop;
695
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400696 compositor->repaint_needed = 1;
697 if (!compositor->repaint_on_timeout) {
698 loop = wl_display_get_event_loop(compositor->wl_display);
699 wl_event_loop_add_idle(loop, repaint, compositor);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500700 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400701}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500702
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500703static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500704surface_destroy(struct wl_client *client,
705 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400706{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500707 struct wlsc_surface *es = (struct wlsc_surface *) surface;
708 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500709
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500710 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400711
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400712 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400713}
714
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500715static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500716surface_attach(struct wl_client *client,
717 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500718 uint32_t width, uint32_t height, uint32_t stride,
719 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400720{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500721 struct wlsc_surface *es = (struct wlsc_surface *) surface;
722 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400723
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500724 if (es->surface != EGL_NO_SURFACE)
725 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400726
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500727 es->width = width;
728 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500729 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500730 name, width, height, stride, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500731 if (visual == &ec->argb_visual.base)
732 es->visual = &ec->argb_visual;
733 else if (visual == &ec->premultiplied_argb_visual.base)
734 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500735 else if (visual == &ec->rgb_visual.base)
736 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500737 else
738 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400739
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500740 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400741 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500742 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
743 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
744 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500745 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400746}
747
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500748static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400749wlsc_session_update_matrix(struct wlsc_session *session);
750
751static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500752surface_map(struct wl_client *client,
753 struct wl_surface *surface,
754 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400755{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500756 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400757
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500758 es->map.x = x;
759 es->map.y = y;
760 es->map.width = width;
761 es->map.height = height;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400762
763 wlsc_session_update_matrix(&es->session);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400764}
765
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500766static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500767surface_copy(struct wl_client *client,
768 struct wl_surface *surface,
769 int32_t dst_x, int32_t dst_y,
770 uint32_t name, uint32_t stride,
771 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500772{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500773 struct wlsc_surface *es = (struct wlsc_surface *) surface;
774 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500775 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500776
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500777 src = eglCreateSurfaceForName(ec->display, ec->config,
778 name, x + width, y + height, stride, NULL);
779
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400780 eglMakeCurrent(ec->display, es->surface, src, ec->context);
781 glDrawBuffer(GL_FRONT);
782 glReadBuffer(GL_FRONT);
783 glRasterPos2d(0, 0);
784 fprintf(stderr, "copypixels\n");
785 glCopyPixels(x, y, width, height, GL_COLOR);
786
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500787 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500788}
789
790static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500791surface_damage(struct wl_client *client,
792 struct wl_surface *surface,
793 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500794{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500795 /* FIXME: This need to take a damage region, of course. */
796}
797
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500798const static struct wl_surface_interface surface_interface = {
799 surface_destroy,
800 surface_attach,
801 surface_map,
802 surface_copy,
803 surface_damage
804};
805
806static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400807wlsc_session_update_matrix(struct wlsc_session *session)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500808{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400809 GLdouble tx, ty;
810 struct wlsc_surface *s = session->surface;
811
812 tx = s->map.x + s->map.width / 2;
813 ty = s->map.y + s->map.height / 2;
814
815 wlsc_matrix_init(&s->matrix);
816 wlsc_matrix_translate(&s->matrix, -tx, -ty, 0);
817 wlsc_matrix_rotate(&s->matrix, session->current_angle, 0, 1, 0);
818 wlsc_matrix_translate(&s->matrix, tx + session->current.x,
819 ty + session->current.y, session->current.z);
820}
821
822static void
823wl_session_animate(struct wlsc_animate *animate,
824 struct wlsc_compositor *compositor,
825 uint32_t frame, uint32_t msecs)
826{
827 struct wlsc_session *session;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500828 struct wlsc_surface *s;
829 double angle_force, angle;
830 struct wlsc_vector force, tmp;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400831 double step = 0.2;
832 double friction = 1.5;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500833 double spring = 0.2;
834
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400835 session = container_of(animate, struct wlsc_session, animate);
836 s = session->surface;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500837
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400838 angle = session->current_angle;
839 angle_force = (session->target_angle - angle) * spring +
840 (session->previous_angle - angle) * friction;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500841
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400842 session->current_angle =
843 angle + (angle - session->previous_angle) + angle_force * step;
844 session->previous_angle = angle;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500845
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400846 force = session->target;
847 wlsc_vector_subtract(&force, &session->current);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500848 wlsc_vector_scalar(&force, spring);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400849 tmp = session->previous;
850 wlsc_vector_subtract(&tmp, &session->current);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500851 wlsc_vector_scalar(&tmp, friction);
852 wlsc_vector_add(&force, &tmp);
853
854 wlsc_vector_scalar(&force, step);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400855 wlsc_vector_add(&force, &session->current);
856 wlsc_vector_subtract(&force, &session->previous);
857 session->previous = session->current;
858 wlsc_vector_add(&session->current, &force);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500859
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400860 wlsc_session_update_matrix(session);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500861
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400862 tmp = session->current;
863 wlsc_vector_subtract(&tmp, &session->target);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500864 if (tmp.x * tmp.x + tmp.y * tmp.y + tmp.z * tmp.z > 0.001) {
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400865 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500866 } else {
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400867 wl_list_remove(&session->animate.link);
868 wl_list_init(&session->animate.link);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500869 }
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500870}
871
872static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400873wlsc_session_activate(struct wlsc_compositor *compositor, struct wlsc_session *session)
874{
875 struct wlsc_session *s;
876 int i;
877
878 compositor->primary = session;
879 if (wl_list_empty(&compositor->session_list))
880 return;
881
882 session->target.x = 0;
883 session->target.y = 0;
884 session->target.z = -600;
885 session->target_angle = 0;
886 wl_list_remove(&session->animate.link);
887 wl_list_insert(compositor->animate_list.prev, &session->animate.link);
888
889 i = 0;
890 s = container_of(session->link.prev,
891 struct wlsc_session, link);
892 while (&s->link != &compositor->session_list) {
893 s->target.x = -1000 - 500 * i;
894 s->target.y = 0;
895 s->target.z = -2500;
896 s->target_angle = M_PI / 4;
897 wl_list_remove(&s->animate.link);
898 wl_list_insert(compositor->animate_list.prev, &s->animate.link);
899 wlsc_surface_lower(s->surface);
900
901 s = container_of(s->link.prev,
902 struct wlsc_session, link);
903 i++;
904 }
905
906 i = 0;
907 s = container_of(session->link.next,
908 struct wlsc_session, link);
909 while (&s->link != &compositor->session_list) {
910 s->target.x = 1000 + 500 * i;
911 s->target.y = 0;
912 s->target.z = -2500;
913 s->target_angle = -M_PI / 4;
914 wl_list_remove(&s->animate.link);
915 wl_list_insert(compositor->animate_list.prev, &s->animate.link);
916 wlsc_surface_lower(s->surface);
917
918 s = container_of(s->link.next,
919 struct wlsc_session, link);
920 i++;
921 }
922
923 wlsc_compositor_schedule_repaint(compositor);
924}
925
926static void
927wlsc_session_handle_surface_destroy(struct wlsc_listener *listener,
928 struct wlsc_surface *surface)
929{
930 struct wlsc_session *session =
931 container_of(listener, struct wlsc_session, listener);
932 struct wlsc_compositor *compositor;
933 struct wlsc_session *primary;
934
935 if (session->surface != surface)
936 return;
937
938 compositor = session->surface->compositor;
939 if (compositor->primary == session) {
940 printf("destroy session %p [%p, %p]\n", session, session->link.prev, session->link.next);
941 if (session->link.next != &compositor->session_list)
942 primary = container_of(session->link.next,
943 struct wlsc_session, link);
944 else if (session->link.prev != &compositor->session_list)
945 primary = container_of(session->link.prev,
946 struct wlsc_session, link);
947 else
948 primary = NULL;
949 }
950
951 wl_list_remove(&session->animate.link);
952 wl_list_remove(&session->link);
953 wl_list_remove(&session->listener.link);
954
955 if (compositor->primary == session) {
956 printf("activating session %p\n", primary);
957 wlsc_session_activate(compositor, primary);
958 }
959}
960
961static void
962wlsc_session_init(struct wlsc_session *session, struct wlsc_surface *surface)
963{
964 struct wlsc_compositor *c = surface->compositor;
965
966 session->animate.animate = wl_session_animate;
967 wl_list_init(&session->animate.link);
968 wl_list_insert(c->session_list.prev, &session->link);
969
970 session->surface = surface;
971 session->current.x = 0;
972 session->current.y = 0;
973 session->current.z = 0;
974 session->current_angle = 0;
975 session->target.x = 0;
976 session->target.y = 0;
977 session->target.z = 0;
978 session->target_angle = 0;
979 session->previous_angle = 0;
980
981 wlsc_session_update_matrix(session);
982
983 session->listener.func = wlsc_session_handle_surface_destroy;
984 wl_list_insert(c->surface_destroy_listener_list.prev,
985 &session->listener.link);
986
987 printf("added session %p [%p, %p]\n", session, session->link.prev, session->link.next);
988 printf(" - prev [%p]\n", session->link.prev->prev);
989}
990
991static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500992compositor_create_surface(struct wl_client *client,
993 struct wl_compositor *compositor, uint32_t id)
994{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500995 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400996 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500997
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400998 surface = malloc(sizeof *surface);
999 if (surface == NULL)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001000 /* FIXME: Send OOM event. */
1001 return;
1002
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001003 wlsc_surface_init(surface, ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001004
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001005 wl_list_insert(ec->surface_list.prev, &surface->link);
1006 wl_client_add_surface(client, &surface->base,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001007 &surface_interface, id);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001008
1009 wlsc_session_init(&surface->session, surface);
1010 wlsc_session_activate(ec, &surface->session);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001011}
1012
1013static void
1014compositor_commit(struct wl_client *client,
1015 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001016{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001017 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001018
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001019 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001020 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001021}
1022
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001023const static struct wl_compositor_interface compositor_interface = {
1024 compositor_create_surface,
1025 compositor_commit
1026};
1027
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001028static void
1029wlsc_surface_transform(struct wlsc_surface *surface,
1030 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1031{
1032 /* Transform to surface coordinates. */
1033 *sx = (x - surface->map.x) * surface->width / surface->map.width;
1034 *sy = (y - surface->map.y) * surface->height / surface->map.height;
1035}
1036
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001037static void
1038wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
1039 struct wlsc_surface *surface)
1040{
1041 if (device->keyboard_focus == surface)
1042 return;
1043
1044 if (device->keyboard_focus &&
1045 (!surface || device->keyboard_focus->base.client != surface->base.client))
1046 wl_surface_post_event(&device->keyboard_focus->base,
1047 &device->base,
Kristian Høgsberg786ca0d2009-03-06 21:25:21 -05001048 WL_INPUT_KEYBOARD_FOCUS, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001049
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001050 if (surface)
1051 wl_surface_post_event(&surface->base,
1052 &device->base,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001053 WL_INPUT_KEYBOARD_FOCUS,
1054 &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001055
1056 device->keyboard_focus = surface;
1057}
1058
1059static void
1060wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
1061 struct wlsc_surface *surface)
1062{
1063 if (device->pointer_focus == surface)
1064 return;
1065
1066 if (device->pointer_focus &&
1067 (!surface || device->pointer_focus->base.client != surface->base.client))
1068 wl_surface_post_event(&device->pointer_focus->base,
1069 &device->base,
1070 WL_INPUT_POINTER_FOCUS, NULL);
1071 if (surface)
1072 wl_surface_post_event(&surface->base,
1073 &device->base,
1074 WL_INPUT_POINTER_FOCUS, &surface->base);
1075
1076 device->pointer_focus = surface;
1077}
1078
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001079static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -05001080pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001081{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001082 struct wlsc_compositor *ec = device->ec;
1083 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001084
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001085 if (device->grab > 0) {
1086 wlsc_surface_transform(device->grab_surface,
1087 device->x, device->y, sx, sy);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001088 return device->grab_surface;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001089 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001090
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001091 es = container_of(ec->surface_list.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001092 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001093 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001094 if (es->map.x <= device->x &&
1095 device->x < es->map.x + es->map.width &&
1096 es->map.y <= device->y &&
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -05001097 device->y < es->map.y + es->map.height) {
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001098 wlsc_surface_transform(es, device->x, device->y, sx, sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001099 return es;
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -05001100 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001101
1102 es = container_of(es->link.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001103 struct wlsc_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -05001104
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001105 }
1106
1107 return NULL;
1108}
1109
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001110void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001111notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001112{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001113 struct wlsc_surface *es;
1114 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001115 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001116 const int hotspot_x = 16, hotspot_y = 16;
1117 int32_t sx, sy;
1118
Ray Strodee96dcb82008-12-20 02:00:49 -05001119 if (!ec->vt_active)
1120 return;
1121
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001122 /* FIXME: We need some multi head love here. */
1123 output = container_of(ec->output_list.next, struct wlsc_output, link);
1124 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -05001125 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001126 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -05001127 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001128 if (x >= output->x + output->width)
1129 x = output->x + output->width - 1;
1130 if (y >= output->y + output->height)
1131 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -05001132
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -05001133 device->x = x;
1134 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -05001135 es = pick_surface(device, &sx, &sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001136
1137 wlsc_input_device_set_pointer_focus(device, es);
1138
Kristian Høgsberg7e972a52008-12-21 17:26:00 -05001139 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001140 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -05001141 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001142
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -05001143 device->sprite->map.x = x - hotspot_x;
1144 device->sprite->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001145
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001146 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001147}
1148
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001149void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001150notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001151 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001152{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001153 struct wlsc_surface *surface;
1154 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001155 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001156
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001157 if (!compositor->vt_active)
Ray Strodee96dcb82008-12-20 02:00:49 -05001158 return;
1159
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001160 surface = pick_surface(device, &sx, &sy);
1161 if (surface) {
1162 wlsc_surface_raise(surface);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -05001163
Kristian Høgsberg29573bc2008-12-11 23:27:27 -05001164 if (state) {
1165 device->grab++;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001166 device->grab_surface = surface;
1167 wlsc_input_device_set_keyboard_focus(device,
1168 surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -05001169 } else {
1170 device->grab--;
1171 }
1172
Kristian Høgsberg5a75c902008-12-10 13:16:50 -05001173 /* FIXME: Swallow click on raise? */
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001174 wl_surface_post_event(&surface->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001175 WL_INPUT_BUTTON, button, state,
1176 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001177
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001178 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001179 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001180}
1181
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001182static void on_term_signal(int signal_number, void *data);
1183
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001184void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001185notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001186 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001187{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001188 struct wlsc_compositor *compositor = device->ec;
1189 struct wlsc_session *s;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001190 uint32_t *k, *end;
1191
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001192 if (!compositor->vt_active)
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001193 return;
Ray Strodee96dcb82008-12-20 02:00:49 -05001194
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001195 switch (key | compositor->meta_state) {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001196 case KEY_EJECTCD | META_DOWN:
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001197 on_term_signal(SIGTERM, compositor);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001198 return;
1199
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001200 case KEY_LEFT | META_DOWN:
1201 if (!compositor->primary || !state)
1202 break;
1203 s = container_of(compositor->primary->link.prev,
1204 struct wlsc_session, link);
1205 if (&s->link != &compositor->session_list) {
1206 wlsc_session_activate(compositor, s);
1207 if (device->grab == 0)
1208 wlsc_input_device_set_keyboard_focus(device, s->surface);
1209 }
1210 return;
1211
1212
1213 case KEY_RIGHT | META_DOWN:
1214 if (!compositor->primary || !state)
1215 break;
1216 s = container_of(compositor->primary->link.next,
1217 struct wlsc_session, link);
1218 if (&s->link != &compositor->session_list) {
1219 wlsc_session_activate(compositor, s);
1220 if (device->grab == 0)
1221 wlsc_input_device_set_keyboard_focus(device, s->surface);
1222
1223 }
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001224 return;
1225
1226 case KEY_LEFTMETA:
1227 case KEY_RIGHTMETA:
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001228 case KEY_LEFTMETA | META_DOWN:
1229 case KEY_RIGHTMETA | META_DOWN:
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001230 compositor->meta_state = state ? META_DOWN : 0;
1231
1232 if (state == 0 && compositor->primary) {
1233 s = compositor->primary;
1234 s->target.z = 0;
1235 wl_list_remove(&s->animate.link);
1236 wl_list_insert(compositor->animate_list.prev, &s->animate.link);
1237 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001238 }
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001239
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001240 return;
1241 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001242
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001243 end = device->keys.data + device->keys.size;
1244 for (k = device->keys.data; k < end; k++) {
1245 if (*k == key)
1246 *k = *--end;
1247 }
1248 device->keys.size = (void *) end - device->keys.data;
1249 if (state) {
1250 k = wl_array_add(&device->keys, sizeof *k);
1251 *k = key;
1252 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001253
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001254 if (device->keyboard_focus != NULL)
1255 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -05001256 &device->base,
1257 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001258}
1259
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001260struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001261evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001262 struct wl_display *display, const char *path);
1263
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001264static void
1265handle_surface_destroy(struct wlsc_listener *listener,
1266 struct wlsc_surface *surface)
1267{
1268 struct wlsc_input_device *device =
1269 container_of(listener, struct wlsc_input_device, listener);
1270
1271 if (device->grab_surface == surface) {
1272 device->grab_surface = NULL;
1273 device->grab = 0;
1274 }
1275 if (device->keyboard_focus == surface)
1276 device->keyboard_focus = NULL;
1277 if (device->pointer_focus == surface)
1278 device->pointer_focus = NULL;
1279}
1280
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001281static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001282create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001283{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001284 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001285
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001286 device = malloc(sizeof *device);
1287 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001288 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001289
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001290 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001291 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001292 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001293 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001294 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001295 device->x = 100;
1296 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001297 device->ec = ec;
1298
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001299 device->listener.func = handle_surface_destroy;
1300 wl_list_insert(ec->surface_destroy_listener_list.prev,
1301 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001302 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001303
1304 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001305}
1306
1307void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001308wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001309{
1310 *x = device->x;
1311 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001312}
1313
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001314static void
1315post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001316{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001317 struct wlsc_output *output =
1318 container_of(global, struct wlsc_output, base);
1319
1320 wl_client_post_event(client, global,
1321 WL_OUTPUT_GEOMETRY,
1322 output->width, output->height);
1323}
1324
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001325static void
1326on_drm_input(int fd, uint32_t mask, void *data)
1327{
1328 drmEventContext evctx;
1329
1330 memset(&evctx, 0, sizeof evctx);
1331 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1332 evctx.page_flip_handler = page_flip_handler;
1333 drmHandleEvent(fd, &evctx);
1334}
1335
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001336static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001337init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001338{
1339 static const EGLint config_attribs[] = {
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001340 EGL_DEPTH_SIZE, 0,
1341 EGL_STENCIL_SIZE, 0,
1342 EGL_CONFIG_CAVEAT, EGL_NONE,
1343 EGL_RED_SIZE, 8,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001344 EGL_NONE
1345 };
1346
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001347 struct wl_event_loop *loop;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001348 EGLint major, minor;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001349 int fd;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001350
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -05001351 ec->display = eglCreateDisplayNative(device);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001352 if (ec->display == NULL) {
1353 fprintf(stderr, "failed to create display\n");
1354 return -1;
1355 }
1356
1357 if (!eglInitialize(ec->display, &major, &minor)) {
1358 fprintf(stderr, "failed to initialize display\n");
1359 return -1;
1360 }
1361
1362 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
1363 return -1;
1364
1365 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
1366 if (ec->context == NULL) {
1367 fprintf(stderr, "failed to create context\n");
1368 return -1;
1369 }
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001370
1371 loop = wl_display_get_event_loop(ec->wl_display);
1372 fd = eglGetDisplayFD(ec->display);
1373 ec->drm_source =
1374 wl_event_loop_add_fd(loop, fd,
1375 WL_EVENT_READABLE, on_drm_input, ec);
1376
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001377 return 0;
1378}
1379
1380static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001381create_output(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001382{
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001383 drmModeConnector *connector;
1384 drmModeRes *resources;
1385 drmModeEncoder *encoder;
Kristian Høgsberg41a10682009-02-15 22:37:03 -05001386 drmModeModeInfo *mode;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001387 struct wlsc_output *output;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001388 uint32_t name, handle, stride;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001389 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001390
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001391 if (ec->display == NULL && init_egl(ec, device) < 0) {
1392 fprintf(stderr, "failed to initialize egl\n");
1393 return -1;
1394 }
1395
1396 output = malloc(sizeof *output);
1397 if (output == NULL)
1398 return -1;
1399
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001400 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001401 resources = drmModeGetResources(fd);
1402 if (!resources) {
1403 fprintf(stderr, "drmModeGetResources failed\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001404 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001405 }
1406
1407 for (i = 0; i < resources->count_connectors; i++) {
1408 connector = drmModeGetConnector(fd, resources->connectors[i]);
1409 if (connector == NULL)
1410 continue;
1411
1412 if (connector->connection == DRM_MODE_CONNECTED &&
1413 connector->count_modes > 0)
1414 break;
1415
1416 drmModeFreeConnector(connector);
1417 }
1418
1419 if (i == resources->count_connectors) {
1420 fprintf(stderr, "No currently active connector found.\n");
1421 return -1;
1422 }
1423
1424 mode = &connector->modes[0];
1425
1426 for (i = 0; i < resources->count_encoders; i++) {
1427 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
1428
1429 if (encoder == NULL)
1430 continue;
1431
1432 if (encoder->encoder_id == connector->encoder_id)
1433 break;
1434
1435 drmModeFreeEncoder(encoder);
1436 }
1437
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001438 output->compositor = ec;
1439 output->crtc_id = encoder->crtc_id;
1440 output->connector_id = connector->connector_id;
1441 output->mode = mode;
1442 output->x = 0;
1443 output->y = 0;
1444 output->width = mode->hdisplay;
1445 output->height = mode->vdisplay;
1446
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001447 output->surface = eglCreateSurface(ec->display,
1448 ec->config,
1449 output->width,
1450 output->height,
1451 2, NULL);
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001452 if (output->surface == NULL) {
1453 fprintf(stderr, "failed to create surface\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001454 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001455 }
1456
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001457 for (i = 0; i < 2; i++) {
1458 eglGetColorBuffer(output->surface,
1459 i, &name, &handle, &stride);
1460
1461 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
1462 32, 32, stride, handle, &output->fb_id[i]);
1463 if (ret) {
1464 fprintf(stderr, "failed to add fb %d: %m\n", i);
1465 return -1;
1466 }
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001467 }
1468
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001469 output->current = 0;
1470 ret = drmModeSetCrtc(fd, encoder->crtc_id,
1471 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001472 &connector->connector_id, 1, mode);
1473 if (ret) {
1474 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001475 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001476 }
1477
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001478 output->base.interface = &wl_output_interface;
1479 wl_display_add_object(ec->wl_display, &output->base);
1480 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
1481 wl_list_insert(ec->output_list.prev, &output->link);
1482
1483 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
1484 fprintf(stderr, "failed to make context current\n");
1485 return -1;
1486 }
1487
1488 output->background = background_create(output, option_background);
1489
1490 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001491}
1492
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001493static const struct wl_interface visual_interface = {
1494 "visual", 1,
1495};
1496
1497static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001498add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001499{
1500 ec->argb_visual.base.interface = &visual_interface;
1501 ec->argb_visual.base.implementation = NULL;
1502 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001503 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001504
1505 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1506 ec->premultiplied_argb_visual.base.implementation = NULL;
1507 wl_display_add_object(ec->wl_display,
1508 &ec->premultiplied_argb_visual.base);
1509 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001510 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001511
1512 ec->rgb_visual.base.interface = &visual_interface;
1513 ec->rgb_visual.base.implementation = NULL;
1514 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001515 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1516}
1517
Ray Strode19ad6a92008-12-19 01:45:41 -05001518static void on_enter_vt(int signal_number, void *data)
1519{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001520 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001521 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001522 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -05001523
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001524 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001525 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001526
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001527 fd = eglGetDisplayFD(ec->display);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001528 output = container_of(ec->output_list.next, struct wlsc_output, link);
1529 while (&output->link != &ec->output_list) {
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001530 ret = drmModeSetCrtc(fd, output->crtc_id,
1531 output->fb_id[output->current], 0, 0,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001532 &output->connector_id, 1, output->mode);
1533 if (ret)
1534 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1535 output->connector_id);
1536
1537 output = container_of(output->link.next,
1538 struct wlsc_output, link);
Ray Strode19ad6a92008-12-19 01:45:41 -05001539 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001540}
1541
1542static void on_leave_vt(int signal_number, void *data)
1543{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001544 struct wlsc_compositor *ec = data;
Ray Strode19ad6a92008-12-19 01:45:41 -05001545
1546 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001547 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001548}
1549
Ray Strode966aa112008-12-19 14:28:02 -05001550static void
1551on_tty_input(int fd, uint32_t mask, void *data)
1552{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001553 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001554
1555 /* Ignore input to tty. We get keyboard events from evdev
1556 */
1557 tcflush(ec->tty_fd, TCIFLUSH);
1558}
1559
1560static void on_term_signal(int signal_number, 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 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1565 fprintf(stderr, "could not restore terminal to canonical mode\n");
1566
1567 exit(0);
1568}
1569
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001570static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001571{
1572 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001573 struct vt_mode mode = { 0 };
1574
1575 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1576 if (ec->tty_fd <= 0) {
1577 fprintf(stderr, "failed to open active tty: %m\n");
1578 return -1;
1579 }
Ray Strode966aa112008-12-19 14:28:02 -05001580
1581 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1582 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001583 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001584 }
1585
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001586 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001587 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001588 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001589
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001590 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001591 raw_attributes.c_oflag |= OPOST | OCRNL;
1592
1593 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1594 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1595
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001596 ec->term_signal_source =
1597 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001598
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001599 ec->tty_input_source =
1600 wl_event_loop_add_fd(loop, ec->tty_fd,
1601 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001602
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001603 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001604 mode.mode = VT_PROCESS;
1605 mode.relsig = SIGUSR1;
1606 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001607 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001608 fprintf(stderr, "failed to take control of vt handling\n");
1609 }
1610
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001611 ec->leave_vt_source =
1612 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1613 ec->enter_vt_source =
1614 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001615
1616 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001617}
1618
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001619static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001620init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001621{
1622 struct udev_enumerate *e;
1623 struct udev_list_entry *entry;
1624 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001625 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001626 struct wlsc_input_device *input_device;
1627
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001628 ec->udev = udev_new();
1629 if (ec->udev == NULL) {
1630 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001631 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001632 }
1633
1634 input_device = create_input_device(ec);
1635
1636 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001637 udev_enumerate_add_match_subsystem(e, "input");
1638 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001639 udev_enumerate_scan_devices(e);
1640 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1641 path = udev_list_entry_get_name(entry);
1642 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001643 evdev_input_device_create(input_device, ec->wl_display,
1644 udev_device_get_devnode(device));
1645 }
1646 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001647
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001648 e = udev_enumerate_new(ec->udev);
1649 udev_enumerate_add_match_subsystem(e, "drm");
1650 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1651 udev_enumerate_scan_devices(e);
1652 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1653 path = udev_list_entry_get_name(entry);
1654 device = udev_device_new_from_syspath(ec->udev, path);
1655 if (create_output(ec, device) < 0) {
1656 fprintf(stderr, "failed to create output for %s\n", path);
1657 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001658 }
1659 }
1660 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001661
1662 /* Create the pointer surface now that we have a current EGL context. */
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -05001663 input_device->sprite =
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001664 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1665
1666 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001667}
1668
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001669static struct wlsc_compositor *
1670wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001671{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001672 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001673 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001674 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001675
1676 ec = malloc(sizeof *ec);
1677 if (ec == NULL)
1678 return NULL;
1679
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001680 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001681 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001682
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001683 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001684
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001685 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001686
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001687 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001688 wl_list_init(&ec->input_device_list);
1689 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001690 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001691 if (init_libudev(ec) < 0) {
1692 fprintf(stderr, "failed to initialize devices\n");
1693 return NULL;
1694 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001695
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001696 shooter = screenshooter_create(ec);
1697 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001698 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001699
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001700 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001701
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001702 setup_tty(ec, loop);
1703
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001704 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001705 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001706
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001707 wl_list_init(&ec->animate_list);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001708 wl_list_init(&ec->session_list);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001709
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001710 return ec;
1711}
1712
1713/* The plan here is to generate a random anonymous socket name and
1714 * advertise that through a service on the session dbus.
1715 */
1716static const char socket_name[] = "\0wayland";
1717
1718int main(int argc, char *argv[])
1719{
1720 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001721 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001722 GError *error = NULL;
1723 GOptionContext *context;
1724
1725 context = g_option_context_new(NULL);
1726 g_option_context_add_main_entries(context, option_entries, "Wayland");
1727 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1728 fprintf(stderr, "option parsing failed: %s\n", error->message);
1729 exit(EXIT_FAILURE);
1730 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001731
1732 display = wl_display_create();
1733
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001734 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001735 if (ec == NULL) {
1736 fprintf(stderr, "failed to create compositor\n");
1737 exit(EXIT_FAILURE);
1738 }
1739
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001740 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001741 fprintf(stderr, "failed to add socket: %m\n");
1742 exit(EXIT_FAILURE);
1743 }
1744
1745 wl_display_run(display);
1746
1747 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001748}