1
0
mirror of git://git.suckless.org/svkbd synced 2024-12-18 04:04:44 +00:00

some code-style changes

This commit is contained in:
Hiltjo Posthuma 2021-03-28 14:51:00 +02:00
parent ea4b058833
commit 9c492b6913

103
svkbd.c
View File

@ -165,7 +165,8 @@ motionnotify(XEvent *e)
for (i = 0; i < numkeys; i++) { for (i = 0; i < numkeys; i++) {
if (!IsModifierKey(keys[i].keysym) && keys[i].pressed == True && lostfocus != gainedfocus) { if (!IsModifierKey(keys[i].keysym) && keys[i].pressed == True && lostfocus != gainedfocus) {
if (debug) printdbg("Pressed key lost focus: %ld\n", keys[i].keysym); if (debug)
printdbg("Pressed key lost focus: %ld\n", keys[i].keysym);
lostfocus = i; lostfocus = i;
ispressingkeysym = 0; ispressingkeysym = 0;
keys[i].pressed = 0; keys[i].pressed = 0;
@ -174,11 +175,11 @@ motionnotify(XEvent *e)
} }
if ((lostfocus != -1) && (gainedfocus != -1) && (lostfocus != gainedfocus)) { if ((lostfocus != -1) && (gainedfocus != -1) && (lostfocus != gainedfocus)) {
if (debug) printdbg("Clicking new key that gained focus\n"); if (debug)
printdbg("Clicking new key that gained focus\n");
press(&keys[gainedfocus], 0); press(&keys[gainedfocus], 0);
keys[gainedfocus].pressed = True; keys[gainedfocus].pressed = True;
keys[gainedfocus].highlighted = True; keys[gainedfocus].highlighted = True;
} }
} }
@ -195,16 +196,16 @@ buttonpress(XEvent *e)
if (!(k = findkey(ev->x, ev->y))) if (!(k = findkey(ev->x, ev->y)))
return; return;
if (k->modifier) if (k->modifier) {
mod = k->modifier; mod = k->modifier;
else } else {
for (i = 0; i < LENGTH(buttonmods); i++) { for (i = 0; i < LENGTH(buttonmods); i++) {
if (ev->button == buttonmods[i].button) { if (ev->button == buttonmods[i].button) {
mod = buttonmods[i].mod; mod = buttonmods[i].mod;
break; break;
} }
} }
}
press(k, mod); press(k, mod);
} }
@ -432,7 +433,8 @@ press(Key *k, KeySym buttonmod)
k->pressed = !k->pressed; k->pressed = !k->pressed;
if (debug) printdbg("Begin click: %ld\n", k->keysym); if (debug)
printdbg("Begin click: %ld\n", k->keysym);
pressbegin.tv_sec = 0; pressbegin.tv_sec = 0;
pressbegin.tv_usec = 0; pressbegin.tv_usec = 0;
ispressingkeysym = 0; ispressingkeysym = 0;
@ -444,17 +446,18 @@ press(Key *k, KeySym buttonmod)
/*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 yet */
record_press_begin(k->keysym); record_press_begin(k->keysym);
} else { } else {
if (debug) printdbg("Simulating press: %ld (mod %ld)\n", k->keysym, buttonmod); if (debug)
printdbg("Simulating press: %ld (mod %ld)\n", k->keysym, buttonmod);
for (i = 0; i < numkeys; i++) { for (i = 0; i < numkeys; i++) {
if (keys[i].pressed && IsModifierKey(keys[i].keysym)) { if (keys[i].pressed && IsModifierKey(keys[i].keysym)) {
simulate_keypress(keys[i].keysym); simulate_keypress(keys[i].keysym);
} }
} }
if (buttonmod) { if (buttonmod)
simulate_keypress(buttonmod); simulate_keypress(buttonmod);
}
simulate_keypress(k->keysym); simulate_keypress(k->keysym);
if (printoutput) printkey(k, buttonmod); if (printoutput)
printkey(k, buttonmod);
for (i = 0; i < numkeys; i++) { for (i = 0; i < numkeys; i++) {
if (keys[i].pressed && IsModifierKey(keys[i].keysym)) { if (keys[i].pressed && IsModifierKey(keys[i].keysym)) {
@ -476,19 +479,24 @@ tmp_remap(KeySym keysym)
} }
void void
printkey(Key *k, KeySym mod) { printkey(Key *k, KeySym mod)
int i; {
int shift = (mod == XK_Shift_L) || (mod == XK_Shift_R) || (mod == XK_Shift_Lock); int i, shift;
shift = (mod == XK_Shift_L) || (mod == XK_Shift_R) || (mod == XK_Shift_Lock);
if (!shift) { if (!shift) {
for (i = 0; i < numkeys; i++) { for (i = 0; i < numkeys; i++) {
if ((keys[i].pressed) && ((keys[i].keysym == XK_Shift_L) || (keys[i].keysym == XK_Shift_R) || (keys[i].keysym == XK_Shift_Lock))) { if ((keys[i].pressed) && ((keys[i].keysym == XK_Shift_L) ||
(keys[i].keysym == XK_Shift_R) || (keys[i].keysym == XK_Shift_Lock))) {
shift = True; shift = True;
break; break;
} }
} }
} }
if (debug) printdbg("Printing key %ld (shift=%d)\n", k->keysym, shift); if (debug)
if (k->keysym == XK_Cancel) return; printdbg("Printing key %ld (shift=%d)\n", k->keysym, shift);
if (k->keysym == XK_Cancel)
return;
KeySym * keysym = &(k->keysym); KeySym * keysym = &(k->keysym);
XIM xim = XOpenIM(dpy, 0, 0, 0); XIM xim = XOpenIM(dpy, 0, 0, 0);
XIC xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, NULL); XIC xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, NULL);
@ -506,7 +514,8 @@ printkey(Key *k, KeySym mod) {
Status return_status; Status return_status;
int l = Xutf8LookupString(xic, &event, buffer, 32, &ignore, &return_status); int l = Xutf8LookupString(xic, &event, buffer, 32, &ignore, &return_status);
buffer[l] = '\0'; buffer[l] = '\0';
if (debug) printdbg("Print buffer: [%s] (length=%d)\n", &buffer, l); if (debug)
printdbg("Print buffer: [%s] (length=%d)\n", &buffer, l);
printf("%s", buffer); printf("%s", buffer);
XDestroyIC(xic); XDestroyIC(xic);
@ -570,7 +579,8 @@ unpress(Key *k, KeySym buttonmod)
} }
if ((pressbegin.tv_sec || pressbegin.tv_usec) && (enableoverlays || pressonrelease) && k && k->keysym == ispressingkeysym) { if ((pressbegin.tv_sec || pressbegin.tv_usec) && (enableoverlays || pressonrelease) && k && k->keysym == ispressingkeysym) {
if (debug) printdbg("Delayed simulation of press after release: %ld\n", k->keysym); if (debug)
printdbg("Delayed simulation of press after release: %ld\n", k->keysym);
/* simulate the press event, as we postponed it earlier in press() */ /* simulate the press event, as we postponed it earlier in press() */
for (i = 0; i < numkeys; i++) { for (i = 0; i < numkeys; i++) {
if (keys[i].pressed && IsModifierKey(keys[i].keysym)) { if (keys[i].pressed && IsModifierKey(keys[i].keysym)) {
@ -597,7 +607,8 @@ unpress(Key *k, KeySym buttonmod)
for (i = 0; i < numkeys; i++) { for (i = 0; i < numkeys; i++) {
if (keys[i].pressed && !IsModifierKey(keys[i].keysym)) { if (keys[i].pressed && !IsModifierKey(keys[i].keysym)) {
simulate_keyrelease(keys[i].keysym); simulate_keyrelease(keys[i].keysym);
if (printoutput) printkey(&keys[i], buttonmod); if (printoutput)
printkey(&keys[i], buttonmod);
keys[i].pressed = 0; keys[i].pressed = 0;
drawkey(&keys[i]); drawkey(&keys[i]);
} }
@ -659,17 +670,20 @@ run(void)
/* time-out expired without anything interesting happening, check for long-presses */ /* time-out expired without anything interesting happening, check for long-presses */
if (ispressing && ispressingkeysym) { if (ispressing && ispressingkeysym) {
duration = get_press_duration(); duration = get_press_duration();
if (debug == 2) printdbg("%f\n", duration); if (debug == 2)
printdbg("%f\n", duration);
overlayidx = hasoverlay(ispressingkeysym); overlayidx = hasoverlay(ispressingkeysym);
duration = get_press_duration(); duration = get_press_duration();
if ((overlayidx != -1) && (duration >= overlay_delay)) { if ((overlayidx != -1) && (duration >= overlay_delay)) {
if (debug) printdbg("press duration %f, activating overlay\n", duration); if (debug)
printdbg("press duration %f, activating overlay\n", duration);
showoverlay(overlayidx); showoverlay(overlayidx);
pressbegin.tv_sec = 0; pressbegin.tv_sec = 0;
pressbegin.tv_usec = 0; pressbegin.tv_usec = 0;
ispressingkeysym = 0; ispressingkeysym = 0;
} else if ((overlayidx == -1) && (duration >= repeat_delay)) { } else if ((overlayidx == -1) && (duration >= repeat_delay)) {
if (debug) printdbg("press duration %f, activating repeat\n", duration); if (debug)
printdbg("press duration %f, activating repeat\n", duration);
simulate_keyrelease(ispressingkeysym); simulate_keyrelease(ispressingkeysym);
simulate_keypress(ispressingkeysym); simulate_keypress(ispressingkeysym);
XSync(dpy, False); XSync(dpy, False);
@ -684,7 +698,8 @@ run(void)
since then X doesn't know the keyup is never coming.. (since since then X doesn't know the keyup is never coming.. (since
process will be dead before finger lifts - in that case we process will be dead before finger lifts - in that case we
just trigger out fake up presses for all keys */ just trigger out fake up presses for all keys */
if (debug) printdbg("signal received, releasing all keys"); if (debug)
printdbg("signal received, releasing all keys");
for (i = 0; i < numkeys; i++) { for (i = 0; i < numkeys; i++) {
XTestFakeKeyEvent(dpy, XKeysymToKeycode(dpy, keys[i].keysym), False, 0); XTestFakeKeyEvent(dpy, XKeysymToKeycode(dpy, keys[i].keysym), False, 0);
} }
@ -694,14 +709,18 @@ run(void)
} }
void void
readxresources(void) { readxresources(void)
{
XrmDatabase xdb;
XrmValue xval;
char *type, *xrm;
XrmInitialize(); XrmInitialize();
char* xrm;
if ((xrm = XResourceManagerString(drw->dpy))) { if ((xrm = XResourceManagerString(drw->dpy))) {
char *type;
XrmDatabase xdb = XrmGetStringDatabase(xrm); xdb = XrmGetStringDatabase(xrm);
XrmValue xval;
if (XrmGetResource(xdb, "svkbd.font", "*", &type, &xval) && !fonts[0]) if (XrmGetResource(xdb, "svkbd.font", "*", &type, &xval) && !fonts[0])
fonts[0] = estrdup(xval.addr); fonts[0] = estrdup(xval.addr);
@ -761,7 +780,6 @@ readxresources(void) {
} }
} }
void void
setup(void) setup(void)
{ {
@ -797,11 +815,11 @@ setup(void)
readxresources(); readxresources();
/* Apply defaults to font and colors*/ /* Apply defaults to font and colors*/
if ( !fonts[0] ) if (!fonts[0])
fonts[0] = estrdup(defaultfonts[0]); fonts[0] = estrdup(defaultfonts[0]);
for (i = 0; i < SchemeLast; ++i){ for (i = 0; i < SchemeLast; ++i) {
for (j = 0; j < 2; ++j){ for (j = 0; j < 2; ++j) {
if ( !colors[i][j] ) if (!colors[i][j])
colors[i][j] = estrdup(defaultcolors[i][j]); colors[i][j] = estrdup(defaultcolors[i][j]);
} }
} }
@ -974,7 +992,8 @@ cyclelayer(void)
currentlayer++; currentlayer++;
if (currentlayer >= numlayers) if (currentlayer >= numlayers)
currentlayer = 0; currentlayer = 0;
if (debug) printdbg("Cycling to layer %d\n", currentlayer); if (debug)
printdbg("Cycling to layer %d\n", currentlayer);
setlayer(); setlayer();
updatekeys(); updatekeys();
drawkeyboard(); drawkeyboard();
@ -988,7 +1007,8 @@ togglelayer(void)
} else if (numlayers > 1) { } else if (numlayers > 1) {
currentlayer = 1; currentlayer = 1;
} }
if (debug) printdbg("Toggling layer %d\n", currentlayer); if (debug)
printdbg("Toggling layer %d\n", currentlayer);
setlayer(); setlayer();
updatekeys(); updatekeys();
drawkeyboard(); drawkeyboard();
@ -997,7 +1017,8 @@ togglelayer(void)
void void
showoverlay(int idx) showoverlay(int idx)
{ {
if (debug) printdbg("Showing overlay %d\n", idx); if (debug)
printdbg("Showing overlay %d\n", idx);
int i,j; int i,j;
/* unpress existing key (visually only) */ /* unpress existing key (visually only) */
@ -1034,7 +1055,8 @@ showoverlay(int idx)
void void
hideoverlay(void) hideoverlay(void)
{ {
if (debug) printdbg("Hiding overlay, overlay was #%d\n", currentoverlay); if (debug)
printdbg("Hiding overlay, overlay was #%d\n", currentoverlay);
currentoverlay = -1; currentoverlay = -1;
overlaykeysym = 0; overlaykeysym = 0;
currentlayer--; currentlayer--;
@ -1046,7 +1068,8 @@ sigterm(int signo)
{ {
running = False; running = False;
sigtermd = True; sigtermd = True;
if (debug) printdbg("SIGTERM received\n"); if (debug)
printdbg("SIGTERM received\n");
} }
void void