2021-08-24 14:26:34 +02:00
|
|
|
#define MAX_LAYERS 25
|
|
|
|
|
2020-09-11 01:25:28 -07:00
|
|
|
enum key_type;
|
|
|
|
enum key_modifier_type;
|
|
|
|
struct clr_scheme;
|
|
|
|
struct key;
|
|
|
|
struct layout;
|
|
|
|
struct kbd;
|
|
|
|
|
|
|
|
enum key_type {
|
2021-08-24 13:08:49 +02:00
|
|
|
Pad = 0, //Padding, not a pressable key
|
|
|
|
Code, //A normal key emitting a keycode
|
|
|
|
Mod, //A modifier key
|
|
|
|
Copy, //Copy key, copies the unicode value specified in code (creates and activates temporary keymap)
|
|
|
|
// used for keys that are not part of the keymap
|
|
|
|
Layout, //Layout switch to a specific layout
|
|
|
|
BackLayer, //Layout switch to the layout that was previously active
|
|
|
|
NextLayer, //Layout switch to the next layout in the layers sequence
|
|
|
|
Compose, //Compose modifier key, switches to a specific associated layout upon next keypress
|
|
|
|
EndRow, //Incidates the end of a key row
|
|
|
|
Last, //Indicated the end of a layout
|
2020-09-11 01:25:28 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/* 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,
|
2021-08-22 23:18:47 +02:00
|
|
|
Alt = 8,
|
2020-09-11 01:25:28 -07:00
|
|
|
Super = 64,
|
|
|
|
AltGr = 128,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct clr_scheme {
|
|
|
|
Color fg;
|
|
|
|
Color bg;
|
|
|
|
Color high;
|
|
|
|
Color text;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct key {
|
2021-08-24 09:53:34 +02:00
|
|
|
const char *label; // primary label
|
|
|
|
const char *shift_label; // secondary label
|
|
|
|
const double width; // relative width (1.0)
|
2020-09-11 01:25:28 -07:00
|
|
|
const enum key_type type;
|
|
|
|
|
2021-08-24 09:53:34 +02:00
|
|
|
const uint32_t
|
|
|
|
code; /* code: key scancode or modifier name (see
|
|
|
|
* `/usr/include/linux/input-event-codes.h` for scancode names, and
|
|
|
|
* `keyboard.h` for modifiers)
|
|
|
|
* XKB keycodes are +8 */
|
|
|
|
struct layout *layout; // pointer back to the parent layout that holds this
|
|
|
|
// key
|
2021-08-22 23:18:47 +02:00
|
|
|
const uint32_t code_mod; /* modifier to force when this key is pressed */
|
2021-08-24 09:53:34 +02:00
|
|
|
uint8_t scheme; // index of the scheme to use
|
|
|
|
bool reset_mod; /* reset modifiers when clicked */
|
2020-09-11 01:25:28 -07:00
|
|
|
|
2021-08-24 09:53:34 +02:00
|
|
|
// actual coordinates on the surface (pixels), will be computed automatically
|
|
|
|
// for all keys
|
2021-08-22 12:38:10 +02:00
|
|
|
uint32_t x, y, w, h;
|
2020-09-11 01:25:28 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
struct layout {
|
|
|
|
struct key *keys;
|
2021-08-24 09:53:34 +02:00
|
|
|
const char *keymap_name;
|
2021-08-24 13:08:49 +02:00
|
|
|
const char *name;
|
2021-08-24 09:53:34 +02:00
|
|
|
uint32_t keyheight; // absolute height (pixels)
|
2020-09-11 01:25:28 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
struct kbd {
|
|
|
|
struct layout *layout;
|
|
|
|
struct clr_scheme scheme;
|
2021-08-23 22:35:14 +02:00
|
|
|
struct clr_scheme scheme1;
|
2020-09-11 01:25:28 -07:00
|
|
|
|
2021-08-24 13:08:49 +02:00
|
|
|
bool print;
|
2020-09-11 01:25:28 -07:00
|
|
|
uint32_t w, h;
|
|
|
|
uint8_t mods;
|
|
|
|
struct key *last_press;
|
2021-08-24 13:08:49 +02:00
|
|
|
struct layout *prevlayout;
|
|
|
|
size_t layer_index;
|
|
|
|
|
2021-08-24 14:26:34 +02:00
|
|
|
struct layout *layouts;
|
|
|
|
enum layout_id *layers;
|
2020-09-11 01:25:28 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-08-24 14:26:34 +02:00
|
|
|
static void kbd_init(struct kbd *kb, struct layout * layouts, char * layer_names_list);
|
2021-08-22 12:38:10 +02:00
|
|
|
static void kbd_init_layout(struct layout *l, uint32_t width, uint32_t height);
|
2020-09-11 01:25:28 -07:00
|
|
|
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);
|
2021-08-24 14:09:35 +02:00
|
|
|
static void kbd_print_key_stdout(struct kbd *kb, struct key *k);
|
2020-09-11 01:25:28 -07:00
|
|
|
static void kbd_draw_key(struct kbd *kb, struct key *k, bool pressed);
|
|
|
|
static void kbd_draw_layout(struct kbd *kb);
|
2021-08-24 09:53:34 +02:00
|
|
|
static void kbd_resize(struct kbd *kb, uint32_t w, uint32_t h,
|
|
|
|
struct layout *layouts, uint8_t layoutcount);
|
2021-08-22 12:38:10 +02:00
|
|
|
static uint8_t kbd_get_rows(struct layout *l);
|
|
|
|
static double kbd_get_row_length(struct key *k);
|
2021-08-24 13:08:49 +02:00
|
|
|
static void kbd_switch_layout(struct kbd *kb, struct layout *l);
|
|
|
|
|
|
|
|
void
|
|
|
|
kbd_switch_layout(struct kbd *kb, struct layout *l) {
|
|
|
|
const struct layout * prevlayout = kb->prevlayout;
|
|
|
|
kb->prevlayout = kb->layout;
|
|
|
|
kb->layout = l;
|
|
|
|
if ((!prevlayout) ||
|
|
|
|
(strcmp(prevlayout->keymap_name, kb->layout->keymap_name) != 0)) {
|
|
|
|
fprintf(stderr, "Switching to keymap %s\n", kb->layout->keymap_name);
|
|
|
|
create_and_upload_keymap(kb->layout->keymap_name, 0, 0);
|
|
|
|
}
|
|
|
|
kbd_draw_layout(kb);
|
|
|
|
}
|
2020-09-11 01:25:28 -07:00
|
|
|
|
2021-08-24 09:53:34 +02:00
|
|
|
uint8_t
|
|
|
|
kbd_get_rows(struct layout *l) {
|
2021-08-22 12:38:10 +02:00
|
|
|
uint8_t rows = 0;
|
2020-09-11 01:25:28 -07:00
|
|
|
struct key *k = l->keys;
|
2021-08-22 12:38:10 +02:00
|
|
|
while (k->type != Last) {
|
|
|
|
if (k->type == EndRow) {
|
|
|
|
rows++;
|
2020-09-11 01:25:28 -07:00
|
|
|
}
|
2021-08-22 12:38:10 +02:00
|
|
|
k++;
|
|
|
|
}
|
|
|
|
return rows + 1;
|
|
|
|
}
|
2020-09-11 01:25:28 -07:00
|
|
|
|
2021-08-24 13:08:49 +02:00
|
|
|
|
2021-08-24 14:26:34 +02:00
|
|
|
void kbd_init(struct kbd *kb, struct layout * layouts, char * layer_names_list) {
|
|
|
|
char *s;
|
|
|
|
int i;
|
|
|
|
bool found;
|
|
|
|
|
|
|
|
fprintf(stderr, "Initializing keyboard\n");
|
|
|
|
|
|
|
|
kb->layouts = layouts;
|
|
|
|
|
|
|
|
struct layout * l = layouts;
|
|
|
|
for (i = 0; i < NumLayouts - 1; i++);
|
|
|
|
fprintf(stderr, "Found %d layers\n",i);
|
|
|
|
|
|
|
|
kb->layer_index = 0;
|
|
|
|
|
|
|
|
if (layer_names_list) {
|
|
|
|
uint8_t numlayers = 0;
|
|
|
|
kb->layers = malloc(MAX_LAYERS * sizeof(enum layout_id));
|
|
|
|
s = strtok(layer_names_list, ",");
|
|
|
|
while (s != NULL) {
|
|
|
|
if (numlayers + 1 == MAX_LAYERS) {
|
|
|
|
fprintf(stderr, "too many layers specified");
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
found = false;
|
|
|
|
for (i = 0; i < NumLayouts - 1; i++) {
|
|
|
|
if (kb->layouts[i].name && strcmp(kb->layouts[i].name, s) == 0) {
|
|
|
|
fprintf(stderr, "layer #%d = %s\n", numlayers + 1, s);
|
|
|
|
kb->layers[numlayers++] = i;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
fprintf(stderr, "No such layer: %s\n", s);
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
s = strtok(NULL,",");
|
|
|
|
}
|
|
|
|
kb->layers[numlayers] = NumLayouts; //mark the end of the sequence
|
|
|
|
if (numlayers == 0) {
|
|
|
|
fprintf(stderr, "No layers defined\n");
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
enum layout_id lid = kb->layers[0];
|
|
|
|
while (lid != NumLayouts) {
|
|
|
|
lid = kb->layers[++i];
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Found %d layers\n",i);
|
|
|
|
|
|
|
|
kb->layout = &kb->layouts[kb->layers[kb->layer_index]];
|
2021-08-24 13:08:49 +02:00
|
|
|
|
|
|
|
/* upload keymap */
|
|
|
|
create_and_upload_keymap(kb->layout->keymap_name, 0, 0);
|
|
|
|
}
|
|
|
|
|
2021-08-22 12:38:10 +02:00
|
|
|
void
|
|
|
|
kbd_init_layout(struct layout *l, uint32_t width, uint32_t height) {
|
|
|
|
uint32_t x = 0, y = 0;
|
2021-08-24 14:26:34 +02:00
|
|
|
fprintf(stderr, "Init layout %s\n", l->name);
|
2021-08-22 12:38:10 +02:00
|
|
|
uint8_t rows = kbd_get_rows(l);
|
2020-09-11 01:25:28 -07:00
|
|
|
|
2021-08-22 12:38:10 +02:00
|
|
|
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;
|
2021-08-24 09:53:34 +02:00
|
|
|
rowlength = kbd_get_row_length(k + 1);
|
2021-08-22 12:38:10 +02:00
|
|
|
} else if (k->width > 0) {
|
|
|
|
k->x = x;
|
|
|
|
k->y = y;
|
2021-08-24 09:53:34 +02:00
|
|
|
k->w = ((double)width / rowlength) * k->width;
|
2021-08-22 12:38:10 +02:00
|
|
|
x += k->w;
|
2020-09-11 01:25:28 -07:00
|
|
|
}
|
2021-08-22 12:38:10 +02:00
|
|
|
k->h = l->keyheight;
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
}
|
2020-09-11 01:25:28 -07:00
|
|
|
|
2021-08-22 12:38:10 +02:00
|
|
|
double
|
|
|
|
kbd_get_row_length(struct key *k) {
|
|
|
|
double l = 0.0;
|
|
|
|
while ((k->type != Last) && (k->type != EndRow)) {
|
|
|
|
l += k->width;
|
2020-09-11 01:25:28 -07:00
|
|
|
k++;
|
|
|
|
}
|
2021-08-22 12:38:10 +02:00
|
|
|
return l;
|
2020-09-11 01:25:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
struct key *
|
|
|
|
kbd_get_key(struct kbd *kb, uint32_t x, uint32_t y) {
|
|
|
|
struct layout *l = kb->layout;
|
2021-08-22 12:38:10 +02:00
|
|
|
struct key *k = l->keys;
|
2021-08-24 09:53:34 +02:00
|
|
|
fprintf(stderr, "get key: +%d+%d\n", x, y);
|
2021-08-22 12:38:10 +02:00
|
|
|
while (k->type != Last) {
|
2021-08-24 09:53:34 +02:00
|
|
|
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)) {
|
2021-08-22 12:38:10 +02:00
|
|
|
return k;
|
|
|
|
}
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
return NULL;
|
2020-09-11 01:25:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-08-22 23:18:47 +02:00
|
|
|
if (kb->last_press->type == Copy) {
|
2021-08-24 09:53:34 +02:00
|
|
|
zwp_virtual_keyboard_v1_key(kb->vkbd, time, 127, // COMP key
|
|
|
|
WL_KEYBOARD_KEY_STATE_RELEASED);
|
2021-08-22 23:18:47 +02:00
|
|
|
} else {
|
|
|
|
zwp_virtual_keyboard_v1_key(kb->vkbd, time, kb->last_press->code,
|
2021-08-24 09:53:34 +02:00
|
|
|
WL_KEYBOARD_KEY_STATE_RELEASED);
|
2021-08-22 23:18:47 +02:00
|
|
|
}
|
2020-09-11 01:25:28 -07:00
|
|
|
kb->last_press = NULL;
|
2021-08-23 19:34:03 +02:00
|
|
|
|
|
|
|
if (compose >= 2) {
|
|
|
|
compose = 0;
|
2021-08-24 09:53:34 +02:00
|
|
|
if ((!kb->prevlayout) ||
|
|
|
|
(strcmp(kb->prevlayout->keymap_name, kb->layout->keymap_name) != 0)) {
|
2021-08-23 20:04:35 +02:00
|
|
|
create_and_upload_keymap(kb->layout->keymap_name, 0, 0);
|
|
|
|
}
|
2021-08-23 19:34:03 +02:00
|
|
|
kb->layout = kb->prevlayout;
|
2021-08-24 09:53:34 +02:00
|
|
|
if ((kb->mods & Shift) == Shift)
|
|
|
|
kb->mods ^= Shift;
|
2021-08-24 09:48:37 +02:00
|
|
|
kbd_draw_layout(kb);
|
2021-08-24 09:37:14 +02:00
|
|
|
} else if ((kb->mods & Shift) == Shift) {
|
2021-08-24 09:48:37 +02:00
|
|
|
kb->mods ^= Shift;
|
2021-08-23 19:34:03 +02:00
|
|
|
kbd_draw_layout(kb);
|
2021-08-24 09:48:37 +02:00
|
|
|
}
|
2020-09-11 01:25:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
|
2021-08-24 09:53:34 +02:00
|
|
|
if ((compose == 1) && (k->type != Compose) && (k->type != Mod) &&
|
|
|
|
(k->layout)) {
|
2021-08-23 23:31:51 +02:00
|
|
|
compose++;
|
2021-08-24 09:53:34 +02:00
|
|
|
fprintf(stderr, "showing compose %d\n", compose);
|
|
|
|
if ((!kb->prevlayout) ||
|
|
|
|
(strcmp(kb->prevlayout->keymap_name, kb->layout->keymap_name) != 0)) {
|
2021-08-23 23:31:51 +02:00
|
|
|
create_and_upload_keymap(kb->layout->keymap_name, 0, 0);
|
|
|
|
}
|
|
|
|
kb->prevlayout = kb->layout;
|
|
|
|
kb->layout = k->layout;
|
|
|
|
kbd_draw_layout(kb);
|
|
|
|
kb->surf->dirty = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-11 01:25:28 -07:00
|
|
|
switch (k->type) {
|
|
|
|
case Code:
|
2021-08-23 15:55:15 +02:00
|
|
|
if (k->code_mod) {
|
2021-08-23 18:58:29 +02:00
|
|
|
if (k->reset_mod) {
|
|
|
|
zwp_virtual_keyboard_v1_modifiers(kb->vkbd, k->code_mod, 0, 0, 0);
|
|
|
|
} else {
|
2021-08-24 09:53:34 +02:00
|
|
|
zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods ^ k->code_mod, 0,
|
|
|
|
0, 0);
|
2021-08-23 18:58:29 +02:00
|
|
|
}
|
2021-08-23 16:36:11 +02:00
|
|
|
} else {
|
2021-08-23 15:55:15 +02:00
|
|
|
zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods, 0, 0, 0);
|
|
|
|
}
|
2021-08-23 23:31:51 +02:00
|
|
|
kb->last_press = k;
|
|
|
|
kbd_draw_key(kb, k, true);
|
|
|
|
zwp_virtual_keyboard_v1_key(kb->vkbd, time, kb->last_press->code,
|
2021-08-24 09:53:34 +02:00
|
|
|
WL_KEYBOARD_KEY_STATE_PRESSED);
|
2021-08-24 14:09:35 +02:00
|
|
|
if (kb->print)
|
|
|
|
kbd_print_key_stdout(kb, k);
|
2021-08-23 23:31:51 +02:00
|
|
|
if (compose) {
|
2021-08-24 09:53:34 +02:00
|
|
|
fprintf(stderr, "pressing composed key\n");
|
2021-08-23 23:31:51 +02:00
|
|
|
compose++;
|
2021-08-22 23:18:47 +02:00
|
|
|
}
|
2020-09-11 01:25:28 -07:00
|
|
|
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;
|
2021-08-24 13:08:49 +02:00
|
|
|
case Layout:
|
|
|
|
//switch to the layout determined by the key
|
|
|
|
kbd_switch_layout(kb, k->layout);
|
|
|
|
break;
|
2021-08-22 23:18:47 +02:00
|
|
|
case Compose:
|
2021-08-24 13:08:49 +02:00
|
|
|
//switch to the associated layout determined by the *next* keypress
|
2021-08-22 23:18:47 +02:00
|
|
|
if (compose == 0) {
|
|
|
|
compose = 1;
|
|
|
|
} else {
|
|
|
|
compose = 0;
|
|
|
|
}
|
2021-08-24 09:53:34 +02:00
|
|
|
kbd_draw_key(kb, k, (bool)compose);
|
2021-08-22 23:18:47 +02:00
|
|
|
break;
|
2021-08-24 13:08:49 +02:00
|
|
|
case NextLayer:
|
|
|
|
//switch to the next layout in the layer sequence
|
|
|
|
kb->layer_index++;
|
|
|
|
if (kb->layers[kb->layer_index] == NumLayouts) {
|
|
|
|
kb->layer_index = 0;
|
2021-08-23 20:04:35 +02:00
|
|
|
}
|
2021-08-24 14:26:34 +02:00
|
|
|
kbd_switch_layout(kb, &kb->layouts[kb->layers[kb->layer_index]]);
|
2021-08-24 13:08:49 +02:00
|
|
|
break;
|
|
|
|
case BackLayer:
|
|
|
|
//switch to the previously active layout
|
|
|
|
if (kb->prevlayout)
|
|
|
|
kbd_switch_layout(kb, kb->prevlayout);
|
|
|
|
break;
|
2021-08-22 23:18:47 +02:00
|
|
|
case Copy:
|
2021-08-24 13:08:49 +02:00
|
|
|
//copy code as unicode chr by setting a temporary keymap
|
2021-08-22 23:18:47 +02:00
|
|
|
kb->last_press = k;
|
|
|
|
kbd_draw_key(kb, k, true);
|
2021-08-24 09:53:34 +02:00
|
|
|
fprintf(stderr, "pressing copy key\n");
|
2021-08-23 20:04:35 +02:00
|
|
|
create_and_upload_keymap(kb->layout->keymap_name, k->code, k->code_mod);
|
2021-08-22 23:18:47 +02:00
|
|
|
zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods, 0, 0, 0);
|
2021-08-24 09:53:34 +02:00
|
|
|
zwp_virtual_keyboard_v1_key(kb->vkbd, time, 127, // COMP key
|
|
|
|
WL_KEYBOARD_KEY_STATE_PRESSED);
|
2021-08-24 14:09:35 +02:00
|
|
|
if (kb->print)
|
|
|
|
kbd_print_key_stdout(kb, k);
|
2021-08-22 23:18:47 +02:00
|
|
|
break;
|
2020-09-11 01:25:28 -07:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
kb->surf->dirty = true;
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:09:35 +02:00
|
|
|
|
|
|
|
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);
|
2021-08-24 14:26:34 +02:00
|
|
|
else if (!(kb->mods & Ctrl) && !(kb->mods & Alt) && !(kb->mods & Super))
|
2021-08-24 14:09:35 +02:00
|
|
|
printf("%s", k->label);
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2020-09-11 01:25:28 -07:00
|
|
|
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;
|
2021-08-24 09:53:34 +02:00
|
|
|
fprintf(stderr, "Draw key +%d+%d %dx%d -> %s\n", k->x, k->y, k->w, k->h,
|
2021-08-24 14:26:34 +02:00
|
|
|
label);
|
2021-08-24 09:53:34 +02:00
|
|
|
struct clr_scheme *scheme = (k->scheme == 0) ? &(kb->scheme) : &(kb->scheme1);
|
2021-08-23 22:35:14 +02:00
|
|
|
Color *fill = pressed ? &scheme->high : &scheme->fg;
|
2021-08-22 12:38:10 +02:00
|
|
|
draw_inset(d, k->x, k->y, k->w, k->h, KBD_KEY_BORDER, fill->color);
|
2021-08-24 09:53:34 +02:00
|
|
|
uint32_t xoffset = k->w / (strlen(label) + 2);
|
2021-08-24 09:48:37 +02:00
|
|
|
fprintf(stderr, " xoffset=%d\n", xoffset);
|
2021-08-24 09:53:34 +02:00
|
|
|
wld_draw_text(d->render, d->ctx->font, scheme->text.color, k->x + xoffset,
|
|
|
|
k->y + (k->h / 2), label, -1, NULL);
|
2020-09-11 01:25:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kbd_draw_layout(struct kbd *kb) {
|
|
|
|
struct drwsurf *d = kb->surf;
|
|
|
|
struct key *next_key = kb->layout->keys;
|
|
|
|
bool pressed = false;
|
2021-08-24 09:48:37 +02:00
|
|
|
fprintf(stderr, "Draw layout");
|
2020-09-11 01:25:28 -07:00
|
|
|
|
|
|
|
wld_fill_rectangle(d->render, kb->scheme.bg.color, 0, 0, kb->w, kb->h);
|
|
|
|
|
|
|
|
while (next_key->type != Last) {
|
2021-08-22 12:38:10 +02:00
|
|
|
if ((next_key->type == Pad) || (next_key->type == EndRow)) {
|
2020-09-11 01:25:28 -07:00
|
|
|
next_key++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pressed = next_key->type == Mod && kb->mods & next_key->code;
|
|
|
|
kbd_draw_key(kb, next_key, pressed);
|
|
|
|
next_key++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-08-24 09:53:34 +02:00
|
|
|
kbd_resize(struct kbd *kb, uint32_t w, uint32_t h, struct layout *layouts,
|
|
|
|
uint8_t layoutcount) {
|
|
|
|
struct drwsurf *d = kb->surf;
|
2020-09-11 01:25:28 -07:00
|
|
|
|
|
|
|
kb->w = w;
|
|
|
|
kb->h = h;
|
|
|
|
|
2021-08-24 09:53:34 +02:00
|
|
|
fprintf(stderr, "Resize %dx%d, %d layouts\n", w, h, layoutcount);
|
2020-09-11 01:25:28 -07:00
|
|
|
|
|
|
|
drwsurf_resize(d, w, h);
|
2021-08-22 12:38:10 +02:00
|
|
|
for (int i = 0; i < layoutcount; i++) {
|
|
|
|
kbd_init_layout(&layouts[i], w, h);
|
|
|
|
}
|
2020-09-11 01:25:28 -07:00
|
|
|
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);
|
|
|
|
}
|