blob: be25849be1016f7137d01fa9cf2064ccb4cee18c [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øgsberg16eb6752008-10-08 22:51:32 -040043
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040044#include <GL/gl.h>
45#include <eagle.h>
46
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050047#include "wayland.h"
48#include "cairo-util.h"
49#include "egl-compositor.h"
50
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040051#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
52
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050053struct egl_input_device {
54 struct wl_object base;
55 int32_t x, y;
56 struct egl_compositor *ec;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050057 struct egl_surface *pointer_surface;
58
59 int grab;
60 struct egl_surface *grab_surface;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061};
62
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040063struct egl_compositor {
64 struct wl_compositor base;
65 EGLDisplay display;
66 EGLSurface surface;
67 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050068 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040069 struct wl_display *wl_display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040070 int gem_fd;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050071 int width, height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050072 struct egl_surface *background;
Kristian Høgsberg54879822008-11-23 17:07:32 -050073 struct egl_surface *overlay;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -050074 double overlay_y, overlay_target, overlay_previous;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050075
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050076 struct egl_input_device *input_device;
77
Kristian Høgsberg201a9042008-12-10 00:40:50 -050078 struct wl_list surface_list;
79
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050080 /* Repaint state. */
81 struct wl_event_source *timer_source;
82 int repaint_needed;
83 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050084 struct timespec previous_swap;
85 uint32_t current_frame;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040086};
87
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050088struct egl_surface {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040089 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040090 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050091 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -050092 struct wl_surface *wl_surface;
93 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -050094 struct wl_list link;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040095};
96
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050097static void
98die(const char *msg, ...)
99{
100 va_list ap;
101
102 va_start (ap, msg);
103 vfprintf(stderr, msg, ap);
104 va_end (ap);
105
106 exit(EXIT_FAILURE);
107}
108
109static void
110stdio_write_func (png_structp png, png_bytep data, png_size_t size)
111{
112 FILE *fp;
113 size_t ret;
114
115 fp = png_get_io_ptr (png);
116 while (size) {
117 ret = fwrite (data, 1, size, fp);
118 size -= ret;
119 data += ret;
120 if (size && ferror (fp))
121 die("write: %m\n");
122 }
123}
124
125static void
126png_simple_output_flush_fn (png_structp png_ptr)
127{
128}
129
130static void
131png_simple_error_callback (png_structp png,
132 png_const_charp error_msg)
133{
134 die("png error: %s\n", error_msg);
135}
136
137static void
138png_simple_warning_callback (png_structp png,
139 png_const_charp error_msg)
140{
141 fprintf(stderr, "png warning: %s\n", error_msg);
142}
143
144static void
145convert_pixels(png_structp png, png_row_infop row_info, png_bytep data)
146{
147 unsigned int i;
148
149 for (i = 0; i < row_info->rowbytes; i += 4) {
150 uint8_t *b = &data[i];
151 uint32_t pixel;
152
153 memcpy (&pixel, b, sizeof (uint32_t));
154 b[0] = (pixel & 0xff0000) >> 16;
155 b[1] = (pixel & 0x00ff00) >> 8;
156 b[2] = (pixel & 0x0000ff) >> 0;
157 b[3] = 0;
158 }
159}
160
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500161struct screenshooter {
162 struct wl_object base;
163 struct egl_compositor *ec;
164};
165
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500166static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500167screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500168{
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500169 struct egl_compositor *ec = shooter->ec;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500170 png_struct *png;
171 png_info *info;
172 png_byte **volatile rows = NULL;
173 png_color_16 white;
174 int depth, i;
175 FILE *fp;
176 uint8_t *data;
177 GLuint stride;
178 static const char filename[] = "wayland-screenshot.png";
179
180 data = eglReadBuffer(ec->display, ec->surface, GL_FRONT_LEFT, &stride);
181 if (data == NULL)
182 die("eglReadBuffer failed\n");
183 rows = malloc(ec->height * sizeof rows[0]);
184 if (rows == NULL)
185 die("malloc failed\n");
186
187 for (i = 0; i < ec->height; i++)
188 rows[i] = (png_byte *) data + i * stride;
189
190 png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
191 png_simple_error_callback,
192 png_simple_warning_callback);
193 if (png == NULL)
194 die("png_create_write_struct failed\n");
195
196 info = png_create_info_struct(png);
197 if (info == NULL)
198 die("png_create_info_struct failed\n");
199
200 fp = fopen(filename, "w");
201 if (fp == NULL)
202 die("fopen failed: %m\n");
203
204 png_set_write_fn(png, fp, stdio_write_func, png_simple_output_flush_fn);
205
206 depth = 8;
207 png_set_IHDR(png, info,
208 ec->width,
209 ec->height, depth,
210 PNG_COLOR_TYPE_RGB,
211 PNG_INTERLACE_NONE,
212 PNG_COMPRESSION_TYPE_DEFAULT,
213 PNG_FILTER_TYPE_DEFAULT);
214
215 white.gray = (1 << depth) - 1;
216 white.red = white.blue = white.green = white.gray;
217 png_set_bKGD(png, info, &white);
218 png_write_info (png, info);
219 png_set_write_user_transform_fn(png, convert_pixels);
220
221 png_set_filler(png, 0, PNG_FILLER_AFTER);
222 png_write_image(png, rows);
223 png_write_end(png, info);
224
225 png_destroy_write_struct(&png, &info);
226 fclose(fp);
227 free(rows);
228 free(data);
229}
230
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500231static const struct wl_method screenshooter_methods[] = {
Kristian Høgsberg73f4e762008-12-08 14:07:33 -0500232 { "shoot", screenshooter_shoot, "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500233};
234
235static const struct wl_interface screenshooter_interface = {
236 "screenshooter", 1,
237 ARRAY_LENGTH(screenshooter_methods),
238 screenshooter_methods,
239};
240
241static struct screenshooter *
242screenshooter_create(struct egl_compositor *ec)
243{
244 struct screenshooter *shooter;
245
246 shooter = malloc(sizeof *shooter);
247 if (shooter == NULL)
248 return NULL;
249
250 shooter->base.interface = &screenshooter_interface;
251 shooter->ec = ec;
252
253 return shooter;
254};
255
Kristian Høgsberg54879822008-11-23 17:07:32 -0500256static struct egl_surface *
257egl_surface_create_from_cairo_surface(cairo_surface_t *surface,
258 int x, int y, int width, int height)
259{
260 struct egl_surface *es;
261 int stride;
262 void *data;
263
264 stride = cairo_image_surface_get_stride(surface);
265 data = cairo_image_surface_get_data(surface);
266
267 es = malloc(sizeof *es);
268 if (es == NULL)
269 return NULL;
270
271 glGenTextures(1, &es->texture);
272 glBindTexture(GL_TEXTURE_2D, es->texture);
273 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500274 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
275 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
276 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500277 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
278 GL_BGRA, GL_UNSIGNED_BYTE, data);
279
280 es->map.x = x;
281 es->map.y = y;
282 es->map.width = width;
283 es->map.height = height;
284 es->surface = EGL_NO_SURFACE;
285
286 return es;
287}
288
289static void
290egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec)
291{
292 glDeleteTextures(1, &es->texture);
293 if (es->surface != EGL_NO_SURFACE)
294 eglDestroySurface(ec->display, es->surface);
295 free(es);
296}
297
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400298static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500299pointer_path(cairo_t *cr, int x, int y)
300{
301 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
302 const int width = 16, height = 16;
303
304 cairo_move_to(cr, x, y);
305 cairo_line_to(cr, x + tx, y + ty);
306 cairo_line_to(cr, x + dx, y + dy);
307 cairo_line_to(cr, x + width - end, y + height);
308 cairo_line_to(cr, x + width, y + height - end);
309 cairo_line_to(cr, x + dy, y + dx);
310 cairo_line_to(cr, x + ty, y + tx);
311 cairo_close_path(cr);
312}
313
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500314static struct egl_surface *
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500315pointer_create(int x, int y, int width, int height)
316{
Kristian Høgsberg54879822008-11-23 17:07:32 -0500317 struct egl_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500318 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500319 cairo_surface_t *surface;
320 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500321
322 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
323 width, height);
324
325 cr = cairo_create(surface);
326 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
327 cairo_set_line_width (cr, 2);
328 cairo_set_source_rgb(cr, 0, 0, 0);
329 cairo_stroke_preserve(cr);
330 cairo_fill(cr);
331 blur_surface(surface, width);
332
333 pointer_path(cr, hotspot_x, hotspot_y);
334 cairo_stroke_preserve(cr);
335 cairo_set_source_rgb(cr, 1, 1, 1);
336 cairo_fill(cr);
337 cairo_destroy(cr);
338
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500339 es = egl_surface_create_from_cairo_surface(surface,
340 x - hotspot_x,
341 y - hotspot_y,
342 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500343
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500344 cairo_surface_destroy(surface);
345
Kristian Høgsberg54879822008-11-23 17:07:32 -0500346 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500347}
348
349static struct egl_surface *
350background_create(const char *filename, int width, int height)
351{
352 struct egl_surface *background;
353 GdkPixbuf *pixbuf;
354 GError *error = NULL;
355 int pixbuf_width, pixbuf_height;
356 void *data;
357
358 background = malloc(sizeof *background);
359 if (background == NULL)
360 return NULL;
361
362 g_type_init();
363
364 pixbuf = gdk_pixbuf_new_from_file(filename, &error);
365 if (error != NULL) {
366 free(background);
367 return NULL;
368 }
369
370 pixbuf_width = gdk_pixbuf_get_width(pixbuf);
371 pixbuf_height = gdk_pixbuf_get_height(pixbuf);
372 data = gdk_pixbuf_get_pixels(pixbuf);
373
374 glGenTextures(1, &background->texture);
375 glBindTexture(GL_TEXTURE_2D, background->texture);
376 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500377 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
378 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
379 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500380 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixbuf_width, pixbuf_height, 0,
381 GL_BGR, GL_UNSIGNED_BYTE, data);
382
383 background->map.x = 0;
384 background->map.y = 0;
385 background->map.width = width;
386 background->map.height = height;
387 background->surface = EGL_NO_SURFACE;
388
389 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500390}
391
392static void
Kristian Høgsberg54879822008-11-23 17:07:32 -0500393rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
394{
395 cairo_move_to(cr, x0, y0 + radius);
396 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
397 cairo_line_to(cr, x1 - radius, y0);
398 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
399 cairo_line_to(cr, x1, y1 - radius);
400 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
401 cairo_line_to(cr, x0 + radius, y1);
402 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
403 cairo_close_path(cr);
404}
405
406static void
407draw_button(cairo_t *cr, int x, int y, int width, int height, const char *text)
408{
409 cairo_pattern_t *gradient;
410 cairo_text_extents_t extents;
411 double bright = 0.15, dim = 0.02;
412 int radius = 10;
413
414 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
415 cairo_set_line_width (cr, 2);
416 rounded_rect(cr, x, y, x + width, y + height, radius);
417 cairo_set_source_rgb(cr, dim, dim, dim);
418 cairo_stroke(cr);
419 rounded_rect(cr, x + 2, y + 2, x + width, y + height, radius);
420 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
421 cairo_stroke(cr);
422
423 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
424 cairo_set_source_rgb(cr, bright, bright, bright);
425 cairo_stroke(cr);
426 rounded_rect(cr, x + 3, y + 3, x + width - 1, y + height - 1, radius - 1);
427 cairo_set_source_rgb(cr, dim, dim, dim);
428 cairo_stroke(cr);
429
430 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
431 gradient = cairo_pattern_create_linear (0, y, 0, y + height);
432 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.15, 0.15, 0.15);
433 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.08, 0.08, 0.08);
434 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.07, 0.07, 0.07);
435 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.1, 0.1, 0.1);
436 cairo_set_source(cr, gradient);
437 cairo_fill(cr);
438
439 cairo_set_font_size(cr, 16);
440 cairo_text_extents(cr, text, &extents);
441 cairo_move_to(cr, x + (width - extents.width) / 2, y + (height - extents.height) / 2 - extents.y_bearing);
442 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
443 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
444 cairo_set_line_width (cr, 4);
445 cairo_text_path(cr, text);
446 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
447 cairo_stroke_preserve(cr);
448 cairo_set_source_rgb(cr, 1, 1, 1);
449 cairo_fill(cr);
450}
451
452static struct egl_surface *
453overlay_create(int x, int y, int width, int height)
454{
455 struct egl_surface *es;
456 cairo_surface_t *surface;
457 cairo_t *cr;
458 int total_width, button_x, button_y;
459 const int button_width = 150;
460 const int button_height = 40;
461 const int spacing = 50;
462
463 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
464 width, height);
465
466 cr = cairo_create(surface);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500467 cairo_set_source_rgba(cr, 0.1, 0.1, 0.1, 0.8);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500468 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
469 cairo_paint(cr);
470
471 total_width = button_width * 2 + spacing;
472 button_x = (width - total_width) / 2;
473 button_y = height - button_height - 20;
474 draw_button(cr, button_x, button_y, button_width, button_height, "Previous");
475 button_x += button_width + spacing;
476 draw_button(cr, button_x, button_y, button_width, button_height, "Next");
477
478 cairo_destroy(cr);
479
480 es = egl_surface_create_from_cairo_surface(surface, x, y, width, height);
481
482 cairo_surface_destroy(surface);
483
484 return es;
485}
486
487static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500488draw_surface(struct egl_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500489{
490 GLint vertices[12];
491 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
492 GLuint indices[4] = { 0, 1, 2, 3 };
493
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500494 vertices[0] = es->map.x;
495 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500496 vertices[2] = 0;
497
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500498 vertices[3] = es->map.x;
499 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500500 vertices[5] = 0;
501
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500502 vertices[6] = es->map.x + es->map.width;
503 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500504 vertices[8] = 0;
505
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500506 vertices[9] = es->map.x + es->map.width;
507 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500508 vertices[11] = 0;
509
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500510 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500511 glEnable(GL_TEXTURE_2D);
512 glEnable(GL_BLEND);
513 /* Assume pre-multiplied alpha for now, this probably
514 * needs to be a wayland visual type of thing. */
515 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
516
517 glEnableClientState(GL_VERTEX_ARRAY);
518 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
519 glVertexPointer(3, GL_INT, 0, vertices);
520 glTexCoordPointer(2, GL_INT, 0, tex_coords);
521 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
522}
523
524static void
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500525schedule_repaint(struct egl_compositor *ec);
526
527static void
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500528animate_overlay(struct egl_compositor *ec)
529{
530 double force, y;
531 int32_t top, bottom;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500532#if 1
533 double bounce = 0.0;
534 double friction = 1.0;
535 double spring = 0.2;
536#else
537 double bounce = 0.2;
538 double friction = 0.04;
539 double spring = 0.09;
540#endif
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500541
542 y = ec->overlay_y;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500543 force = (ec->overlay_target - ec->overlay_y) * spring +
544 (ec->overlay_previous - y) * friction;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500545
546 ec->overlay_y = y + (y - ec->overlay_previous) + force;
547 ec->overlay_previous = y;
548
549 top = ec->height - ec->overlay->map.height;
550 bottom = ec->height;
551 if (ec->overlay_y >= bottom) {
552 ec->overlay_y = bottom;
553 ec->overlay_previous = bottom;
554 }
555
556 if (ec->overlay_y <= top) {
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500557 ec->overlay_y = top + bounce * (top - ec->overlay_y);
558 ec->overlay_previous =
559 top + bounce * (top - ec->overlay_previous);
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500560 }
561
562 ec->overlay->map.y = ec->overlay_y + 0.5;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500563
Kristian Høgsberg73c30582008-11-25 22:45:46 -0500564 if (fabs(y - ec->overlay_target) > 0.2 ||
565 fabs(ec->overlay_y - ec->overlay_target) > 0.2)
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500566 schedule_repaint(ec);
567}
568
569static void
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500570repaint(void *data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400571{
572 struct egl_compositor *ec = data;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500573 struct egl_surface *es;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500574 struct timespec ts;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500575 uint32_t msecs;
576
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500577 if (!ec->repaint_needed) {
578 ec->repaint_on_timeout = 0;
579 return;
580 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500581
582 draw_surface(ec->background);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400583
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500584 es = container_of(ec->surface_list.next,
585 struct egl_surface, link);
586 while (&es->link != &ec->surface_list) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500587 draw_surface(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500588
589 es = container_of(es->link.next,
590 struct egl_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400591 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400592
Kristian Høgsberg54879822008-11-23 17:07:32 -0500593 draw_surface(ec->overlay);
594
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500595 draw_surface(ec->input_device->pointer_surface);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500596
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400597 eglSwapBuffers(ec->display, ec->surface);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500598 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500599
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500600 clock_gettime(CLOCK_MONOTONIC, &ts);
601 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
602 wl_display_post_frame(ec->wl_display, ec->current_frame, msecs);
603 ec->current_frame++;
604
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500605 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500606 ec->repaint_on_timeout = 1;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500607
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500608 animate_overlay(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400609}
610
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500611static void
612schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400613{
614 struct wl_event_loop *loop;
615
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500616 ec->repaint_needed = 1;
617 if (!ec->repaint_on_timeout) {
618 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500619 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500620 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400621}
622
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500623static void
624notify_surface_create(struct wl_compositor *compositor,
625 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400626{
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500627 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500628 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400629
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500630 es = malloc(sizeof *es);
631 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400632 return;
633
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500634 es->surface = EGL_NO_SURFACE;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500635 es->wl_surface = surface;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500636 wl_surface_set_data(surface, es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500637 wl_list_insert(ec->surface_list.prev, &es->link);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400638
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500639 glGenTextures(1, &es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400640}
641
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500642static void
643notify_surface_destroy(struct wl_compositor *compositor,
644 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400645{
646 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500647 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400648
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500649 es = wl_surface_get_data(surface);
650 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400651 return;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500652
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500653 wl_list_remove(&es->link);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500654 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400655
656 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400657}
658
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500659static void
660notify_surface_attach(struct wl_compositor *compositor,
661 struct wl_surface *surface, uint32_t name,
662 uint32_t width, uint32_t height, uint32_t stride)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400663{
664 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500665 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400666
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500667 es = wl_surface_get_data(surface);
668 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400669 return;
670
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500671 if (es->surface != EGL_NO_SURFACE)
672 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400673
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500674 es->width = width;
675 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500676 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500677 name, width, height, stride, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400678
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500679 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400680 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500681 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
682 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
683 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500684 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400685}
686
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500687static void
688notify_surface_map(struct wl_compositor *compositor,
689 struct wl_surface *surface, struct wl_map *map)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400690{
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500691 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400692
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500693 es = wl_surface_get_data(surface);
694 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400695 return;
696
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500697 es->map = *map;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400698}
699
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500700static void
701notify_surface_copy(struct wl_compositor *compositor,
702 struct wl_surface *surface,
703 int32_t dst_x, int32_t dst_y,
704 uint32_t name, uint32_t stride,
705 int32_t x, int32_t y, int32_t width, int32_t height)
706{
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500707 struct egl_compositor *ec = (struct egl_compositor *) compositor;
708 EGLSurface src;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500709 struct egl_surface *es;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500710
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500711 es = wl_surface_get_data(surface);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500712
713 /* FIXME: glCopyPixels should work, but then we'll have to
714 * call eglMakeCurrent to set up the src and dest surfaces
715 * first. This seems cheaper, but maybe there's a better way
716 * to accomplish this. */
717
718 src = eglCreateSurfaceForName(ec->display, ec->config,
719 name, x + width, y + height, stride, NULL);
720
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500721 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500722 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500723 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500724}
725
726static void
727notify_surface_damage(struct wl_compositor *compositor,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500728 struct wl_surface *surface,
729 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500730{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500731 /* FIXME: This need to take a damage region, of course. */
732}
733
734static uint32_t
735notify_commit(struct wl_compositor *compositor)
736{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500737 struct egl_compositor *ec = (struct egl_compositor *) compositor;
738
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500739 schedule_repaint(ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500740
741 return ec->current_frame;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500742}
743
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500744static struct egl_surface *
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500745pick_surface(struct egl_input_device *device)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500746{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500747 struct egl_compositor *ec = device->ec;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500748 struct egl_surface *es;
749
750 es = container_of(ec->surface_list.prev,
751 struct egl_surface, link);
752 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500753 if (es->map.x <= device->x &&
754 device->x < es->map.x + es->map.width &&
755 es->map.y <= device->y &&
756 device->y < es->map.y + es->map.height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500757 return es;
758
759 es = container_of(es->link.prev,
760 struct egl_surface, link);
761 }
762
763 return NULL;
764}
765
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500766void
767notify_motion(struct egl_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500768{
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500769 struct egl_surface *es;
770 const int hotspot_x = 16, hotspot_y = 16;
771 int32_t sx, sy;
772
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500773 if (device->grab > 0)
774 es = device->grab_surface;
775 else
776 es = pick_surface(device);
777
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500778 if (es) {
779 sx = (x - es->map.x) * es->width / es->map.width;
780 sy = (y - es->map.y) * es->height / es->map.height;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500781 wl_surface_post_event(es->wl_surface, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500782 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500783 }
784
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500785 device->x = x;
786 device->y = y;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500787 device->pointer_surface->map.x = x - hotspot_x;
788 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500789
790 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500791}
792
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500793void
794notify_button(struct egl_input_device *device,
795 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500796{
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500797 struct egl_surface *es;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500798
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500799 es = pick_surface(device);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500800 if (es) {
801 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500802 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500803
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500804 if (state) {
805 device->grab++;
806 device->grab_surface = es;
807 } else {
808 device->grab--;
809 }
810
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500811 /* FIXME: Swallow click on raise? */
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500812 wl_surface_post_event(es->wl_surface, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500813 WL_INPUT_BUTTON, button, state);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500814
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500815 schedule_repaint(device->ec);
816 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500817}
818
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500819void
820notify_key(struct egl_input_device *device,
821 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500822{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500823 struct egl_compositor *ec = device->ec;
Kristian Høgsberg7fdff042008-12-10 13:25:00 -0500824 struct egl_surface *es;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500825
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500826 if (key == KEY_ESC && state == 1) {
827 if (ec->overlay_target == ec->height)
828 ec->overlay_target -= 200;
829 else
830 ec->overlay_target += 200;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500831 schedule_repaint(ec);
Kristian Høgsberg7fdff042008-12-10 13:25:00 -0500832 } else if (!wl_list_empty(&ec->surface_list)) {
833 /* FIXME: The event source device should track which
834 * surface has its key focus and send the event there.
835 * For now, just send it to the top surface, which
836 * effectively gives us click to focus behavior. */
837 es = container_of(ec->surface_list.prev,
838 struct egl_surface, link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500839 wl_surface_post_event(es->wl_surface, &device->base,
Kristian Høgsberg7fdff042008-12-10 13:25:00 -0500840 WL_INPUT_KEY, key, state);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500841 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500842}
843
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500844static const struct wl_compositor_interface interface = {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400845 notify_surface_create,
846 notify_surface_destroy,
847 notify_surface_attach,
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500848 notify_surface_map,
849 notify_surface_copy,
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500850 notify_surface_damage,
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500851 notify_commit,
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400852};
853
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500854static const char pointer_device_file[] =
855 "/dev/input/by-id/usb-Apple__Inc._Apple_Internal_Keyboard_._Trackpad-event-mouse";
856static const char keyboard_device_file[] =
857 "/dev/input/by-id/usb-Apple__Inc._Apple_Internal_Keyboard_._Trackpad-event-kbd";
858
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500859struct evdev_input_device *
860evdev_input_device_create(struct egl_input_device *device,
861 struct wl_display *display, const char *path);
862
863void
864egl_device_get_position(struct egl_input_device *device, int32_t *x, int32_t *y);
865
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500866static void
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500867create_input_devices(struct egl_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500868{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500869 struct egl_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500870 const char *path;
871
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500872 device = malloc(sizeof *device);
873 if (device == NULL)
874 return;
875
876 device->base.interface = wl_input_device_get_interface();
877 wl_display_add_object(ec->wl_display, &device->base);
878 ec->input_device = device;
879 device->x = 100;
880 device->y = 100;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500881 device->pointer_surface = pointer_create(device->x, device->y, 64, 64);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500882 device->ec = ec;
883
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500884 path = getenv("WAYLAND_POINTER");
885 if (path == NULL)
886 path = pointer_device_file;
887
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500888 evdev_input_device_create(device, ec->wl_display, path);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500889
890 path = getenv("WAYLAND_KEYBOARD");
891 if (path == NULL)
892 path = keyboard_device_file;
893
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500894 evdev_input_device_create(device, ec->wl_display, path);
895}
896
897void
898egl_device_get_position(struct egl_input_device *device, int32_t *x, int32_t *y)
899{
900 *x = device->x;
901 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500902}
903
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500904static uint32_t
905create_frontbuffer(int fd, int *width, int *height, int *stride)
906{
907 drmModeConnector *connector;
908 drmModeRes *resources;
909 drmModeEncoder *encoder;
910 struct drm_mode_modeinfo *mode;
911 struct drm_i915_gem_create create;
912 struct drm_i915_gem_pin pin;
913 struct drm_gem_flink flink;
914 unsigned int fb_id;
915 int i, ret;
916
917 resources = drmModeGetResources(fd);
918 if (!resources) {
919 fprintf(stderr, "drmModeGetResources failed\n");
920 return 0;
921 }
922
923 for (i = 0; i < resources->count_connectors; i++) {
924 connector = drmModeGetConnector(fd, resources->connectors[i]);
925 if (connector == NULL)
926 continue;
927
928 if (connector->connection == DRM_MODE_CONNECTED &&
929 connector->count_modes > 0)
930 break;
931
932 drmModeFreeConnector(connector);
933 }
934
935 if (i == resources->count_connectors) {
936 fprintf(stderr, "No currently active connector found.\n");
937 return -1;
938 }
939
940 mode = &connector->modes[0];
941
942 for (i = 0; i < resources->count_encoders; i++) {
943 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
944
945 if (encoder == NULL)
946 continue;
947
948 if (encoder->encoder_id == connector->encoder_id)
949 break;
950
951 drmModeFreeEncoder(encoder);
952 }
953
954 /* Mode size at 32 bpp */
955 create.size = mode->hdisplay * mode->vdisplay * 4;
956 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
957 fprintf(stderr, "gem create failed: %m\n");
958 return 0;
959 }
960
961 pin.handle = create.handle;
962 pin.alignment = 4096;
963 if (ioctl(fd, DRM_IOCTL_I915_GEM_PIN, &pin)) {
964 fprintf(stderr, "failed to pin buffer: %m\n");
965 return 0;
966 }
967
968 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
969 32, 32, mode->hdisplay * 4, create.handle, &fb_id);
970 if (ret) {
971 fprintf(stderr, "failed to add fb: %m\n");
972 return 0;
973 }
974
975 ret = drmModeSetCrtc(fd, encoder->crtc_id, fb_id, 0, 0,
976 &connector->connector_id, 1, mode);
977 if (ret) {
978 fprintf(stderr, "failed to set mode: %m\n");
979 return 0;
980 }
981
982 flink.handle = create.handle;
983 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
984 fprintf(stderr, "gem flink failed: %m\n");
985 return 0;
986 }
987
988 *width = mode->hdisplay;
989 *height = mode->vdisplay;
990 *stride = mode->hdisplay * 4;
991
992 return flink.name;
993}
994
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500995static int
996pick_config(struct egl_compositor *ec)
997{
998 EGLConfig configs[100];
999 EGLint value, count;
1000 int i;
1001
1002 if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) {
1003 fprintf(stderr, "failed to get configs\n");
1004 return -1;
1005 }
1006
1007 ec->config = EGL_NO_CONFIG;
1008 for (i = 0; i < count; i++) {
1009 eglGetConfigAttrib(ec->display,
1010 configs[i],
1011 EGL_DEPTH_SIZE,
1012 &value);
1013 if (value > 0) {
1014 fprintf(stderr, "config %d has depth size %d\n", i, value);
1015 continue;
1016 }
1017
1018 eglGetConfigAttrib(ec->display,
1019 configs[i],
1020 EGL_STENCIL_SIZE,
1021 &value);
1022 if (value > 0) {
1023 fprintf(stderr, "config %d has stencil size %d\n", i, value);
1024 continue;
1025 }
1026
1027 eglGetConfigAttrib(ec->display,
1028 configs[i],
1029 EGL_CONFIG_CAVEAT,
1030 &value);
1031 if (value != EGL_NONE) {
1032 fprintf(stderr, "config %d has caveat %d\n", i, value);
1033 continue;
1034 }
1035
1036 ec->config = configs[i];
1037 break;
1038 }
1039
1040 if (ec->config == EGL_NO_CONFIG) {
1041 fprintf(stderr, "found no config without depth or stencil buffers\n");
1042 return -1;
1043 }
1044
1045 return 0;
1046}
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001047
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001048static const char gem_device[] = "/dev/dri/card0";
1049
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001050static struct egl_compositor *
1051egl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001052{
Kristian Høgsberg443853c2008-11-25 12:12:05 -05001053 EGLint major, minor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001054 struct egl_compositor *ec;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -05001055 const char *filename;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001056 struct screenshooter *shooter;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001057 uint32_t fb_name;
1058 int stride;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001059 struct wl_event_loop *loop;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001060 const static EGLint attribs[] =
1061 { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE };
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001062
1063 ec = malloc(sizeof *ec);
1064 if (ec == NULL)
1065 return NULL;
1066
1067 ec->base.interface = &interface;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001068 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001069
Kristian Høgsbergc508d932008-10-13 22:52:42 -04001070 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001071 if (ec->display == NULL) {
1072 fprintf(stderr, "failed to create display\n");
1073 return NULL;
1074 }
1075
1076 if (!eglInitialize(ec->display, &major, &minor)) {
1077 fprintf(stderr, "failed to initialize display\n");
1078 return NULL;
1079 }
1080
Kristian Høgsberg443853c2008-11-25 12:12:05 -05001081 if (pick_config(ec))
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001082 return NULL;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001083
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001084 fb_name = create_frontbuffer(eglGetDisplayFD(ec->display),
1085 &ec->width, &ec->height, &stride);
1086 ec->surface = eglCreateSurfaceForName(ec->display, ec->config,
1087 fb_name, ec->width, ec->height, stride, attribs);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001088 if (ec->surface == NULL) {
1089 fprintf(stderr, "failed to create surface\n");
1090 return NULL;
1091 }
1092
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -05001093 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001094 if (ec->context == NULL) {
1095 fprintf(stderr, "failed to create context\n");
1096 return NULL;
1097 }
1098
1099 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
1100 fprintf(stderr, "failed to make context current\n");
1101 return NULL;
1102 }
1103
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001104 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001105 glMatrixMode(GL_PROJECTION);
1106 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001107 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001108 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001109
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001110 create_input_devices(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001111
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001112 wl_list_init(&ec->surface_list);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -05001113 filename = getenv("WAYLAND_BACKGROUND");
1114 if (filename == NULL)
1115 filename = "background.jpg";
1116 ec->background = background_create(filename, 1280, 800);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001117 ec->overlay = overlay_create(0, ec->height, ec->width, 200);
Kristian Høgsberg9f88b182008-12-08 13:52:08 -05001118 ec->overlay_y = ec->height;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001119 ec->overlay_target = ec->height;
1120 ec->overlay_previous = ec->height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001121
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001122 ec->gem_fd = open(gem_device, O_RDWR);
1123 if (ec->gem_fd < 0) {
1124 fprintf(stderr, "failed to open drm device\n");
1125 return NULL;
1126 }
1127
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001128 shooter = screenshooter_create(ec);
1129 wl_display_add_object(display, &shooter->base);
1130 wl_display_add_global(display, &shooter->base);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001131
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001132 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001133 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001134 ec->repaint_needed = 0;
1135 ec->repaint_on_timeout = 0;
1136
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001137 schedule_repaint(ec);
1138
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001139 return ec;
1140}
1141
1142/* The plan here is to generate a random anonymous socket name and
1143 * advertise that through a service on the session dbus.
1144 */
1145static const char socket_name[] = "\0wayland";
1146
1147int main(int argc, char *argv[])
1148{
1149 struct wl_display *display;
1150 struct egl_compositor *ec;
1151
1152 display = wl_display_create();
1153
1154 ec = egl_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001155 if (ec == NULL) {
1156 fprintf(stderr, "failed to create compositor\n");
1157 exit(EXIT_FAILURE);
1158 }
1159
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001160 wl_display_set_compositor(display, &ec->base);
1161
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001162 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001163 fprintf(stderr, "failed to add socket: %m\n");
1164 exit(EXIT_FAILURE);
1165 }
1166
1167 wl_display_run(display);
1168
1169 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001170}