blob: faece3a6bfefc936cc8e82f7b5ab2ea40edeee37 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04005 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050014 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040015 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050022 */
23
Kristian Høgsberg06bc2642010-12-01 09:50:16 -050024#define _GNU_SOURCE
25
Kristian Høgsberga9410222011-01-14 17:22:35 -050026#include "config.h"
27
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010032#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020034#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040035#include <sys/ioctl.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040036#include <sys/wait.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040037#include <fcntl.h>
38#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050039#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040040#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040041#include <dlfcn.h>
Kristian Høgsbergd34912c2011-05-02 10:36:04 -040042#include <getopt.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040043#include <signal.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050044
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050045#include "wayland-server.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040046#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050047
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010048/* The plan here is to generate a random anonymous socket name and
49 * advertise that through a service on the session dbus.
50 */
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -050051static const char *option_socket_name = NULL;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -040052static int option_idle_time = 300;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050053
Kristian Høgsberg27da5382011-06-21 17:32:25 -040054static struct wl_list child_process_list;
55
56static int
57sigchld_handler(int signal_number, void *data)
58{
59 struct wlsc_process *p;
60 int status;
61 pid_t pid;
62
63 pid = wait(&status);
64 wl_list_for_each(p, &child_process_list, link) {
65 if (p->pid == pid)
66 break;
67 }
68
69 if (&p->link == &child_process_list) {
70 fprintf(stderr, "unknown child process exited\n");
71 return 1;
72 }
73
74 wl_list_remove(&p->link);
75 p->cleanup(p, status);
76
77 return 1;
78}
79
80WL_EXPORT void
81wlsc_watch_process(struct wlsc_process *process)
82{
83 wl_list_insert(&child_process_list, &process->link);
84}
85
Kristian Høgsbergd34912c2011-05-02 10:36:04 -040086WL_EXPORT void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050087wlsc_matrix_init(struct wlsc_matrix *matrix)
88{
89 static const struct wlsc_matrix identity = {
90 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
91 };
92
93 memcpy(matrix, &identity, sizeof identity);
94}
95
96static void
97wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
98{
99 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400100 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500101 div_t d;
102 int i, j;
103
104 for (i = 0; i < 16; i++) {
105 tmp.d[i] = 0;
106 d = div(i, 4);
107 row = m->d + d.quot * 4;
108 column = n->d + d.rem;
109 for (j = 0; j < 4; j++)
110 tmp.d[i] += row[j] * column[j * 4];
111 }
112 memcpy(m, &tmp, sizeof tmp);
113}
114
Kristian Høgsbergd880e142011-05-02 13:53:45 -0400115WL_EXPORT void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400116wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500117{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500118 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500119 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
120 };
121
122 wlsc_matrix_multiply(matrix, &translate);
123}
124
Kristian Høgsbergd880e142011-05-02 13:53:45 -0400125WL_EXPORT void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400126wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500127{
128 struct wlsc_matrix scale = {
129 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
130 };
131
132 wlsc_matrix_multiply(matrix, &scale);
133}
134
135static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400136wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
137{
138 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400139 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400140
141 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400142 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400143 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400144 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400145 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500146
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400147 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400148}
149
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400150WL_EXPORT void
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400151wlsc_spring_init(struct wlsc_spring *spring,
152 double k, double current, double target)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400153{
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400154 spring->k = k;
155 spring->friction = 100.0;
156 spring->current = current;
157 spring->previous = current;
158 spring->target = target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400159}
160
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400161WL_EXPORT void
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400162wlsc_spring_update(struct wlsc_spring *spring, uint32_t msec)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400163{
Kristian Høgsberg4a9be132011-05-02 13:38:03 -0400164 double force, v, current, step;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400165
Kristian Høgsberg52612f12011-05-19 11:55:50 -0400166 step = (msec - spring->timestamp) / 300.0;
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400167 spring->timestamp = msec;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400168
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400169 current = spring->current;
170 v = current - spring->previous;
171 force = spring->k * (spring->target - current) / 10.0 +
172 (spring->previous - current) - v * spring->friction;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400173
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400174 spring->current =
175 current + (current - spring->previous) + force * step * step;
176 spring->previous = current;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400177
Kristian Høgsberg52612f12011-05-19 11:55:50 -0400178#if 0
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400179 if (spring->current >= 1.0) {
Kristian Høgsberg4a9be132011-05-02 13:38:03 -0400180#ifdef TWEENER_BOUNCE
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400181 spring->current = 2.0 - spring->current;
182 spring->previous = 2.0 - spring->previous;
Kristian Høgsberg4a9be132011-05-02 13:38:03 -0400183#else
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400184 spring->current = 1.0;
185 spring->previous = 1.0;
Kristian Høgsberg4a9be132011-05-02 13:38:03 -0400186#endif
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400187 }
188
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400189 if (spring->current <= 0.0) {
190 spring->current = 0.0;
191 spring->previous = 0.0;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400192 }
Kristian Høgsberg52612f12011-05-19 11:55:50 -0400193#endif
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400194}
195
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400196WL_EXPORT int
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400197wlsc_spring_done(struct wlsc_spring *spring)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400198{
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400199 return fabs(spring->previous - spring->target) < 0.0002 &&
200 fabs(spring->current - spring->target) < 0.0002;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400201}
202
Benjamin Franzke06286262011-05-06 19:12:33 +0200203static void
204surface_handle_buffer_destroy(struct wl_listener *listener,
205 struct wl_resource *resource, uint32_t time)
206{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400207 struct wlsc_surface *es = container_of(listener, struct wlsc_surface,
Benjamin Franzke06286262011-05-06 19:12:33 +0200208 buffer_destroy_listener);
Benjamin Franzke06286262011-05-06 19:12:33 +0200209
Kristian Høgsberg24596942011-10-24 17:51:02 -0400210 es->buffer = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200211}
212
213static void
214output_handle_scanout_buffer_destroy(struct wl_listener *listener,
215 struct wl_resource *resource,
216 uint32_t time)
217{
218 struct wlsc_output *output =
219 container_of(listener, struct wlsc_output,
220 scanout_buffer_destroy_listener);
Benjamin Franzke06286262011-05-06 19:12:33 +0200221
Kristian Høgsberg24596942011-10-24 17:51:02 -0400222 output->scanout_buffer = NULL;
Ander Conselvan de Oliveira9a38a0a2011-10-26 16:48:45 +0300223
224 if (!output->pending_scanout_buffer)
225 wlsc_compositor_schedule_repaint(output->compositor);
Benjamin Franzke06286262011-05-06 19:12:33 +0200226}
227
Ander Conselvan de Oliveiraf1621d22011-10-18 17:02:07 +0300228static void
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400229output_handle_pending_scanout_buffer_destroy(struct wl_listener *listener,
230 struct wl_resource *resource,
231 uint32_t time)
Ander Conselvan de Oliveiraf1621d22011-10-18 17:02:07 +0300232{
233 struct wlsc_output *output =
234 container_of(listener, struct wlsc_output,
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400235 pending_scanout_buffer_destroy_listener);
Ander Conselvan de Oliveiraf1621d22011-10-18 17:02:07 +0300236
Kristian Høgsberg24596942011-10-24 17:51:02 -0400237 output->pending_scanout_buffer = NULL;
Ander Conselvan de Oliveira9a38a0a2011-10-26 16:48:45 +0300238
239 wlsc_compositor_schedule_repaint(output->compositor);
Ander Conselvan de Oliveiraf1621d22011-10-18 17:02:07 +0300240}
241
242
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400243WL_EXPORT struct wlsc_surface *
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400244wlsc_surface_create(struct wlsc_compositor *compositor,
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400245 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500246{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400247 struct wlsc_surface *surface;
248
249 surface = malloc(sizeof *surface);
250 if (surface == NULL)
251 return NULL;
252
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500253 wl_list_init(&surface->link);
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100254 wl_list_init(&surface->buffer_link);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500255 surface->map_type = WLSC_SURFACE_MAP_UNMAPPED;
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500256
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500257 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400258 glBindTexture(GL_TEXTURE_2D, surface->texture);
259 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
260 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400261
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400262 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400263
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500264 surface->compositor = compositor;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400265 surface->visual = WLSC_NONE_VISUAL;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200266 surface->image = EGL_NO_IMAGE_KHR;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200267 surface->saved_texture = 0;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400268 surface->x = x;
269 surface->y = y;
270 surface->width = width;
271 surface->height = height;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400272
Kristian Høgsbergcbd06f92011-10-12 16:29:31 -0400273 surface->fullscreen_output = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200274 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400275 surface->output = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200276
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400277 pixman_region32_init(&surface->damage);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400278 pixman_region32_init(&surface->opaque);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400279
Benjamin Franzke06286262011-05-06 19:12:33 +0200280 surface->buffer_destroy_listener.func = surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200281
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400282 surface->transform = NULL;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500283
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400284 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500285}
286
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400287WL_EXPORT void
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500288wlsc_surface_damage_rectangle(struct wlsc_surface *surface,
289 int32_t x, int32_t y,
290 int32_t width, int32_t height)
291{
292 struct wlsc_compositor *compositor = surface->compositor;
293
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400294 pixman_region32_union_rect(&surface->damage,
295 &surface->damage,
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500296 surface->x + x, surface->y + y,
297 width, height);
298 wlsc_compositor_schedule_repaint(compositor);
299}
300
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400301WL_EXPORT void
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500302wlsc_surface_damage(struct wlsc_surface *surface)
303{
304 wlsc_surface_damage_rectangle(surface, 0, 0,
305 surface->width, surface->height);
306}
307
Kristian Høgsbergb8a98332011-06-23 21:00:04 -0400308WL_EXPORT void
309wlsc_surface_damage_below(struct wlsc_surface *surface)
310{
311 struct wlsc_surface *below;
312
313 if (surface->link.next == &surface->compositor->surface_list)
314 return;
315
316 below = container_of(surface->link.next, struct wlsc_surface, link);
317
318 pixman_region32_union_rect(&below->damage,
319 &below->damage,
320 surface->x, surface->y,
321 surface->width, surface->height);
322 wlsc_compositor_schedule_repaint(surface->compositor);
323}
324
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400325WL_EXPORT void
326wlsc_surface_configure(struct wlsc_surface *surface,
327 int x, int y, int width, int height)
328{
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400329 wlsc_surface_damage_below(surface);
330
331 surface->x = x;
332 surface->y = y;
333 surface->width = width;
334 surface->height = height;
335
336 wlsc_surface_assign_output(surface);
337 wlsc_surface_damage(surface);
338
339 pixman_region32_fini(&surface->opaque);
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400340 if (surface->visual == WLSC_RGB_VISUAL)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400341 pixman_region32_init_rect(&surface->opaque,
342 surface->x, surface->y,
343 surface->width, surface->height);
344 else
345 pixman_region32_init(&surface->opaque);
346}
347
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400348WL_EXPORT uint32_t
349wlsc_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500350{
351 struct timeval tv;
352
353 gettimeofday(&tv, NULL);
354
355 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
356}
357
Kristian Høgsberg54879822008-11-23 17:07:32 -0500358static void
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400359wlsc_compositor_repick(struct wlsc_compositor *compositor)
360{
361 struct wlsc_input_device *device;
362 struct wlsc_surface *surface;
363 int32_t sx, sy;
364 uint32_t time;
365
366 time = wlsc_compositor_get_time();
367 wl_list_for_each(device, &compositor->input_device_list, link) {
Kristian Høgsberg32ff1f52011-10-12 00:36:54 -0400368 if (device->input_device.grab)
369 continue;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400370 surface = pick_surface(&device->input_device, &sx, &sy);
371 wl_input_device_set_pointer_focus(&device->input_device,
372 &surface->surface,
373 time,
374 device->input_device.x,
375 device->input_device.y,
376 sx, sy);
377 }
378}
379
380static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400381destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500382{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400383 struct wlsc_surface *surface =
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500384 container_of(resource, struct wlsc_surface, surface.resource);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400385 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400386
Kristian Høgsbergb8a98332011-06-23 21:00:04 -0400387 wlsc_surface_damage_below(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500388
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400389 wl_list_remove(&surface->link);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400390 wlsc_compositor_repick(compositor);
391
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200392 if (surface->saved_texture == 0)
393 glDeleteTextures(1, &surface->texture);
394 else
395 glDeleteTextures(1, &surface->saved_texture);
396
Benjamin Franzke06286262011-05-06 19:12:33 +0200397 if (surface->buffer)
398 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400399
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200400 if (surface->image != EGL_NO_IMAGE_KHR)
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400401 compositor->destroy_image(compositor->display,
402 surface->image);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200403
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100404 wl_list_remove(&surface->buffer_link);
405
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400406 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500407}
408
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100409static void
410wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
411{
412 struct wlsc_surface *es = (struct wlsc_surface *) surface;
413 struct wlsc_compositor *ec = es->compositor;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100414 struct wl_list *surfaces_attached_to;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100415
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100416 if (es->saved_texture != 0)
417 es->texture = es->saved_texture;
418
419 glBindTexture(GL_TEXTURE_2D, es->texture);
420
421 if (wl_buffer_is_shm(buffer)) {
422 /* Unbind any EGLImage texture that may be bound, so we don't
423 * overwrite it.*/
424 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
425 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200426 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100427 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200428 es->pitch, buffer->height, 0,
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100429 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
430 wl_shm_buffer_get_data(buffer));
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400431
432 switch (wl_shm_buffer_get_format(buffer)) {
433 case WL_SHM_FORMAT_ARGB32:
434 es->visual = WLSC_ARGB_VISUAL;
435 break;
436 case WL_SHM_FORMAT_PREMULTIPLIED_ARGB32:
437 es->visual = WLSC_PREMUL_ARGB_VISUAL;
438 break;
439 case WL_SHM_FORMAT_XRGB32:
440 es->visual = WLSC_RGB_VISUAL;
441 break;
442 }
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100443
444 surfaces_attached_to = buffer->user_data;
445
Kristian Høgsbergfac11d22011-05-02 13:35:17 -0400446 wl_list_remove(&es->buffer_link);
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100447 wl_list_insert(surfaces_attached_to, &es->buffer_link);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100448 } else {
Benjamin Franzkefb4b5a22011-06-21 10:44:37 +0200449 if (es->image != EGL_NO_IMAGE_KHR)
450 ec->destroy_image(ec->display, es->image);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400451 es->image = ec->create_image(ec->display, NULL,
452 EGL_WAYLAND_BUFFER_WL,
453 buffer, NULL);
454
455 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400456
457 /* FIXME: we need to get the visual from the wl_buffer */
458 es->visual = WLSC_PREMUL_ARGB_VISUAL;
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200459 es->pitch = es->width;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100460 }
461}
462
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200463static void
464wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
465{
466 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400467 struct wlsc_compositor *ec = es->compositor;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200468
Ander Conselvan de Oliveira0de0aaf2011-10-27 17:09:17 +0300469 es->pitch = sprite->width;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200470 es->image = sprite->image;
471 if (sprite->image != EGL_NO_IMAGE_KHR) {
472 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400473 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200474 } else {
475 if (es->saved_texture == 0)
476 es->saved_texture = es->texture;
477 es->texture = sprite->texture;
478 }
479
480 es->visual = sprite->visual;
Benjamin Franzke06286262011-05-06 19:12:33 +0200481
482 if (es->buffer)
483 es->buffer = NULL;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200484}
485
486enum sprite_usage {
487 SPRITE_USE_CURSOR = (1 << 0),
488};
489
490static struct wlsc_sprite *
491create_sprite_from_png(struct wlsc_compositor *ec,
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400492 const char *filename, uint32_t usage)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500493{
494 uint32_t *pixels;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200495 struct wlsc_sprite *sprite;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400496 int32_t width, height;
Ander Conselvan de Oliveira0de0aaf2011-10-27 17:09:17 +0300497 int32_t egl_img_width, egl_img_height;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400498 uint32_t stride;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500499
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400500 pixels = wlsc_load_image(filename, &width, &height, &stride);
501 if (pixels == NULL)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200502 return NULL;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500503
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200504 sprite = malloc(sizeof *sprite);
505 if (sprite == NULL) {
506 free(pixels);
507 return NULL;
508 }
509
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400510 sprite->visual = WLSC_PREMUL_ARGB_VISUAL;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200511 sprite->width = width;
512 sprite->height = height;
513 sprite->image = EGL_NO_IMAGE_KHR;
514
Ander Conselvan de Oliveira0de0aaf2011-10-27 17:09:17 +0300515 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL) {
516 egl_img_width = width;
517 egl_img_height = height;
518
519 sprite->image = ec->create_cursor_image(ec, &egl_img_width,
520 &egl_img_height);
521 }
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200522
523 glGenTextures(1, &sprite->texture);
524 glBindTexture(GL_TEXTURE_2D, sprite->texture);
525 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
526 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
527 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
528 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
529
530 if (sprite->image != EGL_NO_IMAGE_KHR) {
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400531 ec->image_target_texture_2d(GL_TEXTURE_2D, sprite->image);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200532 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
533 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
Ander Conselvan de Oliveira0de0aaf2011-10-27 17:09:17 +0300534
535 sprite->width = egl_img_width;
536 sprite->height = egl_img_height;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200537 } else {
538 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
539 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
540 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500541
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500542 free(pixels);
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500543
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200544 return sprite;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500545}
546
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400547static const struct {
548 const char *filename;
549 int hotspot_x, hotspot_y;
550} pointer_images[] = {
Kristian Høgsberg4219a402010-08-16 16:43:03 -0400551 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
552 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
553 { DATADIR "/wayland/bottom_side.png", 16, 20 },
554 { DATADIR "/wayland/grabbing.png", 20, 17 },
555 { DATADIR "/wayland/left_ptr.png", 10, 5 },
556 { DATADIR "/wayland/left_side.png", 10, 20 },
557 { DATADIR "/wayland/right_side.png", 30, 19 },
558 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
559 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
560 { DATADIR "/wayland/top_side.png", 18, 8 },
561 { DATADIR "/wayland/xterm.png", 15, 15 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400562};
563
564static void
565create_pointer_images(struct wlsc_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500566{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400567 int i, count;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400568
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400569 count = ARRAY_LENGTH(pointer_images);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200570 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400571 for (i = 0; i < count; i++) {
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200572 ec->pointer_sprites[i] =
573 create_sprite_from_png(ec,
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500574 pointer_images[i].filename,
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200575 SPRITE_USE_CURSOR);
Rob Bradford1d724472011-10-25 13:43:44 +0100576 if (!ec->pointer_sprites[i]) {
577 fprintf(stderr, "Error loading pointer image: %s\n",
578 pointer_images[i].filename);
579 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400580 }
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400581}
582
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400583static int
584texture_region(struct wlsc_surface *es, pixman_region32_t *region)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500585{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500586 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500587 GLfloat *v, inv_width, inv_height;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500588 pixman_box32_t *rectangles;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400589 unsigned int *p;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500590 int i, n;
591
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400592 rectangles = pixman_region32_rectangles(region, &n);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500593 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
594 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200595 inv_width = 1.0 / es->pitch;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500596 inv_height = 1.0 / es->height;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400597
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500598 for (i = 0; i < n; i++, v += 16, p += 6) {
599 v[ 0] = rectangles[i].x1;
600 v[ 1] = rectangles[i].y1;
601 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
602 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500603
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500604 v[ 4] = rectangles[i].x1;
605 v[ 5] = rectangles[i].y2;
606 v[ 6] = v[ 2];
607 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500608
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500609 v[ 8] = rectangles[i].x2;
610 v[ 9] = rectangles[i].y1;
611 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
612 v[11] = v[ 3];
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500613
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500614 v[12] = rectangles[i].x2;
615 v[13] = rectangles[i].y2;
616 v[14] = v[10];
617 v[15] = v[ 7];
618
619 p[0] = i * 4 + 0;
620 p[1] = i * 4 + 1;
621 p[2] = i * 4 + 2;
622 p[3] = i * 4 + 2;
623 p[4] = i * 4 + 1;
624 p[5] = i * 4 + 3;
625 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500626
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400627 return n;
628}
629
630static void
631transform_vertex(struct wlsc_surface *surface,
632 GLfloat x, GLfloat y, GLfloat u, GLfloat v, GLfloat *r)
633{
634 struct wlsc_vector t;
635
636 t.f[0] = x;
637 t.f[1] = y;
638 t.f[2] = 0.0;
639 t.f[3] = 1.0;
640
Kristian Høgsberg0bc0e242011-05-02 14:35:40 -0400641 wlsc_matrix_transform(&surface->transform->matrix, &t);
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400642
643 r[ 0] = t.f[0];
644 r[ 1] = t.f[1];
645 r[ 2] = u;
646 r[ 3] = v;
647}
648
649static int
650texture_transformed_surface(struct wlsc_surface *es)
651{
652 struct wlsc_compositor *ec = es->compositor;
653 GLfloat *v;
654 unsigned int *p;
655
656 v = wl_array_add(&ec->vertices, 16 * sizeof *v);
657 p = wl_array_add(&ec->indices, 6 * sizeof *p);
658
659 transform_vertex(es, es->x, es->y, 0.0, 0.0, &v[0]);
660 transform_vertex(es, es->x, es->y + es->height, 0.0, 1.0, &v[4]);
661 transform_vertex(es, es->x + es->width, es->y, 1.0, 0.0, &v[8]);
662 transform_vertex(es, es->x + es->width, es->y + es->height,
663 1.0, 1.0, &v[12]);
664
665 p[0] = 0;
666 p[1] = 1;
667 p[2] = 2;
668 p[3] = 2;
669 p[4] = 1;
670 p[5] = 3;
671
672 return 1;
673}
674
675static void
676wlsc_surface_draw(struct wlsc_surface *es,
677 struct wlsc_output *output, pixman_region32_t *clip)
678{
679 struct wlsc_compositor *ec = es->compositor;
680 GLfloat *v;
681 pixman_region32_t repaint;
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400682 GLint filter;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400683 int n;
684
685 pixman_region32_init_rect(&repaint,
686 es->x, es->y, es->width, es->height);
687 pixman_region32_intersect(&repaint, &repaint, clip);
688 if (!pixman_region32_not_empty(&repaint))
689 return;
690
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400691 switch (es->visual) {
692 case WLSC_ARGB_VISUAL:
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400693 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
694 glEnable(GL_BLEND);
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400695 break;
696 case WLSC_PREMUL_ARGB_VISUAL:
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400697 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
698 glEnable(GL_BLEND);
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400699 break;
700 case WLSC_RGB_VISUAL:
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400701 glDisable(GL_BLEND);
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400702 break;
703 default:
704 fprintf(stderr, "bogus visual\n");
705 break;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400706 }
707
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400708 if (es->transform == NULL) {
709 filter = GL_NEAREST;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400710 n = texture_region(es, &repaint);
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400711 } else {
712 filter = GL_LINEAR;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400713 n = texture_transformed_surface(es);
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400714 }
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400715
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500716 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400717 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
718 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
719
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500720 v = ec->vertices.data;
721 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
722 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400723 glEnableVertexAttribArray(0);
724 glEnableVertexAttribArray(1);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500725 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
726
727 ec->vertices.size = 0;
728 ec->indices.size = 0;
729 pixman_region32_fini(&repaint);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500730}
731
732static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400733wlsc_surface_raise(struct wlsc_surface *surface)
734{
735 struct wlsc_compositor *compositor = surface->compositor;
736
737 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400738 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400739 wlsc_compositor_repick(compositor);
Kristian Høgsbergcd0d10b2011-06-24 08:28:07 -0400740 wlsc_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400741}
742
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400743WL_EXPORT void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400744wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
745{
746 struct wlsc_output *output;
747
748 wl_list_for_each(output, &compositor->output_list, link)
749 wlsc_output_damage(output);
750}
751
Benjamin Franzke06286262011-05-06 19:12:33 +0200752static inline void
753wlsc_buffer_post_release(struct wl_buffer *buffer)
754{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400755 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200756 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200757
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400758 assert(buffer->resource.client != NULL);
759 wl_resource_post_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200760}
761
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400762WL_EXPORT void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400763wlsc_output_damage(struct wlsc_output *output)
764{
765 struct wlsc_compositor *compositor = output->compositor;
766
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400767 pixman_region32_union(&compositor->damage,
768 &compositor->damage, &output->region);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400769 wlsc_compositor_schedule_repaint(compositor);
770}
771
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400772static void
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400773fade_frame(struct wlsc_animation *animation,
774 struct wlsc_output *output, uint32_t msecs)
775{
776 struct wlsc_compositor *compositor =
777 container_of(animation,
778 struct wlsc_compositor, fade.animation);
779
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400780 wlsc_spring_update(&compositor->fade.spring, msecs);
781 if (wlsc_spring_done(&compositor->fade.spring)) {
782 if (compositor->fade.spring.current > 0.999) {
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400783 compositor->state = WLSC_COMPOSITOR_SLEEPING;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400784 compositor->shell->lock(compositor->shell);
785 }
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400786 compositor->fade.spring.current =
787 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400788 wl_list_remove(&animation->link);
789 wl_list_init(&animation->link);
790 }
791
792 wlsc_output_damage(output);
793}
794
795static void
796fade_output(struct wlsc_output *output,
797 GLfloat tint, pixman_region32_t *region)
798{
799 struct wlsc_compositor *compositor = output->compositor;
800 struct wlsc_surface surface;
801 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
802
803 surface.compositor = compositor;
804 surface.x = output->x;
805 surface.y = output->y;
Tiago Vignatti2cc8b872011-08-19 15:10:40 +0300806 surface.pitch = output->current->width;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400807 surface.width = output->current->width;
808 surface.height = output->current->height;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400809 surface.texture = GL_NONE;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400810 surface.transform = NULL;
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400811
812 if (tint <= 1.0)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400813 surface.visual = WLSC_PREMUL_ARGB_VISUAL;
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400814 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400815 surface.visual = WLSC_RGB_VISUAL;
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400816
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400817 glUseProgram(compositor->solid_shader.program);
818 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
819 1, GL_FALSE, output->matrix.d);
820 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
821 wlsc_surface_draw(&surface, output, region);
822}
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200823
824static void
825wlsc_output_set_cursor(struct wlsc_output *output,
826 struct wl_input_device *dev, int force_sw)
827{
828 struct wlsc_compositor *ec = output->compositor;
829 struct wlsc_input_device *device = (struct wlsc_input_device *) dev;
830 pixman_region32_t cursor_region;
831 int use_hardware_cursor = 1, prior_was_hardware;
832
833 pixman_region32_init_rect(&cursor_region,
834 device->sprite->x, device->sprite->y,
835 device->sprite->width,
836 device->sprite->height);
837
838 pixman_region32_intersect(&cursor_region, &cursor_region, &output->region);
839
840 if (!pixman_region32_not_empty(&cursor_region)) {
Ander Conselvan de Oliveira02924bc2011-10-24 16:30:15 +0300841 output->set_hardware_cursor(output, NULL);
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200842 goto out;
843 }
844
845 prior_was_hardware = wl_list_empty(&device->sprite->link);
846 if (force_sw || output->set_hardware_cursor(output, device) < 0) {
Matt Roper11568a72011-08-29 15:59:37 -0700847 if (prior_was_hardware) {
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200848 wlsc_surface_damage(device->sprite);
Matt Roper11568a72011-08-29 15:59:37 -0700849 output->set_hardware_cursor(output, NULL);
850 }
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200851 use_hardware_cursor = 0;
852 } else if (!prior_was_hardware) {
853 wlsc_surface_damage_below(device->sprite);
854 }
855
856 /* Remove always to be on top. */
857 wl_list_remove(&device->sprite->link);
Kristian Høgsberg4ebf3a02011-08-29 16:47:09 -0400858 if (!use_hardware_cursor && ec->focus)
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200859 wl_list_insert(&ec->surface_list, &device->sprite->link);
860 else
861 wl_list_init(&device->sprite->link);
862
863out:
864 pixman_region32_fini(&cursor_region);
865}
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400866
Ander Conselvan de Oliveira3b199662011-10-18 17:02:06 +0300867static int
868setup_scanout_surface(struct wlsc_output *output, struct wlsc_surface *es)
869{
870 if (es->visual != WLSC_RGB_VISUAL ||
871 output->prepare_scanout_surface(output, es) != 0)
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400872 return -1;
Ander Conselvan de Oliveira3b199662011-10-18 17:02:06 +0300873
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400874 /* assert output->pending_scanout_buffer == NULL */
875 output->pending_scanout_buffer = es->buffer;
876 output->pending_scanout_buffer->busy_count++;
Ander Conselvan de Oliveira3b199662011-10-18 17:02:06 +0300877
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400878 wl_list_insert(output->pending_scanout_buffer->resource.destroy_listener_list.prev,
879 &output->pending_scanout_buffer_destroy_listener.link);
Ander Conselvan de Oliveiraf1621d22011-10-18 17:02:07 +0300880
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400881 return 0;
Ander Conselvan de Oliveira3b199662011-10-18 17:02:06 +0300882}
883
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400884static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400885wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400886{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500887 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500888 struct wlsc_surface *es;
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400889 pixman_region32_t opaque, new_damage, total_damage, repaint;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500890
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100891 output->prepare_render(output);
892
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400893 glViewport(0, 0, output->current->width, output->current->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500894
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -0400895 glUseProgram(ec->texture_shader.program);
896 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
897 1, GL_FALSE, output->matrix.d);
898 glUniform1i(ec->texture_shader.tex_uniform, 0);
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500899
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200900 wlsc_output_set_cursor(output, ec->input_device,
Kristian Høgsberg4ebf3a02011-08-29 16:47:09 -0400901 ec->fade.spring.current >= 0.001);
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200902
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500903 pixman_region32_init(&new_damage);
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400904 pixman_region32_copy(&new_damage, &ec->damage);
905 pixman_region32_init(&opaque);
906
Kristian Høgsberg8b72f602011-06-23 20:46:34 -0400907 wl_list_for_each(es, &ec->surface_list, link) {
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400908 pixman_region32_subtract(&es->damage, &es->damage, &opaque);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -0400909 pixman_region32_union(&new_damage, &new_damage, &es->damage);
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400910 pixman_region32_union(&opaque, &opaque, &es->opaque);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -0400911 }
912
913 pixman_region32_subtract(&ec->damage, &ec->damage, &output->region);
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400914
Kristian Høgsberg8b72f602011-06-23 20:46:34 -0400915 pixman_region32_init(&total_damage);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500916 pixman_region32_union(&total_damage, &new_damage,
Kristian Høgsberg8b72f602011-06-23 20:46:34 -0400917 &output->previous_damage);
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400918 pixman_region32_intersect(&output->previous_damage,
919 &new_damage, &output->region);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500920
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400921 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -0400922 pixman_region32_fini(&new_damage);
923
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500924 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200925
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400926 if (setup_scanout_surface(output, es) == 0) {
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400927 /* We're drawing nothing now,
928 * draw the damaged regions later. */
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400929 pixman_region32_union(&ec->damage, &ec->damage, &total_damage);
Benjamin Franzked72037c2011-06-21 16:33:27 +0200930
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400931 return;
Kristian Høgsberg4d07a1c2011-05-06 14:03:12 -0400932 }
933
934 if (es->fullscreen_output == output) {
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400935 if (es->width < output->current->width ||
936 es->height < output->current->height)
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200937 glClear(GL_COLOR_BUFFER_BIT);
938 wlsc_surface_draw(es, output, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500939 } else {
Kristian Høgsbergaee7f842011-06-23 21:25:20 -0400940 wl_list_for_each(es, &ec->surface_list, link) {
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400941 pixman_region32_copy(&es->damage, &total_damage);
942 pixman_region32_subtract(&total_damage, &total_damage, &es->opaque);
Kristian Høgsbergaee7f842011-06-23 21:25:20 -0400943 }
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500944
Kristian Høgsbergaee7f842011-06-23 21:25:20 -0400945 wl_list_for_each_reverse(es, &ec->surface_list, link) {
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400946 pixman_region32_init(&repaint);
947 pixman_region32_intersect(&repaint, &output->region,
948 &es->damage);
949 wlsc_surface_draw(es, output, &repaint);
950 pixman_region32_subtract(&es->damage,
951 &es->damage, &output->region);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500952 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500953 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400954
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400955 if (ec->fade.spring.current > 0.001)
956 fade_output(output, ec->fade.spring.current, &total_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500957}
958
Kristian Høgsberg33418202011-08-16 23:01:28 -0400959struct wlsc_frame_callback {
960 struct wl_resource resource;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400961 struct wl_list link;
962};
963
Kristian Høgsbergef044142011-06-21 15:02:12 -0400964static void
965repaint(void *data, int msecs)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500966{
Kristian Høgsbergef044142011-06-21 15:02:12 -0400967 struct wlsc_output *output = data;
968 struct wlsc_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -0400969 struct wlsc_animation *animation, *next;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400970 struct wlsc_frame_callback *cb, *cnext;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500971
Kristian Høgsbergef044142011-06-21 15:02:12 -0400972 wlsc_output_repaint(output);
973 output->repaint_needed = 0;
974 output->repaint_scheduled = 1;
975 output->present(output);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100976
Kristian Høgsberg33418202011-08-16 23:01:28 -0400977 wl_list_for_each_safe(cb, cnext, &output->frame_callback_list, link) {
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400978 wl_resource_post_event(&cb->resource, WL_CALLBACK_DONE, msecs);
979 wl_resource_destroy(&cb->resource, 0);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500980 }
981
Kristian Høgsbergef044142011-06-21 15:02:12 -0400982 wl_list_for_each_safe(animation, next,
983 &compositor->animation_list, link)
984 animation->frame(animation, output, msecs);
985}
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400986
Kristian Høgsbergef044142011-06-21 15:02:12 -0400987static void
988idle_repaint(void *data)
989{
990 repaint(data, wlsc_compositor_get_time());
991}
992
993WL_EXPORT void
994wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
995{
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400996 if (output->scanout_buffer) {
997 wlsc_buffer_post_release(output->scanout_buffer);
998 wl_list_remove(&output->scanout_buffer_destroy_listener.link);
999 output->scanout_buffer = NULL;
1000 }
1001
1002 if (output->pending_scanout_buffer) {
1003 output->scanout_buffer = output->pending_scanout_buffer;
1004 wl_list_remove(&output->pending_scanout_buffer_destroy_listener.link);
1005 wl_list_insert(output->scanout_buffer->resource.destroy_listener_list.prev,
1006 &output->scanout_buffer_destroy_listener.link);
1007 output->pending_scanout_buffer = NULL;
1008 }
1009
Kristian Høgsbergef044142011-06-21 15:02:12 -04001010 if (output->repaint_needed)
1011 repaint(output, msecs);
Ander Conselvan de Oliveira22e22a52011-10-24 16:30:14 +03001012 else
1013 output->repaint_scheduled = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001014}
1015
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001016WL_EXPORT void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001017wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001018{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001019 struct wlsc_output *output;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001020 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001021
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001022 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
1023 return;
1024
Kristian Høgsbergef044142011-06-21 15:02:12 -04001025 loop = wl_display_get_event_loop(compositor->wl_display);
1026 wl_list_for_each(output, &compositor->output_list, link) {
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001027 output->repaint_needed = 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001028 if (output->repaint_scheduled)
1029 continue;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001030
Kristian Høgsbergef044142011-06-21 15:02:12 -04001031 wl_event_loop_add_idle(loop, idle_repaint, output);
1032 output->repaint_scheduled = 1;
1033 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001034}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001035
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001036WL_EXPORT void
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001037wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
1038{
1039 int done;
1040
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001041 done = wlsc_spring_done(&compositor->fade.spring);
1042 compositor->fade.spring.target = tint;
1043 if (wlsc_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001044 return;
1045
1046 if (done)
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001047 compositor->fade.spring.timestamp =
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001048 wlsc_compositor_get_time();
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001049
1050 wlsc_compositor_damage_all(compositor);
1051 if (wl_list_empty(&compositor->fade.animation.link))
1052 wl_list_insert(compositor->animation_list.prev,
1053 &compositor->fade.animation.link);
1054}
1055
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001056static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001057surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001058{
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001059 wl_resource_destroy(resource, wlsc_compositor_get_time());
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001060}
1061
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001062WL_EXPORT void
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001063wlsc_surface_assign_output(struct wlsc_surface *es)
1064{
1065 struct wlsc_compositor *ec = es->compositor;
1066 struct wlsc_output *output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001067 pixman_region32_t region;
1068 uint32_t max, area;
1069 pixman_box32_t *e;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001070
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001071 max = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001072 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001073 pixman_region32_init_rect(&region,
1074 es->x, es->y, es->width, es->height);
1075 pixman_region32_intersect(&region, &region, &output->region);
1076
1077 e = pixman_region32_extents(&region);
1078 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1079
1080 if (area >= max) {
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001081 es->output = output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001082 max = area;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001083 }
1084 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001085}
1086
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001087static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001088surface_attach(struct wl_client *client,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001089 struct wl_resource *resource,
1090 struct wl_resource *buffer_resource, int32_t x, int32_t y)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001091{
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001092 struct wlsc_surface *es = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001093 struct wl_buffer *buffer = buffer_resource->data;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001094 int repick = 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001095
Kristian Høgsberg24596942011-10-24 17:51:02 -04001096 if (es->buffer) {
1097 wlsc_buffer_post_release(es->buffer);
1098 wl_list_remove(&es->buffer_destroy_listener.link);
1099 }
Benjamin Franzke06286262011-05-06 19:12:33 +02001100
Kristian Høgsberg24596942011-10-24 17:51:02 -04001101 buffer->busy_count++;
Benjamin Franzke06286262011-05-06 19:12:33 +02001102 es->buffer = buffer;
Benjamin Franzke06286262011-05-06 19:12:33 +02001103 wl_list_insert(es->buffer->resource.destroy_listener_list.prev,
1104 &es->buffer_destroy_listener.link);
1105
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001106 if (es->visual == WLSC_NONE_VISUAL) {
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001107 wl_list_insert(&es->compositor->surface_list, &es->link);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001108 repick = 1;
1109 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001110
Kristian Høgsberg9ffb6b92011-07-21 11:33:47 -07001111 if (x != 0 || y != 0 ||
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001112 es->width != buffer->width || es->height != buffer->height) {
Kristian Høgsberg9ffb6b92011-07-21 11:33:47 -07001113 wlsc_surface_configure(es, es->x + x, es->y + y,
1114 buffer->width, buffer->height);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001115 repick = 1;
1116 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001117
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001118 wlsc_buffer_attach(buffer, &es->surface);
Kristian Høgsberg2e02d242011-05-13 14:06:29 -04001119
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001120 es->compositor->shell->attach(es->compositor->shell, es);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001121
1122 if (repick)
1123 wlsc_compositor_repick(es->compositor);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001124}
1125
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001126static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001127surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001128 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001129 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001130{
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001131 struct wlsc_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001132
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001133 wlsc_surface_damage_rectangle(es, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001134}
1135
Kristian Høgsberg33418202011-08-16 23:01:28 -04001136static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001137destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001138{
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001139 struct wlsc_frame_callback *cb = resource->data;
1140
1141 wl_list_remove(&cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001142 free(resource);
1143}
1144
1145static void
1146surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001147 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001148{
1149 struct wlsc_frame_callback *cb;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001150 struct wlsc_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001151
1152 cb = malloc(sizeof *cb);
1153 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001154 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001155 return;
1156 }
1157
1158 cb->resource.object.interface = &wl_callback_interface;
1159 cb->resource.object.id = callback;
1160 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001161 cb->resource.client = client;
1162 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001163
1164 wl_client_add_resource(client, &cb->resource);
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -04001165
1166 if (es->output) {
1167 wl_list_insert(es->output->frame_callback_list.prev,
1168 &cb->link);
1169 } else {
1170 wl_list_init(&cb->link);
1171 wl_resource_destroy(&cb->resource, 0);
1172 }
Kristian Høgsberg33418202011-08-16 23:01:28 -04001173}
1174
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001175const static struct wl_surface_interface surface_interface = {
1176 surface_destroy,
1177 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001178 surface_damage,
1179 surface_frame
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001180};
1181
1182static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001183wlsc_input_device_attach(struct wlsc_input_device *device,
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001184 int x, int y, int width, int height)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001185{
Kristian Høgsbergb8a98332011-06-23 21:00:04 -04001186 wlsc_surface_damage_below(device->sprite);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001187
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001188 device->hotspot_x = x;
1189 device->hotspot_y = y;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001190
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001191 device->sprite->x = device->input_device.x - device->hotspot_x;
1192 device->sprite->y = device->input_device.y - device->hotspot_y;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001193 device->sprite->width = width;
1194 device->sprite->height = height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001195
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001196 wlsc_surface_damage(device->sprite);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001197}
1198
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001199static void
1200wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
1201 struct wl_buffer *buffer, int x, int y)
1202{
1203 wlsc_buffer_attach(buffer, &device->sprite->surface);
1204 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
1205}
1206
1207static void
1208wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
1209 struct wlsc_sprite *sprite, int x, int y)
1210{
Rob Bradfordd3547112011-10-25 13:38:31 +01001211 if (!sprite)
1212 return;
1213
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001214 wlsc_sprite_attach(sprite, &device->sprite->surface);
1215 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
1216}
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001217
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001218WL_EXPORT void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001219wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
1220 enum wlsc_pointer_type type)
1221{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001222 struct wlsc_compositor *compositor =
1223 (struct wlsc_compositor *) device->input_device.compositor;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001224
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001225 wlsc_input_device_attach_sprite(device,
1226 compositor->pointer_sprites[type],
1227 pointer_images[type].hotspot_x,
1228 pointer_images[type].hotspot_y);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001229}
1230
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001231static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001232compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001233 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001234{
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001235 struct wlsc_compositor *ec = resource->data;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001236 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001237
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -05001238 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001239 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001240 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001241 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001242 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001243
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001244 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001245
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001246 surface->surface.resource.object.id = id;
1247 surface->surface.resource.object.interface = &wl_surface_interface;
1248 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001249 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001250 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001251
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001252 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001253}
1254
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001255const static struct wl_compositor_interface compositor_interface = {
1256 compositor_create_surface,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001257};
1258
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001259static void
1260wlsc_surface_transform(struct wlsc_surface *surface,
1261 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1262{
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001263 *sx = x - surface->x;
1264 *sy = y - surface->y;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001265}
1266
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001267WL_EXPORT struct wlsc_surface *
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001268pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001269{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001270 struct wlsc_compositor *ec =
1271 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001272 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001273
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001274 wl_list_for_each(es, &ec->surface_list, link) {
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001275 if (es->surface.resource.client == NULL)
Kristian Høgsberg8244b442011-06-23 15:44:14 -04001276 continue;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001277 wlsc_surface_transform(es, device->x, device->y, sx, sy);
1278 if (0 <= *sx && *sx < es->width &&
1279 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001280 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001281 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001282
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001283 return NULL;
1284}
1285
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001286
1287static void
Kristian Høgsberg9c4eecb2011-09-06 18:13:14 -04001288implicit_grab_motion(struct wl_grab *grab,
1289 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001290{
1291 struct wlsc_input_device *device =
1292 (struct wlsc_input_device *) grab->input_device;
1293 struct wlsc_surface *es =
1294 (struct wlsc_surface *) device->input_device.pointer_focus;
1295 int32_t sx, sy;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001296 struct wl_resource *resource;
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001297
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001298 resource = grab->input_device->pointer_focus_resource;
1299 if (resource) {
1300 wlsc_surface_transform(es, x, y, &sx, &sy);
1301 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
1302 time, x, y, sx, sy);
1303 }
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001304}
1305
1306static void
Kristian Høgsberg9c4eecb2011-09-06 18:13:14 -04001307implicit_grab_button(struct wl_grab *grab,
1308 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001309{
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001310 struct wl_resource *resource;
1311
1312 resource = grab->input_device->pointer_focus_resource;
1313 if (resource)
1314 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
1315 time, button, state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001316}
1317
1318static void
Kristian Høgsberg9c4eecb2011-09-06 18:13:14 -04001319implicit_grab_end(struct wl_grab *grab, uint32_t time)
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001320{
1321}
1322
Kristian Høgsberg9c4eecb2011-09-06 18:13:14 -04001323static const struct wl_grab_interface implicit_grab_interface = {
1324 implicit_grab_motion,
1325 implicit_grab_button,
1326 implicit_grab_end
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001327};
1328
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001329WL_EXPORT void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001330wlsc_compositor_wake(struct wlsc_compositor *compositor)
1331{
1332 if (compositor->idle_inhibit)
1333 return;
1334
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001335 wlsc_compositor_fade(compositor, 0.0);
1336 compositor->state = WLSC_COMPOSITOR_ACTIVE;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001337
1338 wl_event_source_timer_update(compositor->idle_source,
1339 option_idle_time * 1000);
1340}
1341
1342static void
1343wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1344{
1345 wlsc_compositor_wake(compositor);
1346 compositor->idle_inhibit++;
1347}
1348
1349static void
1350wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1351{
1352 compositor->idle_inhibit--;
1353 wlsc_compositor_wake(compositor);
1354}
1355
1356static int
1357idle_handler(void *data)
1358{
1359 struct wlsc_compositor *compositor = data;
1360
1361 if (compositor->idle_inhibit)
1362 return 1;
1363
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001364 wlsc_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001365
1366 return 1;
1367}
1368
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001369WL_EXPORT void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001370notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001371{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001372 struct wlsc_surface *es;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001373 struct wlsc_compositor *ec =
1374 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001375 struct wlsc_output *output;
Kristian Høgsberg6d65d5f2010-12-07 13:30:18 -05001376 const struct wl_grab_interface *interface;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001377 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -05001378 int32_t sx, sy;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001379 int x_valid = 0, y_valid = 0;
1380 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001381
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001382 wlsc_compositor_wake(ec);
1383
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001384 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001385 if (output->x <= x && x <= output->x + output->current->width)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001386 x_valid = 1;
1387
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001388 if (output->y <= y && y <= output->y + output->current->height)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001389 y_valid = 1;
1390
1391 /* FIXME: calculate this only on output addition/deletion */
1392 if (output->x < min_x)
1393 min_x = output->x;
1394 if (output->y < min_y)
1395 min_y = output->y;
1396
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001397 if (output->x + output->current->width > max_x)
1398 max_x = output->x + output->current->width;
1399 if (output->y + output->current->height > max_y)
1400 max_y = output->y + output->current->height;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001401 }
1402
1403 if (!x_valid) {
1404 if (x < min_x)
1405 x = min_x;
1406 else if (x >= max_x)
1407 x = max_x;
1408 }
1409 if (!y_valid) {
1410 if (y < min_y)
1411 y = min_y;
1412 else if (y >= max_y)
1413 y = max_y;
1414 }
Ray Strode90e701d2008-12-18 23:05:43 -05001415
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -05001416 device->x = x;
1417 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001418
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001419 if (device->grab) {
1420 interface = device->grab->interface;
1421 interface->motion(device->grab, time, x, y);
1422 } else {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001423 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001424 wl_input_device_set_pointer_focus(device,
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001425 &es->surface,
Kristian Høgsberg26437072010-12-01 10:17:47 -05001426 time, x, y, sx, sy);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001427 if (device->pointer_focus_resource)
1428 wl_resource_post_event(device->pointer_focus_resource,
1429 WL_INPUT_DEVICE_MOTION,
1430 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001431 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001432
Kristian Høgsbergb8a98332011-06-23 21:00:04 -04001433 wlsc_surface_damage_below(wd->sprite);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001434
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001435 wd->sprite->x = device->x - wd->hotspot_x;
1436 wd->sprite->y = device->y - wd->hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001437
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001438 wlsc_surface_damage(wd->sprite);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001439}
1440
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001441WL_EXPORT void
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001442wlsc_surface_activate(struct wlsc_surface *surface,
1443 struct wlsc_input_device *device, uint32_t time)
1444{
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001445 struct wlsc_shell *shell = surface->compositor->shell;
1446
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001447 wlsc_surface_raise(surface);
1448 if (device->selection)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001449 shell->set_selection_focus(shell,
1450 device->selection,
1451 &surface->surface, time);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001452
1453 wl_input_device_set_keyboard_focus(&device->input_device,
1454 &surface->surface,
1455 time);
1456}
1457
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001458struct wlsc_binding {
1459 uint32_t key;
1460 uint32_t button;
1461 uint32_t modifier;
1462 wlsc_binding_handler_t handler;
1463 void *data;
1464 struct wl_list link;
1465};
1466
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001467WL_EXPORT void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001468notify_button(struct wl_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001469 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001470{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001471 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001472 struct wlsc_compositor *compositor =
1473 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001474 struct wlsc_binding *b;
1475 struct wlsc_surface *surface =
1476 (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsberg4eaa8302011-09-06 13:41:47 -04001477 int32_t sx, sy;
Kristian Høgsbergea081152010-12-07 08:59:51 -05001478
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001479 if (state)
1480 wlsc_compositor_idle_inhibit(compositor);
1481 else
1482 wlsc_compositor_idle_release(compositor);
1483
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -05001484 if (state && surface && device->grab == NULL) {
Kristian Høgsberg75840622011-09-06 13:48:16 -04001485 compositor->shell->activate(compositor->shell,
1486 surface, wd, time);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001487 wl_input_device_start_grab(device,
Kristian Høgsberg9c4eecb2011-09-06 18:13:14 -04001488 &device->implicit_grab,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001489 button, time);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001490 }
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -05001491
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001492 wl_list_for_each(b, &compositor->binding_list, link) {
1493 if (b->button == button &&
1494 b->modifier == wd->modifier_state && state) {
1495 b->handler(&wd->input_device,
1496 time, 0, button, state, b->data);
1497 break;
1498 }
Benjamin Franzke5a2218a2011-02-01 16:30:31 +01001499 }
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001500
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -05001501 if (device->grab)
1502 device->grab->interface->button(device->grab, time,
1503 button, state);
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -05001504
Kristian Høgsberg4eaa8302011-09-06 13:41:47 -04001505 if (!state && device->grab && device->grab_button == button) {
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001506 wl_input_device_end_grab(device, time);
Kristian Høgsberg4eaa8302011-09-06 13:41:47 -04001507 surface = pick_surface(device, &sx, &sy);
1508 wl_input_device_set_pointer_focus(device, &surface->surface,
1509 time, device->x, device->y,
1510 sx, sy);
1511 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001512}
1513
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001514static void
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001515terminate_binding(struct wl_input_device *device, uint32_t time,
1516 uint32_t key, uint32_t button, uint32_t state, void *data)
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001517{
1518 struct wlsc_compositor *compositor = data;
1519
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001520 if (state)
1521 wl_display_terminate(compositor->wl_display);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001522}
1523
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001524WL_EXPORT struct wlsc_binding *
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001525wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001526 uint32_t key, uint32_t button, uint32_t modifier,
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001527 wlsc_binding_handler_t handler, void *data)
1528{
1529 struct wlsc_binding *binding;
1530
1531 binding = malloc(sizeof *binding);
1532 if (binding == NULL)
1533 return NULL;
1534
1535 binding->key = key;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001536 binding->button = button;
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001537 binding->modifier = modifier;
1538 binding->handler = handler;
1539 binding->data = data;
1540 wl_list_insert(compositor->binding_list.prev, &binding->link);
1541
1542 return binding;
1543}
1544
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001545WL_EXPORT void
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001546wlsc_binding_destroy(struct wlsc_binding *binding)
1547{
1548 wl_list_remove(&binding->link);
1549 free(binding);
1550}
1551
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001552static void
1553update_modifier_state(struct wlsc_input_device *device,
1554 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001555{
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001556 uint32_t modifier;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001557
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001558 switch (key) {
1559 case KEY_LEFTCTRL:
1560 case KEY_RIGHTCTRL:
1561 modifier = MODIFIER_CTRL;
1562 break;
1563
1564 case KEY_LEFTALT:
1565 case KEY_RIGHTALT:
1566 modifier = MODIFIER_ALT;
1567 break;
1568
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -04001569 case KEY_LEFTMETA:
1570 case KEY_RIGHTMETA:
1571 modifier = MODIFIER_SUPER;
1572 break;
1573
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001574 default:
1575 modifier = 0;
1576 break;
1577 }
1578
1579 if (state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001580 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001581 else
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001582 device->modifier_state &= ~modifier;
1583}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001584
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001585WL_EXPORT void
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001586notify_key(struct wl_input_device *device,
1587 uint32_t time, uint32_t key, uint32_t state)
1588{
1589 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1590 struct wlsc_compositor *compositor =
1591 (struct wlsc_compositor *) device->compositor;
1592 uint32_t *k, *end;
1593 struct wlsc_binding *b;
1594
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001595 if (state)
1596 wlsc_compositor_idle_inhibit(compositor);
1597 else
1598 wlsc_compositor_idle_release(compositor);
1599
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001600 wl_list_for_each(b, &compositor->binding_list, link) {
1601 if (b->key == key &&
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001602 b->modifier == wd->modifier_state) {
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001603 b->handler(&wd->input_device,
1604 time, key, 0, state, b->data);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001605 break;
1606 }
1607 }
1608
1609 update_modifier_state(wd, key, state);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001610 end = device->keys.data + device->keys.size;
1611 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001612 if (*k == key)
1613 *k = *--end;
1614 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001615 device->keys.size = (void *) end - device->keys.data;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001616 if (state) {
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001617 k = wl_array_add(&device->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001618 *k = key;
1619 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001620
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001621 if (device->keyboard_focus_resource)
1622 wl_resource_post_event(device->keyboard_focus_resource,
1623 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001624}
1625
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001626WL_EXPORT void
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001627notify_pointer_focus(struct wl_input_device *device,
1628 uint32_t time, struct wlsc_output *output,
1629 int32_t x, int32_t y)
1630{
1631 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1632 struct wlsc_compositor *compositor =
1633 (struct wlsc_compositor *) device->compositor;
1634 struct wlsc_surface *es;
1635 int32_t sx, sy;
1636
1637 if (output) {
1638 device->x = x;
1639 device->y = y;
1640 es = pick_surface(device, &sx, &sy);
1641 wl_input_device_set_pointer_focus(device,
1642 &es->surface,
1643 time, x, y, sx, sy);
1644
1645 compositor->focus = 1;
1646
1647 wd->sprite->x = device->x - wd->hotspot_x;
1648 wd->sprite->y = device->y - wd->hotspot_y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001649 } else {
1650 wl_input_device_set_pointer_focus(device, NULL,
1651 time, 0, 0, 0, 0);
1652 compositor->focus = 0;
1653 }
1654
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001655 wlsc_surface_damage(wd->sprite);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001656}
1657
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001658WL_EXPORT void
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001659notify_keyboard_focus(struct wl_input_device *device,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001660 uint32_t time, struct wlsc_output *output,
1661 struct wl_array *keys)
1662{
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001663 struct wlsc_input_device *wd =
1664 (struct wlsc_input_device *) device;
1665 struct wlsc_compositor *compositor =
1666 (struct wlsc_compositor *) device->compositor;
1667 struct wlsc_surface *es;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001668 uint32_t *k, *end;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001669
1670 if (!wl_list_empty(&compositor->surface_list))
1671 es = container_of(compositor->surface_list.next,
1672 struct wlsc_surface, link);
1673 else
1674 es = NULL;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001675
1676 if (output) {
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001677 wl_array_copy(&wd->input_device.keys, keys);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001678 wd->modifier_state = 0;
1679 end = device->keys.data + device->keys.size;
1680 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001681 wlsc_compositor_idle_inhibit(compositor);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001682 update_modifier_state(wd, *k, 1);
1683 }
1684
Kristian Høgsbergf59da392011-09-06 13:44:56 -04001685 if (es && es->surface.resource.client)
Benjamin Franzkeb7c00a42011-06-23 23:30:30 +02001686 wl_input_device_set_keyboard_focus(&wd->input_device,
1687 &es->surface, time);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001688 } else {
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001689 end = device->keys.data + device->keys.size;
1690 for (k = device->keys.data; k < end; k++)
1691 wlsc_compositor_idle_release(compositor);
1692
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001693 wd->modifier_state = 0;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001694 wl_input_device_set_keyboard_focus(&wd->input_device,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001695 NULL, time);
1696 }
1697}
1698
1699
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001700static void
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001701input_device_attach(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001702 struct wl_resource *resource,
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001703 uint32_t time,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001704 struct wl_resource *buffer_resource, int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001705{
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001706 struct wlsc_input_device *device = resource->data;
Kristian Høgsberg57295eb2011-08-29 16:02:57 -04001707 struct wl_buffer *buffer;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001708
1709 if (time < device->input_device.pointer_focus_time)
1710 return;
1711 if (device->input_device.pointer_focus == NULL)
1712 return;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001713 if (device->input_device.pointer_focus->resource.client != client)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001714 return;
1715
Kristian Høgsberg57295eb2011-08-29 16:02:57 -04001716 if (buffer_resource) {
1717 buffer = buffer_resource->data;
1718 wlsc_input_device_attach_buffer(device, buffer, x, y);
1719 } else {
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001720 wlsc_input_device_set_pointer_image(device,
1721 WLSC_POINTER_LEFT_PTR);
1722 return;
1723 }
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001724}
1725
1726const static struct wl_input_device_interface input_device_interface = {
1727 input_device_attach,
1728};
1729
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001730static void unbind_input_device(struct wl_resource *resource)
1731{
1732 wl_list_remove(&resource->link);
1733 free(resource);
1734}
1735
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001736static void
1737bind_input_device(struct wl_client *client,
1738 void *data, uint32_t version, uint32_t id)
1739{
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04001740 struct wl_input_device *device = data;
1741 struct wl_resource *resource;
1742
1743 resource = wl_client_add_object(client, &wl_input_device_interface,
1744 &input_device_interface, id, data);
1745 wl_list_insert(&device->resource_list, &resource->link);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001746 resource->destroy = unbind_input_device;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001747}
1748
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001749WL_EXPORT void
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001750wlsc_input_device_init(struct wlsc_input_device *device,
1751 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001752{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001753 wl_input_device_init(&device->input_device, &ec->compositor);
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05001754
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001755 wl_display_add_global(ec->wl_display, &wl_input_device_interface,
1756 device, bind_input_device);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001757
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -05001758 device->sprite = wlsc_surface_create(ec,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001759 device->input_device.x,
1760 device->input_device.y, 32, 32);
Kristian Høgsberg8244b442011-06-23 15:44:14 -04001761 wl_list_insert(&ec->surface_list, &device->sprite->link);
1762
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001763 device->hotspot_x = 16;
1764 device->hotspot_y = 16;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001765 device->modifier_state = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001766
Kristian Høgsberg9c4eecb2011-09-06 18:13:14 -04001767 device->input_device.implicit_grab.interface = &implicit_grab_interface;
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001768
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001769 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001770
1771 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001772}
1773
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001774static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001775bind_output(struct wl_client *client,
1776 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001777{
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001778 struct wlsc_output *output = data;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001779 struct wlsc_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04001780 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001781
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04001782 resource = wl_client_add_object(client,
1783 &wl_output_interface, NULL, id, data);
1784
1785 wl_resource_post_event(resource,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001786 WL_OUTPUT_GEOMETRY,
1787 output->x,
1788 output->y,
1789 output->mm_width,
1790 output->mm_height,
1791 output->subpixel,
1792 output->make, output->model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001793
1794 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04001795 wl_resource_post_event(resource,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001796 WL_OUTPUT_MODE,
1797 mode->flags,
1798 mode->width,
1799 mode->height,
1800 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001801 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001802}
1803
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001804static const char vertex_shader[] =
1805 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001806 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001807 "attribute vec2 texcoord;\n"
1808 "varying vec2 v_texcoord;\n"
1809 "void main()\n"
1810 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001811 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001812 " v_texcoord = texcoord;\n"
1813 "}\n";
1814
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001815static const char texture_fragment_shader[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05001816 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001817 "varying vec2 v_texcoord;\n"
1818 "uniform sampler2D tex;\n"
1819 "void main()\n"
1820 "{\n"
1821 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1822 "}\n";
1823
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001824static const char solid_fragment_shader[] =
1825 "precision mediump float;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001826 "uniform vec4 color;\n"
1827 "void main()\n"
1828 "{\n"
1829 " gl_FragColor = color\n;"
1830 "}\n";
1831
Kristian Høgsberga9468212010-06-14 21:03:11 -04001832static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001833compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001834{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001835 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001836 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001837 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001838
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001839 s = glCreateShader(type);
1840 glShaderSource(s, 1, &source, NULL);
1841 glCompileShader(s);
1842 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001843 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001844 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1845 fprintf(stderr, "shader info: %s\n", msg);
1846 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001847 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001848
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001849 return s;
1850}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001851
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001852static int
1853wlsc_shader_init(struct wlsc_shader *shader,
1854 const char *vertex_source, const char *fragment_source)
1855{
1856 char msg[512];
1857 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001858
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001859 shader->vertex_shader =
1860 compile_shader(GL_VERTEX_SHADER, vertex_source);
1861 shader->fragment_shader =
1862 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1863
1864 shader->program = glCreateProgram();
1865 glAttachShader(shader->program, shader->vertex_shader);
1866 glAttachShader(shader->program, shader->fragment_shader);
1867 glBindAttribLocation(shader->program, 0, "position");
1868 glBindAttribLocation(shader->program, 1, "texcoord");
1869
1870 glLinkProgram(shader->program);
1871 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001872 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001873 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001874 fprintf(stderr, "link info: %s\n", msg);
1875 return -1;
1876 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001877
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001878 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1879 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001880
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001881 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001882}
1883
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001884static int
1885init_solid_shader(struct wlsc_shader *shader,
1886 GLuint vertex_shader, const char *fragment_source)
1887{
1888 GLint status;
1889 char msg[512];
1890
1891 shader->vertex_shader = vertex_shader;
1892 shader->fragment_shader =
1893 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1894
1895 shader->program = glCreateProgram();
1896 glAttachShader(shader->program, shader->vertex_shader);
1897 glAttachShader(shader->program, shader->fragment_shader);
1898 glBindAttribLocation(shader->program, 0, "position");
1899 glBindAttribLocation(shader->program, 1, "texcoord");
1900
1901 glLinkProgram(shader->program);
1902 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1903 if (!status) {
1904 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1905 fprintf(stderr, "link info: %s\n", msg);
1906 return -1;
1907 }
1908
1909 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1910 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1911
1912 return 0;
1913}
1914
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001915WL_EXPORT void
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001916wlsc_output_destroy(struct wlsc_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001917{
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04001918 pixman_region32_fini(&output->region);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04001919 pixman_region32_fini(&output->previous_damage);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001920}
1921
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001922WL_EXPORT void
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001923wlsc_output_move(struct wlsc_output *output, int x, int y)
1924{
1925 struct wlsc_compositor *c = output->compositor;
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001926 int flip;
1927
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001928 output->x = x;
1929 output->y = y;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001930
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04001931 pixman_region32_init(&output->previous_damage);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04001932 pixman_region32_init_rect(&output->region, x, y,
1933 output->current->width,
1934 output->current->height);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001935
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001936 wlsc_matrix_init(&output->matrix);
1937 wlsc_matrix_translate(&output->matrix,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001938 -output->x - output->current->width / 2.0,
1939 -output->y - output->current->height / 2.0, 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001940
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001941 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001942 wlsc_matrix_scale(&output->matrix,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001943 2.0 / output->current->width,
1944 flip * 2.0 / output->current->height, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001945
Kristian Høgsberg20300ba2011-06-23 20:29:12 -04001946 pixman_region32_union(&c->damage, &c->damage, &output->region);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001947}
1948
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001949WL_EXPORT void
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001950wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1951 int x, int y, int width, int height, uint32_t flags)
1952{
1953 output->compositor = c;
1954 output->x = x;
1955 output->y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001956 output->mm_width = width;
1957 output->mm_height = height;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001958
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001959 output->flags = flags;
1960 wlsc_output_move(output, x, y);
1961
Benjamin Franzke06286262011-05-06 19:12:33 +02001962 output->scanout_buffer_destroy_listener.func =
1963 output_handle_scanout_buffer_destroy;
Kristian Høgsberg191454e2011-10-20 17:51:45 -04001964 output->pending_scanout_buffer_destroy_listener.func =
1965 output_handle_pending_scanout_buffer_destroy;
Ander Conselvan de Oliveiraf1621d22011-10-18 17:02:07 +03001966
Kristian Høgsberg33418202011-08-16 23:01:28 -04001967 wl_list_init(&output->frame_callback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02001968
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001969 output->resource.object.interface = &wl_output_interface;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001970 wl_display_add_global(c->wl_display,
1971 &wl_output_interface, output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001972}
1973
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001974static void
1975shm_buffer_created(struct wl_buffer *buffer)
1976{
1977 struct wl_list *surfaces_attached_to;
1978
1979 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1980 if (!surfaces_attached_to) {
1981 buffer->user_data = NULL;
1982 return;
1983 }
1984
1985 wl_list_init(surfaces_attached_to);
1986
1987 buffer->user_data = surfaces_attached_to;
1988}
1989
1990static void
1991shm_buffer_damaged(struct wl_buffer *buffer,
1992 int32_t x, int32_t y, int32_t width, int32_t height)
1993{
1994 struct wl_list *surfaces_attached_to = buffer->user_data;
1995 struct wlsc_surface *es;
Benjamin Franzkefab5ec12011-04-25 19:44:47 +02001996 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001997
1998 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1999 glBindTexture(GL_TEXTURE_2D, es->texture);
2000 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Benjamin Franzkefab5ec12011-04-25 19:44:47 +02002001 tex_width, buffer->height, 0,
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002002 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
2003 wl_shm_buffer_get_data(buffer));
2004 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
2005 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
2006 }
2007}
2008
2009static void
2010shm_buffer_destroyed(struct wl_buffer *buffer)
2011{
2012 struct wl_list *surfaces_attached_to = buffer->user_data;
2013 struct wlsc_surface *es, *next;
2014
2015 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002016 wl_list_remove(&es->buffer_link);
Kristian Høgsbergfac11d22011-05-02 13:35:17 -04002017 wl_list_init(&es->buffer_link);
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002018 }
2019
2020 free(surfaces_attached_to);
2021}
2022
2023const static struct wl_shm_callbacks shm_callbacks = {
2024 shm_buffer_created,
2025 shm_buffer_damaged,
2026 shm_buffer_destroyed
2027};
2028
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002029WL_EXPORT int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002030wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
2031{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002032 struct wl_event_loop *loop;
Kristian Høgsbergd711d0c2011-01-14 17:28:21 -05002033 const char *extensions;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002034
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002035 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002036
Kristian Høgsbergc5c510e2010-12-08 15:12:58 -05002037 wl_compositor_init(&ec->compositor, &compositor_interface, display);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002038
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002039 ec->shm = wl_shm_init(display, &shm_callbacks);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002040
2041 ec->image_target_texture_2d =
2042 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2043 ec->image_target_renderbuffer_storage = (void *)
2044 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
2045 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2046 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2047 ec->bind_display =
2048 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
2049 ec->unbind_display =
2050 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
2051
2052 extensions = (const char *) glGetString(GL_EXTENSIONS);
2053 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
2054 fprintf(stderr,
2055 "GL_EXT_texture_format_BGRA8888 not available\n");
2056 return -1;
2057 }
2058
2059 extensions =
2060 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
2061 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
2062 ec->has_bind_display = 1;
2063 if (ec->has_bind_display)
2064 ec->bind_display(ec->display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04002065
Kristian Høgsberg201a9042008-12-10 00:40:50 -05002066 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002067 wl_list_init(&ec->input_device_list);
2068 wl_list_init(&ec->output_list);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04002069 wl_list_init(&ec->binding_list);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002070 wl_list_init(&ec->animation_list);
Kristian Høgsberg269c7822011-05-02 14:38:18 -04002071 wlsc_spring_init(&ec->fade.spring, 0.8, 0.0, 0.0);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002072 ec->fade.animation.frame = fade_frame;
2073 wl_list_init(&ec->fade.animation.link);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04002074
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04002075 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberg3555d092011-04-11 13:58:13 -04002076 MODIFIER_CTRL | MODIFIER_ALT,
2077 terminate_binding, ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002078
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04002079 create_pointer_images(ec);
2080
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002081 screenshooter_create(ec);
2082
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002083 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002084
2085 if (wlsc_shader_init(&ec->texture_shader,
2086 vertex_shader, texture_fragment_shader) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04002087 return -1;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002088 if (init_solid_shader(&ec->solid_shader,
2089 ec->texture_shader.vertex_shader,
2090 solid_fragment_shader) < 0)
2091 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05002092
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002093 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002094 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2095 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
2096
Kristian Høgsberg20300ba2011-06-23 20:29:12 -04002097 pixman_region32_init(&ec->damage);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04002098 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002099
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002100 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002101}
2102
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002103WL_EXPORT void
Matt Roper361d2ad2011-08-29 13:52:23 -07002104wlsc_compositor_shutdown(struct wlsc_compositor *ec)
2105{
2106 struct wlsc_output *output;
2107
2108 /* Destroy all outputs associated with this compositor */
2109 wl_list_for_each(output, &ec->output_list, link)
2110 output->destroy(output);
2111}
2112
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002113static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002114{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002115 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002116
Kristian Høgsberg3cad4362011-07-15 21:09:24 -04002117 fprintf(stderr, "caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002118 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002119
2120 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002121}
2122
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002123static void *
2124load_module(const char *name, const char *entrypoint, void **handle)
2125{
2126 char path[PATH_MAX];
2127 void *module, *init;
2128
2129 if (name[0] != '/')
2130 snprintf(path, sizeof path, MODULEDIR "/%s", name);
2131 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02002132 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002133
2134 module = dlopen(path, RTLD_LAZY);
2135 if (!module) {
2136 fprintf(stderr,
2137 "failed to load module: %s\n", dlerror());
2138 return NULL;
2139 }
2140
2141 init = dlsym(module, entrypoint);
2142 if (!init) {
2143 fprintf(stderr,
2144 "failed to lookup init function: %s\n", dlerror());
2145 return NULL;
2146 }
2147
2148 return init;
2149}
2150
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002151int main(int argc, char *argv[])
2152{
2153 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05002154 struct wlsc_compositor *ec;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002155 struct wl_event_loop *loop;
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002156 int o, xserver = 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002157 void *shell_module, *backend_module;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002158 int (*shell_init)(struct wlsc_compositor *ec);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002159 struct wlsc_compositor
2160 *(*backend_init)(struct wl_display *display, char *options);
2161 char *backend = NULL;
2162 char *backend_options = "";
2163 char *shell = NULL;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002164 char *p;
2165
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002166 static const char opts[] = "B:b:o:S:i:s:x";
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002167 static const struct option longopts[ ] = {
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002168 { "backend", 1, NULL, 'B' },
2169 { "backend-options", 1, NULL, 'o' },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002170 { "socket", 1, NULL, 'S' },
2171 { "idle-time", 1, NULL, 'i' },
2172 { "shell", 1, NULL, 's' },
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002173 { "xserver", 0, NULL, 'x' },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002174 { NULL, }
2175 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05002176
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002177 while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
2178 switch (o) {
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002179 case 'B':
2180 backend = optarg;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002181 break;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002182 case 'o':
2183 backend_options = optarg;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002184 break;
2185 case 'S':
2186 option_socket_name = optarg;
2187 break;
2188 case 'i':
2189 option_idle_time = strtol(optarg, &p, 0);
2190 if (*p != '\0') {
2191 fprintf(stderr,
2192 "invalid idle time option: %s\n",
2193 optarg);
2194 exit(EXIT_FAILURE);
2195 }
2196 break;
2197 case 's':
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002198 shell = optarg;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002199 break;
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002200 case 'x':
2201 xserver = 1;
2202 break;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002203 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04002204 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002205
2206 display = wl_display_create();
2207
Tiago Vignatti2116b892011-08-08 05:52:59 -07002208 loop = wl_display_get_event_loop(display);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002209 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, display);
2210 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, display);
2211 wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal, display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07002212
2213 wl_list_init(&child_process_list);
2214 wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler, NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05002215
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002216 if (!backend) {
2217 if (getenv("WAYLAND_DISPLAY"))
2218 backend = "wayland-backend.so";
2219 else if (getenv("DISPLAY"))
2220 backend = "x11-backend.so";
2221 else if (getenv("OPENWFD"))
2222 backend = "openwfd-backend.so";
2223 else
2224 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002225 }
2226
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002227 if (!shell)
2228 shell = "desktop-shell.so";
Kristian Høgsberga9410222011-01-14 17:22:35 -05002229
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002230 backend_init = load_module(backend, "backend_init", &backend_module);
2231 if (!backend_init)
2232 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05002233
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002234 shell_init = load_module(shell, "shell_init", &shell_module);
2235 if (!shell_init)
2236 exit(EXIT_FAILURE);
Benjamin Franzke5d007092011-04-04 00:30:25 +02002237
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002238 ec = backend_init(display, backend_options);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05002239 if (ec == NULL) {
2240 fprintf(stderr, "failed to create compositor\n");
2241 exit(EXIT_FAILURE);
2242 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04002243
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002244 if (shell_init(ec) < 0)
2245 exit(EXIT_FAILURE);
2246
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002247 if (xserver)
Kristian Høgsberg3cad4362011-07-15 21:09:24 -04002248 wlsc_xserver_init(ec);
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002249
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002250 if (wl_display_add_socket(display, option_socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002251 fprintf(stderr, "failed to add socket: %m\n");
2252 exit(EXIT_FAILURE);
2253 }
2254
2255 wl_display_run(display);
2256
Kristian Høgsberg3cad4362011-07-15 21:09:24 -04002257 if (xserver)
2258 wlsc_xserver_destroy(ec);
2259
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002260 if (ec->has_bind_display)
2261 ec->unbind_display(ec->display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002262 wl_display_destroy(display);
2263
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002264 ec->destroy(ec);
2265
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002266 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002267}