screenshot.c 26.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 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 267 268 269 270 271 272 273 274 275 276 277 278 279 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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 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 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 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 602 603 604 605 606 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 658 659 660 661 662 663 664 665 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 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 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 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 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
/*
 * Copyright © 2008 Kristian Høgsberg
 *
 * 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:
 *
 * 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.
 */

#include "config.h"

#include <stdint.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#include <libgen.h>
#include <unistd.h>
#include <limits.h>
#include <sys/epoll.h>
#include <sys/param.h>
#include <sys/mman.h>
#include <math.h>
#include <time.h>
#include <cairo.h>
#include <signal.h>
#include <assert.h>
#include <errno.h>
#include <linux/input.h>
#include <pixman.h>

#include <wayland-client.h>
#include "weston-screenshooter-client-protocol.h"
#include "shared/os-compatibility.h"
#include "shared/xalloc.h"
#include "shared/file-util.h"
#include "shared/cairo-util.h"
#include "shared/helpers.h"
#include "window.h"

/* The screenshooter is a good example of a custom object exposed by
 * the compositor and serves as a test bed for implementing client
 * side marshalling outside libwayland.so */

int           isloop = 1;
struct image *pimage;

struct screenshooter_output
{
	struct wl_output *output;
	struct wl_buffer *buffer;
	int               width, height, offset_x, offset_y;
	void             *data;
	struct wl_list    link;
};

struct buffer_size
{
	int width, height;

	int min_x, min_y;
	int max_x, max_y;
};

struct screenshooter_data
{
	struct wl_shm *shm;
	struct wl_list output_list;

	struct weston_screenshooter *screenshooter;
	int                          buffer_copy_done;
};

struct image
{
	struct window   *window;
	struct widget   *widget;
	struct display  *display;
	char            *filename;
	cairo_surface_t *image;
	int              fullscreen;
	int             *image_counter;
	int32_t          width, height;

	struct
	{
		double x;
		double y;
	}    pointer;
	bool button_pressed;

	bool           initialized;
	cairo_matrix_t matrix;
};

struct display
{
	struct wl_display                      *display;
	struct wl_registry                     *registry;
	struct wl_compositor                   *compositor;
	struct wl_subcompositor                *subcompositor;
	struct wl_shm                          *shm;
	struct wl_data_device_manager          *data_device_manager;
	struct text_cursor_position            *text_cursor_position;
	struct xdg_wm_base                     *xdg_shell;
	struct zwp_relative_pointer_manager_v1 *relative_pointer_manager;
	struct zwp_pointer_constraints_v1      *pointer_constraints;
	EGLDisplay                              dpy;
	EGLConfig                               argb_config;
	EGLContext                              argb_ctx;
	cairo_device_t                         *argb_device;
	uint32_t                                serial;

	int         display_fd;
	uint32_t    display_fd_events;
	struct task display_task;

	int            epoll_fd;
	struct wl_list deferred_list;

	int running;

	struct wl_list global_list;
	struct wl_list window_list;
	struct wl_list input_list;
	struct wl_list output_list;

	struct theme *theme;

	struct wl_cursor_theme *cursor_theme;
	struct wl_cursor      **cursors;

	display_output_handler_t output_configure_handler;
	display_global_handler_t global_handler;
	display_global_handler_t global_handler_remove;

	void *user_data;

	struct xkb_context *xkb_context;

	/* A hack to get text extents for tooltips */
	cairo_surface_t *dummy_surface;
	void            *dummy_surface_data;

	int                   data_device_manager_version;
	struct wp_viewporter *viewporter;
};

static void handler_sigint(int sig)
{
	isloop = 0;
}

static void
display_handle_geometry(void *data,
		struct wl_output     *wl_output,
		int                   x,
		int                   y,
		int                   physical_width,
		int                   physical_height,
		int                   subpixel,
		const char           *make,
		const char           *model,
		int                   transform)
{
	struct screenshooter_output *output;

	output = wl_output_get_user_data(wl_output);

	if (wl_output == output->output)
	{
		output->offset_x = x;
		output->offset_y = y;
	}
}

