~earboxer/suggpicker

suggpicker/keyboard.c -rw-r--r-- 3.8 KiB
fd0edc9aZach DeCook release: minor version bump 4 months ago
                                                                                
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
#include <stdio.h>
#include <sys/mman.h>
#include "keyboard.h"
#include "drw.h"

#define KBD_KEY_BORDER 1

/* lazy die macro */
#define die(...)                                                               \
	fprintf(stderr, __VA_ARGS__);                                                \
	exit(1)

void
kbd_init(struct kbd *kb) {
	fprintf(stderr, "Initializing keyboard\n");

	kb->suggs[0].label = strdup("Hey");
	kb->suggs[1].label = strdup("Honey");
	kb->suggs[2].label = strdup("Bunches");
	kb->suggs[3].label = NULL;
}

void
kbd_init_suggs(struct key *suggs, uint32_t width, uint32_t height) {
	uint32_t x = 0, y = 0;
	uint8_t rows = 1;

	uint32_t keyheight = height / rows;

	struct key *k = suggs;
	double rowlength = kbd_get_row_length(k);
	uint8_t i = 0;
	while (k->label != NULL) {
		k->x = x;
		k->y = y;
		k->w = width / rowlength;
		x += k->w;
		i++;
		if (x < (width * i) / rowlength) {
			k->w++; x++;
		}
		k->h = keyheight;
		k++;
	}
}

double
kbd_get_row_length(struct key *k) {
	double l = 0.0;
	while (k->label != NULL) {
		l += 1.0;
		k++;
	}
	return l;
}

struct key *
kbd_get_key(struct kbd *kb, uint32_t x, uint32_t y) {
	struct key *k = kb->suggs;
	if (kb->debug)
		fprintf(stderr, "get key: +%d+%d\n", x, y);
	while (k->label != NULL) {
		if ((x >= k->x) && (y >= k->y) && (x < k->x + k->w) && (y < k->y + k->h)) {
			return k;
		}
		k++;
	}
	return NULL;
}

void
kbd_unpress_key(struct kbd *kb) {
	if (kb->last_press) {
		kbd_draw_key(kb, kb->last_press, Unpress);
		kb->last_press = NULL;
		drwsurf_flip(kb->surf);
	}
}

void
kbd_release_key(struct kbd *kb) {
	if (kb->last_press) {
		printf("%s\n", kb->last_press->label);
		// Important so autocompleted words get typed in time
		fflush(stdout);
		kbd_unpress_key(kb);
	}
}

void
kbd_motion_key(struct kbd *kb, uint32_t x, uint32_t y) {
	struct key *next_key;
	next_key = kbd_get_key(kb, x, y);
	if (next_key != kb->last_press) {
		kbd_unpress_key(kb);
		if (next_key) {
			kbd_press_key(kb, next_key);
		}
	}
}

void
kbd_press_key(struct kbd *kb, struct key *k) {
	if (k->label) {
		kb->last_press = k;
		kbd_draw_key(kb, k, Press);
		drwsurf_flip(kb->surf);
	}
}

void
kbd_draw_key(struct kbd *kb, struct key *k, enum key_draw_type type) {
	struct drwsurf *d = kb->surf;
	const char *label = k->label;
	if (kb->debug)
		fprintf(stderr, "Draw key +%d+%d %dx%d -> %s\n", k->x, k->y, k->w, k->h,
		        label);
	struct clr_scheme *scheme = &(kb->scheme);
	switch (type) {
	case Unpress:
		draw_inset(d, k->x, k->y, k->w, k->h, KBD_KEY_BORDER, scheme->fg);
		break;
	case Press:
		draw_inset(d, k->x, k->y, k->w, k->h, KBD_KEY_BORDER, scheme->high);
		break;
	case Swipe:
		draw_over_inset(d, k->x, k->y, k->w, k->h, KBD_KEY_BORDER, scheme->swipe);
		break;
	}
	drw_draw_text(d, scheme->text, k->x, k->y, k->w, k->h, label);
}

void
kbd_draw_layout(struct kbd *kb) {
	struct drwsurf *d = kb->surf;
	struct key *next_key = kb->suggs;
	if (kb->debug)
		fprintf(stderr, "Draw layout");

	drw_fill_rectangle(d, kb->scheme.bg, 0, 0, kb->w, kb->h);

	while (next_key->label != NULL) {
		kbd_draw_key(kb, next_key, Unpress);
		next_key++;
	}
}

void
kbd_resize(struct kbd *kb) {
	struct drwsurf *d = kb->surf;

	fprintf(stderr, "Resize %dx%d %d\n", kb->w, kb->h, kb->s);

	drwsurf_resize(d, kb->w, kb->h, kb->s);
	kbd_init_suggs(kb->suggs, kb->w, kb->h);
	kbd_draw_layout(kb);
}

void
draw_inset(struct drwsurf *ds, uint32_t x, uint32_t y, uint32_t width,
           uint32_t height, uint32_t border, Color color) {
	drw_fill_rectangle(ds, color, x + border, y + border, width - (border*2),
	                   height - (border*2));
}
void
draw_over_inset(struct drwsurf *ds, uint32_t x, uint32_t y, uint32_t width,
                uint32_t height, uint32_t border, Color color) {
	drw_over_rectangle(ds, color, x + border, y + border, width - (border*2),
	                   height - (border*2));
}