suggestion picker: a persistent layer to complement virtual keyboards like wvkbd
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
enum key_type;
enum key_modifier_type;
struct clr_scheme;
struct key;
struct layout;
struct kbd;

enum key_type {
	Pad = 0,
	Code,
	Mod,
	Layout,
	EndRow,
	Last,
};

/* Modifiers passed to the virtual_keyboard protocol. They are based on
 * wayland's wl_keyboard, which doesn't document them.
 */
enum key_modifier_type {
	NoMod = 0,
	Shift = 1,
	CapsLock = 2,
	Ctrl = 4,
	Super = 64,
	AltGr = 128,
};

struct clr_scheme {
	Color fg;
	Color bg;
	Color high;
	Color text;
};

struct key {
	const char *label;
	const char *shift_label;
	const double width;
	const enum key_type type;

	const uint32_t code;
	struct layout *layout;

	//actual coordinates on the surface
	uint32_t x, y, w, h;
};

struct layout {
	struct key *keys;
	uint32_t keyheight;
};

struct kbd {
	struct layout *layout;
	struct clr_scheme scheme;

	uint32_t w, h;
	uint8_t mods;
	struct key *last_press;

	struct drwsurf *surf;
	struct zwp_virtual_keyboard_v1 *vkbd;
};

static inline void draw_inset(struct drwsurf *d, uint32_t x, uint32_t y,
                              uint32_t width, uint32_t height, uint32_t border,
                              uint32_t color);

static void kbd_init_layout(struct layout *l, uint32_t width, uint32_t height);
static struct key *kbd_get_key(struct kbd *kb, uint32_t x, uint32_t y);
static void kbd_unpress_key(struct kbd *kb, uint32_t time);
static void kbd_press_key(struct kbd *kb, struct key *k, uint32_t time);
static void kbd_draw_key(struct kbd *kb, struct key *k, bool pressed);
static void kbd_draw_layout(struct kbd *kb);
static void kbd_resize(struct kbd *kb, uint32_t w, uint32_t h, struct layout * layouts, uint8_t layoutcount);
static uint8_t kbd_get_rows(struct layout *l);
static double kbd_get_row_length(struct key *k);

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_layout(struct layout *l, uint32_t width, uint32_t height) {
	uint32_t x = 0, y = 0;
    fprintf(stderr, "Init layout\n");
	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;
			fprintf(stderr, "(%d/%f)*%f -> %s\n",width,rowlength,k->width, k->label);
			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;
	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) {
	if (kb->last_press) {
		kbd_draw_key(kb, kb->last_press, false);
		kb->surf->dirty = true;

		zwp_virtual_keyboard_v1_key(kb->vkbd, time, kb->last_press->code,
		                            WL_KEYBOARD_KEY_STATE_RELEASED);
		kb->last_press = NULL;
	}
}

void
kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
	switch (k->type) {
	case Code:
		kb->last_press = k;
		kbd_draw_key(kb, k, true);
		zwp_virtual_keyboard_v1_key(kb->vkbd, time, kb->last_press->code,
		                            WL_KEYBOARD_KEY_STATE_PRESSED);
		break;
	case Mod:
		kb->mods ^= k->code;
		if (k->code == Shift) {
			kbd_draw_layout(kb);
		}
		kbd_draw_key(kb, k, kb->mods & k->code);
		zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods, 0, 0, 0);
		break;
	case Layout:
		kb->layout = k->layout;
		kbd_draw_layout(kb);
	default:
		break;
	}

	kb->surf->dirty = true;
}

void
kbd_draw_key(struct kbd *kb, struct key *k, bool pressed) {
	struct drwsurf *d = kb->surf;
	const char *label = (kb->mods & Shift) ? k->shift_label : k->label;
    fprintf(stderr, "Draw key +%d+%d %dx%d -> %s\n", k->x, k->y, k->w, k->h, k->label);
	Color *fill = pressed ? &kb->scheme.high : &kb->scheme.fg;
	draw_inset(d, k->x, k->y, k->w, k->h, KBD_KEY_BORDER, fill->color);
	uint32_t xoffset = k->w /  (strlen(label) + 2);
    fprintf(stderr, "  xoffset=%d\n", xoffset);
	wld_draw_text(d->render, d->ctx->font, kb->scheme.text.color,
	              k->x + xoffset, k->y + (k->h / 2), label, -1, NULL);
}

void
kbd_draw_layout(struct kbd *kb) {
	struct drwsurf *d = kb->surf;
	struct key *next_key = kb->layout->keys;
	bool pressed = false;
    fprintf(stderr, "Draw layout");

	wld_fill_rectangle(d->render, kb->scheme.bg.color, 0, 0, kb->w, kb->h);

	while (next_key->type != Last) {
		if ((next_key->type == Pad) || (next_key->type == EndRow)) {
			next_key++;
			continue;
		}
		pressed = next_key->type == Mod && kb->mods & next_key->code;
		kbd_draw_key(kb, next_key, pressed);
		next_key++;
	}
}

void
kbd_resize(struct kbd *kb, uint32_t w, uint32_t h, struct layout * layouts, uint8_t layoutcount) {
	struct drwsurf *d = kb->surf;

	kb->w = w;
	kb->h = h;

	fprintf(stderr, "Resize %dx%d, %d layouts\n",w,h,layoutcount);

	drwsurf_resize(d, w, h);
	for (int i = 0; i < layoutcount; i++) {
		fprintf(stderr,"i=%d\n",i );
		kbd_init_layout(&layouts[i], w, h);
	}
	kbd_draw_layout(kb);
	d->dirty = true;
}

void
draw_inset(struct drwsurf *d, uint32_t x, uint32_t y, uint32_t width,
           uint32_t height, uint32_t border, uint32_t color) {
	wld_fill_rectangle(d->render, color, x + border, y + border, width - border,
	                   height - border);
}