blob: e8a5cad62e1dbc0824ebf4c30ff3d19b24fabc4c [file] [log] [blame]
Jason Ekstrand01c9ec32013-10-13 19:08:39 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2012-2013 Collabora, Ltd.
4 * Copyright © 2013 Jason Ekstrand
5 *
Bryce Harrington6c6164c2015-06-11 14:20:17 -07006 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
Jason Ekstrand01c9ec32013-10-13 19:08:39 -050013 *
Bryce Harrington6c6164c2015-06-11 14:20:17 -070014 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
Jason Ekstrand01c9ec32013-10-13 19:08:39 -050026 */
27
28#include "config.h"
29
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030030#include <stdint.h>
Jason Ekstrand01c9ec32013-10-13 19:08:39 -050031#include <stdlib.h>
32#include <string.h>
33#include <wayland-util.h>
34#include <linux/input.h>
35
36#include "cairo-util.h"
Derek Foremane2772762018-02-06 15:18:38 -060037#include "shared/file-util.h"
Jason Ekstrand01c9ec32013-10-13 19:08:39 -050038
39enum frame_button_flags {
40 FRAME_BUTTON_ALIGN_RIGHT = 0x1,
41 FRAME_BUTTON_DECORATED = 0x2,
42 FRAME_BUTTON_CLICK_DOWN = 0x4,
43};
44
45struct frame_button {
46 struct frame *frame;
47 struct wl_list link; /* buttons_list */
48
49 cairo_surface_t *icon;
50 enum frame_button_flags flags;
51 int hover_count;
52 int press_count;
53
54 struct {
55 int x, y;
56 int width, height;
57 } allocation;
58
59 enum frame_status status_effect;
60};
61
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -050062struct frame_pointer_button {
63 struct wl_list link;
64 uint32_t button;
65 enum theme_location press_location;
66 struct frame_button *frame_button;
67};
68
Jason Ekstrand01c9ec32013-10-13 19:08:39 -050069struct frame_pointer {
70 struct wl_list link;
71 void *data;
72
73 int x, y;
74
75 struct frame_button *hover_button;
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -050076 struct wl_list down_buttons;
Jason Ekstrand01c9ec32013-10-13 19:08:39 -050077};
78
Jason Ekstrand3f66cf92013-10-13 19:08:40 -050079struct frame_touch {
80 struct wl_list link;
81 void *data;
82
83 int x, y;
84
85 struct frame_button *button;
86};
87
Jason Ekstrand01c9ec32013-10-13 19:08:39 -050088struct frame {
89 int32_t width, height;
90 char *title;
91 uint32_t flags;
92 struct theme *theme;
93
94 struct {
95 int32_t x, y;
96 int32_t width, height;
97 } interior;
98 int shadow_margin;
99 int opaque_margin;
100 int geometry_dirty;
101
Louis-Francis Ratté-Boulianne864e39b2017-11-13 16:20:55 -0500102 cairo_rectangle_int_t title_rect;
103
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500104 uint32_t status;
105
106 struct wl_list buttons;
107 struct wl_list pointers;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500108 struct wl_list touches;
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500109};
110
111static struct frame_button *
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +0100112frame_button_create_from_surface(struct frame *frame, cairo_surface_t *icon,
113 enum frame_status status_effect,
114 enum frame_button_flags flags)
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500115{
116 struct frame_button *button;
117
118 button = calloc(1, sizeof *button);
119 if (!button)
120 return NULL;
121
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +0100122 button->icon = icon;
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500123 button->frame = frame;
124 button->flags = flags;
125 button->status_effect = status_effect;
126
127 wl_list_insert(frame->buttons.prev, &button->link);
128
129 return button;
130}
131
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +0100132static struct frame_button *
133frame_button_create(struct frame *frame, const char *icon_name,
134 enum frame_status status_effect,
135 enum frame_button_flags flags)
136{
137 struct frame_button *button;
138 cairo_surface_t *icon;
139
140 icon = cairo_image_surface_create_from_png(icon_name);
141 if (cairo_surface_status(icon) != CAIRO_STATUS_SUCCESS)
142 goto error;
143
144 button = frame_button_create_from_surface(frame, icon, status_effect,
145 flags);
146 if (!button)
147 goto error;
148
149 return button;
150
151error:
152 cairo_surface_destroy(icon);
153 return NULL;
154}
155
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500156static void
157frame_button_destroy(struct frame_button *button)
158{
159 cairo_surface_destroy(button->icon);
160 free(button);
161}
162
163static void
164frame_button_enter(struct frame_button *button)
165{
166 if (!button->hover_count)
167 button->frame->status |= FRAME_STATUS_REPAINT;
168 button->hover_count++;
169}
170
171static void
172frame_button_leave(struct frame_button *button, struct frame_pointer *pointer)
173{
174 button->hover_count--;
175 if (!button->hover_count)
176 button->frame->status |= FRAME_STATUS_REPAINT;
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500177}
178
179static void
180frame_button_press(struct frame_button *button)
181{
182 if (!button->press_count)
183 button->frame->status |= FRAME_STATUS_REPAINT;
184 button->press_count++;
185
186 if (button->flags & FRAME_BUTTON_CLICK_DOWN)
187 button->frame->status |= button->status_effect;
188}
189
190static void
191frame_button_release(struct frame_button *button)
192{
193 button->press_count--;
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -0500194 if (button->press_count)
195 return;
196
197 button->frame->status |= FRAME_STATUS_REPAINT;
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500198
199 if (!(button->flags & FRAME_BUTTON_CLICK_DOWN))
200 button->frame->status |= button->status_effect;
201}
202
203static void
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -0500204frame_button_cancel(struct frame_button *button)
205{
206 button->press_count--;
207 if (!button->press_count)
208 button->frame->status |= FRAME_STATUS_REPAINT;
209}
210
211static void
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500212frame_button_repaint(struct frame_button *button, cairo_t *cr)
213{
214 int x, y;
215
216 if (!button->allocation.width)
217 return;
218 if (!button->allocation.height)
219 return;
220
221 x = button->allocation.x;
222 y = button->allocation.y;
223
224 cairo_save(cr);
225
226 if (button->flags & FRAME_BUTTON_DECORATED) {
227 cairo_set_line_width(cr, 1);
228
229 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
230 cairo_rectangle(cr, x, y, 25, 16);
231
232 cairo_stroke_preserve(cr);
233
234 if (button->press_count) {
235 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
236 } else if (button->hover_count) {
237 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
238 } else {
239 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
240 }
241
242 cairo_fill (cr);
243
244 x += 4;
245 }
246
247 cairo_set_source_surface(cr, button->icon, x, y);
248 cairo_paint(cr);
249
250 cairo_restore(cr);
251}
252
253static struct frame_pointer *
254frame_pointer_get(struct frame *frame, void *data)
255{
256 struct frame_pointer *pointer;
257
258 wl_list_for_each(pointer, &frame->pointers, link)
259 if (pointer->data == data)
260 return pointer;
261
262 pointer = calloc(1, sizeof *pointer);
263 if (!pointer)
264 return NULL;
265
266 pointer->data = data;
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -0500267 wl_list_init(&pointer->down_buttons);
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500268 wl_list_insert(&frame->pointers, &pointer->link);
269
270 return pointer;
271}
272
273static void
274frame_pointer_destroy(struct frame_pointer *pointer)
275{
276 wl_list_remove(&pointer->link);
277 free(pointer);
278}
279
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500280static struct frame_touch *
281frame_touch_get(struct frame *frame, void *data)
282{
283 struct frame_touch *touch;
284
285 wl_list_for_each(touch, &frame->touches, link)
286 if (touch->data == data)
287 return touch;
288
289 touch = calloc(1, sizeof *touch);
290 if (!touch)
291 return NULL;
292
293 touch->data = data;
294 wl_list_insert(&frame->touches, &touch->link);
295
296 return touch;
297}
298
299static void
300frame_touch_destroy(struct frame_touch *touch)
301{
302 wl_list_remove(&touch->link);
303 free(touch);
304}
305
U. Artie Eoff107de962014-01-17 11:19:46 -0800306void
307frame_destroy(struct frame *frame)
308{
309 struct frame_button *button, *next;
310 struct frame_touch *touch, *next_touch;
311 struct frame_pointer *pointer, *next_pointer;
312
313 wl_list_for_each_safe(button, next, &frame->buttons, link)
314 frame_button_destroy(button);
315
316 wl_list_for_each_safe(touch, next_touch, &frame->touches, link)
317 frame_touch_destroy(touch);
318
319 wl_list_for_each_safe(pointer, next_pointer, &frame->pointers, link)
320 frame_pointer_destroy(pointer);
321
322 free(frame->title);
323 free(frame);
324}
325
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500326struct frame *
327frame_create(struct theme *t, int32_t width, int32_t height, uint32_t buttons,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +0100328 const char *title, cairo_surface_t *icon)
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500329{
330 struct frame *frame;
331 struct frame_button *button;
332
333 frame = calloc(1, sizeof *frame);
334 if (!frame)
335 return NULL;
336
337 frame->width = width;
338 frame->height = height;
339 frame->flags = 0;
340 frame->theme = t;
341 frame->status = FRAME_STATUS_REPAINT;
342 frame->geometry_dirty = 1;
343
U. Artie Eoff107de962014-01-17 11:19:46 -0800344 wl_list_init(&frame->buttons);
345 wl_list_init(&frame->pointers);
346 wl_list_init(&frame->touches);
347
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500348 if (title) {
349 frame->title = strdup(title);
350 if (!frame->title)
351 goto free_frame;
352 }
353
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700354 if (title) {
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +0100355 if (icon) {
356 button = frame_button_create_from_surface(frame,
357 icon,
358 FRAME_STATUS_MENU,
359 FRAME_BUTTON_CLICK_DOWN);
360 } else {
Derek Foremane2772762018-02-06 15:18:38 -0600361 char *name = file_name_with_datadir("icon_window.png");
362
363 if (!name)
364 goto free_frame;
365
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +0100366 button = frame_button_create(frame,
Derek Foremane2772762018-02-06 15:18:38 -0600367 name,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +0100368 FRAME_STATUS_MENU,
369 FRAME_BUTTON_CLICK_DOWN);
Derek Foremane2772762018-02-06 15:18:38 -0600370 free(name);
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +0100371 }
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700372 if (!button)
373 goto free_frame;
374 }
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500375
376 if (buttons & FRAME_BUTTON_CLOSE) {
Derek Foremane2772762018-02-06 15:18:38 -0600377 char *name = file_name_with_datadir("sign_close.png");
378
379 if (!name)
380 goto free_frame;
381
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500382 button = frame_button_create(frame,
Derek Foremane2772762018-02-06 15:18:38 -0600383 name,
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500384 FRAME_STATUS_CLOSE,
385 FRAME_BUTTON_ALIGN_RIGHT |
386 FRAME_BUTTON_DECORATED);
Derek Foremane2772762018-02-06 15:18:38 -0600387 free(name);
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500388 if (!button)
389 goto free_frame;
390 }
391
392 if (buttons & FRAME_BUTTON_MAXIMIZE) {
Derek Foremane2772762018-02-06 15:18:38 -0600393 char *name = file_name_with_datadir("sign_maximize.png");
394
395 if (!name)
396 goto free_frame;
397
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500398 button = frame_button_create(frame,
Derek Foremane2772762018-02-06 15:18:38 -0600399 name,
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500400 FRAME_STATUS_MAXIMIZE,
401 FRAME_BUTTON_ALIGN_RIGHT |
402 FRAME_BUTTON_DECORATED);
Derek Foremane2772762018-02-06 15:18:38 -0600403 free(name);
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500404 if (!button)
405 goto free_frame;
406 }
407
408 if (buttons & FRAME_BUTTON_MINIMIZE) {
Derek Foremane2772762018-02-06 15:18:38 -0600409 char *name = file_name_with_datadir("sign_minimize.png");
410
411 if (!name)
412 goto free_frame;
413
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500414 button = frame_button_create(frame,
Derek Foremane2772762018-02-06 15:18:38 -0600415 name,
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500416 FRAME_STATUS_MINIMIZE,
417 FRAME_BUTTON_ALIGN_RIGHT |
418 FRAME_BUTTON_DECORATED);
Derek Foremane2772762018-02-06 15:18:38 -0600419 free(name);
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500420 if (!button)
421 goto free_frame;
422 }
423
424 return frame;
425
426free_frame:
U. Artie Eoff107de962014-01-17 11:19:46 -0800427 frame_destroy(frame);
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500428 return NULL;
429}
430
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500431int
432frame_set_title(struct frame *frame, const char *title)
433{
434 char *dup = NULL;
435
436 if (title) {
437 dup = strdup(title);
438 if (!dup)
439 return -1;
440 }
441
442 free(frame->title);
443 frame->title = dup;
444
Boyan Ding9c5aedf2014-07-04 15:19:23 +0800445 frame->geometry_dirty = 1;
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500446 frame->status |= FRAME_STATUS_REPAINT;
447
448 return 0;
449}
450
451void
Emmanuel Gil Peyrot44fc1be2018-01-24 23:29:53 +0100452frame_set_icon(struct frame *frame, cairo_surface_t *icon)
453{
454 struct frame_button *button;
455 wl_list_for_each(button, &frame->buttons, link) {
456 if (button->status_effect != FRAME_STATUS_MENU)
457 continue;
458 if (button->icon)
459 cairo_surface_destroy(button->icon);
460 button->icon = icon;
461 frame->status |= FRAME_STATUS_REPAINT;
462 }
463}
464
465void
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500466frame_set_flag(struct frame *frame, enum frame_flag flag)
467{
468 if (flag & FRAME_FLAG_MAXIMIZED && !(frame->flags & FRAME_FLAG_MAXIMIZED))
469 frame->geometry_dirty = 1;
470
471 frame->flags |= flag;
472 frame->status |= FRAME_STATUS_REPAINT;
473}
474
475void
476frame_unset_flag(struct frame *frame, enum frame_flag flag)
477{
478 if (flag & FRAME_FLAG_MAXIMIZED && frame->flags & FRAME_FLAG_MAXIMIZED)
479 frame->geometry_dirty = 1;
480
481 frame->flags &= ~flag;
482 frame->status |= FRAME_STATUS_REPAINT;
483}
484
485void
486frame_resize(struct frame *frame, int32_t width, int32_t height)
487{
488 frame->width = width;
489 frame->height = height;
490
491 frame->geometry_dirty = 1;
492 frame->status |= FRAME_STATUS_REPAINT;
493}
494
495void
496frame_resize_inside(struct frame *frame, int32_t width, int32_t height)
497{
498 struct theme *t = frame->theme;
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700499 int decoration_width, decoration_height, titlebar_height;
500
Boyan Dingc4902122014-07-04 15:19:22 +0800501 if (frame->title || !wl_list_empty(&frame->buttons))
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700502 titlebar_height = t->titlebar_height;
503 else
504 titlebar_height = t->width;
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500505
506 if (frame->flags & FRAME_FLAG_MAXIMIZED) {
507 decoration_width = t->width * 2;
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700508 decoration_height = t->width + titlebar_height;
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500509 } else {
510 decoration_width = (t->width + t->margin) * 2;
511 decoration_height = t->width +
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700512 titlebar_height + t->margin * 2;
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500513 }
514
515 frame_resize(frame, width + decoration_width,
516 height + decoration_height);
517}
518
519int32_t
520frame_width(struct frame *frame)
521{
522 return frame->width;
523}
524
525int32_t
526frame_height(struct frame *frame)
527{
528 return frame->height;
529}
530
531static void
532frame_refresh_geometry(struct frame *frame)
533{
534 struct frame_button *button;
535 struct theme *t = frame->theme;
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700536 int x_l, x_r, y, w, h, titlebar_height;
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500537 int32_t decoration_width, decoration_height;
538
539 if (!frame->geometry_dirty)
540 return;
541
Boyan Dingc4902122014-07-04 15:19:22 +0800542 if (frame->title || !wl_list_empty(&frame->buttons))
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700543 titlebar_height = t->titlebar_height;
544 else
545 titlebar_height = t->width;
546
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500547 if (frame->flags & FRAME_FLAG_MAXIMIZED) {
548 decoration_width = t->width * 2;
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700549 decoration_height = t->width + titlebar_height;
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500550
551 frame->interior.x = t->width;
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700552 frame->interior.y = titlebar_height;
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500553 frame->interior.width = frame->width - decoration_width;
554 frame->interior.height = frame->height - decoration_height;
555
556 frame->opaque_margin = 0;
557 frame->shadow_margin = 0;
558 } else {
559 decoration_width = (t->width + t->margin) * 2;
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700560 decoration_height = t->width + titlebar_height + t->margin * 2;
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500561
562 frame->interior.x = t->width + t->margin;
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700563 frame->interior.y = titlebar_height + t->margin;
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500564 frame->interior.width = frame->width - decoration_width;
565 frame->interior.height = frame->height - decoration_height;
566
567 frame->opaque_margin = t->margin + t->frame_radius;
568 frame->shadow_margin = t->margin;
569 }
570
571 x_r = frame->width - t->width - frame->shadow_margin;
572 x_l = t->width + frame->shadow_margin;
573 y = t->width + frame->shadow_margin;
574 wl_list_for_each(button, &frame->buttons, link) {
575 const int button_padding = 4;
576 w = cairo_image_surface_get_width(button->icon);
577 h = cairo_image_surface_get_height(button->icon);
578
579 if (button->flags & FRAME_BUTTON_DECORATED)
580 w += 10;
581
582 if (button->flags & FRAME_BUTTON_ALIGN_RIGHT) {
583 x_r -= w;
584
585 button->allocation.x = x_r;
586 button->allocation.y = y;
587 button->allocation.width = w + 1;
588 button->allocation.height = h + 1;
589
590 x_r -= button_padding;
591 } else {
592 button->allocation.x = x_l;
593 button->allocation.y = y;
594 button->allocation.width = w + 1;
595 button->allocation.height = h + 1;
596
597 x_l += w;
598 x_l += button_padding;
599 }
600 }
601
Louis-Francis Ratté-Boulianne864e39b2017-11-13 16:20:55 -0500602 frame->title_rect.x = x_l;
603 frame->title_rect.y = y;
604 frame->title_rect.width = x_r - x_l;
605 frame->title_rect.height = titlebar_height;
606
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500607 frame->geometry_dirty = 0;
608}
609
610void
611frame_interior(struct frame *frame, int32_t *x, int32_t *y,
612 int32_t *width, int32_t *height)
613{
614 frame_refresh_geometry(frame);
615
616 if (x)
617 *x = frame->interior.x;
618 if (y)
619 *y = frame->interior.y;
620 if (width)
621 *width = frame->interior.width;
622 if (height)
623 *height = frame->interior.height;
624}
625
626void
627frame_input_rect(struct frame *frame, int32_t *x, int32_t *y,
628 int32_t *width, int32_t *height)
629{
630 frame_refresh_geometry(frame);
631
632 if (x)
633 *x = frame->shadow_margin;
634 if (y)
635 *y = frame->shadow_margin;
636 if (width)
637 *width = frame->width - frame->shadow_margin * 2;
638 if (height)
639 *height = frame->height - frame->shadow_margin * 2;
640}
641
642void
643frame_opaque_rect(struct frame *frame, int32_t *x, int32_t *y,
644 int32_t *width, int32_t *height)
645{
646 frame_refresh_geometry(frame);
647
648 if (x)
649 *x = frame->opaque_margin;
650 if (y)
651 *y = frame->opaque_margin;
652 if (width)
653 *width = frame->width - frame->opaque_margin * 2;
654 if (height)
655 *height = frame->height - frame->opaque_margin * 2;
656}
657
Jasper St. Pierre74073452014-02-01 18:36:41 -0500658int
659frame_get_shadow_margin(struct frame *frame)
660{
661 frame_refresh_geometry(frame);
662
663 return frame->shadow_margin;
664}
665
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500666uint32_t
667frame_status(struct frame *frame)
668{
669 return frame->status;
670}
671
672void
673frame_status_clear(struct frame *frame, enum frame_status status)
674{
675 frame->status &= ~status;
676}
677
678static struct frame_button *
679frame_find_button(struct frame *frame, int x, int y)
680{
681 struct frame_button *button;
682 int rel_x, rel_y;
683
684 wl_list_for_each(button, &frame->buttons, link) {
685 rel_x = x - button->allocation.x;
686 rel_y = y - button->allocation.y;
687
688 if (0 <= rel_x && rel_x < button->allocation.width &&
689 0 <= rel_y && rel_y < button->allocation.height)
690 return button;
691 }
692
693 return NULL;
694}
695
696enum theme_location
697frame_pointer_enter(struct frame *frame, void *data, int x, int y)
698{
699 return frame_pointer_motion(frame, data, x, y);
700}
701
702enum theme_location
703frame_pointer_motion(struct frame *frame, void *data, int x, int y)
704{
705 struct frame_pointer *pointer = frame_pointer_get(frame, data);
706 struct frame_button *button = frame_find_button(frame, x, y);
707 enum theme_location location;
708
709 location = theme_get_location(frame->theme, x, y,
710 frame->width, frame->height,
711 frame->flags & FRAME_FLAG_MAXIMIZED ?
712 THEME_FRAME_MAXIMIZED : 0);
713 if (!pointer)
714 return location;
715
716 pointer->x = x;
717 pointer->y = y;
718
719 if (pointer->hover_button == button)
720 return location;
721
722 if (pointer->hover_button)
723 frame_button_leave(pointer->hover_button, pointer);
724
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500725 pointer->hover_button = button;
726
727 if (pointer->hover_button)
728 frame_button_enter(pointer->hover_button);
729
730 return location;
731}
732
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -0500733static void
734frame_pointer_button_destroy(struct frame_pointer_button *button)
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500735{
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -0500736 wl_list_remove(&button->link);
737 free(button);
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500738}
739
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -0500740static void
741frame_pointer_button_press(struct frame *frame, struct frame_pointer *pointer,
742 struct frame_pointer_button *button)
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500743{
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -0500744 if (button->button == BTN_RIGHT) {
745 if (button->press_location == THEME_LOCATION_TITLEBAR)
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500746 frame->status |= FRAME_STATUS_MENU;
747
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -0500748 frame_pointer_button_destroy(button);
749
750 } else if (button->button == BTN_LEFT) {
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500751 if (pointer->hover_button) {
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500752 frame_button_press(pointer->hover_button);
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500753 } else {
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -0500754 switch (button->press_location) {
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500755 case THEME_LOCATION_TITLEBAR:
756 frame->status |= FRAME_STATUS_MOVE;
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -0500757
758 frame_pointer_button_destroy(button);
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500759 break;
760 case THEME_LOCATION_RESIZING_TOP:
761 case THEME_LOCATION_RESIZING_BOTTOM:
762 case THEME_LOCATION_RESIZING_LEFT:
763 case THEME_LOCATION_RESIZING_RIGHT:
764 case THEME_LOCATION_RESIZING_TOP_LEFT:
765 case THEME_LOCATION_RESIZING_TOP_RIGHT:
766 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
767 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
768 frame->status |= FRAME_STATUS_RESIZE;
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -0500769
770 frame_pointer_button_destroy(button);
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500771 break;
772 default:
773 break;
774 }
775 }
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -0500776 }
777}
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500778
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -0500779static void
780frame_pointer_button_release(struct frame *frame, struct frame_pointer *pointer,
781 struct frame_pointer_button *button)
782{
783 if (button->button == BTN_LEFT && button->frame_button) {
784 if (button->frame_button == pointer->hover_button)
785 frame_button_release(button->frame_button);
786 else
787 frame_button_cancel(button->frame_button);
788 }
789}
790
791static void
792frame_pointer_button_cancel(struct frame *frame, struct frame_pointer *pointer,
793 struct frame_pointer_button *button)
794{
795 if (button->frame_button)
796 frame_button_cancel(button->frame_button);
797}
798
799void
800frame_pointer_leave(struct frame *frame, void *data)
801{
802 struct frame_pointer *pointer = frame_pointer_get(frame, data);
803 struct frame_pointer_button *button, *next;
804 if (!pointer)
805 return;
806
807 if (pointer->hover_button)
808 frame_button_leave(pointer->hover_button, pointer);
809
810 wl_list_for_each_safe(button, next, &pointer->down_buttons, link) {
811 frame_pointer_button_cancel(frame, pointer, button);
812 frame_pointer_button_destroy(button);
813 }
814
815 frame_pointer_destroy(pointer);
816}
817
818enum theme_location
819frame_pointer_button(struct frame *frame, void *data,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +0200820 uint32_t btn, enum wl_pointer_button_state state)
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -0500821{
822 struct frame_pointer *pointer = frame_pointer_get(frame, data);
823 struct frame_pointer_button *button;
U. Artie Eoff6d71c3c2014-01-17 14:44:05 -0800824 enum theme_location location = THEME_LOCATION_EXTERIOR;
825
826 if (!pointer)
827 return location;
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -0500828
829 location = theme_get_location(frame->theme, pointer->x, pointer->y,
830 frame->width, frame->height,
831 frame->flags & FRAME_FLAG_MAXIMIZED ?
832 THEME_FRAME_MAXIMIZED : 0);
833
Quentin Glidicd8b17bc2016-07-10 11:00:55 +0200834 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -0500835 button = malloc(sizeof *button);
836 if (!button)
837 return location;
838
839 button->button = btn;
840 button->press_location = location;
841 button->frame_button = pointer->hover_button;
842 wl_list_insert(&pointer->down_buttons, &button->link);
843
844 frame_pointer_button_press(frame, pointer, button);
Quentin Glidicd8b17bc2016-07-10 11:00:55 +0200845 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Jason Ekstrand0bdd58f2013-10-27 22:25:03 -0500846 button = NULL;
847 wl_list_for_each(button, &pointer->down_buttons, link)
848 if (button->button == btn)
849 break;
850 /* Make sure we didn't hit the end */
851 if (&button->link == &pointer->down_buttons)
852 return location;
853
854 location = button->press_location;
855 frame_pointer_button_release(frame, pointer, button);
856 frame_pointer_button_destroy(button);
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500857 }
858
859 return location;
860}
861
Derek Foreman96906412015-11-06 15:56:07 -0600862enum theme_location
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500863frame_touch_down(struct frame *frame, void *data, int32_t id, int x, int y)
864{
865 struct frame_touch *touch = frame_touch_get(frame, data);
866 struct frame_button *button = frame_find_button(frame, x, y);
867 enum theme_location location;
868
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500869 location = theme_get_location(frame->theme, x, y,
870 frame->width, frame->height,
871 frame->flags & FRAME_FLAG_MAXIMIZED ?
872 THEME_FRAME_MAXIMIZED : 0);
873
Derek Foreman96906412015-11-06 15:56:07 -0600874 if (id > 0)
875 return location;
876
877 if (touch && button) {
878 touch->button = button;
879 frame_button_press(touch->button);
880 return location;
881 }
882
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500883 switch (location) {
884 case THEME_LOCATION_TITLEBAR:
885 frame->status |= FRAME_STATUS_MOVE;
886 break;
887 case THEME_LOCATION_RESIZING_TOP:
888 case THEME_LOCATION_RESIZING_BOTTOM:
889 case THEME_LOCATION_RESIZING_LEFT:
890 case THEME_LOCATION_RESIZING_RIGHT:
891 case THEME_LOCATION_RESIZING_TOP_LEFT:
892 case THEME_LOCATION_RESIZING_TOP_RIGHT:
893 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
894 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
895 frame->status |= FRAME_STATUS_RESIZE;
896 break;
897 default:
898 break;
899 }
Derek Foreman96906412015-11-06 15:56:07 -0600900 return location;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500901}
902
903void
904frame_touch_up(struct frame *frame, void *data, int32_t id)
905{
906 struct frame_touch *touch = frame_touch_get(frame, data);
907
908 if (id > 0)
909 return;
910
U. Artie Eoff6d71c3c2014-01-17 14:44:05 -0800911 if (touch && touch->button) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500912 frame_button_release(touch->button);
913 frame_touch_destroy(touch);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500914 }
915}
916
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800917enum theme_location
918frame_double_click(struct frame *frame, void *data,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +0200919 uint32_t btn, enum wl_pointer_button_state state)
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800920{
921 struct frame_pointer *pointer = frame_pointer_get(frame, data);
922 struct frame_button *button;
923 enum theme_location location = THEME_LOCATION_EXTERIOR;
924
925 location = theme_get_location(frame->theme, pointer->x, pointer->y,
926 frame->width, frame->height,
927 frame->flags & FRAME_FLAG_MAXIMIZED ?
928 THEME_FRAME_MAXIMIZED : 0);
929
930 button = frame_find_button(frame, pointer->x, pointer->y);
931
932 if (location != THEME_LOCATION_TITLEBAR || btn != BTN_LEFT)
933 return location;
934
Quentin Glidicd8b17bc2016-07-10 11:00:55 +0200935 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800936 if (button)
937 frame_button_press(button);
938 else
939 frame->status |= FRAME_STATUS_MAXIMIZE;
Quentin Glidicd8b17bc2016-07-10 11:00:55 +0200940 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800941 if (button)
942 frame_button_release(button);
943 }
944
945 return location;
946}
947
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500948void
Xiong Zhang382de462014-06-12 11:06:26 +0800949frame_double_touch_down(struct frame *frame, void *data, int32_t id,
950 int x, int y)
951{
952 struct frame_touch *touch = frame_touch_get(frame, data);
953 struct frame_button *button = frame_find_button(frame, x, y);
954 enum theme_location location;
955
956 if (touch && button) {
957 touch->button = button;
958 frame_button_press(touch->button);
959 return;
960 }
961
962 location = theme_get_location(frame->theme, x, y,
963 frame->width, frame->height,
964 frame->flags & FRAME_FLAG_MAXIMIZED ?
965 THEME_FRAME_MAXIMIZED : 0);
966
967 switch (location) {
968 case THEME_LOCATION_TITLEBAR:
969 frame->status |= FRAME_STATUS_MAXIMIZE;
970 break;
971 case THEME_LOCATION_RESIZING_TOP:
972 case THEME_LOCATION_RESIZING_BOTTOM:
973 case THEME_LOCATION_RESIZING_LEFT:
974 case THEME_LOCATION_RESIZING_RIGHT:
975 case THEME_LOCATION_RESIZING_TOP_LEFT:
976 case THEME_LOCATION_RESIZING_TOP_RIGHT:
977 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
978 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
979 frame->status |= FRAME_STATUS_RESIZE;
980 break;
981 default:
982 break;
983 }
984}
985
986void
987frame_double_touch_up(struct frame *frame, void *data, int32_t id)
988{
989 struct frame_touch *touch = frame_touch_get(frame, data);
990
991 if (touch && touch->button) {
992 frame_button_release(touch->button);
993 frame_touch_destroy(touch);
994 }
995}
996
997void
Jason Ekstrand01c9ec32013-10-13 19:08:39 -0500998frame_repaint(struct frame *frame, cairo_t *cr)
999{
1000 struct frame_button *button;
1001 uint32_t flags = 0;
1002
1003 frame_refresh_geometry(frame);
1004
1005 if (frame->flags & FRAME_FLAG_MAXIMIZED)
1006 flags |= THEME_FRAME_MAXIMIZED;
1007
1008 if (frame->flags & FRAME_FLAG_ACTIVE)
1009 flags |= THEME_FRAME_ACTIVE;
1010
1011 cairo_save(cr);
1012 theme_render_frame(frame->theme, cr, frame->width, frame->height,
Louis-Francis Ratté-Boulianne864e39b2017-11-13 16:20:55 -05001013 frame->title, &frame->title_rect,
1014 &frame->buttons, flags);
Jason Ekstrand01c9ec32013-10-13 19:08:39 -05001015 cairo_restore(cr);
1016
1017 wl_list_for_each(button, &frame->buttons, link)
1018 frame_button_repaint(button, cr);
1019
1020 frame_status_clear(frame, FRAME_STATUS_REPAINT);
1021}