blob: f993bf637e28cb68c049e6bd5e7ff39992d9b3b4 [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
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010035/* The plan here is to generate a random anonymous socket name and
36 * advertise that through a service on the session dbus.
37 */
38static const char *option_socket_name = "wayland";
39
Kristian Høgsberg61a82512010-10-26 11:26:44 -040040static const char *option_background = "background.jpg";
41static const char *option_geometry = "1024x640";
42static int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050043
44static const GOptionEntry option_entries[] = {
45 { "background", 'b', 0, G_OPTION_ARG_STRING,
46 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -040047 { "connector", 'c', 0, G_OPTION_ARG_INT,
48 &option_connector, "KMS connector" },
Kristian Høgsberg61a82512010-10-26 11:26:44 -040049 { "geometry", 'g', 0, G_OPTION_ARG_STRING,
50 &option_geometry, "Geometry" },
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010051 { "socket", 's', 0, G_OPTION_ARG_STRING,
52 &option_socket_name, "Socket Name" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050053 { NULL }
54};
55
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050056static void
Kristian Høgsberg1db21f12010-08-16 16:08:12 -040057wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
58 struct wlsc_surface *surface,
59 uint32_t time,
60 int32_t x, int32_t y,
61 int32_t sx, int32_t sy);
62
63static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050064wlsc_matrix_init(struct wlsc_matrix *matrix)
65{
66 static const struct wlsc_matrix identity = {
67 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
68 };
69
70 memcpy(matrix, &identity, sizeof identity);
71}
72
73static void
74wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
75{
76 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040077 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050078 div_t d;
79 int i, j;
80
81 for (i = 0; i < 16; i++) {
82 tmp.d[i] = 0;
83 d = div(i, 4);
84 row = m->d + d.quot * 4;
85 column = n->d + d.rem;
86 for (j = 0; j < 4; j++)
87 tmp.d[i] += row[j] * column[j * 4];
88 }
89 memcpy(m, &tmp, sizeof tmp);
90}
91
92static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040093wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050094{
Kristian Høgsberg27803c62010-06-06 22:23:21 -040095 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050096 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
97 };
98
99 wlsc_matrix_multiply(matrix, &translate);
100}
101
102static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400103wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500104{
105 struct wlsc_matrix scale = {
106 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
107 };
108
109 wlsc_matrix_multiply(matrix, &scale);
110}
111
112static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400113wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
114{
115 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400116 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400117
118 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400119 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400120 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400121 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400122 }
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400123
124 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400125}
126
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400127static struct wlsc_surface *
128wlsc_surface_create(struct wlsc_compositor *compositor,
129 struct wl_visual *visual,
130 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500131{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400132 struct wlsc_surface *surface;
133
134 surface = malloc(sizeof *surface);
135 if (surface == NULL)
136 return NULL;
137
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500138 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400139 glBindTexture(GL_TEXTURE_2D, surface->texture);
140 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
141 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
142 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
143 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
144
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500145 surface->compositor = compositor;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500146 surface->visual = visual;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400147 surface->x = x;
148 surface->y = y;
149 surface->width = width;
150 surface->height = height;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400151 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400152 wlsc_matrix_scale(&surface->matrix, width, height, 1);
153 wlsc_matrix_translate(&surface->matrix, x, y, 0);
154
155 wlsc_matrix_init(&surface->matrix_inv);
156 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
157 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500158
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400159 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500160}
161
162static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400163destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500164{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400165 struct wlsc_surface *surface =
166 container_of(resource, struct wlsc_surface, base.base);
167 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500168 struct wlsc_listener *l;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400169
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400170 wl_list_remove(&surface->link);
171 glDeleteTextures(1, &surface->texture);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400172
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500173 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400174 l->func(l, surface);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400175
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400176 free(surface);
Kristian Høgsberg117d5132010-08-11 08:56:47 -0400177
178 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500179}
180
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400181static int
182texture_from_png(const char *filename, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500183{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400184 GdkPixbuf *pixbuf;
185 GError *error = NULL;
186 void *data;
187 GLenum format;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500188
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400189 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
190 width, height,
191 FALSE, &error);
192 if (error != NULL)
193 return -1;
194
195 data = gdk_pixbuf_get_pixels(pixbuf);
196
197 if (gdk_pixbuf_get_has_alpha(pixbuf))
198 format = GL_RGBA;
199 else
200 format = GL_RGB;
201
Chia-I Wu1f411902010-10-29 15:20:16 +0800202 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
203 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400204
205 gdk_pixbuf_unref(pixbuf);
206
207 return 0;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500208}
209
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400210static const struct {
211 const char *filename;
212 int hotspot_x, hotspot_y;
213} pointer_images[] = {
Kristian Høgsberg4219a402010-08-16 16:43:03 -0400214 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
215 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
216 { DATADIR "/wayland/bottom_side.png", 16, 20 },
217 { DATADIR "/wayland/grabbing.png", 20, 17 },
218 { DATADIR "/wayland/left_ptr.png", 10, 5 },
219 { DATADIR "/wayland/left_side.png", 10, 20 },
220 { DATADIR "/wayland/right_side.png", 30, 19 },
221 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
222 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
223 { DATADIR "/wayland/top_side.png", 18, 8 },
224 { DATADIR "/wayland/xterm.png", 15, 15 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400225};
226
227static void
228create_pointer_images(struct wlsc_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500229{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400230 int i, count;
231 GLuint texture;
232 const int width = 32, height = 32;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400233
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400234 glGenTextures(1, &texture);
235 glBindTexture(GL_TEXTURE_2D, texture);
236 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
237 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
238 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
239 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400240
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400241 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400242 ec->pointer_buffers = malloc(count * sizeof *ec->pointer_buffers);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400243 for (i = 0; i < count; i++) {
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400244 ec->pointer_buffers[i] =
245 wlsc_drm_buffer_create(ec, width, height,
246 &ec->argb_visual);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400247 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400248 ec->pointer_buffers[i]->image);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400249 texture_from_png(pointer_images[i].filename, width, height);
250 }
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400251}
252
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500253static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500254background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500255{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500256 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500257 GdkPixbuf *pixbuf;
258 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500259 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500260 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500261
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400262 background = wlsc_surface_create(output->compositor,
263 &output->compositor->rgb_visual,
264 output->x, output->y,
265 output->width, output->height);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500266 if (background == NULL)
267 return NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500268
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500269 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500270 output->width,
271 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500272 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500273 if (error != NULL) {
274 free(background);
275 return NULL;
276 }
277
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500278 data = gdk_pixbuf_get_pixels(pixbuf);
279
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400280 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500281 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500282 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500283 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500284
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500285 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
286 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500287 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500288
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500289 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500290}
291
292static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400293wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500294{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500295 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400296 struct wlsc_matrix tmp;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500297
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400298 tmp = es->matrix;
299 wlsc_matrix_multiply(&tmp, &output->matrix);
300 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
301 glUniform1i(ec->tex_uniform, 0);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500302
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500303 if (es->visual == &ec->argb_visual) {
304 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
305 glEnable(GL_BLEND);
306 } else if (es->visual == &ec->premultiplied_argb_visual) {
307 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
308 glEnable(GL_BLEND);
309 } else {
310 glDisable(GL_BLEND);
311 }
312
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500313 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400314 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
315 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
316 5 * sizeof(GLfloat), NULL);
317 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
318 5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
319 glEnableVertexAttribArray(0);
320 glEnableVertexAttribArray(1);
321 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500322}
323
324static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400325wlsc_surface_raise(struct wlsc_surface *surface)
326{
327 struct wlsc_compositor *compositor = surface->compositor;
328
329 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400330 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400331}
332
333static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400334wlsc_surface_update_matrix(struct wlsc_surface *es)
335{
336 wlsc_matrix_init(&es->matrix);
337 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
338 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
339
340 wlsc_matrix_init(&es->matrix_inv);
341 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
342 wlsc_matrix_scale(&es->matrix_inv,
343 1.0 / es->width, 1.0 / es->height, 1);
344}
345
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400346void
347wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400348{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400349 wl_display_post_frame(compositor->wl_display, msecs);
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400350 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400351}
352
353static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400354wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400355{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500356 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500357 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500358 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500359
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500360 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500361
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500362 if (output->background)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400363 wlsc_surface_draw(output->background, output);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500364 else
365 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400366
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400367 wl_list_for_each_reverse(es, &ec->surface_list, link)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400368 wlsc_surface_draw(es, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400369
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400370 if (ec->focus)
371 wl_list_for_each(eid, &ec->input_device_list, link)
372 wlsc_surface_draw(eid->sprite, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500373}
374
375static void
376repaint(void *data)
377{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500378 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500379 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500380
381 if (!ec->repaint_needed) {
382 ec->repaint_on_timeout = 0;
383 return;
384 }
385
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500386 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400387 wlsc_output_repaint(output);
388
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400389 ec->present(ec);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500390
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500391 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400392}
393
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400394void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400395wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400396{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400397 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400398 if (compositor->repaint_on_timeout)
399 return;
400
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400401 wl_event_source_timer_update(compositor->timer_source, 1);
402 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400403}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500404
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500405static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500406surface_destroy(struct wl_client *client,
407 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400408{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400409 wl_resource_destroy(&surface->base, client);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400410}
411
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500412static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500413surface_attach(struct wl_client *client,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400414 struct wl_surface *surface, struct wl_buffer *buffer)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400415{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500416 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400417
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400418 buffer->attach(buffer, surface);
419 es->buffer = buffer;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400420}
421
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500422static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500423surface_map(struct wl_client *client,
424 struct wl_surface *surface,
425 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400426{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500427 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400428
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400429 es->x = x;
430 es->y = y;
431 es->width = width;
432 es->height = height;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400433
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400434 wlsc_surface_update_matrix(es);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400435 wlsc_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400436}
437
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500438static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500439surface_damage(struct wl_client *client,
440 struct wl_surface *surface,
441 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500442{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400443 struct wlsc_surface *es = (struct wlsc_surface *) surface;
444
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400445 es->buffer->damage(es->buffer, surface, x, y, width, height);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400446 wlsc_compositor_schedule_repaint(es->compositor);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500447}
448
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500449const static struct wl_surface_interface surface_interface = {
450 surface_destroy,
451 surface_attach,
452 surface_map,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500453 surface_damage
454};
455
456static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400457wlsc_input_device_attach(struct wlsc_input_device *device,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400458 struct wl_buffer *buffer, int x, int y)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400459{
460 struct wlsc_compositor *ec = device->ec;
461
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400462 buffer->attach(buffer, &device->sprite->base);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400463 device->hotspot_x = x;
464 device->hotspot_y = y;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400465
466 device->sprite->x = device->x - device->hotspot_x;
467 device->sprite->y = device->y - device->hotspot_y;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400468 device->sprite->width = buffer->width;
469 device->sprite->height = buffer->height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400470 wlsc_surface_update_matrix(device->sprite);
471
472 wlsc_compositor_schedule_repaint(ec);
473}
474
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400475
476static void
477wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
478 enum wlsc_pointer_type type)
479{
480 struct wlsc_compositor *compositor = device->ec;
481
482 wlsc_input_device_attach(device,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400483 &compositor->pointer_buffers[type]->base,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400484 pointer_images[type].hotspot_x,
485 pointer_images[type].hotspot_y);
486}
487
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400488static void
489wlsc_input_device_start_grab(struct wlsc_input_device *device,
490 uint32_t time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400491 enum wlsc_grab_type grab)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400492{
493 device->grab = grab;
494 device->grab_surface = device->pointer_focus;
495 device->grab_dx = device->pointer_focus->x - device->grab_x;
496 device->grab_dy = device->pointer_focus->y - device->grab_y;
497 device->grab_width = device->pointer_focus->width;
498 device->grab_height = device->pointer_focus->height;
499
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400500 wlsc_input_device_set_pointer_focus(device,
501 (struct wlsc_surface *) &wl_grab_surface,
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400502 time, 0, 0, 0, 0);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400503}
504
505static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400506shell_move(struct wl_client *client, struct wl_shell *shell,
507 struct wl_surface *surface,
508 struct wl_input_device *device, uint32_t time)
509{
510 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
511
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400512 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
513 wd->grab_time != time ||
514 &wd->pointer_focus->base != surface)
515 return;
516
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400517 wlsc_input_device_start_grab(wd, time, WLSC_DEVICE_GRAB_MOVE);
518 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400519}
520
521static void
522shell_resize(struct wl_client *client, struct wl_shell *shell,
523 struct wl_surface *surface,
524 struct wl_input_device *device, uint32_t time, uint32_t edges)
525{
526 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400527 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400528
529 if (edges == 0 || edges > 15 ||
530 (edges & 3) == 3 || (edges & 12) == 12)
531 return;
532
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400533 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
534 wd->grab_time != time ||
535 &wd->pointer_focus->base != surface)
536 return;
537
538 switch (edges) {
539 case WLSC_DEVICE_GRAB_RESIZE_TOP:
540 pointer = WLSC_POINTER_TOP;
541 break;
542 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
543 pointer = WLSC_POINTER_BOTTOM;
544 break;
545 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
546 pointer = WLSC_POINTER_LEFT;
547 break;
548 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
549 pointer = WLSC_POINTER_TOP_LEFT;
550 break;
551 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
552 pointer = WLSC_POINTER_BOTTOM_LEFT;
553 break;
554 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
555 pointer = WLSC_POINTER_RIGHT;
556 break;
557 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
558 pointer = WLSC_POINTER_TOP_RIGHT;
559 break;
560 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
561 pointer = WLSC_POINTER_BOTTOM_RIGHT;
562 break;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400563 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400564
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400565 wlsc_input_device_start_grab(wd, time, edges);
566 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400567}
568
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500569static uint32_t
570get_time(void)
571{
572 struct timeval tv;
573
574 gettimeofday(&tv, NULL);
575
576 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
577}
578
579struct wlsc_drag {
580 struct wl_drag drag;
581 struct wlsc_listener listener;
582};
583
584static void
585wl_drag_set_pointer_focus(struct wl_drag *drag,
586 struct wlsc_surface *surface, uint32_t time,
587 int32_t x, int32_t y, int32_t sx, int32_t sy);
588
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400589static void
590destroy_drag(struct wl_resource *resource, struct wl_client *client)
591{
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500592 struct wlsc_drag *drag =
593 container_of(resource, struct wlsc_drag, drag.resource);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400594
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500595 wl_list_remove(&drag->listener.link);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400596
597 free(drag);
598}
599
600const static struct wl_drag_interface drag_interface;
601
602static void
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500603drag_handle_surface_destroy(struct wlsc_listener *listener,
604 struct wlsc_surface *surface)
605{
606 struct wlsc_drag *drag =
607 container_of(listener, struct wlsc_drag, listener);
608 uint32_t time = get_time();
609
610 if (drag->drag.pointer_focus == &surface->base)
611 wl_drag_set_pointer_focus(&drag->drag, NULL, time, 0, 0, 0, 0);
612}
613
614static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400615shell_create_drag(struct wl_client *client,
616 struct wl_shell *shell, uint32_t id)
617{
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500618 struct wlsc_drag *drag;
619 struct wlsc_compositor *ec =
620 container_of(shell, struct wlsc_compositor, shell);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400621
622 drag = malloc(sizeof *drag);
623 if (drag == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400624 wl_client_post_no_memory(client);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400625 return;
626 }
627
628 memset(drag, 0, sizeof *drag);
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500629 drag->drag.resource.base.id = id;
630 drag->drag.resource.base.interface = &wl_drag_interface;
631 drag->drag.resource.base.implementation =
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400632 (void (**)(void)) &drag_interface;
633
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500634 drag->drag.resource.destroy = destroy_drag;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400635
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500636 drag->listener.func = drag_handle_surface_destroy;
637 wl_list_insert(ec->surface_destroy_listener_list.prev,
638 &drag->listener.link);
639
640 wl_client_add_resource(client, &drag->drag.resource);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400641}
642
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400643const static struct wl_shell_interface shell_interface = {
644 shell_move,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400645 shell_resize,
646 shell_create_drag
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400647};
648
649static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500650compositor_create_surface(struct wl_client *client,
651 struct wl_compositor *compositor, uint32_t id)
652{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500653 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400654 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500655
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400656 surface = wlsc_surface_create(ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400657 if (surface == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400658 wl_client_post_no_memory(client);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500659 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400660 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500661
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400662 wl_list_insert(ec->surface_list.prev, &surface->link);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400663 surface->base.base.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400664
665 surface->base.base.base.id = id;
666 surface->base.base.base.interface = &wl_surface_interface;
667 surface->base.base.base.implementation =
668 (void (**)(void)) &surface_interface;
669 surface->base.client = client;
670
671 wl_client_add_resource(client, &surface->base.base);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500672}
673
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500674const static struct wl_compositor_interface compositor_interface = {
675 compositor_create_surface,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500676};
677
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500678static void
679wlsc_surface_transform(struct wlsc_surface *surface,
680 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
681{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400682 struct wlsc_vector v = { { x, y, 0, 1 } };
683
684 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400685 *sx = v.f[0] * surface->width;
686 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500687}
688
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500689static void
690wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400691 struct wlsc_surface *surface,
692 uint32_t time)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500693{
694 if (device->keyboard_focus == surface)
695 return;
696
697 if (device->keyboard_focus &&
698 (!surface || device->keyboard_focus->base.client != surface->base.client))
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400699 wl_client_post_event(device->keyboard_focus->base.client,
700 &device->base.base,
701 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
702 time, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500703
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500704 if (surface)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400705 wl_client_post_event(surface->base.client,
706 &device->base.base,
707 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
708 time, &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500709
710 device->keyboard_focus = surface;
711}
712
713static void
714wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400715 struct wlsc_surface *surface,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400716 uint32_t time,
717 int32_t x, int32_t y,
718 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500719{
720 if (device->pointer_focus == surface)
721 return;
722
723 if (device->pointer_focus &&
724 (!surface || device->pointer_focus->base.client != surface->base.client))
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400725 wl_client_post_event(device->pointer_focus->base.client,
726 &device->base.base,
727 WL_INPUT_DEVICE_POINTER_FOCUS,
728 time, NULL, 0, 0, 0, 0);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500729 if (surface)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400730 wl_client_post_event(surface->base.client,
731 &device->base.base,
732 WL_INPUT_DEVICE_POINTER_FOCUS,
733 time, &surface->base,
734 x, y, sx, sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500735
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400736 if (!surface)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400737 wlsc_input_device_set_pointer_image(device,
738 WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400739
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500740 device->pointer_focus = surface;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400741 device->pointer_focus_time = time;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500742}
743
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500744static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500745pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500746{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500747 struct wlsc_compositor *ec = device->ec;
748 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500749
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400750 wl_list_for_each(es, &ec->surface_list, link) {
751 wlsc_surface_transform(es, device->x, device->y, sx, sy);
752 if (0 <= *sx && *sx < es->width &&
753 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500754 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400755 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500756
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500757 return NULL;
758}
759
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500760void
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400761notify_motion(struct wlsc_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500762{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500763 struct wlsc_surface *es;
764 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500765 struct wlsc_output *output;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400766 int32_t sx, sy, width, height;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500767
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500768 /* FIXME: We need some multi head love here. */
769 output = container_of(ec->output_list.next, struct wlsc_output, link);
770 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500771 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500772 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500773 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500774 if (x >= output->x + output->width)
775 x = output->x + output->width - 1;
776 if (y >= output->y + output->height)
777 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500778
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500779 device->x = x;
780 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500781
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400782 switch (device->grab) {
783 case WLSC_DEVICE_GRAB_NONE:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400784 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400785 wlsc_input_device_set_pointer_focus(device, es,
786 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400787 if (es)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400788 wl_client_post_event(es->base.client,
789 &device->base.base,
790 WL_INPUT_DEVICE_MOTION,
791 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400792 break;
793
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400794 case WLSC_DEVICE_GRAB_MOTION:
795 es = device->pointer_focus;
796 wlsc_surface_transform(es, x, y, &sx, &sy);
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400797 wl_client_post_event(es->base.client,
798 &device->base.base,
799 WL_INPUT_DEVICE_MOTION,
800 time, x, y, sx, sy);
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400801 break;
802
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400803 case WLSC_DEVICE_GRAB_MOVE:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400804 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400805 es->x = x + device->grab_dx;
806 es->y = y + device->grab_dy;;
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400807 wl_client_post_event(es->base.client,
808 &ec->shell.base,
809 WL_SHELL_CONFIGURE,
810 time, device->grab,
811 &es->base, es->x, es->y,
812 es->width, es->height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400813
814 wlsc_surface_update_matrix(es);
815
816 break;
817
818 case WLSC_DEVICE_GRAB_RESIZE_TOP:
819 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
820 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
821 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
822 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
823 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
824 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
825 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
826 case WLSC_DEVICE_GRAB_RESIZE_MASK:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400827 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400828
829 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_LEFT) {
830 sx = x + device->grab_dx;
831 width = device->grab_x - x + device->grab_width;
832 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_RIGHT) {
833 sx = device->grab_x + device->grab_dx;
834 width = x - device->grab_x + device->grab_width;
835 } else {
836 sx = device->grab_x + device->grab_dx;
837 width = device->grab_width;
838 }
839
840 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_TOP) {
841 sy = y + device->grab_dy;
842 height = device->grab_y - y + device->grab_height;
843 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_BOTTOM) {
844 sy = device->grab_y + device->grab_dy;
845 height = y - device->grab_y + device->grab_height;
846 } else {
847 sy = device->grab_y + device->grab_dy;
848 height = device->grab_height;
849 }
850
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400851 wl_client_post_event(es->base.client,
852 &ec->shell.base,
853 WL_SHELL_CONFIGURE, time, device->grab,
854 &es->base, sx, sy, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400855 break;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400856
857 case WLSC_DEVICE_GRAB_DRAG:
858 es = pick_surface(device, &sx, &sy);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400859 wl_drag_set_pointer_focus(device->drag,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400860 es, time, x, y, sx, sy);
861 if (es)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400862 wl_client_post_event(es->base.client,
863 &device->drag->drag_offer.base,
864 WL_DRAG_OFFER_MOTION,
865 time, x, y, sx, sy);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400866 break;
867
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400868 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500869
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400870 device->sprite->x = device->x - device->hotspot_x;
871 device->sprite->y = device->y - device->hotspot_y;
872 wlsc_surface_update_matrix(device->sprite);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500873
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400874 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500875}
876
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400877static void
878wlsc_input_device_end_grab(struct wlsc_input_device *device, uint32_t time)
879{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400880 struct wl_drag *drag = device->drag;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400881 struct wlsc_surface *es;
882 int32_t sx, sy;
883
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400884 switch (device->grab) {
885 case WLSC_DEVICE_GRAB_DRAG:
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400886 if (drag->target)
887 wl_client_post_event(drag->target,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400888 &drag->drag_offer.base,
889 WL_DRAG_OFFER_DROP);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400890 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400891 device->drag = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400892 break;
893 default:
894 break;
895 }
896
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400897 device->grab = WLSC_DEVICE_GRAB_NONE;
898 es = pick_surface(device, &sx, &sy);
899 wlsc_input_device_set_pointer_focus(device, es, time,
900 device->x, device->y, sx, sy);
901}
902
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500903void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500904notify_button(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400905 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500906{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400907 struct wlsc_surface *surface;
908 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500909
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400910 surface = device->pointer_focus;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400911 if (surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400912 if (state && device->grab == WLSC_DEVICE_GRAB_NONE) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400913 wlsc_surface_raise(surface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400914 device->grab = WLSC_DEVICE_GRAB_MOTION;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400915 device->grab_button = button;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400916 device->grab_time = time;
917 device->grab_x = device->x;
918 device->grab_y = device->y;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400919 wlsc_input_device_set_keyboard_focus(device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400920 surface, time);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500921 }
922
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400923 if (state && button == BTN_LEFT &&
924 device->grab == WLSC_DEVICE_GRAB_MOTION &&
925 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400926 shell_move(NULL, (struct wl_shell *) &compositor->shell,
927 &surface->base, &device->base, time);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400928 else if (state && button == BTN_MIDDLE &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400929 device->grab == WLSC_DEVICE_GRAB_MOTION &&
930 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400931 shell_resize(NULL, (struct wl_shell *) &compositor->shell,
932
933 &surface->base, &device->base, time,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400934 WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400935 else if (device->grab == WLSC_DEVICE_GRAB_NONE ||
936 device->grab == WLSC_DEVICE_GRAB_MOTION)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400937 wl_client_post_event(surface->base.client,
938 &device->base.base,
939 WL_INPUT_DEVICE_BUTTON,
940 time, button, state);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500941
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400942 if (!state &&
943 device->grab != WLSC_DEVICE_GRAB_NONE &&
944 device->grab_button == button) {
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400945 wlsc_input_device_end_grab(device, time);
946 }
947
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400948 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500949 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500950}
951
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500952void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500953notify_key(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400954 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500955{
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500956 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400957 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500958
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400959 switch (key | device->modifier_state) {
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400960 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400961 kill(0, SIGTERM);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500962 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500963 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500964
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400965 switch (key) {
966 case KEY_LEFTCTRL:
967 case KEY_RIGHTCTRL:
968 modifier = MODIFIER_CTRL;
969 break;
970
971 case KEY_LEFTALT:
972 case KEY_RIGHTALT:
973 modifier = MODIFIER_ALT;
974 break;
975
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400976 case KEY_LEFTMETA:
977 case KEY_RIGHTMETA:
978 modifier = MODIFIER_SUPER;
979 break;
980
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400981 default:
982 modifier = 0;
983 break;
984 }
985
986 if (state)
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400987 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400988 else
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400989 device->modifier_state &= ~modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400990
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500991 end = device->keys.data + device->keys.size;
992 for (k = device->keys.data; k < end; k++) {
993 if (*k == key)
994 *k = *--end;
995 }
996 device->keys.size = (void *) end - device->keys.data;
997 if (state) {
998 k = wl_array_add(&device->keys, sizeof *k);
999 *k = key;
1000 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001001
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001002 if (device->keyboard_focus != NULL)
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001003 wl_client_post_event(device->keyboard_focus->base.client,
1004 &device->base.base,
1005 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001006}
1007
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001008static void
1009input_device_attach(struct wl_client *client,
1010 struct wl_input_device *device_base,
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001011 uint32_t time,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001012 struct wl_buffer *buffer, int32_t x, int32_t y)
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001013{
1014 struct wlsc_input_device *device =
1015 (struct wlsc_input_device *) device_base;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001016
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001017 if (time < device->pointer_focus_time)
1018 return;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001019 if (device->pointer_focus == NULL)
1020 return;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001021
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001022 if (device->pointer_focus->base.client != client &&
1023 !(&device->pointer_focus->base == &wl_grab_surface &&
1024 device->grab_surface->base.client == client))
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001025 return;
1026
Kristian Høgsbergf4cb2012010-08-16 17:46:25 -04001027 if (buffer == NULL) {
1028 wlsc_input_device_set_pointer_image(device,
1029 WLSC_POINTER_LEFT_PTR);
1030 return;
1031 }
1032
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001033 wlsc_input_device_attach(device, buffer, x, y);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001034}
1035
1036const static struct wl_input_device_interface input_device_interface = {
1037 input_device_attach,
1038};
1039
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001040static void
1041handle_surface_destroy(struct wlsc_listener *listener,
1042 struct wlsc_surface *surface)
1043{
1044 struct wlsc_input_device *device =
1045 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001046 uint32_t time = get_time();
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001047
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001048 if (device->keyboard_focus == surface)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001049 wlsc_input_device_set_keyboard_focus(device, NULL, time);
Kristian Høgsberg3d465342010-11-22 13:58:46 -05001050 if (device->pointer_focus == surface)
1051 wlsc_input_device_set_pointer_focus(device, NULL, time,
1052 0, 0, 0, 0);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001053 if (device->pointer_focus == surface ||
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001054 (&device->pointer_focus->base == &wl_grab_surface &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001055 device->grab_surface == surface))
1056 wlsc_input_device_end_grab(device, time);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001057}
1058
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001059static void
1060wl_drag_set_pointer_focus(struct wl_drag *drag,
1061 struct wlsc_surface *surface, uint32_t time,
1062 int32_t x, int32_t y, int32_t sx, int32_t sy)
1063{
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001064 char **p, **end;
1065
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001066 if (drag->pointer_focus == &surface->base)
1067 return;
1068
1069 if (drag->pointer_focus &&
1070 (!surface || drag->pointer_focus->client != surface->base.client))
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001071 wl_client_post_event(drag->pointer_focus->client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001072 &drag->drag_offer.base,
1073 WL_DRAG_OFFER_POINTER_FOCUS,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001074 time, NULL, 0, 0, 0, 0);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001075
1076 if (surface &&
1077 (!drag->pointer_focus ||
1078 drag->pointer_focus->client != surface->base.client)) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001079 wl_client_post_global(surface->base.client,
1080 &drag->drag_offer.base);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001081
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001082 end = drag->types.data + drag->types.size;
1083 for (p = drag->types.data; p < end; p++)
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001084 wl_client_post_event(surface->base.client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001085 &drag->drag_offer.base,
1086 WL_DRAG_OFFER_OFFER, *p);
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001087 }
1088
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001089 if (surface) {
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001090 wl_client_post_event(surface->base.client,
1091 &drag->drag_offer.base,
1092 WL_DRAG_OFFER_POINTER_FOCUS,
1093 time, &surface->base,
1094 x, y, sx, sy);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001095
1096 }
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001097
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001098 drag->pointer_focus = &surface->base;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001099 drag->pointer_focus_time = time;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001100 drag->target = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001101}
1102
1103static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001104drag_offer_accept(struct wl_client *client,
1105 struct wl_drag_offer *offer, uint32_t time, const char *type)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001106{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001107 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001108 char **p, **end;
1109
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001110 /* If the client responds to drag pointer_focus or motion
1111 * events after the pointer has left the surface, we just
1112 * discard the accept requests. The drag source just won't
1113 * get the corresponding 'target' events and eventually the
1114 * next surface/root will start sending events. */
1115 if (time < drag->pointer_focus_time)
1116 return;
1117
1118 drag->target = client;
1119 drag->type = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001120 end = drag->types.data + drag->types.size;
1121 for (p = drag->types.data; p < end; p++)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001122 if (type && strcmp(*p, type) == 0)
1123 drag->type = *p;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001124
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001125 wl_client_post_event(drag->source->client, &drag->resource.base,
1126 WL_DRAG_TARGET, drag->type);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001127}
1128
1129static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001130drag_offer_receive(struct wl_client *client,
1131 struct wl_drag_offer *offer, int fd)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001132{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001133 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001134
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001135 wl_client_post_event(drag->source->client, &drag->resource.base,
1136 WL_DRAG_FINISH, fd);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001137 close(fd);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001138}
1139
Kristian Høgsbergd44bc8b2010-11-30 15:10:26 -05001140static void
1141drag_offer_reject(struct wl_client *client, struct wl_drag_offer *offer)
1142{
1143 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
1144
1145 wl_client_post_event(drag->source->client, &drag->resource.base,
1146 WL_DRAG_REJECT);
1147}
1148
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001149static const struct wl_drag_offer_interface drag_offer_interface = {
1150 drag_offer_accept,
Kristian Høgsbergd44bc8b2010-11-30 15:10:26 -05001151 drag_offer_receive,
1152 drag_offer_reject
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001153};
1154
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001155static void
1156drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
1157{
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001158 char **p;
1159
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001160 p = wl_array_add(&drag->types, sizeof *p);
1161 if (p)
1162 *p = strdup(type);
1163 if (!p || !*p)
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001164 wl_client_post_no_memory(client);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001165}
1166
1167static void
1168drag_activate(struct wl_client *client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001169 struct wl_drag *drag,
1170 struct wl_surface *surface,
1171 struct wl_input_device *input_device, uint32_t time)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001172{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001173 struct wl_display *display = wl_client_get_display (client);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001174 struct wlsc_input_device *device =
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001175 (struct wlsc_input_device *) input_device;
1176 struct wlsc_surface *target;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001177 int32_t sx, sy;
1178
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001179 if (device->grab != WLSC_DEVICE_GRAB_MOTION ||
1180 &device->pointer_focus->base != surface ||
1181 device->grab_time != time)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001182 return;
1183
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001184 drag->source = surface;
1185 drag->input_device = input_device;
1186
1187 drag->drag_offer.base.interface = &wl_drag_offer_interface;
1188 drag->drag_offer.base.implementation =
1189 (void (**)(void)) &drag_offer_interface;
1190
1191 wl_display_add_object(display, &drag->drag_offer.base);
1192
1193 wlsc_input_device_start_grab(device, time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001194 WLSC_DEVICE_GRAB_DRAG);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001195
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001196 device->drag = drag;
1197 target = pick_surface(device, &sx, &sy);
1198 wl_drag_set_pointer_focus(device->drag, target, time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001199 device->x, device->y, sx, sy);
1200}
1201
1202static void
1203drag_cancel(struct wl_client *client, struct wl_drag *drag)
1204{
1205 struct wlsc_input_device *device =
1206 (struct wlsc_input_device *) drag->input_device;
1207
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001208 if (drag->source == NULL || drag->source->client != client)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001209 return;
1210
1211 wlsc_input_device_end_grab(device, get_time());
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001212 device->drag = NULL;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001213}
1214
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001215static const struct wl_drag_interface drag_interface = {
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001216 drag_offer,
1217 drag_activate,
1218 drag_cancel,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001219};
1220
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001221void
1222wlsc_input_device_init(struct wlsc_input_device *device,
1223 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001224{
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001225 device->base.base.interface = &wl_input_device_interface;
1226 device->base.base.implementation =
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001227 (void (**)(void)) &input_device_interface;
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001228 wl_display_add_object(ec->wl_display, &device->base.base);
1229 wl_display_add_global(ec->wl_display, &device->base.base, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001230
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001231 device->x = 100;
1232 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001233 device->ec = ec;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001234 device->sprite = wlsc_surface_create(ec, &ec->argb_visual,
1235 device->x, device->y, 32, 32);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001236 device->hotspot_x = 16;
1237 device->hotspot_y = 16;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001238
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001239 device->listener.func = handle_surface_destroy;
1240 wl_list_insert(ec->surface_destroy_listener_list.prev,
1241 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001242 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001243
1244 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001245}
1246
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001247static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001248wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001249{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001250 struct wlsc_output *output =
1251 container_of(global, struct wlsc_output, base);
1252
1253 wl_client_post_event(client, global,
1254 WL_OUTPUT_GEOMETRY,
1255 output->width, output->height);
1256}
1257
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001258static const char vertex_shader[] =
1259 "uniform mat4 proj;\n"
1260 "attribute vec4 position;\n"
1261 "attribute vec2 texcoord;\n"
1262 "varying vec2 v_texcoord;\n"
1263 "void main()\n"
1264 "{\n"
1265 " gl_Position = proj * position;\n"
1266 " v_texcoord = texcoord;\n"
1267 "}\n";
1268
1269static const char fragment_shader[] =
1270 /* "precision mediump float;\n" */
1271 "varying vec2 v_texcoord;\n"
1272 "uniform sampler2D tex;\n"
1273 "void main()\n"
1274 "{\n"
1275 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1276 "}\n";
1277
Kristian Høgsberga9468212010-06-14 21:03:11 -04001278static int
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001279init_shaders(struct wlsc_compositor *ec)
1280{
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001281 GLuint v, f, program;
1282 const char *p;
1283 char msg[512];
1284 GLfloat vertices[4 * 5];
1285 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001286
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001287 p = vertex_shader;
1288 v = glCreateShader(GL_VERTEX_SHADER);
1289 glShaderSource(v, 1, &p, NULL);
1290 glCompileShader(v);
1291 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
1292 if (!status) {
1293 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
1294 fprintf(stderr, "vertex shader info: %s\n", msg);
1295 return -1;
1296 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001297
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001298 p = fragment_shader;
1299 f = glCreateShader(GL_FRAGMENT_SHADER);
1300 glShaderSource(f, 1, &p, NULL);
1301 glCompileShader(f);
1302 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
1303 if (!status) {
1304 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
1305 fprintf(stderr, "fragment shader info: %s\n", msg);
1306 return -1;
1307 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001308
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001309 program = glCreateProgram();
1310 glAttachShader(program, v);
1311 glAttachShader(program, f);
1312 glBindAttribLocation(program, 0, "position");
1313 glBindAttribLocation(program, 1, "texcoord");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001314
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001315 glLinkProgram(program);
1316 glGetProgramiv(program, GL_LINK_STATUS, &status);
1317 if (!status) {
1318 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1319 fprintf(stderr, "link info: %s\n", msg);
1320 return -1;
1321 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001322
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001323 glUseProgram(program);
1324 ec->proj_uniform = glGetUniformLocation(program, "proj");
1325 ec->tex_uniform = glGetUniformLocation(program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001326
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001327 vertices[ 0] = 0.0;
1328 vertices[ 1] = 0.0;
1329 vertices[ 2] = 0.0;
1330 vertices[ 3] = 0.0;
1331 vertices[ 4] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001332
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001333 vertices[ 5] = 0.0;
1334 vertices[ 6] = 1.0;
1335 vertices[ 7] = 0.0;
1336 vertices[ 8] = 0.0;
1337 vertices[ 9] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001338
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001339 vertices[10] = 1.0;
1340 vertices[11] = 0.0;
1341 vertices[12] = 0.0;
1342 vertices[13] = 1.0;
1343 vertices[14] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001344
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001345 vertices[15] = 1.0;
1346 vertices[16] = 1.0;
1347 vertices[17] = 0.0;
1348 vertices[18] = 1.0;
1349 vertices[19] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001350
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001351 glGenBuffers(1, &ec->vbo);
1352 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
1353 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001354
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001355 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001356}
1357
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001358static const struct wl_interface visual_interface = {
1359 "visual", 1,
1360};
1361
1362static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001363add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001364{
1365 ec->argb_visual.base.interface = &visual_interface;
1366 ec->argb_visual.base.implementation = NULL;
1367 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001368 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001369
1370 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1371 ec->premultiplied_argb_visual.base.implementation = NULL;
1372 wl_display_add_object(ec->wl_display,
1373 &ec->premultiplied_argb_visual.base);
1374 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001375 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001376
1377 ec->rgb_visual.base.interface = &visual_interface;
1378 ec->rgb_visual.base.implementation = NULL;
1379 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001380 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1381}
1382
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001383void
1384wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1385 int x, int y, int width, int height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001386{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001387 output->compositor = c;
1388 output->x = x;
1389 output->y = y;
1390 output->width = width;
1391 output->height = height;
1392
1393 output->background =
1394 background_create(output, option_background);
1395
1396 wlsc_matrix_init(&output->matrix);
1397 wlsc_matrix_translate(&output->matrix,
1398 -output->x - output->width / 2.0,
1399 -output->y - output->height / 2.0, 0);
1400 wlsc_matrix_scale(&output->matrix,
1401 2.0 / output->width, 2.0 / output->height, 1);
1402
1403 output->base.interface = &wl_output_interface;
1404 wl_display_add_object(c->wl_display, &output->base);
1405 wl_display_add_global(c->wl_display, &output->base,
1406 wlsc_output_post_geometry);
1407}
1408
1409int
1410wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1411{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001412 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001413
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001414 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001415
Kristian Høgsbergf8ffded2010-09-03 15:15:33 -04001416 ec->base.base.interface = &wl_compositor_interface;
1417 ec->base.base.implementation =
1418 (void (**)(void)) &compositor_interface;
1419
1420 wl_display_add_object(display, &ec->base.base);
1421 if (wl_display_add_global(display, &ec->base.base, NULL))
1422 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001423
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001424 wlsc_shm_init(ec);
1425
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001426 ec->shell.base.interface = &wl_shell_interface;
1427 ec->shell.base.implementation = (void (**)(void)) &shell_interface;
1428 wl_display_add_object(display, &ec->shell.base);
1429 if (wl_display_add_global(display, &ec->shell.base, NULL))
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001430 return -1;
1431
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001432 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001433
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001434 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001435 wl_list_init(&ec->input_device_list);
1436 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001437 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001438
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001439 create_pointer_images(ec);
1440
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001441 screenshooter_create(ec);
1442
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001443 glGenFramebuffers(1, &ec->fbo);
1444 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1445 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001446 if (init_shaders(ec) < 0)
1447 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001448
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001449 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001450 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001451 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001452
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001453 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001454}
1455
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001456
1457int main(int argc, char *argv[])
1458{
1459 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001460 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001461 GError *error = NULL;
1462 GOptionContext *context;
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001463 int width, height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001464 char *socket_name;
1465 int socket_name_size;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001466
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001467 g_type_init(); /* GdkPixbuf needs this, it seems. */
1468
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001469 context = g_option_context_new(NULL);
1470 g_option_context_add_main_entries(context, option_entries, "Wayland");
1471 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1472 fprintf(stderr, "option parsing failed: %s\n", error->message);
1473 exit(EXIT_FAILURE);
1474 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001475 if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) {
1476 fprintf(stderr, "invalid geometry option: %s \n",
1477 option_geometry);
1478 exit(EXIT_FAILURE);
1479 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001480
1481 display = wl_display_create();
1482
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001483 if (getenv("WAYLAND_DISPLAY"))
1484 ec = wayland_compositor_create(display, width, height);
1485 else if (getenv("DISPLAY"))
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001486 ec = x11_compositor_create(display, width, height);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001487 else
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001488 ec = drm_compositor_create(display, option_connector);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001489
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001490 if (ec == NULL) {
1491 fprintf(stderr, "failed to create compositor\n");
1492 exit(EXIT_FAILURE);
1493 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001494
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001495 socket_name_size = 1 + asprintf(&socket_name, "%c%s", '\0',
1496 option_socket_name);
1497
1498 if (wl_display_add_socket(display, socket_name, socket_name_size)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001499 fprintf(stderr, "failed to add socket: %m\n");
1500 exit(EXIT_FAILURE);
1501 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001502 free(socket_name);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001503
1504 wl_display_run(display);
1505
1506 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001507}