blob: a09f6d904b5067e2f6c6e2d8f95d1ef8b81c47f1 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05008 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050017 */
18
Kristian Høgsberg06bc2642010-12-01 09:50:16 -050019#define _GNU_SOURCE
20
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040021#include <stdio.h>
22#include <string.h>
23#include <stdlib.h>
24#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050025#include <stdarg.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040026#include <sys/ioctl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040027#include <fcntl.h>
28#include <unistd.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050029#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050030#include <math.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050031#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040032#include <linux/input.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050033
Kristian Høgsberga1f3f602010-08-03 09:26:44 -040034#include "wayland-server-protocol.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040035#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050036
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010037/* The plan here is to generate a random anonymous socket name and
38 * advertise that through a service on the session dbus.
39 */
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -050040static const char *option_socket_name = NULL;
Kristian Høgsberg61a82512010-10-26 11:26:44 -040041static const char *option_background = "background.jpg";
42static const char *option_geometry = "1024x640";
43static int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050044
45static const GOptionEntry option_entries[] = {
46 { "background", 'b', 0, G_OPTION_ARG_STRING,
47 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -040048 { "connector", 'c', 0, G_OPTION_ARG_INT,
49 &option_connector, "KMS connector" },
Kristian Høgsberg61a82512010-10-26 11:26:44 -040050 { "geometry", 'g', 0, G_OPTION_ARG_STRING,
51 &option_geometry, "Geometry" },
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010052 { "socket", 's', 0, G_OPTION_ARG_STRING,
53 &option_socket_name, "Socket Name" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050054 { NULL }
55};
56
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050057static void
58wlsc_matrix_init(struct wlsc_matrix *matrix)
59{
60 static const struct wlsc_matrix identity = {
61 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
62 };
63
64 memcpy(matrix, &identity, sizeof identity);
65}
66
67static void
68wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
69{
70 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040071 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050072 div_t d;
73 int i, j;
74
75 for (i = 0; i < 16; i++) {
76 tmp.d[i] = 0;
77 d = div(i, 4);
78 row = m->d + d.quot * 4;
79 column = n->d + d.rem;
80 for (j = 0; j < 4; j++)
81 tmp.d[i] += row[j] * column[j * 4];
82 }
83 memcpy(m, &tmp, sizeof tmp);
84}
85
86static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040087wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050088{
Kristian Høgsberg27803c62010-06-06 22:23:21 -040089 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050090 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
91 };
92
93 wlsc_matrix_multiply(matrix, &translate);
94}
95
96static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040097wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050098{
99 struct wlsc_matrix scale = {
100 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
101 };
102
103 wlsc_matrix_multiply(matrix, &scale);
104}
105
106static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400107wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
108{
109 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400110 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400111
112 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400113 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400114 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400115 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400116 }
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400117
118 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400119}
120
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400121static struct wlsc_surface *
122wlsc_surface_create(struct wlsc_compositor *compositor,
123 struct wl_visual *visual,
124 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500125{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400126 struct wlsc_surface *surface;
127
128 surface = malloc(sizeof *surface);
129 if (surface == NULL)
130 return NULL;
131
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500132 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400133 glBindTexture(GL_TEXTURE_2D, surface->texture);
134 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
135 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
136 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
137 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
138
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500139 surface->compositor = compositor;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500140 surface->visual = visual;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400141 surface->x = x;
142 surface->y = y;
143 surface->width = width;
144 surface->height = height;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400145 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400146 wlsc_matrix_scale(&surface->matrix, width, height, 1);
147 wlsc_matrix_translate(&surface->matrix, x, y, 0);
148
149 wlsc_matrix_init(&surface->matrix_inv);
150 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
151 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500152
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400153 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500154}
155
156static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400157destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500158{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400159 struct wlsc_surface *surface =
160 container_of(resource, struct wlsc_surface, base.base);
161 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500162 struct wlsc_listener *l;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400163
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400164 wl_list_remove(&surface->link);
165 glDeleteTextures(1, &surface->texture);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400166
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500167 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400168 l->func(l, surface);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400169
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400170 free(surface);
Kristian Høgsberg117d5132010-08-11 08:56:47 -0400171
172 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500173}
174
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400175static int
176texture_from_png(const char *filename, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500177{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400178 GdkPixbuf *pixbuf;
179 GError *error = NULL;
180 void *data;
181 GLenum format;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500182
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400183 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
184 width, height,
185 FALSE, &error);
186 if (error != NULL)
187 return -1;
188
189 data = gdk_pixbuf_get_pixels(pixbuf);
190
191 if (gdk_pixbuf_get_has_alpha(pixbuf))
192 format = GL_RGBA;
193 else
194 format = GL_RGB;
195
Chia-I Wu1f411902010-10-29 15:20:16 +0800196 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
197 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400198
199 gdk_pixbuf_unref(pixbuf);
200
201 return 0;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500202}
203
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400204static const struct {
205 const char *filename;
206 int hotspot_x, hotspot_y;
207} pointer_images[] = {
Kristian Høgsberg4219a402010-08-16 16:43:03 -0400208 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
209 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
210 { DATADIR "/wayland/bottom_side.png", 16, 20 },
211 { DATADIR "/wayland/grabbing.png", 20, 17 },
212 { DATADIR "/wayland/left_ptr.png", 10, 5 },
213 { DATADIR "/wayland/left_side.png", 10, 20 },
214 { DATADIR "/wayland/right_side.png", 30, 19 },
215 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
216 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
217 { DATADIR "/wayland/top_side.png", 18, 8 },
218 { DATADIR "/wayland/xterm.png", 15, 15 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400219};
220
221static void
222create_pointer_images(struct wlsc_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500223{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400224 int i, count;
225 GLuint texture;
226 const int width = 32, height = 32;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400227
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400228 glGenTextures(1, &texture);
229 glBindTexture(GL_TEXTURE_2D, texture);
230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400234
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400235 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400236 ec->pointer_buffers = malloc(count * sizeof *ec->pointer_buffers);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400237 for (i = 0; i < count; i++) {
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400238 ec->pointer_buffers[i] =
239 wlsc_drm_buffer_create(ec, width, height,
240 &ec->argb_visual);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400241 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400242 ec->pointer_buffers[i]->image);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400243 texture_from_png(pointer_images[i].filename, width, height);
244 }
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400245}
246
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500247static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500248background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500249{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500250 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500251 GdkPixbuf *pixbuf;
252 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500253 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500254 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500255
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400256 background = wlsc_surface_create(output->compositor,
257 &output->compositor->rgb_visual,
258 output->x, output->y,
259 output->width, output->height);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500260 if (background == NULL)
261 return NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500262
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500263 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500264 output->width,
265 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500266 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500267 if (error != NULL) {
268 free(background);
269 return NULL;
270 }
271
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500272 data = gdk_pixbuf_get_pixels(pixbuf);
273
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400274 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500275 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500276 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500277 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500278
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500279 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
280 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500281 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500282
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500283 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500284}
285
286static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400287wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500288{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500289 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400290 struct wlsc_matrix tmp;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500291
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400292 tmp = es->matrix;
293 wlsc_matrix_multiply(&tmp, &output->matrix);
294 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
295 glUniform1i(ec->tex_uniform, 0);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500296
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500297 if (es->visual == &ec->argb_visual) {
298 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
299 glEnable(GL_BLEND);
300 } else if (es->visual == &ec->premultiplied_argb_visual) {
301 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
302 glEnable(GL_BLEND);
303 } else {
304 glDisable(GL_BLEND);
305 }
306
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500307 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400308 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
309 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
310 5 * sizeof(GLfloat), NULL);
311 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
312 5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
313 glEnableVertexAttribArray(0);
314 glEnableVertexAttribArray(1);
315 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500316}
317
318static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400319wlsc_surface_raise(struct wlsc_surface *surface)
320{
321 struct wlsc_compositor *compositor = surface->compositor;
322
323 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400324 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400325}
326
327static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400328wlsc_surface_update_matrix(struct wlsc_surface *es)
329{
330 wlsc_matrix_init(&es->matrix);
331 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
332 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
333
334 wlsc_matrix_init(&es->matrix_inv);
335 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
336 wlsc_matrix_scale(&es->matrix_inv,
337 1.0 / es->width, 1.0 / es->height, 1);
338}
339
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400340void
341wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400342{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400343 wl_display_post_frame(compositor->wl_display, msecs);
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400344 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400345}
346
347static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400348wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400349{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500350 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500351 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500352 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500353
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500354 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500355
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500356 if (output->background)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400357 wlsc_surface_draw(output->background, output);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500358 else
359 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400360
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400361 wl_list_for_each_reverse(es, &ec->surface_list, link)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400362 wlsc_surface_draw(es, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400363
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400364 if (ec->focus)
365 wl_list_for_each(eid, &ec->input_device_list, link)
366 wlsc_surface_draw(eid->sprite, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500367}
368
369static void
370repaint(void *data)
371{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500372 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500373 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500374
375 if (!ec->repaint_needed) {
376 ec->repaint_on_timeout = 0;
377 return;
378 }
379
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500380 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400381 wlsc_output_repaint(output);
382
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400383 ec->present(ec);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500384
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500385 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400386}
387
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400388void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400389wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400390{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400391 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400392 if (compositor->repaint_on_timeout)
393 return;
394
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400395 wl_event_source_timer_update(compositor->timer_source, 1);
396 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400397}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500398
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500399static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500400surface_destroy(struct wl_client *client,
401 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400402{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400403 wl_resource_destroy(&surface->base, client);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400404}
405
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500406static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500407surface_attach(struct wl_client *client,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400408 struct wl_surface *surface, struct wl_buffer *buffer)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400409{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500410 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400411
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400412 buffer->attach(buffer, surface);
413 es->buffer = buffer;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400414}
415
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500416static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500417surface_map(struct wl_client *client,
418 struct wl_surface *surface,
419 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400420{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500421 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400422
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400423 es->x = x;
424 es->y = y;
425 es->width = width;
426 es->height = height;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400427
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400428 wlsc_surface_update_matrix(es);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400429 wlsc_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400430}
431
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500432static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500433surface_damage(struct wl_client *client,
434 struct wl_surface *surface,
435 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500436{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400437 struct wlsc_surface *es = (struct wlsc_surface *) surface;
438
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400439 es->buffer->damage(es->buffer, surface, x, y, width, height);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400440 wlsc_compositor_schedule_repaint(es->compositor);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500441}
442
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500443const static struct wl_surface_interface surface_interface = {
444 surface_destroy,
445 surface_attach,
446 surface_map,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500447 surface_damage
448};
449
450static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400451wlsc_input_device_attach(struct wlsc_input_device *device,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400452 struct wl_buffer *buffer, int x, int y)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400453{
454 struct wlsc_compositor *ec = device->ec;
455
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400456 buffer->attach(buffer, &device->sprite->base);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400457 device->hotspot_x = x;
458 device->hotspot_y = y;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400459
460 device->sprite->x = device->x - device->hotspot_x;
461 device->sprite->y = device->y - device->hotspot_y;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400462 device->sprite->width = buffer->width;
463 device->sprite->height = buffer->height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400464 wlsc_surface_update_matrix(device->sprite);
465
466 wlsc_compositor_schedule_repaint(ec);
467}
468
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400469
470static void
471wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
472 enum wlsc_pointer_type type)
473{
474 struct wlsc_compositor *compositor = device->ec;
475
476 wlsc_input_device_attach(device,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400477 &compositor->pointer_buffers[type]->base,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400478 pointer_images[type].hotspot_x,
479 pointer_images[type].hotspot_y);
480}
481
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400482static void
483wlsc_input_device_start_grab(struct wlsc_input_device *device,
484 uint32_t time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400485 enum wlsc_grab_type grab)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400486{
Kristian Høgsberg26437072010-12-01 10:17:47 -0500487 struct wlsc_surface *focus =
488 (struct wlsc_surface *) device->base.pointer_focus;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400489
Kristian Høgsberg26437072010-12-01 10:17:47 -0500490 device->grab = grab;
491 device->grab_surface = focus;
492 device->grab_dx = focus->x - device->grab_x;
493 device->grab_dy = focus->y - device->grab_y;
494 device->grab_width = focus->width;
495 device->grab_height = focus->height;
496
497 wl_input_device_set_pointer_focus(&device->base,
498 &wl_grab_surface, time, 0, 0, 0, 0);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400499}
500
501static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400502shell_move(struct wl_client *client, struct wl_shell *shell,
503 struct wl_surface *surface,
504 struct wl_input_device *device, uint32_t time)
505{
506 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
507
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400508 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
509 wd->grab_time != time ||
Kristian Høgsberg26437072010-12-01 10:17:47 -0500510 wd->base.pointer_focus != surface)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400511 return;
512
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400513 wlsc_input_device_start_grab(wd, time, WLSC_DEVICE_GRAB_MOVE);
514 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400515}
516
517static void
518shell_resize(struct wl_client *client, struct wl_shell *shell,
519 struct wl_surface *surface,
520 struct wl_input_device *device, uint32_t time, uint32_t edges)
521{
522 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400523 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400524
525 if (edges == 0 || edges > 15 ||
526 (edges & 3) == 3 || (edges & 12) == 12)
527 return;
528
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400529 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
530 wd->grab_time != time ||
Kristian Høgsberg26437072010-12-01 10:17:47 -0500531 wd->base.pointer_focus != surface)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400532 return;
533
534 switch (edges) {
535 case WLSC_DEVICE_GRAB_RESIZE_TOP:
536 pointer = WLSC_POINTER_TOP;
537 break;
538 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
539 pointer = WLSC_POINTER_BOTTOM;
540 break;
541 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
542 pointer = WLSC_POINTER_LEFT;
543 break;
544 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
545 pointer = WLSC_POINTER_TOP_LEFT;
546 break;
547 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
548 pointer = WLSC_POINTER_BOTTOM_LEFT;
549 break;
550 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
551 pointer = WLSC_POINTER_RIGHT;
552 break;
553 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
554 pointer = WLSC_POINTER_TOP_RIGHT;
555 break;
556 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
557 pointer = WLSC_POINTER_BOTTOM_RIGHT;
558 break;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400559 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400560
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400561 wlsc_input_device_start_grab(wd, time, edges);
562 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400563}
564
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500565static uint32_t
566get_time(void)
567{
568 struct timeval tv;
569
570 gettimeofday(&tv, NULL);
571
572 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
573}
574
575struct wlsc_drag {
576 struct wl_drag drag;
577 struct wlsc_listener listener;
578};
579
580static void
581wl_drag_set_pointer_focus(struct wl_drag *drag,
582 struct wlsc_surface *surface, uint32_t time,
583 int32_t x, int32_t y, int32_t sx, int32_t sy);
584
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400585static void
586destroy_drag(struct wl_resource *resource, struct wl_client *client)
587{
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500588 struct wlsc_drag *drag =
589 container_of(resource, struct wlsc_drag, drag.resource);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400590
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500591 wl_list_remove(&drag->listener.link);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400592
593 free(drag);
594}
595
596const static struct wl_drag_interface drag_interface;
597
598static void
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500599drag_handle_surface_destroy(struct wlsc_listener *listener,
600 struct wlsc_surface *surface)
601{
602 struct wlsc_drag *drag =
603 container_of(listener, struct wlsc_drag, listener);
604 uint32_t time = get_time();
605
606 if (drag->drag.pointer_focus == &surface->base)
607 wl_drag_set_pointer_focus(&drag->drag, NULL, time, 0, 0, 0, 0);
608}
609
610static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400611shell_create_drag(struct wl_client *client,
612 struct wl_shell *shell, uint32_t id)
613{
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500614 struct wlsc_drag *drag;
615 struct wlsc_compositor *ec =
616 container_of(shell, struct wlsc_compositor, shell);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400617
618 drag = malloc(sizeof *drag);
619 if (drag == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400620 wl_client_post_no_memory(client);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400621 return;
622 }
623
624 memset(drag, 0, sizeof *drag);
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500625 drag->drag.resource.base.id = id;
626 drag->drag.resource.base.interface = &wl_drag_interface;
627 drag->drag.resource.base.implementation =
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400628 (void (**)(void)) &drag_interface;
629
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500630 drag->drag.resource.destroy = destroy_drag;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400631
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500632 drag->listener.func = drag_handle_surface_destroy;
633 wl_list_insert(ec->surface_destroy_listener_list.prev,
634 &drag->listener.link);
635
636 wl_client_add_resource(client, &drag->drag.resource);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400637}
638
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400639const static struct wl_shell_interface shell_interface = {
640 shell_move,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400641 shell_resize,
642 shell_create_drag
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400643};
644
645static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500646compositor_create_surface(struct wl_client *client,
647 struct wl_compositor *compositor, uint32_t id)
648{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500649 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400650 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500651
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400652 surface = wlsc_surface_create(ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400653 if (surface == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400654 wl_client_post_no_memory(client);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500655 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400656 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500657
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400658 wl_list_insert(ec->surface_list.prev, &surface->link);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400659 surface->base.base.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400660
661 surface->base.base.base.id = id;
662 surface->base.base.base.interface = &wl_surface_interface;
663 surface->base.base.base.implementation =
664 (void (**)(void)) &surface_interface;
665 surface->base.client = client;
666
667 wl_client_add_resource(client, &surface->base.base);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500668}
669
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500670const static struct wl_compositor_interface compositor_interface = {
671 compositor_create_surface,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500672};
673
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500674static void
675wlsc_surface_transform(struct wlsc_surface *surface,
676 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
677{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400678 struct wlsc_vector v = { { x, y, 0, 1 } };
679
680 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400681 *sx = v.f[0] * surface->width;
682 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500683}
684
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500685static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500686pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500687{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500688 struct wlsc_compositor *ec = device->ec;
689 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500690
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400691 wl_list_for_each(es, &ec->surface_list, link) {
692 wlsc_surface_transform(es, device->x, device->y, sx, sy);
693 if (0 <= *sx && *sx < es->width &&
694 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500695 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400696 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500697
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500698 return NULL;
699}
700
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500701void
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400702notify_motion(struct wlsc_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500703{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500704 struct wlsc_surface *es;
705 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500706 struct wlsc_output *output;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400707 int32_t sx, sy, width, height;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500708
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500709 /* FIXME: We need some multi head love here. */
710 output = container_of(ec->output_list.next, struct wlsc_output, link);
711 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500712 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500713 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500714 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500715 if (x >= output->x + output->width)
716 x = output->x + output->width - 1;
717 if (y >= output->y + output->height)
718 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500719
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500720 device->x = x;
721 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500722
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400723 switch (device->grab) {
724 case WLSC_DEVICE_GRAB_NONE:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400725 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg26437072010-12-01 10:17:47 -0500726 wl_input_device_set_pointer_focus(&device->base, &es->base,
727 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400728 if (es)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400729 wl_client_post_event(es->base.client,
730 &device->base.base,
731 WL_INPUT_DEVICE_MOTION,
732 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400733 break;
734
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400735 case WLSC_DEVICE_GRAB_MOTION:
Kristian Høgsberg26437072010-12-01 10:17:47 -0500736 es = (struct wlsc_surface *) device->base.pointer_focus;
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400737 wlsc_surface_transform(es, x, y, &sx, &sy);
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400738 wl_client_post_event(es->base.client,
739 &device->base.base,
740 WL_INPUT_DEVICE_MOTION,
741 time, x, y, sx, sy);
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400742 break;
743
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400744 case WLSC_DEVICE_GRAB_MOVE:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400745 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400746 es->x = x + device->grab_dx;
747 es->y = y + device->grab_dy;;
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400748 wl_client_post_event(es->base.client,
749 &ec->shell.base,
750 WL_SHELL_CONFIGURE,
751 time, device->grab,
752 &es->base, es->x, es->y,
753 es->width, es->height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400754
755 wlsc_surface_update_matrix(es);
756
757 break;
758
759 case WLSC_DEVICE_GRAB_RESIZE_TOP:
760 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
761 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
762 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
763 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
764 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
765 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
766 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
767 case WLSC_DEVICE_GRAB_RESIZE_MASK:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400768 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400769
770 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_LEFT) {
771 sx = x + device->grab_dx;
772 width = device->grab_x - x + device->grab_width;
773 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_RIGHT) {
774 sx = device->grab_x + device->grab_dx;
775 width = x - device->grab_x + device->grab_width;
776 } else {
777 sx = device->grab_x + device->grab_dx;
778 width = device->grab_width;
779 }
780
781 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_TOP) {
782 sy = y + device->grab_dy;
783 height = device->grab_y - y + device->grab_height;
784 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_BOTTOM) {
785 sy = device->grab_y + device->grab_dy;
786 height = y - device->grab_y + device->grab_height;
787 } else {
788 sy = device->grab_y + device->grab_dy;
789 height = device->grab_height;
790 }
791
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400792 wl_client_post_event(es->base.client,
793 &ec->shell.base,
794 WL_SHELL_CONFIGURE, time, device->grab,
795 &es->base, sx, sy, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400796 break;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400797
798 case WLSC_DEVICE_GRAB_DRAG:
799 es = pick_surface(device, &sx, &sy);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400800 wl_drag_set_pointer_focus(device->drag,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400801 es, time, x, y, sx, sy);
802 if (es)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400803 wl_client_post_event(es->base.client,
804 &device->drag->drag_offer.base,
805 WL_DRAG_OFFER_MOTION,
806 time, x, y, sx, sy);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400807 break;
808
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400809 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500810
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400811 device->sprite->x = device->x - device->hotspot_x;
812 device->sprite->y = device->y - device->hotspot_y;
813 wlsc_surface_update_matrix(device->sprite);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500814
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400815 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500816}
817
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400818static void
819wlsc_input_device_end_grab(struct wlsc_input_device *device, uint32_t time)
820{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400821 struct wl_drag *drag = device->drag;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400822 struct wlsc_surface *es;
823 int32_t sx, sy;
824
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400825 switch (device->grab) {
826 case WLSC_DEVICE_GRAB_DRAG:
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400827 if (drag->target)
828 wl_client_post_event(drag->target,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400829 &drag->drag_offer.base,
830 WL_DRAG_OFFER_DROP);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400831 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400832 device->drag = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400833 break;
834 default:
835 break;
836 }
837
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400838 device->grab = WLSC_DEVICE_GRAB_NONE;
839 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg26437072010-12-01 10:17:47 -0500840 wl_input_device_set_pointer_focus(&device->base, &es->base, time,
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400841 device->x, device->y, sx, sy);
842}
843
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500844void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500845notify_button(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400846 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500847{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400848 struct wlsc_surface *surface;
849 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500850
Kristian Høgsberg26437072010-12-01 10:17:47 -0500851 surface = (struct wlsc_surface *) device->base.pointer_focus;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400852 if (surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400853 if (state && device->grab == WLSC_DEVICE_GRAB_NONE) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400854 wlsc_surface_raise(surface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400855 device->grab = WLSC_DEVICE_GRAB_MOTION;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400856 device->grab_button = button;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400857 device->grab_time = time;
858 device->grab_x = device->x;
859 device->grab_y = device->y;
Kristian Høgsberg26437072010-12-01 10:17:47 -0500860 wl_input_device_set_keyboard_focus(&device->base,
861 &surface->base,
862 time);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500863 }
864
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400865 if (state && button == BTN_LEFT &&
866 device->grab == WLSC_DEVICE_GRAB_MOTION &&
867 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400868 shell_move(NULL, (struct wl_shell *) &compositor->shell,
869 &surface->base, &device->base, time);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400870 else if (state && button == BTN_MIDDLE &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400871 device->grab == WLSC_DEVICE_GRAB_MOTION &&
872 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400873 shell_resize(NULL, (struct wl_shell *) &compositor->shell,
874
875 &surface->base, &device->base, time,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400876 WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400877 else if (device->grab == WLSC_DEVICE_GRAB_NONE ||
878 device->grab == WLSC_DEVICE_GRAB_MOTION)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400879 wl_client_post_event(surface->base.client,
880 &device->base.base,
881 WL_INPUT_DEVICE_BUTTON,
882 time, button, state);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500883
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400884 if (!state &&
885 device->grab != WLSC_DEVICE_GRAB_NONE &&
886 device->grab_button == button) {
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400887 wlsc_input_device_end_grab(device, time);
888 }
889
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400890 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500891 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500892}
893
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500894void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500895notify_key(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400896 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500897{
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500898 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400899 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500900
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400901 switch (key | device->modifier_state) {
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400902 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500903 wl_display_terminate(device->ec->wl_display);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500904 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500905 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500906
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400907 switch (key) {
908 case KEY_LEFTCTRL:
909 case KEY_RIGHTCTRL:
910 modifier = MODIFIER_CTRL;
911 break;
912
913 case KEY_LEFTALT:
914 case KEY_RIGHTALT:
915 modifier = MODIFIER_ALT;
916 break;
917
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400918 case KEY_LEFTMETA:
919 case KEY_RIGHTMETA:
920 modifier = MODIFIER_SUPER;
921 break;
922
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400923 default:
924 modifier = 0;
925 break;
926 }
927
928 if (state)
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400929 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400930 else
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400931 device->modifier_state &= ~modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400932
Kristian Høgsberg26437072010-12-01 10:17:47 -0500933 end = device->base.keys.data + device->base.keys.size;
934 for (k = device->base.keys.data; k < end; k++) {
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500935 if (*k == key)
936 *k = *--end;
937 }
Kristian Høgsberg26437072010-12-01 10:17:47 -0500938 device->base.keys.size = (void *) end - device->base.keys.data;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500939 if (state) {
Kristian Høgsberg26437072010-12-01 10:17:47 -0500940 k = wl_array_add(&device->base.keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500941 *k = key;
942 }
Ray Strodee96dcb82008-12-20 02:00:49 -0500943
Kristian Høgsberg26437072010-12-01 10:17:47 -0500944 if (device->base.keyboard_focus != NULL)
945 wl_client_post_event(device->base.keyboard_focus->client,
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400946 &device->base.base,
947 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400948}
949
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400950static void
951input_device_attach(struct wl_client *client,
952 struct wl_input_device *device_base,
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400953 uint32_t time,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400954 struct wl_buffer *buffer, int32_t x, int32_t y)
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400955{
956 struct wlsc_input_device *device =
957 (struct wlsc_input_device *) device_base;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400958
Kristian Høgsberg26437072010-12-01 10:17:47 -0500959 if (time < device->base.pointer_focus_time)
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400960 return;
Kristian Høgsberg26437072010-12-01 10:17:47 -0500961 if (device->base.pointer_focus == NULL)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400962 return;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100963
Kristian Høgsberg26437072010-12-01 10:17:47 -0500964 if (device->base.pointer_focus->client != client &&
965 !(device->base.pointer_focus == &wl_grab_surface &&
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400966 device->grab_surface->base.client == client))
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400967 return;
968
Kristian Høgsbergf4cb2012010-08-16 17:46:25 -0400969 if (buffer == NULL) {
970 wlsc_input_device_set_pointer_image(device,
971 WLSC_POINTER_LEFT_PTR);
972 return;
973 }
974
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400975 wlsc_input_device_attach(device, buffer, x, y);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400976}
977
978const static struct wl_input_device_interface input_device_interface = {
979 input_device_attach,
980};
981
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400982static void
983handle_surface_destroy(struct wlsc_listener *listener,
984 struct wlsc_surface *surface)
985{
986 struct wlsc_input_device *device =
987 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400988 uint32_t time = get_time();
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400989
Kristian Høgsberg26437072010-12-01 10:17:47 -0500990 if (device->base.keyboard_focus == &surface->base)
991 wl_input_device_set_keyboard_focus(&device->base, NULL, time);
992 if (device->base.pointer_focus == &surface->base)
993 wl_input_device_set_pointer_focus(&device->base, NULL, time,
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500994 0, 0, 0, 0);
Kristian Høgsberg26437072010-12-01 10:17:47 -0500995 if (device->base.pointer_focus == &surface->base ||
996 (device->base.pointer_focus == &wl_grab_surface &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400997 device->grab_surface == surface))
998 wlsc_input_device_end_grab(device, time);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400999}
1000
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001001static void
1002wl_drag_set_pointer_focus(struct wl_drag *drag,
1003 struct wlsc_surface *surface, uint32_t time,
1004 int32_t x, int32_t y, int32_t sx, int32_t sy)
1005{
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001006 char **p, **end;
1007
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001008 if (drag->pointer_focus == &surface->base)
1009 return;
1010
1011 if (drag->pointer_focus &&
1012 (!surface || drag->pointer_focus->client != surface->base.client))
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001013 wl_client_post_event(drag->pointer_focus->client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001014 &drag->drag_offer.base,
1015 WL_DRAG_OFFER_POINTER_FOCUS,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001016 time, NULL, 0, 0, 0, 0);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001017
1018 if (surface &&
1019 (!drag->pointer_focus ||
1020 drag->pointer_focus->client != surface->base.client)) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001021 wl_client_post_global(surface->base.client,
1022 &drag->drag_offer.base);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001023
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001024 end = drag->types.data + drag->types.size;
1025 for (p = drag->types.data; p < end; p++)
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001026 wl_client_post_event(surface->base.client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001027 &drag->drag_offer.base,
1028 WL_DRAG_OFFER_OFFER, *p);
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001029 }
1030
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001031 if (surface) {
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001032 wl_client_post_event(surface->base.client,
1033 &drag->drag_offer.base,
1034 WL_DRAG_OFFER_POINTER_FOCUS,
1035 time, &surface->base,
1036 x, y, sx, sy);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001037
1038 }
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001039
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001040 drag->pointer_focus = &surface->base;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001041 drag->pointer_focus_time = time;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001042 drag->target = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001043}
1044
1045static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001046drag_offer_accept(struct wl_client *client,
1047 struct wl_drag_offer *offer, uint32_t time, const char *type)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001048{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001049 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001050 char **p, **end;
1051
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001052 /* If the client responds to drag pointer_focus or motion
1053 * events after the pointer has left the surface, we just
1054 * discard the accept requests. The drag source just won't
1055 * get the corresponding 'target' events and eventually the
1056 * next surface/root will start sending events. */
1057 if (time < drag->pointer_focus_time)
1058 return;
1059
1060 drag->target = client;
1061 drag->type = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001062 end = drag->types.data + drag->types.size;
1063 for (p = drag->types.data; p < end; p++)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001064 if (type && strcmp(*p, type) == 0)
1065 drag->type = *p;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001066
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001067 wl_client_post_event(drag->source->client, &drag->resource.base,
1068 WL_DRAG_TARGET, drag->type);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001069}
1070
1071static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001072drag_offer_receive(struct wl_client *client,
1073 struct wl_drag_offer *offer, int fd)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001074{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001075 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001076
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001077 wl_client_post_event(drag->source->client, &drag->resource.base,
1078 WL_DRAG_FINISH, fd);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001079 close(fd);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001080}
1081
Kristian Høgsbergd44bc8b2010-11-30 15:10:26 -05001082static void
1083drag_offer_reject(struct wl_client *client, struct wl_drag_offer *offer)
1084{
1085 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
1086
1087 wl_client_post_event(drag->source->client, &drag->resource.base,
1088 WL_DRAG_REJECT);
1089}
1090
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001091static const struct wl_drag_offer_interface drag_offer_interface = {
1092 drag_offer_accept,
Kristian Høgsbergd44bc8b2010-11-30 15:10:26 -05001093 drag_offer_receive,
1094 drag_offer_reject
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001095};
1096
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001097static void
1098drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
1099{
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001100 char **p;
1101
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001102 p = wl_array_add(&drag->types, sizeof *p);
1103 if (p)
1104 *p = strdup(type);
1105 if (!p || !*p)
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001106 wl_client_post_no_memory(client);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001107}
1108
1109static void
1110drag_activate(struct wl_client *client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001111 struct wl_drag *drag,
1112 struct wl_surface *surface,
1113 struct wl_input_device *input_device, uint32_t time)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001114{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001115 struct wl_display *display = wl_client_get_display (client);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001116 struct wlsc_input_device *device =
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001117 (struct wlsc_input_device *) input_device;
1118 struct wlsc_surface *target;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001119 int32_t sx, sy;
1120
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001121 if (device->grab != WLSC_DEVICE_GRAB_MOTION ||
Kristian Høgsberg26437072010-12-01 10:17:47 -05001122 device->base.pointer_focus != surface ||
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001123 device->grab_time != time)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001124 return;
1125
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001126 drag->source = surface;
1127 drag->input_device = input_device;
1128
1129 drag->drag_offer.base.interface = &wl_drag_offer_interface;
1130 drag->drag_offer.base.implementation =
1131 (void (**)(void)) &drag_offer_interface;
1132
1133 wl_display_add_object(display, &drag->drag_offer.base);
1134
1135 wlsc_input_device_start_grab(device, time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001136 WLSC_DEVICE_GRAB_DRAG);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001137
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001138 device->drag = drag;
1139 target = pick_surface(device, &sx, &sy);
1140 wl_drag_set_pointer_focus(device->drag, target, time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001141 device->x, device->y, sx, sy);
1142}
1143
1144static void
1145drag_cancel(struct wl_client *client, struct wl_drag *drag)
1146{
1147 struct wlsc_input_device *device =
1148 (struct wlsc_input_device *) drag->input_device;
1149
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001150 if (drag->source == NULL || drag->source->client != client)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001151 return;
1152
1153 wlsc_input_device_end_grab(device, get_time());
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001154 device->drag = NULL;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001155}
1156
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001157static const struct wl_drag_interface drag_interface = {
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001158 drag_offer,
1159 drag_activate,
1160 drag_cancel,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001161};
1162
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001163void
1164wlsc_input_device_init(struct wlsc_input_device *device,
1165 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001166{
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001167 device->base.base.interface = &wl_input_device_interface;
1168 device->base.base.implementation =
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001169 (void (**)(void)) &input_device_interface;
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001170 wl_display_add_object(ec->wl_display, &device->base.base);
1171 wl_display_add_global(ec->wl_display, &device->base.base, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001172
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001173 device->x = 100;
1174 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001175 device->ec = ec;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001176 device->sprite = wlsc_surface_create(ec, &ec->argb_visual,
1177 device->x, device->y, 32, 32);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001178 device->hotspot_x = 16;
1179 device->hotspot_y = 16;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001180
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001181 device->listener.func = handle_surface_destroy;
1182 wl_list_insert(ec->surface_destroy_listener_list.prev,
1183 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001184 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001185
1186 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001187}
1188
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001189static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001190wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001191{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001192 struct wlsc_output *output =
1193 container_of(global, struct wlsc_output, base);
1194
1195 wl_client_post_event(client, global,
1196 WL_OUTPUT_GEOMETRY,
1197 output->width, output->height);
1198}
1199
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001200static const char vertex_shader[] =
1201 "uniform mat4 proj;\n"
1202 "attribute vec4 position;\n"
1203 "attribute vec2 texcoord;\n"
1204 "varying vec2 v_texcoord;\n"
1205 "void main()\n"
1206 "{\n"
1207 " gl_Position = proj * position;\n"
1208 " v_texcoord = texcoord;\n"
1209 "}\n";
1210
1211static const char fragment_shader[] =
1212 /* "precision mediump float;\n" */
1213 "varying vec2 v_texcoord;\n"
1214 "uniform sampler2D tex;\n"
1215 "void main()\n"
1216 "{\n"
1217 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1218 "}\n";
1219
Kristian Høgsberga9468212010-06-14 21:03:11 -04001220static int
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001221init_shaders(struct wlsc_compositor *ec)
1222{
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001223 GLuint v, f, program;
1224 const char *p;
1225 char msg[512];
1226 GLfloat vertices[4 * 5];
1227 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001228
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001229 p = vertex_shader;
1230 v = glCreateShader(GL_VERTEX_SHADER);
1231 glShaderSource(v, 1, &p, NULL);
1232 glCompileShader(v);
1233 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
1234 if (!status) {
1235 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
1236 fprintf(stderr, "vertex shader info: %s\n", msg);
1237 return -1;
1238 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001239
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001240 p = fragment_shader;
1241 f = glCreateShader(GL_FRAGMENT_SHADER);
1242 glShaderSource(f, 1, &p, NULL);
1243 glCompileShader(f);
1244 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
1245 if (!status) {
1246 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
1247 fprintf(stderr, "fragment shader info: %s\n", msg);
1248 return -1;
1249 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001250
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001251 program = glCreateProgram();
1252 glAttachShader(program, v);
1253 glAttachShader(program, f);
1254 glBindAttribLocation(program, 0, "position");
1255 glBindAttribLocation(program, 1, "texcoord");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001256
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001257 glLinkProgram(program);
1258 glGetProgramiv(program, GL_LINK_STATUS, &status);
1259 if (!status) {
1260 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1261 fprintf(stderr, "link 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 glUseProgram(program);
1266 ec->proj_uniform = glGetUniformLocation(program, "proj");
1267 ec->tex_uniform = glGetUniformLocation(program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001268
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001269 vertices[ 0] = 0.0;
1270 vertices[ 1] = 0.0;
1271 vertices[ 2] = 0.0;
1272 vertices[ 3] = 0.0;
1273 vertices[ 4] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001274
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001275 vertices[ 5] = 0.0;
1276 vertices[ 6] = 1.0;
1277 vertices[ 7] = 0.0;
1278 vertices[ 8] = 0.0;
1279 vertices[ 9] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001280
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001281 vertices[10] = 1.0;
1282 vertices[11] = 0.0;
1283 vertices[12] = 0.0;
1284 vertices[13] = 1.0;
1285 vertices[14] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001286
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001287 vertices[15] = 1.0;
1288 vertices[16] = 1.0;
1289 vertices[17] = 0.0;
1290 vertices[18] = 1.0;
1291 vertices[19] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001292
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001293 glGenBuffers(1, &ec->vbo);
1294 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
1295 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001296
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001297 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001298}
1299
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001300static const struct wl_interface visual_interface = {
1301 "visual", 1,
1302};
1303
1304static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001305add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001306{
1307 ec->argb_visual.base.interface = &visual_interface;
1308 ec->argb_visual.base.implementation = NULL;
1309 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001310 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001311
1312 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1313 ec->premultiplied_argb_visual.base.implementation = NULL;
1314 wl_display_add_object(ec->wl_display,
1315 &ec->premultiplied_argb_visual.base);
1316 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001317 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001318
1319 ec->rgb_visual.base.interface = &visual_interface;
1320 ec->rgb_visual.base.implementation = NULL;
1321 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001322 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1323}
1324
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001325void
1326wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1327 int x, int y, int width, int height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001328{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001329 output->compositor = c;
1330 output->x = x;
1331 output->y = y;
1332 output->width = width;
1333 output->height = height;
1334
1335 output->background =
1336 background_create(output, option_background);
1337
1338 wlsc_matrix_init(&output->matrix);
1339 wlsc_matrix_translate(&output->matrix,
1340 -output->x - output->width / 2.0,
1341 -output->y - output->height / 2.0, 0);
1342 wlsc_matrix_scale(&output->matrix,
1343 2.0 / output->width, 2.0 / output->height, 1);
1344
1345 output->base.interface = &wl_output_interface;
1346 wl_display_add_object(c->wl_display, &output->base);
1347 wl_display_add_global(c->wl_display, &output->base,
1348 wlsc_output_post_geometry);
1349}
1350
1351int
1352wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1353{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001354 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001355
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001356 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001357
Kristian Høgsbergf8ffded2010-09-03 15:15:33 -04001358 ec->base.base.interface = &wl_compositor_interface;
1359 ec->base.base.implementation =
1360 (void (**)(void)) &compositor_interface;
1361
1362 wl_display_add_object(display, &ec->base.base);
1363 if (wl_display_add_global(display, &ec->base.base, NULL))
1364 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001365
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001366 wlsc_shm_init(ec);
1367
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001368 ec->shell.base.interface = &wl_shell_interface;
1369 ec->shell.base.implementation = (void (**)(void)) &shell_interface;
1370 wl_display_add_object(display, &ec->shell.base);
1371 if (wl_display_add_global(display, &ec->shell.base, NULL))
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001372 return -1;
1373
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001374 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001375
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001376 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001377 wl_list_init(&ec->input_device_list);
1378 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001379 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001380
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001381 create_pointer_images(ec);
1382
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001383 screenshooter_create(ec);
1384
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001385 glGenFramebuffers(1, &ec->fbo);
1386 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1387 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001388 if (init_shaders(ec) < 0)
1389 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001390
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001391 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001392 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001393 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001394
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001395 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001396}
1397
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001398static void on_term_signal(int signal_number, void *data)
1399{
1400 struct wlsc_compositor *ec = data;
1401
1402 wl_display_terminate(ec->wl_display);
1403}
1404
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001405int main(int argc, char *argv[])
1406{
1407 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001408 struct wlsc_compositor *ec;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001409 struct wl_event_loop *loop;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001410 GError *error = NULL;
1411 GOptionContext *context;
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001412 int width, height;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001413
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001414 g_type_init(); /* GdkPixbuf needs this, it seems. */
1415
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001416 context = g_option_context_new(NULL);
1417 g_option_context_add_main_entries(context, option_entries, "Wayland");
1418 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1419 fprintf(stderr, "option parsing failed: %s\n", error->message);
1420 exit(EXIT_FAILURE);
1421 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001422 if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) {
1423 fprintf(stderr, "invalid geometry option: %s \n",
1424 option_geometry);
1425 exit(EXIT_FAILURE);
1426 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001427
1428 display = wl_display_create();
1429
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001430 if (getenv("WAYLAND_DISPLAY"))
1431 ec = wayland_compositor_create(display, width, height);
1432 else if (getenv("DISPLAY"))
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001433 ec = x11_compositor_create(display, width, height);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001434 else
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001435 ec = drm_compositor_create(display, option_connector);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001436
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001437 if (ec == NULL) {
1438 fprintf(stderr, "failed to create compositor\n");
1439 exit(EXIT_FAILURE);
1440 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001441
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001442 if (wl_display_add_socket(display, option_socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001443 fprintf(stderr, "failed to add socket: %m\n");
1444 exit(EXIT_FAILURE);
1445 }
1446
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001447 loop = wl_display_get_event_loop(ec->wl_display);
1448 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
1449 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
1450
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001451 wl_display_run(display);
1452
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001453 wl_display_destroy(display);
1454
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001455 ec->destroy(ec);
1456
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001457 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001458}