From 8f9b5d06852289115738267cc9fea21fc2ce38ed Mon Sep 17 00:00:00 2001 From: Antti K Date: Tue, 29 Jul 2014 23:08:43 +0200 Subject: Simplify result printing There's no need to copy strings around when all we want is to print it anyway. This is faster and easier to read. Also made the output format adapt to whether `printKeyUps` is set: There's no need to print `+` and `-` if all that we're printing is keydowns anyway. --- xkbcat.c | 46 +++++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/xkbcat.c b/xkbcat.c index cf138c3..f4a735e 100644 --- a/xkbcat.c +++ b/xkbcat.c @@ -19,7 +19,7 @@ static inline bool keyState(KbBuffer c, int key) { return ( c[key/8] & (1<<(key%8)) ); } -char * keyPressToString(Display * disp, int code, bool down); +void printKeyPress(Display * disp, int code, bool down, bool printKeyUps); int printUsage() { printf("\ @@ -71,10 +71,7 @@ int main(int argc, char * argv[]) { stateNow = keyState(*keys, i); if ( stateNow != stateBefore // Changed? && (stateNow || printKeyUps) ) // Should print? - { - printf("%s\n", keyPressToString(disp, i, stateNow)); - fflush(stdout); // Ensure pipe is updated - } + printKeyPress(disp, i, stateNow, printKeyUps); } { // Swap buffers @@ -87,28 +84,19 @@ int main(int argc, char * argv[]) { } } -/* - Have a keycode, Look up keysym for it. - Convert keysym into its string representation. - Put it as (+string) or (-string), depending on if it's up or down. - Print out the string. - */ - -char * keyPressToString(Display * disp, int code, bool down) { - const int KEYSYM_STRLEN = 64; - static char * str, buf[KEYSYM_STRLEN + 1]; - KeySym keysym = XkbKeycodeToKeysym(disp, code, 0, 0); - if (NoSymbol == keysym) return ""; - - // Convert keysym to a string, copy it to a local area - str = XKeysymToString(keysym); - - if (NULL == str) return ""; - strncpy(buf, str, KEYSYM_STRLEN); buf[KEYSYM_STRLEN] = 0; - - // Still a string, so put it in form (+str) or (-str) - if (down) strcpy(buf, "+ "); - else strcpy(buf, "- "); - strcat(buf, str); - return buf; +// Since `XKeysymToString` returns a string of unknown length that shouldn't be +// modified, it makes more sense to just `printf` the thing in-place without +// copying it anywhere. It's not needed anywhere else anyway. +void printKeyPress(Display * disp, int code, bool down, bool printKeyUps) { + + KeySym s = XkbKeycodeToKeysym(disp, code, 0, 0); if (NoSymbol == s) return; + char * str = XKeysymToString(s); if (NULL == str) return; + + if (printKeyUps) + printf("%s %s\n", + (down ? "+" : "-"), + str); + else + printf("%s\n", str); + } -- cgit v1.3-2-g0d8e