blob: 6b42322a9c02a9dcd5f9a5e08fb8d5c8fff18fb2 [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;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050058 window_key_handler_t key_handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050059 void *user_data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050060};
61
Kristian Høgsberge4feb562008-11-08 18:53:37 -050062static void
63rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
64{
65 cairo_move_to(cr, x0, y0 + radius);
66 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
67 cairo_line_to(cr, x1 - radius, y0);
68 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
69 cairo_line_to(cr, x1, y1 - radius);
70 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
71 cairo_line_to(cr, x0 + radius, y1);
72 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
73 cairo_close_path(cr);
74}
75
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050076void
77window_draw(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -050078{
79 cairo_surface_t *surface;
80 cairo_t *cr;
Kristian Høgsberg40979232008-11-25 22:40:39 -050081 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -050082 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -050083 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050084
85 surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
Kristian Høgsberg40979232008-11-25 22:40:39 -050086 window->width + window->margin * 2,
87 window->height + window->margin * 2);
Kristian Høgsberg61017b12008-11-02 18:51:48 -050088
Kristian Høgsberge4feb562008-11-08 18:53:37 -050089 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -050090 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -050091 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
92
Kristian Høgsberg61017b12008-11-02 18:51:48 -050093 cr = cairo_create(surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050094
Kristian Høgsberg40979232008-11-25 22:40:39 -050095 cairo_translate(cr, window->margin + 7, window->margin + 5);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050096 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -050097 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg8c304f62008-11-10 10:46:53 -050098 rounded_rect(cr, 0, 0, window->width, window->height, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050099 cairo_fill(cr);
Kristian Høgsberg87330262008-11-17 22:23:55 -0500100 blur_surface(surface, 24 + radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500101
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500102 cairo_translate(cr, -7, -5);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500103 cairo_set_line_width (cr, border);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500104 rounded_rect(cr, 1, 1, window->width - 1, window->height - 1, radius);
105 cairo_set_source(cr, outline);
106 cairo_stroke(cr);
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500107 rounded_rect(cr, 2, 2, window->width - 2, window->height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500108 cairo_set_source(cr, bright);
109 cairo_stroke(cr);
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500110 rounded_rect(cr, 3, 3, window->width - 2, window->height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500111 cairo_set_source(cr, dim);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500112 cairo_stroke(cr);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500113
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500114 rounded_rect(cr, 2, 2, window->width - 2, window->height - 2, radius - 1);
115 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500116 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.4);
117 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.7);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500118 cairo_set_source(cr, gradient);
119 cairo_fill(cr);
120 cairo_pattern_destroy(gradient);
121
122 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
123 cairo_move_to(cr, 10, 50);
124 cairo_line_to(cr, window->width - 10, 50);
125 cairo_line_to(cr, window->width - 10, window->height - 10);
126 cairo_line_to(cr, 10, window->height - 10);
127 cairo_close_path(cr);
128 cairo_set_source(cr, dim);
129 cairo_stroke(cr);
130
131 cairo_move_to(cr, 11, 51);
132 cairo_line_to(cr, window->width - 10, 51);
133 cairo_line_to(cr, window->width - 10, window->height - 10);
134 cairo_line_to(cr, 11, window->height - 10);
135 cairo_close_path(cr);
136 cairo_set_source(cr, bright);
137 cairo_stroke(cr);
138
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500139 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
140 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500141 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500142 cairo_move_to(cr, (window->width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500143 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
144 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
145 cairo_set_line_width (cr, 4);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500146 cairo_text_path(cr, window->title);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500147 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
148 cairo_stroke_preserve(cr);
149 cairo_set_source_rgb(cr, 1, 1, 1);
150 cairo_fill(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500151 cairo_destroy(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500152
Kristian Høgsberg40979232008-11-25 22:40:39 -0500153 window->buffer = buffer_create_from_cairo_surface(window->fd, surface);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500154 cairo_surface_destroy(surface);
155
Kristian Høgsberg40979232008-11-25 22:40:39 -0500156 wl_surface_attach(window->surface,
157 window->buffer->name,
158 window->buffer->width,
159 window->buffer->height,
160 window->buffer->stride);
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500161
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500162 wl_surface_map(window->surface,
163 window->x - window->margin,
164 window->y - window->margin,
165 window->width + 2 * window->margin,
166 window->height + 2 * window->margin);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500167}
168
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500169enum window_state {
170 WINDOW_STABLE,
171 WINDOW_MOVING,
172 WINDOW_RESIZING_UPPER_LEFT,
173 WINDOW_RESIZING_UPPER_RIGHT,
174 WINDOW_RESIZING_LOWER_LEFT,
175 WINDOW_RESIZING_LOWER_RIGHT
176};
177
178enum location {
179 LOCATION_INTERIOR,
180 LOCATION_UPPER_LEFT,
181 LOCATION_UPPER_RIGHT,
182 LOCATION_LOWER_LEFT,
183 LOCATION_LOWER_RIGHT,
184 LOCATION_OUTSIDE
185};
186
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500187static void
188event_handler(struct wl_display *display,
Kristian Høgsberg40979232008-11-25 22:40:39 -0500189 uint32_t object, uint32_t opcode,
190 uint32_t size, uint32_t *p, void *data)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500191{
192 struct window *window = data;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500193 struct rectangle rectangle;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500194 int location;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500195 int grip_size = 16;
196
Kristian Høgsberg40979232008-11-25 22:40:39 -0500197 /* FIXME: Object ID 1 is the display, for anything else we
198 * assume it's an input device. */
199 if (object == 1 && opcode == 3) {
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500200 uint32_t key = p[0];
201
202 /* Ignore acknowledge events for window move requests. */
203 if (key != 0)
204 return;
205
Kristian Høgsberg40979232008-11-25 22:40:39 -0500206 /* The acknowledge event means that the server
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500207 * processed our last commit request and we can now
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500208 * safely free the old window buffer if we resized and
209 * render the next frame into our back buffer.. */
210
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500211 if (window->buffer != NULL) {
Kristian Høgsberg40979232008-11-25 22:40:39 -0500212 buffer_destroy(window->buffer, window->fd);
213 window->buffer = NULL;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500214 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500215 if (window->acknowledge_handler)
216 (*window->acknowledge_handler)(window, key,
217 window->user_data);
218
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500219 } else if (object == 1 && opcode == 4) {
220 /* The frame event means that the previous frame was
221 * composited, and we can now send the request to copy
222 * the frame we've rendered in the mean time into the
223 * servers surface buffer. */
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500224 if (window->frame_handler)
225 (*window->frame_handler)(window, p[0], p[1],
226 window->user_data);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500227 } else if (object == 1) {
228 fprintf(stderr, "unexpected event from display: %d\n",
229 opcode);
230 exit(-1);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500231 } else if (opcode == 0) {
232 int x = p[0], y = p[1];
233
234 window->last_x = x;
235 window->last_y = y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500236 switch (window->state) {
237 case WINDOW_MOVING:
Kristian Høgsberg40979232008-11-25 22:40:39 -0500238 window->x = window->drag_x + x;
239 window->y = window->drag_y + y;
240 wl_surface_map(window->surface,
241 window->x - window->margin,
242 window->y - window->margin,
243 window->width + 2 * window->margin,
244 window->height + 2 * window->margin);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500245 wl_display_commit(window->display, 1);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500246 break;
247 case WINDOW_RESIZING_LOWER_RIGHT:
Kristian Høgsberg40979232008-11-25 22:40:39 -0500248 window->width = window->drag_x + x;
249 window->height = window->drag_y + y;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500250
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500251 window_get_child_rectangle(window, &rectangle);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500252 if (window->resize_handler)
253 (*window->resize_handler)(window,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500254 window->user_data);
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500255
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500256 break;
257 }
Kristian Høgsberg40979232008-11-25 22:40:39 -0500258 } else if (opcode == 1) {
259 int button = p[0], state = p[1];
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500260
Kristian Høgsberg40979232008-11-25 22:40:39 -0500261 if (window->x + window->width - grip_size <= window->last_x &&
262 window->last_x < window->x + window->width &&
263 window->y + window->height - grip_size <= window->last_y &&
264 window->last_y < window->y + window->height) {
265 location = LOCATION_LOWER_RIGHT;
266 } else if (window->x <= window->last_x &&
267 window->last_x < window->x + window->width &&
268 window->y <= window->last_y &&
269 window->last_y < window->y + window->height) {
270 location = LOCATION_INTERIOR;
271 } else {
272 location = LOCATION_OUTSIDE;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500273 }
Kristian Høgsberg40979232008-11-25 22:40:39 -0500274
275 if (button == 0 && state == 1) {
276 switch (location) {
277 case LOCATION_INTERIOR:
278 window->drag_x = window->x - window->last_x;
279 window->drag_y = window->y - window->last_y;
280 window->state = WINDOW_MOVING;
281 break;
282 case LOCATION_LOWER_RIGHT:
283 window->drag_x = window->width - window->last_x;
284 window->drag_y = window->height - window->last_y;
285 window->state = WINDOW_RESIZING_LOWER_RIGHT;
286 break;
287 default:
288 window->state = WINDOW_STABLE;
289 break;
290 }
291 } else if (button == 0 && state == 0) {
292 window->state = WINDOW_STABLE;
293 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500294 } else if (opcode == 2) {
295 if (window->key_handler)
296 (*window->key_handler)(window, p[0], p[1],
297 window->user_data);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500298 }
299}
300
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500301void
302window_get_child_rectangle(struct window *window,
303 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500304{
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500305 rectangle->x = 10;
306 rectangle->y = 50;
307 rectangle->width = window->width - 20;
308 rectangle->height = window->height - 60;
309}
310
311void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500312window_set_child_size(struct window *window,
313 struct rectangle *rectangle)
314{
315 window->width = rectangle->width + 20;
316 window->height = rectangle->height + 60;
317}
318
319void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500320window_copy(struct window *window,
321 struct rectangle *rectangle,
322 uint32_t name, uint32_t stride)
323{
324 wl_surface_copy(window->surface,
325 window->margin + rectangle->x,
326 window->margin + rectangle->y,
327 name, stride,
328 0, 0, rectangle->width, rectangle->height);
329}
330
331void
332window_set_resize_handler(struct window *window,
333 window_resize_handler_t handler, void *data)
334{
335 window->resize_handler = handler;
336 window->user_data = data;
337}
338
339void
340window_set_frame_handler(struct window *window,
341 window_frame_handler_t handler, void *data)
342{
343 window->frame_handler = handler;
344 window->user_data = data;
345}
346
347void
348window_set_acknowledge_handler(struct window *window,
349 window_acknowledge_handler_t handler, void *data)
350{
351 window->acknowledge_handler = handler;
352 window->user_data = data;
353}
354
355void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500356window_set_key_handler(struct window *window,
357 window_key_handler_t handler, void *data)
358{
359 window->key_handler = handler;
360 window->user_data = data;
361}
362
363void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500364window_set_minimum_size(struct window *window, uint32_t width, int32_t height)
365{
366 window->minimum_width = width;
367 window->minimum_height = height;
368}
369
370struct window *
371window_create(struct wl_display *display, int fd,
372 const char *title,
373 int32_t x, int32_t y, int32_t width, int32_t height)
374{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500375 struct window *window;
376
377 window = malloc(sizeof *window);
378 if (window == NULL)
379 return NULL;
380
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500381 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500382 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500383 window->title = strdup(title);
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500384 window->surface = wl_display_create_surface(display);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500385 window->x = x;
386 window->y = y;
387 window->minimum_width = 100;
388 window->minimum_height = 100;
389 window->width = width;
390 window->height = height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500391 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500392 window->state = WINDOW_STABLE;
393 window->fd = fd;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500394
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500395 wl_display_set_event_handler(display, event_handler, window);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500396
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500397 return window;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500398}