cleaned up code style, fixed buffer overflow in layers configuration, added a p… | |
git clone git://git.suckless.org/svkbd | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 3d03b522e04f94e384aa9b7d87117a20b7ae0268 | |
parent 8219b6b9d1e86db5f965d94be8c6fdaea1a2b6d1 | |
Author: Maarten van Gompel <[email protected]> | |
Date: Sun, 2 Aug 2020 20:57:06 +0200 | |
cleaned up code style, fixed buffer overflow in layers configuration, added a p… | |
Diffstat: | |
M svkbd.c | 102 +++++++++++++++++------------… | |
1 file changed, 55 insertions(+), 47 deletions(-) | |
--- | |
diff --git a/svkbd.c b/svkbd.c | |
@@ -57,6 +57,7 @@ typedef struct { | |
} Buttonmod; | |
/* function declarations */ | |
+static void printdbg(const char * fmt, ...); | |
static void motionnotify(XEvent *e); | |
static void buttonpress(XEvent *e); | |
static void buttonrelease(XEvent *e); | |
@@ -103,9 +104,9 @@ static KeySym pressedmod = 0; | |
static struct timeval pressbegin; | |
static int currentlayer = 0; | |
static int enableoverlays = 1; | |
-static int currentoverlay = -1; // -1 = no overlay | |
-static KeySym overlaykeysym = 0; //keysym for which the overlay is presented | |
-static int releaseprotect = 0; //set to 1 after overlay is shown, protecting a… | |
+static int currentoverlay = -1; /* -1 = no overlay */ | |
+static KeySym overlaykeysym = 0; /* keysym for which the overlay is presented … | |
+static int releaseprotect = 0; /* set to 1 after overlay is shown, protecting … | |
static int tmp_keycode = 1; | |
static int rows = 0, ww = 0, wh = 0, wx = 0, wy = 0; | |
static char *name = "svkbd"; | |
@@ -348,7 +349,7 @@ leavenotify(XEvent *e) { | |
} | |
void record_press_begin(KeySym ks) { | |
- //record the begin of the press, don't simulate the actual keypress yet | |
+ /* record the begin of the press, don't simulate the actual keypress y… | |
gettimeofday(&pressbegin, NULL); | |
ispressingkeysym = ks; | |
} | |
@@ -359,7 +360,7 @@ press(Key *k, KeySym mod) { | |
int overlayidx = -1; | |
k->pressed = !k->pressed; | |
- if (debug) { printf("Begin press: %ld\n", k->keysym); fflush(stdout); } | |
+ if (debug) printdbg("Begin press: %ld\n", k->keysym); | |
pressbegin.tv_sec = 0; | |
pressbegin.tv_usec = 0; | |
ispressingkeysym = 0; | |
@@ -369,11 +370,11 @@ press(Key *k, KeySym mod) { | |
overlayidx = hasoverlay(k->keysym); | |
if (enableoverlays && overlayidx != -1) { | |
if (!pressbegin.tv_sec && !pressbegin.tv_usec) { | |
- //record the begin of the press, don't simulat… | |
+ /*record the begin of the press, don't simulat… | |
record_press_begin(k->keysym); | |
} | |
} else { | |
- if (debug) { printf("Simulating press: %ld\n", k->keys… | |
+ if (debug) printdbg("Simulating press: %ld\n", k->keys… | |
for(i = 0; i < numkeys; i++) { | |
if(keys[i].pressed && IsModifierKey(keys[i].ke… | |
simulate_keypress(keys[i].keysym); | |
@@ -454,8 +455,8 @@ unpress(Key *k, KeySym mod) { | |
if ((pressbegin.tv_sec || pressbegin.tv_usec) && enableoverlays && k &… | |
if (currentoverlay == -1) { | |
if (get_press_duration() < overlay_delay) { | |
- if (debug) { printf("Delayed simulation of pre… | |
- //simulate the press event, as we postponed it… | |
+ if (debug) printdbg("Delayed simulation of pre… | |
+ /* simulate the press event, as we postponed i… | |
for(i = 0; i < numkeys; i++) { | |
if(keys[i].pressed && IsModifierKey(ke… | |
simulate_keypress(keys[i].keys… | |
@@ -476,9 +477,9 @@ unpress(Key *k, KeySym mod) { | |
if (debug) { | |
if (k) { | |
- printf("Simulation of release: %ld\n", k->keysym); ffl… | |
+ printdbg("Simulation of release: %ld\n", k->keysym); | |
} else { | |
- printf("Simulation of release (all keys)\n"); fflush(s… | |
+ printdbg("Simulation of release (all keys)\n"); | |
} | |
} | |
@@ -530,7 +531,6 @@ run(void) { | |
tv.tv_sec = 1; | |
- //XSync(dpy, False); | |
XFlush(dpy); | |
while (running) { | |
@@ -546,12 +546,12 @@ run(void) { | |
} | |
} | |
} else { | |
- //time-out expired without anything interesting happen… | |
+ /* time-out expired without anything interesting happe… | |
if (ispressing && ispressingkeysym) { | |
duration = get_press_duration(); | |
- if (debug == 2) { printf("%f\n", duration); ff… | |
+ if (debug == 2) printdbg("%f\n", duration); | |
if (get_press_duration() >= overlay_delay) { | |
- if (debug) { printf("press duration %f… | |
+ if (debug) printdbg("press duration %f… | |
showoverlay(hasoverlay(ispressingkeysy… | |
pressbegin.tv_sec = 0; | |
pressbegin.tv_usec = 0; | |
@@ -559,20 +559,20 @@ run(void) { | |
} | |
} | |
} | |
- if (r == -1 || sigtermd) { | |
- // an error occurred or we received a signal | |
- // E.g. Generally in scripts we want to call SIGTERM on svkbd in w… | |
- // if the user is holding for example the enter key (to execu… | |
- // the kill or script that does the kill), that causes an iss… | |
- // since then X doesn't know the keyup is never coming.. (sin… | |
- // process will be dead before finger lifts - in that case we | |
- // just trigger out fake up presses for all keys | |
- if (debug) { printf("signal received, releasing all keys"); fflush… | |
- for (i = 0; i < numkeys; i++) { | |
- XTestFakeKeyEvent(dpy, XKeysymToKeycode(dpy, keys[i].keysym), … | |
- } | |
- running = False; | |
- } | |
+ if (r == -1 || sigtermd) { | |
+ /* an error occurred or we received a signal */ | |
+ /* E.g. Generally in scripts we want to call SIGTERM o… | |
+ if the user is holding for example the enter k… | |
+ the kill or script that does the kill), that c… | |
+ since then X doesn't know the keyup is never c… | |
+ process will be dead before finger lifts - in … | |
+ just trigger out fake up presses for all keys … | |
+ if (debug) printdbg("signal received, releasing all ke… | |
+ for (i = 0; i < numkeys; i++) { | |
+ XTestFakeKeyEvent(dpy, XKeysymToKeycode(dpy, k… | |
+ } | |
+ running = False; | |
+ } | |
} | |
} | |
@@ -610,7 +610,7 @@ setup(void) { | |
die("no fonts could be loaded."); | |
drw_setscheme(drw, scheme[SchemeNorm]); | |
- //find an unused keycode to use as a temporary keycode (derived from s… | |
+ /* find an unused keycode to use as a temporary keycode (derived from … | |
KeySym *keysyms = NULL; | |
int keysyms_per_keycode = 0; | |
int keycode_low, keycode_high; | |
@@ -743,7 +743,7 @@ usage(char *argv0) { | |
fprintf(stderr, " -O - Disable overlays\n"); | |
fprintf(stderr, " -l - Comma separated list of layers to enab… | |
fprintf(stderr, " -s - Layer to select on program start\n"); | |
- fprintf(stderr, " -H [int] - Height fraction, one key row tak… | |
+ fprintf(stderr, " -H [int] - Height fraction, one key row takes 1/x… | |
fprintf(stderr, " -fn [font] - Set font (Xft, e.g: DejaVu Sans:bold:s… | |
exit(1); | |
} | |
@@ -758,7 +758,7 @@ cyclelayer() { | |
currentlayer++; | |
if (currentlayer >= numlayers) | |
currentlayer = 0; | |
- if (debug) { printf("Cycling to layer %d\n", currentlayer); fflush(std… | |
+ if (debug) printdbg("Cycling to layer %d\n", currentlayer); | |
setlayer(); | |
updatekeys(); | |
drawkeyboard(); | |
@@ -771,7 +771,7 @@ togglelayer() { | |
} else if (numlayers > 1) { | |
currentlayer = 1; | |
} | |
- if (debug) { printf("Toggling layer %d\n", currentlayer); fflush(stdou… | |
+ if (debug) printdbg("Toggling layer %d\n", currentlayer); | |
setlayer(); | |
updatekeys(); | |
drawkeyboard(); | |
@@ -780,9 +780,9 @@ togglelayer() { | |
void | |
showoverlay(int idx) { | |
- if (debug) { printf("Showing overlay %d\n", idx); fflush(stdout); } | |
+ if (debug) printdbg("Showing overlay %d\n", idx); | |
int i,j; | |
- //unpress existing key (visually only) | |
+ /* unpress existing key (visually only) */ | |
for(i = 0; i < numkeys; i++) { | |
if(keys[i].pressed && !IsModifierKey(keys[i].keysym)) { | |
keys[i].pressed = 0; | |
@@ -809,7 +809,7 @@ showoverlay(int idx) { | |
void | |
hideoverlay() { | |
- if (debug) { printf("Hiding overlay %d\n", currentoverlay); fflush(std… | |
+ if (debug) printdbg("Hiding overlay %d\n", currentoverlay); | |
currentoverlay = -1; | |
overlaykeysym = 0; | |
currentlayer = -1; | |
@@ -822,7 +822,7 @@ sigterm(int sig) | |
{ | |
running = False; | |
sigtermd = True; | |
- if (debug) { printf("Sigterm received\n"); fflush(stdout); } | |
+ if (debug) printdbg("Sigterm received\n"); | |
} | |
@@ -868,6 +868,15 @@ init_layers(char * layer_names_list, const char * initial_… | |
setlayer(); | |
} | |
+void | |
+printdbg(const char * fmt, ...) { | |
+ va_list args; | |
+ va_start(args, fmt); | |
+ vfprintf(stderr, fmt, args); | |
+ va_end(args); | |
+ fflush(stderr); | |
+} | |
+ | |
int | |
main(int argc, char *argv[]) { | |
int i, xr, yr, bitm; | |
@@ -878,24 +887,24 @@ main(int argc, char *argv[]) { | |
signal(SIGTERM, sigterm); | |
- //parse environment variables | |
+ /* parse environment variables */ | |
if (OVERLAYS <= 1) { | |
enableoverlays = 0; | |
- } else { | |
+ } else { | |
const char* enableoverlays_env = getenv("SVKBD_ENABLEOVERLAYS"… | |
if (enableoverlays_env != NULL) enableoverlays = atoi(enableov… | |
} | |
const char* layers_env = getenv("SVKBD_LAYERS"); | |
if (layers_env != NULL) { | |
- layer_names_list = malloc(128); | |
- if (!layer_names_list) die("memory allocation error\n"); | |
- strcpy(layer_names_list, layers_env); | |
+ if (!strdup(layer_names_list, layers_env)) { | |
+ die("memory allocation error\n"); | |
+ } | |
} | |
const char* heightfactor_s = getenv("SVKBD_HEIGHTFACTOR"); | |
if (heightfactor_s != NULL) | |
heightfactor = atoi(heightfactor_s); | |
- //parse command line arguments | |
+ /* parse command line arguments */ | |
for (i = 1; argv[i]; i++) { | |
if(!strcmp(argv[i], "-v")) { | |
die("svkbd-"VERSION", © 2006-2020 svkbd engineers," | |
@@ -932,11 +941,10 @@ main(int argc, char *argv[]) { | |
} else if(!strcmp(argv[i], "-l")) { | |
if(i >= argc - 1) | |
continue; | |
- if (layer_names_list == NULL) { | |
- layer_names_list = malloc(128); | |
- if (!layer_names_list) die("memory allocation … | |
+ free(layer_names_list); | |
+ if (!strdup(layer_names_list, argv[++i])) { | |
+ die("memory allocation error\n"); | |
} | |
- strcpy(layer_names_list, argv[++i]); | |
} else if(!strcmp(argv[i], "-s")) { | |
if(i >= argc - 1) | |
continue; |