static void
display_handle_mode(void *data,
		struct wl_output *wl_output,
		uint32_t          flags,
		int               width,
		int               height,
		int               refresh)
{
	struct screenshooter_output *output;

	output = wl_output_get_user_data(wl_output);

	if ((wl_output == output->output) && (flags & WL_OUTPUT_MODE_CURRENT))
	{
		output->width  = width;
		output->height = height;
	}
}

static const struct wl_output_listener output_listener = {
	display_handle_geometry,
	display_handle_mode
};

static void
screenshot_done(void *data, struct weston_screenshooter *screenshooter)
{
	struct screenshooter_data *sh_data = data;
	sh_data->buffer_copy_done = 1;
}

static const struct weston_screenshooter_listener screenshooter_listener = {
	screenshot_done
};

static void
handle_global(void *data, struct wl_registry *registry,
		uint32_t name, const char *interface, uint32_t version)
{
	static struct screenshooter_output *output;
	struct screenshooter_data          *sh_data = data;

	if (strcmp(interface, "wl_output") == 0)
	{
		output         = xmalloc(sizeof *output);
		output->output = wl_registry_bind(registry, name,
						&wl_output_interface, 1);
		wl_list_insert(&sh_data->output_list, &output->link);
		wl_output_add_listener(output->output, &output_listener, output);
	}
	else if (strcmp(interface, "wl_shm") == 0)
	{
		sh_data->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
	}
	else if (strcmp(interface, "weston_screenshooter") == 0)
	{
		sh_data->screenshooter = wl_registry_bind(registry, name,
						&weston_screenshooter_interface,
						1);
	}
}

static void
handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
{
	/* XXX: unimplemented */
}

static const struct wl_registry_listener registry_listener = {
	handle_global,
	handle_global_remove
};

static struct wl_buffer *
screenshot_create_shm_buffer(int width, int height, void **data_out,
		struct wl_shm *shm)
{
	struct wl_shm_pool *pool;
	struct wl_buffer   *buffer;
	int                 fd, size, stride;
	void               *data;

	stride = width * 4;
	size   = stride * height;

	// printf("screenshot_create_shm_buffer width[%d] height[%d] stride[%d] size[%d]\n",width,height,stride,size);
	fd = os_create_anonymous_file(size);
	if (fd < 0)
	{
		fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
				size, strerror(errno));
		return NULL;
	}

	data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (data == MAP_FAILED)
	{
		fprintf(stderr, "mmap failed: %s\n", strerror(errno));
		close(fd);
		return NULL;
	}

	pool = wl_shm_create_pool(shm, fd, size);
	close(fd);
	buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
					WL_SHM_FORMAT_XRGB8888);
	wl_shm_pool_destroy(pool);

	*data_out = data;

	return buffer;
}


static double
get_scale(struct image *image)
{
	assert(image->matrix.xy == 0.0 &&
			image->matrix.yx == 0.0 &&
			image->matrix.xx == image->matrix.yy);
	return image->matrix.xx;
}

static void
clamp_view(struct image *image)
{
	struct rectangle allocation;
	double           scale = get_scale(image);
	double           sw, sh;
	// printf("clamp scale[%f]\n",scale);
	// printf("clamp image->width[%d] height[%d]\n",image->width,image->height);

	sw = image->width * scale;
	sh = image->height * scale;
	widget_get_allocation(image->widget, &allocation);

	// printf("clamp sw[%f] sh[%f]\n",sw,sh);
	// printf("clamp allocation.width[%d] height[%d]\n"),allocation.width,allocation.height;

	if (sw < allocation.width)
	{
		image->matrix.x0 =
				(allocation.width - image->width * scale) / 2;
	}
	else
	{
		if (image->matrix.x0 > 0.0)
		{
			image->matrix.x0 = 0.0;
		}
		if (sw + image->matrix.x0 < allocation.width)
		{
			image->matrix.x0 = allocation.width - sw;
		}
	}

	if (sh < allocation.height)
	{
		image->matrix.y0 =
				(allocation.height - image->height * scale) / 2;
	}
	else
	{
		if (image->matrix.y0 > 0.0)
		{
			image->matrix.y0 = 0.0;
		}
		if (sh + image->matrix.y0 < allocation.height)
		{
			image->matrix.y0 = allocation.height - sh;
		}
	}

	// printf("clamp matrix x0:%f y0:%fxx:%f xy:%f yx:%f yy:%f\n",image->matrix.x0,image->matrix.y0, image->matrix.xx,image->matrix.xy,image->matrix.yx,image->matrix.yy);
	// printf("x0[%f] y0[%f]\n",image->matrix.x0, image->matrix.y0);
}

