blob: 00da446218518bef78410a61d22f4dc5d99ce171 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05008 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050017 */
18
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040019#include <stdio.h>
20#include <string.h>
21#include <stdlib.h>
22#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050023#include <stdarg.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040024#include <sys/ioctl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040025#include <fcntl.h>
26#include <unistd.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050027#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050028#include <math.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050029#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040030#include <linux/input.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050031
Kristian Høgsberga1f3f602010-08-03 09:26:44 -040032#include "wayland-server-protocol.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040033#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050034
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040035const char *option_background = "background.jpg";
36int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050037
38static const GOptionEntry option_entries[] = {
39 { "background", 'b', 0, G_OPTION_ARG_STRING,
40 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -040041 { "connector", 'c', 0, G_OPTION_ARG_INT,
42 &option_connector, "KMS connector" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050043 { NULL }
44};
45
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050046static void
Kristian Høgsberg1db21f12010-08-16 16:08:12 -040047wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
48 struct wlsc_surface *surface,
49 uint32_t time,
50 int32_t x, int32_t y,
51 int32_t sx, int32_t sy);
52
53static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050054wlsc_matrix_init(struct wlsc_matrix *matrix)
55{
56 static const struct wlsc_matrix identity = {
57 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
58 };
59
60 memcpy(matrix, &identity, sizeof identity);
61}
62
63static void
64wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
65{
66 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040067 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050068 div_t d;
69 int i, j;
70
71 for (i = 0; i < 16; i++) {
72 tmp.d[i] = 0;
73 d = div(i, 4);
74 row = m->d + d.quot * 4;
75 column = n->d + d.rem;
76 for (j = 0; j < 4; j++)
77 tmp.d[i] += row[j] * column[j * 4];
78 }
79 memcpy(m, &tmp, sizeof tmp);
80}
81
82static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040083wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050084{
Kristian Høgsberg27803c62010-06-06 22:23:21 -040085 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050086 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
87 };
88
89 wlsc_matrix_multiply(matrix, &translate);
90}
91
92static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040093wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050094{
95 struct wlsc_matrix scale = {
96 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
97 };
98
99 wlsc_matrix_multiply(matrix, &scale);
100}
101
102static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400103wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
104{
105 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400106 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400107
108 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400109 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400110 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400111 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400112 }
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400113
114 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400115}
116
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400117static struct wlsc_surface *
118wlsc_surface_create(struct wlsc_compositor *compositor,
119 struct wl_visual *visual,
120 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500121{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400122 struct wlsc_surface *surface;
123
124 surface = malloc(sizeof *surface);
125 if (surface == NULL)
126 return NULL;
127
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500128 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400129 glBindTexture(GL_TEXTURE_2D, surface->texture);
130 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
131 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
132 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
133 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
134
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500135 surface->compositor = compositor;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500136 surface->visual = visual;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400137 surface->x = x;
138 surface->y = y;
139 surface->width = width;
140 surface->height = height;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400141 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400142 wlsc_matrix_scale(&surface->matrix, width, height, 1);
143 wlsc_matrix_translate(&surface->matrix, x, y, 0);
144
145 wlsc_matrix_init(&surface->matrix_inv);
146 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
147 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500148
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400149 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500150}
151
152static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400153destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500154{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400155 struct wlsc_surface *surface =
156 container_of(resource, struct wlsc_surface, base.base);
157 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500158 struct wlsc_listener *l;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400159
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400160 wl_list_remove(&surface->link);
161 glDeleteTextures(1, &surface->texture);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400162
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500163 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400164 l->func(l, surface);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400165
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400166 free(surface);
Kristian Høgsberg117d5132010-08-11 08:56:47 -0400167
168 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500169}
170
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400171static int
172texture_from_png(const char *filename, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500173{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400174 GdkPixbuf *pixbuf;
175 GError *error = NULL;
176 void *data;
177 GLenum format;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500178
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400179 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
180 width, height,
181 FALSE, &error);
182 if (error != NULL)
183 return -1;
184
185 data = gdk_pixbuf_get_pixels(pixbuf);
186
187 if (gdk_pixbuf_get_has_alpha(pixbuf))
188 format = GL_RGBA;
189 else
190 format = GL_RGB;
191
192 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
193 width, height, 0, format, GL_UNSIGNED_BYTE, data);
194
195 gdk_pixbuf_unref(pixbuf);
196
197 return 0;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500198}
199
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400200static const struct {
201 const char *filename;
202 int hotspot_x, hotspot_y;
203} pointer_images[] = {
Kristian Høgsberg4219a402010-08-16 16:43:03 -0400204 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
205 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
206 { DATADIR "/wayland/bottom_side.png", 16, 20 },
207 { DATADIR "/wayland/grabbing.png", 20, 17 },
208 { DATADIR "/wayland/left_ptr.png", 10, 5 },
209 { DATADIR "/wayland/left_side.png", 10, 20 },
210 { DATADIR "/wayland/right_side.png", 30, 19 },
211 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
212 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
213 { DATADIR "/wayland/top_side.png", 18, 8 },
214 { DATADIR "/wayland/xterm.png", 15, 15 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400215};
216
217static void
218create_pointer_images(struct wlsc_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500219{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400220 int i, count;
221 GLuint texture;
222 const int width = 32, height = 32;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400223
224 EGLint image_attribs[] = {
225 EGL_WIDTH, 0,
226 EGL_HEIGHT, 0,
227 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
228 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
229 EGL_NONE
230 };
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500231
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400232 glGenTextures(1, &texture);
233 glBindTexture(GL_TEXTURE_2D, texture);
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
235 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
236 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
237 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400238
239 image_attribs[1] = width;
240 image_attribs[3] = height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400241 count = ARRAY_LENGTH(pointer_images);
242 ec->pointer_images = malloc(count * sizeof *ec->pointer_images);
243 for (i = 0; i < count; i++) {
244 ec->pointer_images[i] =
245 eglCreateDRMImageMESA(ec->display, image_attribs);
246 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
247 ec->pointer_images[i]);
248 texture_from_png(pointer_images[i].filename, width, height);
249 }
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400250
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400251}
252
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500253static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500254background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500255{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500256 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500257 GdkPixbuf *pixbuf;
258 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500259 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500260 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500261
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400262 background = wlsc_surface_create(output->compositor,
263 &output->compositor->rgb_visual,
264 output->x, output->y,
265 output->width, output->height);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500266 if (background == NULL)
267 return NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500268
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500269 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500270 output->width,
271 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500272 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500273 if (error != NULL) {
274 free(background);
275 return NULL;
276 }
277
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500278 data = gdk_pixbuf_get_pixels(pixbuf);
279
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400280 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500281 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500282 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500283 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500284
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500285 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
286 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500287 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500288
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500289 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500290}
291
292static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400293wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500294{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500295 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400296 struct wlsc_matrix tmp;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500297
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400298 tmp = es->matrix;
299 wlsc_matrix_multiply(&tmp, &output->matrix);
300 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
301 glUniform1i(ec->tex_uniform, 0);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500302
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500303 if (es->visual == &ec->argb_visual) {
304 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
305 glEnable(GL_BLEND);
306 } else if (es->visual == &ec->premultiplied_argb_visual) {
307 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
308 glEnable(GL_BLEND);
309 } else {
310 glDisable(GL_BLEND);
311 }
312
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500313 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500314 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400315 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
316 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
317 5 * sizeof(GLfloat), NULL);
318 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
319 5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
320 glEnableVertexAttribArray(0);
321 glEnableVertexAttribArray(1);
322 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500323}
324
325static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400326wlsc_surface_raise(struct wlsc_surface *surface)
327{
328 struct wlsc_compositor *compositor = surface->compositor;
329
330 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400331 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400332}
333
334static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400335wlsc_surface_update_matrix(struct wlsc_surface *es)
336{
337 wlsc_matrix_init(&es->matrix);
338 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
339 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
340
341 wlsc_matrix_init(&es->matrix_inv);
342 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
343 wlsc_matrix_scale(&es->matrix_inv,
344 1.0 / es->width, 1.0 / es->height, 1);
345}
346
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400347void
348wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400349{
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400350 wl_display_post_frame(compositor->wl_display,
351 &compositor->base,
352 compositor->current_frame, msecs);
353
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400354 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400355 compositor->current_frame++;
356}
357
358static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400359wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400360{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500361 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500362 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500363 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500364
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500365 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500366
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500367 if (output->background)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400368 wlsc_surface_draw(output->background, output);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500369 else
370 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400371
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400372 wl_list_for_each_reverse(es, &ec->surface_list, link)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400373 wlsc_surface_draw(es, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400374
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400375 if (ec->focus)
376 wl_list_for_each(eid, &ec->input_device_list, link)
377 wlsc_surface_draw(eid->sprite, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500378}
379
380static void
381repaint(void *data)
382{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500383 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500384 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500385
386 if (!ec->repaint_needed) {
387 ec->repaint_on_timeout = 0;
388 return;
389 }
390
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500391 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400392 wlsc_output_repaint(output);
393
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400394 ec->present(ec);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500395
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500396 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400397}
398
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400399void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400400wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400401{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400402 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400403 if (compositor->repaint_on_timeout)
404 return;
405
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400406 wl_event_source_timer_update(compositor->timer_source, 1);
407 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400408}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500409
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500410static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500411surface_destroy(struct wl_client *client,
412 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400413{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400414 wl_resource_destroy(&surface->base, client);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400415}
416
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500417static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500418surface_attach(struct wl_client *client,
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400419 struct wl_surface *surface, struct wl_buffer *buffer_base)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400420{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500421 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400422 struct wlsc_buffer *buffer = (struct wlsc_buffer *) buffer_base;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400423
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500424 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400425 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, buffer->image);
426 es->visual = buffer->visual;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400427}
428
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500429static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500430surface_map(struct wl_client *client,
431 struct wl_surface *surface,
432 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400433{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500434 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400435
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400436 es->x = x;
437 es->y = y;
438 es->width = width;
439 es->height = height;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400440
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400441 wlsc_surface_update_matrix(es);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400442}
443
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500444static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500445surface_damage(struct wl_client *client,
446 struct wl_surface *surface,
447 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500448{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500449 /* FIXME: This need to take a damage region, of course. */
450}
451
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500452const static struct wl_surface_interface surface_interface = {
453 surface_destroy,
454 surface_attach,
455 surface_map,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500456 surface_damage
457};
458
459static void
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400460wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
461 enum wlsc_pointer_type type)
462{
463 struct wlsc_compositor *ec = device->ec;
464
465 glBindTexture(GL_TEXTURE_2D, device->sprite->texture);
466 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, ec->pointer_images[type]);
467 device->sprite->visual = &ec->argb_visual;
468 device->hotspot_x = pointer_images[type].hotspot_x;
469 device->hotspot_y = pointer_images[type].hotspot_y;
470
471 device->sprite->x = device->x - device->hotspot_x;
472 device->sprite->y = device->y - device->hotspot_y;
473 wlsc_surface_update_matrix(device->sprite);
474
475 wlsc_compositor_schedule_repaint(ec);
476}
477
478static void
479wlsc_input_device_start_grab(struct wlsc_input_device *device,
480 uint32_t time,
481 enum wlsc_grab_type grab,
482 enum wlsc_pointer_type pointer)
483{
484 device->grab = grab;
485 device->grab_surface = device->pointer_focus;
486 device->grab_dx = device->pointer_focus->x - device->grab_x;
487 device->grab_dy = device->pointer_focus->y - device->grab_y;
488 device->grab_width = device->pointer_focus->width;
489 device->grab_height = device->pointer_focus->height;
490
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400491 wlsc_input_device_set_pointer_focus(device,
492 (struct wlsc_surface *) &wl_grab_surface,
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400493 time, 0, 0, 0, 0);
494
495 wlsc_input_device_set_pointer_image(device, pointer);
496}
497
498static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400499shell_move(struct wl_client *client, struct wl_shell *shell,
500 struct wl_surface *surface,
501 struct wl_input_device *device, uint32_t time)
502{
503 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
504
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400505 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
506 wd->grab_time != time ||
507 &wd->pointer_focus->base != surface)
508 return;
509
510 wlsc_input_device_start_grab(wd, time,
511 WLSC_DEVICE_GRAB_MOVE,
512 WLSC_POINTER_DRAGGING);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400513}
514
515static void
516shell_resize(struct wl_client *client, struct wl_shell *shell,
517 struct wl_surface *surface,
518 struct wl_input_device *device, uint32_t time, uint32_t edges)
519{
520 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400521 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400522
523 if (edges == 0 || edges > 15 ||
524 (edges & 3) == 3 || (edges & 12) == 12)
525 return;
526
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400527 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
528 wd->grab_time != time ||
529 &wd->pointer_focus->base != surface)
530 return;
531
532 switch (edges) {
533 case WLSC_DEVICE_GRAB_RESIZE_TOP:
534 pointer = WLSC_POINTER_TOP;
535 break;
536 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
537 pointer = WLSC_POINTER_BOTTOM;
538 break;
539 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
540 pointer = WLSC_POINTER_LEFT;
541 break;
542 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
543 pointer = WLSC_POINTER_TOP_LEFT;
544 break;
545 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
546 pointer = WLSC_POINTER_BOTTOM_LEFT;
547 break;
548 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
549 pointer = WLSC_POINTER_RIGHT;
550 break;
551 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
552 pointer = WLSC_POINTER_TOP_RIGHT;
553 break;
554 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
555 pointer = WLSC_POINTER_BOTTOM_RIGHT;
556 break;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400557 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400558
559 wlsc_input_device_start_grab(wd, time, edges, pointer);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400560}
561
562const static struct wl_shell_interface shell_interface = {
563 shell_move,
564 shell_resize
565};
566
567static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500568compositor_create_surface(struct wl_client *client,
569 struct wl_compositor *compositor, uint32_t id)
570{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500571 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400572 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500573
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400574 surface = wlsc_surface_create(ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400575 if (surface == NULL) {
576 wl_client_post_event(client,
577 (struct wl_object *) ec->wl_display,
578 WL_DISPLAY_NO_MEMORY, 0);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500579 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400580 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500581
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400582 wl_list_insert(ec->surface_list.prev, &surface->link);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400583 surface->base.base.destroy = destroy_surface;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400584 wl_client_add_surface(client, &surface->base,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500585 &surface_interface, id);
586}
587
588static void
589compositor_commit(struct wl_client *client,
590 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500591{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500592 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500593
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400594 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500595 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500596}
597
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500598const static struct wl_compositor_interface compositor_interface = {
599 compositor_create_surface,
600 compositor_commit
601};
602
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500603static void
604wlsc_surface_transform(struct wlsc_surface *surface,
605 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
606{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400607 struct wlsc_vector v = { { x, y, 0, 1 } };
608
609 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400610 *sx = v.f[0] * surface->width;
611 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500612}
613
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500614static void
615wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400616 struct wlsc_surface *surface,
617 uint32_t time)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500618{
619 if (device->keyboard_focus == surface)
620 return;
621
622 if (device->keyboard_focus &&
623 (!surface || device->keyboard_focus->base.client != surface->base.client))
624 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400625 &device->base.base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400626 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400627 time, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500628
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500629 if (surface)
630 wl_surface_post_event(&surface->base,
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400631 &device->base.base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400632 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400633 time, &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500634
635 device->keyboard_focus = surface;
636}
637
638static void
639wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400640 struct wlsc_surface *surface,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400641 uint32_t time,
642 int32_t x, int32_t y,
643 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500644{
645 if (device->pointer_focus == surface)
646 return;
647
648 if (device->pointer_focus &&
649 (!surface || device->pointer_focus->base.client != surface->base.client))
650 wl_surface_post_event(&device->pointer_focus->base,
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400651 &device->base.base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400652 WL_INPUT_DEVICE_POINTER_FOCUS,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400653 time, NULL, 0, 0, 0, 0);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500654 if (surface)
655 wl_surface_post_event(&surface->base,
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400656 &device->base.base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400657 WL_INPUT_DEVICE_POINTER_FOCUS,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400658 time, &surface->base,
659 x, y, sx, sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500660
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400661 if (!surface)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400662 wlsc_input_device_set_pointer_image(device,
663 WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400664
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500665 device->pointer_focus = surface;
666}
667
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500668static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500669pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500670{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500671 struct wlsc_compositor *ec = device->ec;
672 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500673
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400674 if (device->grab != WLSC_DEVICE_GRAB_NONE) {
675 wlsc_surface_transform(device->pointer_focus,
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500676 device->x, device->y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400677 return device->pointer_focus;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500678 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500679
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400680 wl_list_for_each(es, &ec->surface_list, link) {
681 wlsc_surface_transform(es, device->x, device->y, sx, sy);
682 if (0 <= *sx && *sx < es->width &&
683 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500684 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400685 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500686
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500687 return NULL;
688}
689
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500690void
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400691notify_motion(struct wlsc_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500692{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500693 struct wlsc_surface *es;
694 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500695 struct wlsc_output *output;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400696 int32_t sx, sy, width, height;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500697
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500698 /* FIXME: We need some multi head love here. */
699 output = container_of(ec->output_list.next, struct wlsc_output, link);
700 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500701 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500702 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500703 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500704 if (x >= output->x + output->width)
705 x = output->x + output->width - 1;
706 if (y >= output->y + output->height)
707 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500708
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500709 device->x = x;
710 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500711
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400712 switch (device->grab) {
713 case WLSC_DEVICE_GRAB_NONE:
714 case WLSC_DEVICE_GRAB_MOTION:
715 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400716
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400717 wlsc_input_device_set_pointer_focus(device, es,
718 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400719
720 if (es)
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400721 wl_surface_post_event(&es->base, &device->base.base,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400722 WL_INPUT_DEVICE_MOTION,
723 time, x, y, sx, sy);
724 break;
725
726 case WLSC_DEVICE_GRAB_MOVE:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400727 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400728 es->x = x + device->grab_dx;
729 es->y = y + device->grab_dy;;
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400730 wl_surface_post_event(&es->base, &ec->shell.base,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400731 WL_SHELL_CONFIGURE,
732 time, device->grab,
733 &es->base, es->x, es->y,
734 es->width, es->height);
735
736 wlsc_surface_update_matrix(es);
737
738 break;
739
740 case WLSC_DEVICE_GRAB_RESIZE_TOP:
741 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
742 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
743 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
744 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
745 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
746 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
747 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
748 case WLSC_DEVICE_GRAB_RESIZE_MASK:
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400749 es = device->grab_surface;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400750
751 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_LEFT) {
752 sx = x + device->grab_dx;
753 width = device->grab_x - x + device->grab_width;
754 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_RIGHT) {
755 sx = device->grab_x + device->grab_dx;
756 width = x - device->grab_x + device->grab_width;
757 } else {
758 sx = device->grab_x + device->grab_dx;
759 width = device->grab_width;
760 }
761
762 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_TOP) {
763 sy = y + device->grab_dy;
764 height = device->grab_y - y + device->grab_height;
765 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_BOTTOM) {
766 sy = device->grab_y + device->grab_dy;
767 height = y - device->grab_y + device->grab_height;
768 } else {
769 sy = device->grab_y + device->grab_dy;
770 height = device->grab_height;
771 }
772
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400773 wl_surface_post_event(&es->base, &ec->shell.base,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400774 WL_SHELL_CONFIGURE, time, device->grab,
775 &es->base, sx, sy, width, height);
776 break;
777 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500778
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400779 device->sprite->x = device->x - device->hotspot_x;
780 device->sprite->y = device->y - device->hotspot_y;
781 wlsc_surface_update_matrix(device->sprite);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500782
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400783 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500784}
785
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400786static void
787wlsc_input_device_end_grab(struct wlsc_input_device *device, uint32_t time)
788{
789 struct wlsc_surface *es;
790 int32_t sx, sy;
791
792 device->grab = WLSC_DEVICE_GRAB_NONE;
793 es = pick_surface(device, &sx, &sy);
794 wlsc_input_device_set_pointer_focus(device, es, time,
795 device->x, device->y, sx, sy);
796}
797
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500798void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500799notify_button(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400800 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500801{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400802 struct wlsc_surface *surface;
803 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500804
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400805 surface = device->pointer_focus;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400806 if (surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400807 if (state && device->grab == WLSC_DEVICE_GRAB_NONE) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400808 wlsc_surface_raise(surface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400809 device->grab = WLSC_DEVICE_GRAB_MOTION;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400810 device->grab_button = button;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400811 device->grab_time = time;
812 device->grab_x = device->x;
813 device->grab_y = device->y;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400814 wlsc_input_device_set_keyboard_focus(device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400815 surface, time);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500816 }
817
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400818 if (state && button == BTN_LEFT &&
819 device->grab == WLSC_DEVICE_GRAB_MOTION &&
820 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400821 shell_move(NULL, (struct wl_shell *) &compositor->shell,
822 &surface->base, &device->base, time);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400823 else if (state && button == BTN_MIDDLE &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400824 device->grab == WLSC_DEVICE_GRAB_MOTION &&
825 (device->modifier_state & MODIFIER_SUPER))
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400826 shell_resize(NULL, (struct wl_shell *) &compositor->shell,
827
828 &surface->base, &device->base, time,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400829 WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400830 else if (device->grab == WLSC_DEVICE_GRAB_NONE ||
831 device->grab == WLSC_DEVICE_GRAB_MOTION)
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400832 wl_surface_post_event(&surface->base,
833 &device->base.base,
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400834 WL_INPUT_DEVICE_BUTTON,
835 time, button, state);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500836
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400837 if (!state &&
838 device->grab != WLSC_DEVICE_GRAB_NONE &&
839 device->grab_button == button) {
840 device->grab = WLSC_DEVICE_GRAB_NONE;
841 wlsc_input_device_end_grab(device, time);
842 }
843
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400844 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500845 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500846}
847
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500848void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500849notify_key(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400850 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500851{
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500852 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400853 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500854
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400855 switch (key | device->modifier_state) {
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400856 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400857 kill(0, SIGTERM);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500858 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500859 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500860
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400861 switch (key) {
862 case KEY_LEFTCTRL:
863 case KEY_RIGHTCTRL:
864 modifier = MODIFIER_CTRL;
865 break;
866
867 case KEY_LEFTALT:
868 case KEY_RIGHTALT:
869 modifier = MODIFIER_ALT;
870 break;
871
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400872 case KEY_LEFTMETA:
873 case KEY_RIGHTMETA:
874 modifier = MODIFIER_SUPER;
875 break;
876
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400877 default:
878 modifier = 0;
879 break;
880 }
881
882 if (state)
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400883 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400884 else
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400885 device->modifier_state &= ~modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400886
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500887 end = device->keys.data + device->keys.size;
888 for (k = device->keys.data; k < end; k++) {
889 if (*k == key)
890 *k = *--end;
891 }
892 device->keys.size = (void *) end - device->keys.data;
893 if (state) {
894 k = wl_array_add(&device->keys, sizeof *k);
895 *k = key;
896 }
Ray Strodee96dcb82008-12-20 02:00:49 -0500897
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500898 if (device->keyboard_focus != NULL)
899 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400900 &device->base.base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400901 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400902}
903
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400904static void
905input_device_attach(struct wl_client *client,
906 struct wl_input_device *device_base,
907 struct wl_buffer *buffer_base, int32_t x, int32_t y)
908{
909 struct wlsc_input_device *device =
910 (struct wlsc_input_device *) device_base;
911 struct wlsc_buffer *buffer = (struct wlsc_buffer *) buffer_base;
912
913 if (device->pointer_focus == NULL ||
914 device->pointer_focus->base.client != client)
915 return;
916
Kristian Høgsbergf4cb2012010-08-16 17:46:25 -0400917 if (buffer == NULL) {
918 wlsc_input_device_set_pointer_image(device,
919 WLSC_POINTER_LEFT_PTR);
920 return;
921 }
922
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400923 glBindTexture(GL_TEXTURE_2D, device->sprite->texture);
924 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, buffer->image);
925 device->sprite->visual = buffer->visual;
926 device->hotspot_x = x;
927 device->hotspot_y = y;
Kristian Høgsbergf4cb2012010-08-16 17:46:25 -0400928
929 device->sprite->x = device->x - device->hotspot_x;
930 device->sprite->y = device->y - device->hotspot_y;
931 wlsc_surface_update_matrix(device->sprite);
932
933 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400934}
935
936const static struct wl_input_device_interface input_device_interface = {
937 input_device_attach,
938};
939
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400940static uint32_t
941get_time(void)
942{
943 struct timeval tv;
944
945 gettimeofday(&tv, NULL);
946
947 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500948}
949
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400950static void
951handle_surface_destroy(struct wlsc_listener *listener,
952 struct wlsc_surface *surface)
953{
954 struct wlsc_input_device *device =
955 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400956 uint32_t time = get_time();
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400957
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400958 if (device->keyboard_focus == surface)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400959 wlsc_input_device_set_keyboard_focus(device, NULL, time);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400960 if (device->pointer_focus == surface ||
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400961 (&device->pointer_focus->base == &wl_grab_surface &&
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400962 device->grab_surface == surface))
963 wlsc_input_device_end_grab(device, time);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400964}
965
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400966void
967wlsc_input_device_init(struct wlsc_input_device *device,
968 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500969{
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400970 device->base.base.interface = &wl_input_device_interface;
971 device->base.base.implementation =
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400972 (void (**)(void)) &input_device_interface;
Kristian Høgsberg77a4a792010-08-16 16:24:19 -0400973 wl_display_add_object(ec->wl_display, &device->base.base);
974 wl_display_add_global(ec->wl_display, &device->base.base, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400975
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500976 device->x = 100;
977 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500978 device->ec = ec;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400979 device->sprite = wlsc_surface_create(ec, &ec->argb_visual,
980 device->x, device->y, 32, 32);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400981 device->hotspot_x = 16;
982 device->hotspot_y = 16;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500983
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400984 device->listener.func = handle_surface_destroy;
985 wl_list_insert(ec->surface_destroy_listener_list.prev,
986 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500987 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400988
989 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500990}
991
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500992static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400993wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500994{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500995 struct wlsc_output *output =
996 container_of(global, struct wlsc_output, base);
997
998 wl_client_post_event(client, global,
999 WL_OUTPUT_GEOMETRY,
1000 output->width, output->height);
1001}
1002
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001003static const char vertex_shader[] =
1004 "uniform mat4 proj;\n"
1005 "attribute vec4 position;\n"
1006 "attribute vec2 texcoord;\n"
1007 "varying vec2 v_texcoord;\n"
1008 "void main()\n"
1009 "{\n"
1010 " gl_Position = proj * position;\n"
1011 " v_texcoord = texcoord;\n"
1012 "}\n";
1013
1014static const char fragment_shader[] =
1015 /* "precision mediump float;\n" */
1016 "varying vec2 v_texcoord;\n"
1017 "uniform sampler2D tex;\n"
1018 "void main()\n"
1019 "{\n"
1020 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1021 "}\n";
1022
Kristian Høgsberga9468212010-06-14 21:03:11 -04001023static int
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001024init_shaders(struct wlsc_compositor *ec)
1025{
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001026 GLuint v, f, program;
1027 const char *p;
1028 char msg[512];
1029 GLfloat vertices[4 * 5];
1030 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001031
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001032 p = vertex_shader;
1033 v = glCreateShader(GL_VERTEX_SHADER);
1034 glShaderSource(v, 1, &p, NULL);
1035 glCompileShader(v);
1036 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
1037 if (!status) {
1038 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
1039 fprintf(stderr, "vertex shader info: %s\n", msg);
1040 return -1;
1041 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001042
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001043 p = fragment_shader;
1044 f = glCreateShader(GL_FRAGMENT_SHADER);
1045 glShaderSource(f, 1, &p, NULL);
1046 glCompileShader(f);
1047 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
1048 if (!status) {
1049 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
1050 fprintf(stderr, "fragment shader info: %s\n", msg);
1051 return -1;
1052 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001053
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001054 program = glCreateProgram();
1055 glAttachShader(program, v);
1056 glAttachShader(program, f);
1057 glBindAttribLocation(program, 0, "position");
1058 glBindAttribLocation(program, 1, "texcoord");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001059
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001060 glLinkProgram(program);
1061 glGetProgramiv(program, GL_LINK_STATUS, &status);
1062 if (!status) {
1063 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1064 fprintf(stderr, "link info: %s\n", msg);
1065 return -1;
1066 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001067
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001068 glUseProgram(program);
1069 ec->proj_uniform = glGetUniformLocation(program, "proj");
1070 ec->tex_uniform = glGetUniformLocation(program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001071
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001072 vertices[ 0] = 0.0;
1073 vertices[ 1] = 0.0;
1074 vertices[ 2] = 0.0;
1075 vertices[ 3] = 0.0;
1076 vertices[ 4] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001077
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001078 vertices[ 5] = 0.0;
1079 vertices[ 6] = 1.0;
1080 vertices[ 7] = 0.0;
1081 vertices[ 8] = 0.0;
1082 vertices[ 9] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001083
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001084 vertices[10] = 1.0;
1085 vertices[11] = 0.0;
1086 vertices[12] = 0.0;
1087 vertices[13] = 1.0;
1088 vertices[14] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001089
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001090 vertices[15] = 1.0;
1091 vertices[16] = 1.0;
1092 vertices[17] = 0.0;
1093 vertices[18] = 1.0;
1094 vertices[19] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001095
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001096 glGenBuffers(1, &ec->vbo);
1097 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
1098 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001099
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001100 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001101}
1102
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001103static const struct wl_interface visual_interface = {
1104 "visual", 1,
1105};
1106
1107static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001108add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001109{
1110 ec->argb_visual.base.interface = &visual_interface;
1111 ec->argb_visual.base.implementation = NULL;
1112 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001113 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001114
1115 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1116 ec->premultiplied_argb_visual.base.implementation = NULL;
1117 wl_display_add_object(ec->wl_display,
1118 &ec->premultiplied_argb_visual.base);
1119 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001120 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001121
1122 ec->rgb_visual.base.interface = &visual_interface;
1123 ec->rgb_visual.base.implementation = NULL;
1124 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001125 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1126}
1127
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001128void
1129wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1130 int x, int y, int width, int height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001131{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001132 output->compositor = c;
1133 output->x = x;
1134 output->y = y;
1135 output->width = width;
1136 output->height = height;
1137
1138 output->background =
1139 background_create(output, option_background);
1140
1141 wlsc_matrix_init(&output->matrix);
1142 wlsc_matrix_translate(&output->matrix,
1143 -output->x - output->width / 2.0,
1144 -output->y - output->height / 2.0, 0);
1145 wlsc_matrix_scale(&output->matrix,
1146 2.0 / output->width, 2.0 / output->height, 1);
1147
1148 output->base.interface = &wl_output_interface;
1149 wl_display_add_object(c->wl_display, &output->base);
1150 wl_display_add_global(c->wl_display, &output->base,
1151 wlsc_output_post_geometry);
1152}
1153
1154int
1155wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1156{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001157 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001158
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001159 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001160
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001161 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001162
Kristian Høgsberg77a4a792010-08-16 16:24:19 -04001163 ec->shell.base.interface = &wl_shell_interface;
1164 ec->shell.base.implementation = (void (**)(void)) &shell_interface;
1165 wl_display_add_object(display, &ec->shell.base);
1166 if (wl_display_add_global(display, &ec->shell.base, NULL))
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001167 return -1;
1168
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001169 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001170
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001171 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001172 wl_list_init(&ec->input_device_list);
1173 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001174 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001175
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001176 create_pointer_images(ec);
1177
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001178 screenshooter_create(ec);
1179
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001180 glGenFramebuffers(1, &ec->fbo);
1181 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1182 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001183 if (init_shaders(ec) < 0)
1184 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001185
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001186 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001187 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001188 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001189
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001190 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001191}
1192
1193/* The plan here is to generate a random anonymous socket name and
1194 * advertise that through a service on the session dbus.
1195 */
1196static const char socket_name[] = "\0wayland";
1197
1198int main(int argc, char *argv[])
1199{
1200 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001201 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001202 GError *error = NULL;
1203 GOptionContext *context;
1204
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001205 g_type_init(); /* GdkPixbuf needs this, it seems. */
1206
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001207 context = g_option_context_new(NULL);
1208 g_option_context_add_main_entries(context, option_entries, "Wayland");
1209 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1210 fprintf(stderr, "option parsing failed: %s\n", error->message);
1211 exit(EXIT_FAILURE);
1212 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001213
1214 display = wl_display_create();
1215
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001216 if (getenv("DISPLAY"))
1217 ec = x11_compositor_create(display);
1218 else
1219 ec = drm_compositor_create(display);
1220
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001221 if (ec == NULL) {
1222 fprintf(stderr, "failed to create compositor\n");
1223 exit(EXIT_FAILURE);
1224 }
1225
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001226 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001227 fprintf(stderr, "failed to add socket: %m\n");
1228 exit(EXIT_FAILURE);
1229 }
1230
1231 wl_display_run(display);
1232
1233 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001234}