blob: 6d7a627e47e64759cc98372d2210ebb9e6b4be62 [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øgsberg4adaf5c2009-09-12 16:42:07 -040033#define GL_GLEXT_PROTOTYPES
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -050034#define EGL_EGLEXT_PROTOTYPES
Kristian Høgsberg27803c62010-06-06 22:23:21 -040035#include <GLES2/gl2.h>
36#include <GLES2/gl2ext.h>
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -050037#include <EGL/egl.h>
38#include <EGL/eglext.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040039
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050040#include "wayland.h"
Kristian Høgsberga1f3f602010-08-03 09:26:44 -040041#include "wayland-server-protocol.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050042#include "cairo-util.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040043#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050044
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040045const char *option_background = "background.jpg";
46int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050047
48static const GOptionEntry option_entries[] = {
49 { "background", 'b', 0, G_OPTION_ARG_STRING,
50 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -040051 { "connector", 'c', 0, G_OPTION_ARG_INT,
52 &option_connector, "KMS connector" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050053 { NULL }
54};
55
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050056static void
57wlsc_matrix_init(struct wlsc_matrix *matrix)
58{
59 static const struct wlsc_matrix identity = {
60 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
61 };
62
63 memcpy(matrix, &identity, sizeof identity);
64}
65
66static void
67wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
68{
69 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040070 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050071 div_t d;
72 int i, j;
73
74 for (i = 0; i < 16; i++) {
75 tmp.d[i] = 0;
76 d = div(i, 4);
77 row = m->d + d.quot * 4;
78 column = n->d + d.rem;
79 for (j = 0; j < 4; j++)
80 tmp.d[i] += row[j] * column[j * 4];
81 }
82 memcpy(m, &tmp, sizeof tmp);
83}
84
85static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040086wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050087{
Kristian Høgsberg27803c62010-06-06 22:23:21 -040088 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050089 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
90 };
91
92 wlsc_matrix_multiply(matrix, &translate);
93}
94
95static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040096wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050097{
98 struct wlsc_matrix scale = {
99 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
100 };
101
102 wlsc_matrix_multiply(matrix, &scale);
103}
104
105static void
106wlsc_matrix_rotate(struct wlsc_matrix *matrix,
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400107 GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500108{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400109 GLfloat c = cos(angle);
110 GLfloat s = sin(angle);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500111 struct wlsc_matrix rotate = {
112 { 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 -0500113 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) + x * s, 0,
114 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 -0500115 0, 0, 0, 1 }
116 };
117
118 wlsc_matrix_multiply(matrix, &rotate);
119}
120
121static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400122wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
123{
124 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400125 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400126
127 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400128 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400129 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400130 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400131 }
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400132
133 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400134}
135
136static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500137wlsc_surface_init(struct wlsc_surface *surface,
138 struct wlsc_compositor *compositor, struct wl_visual *visual,
139 int32_t x, int32_t y, int32_t width, int32_t height)
140{
141 glGenTextures(1, &surface->texture);
142 surface->compositor = compositor;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500143 surface->visual = visual;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400144 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400145 wlsc_matrix_scale(&surface->matrix, width, height, 1);
146 wlsc_matrix_translate(&surface->matrix, x, y, 0);
147
148 wlsc_matrix_init(&surface->matrix_inv);
149 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
150 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500151}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500152
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500153static struct wlsc_surface *
154wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500155 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500156 int x, int y, int width, int height)
157{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500158 struct wlsc_surface *es;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500159 int stride;
160 void *data;
161
162 stride = cairo_image_surface_get_stride(surface);
163 data = cairo_image_surface_get_data(surface);
164
165 es = malloc(sizeof *es);
166 if (es == NULL)
167 return NULL;
168
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500169 wlsc_surface_init(es, ec, &ec->premultiplied_argb_visual,
170 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500171 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400172 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
173 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500174 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
175 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500176 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400177 GL_RGBA, GL_UNSIGNED_BYTE, data);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500178
Kristian Høgsberg54879822008-11-23 17:07:32 -0500179 return es;
180}
181
182static void
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400183wlsc_surface_destroy(struct wlsc_surface *surface,
184 struct wlsc_compositor *compositor)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500185{
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500186 struct wlsc_listener *l;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400187
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400188 wl_list_remove(&surface->link);
189 glDeleteTextures(1, &surface->texture);
190 wl_client_remove_surface(surface->base.client, &surface->base);
191
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500192 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400193 l->func(l, surface);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400194
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400195 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500196}
197
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400198static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500199pointer_path(cairo_t *cr, int x, int y)
200{
201 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
202 const int width = 16, height = 16;
203
204 cairo_move_to(cr, x, y);
205 cairo_line_to(cr, x + tx, y + ty);
206 cairo_line_to(cr, x + dx, y + dy);
207 cairo_line_to(cr, x + width - end, y + height);
208 cairo_line_to(cr, x + width, y + height - end);
209 cairo_line_to(cr, x + dy, y + dx);
210 cairo_line_to(cr, x + ty, y + tx);
211 cairo_close_path(cr);
212}
213
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500214static struct wlsc_surface *
215pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500216{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500217 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500218 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500219 cairo_surface_t *surface;
220 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500221
222 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
223 width, height);
224
225 cr = cairo_create(surface);
226 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500227 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500228 cairo_set_source_rgb(cr, 0, 0, 0);
229 cairo_stroke_preserve(cr);
230 cairo_fill(cr);
231 blur_surface(surface, width);
232
233 pointer_path(cr, hotspot_x, hotspot_y);
234 cairo_stroke_preserve(cr);
235 cairo_set_source_rgb(cr, 1, 1, 1);
236 cairo_fill(cr);
237 cairo_destroy(cr);
238
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500239 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500240 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500241 x - hotspot_x,
242 y - hotspot_y,
243 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500244
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500245 cairo_surface_destroy(surface);
246
Kristian Høgsberg54879822008-11-23 17:07:32 -0500247 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500248}
249
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500250static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500251background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500252{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500253 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500254 GdkPixbuf *pixbuf;
255 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500256 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500257 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500258
259 background = malloc(sizeof *background);
260 if (background == NULL)
261 return NULL;
262
263 g_type_init();
264
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500265 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500266 output->width,
267 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500268 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500269 if (error != NULL) {
270 free(background);
271 return NULL;
272 }
273
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500274 data = gdk_pixbuf_get_pixels(pixbuf);
275
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500276 wlsc_surface_init(background, output->compositor,
277 &output->compositor->rgb_visual,
278 output->x, output->y, output->width, output->height);
279
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500280 glBindTexture(GL_TEXTURE_2D, background->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400281 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
282 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500283 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
284 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500285
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500286 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500287 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500288 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500289 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500290
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500291 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
292 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500293 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500294
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500295 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500296}
297
298static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400299wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500300{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500301 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400302 struct wlsc_matrix tmp;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500303
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400304 tmp = es->matrix;
305 wlsc_matrix_multiply(&tmp, &output->matrix);
306 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
307 glUniform1i(ec->tex_uniform, 0);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500308
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500309 if (es->visual == &ec->argb_visual) {
310 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
311 glEnable(GL_BLEND);
312 } else if (es->visual == &ec->premultiplied_argb_visual) {
313 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
314 glEnable(GL_BLEND);
315 } else {
316 glDisable(GL_BLEND);
317 }
318
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500319 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500320 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400321 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
322 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
323 5 * sizeof(GLfloat), NULL);
324 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
325 5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
326 glEnableVertexAttribArray(0);
327 glEnableVertexAttribArray(1);
328 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500329}
330
331static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400332wlsc_surface_raise(struct wlsc_surface *surface)
333{
334 struct wlsc_compositor *compositor = surface->compositor;
335
336 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400337 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400338}
339
340static void
341wlsc_surface_lower(struct wlsc_surface *surface)
342{
343 struct wlsc_compositor *compositor = surface->compositor;
344
345 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400346 wl_list_insert(compositor->surface_list.prev, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400347}
348
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400349static void
350wlsc_surface_update_matrix(struct wlsc_surface *es)
351{
352 wlsc_matrix_init(&es->matrix);
353 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
354 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
355
356 wlsc_matrix_init(&es->matrix_inv);
357 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
358 wlsc_matrix_scale(&es->matrix_inv,
359 1.0 / es->width, 1.0 / es->height, 1);
360}
361
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400362void
363wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400364{
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400365 wl_display_post_frame(compositor->wl_display,
366 &compositor->base,
367 compositor->current_frame, msecs);
368
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400369 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400370 compositor->current_frame++;
371}
372
373static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400374wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400375{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500376 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500377 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500378 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500379
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500380 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500381
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500382 if (output->background)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400383 wlsc_surface_draw(output->background, output);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500384 else
385 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400386
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400387 wl_list_for_each_reverse(es, &ec->surface_list, link)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400388 wlsc_surface_draw(es, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400389
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400390 if (ec->focus)
391 wl_list_for_each(eid, &ec->input_device_list, link)
392 wlsc_surface_draw(eid->sprite, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500393}
394
395static void
396repaint(void *data)
397{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500398 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500399 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500400
401 if (!ec->repaint_needed) {
402 ec->repaint_on_timeout = 0;
403 return;
404 }
405
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500406 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400407 wlsc_output_repaint(output);
408
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400409 ec->present(ec);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500410
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500411 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400412}
413
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400414void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400415wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400416{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400417 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400418 if (compositor->repaint_on_timeout)
419 return;
420
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400421 wl_event_source_timer_update(compositor->timer_source, 1);
422 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400423}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500424
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500425static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500426surface_destroy(struct wl_client *client,
427 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400428{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500429 struct wlsc_surface *es = (struct wlsc_surface *) surface;
430 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500431
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500432 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400433
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400434 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400435}
436
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500437static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500438surface_attach(struct wl_client *client,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400439 struct wl_surface *surface, uint32_t name,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400440 int32_t width, int32_t height, uint32_t stride,
441 struct wl_visual *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400442{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500443 struct wlsc_surface *es = (struct wlsc_surface *) surface;
444 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsbergc071f4d2010-08-05 17:44:31 -0400445 EGLImageKHR image;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500446 EGLint attribs[] = {
Kristian Høgsberga2ee6752010-06-04 22:14:28 -0400447 EGL_WIDTH, 0,
448 EGL_HEIGHT, 0,
Kristian Høgsberga2ee6752010-06-04 22:14:28 -0400449 EGL_IMAGE_STRIDE_MESA, 0,
Kristian Høgsberg6619d382010-06-07 12:23:01 -0400450 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500451 EGL_NONE
452 };
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400453
Kristian Høgsbergc071f4d2010-08-05 17:44:31 -0400454 attribs[1] = width;
455 attribs[3] = height;
456 attribs[5] = stride / 4;
457
458 image = eglCreateImageKHR(ec->display, ec->context,
459 EGL_DRM_IMAGE_MESA,
460 (EGLClientBuffer) name, attribs);
461 if (image == NULL) {
462 /* FIXME: Define a real exception event instead of
463 * abusing this one */
464 wl_client_post_event(client, ec->wl_display,
465 WL_DISPLAY_INVALID_OBJECT, 0);
466 fprintf(stderr, "failed to create image for name %d\n", name);
467 return;
468 }
469
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400470 if (visual == &ec->argb_visual)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500471 es->visual = &ec->argb_visual;
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400472 else if (visual == &ec->premultiplied_argb_visual)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500473 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400474 else if (visual == &ec->rgb_visual)
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500475 es->visual = &ec->rgb_visual;
Kristian Høgsbergc071f4d2010-08-05 17:44:31 -0400476 else {
477 /* FIXME: Define a real exception event instead of
478 * abusing this one */
479 wl_client_post_event(client, ec->display,
480 WL_DISPLAY_INVALID_OBJECT, 0);
481 fprintf(stderr, "invalid visual in surface_attach\n");
482 return;
483 }
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400484
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500485 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400486 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
487 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500488 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
489 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500490
491 if (es->image)
492 eglDestroyImageKHR(ec->display, es->image);
493
Kristian Høgsbergc071f4d2010-08-05 17:44:31 -0400494 es->image = image;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500495
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500496 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400497}
498
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500499static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500500surface_map(struct wl_client *client,
501 struct wl_surface *surface,
502 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400503{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500504 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400505
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400506 es->x = x;
507 es->y = y;
508 es->width = width;
509 es->height = height;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400510
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400511 wlsc_surface_update_matrix(es);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400512}
513
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500514static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500515surface_damage(struct wl_client *client,
516 struct wl_surface *surface,
517 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500518{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500519 /* FIXME: This need to take a damage region, of course. */
520}
521
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500522const static struct wl_surface_interface surface_interface = {
523 surface_destroy,
524 surface_attach,
525 surface_map,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500526 surface_damage
527};
528
529static void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400530shell_move(struct wl_client *client, struct wl_shell *shell,
531 struct wl_surface *surface,
532 struct wl_input_device *device, uint32_t time)
533{
534 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
535
536 if (wd->grab == WLSC_DEVICE_GRAB_MOTION &&
537 wd->grab_time == time &&
538 &wd->pointer_focus->base == surface) {
539 wd->grab = WLSC_DEVICE_GRAB_MOVE;
540 wd->grab_dx = wd->pointer_focus->x - wd->grab_x;
541 wd->grab_dy = wd->pointer_focus->y - wd->grab_y;
542 }
543}
544
545static void
546shell_resize(struct wl_client *client, struct wl_shell *shell,
547 struct wl_surface *surface,
548 struct wl_input_device *device, uint32_t time, uint32_t edges)
549{
550 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
551
552 if (edges == 0 || edges > 15 ||
553 (edges & 3) == 3 || (edges & 12) == 12)
554 return;
555
556 if (wd->grab == WLSC_DEVICE_GRAB_MOTION &&
557 wd->grab_time == time &&
558 &wd->pointer_focus->base == surface) {
559 wd->grab = edges;
560 wd->grab_dx = wd->pointer_focus->x - wd->grab_x;
561 wd->grab_dy = wd->pointer_focus->y - wd->grab_y;
562 wd->grab_width = wd->pointer_focus->width;
563 wd->grab_height = wd->pointer_focus->height;
564 }
565}
566
567const static struct wl_shell_interface shell_interface = {
568 shell_move,
569 shell_resize
570};
571
572static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500573compositor_create_surface(struct wl_client *client,
574 struct wl_compositor *compositor, uint32_t id)
575{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500576 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400577 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500578
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400579 surface = malloc(sizeof *surface);
580 if (surface == NULL)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500581 /* FIXME: Send OOM event. */
582 return;
583
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400584 wlsc_surface_init(surface, ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500585
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400586 wl_list_insert(ec->surface_list.prev, &surface->link);
587 wl_client_add_surface(client, &surface->base,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500588 &surface_interface, id);
589}
590
591static void
592compositor_commit(struct wl_client *client,
593 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500594{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500595 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500596
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400597 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500598 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500599}
600
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500601const static struct wl_compositor_interface compositor_interface = {
602 compositor_create_surface,
603 compositor_commit
604};
605
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500606static void
607wlsc_surface_transform(struct wlsc_surface *surface,
608 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
609{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400610 struct wlsc_vector v = { { x, y, 0, 1 } };
611
612 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400613 *sx = v.f[0] * surface->width;
614 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500615}
616
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500617static void
618wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400619 struct wlsc_surface *surface,
620 uint32_t time)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500621{
622 if (device->keyboard_focus == surface)
623 return;
624
625 if (device->keyboard_focus &&
626 (!surface || device->keyboard_focus->base.client != surface->base.client))
627 wl_surface_post_event(&device->keyboard_focus->base,
628 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400629 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400630 time, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500631
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500632 if (surface)
633 wl_surface_post_event(&surface->base,
634 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400635 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400636 time, &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500637
638 device->keyboard_focus = surface;
639}
640
641static void
642wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400643 struct wlsc_surface *surface,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400644 uint32_t time,
645 int32_t x, int32_t y,
646 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500647{
648 if (device->pointer_focus == surface)
649 return;
650
651 if (device->pointer_focus &&
652 (!surface || device->pointer_focus->base.client != surface->base.client))
653 wl_surface_post_event(&device->pointer_focus->base,
654 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400655 WL_INPUT_DEVICE_POINTER_FOCUS,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400656 time, NULL, 0, 0, 0, 0);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500657 if (surface)
658 wl_surface_post_event(&surface->base,
659 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400660 WL_INPUT_DEVICE_POINTER_FOCUS,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400661 time, &surface->base,
662 x, y, sx, sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500663
664 device->pointer_focus = surface;
665}
666
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500667static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500668pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500669{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500670 struct wlsc_compositor *ec = device->ec;
671 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500672
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400673 if (device->grab != WLSC_DEVICE_GRAB_NONE) {
674 wlsc_surface_transform(device->pointer_focus,
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500675 device->x, device->y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400676 return device->pointer_focus;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500677 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500678
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400679 wl_list_for_each(es, &ec->surface_list, link) {
680 wlsc_surface_transform(es, device->x, device->y, sx, sy);
681 if (0 <= *sx && *sx < es->width &&
682 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500683 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400684 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500685
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500686 return NULL;
687}
688
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500689void
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400690notify_motion(struct wlsc_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500691{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500692 struct wlsc_surface *es;
693 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500694 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500695 const int hotspot_x = 16, hotspot_y = 16;
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)
721 wl_surface_post_event(&es->base, &device->base,
722 WL_INPUT_DEVICE_MOTION,
723 time, x, y, sx, sy);
724 break;
725
726 case WLSC_DEVICE_GRAB_MOVE:
727 es = device->pointer_focus;
728 es->x = x + device->grab_dx;
729 es->y = y + device->grab_dy;;
730 wl_surface_post_event(&es->base, &ec->shell,
731 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:
749 es = device->pointer_focus;
750
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
773 wl_surface_post_event(&es->base, &ec->shell,
774 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øgsberg27803c62010-06-06 22:23:21 -0400779 wlsc_matrix_init(&device->sprite->matrix);
780 wlsc_matrix_scale(&device->sprite->matrix, 64, 64, 1);
781 wlsc_matrix_translate(&device->sprite->matrix,
782 x - hotspot_x, y - hotspot_y, 0);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500783
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400784 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500785}
786
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500787void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500788notify_button(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400789 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500790{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400791 struct wlsc_surface *surface;
792 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500793
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400794 surface = device->pointer_focus;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400795 if (surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400796 if (state && device->grab == WLSC_DEVICE_GRAB_NONE) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400797 wlsc_surface_raise(surface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400798 device->grab = WLSC_DEVICE_GRAB_MOTION;
799 device->grab_time = time;
800 device->grab_x = device->x;
801 device->grab_y = device->y;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400802 wlsc_input_device_set_keyboard_focus(device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400803 surface, time);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500804 } else {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400805 device->grab = WLSC_DEVICE_GRAB_NONE;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500806 }
807
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400808 if (state && button == BTN_LEFT &&
809 device->grab == WLSC_DEVICE_GRAB_MOTION &&
810 (device->modifier_state & MODIFIER_SUPER))
811 shell_move(NULL, &compositor->shell,
812 &surface->base, device, time);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400813 else if (state && button == BTN_MIDDLE &&
814 device->grab == WLSC_DEVICE_GRAB_MOTION &&
815 (device->modifier_state & MODIFIER_SUPER))
816 shell_resize(NULL, &compositor->shell,
817 &surface->base, device, time,
818 WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT);
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400819 else
820 wl_surface_post_event(&surface->base, &device->base,
821 WL_INPUT_DEVICE_BUTTON,
822 time, button, state);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500823
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400824 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500825 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500826}
827
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500828void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500829notify_key(struct wlsc_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400830 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500831{
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500832 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400833 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500834
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400835 switch (key | device->modifier_state) {
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400836 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400837 kill(0, SIGTERM);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500838 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500839 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500840
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400841 switch (key) {
842 case KEY_LEFTCTRL:
843 case KEY_RIGHTCTRL:
844 modifier = MODIFIER_CTRL;
845 break;
846
847 case KEY_LEFTALT:
848 case KEY_RIGHTALT:
849 modifier = MODIFIER_ALT;
850 break;
851
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400852 case KEY_LEFTMETA:
853 case KEY_RIGHTMETA:
854 modifier = MODIFIER_SUPER;
855 break;
856
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400857 default:
858 modifier = 0;
859 break;
860 }
861
862 if (state)
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400863 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400864 else
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -0400865 device->modifier_state &= ~modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400866
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500867 end = device->keys.data + device->keys.size;
868 for (k = device->keys.data; k < end; k++) {
869 if (*k == key)
870 *k = *--end;
871 }
872 device->keys.size = (void *) end - device->keys.data;
873 if (state) {
874 k = wl_array_add(&device->keys, sizeof *k);
875 *k = key;
876 }
Ray Strodee96dcb82008-12-20 02:00:49 -0500877
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500878 if (device->keyboard_focus != NULL)
879 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400880 &device->base,
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400881 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400882}
883
884static uint32_t
885get_time(void)
886{
887 struct timeval tv;
888
889 gettimeofday(&tv, NULL);
890
891 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500892}
893
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400894static void
895handle_surface_destroy(struct wlsc_listener *listener,
896 struct wlsc_surface *surface)
897{
898 struct wlsc_input_device *device =
899 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400900 struct wlsc_surface *focus;
901 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400902 uint32_t time = get_time();
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400903
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400904 if (device->keyboard_focus == surface)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400905 wlsc_input_device_set_keyboard_focus(device, NULL, time);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400906 if (device->pointer_focus == surface) {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400907 device->grab = WLSC_DEVICE_GRAB_NONE;
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400908 focus = pick_surface(device, &sx, &sy);
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400909 wlsc_input_device_set_pointer_focus(device, focus, time,
910 device->x, device->y,
911 sx, sy);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400912 fprintf(stderr, "lost pointer focus surface, reverting to %p\n", focus);
913 }
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400914}
915
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400916void
917wlsc_input_device_init(struct wlsc_input_device *device,
918 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500919{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500920 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500921 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500922 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500923 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500924 device->x = 100;
925 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500926 device->ec = ec;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400927 device->sprite = pointer_create(ec, device->x, device->y, 64, 64);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500928
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400929 device->listener.func = handle_surface_destroy;
930 wl_list_insert(ec->surface_destroy_listener_list.prev,
931 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500932 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500933}
934
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500935static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400936wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500937{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500938 struct wlsc_output *output =
939 container_of(global, struct wlsc_output, base);
940
941 wl_client_post_event(client, global,
942 WL_OUTPUT_GEOMETRY,
943 output->width, output->height);
944}
945
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400946static const char vertex_shader[] =
947 "uniform mat4 proj;\n"
948 "attribute vec4 position;\n"
949 "attribute vec2 texcoord;\n"
950 "varying vec2 v_texcoord;\n"
951 "void main()\n"
952 "{\n"
953 " gl_Position = proj * position;\n"
954 " v_texcoord = texcoord;\n"
955 "}\n";
956
957static const char fragment_shader[] =
958 /* "precision mediump float;\n" */
959 "varying vec2 v_texcoord;\n"
960 "uniform sampler2D tex;\n"
961 "void main()\n"
962 "{\n"
963 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
964 "}\n";
965
Kristian Høgsberga9468212010-06-14 21:03:11 -0400966static int
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400967init_shaders(struct wlsc_compositor *ec)
968{
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400969 GLuint v, f, program;
970 const char *p;
971 char msg[512];
972 GLfloat vertices[4 * 5];
973 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400974
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400975 p = vertex_shader;
976 v = glCreateShader(GL_VERTEX_SHADER);
977 glShaderSource(v, 1, &p, NULL);
978 glCompileShader(v);
979 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
980 if (!status) {
981 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
982 fprintf(stderr, "vertex shader info: %s\n", msg);
983 return -1;
984 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400985
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400986 p = fragment_shader;
987 f = glCreateShader(GL_FRAGMENT_SHADER);
988 glShaderSource(f, 1, &p, NULL);
989 glCompileShader(f);
990 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
991 if (!status) {
992 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
993 fprintf(stderr, "fragment shader info: %s\n", msg);
994 return -1;
995 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400996
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400997 program = glCreateProgram();
998 glAttachShader(program, v);
999 glAttachShader(program, f);
1000 glBindAttribLocation(program, 0, "position");
1001 glBindAttribLocation(program, 1, "texcoord");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001002
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001003 glLinkProgram(program);
1004 glGetProgramiv(program, GL_LINK_STATUS, &status);
1005 if (!status) {
1006 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1007 fprintf(stderr, "link info: %s\n", msg);
1008 return -1;
1009 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001010
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001011 glUseProgram(program);
1012 ec->proj_uniform = glGetUniformLocation(program, "proj");
1013 ec->tex_uniform = glGetUniformLocation(program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001014
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001015 vertices[ 0] = 0.0;
1016 vertices[ 1] = 0.0;
1017 vertices[ 2] = 0.0;
1018 vertices[ 3] = 0.0;
1019 vertices[ 4] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001020
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001021 vertices[ 5] = 0.0;
1022 vertices[ 6] = 1.0;
1023 vertices[ 7] = 0.0;
1024 vertices[ 8] = 0.0;
1025 vertices[ 9] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001026
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001027 vertices[10] = 1.0;
1028 vertices[11] = 0.0;
1029 vertices[12] = 0.0;
1030 vertices[13] = 1.0;
1031 vertices[14] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001032
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001033 vertices[15] = 1.0;
1034 vertices[16] = 1.0;
1035 vertices[17] = 0.0;
1036 vertices[18] = 1.0;
1037 vertices[19] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001038
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001039 glGenBuffers(1, &ec->vbo);
1040 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
1041 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001042
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001043 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001044}
1045
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001046static const struct wl_interface visual_interface = {
1047 "visual", 1,
1048};
1049
1050static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001051add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001052{
1053 ec->argb_visual.base.interface = &visual_interface;
1054 ec->argb_visual.base.implementation = NULL;
1055 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001056 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001057
1058 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1059 ec->premultiplied_argb_visual.base.implementation = NULL;
1060 wl_display_add_object(ec->wl_display,
1061 &ec->premultiplied_argb_visual.base);
1062 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001063 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001064
1065 ec->rgb_visual.base.interface = &visual_interface;
1066 ec->rgb_visual.base.implementation = NULL;
1067 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001068 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1069}
1070
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001071void
1072wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1073 int x, int y, int width, int height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001074{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001075 output->compositor = c;
1076 output->x = x;
1077 output->y = y;
1078 output->width = width;
1079 output->height = height;
1080
1081 output->background =
1082 background_create(output, option_background);
1083
1084 wlsc_matrix_init(&output->matrix);
1085 wlsc_matrix_translate(&output->matrix,
1086 -output->x - output->width / 2.0,
1087 -output->y - output->height / 2.0, 0);
1088 wlsc_matrix_scale(&output->matrix,
1089 2.0 / output->width, 2.0 / output->height, 1);
1090
1091 output->base.interface = &wl_output_interface;
1092 wl_display_add_object(c->wl_display, &output->base);
1093 wl_display_add_global(c->wl_display, &output->base,
1094 wlsc_output_post_geometry);
1095}
1096
1097int
1098wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1099{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001100 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001101
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001102 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001103
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001104 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001105
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001106 ec->shell.interface = &wl_shell_interface;
1107 ec->shell.implementation = (void (**)(void)) &shell_interface;
1108 wl_display_add_object(display, &ec->shell);
1109 if (wl_display_add_global(display, &ec->shell, NULL))
1110 return -1;
1111
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001112 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001113
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001114 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001115 wl_list_init(&ec->input_device_list);
1116 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001117 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001118
1119 screenshooter_create(ec);
1120
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001121 glGenFramebuffers(1, &ec->fbo);
1122 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1123 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001124 if (init_shaders(ec) < 0)
1125 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001126
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001127 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001128 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001129 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001130
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001131 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001132}
1133
1134/* The plan here is to generate a random anonymous socket name and
1135 * advertise that through a service on the session dbus.
1136 */
1137static const char socket_name[] = "\0wayland";
1138
1139int main(int argc, char *argv[])
1140{
1141 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001142 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001143 GError *error = NULL;
1144 GOptionContext *context;
1145
1146 context = g_option_context_new(NULL);
1147 g_option_context_add_main_entries(context, option_entries, "Wayland");
1148 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1149 fprintf(stderr, "option parsing failed: %s\n", error->message);
1150 exit(EXIT_FAILURE);
1151 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001152
1153 display = wl_display_create();
1154
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001155 if (getenv("DISPLAY"))
1156 ec = x11_compositor_create(display);
1157 else
1158 ec = drm_compositor_create(display);
1159
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001160 if (ec == NULL) {
1161 fprintf(stderr, "failed to create compositor\n");
1162 exit(EXIT_FAILURE);
1163 }
1164
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001165 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001166 fprintf(stderr, "failed to add socket: %m\n");
1167 exit(EXIT_FAILURE);
1168 }
1169
1170 wl_display_run(display);
1171
1172 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001173}