/*
 * Copyright © 2008 Kristian Høgsberg
 * Copyright © 2012 Intel Corporation
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <cairo.h>

#include <linux/input.h>
#include <wayland-client.h>
#include "window.h"

struct transformed {
	struct display *display;
	struct window *window;
	struct widget *widget;
	int width, height;
	int fullscreen;
};

static void
update_transform(cairo_t *cr, enum wl_output_transform transform)
{
	double angle;

	cairo_matrix_t m;

	switch(transform) {
	case WL_OUTPUT_TRANSFORM_FLIPPED:
	case WL_OUTPUT_TRANSFORM_FLIPPED_90:
	case WL_OUTPUT_TRANSFORM_FLIPPED_180:
	case WL_OUTPUT_TRANSFORM_FLIPPED_270:
		cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
		break;
	default:
		cairo_matrix_init_identity(&m);
		break;
	}
	switch (transform) {
	case WL_OUTPUT_TRANSFORM_NORMAL:
	case WL_OUTPUT_TRANSFORM_FLIPPED:
	default:
		angle = 0;
		break;
	case WL_OUTPUT_TRANSFORM_90:
	case WL_OUTPUT_TRANSFORM_FLIPPED_90:
		angle = M_PI_2;
		break;
	case WL_OUTPUT_TRANSFORM_180:
	case WL_OUTPUT_TRANSFORM_FLIPPED_180:
		angle = M_PI;
		break;
	case WL_OUTPUT_TRANSFORM_270:
	case WL_OUTPUT_TRANSFORM_FLIPPED_270:
		angle = M_PI + M_PI_2;
		break;
	}

	cairo_rotate(cr, angle);
	cairo_transform(cr, &m);
}

static void
draw_stuff(cairo_surface_t *surface, int width, int height, int transform)
{
	cairo_t *cr;
	int tmp;

	if (transform & 1) {
		tmp = width;
		width = height;
		height = tmp;
	}

	cr = cairo_create(surface);

	cairo_identity_matrix(cr);
	cairo_translate(cr, width / 2, height / 2);
	cairo_scale(cr, width / 2, height / 2);

	update_transform(cr, transform);

	cairo_set_source_rgba(cr, 0, 0, 0.3, 1.0);
	cairo_set_source_rgba(cr, 0, 0, 0, 1.0);
	cairo_rectangle(cr, -1, -1, 2, 2);
	cairo_fill(cr);

	cairo_set_source_rgb(cr, 1, 0, 0);
	cairo_move_to(cr, 0,  0);
	cairo_line_to(cr, 0, -1);

	cairo_save(cr);
	cairo_identity_matrix(cr);
	cairo_set_line_width(cr, 2.0);
	cairo_stroke(cr);
	cairo_restore(cr);

	cairo_set_source_rgb(cr, 0, 1, 0);
	cairo_move_to(cr, 0, 0);
	cairo_line_to(cr, 1, 0);

	cairo_save(cr);
	cairo_identity_matrix(cr);
	cairo_set_line_width(cr, 2.0);
	cairo_stroke(cr);
	cairo_restore(cr);

	cairo_set_source_rgb(cr, 1, 1, 1);
	cairo_move_to(cr, 0, 0);
	cairo_line_to(cr, 0, 1);
	cairo_move_to(cr,  0, 0);
	cairo_line_to(cr, -1, 0);

	cairo_save(cr);
	cairo_identity_matrix(cr);
	cairo_set_line_width(cr, 2.0);
	cairo_stroke(cr);
	cairo_restore(cr);

	cairo_destroy(cr);
}

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

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

static void
redraw_handler(struct widget *widget, void *data)
{
	struct transformed *transformed = data;
	struct rectangle allocation;
	cairo_surface_t *surface;
	int transform;

	surface = window_get_surface(transformed->window);
	if (surface == NULL ||
	    cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
		fprintf(stderr, "failed to create cairo egl surface\n");
		return;
	}

	widget_get_allocation(transformed->widget, &allocation);
	transform = window_get_buffer_transform(transformed->window);

	draw_stuff(surface, allocation.width, allocation.height, transform);

	cairo_surface_destroy(surface);
}

static void
output_handler(struct window *window, struct output *output, int enter,
	       void *data)
{
	if (!enter)
		return;

	window_set_buffer_transform(window, output_get_transform(output));
	window_schedule_redraw(window);
}

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 transformed *transformed = data;

	switch (button) {
	case BTN_LEFT:
		if (state == WL_POINTER_BUTTON_STATE_PRESSED)
			window_move(transformed->window, input,
				    display_get_serial(transformed->display));
		break;
	case BTN_MIDDLE:
		if (state == WL_POINTER_BUTTON_STATE_PRESSED)
			widget_schedule_redraw(widget);
		break;
	case BTN_RIGHT:
		if (state == WL_POINTER_BUTTON_STATE_PRESSED)
			window_show_frame_menu(transformed->window, input, time);
		break;
	}
}

int main(int argc, char *argv[])
{
	struct transformed transformed;
	struct display *d;

	d = display_create(argc, argv);
	if (d == NULL) {
		fprintf(stderr, "failed to create display: %m\n");
		return -1;
	}

	transformed.width = 500;
	transformed.height = 250;
	transformed.display = d;
	transformed.fullscreen = 0;
	transformed.window = window_create(d);
	transformed.widget =
		window_add_widget(transformed.window, &transformed);
	window_set_title(transformed.window, "Transformed");

	widget_set_transparent(transformed.widget, 0);
	widget_set_default_cursor(transformed.widget, CURSOR_BLANK);

	widget_set_redraw_handler(transformed.widget, redraw_handler);
	widget_set_button_handler(transformed.widget, button_handler);

	window_set_fullscreen_handler(transformed.window, fullscreen_handler);
	window_set_output_handler(transformed.window, output_handler);

	window_set_user_data(transformed.window, &transformed);
	window_schedule_resize(transformed.window,
			       transformed.width, transformed.height);

	display_run(d);

	return 0;
}