blob: 4f4b704669ec19e9025d42ed07c38a192cd79247 [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øgsberg61017b12008-11-02 18:51:48 -050033
34#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050035#include "wayland-glib.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050036
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050037#include "cairo-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050038
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050039#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050040
Kristian Høgsberg61017b12008-11-02 18:51:48 -050041struct window {
Kristian Høgsberg40979232008-11-25 22:40:39 -050042 struct wl_display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050043 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050044 const char *title;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050045 int x, y, width, height;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050046 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050047 int margin;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050048 int drag_x, drag_y, last_x, last_y;
49 int state;
50 uint32_t name;
51 int fd;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050052
53 struct buffer *buffer;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050054
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050055 window_resize_handler_t resize_handler;
56 window_frame_handler_t frame_handler;
57 window_acknowledge_handler_t acknowledge_handler;
58 void *user_data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050059};
60
Kristian Høgsberge4feb562008-11-08 18:53:37 -050061static void
62rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
63{
64 cairo_move_to(cr, x0, y0 + radius);
65 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
66 cairo_line_to(cr, x1 - radius, y0);
67 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
68 cairo_line_to(cr, x1, y1 - radius);
69 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
70 cairo_line_to(cr, x0 + radius, y1);
71 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
72 cairo_close_path(cr);
73}
74
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050075void
76window_draw(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -050077{
78 cairo_surface_t *surface;
79 cairo_t *cr;
Kristian Høgsberg40979232008-11-25 22:40:39 -050080 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -050081 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -050082 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050083
84 surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
Kristian Høgsberg40979232008-11-25 22:40:39 -050085 window->width + window->margin * 2,
86 window->height + window->margin * 2);
Kristian Høgsberg61017b12008-11-02 18:51:48 -050087
Kristian Høgsberge4feb562008-11-08 18:53:37 -050088 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -050089 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -050090 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
91
Kristian Høgsberg61017b12008-11-02 18:51:48 -050092 cr = cairo_create(surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050093
Kristian Høgsberg40979232008-11-25 22:40:39 -050094 cairo_translate(cr, window->margin + 7, window->margin + 5);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050095 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -050096 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg8c304f62008-11-10 10:46:53 -050097 rounded_rect(cr, 0, 0, window->width, window->height, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050098 cairo_fill(cr);
Kristian Høgsberg87330262008-11-17 22:23:55 -050099 blur_surface(surface, 24 + radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500100
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500101 cairo_translate(cr, -7, -5);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500102 cairo_set_line_width (cr, border);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500103 rounded_rect(cr, 1, 1, window->width - 1, window->height - 1, radius);
104 cairo_set_source(cr, outline);
105 cairo_stroke(cr);
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500106 rounded_rect(cr, 2, 2, window->width - 2, window->height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500107 cairo_set_source(cr, bright);
108 cairo_stroke(cr);
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500109 rounded_rect(cr, 3, 3, window->width - 2, window->height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500110 cairo_set_source(cr, dim);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500111 cairo_stroke(cr);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500112
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500113 rounded_rect(cr, 2, 2, window->width - 2, window->height - 2, radius - 1);
114 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500115 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.4);
116 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.7);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500117 cairo_set_source(cr, gradient);
118 cairo_fill(cr);
119 cairo_pattern_destroy(gradient);
120
121 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
122 cairo_move_to(cr, 10, 50);
123 cairo_line_to(cr, window->width - 10, 50);
124 cairo_line_to(cr, window->width - 10, window->height - 10);
125 cairo_line_to(cr, 10, window->height - 10);
126 cairo_close_path(cr);
127 cairo_set_source(cr, dim);
128 cairo_stroke(cr);
129
130 cairo_move_to(cr, 11, 51);
131 cairo_line_to(cr, window->width - 10, 51);
132 cairo_line_to(cr, window->width - 10, window->height - 10);
133 cairo_line_to(cr, 11, window->height - 10);
134 cairo_close_path(cr);
135 cairo_set_source(cr, bright);
136 cairo_stroke(cr);
137
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500138 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
139 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500140 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500141 cairo_move_to(cr, (window->width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500142 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
143 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
144 cairo_set_line_width (cr, 4);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500145 cairo_text_path(cr, window->title);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500146 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
147 cairo_stroke_preserve(cr);
148 cairo_set_source_rgb(cr, 1, 1, 1);
149 cairo_fill(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500150 cairo_destroy(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500151
Kristian Høgsberg40979232008-11-25 22:40:39 -0500152 window->buffer = buffer_create_from_cairo_surface(window->fd, surface);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500153 cairo_surface_destroy(surface);
154
Kristian Høgsberg40979232008-11-25 22:40:39 -0500155 wl_surface_attach(window->surface,
156 window->buffer->name,
157 window->buffer->width,
158 window->buffer->height,
159 window->buffer->stride);
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500160
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500161 wl_surface_map(window->surface,
162 window->x - window->margin,
163 window->y - window->margin,
164 window->width + 2 * window->margin,
165 window->height + 2 * window->margin);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500166}
167
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500168enum window_state {
169 WINDOW_STABLE,
170 WINDOW_MOVING,
171 WINDOW_RESIZING_UPPER_LEFT,
172 WINDOW_RESIZING_UPPER_RIGHT,
173 WINDOW_RESIZING_LOWER_LEFT,
174 WINDOW_RESIZING_LOWER_RIGHT
175};
176
177enum location {
178 LOCATION_INTERIOR,
179 LOCATION_UPPER_LEFT,
180 LOCATION_UPPER_RIGHT,
181 LOCATION_LOWER_LEFT,
182 LOCATION_LOWER_RIGHT,
183 LOCATION_OUTSIDE
184};
185
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500186static void
187event_handler(struct wl_display *display,
Kristian Høgsberg40979232008-11-25 22:40:39 -0500188 uint32_t object, uint32_t opcode,
189 uint32_t size, uint32_t *p, void *data)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500190{
191 struct window *window = data;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500192 int location;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500193 int grip_size = 16;
194
Kristian Høgsberg40979232008-11-25 22:40:39 -0500195 /* FIXME: Object ID 1 is the display, for anything else we
196 * assume it's an input device. */
197 if (object == 1 && opcode == 3) {
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500198 uint32_t key = p[0];
199
200 /* Ignore acknowledge events for window move requests. */
201 if (key != 0)
202 return;
203
Kristian Høgsberg40979232008-11-25 22:40:39 -0500204 /* The acknowledge event means that the server
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500205 * processed our last commit request and we can now
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500206 * safely free the old window buffer if we resized and
207 * render the next frame into our back buffer.. */
208
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500209 if (window->buffer != NULL) {
Kristian Høgsberg40979232008-11-25 22:40:39 -0500210 buffer_destroy(window->buffer, window->fd);
211 window->buffer = NULL;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500212 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500213 if (window->acknowledge_handler)
214 (*window->acknowledge_handler)(window, key,
215 window->user_data);
216
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500217 } else if (object == 1 && opcode == 4) {
218 /* The frame event means that the previous frame was
219 * composited, and we can now send the request to copy
220 * the frame we've rendered in the mean time into the
221 * servers surface buffer. */
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500222 if (window->frame_handler)
223 (*window->frame_handler)(window, p[0], p[1],
224 window->user_data);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500225 } else if (object == 1) {
226 fprintf(stderr, "unexpected event from display: %d\n",
227 opcode);
228 exit(-1);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500229 } else if (opcode == 0) {
230 int x = p[0], y = p[1];
231
232 window->last_x = x;
233 window->last_y = y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500234 switch (window->state) {
235 case WINDOW_MOVING:
Kristian Høgsberg40979232008-11-25 22:40:39 -0500236 window->x = window->drag_x + x;
237 window->y = window->drag_y + y;
238 wl_surface_map(window->surface,
239 window->x - window->margin,
240 window->y - window->margin,
241 window->width + 2 * window->margin,
242 window->height + 2 * window->margin);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500243 wl_display_commit(window->display, 1);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500244 break;
245 case WINDOW_RESIZING_LOWER_RIGHT:
Kristian Høgsberg40979232008-11-25 22:40:39 -0500246 window->width = window->drag_x + x;
247 window->height = window->drag_y + y;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500248 if (window->width < window->minimum_width)
249 window->width = window->minimum_width;
250 if (window->height < window->minimum_height)
251 window->height = window->minimum_height;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500252
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500253 if (window->resize_handler)
254 (*window->resize_handler)(window,
255 window->width,
256 window->height,
257 window->user_data);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500258 break;
259 }
Kristian Høgsberg40979232008-11-25 22:40:39 -0500260 } else if (opcode == 1) {
261 int button = p[0], state = p[1];
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500262
Kristian Høgsberg40979232008-11-25 22:40:39 -0500263 if (window->x + window->width - grip_size <= window->last_x &&
264 window->last_x < window->x + window->width &&
265 window->y + window->height - grip_size <= window->last_y &&
266 window->last_y < window->y + window->height) {
267 location = LOCATION_LOWER_RIGHT;
268 } else if (window->x <= window->last_x &&
269 window->last_x < window->x + window->width &&
270 window->y <= window->last_y &&
271 window->last_y < window->y + window->height) {
272 location = LOCATION_INTERIOR;
273 } else {
274 location = LOCATION_OUTSIDE;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500275 }
Kristian Høgsberg40979232008-11-25 22:40:39 -0500276
277 if (button == 0 && state == 1) {
278 switch (location) {
279 case LOCATION_INTERIOR:
280 window->drag_x = window->x - window->last_x;
281 window->drag_y = window->y - window->last_y;
282 window->state = WINDOW_MOVING;
283 break;
284 case LOCATION_LOWER_RIGHT:
285 window->drag_x = window->width - window->last_x;
286 window->drag_y = window->height - window->last_y;
287 window->state = WINDOW_RESIZING_LOWER_RIGHT;
288 break;
289 default:
290 window->state = WINDOW_STABLE;
291 break;
292 }
293 } else if (button == 0 && state == 0) {
294 window->state = WINDOW_STABLE;
295 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500296 }
297}
298
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500299void
300window_get_child_rectangle(struct window *window,
301 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500302{
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500303 rectangle->x = 10;
304 rectangle->y = 50;
305 rectangle->width = window->width - 20;
306 rectangle->height = window->height - 60;
307}
308
309void
310window_copy(struct window *window,
311 struct rectangle *rectangle,
312 uint32_t name, uint32_t stride)
313{
314 wl_surface_copy(window->surface,
315 window->margin + rectangle->x,
316 window->margin + rectangle->y,
317 name, stride,
318 0, 0, rectangle->width, rectangle->height);
319}
320
321void
322window_set_resize_handler(struct window *window,
323 window_resize_handler_t handler, void *data)
324{
325 window->resize_handler = handler;
326 window->user_data = data;
327}
328
329void
330window_set_frame_handler(struct window *window,
331 window_frame_handler_t handler, void *data)
332{
333 window->frame_handler = handler;
334 window->user_data = data;
335}
336
337void
338window_set_acknowledge_handler(struct window *window,
339 window_acknowledge_handler_t handler, void *data)
340{
341 window->acknowledge_handler = handler;
342 window->user_data = data;
343}
344
345void
346window_set_minimum_size(struct window *window, uint32_t width, int32_t height)
347{
348 window->minimum_width = width;
349 window->minimum_height = height;
350}
351
352struct window *
353window_create(struct wl_display *display, int fd,
354 const char *title,
355 int32_t x, int32_t y, int32_t width, int32_t height)
356{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500357 struct window *window;
358
359 window = malloc(sizeof *window);
360 if (window == NULL)
361 return NULL;
362
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500363 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500364 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500365 window->title = strdup(title);
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500366 window->surface = wl_display_create_surface(display);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500367 window->x = x;
368 window->y = y;
369 window->minimum_width = 100;
370 window->minimum_height = 100;
371 window->width = width;
372 window->height = height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500373 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500374 window->state = WINDOW_STABLE;
375 window->fd = fd;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500376
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500377 wl_display_set_event_handler(display, event_handler, window);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500378
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500379 return window;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500380}