blob: b0e5552213d572ce56d68cf115e8cdc6af7d9d82 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040023#include <stdio.h>
24#include <string.h>
25#include <stdlib.h>
26#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050027#include <stdarg.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <i915_drm.h>
29#include <sys/ioctl.h>
30#include <sys/mman.h>
31#include <fcntl.h>
32#include <unistd.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <signal.h>
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050034#include <cairo.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050035#include <gdk-pixbuf/gdk-pixbuf.h>
36#include <glib.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050037#include <sys/poll.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050038#include <png.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050039#include <math.h>
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -050040#include <linux/input.h>
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -050041#include <xf86drmMode.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050042#include <time.h>
Kristian Høgsbergc492b482008-12-12 12:00:02 -050043#include <fnmatch.h>
44#include <dirent.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040045
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040046#include <GL/gl.h>
47#include <eagle.h>
48
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050049#include "wayland.h"
50#include "cairo-util.h"
51#include "egl-compositor.h"
52
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040053#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
54
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050055struct wl_visual {
56 struct wl_object base;
57};
58
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050059struct egl_input_device {
60 struct wl_object base;
61 int32_t x, y;
62 struct egl_compositor *ec;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050063 struct egl_surface *pointer_surface;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050064 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050065
66 int grab;
67 struct egl_surface *grab_surface;
Kristian Høgsberga7700c82008-12-12 13:48:30 -050068 struct egl_surface *focus_surface;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050069};
70
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040071struct egl_compositor {
72 struct wl_compositor base;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050073 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
74
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040075 EGLDisplay display;
76 EGLSurface surface;
77 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050078 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040079 struct wl_display *wl_display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040080 int gem_fd;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050081 int width, height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050082 struct egl_surface *background;
Kristian Høgsberg54879822008-11-23 17:07:32 -050083 struct egl_surface *overlay;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -050084 double overlay_y, overlay_target, overlay_previous;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050085
Kristian Høgsbergc492b482008-12-12 12:00:02 -050086 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -050087 struct wl_list surface_list;
88
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050089 /* Repaint state. */
90 struct wl_event_source *timer_source;
91 int repaint_needed;
92 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050093 struct timespec previous_swap;
94 uint32_t current_frame;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040095};
96
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050097struct egl_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050098 struct wl_surface base;
99 struct egl_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500100 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400101 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400102 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500103 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500104 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500105 struct wl_list link;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400106};
107
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500108struct screenshooter {
109 struct wl_object base;
110 struct egl_compositor *ec;
111};
112
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500113struct screenshooter_interface {
114 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
115};
116
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500117static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500118screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500119{
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500120 struct egl_compositor *ec = shooter->ec;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500121 GLuint stride;
122 static const char filename[] = "wayland-screenshot.png";
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500123 GdkPixbuf *pixbuf;
124 GError *error = NULL;
125 void *data;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500126
127 data = eglReadBuffer(ec->display, ec->surface, GL_FRONT_LEFT, &stride);
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500128 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
129 8, ec->width, ec->height, stride,
130 NULL, NULL);
131 gdk_pixbuf_save(pixbuf, filename, "png", &error, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500132}
133
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500134static const struct wl_method screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500135 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500136};
137
138static const struct wl_interface screenshooter_interface = {
139 "screenshooter", 1,
140 ARRAY_LENGTH(screenshooter_methods),
141 screenshooter_methods,
142};
143
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500144struct screenshooter_interface screenshooter_implementation = {
145 screenshooter_shoot
146};
147
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500148static struct screenshooter *
149screenshooter_create(struct egl_compositor *ec)
150{
151 struct screenshooter *shooter;
152
153 shooter = malloc(sizeof *shooter);
154 if (shooter == NULL)
155 return NULL;
156
157 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500158 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500159 shooter->ec = ec;
160
161 return shooter;
162};
163
Kristian Høgsberg54879822008-11-23 17:07:32 -0500164static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500165egl_surface_create_from_cairo_surface(struct egl_compositor *ec,
166 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500167 int x, int y, int width, int height)
168{
169 struct egl_surface *es;
170 int stride;
171 void *data;
172
173 stride = cairo_image_surface_get_stride(surface);
174 data = cairo_image_surface_get_data(surface);
175
176 es = malloc(sizeof *es);
177 if (es == NULL)
178 return NULL;
179
180 glGenTextures(1, &es->texture);
181 glBindTexture(GL_TEXTURE_2D, es->texture);
182 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
185 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500186 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
187 GL_BGRA, GL_UNSIGNED_BYTE, data);
188
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500189 es->compositor = ec;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500190 es->map.x = x;
191 es->map.y = y;
192 es->map.width = width;
193 es->map.height = height;
194 es->surface = EGL_NO_SURFACE;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500195 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500196
197 return es;
198}
199
200static void
201egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec)
202{
203 glDeleteTextures(1, &es->texture);
204 if (es->surface != EGL_NO_SURFACE)
205 eglDestroySurface(ec->display, es->surface);
206 free(es);
207}
208
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400209static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500210pointer_path(cairo_t *cr, int x, int y)
211{
212 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
213 const int width = 16, height = 16;
214
215 cairo_move_to(cr, x, y);
216 cairo_line_to(cr, x + tx, y + ty);
217 cairo_line_to(cr, x + dx, y + dy);
218 cairo_line_to(cr, x + width - end, y + height);
219 cairo_line_to(cr, x + width, y + height - end);
220 cairo_line_to(cr, x + dy, y + dx);
221 cairo_line_to(cr, x + ty, y + tx);
222 cairo_close_path(cr);
223}
224
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500225static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500226pointer_create(struct egl_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500227{
Kristian Høgsberg54879822008-11-23 17:07:32 -0500228 struct egl_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500229 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500230 cairo_surface_t *surface;
231 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500232
233 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
234 width, height);
235
236 cr = cairo_create(surface);
237 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
238 cairo_set_line_width (cr, 2);
239 cairo_set_source_rgb(cr, 0, 0, 0);
240 cairo_stroke_preserve(cr);
241 cairo_fill(cr);
242 blur_surface(surface, width);
243
244 pointer_path(cr, hotspot_x, hotspot_y);
245 cairo_stroke_preserve(cr);
246 cairo_set_source_rgb(cr, 1, 1, 1);
247 cairo_fill(cr);
248 cairo_destroy(cr);
249
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500250 es = egl_surface_create_from_cairo_surface(ec,
251 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500252 x - hotspot_x,
253 y - hotspot_y,
254 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500255
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500256 cairo_surface_destroy(surface);
257
Kristian Høgsberg54879822008-11-23 17:07:32 -0500258 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500259}
260
261static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500262background_create(struct egl_compositor *ec,
263 const char *filename, int width, int height)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500264{
265 struct egl_surface *background;
266 GdkPixbuf *pixbuf;
267 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500268 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500269 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500270
271 background = malloc(sizeof *background);
272 if (background == NULL)
273 return NULL;
274
275 g_type_init();
276
Kristian Høgsberg2d4219e2008-12-18 21:40:03 -0500277 pixbuf = gdk_pixbuf_new_from_file_at_scale (filename,
278 width, height,
279 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500280 if (error != NULL) {
281 free(background);
282 return NULL;
283 }
284
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500285 data = gdk_pixbuf_get_pixels(pixbuf);
286
287 glGenTextures(1, &background->texture);
288 glBindTexture(GL_TEXTURE_2D, background->texture);
289 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500290 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
291 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
292 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Ray Strode18fd33c2008-12-18 21:05:20 -0500293
294 if (gdk_pixbuf_get_has_alpha (pixbuf)) {
295 format = GL_RGBA;
296 } else {
297 format = GL_RGB;
298 }
299
Kristian Høgsberg2d4219e2008-12-18 21:40:03 -0500300 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500301 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500302
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500303 background->compositor = ec;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500304 background->map.x = 0;
305 background->map.y = 0;
306 background->map.width = width;
307 background->map.height = height;
308 background->surface = EGL_NO_SURFACE;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500309 background->visual = &ec->rgb_visual;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500310
311 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500312}
313
314static void
Kristian Høgsberg54879822008-11-23 17:07:32 -0500315rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
316{
317 cairo_move_to(cr, x0, y0 + radius);
318 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
319 cairo_line_to(cr, x1 - radius, y0);
320 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
321 cairo_line_to(cr, x1, y1 - radius);
322 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
323 cairo_line_to(cr, x0 + radius, y1);
324 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
325 cairo_close_path(cr);
326}
327
328static void
329draw_button(cairo_t *cr, int x, int y, int width, int height, const char *text)
330{
331 cairo_pattern_t *gradient;
332 cairo_text_extents_t extents;
333 double bright = 0.15, dim = 0.02;
334 int radius = 10;
335
336 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
337 cairo_set_line_width (cr, 2);
338 rounded_rect(cr, x, y, x + width, y + height, radius);
339 cairo_set_source_rgb(cr, dim, dim, dim);
340 cairo_stroke(cr);
341 rounded_rect(cr, x + 2, y + 2, x + width, y + height, radius);
342 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
343 cairo_stroke(cr);
344
345 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
346 cairo_set_source_rgb(cr, bright, bright, bright);
347 cairo_stroke(cr);
348 rounded_rect(cr, x + 3, y + 3, x + width - 1, y + height - 1, radius - 1);
349 cairo_set_source_rgb(cr, dim, dim, dim);
350 cairo_stroke(cr);
351
352 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
353 gradient = cairo_pattern_create_linear (0, y, 0, y + height);
354 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.15, 0.15, 0.15);
355 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.08, 0.08, 0.08);
356 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.07, 0.07, 0.07);
357 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.1, 0.1, 0.1);
358 cairo_set_source(cr, gradient);
359 cairo_fill(cr);
360
361 cairo_set_font_size(cr, 16);
362 cairo_text_extents(cr, text, &extents);
363 cairo_move_to(cr, x + (width - extents.width) / 2, y + (height - extents.height) / 2 - extents.y_bearing);
364 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
365 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
366 cairo_set_line_width (cr, 4);
367 cairo_text_path(cr, text);
368 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
369 cairo_stroke_preserve(cr);
370 cairo_set_source_rgb(cr, 1, 1, 1);
371 cairo_fill(cr);
372}
373
374static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500375overlay_create(struct egl_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500376{
377 struct egl_surface *es;
378 cairo_surface_t *surface;
379 cairo_t *cr;
380 int total_width, button_x, button_y;
381 const int button_width = 150;
382 const int button_height = 40;
383 const int spacing = 50;
384
385 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
386 width, height);
387
388 cr = cairo_create(surface);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500389 cairo_set_source_rgba(cr, 0.1, 0.1, 0.1, 0.8);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500390 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
391 cairo_paint(cr);
392
393 total_width = button_width * 2 + spacing;
394 button_x = (width - total_width) / 2;
395 button_y = height - button_height - 20;
396 draw_button(cr, button_x, button_y, button_width, button_height, "Previous");
397 button_x += button_width + spacing;
398 draw_button(cr, button_x, button_y, button_width, button_height, "Next");
399
400 cairo_destroy(cr);
401
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500402 es = egl_surface_create_from_cairo_surface(ec, surface,
403 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500404
405 cairo_surface_destroy(surface);
406
407 return es;
408}
409
410static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500411draw_surface(struct egl_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500412{
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500413 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500414 GLint vertices[12];
415 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
416 GLuint indices[4] = { 0, 1, 2, 3 };
417
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500418 vertices[0] = es->map.x;
419 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500420 vertices[2] = 0;
421
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500422 vertices[3] = es->map.x;
423 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500424 vertices[5] = 0;
425
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500426 vertices[6] = es->map.x + es->map.width;
427 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500428 vertices[8] = 0;
429
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500430 vertices[9] = es->map.x + es->map.width;
431 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500432 vertices[11] = 0;
433
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500434 if (es->visual == &ec->argb_visual) {
435 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
436 glEnable(GL_BLEND);
437 } else if (es->visual == &ec->premultiplied_argb_visual) {
438 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
439 glEnable(GL_BLEND);
440 } else {
441 glDisable(GL_BLEND);
442 }
443
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500444 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500445 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500446 glEnableClientState(GL_VERTEX_ARRAY);
447 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
448 glVertexPointer(3, GL_INT, 0, vertices);
449 glTexCoordPointer(2, GL_INT, 0, tex_coords);
450 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
451}
452
453static void
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500454schedule_repaint(struct egl_compositor *ec);
455
456static void
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500457animate_overlay(struct egl_compositor *ec)
458{
459 double force, y;
460 int32_t top, bottom;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500461#if 1
462 double bounce = 0.0;
463 double friction = 1.0;
464 double spring = 0.2;
465#else
466 double bounce = 0.2;
467 double friction = 0.04;
468 double spring = 0.09;
469#endif
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500470
471 y = ec->overlay_y;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500472 force = (ec->overlay_target - ec->overlay_y) * spring +
473 (ec->overlay_previous - y) * friction;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500474
475 ec->overlay_y = y + (y - ec->overlay_previous) + force;
476 ec->overlay_previous = y;
477
478 top = ec->height - ec->overlay->map.height;
479 bottom = ec->height;
480 if (ec->overlay_y >= bottom) {
481 ec->overlay_y = bottom;
482 ec->overlay_previous = bottom;
483 }
484
485 if (ec->overlay_y <= top) {
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500486 ec->overlay_y = top + bounce * (top - ec->overlay_y);
487 ec->overlay_previous =
488 top + bounce * (top - ec->overlay_previous);
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500489 }
490
491 ec->overlay->map.y = ec->overlay_y + 0.5;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500492
Kristian Høgsberg73c30582008-11-25 22:45:46 -0500493 if (fabs(y - ec->overlay_target) > 0.2 ||
494 fabs(ec->overlay_y - ec->overlay_target) > 0.2)
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500495 schedule_repaint(ec);
496}
497
498static void
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500499repaint(void *data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400500{
501 struct egl_compositor *ec = data;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500502 struct egl_surface *es;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500503 struct egl_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500504 struct timespec ts;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500505 uint32_t msecs;
506
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500507 if (!ec->repaint_needed) {
508 ec->repaint_on_timeout = 0;
509 return;
510 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500511
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500512 if (ec->background)
513 draw_surface(ec->background);
514 else
515 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400516
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500517 es = container_of(ec->surface_list.next,
518 struct egl_surface, link);
519 while (&es->link != &ec->surface_list) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500520 draw_surface(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500521
522 es = container_of(es->link.next,
523 struct egl_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400524 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400525
Kristian Høgsberg54879822008-11-23 17:07:32 -0500526 draw_surface(ec->overlay);
527
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500528 eid = container_of(ec->input_device_list.next,
529 struct egl_input_device, link);
530 while (&eid->link != &ec->input_device_list) {
531 draw_surface(eid->pointer_surface);
532
533 eid = container_of(eid->link.next,
534 struct egl_input_device, link);
535 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500536
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400537 eglSwapBuffers(ec->display, ec->surface);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500538 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500539
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500540 clock_gettime(CLOCK_MONOTONIC, &ts);
541 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500542 wl_display_post_frame(ec->wl_display, &ec->base,
543 ec->current_frame, msecs);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500544 ec->current_frame++;
545
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500546 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500547 ec->repaint_on_timeout = 1;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500548
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500549 animate_overlay(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400550}
551
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500552static void
553schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400554{
555 struct wl_event_loop *loop;
556
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500557 ec->repaint_needed = 1;
558 if (!ec->repaint_on_timeout) {
559 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500560 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500561 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400562}
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400563
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500564static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500565surface_destroy(struct wl_client *client,
566 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400567{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500568 struct egl_surface *es = (struct egl_surface *) surface;
569 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500570
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500571 wl_list_remove(&es->link);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500572 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400573
574 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400575}
576
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500577static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500578surface_attach(struct wl_client *client,
579 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500580 uint32_t width, uint32_t height, uint32_t stride,
581 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400582{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500583 struct egl_surface *es = (struct egl_surface *) surface;
584 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400585
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500586 if (es->surface != EGL_NO_SURFACE)
587 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400588
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500589 es->width = width;
590 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500591 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500592 name, width, height, stride, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500593 if (visual == &ec->argb_visual.base)
594 es->visual = &ec->argb_visual;
595 else if (visual == &ec->premultiplied_argb_visual.base)
596 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500597 else if (visual == &ec->rgb_visual.base)
598 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500599 else
600 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400601
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500602 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400603 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500604 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
605 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
606 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500607 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400608}
609
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500610static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500611surface_map(struct wl_client *client,
612 struct wl_surface *surface,
613 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400614{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500615 struct egl_surface *es = (struct egl_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400616
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500617 es->map.x = x;
618 es->map.y = y;
619 es->map.width = width;
620 es->map.height = height;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400621}
622
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500623static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500624surface_copy(struct wl_client *client,
625 struct wl_surface *surface,
626 int32_t dst_x, int32_t dst_y,
627 uint32_t name, uint32_t stride,
628 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500629{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500630 struct egl_surface *es = (struct egl_surface *) surface;
631 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500632 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500633
634 /* FIXME: glCopyPixels should work, but then we'll have to
635 * call eglMakeCurrent to set up the src and dest surfaces
636 * first. This seems cheaper, but maybe there's a better way
637 * to accomplish this. */
638
639 src = eglCreateSurfaceForName(ec->display, ec->config,
640 name, x + width, y + height, stride, NULL);
641
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500642 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500643 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500644 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500645}
646
647static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500648surface_damage(struct wl_client *client,
649 struct wl_surface *surface,
650 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500651{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500652 /* FIXME: This need to take a damage region, of course. */
653}
654
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500655const static struct wl_surface_interface surface_interface = {
656 surface_destroy,
657 surface_attach,
658 surface_map,
659 surface_copy,
660 surface_damage
661};
662
663static void
664compositor_create_surface(struct wl_client *client,
665 struct wl_compositor *compositor, uint32_t id)
666{
667 struct egl_compositor *ec = (struct egl_compositor *) compositor;
668 struct egl_surface *es;
669
670 es = malloc(sizeof *es);
671 if (es == NULL)
672 /* FIXME: Send OOM event. */
673 return;
674
675 es->compositor = ec;
676 es->surface = EGL_NO_SURFACE;
677 wl_list_insert(ec->surface_list.prev, &es->link);
678 glGenTextures(1, &es->texture);
679 wl_client_add_surface(client, &es->base,
680 &surface_interface, id);
681}
682
683static void
684compositor_commit(struct wl_client *client,
685 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500686{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500687 struct egl_compositor *ec = (struct egl_compositor *) compositor;
688
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500689 schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500690 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500691}
692
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500693const static struct wl_compositor_interface compositor_interface = {
694 compositor_create_surface,
695 compositor_commit
696};
697
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500698static struct egl_surface *
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500699pick_surface(struct egl_input_device *device)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500700{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500701 struct egl_compositor *ec = device->ec;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500702 struct egl_surface *es;
703
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500704 if (device->grab > 0)
705 return device->grab_surface;
706
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500707 es = container_of(ec->surface_list.prev,
708 struct egl_surface, link);
709 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500710 if (es->map.x <= device->x &&
711 device->x < es->map.x + es->map.width &&
712 es->map.y <= device->y &&
713 device->y < es->map.y + es->map.height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500714 return es;
715
716 es = container_of(es->link.prev,
717 struct egl_surface, link);
718 }
719
720 return NULL;
721}
722
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500723void
724notify_motion(struct egl_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500725{
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500726 struct egl_surface *es;
727 const int hotspot_x = 16, hotspot_y = 16;
728 int32_t sx, sy;
729
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500730 es = pick_surface(device);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500731
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500732 if (es) {
733 sx = (x - es->map.x) * es->width / es->map.width;
734 sy = (y - es->map.y) * es->height / es->map.height;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500735 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500736 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500737 }
738
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500739 device->x = x;
740 device->y = y;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500741 device->pointer_surface->map.x = x - hotspot_x;
742 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500743
744 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500745}
746
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500747void
748notify_button(struct egl_input_device *device,
749 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500750{
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500751 struct egl_surface *es;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500752 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500753
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500754 es = pick_surface(device);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500755 if (es) {
756 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500757 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500758
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500759 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500760 /* FIXME: We need callbacks when the surfaces
761 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500762 device->grab++;
763 device->grab_surface = es;
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500764 device->focus_surface = es;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500765 } else {
766 device->grab--;
767 }
768
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500769 sx = (device->x - es->map.x) * es->width / es->map.width;
770 sy = (device->y - es->map.y) * es->height / es->map.height;
771
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500772 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500773 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500774 WL_INPUT_BUTTON, button, state,
775 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500776
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500777 schedule_repaint(device->ec);
778 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500779}
780
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500781void
782notify_key(struct egl_input_device *device,
783 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500784{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500785 struct egl_compositor *ec = device->ec;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500786
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500787 if (key == KEY_ESC && state == 1) {
788 if (ec->overlay_target == ec->height)
789 ec->overlay_target -= 200;
790 else
791 ec->overlay_target += 200;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500792 schedule_repaint(ec);
Kristian Høgsberg7fdff042008-12-10 13:25:00 -0500793 } else if (!wl_list_empty(&ec->surface_list)) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500794 if (device->focus_surface != NULL)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500795 wl_surface_post_event(&device->focus_surface->base,
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500796 &device->base,
797 WL_INPUT_KEY, key, state);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500798 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500799}
800
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500801struct evdev_input_device *
802evdev_input_device_create(struct egl_input_device *device,
803 struct wl_display *display, const char *path);
804
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500805static void
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500806create_input_device(struct egl_compositor *ec, const char *glob)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500807{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500808 struct egl_input_device *device;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500809 struct dirent *de;
810 char path[PATH_MAX];
811 const char *by_path_dir = "/dev/input/by-path";
812 DIR *dir;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500813
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500814 device = malloc(sizeof *device);
815 if (device == NULL)
816 return;
817
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500818 memset(device, 0, sizeof *device);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500819 device->base.interface = wl_input_device_get_interface();
820 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500821 device->x = 100;
822 device->y = 100;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500823 device->pointer_surface = pointer_create(ec,
824 device->x, device->y, 64, 64);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500825 device->ec = ec;
826
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500827 dir = opendir(by_path_dir);
828 if (dir == NULL) {
829 fprintf(stderr, "couldn't read dir %s\n", by_path_dir);
830 return;
831 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500832
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500833 while (de = readdir(dir), de != NULL) {
834 if (fnmatch(glob, de->d_name, 0))
835 continue;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500836
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500837 snprintf(path, sizeof path, "%s/%s", by_path_dir, de->d_name);
838 evdev_input_device_create(device, ec->wl_display, path);
839 }
840 closedir(dir);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500841
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500842 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500843}
844
845void
846egl_device_get_position(struct egl_input_device *device, int32_t *x, int32_t *y)
847{
848 *x = device->x;
849 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500850}
851
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500852static uint32_t
853create_frontbuffer(int fd, int *width, int *height, int *stride)
854{
855 drmModeConnector *connector;
856 drmModeRes *resources;
857 drmModeEncoder *encoder;
858 struct drm_mode_modeinfo *mode;
859 struct drm_i915_gem_create create;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500860 struct drm_gem_flink flink;
861 unsigned int fb_id;
862 int i, ret;
863
864 resources = drmModeGetResources(fd);
865 if (!resources) {
866 fprintf(stderr, "drmModeGetResources failed\n");
867 return 0;
868 }
869
870 for (i = 0; i < resources->count_connectors; i++) {
871 connector = drmModeGetConnector(fd, resources->connectors[i]);
872 if (connector == NULL)
873 continue;
874
875 if (connector->connection == DRM_MODE_CONNECTED &&
876 connector->count_modes > 0)
877 break;
878
879 drmModeFreeConnector(connector);
880 }
881
882 if (i == resources->count_connectors) {
883 fprintf(stderr, "No currently active connector found.\n");
884 return -1;
885 }
886
887 mode = &connector->modes[0];
888
889 for (i = 0; i < resources->count_encoders; i++) {
890 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
891
892 if (encoder == NULL)
893 continue;
894
895 if (encoder->encoder_id == connector->encoder_id)
896 break;
897
898 drmModeFreeEncoder(encoder);
899 }
900
901 /* Mode size at 32 bpp */
902 create.size = mode->hdisplay * mode->vdisplay * 4;
903 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
904 fprintf(stderr, "gem create failed: %m\n");
905 return 0;
906 }
907
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500908 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
909 32, 32, mode->hdisplay * 4, create.handle, &fb_id);
910 if (ret) {
911 fprintf(stderr, "failed to add fb: %m\n");
912 return 0;
913 }
914
915 ret = drmModeSetCrtc(fd, encoder->crtc_id, fb_id, 0, 0,
916 &connector->connector_id, 1, mode);
917 if (ret) {
918 fprintf(stderr, "failed to set mode: %m\n");
919 return 0;
920 }
921
922 flink.handle = create.handle;
923 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
924 fprintf(stderr, "gem flink failed: %m\n");
925 return 0;
926 }
927
928 *width = mode->hdisplay;
929 *height = mode->vdisplay;
930 *stride = mode->hdisplay * 4;
931
932 return flink.name;
933}
934
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500935static int
936pick_config(struct egl_compositor *ec)
937{
938 EGLConfig configs[100];
939 EGLint value, count;
940 int i;
941
942 if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) {
943 fprintf(stderr, "failed to get configs\n");
944 return -1;
945 }
946
947 ec->config = EGL_NO_CONFIG;
948 for (i = 0; i < count; i++) {
949 eglGetConfigAttrib(ec->display,
950 configs[i],
951 EGL_DEPTH_SIZE,
952 &value);
953 if (value > 0) {
954 fprintf(stderr, "config %d has depth size %d\n", i, value);
955 continue;
956 }
957
958 eglGetConfigAttrib(ec->display,
959 configs[i],
960 EGL_STENCIL_SIZE,
961 &value);
962 if (value > 0) {
963 fprintf(stderr, "config %d has stencil size %d\n", i, value);
964 continue;
965 }
966
967 eglGetConfigAttrib(ec->display,
968 configs[i],
969 EGL_CONFIG_CAVEAT,
970 &value);
971 if (value != EGL_NONE) {
972 fprintf(stderr, "config %d has caveat %d\n", i, value);
973 continue;
974 }
975
976 ec->config = configs[i];
977 break;
978 }
979
980 if (ec->config == EGL_NO_CONFIG) {
981 fprintf(stderr, "found no config without depth or stencil buffers\n");
982 return -1;
983 }
984
985 return 0;
986}
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500987
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500988static const struct wl_interface visual_interface = {
989 "visual", 1,
990};
991
992static void
993add_visuals(struct egl_compositor *ec)
994{
995 ec->argb_visual.base.interface = &visual_interface;
996 ec->argb_visual.base.implementation = NULL;
997 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
998 wl_display_add_global(ec->wl_display, &ec->argb_visual.base);
999
1000 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1001 ec->premultiplied_argb_visual.base.implementation = NULL;
1002 wl_display_add_object(ec->wl_display,
1003 &ec->premultiplied_argb_visual.base);
1004 wl_display_add_global(ec->wl_display,
1005 &ec->premultiplied_argb_visual.base);
1006
1007 ec->rgb_visual.base.interface = &visual_interface;
1008 ec->rgb_visual.base.implementation = NULL;
1009 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
1010 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base);
1011}
1012
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001013static const char gem_device[] = "/dev/dri/card0";
1014
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001015static const char *macbook_air_default_input_device[] = {
Kristian Høgsberg64949972008-12-12 14:48:46 -05001016 "pci-0000:00:1d.2-usb-0:2:1*event*",
1017 NULL
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001018};
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001019
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001020static const char *option_background = "background.jpg";
1021static const char **option_input_devices = macbook_air_default_input_device;
1022
1023static const GOptionEntry option_entries[] = {
1024 { "background", 'b', 0, G_OPTION_ARG_STRING,
1025 &option_background, "Background image" },
1026 { "input-device", 'i', 0, G_OPTION_ARG_STRING_ARRAY,
1027 &option_input_devices, "Input device glob" },
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001028 { NULL }
1029};
1030
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001031static struct egl_compositor *
1032egl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001033{
Kristian Høgsberg443853c2008-11-25 12:12:05 -05001034 EGLint major, minor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001035 struct egl_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001036 struct screenshooter *shooter;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001037 uint32_t fb_name;
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001038 int i, stride;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001039 struct wl_event_loop *loop;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001040 const static EGLint attribs[] =
1041 { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE };
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001042
1043 ec = malloc(sizeof *ec);
1044 if (ec == NULL)
1045 return NULL;
1046
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001047 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001048
Kristian Høgsbergc508d932008-10-13 22:52:42 -04001049 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001050 if (ec->display == NULL) {
1051 fprintf(stderr, "failed to create display\n");
1052 return NULL;
1053 }
1054
1055 if (!eglInitialize(ec->display, &major, &minor)) {
1056 fprintf(stderr, "failed to initialize display\n");
1057 return NULL;
1058 }
1059
Kristian Høgsberg443853c2008-11-25 12:12:05 -05001060 if (pick_config(ec))
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001061 return NULL;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001062
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001063 fb_name = create_frontbuffer(eglGetDisplayFD(ec->display),
1064 &ec->width, &ec->height, &stride);
1065 ec->surface = eglCreateSurfaceForName(ec->display, ec->config,
1066 fb_name, ec->width, ec->height, stride, attribs);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001067 if (ec->surface == NULL) {
1068 fprintf(stderr, "failed to create surface\n");
1069 return NULL;
1070 }
1071
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -05001072 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001073 if (ec->context == NULL) {
1074 fprintf(stderr, "failed to create context\n");
1075 return NULL;
1076 }
1077
1078 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
1079 fprintf(stderr, "failed to make context current\n");
1080 return NULL;
1081 }
1082
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001083 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001084 glMatrixMode(GL_PROJECTION);
1085 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001086 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001087 glMatrixMode(GL_MODELVIEW);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -05001088 glClearColor(0, 0, 0.2, 1);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001089
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001090 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001091 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001092
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001093 wl_list_init(&ec->input_device_list);
1094 for (i = 0; option_input_devices[i]; i++)
1095 create_input_device(ec, option_input_devices[i]);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001096
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001097 wl_list_init(&ec->surface_list);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001098 ec->background = background_create(ec, option_background,
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001099 ec->width, ec->height);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001100 ec->overlay = overlay_create(ec, 0, ec->height, ec->width, 200);
Kristian Høgsberg9f88b182008-12-08 13:52:08 -05001101 ec->overlay_y = ec->height;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001102 ec->overlay_target = ec->height;
1103 ec->overlay_previous = ec->height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001104
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001105 ec->gem_fd = open(gem_device, O_RDWR);
1106 if (ec->gem_fd < 0) {
1107 fprintf(stderr, "failed to open drm device\n");
1108 return NULL;
1109 }
1110
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001111 shooter = screenshooter_create(ec);
1112 wl_display_add_object(display, &shooter->base);
1113 wl_display_add_global(display, &shooter->base);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001114
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001115 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001116 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001117 ec->repaint_needed = 0;
1118 ec->repaint_on_timeout = 0;
1119
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001120 schedule_repaint(ec);
1121
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001122 return ec;
1123}
1124
1125/* The plan here is to generate a random anonymous socket name and
1126 * advertise that through a service on the session dbus.
1127 */
1128static const char socket_name[] = "\0wayland";
1129
1130int main(int argc, char *argv[])
1131{
1132 struct wl_display *display;
1133 struct egl_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001134 GError *error = NULL;
1135 GOptionContext *context;
1136
1137 context = g_option_context_new(NULL);
1138 g_option_context_add_main_entries(context, option_entries, "Wayland");
1139 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1140 fprintf(stderr, "option parsing failed: %s\n", error->message);
1141 exit(EXIT_FAILURE);
1142 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001143
1144 display = wl_display_create();
1145
1146 ec = egl_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001147 if (ec == NULL) {
1148 fprintf(stderr, "failed to create compositor\n");
1149 exit(EXIT_FAILURE);
1150 }
1151
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001152 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001153 fprintf(stderr, "failed to add socket: %m\n");
1154 exit(EXIT_FAILURE);
1155 }
1156
1157 wl_display_run(display);
1158
1159 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001160}