blob: 09c77ca3c64731480114abc553afda5760d44214 [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
576const static struct wl_shell_interface shell_interface = {
577 shell_move,
578 shell_resize
579};
580
581static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500582compositor_create_surface(struct wl_client *client,
583 struct wl_compositor *compositor, uint32_t id)
584{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500585 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400586 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500587
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400588 surface = wlsc_surface_create(ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400589 if (surface == NULL) {
590 wl_client_post_event(client,
591 (struct wl_object *) ec->wl_display,
592 WL_DISPLAY_NO_MEMORY, 0);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500593 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400594 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500595
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400596 wl_list_insert(ec->surface_list.prev, &surface->link);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400597 surface->base.base.destroy = destroy_surface;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400598 wl_client_add_surface(client, &surface->base,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500599 &surface_interface, id);
600}
601
602static void
603compositor_commit(struct wl_client *client,
604 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500605{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500606 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500607
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400608 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500609 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500610}
611
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500612const static struct wl_compositor_interface compositor_interface = {
613 compositor_create_surface,
614 compositor_commit
615};
616
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500617static void
618wlsc_surface_transform(struct wlsc_surface *surface,
619 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
620{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400621 struct wlsc_vector v = { { x, y, 0, 1 } };
622
623 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400624 *sx = v.f[0] * surface->width;
625 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500626}
627
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500628static void
629wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400630 struct wlsc_surface *surface,
631 uint32_t time)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500632{
633 if (device->keyboard_focus == surface)
634 return;
635
636 if (device->keyboard_focus &&
637 (!surface || device->keyboard_focus->base.client != surface->base.client))
638 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400639 &device->base.base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400640 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400641 time, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500642
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500643 if (surface)
644 wl_surface_post_event(&surface->base,
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400645 &device->base.base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400646 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400647 time, &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500648
649 device->keyboard_focus = surface;
650}
651
652static void
653wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400654 struct wlsc_surface *surface,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400655 uint32_t time,
656 int32_t x, int32_t y,
657 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500658{
659 if (device->pointer_focus == surface)
660 return;
661
662 if (device->pointer_focus &&
663 (!surface || device->pointer_focus->base.client != surface->base.client))
664 wl_surface_post_event(&device->pointer_focus->base,
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400665 &device->base.base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400666 WL_INPUT_DEVICE_POINTER_FOCUS,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400667 time, NULL, 0, 0, 0, 0);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500668 if (surface)
669 wl_surface_post_event(&surface->base,
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400670 &device->base.base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400671 WL_INPUT_DEVICE_POINTER_FOCUS,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400672 time, &surface->base,
673 x, y, sx, sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500674
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400675 if (!surface)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400676 wlsc_input_device_set_pointer_image(device,
677 WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400678
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500679 device->pointer_focus = surface;
680}
681
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500682static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500683pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500684{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500685 struct wlsc_compositor *ec = device->ec;
686 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500687
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400688 wl_list_for_each(es, &ec->surface_list, link) {
689 wlsc_surface_transform(es, device->x, device->y, sx, sy);
690 if (0 <= *sx && *sx < es->width &&
691 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500692 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400693 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500694
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500695 return NULL;
696}
697
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400698static void
699wl_drag_reset(struct wl_drag *drag);
700static void
701wl_drag_set_pointer_focus(struct wl_drag *drag,
702 struct wlsc_surface *surface, uint32_t time,
703 int32_t x, int32_t y, int32_t sx, int32_t sy);
704
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500705void
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400706notify_motion(struct wlsc_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500707{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500708 struct wlsc_surface *es;
709 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500710 struct wlsc_output *output;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400711 int32_t sx, sy, width, height;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500712
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500713 /* FIXME: We need some multi head love here. */
714 output = container_of(ec->output_list.next, struct wlsc_output, link);
715 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500716 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500717 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500718 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500719 if (x >= output->x + output->width)
720 x = output->x + output->width - 1;
721 if (y >= output->y + output->height)
722 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500723
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500724 device->x = x;
725 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500726
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400727 switch (device->grab) {
728 case WLSC_DEVICE_GRAB_NONE:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400729 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400730 wlsc_input_device_set_pointer_focus(device, es,
731 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400732 if (es)
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400733 wl_surface_post_event(&es->base, &device->base.base,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400734 WL_INPUT_DEVICE_MOTION,
735 time, x, y, sx, sy);
736 break;
737
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400738 case WLSC_DEVICE_GRAB_MOTION:
739 es = device->pointer_focus;
740 wlsc_surface_transform(es, x, y, &sx, &sy);
741 wl_surface_post_event(&es->base, &device->base.base,
742 WL_INPUT_DEVICE_MOTION,
743 time, x, y, sx, sy);
744 break;
745
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400746 case WLSC_DEVICE_GRAB_MOVE:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400747 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400748 es->x = x + device->grab_dx;
749 es->y = y + device->grab_dy;;
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400750 wl_surface_post_event(&es->base, &ec->shell.base,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400751 WL_SHELL_CONFIGURE,
752 time, device->grab,
753 &es->base, es->x, es->y,
754 es->width, es->height);
755
756 wlsc_surface_update_matrix(es);
757
758 break;
759
760 case WLSC_DEVICE_GRAB_RESIZE_TOP:
761 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
762 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
763 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
764 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
765 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
766 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
767 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
768 case WLSC_DEVICE_GRAB_RESIZE_MASK:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400769 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400770
771 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_LEFT) {
772 sx = x + device->grab_dx;
773 width = device->grab_x - x + device->grab_width;
774 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_RIGHT) {
775 sx = device->grab_x + device->grab_dx;
776 width = x - device->grab_x + device->grab_width;
777 } else {
778 sx = device->grab_x + device->grab_dx;
779 width = device->grab_width;
780 }
781
782 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_TOP) {
783 sy = y + device->grab_dy;
784 height = device->grab_y - y + device->grab_height;
785 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_BOTTOM) {
786 sy = device->grab_y + device->grab_dy;
787 height = y - device->grab_y + device->grab_height;
788 } else {
789 sy = device->grab_y + device->grab_dy;
790 height = device->grab_height;
791 }
792
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400793 wl_surface_post_event(&es->base, &ec->shell.base,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400794 WL_SHELL_CONFIGURE, time, device->grab,
795 &es->base, sx, sy, width, height);
796 break;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400797
798 case WLSC_DEVICE_GRAB_DRAG:
799 es = pick_surface(device, &sx, &sy);
800 wl_drag_set_pointer_focus(&device->drag,
801 es, time, x, y, sx, sy);
802 if (es)
803 wl_surface_post_event(&es->base, &device->drag.base,
804 WL_DRAG_MOTION,
805 time, x, y, sx, sy);
806 break;
807
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400808 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500809
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400810 device->sprite->x = device->x - device->hotspot_x;
811 device->sprite->y = device->y - device->hotspot_y;
812 wlsc_surface_update_matrix(device->sprite);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500813
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400814 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500815}
816
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400817static void
818wlsc_input_device_end_grab(struct wlsc_input_device *device, uint32_t time)
819{
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400820 struct wl_drag *drag = &device->drag;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400821 struct wlsc_surface *es;
822 int32_t sx, sy;
823
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400824 switch (device->grab) {
825 case WLSC_DEVICE_GRAB_DRAG:
826 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
827 wl_surface_post_event(drag->source, &drag->base,
828 WL_DRAG_FINISH);
829 wl_drag_reset(drag);
830 break;
831 default:
832 break;
833 }
834
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400835 device->grab = WLSC_DEVICE_GRAB_NONE;
836 es = pick_surface(device, &sx, &sy);
837 wlsc_input_device_set_pointer_focus(device, es, time,
838 device->x, device->y, sx, sy);
839}
840
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500841void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500842notify_button(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400843 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500844{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400845 struct wlsc_surface *surface;
846 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500847
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400848 surface = device->pointer_focus;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400849 if (surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400850 if (state && device->grab == WLSC_DEVICE_GRAB_NONE) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400851 wlsc_surface_raise(surface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400852 device->grab = WLSC_DEVICE_GRAB_MOTION;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400853 device->grab_button = button;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400854 device->grab_time = time;
855 device->grab_x = device->x;
856 device->grab_y = device->y;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400857 wlsc_input_device_set_keyboard_focus(device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400858 surface, time);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500859 }
860
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400861 if (state && button == BTN_LEFT &&
862 device->grab == WLSC_DEVICE_GRAB_MOTION &&
863 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400864 shell_move(NULL, (struct wl_shell *) &compositor->shell,
865 &surface->base, &device->base, time);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400866 else if (state && button == BTN_MIDDLE &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400867 device->grab == WLSC_DEVICE_GRAB_MOTION &&
868 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400869 shell_resize(NULL, (struct wl_shell *) &compositor->shell,
870
871 &surface->base, &device->base, time,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400872 WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400873 else if (device->grab == WLSC_DEVICE_GRAB_NONE ||
874 device->grab == WLSC_DEVICE_GRAB_MOTION)
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400875 wl_surface_post_event(&surface->base,
876 &device->base.base,
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400877 WL_INPUT_DEVICE_BUTTON,
878 time, button, state);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500879
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400880 if (!state &&
881 device->grab != WLSC_DEVICE_GRAB_NONE &&
882 device->grab_button == button) {
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400883 wlsc_input_device_end_grab(device, time);
884 }
885
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400886 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500887 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500888}
889
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500890void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500891notify_key(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400892 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500893{
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500894 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400895 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500896
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400897 switch (key | device->modifier_state) {
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400898 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400899 kill(0, SIGTERM);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500900 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500901 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500902
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400903 switch (key) {
904 case KEY_LEFTCTRL:
905 case KEY_RIGHTCTRL:
906 modifier = MODIFIER_CTRL;
907 break;
908
909 case KEY_LEFTALT:
910 case KEY_RIGHTALT:
911 modifier = MODIFIER_ALT;
912 break;
913
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400914 case KEY_LEFTMETA:
915 case KEY_RIGHTMETA:
916 modifier = MODIFIER_SUPER;
917 break;
918
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400919 default:
920 modifier = 0;
921 break;
922 }
923
924 if (state)
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400925 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400926 else
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400927 device->modifier_state &= ~modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400928
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500929 end = device->keys.data + device->keys.size;
930 for (k = device->keys.data; k < end; k++) {
931 if (*k == key)
932 *k = *--end;
933 }
934 device->keys.size = (void *) end - device->keys.data;
935 if (state) {
936 k = wl_array_add(&device->keys, sizeof *k);
937 *k = key;
938 }
Ray Strodee96dcb82008-12-20 02:00:49 -0500939
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500940 if (device->keyboard_focus != NULL)
941 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400942 &device->base.base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400943 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400944}
945
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400946static void
947input_device_attach(struct wl_client *client,
948 struct wl_input_device *device_base,
949 struct wl_buffer *buffer_base, int32_t x, int32_t y)
950{
951 struct wlsc_input_device *device =
952 (struct wlsc_input_device *) device_base;
953 struct wlsc_buffer *buffer = (struct wlsc_buffer *) buffer_base;
954
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400955 if (device->pointer_focus == NULL)
956 return;
957 if (device->pointer_focus->base.client != client &&
958 !(&device->pointer_focus->base == &wl_grab_surface &&
959 device->grab_surface->base.client == client))
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400960 return;
961
Kristian Høgsbergf4cb2012010-08-16 17:46:25 -0400962 if (buffer == NULL) {
963 wlsc_input_device_set_pointer_image(device,
964 WLSC_POINTER_LEFT_PTR);
965 return;
966 }
967
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400968 wlsc_input_device_attach(device, buffer, x, y);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400969}
970
971const static struct wl_input_device_interface input_device_interface = {
972 input_device_attach,
973};
974
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400975static uint32_t
976get_time(void)
977{
978 struct timeval tv;
979
980 gettimeofday(&tv, NULL);
981
982 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500983}
984
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400985static void
986handle_surface_destroy(struct wlsc_listener *listener,
987 struct wlsc_surface *surface)
988{
989 struct wlsc_input_device *device =
990 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400991 uint32_t time = get_time();
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400992
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400993 if (device->keyboard_focus == surface)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400994 wlsc_input_device_set_keyboard_focus(device, NULL, time);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400995 if (device->pointer_focus == surface ||
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400996 (&device->pointer_focus->base == &wl_grab_surface &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400997 device->grab_surface == surface))
998 wlsc_input_device_end_grab(device, time);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400999}
1000
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001001static void
1002wl_drag_set_pointer_focus(struct wl_drag *drag,
1003 struct wlsc_surface *surface, uint32_t time,
1004 int32_t x, int32_t y, int32_t sx, int32_t sy)
1005{
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001006 char **p, **end;
1007
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001008 if (drag->pointer_focus == &surface->base)
1009 return;
1010
1011 if (drag->pointer_focus &&
1012 (!surface || drag->pointer_focus->client != surface->base.client))
1013 wl_surface_post_event(drag->pointer_focus,
1014 &drag->base,
1015 WL_DRAG_POINTER_FOCUS,
1016 time, NULL, 0, 0, 0, 0);
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001017 if (surface) {
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001018 wl_surface_post_event(&surface->base,
1019 &drag->base,
1020 WL_DRAG_POINTER_FOCUS,
1021 time, &surface->base,
1022 x, y, sx, sy);
1023
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001024 end = drag->types.data + drag->types.size;
1025 for (p = drag->types.data; p < end; p++)
1026 wl_surface_post_event(&surface->base,
1027 &drag->base,
1028 WL_DRAG_OFFER, *p);
1029 }
1030
1031
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001032 drag->pointer_focus = &surface->base;
1033}
1034
1035static void
1036wl_drag_reset(struct wl_drag *drag)
1037{
1038 char **p, **end;
1039
1040 end = drag->types.data + drag->types.size;
1041 for (p = drag->types.data; p < end; p++)
1042 free(*p);
1043 wl_array_release(&drag->types);
1044 wl_array_init(&drag->types);
1045
1046 drag->source = NULL;
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -04001047
1048 /* FIXME: We need to reset drag->target too, but can't right
1049 * now because we need it for send/drop.
1050 *
1051 * drag->target = NULL; */
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001052 drag->time = 0;
1053 drag->pointer_focus = NULL;
1054}
1055
1056static void
1057drag_prepare(struct wl_client *client,
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001058 struct wl_drag *drag, struct wl_surface *surface, uint32_t time)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001059{
1060 struct wlsc_input_device *device =
1061 (struct wlsc_input_device *) drag->input_device;
1062
1063 if (&device->pointer_focus->base != surface ||
1064 device->grab_time != time)
1065 return;
1066
1067 wl_drag_reset(drag);
1068 drag->source = surface;
1069 drag->time = time;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001070}
1071
1072static void
1073drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
1074{
1075 struct wl_display *display = wl_client_get_display (client);
1076 struct wlsc_input_device *device =
1077 (struct wlsc_input_device *) drag->input_device;
1078 char **p;
1079
1080 if (drag->source == NULL ||
1081 drag->source->client != client ||
1082 device->grab != WLSC_DEVICE_GRAB_MOTION ||
1083 &device->pointer_focus->base != drag->source ||
1084 device->grab_time != drag->time)
1085 return;
1086
1087 p = wl_array_add(&drag->types, sizeof *p);
1088 if (p)
1089 *p = strdup(type);
1090 if (!p || !*p)
1091 wl_client_post_event(client,
1092 (struct wl_object *) display,
1093 WL_DISPLAY_NO_MEMORY);
1094}
1095
1096static void
1097drag_activate(struct wl_client *client,
1098 struct wl_drag *drag)
1099{
1100 struct wlsc_input_device *device =
1101 (struct wlsc_input_device *) drag->input_device;
1102 struct wlsc_surface *surface;
1103 int32_t sx, sy;
1104
1105 if (drag->source == NULL ||
1106 drag->source->client != client ||
1107 device->grab != WLSC_DEVICE_GRAB_MOTION ||
1108 &device->pointer_focus->base != drag->source ||
1109 device->grab_time != drag->time)
1110 return;
1111
1112 wlsc_input_device_start_grab(device, drag->time,
1113 WLSC_DEVICE_GRAB_DRAG);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001114
1115 surface = pick_surface(device, &sx, &sy);
1116 wl_drag_set_pointer_focus(&device->drag, surface, drag->time,
1117 device->x, device->y, sx, sy);
1118}
1119
1120static void
1121drag_cancel(struct wl_client *client, struct wl_drag *drag)
1122{
1123 struct wlsc_input_device *device =
1124 (struct wlsc_input_device *) drag->input_device;
1125
1126 if (drag->source == NULL ||
1127 drag->source->client != client ||
1128 device->grab != WLSC_DEVICE_GRAB_DRAG)
1129 return;
1130
1131 wlsc_input_device_end_grab(device, get_time());
1132}
1133
1134static void
1135drag_send(struct wl_client *client,
1136 struct wl_drag *drag, struct wl_array *contents)
1137{
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -04001138 wl_client_post_event(drag->target,
1139 &drag->base, WL_DRAG_DROP, contents);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001140}
1141
1142static void
1143drag_accept(struct wl_client *client,
1144 struct wl_drag *drag, const char *type)
1145{
1146 char **p, **end;
1147
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001148 /* If the client responds to drag motion events after the
1149 * pointer has left the surface, we just discard the accept
1150 * requests. The drag source just won't get the corresponding
1151 * 'target' events and the next surface/root will start
1152 * sending events. */
1153 if (drag->pointer_focus == NULL)
1154 return;
1155
1156 /* The accept request may arrive after the pointer has left
1157 * the surface that received the drag motion events that
1158 * triggered the request. But if the pointer re-enters the
1159 * surface (or another surface from the same client) and we
1160 * receive the 'accept' requests from the first visit to the
1161 * surface later, this check will pass. Then we end up
1162 * sending the 'target' event for the old 'accept' requests
1163 * events *after* potentially sending 'target' from the root
1164 * surface, out of order. So we need to track the time of
1165 * pointer_focus and this request needs a time stamp so we can
1166 * compare and reject 'accept' requests that are older than
1167 * the pointer_focus in timestamp. */
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001168 if (drag->pointer_focus->client != client)
1169 return;
1170
1171 /* FIXME: We need a serial number here to correlate the accept
1172 * request with a pointer_focus/motion event. */
1173 drag->target = client;
1174
1175 end = drag->types.data + drag->types.size;
1176 for (p = drag->types.data; p < end; p++)
1177 if (strcmp(*p, type) == 0)
1178 drag->type = *p;
1179
1180 wl_surface_post_event(drag->source, &drag->base,
1181 WL_DRAG_TARGET, drag->type);
1182}
1183
1184static const struct wl_drag_interface drag_interface = {
1185 drag_prepare,
1186 drag_offer,
1187 drag_activate,
1188 drag_cancel,
1189 drag_send,
1190 drag_accept
1191};
1192
1193static void
1194wl_drag_post_device(struct wl_client *client, struct wl_object *global)
1195{
1196 struct wl_drag *drag = container_of(global, struct wl_drag, base);
1197
1198 wl_client_post_event(client, global,
1199 WL_DRAG_DEVICE, drag->input_device);
1200}
1201
1202static void
1203wl_drag_init(struct wl_drag *drag,
1204 struct wl_display *display, struct wl_input_device *input_device)
1205{
1206 drag->base.interface = &wl_drag_interface;
1207 drag->base.implementation = (void (**)(void))
1208 &drag_interface;
1209
1210 wl_display_add_object(display, &drag->base);
1211 wl_display_add_global(display, &drag->base, wl_drag_post_device);
1212
1213 drag->source = NULL;
1214 wl_array_init(&drag->types);
1215 drag->input_device = input_device;
1216}
1217
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001218void
1219wlsc_input_device_init(struct wlsc_input_device *device,
1220 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001221{
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001222 device->base.base.interface = &wl_input_device_interface;
1223 device->base.base.implementation =
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001224 (void (**)(void)) &input_device_interface;
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001225 wl_display_add_object(ec->wl_display, &device->base.base);
1226 wl_display_add_global(ec->wl_display, &device->base.base, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001227
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001228 wl_drag_init(&device->drag, ec->wl_display, &device->base);
1229
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001230 device->x = 100;
1231 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001232 device->ec = ec;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001233 device->sprite = wlsc_surface_create(ec, &ec->argb_visual,
1234 device->x, device->y, 32, 32);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001235 device->hotspot_x = 16;
1236 device->hotspot_y = 16;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001237
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001238 device->listener.func = handle_surface_destroy;
1239 wl_list_insert(ec->surface_destroy_listener_list.prev,
1240 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001241 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001242
1243 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001244}
1245
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001246static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001247wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001248{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001249 struct wlsc_output *output =
1250 container_of(global, struct wlsc_output, base);
1251
1252 wl_client_post_event(client, global,
1253 WL_OUTPUT_GEOMETRY,
1254 output->width, output->height);
1255}
1256
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001257static const char vertex_shader[] =
1258 "uniform mat4 proj;\n"
1259 "attribute vec4 position;\n"
1260 "attribute vec2 texcoord;\n"
1261 "varying vec2 v_texcoord;\n"
1262 "void main()\n"
1263 "{\n"
1264 " gl_Position = proj * position;\n"
1265 " v_texcoord = texcoord;\n"
1266 "}\n";
1267
1268static const char fragment_shader[] =
1269 /* "precision mediump float;\n" */
1270 "varying vec2 v_texcoord;\n"
1271 "uniform sampler2D tex;\n"
1272 "void main()\n"
1273 "{\n"
1274 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1275 "}\n";
1276
Kristian Høgsberga9468212010-06-14 21:03:11 -04001277static int
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001278init_shaders(struct wlsc_compositor *ec)
1279{
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001280 GLuint v, f, program;
1281 const char *p;
1282 char msg[512];
1283 GLfloat vertices[4 * 5];
1284 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001285
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001286 p = vertex_shader;
1287 v = glCreateShader(GL_VERTEX_SHADER);
1288 glShaderSource(v, 1, &p, NULL);
1289 glCompileShader(v);
1290 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
1291 if (!status) {
1292 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
1293 fprintf(stderr, "vertex shader 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 p = fragment_shader;
1298 f = glCreateShader(GL_FRAGMENT_SHADER);
1299 glShaderSource(f, 1, &p, NULL);
1300 glCompileShader(f);
1301 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
1302 if (!status) {
1303 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
1304 fprintf(stderr, "fragment shader info: %s\n", msg);
1305 return -1;
1306 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001307
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001308 program = glCreateProgram();
1309 glAttachShader(program, v);
1310 glAttachShader(program, f);
1311 glBindAttribLocation(program, 0, "position");
1312 glBindAttribLocation(program, 1, "texcoord");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001313
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001314 glLinkProgram(program);
1315 glGetProgramiv(program, GL_LINK_STATUS, &status);
1316 if (!status) {
1317 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1318 fprintf(stderr, "link info: %s\n", msg);
1319 return -1;
1320 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001321
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001322 glUseProgram(program);
1323 ec->proj_uniform = glGetUniformLocation(program, "proj");
1324 ec->tex_uniform = glGetUniformLocation(program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001325
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001326 vertices[ 0] = 0.0;
1327 vertices[ 1] = 0.0;
1328 vertices[ 2] = 0.0;
1329 vertices[ 3] = 0.0;
1330 vertices[ 4] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001331
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001332 vertices[ 5] = 0.0;
1333 vertices[ 6] = 1.0;
1334 vertices[ 7] = 0.0;
1335 vertices[ 8] = 0.0;
1336 vertices[ 9] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001337
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001338 vertices[10] = 1.0;
1339 vertices[11] = 0.0;
1340 vertices[12] = 0.0;
1341 vertices[13] = 1.0;
1342 vertices[14] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001343
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001344 vertices[15] = 1.0;
1345 vertices[16] = 1.0;
1346 vertices[17] = 0.0;
1347 vertices[18] = 1.0;
1348 vertices[19] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001349
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001350 glGenBuffers(1, &ec->vbo);
1351 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
1352 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001353
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001354 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001355}
1356
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001357static const struct wl_interface visual_interface = {
1358 "visual", 1,
1359};
1360
1361static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001362add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001363{
1364 ec->argb_visual.base.interface = &visual_interface;
1365 ec->argb_visual.base.implementation = NULL;
1366 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001367 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001368
1369 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1370 ec->premultiplied_argb_visual.base.implementation = NULL;
1371 wl_display_add_object(ec->wl_display,
1372 &ec->premultiplied_argb_visual.base);
1373 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001374 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001375
1376 ec->rgb_visual.base.interface = &visual_interface;
1377 ec->rgb_visual.base.implementation = NULL;
1378 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001379 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1380}
1381
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001382void
1383wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1384 int x, int y, int width, int height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001385{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001386 output->compositor = c;
1387 output->x = x;
1388 output->y = y;
1389 output->width = width;
1390 output->height = height;
1391
1392 output->background =
1393 background_create(output, option_background);
1394
1395 wlsc_matrix_init(&output->matrix);
1396 wlsc_matrix_translate(&output->matrix,
1397 -output->x - output->width / 2.0,
1398 -output->y - output->height / 2.0, 0);
1399 wlsc_matrix_scale(&output->matrix,
1400 2.0 / output->width, 2.0 / output->height, 1);
1401
1402 output->base.interface = &wl_output_interface;
1403 wl_display_add_object(c->wl_display, &output->base);
1404 wl_display_add_global(c->wl_display, &output->base,
1405 wlsc_output_post_geometry);
1406}
1407
1408int
1409wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1410{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001411 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001412
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001413 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001414
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001415 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001416
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001417 ec->shell.base.interface = &wl_shell_interface;
1418 ec->shell.base.implementation = (void (**)(void)) &shell_interface;
1419 wl_display_add_object(display, &ec->shell.base);
1420 if (wl_display_add_global(display, &ec->shell.base, NULL))
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001421 return -1;
1422
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001423 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001424
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001425 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001426 wl_list_init(&ec->input_device_list);
1427 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001428 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001429
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001430 create_pointer_images(ec);
1431
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001432 screenshooter_create(ec);
1433
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001434 glGenFramebuffers(1, &ec->fbo);
1435 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1436 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001437 if (init_shaders(ec) < 0)
1438 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001439
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001440 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001441 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001442 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001443
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001444 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001445}
1446
1447/* The plan here is to generate a random anonymous socket name and
1448 * advertise that through a service on the session dbus.
1449 */
1450static const char socket_name[] = "\0wayland";
1451
1452int main(int argc, char *argv[])
1453{
1454 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001455 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001456 GError *error = NULL;
1457 GOptionContext *context;
1458
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001459 g_type_init(); /* GdkPixbuf needs this, it seems. */
1460
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001461 context = g_option_context_new(NULL);
1462 g_option_context_add_main_entries(context, option_entries, "Wayland");
1463 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1464 fprintf(stderr, "option parsing failed: %s\n", error->message);
1465 exit(EXIT_FAILURE);
1466 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001467
1468 display = wl_display_create();
1469
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001470 if (getenv("DISPLAY"))
1471 ec = x11_compositor_create(display);
1472 else
1473 ec = drm_compositor_create(display);
1474
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001475 if (ec == NULL) {
1476 fprintf(stderr, "failed to create compositor\n");
1477 exit(EXIT_FAILURE);
1478 }
1479
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001480 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001481 fprintf(stderr, "failed to add socket: %m\n");
1482 exit(EXIT_FAILURE);
1483 }
1484
1485 wl_display_run(display);
1486
1487 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001488}