blob: e5fe679fdce8077a10a033a5fd07dfae4eee2eeb [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øgsberg5ee1a602008-12-11 23:18:45 -050055struct egl_input_device {
56 struct wl_object base;
57 int32_t x, y;
58 struct egl_compositor *ec;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050059 struct egl_surface *pointer_surface;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050060 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050061
62 int grab;
63 struct egl_surface *grab_surface;
Kristian Høgsberga7700c82008-12-12 13:48:30 -050064 struct egl_surface *focus_surface;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050065};
66
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040067struct egl_compositor {
68 struct wl_compositor base;
69 EGLDisplay display;
70 EGLSurface surface;
71 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050072 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040073 struct wl_display *wl_display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040074 int gem_fd;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050075 int width, height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050076 struct egl_surface *background;
Kristian Høgsberg54879822008-11-23 17:07:32 -050077 struct egl_surface *overlay;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -050078 double overlay_y, overlay_target, overlay_previous;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050079
Kristian Høgsbergc492b482008-12-12 12:00:02 -050080 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -050081 struct wl_list surface_list;
82
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050083 /* Repaint state. */
84 struct wl_event_source *timer_source;
85 int repaint_needed;
86 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050087 struct timespec previous_swap;
88 uint32_t current_frame;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040089};
90
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050091struct egl_surface {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040092 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040093 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050094 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -050095 struct wl_surface *wl_surface;
96 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -050097 struct wl_list link;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040098};
99
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500100struct screenshooter {
101 struct wl_object base;
102 struct egl_compositor *ec;
103};
104
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500105static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500106screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500107{
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500108 struct egl_compositor *ec = shooter->ec;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500109 GLuint stride;
110 static const char filename[] = "wayland-screenshot.png";
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500111 GdkPixbuf *pixbuf;
112 GError *error = NULL;
113 void *data;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500114
115 data = eglReadBuffer(ec->display, ec->surface, GL_FRONT_LEFT, &stride);
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500116 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
117 8, ec->width, ec->height, stride,
118 NULL, NULL);
119 gdk_pixbuf_save(pixbuf, filename, "png", &error, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500120}
121
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500122static const struct wl_method screenshooter_methods[] = {
Kristian Høgsberg73f4e762008-12-08 14:07:33 -0500123 { "shoot", screenshooter_shoot, "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500124};
125
126static const struct wl_interface screenshooter_interface = {
127 "screenshooter", 1,
128 ARRAY_LENGTH(screenshooter_methods),
129 screenshooter_methods,
130};
131
132static struct screenshooter *
133screenshooter_create(struct egl_compositor *ec)
134{
135 struct screenshooter *shooter;
136
137 shooter = malloc(sizeof *shooter);
138 if (shooter == NULL)
139 return NULL;
140
141 shooter->base.interface = &screenshooter_interface;
142 shooter->ec = ec;
143
144 return shooter;
145};
146
Kristian Høgsberg54879822008-11-23 17:07:32 -0500147static struct egl_surface *
148egl_surface_create_from_cairo_surface(cairo_surface_t *surface,
149 int x, int y, int width, int height)
150{
151 struct egl_surface *es;
152 int stride;
153 void *data;
154
155 stride = cairo_image_surface_get_stride(surface);
156 data = cairo_image_surface_get_data(surface);
157
158 es = malloc(sizeof *es);
159 if (es == NULL)
160 return NULL;
161
162 glGenTextures(1, &es->texture);
163 glBindTexture(GL_TEXTURE_2D, es->texture);
164 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500165 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
166 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
167 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500168 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
169 GL_BGRA, GL_UNSIGNED_BYTE, data);
170
171 es->map.x = x;
172 es->map.y = y;
173 es->map.width = width;
174 es->map.height = height;
175 es->surface = EGL_NO_SURFACE;
176
177 return es;
178}
179
180static void
181egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec)
182{
183 glDeleteTextures(1, &es->texture);
184 if (es->surface != EGL_NO_SURFACE)
185 eglDestroySurface(ec->display, es->surface);
186 free(es);
187}
188
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400189static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500190pointer_path(cairo_t *cr, int x, int y)
191{
192 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
193 const int width = 16, height = 16;
194
195 cairo_move_to(cr, x, y);
196 cairo_line_to(cr, x + tx, y + ty);
197 cairo_line_to(cr, x + dx, y + dy);
198 cairo_line_to(cr, x + width - end, y + height);
199 cairo_line_to(cr, x + width, y + height - end);
200 cairo_line_to(cr, x + dy, y + dx);
201 cairo_line_to(cr, x + ty, y + tx);
202 cairo_close_path(cr);
203}
204
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500205static struct egl_surface *
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500206pointer_create(int x, int y, int width, int height)
207{
Kristian Høgsberg54879822008-11-23 17:07:32 -0500208 struct egl_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500209 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500210 cairo_surface_t *surface;
211 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500212
213 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
214 width, height);
215
216 cr = cairo_create(surface);
217 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
218 cairo_set_line_width (cr, 2);
219 cairo_set_source_rgb(cr, 0, 0, 0);
220 cairo_stroke_preserve(cr);
221 cairo_fill(cr);
222 blur_surface(surface, width);
223
224 pointer_path(cr, hotspot_x, hotspot_y);
225 cairo_stroke_preserve(cr);
226 cairo_set_source_rgb(cr, 1, 1, 1);
227 cairo_fill(cr);
228 cairo_destroy(cr);
229
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500230 es = egl_surface_create_from_cairo_surface(surface,
231 x - hotspot_x,
232 y - hotspot_y,
233 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500234
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500235 cairo_surface_destroy(surface);
236
Kristian Høgsberg54879822008-11-23 17:07:32 -0500237 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500238}
239
240static struct egl_surface *
241background_create(const char *filename, int width, int height)
242{
243 struct egl_surface *background;
244 GdkPixbuf *pixbuf;
245 GError *error = NULL;
246 int pixbuf_width, pixbuf_height;
247 void *data;
248
249 background = malloc(sizeof *background);
250 if (background == NULL)
251 return NULL;
252
253 g_type_init();
254
255 pixbuf = gdk_pixbuf_new_from_file(filename, &error);
256 if (error != NULL) {
257 free(background);
258 return NULL;
259 }
260
261 pixbuf_width = gdk_pixbuf_get_width(pixbuf);
262 pixbuf_height = gdk_pixbuf_get_height(pixbuf);
263 data = gdk_pixbuf_get_pixels(pixbuf);
264
265 glGenTextures(1, &background->texture);
266 glBindTexture(GL_TEXTURE_2D, background->texture);
267 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500268 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
269 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
270 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500271 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixbuf_width, pixbuf_height, 0,
272 GL_BGR, GL_UNSIGNED_BYTE, data);
273
274 background->map.x = 0;
275 background->map.y = 0;
276 background->map.width = width;
277 background->map.height = height;
278 background->surface = EGL_NO_SURFACE;
279
280 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500281}
282
283static void
Kristian Høgsberg54879822008-11-23 17:07:32 -0500284rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
285{
286 cairo_move_to(cr, x0, y0 + radius);
287 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
288 cairo_line_to(cr, x1 - radius, y0);
289 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
290 cairo_line_to(cr, x1, y1 - radius);
291 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
292 cairo_line_to(cr, x0 + radius, y1);
293 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
294 cairo_close_path(cr);
295}
296
297static void
298draw_button(cairo_t *cr, int x, int y, int width, int height, const char *text)
299{
300 cairo_pattern_t *gradient;
301 cairo_text_extents_t extents;
302 double bright = 0.15, dim = 0.02;
303 int radius = 10;
304
305 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
306 cairo_set_line_width (cr, 2);
307 rounded_rect(cr, x, y, x + width, y + height, radius);
308 cairo_set_source_rgb(cr, dim, dim, dim);
309 cairo_stroke(cr);
310 rounded_rect(cr, x + 2, y + 2, x + width, y + height, radius);
311 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
312 cairo_stroke(cr);
313
314 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
315 cairo_set_source_rgb(cr, bright, bright, bright);
316 cairo_stroke(cr);
317 rounded_rect(cr, x + 3, y + 3, x + width - 1, y + height - 1, radius - 1);
318 cairo_set_source_rgb(cr, dim, dim, dim);
319 cairo_stroke(cr);
320
321 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
322 gradient = cairo_pattern_create_linear (0, y, 0, y + height);
323 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.15, 0.15, 0.15);
324 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.08, 0.08, 0.08);
325 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.07, 0.07, 0.07);
326 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.1, 0.1, 0.1);
327 cairo_set_source(cr, gradient);
328 cairo_fill(cr);
329
330 cairo_set_font_size(cr, 16);
331 cairo_text_extents(cr, text, &extents);
332 cairo_move_to(cr, x + (width - extents.width) / 2, y + (height - extents.height) / 2 - extents.y_bearing);
333 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
334 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
335 cairo_set_line_width (cr, 4);
336 cairo_text_path(cr, text);
337 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
338 cairo_stroke_preserve(cr);
339 cairo_set_source_rgb(cr, 1, 1, 1);
340 cairo_fill(cr);
341}
342
343static struct egl_surface *
344overlay_create(int x, int y, int width, int height)
345{
346 struct egl_surface *es;
347 cairo_surface_t *surface;
348 cairo_t *cr;
349 int total_width, button_x, button_y;
350 const int button_width = 150;
351 const int button_height = 40;
352 const int spacing = 50;
353
354 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
355 width, height);
356
357 cr = cairo_create(surface);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500358 cairo_set_source_rgba(cr, 0.1, 0.1, 0.1, 0.8);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500359 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
360 cairo_paint(cr);
361
362 total_width = button_width * 2 + spacing;
363 button_x = (width - total_width) / 2;
364 button_y = height - button_height - 20;
365 draw_button(cr, button_x, button_y, button_width, button_height, "Previous");
366 button_x += button_width + spacing;
367 draw_button(cr, button_x, button_y, button_width, button_height, "Next");
368
369 cairo_destroy(cr);
370
371 es = egl_surface_create_from_cairo_surface(surface, x, y, width, height);
372
373 cairo_surface_destroy(surface);
374
375 return es;
376}
377
378static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500379draw_surface(struct egl_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500380{
381 GLint vertices[12];
382 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
383 GLuint indices[4] = { 0, 1, 2, 3 };
384
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500385 vertices[0] = es->map.x;
386 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500387 vertices[2] = 0;
388
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500389 vertices[3] = es->map.x;
390 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500391 vertices[5] = 0;
392
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500393 vertices[6] = es->map.x + es->map.width;
394 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500395 vertices[8] = 0;
396
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500397 vertices[9] = es->map.x + es->map.width;
398 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500399 vertices[11] = 0;
400
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500401 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500402 glEnable(GL_TEXTURE_2D);
403 glEnable(GL_BLEND);
404 /* Assume pre-multiplied alpha for now, this probably
405 * needs to be a wayland visual type of thing. */
406 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
407
408 glEnableClientState(GL_VERTEX_ARRAY);
409 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
410 glVertexPointer(3, GL_INT, 0, vertices);
411 glTexCoordPointer(2, GL_INT, 0, tex_coords);
412 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
413}
414
415static void
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500416schedule_repaint(struct egl_compositor *ec);
417
418static void
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500419animate_overlay(struct egl_compositor *ec)
420{
421 double force, y;
422 int32_t top, bottom;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500423#if 1
424 double bounce = 0.0;
425 double friction = 1.0;
426 double spring = 0.2;
427#else
428 double bounce = 0.2;
429 double friction = 0.04;
430 double spring = 0.09;
431#endif
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500432
433 y = ec->overlay_y;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500434 force = (ec->overlay_target - ec->overlay_y) * spring +
435 (ec->overlay_previous - y) * friction;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500436
437 ec->overlay_y = y + (y - ec->overlay_previous) + force;
438 ec->overlay_previous = y;
439
440 top = ec->height - ec->overlay->map.height;
441 bottom = ec->height;
442 if (ec->overlay_y >= bottom) {
443 ec->overlay_y = bottom;
444 ec->overlay_previous = bottom;
445 }
446
447 if (ec->overlay_y <= top) {
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500448 ec->overlay_y = top + bounce * (top - ec->overlay_y);
449 ec->overlay_previous =
450 top + bounce * (top - ec->overlay_previous);
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500451 }
452
453 ec->overlay->map.y = ec->overlay_y + 0.5;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500454
Kristian Høgsberg73c30582008-11-25 22:45:46 -0500455 if (fabs(y - ec->overlay_target) > 0.2 ||
456 fabs(ec->overlay_y - ec->overlay_target) > 0.2)
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500457 schedule_repaint(ec);
458}
459
460static void
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500461repaint(void *data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400462{
463 struct egl_compositor *ec = data;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500464 struct egl_surface *es;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500465 struct egl_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500466 struct timespec ts;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500467 uint32_t msecs;
468
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500469 if (!ec->repaint_needed) {
470 ec->repaint_on_timeout = 0;
471 return;
472 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500473
474 draw_surface(ec->background);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400475
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500476 es = container_of(ec->surface_list.next,
477 struct egl_surface, link);
478 while (&es->link != &ec->surface_list) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500479 draw_surface(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500480
481 es = container_of(es->link.next,
482 struct egl_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400483 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400484
Kristian Høgsberg54879822008-11-23 17:07:32 -0500485 draw_surface(ec->overlay);
486
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500487 eid = container_of(ec->input_device_list.next,
488 struct egl_input_device, link);
489 while (&eid->link != &ec->input_device_list) {
490 draw_surface(eid->pointer_surface);
491
492 eid = container_of(eid->link.next,
493 struct egl_input_device, link);
494 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500495
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400496 eglSwapBuffers(ec->display, ec->surface);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500497 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500498
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500499 clock_gettime(CLOCK_MONOTONIC, &ts);
500 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
501 wl_display_post_frame(ec->wl_display, ec->current_frame, msecs);
502 ec->current_frame++;
503
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500504 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500505 ec->repaint_on_timeout = 1;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500506
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500507 animate_overlay(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400508}
509
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500510static void
511schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400512{
513 struct wl_event_loop *loop;
514
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500515 ec->repaint_needed = 1;
516 if (!ec->repaint_on_timeout) {
517 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500518 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500519 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400520}
521
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500522static void
523notify_surface_create(struct wl_compositor *compositor,
524 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400525{
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500526 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500527 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400528
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500529 es = malloc(sizeof *es);
530 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400531 return;
532
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500533 es->surface = EGL_NO_SURFACE;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500534 es->wl_surface = surface;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500535 wl_surface_set_data(surface, es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500536 wl_list_insert(ec->surface_list.prev, &es->link);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400537
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500538 glGenTextures(1, &es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400539}
540
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500541static void
542notify_surface_destroy(struct wl_compositor *compositor,
543 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400544{
545 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500546 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400547
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500548 es = wl_surface_get_data(surface);
549 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400550 return;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500551
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500552 wl_list_remove(&es->link);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500553 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400554
555 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400556}
557
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500558static void
559notify_surface_attach(struct wl_compositor *compositor,
560 struct wl_surface *surface, uint32_t name,
561 uint32_t width, uint32_t height, uint32_t stride)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400562{
563 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500564 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400565
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500566 es = wl_surface_get_data(surface);
567 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400568 return;
569
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500570 if (es->surface != EGL_NO_SURFACE)
571 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400572
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500573 es->width = width;
574 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500575 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500576 name, width, height, stride, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400577
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500578 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400579 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500580 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
581 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
582 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500583 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400584}
585
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500586static void
587notify_surface_map(struct wl_compositor *compositor,
588 struct wl_surface *surface, struct wl_map *map)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400589{
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500590 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400591
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500592 es = wl_surface_get_data(surface);
593 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400594 return;
595
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500596 es->map = *map;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400597}
598
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500599static void
600notify_surface_copy(struct wl_compositor *compositor,
601 struct wl_surface *surface,
602 int32_t dst_x, int32_t dst_y,
603 uint32_t name, uint32_t stride,
604 int32_t x, int32_t y, int32_t width, int32_t height)
605{
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500606 struct egl_compositor *ec = (struct egl_compositor *) compositor;
607 EGLSurface src;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500608 struct egl_surface *es;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500609
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500610 es = wl_surface_get_data(surface);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500611
612 /* FIXME: glCopyPixels should work, but then we'll have to
613 * call eglMakeCurrent to set up the src and dest surfaces
614 * first. This seems cheaper, but maybe there's a better way
615 * to accomplish this. */
616
617 src = eglCreateSurfaceForName(ec->display, ec->config,
618 name, x + width, y + height, stride, NULL);
619
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500620 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500621 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500622 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500623}
624
625static void
626notify_surface_damage(struct wl_compositor *compositor,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500627 struct wl_surface *surface,
628 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500629{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500630 /* FIXME: This need to take a damage region, of course. */
631}
632
633static uint32_t
634notify_commit(struct wl_compositor *compositor)
635{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500636 struct egl_compositor *ec = (struct egl_compositor *) compositor;
637
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500638 schedule_repaint(ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500639
640 return ec->current_frame;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500641}
642
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500643static struct egl_surface *
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500644pick_surface(struct egl_input_device *device)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500645{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500646 struct egl_compositor *ec = device->ec;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500647 struct egl_surface *es;
648
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500649 if (device->grab > 0)
650 return device->grab_surface;
651
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500652 es = container_of(ec->surface_list.prev,
653 struct egl_surface, link);
654 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500655 if (es->map.x <= device->x &&
656 device->x < es->map.x + es->map.width &&
657 es->map.y <= device->y &&
658 device->y < es->map.y + es->map.height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500659 return es;
660
661 es = container_of(es->link.prev,
662 struct egl_surface, link);
663 }
664
665 return NULL;
666}
667
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500668void
669notify_motion(struct egl_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500670{
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500671 struct egl_surface *es;
672 const int hotspot_x = 16, hotspot_y = 16;
673 int32_t sx, sy;
674
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500675 es = pick_surface(device);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500676
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500677 if (es) {
678 sx = (x - es->map.x) * es->width / es->map.width;
679 sy = (y - es->map.y) * es->height / es->map.height;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500680 wl_surface_post_event(es->wl_surface, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500681 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500682 }
683
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500684 device->x = x;
685 device->y = y;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500686 device->pointer_surface->map.x = x - hotspot_x;
687 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500688
689 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500690}
691
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500692void
693notify_button(struct egl_input_device *device,
694 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500695{
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500696 struct egl_surface *es;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500697 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500698
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500699 es = pick_surface(device);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500700 if (es) {
701 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500702 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500703
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500704 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500705 /* FIXME: We need callbacks when the surfaces
706 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500707 device->grab++;
708 device->grab_surface = es;
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500709 device->focus_surface = es;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500710 } else {
711 device->grab--;
712 }
713
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500714 sx = (device->x - es->map.x) * es->width / es->map.width;
715 sy = (device->y - es->map.y) * es->height / es->map.height;
716
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500717 /* FIXME: Swallow click on raise? */
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500718 wl_surface_post_event(es->wl_surface, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500719 WL_INPUT_BUTTON, button, state,
720 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500721
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500722 schedule_repaint(device->ec);
723 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500724}
725
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500726void
727notify_key(struct egl_input_device *device,
728 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500729{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500730 struct egl_compositor *ec = device->ec;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500731
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500732 if (key == KEY_ESC && state == 1) {
733 if (ec->overlay_target == ec->height)
734 ec->overlay_target -= 200;
735 else
736 ec->overlay_target += 200;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500737 schedule_repaint(ec);
Kristian Høgsberg7fdff042008-12-10 13:25:00 -0500738 } else if (!wl_list_empty(&ec->surface_list)) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500739 if (device->focus_surface != NULL)
740 wl_surface_post_event(device->focus_surface->wl_surface,
741 &device->base,
742 WL_INPUT_KEY, key, state);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500743 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500744}
745
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500746static const struct wl_compositor_interface interface = {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400747 notify_surface_create,
748 notify_surface_destroy,
749 notify_surface_attach,
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500750 notify_surface_map,
751 notify_surface_copy,
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500752 notify_surface_damage,
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500753 notify_commit,
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400754};
755
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500756struct evdev_input_device *
757evdev_input_device_create(struct egl_input_device *device,
758 struct wl_display *display, const char *path);
759
760void
761egl_device_get_position(struct egl_input_device *device, int32_t *x, int32_t *y);
762
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500763static void
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500764create_input_device(struct egl_compositor *ec, const char *glob)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500765{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500766 struct egl_input_device *device;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500767 struct dirent *de;
768 char path[PATH_MAX];
769 const char *by_path_dir = "/dev/input/by-path";
770 DIR *dir;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500771
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500772 device = malloc(sizeof *device);
773 if (device == NULL)
774 return;
775
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500776 memset(device, 0, sizeof *device);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500777 device->base.interface = wl_input_device_get_interface();
778 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500779 device->x = 100;
780 device->y = 100;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500781 device->pointer_surface = pointer_create(device->x, device->y, 64, 64);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500782 device->ec = ec;
783
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500784 dir = opendir(by_path_dir);
785 if (dir == NULL) {
786 fprintf(stderr, "couldn't read dir %s\n", by_path_dir);
787 return;
788 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500789
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500790 while (de = readdir(dir), de != NULL) {
791 if (fnmatch(glob, de->d_name, 0))
792 continue;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500793
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500794 snprintf(path, sizeof path, "%s/%s", by_path_dir, de->d_name);
795 evdev_input_device_create(device, ec->wl_display, path);
796 }
797 closedir(dir);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500798
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500799 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500800}
801
802void
803egl_device_get_position(struct egl_input_device *device, int32_t *x, int32_t *y)
804{
805 *x = device->x;
806 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500807}
808
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500809static uint32_t
810create_frontbuffer(int fd, int *width, int *height, int *stride)
811{
812 drmModeConnector *connector;
813 drmModeRes *resources;
814 drmModeEncoder *encoder;
815 struct drm_mode_modeinfo *mode;
816 struct drm_i915_gem_create create;
817 struct drm_i915_gem_pin pin;
818 struct drm_gem_flink flink;
819 unsigned int fb_id;
820 int i, ret;
821
822 resources = drmModeGetResources(fd);
823 if (!resources) {
824 fprintf(stderr, "drmModeGetResources failed\n");
825 return 0;
826 }
827
828 for (i = 0; i < resources->count_connectors; i++) {
829 connector = drmModeGetConnector(fd, resources->connectors[i]);
830 if (connector == NULL)
831 continue;
832
833 if (connector->connection == DRM_MODE_CONNECTED &&
834 connector->count_modes > 0)
835 break;
836
837 drmModeFreeConnector(connector);
838 }
839
840 if (i == resources->count_connectors) {
841 fprintf(stderr, "No currently active connector found.\n");
842 return -1;
843 }
844
845 mode = &connector->modes[0];
846
847 for (i = 0; i < resources->count_encoders; i++) {
848 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
849
850 if (encoder == NULL)
851 continue;
852
853 if (encoder->encoder_id == connector->encoder_id)
854 break;
855
856 drmModeFreeEncoder(encoder);
857 }
858
859 /* Mode size at 32 bpp */
860 create.size = mode->hdisplay * mode->vdisplay * 4;
861 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
862 fprintf(stderr, "gem create failed: %m\n");
863 return 0;
864 }
865
866 pin.handle = create.handle;
867 pin.alignment = 4096;
868 if (ioctl(fd, DRM_IOCTL_I915_GEM_PIN, &pin)) {
869 fprintf(stderr, "failed to pin buffer: %m\n");
870 return 0;
871 }
872
873 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
874 32, 32, mode->hdisplay * 4, create.handle, &fb_id);
875 if (ret) {
876 fprintf(stderr, "failed to add fb: %m\n");
877 return 0;
878 }
879
880 ret = drmModeSetCrtc(fd, encoder->crtc_id, fb_id, 0, 0,
881 &connector->connector_id, 1, mode);
882 if (ret) {
883 fprintf(stderr, "failed to set mode: %m\n");
884 return 0;
885 }
886
887 flink.handle = create.handle;
888 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
889 fprintf(stderr, "gem flink failed: %m\n");
890 return 0;
891 }
892
893 *width = mode->hdisplay;
894 *height = mode->vdisplay;
895 *stride = mode->hdisplay * 4;
896
897 return flink.name;
898}
899
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500900static int
901pick_config(struct egl_compositor *ec)
902{
903 EGLConfig configs[100];
904 EGLint value, count;
905 int i;
906
907 if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) {
908 fprintf(stderr, "failed to get configs\n");
909 return -1;
910 }
911
912 ec->config = EGL_NO_CONFIG;
913 for (i = 0; i < count; i++) {
914 eglGetConfigAttrib(ec->display,
915 configs[i],
916 EGL_DEPTH_SIZE,
917 &value);
918 if (value > 0) {
919 fprintf(stderr, "config %d has depth size %d\n", i, value);
920 continue;
921 }
922
923 eglGetConfigAttrib(ec->display,
924 configs[i],
925 EGL_STENCIL_SIZE,
926 &value);
927 if (value > 0) {
928 fprintf(stderr, "config %d has stencil size %d\n", i, value);
929 continue;
930 }
931
932 eglGetConfigAttrib(ec->display,
933 configs[i],
934 EGL_CONFIG_CAVEAT,
935 &value);
936 if (value != EGL_NONE) {
937 fprintf(stderr, "config %d has caveat %d\n", i, value);
938 continue;
939 }
940
941 ec->config = configs[i];
942 break;
943 }
944
945 if (ec->config == EGL_NO_CONFIG) {
946 fprintf(stderr, "found no config without depth or stencil buffers\n");
947 return -1;
948 }
949
950 return 0;
951}
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500952
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400953static const char gem_device[] = "/dev/dri/card0";
954
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500955static const char *macbook_air_default_input_device[] = {
Kristian Høgsberg64949972008-12-12 14:48:46 -0500956 "pci-0000:00:1d.2-usb-0:2:1*event*",
957 NULL
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500958};
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500959
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500960static const char *option_background = "background.jpg";
961static const char **option_input_devices = macbook_air_default_input_device;
962
963static const GOptionEntry option_entries[] = {
964 { "background", 'b', 0, G_OPTION_ARG_STRING,
965 &option_background, "Background image" },
966 { "input-device", 'i', 0, G_OPTION_ARG_STRING_ARRAY,
967 &option_input_devices, "Input device glob" },
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500968 { NULL }
969};
970
Kristian Høgsberg122912c2008-12-05 11:13:50 -0500971static struct egl_compositor *
972egl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400973{
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500974 EGLint major, minor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400975 struct egl_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500976 struct screenshooter *shooter;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500977 uint32_t fb_name;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500978 int i, stride;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500979 struct wl_event_loop *loop;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500980 const static EGLint attribs[] =
981 { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE };
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400982
983 ec = malloc(sizeof *ec);
984 if (ec == NULL)
985 return NULL;
986
987 ec->base.interface = &interface;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400988 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400989
Kristian Høgsbergc508d932008-10-13 22:52:42 -0400990 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400991 if (ec->display == NULL) {
992 fprintf(stderr, "failed to create display\n");
993 return NULL;
994 }
995
996 if (!eglInitialize(ec->display, &major, &minor)) {
997 fprintf(stderr, "failed to initialize display\n");
998 return NULL;
999 }
1000
Kristian Høgsberg443853c2008-11-25 12:12:05 -05001001 if (pick_config(ec))
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001002 return NULL;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001003
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001004 fb_name = create_frontbuffer(eglGetDisplayFD(ec->display),
1005 &ec->width, &ec->height, &stride);
1006 ec->surface = eglCreateSurfaceForName(ec->display, ec->config,
1007 fb_name, ec->width, ec->height, stride, attribs);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001008 if (ec->surface == NULL) {
1009 fprintf(stderr, "failed to create surface\n");
1010 return NULL;
1011 }
1012
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -05001013 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001014 if (ec->context == NULL) {
1015 fprintf(stderr, "failed to create context\n");
1016 return NULL;
1017 }
1018
1019 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
1020 fprintf(stderr, "failed to make context current\n");
1021 return NULL;
1022 }
1023
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001024 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001025 glMatrixMode(GL_PROJECTION);
1026 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001027 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001028 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001029
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001030 wl_list_init(&ec->input_device_list);
1031 for (i = 0; option_input_devices[i]; i++)
1032 create_input_device(ec, option_input_devices[i]);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001033
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001034 wl_list_init(&ec->surface_list);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001035 ec->background = background_create(option_background,
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001036 ec->width, ec->height);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001037 ec->overlay = overlay_create(0, ec->height, ec->width, 200);
Kristian Høgsberg9f88b182008-12-08 13:52:08 -05001038 ec->overlay_y = ec->height;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001039 ec->overlay_target = ec->height;
1040 ec->overlay_previous = ec->height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001041
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001042 ec->gem_fd = open(gem_device, O_RDWR);
1043 if (ec->gem_fd < 0) {
1044 fprintf(stderr, "failed to open drm device\n");
1045 return NULL;
1046 }
1047
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001048 shooter = screenshooter_create(ec);
1049 wl_display_add_object(display, &shooter->base);
1050 wl_display_add_global(display, &shooter->base);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001051
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001052 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001053 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001054 ec->repaint_needed = 0;
1055 ec->repaint_on_timeout = 0;
1056
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001057 schedule_repaint(ec);
1058
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001059 return ec;
1060}
1061
1062/* The plan here is to generate a random anonymous socket name and
1063 * advertise that through a service on the session dbus.
1064 */
1065static const char socket_name[] = "\0wayland";
1066
1067int main(int argc, char *argv[])
1068{
1069 struct wl_display *display;
1070 struct egl_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001071 GError *error = NULL;
1072 GOptionContext *context;
1073
1074 context = g_option_context_new(NULL);
1075 g_option_context_add_main_entries(context, option_entries, "Wayland");
1076 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1077 fprintf(stderr, "option parsing failed: %s\n", error->message);
1078 exit(EXIT_FAILURE);
1079 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001080
1081 display = wl_display_create();
1082
1083 ec = egl_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001084 if (ec == NULL) {
1085 fprintf(stderr, "failed to create compositor\n");
1086 exit(EXIT_FAILURE);
1087 }
1088
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001089 wl_display_set_compositor(display, &ec->base);
1090
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001091 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001092 fprintf(stderr, "failed to add socket: %m\n");
1093 exit(EXIT_FAILURE);
1094 }
1095
1096 wl_display_run(display);
1097
1098 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001099}