static void
redraw_handler(struct widget *widget, void *data)
{
	struct image    *image = data;
	struct rectangle allocation;
	cairo_t         *cr;
	cairo_surface_t *surface;
	double           width, height, doc_aspect, window_aspect, scale;
	cairo_matrix_t   matrix;
	cairo_matrix_t   translate;

	surface = window_get_surface(image->window);
	cr      = cairo_create(surface);
	widget_get_allocation(image->widget, &allocation);
	// printf("allocation x[%d] y[%d] width[%d] height[%d]\n", allocation.x, allocation.y, allocation.width,allocation.height);

	cairo_rectangle(cr, allocation.x, allocation.y,
			allocation.width, allocation.height);
	cairo_clip(cr);
	cairo_push_group(cr);
	cairo_translate(cr, allocation.x, allocation.y);

	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
	// cairo_set_source_rgba(cr, 1, 0, 1, 0.5); // magenta transparency 50%
	cairo_set_source_rgba(cr, 0, 0, 0, 0);
	cairo_paint(cr);

	if (!image->initialized)
	{
		image->initialized = true;
		width              = cairo_image_surface_get_width(image->image);
		height             = cairo_image_surface_get_height(image->image);

		doc_aspect    = width / height;
		window_aspect = (double) allocation.width / allocation.height;
		if (doc_aspect < window_aspect)
		{
			scale = allocation.height / height;
		}
		else
		{
			scale = allocation.width / width;
		}

		// printf("doc_aspect[%f]\n",doc_aspect);
		// printf("window_aspect[%f]\n",window_aspect);
		// printf("scale[%f]\n",scale);

		image->width  = width;
		image->height = height;
		cairo_matrix_init_scale(&image->matrix, scale, scale);

		clamp_view(image);
	}

	matrix = image->matrix;
	cairo_matrix_init_translate(&translate, allocation.x, allocation.y);
	// printf("init matrix x0:%f y0:%f xx:%f xy:%f yx:%f yy:%f\n",matrix.x0,matrix.y0,matrix.xx,matrix.xy,matrix.yx,matrix.yy);
	cairo_matrix_multiply(&matrix, &matrix, &translate);
	// printf("mult matrix x0:%f y0:%f xx:%f xy:%f yx:%f yy:%f\n",matrix.x0,matrix.y0,matrix.xx,matrix.xy,matrix.yx,matrix.yy);
	cairo_set_matrix(cr, &matrix);

	cairo_set_source_surface(cr, image->image, 0, 0);
	cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
	cairo_paint(cr);

	cairo_pop_group_to_source(cr);
	cairo_paint(cr);
	cairo_destroy(cr);

	cairo_surface_destroy(surface);
}

static void
resize_handler(struct widget *widget,
		int32_t width, int32_t height, void *data)
{
	struct image *image = data;

	clamp_view(image);
}

static void
keyboard_focus_handler(struct window *window,
		struct input *device, void *data)
{
	struct image *image = data;

	window_schedule_redraw(image->window);
}

static int
enter_handler(struct widget *widget,
		struct input *input,
		float x, float y, void *data)
{
	struct image    *image = data;
	struct rectangle allocation;

	widget_get_allocation(image->widget, &allocation);
	x -= allocation.x;
	y -= allocation.y;

	image->pointer.x = x;
	image->pointer.y = y;

	return 1;
}

