Refactor keyboard to consider modified keysyms

Press Shift
Press 0       # Reads as ')'
Release Shift
Release 0     # Reads as '0' but we now recognize it as the same
This commit is contained in:
Drew DeVault 2015-08-23 15:28:49 -04:00
parent 663f53b22a
commit 1ac0c8cd47
3 changed files with 46 additions and 32 deletions

View File

@ -6,16 +6,14 @@
/* Keyboard state */
typedef uint32_t keycode;
// returns true if key has been pressed, otherwise false
bool check_key(keycode key);
bool check_key(uint32_t key_sym, uint32_t key_code);
// sets a key as pressed
void press_key(keycode key);
void press_key(uint32_t key_sym, uint32_t key_code);
// unsets a key as pressed
void release_key(keycode key);
void release_key(uint32_t key_sym, uint32_t key_code);
/* Pointer state */

View File

@ -295,22 +295,12 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier
struct sway_mode *mode = config->current_mode;
if (sym < 70000 /* bullshit made up number */) {
if (!isalnum(sym) && sym != ' ' && sym != XKB_KEY_Escape && sym != XKB_KEY_Tab) {
// God fucking dammit
return false;
}
}
// Lowercase if necessary
sym = tolower(sym);
int i;
if (state == WLC_KEY_STATE_PRESSED) {
press_key(sym);
press_key(sym, key);
} else { // WLC_KEY_STATE_RELEASED
release_key(sym);
release_key(sym, key);
}
// TODO: reminder to check conflicts with mod+q+a versus mod+q
@ -322,7 +312,7 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier
int j;
for (j = 0; j < binding->keys->length; ++j) {
xkb_keysym_t *key = binding->keys->items[j];
if ((match = check_key(*key)) == false) {
if ((match = check_key(*key, 0)) == false) {
break;
}
}

View File

@ -1,50 +1,76 @@
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include "log.h"
#include "input_state.h"
#define KEY_STATE_MAX_LENGTH 64
static keycode key_state_array[KEY_STATE_MAX_LENGTH];
struct key_state {
/*
* Aims to store state regardless of modifiers.
* If you press a key, then hold shift, then release the key, we'll
* get two different key syms, but the same key code. This handles
* that scenario and makes sure we can use the right bindings.
*/
uint32_t key_sym;
uint32_t alt_sym;
uint32_t key_code;
};
static struct key_state key_state_array[KEY_STATE_MAX_LENGTH];
void input_init(void) {
int i;
for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) {
key_state_array[i] = 0;
struct key_state none = { 0, 0, 0 };
key_state_array[i] = none;
}
}
static uint8_t find_key(keycode key) {
static uint8_t find_key(uint32_t key_sym, uint32_t key_code, bool update) {
int i;
for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) {
if (key_state_array[i] == key) {
if (0 == key_sym && 0 == key_code && key_state_array[i].key_sym == 0) {
break;
}
if (key_state_array[i].key_sym == key_sym
|| key_state_array[i].alt_sym == key_sym) {
break;
}
if (update && key_state_array[i].key_code == key_code) {
key_state_array[i].alt_sym = key_sym;
break;
}
}
return i;
}
bool check_key(keycode key) {
return find_key(key) < KEY_STATE_MAX_LENGTH;
bool check_key(uint32_t key_sym, uint32_t key_code) {
return find_key(key_sym, key_code, false) < KEY_STATE_MAX_LENGTH;
}
void press_key(keycode key) {
void press_key(uint32_t key_sym, uint32_t key_code) {
if (key_code == 0) {
return;
}
// Check if key exists
if (!check_key(key)) {
if (!check_key(key_sym, key_code)) {
// Check that we dont exceed buffer length
int insert = find_key(0);
int insert = find_key(0, 0, true);
if (insert < KEY_STATE_MAX_LENGTH) {
key_state_array[insert] = key;
key_state_array[insert].key_sym = key_sym;
key_state_array[insert].key_code = key_code;
}
}
}
void release_key(keycode key) {
uint8_t index = find_key(key);
void release_key(uint32_t key_sym, uint32_t key_code) {
uint8_t index = find_key(key_sym, key_code, true);
if (index < KEY_STATE_MAX_LENGTH) {
// shift it over and remove key
key_state_array[index] = 0;
struct key_state none = { 0, 0, 0 };
key_state_array[index] = none;
}
}