key drawing: refactor kbd_draw_key

This commit is contained in:
Zach DeCook 2022-01-08 16:23:51 -05:00 committed by John Sullivan
parent 564eb4536a
commit bddf05e0fc
2 changed files with 38 additions and 27 deletions

View File

@ -167,7 +167,7 @@ kbd_get_key(struct kbd *kb, uint32_t x, uint32_t y) {
void
kbd_unpress_key(struct kbd *kb, uint32_t time) {
if (kb->last_press) {
kbd_draw_press(kb, kb->last_press, false);
kbd_draw_key(kb, kb->last_press, Unpress);
if (kb->last_press->type == Copy) {
zwp_virtual_keyboard_v1_key(kb->vkbd, time, 127, // COMP key
WL_KEYBOARD_KEY_STATE_RELEASED);
@ -207,7 +207,7 @@ void kbd_motion_key(struct kbd *kb, uint32_t time, uint32_t x, uint32_t y) {
if (kb->last_press) {
kbd_unpress_key(kb, time);
// Redraw last press as a swipe.
kbd_draw_swipe(kb, kb->last_swipe);
kbd_draw_key(kb, kb->last_swipe, Swipe);
}
struct key *intersect_key;
intersect_key = kbd_get_key(kb, x, y);
@ -215,7 +215,7 @@ void kbd_motion_key(struct kbd *kb, uint32_t time, uint32_t x, uint32_t y) {
(! kb->last_swipe || intersect_key->label != kb->last_swipe->label)) {
kbd_print_key_stdout(kb, intersect_key);
kb->last_swipe = intersect_key;
kbd_draw_swipe(kb, kb->last_swipe);
kbd_draw_key(kb, kb->last_swipe, Swipe);
}
} else {
kbd_unpress_key(kb, time);
@ -246,7 +246,7 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods, 0, 0, 0);
}
kb->last_swipe = kb->last_press = k;
kbd_draw_press(kb, k, true);
kbd_draw_key(kb, k, Press);
zwp_virtual_keyboard_v1_key(kb->vkbd, time, kb->last_press->code,
WL_KEYBOARD_KEY_STATE_PRESSED);
if (kb->print || kb->print_intersect)
@ -262,7 +262,11 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
if (k->code == Shift) {
kbd_draw_layout(kb);
}
kbd_draw_press(kb, k, kb->mods & k->code);
if (kb->mods & k->code) {
kbd_draw_key(kb, k, Press);
} else {
kbd_draw_key(kb, k, Unpress);
}
zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods, 0, 0, 0);
break;
case Layout:
@ -276,7 +280,11 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
} else {
kb->compose = 0;
}
kbd_draw_press(kb, k, (bool)kb->compose);
if ((bool)kb->compose) {
kbd_draw_key(kb, k, Press);
} else {
kbd_draw_key(kb, k, Unpress);
}
break;
case NextLayer:
// switch to the next layout in the layer sequence
@ -305,7 +313,7 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
case Copy:
// copy code as unicode chr by setting a temporary keymap
kb->last_swipe = kb->last_press = k;
kbd_draw_press(kb, k, true);
kbd_draw_key(kb, k, Press);
if (kb->debug)
fprintf(stderr, "pressing copy key\n");
create_and_upload_keymap(kb, kb->layout->keymap_name, k->code, k->code_mod);
@ -360,35 +368,31 @@ kbd_print_key_stdout(struct kbd *kb, struct key *k) {
}
void
kbd_draw_key(struct kbd *kb, struct key *k, bool pressed, bool swiped) {
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);
if (swiped) {
Color *fill = &scheme->swipe;
draw_over_inset(d, k->x, k->y, k->w, k->h, KBD_KEY_BORDER, *fill);
} else {
Color *fill = pressed ? &scheme->high : &scheme->fg;
draw_inset(d, k->x, k->y, k->w, k->h, KBD_KEY_BORDER, *fill);
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_press(struct kbd *kb, struct key *k, bool pressed) {
kbd_draw_key(kb, k, pressed, false);
}
void kbd_draw_swipe(struct kbd *kb, struct key *k) {
kbd_draw_key(kb, k, false, true);
}
void
kbd_draw_layout(struct kbd *kb) {
struct drwsurf *d = kb->surf;
struct key *next_key = kb->layout->keys;
bool pressed = false;
if (kb->debug)
fprintf(stderr, "Draw layout");
@ -399,8 +403,11 @@ kbd_draw_layout(struct kbd *kb) {
next_key++;
continue;
}
pressed = next_key->type == Mod && kb->mods & next_key->code;
kbd_draw_press(kb, next_key, pressed);
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++;
}
}

View File

@ -41,6 +41,12 @@ enum key_modifier_type {
AltGr = 128,
};
enum key_draw_type {
Unpress = 0,
Press,
Swipe,
};
struct clr_scheme {
Color fg;
Color bg;
@ -117,9 +123,7 @@ void kbd_release_key(struct kbd *kb, uint32_t time);
void kbd_motion_key(struct kbd *kb, uint32_t time, uint32_t x, uint32_t y);
void kbd_press_key(struct kbd *kb, struct key *k, uint32_t time);
void kbd_print_key_stdout(struct kbd *kb, struct key *k);
void kbd_draw_key(struct kbd *kb, struct key *k, bool pressed, bool swiped);
void kbd_draw_press(struct kbd *kb, struct key *k, bool pressed);
void kbd_draw_swipe(struct kbd *kb, struct key *k);
void kbd_draw_key(struct kbd *kb, struct key *k, enum key_draw_type);
void kbd_draw_layout(struct kbd *kb);
void kbd_resize(struct kbd *kb, struct layout *layouts, uint8_t layoutcount);
uint8_t kbd_get_rows(struct layout *l);