blob: 8ec858ccc1b1a2da2eaff3932459ef5bd187d171 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg61017b12008-11-02 18:51:48 -050023#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <time.h>
31#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050032#include <glib.h>
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050033#include <cairo-drm.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050034
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050035#include <linux/input.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050037#include "wayland-glib.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050038
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050039#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050040
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050041struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050042 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050043 struct wl_compositor *compositor;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050044 struct wl_output *output;
45 struct wl_input_device *input_device;
46 struct rectangle screen_allocation;
47 cairo_drm_context_t *ctx;
48 int fd;
49};
50
51struct window {
52 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050053 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050054 const char *title;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050055 struct rectangle allocation, saved_allocation;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050056 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050057 int margin;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050058 int drag_x, drag_y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050059 int state;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050060 int fullscreen;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050061 struct wl_input_device *grab_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050062 uint32_t name;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050063
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050064 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050065
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050066 window_resize_handler_t resize_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050067 window_key_handler_t key_handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050068 void *user_data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050069};
70
Kristian Høgsberge4feb562008-11-08 18:53:37 -050071static void
72rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
73{
74 cairo_move_to(cr, x0, y0 + radius);
75 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
76 cairo_line_to(cr, x1 - radius, y0);
77 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
78 cairo_line_to(cr, x1, y1 - radius);
79 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
80 cairo_line_to(cr, x0 + radius, y1);
81 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
82 cairo_close_path(cr);
83}
84
Kristian Høgsberg0395f302008-12-22 12:14:50 -050085static void
86window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -050087{
Kristian Høgsberg61017b12008-11-02 18:51:48 -050088 cairo_t *cr;
Kristian Høgsberg40979232008-11-25 22:40:39 -050089 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -050090 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -050091 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050092 struct wl_visual *visual;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050093 int width, height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050094
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050095 window->cairo_surface =
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050096 cairo_drm_surface_create(window->display->ctx,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050097 CAIRO_CONTENT_COLOR_ALPHA,
98 window->allocation.width,
99 window->allocation.height);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500100
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500101 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500102 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500103 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
104
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500105 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500106
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500107 width = window->allocation.width - window->margin * 2;
108 height = window->allocation.height - window->margin * 2;
109
Kristian Høgsberg40979232008-11-25 22:40:39 -0500110 cairo_translate(cr, window->margin + 7, window->margin + 5);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500111 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500112 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500113 rounded_rect(cr, 0, 0, width, height, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500114 cairo_fill(cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500115
116#ifdef SLOW_BUT_PWETTY
117 /* FIXME: Aw, pretty drop shadows now have to fallback to sw.
118 * Ideally we should have convolution filters in cairo, but we
119 * can also fallback to compositing the shadow image a bunch
120 * of times according to the blur kernel. */
121 {
122 cairo_surface_t *map;
123
124 map = cairo_drm_surface_map(window->cairo_surface);
125 blur_surface(map);
126 cairo_drm_surface_unmap(window->cairo_surface, map);
127 }
128#endif
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500129
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500130 cairo_translate(cr, -7, -5);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500131 cairo_set_line_width (cr, border);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500132 rounded_rect(cr, 1, 1, width - 1, height - 1, radius);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500133 cairo_set_source(cr, outline);
134 cairo_stroke(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500135 rounded_rect(cr, 2, 2, width - 2, height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500136 cairo_set_source(cr, bright);
137 cairo_stroke(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500138 rounded_rect(cr, 3, 3, width - 2, height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500139 cairo_set_source(cr, dim);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500140 cairo_stroke(cr);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500141
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500142 rounded_rect(cr, 2, 2, width - 2, height - 2, radius - 1);
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500143 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500144 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.4);
145 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.7);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500146 cairo_set_source(cr, gradient);
147 cairo_fill(cr);
148 cairo_pattern_destroy(gradient);
149
150 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
151 cairo_move_to(cr, 10, 50);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500152 cairo_line_to(cr, width - 10, 50);
153 cairo_line_to(cr, width - 10, height - 10);
154 cairo_line_to(cr, 10, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500155 cairo_close_path(cr);
156 cairo_set_source(cr, dim);
157 cairo_stroke(cr);
158
159 cairo_move_to(cr, 11, 51);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500160 cairo_line_to(cr, width - 10, 51);
161 cairo_line_to(cr, width - 10, height - 10);
162 cairo_line_to(cr, 11, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500163 cairo_close_path(cr);
164 cairo_set_source(cr, bright);
165 cairo_stroke(cr);
166
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500167 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
168 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500169 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500170 cairo_move_to(cr, (width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500171 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
172 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
173 cairo_set_line_width (cr, 4);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500174 cairo_text_path(cr, window->title);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500175 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
176 cairo_stroke_preserve(cr);
177 cairo_set_source_rgb(cr, 1, 1, 1);
178 cairo_fill(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500179 cairo_destroy(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500180
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500181 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500182 wl_surface_attach(window->surface,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500183 cairo_drm_surface_get_name(window->cairo_surface),
184 window->allocation.width,
185 window->allocation.height,
186 cairo_drm_surface_get_stride(window->cairo_surface),
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500187 visual);
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500188
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500189 wl_surface_map(window->surface,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500190 window->allocation.x - window->margin,
191 window->allocation.y - window->margin,
192 window->allocation.width,
193 window->allocation.height);
194}
195
196static void
197window_draw_fullscreen(struct window *window)
198{
199 struct wl_visual *visual;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500200
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500201 window->cairo_surface =
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500202 cairo_drm_surface_create(window->display->ctx,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500203 CAIRO_CONTENT_COLOR_ALPHA,
204 window->allocation.width,
205 window->allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500206
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500207 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500208 wl_surface_attach(window->surface,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500209 cairo_drm_surface_get_name(window->cairo_surface),
210 window->allocation.width,
211 window->allocation.height,
212 cairo_drm_surface_get_stride(window->cairo_surface),
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500213 visual);
214
215 wl_surface_map(window->surface,
216 window->allocation.x,
217 window->allocation.y,
218 window->allocation.width,
219 window->allocation.height);
220}
221
222void
223window_draw(struct window *window)
224{
225 if (window->fullscreen)
226 window_draw_fullscreen(window);
227 else
228 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500229}
230
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500231static void
232window_handle_acknowledge(void *data,
233 struct wl_compositor *compositor,
234 uint32_t key, uint32_t frame)
235{
236 struct window *window = data;
237
238 /* The acknowledge event means that the server
239 * processed our last commit request and we can now
240 * safely free the old window buffer if we resized and
241 * render the next frame into our back buffer.. */
242
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500243 if (key == 0 && window->cairo_surface != NULL) {
244 cairo_surface_destroy(window->cairo_surface);
245 window->cairo_surface = NULL;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500246 }
247}
248
249static void
250window_handle_frame(void *data,
251 struct wl_compositor *compositor,
252 uint32_t frame, uint32_t timestamp)
253{
254}
255
256static const struct wl_compositor_listener compositor_listener = {
257 window_handle_acknowledge,
258 window_handle_frame,
259};
260
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500261enum window_state {
262 WINDOW_STABLE,
263 WINDOW_MOVING,
264 WINDOW_RESIZING_UPPER_LEFT,
265 WINDOW_RESIZING_UPPER_RIGHT,
266 WINDOW_RESIZING_LOWER_LEFT,
267 WINDOW_RESIZING_LOWER_RIGHT
268};
269
270enum location {
271 LOCATION_INTERIOR,
272 LOCATION_UPPER_LEFT,
273 LOCATION_UPPER_RIGHT,
274 LOCATION_LOWER_LEFT,
275 LOCATION_LOWER_RIGHT,
276 LOCATION_OUTSIDE
277};
278
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500279static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500280window_handle_motion(void *data, struct wl_input_device *input_device,
281 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500282{
283 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500284
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500285 switch (window->state) {
286 case WINDOW_MOVING:
287 if (window->fullscreen)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500288 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500289 if (window->grab_device != input_device)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500290 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500291 window->allocation.x = window->drag_x + x;
292 window->allocation.y = window->drag_y + y;
293 wl_surface_map(window->surface,
294 window->allocation.x - window->margin,
295 window->allocation.y - window->margin,
296 window->allocation.width,
297 window->allocation.height);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500298 wl_compositor_commit(window->display->compositor, 1);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500299 break;
300 case WINDOW_RESIZING_LOWER_RIGHT:
301 if (window->fullscreen)
302 break;
303 if (window->grab_device != input_device)
304 break;
305 window->allocation.width = window->drag_x + x;
306 window->allocation.height = window->drag_y + y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500307
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500308 if (window->resize_handler)
309 (*window->resize_handler)(window,
310 window->user_data);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500311
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500312 break;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500313 }
314}
315
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500316static void window_handle_button(void *data, struct wl_input_device *input_device,
317 uint32_t button, uint32_t state,
318 int32_t x, int32_t y, int32_t sx, int32_t sy)
319{
320 struct window *window = data;
321 int32_t left = window->allocation.x;
322 int32_t right = window->allocation.x +
323 window->allocation.width - window->margin * 2;
324 int32_t top = window->allocation.y;
325 int32_t bottom = window->allocation.y +
326 window->allocation.height - window->margin * 2;
327 int grip_size = 16, location;
328
329 if (right - grip_size <= x && x < right &&
330 bottom - grip_size <= y && y < bottom) {
331 location = LOCATION_LOWER_RIGHT;
332 } else if (left <= x && x < right && top <= y && y < bottom) {
333 location = LOCATION_INTERIOR;
334 } else {
335 location = LOCATION_OUTSIDE;
336 }
337
338 if (button == BTN_LEFT && state == 1) {
339 switch (location) {
340 case LOCATION_INTERIOR:
341 window->drag_x = window->allocation.x - x;
342 window->drag_y = window->allocation.y - y;
343 window->state = WINDOW_MOVING;
344 window->grab_device = input_device;
345 break;
346 case LOCATION_LOWER_RIGHT:
347 window->drag_x = window->allocation.width - x;
348 window->drag_y = window->allocation.height - y;
349 window->state = WINDOW_RESIZING_LOWER_RIGHT;
350 window->grab_device = input_device;
351 break;
352 default:
353 window->state = WINDOW_STABLE;
354 break;
355 }
356 } else if (button == BTN_LEFT &&
357 state == 0 && window->grab_device == input_device) {
358 window->state = WINDOW_STABLE;
359 }
360}
361
362static void
363window_handle_key(void *data, struct wl_input_device *input_device,
364 uint32_t button, uint32_t state)
365{
366 struct window *window = data;
367
368 if (window->key_handler)
369 (*window->key_handler)(window, button, state,
370 window->user_data);
371}
372
373static const struct wl_input_device_listener input_device_listener = {
374 window_handle_motion,
375 window_handle_button,
376 window_handle_key,
377};
378
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500379void
380window_get_child_rectangle(struct window *window,
381 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500382{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500383 if (window->fullscreen) {
384 *rectangle = window->allocation;
385 } else {
386 rectangle->x = window->margin + 10;
387 rectangle->y = window->margin + 50;
388 rectangle->width = window->allocation.width - 20 - window->margin * 2;
389 rectangle->height = window->allocation.height - 60 - window->margin * 2;
390 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500391}
392
393void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500394window_set_child_size(struct window *window,
395 struct rectangle *rectangle)
396{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500397 if (!window->fullscreen) {
398 window->allocation.width = rectangle->width + 20 + window->margin * 2;
399 window->allocation.height = rectangle->height + 60 + window->margin * 2;
400 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500401}
402
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500403cairo_surface_t *
404window_create_surface(struct window *window,
405 struct rectangle *rectangle)
406{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500407 return cairo_drm_surface_create(window->display->ctx,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500408 CAIRO_CONTENT_COLOR_ALPHA,
409 rectangle->width,
410 rectangle->height);
411}
412
Kristian Høgsberg22106762008-12-08 13:50:07 -0500413void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500414window_copy(struct window *window,
415 struct rectangle *rectangle,
416 uint32_t name, uint32_t stride)
417{
418 wl_surface_copy(window->surface,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500419 rectangle->x,
420 rectangle->y,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500421 name, stride,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500422 0, 0,
423 rectangle->width,
424 rectangle->height);
425}
426
427void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500428window_copy_surface(struct window *window,
429 struct rectangle *rectangle,
430 cairo_surface_t *surface)
431{
432 wl_surface_copy(window->surface,
433 rectangle->x,
434 rectangle->y,
435 cairo_drm_surface_get_name(surface),
436 cairo_drm_surface_get_stride(surface),
437 0, 0,
438 rectangle->width,
439 rectangle->height);
440}
441
442void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500443window_set_fullscreen(struct window *window, int fullscreen)
444{
445 window->fullscreen = fullscreen;
446 if (window->fullscreen) {
447 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500448 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500449 } else {
450 window->allocation = window->saved_allocation;
451 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500452}
453
454void
455window_set_resize_handler(struct window *window,
456 window_resize_handler_t handler, void *data)
457{
458 window->resize_handler = handler;
459 window->user_data = data;
460}
461
462void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500463window_set_key_handler(struct window *window,
464 window_key_handler_t handler, void *data)
465{
466 window->key_handler = handler;
467 window->user_data = data;
468}
469
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500470struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500471window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500472 int32_t x, int32_t y, int32_t width, int32_t height)
473{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500474 struct window *window;
475
476 window = malloc(sizeof *window);
477 if (window == NULL)
478 return NULL;
479
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500480 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500481 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500482 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500483 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500484 window->allocation.x = x;
485 window->allocation.y = y;
486 window->allocation.width = width;
487 window->allocation.height = height;
488 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500489 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500490 window->state = WINDOW_STABLE;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500491
492 wl_compositor_add_listener(display->compositor,
493 &compositor_listener, window);
494
495 wl_input_device_add_listener(display->input_device,
496 &input_device_listener, window);
497
498 return window;
499}
500
501static void
502display_handle_geometry(void *data,
503 struct wl_output *output,
504 int32_t width, int32_t height)
505{
506 struct display *display = data;
507
508 display->screen_allocation.x = 0;
509 display->screen_allocation.y = 0;
510 display->screen_allocation.width = width;
511 display->screen_allocation.height = height;
512}
513
514static const struct wl_output_listener output_listener = {
515 display_handle_geometry,
516};
517
518static void
519display_handle_global(struct wl_display *display,
520 struct wl_object *object, void *data)
521{
522 struct display *d = data;
523
524 if (wl_object_implements(object, "compositor", 1)) {
525 d->compositor = (struct wl_compositor *) object;
526 } else if (wl_object_implements(object, "output", 1)) {
527 d->output = (struct wl_output *) object;
528 wl_output_add_listener(d->output, &output_listener, d);
529 } else if (wl_object_implements(object, "input_device", 1)) {
530 d->input_device =(struct wl_input_device *) object;
531 }
532}
533
534struct display *
535display_create(struct wl_display *display, int fd)
536{
537 struct display *d;
538
539 d = malloc(sizeof *d);
540 if (d == NULL)
541 return NULL;
542
543 d->display = display;
544 d->ctx = cairo_drm_context_get_for_fd(fd);
545 if (d->ctx == NULL) {
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500546 fprintf(stderr, "failed to get cairo drm context\n");
547 return NULL;
548 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500549
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500550 /* Set up listener so we'll catch all events. */
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500551 wl_display_add_global_listener(display,
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500552 display_handle_global, d);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500553
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500554 /* Process connection events. */
555 wl_display_iterate(display, WL_DISPLAY_READABLE);
556
557 return d;
558}
559
560struct wl_compositor *
561display_get_compositor(struct display *display)
562{
563 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500564}