frame.c 23.4 KB
Newer Older
1 2 3 4 5
/*
 * Copyright © 2008 Kristian Høgsberg
 * Copyright © 2012-2013 Collabora, Ltd.
 * Copyright © 2013 Jason Ekstrand
 *
6 7 8 9 10 11 12
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
13
 *
14 15 16 17 18 19 20 21 22 23 24 25
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
26 27 28 29
 */

#include "config.h"

30
#include <stdint.h>
31 32 33 34 35 36
#include <stdlib.h>
#include <string.h>
#include <wayland-util.h>
#include <linux/input.h>

#include "cairo-util.h"
37
#include "shared/file-util.h"
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

enum frame_button_flags {
	FRAME_BUTTON_ALIGN_RIGHT = 0x1,
	FRAME_BUTTON_DECORATED = 0x2,
	FRAME_BUTTON_CLICK_DOWN = 0x4,
};

struct frame_button {
	struct frame *frame;
	struct wl_list link;	/* buttons_list */

	cairo_surface_t *icon;
	enum frame_button_flags flags;
	int hover_count;
	int press_count;

	struct {
		int x, y;
		int width, height;
	} allocation;

	enum frame_status status_effect;
};

62 63 64 65 66 67 68
struct frame_pointer_button {
	struct wl_list link;
	uint32_t button;
	enum theme_location press_location;
	struct frame_button *frame_button;
};

69 70 71 72 73 74 75
struct frame_pointer {
	struct wl_list link;
	void *data;

	int x, y;

	struct frame_button *hover_button;
76
	struct wl_list down_buttons;
77 78
};

79 80 81 82 83 84 85 86 87
struct frame_touch {
	struct wl_list link;
	void *data;

	int x, y;

	struct frame_button *button;
};

88 89 90 91 92 93 94 95 96 97 98 99 100 101
struct frame {
	int32_t width, height;
	char *title;
	uint32_t flags;
	struct theme *theme;

	struct {
		int32_t x, y;
		int32_t width, height;
	} interior;
	int shadow_margin;
	int opaque_margin;
	int geometry_dirty;

102 103
	cairo_rectangle_int_t title_rect;

104 105 106 107
	uint32_t status;

	struct wl_list buttons;
	struct wl_list pointers;
108
	struct wl_list touches;
109 110 111
};

static struct frame_button *
112 113 114
frame_button_create_from_surface(struct frame *frame, cairo_surface_t *icon,
                                 enum frame_status status_effect,
                                 enum frame_button_flags flags)
