blob: ee5b9aa6b66687995907932d9e4b6109ec8ad54f [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05008 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050017 */
18
Kristian Høgsberg06bc2642010-12-01 09:50:16 -050019#define _GNU_SOURCE
20
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040021#include <stdio.h>
22#include <string.h>
23#include <stdlib.h>
24#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050025#include <stdarg.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040026#include <sys/ioctl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040027#include <fcntl.h>
28#include <unistd.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050029#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050030#include <math.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050031#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040032#include <linux/input.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050033
Kristian Høgsberga1f3f602010-08-03 09:26:44 -040034#include "wayland-server-protocol.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040035#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050036
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010037/* The plan here is to generate a random anonymous socket name and
38 * advertise that through a service on the session dbus.
39 */
40static const char *option_socket_name = "wayland";
41
Kristian Høgsberg61a82512010-10-26 11:26:44 -040042static const char *option_background = "background.jpg";
43static const char *option_geometry = "1024x640";
44static int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050045
46static const GOptionEntry option_entries[] = {
47 { "background", 'b', 0, G_OPTION_ARG_STRING,
48 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -040049 { "connector", 'c', 0, G_OPTION_ARG_INT,
50 &option_connector, "KMS connector" },
Kristian Høgsberg61a82512010-10-26 11:26:44 -040051 { "geometry", 'g', 0, G_OPTION_ARG_STRING,
52 &option_geometry, "Geometry" },
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010053 { "socket", 's', 0, G_OPTION_ARG_STRING,
54 &option_socket_name, "Socket Name" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050055 { NULL }
56};
57
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050058static void
Kristian Høgsberg1db21f12010-08-16 16:08:12 -040059wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
60 struct wlsc_surface *surface,
61 uint32_t time,
62 int32_t x, int32_t y,
63 int32_t sx, int32_t sy);
64
65static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050066wlsc_matrix_init(struct wlsc_matrix *matrix)
67{
68 static const struct wlsc_matrix identity = {
69 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
70 };
71
72 memcpy(matrix, &identity, sizeof identity);
73}
74
75static void
76wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
77{
78 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040079 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050080 div_t d;
81 int i, j;
82
83 for (i = 0; i < 16; i++) {
84 tmp.d[i] = 0;
85 d = div(i, 4);
86 row = m->d + d.quot * 4;
87 column = n->d + d.rem;
88 for (j = 0; j < 4; j++)
89 tmp.d[i] += row[j] * column[j * 4];
90 }
91 memcpy(m, &tmp, sizeof tmp);
92}
93
94static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040095wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050096{
Kristian Høgsberg27803c62010-06-06 22:23:21 -040097 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050098 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
99 };
100
101 wlsc_matrix_multiply(matrix, &translate);
102}
103
104static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400105wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500106{
107 struct wlsc_matrix scale = {
108 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
109 };
110
111 wlsc_matrix_multiply(matrix, &scale);
112}
113
114static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400115wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
116{
117 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400118 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400119
120 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400121 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400122 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400123 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400124 }
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400125
126 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400127}
128
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400129static struct wlsc_surface *
130wlsc_surface_create(struct wlsc_compositor *compositor,
131 struct wl_visual *visual,
132 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500133{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400134 struct wlsc_surface *surface;
135
136 surface = malloc(sizeof *surface);
137 if (surface == NULL)
138 return NULL;
139
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500140 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400141 glBindTexture(GL_TEXTURE_2D, surface->texture);
142 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
143 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
144 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
146
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500147 surface->compositor = compositor;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500148 surface->visual = visual;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400149 surface->x = x;
150 surface->y = y;
151 surface->width = width;
152 surface->height = height;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400153 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400154 wlsc_matrix_scale(&surface->matrix, width, height, 1);
155 wlsc_matrix_translate(&surface->matrix, x, y, 0);
156
157 wlsc_matrix_init(&surface->matrix_inv);
158 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
159 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500160
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400161 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500162}
163
164static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400165destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500166{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400167 struct wlsc_surface *surface =
168 container_of(resource, struct wlsc_surface, base.base);
169 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500170 struct wlsc_listener *l;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400171
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400172 wl_list_remove(&surface->link);
173 glDeleteTextures(1, &surface->texture);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400174
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500175 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400176 l->func(l, surface);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400177
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400178 free(surface);
Kristian Høgsberg117d5132010-08-11 08:56:47 -0400179
180 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500181}
182
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400183static int
184texture_from_png(const char *filename, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500185{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400186 GdkPixbuf *pixbuf;
187 GError *error = NULL;
188 void *data;
189 GLenum format;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500190
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400191 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
192 width, height,
193 FALSE, &error);
194 if (error != NULL)
195 return -1;
196
197 data = gdk_pixbuf_get_pixels(pixbuf);
198
199 if (gdk_pixbuf_get_has_alpha(pixbuf))
200 format = GL_RGBA;
201 else
202 format = GL_RGB;
203
Chia-I Wu1f411902010-10-29 15:20:16 +0800204 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
205 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400206
207 gdk_pixbuf_unref(pixbuf);
208
209 return 0;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500210}
211
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400212static const struct {
213 const char *filename;
214 int hotspot_x, hotspot_y;
215} pointer_images[] = {
Kristian Høgsberg4219a402010-08-16 16:43:03 -0400216 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
217 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
218 { DATADIR "/wayland/bottom_side.png", 16, 20 },
219 { DATADIR "/wayland/grabbing.png", 20, 17 },
220 { DATADIR "/wayland/left_ptr.png", 10, 5 },
221 { DATADIR "/wayland/left_side.png", 10, 20 },
222 { DATADIR "/wayland/right_side.png", 30, 19 },
223 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
224 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
225 { DATADIR "/wayland/top_side.png", 18, 8 },
226 { DATADIR "/wayland/xterm.png", 15, 15 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400227};
228
229static void
230create_pointer_images(struct wlsc_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500231{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400232 int i, count;
233 GLuint texture;
234 const int width = 32, height = 32;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400235
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400236 glGenTextures(1, &texture);
237 glBindTexture(GL_TEXTURE_2D, texture);
238 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
239 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
240 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
241 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400242
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400243 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400244 ec->pointer_buffers = malloc(count * sizeof *ec->pointer_buffers);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400245 for (i = 0; i < count; i++) {
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400246 ec->pointer_buffers[i] =
247 wlsc_drm_buffer_create(ec, width, height,
248 &ec->argb_visual);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400249 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400250 ec->pointer_buffers[i]->image);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400251 texture_from_png(pointer_images[i].filename, width, height);
252 }
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400253}
254
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500255static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500256background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500257{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500258 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500259 GdkPixbuf *pixbuf;
260 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500261 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500262 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500263
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400264 background = wlsc_surface_create(output->compositor,
265 &output->compositor->rgb_visual,
266 output->x, output->y,
267 output->width, output->height);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500268 if (background == NULL)
269 return NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500270
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500271 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500272 output->width,
273 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500274 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500275 if (error != NULL) {
276 free(background);
277 return NULL;
278 }
279
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500280 data = gdk_pixbuf_get_pixels(pixbuf);
281
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400282 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500283 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500284 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500285 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500286
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500287 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
288 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500289 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500290
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500291 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500292}
293
294static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400295wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500296{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500297 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400298 struct wlsc_matrix tmp;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500299
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400300 tmp = es->matrix;
301 wlsc_matrix_multiply(&tmp, &output->matrix);
302 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
303 glUniform1i(ec->tex_uniform, 0);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500304
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500305 if (es->visual == &ec->argb_visual) {
306 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
307 glEnable(GL_BLEND);
308 } else if (es->visual == &ec->premultiplied_argb_visual) {
309 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
310 glEnable(GL_BLEND);
311 } else {
312 glDisable(GL_BLEND);
313 }
314
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500315 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400316 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
317 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
318 5 * sizeof(GLfloat), NULL);
319 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
320 5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
321 glEnableVertexAttribArray(0);
322 glEnableVertexAttribArray(1);
323 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500324}
325
326static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400327wlsc_surface_raise(struct wlsc_surface *surface)
328{
329 struct wlsc_compositor *compositor = surface->compositor;
330
331 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400332 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400333}
334
335static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400336wlsc_surface_update_matrix(struct wlsc_surface *es)
337{
338 wlsc_matrix_init(&es->matrix);
339 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
340 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
341
342 wlsc_matrix_init(&es->matrix_inv);
343 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
344 wlsc_matrix_scale(&es->matrix_inv,
345 1.0 / es->width, 1.0 / es->height, 1);
346}
347
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400348void
349wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400350{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400351 wl_display_post_frame(compositor->wl_display, msecs);
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400352 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400353}
354
355static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400356wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400357{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500358 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500359 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500360 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500361
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500362 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500363
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500364 if (output->background)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400365 wlsc_surface_draw(output->background, output);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500366 else
367 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400368
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400369 wl_list_for_each_reverse(es, &ec->surface_list, link)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400370 wlsc_surface_draw(es, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400371
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400372 if (ec->focus)
373 wl_list_for_each(eid, &ec->input_device_list, link)
374 wlsc_surface_draw(eid->sprite, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500375}
376
377static void
378repaint(void *data)
379{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500380 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500381 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500382
383 if (!ec->repaint_needed) {
384 ec->repaint_on_timeout = 0;
385 return;
386 }
387
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500388 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400389 wlsc_output_repaint(output);
390
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400391 ec->present(ec);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500392
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500393 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400394}
395
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400396void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400397wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400398{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400399 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400400 if (compositor->repaint_on_timeout)
401 return;
402
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400403 wl_event_source_timer_update(compositor->timer_source, 1);
404 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400405}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500406
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500407static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500408surface_destroy(struct wl_client *client,
409 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400410{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400411 wl_resource_destroy(&surface->base, client);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400412}
413
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500414static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500415surface_attach(struct wl_client *client,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400416 struct wl_surface *surface, struct wl_buffer *buffer)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400417{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500418 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400419
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400420 buffer->attach(buffer, surface);
421 es->buffer = buffer;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400422}
423
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500424static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500425surface_map(struct wl_client *client,
426 struct wl_surface *surface,
427 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400428{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500429 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400430
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400431 es->x = x;
432 es->y = y;
433 es->width = width;
434 es->height = height;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400435
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400436 wlsc_surface_update_matrix(es);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400437 wlsc_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400438}
439
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500440static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500441surface_damage(struct wl_client *client,
442 struct wl_surface *surface,
443 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500444{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400445 struct wlsc_surface *es = (struct wlsc_surface *) surface;
446
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400447 es->buffer->damage(es->buffer, surface, x, y, width, height);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400448 wlsc_compositor_schedule_repaint(es->compositor);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500449}
450
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500451const static struct wl_surface_interface surface_interface = {
452 surface_destroy,
453 surface_attach,
454 surface_map,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500455 surface_damage
456};
457
458static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400459wlsc_input_device_attach(struct wlsc_input_device *device,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400460 struct wl_buffer *buffer, int x, int y)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400461{
462 struct wlsc_compositor *ec = device->ec;
463
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400464 buffer->attach(buffer, &device->sprite->base);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400465 device->hotspot_x = x;
466 device->hotspot_y = y;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400467
468 device->sprite->x = device->x - device->hotspot_x;
469 device->sprite->y = device->y - device->hotspot_y;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400470 device->sprite->width = buffer->width;
471 device->sprite->height = buffer->height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400472 wlsc_surface_update_matrix(device->sprite);
473
474 wlsc_compositor_schedule_repaint(ec);
475}
476
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400477
478static void
479wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
480 enum wlsc_pointer_type type)
481{
482 struct wlsc_compositor *compositor = device->ec;
483
484 wlsc_input_device_attach(device,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400485 &compositor->pointer_buffers[type]->base,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400486 pointer_images[type].hotspot_x,
487 pointer_images[type].hotspot_y);
488}
489
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400490static void
491wlsc_input_device_start_grab(struct wlsc_input_device *device,
492 uint32_t time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400493 enum wlsc_grab_type grab)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400494{
495 device->grab = grab;
496 device->grab_surface = device->pointer_focus;
497 device->grab_dx = device->pointer_focus->x - device->grab_x;
498 device->grab_dy = device->pointer_focus->y - device->grab_y;
499 device->grab_width = device->pointer_focus->width;
500 device->grab_height = device->pointer_focus->height;
501
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400502 wlsc_input_device_set_pointer_focus(device,
503 (struct wlsc_surface *) &wl_grab_surface,
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400504 time, 0, 0, 0, 0);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400505}
506
507static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400508shell_move(struct wl_client *client, struct wl_shell *shell,
509 struct wl_surface *surface,
510 struct wl_input_device *device, uint32_t time)
511{
512 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
513
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400514 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
515 wd->grab_time != time ||
516 &wd->pointer_focus->base != surface)
517 return;
518
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400519 wlsc_input_device_start_grab(wd, time, WLSC_DEVICE_GRAB_MOVE);
520 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400521}
522
523static void
524shell_resize(struct wl_client *client, struct wl_shell *shell,
525 struct wl_surface *surface,
526 struct wl_input_device *device, uint32_t time, uint32_t edges)
527{
528 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400529 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400530
531 if (edges == 0 || edges > 15 ||
532 (edges & 3) == 3 || (edges & 12) == 12)
533 return;
534
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400535 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
536 wd->grab_time != time ||
537 &wd->pointer_focus->base != surface)
538 return;
539
540 switch (edges) {
541 case WLSC_DEVICE_GRAB_RESIZE_TOP:
542 pointer = WLSC_POINTER_TOP;
543 break;
544 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
545 pointer = WLSC_POINTER_BOTTOM;
546 break;
547 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
548 pointer = WLSC_POINTER_LEFT;
549 break;
550 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
551 pointer = WLSC_POINTER_TOP_LEFT;
552 break;
553 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
554 pointer = WLSC_POINTER_BOTTOM_LEFT;
555 break;
556 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
557 pointer = WLSC_POINTER_RIGHT;
558 break;
559 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
560 pointer = WLSC_POINTER_TOP_RIGHT;
561 break;
562 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
563 pointer = WLSC_POINTER_BOTTOM_RIGHT;
564 break;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400565 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400566
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400567 wlsc_input_device_start_grab(wd, time, edges);
568 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400569}
570
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500571static uint32_t
572get_time(void)
573{
574 struct timeval tv;
575
576 gettimeofday(&tv, NULL);
577
578 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
579}
580
581struct wlsc_drag {
582 struct wl_drag drag;
583 struct wlsc_listener listener;
584};
585
586static void
587wl_drag_set_pointer_focus(struct wl_drag *drag,
588 struct wlsc_surface *surface, uint32_t time,
589 int32_t x, int32_t y, int32_t sx, int32_t sy);
590
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400591static void
592destroy_drag(struct wl_resource *resource, struct wl_client *client)
593{
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500594 struct wlsc_drag *drag =
595 container_of(resource, struct wlsc_drag, drag.resource);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400596
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500597 wl_list_remove(&drag->listener.link);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400598
599 free(drag);
600}
601
602const static struct wl_drag_interface drag_interface;
603
604static void
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500605drag_handle_surface_destroy(struct wlsc_listener *listener,
606 struct wlsc_surface *surface)
607{
608 struct wlsc_drag *drag =
609 container_of(listener, struct wlsc_drag, listener);
610 uint32_t time = get_time();
611
612 if (drag->drag.pointer_focus == &surface->base)
613 wl_drag_set_pointer_focus(&drag->drag, NULL, time, 0, 0, 0, 0);
614}
615
616static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400617shell_create_drag(struct wl_client *client,
618 struct wl_shell *shell, uint32_t id)
619{
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500620 struct wlsc_drag *drag;
621 struct wlsc_compositor *ec =
622 container_of(shell, struct wlsc_compositor, shell);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400623
624 drag = malloc(sizeof *drag);
625 if (drag == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400626 wl_client_post_no_memory(client);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400627 return;
628 }
629
630 memset(drag, 0, sizeof *drag);
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500631 drag->drag.resource.base.id = id;
632 drag->drag.resource.base.interface = &wl_drag_interface;
633 drag->drag.resource.base.implementation =
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400634 (void (**)(void)) &drag_interface;
635
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500636 drag->drag.resource.destroy = destroy_drag;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400637
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500638 drag->listener.func = drag_handle_surface_destroy;
639 wl_list_insert(ec->surface_destroy_listener_list.prev,
640 &drag->listener.link);
641
642 wl_client_add_resource(client, &drag->drag.resource);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400643}
644
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400645const static struct wl_shell_interface shell_interface = {
646 shell_move,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400647 shell_resize,
648 shell_create_drag
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400649};
650
651static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500652compositor_create_surface(struct wl_client *client,
653 struct wl_compositor *compositor, uint32_t id)
654{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500655 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400656 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500657
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400658 surface = wlsc_surface_create(ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400659 if (surface == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400660 wl_client_post_no_memory(client);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500661 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400662 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500663
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400664 wl_list_insert(ec->surface_list.prev, &surface->link);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400665 surface->base.base.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400666
667 surface->base.base.base.id = id;
668 surface->base.base.base.interface = &wl_surface_interface;
669 surface->base.base.base.implementation =
670 (void (**)(void)) &surface_interface;
671 surface->base.client = client;
672
673 wl_client_add_resource(client, &surface->base.base);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500674}
675
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500676const static struct wl_compositor_interface compositor_interface = {
677 compositor_create_surface,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500678};
679
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500680static void
681wlsc_surface_transform(struct wlsc_surface *surface,
682 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
683{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400684 struct wlsc_vector v = { { x, y, 0, 1 } };
685
686 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400687 *sx = v.f[0] * surface->width;
688 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500689}
690
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500691static void
692wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400693 struct wlsc_surface *surface,
694 uint32_t time)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500695{
696 if (device->keyboard_focus == surface)
697 return;
698
699 if (device->keyboard_focus &&
700 (!surface || device->keyboard_focus->base.client != surface->base.client))
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400701 wl_client_post_event(device->keyboard_focus->base.client,
702 &device->base.base,
703 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
704 time, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500705
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500706 if (surface)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400707 wl_client_post_event(surface->base.client,
708 &device->base.base,
709 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
710 time, &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500711
712 device->keyboard_focus = surface;
713}
714
715static void
716wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400717 struct wlsc_surface *surface,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400718 uint32_t time,
719 int32_t x, int32_t y,
720 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500721{
722 if (device->pointer_focus == surface)
723 return;
724
725 if (device->pointer_focus &&
726 (!surface || device->pointer_focus->base.client != surface->base.client))
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400727 wl_client_post_event(device->pointer_focus->base.client,
728 &device->base.base,
729 WL_INPUT_DEVICE_POINTER_FOCUS,
730 time, NULL, 0, 0, 0, 0);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500731 if (surface)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400732 wl_client_post_event(surface->base.client,
733 &device->base.base,
734 WL_INPUT_DEVICE_POINTER_FOCUS,
735 time, &surface->base,
736 x, y, sx, sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500737
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400738 if (!surface)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400739 wlsc_input_device_set_pointer_image(device,
740 WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400741
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500742 device->pointer_focus = surface;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400743 device->pointer_focus_time = time;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500744}
745
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500746static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500747pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500748{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500749 struct wlsc_compositor *ec = device->ec;
750 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500751
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400752 wl_list_for_each(es, &ec->surface_list, link) {
753 wlsc_surface_transform(es, device->x, device->y, sx, sy);
754 if (0 <= *sx && *sx < es->width &&
755 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500756 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400757 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500758
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500759 return NULL;
760}
761
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500762void
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400763notify_motion(struct wlsc_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500764{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500765 struct wlsc_surface *es;
766 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500767 struct wlsc_output *output;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400768 int32_t sx, sy, width, height;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500769
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500770 /* FIXME: We need some multi head love here. */
771 output = container_of(ec->output_list.next, struct wlsc_output, link);
772 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500773 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500774 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500775 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500776 if (x >= output->x + output->width)
777 x = output->x + output->width - 1;
778 if (y >= output->y + output->height)
779 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500780
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500781 device->x = x;
782 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500783
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400784 switch (device->grab) {
785 case WLSC_DEVICE_GRAB_NONE:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400786 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400787 wlsc_input_device_set_pointer_focus(device, es,
788 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400789 if (es)
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øgsberg83fc0612010-08-04 22:44:55 -0400794 break;
795
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400796 case WLSC_DEVICE_GRAB_MOTION:
797 es = device->pointer_focus;
798 wlsc_surface_transform(es, x, y, &sx, &sy);
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400799 wl_client_post_event(es->base.client,
800 &device->base.base,
801 WL_INPUT_DEVICE_MOTION,
802 time, x, y, sx, sy);
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400803 break;
804
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400805 case WLSC_DEVICE_GRAB_MOVE:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400806 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400807 es->x = x + device->grab_dx;
808 es->y = y + device->grab_dy;;
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400809 wl_client_post_event(es->base.client,
810 &ec->shell.base,
811 WL_SHELL_CONFIGURE,
812 time, device->grab,
813 &es->base, es->x, es->y,
814 es->width, es->height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400815
816 wlsc_surface_update_matrix(es);
817
818 break;
819
820 case WLSC_DEVICE_GRAB_RESIZE_TOP:
821 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
822 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
823 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
824 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
825 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
826 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
827 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
828 case WLSC_DEVICE_GRAB_RESIZE_MASK:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400829 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400830
831 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_LEFT) {
832 sx = x + device->grab_dx;
833 width = device->grab_x - x + device->grab_width;
834 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_RIGHT) {
835 sx = device->grab_x + device->grab_dx;
836 width = x - device->grab_x + device->grab_width;
837 } else {
838 sx = device->grab_x + device->grab_dx;
839 width = device->grab_width;
840 }
841
842 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_TOP) {
843 sy = y + device->grab_dy;
844 height = device->grab_y - y + device->grab_height;
845 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_BOTTOM) {
846 sy = device->grab_y + device->grab_dy;
847 height = y - device->grab_y + device->grab_height;
848 } else {
849 sy = device->grab_y + device->grab_dy;
850 height = device->grab_height;
851 }
852
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400853 wl_client_post_event(es->base.client,
854 &ec->shell.base,
855 WL_SHELL_CONFIGURE, time, device->grab,
856 &es->base, sx, sy, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400857 break;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400858
859 case WLSC_DEVICE_GRAB_DRAG:
860 es = pick_surface(device, &sx, &sy);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400861 wl_drag_set_pointer_focus(device->drag,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400862 es, time, x, y, sx, sy);
863 if (es)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400864 wl_client_post_event(es->base.client,
865 &device->drag->drag_offer.base,
866 WL_DRAG_OFFER_MOTION,
867 time, x, y, sx, sy);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400868 break;
869
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400870 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500871
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400872 device->sprite->x = device->x - device->hotspot_x;
873 device->sprite->y = device->y - device->hotspot_y;
874 wlsc_surface_update_matrix(device->sprite);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500875
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400876 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500877}
878
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400879static void
880wlsc_input_device_end_grab(struct wlsc_input_device *device, uint32_t time)
881{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400882 struct wl_drag *drag = device->drag;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400883 struct wlsc_surface *es;
884 int32_t sx, sy;
885
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400886 switch (device->grab) {
887 case WLSC_DEVICE_GRAB_DRAG:
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400888 if (drag->target)
889 wl_client_post_event(drag->target,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400890 &drag->drag_offer.base,
891 WL_DRAG_OFFER_DROP);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400892 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400893 device->drag = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400894 break;
895 default:
896 break;
897 }
898
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400899 device->grab = WLSC_DEVICE_GRAB_NONE;
900 es = pick_surface(device, &sx, &sy);
901 wlsc_input_device_set_pointer_focus(device, es, time,
902 device->x, device->y, sx, sy);
903}
904
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500905void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500906notify_button(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400907 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500908{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400909 struct wlsc_surface *surface;
910 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500911
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400912 surface = device->pointer_focus;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400913 if (surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400914 if (state && device->grab == WLSC_DEVICE_GRAB_NONE) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400915 wlsc_surface_raise(surface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400916 device->grab = WLSC_DEVICE_GRAB_MOTION;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400917 device->grab_button = button;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400918 device->grab_time = time;
919 device->grab_x = device->x;
920 device->grab_y = device->y;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400921 wlsc_input_device_set_keyboard_focus(device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400922 surface, time);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500923 }
924
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400925 if (state && button == BTN_LEFT &&
926 device->grab == WLSC_DEVICE_GRAB_MOTION &&
927 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400928 shell_move(NULL, (struct wl_shell *) &compositor->shell,
929 &surface->base, &device->base, time);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400930 else if (state && button == BTN_MIDDLE &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400931 device->grab == WLSC_DEVICE_GRAB_MOTION &&
932 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400933 shell_resize(NULL, (struct wl_shell *) &compositor->shell,
934
935 &surface->base, &device->base, time,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400936 WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400937 else if (device->grab == WLSC_DEVICE_GRAB_NONE ||
938 device->grab == WLSC_DEVICE_GRAB_MOTION)
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400939 wl_client_post_event(surface->base.client,
940 &device->base.base,
941 WL_INPUT_DEVICE_BUTTON,
942 time, button, state);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500943
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400944 if (!state &&
945 device->grab != WLSC_DEVICE_GRAB_NONE &&
946 device->grab_button == button) {
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400947 wlsc_input_device_end_grab(device, time);
948 }
949
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400950 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500951 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500952}
953
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500954void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500955notify_key(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400956 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500957{
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500958 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400959 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500960
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400961 switch (key | device->modifier_state) {
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400962 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400963 kill(0, SIGTERM);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500964 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500965 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500966
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400967 switch (key) {
968 case KEY_LEFTCTRL:
969 case KEY_RIGHTCTRL:
970 modifier = MODIFIER_CTRL;
971 break;
972
973 case KEY_LEFTALT:
974 case KEY_RIGHTALT:
975 modifier = MODIFIER_ALT;
976 break;
977
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400978 case KEY_LEFTMETA:
979 case KEY_RIGHTMETA:
980 modifier = MODIFIER_SUPER;
981 break;
982
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400983 default:
984 modifier = 0;
985 break;
986 }
987
988 if (state)
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400989 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400990 else
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400991 device->modifier_state &= ~modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400992
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500993 end = device->keys.data + device->keys.size;
994 for (k = device->keys.data; k < end; k++) {
995 if (*k == key)
996 *k = *--end;
997 }
998 device->keys.size = (void *) end - device->keys.data;
999 if (state) {
1000 k = wl_array_add(&device->keys, sizeof *k);
1001 *k = key;
1002 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001003
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001004 if (device->keyboard_focus != NULL)
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001005 wl_client_post_event(device->keyboard_focus->base.client,
1006 &device->base.base,
1007 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001008}
1009
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001010static void
1011input_device_attach(struct wl_client *client,
1012 struct wl_input_device *device_base,
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001013 uint32_t time,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001014 struct wl_buffer *buffer, int32_t x, int32_t y)
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001015{
1016 struct wlsc_input_device *device =
1017 (struct wlsc_input_device *) device_base;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001018
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001019 if (time < device->pointer_focus_time)
1020 return;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001021 if (device->pointer_focus == NULL)
1022 return;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001023
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001024 if (device->pointer_focus->base.client != client &&
1025 !(&device->pointer_focus->base == &wl_grab_surface &&
1026 device->grab_surface->base.client == client))
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001027 return;
1028
Kristian Høgsbergf4cb2012010-08-16 17:46:25 -04001029 if (buffer == NULL) {
1030 wlsc_input_device_set_pointer_image(device,
1031 WLSC_POINTER_LEFT_PTR);
1032 return;
1033 }
1034
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001035 wlsc_input_device_attach(device, buffer, x, y);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001036}
1037
1038const static struct wl_input_device_interface input_device_interface = {
1039 input_device_attach,
1040};
1041
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001042static void
1043handle_surface_destroy(struct wlsc_listener *listener,
1044 struct wlsc_surface *surface)
1045{
1046 struct wlsc_input_device *device =
1047 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001048 uint32_t time = get_time();
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001049
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001050 if (device->keyboard_focus == surface)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001051 wlsc_input_device_set_keyboard_focus(device, NULL, time);
Kristian Høgsberg3d465342010-11-22 13:58:46 -05001052 if (device->pointer_focus == surface)
1053 wlsc_input_device_set_pointer_focus(device, NULL, time,
1054 0, 0, 0, 0);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001055 if (device->pointer_focus == surface ||
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001056 (&device->pointer_focus->base == &wl_grab_surface &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001057 device->grab_surface == surface))
1058 wlsc_input_device_end_grab(device, time);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001059}
1060
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001061static void
1062wl_drag_set_pointer_focus(struct wl_drag *drag,
1063 struct wlsc_surface *surface, uint32_t time,
1064 int32_t x, int32_t y, int32_t sx, int32_t sy)
1065{
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001066 char **p, **end;
1067
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001068 if (drag->pointer_focus == &surface->base)
1069 return;
1070
1071 if (drag->pointer_focus &&
1072 (!surface || drag->pointer_focus->client != surface->base.client))
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001073 wl_client_post_event(drag->pointer_focus->client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001074 &drag->drag_offer.base,
1075 WL_DRAG_OFFER_POINTER_FOCUS,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001076 time, NULL, 0, 0, 0, 0);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001077
1078 if (surface &&
1079 (!drag->pointer_focus ||
1080 drag->pointer_focus->client != surface->base.client)) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001081 wl_client_post_global(surface->base.client,
1082 &drag->drag_offer.base);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001083
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001084 end = drag->types.data + drag->types.size;
1085 for (p = drag->types.data; p < end; p++)
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001086 wl_client_post_event(surface->base.client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001087 &drag->drag_offer.base,
1088 WL_DRAG_OFFER_OFFER, *p);
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001089 }
1090
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001091 if (surface) {
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001092 wl_client_post_event(surface->base.client,
1093 &drag->drag_offer.base,
1094 WL_DRAG_OFFER_POINTER_FOCUS,
1095 time, &surface->base,
1096 x, y, sx, sy);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001097
1098 }
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001099
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001100 drag->pointer_focus = &surface->base;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001101 drag->pointer_focus_time = time;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001102 drag->target = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001103}
1104
1105static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001106drag_offer_accept(struct wl_client *client,
1107 struct wl_drag_offer *offer, uint32_t time, const char *type)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001108{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001109 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001110 char **p, **end;
1111
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001112 /* If the client responds to drag pointer_focus or motion
1113 * events after the pointer has left the surface, we just
1114 * discard the accept requests. The drag source just won't
1115 * get the corresponding 'target' events and eventually the
1116 * next surface/root will start sending events. */
1117 if (time < drag->pointer_focus_time)
1118 return;
1119
1120 drag->target = client;
1121 drag->type = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001122 end = drag->types.data + drag->types.size;
1123 for (p = drag->types.data; p < end; p++)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001124 if (type && strcmp(*p, type) == 0)
1125 drag->type = *p;
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_TARGET, drag->type);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001129}
1130
1131static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001132drag_offer_receive(struct wl_client *client,
1133 struct wl_drag_offer *offer, int fd)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001134{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001135 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001136
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001137 wl_client_post_event(drag->source->client, &drag->resource.base,
1138 WL_DRAG_FINISH, fd);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001139 close(fd);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001140}
1141
Kristian Høgsbergd44bc8b2010-11-30 15:10:26 -05001142static void
1143drag_offer_reject(struct wl_client *client, struct wl_drag_offer *offer)
1144{
1145 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
1146
1147 wl_client_post_event(drag->source->client, &drag->resource.base,
1148 WL_DRAG_REJECT);
1149}
1150
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001151static const struct wl_drag_offer_interface drag_offer_interface = {
1152 drag_offer_accept,
Kristian Høgsbergd44bc8b2010-11-30 15:10:26 -05001153 drag_offer_receive,
1154 drag_offer_reject
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001155};
1156
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001157static void
1158drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
1159{
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001160 char **p;
1161
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001162 p = wl_array_add(&drag->types, sizeof *p);
1163 if (p)
1164 *p = strdup(type);
1165 if (!p || !*p)
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001166 wl_client_post_no_memory(client);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001167}
1168
1169static void
1170drag_activate(struct wl_client *client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001171 struct wl_drag *drag,
1172 struct wl_surface *surface,
1173 struct wl_input_device *input_device, uint32_t time)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001174{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001175 struct wl_display *display = wl_client_get_display (client);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001176 struct wlsc_input_device *device =
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001177 (struct wlsc_input_device *) input_device;
1178 struct wlsc_surface *target;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001179 int32_t sx, sy;
1180
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001181 if (device->grab != WLSC_DEVICE_GRAB_MOTION ||
1182 &device->pointer_focus->base != surface ||
1183 device->grab_time != time)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001184 return;
1185
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001186 drag->source = surface;
1187 drag->input_device = input_device;
1188
1189 drag->drag_offer.base.interface = &wl_drag_offer_interface;
1190 drag->drag_offer.base.implementation =
1191 (void (**)(void)) &drag_offer_interface;
1192
1193 wl_display_add_object(display, &drag->drag_offer.base);
1194
1195 wlsc_input_device_start_grab(device, time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001196 WLSC_DEVICE_GRAB_DRAG);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001197
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001198 device->drag = drag;
1199 target = pick_surface(device, &sx, &sy);
1200 wl_drag_set_pointer_focus(device->drag, target, time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001201 device->x, device->y, sx, sy);
1202}
1203
1204static void
1205drag_cancel(struct wl_client *client, struct wl_drag *drag)
1206{
1207 struct wlsc_input_device *device =
1208 (struct wlsc_input_device *) drag->input_device;
1209
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001210 if (drag->source == NULL || drag->source->client != client)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001211 return;
1212
1213 wlsc_input_device_end_grab(device, get_time());
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001214 device->drag = NULL;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001215}
1216
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001217static const struct wl_drag_interface drag_interface = {
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001218 drag_offer,
1219 drag_activate,
1220 drag_cancel,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001221};
1222
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001223void
1224wlsc_input_device_init(struct wlsc_input_device *device,
1225 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001226{
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001227 device->base.base.interface = &wl_input_device_interface;
1228 device->base.base.implementation =
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001229 (void (**)(void)) &input_device_interface;
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001230 wl_display_add_object(ec->wl_display, &device->base.base);
1231 wl_display_add_global(ec->wl_display, &device->base.base, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001232
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001233 device->x = 100;
1234 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001235 device->ec = ec;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001236 device->sprite = wlsc_surface_create(ec, &ec->argb_visual,
1237 device->x, device->y, 32, 32);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001238 device->hotspot_x = 16;
1239 device->hotspot_y = 16;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001240
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001241 device->listener.func = handle_surface_destroy;
1242 wl_list_insert(ec->surface_destroy_listener_list.prev,
1243 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001244 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001245
1246 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001247}
1248
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001249static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001250wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001251{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001252 struct wlsc_output *output =
1253 container_of(global, struct wlsc_output, base);
1254
1255 wl_client_post_event(client, global,
1256 WL_OUTPUT_GEOMETRY,
1257 output->width, output->height);
1258}
1259
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001260static const char vertex_shader[] =
1261 "uniform mat4 proj;\n"
1262 "attribute vec4 position;\n"
1263 "attribute vec2 texcoord;\n"
1264 "varying vec2 v_texcoord;\n"
1265 "void main()\n"
1266 "{\n"
1267 " gl_Position = proj * position;\n"
1268 " v_texcoord = texcoord;\n"
1269 "}\n";
1270
1271static const char fragment_shader[] =
1272 /* "precision mediump float;\n" */
1273 "varying vec2 v_texcoord;\n"
1274 "uniform sampler2D tex;\n"
1275 "void main()\n"
1276 "{\n"
1277 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1278 "}\n";
1279
Kristian Høgsberga9468212010-06-14 21:03:11 -04001280static int
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001281init_shaders(struct wlsc_compositor *ec)
1282{
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001283 GLuint v, f, program;
1284 const char *p;
1285 char msg[512];
1286 GLfloat vertices[4 * 5];
1287 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001288
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001289 p = vertex_shader;
1290 v = glCreateShader(GL_VERTEX_SHADER);
1291 glShaderSource(v, 1, &p, NULL);
1292 glCompileShader(v);
1293 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
1294 if (!status) {
1295 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
1296 fprintf(stderr, "vertex shader info: %s\n", msg);
1297 return -1;
1298 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001299
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001300 p = fragment_shader;
1301 f = glCreateShader(GL_FRAGMENT_SHADER);
1302 glShaderSource(f, 1, &p, NULL);
1303 glCompileShader(f);
1304 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
1305 if (!status) {
1306 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
1307 fprintf(stderr, "fragment shader info: %s\n", msg);
1308 return -1;
1309 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001310
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001311 program = glCreateProgram();
1312 glAttachShader(program, v);
1313 glAttachShader(program, f);
1314 glBindAttribLocation(program, 0, "position");
1315 glBindAttribLocation(program, 1, "texcoord");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001316
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001317 glLinkProgram(program);
1318 glGetProgramiv(program, GL_LINK_STATUS, &status);
1319 if (!status) {
1320 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1321 fprintf(stderr, "link info: %s\n", msg);
1322 return -1;
1323 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001324
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001325 glUseProgram(program);
1326 ec->proj_uniform = glGetUniformLocation(program, "proj");
1327 ec->tex_uniform = glGetUniformLocation(program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001328
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001329 vertices[ 0] = 0.0;
1330 vertices[ 1] = 0.0;
1331 vertices[ 2] = 0.0;
1332 vertices[ 3] = 0.0;
1333 vertices[ 4] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001334
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001335 vertices[ 5] = 0.0;
1336 vertices[ 6] = 1.0;
1337 vertices[ 7] = 0.0;
1338 vertices[ 8] = 0.0;
1339 vertices[ 9] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001340
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001341 vertices[10] = 1.0;
1342 vertices[11] = 0.0;
1343 vertices[12] = 0.0;
1344 vertices[13] = 1.0;
1345 vertices[14] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001346
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001347 vertices[15] = 1.0;
1348 vertices[16] = 1.0;
1349 vertices[17] = 0.0;
1350 vertices[18] = 1.0;
1351 vertices[19] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001352
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001353 glGenBuffers(1, &ec->vbo);
1354 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
1355 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001356
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001357 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001358}
1359
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001360static const struct wl_interface visual_interface = {
1361 "visual", 1,
1362};
1363
1364static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001365add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001366{
1367 ec->argb_visual.base.interface = &visual_interface;
1368 ec->argb_visual.base.implementation = NULL;
1369 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001370 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001371
1372 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1373 ec->premultiplied_argb_visual.base.implementation = NULL;
1374 wl_display_add_object(ec->wl_display,
1375 &ec->premultiplied_argb_visual.base);
1376 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001377 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001378
1379 ec->rgb_visual.base.interface = &visual_interface;
1380 ec->rgb_visual.base.implementation = NULL;
1381 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001382 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1383}
1384
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001385void
1386wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1387 int x, int y, int width, int height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001388{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001389 output->compositor = c;
1390 output->x = x;
1391 output->y = y;
1392 output->width = width;
1393 output->height = height;
1394
1395 output->background =
1396 background_create(output, option_background);
1397
1398 wlsc_matrix_init(&output->matrix);
1399 wlsc_matrix_translate(&output->matrix,
1400 -output->x - output->width / 2.0,
1401 -output->y - output->height / 2.0, 0);
1402 wlsc_matrix_scale(&output->matrix,
1403 2.0 / output->width, 2.0 / output->height, 1);
1404
1405 output->base.interface = &wl_output_interface;
1406 wl_display_add_object(c->wl_display, &output->base);
1407 wl_display_add_global(c->wl_display, &output->base,
1408 wlsc_output_post_geometry);
1409}
1410
1411int
1412wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1413{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001414 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001415
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001416 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001417
Kristian Høgsbergf8ffded2010-09-03 15:15:33 -04001418 ec->base.base.interface = &wl_compositor_interface;
1419 ec->base.base.implementation =
1420 (void (**)(void)) &compositor_interface;
1421
1422 wl_display_add_object(display, &ec->base.base);
1423 if (wl_display_add_global(display, &ec->base.base, NULL))
1424 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001425
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001426 wlsc_shm_init(ec);
1427
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001428 ec->shell.base.interface = &wl_shell_interface;
1429 ec->shell.base.implementation = (void (**)(void)) &shell_interface;
1430 wl_display_add_object(display, &ec->shell.base);
1431 if (wl_display_add_global(display, &ec->shell.base, NULL))
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001432 return -1;
1433
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001434 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001435
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001436 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001437 wl_list_init(&ec->input_device_list);
1438 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001439 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001440
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001441 create_pointer_images(ec);
1442
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001443 screenshooter_create(ec);
1444
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001445 glGenFramebuffers(1, &ec->fbo);
1446 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1447 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001448 if (init_shaders(ec) < 0)
1449 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001450
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001451 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001452 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001453 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001454
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001455 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001456}
1457
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001458
1459int main(int argc, char *argv[])
1460{
1461 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001462 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001463 GError *error = NULL;
1464 GOptionContext *context;
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001465 int width, height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001466 char *socket_name;
1467 int socket_name_size;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001468
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001469 g_type_init(); /* GdkPixbuf needs this, it seems. */
1470
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001471 context = g_option_context_new(NULL);
1472 g_option_context_add_main_entries(context, option_entries, "Wayland");
1473 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1474 fprintf(stderr, "option parsing failed: %s\n", error->message);
1475 exit(EXIT_FAILURE);
1476 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001477 if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) {
1478 fprintf(stderr, "invalid geometry option: %s \n",
1479 option_geometry);
1480 exit(EXIT_FAILURE);
1481 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001482
1483 display = wl_display_create();
1484
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001485 if (getenv("WAYLAND_DISPLAY"))
1486 ec = wayland_compositor_create(display, width, height);
1487 else if (getenv("DISPLAY"))
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001488 ec = x11_compositor_create(display, width, height);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001489 else
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001490 ec = drm_compositor_create(display, option_connector);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001491
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001492 if (ec == NULL) {
1493 fprintf(stderr, "failed to create compositor\n");
1494 exit(EXIT_FAILURE);
1495 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001496
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001497 socket_name_size = 1 + asprintf(&socket_name, "%c%s", '\0',
1498 option_socket_name);
1499
1500 if (wl_display_add_socket(display, socket_name, socket_name_size)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001501 fprintf(stderr, "failed to add socket: %m\n");
1502 exit(EXIT_FAILURE);
1503 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001504 free(socket_name);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001505
1506 wl_display_run(display);
1507
1508 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001509}