blob: 90610252bd7b1ce69e8ac14d36f1561cd23118dc [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05008 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050017 */
18
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040019#include <stdio.h>
20#include <string.h>
21#include <stdlib.h>
22#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050023#include <stdarg.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040024#include <i915_drm.h>
25#include <sys/ioctl.h>
26#include <sys/mman.h>
27#include <fcntl.h>
28#include <unistd.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050029#include <signal.h>
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050030#include <cairo.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050031#include <gdk-pixbuf/gdk-pixbuf.h>
32#include <glib.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050033#include <sys/poll.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050034#include <png.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050035#include <math.h>
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -050036#include <linux/input.h>
Ray Strode19ad6a92008-12-19 01:45:41 -050037#include <linux/vt.h>
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -050038#include <xf86drmMode.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050039#include <time.h>
Kristian Høgsbergc492b482008-12-12 12:00:02 -050040#include <fnmatch.h>
41#include <dirent.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040042
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040043#include <GL/gl.h>
44#include <eagle.h>
45
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050046#include "wayland.h"
47#include "cairo-util.h"
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050048#include "wayland-system-compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050049
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040050#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
51
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050052struct wl_visual {
53 struct wl_object base;
54};
55
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050056struct wlsc_input_device {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050057 struct wl_object base;
58 int32_t x, y;
59 struct egl_compositor *ec;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050060 struct egl_surface *pointer_surface;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050061 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050062
63 int grab;
64 struct egl_surface *grab_surface;
Kristian Høgsberga7700c82008-12-12 13:48:30 -050065 struct egl_surface *focus_surface;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050066};
67
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040068struct egl_compositor {
69 struct wl_compositor base;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050070 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
71
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040072 EGLDisplay display;
73 EGLSurface surface;
74 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050075 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040076 struct wl_display *wl_display;
Ray Strode19ad6a92008-12-19 01:45:41 -050077 int tty_fd;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -050078 int width, height, stride;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050079 struct egl_surface *background;
Kristian Høgsberg54879822008-11-23 17:07:32 -050080 struct egl_surface *overlay;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -050081 double overlay_y, overlay_target, overlay_previous;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050082
Kristian Høgsbergc492b482008-12-12 12:00:02 -050083 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -050084 struct wl_list surface_list;
85
Ray Strode19ad6a92008-12-19 01:45:41 -050086 struct wl_event_source *enter_vt_source;
87 struct wl_event_source *leave_vt_source;
88
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -050089 /* Modesetting info. */
90 struct drm_mode_modeinfo *mode;
91 uint32_t fb_id;
92 uint32_t crtc_id;
93 uint32_t connector_id;
94
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050095 /* Repaint state. */
96 struct wl_event_source *timer_source;
97 int repaint_needed;
98 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050099 struct timespec previous_swap;
100 uint32_t current_frame;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400101};
102
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500103struct egl_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500104 struct wl_surface base;
105 struct egl_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500106 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400107 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400108 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500109 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500110 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500111 struct wl_list link;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400112};
113
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500114struct screenshooter {
115 struct wl_object base;
116 struct egl_compositor *ec;
117};
118
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500119struct screenshooter_interface {
120 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
121};
122
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500123static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500124screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500125{
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500126 struct egl_compositor *ec = shooter->ec;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500127 GLuint stride;
128 static const char filename[] = "wayland-screenshot.png";
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500129 GdkPixbuf *pixbuf;
130 GError *error = NULL;
131 void *data;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500132
133 data = eglReadBuffer(ec->display, ec->surface, GL_FRONT_LEFT, &stride);
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500134 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
135 8, ec->width, ec->height, stride,
136 NULL, NULL);
137 gdk_pixbuf_save(pixbuf, filename, "png", &error, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500138}
139
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500140static const struct wl_method screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500141 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500142};
143
144static const struct wl_interface screenshooter_interface = {
145 "screenshooter", 1,
146 ARRAY_LENGTH(screenshooter_methods),
147 screenshooter_methods,
148};
149
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500150struct screenshooter_interface screenshooter_implementation = {
151 screenshooter_shoot
152};
153
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500154static struct screenshooter *
155screenshooter_create(struct egl_compositor *ec)
156{
157 struct screenshooter *shooter;
158
159 shooter = malloc(sizeof *shooter);
160 if (shooter == NULL)
161 return NULL;
162
163 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500164 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500165 shooter->ec = ec;
166
167 return shooter;
168};
169
Kristian Høgsberg54879822008-11-23 17:07:32 -0500170static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500171egl_surface_create_from_cairo_surface(struct egl_compositor *ec,
172 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500173 int x, int y, int width, int height)
174{
175 struct egl_surface *es;
176 int stride;
177 void *data;
178
179 stride = cairo_image_surface_get_stride(surface);
180 data = cairo_image_surface_get_data(surface);
181
182 es = malloc(sizeof *es);
183 if (es == NULL)
184 return NULL;
185
186 glGenTextures(1, &es->texture);
187 glBindTexture(GL_TEXTURE_2D, es->texture);
188 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500189 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
190 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
191 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500192 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
193 GL_BGRA, GL_UNSIGNED_BYTE, data);
194
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500195 es->compositor = ec;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500196 es->map.x = x;
197 es->map.y = y;
198 es->map.width = width;
199 es->map.height = height;
200 es->surface = EGL_NO_SURFACE;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500201 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500202
203 return es;
204}
205
206static void
207egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec)
208{
209 glDeleteTextures(1, &es->texture);
210 if (es->surface != EGL_NO_SURFACE)
211 eglDestroySurface(ec->display, es->surface);
212 free(es);
213}
214
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400215static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500216pointer_path(cairo_t *cr, int x, int y)
217{
218 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
219 const int width = 16, height = 16;
220
221 cairo_move_to(cr, x, y);
222 cairo_line_to(cr, x + tx, y + ty);
223 cairo_line_to(cr, x + dx, y + dy);
224 cairo_line_to(cr, x + width - end, y + height);
225 cairo_line_to(cr, x + width, y + height - end);
226 cairo_line_to(cr, x + dy, y + dx);
227 cairo_line_to(cr, x + ty, y + tx);
228 cairo_close_path(cr);
229}
230
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500231static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500232pointer_create(struct egl_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500233{
Kristian Høgsberg54879822008-11-23 17:07:32 -0500234 struct egl_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500235 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500236 cairo_surface_t *surface;
237 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500238
239 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
240 width, height);
241
242 cr = cairo_create(surface);
243 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
244 cairo_set_line_width (cr, 2);
245 cairo_set_source_rgb(cr, 0, 0, 0);
246 cairo_stroke_preserve(cr);
247 cairo_fill(cr);
248 blur_surface(surface, width);
249
250 pointer_path(cr, hotspot_x, hotspot_y);
251 cairo_stroke_preserve(cr);
252 cairo_set_source_rgb(cr, 1, 1, 1);
253 cairo_fill(cr);
254 cairo_destroy(cr);
255
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500256 es = egl_surface_create_from_cairo_surface(ec,
257 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500258 x - hotspot_x,
259 y - hotspot_y,
260 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500261
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500262 cairo_surface_destroy(surface);
263
Kristian Høgsberg54879822008-11-23 17:07:32 -0500264 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500265}
266
267static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500268background_create(struct egl_compositor *ec,
269 const char *filename, int width, int height)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500270{
271 struct egl_surface *background;
272 GdkPixbuf *pixbuf;
273 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500274 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500275 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500276
277 background = malloc(sizeof *background);
278 if (background == NULL)
279 return NULL;
280
281 g_type_init();
282
Kristian Høgsberg2d4219e2008-12-18 21:40:03 -0500283 pixbuf = gdk_pixbuf_new_from_file_at_scale (filename,
284 width, height,
285 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500286 if (error != NULL) {
287 free(background);
288 return NULL;
289 }
290
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500291 data = gdk_pixbuf_get_pixels(pixbuf);
292
293 glGenTextures(1, &background->texture);
294 glBindTexture(GL_TEXTURE_2D, background->texture);
295 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500296 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
297 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
298 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Ray Strode18fd33c2008-12-18 21:05:20 -0500299
300 if (gdk_pixbuf_get_has_alpha (pixbuf)) {
301 format = GL_RGBA;
302 } else {
303 format = GL_RGB;
304 }
305
Kristian Høgsberg2d4219e2008-12-18 21:40:03 -0500306 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500307 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500308
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500309 background->compositor = ec;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500310 background->map.x = 0;
311 background->map.y = 0;
312 background->map.width = width;
313 background->map.height = height;
314 background->surface = EGL_NO_SURFACE;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500315 background->visual = &ec->rgb_visual;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500316
317 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500318}
319
320static void
Kristian Høgsberg54879822008-11-23 17:07:32 -0500321rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
322{
323 cairo_move_to(cr, x0, y0 + radius);
324 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
325 cairo_line_to(cr, x1 - radius, y0);
326 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
327 cairo_line_to(cr, x1, y1 - radius);
328 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
329 cairo_line_to(cr, x0 + radius, y1);
330 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
331 cairo_close_path(cr);
332}
333
334static void
335draw_button(cairo_t *cr, int x, int y, int width, int height, const char *text)
336{
337 cairo_pattern_t *gradient;
338 cairo_text_extents_t extents;
339 double bright = 0.15, dim = 0.02;
340 int radius = 10;
341
342 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
343 cairo_set_line_width (cr, 2);
344 rounded_rect(cr, x, y, x + width, y + height, radius);
345 cairo_set_source_rgb(cr, dim, dim, dim);
346 cairo_stroke(cr);
347 rounded_rect(cr, x + 2, y + 2, x + width, y + height, radius);
348 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
349 cairo_stroke(cr);
350
351 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
352 cairo_set_source_rgb(cr, bright, bright, bright);
353 cairo_stroke(cr);
354 rounded_rect(cr, x + 3, y + 3, x + width - 1, y + height - 1, radius - 1);
355 cairo_set_source_rgb(cr, dim, dim, dim);
356 cairo_stroke(cr);
357
358 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
359 gradient = cairo_pattern_create_linear (0, y, 0, y + height);
360 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.15, 0.15, 0.15);
361 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.08, 0.08, 0.08);
362 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.07, 0.07, 0.07);
363 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.1, 0.1, 0.1);
364 cairo_set_source(cr, gradient);
365 cairo_fill(cr);
366
367 cairo_set_font_size(cr, 16);
368 cairo_text_extents(cr, text, &extents);
369 cairo_move_to(cr, x + (width - extents.width) / 2, y + (height - extents.height) / 2 - extents.y_bearing);
370 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
371 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
372 cairo_set_line_width (cr, 4);
373 cairo_text_path(cr, text);
374 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
375 cairo_stroke_preserve(cr);
376 cairo_set_source_rgb(cr, 1, 1, 1);
377 cairo_fill(cr);
378}
379
380static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500381overlay_create(struct egl_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500382{
383 struct egl_surface *es;
384 cairo_surface_t *surface;
385 cairo_t *cr;
386 int total_width, button_x, button_y;
387 const int button_width = 150;
388 const int button_height = 40;
389 const int spacing = 50;
390
391 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
392 width, height);
393
394 cr = cairo_create(surface);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500395 cairo_set_source_rgba(cr, 0.1, 0.1, 0.1, 0.8);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500396 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
397 cairo_paint(cr);
398
399 total_width = button_width * 2 + spacing;
400 button_x = (width - total_width) / 2;
401 button_y = height - button_height - 20;
402 draw_button(cr, button_x, button_y, button_width, button_height, "Previous");
403 button_x += button_width + spacing;
404 draw_button(cr, button_x, button_y, button_width, button_height, "Next");
405
406 cairo_destroy(cr);
407
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500408 es = egl_surface_create_from_cairo_surface(ec, surface,
409 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500410
411 cairo_surface_destroy(surface);
412
413 return es;
414}
415
416static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500417draw_surface(struct egl_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500418{
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500419 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500420 GLint vertices[12];
421 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
422 GLuint indices[4] = { 0, 1, 2, 3 };
423
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500424 vertices[0] = es->map.x;
425 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500426 vertices[2] = 0;
427
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500428 vertices[3] = es->map.x;
429 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500430 vertices[5] = 0;
431
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500432 vertices[6] = es->map.x + es->map.width;
433 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500434 vertices[8] = 0;
435
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500436 vertices[9] = es->map.x + es->map.width;
437 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500438 vertices[11] = 0;
439
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500440 if (es->visual == &ec->argb_visual) {
441 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
442 glEnable(GL_BLEND);
443 } else if (es->visual == &ec->premultiplied_argb_visual) {
444 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
445 glEnable(GL_BLEND);
446 } else {
447 glDisable(GL_BLEND);
448 }
449
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500450 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500451 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500452 glEnableClientState(GL_VERTEX_ARRAY);
453 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
454 glVertexPointer(3, GL_INT, 0, vertices);
455 glTexCoordPointer(2, GL_INT, 0, tex_coords);
456 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
457}
458
459static void
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500460schedule_repaint(struct egl_compositor *ec);
461
462static void
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500463animate_overlay(struct egl_compositor *ec)
464{
465 double force, y;
466 int32_t top, bottom;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500467#if 1
468 double bounce = 0.0;
469 double friction = 1.0;
470 double spring = 0.2;
471#else
472 double bounce = 0.2;
473 double friction = 0.04;
474 double spring = 0.09;
475#endif
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500476
477 y = ec->overlay_y;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500478 force = (ec->overlay_target - ec->overlay_y) * spring +
479 (ec->overlay_previous - y) * friction;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500480
481 ec->overlay_y = y + (y - ec->overlay_previous) + force;
482 ec->overlay_previous = y;
483
484 top = ec->height - ec->overlay->map.height;
485 bottom = ec->height;
486 if (ec->overlay_y >= bottom) {
487 ec->overlay_y = bottom;
488 ec->overlay_previous = bottom;
489 }
490
491 if (ec->overlay_y <= top) {
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500492 ec->overlay_y = top + bounce * (top - ec->overlay_y);
493 ec->overlay_previous =
494 top + bounce * (top - ec->overlay_previous);
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500495 }
496
497 ec->overlay->map.y = ec->overlay_y + 0.5;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500498
Kristian Høgsberg73c30582008-11-25 22:45:46 -0500499 if (fabs(y - ec->overlay_target) > 0.2 ||
500 fabs(ec->overlay_y - ec->overlay_target) > 0.2)
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500501 schedule_repaint(ec);
502}
503
504static void
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500505repaint(void *data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400506{
507 struct egl_compositor *ec = data;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500508 struct egl_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500509 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500510 struct timespec ts;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500511 uint32_t msecs;
512
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500513 if (!ec->repaint_needed) {
514 ec->repaint_on_timeout = 0;
515 return;
516 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500517
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500518 if (ec->background)
519 draw_surface(ec->background);
520 else
521 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400522
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500523 es = container_of(ec->surface_list.next,
524 struct egl_surface, link);
525 while (&es->link != &ec->surface_list) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500526 draw_surface(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500527
528 es = container_of(es->link.next,
529 struct egl_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400530 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400531
Kristian Høgsberg54879822008-11-23 17:07:32 -0500532 draw_surface(ec->overlay);
533
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500534 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500535 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500536 while (&eid->link != &ec->input_device_list) {
537 draw_surface(eid->pointer_surface);
538
539 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500540 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500541 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500542
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400543 eglSwapBuffers(ec->display, ec->surface);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500544 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500545
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500546 clock_gettime(CLOCK_MONOTONIC, &ts);
547 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500548 wl_display_post_frame(ec->wl_display, &ec->base,
549 ec->current_frame, msecs);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500550 ec->current_frame++;
551
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500552 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500553 ec->repaint_on_timeout = 1;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500554
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500555 animate_overlay(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400556}
557
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500558static void
559schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400560{
561 struct wl_event_loop *loop;
562
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500563 ec->repaint_needed = 1;
564 if (!ec->repaint_on_timeout) {
565 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500566 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500567 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400568}
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400569
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500570static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500571surface_destroy(struct wl_client *client,
572 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400573{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500574 struct egl_surface *es = (struct egl_surface *) surface;
575 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500576
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500577 wl_list_remove(&es->link);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500578 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400579
580 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400581}
582
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500583static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500584surface_attach(struct wl_client *client,
585 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500586 uint32_t width, uint32_t height, uint32_t stride,
587 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400588{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500589 struct egl_surface *es = (struct egl_surface *) surface;
590 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400591
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500592 if (es->surface != EGL_NO_SURFACE)
593 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400594
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500595 es->width = width;
596 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500597 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500598 name, width, height, stride, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500599 if (visual == &ec->argb_visual.base)
600 es->visual = &ec->argb_visual;
601 else if (visual == &ec->premultiplied_argb_visual.base)
602 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500603 else if (visual == &ec->rgb_visual.base)
604 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500605 else
606 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400607
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500608 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400609 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500610 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
611 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
612 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500613 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400614}
615
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500616static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500617surface_map(struct wl_client *client,
618 struct wl_surface *surface,
619 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400620{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500621 struct egl_surface *es = (struct egl_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400622
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500623 es->map.x = x;
624 es->map.y = y;
625 es->map.width = width;
626 es->map.height = height;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400627}
628
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500629static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500630surface_copy(struct wl_client *client,
631 struct wl_surface *surface,
632 int32_t dst_x, int32_t dst_y,
633 uint32_t name, uint32_t stride,
634 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500635{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500636 struct egl_surface *es = (struct egl_surface *) surface;
637 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500638 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500639
640 /* FIXME: glCopyPixels should work, but then we'll have to
641 * call eglMakeCurrent to set up the src and dest surfaces
642 * first. This seems cheaper, but maybe there's a better way
643 * to accomplish this. */
644
645 src = eglCreateSurfaceForName(ec->display, ec->config,
646 name, x + width, y + height, stride, NULL);
647
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500648 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500649 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500650 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500651}
652
653static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500654surface_damage(struct wl_client *client,
655 struct wl_surface *surface,
656 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500657{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500658 /* FIXME: This need to take a damage region, of course. */
659}
660
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500661const static struct wl_surface_interface surface_interface = {
662 surface_destroy,
663 surface_attach,
664 surface_map,
665 surface_copy,
666 surface_damage
667};
668
669static void
670compositor_create_surface(struct wl_client *client,
671 struct wl_compositor *compositor, uint32_t id)
672{
673 struct egl_compositor *ec = (struct egl_compositor *) compositor;
674 struct egl_surface *es;
675
676 es = malloc(sizeof *es);
677 if (es == NULL)
678 /* FIXME: Send OOM event. */
679 return;
680
681 es->compositor = ec;
682 es->surface = EGL_NO_SURFACE;
683 wl_list_insert(ec->surface_list.prev, &es->link);
684 glGenTextures(1, &es->texture);
685 wl_client_add_surface(client, &es->base,
686 &surface_interface, id);
687}
688
689static void
690compositor_commit(struct wl_client *client,
691 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500692{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500693 struct egl_compositor *ec = (struct egl_compositor *) compositor;
694
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500695 schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500696 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500697}
698
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500699const static struct wl_compositor_interface compositor_interface = {
700 compositor_create_surface,
701 compositor_commit
702};
703
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500704static struct egl_surface *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500705pick_surface(struct wlsc_input_device *device)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500706{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500707 struct egl_compositor *ec = device->ec;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500708 struct egl_surface *es;
709
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500710 if (device->grab > 0)
711 return device->grab_surface;
712
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500713 es = container_of(ec->surface_list.prev,
714 struct egl_surface, link);
715 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500716 if (es->map.x <= device->x &&
717 device->x < es->map.x + es->map.width &&
718 es->map.y <= device->y &&
719 device->y < es->map.y + es->map.height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500720 return es;
721
722 es = container_of(es->link.prev,
723 struct egl_surface, link);
724 }
725
726 return NULL;
727}
728
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500729void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500730notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500731{
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500732 struct egl_surface *es;
Ray Strode90e701d2008-12-18 23:05:43 -0500733 struct egl_compositor *ec = device->ec;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500734 const int hotspot_x = 16, hotspot_y = 16;
735 int32_t sx, sy;
736
Ray Strode90e701d2008-12-18 23:05:43 -0500737 if (x < 0)
738 x = 0;
739 if (y < 0)
740 y = 0;
741 if (x >= ec->width)
742 x = ec->width - 1;
743 if (y >= ec->height)
744 y = ec->height - 1;
745
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500746 es = pick_surface(device);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500747
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500748 if (es) {
749 sx = (x - es->map.x) * es->width / es->map.width;
750 sy = (y - es->map.y) * es->height / es->map.height;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500751 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500752 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500753 }
754
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500755 device->x = x;
756 device->y = y;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500757 device->pointer_surface->map.x = x - hotspot_x;
758 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500759
760 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500761}
762
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500763void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500764notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500765 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500766{
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500767 struct egl_surface *es;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500768 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500769
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500770 es = pick_surface(device);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500771 if (es) {
772 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500773 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500774
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500775 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500776 /* FIXME: We need callbacks when the surfaces
777 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500778 device->grab++;
779 device->grab_surface = es;
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500780 device->focus_surface = es;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500781 } else {
782 device->grab--;
783 }
784
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500785 sx = (device->x - es->map.x) * es->width / es->map.width;
786 sy = (device->y - es->map.y) * es->height / es->map.height;
787
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500788 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500789 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500790 WL_INPUT_BUTTON, button, state,
791 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500792
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500793 schedule_repaint(device->ec);
794 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500795}
796
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500797void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500798notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500799 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500800{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500801 struct egl_compositor *ec = device->ec;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500802
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500803 if (key == KEY_ESC && state == 1) {
804 if (ec->overlay_target == ec->height)
805 ec->overlay_target -= 200;
806 else
807 ec->overlay_target += 200;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500808 schedule_repaint(ec);
Kristian Høgsberg7fdff042008-12-10 13:25:00 -0500809 } else if (!wl_list_empty(&ec->surface_list)) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500810 if (device->focus_surface != NULL)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500811 wl_surface_post_event(&device->focus_surface->base,
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500812 &device->base,
813 WL_INPUT_KEY, key, state);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500814 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500815}
816
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500817struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500818evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500819 struct wl_display *display, const char *path);
820
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500821static void
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500822create_input_device(struct egl_compositor *ec, const char *glob)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500823{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500824 struct wlsc_input_device *device;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500825 struct dirent *de;
826 char path[PATH_MAX];
827 const char *by_path_dir = "/dev/input/by-path";
828 DIR *dir;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500829
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500830 device = malloc(sizeof *device);
831 if (device == NULL)
832 return;
833
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500834 memset(device, 0, sizeof *device);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500835 device->base.interface = wl_input_device_get_interface();
836 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500837 device->x = 100;
838 device->y = 100;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500839 device->pointer_surface = pointer_create(ec,
840 device->x, device->y, 64, 64);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500841 device->ec = ec;
842
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500843 dir = opendir(by_path_dir);
844 if (dir == NULL) {
845 fprintf(stderr, "couldn't read dir %s\n", by_path_dir);
846 return;
847 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500848
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500849 while (de = readdir(dir), de != NULL) {
850 if (fnmatch(glob, de->d_name, 0))
851 continue;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500852
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500853 snprintf(path, sizeof path, "%s/%s", by_path_dir, de->d_name);
854 evdev_input_device_create(device, ec->wl_display, path);
855 }
856 closedir(dir);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500857
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500858 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500859}
860
861void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500862wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500863{
864 *x = device->x;
865 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500866}
867
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500868static uint32_t
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500869create_frontbuffer(struct egl_compositor *ec)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500870{
871 drmModeConnector *connector;
872 drmModeRes *resources;
873 drmModeEncoder *encoder;
874 struct drm_mode_modeinfo *mode;
875 struct drm_i915_gem_create create;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500876 struct drm_gem_flink flink;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500877 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500878
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500879 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500880 resources = drmModeGetResources(fd);
881 if (!resources) {
882 fprintf(stderr, "drmModeGetResources failed\n");
883 return 0;
884 }
885
886 for (i = 0; i < resources->count_connectors; i++) {
887 connector = drmModeGetConnector(fd, resources->connectors[i]);
888 if (connector == NULL)
889 continue;
890
891 if (connector->connection == DRM_MODE_CONNECTED &&
892 connector->count_modes > 0)
893 break;
894
895 drmModeFreeConnector(connector);
896 }
897
898 if (i == resources->count_connectors) {
899 fprintf(stderr, "No currently active connector found.\n");
900 return -1;
901 }
902
903 mode = &connector->modes[0];
904
905 for (i = 0; i < resources->count_encoders; i++) {
906 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
907
908 if (encoder == NULL)
909 continue;
910
911 if (encoder->encoder_id == connector->encoder_id)
912 break;
913
914 drmModeFreeEncoder(encoder);
915 }
916
917 /* Mode size at 32 bpp */
918 create.size = mode->hdisplay * mode->vdisplay * 4;
919 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
920 fprintf(stderr, "gem create failed: %m\n");
921 return 0;
922 }
923
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500924 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500925 32, 32, mode->hdisplay * 4, create.handle, &ec->fb_id);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500926 if (ret) {
927 fprintf(stderr, "failed to add fb: %m\n");
928 return 0;
929 }
930
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500931 ret = drmModeSetCrtc(fd, encoder->crtc_id, ec->fb_id, 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500932 &connector->connector_id, 1, mode);
933 if (ret) {
934 fprintf(stderr, "failed to set mode: %m\n");
935 return 0;
936 }
937
938 flink.handle = create.handle;
939 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
940 fprintf(stderr, "gem flink failed: %m\n");
941 return 0;
942 }
943
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500944 ec->crtc_id = encoder->crtc_id;
945 ec->connector_id = connector->connector_id;
946 ec->mode = mode;
947 ec->width = mode->hdisplay;
948 ec->height = mode->vdisplay;
949 ec->stride = mode->hdisplay * 4;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500950
951 return flink.name;
952}
953
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500954static const struct wl_interface visual_interface = {
955 "visual", 1,
956};
957
958static void
959add_visuals(struct egl_compositor *ec)
960{
961 ec->argb_visual.base.interface = &visual_interface;
962 ec->argb_visual.base.implementation = NULL;
963 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
964 wl_display_add_global(ec->wl_display, &ec->argb_visual.base);
965
966 ec->premultiplied_argb_visual.base.interface = &visual_interface;
967 ec->premultiplied_argb_visual.base.implementation = NULL;
968 wl_display_add_object(ec->wl_display,
969 &ec->premultiplied_argb_visual.base);
970 wl_display_add_global(ec->wl_display,
971 &ec->premultiplied_argb_visual.base);
972
973 ec->rgb_visual.base.interface = &visual_interface;
974 ec->rgb_visual.base.implementation = NULL;
975 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
976 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base);
977}
978
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400979static const char gem_device[] = "/dev/dri/card0";
980
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500981static const char *macbook_air_default_input_device[] = {
Kristian Høgsberg64949972008-12-12 14:48:46 -0500982 "pci-0000:00:1d.2-usb-0:2:1*event*",
983 NULL
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500984};
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500985
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500986static const char *option_background = "background.jpg";
987static const char **option_input_devices = macbook_air_default_input_device;
988
989static const GOptionEntry option_entries[] = {
990 { "background", 'b', 0, G_OPTION_ARG_STRING,
991 &option_background, "Background image" },
992 { "input-device", 'i', 0, G_OPTION_ARG_STRING_ARRAY,
993 &option_input_devices, "Input device glob" },
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500994 { NULL }
995};
996
Ray Strode19ad6a92008-12-19 01:45:41 -0500997static void on_enter_vt(int signal_number, void *data)
998{
999 struct egl_compositor *ec = data;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001000 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -05001001
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001002 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
1003
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001004 fd = eglGetDisplayFD(ec->display);
1005 ret = drmModeSetCrtc(fd, ec->crtc_id, ec->fb_id, 0, 0,
1006 &ec->connector_id, 1, ec->mode);
Ray Strode19ad6a92008-12-19 01:45:41 -05001007 if (ret) {
1008 fprintf(stderr, "failed to set mode: %m\n");
1009 return;
1010 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001011}
1012
1013static void on_leave_vt(int signal_number, void *data)
1014{
1015 struct egl_compositor *ec = data;
1016
1017 ioctl (ec->tty_fd, VT_RELDISP, 1);
1018}
1019
1020static void watch_for_vt_changes(struct egl_compositor *ec, struct wl_event_loop *loop)
1021{
Ray Strode19ad6a92008-12-19 01:45:41 -05001022 struct vt_mode mode = { 0 };
1023
1024 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1025 mode.mode = VT_PROCESS;
1026 mode.relsig = SIGUSR1;
1027 mode.acqsig = SIGUSR2;
1028
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001029 if (!ioctl (ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001030 fprintf(stderr, "failed to take control of vt handling\n");
1031 }
1032
1033 ec->leave_vt_source = wl_event_loop_add_signal (loop, SIGUSR1,
1034 on_leave_vt,
1035 ec);
1036 ec->enter_vt_source = wl_event_loop_add_signal (loop, SIGUSR2,
1037 on_enter_vt,
1038 ec);
1039}
1040
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001041static struct egl_compositor *
1042egl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001043{
Kristian Høgsberg15d0f8b2008-12-19 12:13:42 -05001044 static const EGLint config_attribs[] = {
1045 EGL_DEPTH_SIZE, 0,
1046 EGL_STENCIL_SIZE, 0,
1047 EGL_CONFIG_CAVEAT, EGL_NONE,
1048 EGL_NONE
1049 };
1050
1051 const static EGLint attribs[] = {
1052 EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
1053 EGL_NONE
1054 };
1055
Kristian Høgsberg443853c2008-11-25 12:12:05 -05001056 EGLint major, minor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001057 struct egl_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001058 struct screenshooter *shooter;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001059 uint32_t fb_name;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001060 int i;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001061 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001062
1063 ec = malloc(sizeof *ec);
1064 if (ec == NULL)
1065 return NULL;
1066
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001067 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001068
Kristian Høgsbergc508d932008-10-13 22:52:42 -04001069 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001070 if (ec->display == NULL) {
1071 fprintf(stderr, "failed to create display\n");
1072 return NULL;
1073 }
1074
1075 if (!eglInitialize(ec->display, &major, &minor)) {
1076 fprintf(stderr, "failed to initialize display\n");
1077 return NULL;
1078 }
1079
Kristian Høgsberg15d0f8b2008-12-19 12:13:42 -05001080 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001081 return NULL;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001082
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001083 fb_name = create_frontbuffer(ec);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001084 ec->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001085 fb_name, ec->width, ec->height, ec->stride, attribs);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001086 if (ec->surface == NULL) {
1087 fprintf(stderr, "failed to create surface\n");
1088 return NULL;
1089 }
1090
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -05001091 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001092 if (ec->context == NULL) {
1093 fprintf(stderr, "failed to create context\n");
1094 return NULL;
1095 }
1096
1097 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
1098 fprintf(stderr, "failed to make context current\n");
1099 return NULL;
1100 }
1101
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001102 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001103 glMatrixMode(GL_PROJECTION);
1104 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001105 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001106 glMatrixMode(GL_MODELVIEW);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -05001107 glClearColor(0, 0, 0.2, 1);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001108
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001109 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001110 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001111
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001112 wl_list_init(&ec->input_device_list);
1113 for (i = 0; option_input_devices[i]; i++)
1114 create_input_device(ec, option_input_devices[i]);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001115
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001116 wl_list_init(&ec->surface_list);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001117 ec->background = background_create(ec, option_background,
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001118 ec->width, ec->height);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001119 ec->overlay = overlay_create(ec, 0, ec->height, ec->width, 200);
Kristian Høgsberg9f88b182008-12-08 13:52:08 -05001120 ec->overlay_y = ec->height;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001121 ec->overlay_target = ec->height;
1122 ec->overlay_previous = ec->height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001123
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001124 shooter = screenshooter_create(ec);
1125 wl_display_add_object(display, &shooter->base);
1126 wl_display_add_global(display, &shooter->base);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001127
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001128 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strode19ad6a92008-12-19 01:45:41 -05001129 watch_for_vt_changes (ec, loop);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001130 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001131 ec->repaint_needed = 0;
1132 ec->repaint_on_timeout = 0;
1133
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001134 schedule_repaint(ec);
1135
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001136 return ec;
1137}
1138
1139/* The plan here is to generate a random anonymous socket name and
1140 * advertise that through a service on the session dbus.
1141 */
1142static const char socket_name[] = "\0wayland";
1143
1144int main(int argc, char *argv[])
1145{
1146 struct wl_display *display;
1147 struct egl_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001148 GError *error = NULL;
1149 GOptionContext *context;
1150
1151 context = g_option_context_new(NULL);
1152 g_option_context_add_main_entries(context, option_entries, "Wayland");
1153 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1154 fprintf(stderr, "option parsing failed: %s\n", error->message);
1155 exit(EXIT_FAILURE);
1156 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001157
1158 display = wl_display_create();
1159
1160 ec = egl_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001161 if (ec == NULL) {
1162 fprintf(stderr, "failed to create compositor\n");
1163 exit(EXIT_FAILURE);
1164 }
1165
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001166 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001167 fprintf(stderr, "failed to add socket: %m\n");
1168 exit(EXIT_FAILURE);
1169 }
1170
1171 wl_display_run(display);
1172
1173 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001174}