aboutsummaryrefslogtreecommitdiff
path: root/sway/input_state.c
blob: 25308a77a62dd750f9343aeb559cae70867e1496 (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
450
451
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include "log.h"
#include "config.h"

#include "input_state.h"

#define KEY_STATE_MAX_LENGTH 64

struct key_state {
	/*
	 * Aims to store state regardless of modifiers.
	 * If you press a key, then hold shift, then release the key, we'll
	 * get two different key syms, but the same key code. This handles
	 * that scenario and makes sure we can use the right bindings.
	 */
	uint32_t key_sym;
	uint32_t alt_sym;
	uint32_t key_code;
};

static struct key_state key_state_array[KEY_STATE_MAX_LENGTH];

void input_init(void) {
	int i;
	for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) {
		struct key_state none = { 0, 0, 0 };
		key_state_array[i] = none;
	}
}

static uint8_t find_key(uint32_t key_sym, uint32_t key_code, bool update) {
	int i;
	for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) {
		if (0 == key_sym && 0 == key_code && key_state_array[i].key_sym == 0) {
			break;
		}
		if (key_state_array[i].key_sym == key_sym
			|| key_state_array[i].alt_sym == key_sym) {
			break;
		}
		if (update && key_state_array[i].key_code == key_code) {
			key_state_array[i].alt_sym = key_sym;
			break;
		}
	}
	return i;
}

bool check_key(uint32_t key_sym, uint32_t key_code) {
	return find_key(key_sym, key_code, false) < KEY_STATE_MAX_LENGTH;
}

void press_key(uint32_t key_sym, uint32_t key_code) {
	if (key_code == 0) {
		return;
	}
	// Check if key exists
	if (!check_key(key_sym, key_code)) {
		// Check that we dont exceed buffer length
		int insert = find_key(0, 0, true);
		if (insert < KEY_STATE_MAX_LENGTH) {
			key_state_array[insert].key_sym = key_sym;
			key_state_array[insert].key_code = key_code;
		}
	}
}

void release_key(uint32_t key_sym, uint32_t key_code) {
	uint8_t index = find_key(key_sym, key_code, true);
	if (index < KEY_STATE_MAX_LENGTH) {
		struct key_state none = { 0, 0, 0 };
		key_state_array[index] = none;
	}
}

// Pointer state and mode

struct pointer_state pointer_state;

static struct mode_state {
	// initial view state
	double x, y, w, h;
	swayc_t *ptr;
	// Containers used for resizing horizontally
	struct {
		double w;
		swayc_t *ptr;
		struct {
			double w;
			swayc_t *ptr;
		} parent;
	} horiz;
	// Containers used for resizing vertically
	struct {
		double h;
		swayc_t *ptr;
		struct {
			double h;
			swayc_t *ptr;
		} parent;
	} vert;
} initial;

static struct {
	bool left;
	bool top;
} lock;

// initial set/unset

static void set_initial_view(swayc_t *view) {
	initial.ptr = view;
	initial.x = view->x;
	initial.y = view->y;
	initial.w = view->width;
	initial.h = view->height;
}

static void set_initial_sibling(void) {
	bool reset = true;
	swayc_t *ws = swayc_active_workspace_for(initial.ptr);
	if ((initial.horiz.ptr = get_swayc_in_direction_under(initial.ptr,
			lock.left ? MOVE_RIGHT: MOVE_LEFT, ws))) {
		initial.horiz.w = initial.horiz.ptr->width;
		initial.horiz.parent.ptr = get_swayc_in_direction_under(initial.horiz.ptr,
			lock.left ? MOVE_LEFT : MOVE_RIGHT, ws);
		initial.horiz.parent.w = initial.horiz.parent.ptr->width;
		reset = false;
	}
	if ((initial.vert.ptr = get_swayc_in_direction_under(initial.ptr,
			lock.top ? MOVE_DOWN: MOVE_UP, ws))) {
		initial.vert.h = initial.vert.ptr->height;
		initial.vert.parent.ptr = get_swayc_in_direction_under(initial.vert.ptr,
			lock.top ? MOVE_UP : MOVE_DOWN, ws);
		initial.vert.parent.h = initial.vert.parent.ptr->height;
		reset = false;
	}
	// If nothing will change just undo the mode
	if (reset) {
		pointer_state.mode = 0;
	}
}

static void reset_initial_view(void) {
	initial.ptr->x = initial.x;
	initial.ptr->y = initial.y;
	initial.ptr->width = initial.w;
	initial.ptr->height = initial.h;
	arrange_windows(initial.ptr, -1, -1);
	pointer_state.mode = 0;
}

