blob: 16c7fb00760d4811a6b1f9984f1d04fe13f73aa5 [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
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500484 if (ec->background)
485 draw_surface(ec->background);
486 else
487 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400488
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500489 es = container_of(ec->surface_list.next,
490 struct egl_surface, link);
491 while (&es->link != &ec->surface_list) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500492 draw_surface(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500493
494 es = container_of(es->link.next,
495 struct egl_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400496 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400497
Kristian Høgsberg54879822008-11-23 17:07:32 -0500498 draw_surface(ec->overlay);
499
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500500 eid = container_of(ec->input_device_list.next,
501 struct egl_input_device, link);
502 while (&eid->link != &ec->input_device_list) {
503 draw_surface(eid->pointer_surface);
504
505 eid = container_of(eid->link.next,
506 struct egl_input_device, link);
507 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500508
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400509 eglSwapBuffers(ec->display, ec->surface);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500510 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500511
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500512 clock_gettime(CLOCK_MONOTONIC, &ts);
513 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500514 wl_display_post_frame(ec->wl_display, &ec->base,
515 ec->current_frame, msecs);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500516 ec->current_frame++;
517
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500518 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500519 ec->repaint_on_timeout = 1;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500520
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500521 animate_overlay(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400522}
523
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500524static void
525schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400526{
527 struct wl_event_loop *loop;
528
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500529 ec->repaint_needed = 1;
530 if (!ec->repaint_on_timeout) {
531 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500532 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500533 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400534}
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400535
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500536static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500537surface_destroy(struct wl_client *client,
538 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400539{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500540 struct egl_surface *es = (struct egl_surface *) surface;
541 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500542
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500543 wl_list_remove(&es->link);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500544 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400545
546 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400547}
548
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500549static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500550surface_attach(struct wl_client *client,
551 struct wl_surface *surface, uint32_t name,
552 uint32_t width, uint32_t height, uint32_t stride)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400553{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500554 struct egl_surface *es = (struct egl_surface *) surface;
555 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400556
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500557 if (es->surface != EGL_NO_SURFACE)
558 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400559
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500560 es->width = width;
561 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500562 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500563 name, width, height, stride, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400564
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500565 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400566 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500567 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
568 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
569 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500570 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400571}
572
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500573static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500574surface_map(struct wl_client *client,
575 struct wl_surface *surface,
576 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400577{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500578 struct egl_surface *es = (struct egl_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400579
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500580 es->map.x = x;
581 es->map.y = y;
582 es->map.width = width;
583 es->map.height = height;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400584}
585
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500586static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500587surface_copy(struct wl_client *client,
588 struct wl_surface *surface,
589 int32_t dst_x, int32_t dst_y,
590 uint32_t name, uint32_t stride,
591 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500592{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500593 struct egl_surface *es = (struct egl_surface *) surface;
594 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500595 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500596
597 /* FIXME: glCopyPixels should work, but then we'll have to
598 * call eglMakeCurrent to set up the src and dest surfaces
599 * first. This seems cheaper, but maybe there's a better way
600 * to accomplish this. */
601
602 src = eglCreateSurfaceForName(ec->display, ec->config,
603 name, x + width, y + height, stride, NULL);
604
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500605 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500606 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500607 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500608}
609
610static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500611surface_damage(struct wl_client *client,
612 struct wl_surface *surface,
613 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500614{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500615 /* FIXME: This need to take a damage region, of course. */
616}
617
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500618const static struct wl_surface_interface surface_interface = {
619 surface_destroy,
620 surface_attach,
621 surface_map,
622 surface_copy,
623 surface_damage
624};
625
626static void
627compositor_create_surface(struct wl_client *client,
628 struct wl_compositor *compositor, uint32_t id)
629{
630 struct egl_compositor *ec = (struct egl_compositor *) compositor;
631 struct egl_surface *es;
632
633 es = malloc(sizeof *es);
634 if (es == NULL)
635 /* FIXME: Send OOM event. */
636 return;
637
638 es->compositor = ec;
639 es->surface = EGL_NO_SURFACE;
640 wl_list_insert(ec->surface_list.prev, &es->link);
641 glGenTextures(1, &es->texture);
642 wl_client_add_surface(client, &es->base,
643 &surface_interface, id);
644}
645
646static void
647compositor_commit(struct wl_client *client,
648 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500649{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500650 struct egl_compositor *ec = (struct egl_compositor *) compositor;
651
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500652 schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500653 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500654}
655
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500656const static struct wl_compositor_interface compositor_interface = {
657 compositor_create_surface,
658 compositor_commit
659};
660
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500661static struct egl_surface *
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500662pick_surface(struct egl_input_device *device)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500663{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500664 struct egl_compositor *ec = device->ec;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500665 struct egl_surface *es;
666
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500667 if (device->grab > 0)
668 return device->grab_surface;
669
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500670 es = container_of(ec->surface_list.prev,
671 struct egl_surface, link);
672 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500673 if (es->map.x <= device->x &&
674 device->x < es->map.x + es->map.width &&
675 es->map.y <= device->y &&
676 device->y < es->map.y + es->map.height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500677 return es;
678
679 es = container_of(es->link.prev,
680 struct egl_surface, link);
681 }
682
683 return NULL;
684}
685
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500686void
687notify_motion(struct egl_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500688{
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500689 struct egl_surface *es;
690 const int hotspot_x = 16, hotspot_y = 16;
691 int32_t sx, sy;
692
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500693 es = pick_surface(device);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500694
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500695 if (es) {
696 sx = (x - es->map.x) * es->width / es->map.width;
697 sy = (y - es->map.y) * es->height / es->map.height;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500698 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500699 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500700 }
701
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500702 device->x = x;
703 device->y = y;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500704 device->pointer_surface->map.x = x - hotspot_x;
705 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500706
707 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500708}
709
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500710void
711notify_button(struct egl_input_device *device,
712 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500713{
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500714 struct egl_surface *es;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500715 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500716
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500717 es = pick_surface(device);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500718 if (es) {
719 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500720 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500721
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500722 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500723 /* FIXME: We need callbacks when the surfaces
724 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500725 device->grab++;
726 device->grab_surface = es;
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500727 device->focus_surface = es;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500728 } else {
729 device->grab--;
730 }
731
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500732 sx = (device->x - es->map.x) * es->width / es->map.width;
733 sy = (device->y - es->map.y) * es->height / es->map.height;
734
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500735 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500736 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500737 WL_INPUT_BUTTON, button, state,
738 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500739
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500740 schedule_repaint(device->ec);
741 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500742}
743
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500744void
745notify_key(struct egl_input_device *device,
746 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500747{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500748 struct egl_compositor *ec = device->ec;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500749
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500750 if (key == KEY_ESC && state == 1) {
751 if (ec->overlay_target == ec->height)
752 ec->overlay_target -= 200;
753 else
754 ec->overlay_target += 200;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500755 schedule_repaint(ec);
Kristian Høgsberg7fdff042008-12-10 13:25:00 -0500756 } else if (!wl_list_empty(&ec->surface_list)) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500757 if (device->focus_surface != NULL)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500758 wl_surface_post_event(&device->focus_surface->base,
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500759 &device->base,
760 WL_INPUT_KEY, key, state);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500761 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500762}
763
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500764struct evdev_input_device *
765evdev_input_device_create(struct egl_input_device *device,
766 struct wl_display *display, const char *path);
767
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500768static void
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500769create_input_device(struct egl_compositor *ec, const char *glob)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500770{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500771 struct egl_input_device *device;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500772 struct dirent *de;
773 char path[PATH_MAX];
774 const char *by_path_dir = "/dev/input/by-path";
775 DIR *dir;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500776
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500777 device = malloc(sizeof *device);
778 if (device == NULL)
779 return;
780
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500781 memset(device, 0, sizeof *device);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500782 device->base.interface = wl_input_device_get_interface();
783 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500784 device->x = 100;
785 device->y = 100;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500786 device->pointer_surface = pointer_create(device->x, device->y, 64, 64);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500787 device->ec = ec;
788
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500789 dir = opendir(by_path_dir);
790 if (dir == NULL) {
791 fprintf(stderr, "couldn't read dir %s\n", by_path_dir);
792 return;
793 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500794
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500795 while (de = readdir(dir), de != NULL) {
796 if (fnmatch(glob, de->d_name, 0))
797 continue;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500798
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500799 snprintf(path, sizeof path, "%s/%s", by_path_dir, de->d_name);
800 evdev_input_device_create(device, ec->wl_display, path);
801 }
802 closedir(dir);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500803
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500804 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500805}
806
807void
808egl_device_get_position(struct egl_input_device *device, int32_t *x, int32_t *y)
809{
810 *x = device->x;
811 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500812}
813
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500814static uint32_t
815create_frontbuffer(int fd, int *width, int *height, int *stride)
816{
817 drmModeConnector *connector;
818 drmModeRes *resources;
819 drmModeEncoder *encoder;
820 struct drm_mode_modeinfo *mode;
821 struct drm_i915_gem_create create;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500822 struct drm_gem_flink flink;
823 unsigned int fb_id;
824 int i, ret;
825
826 resources = drmModeGetResources(fd);
827 if (!resources) {
828 fprintf(stderr, "drmModeGetResources failed\n");
829 return 0;
830 }
831
832 for (i = 0; i < resources->count_connectors; i++) {
833 connector = drmModeGetConnector(fd, resources->connectors[i]);
834 if (connector == NULL)
835 continue;
836
837 if (connector->connection == DRM_MODE_CONNECTED &&
838 connector->count_modes > 0)
839 break;
840
841 drmModeFreeConnector(connector);
842 }
843
844 if (i == resources->count_connectors) {
845 fprintf(stderr, "No currently active connector found.\n");
846 return -1;
847 }
848
849 mode = &connector->modes[0];
850
851 for (i = 0; i < resources->count_encoders; i++) {
852 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
853
854 if (encoder == NULL)
855 continue;
856
857 if (encoder->encoder_id == connector->encoder_id)
858 break;
859
860 drmModeFreeEncoder(encoder);
861 }
862
863 /* Mode size at 32 bpp */
864 create.size = mode->hdisplay * mode->vdisplay * 4;
865 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
866 fprintf(stderr, "gem create failed: %m\n");
867 return 0;
868 }
869
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500870 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
871 32, 32, mode->hdisplay * 4, create.handle, &fb_id);
872 if (ret) {
873 fprintf(stderr, "failed to add fb: %m\n");
874 return 0;
875 }
876
877 ret = drmModeSetCrtc(fd, encoder->crtc_id, fb_id, 0, 0,
878 &connector->connector_id, 1, mode);
879 if (ret) {
880 fprintf(stderr, "failed to set mode: %m\n");
881 return 0;
882 }
883
884 flink.handle = create.handle;
885 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
886 fprintf(stderr, "gem flink failed: %m\n");
887 return 0;
888 }
889
890 *width = mode->hdisplay;
891 *height = mode->vdisplay;
892 *stride = mode->hdisplay * 4;
893
894 return flink.name;
895}
896
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500897static int
898pick_config(struct egl_compositor *ec)
899{
900 EGLConfig configs[100];
901 EGLint value, count;
902 int i;
903
904 if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) {
905 fprintf(stderr, "failed to get configs\n");
906 return -1;
907 }
908
909 ec->config = EGL_NO_CONFIG;
910 for (i = 0; i < count; i++) {
911 eglGetConfigAttrib(ec->display,
912 configs[i],
913 EGL_DEPTH_SIZE,
914 &value);
915 if (value > 0) {
916 fprintf(stderr, "config %d has depth size %d\n", i, value);
917 continue;
918 }
919
920 eglGetConfigAttrib(ec->display,
921 configs[i],
922 EGL_STENCIL_SIZE,
923 &value);
924 if (value > 0) {
925 fprintf(stderr, "config %d has stencil size %d\n", i, value);
926 continue;
927 }
928
929 eglGetConfigAttrib(ec->display,
930 configs[i],
931 EGL_CONFIG_CAVEAT,
932 &value);
933 if (value != EGL_NONE) {
934 fprintf(stderr, "config %d has caveat %d\n", i, value);
935 continue;
936 }
937
938 ec->config = configs[i];
939 break;
940 }
941
942 if (ec->config == EGL_NO_CONFIG) {
943 fprintf(stderr, "found no config without depth or stencil buffers\n");
944 return -1;
945 }
946
947 return 0;
948}
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500949
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400950static const char gem_device[] = "/dev/dri/card0";
951
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500952static const char *macbook_air_default_input_device[] = {
Kristian Høgsberg64949972008-12-12 14:48:46 -0500953 "pci-0000:00:1d.2-usb-0:2:1*event*",
954 NULL
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500955};
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500956
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500957static const char *option_background = "background.jpg";
958static const char **option_input_devices = macbook_air_default_input_device;
959
960static const GOptionEntry option_entries[] = {
961 { "background", 'b', 0, G_OPTION_ARG_STRING,
962 &option_background, "Background image" },
963 { "input-device", 'i', 0, G_OPTION_ARG_STRING_ARRAY,
964 &option_input_devices, "Input device glob" },
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500965 { NULL }
966};
967
Kristian Høgsberg122912c2008-12-05 11:13:50 -0500968static struct egl_compositor *
969egl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400970{
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500971 EGLint major, minor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400972 struct egl_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500973 struct screenshooter *shooter;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500974 uint32_t fb_name;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500975 int i, stride;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500976 struct wl_event_loop *loop;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500977 const static EGLint attribs[] =
978 { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE };
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400979
980 ec = malloc(sizeof *ec);
981 if (ec == NULL)
982 return NULL;
983
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400984 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400985
Kristian Høgsbergc508d932008-10-13 22:52:42 -0400986 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400987 if (ec->display == NULL) {
988 fprintf(stderr, "failed to create display\n");
989 return NULL;
990 }
991
992 if (!eglInitialize(ec->display, &major, &minor)) {
993 fprintf(stderr, "failed to initialize display\n");
994 return NULL;
995 }
996
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500997 if (pick_config(ec))
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400998 return NULL;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500999
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001000 fb_name = create_frontbuffer(eglGetDisplayFD(ec->display),
1001 &ec->width, &ec->height, &stride);
1002 ec->surface = eglCreateSurfaceForName(ec->display, ec->config,
1003 fb_name, ec->width, ec->height, stride, attribs);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001004 if (ec->surface == NULL) {
1005 fprintf(stderr, "failed to create surface\n");
1006 return NULL;
1007 }
1008
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -05001009 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001010 if (ec->context == NULL) {
1011 fprintf(stderr, "failed to create context\n");
1012 return NULL;
1013 }
1014
1015 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
1016 fprintf(stderr, "failed to make context current\n");
1017 return NULL;
1018 }
1019
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001020 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001021 glMatrixMode(GL_PROJECTION);
1022 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001023 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001024 glMatrixMode(GL_MODELVIEW);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -05001025 glClearColor(0, 0, 0.2, 1);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001026
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001027 wl_display_set_compositor(display, &ec->base, &compositor_interface);
1028
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001029 wl_list_init(&ec->input_device_list);
1030 for (i = 0; option_input_devices[i]; i++)
1031 create_input_device(ec, option_input_devices[i]);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001032
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001033 wl_list_init(&ec->surface_list);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001034 ec->background = background_create(option_background,
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001035 ec->width, ec->height);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001036 ec->overlay = overlay_create(0, ec->height, ec->width, 200);
Kristian Høgsberg9f88b182008-12-08 13:52:08 -05001037 ec->overlay_y = ec->height;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001038 ec->overlay_target = ec->height;
1039 ec->overlay_previous = ec->height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001040
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001041 ec->gem_fd = open(gem_device, O_RDWR);
1042 if (ec->gem_fd < 0) {
1043 fprintf(stderr, "failed to open drm device\n");
1044 return NULL;
1045 }
1046
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001047 shooter = screenshooter_create(ec);
1048 wl_display_add_object(display, &shooter->base);
1049 wl_display_add_global(display, &shooter->base);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001050
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001051 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001052 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001053 ec->repaint_needed = 0;
1054 ec->repaint_on_timeout = 0;
1055
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001056 schedule_repaint(ec);
1057
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001058 return ec;
1059}
1060
1061/* The plan here is to generate a random anonymous socket name and
1062 * advertise that through a service on the session dbus.
1063 */
1064static const char socket_name[] = "\0wayland";
1065
1066int main(int argc, char *argv[])
1067{
1068 struct wl_display *display;
1069 struct egl_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001070 GError *error = NULL;
1071 GOptionContext *context;
1072
1073 context = g_option_context_new(NULL);
1074 g_option_context_add_main_entries(context, option_entries, "Wayland");
1075 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1076 fprintf(stderr, "option parsing failed: %s\n", error->message);
1077 exit(EXIT_FAILURE);
1078 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001079
1080 display = wl_display_create();
1081
1082 ec = egl_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001083 if (ec == NULL) {
1084 fprintf(stderr, "failed to create compositor\n");
1085 exit(EXIT_FAILURE);
1086 }
1087
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001088 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001089 fprintf(stderr, "failed to add socket: %m\n");
1090 exit(EXIT_FAILURE);
1091 }
1092
1093 wl_display_run(display);
1094
1095 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001096}