115 116 117 118 119 120 121
{
	struct frame_button *button;

	button = calloc(1, sizeof *button);
	if (!button)
		return NULL;

122
	button->icon = icon;
123 124 125 126 127 128 129 130 131
	button->frame = frame;
	button->flags = flags;
	button->status_effect = status_effect;

	wl_list_insert(frame->buttons.prev, &button->link);

	return button;
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
static struct frame_button *
frame_button_create(struct frame *frame, const char *icon_name,
                    enum frame_status status_effect,
                    enum frame_button_flags flags)
{
	struct frame_button *button;
	cairo_surface_t *icon;

	icon = cairo_image_surface_create_from_png(icon_name);
	if (cairo_surface_status(icon) != CAIRO_STATUS_SUCCESS)
		goto error;

	button = frame_button_create_from_surface(frame, icon, status_effect,
	                                          flags);
	if (!button)
		goto error;

	return button;

error:
	cairo_surface_destroy(icon);
	return NULL;
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
static void
frame_button_destroy(struct frame_button *button)
{
	cairo_surface_destroy(button->icon);
	free(button);
}

static void
frame_button_enter(struct frame_button *button)
{
	if (!button->hover_count)
		button->frame->status |= FRAME_STATUS_REPAINT;
	button->hover_count++;
}

static void
frame_button_leave(struct frame_button *button, struct frame_pointer *pointer)
{
	button->hover_count--;
	if (!button->hover_count)
		button->frame->status |= FRAME_STATUS_REPAINT;
}

static void
frame_button_press(struct frame_button *button)
{
	if (!button->press_count)
		button->frame->status |= FRAME_STATUS_REPAINT;
	button->press_count++;

	if (button->flags & FRAME_BUTTON_CLICK_DOWN)
		button->frame->status |= button->status_effect;
}

static void
frame_button_release(struct frame_button *button)
{
	button->press_count--;
194 195 196 197
	if (button->press_count)
		return;

	button->frame->status |= FRAME_STATUS_REPAINT;
198 199 200 201 202

	if (!(button->flags & FRAME_BUTTON_CLICK_DOWN))
		button->frame->status |= button->status_effect;
}

203 204 205 206 207 208 209 210
static void
frame_button_cancel(struct frame_button *button)
{
	button->press_count--;
	if (!button->press_count)
		button->frame->status |= FRAME_STATUS_REPAINT;
}

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
static void
frame_button_repaint(struct frame_button *button, cairo_t *cr)
{
	int x, y;

	if (!button->allocation.width)
		return;
	if (!button->allocation.height)
		return;

	x = button->allocation.x;
	y = button->allocation.y;

	cairo_save(cr);

	if (button->flags & FRAME_BUTTON_DECORATED) {
		cairo_set_line_width(cr, 1);

		cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
		cairo_rectangle(cr, x, y, 25, 16);

		cairo_stroke_preserve(cr);

		if (button->press_count) {
			cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
		} else if (button->hover_count) {
			cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
		} else {
			cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
		}

		cairo_fill (cr);

		x += 4;
	}

	cairo_set_source_surface(cr, button->icon, x, y);
	cairo_paint(cr);

	cairo_restore(cr);
}

static struct frame_pointer *
frame_pointer_get(struct frame *frame, void *data)
{
	struct frame_pointer *pointer;

	wl_list_for_each(pointer, &frame->pointers, link)
		if (pointer->data == data)
			return pointer;

	pointer = calloc(1, sizeof *pointer);
	if (!pointer)
		return NULL;

	pointer->data = data;
267
	wl_list_init(&pointer->down_buttons);
268 269 270 271 272 273 274 275 276 277 278 279
	wl_list_insert(&frame->pointers, &pointer->link);

	return pointer;
}

static void
frame_pointer_destroy(struct frame_pointer *pointer)
{
	wl_list_remove(&pointer->link);
	free(pointer);
}

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
static struct frame_touch *
frame_touch_get(struct frame *frame, void *data)
{
	struct frame_touch *touch;

	wl_list_for_each(touch, &frame->touches, link)
		if (touch->data == data)
			return touch;

	touch = calloc(1, sizeof *touch);
	if (!touch)
		return NULL;

	touch->data = data;
	wl_list_insert(&frame->touches, &touch->link);

	return touch;
}

static void
frame_touch_destroy(struct frame_touch *touch)
{
	wl_list_remove(&touch->link);
	free(touch);
}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
void
frame_destroy(struct frame *frame)
{
	struct frame_button *button, *next;
	struct frame_touch *touch, *next_touch;
	struct frame_pointer *pointer, *next_pointer;

	wl_list_for_each_safe(button, next, &frame->buttons, link)
		frame_button_destroy(button);

	wl_list_for_each_safe(touch, next_touch, &frame->touches, link)
		frame_touch_destroy(touch);

	wl_list_for_each_safe(pointer, next_pointer, &frame->pointers, link)
		frame_pointer_destroy(pointer);

	free(frame->title);
	free(frame);
}

326 327
struct frame *
frame_create(struct theme *t, int32_t width, int32_t height, uint32_t buttons,
328
             const char *title, cairo_surface_t *icon)
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
{
	struct frame *frame;
	struct frame_button *button;

	frame = calloc(1, sizeof *frame);
	if (!frame)
		return NULL;

	frame->width = width;
	frame->height = height;
	frame->flags = 0;
	frame->theme = t;
	frame->status = FRAME_STATUS_REPAINT;
	frame->geometry_dirty = 1;

344 345 346 347
	wl_list_init(&frame->buttons);
	wl_list_init(&frame->pointers);
	wl_list_init(&frame->touches);

348 349 350 351 352 353
	if (title) {
		frame->title = strdup(title);
		if (!frame->title)
			goto free_frame;
	}

354
	if (title) {
355 356 357 358 359 360
		if (icon) {
			button = frame_button_create_from_surface(frame,
			                                          icon,
			                                          FRAME_STATUS_MENU,
			                                          FRAME_BUTTON_CLICK_DOWN);
		} else {
361 362 363 364 365
			char *name = file_name_with_datadir("icon_window.png");

			if (!name)
				goto free_frame;

366
			button = frame_button_create(frame,
367
			                             name,
368 369
			                             FRAME_STATUS_MENU,
			                             FRAME_BUTTON_CLICK_DOWN);
370
			free(name);
371
		}
372 373 374
		if (!button)
			goto free_frame;
	}
375 376

	if (buttons & FRAME_BUTTON_CLOSE) {
377 378 379 380 381
		char *name = file_name_with_datadir("sign_close.png");

		if (!name)
			goto free_frame;

382
		button = frame_button_create(frame,
383
					     name,
384 385 386
					     FRAME_STATUS_CLOSE,
					     FRAME_BUTTON_ALIGN_RIGHT |
					     FRAME_BUTTON_DECORATED);
387
		free(name);
388 389 390 391 392
		if (!button)
			goto free_frame;
	}

	if (buttons & FRAME_BUTTON_MAXIMIZE) {
393 394 395 396 397
		char *name = file_name_with_datadir("sign_maximize.png");

		if (!name)
			goto free_frame;

398
		button = frame_button_create(frame,
399
					     name,
400 401 402
					     FRAME_STATUS_MAXIMIZE,
					     FRAME_BUTTON_ALIGN_RIGHT |
					     FRAME_BUTTON_DECORATED);
403
		free(name);
404 405 406 407 408
		if (!button)
			goto free_frame;
	}

	if (buttons & FRAME_BUTTON_MINIMIZE) {
409 410 411 412 413
		char *name = file_name_with_datadir("sign_minimize.png");

		if (!name)
			goto free_frame;

414
		button = frame_button_create(frame,
415
					     name,
416 417 418
					     FRAME_STATUS_MINIMIZE,
					     FRAME_BUTTON_ALIGN_RIGHT |
					     FRAME_BUTTON_DECORATED);
419
		free(name);
420 421 422 423 424 425 426
		if (!button)
			goto free_frame;
	}

	return frame;

free_frame:
427
	frame_destroy(frame);
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
	return NULL;
}

int
frame_set_title(struct frame *frame, const char *title)
{
	char *dup = NULL;

	if (title) {
		dup = strdup(title);
		if (!dup)
			return -1;
	}

	free(frame->title);
	frame->title = dup;

445
	frame->geometry_dirty = 1;
446 447 448 449 450
	frame->status |= FRAME_STATUS_REPAINT;

	return 0;
}

451 452 453 454 455 456 457 458 459 460 461 462 463 464
void
frame_set_icon(struct frame *frame, cairo_surface_t *icon)
{
	struct frame_button *button;
	wl_list_for_each(button, &frame->buttons, link) {
		if (button->status_effect != FRAME_STATUS_MENU)
			continue;
		if (button->icon)
			cairo_surface_destroy(button->icon);
		button->icon = icon;
		frame->status |= FRAME_STATUS_REPAINT;
	}
}

465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
void
frame_set_flag(struct frame *frame, enum frame_flag flag)
{
	if (flag & FRAME_FLAG_MAXIMIZED && !(frame->flags & FRAME_FLAG_MAXIMIZED))
		frame->geometry_dirty = 1;

	frame->flags |= flag;
	frame->status |= FRAME_STATUS_REPAINT;
}

void
frame_unset_flag(struct frame *frame, enum frame_flag flag)
{
	if (flag & FRAME_FLAG_MAXIMIZED && frame->flags & FRAME_FLAG_MAXIMIZED)
		frame->geometry_dirty = 1;

	frame->flags &= ~flag;
	frame->status |= FRAME_STATUS_REPAINT;
}

void
frame_resize(struct frame *frame, int32_t width, int32_t height)
{
	frame->width = width;
	frame->height = height;

	frame->geometry_dirty = 1;
	frame->status |= FRAME_STATUS_REPAINT;
}

void
frame_resize_inside(struct frame *frame, int32_t width, int32_t height)
{
	struct theme *t = frame->theme;
499 500
	int decoration_width, decoration_height, titlebar_height;

501
	if (frame->title || !wl_list_empty(&frame->buttons))
502 503 504
		titlebar_height = t->titlebar_height;
	else
		titlebar_height = t->width;
505 506 507

	if (frame->flags & FRAME_FLAG_MAXIMIZED) {
		decoration_width = t->width * 2;
508
		decoration_height = t->width + titlebar_height;
509 510 511
	} else {
		decoration_width = (t->width + t->margin) * 2;
		decoration_height = t->width +
512
			titlebar_height + t->margin * 2;
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
	}

	frame_resize(frame, width + decoration_width,
		     height + decoration_height);
}

int32_t
frame_width(struct frame *frame)
{
	return frame->width;
}

int32_t
frame_height(struct frame *frame)
{
	return frame->height;
}

static void
frame_refresh_geometry(struct frame *frame)
{
	struct frame_button *button;
	struct theme *t = frame->theme;
536
	int x_l, x_r, y, w, h, titlebar_height;
537 538 539 540 541
	int32_t decoration_width, decoration_height;

	if (!frame->geometry_dirty)
		return;

542
	if (frame->title || !wl_list_empty(&frame->buttons))
543 544 545 546
		titlebar_height = t->titlebar_height;
	else
		titlebar_height = t->width;

547 548
	if (frame->flags & FRAME_FLAG_MAXIMIZED) {
		decoration_width = t->width * 2;
549
		decoration_height = t->width + titlebar_height;
550 551

		frame->interior.x = t->width;
552
		frame->interior.y = titlebar_height;
553 554 555 556 557 558 559
		frame->interior.width = frame->width - decoration_width;
		frame->interior.height = frame->height - decoration_height;

		frame->opaque_margin = 0;
		frame->shadow_margin = 0;
	} else {
		decoration_width = (t->width + t->margin) * 2;
560
		decoration_height = t->width + titlebar_height + t->margin * 2;
561 562

		frame->interior.x = t->width + t->margin;
563
		frame->interior.y = titlebar_height + t->margin;
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
		frame->interior.width = frame->width - decoration_width;
		frame->interior.height = frame->height - decoration_height;

		frame->opaque_margin = t->margin + t->frame_radius;
		frame->shadow_margin = t->margin;
	}

	x_r = frame->width - t->width - frame->shadow_margin;
	x_l = t->width + frame->shadow_margin;
	y = t->width + frame->shadow_margin;
	wl_list_for_each(button, &frame->buttons, link) {
		const int button_padding = 4;
		w = cairo_image_surface_get_width(button->icon);
		h = cairo_image_surface_get_height(button->icon);

		if (button->flags & FRAME_BUTTON_DECORATED)
			w += 10;

		if (button->flags & FRAME_BUTTON_ALIGN_RIGHT) {
			x_r -= w;

			button->allocation.x = x_r;
			button->allocation.y = y;
			button->allocation.width = w + 1;
			button->allocation.height = h + 1;

			x_r -= button_padding;
		} else {
			button->allocation.x = x_l;
			button->allocation.y = y;
			button->allocation.width = w + 1;
			button->allocation.height = h + 1;

			x_l += w;
			x_l += button_padding;
		}
	}

602 603 604 605 606
	frame->title_rect.x = x_l;
	frame->title_rect.y = y;
	frame->title_rect.width = x_r - x_l;
	frame->title_rect.height = titlebar_height;

607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
	frame->geometry_dirty = 0;
}

void
frame_interior(struct frame *frame, int32_t *x, int32_t *y,
		int32_t *width, int32_t *height)
{
	frame_refresh_geometry(frame);

	if (x)
		*x = frame->interior.x;
	if (y)
		*y = frame->interior.y;
	if (width)
		*width = frame->interior.width;
	if (height)
		*height = frame->interior.height;
}

void
frame_input_rect(struct frame *frame, int32_t *x, int32_t *y,
		 int32_t *width, int32_t *height)
{
	frame_refresh_geometry(frame);

	if (x)
		*x = frame->shadow_margin;
	if (y)
		*y = frame->shadow_margin;
	if (width)
		*width = frame->width - frame->shadow_margin * 2;
	if (height)
		*height = frame->height - frame->shadow_margin * 2;
}

void
frame_opaque_rect(struct frame *frame, int32_t *x, int32_t *y,
		  int32_t *width, int32_t *height)
{
	frame_refresh_geometry(frame);

	if (x)
		*x = frame->opaque_margin;
	if (y)
		*y = frame->opaque_margin;
	if (width)
		*width = frame->width - frame->opaque_margin * 2;
	if (height)
		*height = frame->height - frame->opaque_margin * 2;
}

658 659 660 661 662 663 664 665
int
frame_get_shadow_margin(struct frame *frame)
{
	frame_refresh_geometry(frame);

	return frame->shadow_margin;
}

666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
uint32_t
frame_status(struct frame *frame)
{
	return frame->status;
}

void
frame_status_clear(struct frame *frame, enum frame_status status)
{
	frame->status &= ~status;
}

static struct frame_button *
frame_find_button(struct frame *frame, int x, int y)
{
	struct frame_button *button;
	int rel_x, rel_y;

	wl_list_for_each(button, &frame->buttons, link) {
		rel_x = x - button->allocation.x;
		rel_y = y - button->allocation.y;

		if (0 <= rel_x && rel_x < button->allocation.width &&
		    0 <= rel_y && rel_y < button->allocation.height)
			return button;
	}

	return NULL;
}

enum theme_location
frame_pointer_enter(struct frame *frame, void *data, int x, int y)
{
	return frame_pointer_motion(frame, data, x, y);
}

enum theme_location
frame_pointer_motion(struct frame *frame, void *data, int x, int y)
{
	struct frame_pointer *pointer = frame_pointer_get(frame, data);
	struct frame_button *button = frame_find_button(frame, x, y);
	enum theme_location location;

	location = theme_get_location(frame->theme, x, y,
				      frame->width, frame->height,
				      frame->flags & FRAME_FLAG_MAXIMIZED ?
				      THEME_FRAME_MAXIMIZED : 0);
	if (!pointer)
		return location;

	pointer->x = x;
	pointer->y = y;

	if (pointer->hover_button == button)
		return location;

	if (pointer->hover_button)
		frame_button_leave(pointer->hover_button, pointer);

	pointer->hover_button = button;

	if (pointer->hover_button)
		frame_button_enter(pointer->hover_button);

	return location;
}

733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
static void
frame_pointer_button_destroy(struct frame_pointer_button *button)
{
	wl_list_remove(&button->link);
	free(button);
}

static void
frame_pointer_button_press(struct frame *frame, struct frame_pointer *pointer,
			   struct frame_pointer_button *button)
{
	if (button->button == BTN_RIGHT) {
		if (button->press_location == THEME_LOCATION_TITLEBAR)
			frame->status |= FRAME_STATUS_MENU;

		frame_pointer_button_destroy(button);

	} else if (button->button == BTN_LEFT) {
		if (pointer->hover_button) {
			frame_button_press(pointer->hover_button);
		} else {
			switch (button->press_location) {
			case THEME_LOCATION_TITLEBAR:
				frame->status |= FRAME_STATUS_MOVE;

				frame_pointer_button_destroy(button);
				break;
			case THEME_LOCATION_RESIZING_TOP:
			case THEME_LOCATION_RESIZING_BOTTOM:
			case THEME_LOCATION_RESIZING_LEFT:
			case THEME_LOCATION_RESIZING_RIGHT:
			case THEME_LOCATION_RESIZING_TOP_LEFT:
			case THEME_LOCATION_RESIZING_TOP_RIGHT:
			case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
			case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
				frame->status |= FRAME_STATUS_RESIZE;

				frame_pointer_button_destroy(button);
				break;
			default:
				break;
			}
		}
	}
}

static void
frame_pointer_button_release(struct frame *frame, struct frame_pointer *pointer,
			     struct frame_pointer_button *button)
{
	if (button->button == BTN_LEFT && button->frame_button) {
		if (button->frame_button == pointer->hover_button)
			frame_button_release(button->frame_button);
		else
			frame_button_cancel(button->frame_button);
	}
}

static void
frame_pointer_button_cancel(struct frame *frame, struct frame_pointer *pointer,
			    struct frame_pointer_button *button)
{
	if (button->frame_button)
		frame_button_cancel(button->frame_button);
}

799 800 801 802
void
frame_pointer_leave(struct frame *frame, void *data)
{
	struct frame_pointer *pointer = frame_pointer_get(frame, data);
803
	struct frame_pointer_button *button, *next;
804 805 806 807 808 809
	if (!pointer)
		return;

	if (pointer->hover_button)
		frame_button_leave(pointer->hover_button, pointer);

810 811 812 813 814
	wl_list_for_each_safe(button, next, &pointer->down_buttons, link) {
		frame_pointer_button_cancel(frame, pointer, button);
		frame_pointer_button_destroy(button);
	}

815 816 817 818 819
	frame_pointer_destroy(pointer);
}

enum theme_location
frame_pointer_button(struct frame *frame, void *data,
820
		     uint32_t btn, enum wl_pointer_button_state state)
821 822
{
	struct frame_pointer *pointer = frame_pointer_get(frame, data);
823
	struct frame_pointer_button *button;
824 825 826 827
	enum theme_location location = THEME_LOCATION_EXTERIOR;

	if (!pointer)
		return location;
828 829 830 831 832 833

	location = theme_get_location(frame->theme, pointer->x, pointer->y,
				      frame->width, frame->height,
				      frame->flags & FRAME_FLAG_MAXIMIZED ?
				      THEME_FRAME_MAXIMIZED : 0);

834
	if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
835 836
		button = malloc(sizeof *button);
		if (!button)
837
			return location;
838 839 840 841 842 843 844

		button->button = btn;
		button->press_location = location;
		button->frame_button = pointer->hover_button;
		wl_list_insert(&pointer->down_buttons, &button->link);

		frame_pointer_button_press(frame, pointer, button);
845
	} else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
846 847 848
		button = NULL;
		wl_list_for_each(button, &pointer->down_buttons, link)
			if (button->button == btn)
849
				break;
850 851 852
		/* Make sure we didn't hit the end */
		if (&button->link == &pointer->down_buttons)
			return location;
853

854 855 856
		location = button->press_location;
		frame_pointer_button_release(frame, pointer, button);
		frame_pointer_button_destroy(button);
857 858 859 860 861
	}

	return location;
}

