blob: 980e97fe608ef42a57a48f4779508e07a5c2fad7 [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øgsberg27803c62010-06-06 22:23:21 -040088 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050089 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
90 };
91
92 wlsc_matrix_multiply(matrix, &translate);
93}
94
95static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040096wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050097{
98 struct wlsc_matrix scale = {
99 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
100 };
101
102 wlsc_matrix_multiply(matrix, &scale);
103}
104
105static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400106wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
107{
108 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400109 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400110
111 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400112 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400113 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400114 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400115 }
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400116
117 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400118}
119
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400120static struct wlsc_surface *
121wlsc_surface_create(struct wlsc_compositor *compositor,
122 struct wl_visual *visual,
123 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500124{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400125 struct wlsc_surface *surface;
126
127 surface = malloc(sizeof *surface);
128 if (surface == NULL)
129 return NULL;
130
Kristian Hø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
157static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400158destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500159{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400160 struct wlsc_surface *surface =
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500161 container_of(resource, struct wlsc_surface, surface.resource);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400162 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500163 struct wl_listener *l, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400164
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400165 wl_list_remove(&surface->link);
166 glDeleteTextures(1, &surface->texture);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400167
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500168 wl_list_for_each_safe(l, next,
169 &surface->surface.destroy_listener_list, link)
170 l->func(l, &surface->surface);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400171
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400172 free(surface);
Kristian Høgsberg117d5132010-08-11 08:56:47 -0400173
174 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500175}
176
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400177static int
178texture_from_png(const char *filename, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500179{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400180 GdkPixbuf *pixbuf;
181 GError *error = NULL;
182 void *data;
183 GLenum format;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500184
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400185 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
186 width, height,
187 FALSE, &error);
188 if (error != NULL)
189 return -1;
190
191 data = gdk_pixbuf_get_pixels(pixbuf);
192
193 if (gdk_pixbuf_get_has_alpha(pixbuf))
194 format = GL_RGBA;
195 else
196 format = GL_RGB;
197
Chia-I Wu1f411902010-10-29 15:20:16 +0800198 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
199 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400200
201 gdk_pixbuf_unref(pixbuf);
202
203 return 0;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500204}
205
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400206static const struct {
207 const char *filename;
208 int hotspot_x, hotspot_y;
209} pointer_images[] = {
Kristian Høgsberg4219a402010-08-16 16:43:03 -0400210 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
211 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
212 { DATADIR "/wayland/bottom_side.png", 16, 20 },
213 { DATADIR "/wayland/grabbing.png", 20, 17 },
214 { DATADIR "/wayland/left_ptr.png", 10, 5 },
215 { DATADIR "/wayland/left_side.png", 10, 20 },
216 { DATADIR "/wayland/right_side.png", 30, 19 },
217 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
218 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
219 { DATADIR "/wayland/top_side.png", 18, 8 },
220 { DATADIR "/wayland/xterm.png", 15, 15 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400221};
222
223static void
224create_pointer_images(struct wlsc_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500225{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400226 int i, count;
227 GLuint texture;
228 const int width = 32, height = 32;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400229
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400230 glGenTextures(1, &texture);
231 glBindTexture(GL_TEXTURE_2D, texture);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
235 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400236
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400237 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400238 ec->pointer_buffers = malloc(count * sizeof *ec->pointer_buffers);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400239 for (i = 0; i < count; i++) {
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400240 ec->pointer_buffers[i] =
241 wlsc_drm_buffer_create(ec, width, height,
242 &ec->argb_visual);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400243 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400244 ec->pointer_buffers[i]->image);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400245 texture_from_png(pointer_images[i].filename, width, height);
246 }
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400247}
248
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500249static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500250background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500251{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500252 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500253 GdkPixbuf *pixbuf;
254 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500255 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500256 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500257
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400258 background = wlsc_surface_create(output->compositor,
259 &output->compositor->rgb_visual,
260 output->x, output->y,
261 output->width, output->height);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500262 if (background == NULL)
263 return NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500264
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500265 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500266 output->width,
267 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500268 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500269 if (error != NULL) {
270 free(background);
271 return NULL;
272 }
273
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500274 data = gdk_pixbuf_get_pixels(pixbuf);
275
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400276 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500277 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500278 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500279 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500280
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500281 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
282 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500283 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500284
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500285 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500286}
287
288static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400289wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500290{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500291 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400292 struct wlsc_matrix tmp;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500293
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400294 tmp = es->matrix;
295 wlsc_matrix_multiply(&tmp, &output->matrix);
296 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
297 glUniform1i(ec->tex_uniform, 0);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500298
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500299 if (es->visual == &ec->argb_visual) {
300 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
301 glEnable(GL_BLEND);
302 } else if (es->visual == &ec->premultiplied_argb_visual) {
303 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
304 glEnable(GL_BLEND);
305 } else {
306 glDisable(GL_BLEND);
307 }
308
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500309 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400310 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
311 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
312 5 * sizeof(GLfloat), NULL);
313 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
314 5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
315 glEnableVertexAttribArray(0);
316 glEnableVertexAttribArray(1);
317 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500318}
319
320static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400321wlsc_surface_raise(struct wlsc_surface *surface)
322{
323 struct wlsc_compositor *compositor = surface->compositor;
324
325 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400326 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400327}
328
329static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400330wlsc_surface_update_matrix(struct wlsc_surface *es)
331{
332 wlsc_matrix_init(&es->matrix);
333 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
334 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
335
336 wlsc_matrix_init(&es->matrix_inv);
337 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
338 wlsc_matrix_scale(&es->matrix_inv,
339 1.0 / es->width, 1.0 / es->height, 1);
340}
341
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400342void
343wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400344{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400345 wl_display_post_frame(compositor->wl_display, msecs);
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400346 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400347}
348
349static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400350wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400351{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500352 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500353 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500354 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500355
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500356 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500357
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500358 if (output->background)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400359 wlsc_surface_draw(output->background, output);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500360 else
361 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400362
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400363 wl_list_for_each_reverse(es, &ec->surface_list, link)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400364 wlsc_surface_draw(es, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400365
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400366 if (ec->focus)
367 wl_list_for_each(eid, &ec->input_device_list, link)
368 wlsc_surface_draw(eid->sprite, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500369}
370
371static void
372repaint(void *data)
373{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500374 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500375 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500376
377 if (!ec->repaint_needed) {
378 ec->repaint_on_timeout = 0;
379 return;
380 }
381
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500382 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400383 wlsc_output_repaint(output);
384
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400385 ec->present(ec);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500386
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500387 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400388}
389
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400390void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400391wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400392{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400393 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400394 if (compositor->repaint_on_timeout)
395 return;
396
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400397 wl_event_source_timer_update(compositor->timer_source, 1);
398 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400399}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500400
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500401static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500402surface_destroy(struct wl_client *client,
403 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400404{
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500405 wl_resource_destroy(&surface->resource, client);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400406}
407
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500408static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500409surface_attach(struct wl_client *client,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400410 struct wl_surface *surface, struct wl_buffer *buffer)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400411{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500412 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400413
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400414 buffer->attach(buffer, surface);
415 es->buffer = buffer;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400416}
417
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500418static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500419surface_map(struct wl_client *client,
420 struct wl_surface *surface,
421 int32_t x, int32_t y, int32_t width, int32_t height)
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øgsberg16eb6752008-10-08 22:51:32 -0400424
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400425 es->x = x;
426 es->y = y;
427 es->width = width;
428 es->height = height;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400429
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400430 wlsc_surface_update_matrix(es);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400431 wlsc_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400432}
433
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500434static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500435surface_damage(struct wl_client *client,
436 struct wl_surface *surface,
437 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500438{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400439 struct wlsc_surface *es = (struct wlsc_surface *) surface;
440
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400441 es->buffer->damage(es->buffer, surface, x, y, width, height);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400442 wlsc_compositor_schedule_repaint(es->compositor);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500443}
444
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500445const static struct wl_surface_interface surface_interface = {
446 surface_destroy,
447 surface_attach,
448 surface_map,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500449 surface_damage
450};
451
452static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400453wlsc_input_device_attach(struct wlsc_input_device *device,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400454 struct wl_buffer *buffer, int x, int y)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400455{
456 struct wlsc_compositor *ec = device->ec;
457
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500458 buffer->attach(buffer, &device->sprite->surface);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400459 device->hotspot_x = x;
460 device->hotspot_y = y;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400461
462 device->sprite->x = device->x - device->hotspot_x;
463 device->sprite->y = device->y - device->hotspot_y;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400464 device->sprite->width = buffer->width;
465 device->sprite->height = buffer->height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400466 wlsc_surface_update_matrix(device->sprite);
467
468 wlsc_compositor_schedule_repaint(ec);
469}
470
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400471
472static void
473wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
474 enum wlsc_pointer_type type)
475{
476 struct wlsc_compositor *compositor = device->ec;
477
478 wlsc_input_device_attach(device,
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500479 &compositor->pointer_buffers[type]->buffer,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400480 pointer_images[type].hotspot_x,
481 pointer_images[type].hotspot_y);
482}
483
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400484static void
485wlsc_input_device_start_grab(struct wlsc_input_device *device,
486 uint32_t time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400487 enum wlsc_grab_type grab)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400488{
Kristian Høgsberg26437072010-12-01 10:17:47 -0500489 struct wlsc_surface *focus =
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500490 (struct wlsc_surface *) device->input_device.pointer_focus;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400491
Kristian Høgsberg26437072010-12-01 10:17:47 -0500492 device->grab = grab;
493 device->grab_surface = focus;
494 device->grab_dx = focus->x - device->grab_x;
495 device->grab_dy = focus->y - device->grab_y;
496 device->grab_width = focus->width;
497 device->grab_height = focus->height;
498
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500499 wl_input_device_set_pointer_focus(&device->input_device,
Kristian Høgsberg26437072010-12-01 10:17:47 -0500500 &wl_grab_surface, time, 0, 0, 0, 0);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400501}
502
503static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400504shell_move(struct wl_client *client, struct wl_shell *shell,
505 struct wl_surface *surface,
506 struct wl_input_device *device, uint32_t time)
507{
508 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
509
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400510 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
511 wd->grab_time != time ||
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500512 wd->input_device.pointer_focus != surface)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400513 return;
514
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400515 wlsc_input_device_start_grab(wd, time, WLSC_DEVICE_GRAB_MOVE);
516 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400517}
518
519static void
520shell_resize(struct wl_client *client, struct wl_shell *shell,
521 struct wl_surface *surface,
522 struct wl_input_device *device, uint32_t time, uint32_t edges)
523{
524 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400525 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400526
527 if (edges == 0 || edges > 15 ||
528 (edges & 3) == 3 || (edges & 12) == 12)
529 return;
530
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400531 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
532 wd->grab_time != time ||
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500533 wd->input_device.pointer_focus != surface)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400534 return;
535
536 switch (edges) {
537 case WLSC_DEVICE_GRAB_RESIZE_TOP:
538 pointer = WLSC_POINTER_TOP;
539 break;
540 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
541 pointer = WLSC_POINTER_BOTTOM;
542 break;
543 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
544 pointer = WLSC_POINTER_LEFT;
545 break;
546 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
547 pointer = WLSC_POINTER_TOP_LEFT;
548 break;
549 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
550 pointer = WLSC_POINTER_BOTTOM_LEFT;
551 break;
552 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
553 pointer = WLSC_POINTER_RIGHT;
554 break;
555 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
556 pointer = WLSC_POINTER_TOP_RIGHT;
557 break;
558 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
559 pointer = WLSC_POINTER_BOTTOM_RIGHT;
560 break;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400561 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400562
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400563 wlsc_input_device_start_grab(wd, time, edges);
564 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400565}
566
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500567static void
568wl_drag_set_pointer_focus(struct wl_drag *drag,
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500569 struct wl_surface *surface, uint32_t time,
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500570 int32_t x, int32_t y, int32_t sx, int32_t sy);
571
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400572static void
573destroy_drag(struct wl_resource *resource, struct wl_client *client)
574{
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500575 struct wl_drag *drag =
576 container_of(resource, struct wl_drag, resource);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400577
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500578 wl_list_remove(&drag->drag_focus_listener.link);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400579
580 free(drag);
581}
582
583const static struct wl_drag_interface drag_interface;
584
585static void
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500586drag_handle_surface_destroy(struct wl_listener *listener,
587 struct wl_surface *surface)
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500588{
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500589 struct wl_drag *drag =
590 container_of(listener, struct wl_drag, drag_focus_listener);
Kristian Høgsbergab8475c2010-12-06 16:56:28 -0500591 uint32_t time =
592 wl_display_get_time(wl_client_get_display(surface->client));
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500593
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500594 if (drag->drag_focus == surface)
595 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500596}
597
598static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400599shell_create_drag(struct wl_client *client,
600 struct wl_shell *shell, uint32_t id)
601{
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500602 struct wl_drag *drag;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400603
604 drag = malloc(sizeof *drag);
605 if (drag == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400606 wl_client_post_no_memory(client);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400607 return;
608 }
609
610 memset(drag, 0, sizeof *drag);
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500611 drag->resource.object.id = id;
612 drag->resource.object.interface = &wl_drag_interface;
613 drag->resource.object.implementation =
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400614 (void (**)(void)) &drag_interface;
615
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500616 drag->resource.destroy = destroy_drag;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400617
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500618 drag->drag_focus_listener.func = drag_handle_surface_destroy;
619 wl_list_init(&drag->drag_focus_listener.link);
Kristian Høgsberg3d465342010-11-22 13:58:46 -0500620
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500621 wl_client_add_resource(client, &drag->resource);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400622}
623
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400624const static struct wl_shell_interface shell_interface = {
625 shell_move,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400626 shell_resize,
627 shell_create_drag
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400628};
629
630static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500631compositor_create_surface(struct wl_client *client,
632 struct wl_compositor *compositor, uint32_t id)
633{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500634 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400635 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500636
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400637 surface = wlsc_surface_create(ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400638 if (surface == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400639 wl_client_post_no_memory(client);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500640 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400641 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500642
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400643 wl_list_insert(ec->surface_list.prev, &surface->link);
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500644 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400645
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500646 surface->surface.resource.object.id = id;
647 surface->surface.resource.object.interface = &wl_surface_interface;
648 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400649 (void (**)(void)) &surface_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500650 surface->surface.client = client;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400651
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500652 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500653}
654
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500655const static struct wl_compositor_interface compositor_interface = {
656 compositor_create_surface,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500657};
658
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500659static void
660wlsc_surface_transform(struct wlsc_surface *surface,
661 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
662{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400663 struct wlsc_vector v = { { x, y, 0, 1 } };
664
665 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400666 *sx = v.f[0] * surface->width;
667 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500668}
669
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500670static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500671pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500672{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500673 struct wlsc_compositor *ec = device->ec;
674 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500675
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400676 wl_list_for_each(es, &ec->surface_list, link) {
677 wlsc_surface_transform(es, device->x, device->y, sx, sy);
678 if (0 <= *sx && *sx < es->width &&
679 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500680 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400681 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500682
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500683 return NULL;
684}
685
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500686void
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400687notify_motion(struct wlsc_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500688{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500689 struct wlsc_surface *es;
690 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500691 struct wlsc_output *output;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400692 int32_t sx, sy, width, height;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500693
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500694 /* FIXME: We need some multi head love here. */
695 output = container_of(ec->output_list.next, struct wlsc_output, link);
696 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500697 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500698 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500699 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500700 if (x >= output->x + output->width)
701 x = output->x + output->width - 1;
702 if (y >= output->y + output->height)
703 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500704
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500705 device->x = x;
706 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500707
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400708 switch (device->grab) {
709 case WLSC_DEVICE_GRAB_NONE:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400710 es = pick_surface(device, &sx, &sy);
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500711 wl_input_device_set_pointer_focus(&device->input_device,
712 &es->surface,
Kristian Høgsberg26437072010-12-01 10:17:47 -0500713 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400714 if (es)
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500715 wl_client_post_event(es->surface.client,
716 &device->input_device.object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400717 WL_INPUT_DEVICE_MOTION,
718 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400719 break;
720
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400721 case WLSC_DEVICE_GRAB_MOTION:
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500722 es = (struct wlsc_surface *) device->input_device.pointer_focus;
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400723 wlsc_surface_transform(es, x, y, &sx, &sy);
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500724 wl_client_post_event(es->surface.client,
725 &device->input_device.object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400726 WL_INPUT_DEVICE_MOTION,
727 time, x, y, sx, sy);
Kristian Høgsberg225a1762010-08-17 13:14:24 -0400728 break;
729
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400730 case WLSC_DEVICE_GRAB_MOVE:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400731 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400732 es->x = x + device->grab_dx;
733 es->y = y + device->grab_dy;;
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500734 wl_client_post_event(es->surface.client,
735 &ec->shell.object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400736 WL_SHELL_CONFIGURE,
737 time, device->grab,
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500738 &es->surface, es->x, es->y,
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400739 es->width, es->height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400740
741 wlsc_surface_update_matrix(es);
742
743 break;
744
745 case WLSC_DEVICE_GRAB_RESIZE_TOP:
746 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
747 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
748 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
749 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
750 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
751 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
752 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
753 case WLSC_DEVICE_GRAB_RESIZE_MASK:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400754 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400755
756 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_LEFT) {
757 sx = x + device->grab_dx;
758 width = device->grab_x - x + device->grab_width;
759 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_RIGHT) {
760 sx = device->grab_x + device->grab_dx;
761 width = x - device->grab_x + device->grab_width;
762 } else {
763 sx = device->grab_x + device->grab_dx;
764 width = device->grab_width;
765 }
766
767 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_TOP) {
768 sy = y + device->grab_dy;
769 height = device->grab_y - y + device->grab_height;
770 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_BOTTOM) {
771 sy = device->grab_y + device->grab_dy;
772 height = y - device->grab_y + device->grab_height;
773 } else {
774 sy = device->grab_y + device->grab_dy;
775 height = device->grab_height;
776 }
777
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500778 wl_client_post_event(es->surface.client,
779 &ec->shell.object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400780 WL_SHELL_CONFIGURE, time, device->grab,
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500781 &es->surface, sx, sy, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400782 break;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400783
784 case WLSC_DEVICE_GRAB_DRAG:
785 es = pick_surface(device, &sx, &sy);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400786 wl_drag_set_pointer_focus(device->drag,
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500787 &es->surface, time, x, y, sx, sy);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400788 if (es)
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500789 wl_client_post_event(es->surface.client,
790 &device->drag->drag_offer.object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400791 WL_DRAG_OFFER_MOTION,
792 time, x, y, sx, sy);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400793 break;
794
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400795 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500796
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400797 device->sprite->x = device->x - device->hotspot_x;
798 device->sprite->y = device->y - device->hotspot_y;
799 wlsc_surface_update_matrix(device->sprite);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500800
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400801 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500802}
803
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400804static void
805wlsc_input_device_end_grab(struct wlsc_input_device *device, uint32_t time)
806{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400807 struct wl_drag *drag = device->drag;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400808 struct wlsc_surface *es;
809 int32_t sx, sy;
810
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400811 switch (device->grab) {
812 case WLSC_DEVICE_GRAB_DRAG:
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400813 if (drag->target)
814 wl_client_post_event(drag->target,
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500815 &drag->drag_offer.object,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400816 WL_DRAG_OFFER_DROP);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400817 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400818 device->drag = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400819 break;
820 default:
821 break;
822 }
823
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400824 device->grab = WLSC_DEVICE_GRAB_NONE;
825 es = pick_surface(device, &sx, &sy);
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500826 wl_input_device_set_pointer_focus(&device->input_device,
827 &es->surface, time,
828 device->x, device->y, sx, sy);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400829}
830
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500831void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500832notify_button(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400833 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500834{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400835 struct wlsc_surface *surface;
836 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500837
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500838 surface = (struct wlsc_surface *) device->input_device.pointer_focus;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400839 if (surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400840 if (state && device->grab == WLSC_DEVICE_GRAB_NONE) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400841 wlsc_surface_raise(surface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400842 device->grab = WLSC_DEVICE_GRAB_MOTION;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400843 device->grab_button = button;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400844 device->grab_time = time;
845 device->grab_x = device->x;
846 device->grab_y = device->y;
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500847 wl_input_device_set_keyboard_focus(&device->input_device,
848 &surface->surface,
Kristian Høgsberg26437072010-12-01 10:17:47 -0500849 time);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500850 }
851
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400852 if (state && button == BTN_LEFT &&
853 device->grab == WLSC_DEVICE_GRAB_MOTION &&
854 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500855 shell_move(NULL,
856 (struct wl_shell *) &compositor->shell,
857 &surface->surface,
858 &device->input_device, time);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400859 else if (state && button == BTN_MIDDLE &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400860 device->grab == WLSC_DEVICE_GRAB_MOTION &&
861 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500862 shell_resize(NULL,
863 (struct wl_shell *) &compositor->shell,
864 &surface->surface,
865 &device->input_device, time,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400866 WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400867 else if (device->grab == WLSC_DEVICE_GRAB_NONE ||
868 device->grab == WLSC_DEVICE_GRAB_MOTION)
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500869 wl_client_post_event(surface->surface.client,
870 &device->input_device.object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400871 WL_INPUT_DEVICE_BUTTON,
872 time, button, state);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500873
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400874 if (!state &&
875 device->grab != WLSC_DEVICE_GRAB_NONE &&
876 device->grab_button == button) {
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400877 wlsc_input_device_end_grab(device, time);
878 }
879
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400880 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500881 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500882}
883
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500884void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500885notify_key(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400886 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500887{
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500888 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400889 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500890
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400891 switch (key | device->modifier_state) {
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400892 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500893 wl_display_terminate(device->ec->wl_display);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500894 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500895 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500896
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400897 switch (key) {
898 case KEY_LEFTCTRL:
899 case KEY_RIGHTCTRL:
900 modifier = MODIFIER_CTRL;
901 break;
902
903 case KEY_LEFTALT:
904 case KEY_RIGHTALT:
905 modifier = MODIFIER_ALT;
906 break;
907
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400908 case KEY_LEFTMETA:
909 case KEY_RIGHTMETA:
910 modifier = MODIFIER_SUPER;
911 break;
912
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400913 default:
914 modifier = 0;
915 break;
916 }
917
918 if (state)
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400919 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400920 else
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400921 device->modifier_state &= ~modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400922
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500923 end = device->input_device.keys.data + device->input_device.keys.size;
924 for (k = device->input_device.keys.data; k < end; k++) {
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500925 if (*k == key)
926 *k = *--end;
927 }
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500928 device->input_device.keys.size = (void *) end - device->input_device.keys.data;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500929 if (state) {
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500930 k = wl_array_add(&device->input_device.keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500931 *k = key;
932 }
Ray Strodee96dcb82008-12-20 02:00:49 -0500933
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500934 if (device->input_device.keyboard_focus != NULL)
935 wl_client_post_event(device->input_device.keyboard_focus->client,
936 &device->input_device.object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -0400937 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400938}
939
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400940static void
941input_device_attach(struct wl_client *client,
942 struct wl_input_device *device_base,
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400943 uint32_t time,
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400944 struct wl_buffer *buffer, int32_t x, int32_t y)
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400945{
946 struct wlsc_input_device *device =
947 (struct wlsc_input_device *) device_base;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400948
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500949 if (time < device->input_device.pointer_focus_time)
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400950 return;
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500951 if (device->input_device.pointer_focus == NULL)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400952 return;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100953
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500954 if (device->input_device.pointer_focus->client != client &&
955 !(device->input_device.pointer_focus == &wl_grab_surface &&
956 device->grab_surface->surface.client == client))
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400957 return;
958
Kristian Høgsbergf4cb2012010-08-16 17:46:25 -0400959 if (buffer == NULL) {
960 wlsc_input_device_set_pointer_image(device,
961 WLSC_POINTER_LEFT_PTR);
962 return;
963 }
964
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400965 wlsc_input_device_attach(device, buffer, x, y);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400966}
967
968const static struct wl_input_device_interface input_device_interface = {
969 input_device_attach,
970};
971
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400972
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400973static void
974wl_drag_set_pointer_focus(struct wl_drag *drag,
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500975 struct wl_surface *surface, uint32_t time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400976 int32_t x, int32_t y, int32_t sx, int32_t sy)
977{
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400978 char **p, **end;
979
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500980 if (drag->drag_focus == surface)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400981 return;
982
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500983 if (drag->drag_focus &&
984 (!surface || drag->drag_focus->client != surface->client))
985 wl_client_post_event(drag->drag_focus->client,
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500986 &drag->drag_offer.object,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400987 WL_DRAG_OFFER_POINTER_FOCUS,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400988 time, NULL, 0, 0, 0, 0);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400989
990 if (surface &&
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500991 (!drag->drag_focus ||
992 drag->drag_focus->client != surface->client)) {
993 wl_client_post_global(surface->client,
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500994 &drag->drag_offer.object);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400995
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400996 end = drag->types.data + drag->types.size;
997 for (p = drag->types.data; p < end; p++)
Kristian Høgsberg3d76e652010-12-06 17:33:11 -0500998 wl_client_post_event(surface->client,
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500999 &drag->drag_offer.object,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001000 WL_DRAG_OFFER_OFFER, *p);
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001001 }
1002
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001003 if (surface) {
Kristian Høgsberg3d76e652010-12-06 17:33:11 -05001004 wl_client_post_event(surface->client,
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001005 &drag->drag_offer.object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001006 WL_DRAG_OFFER_POINTER_FOCUS,
Kristian Høgsberg3d76e652010-12-06 17:33:11 -05001007 time, surface,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001008 x, y, sx, sy);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001009
1010 }
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001011
Kristian Høgsberg3d76e652010-12-06 17:33:11 -05001012 drag->drag_focus = surface;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001013 drag->pointer_focus_time = time;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001014 drag->target = NULL;
Kristian Høgsberg3d76e652010-12-06 17:33:11 -05001015
1016 wl_list_remove(&drag->drag_focus_listener.link);
1017 if (surface)
1018 wl_list_insert(surface->destroy_listener_list.prev,
1019 &drag->drag_focus_listener.link);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001020}
1021
1022static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001023drag_offer_accept(struct wl_client *client,
1024 struct wl_drag_offer *offer, uint32_t time, const char *type)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001025{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001026 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001027 char **p, **end;
1028
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001029 /* If the client responds to drag pointer_focus or motion
1030 * events after the pointer has left the surface, we just
1031 * discard the accept requests. The drag source just won't
1032 * get the corresponding 'target' events and eventually the
1033 * next surface/root will start sending events. */
1034 if (time < drag->pointer_focus_time)
1035 return;
1036
1037 drag->target = client;
1038 drag->type = NULL;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001039 end = drag->types.data + drag->types.size;
1040 for (p = drag->types.data; p < end; p++)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001041 if (type && strcmp(*p, type) == 0)
1042 drag->type = *p;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001043
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001044 wl_client_post_event(drag->source->client, &drag->resource.object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001045 WL_DRAG_TARGET, drag->type);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001046}
1047
1048static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001049drag_offer_receive(struct wl_client *client,
1050 struct wl_drag_offer *offer, int fd)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001051{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001052 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001053
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001054 wl_client_post_event(drag->source->client, &drag->resource.object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001055 WL_DRAG_FINISH, fd);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001056 close(fd);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001057}
1058
Kristian Høgsbergd44bc8b2010-11-30 15:10:26 -05001059static void
1060drag_offer_reject(struct wl_client *client, struct wl_drag_offer *offer)
1061{
1062 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
1063
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001064 wl_client_post_event(drag->source->client, &drag->resource.object,
Kristian Høgsbergd44bc8b2010-11-30 15:10:26 -05001065 WL_DRAG_REJECT);
1066}
1067
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001068static const struct wl_drag_offer_interface drag_offer_interface = {
1069 drag_offer_accept,
Kristian Høgsbergd44bc8b2010-11-30 15:10:26 -05001070 drag_offer_receive,
1071 drag_offer_reject
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001072};
1073
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001074static void
1075drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
1076{
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001077 char **p;
1078
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001079 p = wl_array_add(&drag->types, sizeof *p);
1080 if (p)
1081 *p = strdup(type);
1082 if (!p || !*p)
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001083 wl_client_post_no_memory(client);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001084}
1085
1086static void
1087drag_activate(struct wl_client *client,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001088 struct wl_drag *drag,
1089 struct wl_surface *surface,
1090 struct wl_input_device *input_device, uint32_t time)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001091{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001092 struct wl_display *display = wl_client_get_display (client);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001093 struct wlsc_input_device *device =
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001094 (struct wlsc_input_device *) input_device;
1095 struct wlsc_surface *target;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001096 int32_t sx, sy;
1097
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001098 if (device->grab != WLSC_DEVICE_GRAB_MOTION ||
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001099 device->input_device.pointer_focus != surface ||
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001100 device->grab_time != time)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001101 return;
1102
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001103 drag->source = surface;
1104 drag->input_device = input_device;
1105
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001106 drag->drag_offer.object.interface = &wl_drag_offer_interface;
1107 drag->drag_offer.object.implementation =
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001108 (void (**)(void)) &drag_offer_interface;
1109
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001110 wl_display_add_object(display, &drag->drag_offer.object);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001111
1112 wlsc_input_device_start_grab(device, time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001113 WLSC_DEVICE_GRAB_DRAG);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001114
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001115 device->drag = drag;
1116 target = pick_surface(device, &sx, &sy);
Kristian Høgsberg3d76e652010-12-06 17:33:11 -05001117 wl_drag_set_pointer_focus(device->drag, &target->surface, time,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001118 device->x, device->y, sx, sy);
1119}
1120
1121static void
1122drag_cancel(struct wl_client *client, struct wl_drag *drag)
1123{
1124 struct wlsc_input_device *device =
1125 (struct wlsc_input_device *) drag->input_device;
Kristian Høgsbergab8475c2010-12-06 16:56:28 -05001126 uint32_t time;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001127
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001128 if (drag->source == NULL || drag->source->client != client)
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001129 return;
1130
Kristian Høgsbergab8475c2010-12-06 16:56:28 -05001131 time = wl_display_get_time(wl_client_get_display(client));
1132 wlsc_input_device_end_grab(device, time);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001133 device->drag = NULL;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001134}
1135
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001136static const struct wl_drag_interface drag_interface = {
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001137 drag_offer,
1138 drag_activate,
1139 drag_cancel,
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001140};
1141
Kristian Høgsbergc551bd22010-12-06 16:43:16 -05001142static void
1143lose_pointer_focus(struct wl_listener *listener,
1144 struct wl_surface *surface)
1145{
Kristian Høgsbergc551bd22010-12-06 16:43:16 -05001146 struct wlsc_input_device *device =
1147 container_of(listener, struct wlsc_input_device,
1148 input_device.pointer_focus_listener);
Kristian Høgsbergab8475c2010-12-06 16:56:28 -05001149 uint32_t time;
Kristian Høgsbergc551bd22010-12-06 16:43:16 -05001150
Kristian Høgsbergab8475c2010-12-06 16:56:28 -05001151 time = wl_display_get_time(wl_client_get_display(surface->client));
Kristian Høgsbergc551bd22010-12-06 16:43:16 -05001152 wl_input_device_set_pointer_focus(&device->input_device,
1153 NULL, time, 0, 0, 0, 0);
1154 wlsc_input_device_end_grab(device, time);
1155}
1156
1157static void
1158lose_keyboard_focus(struct wl_listener *listener,
1159 struct wl_surface *surface)
1160{
Kristian Høgsbergc551bd22010-12-06 16:43:16 -05001161 struct wlsc_input_device *device =
1162 container_of(listener, struct wlsc_input_device,
1163 input_device.keyboard_focus_listener);
Kristian Høgsbergab8475c2010-12-06 16:56:28 -05001164 uint32_t time;
Kristian Høgsbergc551bd22010-12-06 16:43:16 -05001165
Kristian Høgsbergab8475c2010-12-06 16:56:28 -05001166 time = wl_display_get_time(wl_client_get_display(surface->client));
Kristian Høgsbergc551bd22010-12-06 16:43:16 -05001167 wl_input_device_set_keyboard_focus(&device->input_device, NULL, time);
1168}
1169
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001170void
1171wlsc_input_device_init(struct wlsc_input_device *device,
1172 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001173{
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001174 device->input_device.object.interface = &wl_input_device_interface;
1175 device->input_device.object.implementation =
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001176 (void (**)(void)) &input_device_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001177 wl_display_add_object(ec->wl_display, &device->input_device.object);
1178 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001179
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001180 device->x = 100;
1181 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001182 device->ec = ec;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001183 device->sprite = wlsc_surface_create(ec, &ec->argb_visual,
1184 device->x, device->y, 32, 32);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001185 device->hotspot_x = 16;
1186 device->hotspot_y = 16;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001187
Kristian Høgsbergc551bd22010-12-06 16:43:16 -05001188 wl_list_init(&device->input_device.pointer_focus_listener.link);
1189 device->input_device.pointer_focus_listener.func = lose_pointer_focus;
1190 wl_list_init(&device->input_device.keyboard_focus_listener.link);
1191 device->input_device.keyboard_focus_listener.func = lose_keyboard_focus;
1192
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001193 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001194
1195 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001196}
1197
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001198static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001199wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001200{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001201 struct wlsc_output *output =
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001202 container_of(global, struct wlsc_output, object);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001203
1204 wl_client_post_event(client, global,
1205 WL_OUTPUT_GEOMETRY,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001206 output->x, output->y,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001207 output->width, output->height);
1208}
1209
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001210static const char vertex_shader[] =
1211 "uniform mat4 proj;\n"
1212 "attribute vec4 position;\n"
1213 "attribute vec2 texcoord;\n"
1214 "varying vec2 v_texcoord;\n"
1215 "void main()\n"
1216 "{\n"
1217 " gl_Position = proj * position;\n"
1218 " v_texcoord = texcoord;\n"
1219 "}\n";
1220
1221static const char fragment_shader[] =
1222 /* "precision mediump float;\n" */
1223 "varying vec2 v_texcoord;\n"
1224 "uniform sampler2D tex;\n"
1225 "void main()\n"
1226 "{\n"
1227 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1228 "}\n";
1229
Kristian Høgsberga9468212010-06-14 21:03:11 -04001230static int
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001231init_shaders(struct wlsc_compositor *ec)
1232{
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001233 GLuint v, f, program;
1234 const char *p;
1235 char msg[512];
1236 GLfloat vertices[4 * 5];
1237 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001238
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001239 p = vertex_shader;
1240 v = glCreateShader(GL_VERTEX_SHADER);
1241 glShaderSource(v, 1, &p, NULL);
1242 glCompileShader(v);
1243 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
1244 if (!status) {
1245 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
1246 fprintf(stderr, "vertex shader info: %s\n", msg);
1247 return -1;
1248 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001249
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001250 p = fragment_shader;
1251 f = glCreateShader(GL_FRAGMENT_SHADER);
1252 glShaderSource(f, 1, &p, NULL);
1253 glCompileShader(f);
1254 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
1255 if (!status) {
1256 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
1257 fprintf(stderr, "fragment shader info: %s\n", msg);
1258 return -1;
1259 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001260
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001261 program = glCreateProgram();
1262 glAttachShader(program, v);
1263 glAttachShader(program, f);
1264 glBindAttribLocation(program, 0, "position");
1265 glBindAttribLocation(program, 1, "texcoord");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001266
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001267 glLinkProgram(program);
1268 glGetProgramiv(program, GL_LINK_STATUS, &status);
1269 if (!status) {
1270 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1271 fprintf(stderr, "link info: %s\n", msg);
1272 return -1;
1273 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001274
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001275 glUseProgram(program);
1276 ec->proj_uniform = glGetUniformLocation(program, "proj");
1277 ec->tex_uniform = glGetUniformLocation(program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001278
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001279 vertices[ 0] = 0.0;
1280 vertices[ 1] = 0.0;
1281 vertices[ 2] = 0.0;
1282 vertices[ 3] = 0.0;
1283 vertices[ 4] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001284
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001285 vertices[ 5] = 0.0;
1286 vertices[ 6] = 1.0;
1287 vertices[ 7] = 0.0;
1288 vertices[ 8] = 0.0;
1289 vertices[ 9] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001290
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001291 vertices[10] = 1.0;
1292 vertices[11] = 0.0;
1293 vertices[12] = 0.0;
1294 vertices[13] = 1.0;
1295 vertices[14] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001296
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001297 vertices[15] = 1.0;
1298 vertices[16] = 1.0;
1299 vertices[17] = 0.0;
1300 vertices[18] = 1.0;
1301 vertices[19] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001302
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001303 glGenBuffers(1, &ec->vbo);
1304 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
1305 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001306
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001307 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001308}
1309
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001310static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001311add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001312{
Kristian Høgsbergaa827672010-12-01 20:06:39 -05001313 ec->argb_visual.object.interface = &wl_visual_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001314 ec->argb_visual.object.implementation = NULL;
1315 wl_display_add_object(ec->wl_display, &ec->argb_visual.object);
1316 wl_display_add_global(ec->wl_display, &ec->argb_visual.object, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001317
Kristian Høgsbergaa827672010-12-01 20:06:39 -05001318 ec->premultiplied_argb_visual.object.interface = &wl_visual_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001319 ec->premultiplied_argb_visual.object.implementation = NULL;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001320 wl_display_add_object(ec->wl_display,
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001321 &ec->premultiplied_argb_visual.object);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001322 wl_display_add_global(ec->wl_display,
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001323 &ec->premultiplied_argb_visual.object, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001324
Kristian Høgsbergaa827672010-12-01 20:06:39 -05001325 ec->rgb_visual.object.interface = &wl_visual_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001326 ec->rgb_visual.object.implementation = NULL;
1327 wl_display_add_object(ec->wl_display, &ec->rgb_visual.object);
1328 wl_display_add_global(ec->wl_display, &ec->rgb_visual.object, NULL);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001329}
1330
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001331void
1332wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1333 int x, int y, int width, int height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001334{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001335 output->compositor = c;
1336 output->x = x;
1337 output->y = y;
1338 output->width = width;
1339 output->height = height;
1340
1341 output->background =
1342 background_create(output, option_background);
1343
1344 wlsc_matrix_init(&output->matrix);
1345 wlsc_matrix_translate(&output->matrix,
1346 -output->x - output->width / 2.0,
1347 -output->y - output->height / 2.0, 0);
1348 wlsc_matrix_scale(&output->matrix,
1349 2.0 / output->width, 2.0 / output->height, 1);
1350
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001351 output->object.interface = &wl_output_interface;
1352 wl_display_add_object(c->wl_display, &output->object);
1353 wl_display_add_global(c->wl_display, &output->object,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001354 wlsc_output_post_geometry);
1355}
1356
1357int
1358wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1359{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001360 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001361
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001362 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001363
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001364 ec->compositor.object.interface = &wl_compositor_interface;
1365 ec->compositor.object.implementation =
Kristian Høgsbergf8ffded2010-09-03 15:15:33 -04001366 (void (**)(void)) &compositor_interface;
1367
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001368 wl_display_add_object(display, &ec->compositor.object);
1369 if (wl_display_add_global(display, &ec->compositor.object, NULL))
Kristian Høgsbergf8ffded2010-09-03 15:15:33 -04001370 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001371
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001372 wlsc_shm_init(ec);
1373
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001374 ec->shell.object.interface = &wl_shell_interface;
1375 ec->shell.object.implementation = (void (**)(void)) &shell_interface;
1376 wl_display_add_object(display, &ec->shell.object);
1377 if (wl_display_add_global(display, &ec->shell.object, NULL))
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001378 return -1;
1379
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001380 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001381
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001382 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001383 wl_list_init(&ec->input_device_list);
1384 wl_list_init(&ec->output_list);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001385
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001386 create_pointer_images(ec);
1387
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001388 screenshooter_create(ec);
1389
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001390 glGenFramebuffers(1, &ec->fbo);
1391 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1392 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001393 if (init_shaders(ec) < 0)
1394 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001395
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001396 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001397 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001398 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001399
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001400 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001401}
1402
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001403static void on_term_signal(int signal_number, void *data)
1404{
1405 struct wlsc_compositor *ec = data;
1406
1407 wl_display_terminate(ec->wl_display);
1408}
1409
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001410int main(int argc, char *argv[])
1411{
1412 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001413 struct wlsc_compositor *ec;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001414 struct wl_event_loop *loop;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001415 GError *error = NULL;
1416 GOptionContext *context;
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001417 int width, height;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001418
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001419 g_type_init(); /* GdkPixbuf needs this, it seems. */
1420
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001421 context = g_option_context_new(NULL);
1422 g_option_context_add_main_entries(context, option_entries, "Wayland");
1423 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1424 fprintf(stderr, "option parsing failed: %s\n", error->message);
1425 exit(EXIT_FAILURE);
1426 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001427 if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) {
1428 fprintf(stderr, "invalid geometry option: %s \n",
1429 option_geometry);
1430 exit(EXIT_FAILURE);
1431 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001432
1433 display = wl_display_create();
1434
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001435 if (getenv("WAYLAND_DISPLAY"))
1436 ec = wayland_compositor_create(display, width, height);
1437 else if (getenv("DISPLAY"))
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001438 ec = x11_compositor_create(display, width, height);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001439 else
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001440 ec = drm_compositor_create(display, option_connector);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001441
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001442 if (ec == NULL) {
1443 fprintf(stderr, "failed to create compositor\n");
1444 exit(EXIT_FAILURE);
1445 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001446
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001447 if (wl_display_add_socket(display, option_socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001448 fprintf(stderr, "failed to add socket: %m\n");
1449 exit(EXIT_FAILURE);
1450 }
1451
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001452 loop = wl_display_get_event_loop(ec->wl_display);
1453 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
1454 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
1455
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001456 wl_display_run(display);
1457
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001458 wl_display_destroy(display);
1459
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001460 ec->destroy(ec);
1461
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001462 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001463}