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
|
#include <linux/input-event-codes.h>
#include <stdio.h>
#include <sys/mman.h>
#include "keyboard.h"
#include "drw.h"
#include "os-compatibility.h"
#define MAX_LAYERS 25
/* lazy die macro */
#define die(...) \
fprintf(stderr, __VA_ARGS__); \
exit(1)
void
kbd_switch_layout(struct kbd *kb, struct layout *l) {
kb->prevlayout = kb->layout;
kb->layout = l;
if (kb->debug)
fprintf(stderr, "Switching to layout %s)\n", kb->layout->name);
if ((!kb->prevlayout) ||
(strcmp(kb->prevlayout->keymap_name, kb->layout->keymap_name) != 0)) {
fprintf(stderr, "Switching to keymap %s\n", kb->layout->keymap_name);
}
kbd_draw_layout(kb);
}
uint8_t
kbd_get_rows(struct layout *l) {
uint8_t rows = 0;
struct key *k = l->keys;
while (k->type != Last) {
if (k->type == EndRow) {
rows++;
}
k++;
}
return rows + 1;
}
void
kbd_init(struct kbd *kb, struct layout *layouts) {
char *s;
int i;
bool found;
fprintf(stderr, "Initializing keyboard\n");
kb->layouts = layouts;
for (i = 0; i < NumLayouts - 1; i++)
;
fprintf(stderr, "Found %d layouts\n", i);
kb->layer_index = 0;
i = 0;
enum layout_id lid = kb->layers[0];
while (lid != NumLayouts) {
lid = kb->layers[++i];
}
fprintf(stderr, "Found %d layers\n", i);
enum layout_id layer;
if (kb->landscape) {
layer = kb->landscape_layers[kb->layer_index];
} else {
layer = kb->layers[kb->layer_index];
}
kb->layout = &kb->layouts[layer];
kb->prevlayout = kb->layout;
}
void
kbd_init_layout(struct layout *l, uint32_t width, uint32_t height) {
uint32_t x = 0, y = 0;
uint8_t rows = kbd_get_rows(l);
l->keyheight = height / rows;
struct key *k = l->keys;
double rowlength = kbd_get_row_length(k);
while (k->type != Last) {
if (k->type == EndRow) {
y += l->keyheight;
x = 0;
rowlength = kbd_get_row_length(k + 1);
} else if (k->width > 0) {
k->x = x;
k->y = y;
k->w = ((double)width / rowlength) * k->width;
x += k->w;
}
k->h = l->keyheight;
k++;
}
}
double
kbd_get_row_length(struct key *k) {
double l = 0.0;
while ((k->type != Last) && (k->type != EndRow)) {
l += k->width;
k++;
}
return l;
}
struct key *
kbd_get_key(struct kbd *kb, uint32_t x, uint32_t y) {
struct layout *l = kb->layout;
struct key *k = l->keys;
if (kb->debug)
fprintf(stderr, "get key: +%d+%d\n", x, y);
while (k->type != Last) {
if ((k->type != EndRow) && (k->type != Pad) && (k->type != Pad) &&
(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, uint32_t time) {
bool unlatch_shift = false;
if (kb->last_press) {
if (kb->compose >= 2) {
kb->compose = 0;
kbd_switch_layout(kb, kb->prevlayout);
} else if (unlatch_shift) {
kbd_draw_layout(kb);
} else {
kbd_draw_key(kb, kb->last_press, Unpress);
}
kb->last_press = NULL;
}
}
void
kbd_release_key(struct kbd *kb, uint32_t time) {
kbd_unpress_key(kb, time);
if (kb->print_intersect && kb->last_swipe) {
printf("\n");
// Important so autocompleted words get typed in time
fflush(stdout);
kbd_draw_layout(kb);
kb->last_swipe = NULL;
}
}
void
kbd_motion_key(struct kbd *kb, uint32_t time, uint32_t x, uint32_t y) {
// Output intersecting keys
// (for external 'swiping'-based accelerators).
if (kb->print_intersect) {
if (kb->last_press) {
kbd_unpress_key(kb, time);
// Redraw last press as a swipe.
kbd_draw_key(kb, kb->last_swipe, Swipe);
}
struct key *intersect_key;
intersect_key = kbd_get_key(kb, x, y);
if (intersect_key &&
(!kb->last_swipe || intersect_key->label != kb->last_swipe->label)) {
kbd_print_key_stdout(kb, intersect_key);
kb->last_swipe = intersect_key;
kbd_draw_key(kb, kb->last_swipe, Swipe);
}
} else {
kbd_unpress_key(kb, time);
}
}
void
kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
if ((kb->compose == 1) && (k->type != Compose) && (k->type != Mod) &&
(k->layout)) {
kb->compose++;
if (kb->debug)
fprintf(stderr, "showing compose %d\n", kb->compose);
kbd_switch_layout(kb, k->layout);
return;
}
switch (k->type) {
case Code:
kb->last_swipe = kb->last_press = k;
kbd_draw_key(kb, k, Press);
if (kb->print || kb->print_intersect)
kbd_print_key_stdout(kb, k);
if (kb->compose) {
if (kb->debug)
fprintf(stderr, "pressing composed key\n");
kb->compose++;
}
break;
default:
break;
}
}
void
kbd_print_key_stdout(struct kbd *kb, struct key *k) {
/* printed keys may slightly differ from the actual output
* we generally print what is on the key LABEL and only support the normal
* and shift layers. Other modifiers produce no output (Ctrl,Alt)
* */
bool handled = true;
if (k->type == Code) {
switch (k->code) {
case KEY_SPACE:
printf(" ");
break;
case KEY_ENTER:
printf("\n");
break;
case KEY_BACKSPACE:
printf("\b");
break;
case KEY_TAB:
printf("\t");
break;
default:
handled = false;
break;
}
} else if (k->type != Copy) {
return;
}
if (!handled) {
if ((kb->mods & Shift) || (kb->mods & CapsLock))
printf("%s", k->shift_label);
else if (!(kb->mods & Ctrl) && !(kb->mods & Alt) && !(kb->mods & Super))
printf("%s", k->label);
}
fflush(stdout);
}
void
kbd_draw_key(struct kbd *kb, struct key *k, enum key_draw_type type) {
struct drwsurf *d = kb->surf;
const char *label = (kb->mods & Shift) ? k->shift_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 = (k->scheme == 0) ? &(kb->scheme) : &(kb->scheme1);
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->layout->keys;
if (kb->debug)
fprintf(stderr, "Draw layout");
drw_fill_rectangle(d, kb->scheme.bg, 0, 0, kb->w, kb->h);
while (next_key->type != Last) {
if ((next_key->type == Pad) || (next_key->type == EndRow)) {
next_key++;
continue;
}
if (next_key->type == Mod && kb->mods & next_key->code) {
kbd_draw_key(kb, next_key, Press);
} else {
kbd_draw_key(kb, next_key, Unpress);
}
next_key++;
}
}
void
kbd_resize(struct kbd *kb, struct layout *layouts, uint8_t layoutcount) {
struct drwsurf *d = kb->surf;
fprintf(stderr, "Resize %dx%d %d, %d layouts\n", kb->w, kb->h, kb->s,
layoutcount);
drwsurf_resize(d, kb->w, kb->h, kb->s);
for (int i = 0; i < layoutcount; i++) {
kbd_init_layout(&layouts[i], 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,
height - border);
}
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,
height - border);
}
|