862
enum theme_location
863 864 865 866 867 868
frame_touch_down(struct frame *frame, void *data, int32_t id, int x, int y)
{
	struct frame_touch *touch = frame_touch_get(frame, data);
	struct frame_button *button = frame_find_button(frame, x, y);
	enum theme_location location;

869 870 871 872 873
	location = theme_get_location(frame->theme, x, y,
				      frame->width, frame->height,
				      frame->flags & FRAME_FLAG_MAXIMIZED ?
				      THEME_FRAME_MAXIMIZED : 0);

874
	if (id > 0)
875
		return location;
876

877
	if (touch && button) {
878 879
		touch->button = button;
		frame_button_press(touch->button);
880
		return location;
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
	}

	switch (location) {
	case THEME_LOCATION_TITLEBAR:
		frame->status |= FRAME_STATUS_MOVE;
		break;
	case THEME_LOCATION_RESIZING_TOP:
	case THEME_LOCATION_RESIZING_BOTTOM:
	case THEME_LOCATION_RESIZING_LEFT:
	case THEME_LOCATION_RESIZING_RIGHT:
	case THEME_LOCATION_RESIZING_TOP_LEFT:
	case THEME_LOCATION_RESIZING_TOP_RIGHT:
	case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
	case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
		frame->status |= FRAME_STATUS_RESIZE;
		break;
	default:
		break;
	}
900
	return location;
901 902 903 904 905 906 907 908 909 910
}

