blob: d6426c293283e232997a2368fe9109d39a0016d2 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040023#include <stdio.h>
24#include <string.h>
25#include <stdlib.h>
26#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050027#include <stdarg.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <i915_drm.h>
29#include <sys/ioctl.h>
30#include <sys/mman.h>
31#include <fcntl.h>
32#include <unistd.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <signal.h>
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050034#include <cairo.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050035#include <gdk-pixbuf/gdk-pixbuf.h>
36#include <glib.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050037#include <sys/poll.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050038#include <png.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050039#include <math.h>
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -050040#include <linux/input.h>
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -050041#include <xf86drmMode.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050042#include <time.h>
Kristian Høgsbergc492b482008-12-12 12:00:02 -050043#include <fnmatch.h>
44#include <dirent.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040045
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040046#include <GL/gl.h>
47#include <eagle.h>
48
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050049#include "wayland.h"
50#include "cairo-util.h"
51#include "egl-compositor.h"
52
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040053#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
54
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050055struct egl_input_device {
56 struct wl_object base;
57 int32_t x, y;
58 struct egl_compositor *ec;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050059 struct egl_surface *pointer_surface;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050060 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050061
62 int grab;
63 struct egl_surface *grab_surface;
Kristian Høgsberga7700c82008-12-12 13:48:30 -050064 struct egl_surface *focus_surface;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050065};
66
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040067struct egl_compositor {
68 struct wl_compositor base;
69 EGLDisplay display;
70 EGLSurface surface;
71 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050072 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040073 struct wl_display *wl_display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040074 int gem_fd;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050075 int width, height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050076 struct egl_surface *background;
Kristian Høgsberg54879822008-11-23 17:07:32 -050077 struct egl_surface *overlay;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -050078 double overlay_y, overlay_target, overlay_previous;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050079
Kristian Høgsbergc492b482008-12-12 12:00:02 -050080 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -050081 struct wl_list surface_list;
82
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050083 /* Repaint state. */
84 struct wl_event_source *timer_source;
85 int repaint_needed;
86 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050087 struct timespec previous_swap;
88 uint32_t current_frame;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040089};
90
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050091struct egl_surface {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040092 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040093 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050094 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -050095 struct wl_surface *wl_surface;
96 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -050097 struct wl_list link;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040098};
99
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500100static void
101die(const char *msg, ...)
102{
103 va_list ap;
104
105 va_start (ap, msg);
106 vfprintf(stderr, msg, ap);
107 va_end (ap);
108
109 exit(EXIT_FAILURE);
110}
111
112static void
113stdio_write_func (png_structp png, png_bytep data, png_size_t size)
114{
115 FILE *fp;
116 size_t ret;
117
118 fp = png_get_io_ptr (png);
119 while (size) {
120 ret = fwrite (data, 1, size, fp);
121 size -= ret;
122 data += ret;
123 if (size && ferror (fp))
124 die("write: %m\n");
125 }
126}
127
128static void
129png_simple_output_flush_fn (png_structp png_ptr)
130{
131}
132
133static void
134png_simple_error_callback (png_structp png,
135 png_const_charp error_msg)
136{
137 die("png error: %s\n", error_msg);
138}
139
140static void
141png_simple_warning_callback (png_structp png,
142 png_const_charp error_msg)
143{
144 fprintf(stderr, "png warning: %s\n", error_msg);
145}
146
147static void
148convert_pixels(png_structp png, png_row_infop row_info, png_bytep data)
149{
150 unsigned int i;
151
152 for (i = 0; i < row_info->rowbytes; i += 4) {
153 uint8_t *b = &data[i];
154 uint32_t pixel;
155
156 memcpy (&pixel, b, sizeof (uint32_t));
157 b[0] = (pixel & 0xff0000) >> 16;
158 b[1] = (pixel & 0x00ff00) >> 8;
159 b[2] = (pixel & 0x0000ff) >> 0;
160 b[3] = 0;
161 }
162}
163
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500164struct screenshooter {
165 struct wl_object base;
166 struct egl_compositor *ec;
167};
168
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500169static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500170screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500171{
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500172 struct egl_compositor *ec = shooter->ec;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500173 png_struct *png;
174 png_info *info;
175 png_byte **volatile rows = NULL;
176 png_color_16 white;
177 int depth, i;
178 FILE *fp;
179 uint8_t *data;
180 GLuint stride;
181 static const char filename[] = "wayland-screenshot.png";
182
183 data = eglReadBuffer(ec->display, ec->surface, GL_FRONT_LEFT, &stride);
184 if (data == NULL)
185 die("eglReadBuffer failed\n");
186 rows = malloc(ec->height * sizeof rows[0]);
187 if (rows == NULL)
188 die("malloc failed\n");
189
190 for (i = 0; i < ec->height; i++)
191 rows[i] = (png_byte *) data + i * stride;
192
193 png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
194 png_simple_error_callback,
195 png_simple_warning_callback);
196 if (png == NULL)
197 die("png_create_write_struct failed\n");
198
199 info = png_create_info_struct(png);
200 if (info == NULL)
201 die("png_create_info_struct failed\n");
202
203 fp = fopen(filename, "w");
204 if (fp == NULL)
205 die("fopen failed: %m\n");
206
207 png_set_write_fn(png, fp, stdio_write_func, png_simple_output_flush_fn);
208
209 depth = 8;
210 png_set_IHDR(png, info,
211 ec->width,
212 ec->height, depth,
213 PNG_COLOR_TYPE_RGB,
214 PNG_INTERLACE_NONE,
215 PNG_COMPRESSION_TYPE_DEFAULT,
216 PNG_FILTER_TYPE_DEFAULT);
217
218 white.gray = (1 << depth) - 1;
219 white.red = white.blue = white.green = white.gray;
220 png_set_bKGD(png, info, &white);
221 png_write_info (png, info);
222 png_set_write_user_transform_fn(png, convert_pixels);
223
224 png_set_filler(png, 0, PNG_FILLER_AFTER);
225 png_write_image(png, rows);
226 png_write_end(png, info);
227
228 png_destroy_write_struct(&png, &info);
229 fclose(fp);
230 free(rows);
231 free(data);
232}
233
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500234static const struct wl_method screenshooter_methods[] = {
Kristian Høgsberg73f4e762008-12-08 14:07:33 -0500235 { "shoot", screenshooter_shoot, "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500236};
237
238static const struct wl_interface screenshooter_interface = {
239 "screenshooter", 1,
240 ARRAY_LENGTH(screenshooter_methods),
241 screenshooter_methods,
242};
243
244static struct screenshooter *
245screenshooter_create(struct egl_compositor *ec)
246{
247 struct screenshooter *shooter;
248
249 shooter = malloc(sizeof *shooter);
250 if (shooter == NULL)
251 return NULL;
252
253 shooter->base.interface = &screenshooter_interface;
254 shooter->ec = ec;
255
256 return shooter;
257};
258
Kristian Høgsberg54879822008-11-23 17:07:32 -0500259static struct egl_surface *
260egl_surface_create_from_cairo_surface(cairo_surface_t *surface,
261 int x, int y, int width, int height)
262{
263 struct egl_surface *es;
264 int stride;
265 void *data;
266
267 stride = cairo_image_surface_get_stride(surface);
268 data = cairo_image_surface_get_data(surface);
269
270 es = malloc(sizeof *es);
271 if (es == NULL)
272 return NULL;
273
274 glGenTextures(1, &es->texture);
275 glBindTexture(GL_TEXTURE_2D, es->texture);
276 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500277 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
278 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
279 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500280 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
281 GL_BGRA, GL_UNSIGNED_BYTE, data);
282
283 es->map.x = x;
284 es->map.y = y;
285 es->map.width = width;
286 es->map.height = height;
287 es->surface = EGL_NO_SURFACE;
288
289 return es;
290}
291
292static void
293egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec)
294{
295 glDeleteTextures(1, &es->texture);
296 if (es->surface != EGL_NO_SURFACE)
297 eglDestroySurface(ec->display, es->surface);
298 free(es);
299}
300
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400301static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500302pointer_path(cairo_t *cr, int x, int y)
303{
304 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
305 const int width = 16, height = 16;
306
307 cairo_move_to(cr, x, y);
308 cairo_line_to(cr, x + tx, y + ty);
309 cairo_line_to(cr, x + dx, y + dy);
310 cairo_line_to(cr, x + width - end, y + height);
311 cairo_line_to(cr, x + width, y + height - end);
312 cairo_line_to(cr, x + dy, y + dx);
313 cairo_line_to(cr, x + ty, y + tx);
314 cairo_close_path(cr);
315}
316
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500317static struct egl_surface *
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500318pointer_create(int x, int y, int width, int height)
319{
Kristian Høgsberg54879822008-11-23 17:07:32 -0500320 struct egl_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500321 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500322 cairo_surface_t *surface;
323 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500324
325 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
326 width, height);
327
328 cr = cairo_create(surface);
329 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
330 cairo_set_line_width (cr, 2);
331 cairo_set_source_rgb(cr, 0, 0, 0);
332 cairo_stroke_preserve(cr);
333 cairo_fill(cr);
334 blur_surface(surface, width);
335
336 pointer_path(cr, hotspot_x, hotspot_y);
337 cairo_stroke_preserve(cr);
338 cairo_set_source_rgb(cr, 1, 1, 1);
339 cairo_fill(cr);
340 cairo_destroy(cr);
341
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500342 es = egl_surface_create_from_cairo_surface(surface,
343 x - hotspot_x,
344 y - hotspot_y,
345 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500346
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500347 cairo_surface_destroy(surface);
348
Kristian Høgsberg54879822008-11-23 17:07:32 -0500349 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500350}
351
352static struct egl_surface *
353background_create(const char *filename, int width, int height)
354{
355 struct egl_surface *background;
356 GdkPixbuf *pixbuf;
357 GError *error = NULL;
358 int pixbuf_width, pixbuf_height;
359 void *data;
360
361 background = malloc(sizeof *background);
362 if (background == NULL)
363 return NULL;
364
365 g_type_init();
366
367 pixbuf = gdk_pixbuf_new_from_file(filename, &error);
368 if (error != NULL) {
369 free(background);
370 return NULL;
371 }
372
373 pixbuf_width = gdk_pixbuf_get_width(pixbuf);
374 pixbuf_height = gdk_pixbuf_get_height(pixbuf);
375 data = gdk_pixbuf_get_pixels(pixbuf);
376
377 glGenTextures(1, &background->texture);
378 glBindTexture(GL_TEXTURE_2D, background->texture);
379 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500380 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
381 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
382 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500383 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixbuf_width, pixbuf_height, 0,
384 GL_BGR, GL_UNSIGNED_BYTE, data);
385
386 background->map.x = 0;
387 background->map.y = 0;
388 background->map.width = width;
389 background->map.height = height;
390 background->surface = EGL_NO_SURFACE;
391
392 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500393}
394
395static void
Kristian Høgsberg54879822008-11-23 17:07:32 -0500396rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
397{
398 cairo_move_to(cr, x0, y0 + radius);
399 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
400 cairo_line_to(cr, x1 - radius, y0);
401 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
402 cairo_line_to(cr, x1, y1 - radius);
403 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
404 cairo_line_to(cr, x0 + radius, y1);
405 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
406 cairo_close_path(cr);
407}
408
409static void
410draw_button(cairo_t *cr, int x, int y, int width, int height, const char *text)
411{
412 cairo_pattern_t *gradient;
413 cairo_text_extents_t extents;
414 double bright = 0.15, dim = 0.02;
415 int radius = 10;
416
417 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
418 cairo_set_line_width (cr, 2);
419 rounded_rect(cr, x, y, x + width, y + height, radius);
420 cairo_set_source_rgb(cr, dim, dim, dim);
421 cairo_stroke(cr);
422 rounded_rect(cr, x + 2, y + 2, x + width, y + height, radius);
423 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
424 cairo_stroke(cr);
425
426 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
427 cairo_set_source_rgb(cr, bright, bright, bright);
428 cairo_stroke(cr);
429 rounded_rect(cr, x + 3, y + 3, x + width - 1, y + height - 1, radius - 1);
430 cairo_set_source_rgb(cr, dim, dim, dim);
431 cairo_stroke(cr);
432
433 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
434 gradient = cairo_pattern_create_linear (0, y, 0, y + height);
435 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.15, 0.15, 0.15);
436 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.08, 0.08, 0.08);
437 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.07, 0.07, 0.07);
438 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.1, 0.1, 0.1);
439 cairo_set_source(cr, gradient);
440 cairo_fill(cr);
441
442 cairo_set_font_size(cr, 16);
443 cairo_text_extents(cr, text, &extents);
444 cairo_move_to(cr, x + (width - extents.width) / 2, y + (height - extents.height) / 2 - extents.y_bearing);
445 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
446 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
447 cairo_set_line_width (cr, 4);
448 cairo_text_path(cr, text);
449 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
450 cairo_stroke_preserve(cr);
451 cairo_set_source_rgb(cr, 1, 1, 1);
452 cairo_fill(cr);
453}
454
455static struct egl_surface *
456overlay_create(int x, int y, int width, int height)
457{
458 struct egl_surface *es;
459 cairo_surface_t *surface;
460 cairo_t *cr;
461 int total_width, button_x, button_y;
462 const int button_width = 150;
463 const int button_height = 40;
464 const int spacing = 50;
465
466 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
467 width, height);
468
469 cr = cairo_create(surface);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500470 cairo_set_source_rgba(cr, 0.1, 0.1, 0.1, 0.8);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500471 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
472 cairo_paint(cr);
473
474 total_width = button_width * 2 + spacing;
475 button_x = (width - total_width) / 2;
476 button_y = height - button_height - 20;
477 draw_button(cr, button_x, button_y, button_width, button_height, "Previous");
478 button_x += button_width + spacing;
479 draw_button(cr, button_x, button_y, button_width, button_height, "Next");
480
481 cairo_destroy(cr);
482
483 es = egl_surface_create_from_cairo_surface(surface, x, y, width, height);
484
485 cairo_surface_destroy(surface);
486
487 return es;
488}
489
490static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500491draw_surface(struct egl_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500492{
493 GLint vertices[12];
494 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
495 GLuint indices[4] = { 0, 1, 2, 3 };
496
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500497 vertices[0] = es->map.x;
498 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500499 vertices[2] = 0;
500
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500501 vertices[3] = es->map.x;
502 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500503 vertices[5] = 0;
504
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500505 vertices[6] = es->map.x + es->map.width;
506 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500507 vertices[8] = 0;
508
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500509 vertices[9] = es->map.x + es->map.width;
510 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500511 vertices[11] = 0;
512
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500513 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500514 glEnable(GL_TEXTURE_2D);
515 glEnable(GL_BLEND);
516 /* Assume pre-multiplied alpha for now, this probably
517 * needs to be a wayland visual type of thing. */
518 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
519
520 glEnableClientState(GL_VERTEX_ARRAY);
521 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
522 glVertexPointer(3, GL_INT, 0, vertices);
523 glTexCoordPointer(2, GL_INT, 0, tex_coords);
524 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
525}
526
527static void
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500528schedule_repaint(struct egl_compositor *ec);
529
530static void
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500531animate_overlay(struct egl_compositor *ec)
532{
533 double force, y;
534 int32_t top, bottom;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500535#if 1
536 double bounce = 0.0;
537 double friction = 1.0;
538 double spring = 0.2;
539#else
540 double bounce = 0.2;
541 double friction = 0.04;
542 double spring = 0.09;
543#endif
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500544
545 y = ec->overlay_y;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500546 force = (ec->overlay_target - ec->overlay_y) * spring +
547 (ec->overlay_previous - y) * friction;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500548
549 ec->overlay_y = y + (y - ec->overlay_previous) + force;
550 ec->overlay_previous = y;
551
552 top = ec->height - ec->overlay->map.height;
553 bottom = ec->height;
554 if (ec->overlay_y >= bottom) {
555 ec->overlay_y = bottom;
556 ec->overlay_previous = bottom;
557 }
558
559 if (ec->overlay_y <= top) {
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500560 ec->overlay_y = top + bounce * (top - ec->overlay_y);
561 ec->overlay_previous =
562 top + bounce * (top - ec->overlay_previous);
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500563 }
564
565 ec->overlay->map.y = ec->overlay_y + 0.5;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500566
Kristian Høgsberg73c30582008-11-25 22:45:46 -0500567 if (fabs(y - ec->overlay_target) > 0.2 ||
568 fabs(ec->overlay_y - ec->overlay_target) > 0.2)
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500569 schedule_repaint(ec);
570}
571
572static void
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500573repaint(void *data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400574{
575 struct egl_compositor *ec = data;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500576 struct egl_surface *es;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500577 struct egl_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500578 struct timespec ts;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500579 uint32_t msecs;
580
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500581 if (!ec->repaint_needed) {
582 ec->repaint_on_timeout = 0;
583 return;
584 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500585
586 draw_surface(ec->background);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400587
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500588 es = container_of(ec->surface_list.next,
589 struct egl_surface, link);
590 while (&es->link != &ec->surface_list) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500591 draw_surface(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500592
593 es = container_of(es->link.next,
594 struct egl_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400595 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400596
Kristian Høgsberg54879822008-11-23 17:07:32 -0500597 draw_surface(ec->overlay);
598
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500599 eid = container_of(ec->input_device_list.next,
600 struct egl_input_device, link);
601 while (&eid->link != &ec->input_device_list) {
602 draw_surface(eid->pointer_surface);
603
604 eid = container_of(eid->link.next,
605 struct egl_input_device, link);
606 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500607
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400608 eglSwapBuffers(ec->display, ec->surface);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500609 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500610
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500611 clock_gettime(CLOCK_MONOTONIC, &ts);
612 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
613 wl_display_post_frame(ec->wl_display, ec->current_frame, msecs);
614 ec->current_frame++;
615
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500616 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500617 ec->repaint_on_timeout = 1;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500618
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500619 animate_overlay(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400620}
621
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500622static void
623schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400624{
625 struct wl_event_loop *loop;
626
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500627 ec->repaint_needed = 1;
628 if (!ec->repaint_on_timeout) {
629 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500630 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500631 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400632}
633
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500634static void
635notify_surface_create(struct wl_compositor *compositor,
636 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400637{
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500638 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500639 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400640
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500641 es = malloc(sizeof *es);
642 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400643 return;
644
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500645 es->surface = EGL_NO_SURFACE;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500646 es->wl_surface = surface;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500647 wl_surface_set_data(surface, es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500648 wl_list_insert(ec->surface_list.prev, &es->link);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400649
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500650 glGenTextures(1, &es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400651}
652
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500653static void
654notify_surface_destroy(struct wl_compositor *compositor,
655 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400656{
657 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500658 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400659
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500660 es = wl_surface_get_data(surface);
661 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400662 return;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500663
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500664 wl_list_remove(&es->link);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500665 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400666
667 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400668}
669
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500670static void
671notify_surface_attach(struct wl_compositor *compositor,
672 struct wl_surface *surface, uint32_t name,
673 uint32_t width, uint32_t height, uint32_t stride)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400674{
675 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500676 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400677
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500678 es = wl_surface_get_data(surface);
679 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400680 return;
681
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500682 if (es->surface != EGL_NO_SURFACE)
683 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400684
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500685 es->width = width;
686 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500687 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500688 name, width, height, stride, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400689
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500690 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400691 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500692 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
693 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
694 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500695 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400696}
697
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500698static void
699notify_surface_map(struct wl_compositor *compositor,
700 struct wl_surface *surface, struct wl_map *map)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400701{
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500702 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400703
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500704 es = wl_surface_get_data(surface);
705 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400706 return;
707
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500708 es->map = *map;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400709}
710
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500711static void
712notify_surface_copy(struct wl_compositor *compositor,
713 struct wl_surface *surface,
714 int32_t dst_x, int32_t dst_y,
715 uint32_t name, uint32_t stride,
716 int32_t x, int32_t y, int32_t width, int32_t height)
717{
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500718 struct egl_compositor *ec = (struct egl_compositor *) compositor;
719 EGLSurface src;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500720 struct egl_surface *es;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500721
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500722 es = wl_surface_get_data(surface);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500723
724 /* FIXME: glCopyPixels should work, but then we'll have to
725 * call eglMakeCurrent to set up the src and dest surfaces
726 * first. This seems cheaper, but maybe there's a better way
727 * to accomplish this. */
728
729 src = eglCreateSurfaceForName(ec->display, ec->config,
730 name, x + width, y + height, stride, NULL);
731
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500732 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500733 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500734 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500735}
736
737static void
738notify_surface_damage(struct wl_compositor *compositor,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500739 struct wl_surface *surface,
740 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500741{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500742 /* FIXME: This need to take a damage region, of course. */
743}
744
745static uint32_t
746notify_commit(struct wl_compositor *compositor)
747{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500748 struct egl_compositor *ec = (struct egl_compositor *) compositor;
749
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500750 schedule_repaint(ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500751
752 return ec->current_frame;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500753}
754
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500755static struct egl_surface *
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500756pick_surface(struct egl_input_device *device)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500757{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500758 struct egl_compositor *ec = device->ec;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500759 struct egl_surface *es;
760
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500761 if (device->grab > 0)
762 return device->grab_surface;
763
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500764 es = container_of(ec->surface_list.prev,
765 struct egl_surface, link);
766 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500767 if (es->map.x <= device->x &&
768 device->x < es->map.x + es->map.width &&
769 es->map.y <= device->y &&
770 device->y < es->map.y + es->map.height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500771 return es;
772
773 es = container_of(es->link.prev,
774 struct egl_surface, link);
775 }
776
777 return NULL;
778}
779
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500780void
781notify_motion(struct egl_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500782{
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500783 struct egl_surface *es;
784 const int hotspot_x = 16, hotspot_y = 16;
785 int32_t sx, sy;
786
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500787 es = pick_surface(device);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500788
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500789 if (es) {
790 sx = (x - es->map.x) * es->width / es->map.width;
791 sy = (y - es->map.y) * es->height / es->map.height;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500792 wl_surface_post_event(es->wl_surface, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500793 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500794 }
795
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500796 device->x = x;
797 device->y = y;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500798 device->pointer_surface->map.x = x - hotspot_x;
799 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500800
801 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500802}
803
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500804void
805notify_button(struct egl_input_device *device,
806 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500807{
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500808 struct egl_surface *es;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500809 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500810
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500811 es = pick_surface(device);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500812 if (es) {
813 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500814 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500815
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500816 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500817 /* FIXME: We need callbacks when the surfaces
818 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500819 device->grab++;
820 device->grab_surface = es;
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500821 device->focus_surface = es;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500822 } else {
823 device->grab--;
824 }
825
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500826 sx = (device->x - es->map.x) * es->width / es->map.width;
827 sy = (device->y - es->map.y) * es->height / es->map.height;
828
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500829 /* FIXME: Swallow click on raise? */
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500830 wl_surface_post_event(es->wl_surface, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500831 WL_INPUT_BUTTON, button, state,
832 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500833
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500834 schedule_repaint(device->ec);
835 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500836}
837
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500838void
839notify_key(struct egl_input_device *device,
840 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500841{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500842 struct egl_compositor *ec = device->ec;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500843
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500844 if (key == KEY_ESC && state == 1) {
845 if (ec->overlay_target == ec->height)
846 ec->overlay_target -= 200;
847 else
848 ec->overlay_target += 200;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500849 schedule_repaint(ec);
Kristian Høgsberg7fdff042008-12-10 13:25:00 -0500850 } else if (!wl_list_empty(&ec->surface_list)) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500851 if (device->focus_surface != NULL)
852 wl_surface_post_event(device->focus_surface->wl_surface,
853 &device->base,
854 WL_INPUT_KEY, key, state);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500855 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500856}
857
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500858static const struct wl_compositor_interface interface = {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400859 notify_surface_create,
860 notify_surface_destroy,
861 notify_surface_attach,
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500862 notify_surface_map,
863 notify_surface_copy,
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500864 notify_surface_damage,
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500865 notify_commit,
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400866};
867
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500868struct evdev_input_device *
869evdev_input_device_create(struct egl_input_device *device,
870 struct wl_display *display, const char *path);
871
872void
873egl_device_get_position(struct egl_input_device *device, int32_t *x, int32_t *y);
874
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500875static void
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500876create_input_device(struct egl_compositor *ec, const char *glob)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500877{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500878 struct egl_input_device *device;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500879 struct dirent *de;
880 char path[PATH_MAX];
881 const char *by_path_dir = "/dev/input/by-path";
882 DIR *dir;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500883
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500884 device = malloc(sizeof *device);
885 if (device == NULL)
886 return;
887
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500888 memset(device, 0, sizeof *device);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500889 device->base.interface = wl_input_device_get_interface();
890 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500891 device->x = 100;
892 device->y = 100;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500893 device->pointer_surface = pointer_create(device->x, device->y, 64, 64);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500894 device->ec = ec;
895
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500896 dir = opendir(by_path_dir);
897 if (dir == NULL) {
898 fprintf(stderr, "couldn't read dir %s\n", by_path_dir);
899 return;
900 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500901
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500902 while (de = readdir(dir), de != NULL) {
903 if (fnmatch(glob, de->d_name, 0))
904 continue;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500905
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500906 snprintf(path, sizeof path, "%s/%s", by_path_dir, de->d_name);
907 evdev_input_device_create(device, ec->wl_display, path);
908 }
909 closedir(dir);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500910
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500911 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500912}
913
914void
915egl_device_get_position(struct egl_input_device *device, int32_t *x, int32_t *y)
916{
917 *x = device->x;
918 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500919}
920
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500921static uint32_t
922create_frontbuffer(int fd, int *width, int *height, int *stride)
923{
924 drmModeConnector *connector;
925 drmModeRes *resources;
926 drmModeEncoder *encoder;
927 struct drm_mode_modeinfo *mode;
928 struct drm_i915_gem_create create;
929 struct drm_i915_gem_pin pin;
930 struct drm_gem_flink flink;
931 unsigned int fb_id;
932 int i, ret;
933
934 resources = drmModeGetResources(fd);
935 if (!resources) {
936 fprintf(stderr, "drmModeGetResources failed\n");
937 return 0;
938 }
939
940 for (i = 0; i < resources->count_connectors; i++) {
941 connector = drmModeGetConnector(fd, resources->connectors[i]);
942 if (connector == NULL)
943 continue;
944
945 if (connector->connection == DRM_MODE_CONNECTED &&
946 connector->count_modes > 0)
947 break;
948
949 drmModeFreeConnector(connector);
950 }
951
952 if (i == resources->count_connectors) {
953 fprintf(stderr, "No currently active connector found.\n");
954 return -1;
955 }
956
957 mode = &connector->modes[0];
958
959 for (i = 0; i < resources->count_encoders; i++) {
960 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
961
962 if (encoder == NULL)
963 continue;
964
965 if (encoder->encoder_id == connector->encoder_id)
966 break;
967
968 drmModeFreeEncoder(encoder);
969 }
970
971 /* Mode size at 32 bpp */
972 create.size = mode->hdisplay * mode->vdisplay * 4;
973 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
974 fprintf(stderr, "gem create failed: %m\n");
975 return 0;
976 }
977
978 pin.handle = create.handle;
979 pin.alignment = 4096;
980 if (ioctl(fd, DRM_IOCTL_I915_GEM_PIN, &pin)) {
981 fprintf(stderr, "failed to pin buffer: %m\n");
982 return 0;
983 }
984
985 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
986 32, 32, mode->hdisplay * 4, create.handle, &fb_id);
987 if (ret) {
988 fprintf(stderr, "failed to add fb: %m\n");
989 return 0;
990 }
991
992 ret = drmModeSetCrtc(fd, encoder->crtc_id, fb_id, 0, 0,
993 &connector->connector_id, 1, mode);
994 if (ret) {
995 fprintf(stderr, "failed to set mode: %m\n");
996 return 0;
997 }
998
999 flink.handle = create.handle;
1000 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
1001 fprintf(stderr, "gem flink failed: %m\n");
1002 return 0;
1003 }
1004
1005 *width = mode->hdisplay;
1006 *height = mode->vdisplay;
1007 *stride = mode->hdisplay * 4;
1008
1009 return flink.name;
1010}
1011
Kristian Høgsberg443853c2008-11-25 12:12:05 -05001012static int
1013pick_config(struct egl_compositor *ec)
1014{
1015 EGLConfig configs[100];
1016 EGLint value, count;
1017 int i;
1018
1019 if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) {
1020 fprintf(stderr, "failed to get configs\n");
1021 return -1;
1022 }
1023
1024 ec->config = EGL_NO_CONFIG;
1025 for (i = 0; i < count; i++) {
1026 eglGetConfigAttrib(ec->display,
1027 configs[i],
1028 EGL_DEPTH_SIZE,
1029 &value);
1030 if (value > 0) {
1031 fprintf(stderr, "config %d has depth size %d\n", i, value);
1032 continue;
1033 }
1034
1035 eglGetConfigAttrib(ec->display,
1036 configs[i],
1037 EGL_STENCIL_SIZE,
1038 &value);
1039 if (value > 0) {
1040 fprintf(stderr, "config %d has stencil size %d\n", i, value);
1041 continue;
1042 }
1043
1044 eglGetConfigAttrib(ec->display,
1045 configs[i],
1046 EGL_CONFIG_CAVEAT,
1047 &value);
1048 if (value != EGL_NONE) {
1049 fprintf(stderr, "config %d has caveat %d\n", i, value);
1050 continue;
1051 }
1052
1053 ec->config = configs[i];
1054 break;
1055 }
1056
1057 if (ec->config == EGL_NO_CONFIG) {
1058 fprintf(stderr, "found no config without depth or stencil buffers\n");
1059 return -1;
1060 }
1061
1062 return 0;
1063}
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001064
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001065static const char gem_device[] = "/dev/dri/card0";
1066
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001067static const char *macbook_air_default_input_device[] = {
1068 "pci-0000:00:1d.0-usb-0:2:1*event*"
1069};
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001070
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001071static const char *option_background = "background.jpg";
1072static const char **option_input_devices = macbook_air_default_input_device;
1073
1074static const GOptionEntry option_entries[] = {
1075 { "background", 'b', 0, G_OPTION_ARG_STRING,
1076 &option_background, "Background image" },
1077 { "input-device", 'i', 0, G_OPTION_ARG_STRING_ARRAY,
1078 &option_input_devices, "Input device glob" },
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001079 { NULL }
1080};
1081
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001082static struct egl_compositor *
1083egl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001084{
Kristian Høgsberg443853c2008-11-25 12:12:05 -05001085 EGLint major, minor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001086 struct egl_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001087 struct screenshooter *shooter;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001088 uint32_t fb_name;
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001089 int i, stride;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001090 struct wl_event_loop *loop;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001091 const static EGLint attribs[] =
1092 { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE };
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001093
1094 ec = malloc(sizeof *ec);
1095 if (ec == NULL)
1096 return NULL;
1097
1098 ec->base.interface = &interface;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001099 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001100
Kristian Høgsbergc508d932008-10-13 22:52:42 -04001101 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001102 if (ec->display == NULL) {
1103 fprintf(stderr, "failed to create display\n");
1104 return NULL;
1105 }
1106
1107 if (!eglInitialize(ec->display, &major, &minor)) {
1108 fprintf(stderr, "failed to initialize display\n");
1109 return NULL;
1110 }
1111
Kristian Høgsberg443853c2008-11-25 12:12:05 -05001112 if (pick_config(ec))
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001113 return NULL;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001114
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001115 fb_name = create_frontbuffer(eglGetDisplayFD(ec->display),
1116 &ec->width, &ec->height, &stride);
1117 ec->surface = eglCreateSurfaceForName(ec->display, ec->config,
1118 fb_name, ec->width, ec->height, stride, attribs);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001119 if (ec->surface == NULL) {
1120 fprintf(stderr, "failed to create surface\n");
1121 return NULL;
1122 }
1123
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -05001124 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001125 if (ec->context == NULL) {
1126 fprintf(stderr, "failed to create context\n");
1127 return NULL;
1128 }
1129
1130 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
1131 fprintf(stderr, "failed to make context current\n");
1132 return NULL;
1133 }
1134
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001135 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001136 glMatrixMode(GL_PROJECTION);
1137 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001138 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001139 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001140
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001141 wl_list_init(&ec->input_device_list);
1142 for (i = 0; option_input_devices[i]; i++)
1143 create_input_device(ec, option_input_devices[i]);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001144
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001145 wl_list_init(&ec->surface_list);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001146 ec->background = background_create(option_background,
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001147 ec->width, ec->height);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001148 ec->overlay = overlay_create(0, ec->height, ec->width, 200);
Kristian Høgsberg9f88b182008-12-08 13:52:08 -05001149 ec->overlay_y = ec->height;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001150 ec->overlay_target = ec->height;
1151 ec->overlay_previous = ec->height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001152
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001153 ec->gem_fd = open(gem_device, O_RDWR);
1154 if (ec->gem_fd < 0) {
1155 fprintf(stderr, "failed to open drm device\n");
1156 return NULL;
1157 }
1158
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001159 shooter = screenshooter_create(ec);
1160 wl_display_add_object(display, &shooter->base);
1161 wl_display_add_global(display, &shooter->base);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001162
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001163 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001164 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001165 ec->repaint_needed = 0;
1166 ec->repaint_on_timeout = 0;
1167
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001168 schedule_repaint(ec);
1169
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001170 return ec;
1171}
1172
1173/* The plan here is to generate a random anonymous socket name and
1174 * advertise that through a service on the session dbus.
1175 */
1176static const char socket_name[] = "\0wayland";
1177
1178int main(int argc, char *argv[])
1179{
1180 struct wl_display *display;
1181 struct egl_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001182 GError *error = NULL;
1183 GOptionContext *context;
1184
1185 context = g_option_context_new(NULL);
1186 g_option_context_add_main_entries(context, option_entries, "Wayland");
1187 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1188 fprintf(stderr, "option parsing failed: %s\n", error->message);
1189 exit(EXIT_FAILURE);
1190 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001191
1192 display = wl_display_create();
1193
1194 ec = egl_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001195 if (ec == NULL) {
1196 fprintf(stderr, "failed to create compositor\n");
1197 exit(EXIT_FAILURE);
1198 }
1199
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001200 wl_display_set_compositor(display, &ec->base);
1201
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001202 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001203 fprintf(stderr, "failed to add socket: %m\n");
1204 exit(EXIT_FAILURE);
1205 }
1206
1207 wl_display_run(display);
1208
1209 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001210}