2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2012-2013 Collabora, Ltd.
4 * Copyright © 2013 Jason Ekstrand
6 * 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:
14 * 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.
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
33 #include <wayland-util.h>
34 #include <linux/input.h>
36 #include "cairo-util.h"
37 #include "shared/file-util.h"
39 enum frame_button_flags {
40 FRAME_BUTTON_ALIGN_RIGHT = 0x1,
41 FRAME_BUTTON_DECORATED = 0x2,
42 FRAME_BUTTON_CLICK_DOWN = 0x4,
47 struct wl_list link; /* buttons_list */
49 cairo_surface_t *icon;
50 enum frame_button_flags flags;
59 enum frame_status status_effect;
62 struct frame_pointer_button {
65 enum theme_location press_location;
66 struct frame_button *frame_button;
69 struct frame_pointer {
75 struct frame_button *hover_button;
76 struct wl_list down_buttons;
85 struct frame_button *button;
89 int32_t width, height;
96 int32_t width, height;
102 cairo_rectangle_int_t title_rect;
106 struct wl_list buttons;
107 struct wl_list pointers;
108 struct wl_list touches;
111 static struct frame_button *
112 frame_button_create_from_surface(struct frame *frame, cairo_surface_t *icon,
113 enum frame_status status_effect,
114 enum frame_button_flags flags)
116 struct frame_button *button;
118 button = calloc(1, sizeof *button);
123 button->frame = frame;
124 button->flags = flags;
125 button->status_effect = status_effect;
127 wl_list_insert(frame->buttons.prev, &button->link);
132 static struct frame_button *
133 frame_button_create(struct frame *frame, const char *icon_name,
134 enum frame_status status_effect,
135 enum frame_button_flags flags)
137 struct frame_button *button;
138 cairo_surface_t *icon;
140 icon = cairo_image_surface_create_from_png(icon_name);
141 if (cairo_surface_status(icon) != CAIRO_STATUS_SUCCESS)
144 button = frame_button_create_from_surface(frame, icon, status_effect,
152 cairo_surface_destroy(icon);
157 frame_button_destroy(struct frame_button *button)
159 cairo_surface_destroy(button->icon);
164 frame_button_enter(struct frame_button *button)
166 if (!button->hover_count)
167 button->frame->status |= FRAME_STATUS_REPAINT;
168 button->hover_count++;
172 frame_button_leave(struct frame_button *button, struct frame_pointer *pointer)
174 button->hover_count--;
175 if (!button->hover_count)
176 button->frame->status |= FRAME_STATUS_REPAINT;
180 frame_button_press(struct frame_button *button)
182 if (!button->press_count)
183 button->frame->status |= FRAME_STATUS_REPAINT;
184 button->press_count++;
186 if (button->flags & FRAME_BUTTON_CLICK_DOWN)
187 button->frame->status |= button->status_effect;
191 frame_button_release(struct frame_button *button)
193 button->press_count--;
194 if (button->press_count)
197 button->frame->status |= FRAME_STATUS_REPAINT;
199 if (!(button->flags & FRAME_BUTTON_CLICK_DOWN))
200 button->frame->status |= button->status_effect;
204 frame_button_cancel(struct frame_button *button)
206 button->press_count--;
207 if (!button->press_count)
208 button->frame->status |= FRAME_STATUS_REPAINT;
212 frame_button_repaint(struct frame_button *button, cairo_t *cr)
216 if (!button->allocation.width)
218 if (!button->allocation.height)
221 x = button->allocation.x;
222 y = button->allocation.y;
226 if (button->flags & FRAME_BUTTON_DECORATED) {
227 cairo_set_line_width(cr, 1);
229 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
230 cairo_rectangle(cr, x, y, 25, 16);
232 cairo_stroke_preserve(cr);
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);
239 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
247 cairo_set_source_surface(cr, button->icon, x, y);
253 static struct frame_pointer *
254 frame_pointer_get(struct frame *frame, void *data)
256 struct frame_pointer *pointer;
258 wl_list_for_each(pointer, &frame->pointers, link)
259 if (pointer->data == data)
262 pointer = calloc(1, sizeof *pointer);
266 pointer->data = data;
267 wl_list_init(&pointer->down_buttons);
268 wl_list_insert(&frame->pointers, &pointer->link);
274 frame_pointer_destroy(struct frame_pointer *pointer)
276 wl_list_remove(&pointer->link);
280 static struct frame_touch *
281 frame_touch_get(struct frame *frame, void *data)
283 struct frame_touch *touch;
285 wl_list_for_each(touch, &frame->touches, link)
286 if (touch->data == data)
289 touch = calloc(1, sizeof *touch);
294 wl_list_insert(&frame->touches, &touch->link);
300 frame_touch_destroy(struct frame_touch *touch)
302 wl_list_remove(&touch->link);
307 frame_destroy(struct frame *frame)
309 struct frame_button *button, *next;
310 struct frame_touch *touch, *next_touch;
311 struct frame_pointer *pointer, *next_pointer;
313 wl_list_for_each_safe(button, next, &frame->buttons, link)
314 frame_button_destroy(button);
316 wl_list_for_each_safe(touch, next_touch, &frame->touches, link)
317 frame_touch_destroy(touch);
319 wl_list_for_each_safe(pointer, next_pointer, &frame->pointers, link)
320 frame_pointer_destroy(pointer);
327 frame_create(struct theme *t, int32_t width, int32_t height, uint32_t buttons,
328 const char *title, cairo_surface_t *icon)
331 struct frame_button *button;
333 frame = calloc(1, sizeof *frame);
337 frame->width = width;
338 frame->height = height;
341 frame->status = FRAME_STATUS_REPAINT;
342 frame->geometry_dirty = 1;
344 wl_list_init(&frame->buttons);
345 wl_list_init(&frame->pointers);
346 wl_list_init(&frame->touches);
349 frame->title = strdup(title);
356 button = frame_button_create_from_surface(frame,
359 FRAME_BUTTON_CLICK_DOWN);
361 char *name = file_name_with_datadir("icon_window.png");
366 button = frame_button_create(frame,
369 FRAME_BUTTON_CLICK_DOWN);
376 if (buttons & FRAME_BUTTON_CLOSE) {
377 char *name = file_name_with_datadir("sign_close.png");
382 button = frame_button_create(frame,
385 FRAME_BUTTON_ALIGN_RIGHT |
386 FRAME_BUTTON_DECORATED);
392 if (buttons & FRAME_BUTTON_MAXIMIZE) {
393 char *name = file_name_with_datadir("sign_maximize.png");
398 button = frame_button_create(frame,
400 FRAME_STATUS_MAXIMIZE,
401 FRAME_BUTTON_ALIGN_RIGHT |
402 FRAME_BUTTON_DECORATED);
408 if (buttons & FRAME_BUTTON_MINIMIZE) {
409 char *name = file_name_with_datadir("sign_minimize.png");
414 button = frame_button_create(frame,
416 FRAME_STATUS_MINIMIZE,
417 FRAME_BUTTON_ALIGN_RIGHT |
418 FRAME_BUTTON_DECORATED);
427 frame_destroy(frame);
432 frame_set_title(struct frame *frame, const char *title)
445 frame->geometry_dirty = 1;
446 frame->status |= FRAME_STATUS_REPAINT;
452 frame_set_icon(struct frame *frame, cairo_surface_t *icon)
454 struct frame_button *button;
455 wl_list_for_each(button, &frame->buttons, link) {
456 if (button->status_effect != FRAME_STATUS_MENU)
459 cairo_surface_destroy(button->icon);
461 frame->status |= FRAME_STATUS_REPAINT;
466 frame_set_flag(struct frame *frame, enum frame_flag flag)
468 if (flag & FRAME_FLAG_MAXIMIZED && !(frame->flags & FRAME_FLAG_MAXIMIZED))
469 frame->geometry_dirty = 1;
471 frame->flags |= flag;
472 frame->status |= FRAME_STATUS_REPAINT;
476 frame_unset_flag(struct frame *frame, enum frame_flag flag)
478 if (flag & FRAME_FLAG_MAXIMIZED && frame->flags & FRAME_FLAG_MAXIMIZED)
479 frame->geometry_dirty = 1;
481 frame->flags &= ~flag;
482 frame->status |= FRAME_STATUS_REPAINT;
486 frame_resize(struct frame *frame, int32_t width, int32_t height)
488 frame->width = width;
489 frame->height = height;
491 frame->geometry_dirty = 1;
492 frame->status |= FRAME_STATUS_REPAINT;
496 frame_resize_inside(struct frame *frame, int32_t width, int32_t height)
498 struct theme *t = frame->theme;
499 int decoration_width, decoration_height, titlebar_height;
501 if (frame->title || !wl_list_empty(&frame->buttons))
502 titlebar_height = t->titlebar_height;
504 titlebar_height = t->width;
506 if (frame->flags & FRAME_FLAG_MAXIMIZED) {
507 decoration_width = t->width * 2;
508 decoration_height = t->width + titlebar_height;
510 decoration_width = (t->width + t->margin) * 2;
511 decoration_height = t->width +
512 titlebar_height + t->margin * 2;
515 frame_resize(frame, width + decoration_width,
516 height + decoration_height);
520 frame_width(struct frame *frame)
526 frame_height(struct frame *frame)
528 return frame->height;
532 frame_refresh_geometry(struct frame *frame)
534 struct frame_button *button;
535 struct theme *t = frame->theme;
536 int x_l, x_r, y, w, h, titlebar_height;
537 int32_t decoration_width, decoration_height;
539 if (!frame->geometry_dirty)
542 if (frame->title || !wl_list_empty(&frame->buttons))
543 titlebar_height = t->titlebar_height;
545 titlebar_height = t->width;
547 if (frame->flags & FRAME_FLAG_MAXIMIZED) {
548 decoration_width = t->width * 2;
549 decoration_height = t->width + titlebar_height;
551 frame->interior.x = t->width;
552 frame->interior.y = titlebar_height;
553 frame->interior.width = frame->width - decoration_width;
554 frame->interior.height = frame->height - decoration_height;
556 frame->opaque_margin = 0;
557 frame->shadow_margin = 0;
559 decoration_width = (t->width + t->margin) * 2;
560 decoration_height = t->width + titlebar_height + t->margin * 2;
562 frame->interior.x = t->width + t->margin;
563 frame->interior.y = titlebar_height + t->margin;
564 frame->interior.width = frame->width - decoration_width;
565 frame->interior.height = frame->height - decoration_height;
567 frame->opaque_margin = t->margin + t->frame_radius;
568 frame->shadow_margin = t->margin;
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);
579 if (button->flags & FRAME_BUTTON_DECORATED)
582 if (button->flags & FRAME_BUTTON_ALIGN_RIGHT) {
585 button->allocation.x = x_r;
586 button->allocation.y = y;
587 button->allocation.width = w + 1;
588 button->allocation.height = h + 1;
590 x_r -= button_padding;
592 button->allocation.x = x_l;
593 button->allocation.y = y;
594 button->allocation.width = w + 1;
595 button->allocation.height = h + 1;
598 x_l += button_padding;
602 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;
607 frame->geometry_dirty = 0;
611 frame_interior(struct frame *frame, int32_t *x, int32_t *y,
612 int32_t *width, int32_t *height)
614 frame_refresh_geometry(frame);
617 *x = frame->interior.x;
619 *y = frame->interior.y;
621 *width = frame->interior.width;
623 *height = frame->interior.height;
627 frame_input_rect(struct frame *frame, int32_t *x, int32_t *y,
628 int32_t *width, int32_t *height)
630 frame_refresh_geometry(frame);
633 *x = frame->shadow_margin;
635 *y = frame->shadow_margin;
637 *width = frame->width - frame->shadow_margin * 2;
639 *height = frame->height - frame->shadow_margin * 2;
643 frame_opaque_rect(struct frame *frame, int32_t *x, int32_t *y,
644 int32_t *width, int32_t *height)
646 frame_refresh_geometry(frame);
649 *x = frame->opaque_margin;
651 *y = frame->opaque_margin;
653 *width = frame->width - frame->opaque_margin * 2;
655 *height = frame->height - frame->opaque_margin * 2;
659 frame_get_shadow_margin(struct frame *frame)
661 frame_refresh_geometry(frame);
663 return frame->shadow_margin;
667 frame_status(struct frame *frame)
669 return frame->status;
673 frame_status_clear(struct frame *frame, enum frame_status status)
675 frame->status &= ~status;
678 static struct frame_button *
679 frame_find_button(struct frame *frame, int x, int y)
681 struct frame_button *button;
684 wl_list_for_each(button, &frame->buttons, link) {
685 rel_x = x - button->allocation.x;
686 rel_y = y - button->allocation.y;
688 if (0 <= rel_x && rel_x < button->allocation.width &&
689 0 <= rel_y && rel_y < button->allocation.height)
697 frame_pointer_enter(struct frame *frame, void *data, int x, int y)
699 return frame_pointer_motion(frame, data, x, y);
703 frame_pointer_motion(struct frame *frame, void *data, int x, int y)
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;
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);
719 if (pointer->hover_button == button)
722 if (pointer->hover_button)
723 frame_button_leave(pointer->hover_button, pointer);
725 pointer->hover_button = button;
727 if (pointer->hover_button)
728 frame_button_enter(pointer->hover_button);
734 frame_pointer_button_destroy(struct frame_pointer_button *button)
736 wl_list_remove(&button->link);
741 frame_pointer_button_press(struct frame *frame, struct frame_pointer *pointer,
742 struct frame_pointer_button *button)
744 if (button->button == BTN_RIGHT) {
745 if (button->press_location == THEME_LOCATION_TITLEBAR)
746 frame->status |= FRAME_STATUS_MENU;
748 frame_pointer_button_destroy(button);
750 } else if (button->button == BTN_LEFT) {
751 if (pointer->hover_button) {
752 frame_button_press(pointer->hover_button);
754 switch (button->press_location) {
755 case THEME_LOCATION_TITLEBAR:
756 frame->status |= FRAME_STATUS_MOVE;
758 frame_pointer_button_destroy(button);
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;
770 frame_pointer_button_destroy(button);
780 frame_pointer_button_release(struct frame *frame, struct frame_pointer *pointer,
781 struct frame_pointer_button *button)
783 if (button->button == BTN_LEFT && button->frame_button) {
784 if (button->frame_button == pointer->hover_button)
785 frame_button_release(button->frame_button);
787 frame_button_cancel(button->frame_button);
792 frame_pointer_button_cancel(struct frame *frame, struct frame_pointer *pointer,
793 struct frame_pointer_button *button)
795 if (button->frame_button)
796 frame_button_cancel(button->frame_button);
800 frame_pointer_leave(struct frame *frame, void *data)
802 struct frame_pointer *pointer = frame_pointer_get(frame, data);
803 struct frame_pointer_button *button, *next;
807 if (pointer->hover_button)
808 frame_button_leave(pointer->hover_button, pointer);
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);
815 frame_pointer_destroy(pointer);
819 frame_pointer_button(struct frame *frame, void *data,
820 uint32_t btn, enum wl_pointer_button_state state)
822 struct frame_pointer *pointer = frame_pointer_get(frame, data);
823 struct frame_pointer_button *button;
824 enum theme_location location = THEME_LOCATION_EXTERIOR;
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);
834 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
835 button = malloc(sizeof *button);
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);
844 frame_pointer_button_press(frame, pointer, button);
845 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
847 wl_list_for_each(button, &pointer->down_buttons, link)
848 if (button->button == btn)
850 /* Make sure we didn't hit the end */
851 if (&button->link == &pointer->down_buttons)
854 location = button->press_location;
855 frame_pointer_button_release(frame, pointer, button);
856 frame_pointer_button_destroy(button);
863 frame_touch_down(struct frame *frame, void *data, int32_t id, int x, int y)
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;
869 location = theme_get_location(frame->theme, x, y,
870 frame->width, frame->height,
871 frame->flags & FRAME_FLAG_MAXIMIZED ?
872 THEME_FRAME_MAXIMIZED : 0);
877 if (touch && button) {
878 touch->button = button;
879 frame_button_press(touch->button);
884 case THEME_LOCATION_TITLEBAR:
885 frame->status |= FRAME_STATUS_MOVE;
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;
904 frame_touch_up(struct frame *frame, void *data, int32_t id)
906 struct frame_touch *touch = frame_touch_get(frame, data);
911 if (touch && touch->button) {
912 frame_button_release(touch->button);
913 frame_touch_destroy(touch);
918 frame_double_click(struct frame *frame, void *data,
919 uint32_t btn, enum wl_pointer_button_state state)
921 struct frame_pointer *pointer = frame_pointer_get(frame, data);
922 struct frame_button *button;
923 enum theme_location location = THEME_LOCATION_EXTERIOR;
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);
930 button = frame_find_button(frame, pointer->x, pointer->y);
932 if (location != THEME_LOCATION_TITLEBAR || btn != BTN_LEFT)
935 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
937 frame_button_press(button);
939 frame->status |= FRAME_STATUS_MAXIMIZE;
940 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
942 frame_button_release(button);
949 frame_double_touch_down(struct frame *frame, void *data, int32_t id,
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;
956 if (touch && button) {
957 touch->button = button;
958 frame_button_press(touch->button);
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);
968 case THEME_LOCATION_TITLEBAR:
969 frame->status |= FRAME_STATUS_MAXIMIZE;
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;
987 frame_double_touch_up(struct frame *frame, void *data, int32_t id)
989 struct frame_touch *touch = frame_touch_get(frame, data);
991 if (touch && touch->button) {
992 frame_button_release(touch->button);
993 frame_touch_destroy(touch);
998 frame_repaint(struct frame *frame, cairo_t *cr)
1000 struct frame_button *button;
1003 frame_refresh_geometry(frame);
1005 if (frame->flags & FRAME_FLAG_MAXIMIZED)
1006 flags |= THEME_FRAME_MAXIMIZED;
1008 if (frame->flags & FRAME_FLAG_ACTIVE)
1009 flags |= THEME_FRAME_ACTIVE;
1012 theme_render_frame(frame->theme, cr, frame->width, frame->height,
1013 frame->title, &frame->title_rect,
1014 &frame->buttons, flags);
1017 wl_list_for_each(button, &frame->buttons, link)
1018 frame_button_repaint(button, cr);
1020 frame_status_clear(frame, FRAME_STATUS_REPAINT);