blob: ff242245b9f75c91bd04b0604bcc7780d08e208d [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05008 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050017 */
18
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040019#include <stdio.h>
20#include <string.h>
21#include <stdlib.h>
22#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050023#include <stdarg.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040024#include <sys/ioctl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040025#include <fcntl.h>
26#include <unistd.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050027#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050028#include <math.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050029#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040030#include <linux/input.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050031
Kristian Høgsberga1f3f602010-08-03 09:26:44 -040032#include "wayland-server-protocol.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040033#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050034
Kristian Høgsberg61a82512010-10-26 11:26:44 -040035static const char *option_background = "background.jpg";
36static const char *option_geometry = "1024x640";
37static int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050038
39static const GOptionEntry option_entries[] = {
40 { "background", 'b', 0, G_OPTION_ARG_STRING,
41 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -040042 { "connector", 'c', 0, G_OPTION_ARG_INT,
43 &option_connector, "KMS connector" },
Kristian Høgsberg61a82512010-10-26 11:26:44 -040044 { "geometry", 'g', 0, G_OPTION_ARG_STRING,
45 &option_geometry, "Geometry" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050046 { NULL }
47};
48
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050049static void
Kristian Høgsberg1db21f12010-08-16 16:08:12 -040050wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
51 struct wlsc_surface *surface,
52 uint32_t time,
53 int32_t x, int32_t y,
54 int32_t sx, int32_t sy);
55
56static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050057wlsc_matrix_init(struct wlsc_matrix *matrix)
58{
59 static const struct wlsc_matrix identity = {
60 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
61 };
62
63 memcpy(matrix, &identity, sizeof identity);
64}
65
66static void
67wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
68{
69 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040070 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050071 div_t d;
72 int i, j;
73
74 for (i = 0; i < 16; i++) {
75 tmp.d[i] = 0;
76 d = div(i, 4);
77 row = m->d + d.quot * 4;
78 column = n->d + d.rem;
79 for (j = 0; j < 4; j++)
80 tmp.d[i] += row[j] * column[j * 4];
81 }
82 memcpy(m, &tmp, sizeof tmp);
83}
84
85static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040086wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050087{
Kristian Høgsberg27803c62010-06-06 22:23:21 -040088 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050089 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
90 };
91
92 wlsc_matrix_multiply(matrix, &translate);
93}
94
95static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040096wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050097{
98 struct wlsc_matrix scale = {
99 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
100 };
101
102 wlsc_matrix_multiply(matrix, &scale);
103}
104
105static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400106wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
107{
108 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400109 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400110
111 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400112 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400113 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400114 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400115 }
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400116
117 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400118}
119
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400120static struct wlsc_surface *
121wlsc_surface_create(struct wlsc_compositor *compositor,
122 struct wl_visual *visual,
123 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500124{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400125 struct wlsc_surface *surface;
126
127 surface = malloc(sizeof *surface);
128 if (surface == NULL)
129 return NULL;
130
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500131 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400132 glBindTexture(GL_TEXTURE_2D, surface->texture);
133 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
134 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
135 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
136 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
137
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500138 surface->compositor = compositor;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500139 surface->visual = visual;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400140 surface->x = x;
141 surface->y = y;
142 surface->width = width;
143 surface->height = height;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400144 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400145 wlsc_matrix_scale(&surface->matrix, width, height, 1);
146 wlsc_matrix_translate(&surface->matrix, x, y, 0);
147
148 wlsc_matrix_init(&surface->matrix_inv);
149 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
150 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500151
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400152 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500153}
154
155static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400156destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500157{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400158 struct wlsc_surface *surface =
159 container_of(resource, struct wlsc_surface, base.base);
160 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500161 struct wlsc_listener *l;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400162
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400163 wl_list_remove(&surface->link);
164 glDeleteTextures(1, &surface->texture);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400165
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500166 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400167 l->func(l, surface);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400168
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400169 free(surface);
Kristian Høgsberg117d5132010-08-11 08:56:47 -0400170
171 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500172}
173
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400174static int
175texture_from_png(const char *filename, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500176{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400177 GdkPixbuf *pixbuf;
178 GError *error = NULL;
179 void *data;
180 GLenum format;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500181
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400182 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
183 width, height,
184 FALSE, &error);
185 if (error != NULL)
186 return -1;
187
188 data = gdk_pixbuf_get_pixels(pixbuf);
189
190 if (gdk_pixbuf_get_has_alpha(pixbuf))
191 format = GL_RGBA;
192 else
193 format = GL_RGB;
194
Chia-I Wu1f411902010-10-29 15:20:16 +0800195 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
196 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400197
198 gdk_pixbuf_unref(pixbuf);
199
200 return 0;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500201}
202
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400203static const struct {
204 const char *filename;
205 int hotspot_x, hotspot_y;
206} pointer_images[] = {
Kristian Høgsberg4219a402010-08-16 16:43:03 -0400207 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
208 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
209 { DATADIR "/wayland/bottom_side.png", 16, 20 },
210 { DATADIR "/wayland/grabbing.png", 20, 17 },
211 { DATADIR "/wayland/left_ptr.png", 10, 5 },
212 { DATADIR "/wayland/left_side.png", 10, 20 },
213 { DATADIR "/wayland/right_side.png", 30, 19 },
214 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
215 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
216 { DATADIR "/wayland/top_side.png", 18, 8 },
217 { DATADIR "/wayland/xterm.png", 15, 15 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400218};
219
220static void
221create_pointer_images(struct wlsc_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500222{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400223 int i, count;
224 GLuint texture;
225 const int width = 32, height = 32;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400226
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400227 glGenTextures(1, &texture);
228 glBindTexture(GL_TEXTURE_2D, texture);
229 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400233
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400234 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400235 ec->pointer_buffers = malloc(count * sizeof *ec->pointer_buffers);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400236 for (i = 0; i < count; i++) {
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400237 ec->pointer_buffers[i] =
238 wlsc_drm_buffer_create(ec, width, height,
239 &ec->argb_visual);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400240 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400241 ec->pointer_buffers[i]->image);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400242 texture_from_png(pointer_images[i].filename, width, height);
243 }
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400244}
245
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500246static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500247background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500248{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500249 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500250 GdkPixbuf *pixbuf;
251 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500252 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500253 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500254
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400255 background = wlsc_surface_create(output->compositor,
256 &output->compositor->rgb_visual,
257 output->x, output->y,
258 output->width, output->height);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500259 if (background == NULL)
260 return NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500261
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500262 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500263 output->width,
264 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500265 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500266 if (error != NULL) {
267 free(background);
268 return NULL;
269 }
270
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500271 data = gdk_pixbuf_get_pixels(pixbuf);
272
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400273 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500274 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500275 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500276 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500277
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500278 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
279 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500280 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500281
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500282 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500283}
284
285static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400286wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500287{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500288 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400289 struct wlsc_matrix tmp;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500290
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400291 tmp = es->matrix;
292 wlsc_matrix_multiply(&tmp, &output->matrix);
293 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
294 glUniform1i(ec->tex_uniform, 0);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500295
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500296 if (es->visual == &ec->argb_visual) {
297 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
298 glEnable(GL_BLEND);
299 } else if (es->visual == &ec->premultiplied_argb_visual) {
300 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
301 glEnable(GL_BLEND);
302 } else {
303 glDisable(GL_BLEND);
304 }
305
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500306 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400307 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
308 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
309 5 * sizeof(GLfloat), NULL);
310 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
311 5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
312 glEnableVertexAttribArray(0);
313 glEnableVertexAttribArray(1);
314 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500315}
316
317static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400318wlsc_surface_raise(struct wlsc_surface *surface)
319{
320 struct wlsc_compositor *compositor = surface->compositor;
321
322 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400323 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400324}
325
326static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400327wlsc_surface_update_matrix(struct wlsc_surface *es)
328{
329 wlsc_matrix_init(&es->matrix);
330 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
331 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
332
333 wlsc_matrix_init(&es->matrix_inv);
334 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
335 wlsc_matrix_scale(&es->matrix_inv,
336 1.0 / es->width, 1.0 / es->height, 1);
337}
338
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400339void
340wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400341{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400342 wl_display_post_frame(compositor->wl_display, msecs);
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400343 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400344}
345
346static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400347wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400348{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500349 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500350 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500351 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500352
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500353 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500354
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500355 if (output->background)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400356 wlsc_surface_draw(output->background, output);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500357 else
358 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400359
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400360 wl_list_for_each_reverse(es, &ec->surface_list, link)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400361 wlsc_surface_draw(es, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400362
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400363 if (ec->focus)
364 wl_list_for_each(eid, &ec->input_device_list, link)
365 wlsc_surface_draw(eid->sprite, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500366}
367
368static void
369repaint(void *data)
370{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500371 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500372 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500373
374 if (!ec->repaint_needed) {
375 ec->repaint_on_timeout = 0;
376 return;
377 }
378
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500379 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400380 wlsc_output_repaint(output);
381
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400382 ec->present(ec);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500383
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500384 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400385}
386
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400387void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400388wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400389{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400390 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400391 if (compositor->repaint_on_timeout)
392 return;
393
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400394 wl_event_source_timer_update(compositor->timer_source, 1);
395 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400396}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500397
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500398static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500399surface_destroy(struct wl_client *client,
400 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400401{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400402 wl_resource_destroy(&surface->base, client);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400403}
404
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500405static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500406surface_attach(struct wl_client *client,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400407 struct wl_surface *surface, struct wl_buffer *buffer)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400408{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500409 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400410
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400411 buffer->attach(buffer, surface);
412 es->buffer = buffer;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400413}
414
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500415static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500416surface_map(struct wl_client *client,
417 struct wl_surface *surface,
418 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400419{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500420 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400421
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400422 es->x = x;
423 es->y = y;
424 es->width = width;
425 es->height = height;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400426
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400427 wlsc_surface_update_matrix(es);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400428 wlsc_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400429}
430
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500431static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500432surface_damage(struct wl_client *client,
433 struct wl_surface *surface,
434 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500435{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400436 struct wlsc_surface *es = (struct wlsc_surface *) surface;
437
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400438 es->buffer->damage(es->buffer, surface, x, y, width, height);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400439 wlsc_compositor_schedule_repaint(es->compositor);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500440}
441
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500442const static struct wl_surface_interface surface_interface = {
443 surface_destroy,
444 surface_attach,
445 surface_map,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500446 surface_damage
447};
448
449static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400450wlsc_input_device_attach(struct wlsc_input_device *device,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400451 struct wl_buffer *buffer, int x, int y)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400452{
453 struct wlsc_compositor *ec = device->ec;
454
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400455 buffer->attach(buffer, &device->sprite->base);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400456 device->hotspot_x = x;
457 device->hotspot_y = y;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400458
459 device->sprite->x = device->x - device->hotspot_x;
460 device->sprite->y = device->y - device->hotspot_y;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400461 device->sprite->width = buffer->width;
462 device->sprite->height = buffer->height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400463 wlsc_surface_update_matrix(device->sprite);
464
465 wlsc_compositor_schedule_repaint(ec);
466}
467
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400468
469static void
470wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
471 enum wlsc_pointer_type type)
472{
473 struct wlsc_compositor *compositor = device->ec;
474
475 wlsc_input_device_attach(device,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400476 &compositor->pointer_buffers[type]->base,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400477 pointer_images[type].hotspot_x,
478 pointer_images[type].hotspot_y);
479}
480
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400481static void
482wlsc_input_device_start_grab(struct wlsc_input_device *device,
483 uint32_t time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400484 enum wlsc_grab_type grab)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400485{
486 device->grab = grab;
487 device->grab_surface = device->pointer_focus;
488 device->grab_dx = device->pointer_focus->x - device->grab_x;
489 device->grab_dy = device->pointer_focus->y - device->grab_y;
490 device->grab_width = device->pointer_focus->width;
491 device->grab_height = device->pointer_focus->height;
492
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400493 wlsc_input_device_set_pointer_focus(device,
494 (struct wlsc_surface *) &wl_grab_surface,
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400495 time, 0, 0, 0, 0);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400496}
497
498static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400499shell_move(struct wl_client *client, struct wl_shell *shell,
500 struct wl_surface *surface,
501 struct wl_input_device *device, uint32_t time)
502{
503 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
504
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400505 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
506 wd->grab_time != time ||
507 &wd->pointer_focus->base != surface)
508 return;
509
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400510 wlsc_input_device_start_grab(wd, time, WLSC_DEVICE_GRAB_MOVE);
511 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400512}
513
514static void
515shell_resize(struct wl_client *client, struct wl_shell *shell,
516 struct wl_surface *surface,
517 struct wl_input_device *device, uint32_t time, uint32_t edges)
518{
519 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400520 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400521
522 if (edges == 0 || edges > 15 ||
523 (edges & 3) == 3 || (edges & 12) == 12)
524 return;
525
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400526 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
527 wd->grab_time != time ||
528 &wd->pointer_focus->base != surface)
529 return;
530
531 switch (edges) {
532 case WLSC_DEVICE_GRAB_RESIZE_TOP:
533 pointer = WLSC_POINTER_TOP;
534 break;
535 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
536 pointer = WLSC_POINTER_BOTTOM;
537 break;
538 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
539 pointer = WLSC_POINTER_LEFT;
540 break;
541 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
542 pointer = WLSC_POINTER_TOP_LEFT;
543 break;
544 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
545 pointer = WLSC_POINTER_BOTTOM_LEFT;
546 break;
547 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
548 pointer = WLSC_POINTER_RIGHT;
549 break;
550 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
551 pointer = WLSC_POINTER_TOP_RIGHT;
552 break;
553 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
554 pointer = WLSC_POINTER_BOTTOM_RIGHT;
555 break;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400556 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400557
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400558 wlsc_input_device_start_grab(wd, time, edges);
559 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400560}
561
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500562static uint32_t
563get_time(void)
564{
565 struct timeval tv;
566
567 gettimeofday(&tv, NULL);
568
569 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
570}
571
572struct wlsc_drag {
573 struct wl_drag drag;
574 struct wlsc_listener listener;
575};
576
577static void
578wl_drag_set_pointer_focus(struct wl_drag *drag,
579 struct wlsc_surface *surface, uint32_t time,
580 int32_t x, int32_t y, int32_t sx, int32_t sy);
581
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400582static void
583destroy_drag(struct wl_resource *resource, struct wl_client *client)
584{
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500585 struct wlsc_drag *drag =
586 container_of(resource, struct wlsc_drag, drag.resource);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400587
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500588 wl_list_remove(&drag->listener.link);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400589
590 free(drag);
591}
592
593const static struct wl_drag_interface drag_interface;
594
595static void
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500596drag_handle_surface_destroy(struct wlsc_listener *listener,
597 struct wlsc_surface *surface)
598{
599 struct wlsc_drag *drag =
600 container_of(listener, struct wlsc_drag, listener);
601 uint32_t time = get_time();
602
603 if (drag->drag.pointer_focus == &surface->base)
604 wl_drag_set_pointer_focus(&drag->drag, NULL, time, 0, 0, 0, 0);
605}
606
607static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400608shell_create_drag(struct wl_client *client,
609 struct wl_shell *shell, uint32_t id)
610{
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500611 struct wlsc_drag *drag;
612 struct wlsc_compositor *ec =
613 container_of(shell, struct wlsc_compositor, shell);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400614
615 drag = malloc(sizeof *drag);
616 if (drag == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400617 wl_client_post_no_memory(client);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400618 return;
619 }
620
621 memset(drag, 0, sizeof *drag);
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500622 drag->drag.resource.base.id = id;
623 drag->drag.resource.base.interface = &wl_drag_interface;
624 drag->drag.resource.base.implementation =
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400625 (void (**)(void)) &drag_interface;
626
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500627 drag->drag.resource.destroy = destroy_drag;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400628
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500629 drag->listener.func = drag_handle_surface_destroy;
630 wl_list_insert(ec->surface_destroy_listener_list.prev,
631 &drag->listener.link);
632
633 wl_client_add_resource(client, &drag->drag.resource);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400634}
635
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400636const static struct wl_shell_interface shell_interface = {
637 shell_move,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400638 shell_resize,
639 shell_create_drag
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400640};
641
642static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500643compositor_create_surface(struct wl_client *client,
644 struct wl_compositor *compositor, uint32_t id)
645{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500646 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400647 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500648
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400649 surface = wlsc_surface_create(ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400650 if (surface == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400651 wl_client_post_no_memory(client);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500652 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400653 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500654
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400655 wl_list_insert(ec->surface_list.prev, &surface->link);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400656 surface->base.base.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400657
658 surface->base.base.base.id = id;
659 surface->base.base.base.interface = &wl_surface_interface;
660 surface->base.base.base.implementation =
661 (void (**)(void)) &surface_interface;
662 surface->base.client = client;
663
664 wl_client_add_resource(client, &surface->base.base);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500665}
666
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500667const static struct wl_compositor_interface compositor_interface = {
668 compositor_create_surface,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500669};
670
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500671static void
672wlsc_surface_transform(struct wlsc_surface *surface,
673 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
674{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400675 struct wlsc_vector v = { { x, y, 0, 1 } };
676
677 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400678 *sx = v.f[0] * surface->width;
679 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500680}
681
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500682static void
683wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400684 struct wlsc_surface *surface,
685 uint32_t time)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500686{
687 if (device->keyboard_focus == surface)
688 return;
689
690 if (device->keyboard_focus &&
691 (!surface || device->keyboard_focus->base.client != surface->base.client))
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400692 wl_client_post_event(device->keyboard_focus->base.client,
693 &device->base.base,
694 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
695 time, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500696
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500697 if (surface)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400698 wl_client_post_event(surface->base.client,
699 &device->base.base,
700 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
701 time, &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500702
703 device->keyboard_focus = surface;
704}
705
706static void
707wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400708 struct wlsc_surface *surface,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400709 uint32_t time,
710 int32_t x, int32_t y,
711 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500712{
713 if (device->pointer_focus == surface)
714 return;
715
716 if (device->pointer_focus &&
717 (!surface || device->pointer_focus->base.client != surface->base.client))
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400718 wl_client_post_event(device->pointer_focus->base.client,
719 &device->base.base,
720 WL_INPUT_DEVICE_POINTER_FOCUS,
721 time, NULL, 0, 0, 0, 0);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500722 if (surface)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400723 wl_client_post_event(surface->base.client,
724 &device->base.base,
725 WL_INPUT_DEVICE_POINTER_FOCUS,
726 time, &surface->base,
727 x, y, sx, sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500728
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400729 if (!surface)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400730 wlsc_input_device_set_pointer_image(device,
731 WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400732
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500733 device->pointer_focus = surface;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400734 device->pointer_focus_time = time;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500735}
736
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500737static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500738pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500739{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500740 struct wlsc_compositor *ec = device->ec;
741 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500742
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400743 wl_list_for_each(es, &ec->surface_list, link) {
744 wlsc_surface_transform(es, device->x, device->y, sx, sy);
745 if (0 <= *sx && *sx < es->width &&
746 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500747 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400748 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500749
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500750 return NULL;
751}
752
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500753void
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400754notify_motion(struct wlsc_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500755{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500756 struct wlsc_surface *es;
757 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500758 struct wlsc_output *output;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400759 int32_t sx, sy, width, height;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500760
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500761 /* FIXME: We need some multi head love here. */
762 output = container_of(ec->output_list.next, struct wlsc_output, link);
763 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500764 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500765 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500766 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500767 if (x >= output->x + output->width)
768 x = output->x + output->width - 1;
769 if (y >= output->y + output->height)
770 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500771
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500772 device->x = x;
773 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500774
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400775 switch (device->grab) {
776 case WLSC_DEVICE_GRAB_NONE:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400777 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400778 wlsc_input_device_set_pointer_focus(device, es,
779 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400780 if (es)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400781 wl_client_post_event(es->base.client,
782 &device->base.base,
783 WL_INPUT_DEVICE_MOTION,
784 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400785 break;
786
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400787 case WLSC_DEVICE_GRAB_MOTION:
788 es = device->pointer_focus;
789 wlsc_surface_transform(es, x, y, &sx, &sy);
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400790 wl_client_post_event(es->base.client,
791 &device->base.base,
792 WL_INPUT_DEVICE_MOTION,
793 time, x, y, sx, sy);
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400794 break;
795
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400796 case WLSC_DEVICE_GRAB_MOVE:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400797 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400798 es->x = x + device->grab_dx;
799 es->y = y + device->grab_dy;;
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400800 wl_client_post_event(es->base.client,
801 &ec->shell.base,
802 WL_SHELL_CONFIGURE,
803 time, device->grab,
804 &es->base, es->x, es->y,
805 es->width, es->height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400806
807 wlsc_surface_update_matrix(es);
808
809 break;
810
811 case WLSC_DEVICE_GRAB_RESIZE_TOP:
812 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
813 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
814 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
815 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
816 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
817 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
818 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
819 case WLSC_DEVICE_GRAB_RESIZE_MASK:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400820 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400821
822 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_LEFT) {
823 sx = x + device->grab_dx;
824 width = device->grab_x - x + device->grab_width;
825 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_RIGHT) {
826 sx = device->grab_x + device->grab_dx;
827 width = x - device->grab_x + device->grab_width;
828 } else {
829 sx = device->grab_x + device->grab_dx;
830 width = device->grab_width;
831 }
832
833 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_TOP) {
834 sy = y + device->grab_dy;
835 height = device->grab_y - y + device->grab_height;
836 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_BOTTOM) {
837 sy = device->grab_y + device->grab_dy;
838 height = y - device->grab_y + device->grab_height;
839 } else {
840 sy = device->grab_y + device->grab_dy;
841 height = device->grab_height;
842 }
843
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400844 wl_client_post_event(es->base.client,
845 &ec->shell.base,
846 WL_SHELL_CONFIGURE, time, device->grab,
847 &es->base, sx, sy, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400848 break;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400849
850 case WLSC_DEVICE_GRAB_DRAG:
851 es = pick_surface(device, &sx, &sy);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400852 wl_drag_set_pointer_focus(device->drag,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400853 es, time, x, y, sx, sy);
854 if (es)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400855 wl_client_post_event(es->base.client,
856 &device->drag->drag_offer.base,
857 WL_DRAG_OFFER_MOTION,
858 time, x, y, sx, sy);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400859 break;
860
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400861 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500862
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400863 device->sprite->x = device->x - device->hotspot_x;
864 device->sprite->y = device->y - device->hotspot_y;
865 wlsc_surface_update_matrix(device->sprite);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500866
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400867 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500868}
869
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400870static void
871wlsc_input_device_end_grab(struct wlsc_input_device *device, uint32_t time)
872{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400873 struct wl_drag *drag = device->drag;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400874 struct wlsc_surface *es;
875 int32_t sx, sy;
876
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400877 switch (device->grab) {
878 case WLSC_DEVICE_GRAB_DRAG:
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400879 if (drag->target)
880 wl_client_post_event(drag->target,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400881 &drag->drag_offer.base,
882 WL_DRAG_OFFER_DROP);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400883 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400884 device->drag = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400885 break;
886 default:
887 break;
888 }
889
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400890 device->grab = WLSC_DEVICE_GRAB_NONE;
891 es = pick_surface(device, &sx, &sy);
892 wlsc_input_device_set_pointer_focus(device, es, time,
893 device->x, device->y, sx, sy);
894}
895
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500896void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500897notify_button(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400898 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500899{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400900 struct wlsc_surface *surface;
901 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500902
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400903 surface = device->pointer_focus;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400904 if (surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400905 if (state && device->grab == WLSC_DEVICE_GRAB_NONE) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400906 wlsc_surface_raise(surface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400907 device->grab = WLSC_DEVICE_GRAB_MOTION;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400908 device->grab_button = button;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400909 device->grab_time = time;
910 device->grab_x = device->x;
911 device->grab_y = device->y;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400912 wlsc_input_device_set_keyboard_focus(device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400913 surface, time);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500914 }
915
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400916 if (state && button == BTN_LEFT &&
917 device->grab == WLSC_DEVICE_GRAB_MOTION &&
918 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400919 shell_move(NULL, (struct wl_shell *) &compositor->shell,
920 &surface->base, &device->base, time);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400921 else if (state && button == BTN_MIDDLE &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400922 device->grab == WLSC_DEVICE_GRAB_MOTION &&
923 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400924 shell_resize(NULL, (struct wl_shell *) &compositor->shell,
925
926 &surface->base, &device->base, time,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400927 WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400928 else if (device->grab == WLSC_DEVICE_GRAB_NONE ||
929 device->grab == WLSC_DEVICE_GRAB_MOTION)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400930 wl_client_post_event(surface->base.client,
931 &device->base.base,
932 WL_INPUT_DEVICE_BUTTON,
933 time, button, state);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500934
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400935 if (!state &&
936 device->grab != WLSC_DEVICE_GRAB_NONE &&
937 device->grab_button == button) {
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400938 wlsc_input_device_end_grab(device, time);
939 }
940
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400941 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500942 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500943}
944
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500945void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500946notify_key(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400947 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500948{
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500949 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400950 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500951
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400952 switch (key | device->modifier_state) {
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400953 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400954 kill(0, SIGTERM);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500955 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500956 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500957
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400958 switch (key) {
959 case KEY_LEFTCTRL:
960 case KEY_RIGHTCTRL:
961 modifier = MODIFIER_CTRL;
962 break;
963
964 case KEY_LEFTALT:
965 case KEY_RIGHTALT:
966 modifier = MODIFIER_ALT;
967 break;
968
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400969 case KEY_LEFTMETA:
970 case KEY_RIGHTMETA:
971 modifier = MODIFIER_SUPER;
972 break;
973
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400974 default:
975 modifier = 0;
976 break;
977 }
978
979 if (state)
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400980 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400981 else
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400982 device->modifier_state &= ~modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400983
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500984 end = device->keys.data + device->keys.size;
985 for (k = device->keys.data; k < end; k++) {
986 if (*k == key)
987 *k = *--end;
988 }
989 device->keys.size = (void *) end - device->keys.data;
990 if (state) {
991 k = wl_array_add(&device->keys, sizeof *k);
992 *k = key;
993 }
Ray Strodee96dcb82008-12-20 02:00:49 -0500994
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500995 if (device->keyboard_focus != NULL)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400996 wl_client_post_event(device->keyboard_focus->base.client,
997 &device->base.base,
998 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400999}
1000
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001001static void
1002input_device_attach(struct wl_client *client,
1003 struct wl_input_device *device_base,
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001004 uint32_t time,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001005 struct wl_buffer *buffer, int32_t x, int32_t y)
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001006{
1007 struct wlsc_input_device *device =
1008 (struct wlsc_input_device *) device_base;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001009
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001010 if (time < device->pointer_focus_time)
1011 return;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001012 if (device->pointer_focus == NULL)
1013 return;
1014 if (device->pointer_focus->base.client != client &&
1015 !(&device->pointer_focus->base == &wl_grab_surface &&
1016 device->grab_surface->base.client == client))
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001017 return;
1018
Kristian Høgsbergf4cb2012010-08-16 17:46:25 -04001019 if (buffer == NULL) {
1020 wlsc_input_device_set_pointer_image(device,
1021 WLSC_POINTER_LEFT_PTR);
1022 return;
1023 }
1024
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001025 wlsc_input_device_attach(device, buffer, x, y);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001026}
1027
1028const static struct wl_input_device_interface input_device_interface = {
1029 input_device_attach,
1030};
1031
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001032static void
1033handle_surface_destroy(struct wlsc_listener *listener,
1034 struct wlsc_surface *surface)
1035{
1036 struct wlsc_input_device *device =
1037 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001038 uint32_t time = get_time();
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001039
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001040 if (device->keyboard_focus == surface)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001041 wlsc_input_device_set_keyboard_focus(device, NULL, time);
Kristian Høgsberg3d465342010-11-22 13:58:46 -05001042 if (device->pointer_focus == surface)
1043 wlsc_input_device_set_pointer_focus(device, NULL, time,
1044 0, 0, 0, 0);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001045 if (device->pointer_focus == surface ||
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001046 (&device->pointer_focus->base == &wl_grab_surface &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001047 device->grab_surface == surface))
1048 wlsc_input_device_end_grab(device, time);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001049}
1050
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001051static void
1052wl_drag_set_pointer_focus(struct wl_drag *drag,
1053 struct wlsc_surface *surface, uint32_t time,
1054 int32_t x, int32_t y, int32_t sx, int32_t sy)
1055{
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001056 char **p, **end;
1057
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001058 if (drag->pointer_focus == &surface->base)
1059 return;
1060
1061 if (drag->pointer_focus &&
1062 (!surface || drag->pointer_focus->client != surface->base.client))
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001063 wl_client_post_event(drag->pointer_focus->client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001064 &drag->drag_offer.base,
1065 WL_DRAG_OFFER_POINTER_FOCUS,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001066 time, NULL, 0, 0, 0, 0);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001067
1068 if (surface &&
1069 (!drag->pointer_focus ||
1070 drag->pointer_focus->client != surface->base.client)) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001071 wl_client_post_global(surface->base.client,
1072 &drag->drag_offer.base);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001073
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001074 end = drag->types.data + drag->types.size;
1075 for (p = drag->types.data; p < end; p++)
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001076 wl_client_post_event(surface->base.client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001077 &drag->drag_offer.base,
1078 WL_DRAG_OFFER_OFFER, *p);
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001079 }
1080
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001081 if (surface) {
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001082 wl_client_post_event(surface->base.client,
1083 &drag->drag_offer.base,
1084 WL_DRAG_OFFER_POINTER_FOCUS,
1085 time, &surface->base,
1086 x, y, sx, sy);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001087
1088 }
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001089
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001090 drag->pointer_focus = &surface->base;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001091 drag->pointer_focus_time = time;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001092 drag->target = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001093}
1094
1095static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001096drag_offer_accept(struct wl_client *client,
1097 struct wl_drag_offer *offer, uint32_t time, const char *type)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001098{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001099 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001100 char **p, **end;
1101
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001102 /* If the client responds to drag pointer_focus or motion
1103 * events after the pointer has left the surface, we just
1104 * discard the accept requests. The drag source just won't
1105 * get the corresponding 'target' events and eventually the
1106 * next surface/root will start sending events. */
1107 if (time < drag->pointer_focus_time)
1108 return;
1109
1110 drag->target = client;
1111 drag->type = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001112 end = drag->types.data + drag->types.size;
1113 for (p = drag->types.data; p < end; p++)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001114 if (type && strcmp(*p, type) == 0)
1115 drag->type = *p;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001116
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001117 wl_client_post_event(drag->source->client, &drag->resource.base,
1118 WL_DRAG_TARGET, drag->type);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001119}
1120
1121static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001122drag_offer_receive(struct wl_client *client,
1123 struct wl_drag_offer *offer, int fd)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001124{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001125 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001126
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001127 wl_client_post_event(drag->source->client, &drag->resource.base,
1128 WL_DRAG_FINISH, fd);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001129 close(fd);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001130}
1131
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001132static const struct wl_drag_offer_interface drag_offer_interface = {
1133 drag_offer_accept,
1134 drag_offer_receive
1135};
1136
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001137static void
1138drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
1139{
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001140 char **p;
1141
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001142 p = wl_array_add(&drag->types, sizeof *p);
1143 if (p)
1144 *p = strdup(type);
1145 if (!p || !*p)
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001146 wl_client_post_no_memory(client);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001147}
1148
1149static void
1150drag_activate(struct wl_client *client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001151 struct wl_drag *drag,
1152 struct wl_surface *surface,
1153 struct wl_input_device *input_device, uint32_t time)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001154{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001155 struct wl_display *display = wl_client_get_display (client);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001156 struct wlsc_input_device *device =
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001157 (struct wlsc_input_device *) input_device;
1158 struct wlsc_surface *target;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001159 int32_t sx, sy;
1160
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001161 if (device->grab != WLSC_DEVICE_GRAB_MOTION ||
1162 &device->pointer_focus->base != surface ||
1163 device->grab_time != time)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001164 return;
1165
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001166 drag->source = surface;
1167 drag->input_device = input_device;
1168
1169 drag->drag_offer.base.interface = &wl_drag_offer_interface;
1170 drag->drag_offer.base.implementation =
1171 (void (**)(void)) &drag_offer_interface;
1172
1173 wl_display_add_object(display, &drag->drag_offer.base);
1174
1175 wlsc_input_device_start_grab(device, time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001176 WLSC_DEVICE_GRAB_DRAG);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001177
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001178 device->drag = drag;
1179 target = pick_surface(device, &sx, &sy);
1180 wl_drag_set_pointer_focus(device->drag, target, time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001181 device->x, device->y, sx, sy);
1182}
1183
1184static void
1185drag_cancel(struct wl_client *client, struct wl_drag *drag)
1186{
1187 struct wlsc_input_device *device =
1188 (struct wlsc_input_device *) drag->input_device;
1189
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001190 if (drag->source == NULL || drag->source->client != client)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001191 return;
1192
1193 wlsc_input_device_end_grab(device, get_time());
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001194 device->drag = NULL;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001195}
1196
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001197static const struct wl_drag_interface drag_interface = {
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001198 drag_offer,
1199 drag_activate,
1200 drag_cancel,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001201};
1202
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001203void
1204wlsc_input_device_init(struct wlsc_input_device *device,
1205 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001206{
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001207 device->base.base.interface = &wl_input_device_interface;
1208 device->base.base.implementation =
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001209 (void (**)(void)) &input_device_interface;
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001210 wl_display_add_object(ec->wl_display, &device->base.base);
1211 wl_display_add_global(ec->wl_display, &device->base.base, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001212
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001213 device->x = 100;
1214 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001215 device->ec = ec;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001216 device->sprite = wlsc_surface_create(ec, &ec->argb_visual,
1217 device->x, device->y, 32, 32);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001218 device->hotspot_x = 16;
1219 device->hotspot_y = 16;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001220
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001221 device->listener.func = handle_surface_destroy;
1222 wl_list_insert(ec->surface_destroy_listener_list.prev,
1223 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001224 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001225
1226 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001227}
1228
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001229static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001230wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001231{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001232 struct wlsc_output *output =
1233 container_of(global, struct wlsc_output, base);
1234
1235 wl_client_post_event(client, global,
1236 WL_OUTPUT_GEOMETRY,
1237 output->width, output->height);
1238}
1239
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001240static const char vertex_shader[] =
1241 "uniform mat4 proj;\n"
1242 "attribute vec4 position;\n"
1243 "attribute vec2 texcoord;\n"
1244 "varying vec2 v_texcoord;\n"
1245 "void main()\n"
1246 "{\n"
1247 " gl_Position = proj * position;\n"
1248 " v_texcoord = texcoord;\n"
1249 "}\n";
1250
1251static const char fragment_shader[] =
1252 /* "precision mediump float;\n" */
1253 "varying vec2 v_texcoord;\n"
1254 "uniform sampler2D tex;\n"
1255 "void main()\n"
1256 "{\n"
1257 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1258 "}\n";
1259
Kristian Høgsberga9468212010-06-14 21:03:11 -04001260static int
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001261init_shaders(struct wlsc_compositor *ec)
1262{
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001263 GLuint v, f, program;
1264 const char *p;
1265 char msg[512];
1266 GLfloat vertices[4 * 5];
1267 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001268
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001269 p = vertex_shader;
1270 v = glCreateShader(GL_VERTEX_SHADER);
1271 glShaderSource(v, 1, &p, NULL);
1272 glCompileShader(v);
1273 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
1274 if (!status) {
1275 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
1276 fprintf(stderr, "vertex shader info: %s\n", msg);
1277 return -1;
1278 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001279
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001280 p = fragment_shader;
1281 f = glCreateShader(GL_FRAGMENT_SHADER);
1282 glShaderSource(f, 1, &p, NULL);
1283 glCompileShader(f);
1284 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
1285 if (!status) {
1286 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
1287 fprintf(stderr, "fragment shader info: %s\n", msg);
1288 return -1;
1289 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001290
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001291 program = glCreateProgram();
1292 glAttachShader(program, v);
1293 glAttachShader(program, f);
1294 glBindAttribLocation(program, 0, "position");
1295 glBindAttribLocation(program, 1, "texcoord");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001296
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001297 glLinkProgram(program);
1298 glGetProgramiv(program, GL_LINK_STATUS, &status);
1299 if (!status) {
1300 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1301 fprintf(stderr, "link info: %s\n", msg);
1302 return -1;
1303 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001304
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001305 glUseProgram(program);
1306 ec->proj_uniform = glGetUniformLocation(program, "proj");
1307 ec->tex_uniform = glGetUniformLocation(program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001308
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001309 vertices[ 0] = 0.0;
1310 vertices[ 1] = 0.0;
1311 vertices[ 2] = 0.0;
1312 vertices[ 3] = 0.0;
1313 vertices[ 4] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001314
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001315 vertices[ 5] = 0.0;
1316 vertices[ 6] = 1.0;
1317 vertices[ 7] = 0.0;
1318 vertices[ 8] = 0.0;
1319 vertices[ 9] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001320
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001321 vertices[10] = 1.0;
1322 vertices[11] = 0.0;
1323 vertices[12] = 0.0;
1324 vertices[13] = 1.0;
1325 vertices[14] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001326
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001327 vertices[15] = 1.0;
1328 vertices[16] = 1.0;
1329 vertices[17] = 0.0;
1330 vertices[18] = 1.0;
1331 vertices[19] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001332
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001333 glGenBuffers(1, &ec->vbo);
1334 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
1335 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001336
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001337 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001338}
1339
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001340static const struct wl_interface visual_interface = {
1341 "visual", 1,
1342};
1343
1344static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001345add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001346{
1347 ec->argb_visual.base.interface = &visual_interface;
1348 ec->argb_visual.base.implementation = NULL;
1349 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001350 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001351
1352 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1353 ec->premultiplied_argb_visual.base.implementation = NULL;
1354 wl_display_add_object(ec->wl_display,
1355 &ec->premultiplied_argb_visual.base);
1356 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001357 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001358
1359 ec->rgb_visual.base.interface = &visual_interface;
1360 ec->rgb_visual.base.implementation = NULL;
1361 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001362 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1363}
1364
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001365void
1366wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1367 int x, int y, int width, int height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001368{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001369 output->compositor = c;
1370 output->x = x;
1371 output->y = y;
1372 output->width = width;
1373 output->height = height;
1374
1375 output->background =
1376 background_create(output, option_background);
1377
1378 wlsc_matrix_init(&output->matrix);
1379 wlsc_matrix_translate(&output->matrix,
1380 -output->x - output->width / 2.0,
1381 -output->y - output->height / 2.0, 0);
1382 wlsc_matrix_scale(&output->matrix,
1383 2.0 / output->width, 2.0 / output->height, 1);
1384
1385 output->base.interface = &wl_output_interface;
1386 wl_display_add_object(c->wl_display, &output->base);
1387 wl_display_add_global(c->wl_display, &output->base,
1388 wlsc_output_post_geometry);
1389}
1390
1391int
1392wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1393{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001394 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001395
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001396 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001397
Kristian Høgsbergf8ffded2010-09-03 15:15:33 -04001398 ec->base.base.interface = &wl_compositor_interface;
1399 ec->base.base.implementation =
1400 (void (**)(void)) &compositor_interface;
1401
1402 wl_display_add_object(display, &ec->base.base);
1403 if (wl_display_add_global(display, &ec->base.base, NULL))
1404 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001405
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001406 wlsc_shm_init(ec);
1407
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001408 ec->shell.base.interface = &wl_shell_interface;
1409 ec->shell.base.implementation = (void (**)(void)) &shell_interface;
1410 wl_display_add_object(display, &ec->shell.base);
1411 if (wl_display_add_global(display, &ec->shell.base, NULL))
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001412 return -1;
1413
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001414 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001415
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001416 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001417 wl_list_init(&ec->input_device_list);
1418 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001419 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001420
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001421 create_pointer_images(ec);
1422
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001423 screenshooter_create(ec);
1424
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001425 glGenFramebuffers(1, &ec->fbo);
1426 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1427 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001428 if (init_shaders(ec) < 0)
1429 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001430
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001431 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001432 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001433 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001434
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001435 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001436}
1437
1438/* The plan here is to generate a random anonymous socket name and
1439 * advertise that through a service on the session dbus.
1440 */
1441static const char socket_name[] = "\0wayland";
1442
1443int main(int argc, char *argv[])
1444{
1445 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001446 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001447 GError *error = NULL;
1448 GOptionContext *context;
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001449 int width, height;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001450
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001451 g_type_init(); /* GdkPixbuf needs this, it seems. */
1452
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001453 context = g_option_context_new(NULL);
1454 g_option_context_add_main_entries(context, option_entries, "Wayland");
1455 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1456 fprintf(stderr, "option parsing failed: %s\n", error->message);
1457 exit(EXIT_FAILURE);
1458 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001459 if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) {
1460 fprintf(stderr, "invalid geometry option: %s \n",
1461 option_geometry);
1462 exit(EXIT_FAILURE);
1463 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001464
1465 display = wl_display_create();
1466
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001467 if (getenv("DISPLAY"))
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001468 ec = x11_compositor_create(display, width, height);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001469 else
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001470 ec = drm_compositor_create(display, option_connector);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001471
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001472 if (ec == NULL) {
1473 fprintf(stderr, "failed to create compositor\n");
1474 exit(EXIT_FAILURE);
1475 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001476
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001477 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001478 fprintf(stderr, "failed to add socket: %m\n");
1479 exit(EXIT_FAILURE);
1480 }
1481
1482 wl_display_run(display);
1483
1484 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001485}