blob: 3b1c2032a0e74e27958098ded6cd49beadda152d [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 <i915_drm.h>
26#include <sys/ioctl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040027#include <fcntl.h>
28#include <unistd.h>
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050029#include <cairo.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050030#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050031#include <math.h>
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -050032#include <linux/input.h>
Ray Strode19ad6a92008-12-19 01:45:41 -050033#include <linux/vt.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;
72 int32_t x, y, width, height, stride;
73
Kristian Høgsberg41a10682009-02-15 22:37:03 -050074 drmModeModeInfo *mode;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050075 uint32_t fb_id;
76 uint32_t crtc_id;
77 uint32_t connector_id;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050078};
79
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050080struct wlsc_input_device {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050081 struct wl_object base;
82 int32_t x, y;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050083 struct wlsc_compositor *ec;
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -050084 struct wlsc_surface *sprite;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050085 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050086
87 int grab;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050088 struct wlsc_surface *grab_surface;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -050089 struct wlsc_surface *pointer_focus;
90 struct wlsc_surface *keyboard_focus;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050091 struct wl_array keys;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -040092
93 struct wlsc_listener listener;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050094};
95
Kristian Høgsberg8e438622009-01-26 23:07:00 -050096struct wlsc_compositor {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040097 struct wl_compositor base;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050098 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
99
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400100 EGLDisplay display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400101 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500102 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400103 struct wl_display *wl_display;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500104
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500105 struct wl_list output_list;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500106 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500107 struct wl_list surface_list;
108
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400109 struct wl_list surface_destroy_listener_list;
110
Ray Strode966aa112008-12-19 14:28:02 -0500111 struct wl_event_source *term_signal_source;
112
113 /* tty handling state */
114 int tty_fd;
Ray Strodee96dcb82008-12-20 02:00:49 -0500115 uint32_t vt_active : 1;
Ray Strode966aa112008-12-19 14:28:02 -0500116
117 struct termios terminal_attributes;
118 struct wl_event_source *tty_input_source;
Ray Strode19ad6a92008-12-19 01:45:41 -0500119 struct wl_event_source *enter_vt_source;
120 struct wl_event_source *leave_vt_source;
121
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500122 struct udev *udev;
123
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500124 /* Repaint state. */
125 struct wl_event_source *timer_source;
126 int repaint_needed;
127 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500128 struct timespec previous_swap;
129 uint32_t current_frame;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500130
131 uint32_t meta_state;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500132 struct wl_list animate_list;
133 struct wlsc_surface *primary;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400134};
135
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500136#define META_DOWN 256
137
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500138struct wlsc_animate {
139 struct wl_list link;
140 void (*animate)(struct wlsc_animate *animate,
141 struct wlsc_compositor *compositor,
142 uint32_t frame, uint32_t msecs);
143};
144
145struct wlsc_vector {
146 GLdouble x, y, z;
147};
148
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500149struct wlsc_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500150 struct wl_surface base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500151 struct wlsc_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500152 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400153 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400154 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500155 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500156 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500157 struct wl_list link;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500158 struct wlsc_matrix matrix;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500159
160 struct wlsc_vector target, current, previous;
161 GLdouble target_angle, current_angle, previous_angle;
162 struct wlsc_animate animate;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400163};
164
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500165static const char *option_background = "background.jpg";
166
167static const GOptionEntry option_entries[] = {
168 { "background", 'b', 0, G_OPTION_ARG_STRING,
169 &option_background, "Background image" },
170 { NULL }
171};
172
173
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500174struct screenshooter {
175 struct wl_object base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500176 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500177};
178
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500179struct screenshooter_interface {
180 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
181};
182
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500183static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500184screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500185{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500186 struct wlsc_compositor *ec = shooter->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500187 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500188 char buffer[256];
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500189 GdkPixbuf *pixbuf, *normal;
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500190 GError *error = NULL;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500191 unsigned char *data;
192 int i, j;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500193
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500194 i = 0;
195 output = container_of(ec->output_list.next, struct wlsc_output, link);
196 while (&output->link != &ec->output_list) {
197 snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500198 data = malloc(output->width * output->height * 4);
199 if (data == NULL) {
200 fprintf(stderr, "couldn't allocate image buffer\n");
201 continue;
202 }
203
204 glReadBuffer(GL_FRONT);
205 glPixelStorei(GL_PACK_ALIGNMENT, 1);
206 glReadPixels(0, 0, output->width, output->height,
207 GL_RGBA, GL_UNSIGNED_BYTE, data);
208
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500209 /* FIXME: We should just use a RGB visual for the frontbuffer. */
210 for (j = 3; j < output->width * output->height * 4; j += 4)
211 data[j] = 0xff;
212
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500213 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500214 8, output->width, output->height, output->width * 4,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500215 NULL, NULL);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500216 normal = gdk_pixbuf_flip(pixbuf, FALSE);
217 gdk_pixbuf_save(normal, buffer, "png", &error, NULL);
218 gdk_pixbuf_unref(normal);
219 gdk_pixbuf_unref(pixbuf);
220 free(data);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500221
222 output = container_of(output->link.next,
223 struct wlsc_output, link);
224 }
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500225}
226
Kristian Høgsbergfb6d68d2008-12-21 21:54:51 -0500227static const struct wl_message screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500228 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500229};
230
231static const struct wl_interface screenshooter_interface = {
232 "screenshooter", 1,
233 ARRAY_LENGTH(screenshooter_methods),
234 screenshooter_methods,
235};
236
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500237struct screenshooter_interface screenshooter_implementation = {
238 screenshooter_shoot
239};
240
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500241static struct screenshooter *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500242screenshooter_create(struct wlsc_compositor *ec)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500243{
244 struct screenshooter *shooter;
245
246 shooter = malloc(sizeof *shooter);
247 if (shooter == NULL)
248 return NULL;
249
250 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500251 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500252 shooter->ec = ec;
253
254 return shooter;
255};
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500256
257static void
258wlsc_matrix_init(struct wlsc_matrix *matrix)
259{
260 static const struct wlsc_matrix identity = {
261 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
262 };
263
264 memcpy(matrix, &identity, sizeof identity);
265}
266
267static void
268wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
269{
270 struct wlsc_matrix tmp;
271 const GLdouble *row, *column;
272 div_t d;
273 int i, j;
274
275 for (i = 0; i < 16; i++) {
276 tmp.d[i] = 0;
277 d = div(i, 4);
278 row = m->d + d.quot * 4;
279 column = n->d + d.rem;
280 for (j = 0; j < 4; j++)
281 tmp.d[i] += row[j] * column[j * 4];
282 }
283 memcpy(m, &tmp, sizeof tmp);
284}
285
286static void
287wlsc_matrix_translate(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
288{
289 struct wlsc_matrix translate = {
290 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
291 };
292
293 wlsc_matrix_multiply(matrix, &translate);
294}
295
296static void
297wlsc_matrix_scale(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
298{
299 struct wlsc_matrix scale = {
300 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
301 };
302
303 wlsc_matrix_multiply(matrix, &scale);
304}
305
306static void
307wlsc_matrix_rotate(struct wlsc_matrix *matrix,
308 GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
309{
310 GLdouble c = cos(angle);
311 GLdouble s = sin(angle);
312 struct wlsc_matrix rotate = {
313 { x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
314 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) - x * s, 0,
315 x * z * (1 - c) + y * x, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
316 0, 0, 0, 1 }
317 };
318
319 wlsc_matrix_multiply(matrix, &rotate);
320}
321
322static void
323wlsc_surface_update_matrix(struct wlsc_surface *es)
324{
325 GLdouble tx, ty;
326
327 tx = es->map.x + es->map.width / 2;
328 ty = es->map.y + es->map.height / 2;
329
330 wlsc_matrix_init(&es->matrix);
331 wlsc_matrix_translate(&es->matrix, -tx, -ty, 0);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500332 wlsc_matrix_rotate(&es->matrix, es->current_angle, 0, 1, 0);
333 wlsc_matrix_translate(&es->matrix, tx + es->current.x,
334 ty + es->current.y, es->current.z);
335}
336
337static void
338wlsc_surface_init(struct wlsc_surface *surface,
339 struct wlsc_compositor *compositor, struct wl_visual *visual,
340 int32_t x, int32_t y, int32_t width, int32_t height)
341{
342 glGenTextures(1, &surface->texture);
343 surface->compositor = compositor;
344 surface->map.x = x;
345 surface->map.y = y;
346 surface->map.width = width;
347 surface->map.height = height;
348 surface->surface = EGL_NO_SURFACE;
349 surface->visual = visual;
350 surface->current.x = 0;
351 surface->current.y = 0;
352 surface->current.z = 0;
353 surface->current_angle = 0;
354 surface->target.x = 0;
355 surface->target.y = 0;
356 surface->target.z = 0;
357 surface->target_angle = 0;
358 surface->previous_angle = 0;
359
360 wlsc_surface_update_matrix(surface);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500361}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500362
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500363static struct wlsc_surface *
364wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500365 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500366 int x, int y, int width, int height)
367{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500368 struct wlsc_surface *es;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500369 int stride;
370 void *data;
371
372 stride = cairo_image_surface_get_stride(surface);
373 data = cairo_image_surface_get_data(surface);
374
375 es = malloc(sizeof *es);
376 if (es == NULL)
377 return NULL;
378
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500379 wlsc_surface_init(es, ec, &ec->premultiplied_argb_visual,
380 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500381 glBindTexture(GL_TEXTURE_2D, es->texture);
382 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500383 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
384 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
385 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500386 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
387 GL_BGRA, GL_UNSIGNED_BYTE, data);
388
Kristian Høgsberg54879822008-11-23 17:07:32 -0500389 return es;
390}
391
392static void
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400393wlsc_surface_destroy(struct wlsc_surface *surface,
394 struct wlsc_compositor *compositor)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500395{
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400396 struct wlsc_listener *l;
397
398 l = container_of(compositor->surface_destroy_listener_list.next,
399 struct wlsc_listener, link);
400 while (&l->link != &compositor->surface_destroy_listener_list) {
401 l->func(l, surface);
402 l = container_of(l->link.next, struct wlsc_listener, link);
403 }
404
405 wl_list_remove(&surface->link);
406 wl_list_remove(&surface->animate.link);
407
408 glDeleteTextures(1, &surface->texture);
409 if (surface->surface != EGL_NO_SURFACE)
410 eglDestroySurface(compositor->display, surface->surface);
411 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500412}
413
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400414static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500415pointer_path(cairo_t *cr, int x, int y)
416{
417 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
418 const int width = 16, height = 16;
419
420 cairo_move_to(cr, x, y);
421 cairo_line_to(cr, x + tx, y + ty);
422 cairo_line_to(cr, x + dx, y + dy);
423 cairo_line_to(cr, x + width - end, y + height);
424 cairo_line_to(cr, x + width, y + height - end);
425 cairo_line_to(cr, x + dy, y + dx);
426 cairo_line_to(cr, x + ty, y + tx);
427 cairo_close_path(cr);
428}
429
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500430static struct wlsc_surface *
431pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500432{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500433 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500434 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500435 cairo_surface_t *surface;
436 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500437
438 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
439 width, height);
440
441 cr = cairo_create(surface);
442 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500443 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500444 cairo_set_source_rgb(cr, 0, 0, 0);
445 cairo_stroke_preserve(cr);
446 cairo_fill(cr);
447 blur_surface(surface, width);
448
449 pointer_path(cr, hotspot_x, hotspot_y);
450 cairo_stroke_preserve(cr);
451 cairo_set_source_rgb(cr, 1, 1, 1);
452 cairo_fill(cr);
453 cairo_destroy(cr);
454
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500455 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500456 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500457 x - hotspot_x,
458 y - hotspot_y,
459 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500460
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500461 cairo_surface_destroy(surface);
462
Kristian Høgsberg54879822008-11-23 17:07:32 -0500463 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500464}
465
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500466static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500467background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500468{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500469 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500470 GdkPixbuf *pixbuf;
471 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500472 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500473 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500474
475 background = malloc(sizeof *background);
476 if (background == NULL)
477 return NULL;
478
479 g_type_init();
480
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500481 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500482 output->width,
483 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500484 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500485 if (error != NULL) {
486 free(background);
487 return NULL;
488 }
489
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500490 data = gdk_pixbuf_get_pixels(pixbuf);
491
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500492 wlsc_surface_init(background, output->compositor,
493 &output->compositor->rgb_visual,
494 output->x, output->y, output->width, output->height);
495
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500496 glBindTexture(GL_TEXTURE_2D, background->texture);
497 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500498 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500499 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
500 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500501
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500502 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500503 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500504 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500505 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500506
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500507 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
508 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500509 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500510
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500511 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500512}
513
514static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500515wlsc_surface_draw(struct wlsc_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500516{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500517 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500518 GLint vertices[12];
519 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
520 GLuint indices[4] = { 0, 1, 2, 3 };
521
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500522 vertices[0] = es->map.x;
523 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500524 vertices[2] = 0;
525
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500526 vertices[3] = es->map.x;
527 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500528 vertices[5] = 0;
529
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500530 vertices[6] = es->map.x + es->map.width;
531 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500532 vertices[8] = 0;
533
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500534 vertices[9] = es->map.x + es->map.width;
535 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500536 vertices[11] = 0;
537
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500538 if (es->visual == &ec->argb_visual) {
539 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
540 glEnable(GL_BLEND);
541 } else if (es->visual == &ec->premultiplied_argb_visual) {
542 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
543 glEnable(GL_BLEND);
544 } else {
545 glDisable(GL_BLEND);
546 }
547
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500548 glPushMatrix();
549 glMultMatrixd(es->matrix.d);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500550 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500551 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500552 glEnableClientState(GL_VERTEX_ARRAY);
553 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
554 glVertexPointer(3, GL_INT, 0, vertices);
555 glTexCoordPointer(2, GL_INT, 0, tex_coords);
556 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500557 glPopMatrix();
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500558}
559
560static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500561wlsc_vector_add(struct wlsc_vector *v1, struct wlsc_vector *v2)
562{
563 v1->x += v2->x;
564 v1->y += v2->y;
565 v1->z += v2->z;
566}
567
568static void
569wlsc_vector_subtract(struct wlsc_vector *v1, struct wlsc_vector *v2)
570{
571 v1->x -= v2->x;
572 v1->y -= v2->y;
573 v1->z -= v2->z;
574}
575
576static void
577wlsc_vector_scalar(struct wlsc_vector *v1, GLdouble s)
578{
579 v1->x *= s;
580 v1->y *= s;
581 v1->z *= s;
582}
583
584static void
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500585repaint_output(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400586{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500587 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500588 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500589 struct wlsc_input_device *eid;
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500590 double s = 3000;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500591
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500592 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
593 fprintf(stderr, "failed to make context current\n");
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500594 return;
595 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500596
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500597 glViewport(0, 0, output->width, output->height);
598 glMatrixMode(GL_PROJECTION);
599 glLoadIdentity();
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500600 glFrustum(-output->width / s, output->width / s,
601 output->height / s, -output->height / s, 1, 2 * s);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500602 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500603 glLoadIdentity();
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500604 glClearColor(0, 0, 0.2, 1);
605
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500606 glTranslatef(-output->width / 2, -output->height / 2, -s / 2);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500607
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500608 if (output->background)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500609 wlsc_surface_draw(output->background);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500610 else
611 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400612
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500613 es = container_of(ec->surface_list.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500614 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500615 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500616 wlsc_surface_draw(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500617 es = container_of(es->link.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500618 struct wlsc_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400619 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400620
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500621 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500622 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500623 while (&eid->link != &ec->input_device_list) {
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500624 wlsc_surface_draw(eid->sprite);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500625
626 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500627 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500628 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500629
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500630 eglSwapBuffers(ec->display, output->surface);
631}
632
633static void
634repaint(void *data)
635{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500636 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500637 struct wlsc_output *output;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500638 struct wlsc_animate *animate, *next;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500639 struct timespec ts;
640 uint32_t msecs;
641
642 if (!ec->repaint_needed) {
643 ec->repaint_on_timeout = 0;
644 return;
645 }
646
647 output = container_of(ec->output_list.next, struct wlsc_output, link);
648 while (&output->link != &ec->output_list) {
649 repaint_output(output);
650 output = container_of(output->link.next,
651 struct wlsc_output, link);
652 }
653
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500654 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500655
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500656 clock_gettime(CLOCK_MONOTONIC, &ts);
657 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500658 wl_display_post_frame(ec->wl_display, &ec->base,
659 ec->current_frame, msecs);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500660
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500661 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500662 ec->repaint_on_timeout = 1;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500663
664 animate = container_of(ec->animate_list.next, struct wlsc_animate, link);
665 while (&animate->link != &ec->animate_list) {
666 next = container_of(animate->link.next,
667 struct wlsc_animate, link);
668 animate->animate(animate, ec, ec->current_frame, msecs);
669 animate = next;
670 }
671
672 ec->current_frame++;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400673}
674
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500675static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500676schedule_repaint(struct wlsc_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400677{
678 struct wl_event_loop *loop;
679
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500680 ec->repaint_needed = 1;
681 if (!ec->repaint_on_timeout) {
682 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500683 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500684 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400685}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500686
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500687static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500688surface_destroy(struct wl_client *client,
689 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400690{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500691 struct wlsc_surface *es = (struct wlsc_surface *) surface;
692 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500693
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500694 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400695
696 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400697}
698
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500699static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500700surface_attach(struct wl_client *client,
701 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500702 uint32_t width, uint32_t height, uint32_t stride,
703 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400704{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500705 struct wlsc_surface *es = (struct wlsc_surface *) surface;
706 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400707
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500708 if (es->surface != EGL_NO_SURFACE)
709 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400710
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500711 es->width = width;
712 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500713 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500714 name, width, height, stride, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500715 if (visual == &ec->argb_visual.base)
716 es->visual = &ec->argb_visual;
717 else if (visual == &ec->premultiplied_argb_visual.base)
718 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500719 else if (visual == &ec->rgb_visual.base)
720 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500721 else
722 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400723
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500724 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400725 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500726 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
727 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
728 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500729 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400730}
731
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500732static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500733surface_map(struct wl_client *client,
734 struct wl_surface *surface,
735 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400736{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500737 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400738
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500739 es->map.x = x;
740 es->map.y = y;
741 es->map.width = width;
742 es->map.height = height;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500743 wlsc_surface_update_matrix(es);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400744}
745
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500746static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500747surface_copy(struct wl_client *client,
748 struct wl_surface *surface,
749 int32_t dst_x, int32_t dst_y,
750 uint32_t name, uint32_t stride,
751 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500752{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500753 struct wlsc_surface *es = (struct wlsc_surface *) surface;
754 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500755 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500756
757 /* FIXME: glCopyPixels should work, but then we'll have to
758 * call eglMakeCurrent to set up the src and dest surfaces
759 * first. This seems cheaper, but maybe there's a better way
760 * to accomplish this. */
761
762 src = eglCreateSurfaceForName(ec->display, ec->config,
763 name, x + width, y + height, stride, NULL);
764
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500765 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500766 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500767 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500768}
769
770static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500771surface_damage(struct wl_client *client,
772 struct wl_surface *surface,
773 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500774{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500775 /* FIXME: This need to take a damage region, of course. */
776}
777
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500778const static struct wl_surface_interface surface_interface = {
779 surface_destroy,
780 surface_attach,
781 surface_map,
782 surface_copy,
783 surface_damage
784};
785
786static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500787animate_surface(struct wlsc_animate *animate,
788 struct wlsc_compositor *compositor,
789 uint32_t frame, uint32_t msecs)
790{
791 struct wlsc_surface *s;
792 double angle_force, angle;
793 struct wlsc_vector force, tmp;
794 double step = 0.3;
795 double friction = 1;
796 double spring = 0.2;
797
798 s = container_of(animate, struct wlsc_surface, animate);
799
800 angle = s->current_angle;
801 angle_force = (s->target_angle - angle) * spring +
802 (s->previous_angle - angle) * friction;
803
804 s->current_angle = angle + (angle - s->previous_angle) + angle_force * step;
805 s->previous_angle = angle;
806
807 force = s->target;
808 wlsc_vector_subtract(&force, &s->current);
809 wlsc_vector_scalar(&force, spring);
810 tmp = s->previous;
811 wlsc_vector_subtract(&tmp, &s->current);
812 wlsc_vector_scalar(&tmp, friction);
813 wlsc_vector_add(&force, &tmp);
814
815 wlsc_vector_scalar(&force, step);
816 wlsc_vector_add(&force, &s->current);
817 wlsc_vector_subtract(&force, &s->previous);
818 s->previous = s->current;
819 wlsc_vector_add(&s->current, &force);
820
821 wlsc_surface_update_matrix(s);
822
823 tmp = s->current;
824 wlsc_vector_subtract(&tmp, &s->target);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500825 if (tmp.x * tmp.x + tmp.y * tmp.y + tmp.z * tmp.z > 0.001) {
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500826 schedule_repaint(compositor);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500827 } else {
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500828 wl_list_remove(&s->animate.link);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500829 wl_list_init(&s->animate.link);
830 }
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500831}
832
833static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500834compositor_create_surface(struct wl_client *client,
835 struct wl_compositor *compositor, uint32_t id)
836{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500837 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
838 struct wlsc_surface *es;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500839
840 es = malloc(sizeof *es);
841 if (es == NULL)
842 /* FIXME: Send OOM event. */
843 return;
844
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500845 wlsc_surface_init(es, ec, NULL, 0, 0, 0, 0);
846 es->animate.animate = animate_surface;
847 wl_list_init(&es->animate.link);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500848
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500849 wl_list_insert(ec->surface_list.prev, &es->link);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500850 wl_client_add_surface(client, &es->base,
851 &surface_interface, id);
852}
853
854static void
855compositor_commit(struct wl_client *client,
856 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500857{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500858 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500859
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500860 schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500861 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500862}
863
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500864const static struct wl_compositor_interface compositor_interface = {
865 compositor_create_surface,
866 compositor_commit
867};
868
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500869static void
870wlsc_surface_transform(struct wlsc_surface *surface,
871 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
872{
873 /* Transform to surface coordinates. */
874 *sx = (x - surface->map.x) * surface->width / surface->map.width;
875 *sy = (y - surface->map.y) * surface->height / surface->map.height;
876}
877
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500878static void
879wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
880 struct wlsc_surface *surface)
881{
882 if (device->keyboard_focus == surface)
883 return;
884
885 if (device->keyboard_focus &&
886 (!surface || device->keyboard_focus->base.client != surface->base.client))
887 wl_surface_post_event(&device->keyboard_focus->base,
888 &device->base,
Kristian Høgsberg786ca0d2009-03-06 21:25:21 -0500889 WL_INPUT_KEYBOARD_FOCUS, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500890
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500891 if (surface)
892 wl_surface_post_event(&surface->base,
893 &device->base,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500894 WL_INPUT_KEYBOARD_FOCUS,
895 &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500896
897 device->keyboard_focus = surface;
898}
899
900static void
901wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
902 struct wlsc_surface *surface)
903{
904 if (device->pointer_focus == surface)
905 return;
906
907 if (device->pointer_focus &&
908 (!surface || device->pointer_focus->base.client != surface->base.client))
909 wl_surface_post_event(&device->pointer_focus->base,
910 &device->base,
911 WL_INPUT_POINTER_FOCUS, NULL);
912 if (surface)
913 wl_surface_post_event(&surface->base,
914 &device->base,
915 WL_INPUT_POINTER_FOCUS, &surface->base);
916
917 device->pointer_focus = surface;
918}
919
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500920static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500921pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500922{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500923 struct wlsc_compositor *ec = device->ec;
924 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500925
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500926 if (device->grab > 0) {
927 wlsc_surface_transform(device->grab_surface,
928 device->x, device->y, sx, sy);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500929 return device->grab_surface;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500930 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500931
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500932 es = container_of(ec->surface_list.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500933 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500934 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500935 if (es->map.x <= device->x &&
936 device->x < es->map.x + es->map.width &&
937 es->map.y <= device->y &&
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500938 device->y < es->map.y + es->map.height) {
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500939 wlsc_surface_transform(es, device->x, device->y, sx, sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500940 return es;
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500941 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500942
943 es = container_of(es->link.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500944 struct wlsc_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500945
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500946 }
947
948 return NULL;
949}
950
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500951void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500952notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500953{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500954 struct wlsc_surface *es;
955 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500956 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500957 const int hotspot_x = 16, hotspot_y = 16;
958 int32_t sx, sy;
959
Ray Strodee96dcb82008-12-20 02:00:49 -0500960 if (!ec->vt_active)
961 return;
962
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500963 /* FIXME: We need some multi head love here. */
964 output = container_of(ec->output_list.next, struct wlsc_output, link);
965 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500966 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500967 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500968 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500969 if (x >= output->x + output->width)
970 x = output->x + output->width - 1;
971 if (y >= output->y + output->height)
972 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500973
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500974 device->x = x;
975 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500976 es = pick_surface(device, &sx, &sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500977
978 wlsc_input_device_set_pointer_focus(device, es);
979
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500980 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500981 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500982 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500983
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500984 device->sprite->map.x = x - hotspot_x;
985 device->sprite->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500986
987 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500988}
989
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500990void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500991notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500992 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500993{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500994 struct wlsc_surface *es;
995 struct wlsc_compositor *ec = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500996 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500997
Ray Strodee96dcb82008-12-20 02:00:49 -0500998 if (!ec->vt_active)
999 return;
1000
Kristian Høgsberg7e972a52008-12-21 17:26:00 -05001001 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001002 if (es) {
1003 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001004 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -05001005
Kristian Høgsberg29573bc2008-12-11 23:27:27 -05001006 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -05001007 /* FIXME: We need callbacks when the surfaces
1008 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -05001009 device->grab++;
1010 device->grab_surface = es;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001011 wlsc_input_device_set_keyboard_focus(device, es);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -05001012 } else {
1013 device->grab--;
1014 }
1015
Kristian Høgsberg5a75c902008-12-10 13:16:50 -05001016 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001017 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001018 WL_INPUT_BUTTON, button, state,
1019 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001020
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001021 schedule_repaint(device->ec);
1022 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001023}
1024
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001025static void on_term_signal(int signal_number, void *data);
1026
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001027static void
1028update_surface_targets(struct wlsc_compositor *compositor, int primary)
1029{
1030 struct wlsc_surface *s;
1031 int i;
1032
1033 i = 0;
1034 s = container_of(compositor->surface_list.next,
1035 struct wlsc_surface, link);
1036 while (&s->link != &compositor->surface_list) {
1037 if (i < primary) {
1038 s->target.x = -400 + 500 * (i - primary);
1039 s->target.y = 0;
1040 s->target.z = -1500;
1041 s->target_angle = M_PI / 4;
1042 } else if (i == primary) {
1043 s->target.x = 0;
1044 s->target.y = 0;
1045 s->target.z = -1000;
1046 s->target_angle = 0;
1047 compositor->primary = s;
1048 } else {
1049 s->target.x = 400 + 500 * (i - primary);
1050 s->target.y = 0;
1051 s->target.z = -1500;
1052 s->target_angle = -M_PI / 4;
1053 }
1054 wl_list_remove(&s->animate.link);
1055 wl_list_insert(compositor->animate_list.prev, &s->animate.link);
1056 s = container_of(s->link.next,
1057 struct wlsc_surface, link);
1058 i++;
1059 }
1060
1061 schedule_repaint(compositor);
1062}
1063
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001064void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001065notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001066 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001067{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001068 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001069 uint32_t *k, *end;
1070
1071 if (!ec->vt_active)
1072 return;
Ray Strodee96dcb82008-12-20 02:00:49 -05001073
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001074 switch (key | ec->meta_state) {
1075 case KEY_EJECTCD | META_DOWN:
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001076 on_term_signal(SIGTERM, ec);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001077 return;
1078
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001079 case KEY_1 | META_DOWN:
1080 case KEY_2 | META_DOWN:
1081 case KEY_3 | META_DOWN:
1082 case KEY_4 | META_DOWN:
1083 case KEY_5 | META_DOWN:
1084 update_surface_targets(ec, key - KEY_1);
Kristian Høgsbergce5a9c12009-02-22 20:59:44 -05001085 if (device->grab == 0)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001086 wlsc_input_device_set_keyboard_focus(device, ec->primary);
Kristian Høgsbergce5a9c12009-02-22 20:59:44 -05001087 device->keyboard_focus = ec->primary;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001088 return;
1089
1090 case KEY_LEFTMETA:
1091 case KEY_RIGHTMETA:
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001092 case KEY_LEFTMETA | META_DOWN:
1093 case KEY_RIGHTMETA | META_DOWN:
1094 ec->meta_state = state ? META_DOWN : 0;
Kristian Høgsberg1febe012009-02-10 18:30:14 -05001095 if (state == 0 && ec->primary != NULL) {
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001096 ec->primary->target.z = 0;
1097 wl_list_remove(&ec->primary->animate.link);
1098 wl_list_insert(&ec->animate_list,
1099 &ec->primary->animate.link);
1100 schedule_repaint(ec);
1101 }
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001102 return;
1103 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001104
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001105 end = device->keys.data + device->keys.size;
1106 for (k = device->keys.data; k < end; k++) {
1107 if (*k == key)
1108 *k = *--end;
1109 }
1110 device->keys.size = (void *) end - device->keys.data;
1111 if (state) {
1112 k = wl_array_add(&device->keys, sizeof *k);
1113 *k = key;
1114 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001115
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001116 if (device->keyboard_focus != NULL)
1117 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -05001118 &device->base,
1119 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001120}
1121
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001122struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001123evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001124 struct wl_display *display, const char *path);
1125
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001126static void
1127handle_surface_destroy(struct wlsc_listener *listener,
1128 struct wlsc_surface *surface)
1129{
1130 struct wlsc_input_device *device =
1131 container_of(listener, struct wlsc_input_device, listener);
1132
1133 if (device->grab_surface == surface) {
1134 device->grab_surface = NULL;
1135 device->grab = 0;
1136 }
1137 if (device->keyboard_focus == surface)
1138 device->keyboard_focus = NULL;
1139 if (device->pointer_focus == surface)
1140 device->pointer_focus = NULL;
1141}
1142
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001143static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001144create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001145{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001146 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001147
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001148 device = malloc(sizeof *device);
1149 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001150 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001151
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001152 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001153 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001154 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001155 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001156 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001157 device->x = 100;
1158 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001159 device->ec = ec;
1160
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001161 device->listener.func = handle_surface_destroy;
1162 wl_list_insert(ec->surface_destroy_listener_list.prev,
1163 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001164 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001165
1166 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001167}
1168
1169void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001170wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001171{
1172 *x = device->x;
1173 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001174}
1175
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001176static void
1177post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001178{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001179 struct wlsc_output *output =
1180 container_of(global, struct wlsc_output, base);
1181
1182 wl_client_post_event(client, global,
1183 WL_OUTPUT_GEOMETRY,
1184 output->width, output->height);
1185}
1186
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001187static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001188init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001189{
1190 static const EGLint config_attribs[] = {
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001191 EGL_DEPTH_SIZE, 0,
1192 EGL_STENCIL_SIZE, 0,
1193 EGL_CONFIG_CAVEAT, EGL_NONE,
1194 EGL_RED_SIZE, 8,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001195 EGL_NONE
1196 };
1197
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001198 EGLint major, minor;
1199
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -05001200 ec->display = eglCreateDisplayNative(device);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001201 if (ec->display == NULL) {
1202 fprintf(stderr, "failed to create display\n");
1203 return -1;
1204 }
1205
1206 if (!eglInitialize(ec->display, &major, &minor)) {
1207 fprintf(stderr, "failed to initialize display\n");
1208 return -1;
1209 }
1210
1211 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
1212 return -1;
1213
1214 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
1215 if (ec->context == NULL) {
1216 fprintf(stderr, "failed to create context\n");
1217 return -1;
1218 }
1219 return 0;
1220}
1221
1222static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001223create_output(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001224{
1225 const static EGLint surface_attribs[] = {
1226 EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
1227 EGL_NONE
1228 };
1229
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001230 drmModeConnector *connector;
1231 drmModeRes *resources;
1232 drmModeEncoder *encoder;
Kristian Høgsberg41a10682009-02-15 22:37:03 -05001233 drmModeModeInfo *mode;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001234 struct drm_i915_gem_create create;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001235 struct drm_gem_flink flink;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001236 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001237 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001238
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001239 if (ec->display == NULL && init_egl(ec, device) < 0) {
1240 fprintf(stderr, "failed to initialize egl\n");
1241 return -1;
1242 }
1243
1244 output = malloc(sizeof *output);
1245 if (output == NULL)
1246 return -1;
1247
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001248 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001249 resources = drmModeGetResources(fd);
1250 if (!resources) {
1251 fprintf(stderr, "drmModeGetResources failed\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001252 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001253 }
1254
1255 for (i = 0; i < resources->count_connectors; i++) {
1256 connector = drmModeGetConnector(fd, resources->connectors[i]);
1257 if (connector == NULL)
1258 continue;
1259
1260 if (connector->connection == DRM_MODE_CONNECTED &&
1261 connector->count_modes > 0)
1262 break;
1263
1264 drmModeFreeConnector(connector);
1265 }
1266
1267 if (i == resources->count_connectors) {
1268 fprintf(stderr, "No currently active connector found.\n");
1269 return -1;
1270 }
1271
1272 mode = &connector->modes[0];
1273
1274 for (i = 0; i < resources->count_encoders; i++) {
1275 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
1276
1277 if (encoder == NULL)
1278 continue;
1279
1280 if (encoder->encoder_id == connector->encoder_id)
1281 break;
1282
1283 drmModeFreeEncoder(encoder);
1284 }
1285
1286 /* Mode size at 32 bpp */
1287 create.size = mode->hdisplay * mode->vdisplay * 4;
1288 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
1289 fprintf(stderr, "gem create failed: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001290 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001291 }
1292
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001293 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001294 32, 32, mode->hdisplay * 4, create.handle, &output->fb_id);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001295 if (ret) {
1296 fprintf(stderr, "failed to add fb: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001297 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001298 }
1299
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001300 ret = drmModeSetCrtc(fd, encoder->crtc_id, output->fb_id, 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001301 &connector->connector_id, 1, mode);
1302 if (ret) {
1303 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001304 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001305 }
1306
1307 flink.handle = create.handle;
1308 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
1309 fprintf(stderr, "gem flink failed: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001310 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001311 }
1312
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001313 output->compositor = ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001314 output->crtc_id = encoder->crtc_id;
1315 output->connector_id = connector->connector_id;
1316 output->mode = mode;
Kristian Høgsberge68d24c2009-01-15 11:44:35 -05001317 output->x = 0;
1318 output->y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001319 output->width = mode->hdisplay;
1320 output->height = mode->vdisplay;
1321 output->stride = mode->hdisplay * 4;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001322
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001323 output->surface = eglCreateSurfaceForName(ec->display, ec->config,
1324 flink.name,
1325 output->width, output->height,
1326 output->stride,
1327 surface_attribs);
1328 if (output->surface == NULL) {
1329 fprintf(stderr, "failed to create surface\n");
1330 return -1;
1331 }
1332
1333 output->base.interface = &wl_output_interface;
1334 wl_display_add_object(ec->wl_display, &output->base);
1335 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
1336 wl_list_insert(ec->output_list.prev, &output->link);
1337
1338 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
1339 fprintf(stderr, "failed to make context current\n");
1340 return -1;
1341 }
1342
1343 output->background = background_create(output, option_background);
1344
1345 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001346}
1347
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001348static const struct wl_interface visual_interface = {
1349 "visual", 1,
1350};
1351
1352static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001353add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001354{
1355 ec->argb_visual.base.interface = &visual_interface;
1356 ec->argb_visual.base.implementation = NULL;
1357 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001358 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001359
1360 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1361 ec->premultiplied_argb_visual.base.implementation = NULL;
1362 wl_display_add_object(ec->wl_display,
1363 &ec->premultiplied_argb_visual.base);
1364 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001365 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001366
1367 ec->rgb_visual.base.interface = &visual_interface;
1368 ec->rgb_visual.base.implementation = NULL;
1369 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001370 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1371}
1372
Ray Strode19ad6a92008-12-19 01:45:41 -05001373static void on_enter_vt(int signal_number, void *data)
1374{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001375 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001376 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001377 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -05001378
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001379 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001380 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001381
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001382 fd = eglGetDisplayFD(ec->display);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001383 output = container_of(ec->output_list.next, struct wlsc_output, link);
1384 while (&output->link != &ec->output_list) {
1385 ret = drmModeSetCrtc(fd, output->crtc_id, output->fb_id, 0, 0,
1386 &output->connector_id, 1, output->mode);
1387 if (ret)
1388 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1389 output->connector_id);
1390
1391 output = container_of(output->link.next,
1392 struct wlsc_output, link);
Ray Strode19ad6a92008-12-19 01:45:41 -05001393 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001394}
1395
1396static void on_leave_vt(int signal_number, void *data)
1397{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001398 struct wlsc_compositor *ec = data;
Ray Strode19ad6a92008-12-19 01:45:41 -05001399
1400 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001401 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001402}
1403
Ray Strode966aa112008-12-19 14:28:02 -05001404static void
1405on_tty_input(int fd, uint32_t mask, void *data)
1406{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001407 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001408
1409 /* Ignore input to tty. We get keyboard events from evdev
1410 */
1411 tcflush(ec->tty_fd, TCIFLUSH);
1412}
1413
1414static void on_term_signal(int signal_number, void *data)
1415{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001416 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001417
1418 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1419 fprintf(stderr, "could not restore terminal to canonical mode\n");
1420
1421 exit(0);
1422}
1423
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001424static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001425{
1426 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001427 struct vt_mode mode = { 0 };
1428
1429 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1430 if (ec->tty_fd <= 0) {
1431 fprintf(stderr, "failed to open active tty: %m\n");
1432 return -1;
1433 }
Ray Strode966aa112008-12-19 14:28:02 -05001434
1435 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1436 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001437 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001438 }
1439
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001440 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001441 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001442 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001443
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001444 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001445 raw_attributes.c_oflag |= OPOST | OCRNL;
1446
1447 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1448 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1449
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001450 ec->term_signal_source =
1451 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001452
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001453 ec->tty_input_source =
1454 wl_event_loop_add_fd(loop, ec->tty_fd,
1455 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001456
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001457 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001458 mode.mode = VT_PROCESS;
1459 mode.relsig = SIGUSR1;
1460 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001461 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001462 fprintf(stderr, "failed to take control of vt handling\n");
1463 }
1464
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001465 ec->leave_vt_source =
1466 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1467 ec->enter_vt_source =
1468 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001469
1470 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001471}
1472
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001473static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001474init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001475{
1476 struct udev_enumerate *e;
1477 struct udev_list_entry *entry;
1478 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001479 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001480 struct wlsc_input_device *input_device;
1481
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001482 ec->udev = udev_new();
1483 if (ec->udev == NULL) {
1484 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001485 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001486 }
1487
1488 input_device = create_input_device(ec);
1489
1490 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001491 udev_enumerate_add_match_subsystem(e, "input");
1492 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001493 udev_enumerate_scan_devices(e);
1494 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1495 path = udev_list_entry_get_name(entry);
1496 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001497 evdev_input_device_create(input_device, ec->wl_display,
1498 udev_device_get_devnode(device));
1499 }
1500 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001501
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001502 e = udev_enumerate_new(ec->udev);
1503 udev_enumerate_add_match_subsystem(e, "drm");
1504 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1505 udev_enumerate_scan_devices(e);
1506 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1507 path = udev_list_entry_get_name(entry);
1508 device = udev_device_new_from_syspath(ec->udev, path);
1509 if (create_output(ec, device) < 0) {
1510 fprintf(stderr, "failed to create output for %s\n", path);
1511 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001512 }
1513 }
1514 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001515
1516 /* Create the pointer surface now that we have a current EGL context. */
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -05001517 input_device->sprite =
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001518 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1519
1520 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001521}
1522
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001523static struct wlsc_compositor *
1524wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001525{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001526 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001527 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001528 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001529
1530 ec = malloc(sizeof *ec);
1531 if (ec == NULL)
1532 return NULL;
1533
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001534 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001535 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001536
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001537 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001538
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001539 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001540
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001541 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001542 wl_list_init(&ec->input_device_list);
1543 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001544 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001545 if (init_libudev(ec) < 0) {
1546 fprintf(stderr, "failed to initialize devices\n");
1547 return NULL;
1548 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001549
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001550 shooter = screenshooter_create(ec);
1551 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001552 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001553
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001554 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001555
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001556 setup_tty(ec, loop);
1557
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001558 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001559 schedule_repaint(ec);
1560
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001561 wl_list_init(&ec->animate_list);
1562
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001563 return ec;
1564}
1565
1566/* The plan here is to generate a random anonymous socket name and
1567 * advertise that through a service on the session dbus.
1568 */
1569static const char socket_name[] = "\0wayland";
1570
1571int main(int argc, char *argv[])
1572{
1573 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001574 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001575 GError *error = NULL;
1576 GOptionContext *context;
1577
1578 context = g_option_context_new(NULL);
1579 g_option_context_add_main_entries(context, option_entries, "Wayland");
1580 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1581 fprintf(stderr, "option parsing failed: %s\n", error->message);
1582 exit(EXIT_FAILURE);
1583 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001584
1585 display = wl_display_create();
1586
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001587 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001588 if (ec == NULL) {
1589 fprintf(stderr, "failed to create compositor\n");
1590 exit(EXIT_FAILURE);
1591 }
1592
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001593 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001594 fprintf(stderr, "failed to add socket: %m\n");
1595 exit(EXIT_FAILURE);
1596 }
1597
1598 wl_display_run(display);
1599
1600 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001601}