aboutsummaryrefslogtreecommitdiff
path: root/sway/input_state.c
blob: ef5d6df076f0b12b4d54ef6ef8ec2baa0f3e22c5 (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
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

#include "input_state.h"

#define KEY_STATE_MAX_LENGTH 64

static keycode key_state_array[KEY_STATE_MAX_LENGTH];

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

static uint8_t find_key(keycode key) {
	int i;
	for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) {
		if (key_state_array[i] == key) {
			break;
		}
	}
	return i;
}

bool check_key(keycode key) {
	return find_key(key) < KEY_STATE_MAX_LENGTH;
}

void press_key(keycode key) {
	// Check if key exists
	if (!check_key(key)) {
		// Check that we dont exceed buffer length
		int insert = find_key(0);
		if (insert < KEY_STATE_MAX_LENGTH) {
			key_state_array[insert] = key;
		}
	}
}

void release_key(keycode key) {
	uint8_t index = find_key(key);
	if (index < KEY_STATE_MAX_LENGTH) {
		// shift it over and remove key
		key_state_array[index] = 0;
	}
}

struct pointer_state pointer_state = {0, 0, {0, 0}, {0}, {0, 0, 0, 0}};

static struct wlc_geometry saved_floating;

void start_floating(swayc_t *view) {
	if (view->is_floating) {
		saved_floating.origin.x = view->x;
		saved_floating.origin.y = view->y;
		saved_floating.size.w = view->width;
		saved_floating.size.h = view->height;
	}
}

void reset_floating(swayc_t *view) {
	if (view->is_floating) {
		view->x = saved_floating.origin.x;
		view->y = saved_floating.origin.y;
		view->width = saved_floating.size.w;
		view->height = saved_floating.size.h;
		arrange_windows(view->parent, -1, -1);
	}
	pointer_state.floating = (struct pointer_floating){0,0};
	pointer_state.lock = (struct pointer_lock){0,0,0,0};
}