blob: 7ad2b6827940eb3fb9ea1991b5e0a0d512448501 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05008 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050017 */
18
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040019#include <stdio.h>
20#include <string.h>
21#include <stdlib.h>
22#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050023#include <stdarg.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040024#include <sys/ioctl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040025#include <fcntl.h>
26#include <unistd.h>
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050027#include <cairo.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050028#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050029#include <math.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050030#include <time.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øgsberg5ee1a602008-12-11 23:18:45 -050034#include "cairo-util.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040035#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050036
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040037const char *option_background = "background.jpg";
38int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050039
40static const GOptionEntry option_entries[] = {
41 { "background", 'b', 0, G_OPTION_ARG_STRING,
42 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -040043 { "connector", 'c', 0, G_OPTION_ARG_INT,
44 &option_connector, "KMS connector" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050045 { NULL }
46};
47
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050048static void
Kristian Høgsberg1db21f12010-08-16 16:08:12 -040049wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
50 struct wlsc_surface *surface,
51 uint32_t time,
52 int32_t x, int32_t y,
53 int32_t sx, int32_t sy);
54
55static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050056wlsc_matrix_init(struct wlsc_matrix *matrix)
57{
58 static const struct wlsc_matrix identity = {
59 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
60 };
61
62 memcpy(matrix, &identity, sizeof identity);
63}
64
65static void
66wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
67{
68 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040069 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050070 div_t d;
71 int i, j;
72
73 for (i = 0; i < 16; i++) {
74 tmp.d[i] = 0;
75 d = div(i, 4);
76 row = m->d + d.quot * 4;
77 column = n->d + d.rem;
78 for (j = 0; j < 4; j++)
79 tmp.d[i] += row[j] * column[j * 4];
80 }
81 memcpy(m, &tmp, sizeof tmp);
82}
83
84static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040085wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050086{
Kristian Høgsberg27803c62010-06-06 22:23:21 -040087 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050088 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
89 };
90
91 wlsc_matrix_multiply(matrix, &translate);
92}
93
94static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040095wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050096{
97 struct wlsc_matrix scale = {
98 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
99 };
100
101 wlsc_matrix_multiply(matrix, &scale);
102}
103
104static void
105wlsc_matrix_rotate(struct wlsc_matrix *matrix,
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400106 GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500107{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400108 GLfloat c = cos(angle);
109 GLfloat s = sin(angle);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500110 struct wlsc_matrix rotate = {
111 { x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
Kristian Høgsbergfa548852010-03-08 17:16:01 -0500112 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) + x * s, 0,
113 x * z * (1 - c) + y * s, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500114 0, 0, 0, 1 }
115 };
116
117 wlsc_matrix_multiply(matrix, &rotate);
118}
119
120static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400121wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
122{
123 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400124 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400125
126 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400127 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400128 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400129 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400130 }
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400131
132 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400133}
134
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400135static struct wlsc_surface *
136wlsc_surface_create(struct wlsc_compositor *compositor,
137 struct wl_visual *visual,
138 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500139{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400140 struct wlsc_surface *surface;
141
142 surface = malloc(sizeof *surface);
143 if (surface == NULL)
144 return NULL;
145
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500146 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400147 glBindTexture(GL_TEXTURE_2D, surface->texture);
148 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
149 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
150 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
151 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
152
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500153 surface->compositor = compositor;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500154 surface->visual = visual;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400155 surface->x = x;
156 surface->y = y;
157 surface->width = width;
158 surface->height = height;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400159 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400160 wlsc_matrix_scale(&surface->matrix, width, height, 1);
161 wlsc_matrix_translate(&surface->matrix, x, y, 0);
162
163 wlsc_matrix_init(&surface->matrix_inv);
164 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
165 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500166
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400167 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500168}
169
170static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400171destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500172{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400173 struct wlsc_surface *surface =
174 container_of(resource, struct wlsc_surface, base.base);
175 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500176 struct wlsc_listener *l;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400177
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400178 wl_list_remove(&surface->link);
179 glDeleteTextures(1, &surface->texture);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400180
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500181 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400182 l->func(l, surface);
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
210 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
211 width, height, 0, format, GL_UNSIGNED_BYTE, data);
212
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[] = {
222 { "resources/bottom_left_corner.png", 6, 30 },
223 { "resources/bottom_right_corner.png", 28, 28 },
224 { "resources/bottom_side.png", 16, 20 },
225 { "resources/grabbing.png", 20, 17 },
226 { "resources/left_ptr.png", 10, 5 },
227 { "resources/left_side.png", 10, 20 },
228 { "resources/right_side.png", 30, 19 },
229 { "resources/top_left_corner.png", 8, 8 },
230 { "resources/top_right_corner.png", 26, 8 },
231 { "resources/top_side.png", 18, 8 },
232 { "resources/xterm.png", 15, 15 }
233};
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
242 EGLint image_attribs[] = {
243 EGL_WIDTH, 0,
244 EGL_HEIGHT, 0,
245 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
246 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
247 EGL_NONE
248 };
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500249
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400250 glGenTextures(1, &texture);
251 glBindTexture(GL_TEXTURE_2D, texture);
252 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
253 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
254 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
255 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400256
257 image_attribs[1] = width;
258 image_attribs[3] = height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400259 count = ARRAY_LENGTH(pointer_images);
260 ec->pointer_images = malloc(count * sizeof *ec->pointer_images);
261 for (i = 0; i < count; i++) {
262 ec->pointer_images[i] =
263 eglCreateDRMImageMESA(ec->display, image_attribs);
264 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
265 ec->pointer_images[i]);
266 texture_from_png(pointer_images[i].filename, width, height);
267 }
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400268
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400269}
270
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500271static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500272background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500273{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500274 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500275 GdkPixbuf *pixbuf;
276 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500277 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500278 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500279
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400280 background = wlsc_surface_create(output->compositor,
281 &output->compositor->rgb_visual,
282 output->x, output->y,
283 output->width, output->height);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500284 if (background == NULL)
285 return NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500286
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500287 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500288 output->width,
289 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500290 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500291 if (error != NULL) {
292 free(background);
293 return NULL;
294 }
295
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500296 data = gdk_pixbuf_get_pixels(pixbuf);
297
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400298 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500299 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500300 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500301 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500302
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500303 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
304 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500305 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500306
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500307 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500308}
309
310static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400311wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500312{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500313 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400314 struct wlsc_matrix tmp;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500315
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400316 tmp = es->matrix;
317 wlsc_matrix_multiply(&tmp, &output->matrix);
318 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
319 glUniform1i(ec->tex_uniform, 0);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500320
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500321 if (es->visual == &ec->argb_visual) {
322 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
323 glEnable(GL_BLEND);
324 } else if (es->visual == &ec->premultiplied_argb_visual) {
325 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
326 glEnable(GL_BLEND);
327 } else {
328 glDisable(GL_BLEND);
329 }
330
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500331 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500332 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400333 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
334 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
335 5 * sizeof(GLfloat), NULL);
336 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
337 5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
338 glEnableVertexAttribArray(0);
339 glEnableVertexAttribArray(1);
340 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500341}
342
343static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400344wlsc_surface_raise(struct wlsc_surface *surface)
345{
346 struct wlsc_compositor *compositor = surface->compositor;
347
348 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400349 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400350}
351
352static void
353wlsc_surface_lower(struct wlsc_surface *surface)
354{
355 struct wlsc_compositor *compositor = surface->compositor;
356
357 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400358 wl_list_insert(compositor->surface_list.prev, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400359}
360
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400361static void
362wlsc_surface_update_matrix(struct wlsc_surface *es)
363{
364 wlsc_matrix_init(&es->matrix);
365 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
366 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
367
368 wlsc_matrix_init(&es->matrix_inv);
369 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
370 wlsc_matrix_scale(&es->matrix_inv,
371 1.0 / es->width, 1.0 / es->height, 1);
372}
373
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400374void
375wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400376{
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400377 wl_display_post_frame(compositor->wl_display,
378 &compositor->base,
379 compositor->current_frame, msecs);
380
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400381 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400382 compositor->current_frame++;
383}
384
385static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400386wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400387{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500388 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500389 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500390 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500391
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500392 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500393
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500394 if (output->background)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400395 wlsc_surface_draw(output->background, output);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500396 else
397 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400398
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400399 wl_list_for_each_reverse(es, &ec->surface_list, link)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400400 wlsc_surface_draw(es, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400401
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400402 if (ec->focus)
403 wl_list_for_each(eid, &ec->input_device_list, link)
404 wlsc_surface_draw(eid->sprite, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500405}
406
407static void
408repaint(void *data)
409{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500410 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500411 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500412
413 if (!ec->repaint_needed) {
414 ec->repaint_on_timeout = 0;
415 return;
416 }
417
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500418 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400419 wlsc_output_repaint(output);
420
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400421 ec->present(ec);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500422
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500423 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400424}
425
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400426void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400427wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400428{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400429 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400430 if (compositor->repaint_on_timeout)
431 return;
432
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400433 wl_event_source_timer_update(compositor->timer_source, 1);
434 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400435}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500436
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500437static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500438surface_destroy(struct wl_client *client,
439 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400440{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400441 wl_resource_destroy(&surface->base, client);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400442}
443
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500444static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500445surface_attach(struct wl_client *client,
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400446 struct wl_surface *surface, struct wl_buffer *buffer_base)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400447{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500448 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400449 struct wlsc_buffer *buffer = (struct wlsc_buffer *) buffer_base;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400450
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500451 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400452 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, buffer->image);
453 es->visual = buffer->visual;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400454}
455
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500456static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500457surface_map(struct wl_client *client,
458 struct wl_surface *surface,
459 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400460{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500461 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400462
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400463 es->x = x;
464 es->y = y;
465 es->width = width;
466 es->height = height;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400467
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400468 wlsc_surface_update_matrix(es);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400469}
470
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500471static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500472surface_damage(struct wl_client *client,
473 struct wl_surface *surface,
474 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500475{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500476 /* FIXME: This need to take a damage region, of course. */
477}
478
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500479const static struct wl_surface_interface surface_interface = {
480 surface_destroy,
481 surface_attach,
482 surface_map,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500483 surface_damage
484};
485
486static void
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400487wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
488 enum wlsc_pointer_type type)
489{
490 struct wlsc_compositor *ec = device->ec;
491
492 glBindTexture(GL_TEXTURE_2D, device->sprite->texture);
493 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, ec->pointer_images[type]);
494 device->sprite->visual = &ec->argb_visual;
495 device->hotspot_x = pointer_images[type].hotspot_x;
496 device->hotspot_y = pointer_images[type].hotspot_y;
497
498 device->sprite->x = device->x - device->hotspot_x;
499 device->sprite->y = device->y - device->hotspot_y;
500 wlsc_surface_update_matrix(device->sprite);
501
502 wlsc_compositor_schedule_repaint(ec);
503}
504
505static void
506wlsc_input_device_start_grab(struct wlsc_input_device *device,
507 uint32_t time,
508 enum wlsc_grab_type grab,
509 enum wlsc_pointer_type pointer)
510{
511 device->grab = grab;
512 device->grab_surface = device->pointer_focus;
513 device->grab_dx = device->pointer_focus->x - device->grab_x;
514 device->grab_dy = device->pointer_focus->y - device->grab_y;
515 device->grab_width = device->pointer_focus->width;
516 device->grab_height = device->pointer_focus->height;
517
518 wlsc_input_device_set_pointer_focus(device, &wl_grab_surface,
519 time, 0, 0, 0, 0);
520
521 wlsc_input_device_set_pointer_image(device, pointer);
522}
523
524static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400525shell_move(struct wl_client *client, struct wl_shell *shell,
526 struct wl_surface *surface,
527 struct wl_input_device *device, uint32_t time)
528{
529 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
530
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400531 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
532 wd->grab_time != time ||
533 &wd->pointer_focus->base != surface)
534 return;
535
536 wlsc_input_device_start_grab(wd, time,
537 WLSC_DEVICE_GRAB_MOVE,
538 WLSC_POINTER_DRAGGING);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400539}
540
541static void
542shell_resize(struct wl_client *client, struct wl_shell *shell,
543 struct wl_surface *surface,
544 struct wl_input_device *device, uint32_t time, uint32_t edges)
545{
546 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400547 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400548
549 if (edges == 0 || edges > 15 ||
550 (edges & 3) == 3 || (edges & 12) == 12)
551 return;
552
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400553 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
554 wd->grab_time != time ||
555 &wd->pointer_focus->base != surface)
556 return;
557
558 switch (edges) {
559 case WLSC_DEVICE_GRAB_RESIZE_TOP:
560 pointer = WLSC_POINTER_TOP;
561 break;
562 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
563 pointer = WLSC_POINTER_BOTTOM;
564 break;
565 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
566 pointer = WLSC_POINTER_LEFT;
567 break;
568 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
569 pointer = WLSC_POINTER_TOP_LEFT;
570 break;
571 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
572 pointer = WLSC_POINTER_BOTTOM_LEFT;
573 break;
574 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
575 pointer = WLSC_POINTER_RIGHT;
576 break;
577 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
578 pointer = WLSC_POINTER_TOP_RIGHT;
579 break;
580 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
581 pointer = WLSC_POINTER_BOTTOM_RIGHT;
582 break;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400583 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400584
585 wlsc_input_device_start_grab(wd, time, edges, pointer);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400586}
587
588const static struct wl_shell_interface shell_interface = {
589 shell_move,
590 shell_resize
591};
592
593static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500594compositor_create_surface(struct wl_client *client,
595 struct wl_compositor *compositor, uint32_t id)
596{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500597 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400598 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500599
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400600 surface = wlsc_surface_create(ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400601 if (surface == NULL) {
602 wl_client_post_event(client,
603 (struct wl_object *) ec->wl_display,
604 WL_DISPLAY_NO_MEMORY, 0);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500605 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400606 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500607
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400608 wl_list_insert(ec->surface_list.prev, &surface->link);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400609 surface->base.base.destroy = destroy_surface;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400610 wl_client_add_surface(client, &surface->base,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500611 &surface_interface, id);
612}
613
614static void
615compositor_commit(struct wl_client *client,
616 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500617{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500618 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500619
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400620 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500621 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500622}
623
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500624const static struct wl_compositor_interface compositor_interface = {
625 compositor_create_surface,
626 compositor_commit
627};
628
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500629static void
630wlsc_surface_transform(struct wlsc_surface *surface,
631 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
632{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400633 struct wlsc_vector v = { { x, y, 0, 1 } };
634
635 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400636 *sx = v.f[0] * surface->width;
637 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500638}
639
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500640static void
641wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400642 struct wlsc_surface *surface,
643 uint32_t time)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500644{
645 if (device->keyboard_focus == surface)
646 return;
647
648 if (device->keyboard_focus &&
649 (!surface || device->keyboard_focus->base.client != surface->base.client))
650 wl_surface_post_event(&device->keyboard_focus->base,
651 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400652 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400653 time, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500654
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500655 if (surface)
656 wl_surface_post_event(&surface->base,
657 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400658 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400659 time, &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500660
661 device->keyboard_focus = surface;
662}
663
664static void
665wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400666 struct wlsc_surface *surface,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400667 uint32_t time,
668 int32_t x, int32_t y,
669 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500670{
671 if (device->pointer_focus == surface)
672 return;
673
674 if (device->pointer_focus &&
675 (!surface || device->pointer_focus->base.client != surface->base.client))
676 wl_surface_post_event(&device->pointer_focus->base,
677 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400678 WL_INPUT_DEVICE_POINTER_FOCUS,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400679 time, NULL, 0, 0, 0, 0);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500680 if (surface)
681 wl_surface_post_event(&surface->base,
682 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400683 WL_INPUT_DEVICE_POINTER_FOCUS,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400684 time, &surface->base,
685 x, y, sx, sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500686
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400687 if (!surface)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400688 wlsc_input_device_set_pointer_image(device,
689 WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400690
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500691 device->pointer_focus = surface;
692}
693
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500694static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500695pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500696{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500697 struct wlsc_compositor *ec = device->ec;
698 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500699
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400700 if (device->grab != WLSC_DEVICE_GRAB_NONE) {
701 wlsc_surface_transform(device->pointer_focus,
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500702 device->x, device->y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400703 return device->pointer_focus;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500704 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500705
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400706 wl_list_for_each(es, &ec->surface_list, link) {
707 wlsc_surface_transform(es, device->x, device->y, sx, sy);
708 if (0 <= *sx && *sx < es->width &&
709 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500710 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400711 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500712
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500713 return NULL;
714}
715
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500716void
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400717notify_motion(struct wlsc_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500718{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500719 struct wlsc_surface *es;
720 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500721 struct wlsc_output *output;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400722 int32_t sx, sy, width, height;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500723
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500724 /* FIXME: We need some multi head love here. */
725 output = container_of(ec->output_list.next, struct wlsc_output, link);
726 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500727 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500728 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500729 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500730 if (x >= output->x + output->width)
731 x = output->x + output->width - 1;
732 if (y >= output->y + output->height)
733 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500734
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500735 device->x = x;
736 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500737
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400738 switch (device->grab) {
739 case WLSC_DEVICE_GRAB_NONE:
740 case WLSC_DEVICE_GRAB_MOTION:
741 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400742
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400743 wlsc_input_device_set_pointer_focus(device, es,
744 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400745
746 if (es)
747 wl_surface_post_event(&es->base, &device->base,
748 WL_INPUT_DEVICE_MOTION,
749 time, x, y, sx, sy);
750 break;
751
752 case WLSC_DEVICE_GRAB_MOVE:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400753 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400754 es->x = x + device->grab_dx;
755 es->y = y + device->grab_dy;;
756 wl_surface_post_event(&es->base, &ec->shell,
757 WL_SHELL_CONFIGURE,
758 time, device->grab,
759 &es->base, es->x, es->y,
760 es->width, es->height);
761
762 wlsc_surface_update_matrix(es);
763
764 break;
765
766 case WLSC_DEVICE_GRAB_RESIZE_TOP:
767 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
768 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
769 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
770 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
771 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
772 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
773 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
774 case WLSC_DEVICE_GRAB_RESIZE_MASK:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400775 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400776
777 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_LEFT) {
778 sx = x + device->grab_dx;
779 width = device->grab_x - x + device->grab_width;
780 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_RIGHT) {
781 sx = device->grab_x + device->grab_dx;
782 width = x - device->grab_x + device->grab_width;
783 } else {
784 sx = device->grab_x + device->grab_dx;
785 width = device->grab_width;
786 }
787
788 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_TOP) {
789 sy = y + device->grab_dy;
790 height = device->grab_y - y + device->grab_height;
791 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_BOTTOM) {
792 sy = device->grab_y + device->grab_dy;
793 height = y - device->grab_y + device->grab_height;
794 } else {
795 sy = device->grab_y + device->grab_dy;
796 height = device->grab_height;
797 }
798
799 wl_surface_post_event(&es->base, &ec->shell,
800 WL_SHELL_CONFIGURE, time, device->grab,
801 &es->base, sx, sy, width, height);
802 break;
803 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500804
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400805 device->sprite->x = device->x - device->hotspot_x;
806 device->sprite->y = device->y - device->hotspot_y;
807 wlsc_surface_update_matrix(device->sprite);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500808
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400809 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500810}
811
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400812static void
813wlsc_input_device_end_grab(struct wlsc_input_device *device, uint32_t time)
814{
815 struct wlsc_surface *es;
816 int32_t sx, sy;
817
818 device->grab = WLSC_DEVICE_GRAB_NONE;
819 es = pick_surface(device, &sx, &sy);
820 wlsc_input_device_set_pointer_focus(device, es, time,
821 device->x, device->y, sx, sy);
822}
823
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500824void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500825notify_button(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400826 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500827{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400828 struct wlsc_surface *surface;
829 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500830
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400831 surface = device->pointer_focus;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400832 if (surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400833 if (state && device->grab == WLSC_DEVICE_GRAB_NONE) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400834 wlsc_surface_raise(surface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400835 device->grab = WLSC_DEVICE_GRAB_MOTION;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400836 device->grab_button = button;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400837 device->grab_time = time;
838 device->grab_x = device->x;
839 device->grab_y = device->y;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400840 wlsc_input_device_set_keyboard_focus(device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400841 surface, time);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500842 }
843
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400844 if (state && button == BTN_LEFT &&
845 device->grab == WLSC_DEVICE_GRAB_MOTION &&
846 (device->modifier_state & MODIFIER_SUPER))
847 shell_move(NULL, &compositor->shell,
848 &surface->base, device, time);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400849 else if (state && button == BTN_MIDDLE &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400850 device->grab == WLSC_DEVICE_GRAB_MOTION &&
851 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400852 shell_resize(NULL, &compositor->shell,
853 &surface->base, device, time,
854 WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400855 else if (device->grab == WLSC_DEVICE_GRAB_NONE ||
856 device->grab == WLSC_DEVICE_GRAB_MOTION)
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400857 wl_surface_post_event(&surface->base, &device->base,
858 WL_INPUT_DEVICE_BUTTON,
859 time, button, state);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500860
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400861 if (!state &&
862 device->grab != WLSC_DEVICE_GRAB_NONE &&
863 device->grab_button == button) {
864 device->grab = WLSC_DEVICE_GRAB_NONE;
865 wlsc_input_device_end_grab(device, time);
866 }
867
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400868 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500869 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500870}
871
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500872void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500873notify_key(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400874 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500875{
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500876 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400877 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500878
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400879 switch (key | device->modifier_state) {
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400880 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400881 kill(0, SIGTERM);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500882 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500883 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500884
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400885 switch (key) {
886 case KEY_LEFTCTRL:
887 case KEY_RIGHTCTRL:
888 modifier = MODIFIER_CTRL;
889 break;
890
891 case KEY_LEFTALT:
892 case KEY_RIGHTALT:
893 modifier = MODIFIER_ALT;
894 break;
895
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400896 case KEY_LEFTMETA:
897 case KEY_RIGHTMETA:
898 modifier = MODIFIER_SUPER;
899 break;
900
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400901 default:
902 modifier = 0;
903 break;
904 }
905
906 if (state)
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400907 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400908 else
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400909 device->modifier_state &= ~modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400910
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500911 end = device->keys.data + device->keys.size;
912 for (k = device->keys.data; k < end; k++) {
913 if (*k == key)
914 *k = *--end;
915 }
916 device->keys.size = (void *) end - device->keys.data;
917 if (state) {
918 k = wl_array_add(&device->keys, sizeof *k);
919 *k = key;
920 }
Ray Strodee96dcb82008-12-20 02:00:49 -0500921
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500922 if (device->keyboard_focus != NULL)
923 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400924 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400925 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400926}
927
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400928static void
929input_device_attach(struct wl_client *client,
930 struct wl_input_device *device_base,
931 struct wl_buffer *buffer_base, int32_t x, int32_t y)
932{
933 struct wlsc_input_device *device =
934 (struct wlsc_input_device *) device_base;
935 struct wlsc_buffer *buffer = (struct wlsc_buffer *) buffer_base;
936
937 if (device->pointer_focus == NULL ||
938 device->pointer_focus->base.client != client)
939 return;
940
941 glBindTexture(GL_TEXTURE_2D, device->sprite->texture);
942 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, buffer->image);
943 device->sprite->visual = buffer->visual;
944 device->hotspot_x = x;
945 device->hotspot_y = y;
946}
947
948const static struct wl_input_device_interface input_device_interface = {
949 input_device_attach,
950};
951
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400952static uint32_t
953get_time(void)
954{
955 struct timeval tv;
956
957 gettimeofday(&tv, NULL);
958
959 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500960}
961
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400962static void
963handle_surface_destroy(struct wlsc_listener *listener,
964 struct wlsc_surface *surface)
965{
966 struct wlsc_input_device *device =
967 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400968 uint32_t time = get_time();
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400969
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400970 if (device->keyboard_focus == surface)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400971 wlsc_input_device_set_keyboard_focus(device, NULL, time);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400972 if (device->pointer_focus == surface ||
973 (device->pointer_focus == &wl_grab_surface &&
974 device->grab_surface == surface))
975 wlsc_input_device_end_grab(device, time);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400976}
977
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400978void
979wlsc_input_device_init(struct wlsc_input_device *device,
980 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500981{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500982 device->base.interface = &wl_input_device_interface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400983 device->base.implementation =
984 (void (**)(void)) &input_device_interface;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500985 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500986 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400987
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500988 device->x = 100;
989 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500990 device->ec = ec;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400991 device->sprite = wlsc_surface_create(ec, &ec->argb_visual,
992 device->x, device->y, 32, 32);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400993 device->hotspot_x = 16;
994 device->hotspot_y = 16;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500995
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400996 device->listener.func = handle_surface_destroy;
997 wl_list_insert(ec->surface_destroy_listener_list.prev,
998 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500999 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001000
1001 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001002}
1003
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001004static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001005wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001006{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001007 struct wlsc_output *output =
1008 container_of(global, struct wlsc_output, base);
1009
1010 wl_client_post_event(client, global,
1011 WL_OUTPUT_GEOMETRY,
1012 output->width, output->height);
1013}
1014
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001015static const char vertex_shader[] =
1016 "uniform mat4 proj;\n"
1017 "attribute vec4 position;\n"
1018 "attribute vec2 texcoord;\n"
1019 "varying vec2 v_texcoord;\n"
1020 "void main()\n"
1021 "{\n"
1022 " gl_Position = proj * position;\n"
1023 " v_texcoord = texcoord;\n"
1024 "}\n";
1025
1026static const char fragment_shader[] =
1027 /* "precision mediump float;\n" */
1028 "varying vec2 v_texcoord;\n"
1029 "uniform sampler2D tex;\n"
1030 "void main()\n"
1031 "{\n"
1032 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1033 "}\n";
1034
Kristian Høgsberga9468212010-06-14 21:03:11 -04001035static int
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001036init_shaders(struct wlsc_compositor *ec)
1037{
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001038 GLuint v, f, program;
1039 const char *p;
1040 char msg[512];
1041 GLfloat vertices[4 * 5];
1042 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001043
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001044 p = vertex_shader;
1045 v = glCreateShader(GL_VERTEX_SHADER);
1046 glShaderSource(v, 1, &p, NULL);
1047 glCompileShader(v);
1048 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
1049 if (!status) {
1050 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
1051 fprintf(stderr, "vertex shader info: %s\n", msg);
1052 return -1;
1053 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001054
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001055 p = fragment_shader;
1056 f = glCreateShader(GL_FRAGMENT_SHADER);
1057 glShaderSource(f, 1, &p, NULL);
1058 glCompileShader(f);
1059 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
1060 if (!status) {
1061 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
1062 fprintf(stderr, "fragment shader info: %s\n", msg);
1063 return -1;
1064 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001065
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001066 program = glCreateProgram();
1067 glAttachShader(program, v);
1068 glAttachShader(program, f);
1069 glBindAttribLocation(program, 0, "position");
1070 glBindAttribLocation(program, 1, "texcoord");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001071
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001072 glLinkProgram(program);
1073 glGetProgramiv(program, GL_LINK_STATUS, &status);
1074 if (!status) {
1075 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1076 fprintf(stderr, "link info: %s\n", msg);
1077 return -1;
1078 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001079
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001080 glUseProgram(program);
1081 ec->proj_uniform = glGetUniformLocation(program, "proj");
1082 ec->tex_uniform = glGetUniformLocation(program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001083
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001084 vertices[ 0] = 0.0;
1085 vertices[ 1] = 0.0;
1086 vertices[ 2] = 0.0;
1087 vertices[ 3] = 0.0;
1088 vertices[ 4] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001089
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001090 vertices[ 5] = 0.0;
1091 vertices[ 6] = 1.0;
1092 vertices[ 7] = 0.0;
1093 vertices[ 8] = 0.0;
1094 vertices[ 9] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001095
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001096 vertices[10] = 1.0;
1097 vertices[11] = 0.0;
1098 vertices[12] = 0.0;
1099 vertices[13] = 1.0;
1100 vertices[14] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001101
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001102 vertices[15] = 1.0;
1103 vertices[16] = 1.0;
1104 vertices[17] = 0.0;
1105 vertices[18] = 1.0;
1106 vertices[19] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001107
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001108 glGenBuffers(1, &ec->vbo);
1109 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
1110 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001111
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001112 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001113}
1114
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001115static const struct wl_interface visual_interface = {
1116 "visual", 1,
1117};
1118
1119static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001120add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001121{
1122 ec->argb_visual.base.interface = &visual_interface;
1123 ec->argb_visual.base.implementation = NULL;
1124 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001125 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001126
1127 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1128 ec->premultiplied_argb_visual.base.implementation = NULL;
1129 wl_display_add_object(ec->wl_display,
1130 &ec->premultiplied_argb_visual.base);
1131 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001132 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001133
1134 ec->rgb_visual.base.interface = &visual_interface;
1135 ec->rgb_visual.base.implementation = NULL;
1136 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001137 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1138}
1139
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001140void
1141wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1142 int x, int y, int width, int height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001143{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001144 output->compositor = c;
1145 output->x = x;
1146 output->y = y;
1147 output->width = width;
1148 output->height = height;
1149
1150 output->background =
1151 background_create(output, option_background);
1152
1153 wlsc_matrix_init(&output->matrix);
1154 wlsc_matrix_translate(&output->matrix,
1155 -output->x - output->width / 2.0,
1156 -output->y - output->height / 2.0, 0);
1157 wlsc_matrix_scale(&output->matrix,
1158 2.0 / output->width, 2.0 / output->height, 1);
1159
1160 output->base.interface = &wl_output_interface;
1161 wl_display_add_object(c->wl_display, &output->base);
1162 wl_display_add_global(c->wl_display, &output->base,
1163 wlsc_output_post_geometry);
1164}
1165
1166int
1167wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1168{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001169 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001170
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001171 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001172
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001173 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001174
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001175 ec->shell.interface = &wl_shell_interface;
1176 ec->shell.implementation = (void (**)(void)) &shell_interface;
1177 wl_display_add_object(display, &ec->shell);
1178 if (wl_display_add_global(display, &ec->shell, NULL))
1179 return -1;
1180
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001181 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001182
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001183 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001184 wl_list_init(&ec->input_device_list);
1185 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001186 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001187
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001188 create_pointer_images(ec);
1189
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001190 screenshooter_create(ec);
1191
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001192 glGenFramebuffers(1, &ec->fbo);
1193 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1194 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001195 if (init_shaders(ec) < 0)
1196 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001197
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001198 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001199 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001200 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001201
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001202 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001203}
1204
1205/* The plan here is to generate a random anonymous socket name and
1206 * advertise that through a service on the session dbus.
1207 */
1208static const char socket_name[] = "\0wayland";
1209
1210int main(int argc, char *argv[])
1211{
1212 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001213 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001214 GError *error = NULL;
1215 GOptionContext *context;
1216
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001217 g_type_init(); /* GdkPixbuf needs this, it seems. */
1218
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001219 context = g_option_context_new(NULL);
1220 g_option_context_add_main_entries(context, option_entries, "Wayland");
1221 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1222 fprintf(stderr, "option parsing failed: %s\n", error->message);
1223 exit(EXIT_FAILURE);
1224 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001225
1226 display = wl_display_create();
1227
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001228 if (getenv("DISPLAY"))
1229 ec = x11_compositor_create(display);
1230 else
1231 ec = drm_compositor_create(display);
1232
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001233 if (ec == NULL) {
1234 fprintf(stderr, "failed to create compositor\n");
1235 exit(EXIT_FAILURE);
1236 }
1237
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001238 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001239 fprintf(stderr, "failed to add socket: %m\n");
1240 exit(EXIT_FAILURE);
1241 }
1242
1243 wl_display_run(display);
1244
1245 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001246}