static void
move_viewport(struct image *image, double dx, double dy)
{
	double scale = get_scale(image);

	if (!image->initialized)
	{
		return;
	}

	cairo_matrix_translate(&image->matrix, -dx/scale, -dy/scale);
	clamp_view(image);

	window_schedule_redraw(image->window);
}

static int
motion_handler(struct widget *widget,
		struct input *input, uint32_t time,
		float x, float y, void *data)
{
	struct image    *image = data;
	struct rectangle allocation;

	widget_get_allocation(image->widget, &allocation);
	x -= allocation.x;
	y -= allocation.y;

	if (image->button_pressed)
	{
		move_viewport(image, image->pointer.x - x,
				image->pointer.y - y);
	}

	image->pointer.x = x;
	image->pointer.y = y;

	return image->button_pressed ? CURSOR_DRAGGING : CURSOR_LEFT_PTR;
}

static void
button_handler(struct widget *widget,
		struct input *input, uint32_t time,
		uint32_t button,
		enum wl_pointer_button_state state,
		void *data)
{
	struct image *image = data;

	if (button == BTN_LEFT)
	{
		image->button_pressed =
				state == WL_POINTER_BUTTON_STATE_PRESSED;

		if (state == WL_POINTER_BUTTON_STATE_PRESSED)
		{
			input_set_pointer_image(input, CURSOR_DRAGGING);
		}
		else
		{
			input_set_pointer_image(input, CURSOR_LEFT_PTR);
		}
	}
}

static void
zoom(struct image *image, double scale)
{
	double         x = image->pointer.x;
	double         y = image->pointer.y;
	cairo_matrix_t scale_matrix;

	if (!image->initialized)
	{
		return;
	}

	if ((get_scale(image) * scale > 20.0) ||
			(get_scale(image) * scale < 0.02))
	{
		return;
	}

	cairo_matrix_init_identity(&scale_matrix);
	cairo_matrix_translate(&scale_matrix, x, y);
	cairo_matrix_scale(&scale_matrix, scale, scale);
	cairo_matrix_translate(&scale_matrix, -x, -y);

	cairo_matrix_multiply(&image->matrix, &image->matrix, &scale_matrix);
	clamp_view(image);
}

static void
key_handler(struct window *window, struct input *input, uint32_t time,
		uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
		void *data)
{
	struct image *image = data;
	int           transform;

	if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
	{
		return;
	}

	// printf("XKB_KEY_f[%x] XKB_KEY_t[%x]\n",XKB_KEY_f,XKB_KEY_t);
	// printf("sym[%x]\n",sym);

	switch (sym)
	{
/*
    case XKB_KEY_minus:
        zoom(image, 0.8);
        window_schedule_redraw(image->window);
        break;
    case XKB_KEY_equal:
    case XKB_KEY_plus:
        zoom(image, 1.2);
        window_schedule_redraw(image->window);
        break;

    case XKB_KEY_1:
        image->matrix.xx = 1.0;
        image->matrix.xy = 0.0;
        image->matrix.yx = 0.0;
        image->matrix.yy = 1.0;
        clamp_view(image);
        window_schedule_redraw(image->window);
        break;

    case XKB_KEY_2:
        image->matrix.xx = 0.5;
        image->matrix.xy = 0.0;
        image->matrix.yx = 0.0;
        image->matrix.yy = 0.5;
        clamp_view(image);
        window_schedule_redraw(image->window);
        break;
 */
	case XKB_KEY_f:
		image->fullscreen ^= 1;
		printf("image->fullscreen[%d]\n",image->fullscreen);

		window_set_fullscreen(image->window, image->fullscreen);
		clamp_view(image);
		window_schedule_redraw(image->window);
		break;

/*
    case XKB_KEY_t:
        transform = window_get_buffer_transform (image->window);
        printf("transform[%d]\n",transform);
        transform = (transform + 1) % 8;
        window_set_buffer_transform(image->window, transform);
        window_schedule_redraw(image->window);
        break;
 */
	case XKB_KEY_q:
		exit(0);
		break;
	}
}

