blob: 23a496f20beee319c2231479621422a97d205e1f [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05008 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050017 */
18
Kristian Høgsberg06bc2642010-12-01 09:50:16 -050019#define _GNU_SOURCE
20
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040021#include <stdio.h>
22#include <string.h>
23#include <stdlib.h>
24#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050025#include <stdarg.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040026#include <sys/ioctl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040027#include <fcntl.h>
28#include <unistd.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050029#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050030#include <math.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050031#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040032#include <linux/input.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050033
Kristian Høgsberga1f3f602010-08-03 09:26:44 -040034#include "wayland-server-protocol.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040035#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050036
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010037/* The plan here is to generate a random anonymous socket name and
38 * advertise that through a service on the session dbus.
39 */
40static const char *option_socket_name = "wayland";
41
Kristian Høgsberg61a82512010-10-26 11:26:44 -040042static const char *option_background = "background.jpg";
43static const char *option_geometry = "1024x640";
44static int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050045
46static const GOptionEntry option_entries[] = {
47 { "background", 'b', 0, G_OPTION_ARG_STRING,
48 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -040049 { "connector", 'c', 0, G_OPTION_ARG_INT,
50 &option_connector, "KMS connector" },
Kristian Høgsberg61a82512010-10-26 11:26:44 -040051 { "geometry", 'g', 0, G_OPTION_ARG_STRING,
52 &option_geometry, "Geometry" },
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010053 { "socket", 's', 0, G_OPTION_ARG_STRING,
54 &option_socket_name, "Socket Name" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050055 { NULL }
56};
57
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050058static void
59wlsc_matrix_init(struct wlsc_matrix *matrix)
60{
61 static const struct wlsc_matrix identity = {
62 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
63 };
64
65 memcpy(matrix, &identity, sizeof identity);
66}
67
68static void
69wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
70{
71 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040072 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050073 div_t d;
74 int i, j;
75
76 for (i = 0; i < 16; i++) {
77 tmp.d[i] = 0;
78 d = div(i, 4);
79 row = m->d + d.quot * 4;
80 column = n->d + d.rem;
81 for (j = 0; j < 4; j++)
82 tmp.d[i] += row[j] * column[j * 4];
83 }
84 memcpy(m, &tmp, sizeof tmp);
85}
86
87static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040088wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050089{
Kristian Høgsberg27803c62010-06-06 22:23:21 -040090 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050091 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
92 };
93
94 wlsc_matrix_multiply(matrix, &translate);
95}
96
97static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040098wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050099{
100 struct wlsc_matrix scale = {
101 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
102 };
103
104 wlsc_matrix_multiply(matrix, &scale);
105}
106
107static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400108wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
109{
110 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400111 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400112
113 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400114 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400115 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400116 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400117 }
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400118
119 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400120}
121
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400122static struct wlsc_surface *
123wlsc_surface_create(struct wlsc_compositor *compositor,
124 struct wl_visual *visual,
125 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500126{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400127 struct wlsc_surface *surface;
128
129 surface = malloc(sizeof *surface);
130 if (surface == NULL)
131 return NULL;
132
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500133 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400134 glBindTexture(GL_TEXTURE_2D, surface->texture);
135 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
136 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
137 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
138 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
139
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500140 surface->compositor = compositor;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500141 surface->visual = visual;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400142 surface->x = x;
143 surface->y = y;
144 surface->width = width;
145 surface->height = height;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400146 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400147 wlsc_matrix_scale(&surface->matrix, width, height, 1);
148 wlsc_matrix_translate(&surface->matrix, x, y, 0);
149
150 wlsc_matrix_init(&surface->matrix_inv);
151 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
152 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500153
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400154 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500155}
156
157static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400158destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500159{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400160 struct wlsc_surface *surface =
161 container_of(resource, struct wlsc_surface, base.base);
162 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500163 struct wlsc_listener *l;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400164
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400165 wl_list_remove(&surface->link);
166 glDeleteTextures(1, &surface->texture);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400167
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500168 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400169 l->func(l, surface);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400170
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400171 free(surface);
Kristian Høgsberg117d5132010-08-11 08:56:47 -0400172
173 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500174}
175
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400176static int
177texture_from_png(const char *filename, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500178{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400179 GdkPixbuf *pixbuf;
180 GError *error = NULL;
181 void *data;
182 GLenum format;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500183
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400184 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
185 width, height,
186 FALSE, &error);
187 if (error != NULL)
188 return -1;
189
190 data = gdk_pixbuf_get_pixels(pixbuf);
191
192 if (gdk_pixbuf_get_has_alpha(pixbuf))
193 format = GL_RGBA;
194 else
195 format = GL_RGB;
196
Chia-I Wu1f411902010-10-29 15:20:16 +0800197 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
198 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400199
200 gdk_pixbuf_unref(pixbuf);
201
202 return 0;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500203}
204
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400205static const struct {
206 const char *filename;
207 int hotspot_x, hotspot_y;
208} pointer_images[] = {
Kristian Høgsberg4219a402010-08-16 16:43:03 -0400209 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
210 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
211 { DATADIR "/wayland/bottom_side.png", 16, 20 },
212 { DATADIR "/wayland/grabbing.png", 20, 17 },
213 { DATADIR "/wayland/left_ptr.png", 10, 5 },
214 { DATADIR "/wayland/left_side.png", 10, 20 },
215 { DATADIR "/wayland/right_side.png", 30, 19 },
216 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
217 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
218 { DATADIR "/wayland/top_side.png", 18, 8 },
219 { DATADIR "/wayland/xterm.png", 15, 15 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400220};
221
222static void
223create_pointer_images(struct wlsc_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500224{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400225 int i, count;
226 GLuint texture;
227 const int width = 32, height = 32;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400228
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400229 glGenTextures(1, &texture);
230 glBindTexture(GL_TEXTURE_2D, texture);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400235
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400236 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400237 ec->pointer_buffers = malloc(count * sizeof *ec->pointer_buffers);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400238 for (i = 0; i < count; i++) {
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400239 ec->pointer_buffers[i] =
240 wlsc_drm_buffer_create(ec, width, height,
241 &ec->argb_visual);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400242 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400243 ec->pointer_buffers[i]->image);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400244 texture_from_png(pointer_images[i].filename, width, height);
245 }
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400246}
247
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500248static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500249background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500250{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500251 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500252 GdkPixbuf *pixbuf;
253 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500254 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500255 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500256
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400257 background = wlsc_surface_create(output->compositor,
258 &output->compositor->rgb_visual,
259 output->x, output->y,
260 output->width, output->height);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500261 if (background == NULL)
262 return NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500263
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500264 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500265 output->width,
266 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500267 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500268 if (error != NULL) {
269 free(background);
270 return NULL;
271 }
272
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500273 data = gdk_pixbuf_get_pixels(pixbuf);
274
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400275 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500276 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500277 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500278 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500279
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500280 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
281 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500282 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500283
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500284 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500285}
286
287static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400288wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500289{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500290 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400291 struct wlsc_matrix tmp;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500292
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400293 tmp = es->matrix;
294 wlsc_matrix_multiply(&tmp, &output->matrix);
295 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
296 glUniform1i(ec->tex_uniform, 0);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500297
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500298 if (es->visual == &ec->argb_visual) {
299 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
300 glEnable(GL_BLEND);
301 } else if (es->visual == &ec->premultiplied_argb_visual) {
302 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
303 glEnable(GL_BLEND);
304 } else {
305 glDisable(GL_BLEND);
306 }
307
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500308 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400309 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
310 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
311 5 * sizeof(GLfloat), NULL);
312 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
313 5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
314 glEnableVertexAttribArray(0);
315 glEnableVertexAttribArray(1);
316 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500317}
318
319static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400320wlsc_surface_raise(struct wlsc_surface *surface)
321{
322 struct wlsc_compositor *compositor = surface->compositor;
323
324 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400325 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400326}
327
328static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400329wlsc_surface_update_matrix(struct wlsc_surface *es)
330{
331 wlsc_matrix_init(&es->matrix);
332 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
333 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
334
335 wlsc_matrix_init(&es->matrix_inv);
336 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
337 wlsc_matrix_scale(&es->matrix_inv,
338 1.0 / es->width, 1.0 / es->height, 1);
339}
340
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400341void
342wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400343{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400344 wl_display_post_frame(compositor->wl_display, msecs);
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400345 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400346}
347
348static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400349wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400350{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500351 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500352 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500353 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500354
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500355 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500356
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500357 if (output->background)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400358 wlsc_surface_draw(output->background, output);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500359 else
360 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400361
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400362 wl_list_for_each_reverse(es, &ec->surface_list, link)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400363 wlsc_surface_draw(es, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400364
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400365 if (ec->focus)
366 wl_list_for_each(eid, &ec->input_device_list, link)
367 wlsc_surface_draw(eid->sprite, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500368}
369
370static void
371repaint(void *data)
372{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500373 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500374 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500375
376 if (!ec->repaint_needed) {
377 ec->repaint_on_timeout = 0;
378 return;
379 }
380
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500381 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400382 wlsc_output_repaint(output);
383
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400384 ec->present(ec);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500385
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500386 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400387}
388
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400389void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400390wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400391{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400392 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400393 if (compositor->repaint_on_timeout)
394 return;
395
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400396 wl_event_source_timer_update(compositor->timer_source, 1);
397 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400398}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500399
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500400static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500401surface_destroy(struct wl_client *client,
402 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400403{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400404 wl_resource_destroy(&surface->base, client);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400405}
406
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500407static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500408surface_attach(struct wl_client *client,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400409 struct wl_surface *surface, struct wl_buffer *buffer)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400410{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500411 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400412
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400413 buffer->attach(buffer, surface);
414 es->buffer = buffer;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400415}
416
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500417static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500418surface_map(struct wl_client *client,
419 struct wl_surface *surface,
420 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400421{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500422 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400423
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400424 es->x = x;
425 es->y = y;
426 es->width = width;
427 es->height = height;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400428
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400429 wlsc_surface_update_matrix(es);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400430 wlsc_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400431}
432
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500433static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500434surface_damage(struct wl_client *client,
435 struct wl_surface *surface,
436 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500437{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400438 struct wlsc_surface *es = (struct wlsc_surface *) surface;
439
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400440 es->buffer->damage(es->buffer, surface, x, y, width, height);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400441 wlsc_compositor_schedule_repaint(es->compositor);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500442}
443
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500444const static struct wl_surface_interface surface_interface = {
445 surface_destroy,
446 surface_attach,
447 surface_map,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500448 surface_damage
449};
450
451static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400452wlsc_input_device_attach(struct wlsc_input_device *device,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400453 struct wl_buffer *buffer, int x, int y)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400454{
455 struct wlsc_compositor *ec = device->ec;
456
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400457 buffer->attach(buffer, &device->sprite->base);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400458 device->hotspot_x = x;
459 device->hotspot_y = y;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400460
461 device->sprite->x = device->x - device->hotspot_x;
462 device->sprite->y = device->y - device->hotspot_y;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400463 device->sprite->width = buffer->width;
464 device->sprite->height = buffer->height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400465 wlsc_surface_update_matrix(device->sprite);
466
467 wlsc_compositor_schedule_repaint(ec);
468}
469
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400470
471static void
472wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
473 enum wlsc_pointer_type type)
474{
475 struct wlsc_compositor *compositor = device->ec;
476
477 wlsc_input_device_attach(device,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400478 &compositor->pointer_buffers[type]->base,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400479 pointer_images[type].hotspot_x,
480 pointer_images[type].hotspot_y);
481}
482
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400483static void
484wlsc_input_device_start_grab(struct wlsc_input_device *device,
485 uint32_t time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400486 enum wlsc_grab_type grab)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400487{
Kristian Høgsberg26437072010-12-01 10:17:47 -0500488 struct wlsc_surface *focus =
489 (struct wlsc_surface *) device->base.pointer_focus;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400490
Kristian Høgsberg26437072010-12-01 10:17:47 -0500491 device->grab = grab;
492 device->grab_surface = focus;
493 device->grab_dx = focus->x - device->grab_x;
494 device->grab_dy = focus->y - device->grab_y;
495 device->grab_width = focus->width;
496 device->grab_height = focus->height;
497
498 wl_input_device_set_pointer_focus(&device->base,
499 &wl_grab_surface, time, 0, 0, 0, 0);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400500}
501
502static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400503shell_move(struct wl_client *client, struct wl_shell *shell,
504 struct wl_surface *surface,
505 struct wl_input_device *device, uint32_t time)
506{
507 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
508
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400509 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
510 wd->grab_time != time ||
Kristian Høgsberg26437072010-12-01 10:17:47 -0500511 wd->base.pointer_focus != surface)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400512 return;
513
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400514 wlsc_input_device_start_grab(wd, time, WLSC_DEVICE_GRAB_MOVE);
515 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400516}
517
518static void
519shell_resize(struct wl_client *client, struct wl_shell *shell,
520 struct wl_surface *surface,
521 struct wl_input_device *device, uint32_t time, uint32_t edges)
522{
523 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400524 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400525
526 if (edges == 0 || edges > 15 ||
527 (edges & 3) == 3 || (edges & 12) == 12)
528 return;
529
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400530 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
531 wd->grab_time != time ||
Kristian Høgsberg26437072010-12-01 10:17:47 -0500532 wd->base.pointer_focus != surface)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400533 return;
534
535 switch (edges) {
536 case WLSC_DEVICE_GRAB_RESIZE_TOP:
537 pointer = WLSC_POINTER_TOP;
538 break;
539 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
540 pointer = WLSC_POINTER_BOTTOM;
541 break;
542 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
543 pointer = WLSC_POINTER_LEFT;
544 break;
545 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
546 pointer = WLSC_POINTER_TOP_LEFT;
547 break;
548 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
549 pointer = WLSC_POINTER_BOTTOM_LEFT;
550 break;
551 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
552 pointer = WLSC_POINTER_RIGHT;
553 break;
554 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
555 pointer = WLSC_POINTER_TOP_RIGHT;
556 break;
557 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
558 pointer = WLSC_POINTER_BOTTOM_RIGHT;
559 break;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400560 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400561
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400562 wlsc_input_device_start_grab(wd, time, edges);
563 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400564}
565
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500566static uint32_t
567get_time(void)
568{
569 struct timeval tv;
570
571 gettimeofday(&tv, NULL);
572
573 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
574}
575
576struct wlsc_drag {
577 struct wl_drag drag;
578 struct wlsc_listener listener;
579};
580
581static void
582wl_drag_set_pointer_focus(struct wl_drag *drag,
583 struct wlsc_surface *surface, uint32_t time,
584 int32_t x, int32_t y, int32_t sx, int32_t sy);
585
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400586static void
587destroy_drag(struct wl_resource *resource, struct wl_client *client)
588{
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500589 struct wlsc_drag *drag =
590 container_of(resource, struct wlsc_drag, drag.resource);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400591
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500592 wl_list_remove(&drag->listener.link);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400593
594 free(drag);
595}
596
597const static struct wl_drag_interface drag_interface;
598
599static void
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500600drag_handle_surface_destroy(struct wlsc_listener *listener,
601 struct wlsc_surface *surface)
602{
603 struct wlsc_drag *drag =
604 container_of(listener, struct wlsc_drag, listener);
605 uint32_t time = get_time();
606
607 if (drag->drag.pointer_focus == &surface->base)
608 wl_drag_set_pointer_focus(&drag->drag, NULL, time, 0, 0, 0, 0);
609}
610
611static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400612shell_create_drag(struct wl_client *client,
613 struct wl_shell *shell, uint32_t id)
614{
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500615 struct wlsc_drag *drag;
616 struct wlsc_compositor *ec =
617 container_of(shell, struct wlsc_compositor, shell);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400618
619 drag = malloc(sizeof *drag);
620 if (drag == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400621 wl_client_post_no_memory(client);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400622 return;
623 }
624
625 memset(drag, 0, sizeof *drag);
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500626 drag->drag.resource.base.id = id;
627 drag->drag.resource.base.interface = &wl_drag_interface;
628 drag->drag.resource.base.implementation =
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400629 (void (**)(void)) &drag_interface;
630
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500631 drag->drag.resource.destroy = destroy_drag;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400632
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500633 drag->listener.func = drag_handle_surface_destroy;
634 wl_list_insert(ec->surface_destroy_listener_list.prev,
635 &drag->listener.link);
636
637 wl_client_add_resource(client, &drag->drag.resource);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400638}
639
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400640const static struct wl_shell_interface shell_interface = {
641 shell_move,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400642 shell_resize,
643 shell_create_drag
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400644};
645
646static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500647compositor_create_surface(struct wl_client *client,
648 struct wl_compositor *compositor, uint32_t id)
649{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500650 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400651 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500652
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400653 surface = wlsc_surface_create(ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400654 if (surface == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400655 wl_client_post_no_memory(client);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500656 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400657 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500658
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400659 wl_list_insert(ec->surface_list.prev, &surface->link);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400660 surface->base.base.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400661
662 surface->base.base.base.id = id;
663 surface->base.base.base.interface = &wl_surface_interface;
664 surface->base.base.base.implementation =
665 (void (**)(void)) &surface_interface;
666 surface->base.client = client;
667
668 wl_client_add_resource(client, &surface->base.base);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500669}
670
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500671const static struct wl_compositor_interface compositor_interface = {
672 compositor_create_surface,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500673};
674
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500675static void
676wlsc_surface_transform(struct wlsc_surface *surface,
677 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
678{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400679 struct wlsc_vector v = { { x, y, 0, 1 } };
680
681 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400682 *sx = v.f[0] * surface->width;
683 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500684}
685
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500686static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500687pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500688{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500689 struct wlsc_compositor *ec = device->ec;
690 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500691
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400692 wl_list_for_each(es, &ec->surface_list, link) {
693 wlsc_surface_transform(es, device->x, device->y, sx, sy);
694 if (0 <= *sx && *sx < es->width &&
695 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500696 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400697 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500698
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500699 return NULL;
700}
701
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500702void
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400703notify_motion(struct wlsc_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500704{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500705 struct wlsc_surface *es;
706 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500707 struct wlsc_output *output;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400708 int32_t sx, sy, width, height;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500709
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500710 /* FIXME: We need some multi head love here. */
711 output = container_of(ec->output_list.next, struct wlsc_output, link);
712 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500713 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500714 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500715 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500716 if (x >= output->x + output->width)
717 x = output->x + output->width - 1;
718 if (y >= output->y + output->height)
719 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500720
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500721 device->x = x;
722 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500723
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400724 switch (device->grab) {
725 case WLSC_DEVICE_GRAB_NONE:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400726 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg26437072010-12-01 10:17:47 -0500727 wl_input_device_set_pointer_focus(&device->base, &es->base,
728 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400729 if (es)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400730 wl_client_post_event(es->base.client,
731 &device->base.base,
732 WL_INPUT_DEVICE_MOTION,
733 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400734 break;
735
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400736 case WLSC_DEVICE_GRAB_MOTION:
Kristian Høgsberg26437072010-12-01 10:17:47 -0500737 es = (struct wlsc_surface *) device->base.pointer_focus;
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400738 wlsc_surface_transform(es, x, y, &sx, &sy);
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400739 wl_client_post_event(es->base.client,
740 &device->base.base,
741 WL_INPUT_DEVICE_MOTION,
742 time, x, y, sx, sy);
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400743 break;
744
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400745 case WLSC_DEVICE_GRAB_MOVE:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400746 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400747 es->x = x + device->grab_dx;
748 es->y = y + device->grab_dy;;
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400749 wl_client_post_event(es->base.client,
750 &ec->shell.base,
751 WL_SHELL_CONFIGURE,
752 time, device->grab,
753 &es->base, es->x, es->y,
754 es->width, es->height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400755
756 wlsc_surface_update_matrix(es);
757
758 break;
759
760 case WLSC_DEVICE_GRAB_RESIZE_TOP:
761 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
762 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
763 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
764 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
765 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
766 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
767 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
768 case WLSC_DEVICE_GRAB_RESIZE_MASK:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400769 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400770
771 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_LEFT) {
772 sx = x + device->grab_dx;
773 width = device->grab_x - x + device->grab_width;
774 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_RIGHT) {
775 sx = device->grab_x + device->grab_dx;
776 width = x - device->grab_x + device->grab_width;
777 } else {
778 sx = device->grab_x + device->grab_dx;
779 width = device->grab_width;
780 }
781
782 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_TOP) {
783 sy = y + device->grab_dy;
784 height = device->grab_y - y + device->grab_height;
785 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_BOTTOM) {
786 sy = device->grab_y + device->grab_dy;
787 height = y - device->grab_y + device->grab_height;
788 } else {
789 sy = device->grab_y + device->grab_dy;
790 height = device->grab_height;
791 }
792
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400793 wl_client_post_event(es->base.client,
794 &ec->shell.base,
795 WL_SHELL_CONFIGURE, time, device->grab,
796 &es->base, sx, sy, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400797 break;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400798
799 case WLSC_DEVICE_GRAB_DRAG:
800 es = pick_surface(device, &sx, &sy);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400801 wl_drag_set_pointer_focus(device->drag,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400802 es, time, x, y, sx, sy);
803 if (es)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400804 wl_client_post_event(es->base.client,
805 &device->drag->drag_offer.base,
806 WL_DRAG_OFFER_MOTION,
807 time, x, y, sx, sy);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400808 break;
809
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400810 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500811
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400812 device->sprite->x = device->x - device->hotspot_x;
813 device->sprite->y = device->y - device->hotspot_y;
814 wlsc_surface_update_matrix(device->sprite);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500815
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400816 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500817}
818
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400819static void
820wlsc_input_device_end_grab(struct wlsc_input_device *device, uint32_t time)
821{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400822 struct wl_drag *drag = device->drag;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400823 struct wlsc_surface *es;
824 int32_t sx, sy;
825
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400826 switch (device->grab) {
827 case WLSC_DEVICE_GRAB_DRAG:
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400828 if (drag->target)
829 wl_client_post_event(drag->target,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400830 &drag->drag_offer.base,
831 WL_DRAG_OFFER_DROP);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400832 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400833 device->drag = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400834 break;
835 default:
836 break;
837 }
838
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400839 device->grab = WLSC_DEVICE_GRAB_NONE;
840 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg26437072010-12-01 10:17:47 -0500841 wl_input_device_set_pointer_focus(&device->base, &es->base, time,
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400842 device->x, device->y, sx, sy);
843}
844
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500845void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500846notify_button(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400847 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500848{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400849 struct wlsc_surface *surface;
850 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500851
Kristian Høgsberg26437072010-12-01 10:17:47 -0500852 surface = (struct wlsc_surface *) device->base.pointer_focus;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400853 if (surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400854 if (state && device->grab == WLSC_DEVICE_GRAB_NONE) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400855 wlsc_surface_raise(surface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400856 device->grab = WLSC_DEVICE_GRAB_MOTION;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400857 device->grab_button = button;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400858 device->grab_time = time;
859 device->grab_x = device->x;
860 device->grab_y = device->y;
Kristian Høgsberg26437072010-12-01 10:17:47 -0500861 wl_input_device_set_keyboard_focus(&device->base,
862 &surface->base,
863 time);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500864 }
865
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400866 if (state && button == BTN_LEFT &&
867 device->grab == WLSC_DEVICE_GRAB_MOTION &&
868 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400869 shell_move(NULL, (struct wl_shell *) &compositor->shell,
870 &surface->base, &device->base, time);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400871 else if (state && button == BTN_MIDDLE &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400872 device->grab == WLSC_DEVICE_GRAB_MOTION &&
873 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400874 shell_resize(NULL, (struct wl_shell *) &compositor->shell,
875
876 &surface->base, &device->base, time,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400877 WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400878 else if (device->grab == WLSC_DEVICE_GRAB_NONE ||
879 device->grab == WLSC_DEVICE_GRAB_MOTION)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400880 wl_client_post_event(surface->base.client,
881 &device->base.base,
882 WL_INPUT_DEVICE_BUTTON,
883 time, button, state);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500884
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400885 if (!state &&
886 device->grab != WLSC_DEVICE_GRAB_NONE &&
887 device->grab_button == button) {
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400888 wlsc_input_device_end_grab(device, time);
889 }
890
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400891 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500892 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500893}
894
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500895void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500896notify_key(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400897 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500898{
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500899 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400900 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500901
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400902 switch (key | device->modifier_state) {
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400903 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400904 kill(0, SIGTERM);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500905 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500906 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500907
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400908 switch (key) {
909 case KEY_LEFTCTRL:
910 case KEY_RIGHTCTRL:
911 modifier = MODIFIER_CTRL;
912 break;
913
914 case KEY_LEFTALT:
915 case KEY_RIGHTALT:
916 modifier = MODIFIER_ALT;
917 break;
918
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400919 case KEY_LEFTMETA:
920 case KEY_RIGHTMETA:
921 modifier = MODIFIER_SUPER;
922 break;
923
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400924 default:
925 modifier = 0;
926 break;
927 }
928
929 if (state)
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400930 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400931 else
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400932 device->modifier_state &= ~modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400933
Kristian Høgsberg26437072010-12-01 10:17:47 -0500934 end = device->base.keys.data + device->base.keys.size;
935 for (k = device->base.keys.data; k < end; k++) {
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500936 if (*k == key)
937 *k = *--end;
938 }
Kristian Høgsberg26437072010-12-01 10:17:47 -0500939 device->base.keys.size = (void *) end - device->base.keys.data;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500940 if (state) {
Kristian Høgsberg26437072010-12-01 10:17:47 -0500941 k = wl_array_add(&device->base.keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500942 *k = key;
943 }
Ray Strodee96dcb82008-12-20 02:00:49 -0500944
Kristian Høgsberg26437072010-12-01 10:17:47 -0500945 if (device->base.keyboard_focus != NULL)
946 wl_client_post_event(device->base.keyboard_focus->client,
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400947 &device->base.base,
948 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400949}
950
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400951static void
952input_device_attach(struct wl_client *client,
953 struct wl_input_device *device_base,
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400954 uint32_t time,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400955 struct wl_buffer *buffer, int32_t x, int32_t y)
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400956{
957 struct wlsc_input_device *device =
958 (struct wlsc_input_device *) device_base;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400959
Kristian Høgsberg26437072010-12-01 10:17:47 -0500960 if (time < device->base.pointer_focus_time)
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400961 return;
Kristian Høgsberg26437072010-12-01 10:17:47 -0500962 if (device->base.pointer_focus == NULL)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400963 return;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100964
Kristian Høgsberg26437072010-12-01 10:17:47 -0500965 if (device->base.pointer_focus->client != client &&
966 !(device->base.pointer_focus == &wl_grab_surface &&
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400967 device->grab_surface->base.client == client))
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400968 return;
969
Kristian Høgsbergf4cb2012010-08-16 17:46:25 -0400970 if (buffer == NULL) {
971 wlsc_input_device_set_pointer_image(device,
972 WLSC_POINTER_LEFT_PTR);
973 return;
974 }
975
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400976 wlsc_input_device_attach(device, buffer, x, y);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400977}
978
979const static struct wl_input_device_interface input_device_interface = {
980 input_device_attach,
981};
982
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400983static void
984handle_surface_destroy(struct wlsc_listener *listener,
985 struct wlsc_surface *surface)
986{
987 struct wlsc_input_device *device =
988 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400989 uint32_t time = get_time();
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400990
Kristian Høgsberg26437072010-12-01 10:17:47 -0500991 if (device->base.keyboard_focus == &surface->base)
992 wl_input_device_set_keyboard_focus(&device->base, NULL, time);
993 if (device->base.pointer_focus == &surface->base)
994 wl_input_device_set_pointer_focus(&device->base, NULL, time,
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500995 0, 0, 0, 0);
Kristian Høgsberg26437072010-12-01 10:17:47 -0500996 if (device->base.pointer_focus == &surface->base ||
997 (device->base.pointer_focus == &wl_grab_surface &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400998 device->grab_surface == surface))
999 wlsc_input_device_end_grab(device, time);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001000}
1001
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001002static void
1003wl_drag_set_pointer_focus(struct wl_drag *drag,
1004 struct wlsc_surface *surface, uint32_t time,
1005 int32_t x, int32_t y, int32_t sx, int32_t sy)
1006{
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001007 char **p, **end;
1008
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001009 if (drag->pointer_focus == &surface->base)
1010 return;
1011
1012 if (drag->pointer_focus &&
1013 (!surface || drag->pointer_focus->client != surface->base.client))
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001014 wl_client_post_event(drag->pointer_focus->client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001015 &drag->drag_offer.base,
1016 WL_DRAG_OFFER_POINTER_FOCUS,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001017 time, NULL, 0, 0, 0, 0);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001018
1019 if (surface &&
1020 (!drag->pointer_focus ||
1021 drag->pointer_focus->client != surface->base.client)) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001022 wl_client_post_global(surface->base.client,
1023 &drag->drag_offer.base);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001024
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001025 end = drag->types.data + drag->types.size;
1026 for (p = drag->types.data; p < end; p++)
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001027 wl_client_post_event(surface->base.client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001028 &drag->drag_offer.base,
1029 WL_DRAG_OFFER_OFFER, *p);
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001030 }
1031
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001032 if (surface) {
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001033 wl_client_post_event(surface->base.client,
1034 &drag->drag_offer.base,
1035 WL_DRAG_OFFER_POINTER_FOCUS,
1036 time, &surface->base,
1037 x, y, sx, sy);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001038
1039 }
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001040
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001041 drag->pointer_focus = &surface->base;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001042 drag->pointer_focus_time = time;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001043 drag->target = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001044}
1045
1046static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001047drag_offer_accept(struct wl_client *client,
1048 struct wl_drag_offer *offer, uint32_t time, const char *type)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001049{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001050 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001051 char **p, **end;
1052
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001053 /* If the client responds to drag pointer_focus or motion
1054 * events after the pointer has left the surface, we just
1055 * discard the accept requests. The drag source just won't
1056 * get the corresponding 'target' events and eventually the
1057 * next surface/root will start sending events. */
1058 if (time < drag->pointer_focus_time)
1059 return;
1060
1061 drag->target = client;
1062 drag->type = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001063 end = drag->types.data + drag->types.size;
1064 for (p = drag->types.data; p < end; p++)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001065 if (type && strcmp(*p, type) == 0)
1066 drag->type = *p;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001067
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001068 wl_client_post_event(drag->source->client, &drag->resource.base,
1069 WL_DRAG_TARGET, drag->type);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001070}
1071
1072static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001073drag_offer_receive(struct wl_client *client,
1074 struct wl_drag_offer *offer, int fd)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001075{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001076 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001077
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001078 wl_client_post_event(drag->source->client, &drag->resource.base,
1079 WL_DRAG_FINISH, fd);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001080 close(fd);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001081}
1082
Kristian Høgsbergd44bc8b2010-11-30 15:10:26 -05001083static void
1084drag_offer_reject(struct wl_client *client, struct wl_drag_offer *offer)
1085{
1086 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
1087
1088 wl_client_post_event(drag->source->client, &drag->resource.base,
1089 WL_DRAG_REJECT);
1090}
1091
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001092static const struct wl_drag_offer_interface drag_offer_interface = {
1093 drag_offer_accept,
Kristian Høgsbergd44bc8b2010-11-30 15:10:26 -05001094 drag_offer_receive,
1095 drag_offer_reject
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001096};
1097
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001098static void
1099drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
1100{
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001101 char **p;
1102
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001103 p = wl_array_add(&drag->types, sizeof *p);
1104 if (p)
1105 *p = strdup(type);
1106 if (!p || !*p)
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001107 wl_client_post_no_memory(client);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001108}
1109
1110static void
1111drag_activate(struct wl_client *client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001112 struct wl_drag *drag,
1113 struct wl_surface *surface,
1114 struct wl_input_device *input_device, uint32_t time)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001115{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001116 struct wl_display *display = wl_client_get_display (client);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001117 struct wlsc_input_device *device =
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001118 (struct wlsc_input_device *) input_device;
1119 struct wlsc_surface *target;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001120 int32_t sx, sy;
1121
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001122 if (device->grab != WLSC_DEVICE_GRAB_MOTION ||
Kristian Høgsberg26437072010-12-01 10:17:47 -05001123 device->base.pointer_focus != surface ||
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001124 device->grab_time != time)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001125 return;
1126
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001127 drag->source = surface;
1128 drag->input_device = input_device;
1129
1130 drag->drag_offer.base.interface = &wl_drag_offer_interface;
1131 drag->drag_offer.base.implementation =
1132 (void (**)(void)) &drag_offer_interface;
1133
1134 wl_display_add_object(display, &drag->drag_offer.base);
1135
1136 wlsc_input_device_start_grab(device, time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001137 WLSC_DEVICE_GRAB_DRAG);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001138
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001139 device->drag = drag;
1140 target = pick_surface(device, &sx, &sy);
1141 wl_drag_set_pointer_focus(device->drag, target, time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001142 device->x, device->y, sx, sy);
1143}
1144
1145static void
1146drag_cancel(struct wl_client *client, struct wl_drag *drag)
1147{
1148 struct wlsc_input_device *device =
1149 (struct wlsc_input_device *) drag->input_device;
1150
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001151 if (drag->source == NULL || drag->source->client != client)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001152 return;
1153
1154 wlsc_input_device_end_grab(device, get_time());
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001155 device->drag = NULL;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001156}
1157
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001158static const struct wl_drag_interface drag_interface = {
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001159 drag_offer,
1160 drag_activate,
1161 drag_cancel,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001162};
1163
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001164void
1165wlsc_input_device_init(struct wlsc_input_device *device,
1166 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001167{
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001168 device->base.base.interface = &wl_input_device_interface;
1169 device->base.base.implementation =
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001170 (void (**)(void)) &input_device_interface;
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001171 wl_display_add_object(ec->wl_display, &device->base.base);
1172 wl_display_add_global(ec->wl_display, &device->base.base, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001173
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001174 device->x = 100;
1175 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001176 device->ec = ec;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001177 device->sprite = wlsc_surface_create(ec, &ec->argb_visual,
1178 device->x, device->y, 32, 32);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001179 device->hotspot_x = 16;
1180 device->hotspot_y = 16;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001181
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001182 device->listener.func = handle_surface_destroy;
1183 wl_list_insert(ec->surface_destroy_listener_list.prev,
1184 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001185 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001186
1187 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001188}
1189
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001190static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001191wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001192{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001193 struct wlsc_output *output =
1194 container_of(global, struct wlsc_output, base);
1195
1196 wl_client_post_event(client, global,
1197 WL_OUTPUT_GEOMETRY,
1198 output->width, output->height);
1199}
1200
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001201static const char vertex_shader[] =
1202 "uniform mat4 proj;\n"
1203 "attribute vec4 position;\n"
1204 "attribute vec2 texcoord;\n"
1205 "varying vec2 v_texcoord;\n"
1206 "void main()\n"
1207 "{\n"
1208 " gl_Position = proj * position;\n"
1209 " v_texcoord = texcoord;\n"
1210 "}\n";
1211
1212static const char fragment_shader[] =
1213 /* "precision mediump float;\n" */
1214 "varying vec2 v_texcoord;\n"
1215 "uniform sampler2D tex;\n"
1216 "void main()\n"
1217 "{\n"
1218 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1219 "}\n";
1220
Kristian Høgsberga9468212010-06-14 21:03:11 -04001221static int
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001222init_shaders(struct wlsc_compositor *ec)
1223{
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001224 GLuint v, f, program;
1225 const char *p;
1226 char msg[512];
1227 GLfloat vertices[4 * 5];
1228 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001229
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001230 p = vertex_shader;
1231 v = glCreateShader(GL_VERTEX_SHADER);
1232 glShaderSource(v, 1, &p, NULL);
1233 glCompileShader(v);
1234 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
1235 if (!status) {
1236 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
1237 fprintf(stderr, "vertex shader info: %s\n", msg);
1238 return -1;
1239 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001240
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001241 p = fragment_shader;
1242 f = glCreateShader(GL_FRAGMENT_SHADER);
1243 glShaderSource(f, 1, &p, NULL);
1244 glCompileShader(f);
1245 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
1246 if (!status) {
1247 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
1248 fprintf(stderr, "fragment shader info: %s\n", msg);
1249 return -1;
1250 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001251
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001252 program = glCreateProgram();
1253 glAttachShader(program, v);
1254 glAttachShader(program, f);
1255 glBindAttribLocation(program, 0, "position");
1256 glBindAttribLocation(program, 1, "texcoord");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001257
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001258 glLinkProgram(program);
1259 glGetProgramiv(program, GL_LINK_STATUS, &status);
1260 if (!status) {
1261 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1262 fprintf(stderr, "link info: %s\n", msg);
1263 return -1;
1264 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001265
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001266 glUseProgram(program);
1267 ec->proj_uniform = glGetUniformLocation(program, "proj");
1268 ec->tex_uniform = glGetUniformLocation(program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001269
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001270 vertices[ 0] = 0.0;
1271 vertices[ 1] = 0.0;
1272 vertices[ 2] = 0.0;
1273 vertices[ 3] = 0.0;
1274 vertices[ 4] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001275
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001276 vertices[ 5] = 0.0;
1277 vertices[ 6] = 1.0;
1278 vertices[ 7] = 0.0;
1279 vertices[ 8] = 0.0;
1280 vertices[ 9] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001281
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001282 vertices[10] = 1.0;
1283 vertices[11] = 0.0;
1284 vertices[12] = 0.0;
1285 vertices[13] = 1.0;
1286 vertices[14] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001287
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001288 vertices[15] = 1.0;
1289 vertices[16] = 1.0;
1290 vertices[17] = 0.0;
1291 vertices[18] = 1.0;
1292 vertices[19] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001293
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001294 glGenBuffers(1, &ec->vbo);
1295 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
1296 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001297
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001298 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001299}
1300
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001301static const struct wl_interface visual_interface = {
1302 "visual", 1,
1303};
1304
1305static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001306add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001307{
1308 ec->argb_visual.base.interface = &visual_interface;
1309 ec->argb_visual.base.implementation = NULL;
1310 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001311 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001312
1313 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1314 ec->premultiplied_argb_visual.base.implementation = NULL;
1315 wl_display_add_object(ec->wl_display,
1316 &ec->premultiplied_argb_visual.base);
1317 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001318 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001319
1320 ec->rgb_visual.base.interface = &visual_interface;
1321 ec->rgb_visual.base.implementation = NULL;
1322 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001323 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1324}
1325
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001326void
1327wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1328 int x, int y, int width, int height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001329{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001330 output->compositor = c;
1331 output->x = x;
1332 output->y = y;
1333 output->width = width;
1334 output->height = height;
1335
1336 output->background =
1337 background_create(output, option_background);
1338
1339 wlsc_matrix_init(&output->matrix);
1340 wlsc_matrix_translate(&output->matrix,
1341 -output->x - output->width / 2.0,
1342 -output->y - output->height / 2.0, 0);
1343 wlsc_matrix_scale(&output->matrix,
1344 2.0 / output->width, 2.0 / output->height, 1);
1345
1346 output->base.interface = &wl_output_interface;
1347 wl_display_add_object(c->wl_display, &output->base);
1348 wl_display_add_global(c->wl_display, &output->base,
1349 wlsc_output_post_geometry);
1350}
1351
1352int
1353wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1354{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001355 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001356
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001357 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001358
Kristian Høgsbergf8ffded2010-09-03 15:15:33 -04001359 ec->base.base.interface = &wl_compositor_interface;
1360 ec->base.base.implementation =
1361 (void (**)(void)) &compositor_interface;
1362
1363 wl_display_add_object(display, &ec->base.base);
1364 if (wl_display_add_global(display, &ec->base.base, NULL))
1365 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001366
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001367 wlsc_shm_init(ec);
1368
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001369 ec->shell.base.interface = &wl_shell_interface;
1370 ec->shell.base.implementation = (void (**)(void)) &shell_interface;
1371 wl_display_add_object(display, &ec->shell.base);
1372 if (wl_display_add_global(display, &ec->shell.base, NULL))
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001373 return -1;
1374
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001375 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001376
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001377 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001378 wl_list_init(&ec->input_device_list);
1379 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001380 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001381
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001382 create_pointer_images(ec);
1383
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001384 screenshooter_create(ec);
1385
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001386 glGenFramebuffers(1, &ec->fbo);
1387 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1388 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001389 if (init_shaders(ec) < 0)
1390 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001391
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001392 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001393 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001394 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001395
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001396 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001397}
1398
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001399
1400int main(int argc, char *argv[])
1401{
1402 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001403 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001404 GError *error = NULL;
1405 GOptionContext *context;
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001406 int width, height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001407 char *socket_name;
1408 int socket_name_size;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001409
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001410 g_type_init(); /* GdkPixbuf needs this, it seems. */
1411
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001412 context = g_option_context_new(NULL);
1413 g_option_context_add_main_entries(context, option_entries, "Wayland");
1414 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1415 fprintf(stderr, "option parsing failed: %s\n", error->message);
1416 exit(EXIT_FAILURE);
1417 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001418 if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) {
1419 fprintf(stderr, "invalid geometry option: %s \n",
1420 option_geometry);
1421 exit(EXIT_FAILURE);
1422 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001423
1424 display = wl_display_create();
1425
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001426 if (getenv("WAYLAND_DISPLAY"))
1427 ec = wayland_compositor_create(display, width, height);
1428 else if (getenv("DISPLAY"))
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001429 ec = x11_compositor_create(display, width, height);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001430 else
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001431 ec = drm_compositor_create(display, option_connector);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001432
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001433 if (ec == NULL) {
1434 fprintf(stderr, "failed to create compositor\n");
1435 exit(EXIT_FAILURE);
1436 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001437
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001438 socket_name_size = 1 + asprintf(&socket_name, "%c%s", '\0',
1439 option_socket_name);
1440
1441 if (wl_display_add_socket(display, socket_name, socket_name_size)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001442 fprintf(stderr, "failed to add socket: %m\n");
1443 exit(EXIT_FAILURE);
1444 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001445 free(socket_name);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001446
1447 wl_display_run(display);
1448
1449 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001450}