some code-style consistency changes + small tweaks
This commit is contained in:
parent
4983666e33
commit
73f0edf709
227
svkbd.c
227
svkbd.c
|
@ -1,12 +1,16 @@
|
|||
/* See LICENSE file for copyright and license details.
|
||||
*
|
||||
* To understand svkbd, start reading main().
|
||||
*/
|
||||
/* See LICENSE file for copyright and license details. */
|
||||
#include <sys/select.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <locale.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <X11/keysym.h>
|
||||
#include <X11/keysymdef.h>
|
||||
#include <X11/XF86keysym.h>
|
||||
|
@ -19,16 +23,10 @@
|
|||
#ifdef XINERAMA
|
||||
#include <X11/extensions/Xinerama.h>
|
||||
#endif
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "drw.h"
|
||||
#include "util.h"
|
||||
|
||||
|
||||
/* macros */
|
||||
#define LENGTH(x) (sizeof x / sizeof x[0])
|
||||
#define TEXTW(X) (drw_fontset_getwidth(drw, (X)))
|
||||
|
@ -64,7 +62,7 @@ static void buttonrelease(XEvent *e);
|
|||
static void cleanup(void);
|
||||
static void configurenotify(XEvent *e);
|
||||
static void countrows();
|
||||
static int countkeys(Key *k);
|
||||
static int countkeys(Key *layer);
|
||||
static void drawkeyboard(void);
|
||||
static void drawkey(Key *k);
|
||||
static void expose(XEvent *e);
|
||||
|
@ -164,11 +162,12 @@ motionnotify(XEvent *e)
|
|||
}
|
||||
|
||||
void
|
||||
buttonpress(XEvent *e) {
|
||||
int i;
|
||||
buttonpress(XEvent *e)
|
||||
{
|
||||
XButtonPressedEvent *ev = &e->xbutton;
|
||||
Key *k;
|
||||
KeySym mod = 0;
|
||||
int i;
|
||||
|
||||
ispressing = True;
|
||||
|
||||
|
@ -183,11 +182,12 @@ buttonpress(XEvent *e) {
|
|||
}
|
||||
|
||||
void
|
||||
buttonrelease(XEvent *e) {
|
||||
int i;
|
||||
buttonrelease(XEvent *e)
|
||||
{
|
||||
XButtonPressedEvent *ev = &e->xbutton;
|
||||
Key *k;
|
||||
KeySym mod = 0;
|
||||
int i;
|
||||
|
||||
ispressing = False;
|
||||
|
||||
|
@ -207,7 +207,8 @@ buttonrelease(XEvent *e) {
|
|||
}
|
||||
|
||||
void
|
||||
cleanup(void) {
|
||||
cleanup(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < SchemeLast; i++)
|
||||
|
@ -222,7 +223,8 @@ cleanup(void) {
|
|||
}
|
||||
|
||||
void
|
||||
configurenotify(XEvent *e) {
|
||||
configurenotify(XEvent *e)
|
||||
{
|
||||
XConfigureEvent *ev = &e->xconfigure;
|
||||
|
||||
if (ev->window == win && (ev->width != ww || ev->height != wh)) {
|
||||
|
@ -234,8 +236,9 @@ configurenotify(XEvent *e) {
|
|||
}
|
||||
|
||||
void
|
||||
countrows() {
|
||||
int i = 0;
|
||||
countrows(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0, rows = 1; i < numkeys; i++) {
|
||||
if (keys[i].keysym == 0)
|
||||
|
@ -244,24 +247,24 @@ countrows() {
|
|||
}
|
||||
|
||||
int
|
||||
countkeys(Key * layer) {
|
||||
int keys = 0;
|
||||
int i;
|
||||
countkeys(Key *layer)
|
||||
{
|
||||
int i, nkeys = 0;
|
||||
|
||||
for (i = 0; i < KEYS; i++) {
|
||||
if (i > 0 && layer[i].keysym == 0 && layer[i - 1].keysym == 0) {
|
||||
keys--;
|
||||
nkeys--;
|
||||
break;
|
||||
}
|
||||
keys++;
|
||||
nkeys++;
|
||||
}
|
||||
|
||||
return keys;
|
||||
return nkeys;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
drawkeyboard(void) {
|
||||
drawkeyboard(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < numkeys; i++) {
|
||||
|
@ -271,7 +274,8 @@ drawkeyboard(void) {
|
|||
}
|
||||
|
||||
void
|
||||
drawkey(Key *k) {
|
||||
drawkey(Key *k)
|
||||
{
|
||||
int x, y, w, h;
|
||||
const char *l;
|
||||
|
||||
|
@ -304,7 +308,8 @@ drawkey(Key *k) {
|
|||
}
|
||||
|
||||
void
|
||||
expose(XEvent *e) {
|
||||
expose(XEvent *e)
|
||||
{
|
||||
XExposeEvent *ev = &e->xexpose;
|
||||
|
||||
if (ev->count == 0 && (ev->window == win))
|
||||
|
@ -325,10 +330,11 @@ findkey(int x, int y) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
hasoverlay(KeySym keysym) {
|
||||
hasoverlay(KeySym keysym)
|
||||
{
|
||||
int begin, i;
|
||||
|
||||
begin = 0;
|
||||
for (i = 0; i < OVERLAYS; i++) {
|
||||
if (overlay[i].keysym == XK_Cancel) {
|
||||
|
@ -341,23 +347,28 @@ hasoverlay(KeySym keysym) {
|
|||
}
|
||||
|
||||
void
|
||||
leavenotify(XEvent *e) {
|
||||
leavenotify(XEvent *e)
|
||||
{
|
||||
if (currentoverlay != -1) {
|
||||
hideoverlay();
|
||||
}
|
||||
unpress(NULL, 0);
|
||||
}
|
||||
|
||||
void record_press_begin(KeySym ks) {
|
||||
void
|
||||
record_press_begin(KeySym ks)
|
||||
{
|
||||
/* record the begin of the press, don't simulate the actual keypress yet */
|
||||
gettimeofday(&pressbegin, NULL);
|
||||
ispressingkeysym = ks;
|
||||
}
|
||||
|
||||
void
|
||||
press(Key *k, KeySym mod) {
|
||||
press(Key *k, KeySym mod)
|
||||
{
|
||||
int i;
|
||||
int overlayidx = -1;
|
||||
|
||||
k->pressed = !k->pressed;
|
||||
|
||||
if (debug) printdbg("Begin press: %ld\n", k->keysym);
|
||||
|
@ -396,18 +407,18 @@ press(Key *k, KeySym mod) {
|
|||
drawkey(k);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int tmp_remap(KeySym keysym) {
|
||||
int
|
||||
tmp_remap(KeySym keysym)
|
||||
{
|
||||
XChangeKeyboardMapping(dpy, tmp_keycode, 1, &keysym, 1);
|
||||
XSync(dpy, False);
|
||||
|
||||
return tmp_keycode;
|
||||
}
|
||||
|
||||
void
|
||||
simulate_keypress(KeySym keysym) {
|
||||
simulate_keypress(KeySym keysym)
|
||||
{
|
||||
KeyCode code = XKeysymToKeycode(dpy, keysym);
|
||||
if (code == 0)
|
||||
code = tmp_remap(keysym);
|
||||
|
@ -415,22 +426,29 @@ simulate_keypress(KeySym keysym) {
|
|||
}
|
||||
|
||||
void
|
||||
simulate_keyrelease(KeySym keysym) {
|
||||
simulate_keyrelease(KeySym keysym)
|
||||
{
|
||||
KeyCode code = XKeysymToKeycode(dpy, keysym);
|
||||
if (code == 0)
|
||||
code = tmp_remap(keysym);
|
||||
XTestFakeKeyEvent(dpy, code, False, 0);
|
||||
}
|
||||
|
||||
|
||||
double get_press_duration() {
|
||||
double
|
||||
get_press_duration(void)
|
||||
{
|
||||
struct timeval now;
|
||||
|
||||
gettimeofday(&now, NULL);
|
||||
return (double) ((now.tv_sec * 1000000L + now.tv_usec) - (pressbegin.tv_sec * 1000000L + pressbegin.tv_usec)) / (double) 1000000L;
|
||||
|
||||
return (double) ((now.tv_sec * 1000000L + now.tv_usec) -
|
||||
(pressbegin.tv_sec * 1000000L + pressbegin.tv_usec)) /
|
||||
(double) 1000000L;
|
||||
}
|
||||
|
||||
void
|
||||
unpress(Key *k, KeySym mod) {
|
||||
unpress(Key *k, KeySym mod)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (k != NULL) {
|
||||
|
@ -451,7 +469,6 @@ unpress(Key *k, KeySym mod) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
if ((pressbegin.tv_sec || pressbegin.tv_usec) && enableoverlays && k && k->keysym == ispressingkeysym) {
|
||||
if (currentoverlay == -1) {
|
||||
if (get_press_duration() < overlay_delay) {
|
||||
|
@ -517,7 +534,8 @@ unpress(Key *k, KeySym mod) {
|
|||
}
|
||||
|
||||
void
|
||||
run(void) {
|
||||
run(void)
|
||||
{
|
||||
XEvent ev;
|
||||
int xfd;
|
||||
fd_set fds;
|
||||
|
@ -525,16 +543,14 @@ run(void) {
|
|||
double duration = 0.0;
|
||||
int i, r;
|
||||
|
||||
|
||||
xfd = ConnectionNumber(dpy);
|
||||
tv.tv_usec = 0;
|
||||
tv.tv_sec = 1;
|
||||
|
||||
tv.tv_usec = 0;
|
||||
|
||||
XFlush(dpy);
|
||||
|
||||
while (running) {
|
||||
usleep(100000L);
|
||||
usleep(100000L); /* 100ms */
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(xfd, &fds);
|
||||
r = select(xfd + 1, &fds, NULL, NULL, &tv);
|
||||
|
@ -577,7 +593,8 @@ run(void) {
|
|||
}
|
||||
|
||||
void
|
||||
setup(void) {
|
||||
setup(void)
|
||||
{
|
||||
XSetWindowAttributes wa;
|
||||
XTextProperty str;
|
||||
XSizeHints *sizeh = NULL;
|
||||
|
@ -586,14 +603,14 @@ setup(void) {
|
|||
int i, j, sh, sw;
|
||||
XWMHints *wmh;
|
||||
|
||||
#if XINERAMA
|
||||
#ifdef XINERAMA
|
||||
XineramaScreenInfo *info = NULL;
|
||||
#endif
|
||||
|
||||
/* init screen */
|
||||
screen = DefaultScreen(dpy);
|
||||
root = RootWindow(dpy, screen);
|
||||
#if XINERAMA
|
||||
#ifdef XINERAMA
|
||||
if (XineramaIsActive(dpy)) {
|
||||
info = XineramaQueryScreens(dpy, &i);
|
||||
sw = info[0].width;
|
||||
|
@ -607,15 +624,17 @@ setup(void) {
|
|||
}
|
||||
drw = drw_create(dpy, screen, root, sw, sh);
|
||||
if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
|
||||
die("no fonts could be loaded.");
|
||||
die("no fonts could be loaded");
|
||||
drw_setscheme(drw, scheme[SchemeNorm]);
|
||||
|
||||
/* find an unused keycode to use as a temporary keycode (derived from source: https://stackoverflow.com/questions/44313966/c-xtest-emitting-key-presses-for-every-unicode-character) */
|
||||
/* find an unused keycode to use as a temporary keycode (derived from source:
|
||||
https://stackoverflow.com/questions/44313966/c-xtest-emitting-key-presses-for-every-unicode-character) */
|
||||
KeySym *keysyms = NULL;
|
||||
int keysyms_per_keycode = 0;
|
||||
int keycode_low, keycode_high;
|
||||
Bool key_is_empty;
|
||||
int symindex;
|
||||
|
||||
XDisplayKeycodes(dpy, &keycode_low, &keycode_high);
|
||||
keysyms = XGetKeyboardMapping(dpy, keycode_low, keycode_high - keycode_low, &keysyms_per_keycode);
|
||||
for (i = keycode_low; i <= keycode_high; i++) {
|
||||
|
@ -711,9 +730,9 @@ setup(void) {
|
|||
drawkeyboard();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
updatekeys() {
|
||||
updatekeys(void)
|
||||
{
|
||||
int i, j;
|
||||
int x = 0, y = 0, h, base, r = rows;
|
||||
|
||||
|
@ -735,7 +754,8 @@ updatekeys() {
|
|||
}
|
||||
|
||||
void
|
||||
usage(char *argv0) {
|
||||
usage(char *argv0)
|
||||
{
|
||||
fprintf(stderr, "usage: %s [-hdvDO] [-g geometry] [-fn font] [-l layers] [-s initial_layer]\n", argv0);
|
||||
fprintf(stderr, "Options:\n");
|
||||
fprintf(stderr, " -d - Set Dock Window Type\n");
|
||||
|
@ -748,13 +768,16 @@ usage(char *argv0) {
|
|||
exit(1);
|
||||
}
|
||||
|
||||
void setlayer() {
|
||||
void
|
||||
setlayer(void)
|
||||
{
|
||||
numkeys = countkeys(layers[currentlayer]);
|
||||
memcpy(&keys, layers[currentlayer], sizeof(Key) * numkeys);
|
||||
}
|
||||
|
||||
void
|
||||
cyclelayer() {
|
||||
cyclelayer(void)
|
||||
{
|
||||
currentlayer++;
|
||||
if (currentlayer >= numlayers)
|
||||
currentlayer = 0;
|
||||
|
@ -765,7 +788,8 @@ cyclelayer() {
|
|||
}
|
||||
|
||||
void
|
||||
togglelayer() {
|
||||
togglelayer(void)
|
||||
{
|
||||
if (currentlayer > 0) {
|
||||
currentlayer = 0;
|
||||
} else if (numlayers > 1) {
|
||||
|
@ -777,11 +801,12 @@ togglelayer() {
|
|||
drawkeyboard();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
showoverlay(int idx) {
|
||||
showoverlay(int idx)
|
||||
{
|
||||
if (debug) printdbg("Showing overlay %d\n", idx);
|
||||
int i,j;
|
||||
|
||||
/* unpress existing key (visually only) */
|
||||
for (i = 0; i < numkeys; i++) {
|
||||
if (keys[i].pressed && !IsModifierKey(keys[i].keysym)) {
|
||||
|
@ -808,7 +833,8 @@ showoverlay(int idx) {
|
|||
}
|
||||
|
||||
void
|
||||
hideoverlay() {
|
||||
hideoverlay(void)
|
||||
{
|
||||
if (debug) printdbg("Hiding overlay %d\n", currentoverlay);
|
||||
currentoverlay = -1;
|
||||
overlaykeysym = 0;
|
||||
|
@ -816,19 +842,20 @@ hideoverlay() {
|
|||
cyclelayer();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
sigterm(int sig)
|
||||
sigterm(int signo)
|
||||
{
|
||||
running = False;
|
||||
sigtermd = True;
|
||||
if (debug) printdbg("Sigterm received\n");
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
init_layers(char * layer_names_list, const char * initial_layer_name) {
|
||||
int j;
|
||||
init_layers(char *layer_names_list, const char *initial_layer_name)
|
||||
{
|
||||
char *s;
|
||||
int j, found;
|
||||
|
||||
if (layer_names_list == NULL) {
|
||||
numlayers = LAYERS;
|
||||
memcpy(&layers, &available_layers, sizeof(available_layers));
|
||||
|
@ -841,11 +868,11 @@ init_layers(char * layer_names_list, const char * initial_layer_name) {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
char * s;
|
||||
s = strtok(layer_names_list, ",");
|
||||
while (s != NULL) {
|
||||
if (numlayers+1 > LAYERS) die("too many layers specified");
|
||||
int found = 0;
|
||||
if (numlayers+1 > LAYERS)
|
||||
die("too many layers specified");
|
||||
found = 0;
|
||||
for (j = 0; j < LAYERS; j++) {
|
||||
if (strcmp(layer_names[j], s) == 0) {
|
||||
fprintf(stderr, "Adding layer %s\n", s);
|
||||
|
@ -869,45 +896,44 @@ init_layers(char * layer_names_list, const char * initial_layer_name) {
|
|||
}
|
||||
|
||||
void
|
||||
printdbg(const char * fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vfprintf(stderr, fmt, args);
|
||||
va_end(args);
|
||||
printdbg(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vfprintf(stderr, fmt, ap);
|
||||
va_end(ap);
|
||||
fflush(stderr);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[]) {
|
||||
int i, xr, yr, bitm;
|
||||
unsigned int wr, hr;
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
char *initial_layer_name = NULL;
|
||||
char *layer_names_list = NULL;
|
||||
char *tmp;
|
||||
int i, xr, yr, bitm;
|
||||
unsigned int wr, hr;
|
||||
|
||||
signal(SIGTERM, sigterm);
|
||||
|
||||
/* parse environment variables */
|
||||
if (OVERLAYS <= 1) {
|
||||
enableoverlays = 0;
|
||||
} else {
|
||||
const char* enableoverlays_env = getenv("SVKBD_ENABLEOVERLAYS");
|
||||
if (enableoverlays_env != NULL) enableoverlays = atoi(enableoverlays_env);
|
||||
if ((tmp = getenv("SVKBD_ENABLEOVERLAYS")))
|
||||
enableoverlays = atoi(tmp);
|
||||
}
|
||||
char *layers_env = getenv("SVKBD_LAYERS");
|
||||
if (layers_env != NULL) {
|
||||
if (!(layer_names_list = strdup(layers_env))) {
|
||||
die("memory allocation error\n");
|
||||
if ((tmp = getenv("SVKBD_LAYERS"))) {
|
||||
if (!(layer_names_list = strdup(tmp)))
|
||||
die("memory allocation error");
|
||||
}
|
||||
}
|
||||
const char* heightfactor_s = getenv("SVKBD_HEIGHTFACTOR");
|
||||
if (heightfactor_s != NULL)
|
||||
heightfactor = atoi(heightfactor_s);
|
||||
|
||||
if ((tmp = getenv("SVKBD_HEIGHTFACTOR")))
|
||||
heightfactor = atoi(tmp);
|
||||
|
||||
/* parse command line arguments */
|
||||
for (i = 1; argv[i]; i++) {
|
||||
if (!strcmp(argv[i], "-v")) {
|
||||
die("svkbd-"VERSION", © 2006-2020 svkbd engineers,"
|
||||
" see LICENSE for details\n");
|
||||
die("svkbd-"VERSION);
|
||||
} else if (!strcmp(argv[i], "-d")) {
|
||||
isdock = True;
|
||||
continue;
|
||||
|
@ -941,9 +967,8 @@ main(int argc, char *argv[]) {
|
|||
if (i >= argc - 1)
|
||||
continue;
|
||||
free(layer_names_list);
|
||||
if (!(layer_names_list = strdup(argv[++i]))) {
|
||||
die("memory allocation error\n");
|
||||
}
|
||||
if (!(layer_names_list = strdup(argv[++i])))
|
||||
die("memory allocation error");
|
||||
} else if (!strcmp(argv[i], "-s")) {
|
||||
if (i >= argc - 1)
|
||||
continue;
|
||||
|
@ -958,18 +983,20 @@ main(int argc, char *argv[]) {
|
|||
}
|
||||
}
|
||||
|
||||
if (heightfactor <= 0) die("height factor must be a positive integer\n");
|
||||
if (heightfactor <= 0)
|
||||
die("height factor must be a positive integer");
|
||||
|
||||
init_layers(layer_names_list, initial_layer_name);
|
||||
|
||||
if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
|
||||
fprintf(stderr, "warning: no locale support\n");
|
||||
fprintf(stderr, "warning: no locale support");
|
||||
if (!(dpy = XOpenDisplay(0)))
|
||||
die("svkbd: cannot open display\n");
|
||||
die("cannot open display");
|
||||
setup();
|
||||
run();
|
||||
cleanup();
|
||||
XCloseDisplay(dpy);
|
||||
free(layer_names_list);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue