summaryrefslogtreecommitdiff
path: root/clients/resizor.c
blob: 395fdcdb0d6adf5ba9e61b2a80beb723ee8fc597 (plain)
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
/*
 * Copyright © 2010 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 <cairo.h>
#include <math.h>
#include <assert.h>

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

#include "window.h"

#include <X11/keysym.h>

struct resizor {
	struct display *display;
	struct window *window;
	struct widget *widget;
	struct window *menu;
	int32_t width;

	struct {
		double current;
		double target;
		double previous;
	} height;
	struct wl_callback *frame_callback;
};

static void
frame_callback(void *data, struct wl_callback *callback, uint32_t time)
{
	static const struct wl_callback_listener listener = {
		frame_callback
	};
	struct resizor *resizor = data;
	double force, height;

	assert(!callback || callback == resizor->frame_callback);

	height = resizor->height.current;
	force = (resizor->height.target - height) / 10.0 +
		(resizor->height.previous - height);

	resizor->height.current =
		height + (height - resizor->height.previous) + force;
	resizor->height.previous = height;

	if (resizor->height.current >= 400) {
		resizor->height.current = 400;
		resizor->height.previous = 400;
	}

	if (resizor->height.current <= 200) {
		resizor->height.current = 200;
		resizor->height.previous = 200;
	}

	window_set_child_size(resizor->window, resizor->width, height + 0.5);

	window_schedule_redraw(resizor->window);

	if (resizor->frame_callback) {
		wl_callback_destroy(resizor->frame_callback);
		resizor->frame_callback = NULL;
	}

	if (fabs(resizor->height.previous - resizor->height.target) > 0.1) {
		resizor->frame_callback =
			wl_surface_frame(
				window_get_wl_surface(resizor->window));
		wl_callback_add_listener(resizor->frame_callback, &listener,
					 resizor);
	}
}

static void
resizor_draw(struct resizor *resizor)
{
	cairo_surface_t *surface;
	cairo_t *cr;
	struct rectangle allocation;

	window_get_child_allocation(resizor->window, &allocation);

	surface = window_get_surface(resizor->window);

	cr = cairo_create(surface);
	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
	cairo_rectangle(cr,
			allocation.x,
			allocation.y,
			allocation.width,
			allocation.height);
	cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
	cairo_fill(cr);
	cairo_destroy(cr);

	cairo_surface_destroy(surface);
}

static void
redraw_handler(struct window *window, void *data)
{
	struct resizor *resizor = data;

	resizor_draw(resizor);
}

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

	window_schedule_redraw(resizor->window);
}

static void
key_handler(struct window *window, struct input *input, uint32_t time,
	    uint32_t key, uint32_t sym, uint32_t state, void *data)
{
	struct resizor *resizor = data;

	if (state == 0)
		return;

	switch (sym) {
	case XK_Down:
		resizor->height.target = 400;
		frame_callback(resizor, NULL, 0);
		break;
	case XK_Up:
		resizor->height.target = 200;
		frame_callback(resizor, NULL, 0);
		break;
	case XK_Escape:
		display_exit(resizor->display);
		break;
	}
}

static void
menu_func(struct window *window, int index, void *user_data)
{
	fprintf(stderr, "picked entry %d\n", index);
}

static void
show_menu(struct resizor *resizor, struct input *input, uint32_t time)
{
	int32_t x, y;
	static const char *entries[] = {
		"Roy", "Pris", "Leon", "Zhora"
	};

	input_get_position(input, &x, &y);
	resizor->menu = window_create_menu(resizor->display,
					   input, time, resizor->window,
					   x - 10, y - 10,
					   menu_func, entries, 4);

	window_schedule_redraw(resizor->menu);
}

static void
button_handler(struct widget *widget,
	       struct input *input, uint32_t time,
	       int button, int state, void *data)
{
	struct resizor *resizor = data;

	switch (button) {
	case BTN_RIGHT:
		if (state)
			show_menu(resizor, input, time);
		break;
	}
}

static struct resizor *
resizor_create(struct display *display)
{
	struct resizor *resizor;
	int32_t height;

	resizor = malloc(sizeof *resizor);
	if (resizor == NULL)
		return resizor;
	memset(resizor, 0, sizeof *resizor);

	resizor->window = window_create(display, 500, 400);
	resizor->widget = window_add_widget(resizor->window, resizor);
	window_set_title(resizor->window, "Wayland Resizor");
	resizor->display = display;

	window_set_key_handler(resizor->window, key_handler);
	window_set_user_data(resizor->window, resizor);
	window_set_redraw_handler(resizor->window, redraw_handler);
	window_set_keyboard_focus_handler(resizor->window,
					  keyboard_focus_handler);

	resizor->width = 300;
	resizor->height.current = 400;
	resizor->height.previous = resizor->height.current;
	resizor->height.target = resizor->height.current;
	height = resizor->height.current + 0.5;

	window_set_child_size(resizor->window, resizor->width, height);
	widget_set_button_handler(resizor->widget, button_handler);

	window_schedule_redraw(resizor->window);

	return resizor;
}

static void
resizor_destroy(struct resizor *resizor)
{
	if (resizor->frame_callback)
		wl_callback_destroy(resizor->frame_callback);

	window_destroy(resizor->window);
	free(resizor);
}

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

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

	resizor = resizor_create(display);

	display_run(display);

	resizor_destroy(resizor);
	display_destroy(display);

	return 0;
}