static void
axis_handler(struct widget *widget, struct input *input, uint32_t time,
		uint32_t axis, wl_fixed_t value, void *data)
{
	struct image *image = data;

	if ((axis == WL_POINTER_AXIS_VERTICAL_SCROLL) &&
			(input_get_modifiers(input) == MOD_CONTROL_MASK))
	{
		/* set zoom level to 2% per 10 axis units */
		zoom(image, (1.0 - wl_fixed_to_double(value) / 500.0));

		window_schedule_redraw(image->window);
	}
	else if (input_get_modifiers(input) == 0)
	{
		if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
		{
			move_viewport(image, 0, wl_fixed_to_double(value));
		}
		else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL)
		{
			move_viewport(image, wl_fixed_to_double(value), 0);
		}
	}
}

static void
fullscreen_handler(struct window *window, void *data)
{
	struct image *image = data;

	// image->fullscreen ^= 1;
	window_set_fullscreen(window, image->fullscreen);
}

int is_pass_close_handler = 0;
static void
close_handler(void *data)
{
	struct image *image = data;

	*image->image_counter -= 1;

	if (*image->image_counter == 0)
	{
		display_exit(image->display);
	}

	widget_destroy(image->widget);
	window_destroy(image->window);

	free(image);

	is_pass_close_handler = 1;
}

static char *
get_screenshot(const struct buffer_size *buff_size,
		struct wl_list                  *output_list)
{
	int                          output_stride, buffer_stride, i;
	cairo_surface_t             *surface;
	char                        *data, *d, *s;
	struct screenshooter_output *output, *next;
	FILE                        *fp;
	char                         filepath[PATH_MAX];

	buffer_stride = buff_size->width * 4;

	// printf("buffer_stride[%d] * buff_size->height[%d] = %d\n",buffer_stride, buff_size->height, buffer_stride * buff_size->height);
	data = xmalloc(1024 * 4 * 768);
	if (!data)
	{
		return;
	}

	wl_list_for_each_safe(output, next, output_list, link)
	{
		if (output->offset_x == 0)
		{
			output_stride = 4;
			d             = data + 1024 * 4 * 767 + 1023 * 4;
			s             = output->data;

			for (i = 0; i < 1024*768; i++)
			{
				memcpy(d, s, output_stride);
				d -= output_stride;
				s += output_stride;
			}

			wl_buffer_destroy(output->buffer);
			int ret = munmap(output->data, 1024*4*768);
			// printf("munmap error[%d]\n",ret);
			// printf("%s\n",strerror(errno));
			// free(output->buffer);
		}

		wl_registry_destroy(output->output);
		free(output);
	}

	// fp = file_create_dated("/tmp", "raw-",
	//             ".png", filepath, sizeof(filepath));
	// if (fp) {
	//  fwrite(data, 1, 1024 * 4 * 768, fp);
	//  fclose (fp);
	// }

	return data;
}

static struct image *
image_create(struct display *display, const char *filename,
		int *image_counter, char *cap_img)
{
	struct image *image;
	char         *b, *copy, title[512];

	image = zalloc(sizeof *image);
	if (image == NULL)
	{
		return image;
	}

	sprintf(title,"weston_clone_app");
	image->filename = strdup(title);

	image->image = cairo_image_surface_create_for_data(cap_img, CAIRO_FORMAT_ARGB32, 1024, 768, 1024*4);
	double width  = cairo_image_surface_get_width(image->image);
	double height = cairo_image_surface_get_height(image->image);
	printf("1 width[%f] height[%f]\n",width,height);

	if (!image->image)
	{
		free(image->filename);
		free(image);

		printf("image->image failed\n");
		return NULL;
	}

	image->window = window_create(display);
	image->widget = window_frame_create(image->window, image);
	window_set_title(image->window, title);
	image->display       = display;
	image->image_counter = image_counter;
	*image_counter      += 1;
	image->initialized   = false;

	window_set_user_data(image->window, image);
	widget_set_redraw_handler(image->widget, redraw_handler);
	// widget_set_resize_handler(image->widget, resize_handler);
	// window_set_keyboard_focus_handler(image->window, keyboard_focus_handler);
	// window_set_fullscreen_handler(image->window, fullscreen_handler);
	window_set_close_handler(image->window, close_handler);

	// widget_set_enter_handler(image->widget, enter_handler);
	// widget_set_motion_handler(image->widget, motion_handler);
	// widget_set_button_handler(image->widget, button_handler);
	// widget_set_axis_handler(image->widget, axis_handler);
	window_set_key_handler(image->window, key_handler);
	widget_schedule_resize(image->widget, 600, 400);

	return image;
}

