diff options
author | taiyu <[email protected]> | 2015-08-19 18:59:27 -0700 |
---|---|---|
committer | taiyu <[email protected]> | 2015-08-19 18:59:27 -0700 |
commit | 470b4dfbae146d83c0061b39534c16b5aad90f1c (patch) | |
tree | 89560b95f1501ecd24036ca69f524771230fe2f0 /sway/key_state.c | |
parent | 4db89b5fe46e8e7dc741b0ccb9fa3d0708c6fa59 (diff) |
key_state.ch, and command conflicts resolved
Diffstat (limited to 'sway/key_state.c')
-rw-r--r-- | sway/key_state.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/sway/key_state.c b/sway/key_state.c new file mode 100644 index 00000000..76561dbc --- /dev/null +++ b/sway/key_state.c @@ -0,0 +1,52 @@ +#include <string.h> +#include <stdbool.h> +#include <ctype.h> + +#include "key_state.h" + +enum { KEY_STATE_MAX_LENGTH = 64 }; + +static keycode key_state_array[KEY_STATE_MAX_LENGTH]; +static uint8_t key_state_length = 0; + +static uint8_t find_key(keycode key) +{ + int i; + for (i = 0; i < key_state_length; ++i) + { + if (key_state_array[i] == key) + { + break; + } + } + return i; +} + +bool check_key(keycode key) +{ + return find_key(key) < key_state_length; +} + +void press_key(keycode key) +{ + // Check if key exists + if (!check_key(key)) + { + // Check that we dont exceed buffer length + if (key_state_length < KEY_STATE_MAX_LENGTH) { + key_state_array[key_state_length++] = key; + } + } +} + +void release_key(keycode key) +{ + uint8_t index = find_key(key); + if (index < key_state_length) + { + //shift it over and remove key + memmove(&key_state_array[index], + &key_state_array[index + 1], + sizeof(*key_state_array) * (--key_state_length - index)); + } +} |