void
frame_touch_up(struct frame *frame, void *data, int32_t id)
{
	struct frame_touch *touch = frame_touch_get(frame, data);

	if (id > 0)
		return;

911
	if (touch && touch->button) {
912 913 914 915 916
		frame_button_release(touch->button);
		frame_touch_destroy(touch);
	}
}

917 918
enum theme_location
frame_double_click(struct frame *frame, void *data,
919
		   uint32_t btn, enum wl_pointer_button_state state)
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
{
	struct frame_pointer *pointer = frame_pointer_get(frame, data);
	struct frame_button *button;
	enum theme_location location = THEME_LOCATION_EXTERIOR;

	location = theme_get_location(frame->theme, pointer->x, pointer->y,
				      frame->width, frame->height,
				      frame->flags & FRAME_FLAG_MAXIMIZED ?
				      THEME_FRAME_MAXIMIZED : 0);

	button = frame_find_button(frame, pointer->x, pointer->y);

	if (location != THEME_LOCATION_TITLEBAR || btn != BTN_LEFT)
		return location;

935
	if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
936 937 938 939
		if (button)
			frame_button_press(button);
		else
			frame->status |= FRAME_STATUS_MAXIMIZE;
940
	} else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
941 942 943 944 945 946 947
		if (button)
			frame_button_release(button);
	}

	return location;
}

