blob: 2cf9ec121b7ff85f45022b57010324f493cb6185 [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øgsbergd2412e22008-12-15 20:35:24 -050092 struct wl_surface base;
93 struct egl_compositor *compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040094 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040095 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050096 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -050097 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -050098 struct wl_list link;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040099};
100
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500101struct screenshooter {
102 struct wl_object base;
103 struct egl_compositor *ec;
104};
105
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500106struct screenshooter_interface {
107 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
108};
109
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500110static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500111screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500112{
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500113 struct egl_compositor *ec = shooter->ec;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500114 GLuint stride;
115 static const char filename[] = "wayland-screenshot.png";
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500116 GdkPixbuf *pixbuf;
117 GError *error = NULL;
118 void *data;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500119
120 data = eglReadBuffer(ec->display, ec->surface, GL_FRONT_LEFT, &stride);
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500121 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
122 8, ec->width, ec->height, stride,
123 NULL, NULL);
124 gdk_pixbuf_save(pixbuf, filename, "png", &error, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500125}
126
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500127static const struct wl_method screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500128 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500129};
130
131static const struct wl_interface screenshooter_interface = {
132 "screenshooter", 1,
133 ARRAY_LENGTH(screenshooter_methods),
134 screenshooter_methods,
135};
136
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500137struct screenshooter_interface screenshooter_implementation = {
138 screenshooter_shoot
139};
140
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500141static struct screenshooter *
142screenshooter_create(struct egl_compositor *ec)
143{
144 struct screenshooter *shooter;
145
146 shooter = malloc(sizeof *shooter);
147 if (shooter == NULL)
148 return NULL;
149
150 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500151 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500152 shooter->ec = ec;
153
154 return shooter;
155};
156
Kristian Høgsberg54879822008-11-23 17:07:32 -0500157static struct egl_surface *
158egl_surface_create_from_cairo_surface(cairo_surface_t *surface,
159 int x, int y, int width, int height)
160{
161 struct egl_surface *es;
162 int stride;
163 void *data;
164
165 stride = cairo_image_surface_get_stride(surface);
166 data = cairo_image_surface_get_data(surface);
167
168 es = malloc(sizeof *es);
169 if (es == NULL)
170 return NULL;
171
172 glGenTextures(1, &es->texture);
173 glBindTexture(GL_TEXTURE_2D, es->texture);
174 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500175 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
176 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
177 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500178 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
179 GL_BGRA, GL_UNSIGNED_BYTE, data);
180
181 es->map.x = x;
182 es->map.y = y;
183 es->map.width = width;
184 es->map.height = height;
185 es->surface = EGL_NO_SURFACE;
186
187 return es;
188}
189
190static void
191egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec)
192{
193 glDeleteTextures(1, &es->texture);
194 if (es->surface != EGL_NO_SURFACE)
195 eglDestroySurface(ec->display, es->surface);
196 free(es);
197}
198
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400199static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500200pointer_path(cairo_t *cr, int x, int y)
201{
202 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
203 const int width = 16, height = 16;
204
205 cairo_move_to(cr, x, y);
206 cairo_line_to(cr, x + tx, y + ty);
207 cairo_line_to(cr, x + dx, y + dy);
208 cairo_line_to(cr, x + width - end, y + height);
209 cairo_line_to(cr, x + width, y + height - end);
210 cairo_line_to(cr, x + dy, y + dx);
211 cairo_line_to(cr, x + ty, y + tx);
212 cairo_close_path(cr);
213}
214
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500215static struct egl_surface *
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500216pointer_create(int x, int y, int width, int height)
217{
Kristian Høgsberg54879822008-11-23 17:07:32 -0500218 struct egl_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500219 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500220 cairo_surface_t *surface;
221 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500222
223 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
224 width, height);
225
226 cr = cairo_create(surface);
227 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
228 cairo_set_line_width (cr, 2);
229 cairo_set_source_rgb(cr, 0, 0, 0);
230 cairo_stroke_preserve(cr);
231 cairo_fill(cr);
232 blur_surface(surface, width);
233
234 pointer_path(cr, hotspot_x, hotspot_y);
235 cairo_stroke_preserve(cr);
236 cairo_set_source_rgb(cr, 1, 1, 1);
237 cairo_fill(cr);
238 cairo_destroy(cr);
239
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500240 es = egl_surface_create_from_cairo_surface(surface,
241 x - hotspot_x,
242 y - hotspot_y,
243 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500244
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500245 cairo_surface_destroy(surface);
246
Kristian Høgsberg54879822008-11-23 17:07:32 -0500247 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500248}
249
250static struct egl_surface *
251background_create(const char *filename, int width, int height)
252{
253 struct egl_surface *background;
254 GdkPixbuf *pixbuf;
255 GError *error = NULL;
256 int pixbuf_width, pixbuf_height;
257 void *data;
258
259 background = malloc(sizeof *background);
260 if (background == NULL)
261 return NULL;
262
263 g_type_init();
264
265 pixbuf = gdk_pixbuf_new_from_file(filename, &error);
266 if (error != NULL) {
267 free(background);
268 return NULL;
269 }
270
271 pixbuf_width = gdk_pixbuf_get_width(pixbuf);
272 pixbuf_height = gdk_pixbuf_get_height(pixbuf);
273 data = gdk_pixbuf_get_pixels(pixbuf);
274
275 glGenTextures(1, &background->texture);
276 glBindTexture(GL_TEXTURE_2D, background->texture);
277 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500278 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
279 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
280 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500281 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixbuf_width, pixbuf_height, 0,
282 GL_BGR, GL_UNSIGNED_BYTE, data);
283
284 background->map.x = 0;
285 background->map.y = 0;
286 background->map.width = width;
287 background->map.height = height;
288 background->surface = EGL_NO_SURFACE;
289
290 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500291}
292
293static void
Kristian Høgsberg54879822008-11-23 17:07:32 -0500294rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
295{
296 cairo_move_to(cr, x0, y0 + radius);
297 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
298 cairo_line_to(cr, x1 - radius, y0);
299 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
300 cairo_line_to(cr, x1, y1 - radius);
301 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
302 cairo_line_to(cr, x0 + radius, y1);
303 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
304 cairo_close_path(cr);
305}
306
307static void
308draw_button(cairo_t *cr, int x, int y, int width, int height, const char *text)
309{
310 cairo_pattern_t *gradient;
311 cairo_text_extents_t extents;
312 double bright = 0.15, dim = 0.02;
313 int radius = 10;
314
315 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
316 cairo_set_line_width (cr, 2);
317 rounded_rect(cr, x, y, x + width, y + height, radius);
318 cairo_set_source_rgb(cr, dim, dim, dim);
319 cairo_stroke(cr);
320 rounded_rect(cr, x + 2, y + 2, x + width, y + height, radius);
321 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
322 cairo_stroke(cr);
323
324 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
325 cairo_set_source_rgb(cr, bright, bright, bright);
326 cairo_stroke(cr);
327 rounded_rect(cr, x + 3, y + 3, x + width - 1, y + height - 1, radius - 1);
328 cairo_set_source_rgb(cr, dim, dim, dim);
329 cairo_stroke(cr);
330
331 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
332 gradient = cairo_pattern_create_linear (0, y, 0, y + height);
333 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.15, 0.15, 0.15);
334 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.08, 0.08, 0.08);
335 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.07, 0.07, 0.07);
336 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.1, 0.1, 0.1);
337 cairo_set_source(cr, gradient);
338 cairo_fill(cr);
339
340 cairo_set_font_size(cr, 16);
341 cairo_text_extents(cr, text, &extents);
342 cairo_move_to(cr, x + (width - extents.width) / 2, y + (height - extents.height) / 2 - extents.y_bearing);
343 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
344 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
345 cairo_set_line_width (cr, 4);
346 cairo_text_path(cr, text);
347 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
348 cairo_stroke_preserve(cr);
349 cairo_set_source_rgb(cr, 1, 1, 1);
350 cairo_fill(cr);
351}
352
353static struct egl_surface *
354overlay_create(int x, int y, int width, int height)
355{
356 struct egl_surface *es;
357 cairo_surface_t *surface;
358 cairo_t *cr;
359 int total_width, button_x, button_y;
360 const int button_width = 150;
361 const int button_height = 40;
362 const int spacing = 50;
363
364 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
365 width, height);
366
367 cr = cairo_create(surface);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500368 cairo_set_source_rgba(cr, 0.1, 0.1, 0.1, 0.8);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500369 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
370 cairo_paint(cr);
371
372 total_width = button_width * 2 + spacing;
373 button_x = (width - total_width) / 2;
374 button_y = height - button_height - 20;
375 draw_button(cr, button_x, button_y, button_width, button_height, "Previous");
376 button_x += button_width + spacing;
377 draw_button(cr, button_x, button_y, button_width, button_height, "Next");
378
379 cairo_destroy(cr);
380
381 es = egl_surface_create_from_cairo_surface(surface, x, y, width, height);
382
383 cairo_surface_destroy(surface);
384
385 return es;
386}
387
388static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500389draw_surface(struct egl_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500390{
391 GLint vertices[12];
392 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
393 GLuint indices[4] = { 0, 1, 2, 3 };
394
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500395 vertices[0] = es->map.x;
396 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500397 vertices[2] = 0;
398
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500399 vertices[3] = es->map.x;
400 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500401 vertices[5] = 0;
402
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500403 vertices[6] = es->map.x + es->map.width;
404 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500405 vertices[8] = 0;
406
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500407 vertices[9] = es->map.x + es->map.width;
408 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500409 vertices[11] = 0;
410
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500411 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500412 glEnable(GL_TEXTURE_2D);
413 glEnable(GL_BLEND);
414 /* Assume pre-multiplied alpha for now, this probably
415 * needs to be a wayland visual type of thing. */
416 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
417
418 glEnableClientState(GL_VERTEX_ARRAY);
419 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
420 glVertexPointer(3, GL_INT, 0, vertices);
421 glTexCoordPointer(2, GL_INT, 0, tex_coords);
422 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
423}
424
425static void
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500426schedule_repaint(struct egl_compositor *ec);
427
428static void
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500429animate_overlay(struct egl_compositor *ec)
430{
431 double force, y;
432 int32_t top, bottom;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500433#if 1
434 double bounce = 0.0;
435 double friction = 1.0;
436 double spring = 0.2;
437#else
438 double bounce = 0.2;
439 double friction = 0.04;
440 double spring = 0.09;
441#endif
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500442
443 y = ec->overlay_y;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500444 force = (ec->overlay_target - ec->overlay_y) * spring +
445 (ec->overlay_previous - y) * friction;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500446
447 ec->overlay_y = y + (y - ec->overlay_previous) + force;
448 ec->overlay_previous = y;
449
450 top = ec->height - ec->overlay->map.height;
451 bottom = ec->height;
452 if (ec->overlay_y >= bottom) {
453 ec->overlay_y = bottom;
454 ec->overlay_previous = bottom;
455 }
456
457 if (ec->overlay_y <= top) {
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500458 ec->overlay_y = top + bounce * (top - ec->overlay_y);
459 ec->overlay_previous =
460 top + bounce * (top - ec->overlay_previous);
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500461 }
462
463 ec->overlay->map.y = ec->overlay_y + 0.5;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500464
Kristian Høgsberg73c30582008-11-25 22:45:46 -0500465 if (fabs(y - ec->overlay_target) > 0.2 ||
466 fabs(ec->overlay_y - ec->overlay_target) > 0.2)
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500467 schedule_repaint(ec);
468}
469
470static void
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500471repaint(void *data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400472{
473 struct egl_compositor *ec = data;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500474 struct egl_surface *es;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500475 struct egl_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500476 struct timespec ts;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500477 uint32_t msecs;
478
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500479 if (!ec->repaint_needed) {
480 ec->repaint_on_timeout = 0;
481 return;
482 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500483
484 draw_surface(ec->background);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400485
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500486 es = container_of(ec->surface_list.next,
487 struct egl_surface, link);
488 while (&es->link != &ec->surface_list) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500489 draw_surface(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500490
491 es = container_of(es->link.next,
492 struct egl_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400493 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400494
Kristian Høgsberg54879822008-11-23 17:07:32 -0500495 draw_surface(ec->overlay);
496
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500497 eid = container_of(ec->input_device_list.next,
498 struct egl_input_device, link);
499 while (&eid->link != &ec->input_device_list) {
500 draw_surface(eid->pointer_surface);
501
502 eid = container_of(eid->link.next,
503 struct egl_input_device, link);
504 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500505
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400506 eglSwapBuffers(ec->display, ec->surface);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500507 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500508
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500509 clock_gettime(CLOCK_MONOTONIC, &ts);
510 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500511 wl_display_post_frame(ec->wl_display, &ec->base,
512 ec->current_frame, msecs);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500513 ec->current_frame++;
514
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500515 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500516 ec->repaint_on_timeout = 1;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500517
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500518 animate_overlay(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400519}
520
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500521static void
522schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400523{
524 struct wl_event_loop *loop;
525
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500526 ec->repaint_needed = 1;
527 if (!ec->repaint_on_timeout) {
528 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500529 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500530 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400531}
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400532
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500533static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500534surface_destroy(struct wl_client *client,
535 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400536{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500537 struct egl_surface *es = (struct egl_surface *) surface;
538 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500539
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500540 wl_list_remove(&es->link);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500541 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400542
543 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400544}
545
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500546static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500547surface_attach(struct wl_client *client,
548 struct wl_surface *surface, uint32_t name,
549 uint32_t width, uint32_t height, uint32_t stride)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400550{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500551 struct egl_surface *es = (struct egl_surface *) surface;
552 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400553
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500554 if (es->surface != EGL_NO_SURFACE)
555 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400556
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500557 es->width = width;
558 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500559 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500560 name, width, height, stride, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400561
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500562 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400563 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500564 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
565 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
566 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500567 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400568}
569
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500570static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500571surface_map(struct wl_client *client,
572 struct wl_surface *surface,
573 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400574{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500575 struct egl_surface *es = (struct egl_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400576
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500577 es->map.x = x;
578 es->map.y = y;
579 es->map.width = width;
580 es->map.height = height;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400581}
582
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500583static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500584surface_copy(struct wl_client *client,
585 struct wl_surface *surface,
586 int32_t dst_x, int32_t dst_y,
587 uint32_t name, uint32_t stride,
588 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500589{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500590 struct egl_surface *es = (struct egl_surface *) surface;
591 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500592 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500593
594 /* FIXME: glCopyPixels should work, but then we'll have to
595 * call eglMakeCurrent to set up the src and dest surfaces
596 * first. This seems cheaper, but maybe there's a better way
597 * to accomplish this. */
598
599 src = eglCreateSurfaceForName(ec->display, ec->config,
600 name, x + width, y + height, stride, NULL);
601
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500602 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500603 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500604 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500605}
606
607static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500608surface_damage(struct wl_client *client,
609 struct wl_surface *surface,
610 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500611{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500612 /* FIXME: This need to take a damage region, of course. */
613}
614
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500615const static struct wl_surface_interface surface_interface = {
616 surface_destroy,
617 surface_attach,
618 surface_map,
619 surface_copy,
620 surface_damage
621};
622
623static void
624compositor_create_surface(struct wl_client *client,
625 struct wl_compositor *compositor, uint32_t id)
626{
627 struct egl_compositor *ec = (struct egl_compositor *) compositor;
628 struct egl_surface *es;
629
630 es = malloc(sizeof *es);
631 if (es == NULL)
632 /* FIXME: Send OOM event. */
633 return;
634
635 es->compositor = ec;
636 es->surface = EGL_NO_SURFACE;
637 wl_list_insert(ec->surface_list.prev, &es->link);
638 glGenTextures(1, &es->texture);
639 wl_client_add_surface(client, &es->base,
640 &surface_interface, id);
641}
642
643static void
644compositor_commit(struct wl_client *client,
645 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500646{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500647 struct egl_compositor *ec = (struct egl_compositor *) compositor;
648
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500649 schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500650 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500651}
652
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500653const static struct wl_compositor_interface compositor_interface = {
654 compositor_create_surface,
655 compositor_commit
656};
657
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500658static struct egl_surface *
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500659pick_surface(struct egl_input_device *device)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500660{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500661 struct egl_compositor *ec = device->ec;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500662 struct egl_surface *es;
663
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500664 if (device->grab > 0)
665 return device->grab_surface;
666
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500667 es = container_of(ec->surface_list.prev,
668 struct egl_surface, link);
669 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500670 if (es->map.x <= device->x &&
671 device->x < es->map.x + es->map.width &&
672 es->map.y <= device->y &&
673 device->y < es->map.y + es->map.height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500674 return es;
675
676 es = container_of(es->link.prev,
677 struct egl_surface, link);
678 }
679
680 return NULL;
681}
682
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500683void
684notify_motion(struct egl_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500685{
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500686 struct egl_surface *es;
687 const int hotspot_x = 16, hotspot_y = 16;
688 int32_t sx, sy;
689
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500690 es = pick_surface(device);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500691
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500692 if (es) {
693 sx = (x - es->map.x) * es->width / es->map.width;
694 sy = (y - es->map.y) * es->height / es->map.height;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500695 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500696 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500697 }
698
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500699 device->x = x;
700 device->y = y;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500701 device->pointer_surface->map.x = x - hotspot_x;
702 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500703
704 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500705}
706
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500707void
708notify_button(struct egl_input_device *device,
709 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500710{
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500711 struct egl_surface *es;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500712 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500713
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500714 es = pick_surface(device);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500715 if (es) {
716 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500717 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500718
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500719 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500720 /* FIXME: We need callbacks when the surfaces
721 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500722 device->grab++;
723 device->grab_surface = es;
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500724 device->focus_surface = es;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500725 } else {
726 device->grab--;
727 }
728
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500729 sx = (device->x - es->map.x) * es->width / es->map.width;
730 sy = (device->y - es->map.y) * es->height / es->map.height;
731
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500732 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500733 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500734 WL_INPUT_BUTTON, button, state,
735 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500736
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500737 schedule_repaint(device->ec);
738 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500739}
740
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500741void
742notify_key(struct egl_input_device *device,
743 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500744{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500745 struct egl_compositor *ec = device->ec;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500746
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500747 if (key == KEY_ESC && state == 1) {
748 if (ec->overlay_target == ec->height)
749 ec->overlay_target -= 200;
750 else
751 ec->overlay_target += 200;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500752 schedule_repaint(ec);
Kristian Høgsberg7fdff042008-12-10 13:25:00 -0500753 } else if (!wl_list_empty(&ec->surface_list)) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500754 if (device->focus_surface != NULL)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500755 wl_surface_post_event(&device->focus_surface->base,
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500756 &device->base,
757 WL_INPUT_KEY, key, state);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500758 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500759}
760
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500761struct evdev_input_device *
762evdev_input_device_create(struct egl_input_device *device,
763 struct wl_display *display, const char *path);
764
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500765static void
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500766create_input_device(struct egl_compositor *ec, const char *glob)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500767{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500768 struct egl_input_device *device;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500769 struct dirent *de;
770 char path[PATH_MAX];
771 const char *by_path_dir = "/dev/input/by-path";
772 DIR *dir;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500773
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500774 device = malloc(sizeof *device);
775 if (device == NULL)
776 return;
777
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500778 memset(device, 0, sizeof *device);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500779 device->base.interface = wl_input_device_get_interface();
780 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500781 device->x = 100;
782 device->y = 100;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500783 device->pointer_surface = pointer_create(device->x, device->y, 64, 64);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500784 device->ec = ec;
785
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500786 dir = opendir(by_path_dir);
787 if (dir == NULL) {
788 fprintf(stderr, "couldn't read dir %s\n", by_path_dir);
789 return;
790 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500791
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500792 while (de = readdir(dir), de != NULL) {
793 if (fnmatch(glob, de->d_name, 0))
794 continue;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500795
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500796 snprintf(path, sizeof path, "%s/%s", by_path_dir, de->d_name);
797 evdev_input_device_create(device, ec->wl_display, path);
798 }
799 closedir(dir);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500800
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500801 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500802}
803
804void
805egl_device_get_position(struct egl_input_device *device, int32_t *x, int32_t *y)
806{
807 *x = device->x;
808 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500809}
810
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500811static uint32_t
812create_frontbuffer(int fd, int *width, int *height, int *stride)
813{
814 drmModeConnector *connector;
815 drmModeRes *resources;
816 drmModeEncoder *encoder;
817 struct drm_mode_modeinfo *mode;
818 struct drm_i915_gem_create create;
819 struct drm_i915_gem_pin pin;
820 struct drm_gem_flink flink;
821 unsigned int fb_id;
822 int i, ret;
823
824 resources = drmModeGetResources(fd);
825 if (!resources) {
826 fprintf(stderr, "drmModeGetResources failed\n");
827 return 0;
828 }
829
830 for (i = 0; i < resources->count_connectors; i++) {
831 connector = drmModeGetConnector(fd, resources->connectors[i]);
832 if (connector == NULL)
833 continue;
834
835 if (connector->connection == DRM_MODE_CONNECTED &&
836 connector->count_modes > 0)
837 break;
838
839 drmModeFreeConnector(connector);
840 }
841
842 if (i == resources->count_connectors) {
843 fprintf(stderr, "No currently active connector found.\n");
844 return -1;
845 }
846
847 mode = &connector->modes[0];
848
849 for (i = 0; i < resources->count_encoders; i++) {
850 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
851
852 if (encoder == NULL)
853 continue;
854
855 if (encoder->encoder_id == connector->encoder_id)
856 break;
857
858 drmModeFreeEncoder(encoder);
859 }
860
861 /* Mode size at 32 bpp */
862 create.size = mode->hdisplay * mode->vdisplay * 4;
863 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
864 fprintf(stderr, "gem create failed: %m\n");
865 return 0;
866 }
867
868 pin.handle = create.handle;
869 pin.alignment = 4096;
870 if (ioctl(fd, DRM_IOCTL_I915_GEM_PIN, &pin)) {
871 fprintf(stderr, "failed to pin buffer: %m\n");
872 return 0;
873 }
874
875 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
876 32, 32, mode->hdisplay * 4, create.handle, &fb_id);
877 if (ret) {
878 fprintf(stderr, "failed to add fb: %m\n");
879 return 0;
880 }
881
882 ret = drmModeSetCrtc(fd, encoder->crtc_id, fb_id, 0, 0,
883 &connector->connector_id, 1, mode);
884 if (ret) {
885 fprintf(stderr, "failed to set mode: %m\n");
886 return 0;
887 }
888
889 flink.handle = create.handle;
890 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
891 fprintf(stderr, "gem flink failed: %m\n");
892 return 0;
893 }
894
895 *width = mode->hdisplay;
896 *height = mode->vdisplay;
897 *stride = mode->hdisplay * 4;
898
899 return flink.name;
900}
901
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500902static int
903pick_config(struct egl_compositor *ec)
904{
905 EGLConfig configs[100];
906 EGLint value, count;
907 int i;
908
909 if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) {
910 fprintf(stderr, "failed to get configs\n");
911 return -1;
912 }
913
914 ec->config = EGL_NO_CONFIG;
915 for (i = 0; i < count; i++) {
916 eglGetConfigAttrib(ec->display,
917 configs[i],
918 EGL_DEPTH_SIZE,
919 &value);
920 if (value > 0) {
921 fprintf(stderr, "config %d has depth size %d\n", i, value);
922 continue;
923 }
924
925 eglGetConfigAttrib(ec->display,
926 configs[i],
927 EGL_STENCIL_SIZE,
928 &value);
929 if (value > 0) {
930 fprintf(stderr, "config %d has stencil size %d\n", i, value);
931 continue;
932 }
933
934 eglGetConfigAttrib(ec->display,
935 configs[i],
936 EGL_CONFIG_CAVEAT,
937 &value);
938 if (value != EGL_NONE) {
939 fprintf(stderr, "config %d has caveat %d\n", i, value);
940 continue;
941 }
942
943 ec->config = configs[i];
944 break;
945 }
946
947 if (ec->config == EGL_NO_CONFIG) {
948 fprintf(stderr, "found no config without depth or stencil buffers\n");
949 return -1;
950 }
951
952 return 0;
953}
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500954
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400955static const char gem_device[] = "/dev/dri/card0";
956
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500957static const char *macbook_air_default_input_device[] = {
Kristian Høgsberg64949972008-12-12 14:48:46 -0500958 "pci-0000:00:1d.2-usb-0:2:1*event*",
959 NULL
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500960};
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500961
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500962static const char *option_background = "background.jpg";
963static const char **option_input_devices = macbook_air_default_input_device;
964
965static const GOptionEntry option_entries[] = {
966 { "background", 'b', 0, G_OPTION_ARG_STRING,
967 &option_background, "Background image" },
968 { "input-device", 'i', 0, G_OPTION_ARG_STRING_ARRAY,
969 &option_input_devices, "Input device glob" },
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500970 { NULL }
971};
972
Kristian Høgsberg122912c2008-12-05 11:13:50 -0500973static struct egl_compositor *
974egl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400975{
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500976 EGLint major, minor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400977 struct egl_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500978 struct screenshooter *shooter;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500979 uint32_t fb_name;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500980 int i, stride;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500981 struct wl_event_loop *loop;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500982 const static EGLint attribs[] =
983 { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE };
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400984
985 ec = malloc(sizeof *ec);
986 if (ec == NULL)
987 return NULL;
988
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400989 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400990
Kristian Høgsbergc508d932008-10-13 22:52:42 -0400991 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400992 if (ec->display == NULL) {
993 fprintf(stderr, "failed to create display\n");
994 return NULL;
995 }
996
997 if (!eglInitialize(ec->display, &major, &minor)) {
998 fprintf(stderr, "failed to initialize display\n");
999 return NULL;
1000 }
1001
Kristian Høgsberg443853c2008-11-25 12:12:05 -05001002 if (pick_config(ec))
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001003 return NULL;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001004
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001005 fb_name = create_frontbuffer(eglGetDisplayFD(ec->display),
1006 &ec->width, &ec->height, &stride);
1007 ec->surface = eglCreateSurfaceForName(ec->display, ec->config,
1008 fb_name, ec->width, ec->height, stride, attribs);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001009 if (ec->surface == NULL) {
1010 fprintf(stderr, "failed to create surface\n");
1011 return NULL;
1012 }
1013
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -05001014 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001015 if (ec->context == NULL) {
1016 fprintf(stderr, "failed to create context\n");
1017 return NULL;
1018 }
1019
1020 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
1021 fprintf(stderr, "failed to make context current\n");
1022 return NULL;
1023 }
1024
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001025 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001026 glMatrixMode(GL_PROJECTION);
1027 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001028 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001029 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001030
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001031 wl_display_set_compositor(display, &ec->base, &compositor_interface);
1032
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001033 wl_list_init(&ec->input_device_list);
1034 for (i = 0; option_input_devices[i]; i++)
1035 create_input_device(ec, option_input_devices[i]);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001036
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001037 wl_list_init(&ec->surface_list);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001038 ec->background = background_create(option_background,
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001039 ec->width, ec->height);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001040 ec->overlay = overlay_create(0, ec->height, ec->width, 200);
Kristian Høgsberg9f88b182008-12-08 13:52:08 -05001041 ec->overlay_y = ec->height;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001042 ec->overlay_target = ec->height;
1043 ec->overlay_previous = ec->height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001044
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001045 ec->gem_fd = open(gem_device, O_RDWR);
1046 if (ec->gem_fd < 0) {
1047 fprintf(stderr, "failed to open drm device\n");
1048 return NULL;
1049 }
1050
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001051 shooter = screenshooter_create(ec);
1052 wl_display_add_object(display, &shooter->base);
1053 wl_display_add_global(display, &shooter->base);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001054
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001055 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001056 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001057 ec->repaint_needed = 0;
1058 ec->repaint_on_timeout = 0;
1059
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001060 schedule_repaint(ec);
1061
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001062 return ec;
1063}
1064
1065/* The plan here is to generate a random anonymous socket name and
1066 * advertise that through a service on the session dbus.
1067 */
1068static const char socket_name[] = "\0wayland";
1069
1070int main(int argc, char *argv[])
1071{
1072 struct wl_display *display;
1073 struct egl_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001074 GError *error = NULL;
1075 GOptionContext *context;
1076
1077 context = g_option_context_new(NULL);
1078 g_option_context_add_main_entries(context, option_entries, "Wayland");
1079 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1080 fprintf(stderr, "option parsing failed: %s\n", error->message);
1081 exit(EXIT_FAILURE);
1082 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001083
1084 display = wl_display_create();
1085
1086 ec = egl_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001087 if (ec == NULL) {
1088 fprintf(stderr, "failed to create compositor\n");
1089 exit(EXIT_FAILURE);
1090 }
1091
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001092 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001093 fprintf(stderr, "failed to add socket: %m\n");
1094 exit(EXIT_FAILURE);
1095 }
1096
1097 wl_display_run(display);
1098
1099 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001100}