summary refs log tree commit diff
path: root/keyboard.c
diff options
context:
space:
mode:
authorZach DeCook <zachdecook@librem.one>2022-01-08 16:23:51 -0500
committerJohn Sullivan <jsullivan@csumb.edu>2022-01-10 03:08:06 +0000
commite5c78bb9ab1b78c09edd7b1f0c1cfb1ff585c3a5 (patch)
treecbf5b4e21d6c72f38fe45594f48f12de44e8a1a8 /keyboard.c
parent8120fd052120423cc4e84d7e893bef125fb06a6b (diff)
downloadsuggpicker-e5c78bb9ab1b78c09edd7b1f0c1cfb1ff585c3a5.tar.gz
key drawing: refactor kbd_draw_key
Diffstat (limited to 'keyboard.c')
-rw-r--r--keyboard.c55
1 files changed, 31 insertions, 24 deletions
diff --git a/keyboard.c b/keyboard.c
index ca7844c..3bcdaaa 100644
--- a/keyboard.c
+++ b/keyboard.c
@@ -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++;
 	}
 }