948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
void
frame_double_touch_down(struct frame *frame, void *data, int32_t id,
			int x, int y)
{
	struct frame_touch *touch = frame_touch_get(frame, data);
	struct frame_button *button = frame_find_button(frame, x, y);
	enum theme_location location;

	if (touch && button) {
		touch->button = button;
		frame_button_press(touch->button);
		return;
	}

	location = theme_get_location(frame->theme, x, y,
				      frame->width, frame->height,
				      frame->flags & FRAME_FLAG_MAXIMIZED ?
				      THEME_FRAME_MAXIMIZED : 0);

	switch (location) {
	case THEME_LOCATION_TITLEBAR:
		frame->status |= FRAME_STATUS_MAXIMIZE;
		break;
	case THEME_LOCATION_RESIZING_TOP:
	case THEME_LOCATION_RESIZING_BOTTOM:
	case THEME_LOCATION_RESIZING_LEFT:
	case THEME_LOCATION_RESIZING_RIGHT:
	case THEME_LOCATION_RESIZING_TOP_LEFT:
	case THEME_LOCATION_RESIZING_TOP_RIGHT:
	case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
	case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
		frame->status |= FRAME_STATUS_RESIZE;
		break;
	default:
		break;
	}
}

void
frame_double_touch_up(struct frame *frame, void *data, int32_t id)
{
	struct frame_touch *touch = frame_touch_get(frame, data);

	if (touch && touch->button) {
		frame_button_release(touch->button);
		frame_touch_destroy(touch);
	}
}

997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
void
frame_repaint(struct frame *frame, cairo_t *cr)
{
	struct frame_button *button;
	uint32_t flags = 0;

	frame_refresh_geometry(frame);

	if (frame->flags & FRAME_FLAG_MAXIMIZED)
		flags |= THEME_FRAME_MAXIMIZED;

	if (frame->flags & FRAME_FLAG_ACTIVE)
		flags |= THEME_FRAME_ACTIVE;

	cairo_save(cr);
	theme_render_frame(frame->theme, cr, frame->width, frame->height,
1013 1014
			   frame->title, &frame->title_rect,
			   &frame->buttons, flags);
1015 1016 1017 1018 1019 1020 1021
	cairo_restore(cr);

	wl_list_for_each(button, &frame->buttons, link)
		frame_button_repaint(button, cr);

	frame_status_clear(frame, FRAME_STATUS_REPAINT);
}