static void reset_initial_sibling(void) {
	initial.horiz.ptr->width = initial.horiz.w;
	initial.horiz.parent.ptr->width = initial.horiz.parent.w;
	initial.vert.ptr->height = initial.vert.h;
	initial.vert.parent.ptr->height = initial.vert.parent.h;
	arrange_windows(initial.horiz.ptr->parent, -1, -1);
	arrange_windows(initial.vert.ptr->parent, -1, -1);
	pointer_state.mode = 0;
}

void pointer_position_set(struct wlc_point *new_origin, bool force_focus) {
	struct wlc_point origin;
	wlc_pointer_get_position(&origin);
	pointer_state.delta.x = new_origin->x - origin.x;
	pointer_state.delta.y = new_origin->y - origin.y;

	// Update view under pointer
	swayc_t *prev_view = pointer_state.view;
	pointer_state.view = container_under_pointer();

	// If pointer is in a mode, update it
	if (pointer_state.mode) {
		pointer_mode_update();
	// Otherwise change focus if config is set
	} else if (force_focus || (prev_view != pointer_state.view && config->focus_follows_mouse)) {
		if (pointer_state.view && pointer_state.view->type == C_VIEW) {
			set_focused_container(pointer_state.view);
		}
	}

	wlc_pointer_set_position(new_origin);
}

void center_pointer_on(swayc_t *view) {
	struct wlc_point new_origin;
	new_origin.x = view->x + view->width/2;
	new_origin.y = view->y + view->height/2;
	pointer_position_set(&new_origin, true);
}

// Mode set left/right click

static void pointer_mode_set_dragging(void) {
	switch (config->dragging_key) {
	case M_LEFT_CLICK:
		set_initial_view(pointer_state.left.view);
		break;
	case M_RIGHT_CLICK:
		set_initial_view(pointer_state.right.view);
		break;
	}

	if (initial.ptr->is_floating) {
		pointer_state.mode = M_DRAGGING | M_FLOATING;
	} else {
		pointer_state.mode = M_DRAGGING | M_TILING;
		// unset mode if we cant drag tile
		if (initial.ptr->parent->type == C_WORKSPACE &&
				initial.ptr->parent->children->length == 1) {
			pointer_state.mode = 0;
		}
	}
}

static void pointer_mode_set_resizing(void) {
	switch (config->resizing_key) {
	case M_LEFT_CLICK:
		set_initial_view(pointer_state.left.view);
		break;
	case M_RIGHT_CLICK:
		set_initial_view(pointer_state.right.view);
		break;
	}
	// Setup locking information
	int midway_x = initial.ptr->x + initial.ptr->width/2;
	int midway_y = initial.ptr->y + initial.ptr->height/2;

	struct wlc_point origin;
	wlc_pointer_get_position(&origin);
	lock.left = origin.x > midway_x;
	lock.top = origin.y > midway_y;

	if (initial.ptr->is_floating) {
		pointer_state.mode = M_RESIZING | M_FLOATING;
	} else {
		pointer_state.mode = M_RESIZING | M_TILING;
		set_initial_sibling();
	}
}

// Mode set/update/reset

void pointer_mode_set(uint32_t button, bool condition) {
	// switch on drag/resize mode
	switch (pointer_state.mode & (M_DRAGGING | M_RESIZING)) {
	case M_DRAGGING:
		// end drag mode when 'dragging' click is unpressed
		if (config->dragging_key == M_LEFT_CLICK && !pointer_state.left.held) {
			pointer_state.mode = 0;
		} else if (config->dragging_key == M_RIGHT_CLICK && !pointer_state.right.held) {
			pointer_state.mode = 0;
		}
		break;

	case M_RESIZING:
		// end resize mode when 'resizing' click is unpressed
		if (config->resizing_key == M_LEFT_CLICK && !pointer_state.left.held) {
			pointer_state.mode = 0;
		} else if (config->resizing_key == M_RIGHT_CLICK && !pointer_state.right.held) {
			pointer_state.mode = 0;
		}
		break;

	// No mode case
	default:
		// return if failed condition, or no view
		if (!condition || !pointer_state.view) {
			break;
		}

		// Set mode depending on current button press
		switch (button) {
		// Start left-click mode
		case M_LEFT_CLICK:
			// if button release dont do anything
			if (pointer_state.left.held) {
				if (config->dragging_key == M_LEFT_CLICK) {
					pointer_mode_set_dragging();
				} else if (config->resizing_key == M_LEFT_CLICK) {
					pointer_mode_set_resizing();
				}
			}
			break;

		// Start right-click mode
		case M_RIGHT_CLICK:
			// if button release dont do anyhting
			if (pointer_state.right.held) {
				if (config->dragging_key == M_RIGHT_CLICK) {
					pointer_mode_set_dragging();
				} else if (config->resizing_key == M_RIGHT_CLICK) {
					pointer_mode_set_resizing();
				}
			}
			break;
		}
	}
}