static void display_run_nonloop(struct display *display)
{
	struct task       *task;
	struct epoll_event ep[16];
	int                i, count, ret;
	int                idx = 0;

	display->running = 1;
	// while (1)
	{
		while (!wl_list_empty(&display->deferred_list))
		{
			task = container_of(display->deferred_list.prev,
							struct task, link);
			wl_list_remove(&task->link);
			task->run(task, 0);
		}

		wl_display_dispatch_pending(display->display);

		ret = wl_display_flush(display->display);
		if ((ret < 0) && (errno == EAGAIN))
		{
			ep[0].events =
					EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
			ep[0].data.ptr = &display->display_task;

			epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
					display->display_fd, &ep[0]);
		}
		else if (ret < 0)
		{
			// break;
			return;
		}

		count = epoll_wait(display->epoll_fd,
						ep, ARRAY_LENGTH(ep), -1);
		for (i = 0; i < count; i++)
		{
			task = ep[i].data.ptr;
			task->run(task, ep[i].events);
		}
	}
}

static int
screenshot_set_buffer_size(struct buffer_size *buff_size, struct wl_list *output_list)
{
	struct screenshooter_output *output;
	buff_size->min_x = buff_size->min_y = INT_MAX;
	buff_size->max_x = buff_size->max_y = INT_MIN;
	int position = 0;

	wl_list_for_each_reverse(output, output_list, link)
	{
		output->offset_x = position;
		position        += output->width;
	}

	wl_list_for_each(output, output_list, link)
	{
		buff_size->min_x = MIN(buff_size->min_x, output->offset_x);
		buff_size->min_y = MIN(buff_size->min_y, output->offset_y);
		buff_size->max_x =
				MAX(buff_size->max_x, output->offset_x + output->width);
		buff_size->max_y =
				MAX(buff_size->max_y, output->offset_y + output->height);
	}

	if ((buff_size->max_x <= buff_size->min_x) ||
			(buff_size->max_y <= buff_size->min_y))
	{
		return -1;
	}

	buff_size->width  = buff_size->max_x - buff_size->min_x;
	buff_size->height = buff_size->max_y - buff_size->min_y;

	return 0;
}

