blob: 175e5a720437d03dbb6b39e85224fc0596ffc6ab [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øgsbergfc783d42010-06-11 12:56:24 -040035const char *option_background = "background.jpg";
36int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050037
38static const GOptionEntry option_entries[] = {
39 { "background", 'b', 0, G_OPTION_ARG_STRING,
40 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -040041 { "connector", 'c', 0, G_OPTION_ARG_INT,
42 &option_connector, "KMS connector" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050043 { NULL }
44};
45
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050046static void
Kristian Høgsberg1db21f12010-08-16 16:08:12 -040047wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
48 struct wlsc_surface *surface,
49 uint32_t time,
50 int32_t x, int32_t y,
51 int32_t sx, int32_t sy);
52
53static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050054wlsc_matrix_init(struct wlsc_matrix *matrix)
55{
56 static const struct wlsc_matrix identity = {
57 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
58 };
59
60 memcpy(matrix, &identity, sizeof identity);
61}
62
63static void
64wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
65{
66 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040067 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050068 div_t d;
69 int i, j;
70
71 for (i = 0; i < 16; i++) {
72 tmp.d[i] = 0;
73 d = div(i, 4);
74 row = m->d + d.quot * 4;
75 column = n->d + d.rem;
76 for (j = 0; j < 4; j++)
77 tmp.d[i] += row[j] * column[j * 4];
78 }
79 memcpy(m, &tmp, sizeof tmp);
80}
81
82static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040083wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050084{
Kristian Høgsberg27803c62010-06-06 22:23:21 -040085 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050086 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
87 };
88
89 wlsc_matrix_multiply(matrix, &translate);
90}
91
92static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040093wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050094{
95 struct wlsc_matrix scale = {
96 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
97 };
98
99 wlsc_matrix_multiply(matrix, &scale);
100}
101
102static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400103wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
104{
105 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400106 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400107
108 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400109 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400110 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400111 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400112 }
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400113
114 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400115}
116
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400117static struct wlsc_surface *
118wlsc_surface_create(struct wlsc_compositor *compositor,
119 struct wl_visual *visual,
120 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500121{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400122 struct wlsc_surface *surface;
123
124 surface = malloc(sizeof *surface);
125 if (surface == NULL)
126 return NULL;
127
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500128 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400129 glBindTexture(GL_TEXTURE_2D, surface->texture);
130 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
131 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
132 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
133 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
134
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500135 surface->compositor = compositor;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500136 surface->visual = visual;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400137 surface->x = x;
138 surface->y = y;
139 surface->width = width;
140 surface->height = height;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400141 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400142 wlsc_matrix_scale(&surface->matrix, width, height, 1);
143 wlsc_matrix_translate(&surface->matrix, x, y, 0);
144
145 wlsc_matrix_init(&surface->matrix_inv);
146 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
147 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500148
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400149 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500150}
151
152static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400153destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500154{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400155 struct wlsc_surface *surface =
156 container_of(resource, struct wlsc_surface, base.base);
157 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500158 struct wlsc_listener *l;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400159
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400160 wl_list_remove(&surface->link);
161 glDeleteTextures(1, &surface->texture);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400162
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500163 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400164 l->func(l, surface);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400165
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400166 free(surface);
Kristian Høgsberg117d5132010-08-11 08:56:47 -0400167
168 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500169}
170
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400171static int
172texture_from_png(const char *filename, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500173{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400174 GdkPixbuf *pixbuf;
175 GError *error = NULL;
176 void *data;
177 GLenum format;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500178
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400179 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
180 width, height,
181 FALSE, &error);
182 if (error != NULL)
183 return -1;
184
185 data = gdk_pixbuf_get_pixels(pixbuf);
186
187 if (gdk_pixbuf_get_has_alpha(pixbuf))
188 format = GL_RGBA;
189 else
190 format = GL_RGB;
191
192 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
193 width, height, 0, format, GL_UNSIGNED_BYTE, data);
194
195 gdk_pixbuf_unref(pixbuf);
196
197 return 0;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500198}
199
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400200static const struct {
201 const char *filename;
202 int hotspot_x, hotspot_y;
203} pointer_images[] = {
Kristian Høgsberg4219a402010-08-16 16:43:03 -0400204 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
205 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
206 { DATADIR "/wayland/bottom_side.png", 16, 20 },
207 { DATADIR "/wayland/grabbing.png", 20, 17 },
208 { DATADIR "/wayland/left_ptr.png", 10, 5 },
209 { DATADIR "/wayland/left_side.png", 10, 20 },
210 { DATADIR "/wayland/right_side.png", 30, 19 },
211 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
212 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
213 { DATADIR "/wayland/top_side.png", 18, 8 },
214 { DATADIR "/wayland/xterm.png", 15, 15 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400215};
216
217static void
218create_pointer_images(struct wlsc_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500219{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400220 int i, count;
221 GLuint texture;
222 const int width = 32, height = 32;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400223
224 EGLint image_attribs[] = {
225 EGL_WIDTH, 0,
226 EGL_HEIGHT, 0,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400227 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
228 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400229 EGL_NONE
230 };
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500231
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400232 glGenTextures(1, &texture);
233 glBindTexture(GL_TEXTURE_2D, texture);
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
235 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
236 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
237 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400238
239 image_attribs[1] = width;
240 image_attribs[3] = height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400241 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400242 ec->pointer_buffers = malloc(count * sizeof *ec->pointer_buffers);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400243 for (i = 0; i < count; i++) {
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400244 ec->pointer_buffers[i].image =
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400245 eglCreateDRMImageMESA(ec->display, image_attribs);
246 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400247 ec->pointer_buffers[i].image);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400248 texture_from_png(pointer_images[i].filename, width, height);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400249 ec->pointer_buffers[i].visual = &ec->argb_visual;
250 ec->pointer_buffers[i].width = width;
251 ec->pointer_buffers[i].height = height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400252 }
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øgsberg4c9f2c92008-11-21 19:25:44 -0500316 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400317 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
318 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
319 5 * sizeof(GLfloat), NULL);
320 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
321 5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
322 glEnableVertexAttribArray(0);
323 glEnableVertexAttribArray(1);
324 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500325}
326
327static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400328wlsc_surface_raise(struct wlsc_surface *surface)
329{
330 struct wlsc_compositor *compositor = surface->compositor;
331
332 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400333 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400334}
335
336static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400337wlsc_surface_update_matrix(struct wlsc_surface *es)
338{
339 wlsc_matrix_init(&es->matrix);
340 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
341 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
342
343 wlsc_matrix_init(&es->matrix_inv);
344 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
345 wlsc_matrix_scale(&es->matrix_inv,
346 1.0 / es->width, 1.0 / es->height, 1);
347}
348
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400349void
350wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400351{
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400352 wl_display_post_frame(compositor->wl_display,
353 &compositor->base,
354 compositor->current_frame, msecs);
355
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400356 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400357 compositor->current_frame++;
358}
359
360static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400361wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400362{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500363 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500364 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500365 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500366
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500367 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500368
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500369 if (output->background)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400370 wlsc_surface_draw(output->background, output);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500371 else
372 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400373
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400374 wl_list_for_each_reverse(es, &ec->surface_list, link)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400375 wlsc_surface_draw(es, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400376
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400377 if (ec->focus)
378 wl_list_for_each(eid, &ec->input_device_list, link)
379 wlsc_surface_draw(eid->sprite, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500380}
381
382static void
383repaint(void *data)
384{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500385 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500386 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500387
388 if (!ec->repaint_needed) {
389 ec->repaint_on_timeout = 0;
390 return;
391 }
392
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500393 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400394 wlsc_output_repaint(output);
395
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400396 ec->present(ec);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500397
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500398 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400399}
400
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400401void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400402wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400403{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400404 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400405 if (compositor->repaint_on_timeout)
406 return;
407
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400408 wl_event_source_timer_update(compositor->timer_source, 1);
409 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400410}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500411
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500412static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500413surface_destroy(struct wl_client *client,
414 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400415{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400416 wl_resource_destroy(&surface->base, client);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400417}
418
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500419static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500420surface_attach(struct wl_client *client,
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400421 struct wl_surface *surface, struct wl_buffer *buffer_base)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400422{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500423 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400424 struct wlsc_buffer *buffer = (struct wlsc_buffer *) buffer_base;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400425
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500426 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400427 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, buffer->image);
428 es->visual = buffer->visual;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400429}
430
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500431static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500432surface_map(struct wl_client *client,
433 struct wl_surface *surface,
434 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400435{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500436 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400437
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400438 es->x = x;
439 es->y = y;
440 es->width = width;
441 es->height = height;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400442
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400443 wlsc_surface_update_matrix(es);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400444}
445
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500446static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500447surface_damage(struct wl_client *client,
448 struct wl_surface *surface,
449 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500450{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500451 /* FIXME: This need to take a damage region, of course. */
452}
453
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500454const static struct wl_surface_interface surface_interface = {
455 surface_destroy,
456 surface_attach,
457 surface_map,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500458 surface_damage
459};
460
461static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400462wlsc_input_device_attach(struct wlsc_input_device *device,
463 struct wlsc_buffer *buffer, int x, int y)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400464{
465 struct wlsc_compositor *ec = device->ec;
466
467 glBindTexture(GL_TEXTURE_2D, device->sprite->texture);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400468 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, buffer->image);
469 device->sprite->visual = buffer->visual;
470 device->hotspot_x = x;
471 device->hotspot_y = y;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400472
473 device->sprite->x = device->x - device->hotspot_x;
474 device->sprite->y = device->y - device->hotspot_y;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400475 device->sprite->width = buffer->width;
476 device->sprite->height = buffer->height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400477 wlsc_surface_update_matrix(device->sprite);
478
479 wlsc_compositor_schedule_repaint(ec);
480}
481
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400482
483static void
484wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
485 enum wlsc_pointer_type type)
486{
487 struct wlsc_compositor *compositor = device->ec;
488
489 wlsc_input_device_attach(device,
490 &compositor->pointer_buffers[type],
491 pointer_images[type].hotspot_x,
492 pointer_images[type].hotspot_y);
493}
494
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400495static void
496wlsc_input_device_start_grab(struct wlsc_input_device *device,
497 uint32_t time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400498 enum wlsc_grab_type grab)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400499{
500 device->grab = grab;
501 device->grab_surface = device->pointer_focus;
502 device->grab_dx = device->pointer_focus->x - device->grab_x;
503 device->grab_dy = device->pointer_focus->y - device->grab_y;
504 device->grab_width = device->pointer_focus->width;
505 device->grab_height = device->pointer_focus->height;
506
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400507 wlsc_input_device_set_pointer_focus(device,
508 (struct wlsc_surface *) &wl_grab_surface,
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400509 time, 0, 0, 0, 0);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400510}
511
512static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400513shell_move(struct wl_client *client, struct wl_shell *shell,
514 struct wl_surface *surface,
515 struct wl_input_device *device, uint32_t time)
516{
517 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
518
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400519 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
520 wd->grab_time != time ||
521 &wd->pointer_focus->base != surface)
522 return;
523
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400524 wlsc_input_device_start_grab(wd, time, WLSC_DEVICE_GRAB_MOVE);
525 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400526}
527
528static void
529shell_resize(struct wl_client *client, struct wl_shell *shell,
530 struct wl_surface *surface,
531 struct wl_input_device *device, uint32_t time, uint32_t edges)
532{
533 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400534 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400535
536 if (edges == 0 || edges > 15 ||
537 (edges & 3) == 3 || (edges & 12) == 12)
538 return;
539
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400540 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
541 wd->grab_time != time ||
542 &wd->pointer_focus->base != surface)
543 return;
544
545 switch (edges) {
546 case WLSC_DEVICE_GRAB_RESIZE_TOP:
547 pointer = WLSC_POINTER_TOP;
548 break;
549 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
550 pointer = WLSC_POINTER_BOTTOM;
551 break;
552 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
553 pointer = WLSC_POINTER_LEFT;
554 break;
555 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
556 pointer = WLSC_POINTER_TOP_LEFT;
557 break;
558 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
559 pointer = WLSC_POINTER_BOTTOM_LEFT;
560 break;
561 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
562 pointer = WLSC_POINTER_RIGHT;
563 break;
564 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
565 pointer = WLSC_POINTER_TOP_RIGHT;
566 break;
567 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
568 pointer = WLSC_POINTER_BOTTOM_RIGHT;
569 break;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400570 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400571
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400572 wlsc_input_device_start_grab(wd, time, edges);
573 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400574}
575
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400576static void
577destroy_drag(struct wl_resource *resource, struct wl_client *client)
578{
579 struct wl_drag *drag =
580 container_of(resource, struct wl_drag, resource);
581
582 /* FIXME: More stuff */
583
584 free(drag);
585}
586
587const static struct wl_drag_interface drag_interface;
588
589static void
590shell_create_drag(struct wl_client *client,
591 struct wl_shell *shell, uint32_t id)
592{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400593 struct wl_drag *drag;
594
595 drag = malloc(sizeof *drag);
596 if (drag == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400597 wl_client_post_no_memory(client);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400598 return;
599 }
600
601 memset(drag, 0, sizeof *drag);
602 drag->resource.base.id = id;
603 drag->resource.base.interface = &wl_drag_interface;
604 drag->resource.base.implementation =
605 (void (**)(void)) &drag_interface;
606
607 drag->resource.destroy = destroy_drag;
608
609 wl_client_add_resource(client, &drag->resource);
610}
611
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400612const static struct wl_shell_interface shell_interface = {
613 shell_move,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400614 shell_resize,
615 shell_create_drag
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400616};
617
618static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500619compositor_create_surface(struct wl_client *client,
620 struct wl_compositor *compositor, uint32_t id)
621{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500622 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400623 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500624
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400625 surface = wlsc_surface_create(ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400626 if (surface == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400627 wl_client_post_no_memory(client);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500628 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400629 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500630
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400631 wl_list_insert(ec->surface_list.prev, &surface->link);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400632 surface->base.base.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400633
634 surface->base.base.base.id = id;
635 surface->base.base.base.interface = &wl_surface_interface;
636 surface->base.base.base.implementation =
637 (void (**)(void)) &surface_interface;
638 surface->base.client = client;
639
640 wl_client_add_resource(client, &surface->base.base);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500641}
642
643static void
644compositor_commit(struct wl_client *client,
645 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500646{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500647 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500648
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400649 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500650 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500651}
652
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500653const static struct wl_compositor_interface compositor_interface = {
654 compositor_create_surface,
655 compositor_commit
656};
657
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500658static void
659wlsc_surface_transform(struct wlsc_surface *surface,
660 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
661{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400662 struct wlsc_vector v = { { x, y, 0, 1 } };
663
664 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400665 *sx = v.f[0] * surface->width;
666 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500667}
668
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500669static void
670wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400671 struct wlsc_surface *surface,
672 uint32_t time)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500673{
674 if (device->keyboard_focus == surface)
675 return;
676
677 if (device->keyboard_focus &&
678 (!surface || device->keyboard_focus->base.client != surface->base.client))
679 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400680 &device->base.base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400681 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400682 time, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500683
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500684 if (surface)
685 wl_surface_post_event(&surface->base,
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400686 &device->base.base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400687 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400688 time, &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500689
690 device->keyboard_focus = surface;
691}
692
693static void
694wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400695 struct wlsc_surface *surface,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400696 uint32_t time,
697 int32_t x, int32_t y,
698 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500699{
700 if (device->pointer_focus == surface)
701 return;
702
703 if (device->pointer_focus &&
704 (!surface || device->pointer_focus->base.client != surface->base.client))
705 wl_surface_post_event(&device->pointer_focus->base,
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400706 &device->base.base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400707 WL_INPUT_DEVICE_POINTER_FOCUS,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400708 time, NULL, 0, 0, 0, 0);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500709 if (surface)
710 wl_surface_post_event(&surface->base,
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400711 &device->base.base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400712 WL_INPUT_DEVICE_POINTER_FOCUS,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400713 time, &surface->base,
714 x, y, sx, sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500715
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400716 if (!surface)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400717 wlsc_input_device_set_pointer_image(device,
718 WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400719
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500720 device->pointer_focus = surface;
721}
722
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500723static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500724pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500725{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500726 struct wlsc_compositor *ec = device->ec;
727 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500728
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400729 wl_list_for_each(es, &ec->surface_list, link) {
730 wlsc_surface_transform(es, device->x, device->y, sx, sy);
731 if (0 <= *sx && *sx < es->width &&
732 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500733 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400734 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500735
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500736 return NULL;
737}
738
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400739static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400740wl_drag_set_pointer_focus(struct wl_drag *drag,
741 struct wlsc_surface *surface, uint32_t time,
742 int32_t x, int32_t y, int32_t sx, int32_t sy);
743
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500744void
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400745notify_motion(struct wlsc_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500746{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500747 struct wlsc_surface *es;
748 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500749 struct wlsc_output *output;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400750 int32_t sx, sy, width, height;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500751
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500752 /* FIXME: We need some multi head love here. */
753 output = container_of(ec->output_list.next, struct wlsc_output, link);
754 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500755 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500756 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500757 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500758 if (x >= output->x + output->width)
759 x = output->x + output->width - 1;
760 if (y >= output->y + output->height)
761 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500762
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500763 device->x = x;
764 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500765
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400766 switch (device->grab) {
767 case WLSC_DEVICE_GRAB_NONE:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400768 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400769 wlsc_input_device_set_pointer_focus(device, es,
770 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400771 if (es)
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400772 wl_surface_post_event(&es->base, &device->base.base,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400773 WL_INPUT_DEVICE_MOTION,
774 time, x, y, sx, sy);
775 break;
776
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400777 case WLSC_DEVICE_GRAB_MOTION:
778 es = device->pointer_focus;
779 wlsc_surface_transform(es, x, y, &sx, &sy);
780 wl_surface_post_event(&es->base, &device->base.base,
781 WL_INPUT_DEVICE_MOTION,
782 time, x, y, sx, sy);
783 break;
784
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400785 case WLSC_DEVICE_GRAB_MOVE:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400786 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400787 es->x = x + device->grab_dx;
788 es->y = y + device->grab_dy;;
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400789 wl_surface_post_event(&es->base, &ec->shell.base,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400790 WL_SHELL_CONFIGURE,
791 time, device->grab,
792 &es->base, es->x, es->y,
793 es->width, es->height);
794
795 wlsc_surface_update_matrix(es);
796
797 break;
798
799 case WLSC_DEVICE_GRAB_RESIZE_TOP:
800 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
801 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
802 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
803 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
804 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
805 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
806 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
807 case WLSC_DEVICE_GRAB_RESIZE_MASK:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400808 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400809
810 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_LEFT) {
811 sx = x + device->grab_dx;
812 width = device->grab_x - x + device->grab_width;
813 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_RIGHT) {
814 sx = device->grab_x + device->grab_dx;
815 width = x - device->grab_x + device->grab_width;
816 } else {
817 sx = device->grab_x + device->grab_dx;
818 width = device->grab_width;
819 }
820
821 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_TOP) {
822 sy = y + device->grab_dy;
823 height = device->grab_y - y + device->grab_height;
824 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_BOTTOM) {
825 sy = device->grab_y + device->grab_dy;
826 height = y - device->grab_y + device->grab_height;
827 } else {
828 sy = device->grab_y + device->grab_dy;
829 height = device->grab_height;
830 }
831
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400832 wl_surface_post_event(&es->base, &ec->shell.base,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400833 WL_SHELL_CONFIGURE, time, device->grab,
834 &es->base, sx, sy, width, height);
835 break;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400836
837 case WLSC_DEVICE_GRAB_DRAG:
838 es = pick_surface(device, &sx, &sy);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400839 wl_drag_set_pointer_focus(device->drag,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400840 es, time, x, y, sx, sy);
841 if (es)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400842 wl_surface_post_event(&es->base,
843 &device->drag->drag_offer.base,
844 WL_DRAG_OFFER_MOTION,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400845 time, x, y, sx, sy);
846 break;
847
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400848 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500849
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400850 device->sprite->x = device->x - device->hotspot_x;
851 device->sprite->y = device->y - device->hotspot_y;
852 wlsc_surface_update_matrix(device->sprite);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500853
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400854 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500855}
856
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400857static void
858wlsc_input_device_end_grab(struct wlsc_input_device *device, uint32_t time)
859{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400860 struct wl_drag *drag = device->drag;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400861 struct wlsc_surface *es;
862 int32_t sx, sy;
863
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400864 switch (device->grab) {
865 case WLSC_DEVICE_GRAB_DRAG:
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400866 if (drag->target)
867 wl_client_post_event(drag->target,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400868 &drag->drag_offer.base,
869 WL_DRAG_OFFER_DROP);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400870 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400871 device->drag = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400872 break;
873 default:
874 break;
875 }
876
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400877 device->grab = WLSC_DEVICE_GRAB_NONE;
878 es = pick_surface(device, &sx, &sy);
879 wlsc_input_device_set_pointer_focus(device, es, time,
880 device->x, device->y, sx, sy);
881}
882
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500883void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500884notify_button(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400885 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500886{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400887 struct wlsc_surface *surface;
888 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500889
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400890 surface = device->pointer_focus;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400891 if (surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400892 if (state && device->grab == WLSC_DEVICE_GRAB_NONE) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400893 wlsc_surface_raise(surface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400894 device->grab = WLSC_DEVICE_GRAB_MOTION;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400895 device->grab_button = button;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400896 device->grab_time = time;
897 device->grab_x = device->x;
898 device->grab_y = device->y;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400899 wlsc_input_device_set_keyboard_focus(device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400900 surface, time);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500901 }
902
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400903 if (state && button == BTN_LEFT &&
904 device->grab == WLSC_DEVICE_GRAB_MOTION &&
905 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400906 shell_move(NULL, (struct wl_shell *) &compositor->shell,
907 &surface->base, &device->base, time);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400908 else if (state && button == BTN_MIDDLE &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400909 device->grab == WLSC_DEVICE_GRAB_MOTION &&
910 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400911 shell_resize(NULL, (struct wl_shell *) &compositor->shell,
912
913 &surface->base, &device->base, time,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400914 WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400915 else if (device->grab == WLSC_DEVICE_GRAB_NONE ||
916 device->grab == WLSC_DEVICE_GRAB_MOTION)
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400917 wl_surface_post_event(&surface->base,
918 &device->base.base,
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400919 WL_INPUT_DEVICE_BUTTON,
920 time, button, state);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500921
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400922 if (!state &&
923 device->grab != WLSC_DEVICE_GRAB_NONE &&
924 device->grab_button == button) {
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400925 wlsc_input_device_end_grab(device, time);
926 }
927
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400928 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500929 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500930}
931
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500932void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500933notify_key(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400934 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500935{
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500936 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400937 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500938
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400939 switch (key | device->modifier_state) {
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400940 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400941 kill(0, SIGTERM);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500942 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500943 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500944
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400945 switch (key) {
946 case KEY_LEFTCTRL:
947 case KEY_RIGHTCTRL:
948 modifier = MODIFIER_CTRL;
949 break;
950
951 case KEY_LEFTALT:
952 case KEY_RIGHTALT:
953 modifier = MODIFIER_ALT;
954 break;
955
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400956 case KEY_LEFTMETA:
957 case KEY_RIGHTMETA:
958 modifier = MODIFIER_SUPER;
959 break;
960
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400961 default:
962 modifier = 0;
963 break;
964 }
965
966 if (state)
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400967 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400968 else
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400969 device->modifier_state &= ~modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400970
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500971 end = device->keys.data + device->keys.size;
972 for (k = device->keys.data; k < end; k++) {
973 if (*k == key)
974 *k = *--end;
975 }
976 device->keys.size = (void *) end - device->keys.data;
977 if (state) {
978 k = wl_array_add(&device->keys, sizeof *k);
979 *k = key;
980 }
Ray Strodee96dcb82008-12-20 02:00:49 -0500981
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500982 if (device->keyboard_focus != NULL)
983 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400984 &device->base.base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400985 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400986}
987
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400988static void
989input_device_attach(struct wl_client *client,
990 struct wl_input_device *device_base,
991 struct wl_buffer *buffer_base, int32_t x, int32_t y)
992{
993 struct wlsc_input_device *device =
994 (struct wlsc_input_device *) device_base;
995 struct wlsc_buffer *buffer = (struct wlsc_buffer *) buffer_base;
996
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400997 if (device->pointer_focus == NULL)
998 return;
999 if (device->pointer_focus->base.client != client &&
1000 !(&device->pointer_focus->base == &wl_grab_surface &&
1001 device->grab_surface->base.client == client))
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001002 return;
1003
Kristian Høgsbergf4cb2012010-08-16 17:46:25 -04001004 if (buffer == NULL) {
1005 wlsc_input_device_set_pointer_image(device,
1006 WLSC_POINTER_LEFT_PTR);
1007 return;
1008 }
1009
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001010 wlsc_input_device_attach(device, buffer, x, y);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001011}
1012
1013const static struct wl_input_device_interface input_device_interface = {
1014 input_device_attach,
1015};
1016
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001017static uint32_t
1018get_time(void)
1019{
1020 struct timeval tv;
1021
1022 gettimeofday(&tv, NULL);
1023
1024 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001025}
1026
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001027static void
1028handle_surface_destroy(struct wlsc_listener *listener,
1029 struct wlsc_surface *surface)
1030{
1031 struct wlsc_input_device *device =
1032 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001033 uint32_t time = get_time();
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001034
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001035 if (device->keyboard_focus == surface)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001036 wlsc_input_device_set_keyboard_focus(device, NULL, time);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001037 if (device->pointer_focus == surface ||
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001038 (&device->pointer_focus->base == &wl_grab_surface &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001039 device->grab_surface == surface))
1040 wlsc_input_device_end_grab(device, time);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001041}
1042
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001043static void
1044wl_drag_set_pointer_focus(struct wl_drag *drag,
1045 struct wlsc_surface *surface, uint32_t time,
1046 int32_t x, int32_t y, int32_t sx, int32_t sy)
1047{
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001048 char **p, **end;
1049
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001050 if (drag->pointer_focus == &surface->base)
1051 return;
1052
1053 if (drag->pointer_focus &&
1054 (!surface || drag->pointer_focus->client != surface->base.client))
1055 wl_surface_post_event(drag->pointer_focus,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001056 &drag->drag_offer.base,
1057 WL_DRAG_OFFER_POINTER_FOCUS,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001058 time, NULL, 0, 0, 0, 0);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001059
1060 if (surface &&
1061 (!drag->pointer_focus ||
1062 drag->pointer_focus->client != surface->base.client)) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001063 wl_client_post_global(surface->base.client,
1064 &drag->drag_offer.base);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001065
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001066 end = drag->types.data + drag->types.size;
1067 for (p = drag->types.data; p < end; p++)
1068 wl_surface_post_event(&surface->base,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001069 &drag->drag_offer.base,
1070 WL_DRAG_OFFER_OFFER, *p);
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001071 }
1072
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001073 if (surface) {
1074 wl_surface_post_event(&surface->base,
1075 &drag->drag_offer.base,
1076 WL_DRAG_OFFER_POINTER_FOCUS,
1077 time, &surface->base,
1078 x, y, sx, sy);
1079
1080 }
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001081
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001082 drag->pointer_focus = &surface->base;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001083 drag->pointer_focus_time = time;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001084 drag->target = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001085}
1086
1087static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001088drag_offer_accept(struct wl_client *client,
1089 struct wl_drag_offer *offer, uint32_t time, const char *type)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001090{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001091 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001092 char **p, **end;
1093
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001094 /* If the client responds to drag pointer_focus or motion
1095 * events after the pointer has left the surface, we just
1096 * discard the accept requests. The drag source just won't
1097 * get the corresponding 'target' events and eventually the
1098 * next surface/root will start sending events. */
1099 if (time < drag->pointer_focus_time)
1100 return;
1101
1102 drag->target = client;
1103 drag->type = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001104 end = drag->types.data + drag->types.size;
1105 for (p = drag->types.data; p < end; p++)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001106 if (type && strcmp(*p, type) == 0)
1107 drag->type = *p;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001108
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001109 wl_surface_post_event(drag->source, &drag->resource.base,
1110 WL_DRAG_TARGET, drag->type);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001111}
1112
1113static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001114drag_offer_receive(struct wl_client *client,
1115 struct wl_drag_offer *offer, int fd)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001116{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001117 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001118
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001119 wl_surface_post_event(drag->source, &drag->resource.base,
1120 WL_DRAG_FINISH, fd);
1121 close(fd);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001122}
1123
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001124static const struct wl_drag_offer_interface drag_offer_interface = {
1125 drag_offer_accept,
1126 drag_offer_receive
1127};
1128
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001129static void
1130drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
1131{
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001132 char **p;
1133
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001134 p = wl_array_add(&drag->types, sizeof *p);
1135 if (p)
1136 *p = strdup(type);
1137 if (!p || !*p)
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001138 wl_client_post_no_memory(client);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001139}
1140
1141static void
1142drag_activate(struct wl_client *client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001143 struct wl_drag *drag,
1144 struct wl_surface *surface,
1145 struct wl_input_device *input_device, uint32_t time)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001146{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001147 struct wl_display *display = wl_client_get_display (client);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001148 struct wlsc_input_device *device =
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001149 (struct wlsc_input_device *) input_device;
1150 struct wlsc_surface *target;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001151 int32_t sx, sy;
1152
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001153 if (device->grab != WLSC_DEVICE_GRAB_MOTION ||
1154 &device->pointer_focus->base != surface ||
1155 device->grab_time != time)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001156 return;
1157
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001158 drag->source = surface;
1159 drag->input_device = input_device;
1160
1161 drag->drag_offer.base.interface = &wl_drag_offer_interface;
1162 drag->drag_offer.base.implementation =
1163 (void (**)(void)) &drag_offer_interface;
1164
1165 wl_display_add_object(display, &drag->drag_offer.base);
1166
1167 wlsc_input_device_start_grab(device, time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001168 WLSC_DEVICE_GRAB_DRAG);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001169
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001170 device->drag = drag;
1171 target = pick_surface(device, &sx, &sy);
1172 wl_drag_set_pointer_focus(device->drag, target, time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001173 device->x, device->y, sx, sy);
1174}
1175
1176static void
1177drag_cancel(struct wl_client *client, struct wl_drag *drag)
1178{
1179 struct wlsc_input_device *device =
1180 (struct wlsc_input_device *) drag->input_device;
1181
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001182 if (drag->source == NULL || drag->source->client != client)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001183 return;
1184
1185 wlsc_input_device_end_grab(device, get_time());
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001186 device->drag = NULL;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001187}
1188
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001189static const struct wl_drag_interface drag_interface = {
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001190 drag_offer,
1191 drag_activate,
1192 drag_cancel,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001193};
1194
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001195void
1196wlsc_input_device_init(struct wlsc_input_device *device,
1197 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001198{
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001199 device->base.base.interface = &wl_input_device_interface;
1200 device->base.base.implementation =
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001201 (void (**)(void)) &input_device_interface;
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001202 wl_display_add_object(ec->wl_display, &device->base.base);
1203 wl_display_add_global(ec->wl_display, &device->base.base, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001204
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001205 device->x = 100;
1206 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001207 device->ec = ec;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001208 device->sprite = wlsc_surface_create(ec, &ec->argb_visual,
1209 device->x, device->y, 32, 32);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001210 device->hotspot_x = 16;
1211 device->hotspot_y = 16;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001212
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001213 device->listener.func = handle_surface_destroy;
1214 wl_list_insert(ec->surface_destroy_listener_list.prev,
1215 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001216 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001217
1218 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001219}
1220
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001221static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001222wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001223{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001224 struct wlsc_output *output =
1225 container_of(global, struct wlsc_output, base);
1226
1227 wl_client_post_event(client, global,
1228 WL_OUTPUT_GEOMETRY,
1229 output->width, output->height);
1230}
1231
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001232static const char vertex_shader[] =
1233 "uniform mat4 proj;\n"
1234 "attribute vec4 position;\n"
1235 "attribute vec2 texcoord;\n"
1236 "varying vec2 v_texcoord;\n"
1237 "void main()\n"
1238 "{\n"
1239 " gl_Position = proj * position;\n"
1240 " v_texcoord = texcoord;\n"
1241 "}\n";
1242
1243static const char fragment_shader[] =
1244 /* "precision mediump float;\n" */
1245 "varying vec2 v_texcoord;\n"
1246 "uniform sampler2D tex;\n"
1247 "void main()\n"
1248 "{\n"
1249 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1250 "}\n";
1251
Kristian Høgsberga9468212010-06-14 21:03:11 -04001252static int
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001253init_shaders(struct wlsc_compositor *ec)
1254{
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001255 GLuint v, f, program;
1256 const char *p;
1257 char msg[512];
1258 GLfloat vertices[4 * 5];
1259 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001260
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001261 p = vertex_shader;
1262 v = glCreateShader(GL_VERTEX_SHADER);
1263 glShaderSource(v, 1, &p, NULL);
1264 glCompileShader(v);
1265 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
1266 if (!status) {
1267 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
1268 fprintf(stderr, "vertex shader info: %s\n", msg);
1269 return -1;
1270 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001271
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001272 p = fragment_shader;
1273 f = glCreateShader(GL_FRAGMENT_SHADER);
1274 glShaderSource(f, 1, &p, NULL);
1275 glCompileShader(f);
1276 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
1277 if (!status) {
1278 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
1279 fprintf(stderr, "fragment shader info: %s\n", msg);
1280 return -1;
1281 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001282
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001283 program = glCreateProgram();
1284 glAttachShader(program, v);
1285 glAttachShader(program, f);
1286 glBindAttribLocation(program, 0, "position");
1287 glBindAttribLocation(program, 1, "texcoord");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001288
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001289 glLinkProgram(program);
1290 glGetProgramiv(program, GL_LINK_STATUS, &status);
1291 if (!status) {
1292 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1293 fprintf(stderr, "link info: %s\n", msg);
1294 return -1;
1295 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001296
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001297 glUseProgram(program);
1298 ec->proj_uniform = glGetUniformLocation(program, "proj");
1299 ec->tex_uniform = glGetUniformLocation(program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001300
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001301 vertices[ 0] = 0.0;
1302 vertices[ 1] = 0.0;
1303 vertices[ 2] = 0.0;
1304 vertices[ 3] = 0.0;
1305 vertices[ 4] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001306
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001307 vertices[ 5] = 0.0;
1308 vertices[ 6] = 1.0;
1309 vertices[ 7] = 0.0;
1310 vertices[ 8] = 0.0;
1311 vertices[ 9] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001312
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001313 vertices[10] = 1.0;
1314 vertices[11] = 0.0;
1315 vertices[12] = 0.0;
1316 vertices[13] = 1.0;
1317 vertices[14] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001318
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001319 vertices[15] = 1.0;
1320 vertices[16] = 1.0;
1321 vertices[17] = 0.0;
1322 vertices[18] = 1.0;
1323 vertices[19] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001324
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001325 glGenBuffers(1, &ec->vbo);
1326 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
1327 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001328
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001329 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001330}
1331
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001332static const struct wl_interface visual_interface = {
1333 "visual", 1,
1334};
1335
1336static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001337add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001338{
1339 ec->argb_visual.base.interface = &visual_interface;
1340 ec->argb_visual.base.implementation = NULL;
1341 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001342 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001343
1344 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1345 ec->premultiplied_argb_visual.base.implementation = NULL;
1346 wl_display_add_object(ec->wl_display,
1347 &ec->premultiplied_argb_visual.base);
1348 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001349 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001350
1351 ec->rgb_visual.base.interface = &visual_interface;
1352 ec->rgb_visual.base.implementation = NULL;
1353 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001354 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1355}
1356
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001357void
1358wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1359 int x, int y, int width, int height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001360{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001361 output->compositor = c;
1362 output->x = x;
1363 output->y = y;
1364 output->width = width;
1365 output->height = height;
1366
1367 output->background =
1368 background_create(output, option_background);
1369
1370 wlsc_matrix_init(&output->matrix);
1371 wlsc_matrix_translate(&output->matrix,
1372 -output->x - output->width / 2.0,
1373 -output->y - output->height / 2.0, 0);
1374 wlsc_matrix_scale(&output->matrix,
1375 2.0 / output->width, 2.0 / output->height, 1);
1376
1377 output->base.interface = &wl_output_interface;
1378 wl_display_add_object(c->wl_display, &output->base);
1379 wl_display_add_global(c->wl_display, &output->base,
1380 wlsc_output_post_geometry);
1381}
1382
1383int
1384wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1385{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001386 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001387
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001388 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001389
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001390 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001391
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001392 ec->shell.base.interface = &wl_shell_interface;
1393 ec->shell.base.implementation = (void (**)(void)) &shell_interface;
1394 wl_display_add_object(display, &ec->shell.base);
1395 if (wl_display_add_global(display, &ec->shell.base, NULL))
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001396 return -1;
1397
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001398 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001399
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001400 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001401 wl_list_init(&ec->input_device_list);
1402 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001403 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001404
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001405 create_pointer_images(ec);
1406
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001407 screenshooter_create(ec);
1408
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001409 glGenFramebuffers(1, &ec->fbo);
1410 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1411 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001412 if (init_shaders(ec) < 0)
1413 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001414
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001415 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001416 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001417 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001418
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001419 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001420}
1421
1422/* The plan here is to generate a random anonymous socket name and
1423 * advertise that through a service on the session dbus.
1424 */
1425static const char socket_name[] = "\0wayland";
1426
1427int main(int argc, char *argv[])
1428{
1429 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001430 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001431 GError *error = NULL;
1432 GOptionContext *context;
1433
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001434 g_type_init(); /* GdkPixbuf needs this, it seems. */
1435
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001436 context = g_option_context_new(NULL);
1437 g_option_context_add_main_entries(context, option_entries, "Wayland");
1438 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1439 fprintf(stderr, "option parsing failed: %s\n", error->message);
1440 exit(EXIT_FAILURE);
1441 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001442
1443 display = wl_display_create();
1444
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001445 if (getenv("DISPLAY"))
1446 ec = x11_compositor_create(display);
1447 else
1448 ec = drm_compositor_create(display);
1449
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001450 if (ec == NULL) {
1451 fprintf(stderr, "failed to create compositor\n");
1452 exit(EXIT_FAILURE);
1453 }
1454
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001455 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001456 fprintf(stderr, "failed to add socket: %m\n");
1457 exit(EXIT_FAILURE);
1458 }
1459
1460 wl_display_run(display);
1461
1462 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001463}