void pointer_mode_update(void) {
	if (initial.ptr->type != C_VIEW) {
		pointer_state.mode = 0;
		return;
	}
	struct wlc_point origin;
	wlc_pointer_get_position(&origin);
	int dx = origin.x;
	int dy = origin.y;

	switch (pointer_state.mode) {
	case M_FLOATING | M_DRAGGING:
		// Update position
		switch (config->dragging_key) {
		case M_LEFT_CLICK:
			dx -= pointer_state.left.x;
			dy -= pointer_state.left.y;
			break;
		case M_RIGHT_CLICK:
			dx -= pointer_state.right.x;
			dy -= pointer_state.right.y;
			break;
		}

		if (initial.x + dx != initial.ptr->x) {
			initial.ptr->x = initial.x + dx;
		}
		if (initial.y + dy != initial.ptr->y) {
			initial.ptr->y = initial.y + dy;
		}
		update_geometry(initial.ptr);
		break;

	case M_FLOATING | M_RESIZING:
		switch (config->resizing_key) {
		case M_LEFT_CLICK:
			dx -= pointer_state.left.x;
			dy -= pointer_state.left.y;
			initial.ptr = pointer_state.left.view;
			break;
		case M_RIGHT_CLICK:
			dx -= pointer_state.right.x;
			dy -= pointer_state.right.y;
			initial.ptr = pointer_state.right.view;
			break;
		}

		if (lock.left) {
			if (initial.w + dx > min_sane_w) {
				initial.ptr->width = initial.w + dx;
			}
		} else { // lock.right
			if (initial.w - dx > min_sane_w) {
				initial.ptr->width = initial.w - dx;
				initial.ptr->x = initial.x + dx;
			}
		}
		if (lock.top) {
			if (initial.h + dy > min_sane_h) {
				initial.ptr->height = initial.h + dy;
			}
		} else { // lock.bottom
			if (initial.h - dy > min_sane_h) {
				initial.ptr->height = initial.h - dy;
				initial.ptr->y = initial.y + dy;
			}
		}
		update_geometry(initial.ptr);
		break;

	case M_TILING | M_DRAGGING:
		// swap current view under pointer with dragged view
		if (pointer_state.view && pointer_state.view->type == C_VIEW
				&& pointer_state.view != initial.ptr
				&& !pointer_state.view->is_floating) {
			// Swap them around
			swap_container(pointer_state.view, initial.ptr);
			swap_geometry(pointer_state.view, initial.ptr);
			update_geometry(pointer_state.view);
			update_geometry(initial.ptr);
			// Set focus back to initial view
			set_focused_container(initial.ptr);
		}
		break;

	case M_TILING | M_RESIZING:
		switch (config->resizing_key) {
		case M_LEFT_CLICK:
			dx -= pointer_state.left.x;
			dy -= pointer_state.left.y;
			break;
		case M_RIGHT_CLICK:
			dx -= pointer_state.right.x;
			dy -= pointer_state.right.y;
			break;
		}

		// resize if we can
		if (initial.horiz.ptr) {
			if (lock.left) {
				// Check whether its fine to resize
				if (initial.w + dx > min_sane_w && initial.horiz.w - dx > min_sane_w) {
					initial.horiz.ptr->width = initial.horiz.w - dx;
					initial.horiz.parent.ptr->width = initial.horiz.parent.w + dx;
				}
			} else { // lock.right
				if (initial.w - dx > min_sane_w && initial.horiz.w + dx > min_sane_w) {
					initial.horiz.ptr->width = initial.horiz.w + dx;
					initial.horiz.parent.ptr->width = initial.horiz.parent.w - dx;
				}
			}
			arrange_windows(initial.horiz.ptr->parent, -1, -1);
		}
		if (initial.vert.ptr) {
			if (lock.top) {
				if (initial.h + dy > min_sane_h && initial.vert.h - dy > min_sane_h) {
					initial.vert.ptr->height = initial.vert.h - dy;
					initial.vert.parent.ptr->height = initial.vert.parent.h + dy;
				}
			} else { // lock.bottom
				if (initial.h - dy > min_sane_h && initial.vert.h + dy > min_sane_h) {
					initial.vert.ptr->height = initial.vert.h + dy;
					initial.vert.parent.ptr->height = initial.vert.parent.h - dy;
				}
			}
			arrange_windows(initial.vert.ptr->parent, -1, -1);
		}
	default:
		return;
	}
}

void pointer_mode_reset(void) {
	switch (pointer_state.mode) {
	case M_FLOATING | M_RESIZING:
	case M_FLOATING | M_DRAGGING:
		reset_initial_view();
		break;

	case M_TILING | M_RESIZING:
		(void) reset_initial_sibling;
		break;

	case M_TILING | M_DRAGGING:
	default:
		break;
	}
}