summaryrefslogtreecommitdiff
path: root/clients/resizor.c
blob: 5d342e1fc33f960a2ee1cf293730d3692f0fbc4d (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
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
/*
 * Copyright © 2010 Intel Corporation
 *
 * 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 <stdbool.h>
#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 "shared/xalloc.h"

struct spring {
	double current;
	double target;
	double previous;
};

struct resizor {
	struct display *display;
	struct window *window;
	struct widget *widget;
	struct window *menu;
	struct spring width;
	struct spring height;
	struct wl_callback *frame_callback;
	bool pointer_locked;
	struct input *locked_input;
	float pointer_x;
	float pointer_y;
};

static void
spring_update(struct spring *spring)
{
	double current, force;

	current = spring->current;
	force = (spring->target - current) / 20.0 +
		(spring->previous - current);

	spring->current = current + (current - spring->previous) + force;
	spring->previous = current;
}

static int
spring_done(struct spring *spring)
{
	return fabs(spring->previous - spring->target) < 0.1;
}

static const struct wl_callback_listener listener;

static void
frame_callback(void *data, struct wl_callback *callback, uint32_t time)
{
	struct resizor *resizor = data;

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

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

	if (window_is_maximized(resizor->window))
		return;

	spring_update(&resizor->width);
	spring_update(&resizor->height);

	widget_schedule_resize(resizor->widget,
			       resizor->width.current + 0.5,
			       resizor->height.current + 0.5);

	if (!spring_done(&resizor->width) || !spring_done(&resizor->height)) {
		resizor->frame_callback =
			wl_surface_frame(
				window_get_wl_surface(resizor->window));
		wl_callback_add_listener(resizor->frame_callback, &listener,
					 resizor);
	}
}

static const struct wl_callback_listener listener = {
	frame_callback
};

static void
redraw_handler(struct widget *widget, void *data)
{
	struct resizor *resizor = data;
	cairo_surface_t *surface;
	cairo_t *cr;
	struct rectangle allocation;

	widget_get_allocation(resizor->widget, &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
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, enum wl_keyboard_key_state state,
	    void *data)
{
	struct resizor *resizor = data;
	struct rectangle allocation;

	if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
		return;

	window_get_allocation(resizor->window, &allocation);
	resizor->width.current = allocation.width;
	if (spring_done(&resizor->width))
		resizor->width.target = allocation.width;
	resizor->height.current = allocation.height;
	if (spring_done(&resizor->height))
		resizor->height.target = allocation.height;

	switch (sym) {
	case XKB_KEY_Up:
		if (allocation.height < 400)
			break;

		resizor->height.target = allocation.height - 200;
		break;

	case XKB_KEY_Down:
		if (allocation.height > 1000)
			break;

		resizor->height.target = allocation.height + 200;
		break;

	case XKB_KEY_Left:
		if (allocation.width < 400)
			break;

		resizor->width.target = allocation.width - 200;
		break;

	case XKB_KEY_Right:
		if (allocation.width > 1000)
			break;

		resizor->width.target = allocation.width + 200;
		break;

	case XKB_KEY_Escape:
		display_exit(resizor->display);
		break;
	}

	if (!resizor->frame_callback)
		frame_callback(resizor, NULL, 0);
}

static void
menu_func(void *data, struct input *input, int index)
{
	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);
	window_show_menu(resizor->display, input, time, resizor->window,
			 x - 10, y - 10, menu_func, entries, 4);
}

static void
locked_pointer_handle_motion(struct window *window,
			     struct input *input,
			     uint32_t time,
			     float dx,
			     float dy,
			     void *data)
{
	struct resizor *resizor = data;

	resizor->width.current += dx;
	resizor->width.previous = resizor->width.current;
	resizor->width.target = resizor->width.current;

	resizor->height.current += dy;
	resizor->height.previous = resizor->height.current;
	resizor->height.target = resizor->height.current;

	widget_schedule_resize(resizor->widget,
			       resizor->width.current,
			       resizor->height.current);
}

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

	resizor->pointer_locked = true;
	input_set_pointer_image(input, CURSOR_BLANK);
}

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

	resizor->pointer_locked = false;
	input_set_pointer_image(input, CURSOR_LEFT_PTR);
}

static const struct wl_callback_listener locked_pointer_frame_listener;

static void
locked_pointer_frame_callback(void *data,
			      struct wl_callback *callback,
			      uint32_t time)
{
	struct resizor *resizor = data;
	struct wl_surface *surface;
	struct rectangle allocation;
	float x, y;

	if (resizor->pointer_locked) {
		widget_get_allocation(resizor->widget, &allocation);

		x = resizor->pointer_x + (allocation.width - allocation.x);
		y = resizor->pointer_y + (allocation.height - allocation.y);

		widget_set_locked_pointer_cursor_hint(resizor->widget, x, y);
	}

	wl_callback_destroy(callback);

	surface = window_get_wl_surface(resizor->window);
	callback = wl_surface_frame(surface);
	wl_callback_add_listener(callback,
				 &locked_pointer_frame_listener,
				 resizor);
}

static const struct wl_callback_listener locked_pointer_frame_listener = {
	locked_pointer_frame_callback
};

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 resizor *resizor = data;
	struct rectangle allocation;
	struct wl_surface *surface;
	struct wl_callback *callback;

	if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
		show_menu(resizor, input, time);
	} else if (button == BTN_LEFT &&
		   state == WL_POINTER_BUTTON_STATE_PRESSED) {
		window_get_allocation(resizor->window, &allocation);

		resizor->width.current = allocation.width;
		resizor->width.previous = allocation.width;
		resizor->width.target = allocation.width;

		resizor->height.current = allocation.height;
		resizor->height.previous = allocation.height;
		resizor->height.target = allocation.height;

		window_lock_pointer(resizor->window, input);
		window_set_pointer_locked_handler(resizor->window,
						  handle_pointer_locked,
						  handle_pointer_unlocked);
		resizor->locked_input = input;

		surface = window_get_wl_surface(resizor->window);
		callback = wl_surface_frame(surface);
		wl_callback_add_listener(callback,
					 &locked_pointer_frame_listener,
					 resizor);
	} else if (button == BTN_LEFT &&
		   state == WL_POINTER_BUTTON_STATE_RELEASED) {
		input_set_pointer_image(input, CURSOR_LEFT_PTR);
		window_unlock_pointer(resizor->window);
	}
}

static void
set_cursor_inv_offset(struct resizor *resizor, float x, float y)
{
	struct rectangle allocation;

	widget_get_allocation(resizor->widget, &allocation);

	resizor->pointer_x = x - (allocation.width - allocation.x);
	resizor->pointer_y = y - (allocation.height - allocation.y);
}

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

	set_cursor_inv_offset(resizor, x , y);

	return CURSOR_LEFT_PTR;
}

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

	set_cursor_inv_offset(resizor, x , y);

	return CURSOR_LEFT_PTR;
}

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

	resizor = xzalloc(sizeof *resizor);
	resizor->window = window_create(display);
	resizor->widget = window_frame_create(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);
	widget_set_redraw_handler(resizor->widget, redraw_handler);
	window_set_keyboard_focus_handler(resizor->window,
					  keyboard_focus_handler);

	widget_set_enter_handler(resizor->widget, enter_handler);
	widget_set_motion_handler(resizor->widget, motion_handler);

	window_set_locked_pointer_motion_handler(
			resizor->window, locked_pointer_handle_motion);

	widget_set_button_handler(resizor->widget, button_handler);

	resizor->height.previous = 400;
	resizor->height.current = 400;
	resizor->height.target = 400;

	resizor->width.previous = 400;
	resizor->width.current = 400;
	resizor->width.target = 400;

	widget_schedule_resize(resizor->widget, 400, 400);

	return resizor;
}

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

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

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

	display = display_create(&argc, argv);
	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;
}