char *get_weston_screen_image()
{
	struct wl_display           *display_cap; // screenshooter variable
	struct wl_registry          *registry;
	struct screenshooter_output *output;
	struct buffer_size           buff_size = {};
	struct screenshooter_data    sh_data   = {};
	char                        *cap_img   = NULL;

	// start of screenshoot
	display_cap = wl_display_connect(NULL);
	if (display_cap == NULL)
	{
		fprintf(stderr, "failed to create display: %s\n",
				strerror(errno));
		return -1;
	}

	wl_list_init(&sh_data.output_list);
	registry = wl_display_get_registry(display_cap);
	wl_registry_add_listener(registry, &registry_listener, &sh_data);
	wl_display_dispatch(display_cap);
	// printf("0.1 sh_data.shm[%p]\n",sh_data.shm);
	wl_display_roundtrip(display_cap);
	if (sh_data.screenshooter == NULL)
	{
		fprintf(stderr, "display doesn't support screenshooter\n");
		return -1;
	}
	weston_screenshooter_add_listener(sh_data.screenshooter,
			&screenshooter_listener,
			&sh_data);
	if (screenshot_set_buffer_size(&buff_size, &sh_data.output_list))
	{
		printf("screenshot_set_buffer_size fail\n");
		// continue;
		return -1;
	}

	int idx = 0;
	wl_list_for_each(output, &sh_data.output_list, link)
	{
		if ((output->offset_x == 0) &&
				((output->width == 1024) && (output->height == 768)))
		{
			// printf("output->offset_x[%d]\n",output->offset_x);
			output->buffer =
					screenshot_create_shm_buffer(output->width,
							output->height,
							&output->data,
							sh_data.shm);
			if (output->buffer == NULL)
			{
				continue;
			}

			weston_screenshooter_shoot(sh_data.screenshooter,
					output->output,
					output->buffer);

			sh_data.buffer_copy_done = 0;
			wl_display_roundtrip(display_cap);
			usleep(35000);

			while (!sh_data.buffer_copy_done)
			{
				wl_display_roundtrip(display_cap);
				usleep(1000);
			}
		}
	}
	// end of screenshoot

	if (buff_size.height == 768)
	{
		cap_img = get_screenshot(&buff_size, &sh_data.output_list);
	}
	else
	{
		struct screenshooter_output *output, *next;
		wl_list_for_each_safe(output, next, &sh_data.output_list, link)
		{
			int ret = munmap(output->data, 1024*4*768);
			// printf("munmap error[%d]\n",ret);
			// printf("%s\n",strerror(errno));

			wl_registry_destroy(output->output);
			wl_list_remove(&output->link);
			wl_buffer_destroy(output->buffer);
			// free(output->buffer);
			free(output);
		}
	}

	wl_registry_destroy(sh_data.shm);
	wl_registry_destroy(sh_data.screenshooter);

	wl_registry_destroy(registry);
	wl_display_disconnect(display_cap);

	return cap_img;
}

int main(int argc, char *argv[])
{
	struct display *d;          // image.c variable
	int             i;
	int             image_counter = 0;
	char           *cap_img = NULL;
	struct timespec start_time, end_time;

	d       = display_create(&argc, argv);
	cap_img = get_weston_screen_image();
	pimage  = image_create(d, argv[i], &image_counter,cap_img);

	// full screen with 1x zoom scale
	window_set_fullscreen(pimage->window, pimage->fullscreen = 1);
	pimage->matrix.xx                                        = 1.0;
	pimage->matrix.xy                                        = 0.0;
	pimage->matrix.yx                                        = 0.0;
	pimage->matrix.yy                                        = 1.0;
	clamp_view(pimage);
	window_schedule_redraw(pimage->window);
	display_run_nonloop(d);

	struct sigaction sigint;
	sigint.sa_handler = handler_sigint;
	sigemptyset(&sigint.sa_mask);
	sigint.sa_flags = SA_RESETHAND;
	sigaction(SIGINT, &sigint, NULL);

	clock_gettime(CLOCK_MONOTONIC, &end_time);
	memcpy(&start_time, &end_time, sizeof(struct timespec));

	int       loop = 0;
	long long usec;
	while (isloop)
	{
		// ui loop
		display_run_nonloop(d); // 20 msec

		// reload image
		free(cap_img);
		cap_img = get_weston_screen_image();    // 80 msec
		cairo_surface_destroy(pimage->image);   // 70 usec
		pimage->image = cairo_image_surface_create_for_data(cap_img, CAIRO_FORMAT_ARGB32,1024, 768, 1024*4);

		// ui redraw request
		window_schedule_redraw(pimage->window);

		// fps calc.
		clock_gettime(CLOCK_MONOTONIC, &end_time);
		usec = (end_time.tv_sec - start_time.tv_sec) * 1000000 + (end_time.tv_nsec - start_time.tv_nsec) / 1000;
		// printf("%d : %lld usec / %lld fps\n", loop++, usec, 1000000UL / usec);
		memcpy(&start_time, &end_time, sizeof(struct timespec));
	}

	free(cap_img);
	if (!is_pass_close_handler)
	{
		widget_destroy(pimage->widget);
		window_destroy(pimage->window);
	}
	cairo_surface_destroy(pimage->image);
	free(pimage);

	display_destroy(d);

	return 0;
}