blob: 8cfa394ef2e461b9741fcffae3d8297dff80cbe9 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05008 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050017 */
18
Kristian Høgsberg06bc2642010-12-01 09:50:16 -050019#define _GNU_SOURCE
20
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040021#include <stdio.h>
22#include <string.h>
23#include <stdlib.h>
24#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050025#include <stdarg.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040026#include <sys/ioctl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040027#include <fcntl.h>
28#include <unistd.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050029#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050030#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040031#include <linux/input.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050032
Kristian Høgsberga1f3f602010-08-03 09:26:44 -040033#include "wayland-server-protocol.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040034#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050035
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010036/* The plan here is to generate a random anonymous socket name and
37 * advertise that through a service on the session dbus.
38 */
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -050039static const char *option_socket_name = NULL;
Kristian Høgsberg61a82512010-10-26 11:26:44 -040040static const char *option_background = "background.jpg";
41static const char *option_geometry = "1024x640";
42static int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050043
44static const GOptionEntry option_entries[] = {
45 { "background", 'b', 0, G_OPTION_ARG_STRING,
46 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -040047 { "connector", 'c', 0, G_OPTION_ARG_INT,
48 &option_connector, "KMS connector" },
Kristian Høgsberg61a82512010-10-26 11:26:44 -040049 { "geometry", 'g', 0, G_OPTION_ARG_STRING,
50 &option_geometry, "Geometry" },
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010051 { "socket", 's', 0, G_OPTION_ARG_STRING,
52 &option_socket_name, "Socket Name" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050053 { NULL }
54};
55
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050056static void
57wlsc_matrix_init(struct wlsc_matrix *matrix)
58{
59 static const struct wlsc_matrix identity = {
60 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
61 };
62
63 memcpy(matrix, &identity, sizeof identity);
64}
65
66static void
67wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
68{
69 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040070 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050071 div_t d;
72 int i, j;
73
74 for (i = 0; i < 16; i++) {
75 tmp.d[i] = 0;
76 d = div(i, 4);
77 row = m->d + d.quot * 4;
78 column = n->d + d.rem;
79 for (j = 0; j < 4; j++)
80 tmp.d[i] += row[j] * column[j * 4];
81 }
82 memcpy(m, &tmp, sizeof tmp);
83}
84
85static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040086wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050087{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -050088 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050089 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
90 };
91
92 wlsc_matrix_multiply(matrix, &translate);
93}
94
95static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040096wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050097{
98 struct wlsc_matrix scale = {
99 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
100 };
101
102 wlsc_matrix_multiply(matrix, &scale);
103}
104
105static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400106wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
107{
108 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400109 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400110
111 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400112 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400113 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400114 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400115 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500116
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400117 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400118}
119
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400120static struct wlsc_surface *
121wlsc_surface_create(struct wlsc_compositor *compositor,
122 struct wl_visual *visual,
123 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500124{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400125 struct wlsc_surface *surface;
126
127 surface = malloc(sizeof *surface);
128 if (surface == NULL)
129 return NULL;
130
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500131 wl_list_init(&surface->surface.destroy_listener_list);
132
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500133 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400134 glBindTexture(GL_TEXTURE_2D, surface->texture);
135 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
136 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
137 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
138 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
139
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500140 surface->compositor = compositor;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500141 surface->visual = visual;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400142 surface->x = x;
143 surface->y = y;
144 surface->width = width;
145 surface->height = height;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400146 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400147 wlsc_matrix_scale(&surface->matrix, width, height, 1);
148 wlsc_matrix_translate(&surface->matrix, x, y, 0);
149
150 wlsc_matrix_init(&surface->matrix_inv);
151 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
152 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500153
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400154 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500155}
156
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500157static uint32_t
158get_time(void)
159{
160 struct timeval tv;
161
162 gettimeofday(&tv, NULL);
163
164 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
165}
166
Kristian Høgsberg54879822008-11-23 17:07:32 -0500167static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400168destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500169{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400170 struct wlsc_surface *surface =
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500171 container_of(resource, struct wlsc_surface, surface.resource);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400172 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500173 struct wl_listener *l, *next;
Kristian Høgsberg4685fa32010-12-06 21:38:50 -0500174 uint32_t time;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400175
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400176 wl_list_remove(&surface->link);
177 glDeleteTextures(1, &surface->texture);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400178
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500179 time = get_time();
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500180 wl_list_for_each_safe(l, next,
181 &surface->surface.destroy_listener_list, link)
Kristian Høgsberg4685fa32010-12-06 21:38:50 -0500182 l->func(l, &surface->surface, time);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400183
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400184 free(surface);
Kristian Høgsberg117d5132010-08-11 08:56:47 -0400185
186 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500187}
188
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400189static int
190texture_from_png(const char *filename, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500191{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400192 GdkPixbuf *pixbuf;
193 GError *error = NULL;
194 void *data;
195 GLenum format;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500196
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400197 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
198 width, height,
199 FALSE, &error);
200 if (error != NULL)
201 return -1;
202
203 data = gdk_pixbuf_get_pixels(pixbuf);
204
205 if (gdk_pixbuf_get_has_alpha(pixbuf))
206 format = GL_RGBA;
207 else
208 format = GL_RGB;
209
Chia-I Wu1f411902010-10-29 15:20:16 +0800210 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
211 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400212
213 gdk_pixbuf_unref(pixbuf);
214
215 return 0;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500216}
217
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400218static const struct {
219 const char *filename;
220 int hotspot_x, hotspot_y;
221} pointer_images[] = {
Kristian Høgsberg4219a402010-08-16 16:43:03 -0400222 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
223 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
224 { DATADIR "/wayland/bottom_side.png", 16, 20 },
225 { DATADIR "/wayland/grabbing.png", 20, 17 },
226 { DATADIR "/wayland/left_ptr.png", 10, 5 },
227 { DATADIR "/wayland/left_side.png", 10, 20 },
228 { DATADIR "/wayland/right_side.png", 30, 19 },
229 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
230 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
231 { DATADIR "/wayland/top_side.png", 18, 8 },
232 { DATADIR "/wayland/xterm.png", 15, 15 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400233};
234
235static void
236create_pointer_images(struct wlsc_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500237{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400238 int i, count;
239 GLuint texture;
240 const int width = 32, height = 32;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400241
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400242 glGenTextures(1, &texture);
243 glBindTexture(GL_TEXTURE_2D, texture);
244 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
245 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
246 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
247 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400248
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400249 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400250 ec->pointer_buffers = malloc(count * sizeof *ec->pointer_buffers);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400251 for (i = 0; i < count; i++) {
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400252 ec->pointer_buffers[i] =
253 wlsc_drm_buffer_create(ec, width, height,
254 &ec->argb_visual);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400255 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400256 ec->pointer_buffers[i]->image);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400257 texture_from_png(pointer_images[i].filename, width, height);
258 }
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400259}
260
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500261static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500262background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500263{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500264 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500265 GdkPixbuf *pixbuf;
266 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500267 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500268 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500269
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400270 background = wlsc_surface_create(output->compositor,
271 &output->compositor->rgb_visual,
272 output->x, output->y,
273 output->width, output->height);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500274 if (background == NULL)
275 return NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500276
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500277 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500278 output->width,
279 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500280 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500281 if (error != NULL) {
282 free(background);
283 return NULL;
284 }
285
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500286 data = gdk_pixbuf_get_pixels(pixbuf);
287
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400288 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500289 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500290 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500291 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500292
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500293 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
294 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500295 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500296
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500297 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500298}
299
300static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400301wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500302{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500303 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400304 struct wlsc_matrix tmp;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500305
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400306 tmp = es->matrix;
307 wlsc_matrix_multiply(&tmp, &output->matrix);
308 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
309 glUniform1i(ec->tex_uniform, 0);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500310
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500311 if (es->visual == &ec->argb_visual) {
312 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
313 glEnable(GL_BLEND);
314 } else if (es->visual == &ec->premultiplied_argb_visual) {
315 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
316 glEnable(GL_BLEND);
317 } else {
318 glDisable(GL_BLEND);
319 }
320
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500321 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400322 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
323 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
324 5 * sizeof(GLfloat), NULL);
325 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
326 5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
327 glEnableVertexAttribArray(0);
328 glEnableVertexAttribArray(1);
329 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500330}
331
332static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400333wlsc_surface_raise(struct wlsc_surface *surface)
334{
335 struct wlsc_compositor *compositor = surface->compositor;
336
337 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400338 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400339}
340
341static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400342wlsc_surface_update_matrix(struct wlsc_surface *es)
343{
344 wlsc_matrix_init(&es->matrix);
345 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
346 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
347
348 wlsc_matrix_init(&es->matrix_inv);
349 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
350 wlsc_matrix_scale(&es->matrix_inv,
351 1.0 / es->width, 1.0 / es->height, 1);
352}
353
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400354void
355wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400356{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400357 wl_display_post_frame(compositor->wl_display, msecs);
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400358 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400359}
360
361static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400362wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400363{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500364 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500365 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500366 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500367
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500368 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500369
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500370 if (output->background)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400371 wlsc_surface_draw(output->background, output);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500372 else
373 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400374
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400375 wl_list_for_each_reverse(es, &ec->surface_list, link)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400376 wlsc_surface_draw(es, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400377
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400378 if (ec->focus)
379 wl_list_for_each(eid, &ec->input_device_list, link)
380 wlsc_surface_draw(eid->sprite, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500381}
382
383static void
384repaint(void *data)
385{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500386 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500387 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500388
389 if (!ec->repaint_needed) {
390 ec->repaint_on_timeout = 0;
391 return;
392 }
393
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500394 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400395 wlsc_output_repaint(output);
396
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400397 ec->present(ec);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500398
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500399 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400400}
401
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400402void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400403wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400404{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400405 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400406 if (compositor->repaint_on_timeout)
407 return;
408
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400409 wl_event_source_timer_update(compositor->timer_source, 1);
410 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400411}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500412
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500413static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500414surface_destroy(struct wl_client *client,
415 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400416{
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500417 wl_resource_destroy(&surface->resource, client);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400418}
419
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500420static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500421surface_attach(struct wl_client *client,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400422 struct wl_surface *surface, struct wl_buffer *buffer)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400423{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500424 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400425
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400426 buffer->attach(buffer, surface);
427 es->buffer = buffer;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400428}
429
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500430static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500431surface_map(struct wl_client *client,
432 struct wl_surface *surface,
433 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400434{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500435 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400436
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400437 es->x = x;
438 es->y = y;
439 es->width = width;
440 es->height = height;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400441
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400442 wlsc_surface_update_matrix(es);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400443 wlsc_compositor_schedule_repaint(es->compositor);
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øgsberg9d69f8e2010-09-03 14:46:38 -0400451 struct wlsc_surface *es = (struct wlsc_surface *) surface;
452
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400453 es->buffer->damage(es->buffer, surface, x, y, width, height);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400454 wlsc_compositor_schedule_repaint(es->compositor);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500455}
456
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500457const static struct wl_surface_interface surface_interface = {
458 surface_destroy,
459 surface_attach,
460 surface_map,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500461 surface_damage
462};
463
464static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400465wlsc_input_device_attach(struct wlsc_input_device *device,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400466 struct wl_buffer *buffer, int x, int y)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400467{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500468 struct wlsc_compositor *ec =
469 (struct wlsc_compositor *) device->input_device.compositor;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400470
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500471 buffer->attach(buffer, &device->sprite->surface);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400472 device->hotspot_x = x;
473 device->hotspot_y = y;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400474
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500475 device->sprite->x = device->input_device.x - device->hotspot_x;
476 device->sprite->y = device->input_device.y - device->hotspot_y;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400477 device->sprite->width = buffer->width;
478 device->sprite->height = buffer->height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400479 wlsc_surface_update_matrix(device->sprite);
480
481 wlsc_compositor_schedule_repaint(ec);
482}
483
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400484
485static void
486wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
487 enum wlsc_pointer_type type)
488{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500489 struct wlsc_compositor *compositor =
490 (struct wlsc_compositor *) device->input_device.compositor;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400491
492 wlsc_input_device_attach(device,
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500493 &compositor->pointer_buffers[type]->buffer,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400494 pointer_images[type].hotspot_x,
495 pointer_images[type].hotspot_y);
496}
497
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400498static void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500499wl_input_device_end_grab(struct wl_input_device *device, uint32_t time);
Kristian Høgsberga9e89612010-12-06 21:32:15 -0500500
501static void
502lose_grab_surface(struct wl_listener *listener,
Kristian Høgsberg4685fa32010-12-06 21:38:50 -0500503 struct wl_surface *surface, uint32_t time)
Kristian Høgsberga9e89612010-12-06 21:32:15 -0500504{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500505 struct wl_input_device *device =
Kristian Høgsberga9e89612010-12-06 21:32:15 -0500506 container_of(listener,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500507 struct wl_input_device, grab_listener);
Kristian Høgsberga9e89612010-12-06 21:32:15 -0500508
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500509 wl_input_device_end_grab(device, time);
Kristian Høgsberga9e89612010-12-06 21:32:15 -0500510}
511
512static void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500513wl_input_device_start_grab(struct wl_input_device *device,
514 struct wl_grab *grab,
515 uint32_t button, uint32_t time)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400516{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500517 struct wl_surface *focus = device->pointer_focus;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400518
Kristian Høgsberg26437072010-12-01 10:17:47 -0500519 device->grab = grab;
Kristian Høgsbergea081152010-12-07 08:59:51 -0500520 device->grab_button = button;
521 device->grab_time = time;
522 device->grab_x = device->x;
523 device->grab_y = device->y;
Kristian Høgsberg26437072010-12-01 10:17:47 -0500524
Kristian Høgsberga9e89612010-12-06 21:32:15 -0500525 device->grab_listener.func = lose_grab_surface;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500526 wl_list_insert(focus->destroy_listener_list.prev,
Kristian Høgsberga9e89612010-12-06 21:32:15 -0500527 &device->grab_listener.link);
Kristian Høgsberg8321e692010-12-07 17:06:15 -0500528
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500529 grab->input_device = device;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400530}
531
Kristian Høgsbergea081152010-12-07 08:59:51 -0500532static int
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500533wl_input_device_update_grab(struct wl_input_device *device,
534 struct wl_grab *grab,
535 struct wl_surface *surface, uint32_t time)
Kristian Høgsbergea081152010-12-07 08:59:51 -0500536{
Kristian Høgsberg8321e692010-12-07 17:06:15 -0500537 if (device->grab != &device->motion_grab ||
Kristian Høgsbergea081152010-12-07 08:59:51 -0500538 device->grab_time != time ||
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500539 device->pointer_focus != surface)
Kristian Høgsbergea081152010-12-07 08:59:51 -0500540 return -1;
541
Kristian Høgsberg8321e692010-12-07 17:06:15 -0500542 device->grab = grab;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500543 grab->input_device = device;
Kristian Høgsbergea081152010-12-07 08:59:51 -0500544
545 return 0;
546}
547
Kristian Høgsberg6d65d5f2010-12-07 13:30:18 -0500548struct wlsc_move_grab {
549 struct wl_grab grab;
550 int32_t dx, dy;
551};
552
553static void
554move_grab_motion(struct wl_grab *grab,
555 uint32_t time, int32_t x, int32_t y)
556{
557 struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab;
Kristian Høgsberg6d65d5f2010-12-07 13:30:18 -0500558 struct wlsc_surface *es =
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500559 (struct wlsc_surface *) grab->input_device->pointer_focus;
560 struct wlsc_compositor *ec =
561 (struct wlsc_compositor *) grab->input_device->compositor;
Kristian Høgsberg6d65d5f2010-12-07 13:30:18 -0500562
563 es->x = x + move->dx;
564 es->y = y + move->dy;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500565 wl_client_post_event(es->surface.client, &ec->shell.object,
Kristian Høgsberg6d65d5f2010-12-07 13:30:18 -0500566 WL_SHELL_CONFIGURE,
567 time, WLSC_DEVICE_GRAB_MOVE,
568 &es->surface, es->x, es->y,
569 es->width, es->height);
570
571 wlsc_surface_update_matrix(es);
572}
573
574static void
Kristian Høgsberg287343a2010-12-07 14:58:57 -0500575move_grab_end(struct wl_grab *grab, uint32_t time)
Kristian Høgsberg6d65d5f2010-12-07 13:30:18 -0500576{
577 free(grab);
578}
579
580static const struct wl_grab_interface move_grab_interface = {
581 move_grab_motion,
582 move_grab_end
583};
584
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400585static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400586shell_move(struct wl_client *client, struct wl_shell *shell,
587 struct wl_surface *surface,
588 struct wl_input_device *device, uint32_t time)
589{
590 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg6d65d5f2010-12-07 13:30:18 -0500591 struct wlsc_surface *es = (struct wlsc_surface *) surface;
592 struct wlsc_move_grab *move;
593
594 move = malloc(sizeof *move);
595 if (!move) {
596 wl_client_post_no_memory(client);
597 return;
598 }
599
600 move->grab.interface = &move_grab_interface;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500601 move->dx = es->x - wd->input_device.grab_x;
602 move->dy = es->y - wd->input_device.grab_y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400603
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500604 if (wl_input_device_update_grab(&wd->input_device,
605 &move->grab, surface, time) < 0)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400606 return;
607
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400608 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400609}
610
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -0500611struct wlsc_resize_grab {
612 struct wl_grab grab;
613 uint32_t edges;
614 int32_t dx, dy, width, height;
615};
616
617static void
618resize_grab_motion(struct wl_grab *grab,
619 uint32_t time, int32_t x, int32_t y)
620{
621 struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500622 struct wl_input_device *device = grab->input_device;
623 struct wlsc_compositor *ec =
624 (struct wlsc_compositor *) device->compositor;
625 struct wl_surface *surface = device->pointer_focus;
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -0500626 int32_t sx, sy, width, height;
627
628 if (resize->edges & WLSC_DEVICE_GRAB_RESIZE_LEFT) {
629 sx = x + resize->dx;
630 width = device->grab_x - x + resize->width;
631 } else if (resize->edges & WLSC_DEVICE_GRAB_RESIZE_RIGHT) {
632 sx = device->grab_x + resize->dx;
633 width = x - device->grab_x + resize->width;
634 } else {
635 sx = device->grab_x + resize->dx;
636 width = resize->width;
637 }
638
639 if (resize->edges & WLSC_DEVICE_GRAB_RESIZE_TOP) {
640 sy = y + resize->dy;
641 height = device->grab_y - y + resize->height;
642 } else if (resize->edges & WLSC_DEVICE_GRAB_RESIZE_BOTTOM) {
643 sy = device->grab_y + resize->dy;
644 height = y - device->grab_y + resize->height;
645 } else {
646 sy = device->grab_y + resize->dy;
647 height = resize->height;
648 }
649
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500650 wl_client_post_event(surface->client, &ec->shell.object,
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -0500651 WL_SHELL_CONFIGURE, time, resize->edges,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500652 surface, sx, sy, width, height);
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -0500653}
654
655static void
Kristian Høgsberg287343a2010-12-07 14:58:57 -0500656resize_grab_end(struct wl_grab *grab, uint32_t time)
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -0500657{
658 free(grab);
659}
660
Kristian Høgsberg6d65d5f2010-12-07 13:30:18 -0500661static const struct wl_grab_interface resize_grab_interface = {
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -0500662 resize_grab_motion,
663 resize_grab_end
664};
665
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400666static void
667shell_resize(struct wl_client *client, struct wl_shell *shell,
668 struct wl_surface *surface,
669 struct wl_input_device *device, uint32_t time, uint32_t edges)
670{
671 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -0500672 struct wlsc_resize_grab *resize;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400673 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -0500674 struct wlsc_surface *es = (struct wlsc_surface *) surface;
675
676 resize = malloc(sizeof *resize);
677 if (!resize) {
678 wl_client_post_no_memory(client);
679 return;
680 }
681
682 resize->grab.interface = &resize_grab_interface;
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -0500683 resize->edges = edges;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500684 resize->dx = es->x - wd->input_device.grab_x;
685 resize->dy = es->y - wd->input_device.grab_y;
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -0500686 resize->width = es->width;
687 resize->height = es->height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400688
689 if (edges == 0 || edges > 15 ||
690 (edges & 3) == 3 || (edges & 12) == 12)
691 return;
692
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400693 switch (edges) {
694 case WLSC_DEVICE_GRAB_RESIZE_TOP:
695 pointer = WLSC_POINTER_TOP;
696 break;
697 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
698 pointer = WLSC_POINTER_BOTTOM;
699 break;
700 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
701 pointer = WLSC_POINTER_LEFT;
702 break;
703 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
704 pointer = WLSC_POINTER_TOP_LEFT;
705 break;
706 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
707 pointer = WLSC_POINTER_BOTTOM_LEFT;
708 break;
709 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
710 pointer = WLSC_POINTER_RIGHT;
711 break;
712 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
713 pointer = WLSC_POINTER_TOP_RIGHT;
714 break;
715 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
716 pointer = WLSC_POINTER_BOTTOM_RIGHT;
717 break;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400718 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400719
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500720 if (wl_input_device_update_grab(&wd->input_device,
721 &resize->grab, surface, time) < 0)
Kristian Høgsbergea081152010-12-07 08:59:51 -0500722 return;
723
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400724 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400725}
726
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500727static void
728wl_drag_set_pointer_focus(struct wl_drag *drag,
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500729 struct wl_surface *surface, uint32_t time,
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500730 int32_t x, int32_t y, int32_t sx, int32_t sy);
731
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400732static void
733destroy_drag(struct wl_resource *resource, struct wl_client *client)
734{
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500735 struct wl_drag *drag =
736 container_of(resource, struct wl_drag, resource);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400737
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500738 wl_list_remove(&drag->drag_focus_listener.link);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500739 if (drag->grab.input_device)
740 wl_input_device_end_grab(drag->grab.input_device, get_time());
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400741
742 free(drag);
743}
744
745const static struct wl_drag_interface drag_interface;
746
747static void
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500748drag_handle_surface_destroy(struct wl_listener *listener,
Kristian Høgsberg4685fa32010-12-06 21:38:50 -0500749 struct wl_surface *surface, uint32_t time)
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500750{
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500751 struct wl_drag *drag =
752 container_of(listener, struct wl_drag, drag_focus_listener);
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500753
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500754 if (drag->drag_focus == surface)
755 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500756}
757
758static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400759shell_create_drag(struct wl_client *client,
760 struct wl_shell *shell, uint32_t id)
761{
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500762 struct wl_drag *drag;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400763
764 drag = malloc(sizeof *drag);
765 if (drag == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400766 wl_client_post_no_memory(client);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400767 return;
768 }
769
770 memset(drag, 0, sizeof *drag);
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500771 drag->resource.object.id = id;
772 drag->resource.object.interface = &wl_drag_interface;
773 drag->resource.object.implementation =
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400774 (void (**)(void)) &drag_interface;
775
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500776 drag->resource.destroy = destroy_drag;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400777
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500778 drag->drag_focus_listener.func = drag_handle_surface_destroy;
779 wl_list_init(&drag->drag_focus_listener.link);
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500780
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500781 wl_client_add_resource(client, &drag->resource);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400782}
783
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400784const static struct wl_shell_interface shell_interface = {
785 shell_move,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400786 shell_resize,
787 shell_create_drag
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400788};
789
790static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500791compositor_create_surface(struct wl_client *client,
792 struct wl_compositor *compositor, uint32_t id)
793{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500794 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400795 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500796
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400797 surface = wlsc_surface_create(ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400798 if (surface == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400799 wl_client_post_no_memory(client);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500800 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400801 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500802
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400803 wl_list_insert(ec->surface_list.prev, &surface->link);
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500804 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400805
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500806 surface->surface.resource.object.id = id;
807 surface->surface.resource.object.interface = &wl_surface_interface;
808 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400809 (void (**)(void)) &surface_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500810 surface->surface.client = client;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400811
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500812 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500813}
814
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500815const static struct wl_compositor_interface compositor_interface = {
816 compositor_create_surface,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500817};
818
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500819static void
820wlsc_surface_transform(struct wlsc_surface *surface,
821 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
822{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400823 struct wlsc_vector v = { { x, y, 0, 1 } };
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500824
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400825 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400826 *sx = v.f[0] * surface->width;
827 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500828}
829
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500830static struct wlsc_surface *
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500831pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500832{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500833 struct wlsc_compositor *ec =
834 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500835 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500836
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400837 wl_list_for_each(es, &ec->surface_list, link) {
838 wlsc_surface_transform(es, device->x, device->y, sx, sy);
839 if (0 <= *sx && *sx < es->width &&
840 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500841 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400842 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500843
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500844 return NULL;
845}
846
Kristian Høgsberg8321e692010-12-07 17:06:15 -0500847
848static void
849motion_grab_motion(struct wl_grab *grab,
850 uint32_t time, int32_t x, int32_t y)
851{
852 struct wlsc_input_device *device =
853 (struct wlsc_input_device *) grab->input_device;
854 struct wlsc_surface *es =
855 (struct wlsc_surface *) device->input_device.pointer_focus;
856 int32_t sx, sy;
857
858 wlsc_surface_transform(es, x, y, &sx, &sy);
859 wl_client_post_event(es->surface.client,
860 &device->input_device.object,
861 WL_INPUT_DEVICE_MOTION,
862 time, x, y, sx, sy);
863}
864
865static void
866motion_grab_end(struct wl_grab *grab, uint32_t time)
867{
868}
869
870static const struct wl_grab_interface motion_grab_interface = {
871 motion_grab_motion,
872 motion_grab_end
873};
874
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500875void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500876notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500877{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500878 struct wlsc_surface *es;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500879 struct wlsc_compositor *ec =
880 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500881 struct wlsc_output *output;
Kristian Høgsberg6d65d5f2010-12-07 13:30:18 -0500882 const struct wl_grab_interface *interface;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500883 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -0500884 int32_t sx, sy;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500885
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500886 /* FIXME: We need some multi head love here. */
887 output = container_of(ec->output_list.next, struct wlsc_output, link);
888 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500889 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500890 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500891 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500892 if (x >= output->x + output->width)
893 x = output->x + output->width - 1;
894 if (y >= output->y + output->height)
895 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500896
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500897 device->x = x;
898 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500899
Kristian Høgsberg8321e692010-12-07 17:06:15 -0500900 if (device->grab) {
901 interface = device->grab->interface;
902 interface->motion(device->grab, time, x, y);
903 } else {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400904 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500905 wl_input_device_set_pointer_focus(device,
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500906 &es->surface,
Kristian Høgsberg26437072010-12-01 10:17:47 -0500907 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400908 if (es)
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500909 wl_client_post_event(es->surface.client,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500910 &device->object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400911 WL_INPUT_DEVICE_MOTION,
912 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400913 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500914
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500915 wd->sprite->x = device->x - wd->hotspot_x;
916 wd->sprite->y = device->y - wd->hotspot_y;
917 wlsc_surface_update_matrix(wd->sprite);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500918
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500919 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500920}
921
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400922static void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500923wl_input_device_end_grab(struct wl_input_device *device, uint32_t time)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400924{
925 struct wlsc_surface *es;
Kristian Høgsberg6d65d5f2010-12-07 13:30:18 -0500926 const struct wl_grab_interface *interface;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400927 int32_t sx, sy;
928
Kristian Høgsberg8321e692010-12-07 17:06:15 -0500929 interface = device->grab->interface;
930 interface->end(device->grab, time);
931 device->grab->input_device = NULL;
932 device->grab = NULL;
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -0500933
Kristian Høgsberga9e89612010-12-06 21:32:15 -0500934 wl_list_remove(&device->grab_listener.link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400935 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500936 wl_input_device_set_pointer_focus(device,
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500937 &es->surface, time,
938 device->x, device->y, sx, sy);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400939}
940
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500941void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500942notify_button(struct wl_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400943 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500944{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500945 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400946 struct wlsc_surface *surface;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500947 struct wlsc_compositor *compositor =
948 (struct wlsc_compositor *) device->compositor;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500949
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500950 surface = (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -0500951 if (!surface)
952 return;
Kristian Høgsbergea081152010-12-07 08:59:51 -0500953
Kristian Høgsberg8321e692010-12-07 17:06:15 -0500954 if (state && device->grab == NULL) {
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -0500955 wlsc_surface_raise(surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500956
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500957 wl_input_device_start_grab(device,
958 &device->motion_grab,
959 button, time);
960 wl_input_device_set_keyboard_focus(device,
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -0500961 &surface->surface,
962 time);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500963 }
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -0500964
965 if (state && button == BTN_LEFT &&
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500966 (wd->modifier_state & MODIFIER_SUPER))
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -0500967 shell_move(NULL,
968 (struct wl_shell *) &compositor->shell,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500969 &surface->surface, device, time);
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -0500970 else if (state && button == BTN_MIDDLE &&
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500971 (wd->modifier_state & MODIFIER_SUPER))
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -0500972 shell_resize(NULL,
973 (struct wl_shell *) &compositor->shell,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500974 &surface->surface, device, time,
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -0500975 WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT);
Kristian Høgsberg8321e692010-12-07 17:06:15 -0500976 else if (device->grab == NULL || device->grab == &device->motion_grab)
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -0500977 wl_client_post_event(surface->surface.client,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500978 &device->object,
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -0500979 WL_INPUT_DEVICE_BUTTON,
980 time, button, state);
981
Kristian Høgsberg8321e692010-12-07 17:06:15 -0500982 if (!state && device->grab && device->grab_button == button)
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500983 wl_input_device_end_grab(device, time);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500984}
985
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500986void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500987notify_key(struct wl_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400988 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500989{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500990 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
991 struct wlsc_compositor *compositor =
992 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500993 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400994 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500995
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500996 switch (key | wd->modifier_state) {
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400997 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500998 wl_display_terminate(compositor->wl_display);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500999 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001000 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001001
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001002 switch (key) {
1003 case KEY_LEFTCTRL:
1004 case KEY_RIGHTCTRL:
1005 modifier = MODIFIER_CTRL;
1006 break;
1007
1008 case KEY_LEFTALT:
1009 case KEY_RIGHTALT:
1010 modifier = MODIFIER_ALT;
1011 break;
1012
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -04001013 case KEY_LEFTMETA:
1014 case KEY_RIGHTMETA:
1015 modifier = MODIFIER_SUPER;
1016 break;
1017
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001018 default:
1019 modifier = 0;
1020 break;
1021 }
1022
1023 if (state)
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001024 wd->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001025 else
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001026 wd->modifier_state &= ~modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001027
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001028 end = device->keys.data + device->keys.size;
1029 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001030 if (*k == key)
1031 *k = *--end;
1032 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001033 device->keys.size = (void *) end - device->keys.data;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001034 if (state) {
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001035 k = wl_array_add(&device->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001036 *k = key;
1037 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001038
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001039 if (device->keyboard_focus != NULL)
1040 wl_client_post_event(device->keyboard_focus->client,
1041 &device->object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001042 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001043}
1044
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001045static void
1046input_device_attach(struct wl_client *client,
1047 struct wl_input_device *device_base,
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001048 uint32_t time,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001049 struct wl_buffer *buffer, int32_t x, int32_t y)
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001050{
1051 struct wlsc_input_device *device =
1052 (struct wlsc_input_device *) device_base;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001053
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001054 if (time < device->input_device.pointer_focus_time)
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001055 return;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001056 if (device->input_device.pointer_focus == NULL)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001057 return;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001058
Kristian Høgsberg4be2ed92010-12-07 09:15:57 -05001059 if (device->input_device.pointer_focus->client != client)
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001060 return;
1061
Kristian Høgsbergf4cb2012010-08-16 17:46:25 -04001062 if (buffer == NULL) {
1063 wlsc_input_device_set_pointer_image(device,
1064 WLSC_POINTER_LEFT_PTR);
1065 return;
1066 }
1067
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001068 wlsc_input_device_attach(device, buffer, x, y);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001069}
1070
1071const static struct wl_input_device_interface input_device_interface = {
1072 input_device_attach,
1073};
1074
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001075
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001076static void
1077wl_drag_set_pointer_focus(struct wl_drag *drag,
Kristian Høgsberg3d76e652010-12-06 17:33:11 -05001078 struct wl_surface *surface, uint32_t time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001079 int32_t x, int32_t y, int32_t sx, int32_t sy)
1080{
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001081 char **p, **end;
1082
Kristian Høgsberg3d76e652010-12-06 17:33:11 -05001083 if (drag->drag_focus == surface)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001084 return;
1085
Kristian Høgsberg3d76e652010-12-06 17:33:11 -05001086 if (drag->drag_focus &&
1087 (!surface || drag->drag_focus->client != surface->client))
1088 wl_client_post_event(drag->drag_focus->client,
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001089 &drag->drag_offer.object,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001090 WL_DRAG_OFFER_POINTER_FOCUS,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001091 time, NULL, 0, 0, 0, 0);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001092
1093 if (surface &&
Kristian Høgsberg3d76e652010-12-06 17:33:11 -05001094 (!drag->drag_focus ||
1095 drag->drag_focus->client != surface->client)) {
1096 wl_client_post_global(surface->client,
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001097 &drag->drag_offer.object);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001098
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001099 end = drag->types.data + drag->types.size;
1100 for (p = drag->types.data; p < end; p++)
Kristian Høgsberg3d76e652010-12-06 17:33:11 -05001101 wl_client_post_event(surface->client,
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001102 &drag->drag_offer.object,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001103 WL_DRAG_OFFER_OFFER, *p);
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001104 }
1105
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001106 if (surface) {
Kristian Høgsberg3d76e652010-12-06 17:33:11 -05001107 wl_client_post_event(surface->client,
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001108 &drag->drag_offer.object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001109 WL_DRAG_OFFER_POINTER_FOCUS,
Kristian Høgsberg3d76e652010-12-06 17:33:11 -05001110 time, surface,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001111 x, y, sx, sy);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001112
1113 }
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001114
Kristian Høgsberg3d76e652010-12-06 17:33:11 -05001115 drag->drag_focus = surface;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001116 drag->pointer_focus_time = time;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001117 drag->target = NULL;
Kristian Høgsberg3d76e652010-12-06 17:33:11 -05001118
1119 wl_list_remove(&drag->drag_focus_listener.link);
1120 if (surface)
1121 wl_list_insert(surface->destroy_listener_list.prev,
1122 &drag->drag_focus_listener.link);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001123}
1124
1125static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001126drag_offer_accept(struct wl_client *client,
1127 struct wl_drag_offer *offer, uint32_t time, const char *type)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001128{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001129 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001130 char **p, **end;
1131
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001132 /* If the client responds to drag pointer_focus or motion
1133 * events after the pointer has left the surface, we just
1134 * discard the accept requests. The drag source just won't
1135 * get the corresponding 'target' events and eventually the
1136 * next surface/root will start sending events. */
1137 if (time < drag->pointer_focus_time)
1138 return;
1139
1140 drag->target = client;
1141 drag->type = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001142 end = drag->types.data + drag->types.size;
1143 for (p = drag->types.data; p < end; p++)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001144 if (type && strcmp(*p, type) == 0)
1145 drag->type = *p;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001146
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001147 wl_client_post_event(drag->source->client, &drag->resource.object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001148 WL_DRAG_TARGET, drag->type);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001149}
1150
1151static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001152drag_offer_receive(struct wl_client *client,
1153 struct wl_drag_offer *offer, int fd)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001154{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001155 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001156
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001157 wl_client_post_event(drag->source->client, &drag->resource.object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001158 WL_DRAG_FINISH, fd);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001159 close(fd);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001160}
1161
Kristian Høgsbergd44bc8b2010-11-30 15:10:26 -05001162static void
1163drag_offer_reject(struct wl_client *client, struct wl_drag_offer *offer)
1164{
1165 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
1166
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001167 wl_client_post_event(drag->source->client, &drag->resource.object,
Kristian Høgsbergd44bc8b2010-11-30 15:10:26 -05001168 WL_DRAG_REJECT);
1169}
1170
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001171static const struct wl_drag_offer_interface drag_offer_interface = {
1172 drag_offer_accept,
Kristian Høgsbergd44bc8b2010-11-30 15:10:26 -05001173 drag_offer_receive,
1174 drag_offer_reject
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001175};
1176
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001177static void
1178drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
1179{
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001180 char **p;
1181
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001182 p = wl_array_add(&drag->types, sizeof *p);
1183 if (p)
1184 *p = strdup(type);
1185 if (!p || !*p)
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001186 wl_client_post_no_memory(client);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001187}
1188
1189static void
Kristian Høgsberg287343a2010-12-07 14:58:57 -05001190drag_grab_motion(struct wl_grab *grab,
1191 uint32_t time, int32_t x, int32_t y)
1192{
1193 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
Kristian Høgsberg287343a2010-12-07 14:58:57 -05001194 struct wlsc_surface *es;
1195 int32_t sx, sy;
1196
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001197 es = pick_surface(grab->input_device, &sx, &sy);
Kristian Høgsberg287343a2010-12-07 14:58:57 -05001198 wl_drag_set_pointer_focus(drag, &es->surface, time, x, y, sx, sy);
1199 if (es)
1200 wl_client_post_event(es->surface.client,
1201 &drag->drag_offer.object,
1202 WL_DRAG_OFFER_MOTION,
1203 time, x, y, sx, sy);
1204}
1205
1206static void
1207drag_grab_end(struct wl_grab *grab, uint32_t time)
1208{
1209 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
1210
1211 if (drag->target)
1212 wl_client_post_event(drag->target,
1213 &drag->drag_offer.object,
1214 WL_DRAG_OFFER_DROP);
1215
1216 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
1217}
1218
1219static const struct wl_grab_interface drag_grab_interface = {
1220 drag_grab_motion,
1221 drag_grab_end
1222};
1223
1224static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001225drag_activate(struct wl_client *client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001226 struct wl_drag *drag,
1227 struct wl_surface *surface,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001228 struct wl_input_device *device, uint32_t time)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001229{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001230 struct wl_display *display = wl_client_get_display (client);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001231 struct wlsc_surface *target;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001232 int32_t sx, sy;
1233
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001234 if (wl_input_device_update_grab(device,
1235 &drag->grab, surface, time) < 0)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001236 return;
1237
Kristian Høgsberg287343a2010-12-07 14:58:57 -05001238 drag->grab.interface = &drag_grab_interface;
Kristian Høgsberg287343a2010-12-07 14:58:57 -05001239
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001240 drag->source = surface;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001241
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001242 drag->drag_offer.object.interface = &wl_drag_offer_interface;
1243 drag->drag_offer.object.implementation =
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001244 (void (**)(void)) &drag_offer_interface;
1245
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001246 wl_display_add_object(display, &drag->drag_offer.object);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001247
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001248 target = pick_surface(device, &sx, &sy);
Kristian Høgsberg287343a2010-12-07 14:58:57 -05001249 wl_drag_set_pointer_focus(drag, &target->surface, time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001250 device->x, device->y, sx, sy);
1251}
1252
1253static void
Kristian Høgsberg287343a2010-12-07 14:58:57 -05001254drag_destroy(struct wl_client *client, struct wl_drag *drag)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001255{
Kristian Høgsberg287343a2010-12-07 14:58:57 -05001256 wl_resource_destroy(&drag->resource, client);
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001257}
1258
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001259static const struct wl_drag_interface drag_interface = {
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001260 drag_offer,
1261 drag_activate,
Kristian Høgsberg287343a2010-12-07 14:58:57 -05001262 drag_destroy,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001263};
1264
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001265void
1266wlsc_input_device_init(struct wlsc_input_device *device,
1267 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001268{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001269 wl_input_device_init(&device->input_device, &ec->compositor);
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05001270
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001271 device->input_device.object.interface = &wl_input_device_interface;
1272 device->input_device.object.implementation =
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001273 (void (**)(void)) &input_device_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001274 wl_display_add_object(ec->wl_display, &device->input_device.object);
1275 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001276
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001277 device->sprite = wlsc_surface_create(ec, &ec->argb_visual,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001278 device->input_device.x,
1279 device->input_device.y, 32, 32);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001280 device->hotspot_x = 16;
1281 device->hotspot_y = 16;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001282
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001283 device->input_device.motion_grab.interface = &motion_grab_interface;
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001284
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001285 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001286
1287 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001288}
1289
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001290static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001291wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001292{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001293 struct wlsc_output *output =
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001294 container_of(global, struct wlsc_output, object);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001295
1296 wl_client_post_event(client, global,
1297 WL_OUTPUT_GEOMETRY,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001298 output->x, output->y,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001299 output->width, output->height);
1300}
1301
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001302static const char vertex_shader[] =
1303 "uniform mat4 proj;\n"
1304 "attribute vec4 position;\n"
1305 "attribute vec2 texcoord;\n"
1306 "varying vec2 v_texcoord;\n"
1307 "void main()\n"
1308 "{\n"
1309 " gl_Position = proj * position;\n"
1310 " v_texcoord = texcoord;\n"
1311 "}\n";
1312
1313static const char fragment_shader[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05001314 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001315 "varying vec2 v_texcoord;\n"
1316 "uniform sampler2D tex;\n"
1317 "void main()\n"
1318 "{\n"
1319 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1320 "}\n";
1321
Kristian Høgsberga9468212010-06-14 21:03:11 -04001322static int
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001323init_shaders(struct wlsc_compositor *ec)
1324{
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001325 GLuint v, f, program;
1326 const char *p;
1327 char msg[512];
1328 GLfloat vertices[4 * 5];
1329 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001330
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001331 p = vertex_shader;
1332 v = glCreateShader(GL_VERTEX_SHADER);
1333 glShaderSource(v, 1, &p, NULL);
1334 glCompileShader(v);
1335 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
1336 if (!status) {
1337 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
1338 fprintf(stderr, "vertex shader info: %s\n", msg);
1339 return -1;
1340 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001341
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001342 p = fragment_shader;
1343 f = glCreateShader(GL_FRAGMENT_SHADER);
1344 glShaderSource(f, 1, &p, NULL);
1345 glCompileShader(f);
1346 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
1347 if (!status) {
1348 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
1349 fprintf(stderr, "fragment shader info: %s\n", msg);
1350 return -1;
1351 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001352
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001353 program = glCreateProgram();
1354 glAttachShader(program, v);
1355 glAttachShader(program, f);
1356 glBindAttribLocation(program, 0, "position");
1357 glBindAttribLocation(program, 1, "texcoord");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001358
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001359 glLinkProgram(program);
1360 glGetProgramiv(program, GL_LINK_STATUS, &status);
1361 if (!status) {
1362 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1363 fprintf(stderr, "link info: %s\n", msg);
1364 return -1;
1365 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001366
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001367 glUseProgram(program);
1368 ec->proj_uniform = glGetUniformLocation(program, "proj");
1369 ec->tex_uniform = glGetUniformLocation(program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001370
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001371 vertices[ 0] = 0.0;
1372 vertices[ 1] = 0.0;
1373 vertices[ 2] = 0.0;
1374 vertices[ 3] = 0.0;
1375 vertices[ 4] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001376
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001377 vertices[ 5] = 0.0;
1378 vertices[ 6] = 1.0;
1379 vertices[ 7] = 0.0;
1380 vertices[ 8] = 0.0;
1381 vertices[ 9] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001382
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001383 vertices[10] = 1.0;
1384 vertices[11] = 0.0;
1385 vertices[12] = 0.0;
1386 vertices[13] = 1.0;
1387 vertices[14] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001388
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001389 vertices[15] = 1.0;
1390 vertices[16] = 1.0;
1391 vertices[17] = 0.0;
1392 vertices[18] = 1.0;
1393 vertices[19] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001394
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001395 glGenBuffers(1, &ec->vbo);
1396 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
1397 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001398
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001399 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001400}
1401
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001402static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001403add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001404{
Kristian Høgsbergaa827672010-12-01 20:06:39 -05001405 ec->argb_visual.object.interface = &wl_visual_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001406 ec->argb_visual.object.implementation = NULL;
1407 wl_display_add_object(ec->wl_display, &ec->argb_visual.object);
1408 wl_display_add_global(ec->wl_display, &ec->argb_visual.object, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001409
Kristian Høgsbergaa827672010-12-01 20:06:39 -05001410 ec->premultiplied_argb_visual.object.interface = &wl_visual_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001411 ec->premultiplied_argb_visual.object.implementation = NULL;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001412 wl_display_add_object(ec->wl_display,
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001413 &ec->premultiplied_argb_visual.object);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001414 wl_display_add_global(ec->wl_display,
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001415 &ec->premultiplied_argb_visual.object, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001416
Kristian Høgsbergaa827672010-12-01 20:06:39 -05001417 ec->rgb_visual.object.interface = &wl_visual_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001418 ec->rgb_visual.object.implementation = NULL;
1419 wl_display_add_object(ec->wl_display, &ec->rgb_visual.object);
1420 wl_display_add_global(ec->wl_display, &ec->rgb_visual.object, NULL);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001421}
1422
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001423void
1424wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1425 int x, int y, int width, int height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001426{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001427 output->compositor = c;
1428 output->x = x;
1429 output->y = y;
1430 output->width = width;
1431 output->height = height;
1432
1433 output->background =
1434 background_create(output, option_background);
1435
1436 wlsc_matrix_init(&output->matrix);
1437 wlsc_matrix_translate(&output->matrix,
1438 -output->x - output->width / 2.0,
1439 -output->y - output->height / 2.0, 0);
1440 wlsc_matrix_scale(&output->matrix,
1441 2.0 / output->width, 2.0 / output->height, 1);
1442
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001443 output->object.interface = &wl_output_interface;
1444 wl_display_add_object(c->wl_display, &output->object);
1445 wl_display_add_global(c->wl_display, &output->object,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001446 wlsc_output_post_geometry);
1447}
1448
1449int
1450wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1451{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001452 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001453
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001454 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001455
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001456 ec->compositor.object.interface = &wl_compositor_interface;
1457 ec->compositor.object.implementation =
Kristian Høgsbergf8ffded2010-09-03 15:15:33 -04001458 (void (**)(void)) &compositor_interface;
1459
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001460 wl_display_add_object(display, &ec->compositor.object);
1461 if (wl_display_add_global(display, &ec->compositor.object, NULL))
Kristian Høgsbergf8ffded2010-09-03 15:15:33 -04001462 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001463
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001464 wlsc_shm_init(ec);
1465
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001466 ec->shell.object.interface = &wl_shell_interface;
1467 ec->shell.object.implementation = (void (**)(void)) &shell_interface;
1468 wl_display_add_object(display, &ec->shell.object);
1469 if (wl_display_add_global(display, &ec->shell.object, NULL))
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001470 return -1;
1471
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001472 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001473
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001474 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001475 wl_list_init(&ec->input_device_list);
1476 wl_list_init(&ec->output_list);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001477
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001478 create_pointer_images(ec);
1479
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001480 screenshooter_create(ec);
1481
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001482 glGenFramebuffers(1, &ec->fbo);
1483 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1484 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001485 if (init_shaders(ec) < 0)
1486 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001487
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001488 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001489 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001490 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001491
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001492 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001493}
1494
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001495static void on_term_signal(int signal_number, void *data)
1496{
1497 struct wlsc_compositor *ec = data;
1498
1499 wl_display_terminate(ec->wl_display);
1500}
1501
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001502int main(int argc, char *argv[])
1503{
1504 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001505 struct wlsc_compositor *ec;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001506 struct wl_event_loop *loop;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001507 GError *error = NULL;
1508 GOptionContext *context;
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001509 int width, height;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001510
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001511 g_type_init(); /* GdkPixbuf needs this, it seems. */
1512
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001513 context = g_option_context_new(NULL);
1514 g_option_context_add_main_entries(context, option_entries, "Wayland");
1515 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1516 fprintf(stderr, "option parsing failed: %s\n", error->message);
1517 exit(EXIT_FAILURE);
1518 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001519 if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) {
1520 fprintf(stderr, "invalid geometry option: %s \n",
1521 option_geometry);
1522 exit(EXIT_FAILURE);
1523 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001524
1525 display = wl_display_create();
1526
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001527 if (getenv("WAYLAND_DISPLAY"))
1528 ec = wayland_compositor_create(display, width, height);
1529 else if (getenv("DISPLAY"))
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001530 ec = x11_compositor_create(display, width, height);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001531 else
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001532 ec = drm_compositor_create(display, option_connector);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001533
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001534 if (ec == NULL) {
1535 fprintf(stderr, "failed to create compositor\n");
1536 exit(EXIT_FAILURE);
1537 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001538
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001539 if (wl_display_add_socket(display, option_socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001540 fprintf(stderr, "failed to add socket: %m\n");
1541 exit(EXIT_FAILURE);
1542 }
1543
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001544 loop = wl_display_get_event_loop(ec->wl_display);
1545 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
1546 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
1547
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001548 wl_display_run(display);
1549
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001550 wl_display_destroy(display);
1551
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001552 ec->destroy(ec);
1553
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001554 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001555}