blob: 94e70a56b33a88278a7789a72797029001a20b71 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05008 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050017 */
18
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040019#include <stdio.h>
20#include <string.h>
21#include <stdlib.h>
22#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050023#include <stdarg.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040024#include <sys/ioctl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040025#include <fcntl.h>
26#include <unistd.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050027#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050028#include <math.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050029#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040030#include <linux/input.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050031
Kristian Høgsberga1f3f602010-08-03 09:26:44 -040032#include "wayland-server-protocol.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040033#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050034
Kristian Høgsberg61a82512010-10-26 11:26:44 -040035static const char *option_background = "background.jpg";
36static const char *option_geometry = "1024x640";
37static int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050038
39static const GOptionEntry option_entries[] = {
40 { "background", 'b', 0, G_OPTION_ARG_STRING,
41 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -040042 { "connector", 'c', 0, G_OPTION_ARG_INT,
43 &option_connector, "KMS connector" },
Kristian Høgsberg61a82512010-10-26 11:26:44 -040044 { "geometry", 'g', 0, G_OPTION_ARG_STRING,
45 &option_geometry, "Geometry" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050046 { NULL }
47};
48
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050049static void
Kristian Høgsberg1db21f12010-08-16 16:08:12 -040050wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
51 struct wlsc_surface *surface,
52 uint32_t time,
53 int32_t x, int32_t y,
54 int32_t sx, int32_t sy);
55
56static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050057wlsc_matrix_init(struct wlsc_matrix *matrix)
58{
59 static const struct wlsc_matrix identity = {
60 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
61 };
62
63 memcpy(matrix, &identity, sizeof identity);
64}
65
66static void
67wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
68{
69 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040070 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050071 div_t d;
72 int i, j;
73
74 for (i = 0; i < 16; i++) {
75 tmp.d[i] = 0;
76 d = div(i, 4);
77 row = m->d + d.quot * 4;
78 column = n->d + d.rem;
79 for (j = 0; j < 4; j++)
80 tmp.d[i] += row[j] * column[j * 4];
81 }
82 memcpy(m, &tmp, sizeof tmp);
83}
84
85static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040086wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050087{
Kristian Høgsberg27803c62010-06-06 22:23:21 -040088 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050089 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
90 };
91
92 wlsc_matrix_multiply(matrix, &translate);
93}
94
95static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040096wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050097{
98 struct wlsc_matrix scale = {
99 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
100 };
101
102 wlsc_matrix_multiply(matrix, &scale);
103}
104
105static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400106wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
107{
108 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400109 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400110
111 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400112 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400113 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400114 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400115 }
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400116
117 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400118}
119
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400120static struct wlsc_surface *
121wlsc_surface_create(struct wlsc_compositor *compositor,
122 struct wl_visual *visual,
123 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500124{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400125 struct wlsc_surface *surface;
126
127 surface = malloc(sizeof *surface);
128 if (surface == NULL)
129 return NULL;
130
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500131 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400132 glBindTexture(GL_TEXTURE_2D, surface->texture);
133 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
134 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
135 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
136 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
137
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500138 surface->compositor = compositor;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500139 surface->visual = visual;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400140 surface->x = x;
141 surface->y = y;
142 surface->width = width;
143 surface->height = height;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400144 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400145 wlsc_matrix_scale(&surface->matrix, width, height, 1);
146 wlsc_matrix_translate(&surface->matrix, x, y, 0);
147
148 wlsc_matrix_init(&surface->matrix_inv);
149 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
150 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500151
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400152 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500153}
154
155static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400156destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500157{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400158 struct wlsc_surface *surface =
159 container_of(resource, struct wlsc_surface, base.base);
160 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500161 struct wlsc_listener *l;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400162
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400163 wl_list_remove(&surface->link);
164 glDeleteTextures(1, &surface->texture);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400165
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500166 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400167 l->func(l, surface);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400168
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400169 free(surface);
Kristian Høgsberg117d5132010-08-11 08:56:47 -0400170
171 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500172}
173
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400174static int
175texture_from_png(const char *filename, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500176{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400177 GdkPixbuf *pixbuf;
178 GError *error = NULL;
179 void *data;
180 GLenum format;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500181
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400182 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
183 width, height,
184 FALSE, &error);
185 if (error != NULL)
186 return -1;
187
188 data = gdk_pixbuf_get_pixels(pixbuf);
189
190 if (gdk_pixbuf_get_has_alpha(pixbuf))
191 format = GL_RGBA;
192 else
193 format = GL_RGB;
194
Chia-I Wu1f411902010-10-29 15:20:16 +0800195 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
196 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400197
198 gdk_pixbuf_unref(pixbuf);
199
200 return 0;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500201}
202
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400203static const struct {
204 const char *filename;
205 int hotspot_x, hotspot_y;
206} pointer_images[] = {
Kristian Høgsberg4219a402010-08-16 16:43:03 -0400207 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
208 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
209 { DATADIR "/wayland/bottom_side.png", 16, 20 },
210 { DATADIR "/wayland/grabbing.png", 20, 17 },
211 { DATADIR "/wayland/left_ptr.png", 10, 5 },
212 { DATADIR "/wayland/left_side.png", 10, 20 },
213 { DATADIR "/wayland/right_side.png", 30, 19 },
214 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
215 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
216 { DATADIR "/wayland/top_side.png", 18, 8 },
217 { DATADIR "/wayland/xterm.png", 15, 15 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400218};
219
220static void
221create_pointer_images(struct wlsc_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500222{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400223 int i, count;
224 GLuint texture;
225 const int width = 32, height = 32;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400226
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400227 glGenTextures(1, &texture);
228 glBindTexture(GL_TEXTURE_2D, texture);
229 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400233
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400234 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400235 ec->pointer_buffers = malloc(count * sizeof *ec->pointer_buffers);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400236 for (i = 0; i < count; i++) {
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400237 ec->pointer_buffers[i] =
238 wlsc_drm_buffer_create(ec, width, height,
239 &ec->argb_visual);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400240 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400241 ec->pointer_buffers[i]->image);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400242 texture_from_png(pointer_images[i].filename, width, height);
243 }
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400244}
245
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500246static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500247background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500248{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500249 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500250 GdkPixbuf *pixbuf;
251 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500252 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500253 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500254
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400255 background = wlsc_surface_create(output->compositor,
256 &output->compositor->rgb_visual,
257 output->x, output->y,
258 output->width, output->height);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500259 if (background == NULL)
260 return NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500261
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500262 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500263 output->width,
264 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500265 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500266 if (error != NULL) {
267 free(background);
268 return NULL;
269 }
270
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500271 data = gdk_pixbuf_get_pixels(pixbuf);
272
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400273 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500274 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500275 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500276 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500277
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500278 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
279 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500280 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500281
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500282 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500283}
284
285static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400286wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500287{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500288 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400289 struct wlsc_matrix tmp;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500290
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400291 tmp = es->matrix;
292 wlsc_matrix_multiply(&tmp, &output->matrix);
293 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
294 glUniform1i(ec->tex_uniform, 0);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500295
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500296 if (es->visual == &ec->argb_visual) {
297 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
298 glEnable(GL_BLEND);
299 } else if (es->visual == &ec->premultiplied_argb_visual) {
300 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
301 glEnable(GL_BLEND);
302 } else {
303 glDisable(GL_BLEND);
304 }
305
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500306 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400307 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
308 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
309 5 * sizeof(GLfloat), NULL);
310 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
311 5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
312 glEnableVertexAttribArray(0);
313 glEnableVertexAttribArray(1);
314 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500315}
316
317static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400318wlsc_surface_raise(struct wlsc_surface *surface)
319{
320 struct wlsc_compositor *compositor = surface->compositor;
321
322 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400323 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400324}
325
326static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400327wlsc_surface_update_matrix(struct wlsc_surface *es)
328{
329 wlsc_matrix_init(&es->matrix);
330 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
331 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
332
333 wlsc_matrix_init(&es->matrix_inv);
334 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
335 wlsc_matrix_scale(&es->matrix_inv,
336 1.0 / es->width, 1.0 / es->height, 1);
337}
338
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400339void
340wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400341{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400342 wl_display_post_frame(compositor->wl_display, msecs);
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400343 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400344}
345
346static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400347wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400348{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500349 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500350 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500351 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500352
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500353 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500354
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500355 if (output->background)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400356 wlsc_surface_draw(output->background, output);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500357 else
358 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400359
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400360 wl_list_for_each_reverse(es, &ec->surface_list, link)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400361 wlsc_surface_draw(es, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400362
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400363 if (ec->focus)
364 wl_list_for_each(eid, &ec->input_device_list, link)
365 wlsc_surface_draw(eid->sprite, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500366}
367
368static void
369repaint(void *data)
370{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500371 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500372 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500373
374 if (!ec->repaint_needed) {
375 ec->repaint_on_timeout = 0;
376 return;
377 }
378
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500379 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400380 wlsc_output_repaint(output);
381
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400382 ec->present(ec);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500383
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500384 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400385}
386
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400387void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400388wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400389{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400390 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400391 if (compositor->repaint_on_timeout)
392 return;
393
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400394 wl_event_source_timer_update(compositor->timer_source, 1);
395 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400396}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500397
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500398static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500399surface_destroy(struct wl_client *client,
400 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400401{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400402 wl_resource_destroy(&surface->base, client);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400403}
404
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500405static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500406surface_attach(struct wl_client *client,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400407 struct wl_surface *surface, struct wl_buffer *buffer)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400408{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500409 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400410
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400411 buffer->attach(buffer, surface);
412 es->buffer = buffer;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400413}
414
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500415static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500416surface_map(struct wl_client *client,
417 struct wl_surface *surface,
418 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400419{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500420 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400421
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400422 es->x = x;
423 es->y = y;
424 es->width = width;
425 es->height = height;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400426
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400427 wlsc_surface_update_matrix(es);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400428 wlsc_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400429}
430
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500431static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500432surface_damage(struct wl_client *client,
433 struct wl_surface *surface,
434 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500435{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400436 struct wlsc_surface *es = (struct wlsc_surface *) surface;
437
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400438 es->buffer->damage(es->buffer, surface, x, y, width, height);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400439 wlsc_compositor_schedule_repaint(es->compositor);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500440}
441
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500442const static struct wl_surface_interface surface_interface = {
443 surface_destroy,
444 surface_attach,
445 surface_map,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500446 surface_damage
447};
448
449static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400450wlsc_input_device_attach(struct wlsc_input_device *device,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400451 struct wl_buffer *buffer, int x, int y)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400452{
453 struct wlsc_compositor *ec = device->ec;
454
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400455 buffer->attach(buffer, &device->sprite->base);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400456 device->hotspot_x = x;
457 device->hotspot_y = y;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400458
459 device->sprite->x = device->x - device->hotspot_x;
460 device->sprite->y = device->y - device->hotspot_y;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400461 device->sprite->width = buffer->width;
462 device->sprite->height = buffer->height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400463 wlsc_surface_update_matrix(device->sprite);
464
465 wlsc_compositor_schedule_repaint(ec);
466}
467
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400468
469static void
470wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
471 enum wlsc_pointer_type type)
472{
473 struct wlsc_compositor *compositor = device->ec;
474
475 wlsc_input_device_attach(device,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400476 &compositor->pointer_buffers[type]->base,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400477 pointer_images[type].hotspot_x,
478 pointer_images[type].hotspot_y);
479}
480
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400481static void
482wlsc_input_device_start_grab(struct wlsc_input_device *device,
483 uint32_t time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400484 enum wlsc_grab_type grab)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400485{
486 device->grab = grab;
487 device->grab_surface = device->pointer_focus;
488 device->grab_dx = device->pointer_focus->x - device->grab_x;
489 device->grab_dy = device->pointer_focus->y - device->grab_y;
490 device->grab_width = device->pointer_focus->width;
491 device->grab_height = device->pointer_focus->height;
492
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400493 wlsc_input_device_set_pointer_focus(device,
494 (struct wlsc_surface *) &wl_grab_surface,
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400495 time, 0, 0, 0, 0);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400496}
497
498static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400499shell_move(struct wl_client *client, struct wl_shell *shell,
500 struct wl_surface *surface,
501 struct wl_input_device *device, uint32_t time)
502{
503 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
504
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400505 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
506 wd->grab_time != time ||
507 &wd->pointer_focus->base != surface)
508 return;
509
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400510 wlsc_input_device_start_grab(wd, time, WLSC_DEVICE_GRAB_MOVE);
511 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400512}
513
514static void
515shell_resize(struct wl_client *client, struct wl_shell *shell,
516 struct wl_surface *surface,
517 struct wl_input_device *device, uint32_t time, uint32_t edges)
518{
519 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400520 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400521
522 if (edges == 0 || edges > 15 ||
523 (edges & 3) == 3 || (edges & 12) == 12)
524 return;
525
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400526 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
527 wd->grab_time != time ||
528 &wd->pointer_focus->base != surface)
529 return;
530
531 switch (edges) {
532 case WLSC_DEVICE_GRAB_RESIZE_TOP:
533 pointer = WLSC_POINTER_TOP;
534 break;
535 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
536 pointer = WLSC_POINTER_BOTTOM;
537 break;
538 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
539 pointer = WLSC_POINTER_LEFT;
540 break;
541 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
542 pointer = WLSC_POINTER_TOP_LEFT;
543 break;
544 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
545 pointer = WLSC_POINTER_BOTTOM_LEFT;
546 break;
547 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
548 pointer = WLSC_POINTER_RIGHT;
549 break;
550 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
551 pointer = WLSC_POINTER_TOP_RIGHT;
552 break;
553 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
554 pointer = WLSC_POINTER_BOTTOM_RIGHT;
555 break;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400556 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400557
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400558 wlsc_input_device_start_grab(wd, time, edges);
559 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400560}
561
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400562static void
563destroy_drag(struct wl_resource *resource, struct wl_client *client)
564{
565 struct wl_drag *drag =
566 container_of(resource, struct wl_drag, resource);
567
568 /* FIXME: More stuff */
569
570 free(drag);
571}
572
573const static struct wl_drag_interface drag_interface;
574
575static void
576shell_create_drag(struct wl_client *client,
577 struct wl_shell *shell, uint32_t id)
578{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400579 struct wl_drag *drag;
580
581 drag = malloc(sizeof *drag);
582 if (drag == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400583 wl_client_post_no_memory(client);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400584 return;
585 }
586
587 memset(drag, 0, sizeof *drag);
588 drag->resource.base.id = id;
589 drag->resource.base.interface = &wl_drag_interface;
590 drag->resource.base.implementation =
591 (void (**)(void)) &drag_interface;
592
593 drag->resource.destroy = destroy_drag;
594
595 wl_client_add_resource(client, &drag->resource);
596}
597
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400598const static struct wl_shell_interface shell_interface = {
599 shell_move,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400600 shell_resize,
601 shell_create_drag
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400602};
603
604static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500605compositor_create_surface(struct wl_client *client,
606 struct wl_compositor *compositor, uint32_t id)
607{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500608 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400609 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500610
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400611 surface = wlsc_surface_create(ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400612 if (surface == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400613 wl_client_post_no_memory(client);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500614 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400615 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500616
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400617 wl_list_insert(ec->surface_list.prev, &surface->link);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400618 surface->base.base.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400619
620 surface->base.base.base.id = id;
621 surface->base.base.base.interface = &wl_surface_interface;
622 surface->base.base.base.implementation =
623 (void (**)(void)) &surface_interface;
624 surface->base.client = client;
625
626 wl_client_add_resource(client, &surface->base.base);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500627}
628
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500629const static struct wl_compositor_interface compositor_interface = {
630 compositor_create_surface,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500631};
632
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500633static void
634wlsc_surface_transform(struct wlsc_surface *surface,
635 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
636{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400637 struct wlsc_vector v = { { x, y, 0, 1 } };
638
639 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400640 *sx = v.f[0] * surface->width;
641 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500642}
643
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500644static void
645wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400646 struct wlsc_surface *surface,
647 uint32_t time)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500648{
649 if (device->keyboard_focus == surface)
650 return;
651
652 if (device->keyboard_focus &&
653 (!surface || device->keyboard_focus->base.client != surface->base.client))
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400654 wl_client_post_event(device->keyboard_focus->base.client,
655 &device->base.base,
656 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
657 time, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500658
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500659 if (surface)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400660 wl_client_post_event(surface->base.client,
661 &device->base.base,
662 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
663 time, &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500664
665 device->keyboard_focus = surface;
666}
667
668static void
669wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400670 struct wlsc_surface *surface,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400671 uint32_t time,
672 int32_t x, int32_t y,
673 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500674{
675 if (device->pointer_focus == surface)
676 return;
677
678 if (device->pointer_focus &&
679 (!surface || device->pointer_focus->base.client != surface->base.client))
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400680 wl_client_post_event(device->pointer_focus->base.client,
681 &device->base.base,
682 WL_INPUT_DEVICE_POINTER_FOCUS,
683 time, NULL, 0, 0, 0, 0);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500684 if (surface)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400685 wl_client_post_event(surface->base.client,
686 &device->base.base,
687 WL_INPUT_DEVICE_POINTER_FOCUS,
688 time, &surface->base,
689 x, y, sx, sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500690
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400691 if (!surface)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400692 wlsc_input_device_set_pointer_image(device,
693 WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400694
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500695 device->pointer_focus = surface;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400696 device->pointer_focus_time = time;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500697}
698
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500699static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500700pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500701{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500702 struct wlsc_compositor *ec = device->ec;
703 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500704
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400705 wl_list_for_each(es, &ec->surface_list, link) {
706 wlsc_surface_transform(es, device->x, device->y, sx, sy);
707 if (0 <= *sx && *sx < es->width &&
708 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500709 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400710 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500711
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500712 return NULL;
713}
714
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400715static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400716wl_drag_set_pointer_focus(struct wl_drag *drag,
717 struct wlsc_surface *surface, uint32_t time,
718 int32_t x, int32_t y, int32_t sx, int32_t sy);
719
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500720void
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400721notify_motion(struct wlsc_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500722{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500723 struct wlsc_surface *es;
724 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500725 struct wlsc_output *output;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400726 int32_t sx, sy, width, height;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500727
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500728 /* FIXME: We need some multi head love here. */
729 output = container_of(ec->output_list.next, struct wlsc_output, link);
730 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500731 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500732 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500733 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500734 if (x >= output->x + output->width)
735 x = output->x + output->width - 1;
736 if (y >= output->y + output->height)
737 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500738
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500739 device->x = x;
740 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500741
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400742 switch (device->grab) {
743 case WLSC_DEVICE_GRAB_NONE:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400744 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400745 wlsc_input_device_set_pointer_focus(device, es,
746 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400747 if (es)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400748 wl_client_post_event(es->base.client,
749 &device->base.base,
750 WL_INPUT_DEVICE_MOTION,
751 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400752 break;
753
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400754 case WLSC_DEVICE_GRAB_MOTION:
755 es = device->pointer_focus;
756 wlsc_surface_transform(es, x, y, &sx, &sy);
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400757 wl_client_post_event(es->base.client,
758 &device->base.base,
759 WL_INPUT_DEVICE_MOTION,
760 time, x, y, sx, sy);
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400761 break;
762
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400763 case WLSC_DEVICE_GRAB_MOVE:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400764 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400765 es->x = x + device->grab_dx;
766 es->y = y + device->grab_dy;;
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400767 wl_client_post_event(es->base.client,
768 &ec->shell.base,
769 WL_SHELL_CONFIGURE,
770 time, device->grab,
771 &es->base, es->x, es->y,
772 es->width, es->height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400773
774 wlsc_surface_update_matrix(es);
775
776 break;
777
778 case WLSC_DEVICE_GRAB_RESIZE_TOP:
779 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
780 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
781 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
782 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
783 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
784 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
785 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
786 case WLSC_DEVICE_GRAB_RESIZE_MASK:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400787 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400788
789 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_LEFT) {
790 sx = x + device->grab_dx;
791 width = device->grab_x - x + device->grab_width;
792 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_RIGHT) {
793 sx = device->grab_x + device->grab_dx;
794 width = x - device->grab_x + device->grab_width;
795 } else {
796 sx = device->grab_x + device->grab_dx;
797 width = device->grab_width;
798 }
799
800 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_TOP) {
801 sy = y + device->grab_dy;
802 height = device->grab_y - y + device->grab_height;
803 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_BOTTOM) {
804 sy = device->grab_y + device->grab_dy;
805 height = y - device->grab_y + device->grab_height;
806 } else {
807 sy = device->grab_y + device->grab_dy;
808 height = device->grab_height;
809 }
810
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400811 wl_client_post_event(es->base.client,
812 &ec->shell.base,
813 WL_SHELL_CONFIGURE, time, device->grab,
814 &es->base, sx, sy, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400815 break;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400816
817 case WLSC_DEVICE_GRAB_DRAG:
818 es = pick_surface(device, &sx, &sy);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400819 wl_drag_set_pointer_focus(device->drag,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400820 es, time, x, y, sx, sy);
821 if (es)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400822 wl_client_post_event(es->base.client,
823 &device->drag->drag_offer.base,
824 WL_DRAG_OFFER_MOTION,
825 time, x, y, sx, sy);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400826 break;
827
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400828 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500829
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400830 device->sprite->x = device->x - device->hotspot_x;
831 device->sprite->y = device->y - device->hotspot_y;
832 wlsc_surface_update_matrix(device->sprite);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500833
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400834 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500835}
836
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400837static void
838wlsc_input_device_end_grab(struct wlsc_input_device *device, uint32_t time)
839{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400840 struct wl_drag *drag = device->drag;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400841 struct wlsc_surface *es;
842 int32_t sx, sy;
843
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400844 switch (device->grab) {
845 case WLSC_DEVICE_GRAB_DRAG:
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400846 if (drag->target)
847 wl_client_post_event(drag->target,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400848 &drag->drag_offer.base,
849 WL_DRAG_OFFER_DROP);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400850 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400851 device->drag = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400852 break;
853 default:
854 break;
855 }
856
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400857 device->grab = WLSC_DEVICE_GRAB_NONE;
858 es = pick_surface(device, &sx, &sy);
859 wlsc_input_device_set_pointer_focus(device, es, time,
860 device->x, device->y, sx, sy);
861}
862
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500863void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500864notify_button(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400865 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500866{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400867 struct wlsc_surface *surface;
868 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500869
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400870 surface = device->pointer_focus;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400871 if (surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400872 if (state && device->grab == WLSC_DEVICE_GRAB_NONE) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400873 wlsc_surface_raise(surface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400874 device->grab = WLSC_DEVICE_GRAB_MOTION;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400875 device->grab_button = button;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400876 device->grab_time = time;
877 device->grab_x = device->x;
878 device->grab_y = device->y;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400879 wlsc_input_device_set_keyboard_focus(device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400880 surface, time);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500881 }
882
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400883 if (state && button == BTN_LEFT &&
884 device->grab == WLSC_DEVICE_GRAB_MOTION &&
885 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400886 shell_move(NULL, (struct wl_shell *) &compositor->shell,
887 &surface->base, &device->base, time);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400888 else if (state && button == BTN_MIDDLE &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400889 device->grab == WLSC_DEVICE_GRAB_MOTION &&
890 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400891 shell_resize(NULL, (struct wl_shell *) &compositor->shell,
892
893 &surface->base, &device->base, time,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400894 WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400895 else if (device->grab == WLSC_DEVICE_GRAB_NONE ||
896 device->grab == WLSC_DEVICE_GRAB_MOTION)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400897 wl_client_post_event(surface->base.client,
898 &device->base.base,
899 WL_INPUT_DEVICE_BUTTON,
900 time, button, state);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500901
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400902 if (!state &&
903 device->grab != WLSC_DEVICE_GRAB_NONE &&
904 device->grab_button == button) {
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400905 wlsc_input_device_end_grab(device, time);
906 }
907
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400908 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500909 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500910}
911
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500912void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500913notify_key(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400914 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500915{
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500916 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400917 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500918
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400919 switch (key | device->modifier_state) {
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400920 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400921 kill(0, SIGTERM);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500922 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500923 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500924
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400925 switch (key) {
926 case KEY_LEFTCTRL:
927 case KEY_RIGHTCTRL:
928 modifier = MODIFIER_CTRL;
929 break;
930
931 case KEY_LEFTALT:
932 case KEY_RIGHTALT:
933 modifier = MODIFIER_ALT;
934 break;
935
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400936 case KEY_LEFTMETA:
937 case KEY_RIGHTMETA:
938 modifier = MODIFIER_SUPER;
939 break;
940
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400941 default:
942 modifier = 0;
943 break;
944 }
945
946 if (state)
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400947 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400948 else
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400949 device->modifier_state &= ~modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400950
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500951 end = device->keys.data + device->keys.size;
952 for (k = device->keys.data; k < end; k++) {
953 if (*k == key)
954 *k = *--end;
955 }
956 device->keys.size = (void *) end - device->keys.data;
957 if (state) {
958 k = wl_array_add(&device->keys, sizeof *k);
959 *k = key;
960 }
Ray Strodee96dcb82008-12-20 02:00:49 -0500961
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500962 if (device->keyboard_focus != NULL)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400963 wl_client_post_event(device->keyboard_focus->base.client,
964 &device->base.base,
965 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400966}
967
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400968static void
969input_device_attach(struct wl_client *client,
970 struct wl_input_device *device_base,
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400971 uint32_t time,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400972 struct wl_buffer *buffer, int32_t x, int32_t y)
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400973{
974 struct wlsc_input_device *device =
975 (struct wlsc_input_device *) device_base;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400976
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400977 if (time < device->pointer_focus_time)
978 return;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400979 if (device->pointer_focus == NULL)
980 return;
981 if (device->pointer_focus->base.client != client &&
982 !(&device->pointer_focus->base == &wl_grab_surface &&
983 device->grab_surface->base.client == client))
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400984 return;
985
Kristian Høgsbergf4cb2012010-08-16 17:46:25 -0400986 if (buffer == NULL) {
987 wlsc_input_device_set_pointer_image(device,
988 WLSC_POINTER_LEFT_PTR);
989 return;
990 }
991
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400992 wlsc_input_device_attach(device, buffer, x, y);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400993}
994
995const static struct wl_input_device_interface input_device_interface = {
996 input_device_attach,
997};
998
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400999static uint32_t
1000get_time(void)
1001{
1002 struct timeval tv;
1003
1004 gettimeofday(&tv, NULL);
1005
1006 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001007}
1008
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001009static void
1010handle_surface_destroy(struct wlsc_listener *listener,
1011 struct wlsc_surface *surface)
1012{
1013 struct wlsc_input_device *device =
1014 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001015 uint32_t time = get_time();
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001016
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001017 if (device->keyboard_focus == surface)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001018 wlsc_input_device_set_keyboard_focus(device, NULL, time);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001019 if (device->pointer_focus == surface ||
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001020 (&device->pointer_focus->base == &wl_grab_surface &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001021 device->grab_surface == surface))
1022 wlsc_input_device_end_grab(device, time);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001023}
1024
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001025static void
1026wl_drag_set_pointer_focus(struct wl_drag *drag,
1027 struct wlsc_surface *surface, uint32_t time,
1028 int32_t x, int32_t y, int32_t sx, int32_t sy)
1029{
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001030 char **p, **end;
1031
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001032 if (drag->pointer_focus == &surface->base)
1033 return;
1034
1035 if (drag->pointer_focus &&
1036 (!surface || drag->pointer_focus->client != surface->base.client))
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001037 wl_client_post_event(drag->pointer_focus->client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001038 &drag->drag_offer.base,
1039 WL_DRAG_OFFER_POINTER_FOCUS,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001040 time, NULL, 0, 0, 0, 0);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001041
1042 if (surface &&
1043 (!drag->pointer_focus ||
1044 drag->pointer_focus->client != surface->base.client)) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001045 wl_client_post_global(surface->base.client,
1046 &drag->drag_offer.base);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001047
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001048 end = drag->types.data + drag->types.size;
1049 for (p = drag->types.data; p < end; p++)
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001050 wl_client_post_event(surface->base.client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001051 &drag->drag_offer.base,
1052 WL_DRAG_OFFER_OFFER, *p);
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001053 }
1054
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001055 if (surface) {
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001056 wl_client_post_event(surface->base.client,
1057 &drag->drag_offer.base,
1058 WL_DRAG_OFFER_POINTER_FOCUS,
1059 time, &surface->base,
1060 x, y, sx, sy);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001061
1062 }
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001063
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001064 drag->pointer_focus = &surface->base;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001065 drag->pointer_focus_time = time;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001066 drag->target = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001067}
1068
1069static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001070drag_offer_accept(struct wl_client *client,
1071 struct wl_drag_offer *offer, uint32_t time, const char *type)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001072{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001073 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001074 char **p, **end;
1075
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001076 /* If the client responds to drag pointer_focus or motion
1077 * events after the pointer has left the surface, we just
1078 * discard the accept requests. The drag source just won't
1079 * get the corresponding 'target' events and eventually the
1080 * next surface/root will start sending events. */
1081 if (time < drag->pointer_focus_time)
1082 return;
1083
1084 drag->target = client;
1085 drag->type = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001086 end = drag->types.data + drag->types.size;
1087 for (p = drag->types.data; p < end; p++)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001088 if (type && strcmp(*p, type) == 0)
1089 drag->type = *p;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001090
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001091 wl_client_post_event(drag->source->client, &drag->resource.base,
1092 WL_DRAG_TARGET, drag->type);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001093}
1094
1095static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001096drag_offer_receive(struct wl_client *client,
1097 struct wl_drag_offer *offer, int fd)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001098{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001099 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001100
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001101 wl_client_post_event(drag->source->client, &drag->resource.base,
1102 WL_DRAG_FINISH, fd);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001103 close(fd);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001104}
1105
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001106static const struct wl_drag_offer_interface drag_offer_interface = {
1107 drag_offer_accept,
1108 drag_offer_receive
1109};
1110
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001111static void
1112drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
1113{
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001114 char **p;
1115
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001116 p = wl_array_add(&drag->types, sizeof *p);
1117 if (p)
1118 *p = strdup(type);
1119 if (!p || !*p)
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001120 wl_client_post_no_memory(client);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001121}
1122
1123static void
1124drag_activate(struct wl_client *client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001125 struct wl_drag *drag,
1126 struct wl_surface *surface,
1127 struct wl_input_device *input_device, uint32_t time)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001128{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001129 struct wl_display *display = wl_client_get_display (client);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001130 struct wlsc_input_device *device =
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001131 (struct wlsc_input_device *) input_device;
1132 struct wlsc_surface *target;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001133 int32_t sx, sy;
1134
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001135 if (device->grab != WLSC_DEVICE_GRAB_MOTION ||
1136 &device->pointer_focus->base != surface ||
1137 device->grab_time != time)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001138 return;
1139
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001140 drag->source = surface;
1141 drag->input_device = input_device;
1142
1143 drag->drag_offer.base.interface = &wl_drag_offer_interface;
1144 drag->drag_offer.base.implementation =
1145 (void (**)(void)) &drag_offer_interface;
1146
1147 wl_display_add_object(display, &drag->drag_offer.base);
1148
1149 wlsc_input_device_start_grab(device, time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001150 WLSC_DEVICE_GRAB_DRAG);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001151
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001152 device->drag = drag;
1153 target = pick_surface(device, &sx, &sy);
1154 wl_drag_set_pointer_focus(device->drag, target, time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001155 device->x, device->y, sx, sy);
1156}
1157
1158static void
1159drag_cancel(struct wl_client *client, struct wl_drag *drag)
1160{
1161 struct wlsc_input_device *device =
1162 (struct wlsc_input_device *) drag->input_device;
1163
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001164 if (drag->source == NULL || drag->source->client != client)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001165 return;
1166
1167 wlsc_input_device_end_grab(device, get_time());
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001168 device->drag = NULL;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001169}
1170
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001171static const struct wl_drag_interface drag_interface = {
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001172 drag_offer,
1173 drag_activate,
1174 drag_cancel,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001175};
1176
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001177void
1178wlsc_input_device_init(struct wlsc_input_device *device,
1179 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001180{
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001181 device->base.base.interface = &wl_input_device_interface;
1182 device->base.base.implementation =
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001183 (void (**)(void)) &input_device_interface;
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001184 wl_display_add_object(ec->wl_display, &device->base.base);
1185 wl_display_add_global(ec->wl_display, &device->base.base, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001186
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001187 device->x = 100;
1188 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001189 device->ec = ec;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001190 device->sprite = wlsc_surface_create(ec, &ec->argb_visual,
1191 device->x, device->y, 32, 32);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001192 device->hotspot_x = 16;
1193 device->hotspot_y = 16;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001194
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001195 device->listener.func = handle_surface_destroy;
1196 wl_list_insert(ec->surface_destroy_listener_list.prev,
1197 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001198 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001199
1200 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001201}
1202
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001203static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001204wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001205{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001206 struct wlsc_output *output =
1207 container_of(global, struct wlsc_output, base);
1208
1209 wl_client_post_event(client, global,
1210 WL_OUTPUT_GEOMETRY,
1211 output->width, output->height);
1212}
1213
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001214static const char vertex_shader[] =
1215 "uniform mat4 proj;\n"
1216 "attribute vec4 position;\n"
1217 "attribute vec2 texcoord;\n"
1218 "varying vec2 v_texcoord;\n"
1219 "void main()\n"
1220 "{\n"
1221 " gl_Position = proj * position;\n"
1222 " v_texcoord = texcoord;\n"
1223 "}\n";
1224
1225static const char fragment_shader[] =
1226 /* "precision mediump float;\n" */
1227 "varying vec2 v_texcoord;\n"
1228 "uniform sampler2D tex;\n"
1229 "void main()\n"
1230 "{\n"
1231 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1232 "}\n";
1233
Kristian Høgsberga9468212010-06-14 21:03:11 -04001234static int
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001235init_shaders(struct wlsc_compositor *ec)
1236{
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001237 GLuint v, f, program;
1238 const char *p;
1239 char msg[512];
1240 GLfloat vertices[4 * 5];
1241 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001242
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001243 p = vertex_shader;
1244 v = glCreateShader(GL_VERTEX_SHADER);
1245 glShaderSource(v, 1, &p, NULL);
1246 glCompileShader(v);
1247 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
1248 if (!status) {
1249 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
1250 fprintf(stderr, "vertex shader info: %s\n", msg);
1251 return -1;
1252 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001253
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001254 p = fragment_shader;
1255 f = glCreateShader(GL_FRAGMENT_SHADER);
1256 glShaderSource(f, 1, &p, NULL);
1257 glCompileShader(f);
1258 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
1259 if (!status) {
1260 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
1261 fprintf(stderr, "fragment shader info: %s\n", msg);
1262 return -1;
1263 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001264
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001265 program = glCreateProgram();
1266 glAttachShader(program, v);
1267 glAttachShader(program, f);
1268 glBindAttribLocation(program, 0, "position");
1269 glBindAttribLocation(program, 1, "texcoord");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001270
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001271 glLinkProgram(program);
1272 glGetProgramiv(program, GL_LINK_STATUS, &status);
1273 if (!status) {
1274 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1275 fprintf(stderr, "link info: %s\n", msg);
1276 return -1;
1277 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001278
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001279 glUseProgram(program);
1280 ec->proj_uniform = glGetUniformLocation(program, "proj");
1281 ec->tex_uniform = glGetUniformLocation(program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001282
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001283 vertices[ 0] = 0.0;
1284 vertices[ 1] = 0.0;
1285 vertices[ 2] = 0.0;
1286 vertices[ 3] = 0.0;
1287 vertices[ 4] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001288
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001289 vertices[ 5] = 0.0;
1290 vertices[ 6] = 1.0;
1291 vertices[ 7] = 0.0;
1292 vertices[ 8] = 0.0;
1293 vertices[ 9] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001294
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001295 vertices[10] = 1.0;
1296 vertices[11] = 0.0;
1297 vertices[12] = 0.0;
1298 vertices[13] = 1.0;
1299 vertices[14] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001300
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001301 vertices[15] = 1.0;
1302 vertices[16] = 1.0;
1303 vertices[17] = 0.0;
1304 vertices[18] = 1.0;
1305 vertices[19] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001306
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001307 glGenBuffers(1, &ec->vbo);
1308 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
1309 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001310
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001311 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001312}
1313
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001314static const struct wl_interface visual_interface = {
1315 "visual", 1,
1316};
1317
1318static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001319add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001320{
1321 ec->argb_visual.base.interface = &visual_interface;
1322 ec->argb_visual.base.implementation = NULL;
1323 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001324 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001325
1326 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1327 ec->premultiplied_argb_visual.base.implementation = NULL;
1328 wl_display_add_object(ec->wl_display,
1329 &ec->premultiplied_argb_visual.base);
1330 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001331 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001332
1333 ec->rgb_visual.base.interface = &visual_interface;
1334 ec->rgb_visual.base.implementation = NULL;
1335 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001336 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1337}
1338
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001339void
1340wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1341 int x, int y, int width, int height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001342{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001343 output->compositor = c;
1344 output->x = x;
1345 output->y = y;
1346 output->width = width;
1347 output->height = height;
1348
1349 output->background =
1350 background_create(output, option_background);
1351
1352 wlsc_matrix_init(&output->matrix);
1353 wlsc_matrix_translate(&output->matrix,
1354 -output->x - output->width / 2.0,
1355 -output->y - output->height / 2.0, 0);
1356 wlsc_matrix_scale(&output->matrix,
1357 2.0 / output->width, 2.0 / output->height, 1);
1358
1359 output->base.interface = &wl_output_interface;
1360 wl_display_add_object(c->wl_display, &output->base);
1361 wl_display_add_global(c->wl_display, &output->base,
1362 wlsc_output_post_geometry);
1363}
1364
1365int
1366wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1367{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001368 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001369
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001370 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001371
Kristian Høgsbergf8ffded2010-09-03 15:15:33 -04001372 ec->base.base.interface = &wl_compositor_interface;
1373 ec->base.base.implementation =
1374 (void (**)(void)) &compositor_interface;
1375
1376 wl_display_add_object(display, &ec->base.base);
1377 if (wl_display_add_global(display, &ec->base.base, NULL))
1378 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001379
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001380 wlsc_shm_init(ec);
1381
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001382 ec->shell.base.interface = &wl_shell_interface;
1383 ec->shell.base.implementation = (void (**)(void)) &shell_interface;
1384 wl_display_add_object(display, &ec->shell.base);
1385 if (wl_display_add_global(display, &ec->shell.base, NULL))
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001386 return -1;
1387
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001388 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001389
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001390 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001391 wl_list_init(&ec->input_device_list);
1392 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001393 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001394
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001395 create_pointer_images(ec);
1396
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001397 screenshooter_create(ec);
1398
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001399 glGenFramebuffers(1, &ec->fbo);
1400 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1401 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001402 if (init_shaders(ec) < 0)
1403 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001404
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001405 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001406 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001407 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001408
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001409 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001410}
1411
1412/* The plan here is to generate a random anonymous socket name and
1413 * advertise that through a service on the session dbus.
1414 */
1415static const char socket_name[] = "\0wayland";
1416
1417int main(int argc, char *argv[])
1418{
1419 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001420 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001421 GError *error = NULL;
1422 GOptionContext *context;
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001423 int width, height;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001424
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001425 g_type_init(); /* GdkPixbuf needs this, it seems. */
1426
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001427 context = g_option_context_new(NULL);
1428 g_option_context_add_main_entries(context, option_entries, "Wayland");
1429 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1430 fprintf(stderr, "option parsing failed: %s\n", error->message);
1431 exit(EXIT_FAILURE);
1432 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001433 if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) {
1434 fprintf(stderr, "invalid geometry option: %s \n",
1435 option_geometry);
1436 exit(EXIT_FAILURE);
1437 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001438
1439 display = wl_display_create();
1440
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001441 if (getenv("DISPLAY"))
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001442 ec = x11_compositor_create(display, width, height);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001443 else
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001444 ec = drm_compositor_create(display, option_connector);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001445
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001446 if (ec == NULL) {
1447 fprintf(stderr, "failed to create compositor\n");
1448 exit(EXIT_FAILURE);
1449 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001450
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001451 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001452 fprintf(stderr, "failed to add socket: %m\n");
1453 exit(EXIT_FAILURE);
1454 }
1455
1456 wl_display_run(display);
1457
1458 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001459}