diff options
| author | 2014-07-29 22:47:01 +0200 | |
|---|---|---|
| committer | 2014-07-30 01:11:08 +0200 | |
| commit | 764e618fc7d2f0128b916ceb36e65ca6983921d4 (patch) | |
| tree | 5f61b0d13cb01b805819672f5ceaf4f08c17ad72 | |
| parent | d73347e15ea3f7c6d506bbb8073faa448af7439f (diff) | |
| download | xkbcat-764e618fc7d2f0128b916ceb36e65ca6983921d4.tar.gz xkbcat-764e618fc7d2f0128b916ceb36e65ca6983921d4.zip | |
Clarify key state buffer handling
The typedef makes stuff a little more readable and introduces to the
typechecker that its size must be 32 elements.
Naming clarifications too.
The logic in the inner conditionals was often `fflush`ing even when
there had been no `printf`.
| -rw-r--r-- | xkbcat.c | 37 |
1 files changed, 22 insertions, 15 deletions
@@ -19,7 +19,10 @@ char * DEFAULT_DISPLAY = ":0"; const int DEFAULT_DELAY = 10000000; const bool DEFAULT_PRINT_UP = false; -static inline int BIT(char *c, int x) { return ( c[x/8]& (1<<(x%8)) ); } +typedef char KbBuffer[32]; +static inline bool keyState(KbBuffer c, int key) { + return ( c[key/8] & (1<<(key%8)) ); +} const int KEYSYM_STRLEN = 64; char *keyPressToString(Display *disp, int code, bool down); @@ -58,27 +61,31 @@ int main(int argc, char *argv[]) { XSynchronize(disp, true); // Setup buffers - char keyBuffer1[32], keyBuffer2[32]; - char *saved = keyBuffer1, - *keys = keyBuffer2; - XQueryKeymap(disp, saved); + KbBuffer keyBuffer1, keyBuffer2; + KbBuffer *oldKeys = &keyBuffer1, + *keys = &keyBuffer2; + XQueryKeymap(disp, *oldKeys); // Initial get struct timespec sleepTime = { .tv_nsec = delay }; - while (1) { - // find changed keys - XQueryKeymap(disp, keys); - for (int i = 0; i < 32*8; i++) { - if (BIT(keys, i) != BIT(saved, i)) { - register char *str = keyPressToString(disp, i, BIT(keys, i)); - if (BIT(keys, i) != 0 || printKeyUps) printf("%s\n",str); - fflush(stdout); // In case user is writing to a pipe + while (1) { // Forever + // Get changed keys + XQueryKeymap(disp, *keys); + + for (int i = 0; i < sizeof(KbBuffer)*8; i++) { + bool stateBefore = keyState(*oldKeys, i), + stateNow = keyState(*keys, i); + if ( stateNow != stateBefore // Changed? + && (stateNow || printKeyUps) ) // Print? + { + printf("%s\n",keyPressToString(disp, i, stateNow)); + fflush(stdout); // Ensure pipe is updated } } { // Swap buffers - char * temp = saved; - saved = keys; + KbBuffer *temp = oldKeys; + oldKeys = keys; keys = temp; } |
