svkbd_dvorak/svkbd.c

618 lines
13 KiB
C
Raw Normal View History

2008-07-15 18:53:38 +02:00
/* See LICENSE file for copyright and license details.
*
* To understand svkbd, start reading main().
2008-07-15 18:53:38 +02:00
*/
#include <locale.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
2008-07-15 22:12:55 +02:00
#include <stdlib.h>
2008-07-15 18:53:38 +02:00
#include <X11/keysym.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xproto.h>
2008-07-15 22:12:55 +02:00
#include <X11/extensions/XTest.h>
2008-07-15 18:53:38 +02:00
/* macros */
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define LENGTH(x) (sizeof x / sizeof x[0])
/* enums */
enum { ColFG, ColBG, ColLast };
enum { NetWMWindowType, NetLast };
2008-07-15 18:53:38 +02:00
/* typedefs */
typedef unsigned int uint;
typedef unsigned long ulong;
typedef struct {
ulong norm[ColLast];
2008-07-15 22:12:55 +02:00
ulong press[ColLast];
2011-10-09 17:22:50 +02:00
ulong high[ColLast];
2008-07-15 18:53:38 +02:00
Drawable drawable;
GC gc;
struct {
int ascent;
int descent;
int height;
XFontSet set;
XFontStruct *xfont;
} font;
} DC; /* draw context */
typedef struct {
2008-07-15 22:12:55 +02:00
char *label;
2008-07-15 18:53:38 +02:00
KeySym keysym;
2008-07-15 22:12:55 +02:00
uint width;
2008-07-15 18:53:38 +02:00
int x, y, w, h;
2008-07-15 22:12:55 +02:00
Bool pressed;
2011-10-09 17:22:50 +02:00
Bool highlighted;
2008-07-15 18:53:38 +02:00
} Key;
typedef struct {
KeySym mod;
uint button;
} Buttonmod;
2008-07-15 18:53:38 +02:00
/* function declarations */
2011-10-09 17:22:50 +02:00
static void motionnotify(XEvent *e);
2008-07-15 18:53:38 +02:00
static void buttonpress(XEvent *e);
2008-07-15 22:12:55 +02:00
static void buttonrelease(XEvent *e);
2008-07-15 18:53:38 +02:00
static void cleanup(void);
static void configurenotify(XEvent *e);
2011-03-25 15:14:12 +01:00
static void countrows();
static void unmapnotify(XEvent *e);
2008-07-15 18:53:38 +02:00
static void die(const char *errstr, ...);
static void drawkeyboard(void);
2008-07-15 22:12:55 +02:00
static void drawkey(Key *k);
2008-07-15 18:53:38 +02:00
static void expose(XEvent *e);
2008-07-15 22:12:55 +02:00
static Key *findkey(int x, int y);
2008-07-15 18:53:38 +02:00
static ulong getcolor(const char *colstr);
static void initfont(const char *fontstr);
static void leavenotify(XEvent *e);
static void press(Key *k, KeySym mod);
2008-07-15 18:53:38 +02:00
static void run(void);
static void setup(void);
static int textnw(const char *text, uint len);
2011-10-09 17:22:50 +02:00
static void unpress(Key *k, KeySym mod);
2008-07-15 22:12:55 +02:00
static void updatekeys();
2008-07-15 18:53:38 +02:00
/* variables */
static int screen;
static void (*handler[LASTEvent]) (XEvent *) = {
[ButtonPress] = buttonpress,
2008-07-15 22:12:55 +02:00
[ButtonRelease] = buttonrelease,
2008-07-15 18:53:38 +02:00
[ConfigureNotify] = configurenotify,
[UnmapNotify] = unmapnotify,
2008-07-15 18:53:38 +02:00
[Expose] = expose,
[LeaveNotify] = leavenotify,
2011-10-09 17:22:50 +02:00
[MotionNotify] = motionnotify
2008-07-15 18:53:38 +02:00
};
2011-03-25 15:14:12 +01:00
static Atom netatom[NetLast];
2008-07-15 18:53:38 +02:00
static Display *dpy;
static DC dc;
static Window root, win;
2011-10-09 17:45:12 +02:00
static Bool running = True, isdock = False;
static KeySym pressedmod = 0;
2011-04-04 13:47:27 +02:00
static int rows = 0, ww = 0, wh = 0, wx = 0, wy = 0;
2011-03-25 15:14:12 +01:00
static char *name = "svkbd";
2011-10-09 17:22:50 +02:00
Bool ispressing = False;
2008-07-15 18:53:38 +02:00
/* configuration, allows nested code to access above variables */
#include "config.h"
2011-03-25 15:14:12 +01:00
#include "layout.h"
2008-07-15 18:53:38 +02:00
2011-10-09 17:22:50 +02:00
void
motionnotify(XEvent *e)
{
XPointerMovedEvent *ev = &e->xmotion;
int i;
for(i = 0; i < LENGTH(keys); i++) {
if(keys[i].keysym && ev->x > keys[i].x
&& ev->x < keys[i].x + keys[i].w
&& ev->y > keys[i].y
&& ev->y < keys[i].y + keys[i].h) {
if(keys[i].highlighted != True) {
if(ispressing) {
keys[i].pressed = True;
} else {
keys[i].highlighted = True;
}
drawkey(&keys[i]);
}
continue;
}
if(!IsModifierKey(keys[i].keysym) && keys[i].pressed == True) {
keys[i].pressed = False;
drawkey(&keys[i]);
}
if(keys[i].highlighted == True) {
keys[i].highlighted = False;
drawkey(&keys[i]);
}
}
}
2008-07-15 18:53:38 +02:00
void
buttonpress(XEvent *e) {
int i;
2008-07-15 22:12:55 +02:00
XButtonPressedEvent *ev = &e->xbutton;
Key *k;
KeySym mod = 0;
2008-07-15 22:12:55 +02:00
2011-10-09 17:22:50 +02:00
ispressing = True;
for(i = 0; i < LENGTH(buttonmods); i++) {
if(ev->button == buttonmods[i].button) {
mod = buttonmods[i].mod;
break;
2008-07-15 22:12:55 +02:00
}
2011-10-09 17:22:50 +02:00
}
if((k = findkey(ev->x, ev->y)))
press(k, mod);
2008-07-15 22:12:55 +02:00
}
void
buttonrelease(XEvent *e) {
2011-10-09 17:22:50 +02:00
int i;
2008-07-15 22:12:55 +02:00
XButtonPressedEvent *ev = &e->xbutton;
Key *k;
2011-10-09 17:22:50 +02:00
KeySym mod = 0;
ispressing = False;
2008-07-15 22:12:55 +02:00
2011-10-09 17:22:50 +02:00
for(i = 0; i < LENGTH(buttonmods); i++) {
if(ev->button == buttonmods[i].button) {
mod = buttonmods[i].mod;
break;
}
}
if((k = findkey(ev->x, ev->y)))
2011-10-09 17:22:50 +02:00
unpress(k, mod);
2008-07-15 18:53:38 +02:00
}
void
cleanup(void) {
if(dc.font.set)
XFreeFontSet(dpy, dc.font.set);
else
XFreeFont(dpy, dc.font.xfont);
XFreePixmap(dpy, dc.drawable);
XFreeGC(dpy, dc.gc);
XDestroyWindow(dpy, win);
XSync(dpy, False);
XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
}
void
configurenotify(XEvent *e) {
XConfigureEvent *ev = &e->xconfigure;
if(ev->window == win && (ev->width != ww || ev->height != wh)) {
ww = ev->width;
wh = ev->height;
XFreePixmap(dpy, dc.drawable);
2011-10-09 17:22:50 +02:00
dc.drawable = XCreatePixmap(dpy, root, ww, wh,
DefaultDepth(dpy, screen));
2008-07-15 22:12:55 +02:00
updatekeys();
2008-07-15 18:53:38 +02:00
}
}
2011-03-25 15:14:12 +01:00
void
countrows() {
int i = 0;
2011-10-09 17:22:50 +02:00
for(i = 0, rows = 1; i < LENGTH(keys); i++) {
2011-03-25 15:14:12 +01:00
if(keys[i].keysym == 0)
rows++;
2011-10-09 17:22:50 +02:00
}
2011-03-25 15:14:12 +01:00
}
2008-07-15 18:53:38 +02:00
void
die(const char *errstr, ...) {
va_list ap;
va_start(ap, errstr);
vfprintf(stderr, errstr, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
void
drawkeyboard(void) {
2008-07-15 22:12:55 +02:00
int i;
2008-07-15 18:53:38 +02:00
for(i = 0; i < LENGTH(keys); i++) {
if(keys[i].keysym != 0)
2008-07-15 22:12:55 +02:00
drawkey(&keys[i]);
2008-07-15 18:53:38 +02:00
}
XSync(dpy, False);
}
void
2008-07-15 22:12:55 +02:00
drawkey(Key *k) {
2008-07-15 18:53:38 +02:00
int x, y, h, len;
XRectangle r = { k->x, k->y, k->w, k->h};
2008-07-15 22:12:55 +02:00
const char *l;
ulong *col;
2008-07-15 18:53:38 +02:00
2008-07-15 22:12:55 +02:00
if(k->pressed)
col = dc.press;
2011-10-09 17:22:50 +02:00
else if(k->highlighted)
col = dc.high;
2008-07-15 22:12:55 +02:00
else
col = dc.norm;
2011-10-09 17:22:50 +02:00
2008-07-15 18:53:38 +02:00
XSetForeground(dpy, dc.gc, col[ColBG]);
XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
XSetForeground(dpy, dc.gc, dc.norm[ColFG]);
r.height -= 1;
r.width -= 1;
2008-07-15 18:53:38 +02:00
XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
XSetForeground(dpy, dc.gc, col[ColFG]);
2011-10-09 17:22:50 +02:00
if(k->label) {
2008-07-15 22:12:55 +02:00
l = k->label;
2011-10-09 17:22:50 +02:00
} else {
2008-07-15 22:12:55 +02:00
l = XKeysymToString(k->keysym);
2011-10-09 17:22:50 +02:00
}
2008-07-15 22:12:55 +02:00
len = strlen(l);
2008-07-15 18:53:38 +02:00
h = dc.font.ascent + dc.font.descent;
y = k->y + (k->h / 2) - (h / 2) + dc.font.ascent;
2008-07-15 22:12:55 +02:00
x = k->x + (k->w / 2) - (textnw(l, len) / 2);
2011-10-09 17:22:50 +02:00
if(dc.font.set) {
XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, l,
len);
} else {
2008-07-15 22:12:55 +02:00
XDrawString(dpy, dc.drawable, dc.gc, x, y, l, len);
2011-10-09 17:22:50 +02:00
}
XCopyArea(dpy, dc.drawable, win, dc.gc, k->x, k->y, k->w, k->h,
k->x, k->y);
2008-07-15 18:53:38 +02:00
}
void
unmapnotify(XEvent *e) {
2008-07-15 22:12:55 +02:00
running = False;
2008-07-15 18:53:38 +02:00
}
void
expose(XEvent *e) {
XExposeEvent *ev = &e->xexpose;
if(ev->count == 0 && (ev->window == win))
drawkeyboard();
}
2008-07-15 22:12:55 +02:00
Key *
findkey(int x, int y) {
int i;
2011-10-09 17:22:50 +02:00
for(i = 0; i < LENGTH(keys); i++) {
2008-07-15 22:12:55 +02:00
if(keys[i].keysym && x > keys[i].x &&
x < keys[i].x + keys[i].w &&
2011-10-09 17:22:50 +02:00
y > keys[i].y && y < keys[i].y + keys[i].h) {
2008-07-15 22:12:55 +02:00
return &keys[i];
2011-10-09 17:22:50 +02:00
}
}
2008-07-15 22:12:55 +02:00
return NULL;
}
2008-07-15 18:53:38 +02:00
ulong
getcolor(const char *colstr) {
Colormap cmap = DefaultColormap(dpy, screen);
XColor color;
if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
die("error, cannot allocate color '%s'\n", colstr);
return color.pixel;
}
void
initfont(const char *fontstr) {
char *def, **missing;
int i, n;
missing = NULL;
if(dc.font.set)
XFreeFontSet(dpy, dc.font.set);
dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
if(missing) {
while(n--)
fprintf(stderr, "svkbd: missing fontset: %s\n", missing[n]);
2008-07-15 18:53:38 +02:00
XFreeStringList(missing);
}
if(dc.font.set) {
XFontStruct **xfonts;
char **font_names;
dc.font.ascent = dc.font.descent = 0;
n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
xfonts++;
}
2011-10-09 17:22:50 +02:00
} else {
2008-07-15 18:53:38 +02:00
if(dc.font.xfont)
XFreeFont(dpy, dc.font.xfont);
dc.font.xfont = NULL;
if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
&& !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
die("error, cannot load font: '%s'\n", fontstr);
dc.font.ascent = dc.font.xfont->ascent;
dc.font.descent = dc.font.xfont->descent;
}
dc.font.height = dc.font.ascent + dc.font.descent;
}
void
leavenotify(XEvent *e) {
2011-10-09 17:22:50 +02:00
unpress(NULL, 0);
2008-07-15 18:53:38 +02:00
}
void
press(Key *k, KeySym mod) {
int i;
k->pressed = !k->pressed;
if(!IsModifierKey(k->keysym)) {
2011-10-09 17:22:50 +02:00
for(i = 0; i < LENGTH(keys); i++) {
if(keys[i].pressed && IsModifierKey(keys[i].keysym)) {
XTestFakeKeyEvent(dpy,
XKeysymToKeycode(dpy, keys[i].keysym),
True, 0);
}
}
pressedmod = mod;
2011-10-09 17:22:50 +02:00
if(pressedmod) {
XTestFakeKeyEvent(dpy, XKeysymToKeycode(dpy, mod),
True, 0);
}
XTestFakeKeyEvent(dpy, XKeysymToKeycode(dpy, k->keysym), True, 0);
}
drawkey(k);
}
2011-10-09 17:22:50 +02:00
void
unpress(Key *k, KeySym mod) {
int i;
if(k != NULL) {
switch(k->keysym) {
case XK_Cancel:
exit(0);
default:
break;
}
}
for(i = 0; i < LENGTH(keys); i++) {
if(keys[i].pressed && !IsModifierKey(keys[i].keysym)) {
XTestFakeKeyEvent(dpy,
XKeysymToKeycode(dpy, keys[i].keysym),
False, 0);
keys[i].pressed = 0;
drawkey(&keys[i]);
break;
}
}
if(i != LENGTH(keys)) {
for(i = 0; i < LENGTH(keys); i++) {
if(pressedmod) {
XTestFakeKeyEvent(dpy,
XKeysymToKeycode(dpy, pressedmod),
False, 0);
}
pressedmod = 0;
if(keys[i].pressed) {
XTestFakeKeyEvent(dpy,
XKeysymToKeycode(dpy,
keys[i].keysym), False, 0);
keys[i].pressed = 0;
drawkey(&keys[i]);
}
}
}
}
2008-07-15 18:53:38 +02:00
void
run(void) {
XEvent ev;
2010-08-10 23:15:12 +02:00
/* main event loop */
2008-07-15 18:53:38 +02:00
XSync(dpy, False);
while(running) {
XNextEvent(dpy, &ev);
if(handler[ev.type])
(handler[ev.type])(&ev); /* call handler */
}
}
void
setup(void) {
XSetWindowAttributes wa;
2011-03-25 15:14:12 +01:00
XTextProperty str;
2011-10-09 17:45:12 +02:00
XSizeHints *sizeh = NULL;
2011-03-25 15:14:12 +01:00
XClassHint *ch;
2011-10-09 17:45:12 +02:00
Atom atype = -1;
2011-03-25 15:14:12 +01:00
int i, sh, sw;
XWMHints *wmh;
2008-07-15 22:12:55 +02:00
2008-07-15 18:53:38 +02:00
/* init screen */
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
sw = DisplayWidth(dpy, screen);
sh = DisplayHeight(dpy, screen);
2008-07-15 18:53:38 +02:00
initfont(font);
2011-03-25 15:14:12 +01:00
/* init atoms */
2011-10-09 17:59:03 +02:00
if(isdock) {
netatom[NetWMWindowType] = XInternAtom(dpy,
"_NET_WM_WINDOW_TYPE", False);
atype = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DOCK", False);
2011-10-09 17:59:03 +02:00
}
2011-03-25 15:14:12 +01:00
/* init appearance */
countrows();
if(!ww)
2011-10-09 17:59:03 +02:00
ww = sw;
2011-03-25 15:14:12 +01:00
if(!wh)
wh = sh * rows / 32;
2011-10-09 17:59:03 +02:00
if(!wx)
wx = 0;
if(wx < 0)
wx = sw + wx - ww;
2011-03-25 15:14:12 +01:00
if(!wy)
wy = sh - wh;
if(wy < 0)
wy = sh + wy - wh;
2008-07-15 18:53:38 +02:00
dc.norm[ColBG] = getcolor(normbgcolor);
dc.norm[ColFG] = getcolor(normfgcolor);
2008-07-15 22:12:55 +02:00
dc.press[ColBG] = getcolor(pressbgcolor);
dc.press[ColFG] = getcolor(pressfgcolor);
2011-10-09 17:22:50 +02:00
dc.high[ColBG] = getcolor(highlightbgcolor);
dc.high[ColFG] = getcolor(highlightfgcolor);
dc.drawable = XCreatePixmap(dpy, root, ww, wh,
DefaultDepth(dpy, screen));
2008-07-15 18:53:38 +02:00
dc.gc = XCreateGC(dpy, root, 0, 0);
if(!dc.font.set)
XSetFont(dpy, dc.gc, dc.font.xfont->fid);
2008-07-15 22:12:55 +02:00
for(i = 0; i < LENGTH(keys); i++)
keys[i].pressed = 0;
2008-07-15 18:53:38 +02:00
2011-03-25 15:14:12 +01:00
wa.override_redirect = !wmborder;
wa.border_pixel = dc.norm[ColFG];
wa.background_pixel = dc.norm[ColBG];
win = XCreateWindow(dpy, root, wx, wy, ww, wh, 0,
CopyFromParent, CopyFromParent, CopyFromParent,
2011-10-09 17:22:50 +02:00
CWOverrideRedirect | CWBorderPixel |
CWBackingPixel, &wa);
2010-05-16 12:17:09 +02:00
XSelectInput(dpy, win, StructureNotifyMask|ButtonReleaseMask|
2011-10-09 17:22:50 +02:00
ButtonPressMask|ExposureMask|LeaveWindowMask|
PointerMotionMask);
2011-03-25 15:14:12 +01:00
2008-07-15 22:12:55 +02:00
wmh = XAllocWMHints();
wmh->input = False;
wmh->flags = InputHint;
2011-10-09 17:45:12 +02:00
if(!isdock) {
sizeh = XAllocSizeHints();
sizeh->flags = PMaxSize | PMinSize;
sizeh->min_width = sizeh->max_width = ww;
sizeh->min_height = sizeh->max_height = wh;
}
2011-03-25 15:14:12 +01:00
XStringListToTextProperty(&name, 1, &str);
ch = XAllocClassHint();
ch->res_class = name;
ch->res_name = name;
2011-10-09 17:45:12 +02:00
XSetWMProperties(dpy, win, &str, &str, NULL, 0, sizeh, wmh,
2011-03-25 15:14:12 +01:00
ch);
XFree(ch);
2008-07-15 22:12:55 +02:00
XFree(wmh);
2011-03-25 15:14:12 +01:00
XFree(str.value);
2011-10-09 17:45:12 +02:00
if(sizeh != NULL)
XFree(sizeh);
if(isdock) {
XChangeProperty(dpy, win, netatom[NetWMWindowType], XA_ATOM,
32, PropModeReplace,
(unsigned char *)&atype, 1);
}
2011-03-25 15:14:12 +01:00
2008-07-15 18:53:38 +02:00
XMapRaised(dpy, win);
2008-07-15 22:12:55 +02:00
updatekeys();
2008-07-15 18:53:38 +02:00
drawkeyboard();
}
int
textnw(const char *text, uint len) {
XRectangle r;
if(dc.font.set) {
XmbTextExtents(dc.font.set, text, len, NULL, &r);
return r.width;
}
return XTextWidth(dc.font.xfont, text, len);
}
2008-07-15 22:12:55 +02:00
void
updatekeys() {
2011-03-25 15:14:12 +01:00
int i, j;
2011-04-13 09:46:51 +02:00
int x = 0, y = 0, h, base, r = rows;
2008-07-15 22:12:55 +02:00
h = (wh - 1) / rows;
2011-04-13 09:46:51 +02:00
for(i = 0; i < LENGTH(keys); i++, r--) {
2008-07-15 22:12:55 +02:00
for(j = i, base = 0; j < LENGTH(keys) && keys[j].keysym != 0; j++)
base += keys[j].width;
2011-04-13 09:46:51 +02:00
for(x = 0; i < LENGTH(keys) && keys[i].keysym != 0; i++) {
2008-07-15 22:12:55 +02:00
keys[i].x = x;
keys[i].y = y;
keys[i].w = keys[i].width * (ww - 1) / base;
2011-04-13 19:09:53 +02:00
keys[i].h = r == 1 ? wh - y - 1 : h;
2008-07-15 22:12:55 +02:00
x += keys[i].w;
}
if(base != 0)
keys[i - 1].w = ww - 1 - keys[i - 1].x;
2008-07-15 22:12:55 +02:00
y += h;
}
}
2011-03-24 19:41:58 +01:00
void
usage(char *argv0) {
2011-10-09 17:45:12 +02:00
fprintf(stderr, "usage: %s [-hdv] [-g geometry]\n", argv0);
2011-03-24 19:41:58 +01:00
exit(1);
}
2008-07-15 18:53:38 +02:00
int
main(int argc, char *argv[]) {
int i, xr, yr, bitm;
unsigned int wr, hr;
2011-03-24 19:41:58 +01:00
for (i = 1; argv[i]; i++) {
if(!strcmp(argv[i], "-v")) {
die("svkbd-"VERSION", © 2006-2010 svkbd engineers,"
" see LICENSE for details\n");
2011-10-09 17:45:12 +02:00
} else if(!strcmp(argv[i], "-d")) {
isdock = True;
continue;
2011-10-09 18:00:21 +02:00
} else if(!strncmp(argv[i], "-g", 2)) {
2011-10-19 10:49:23 +02:00
if(i >= argc - 1)
continue;
bitm = XParseGeometry(argv[i+1], &xr, &yr, &wr, &hr);
if(bitm & XValue)
wx = xr;
if(bitm & YValue)
wy = yr;
if(bitm & WidthValue)
ww = (int)wr;
if(bitm & HeightValue)
wh = (int)hr;
2011-10-09 17:59:03 +02:00
if(bitm & XNegative && wx == 0)
wx = -1;
if(bitm & YNegative && wy == 0)
wy = -1;
2011-10-09 18:03:23 +02:00
i++;
} else if(!strcmp(argv[i], "-h")) {
2011-03-24 19:41:58 +01:00
usage(argv[0]);
}
2011-03-24 19:41:58 +01:00
}
2008-07-15 18:53:38 +02:00
if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
fprintf(stderr, "warning: no locale support\n");
if(!(dpy = XOpenDisplay(0)))
die("svkbd: cannot open display\n");
setup();
run();
cleanup();
XCloseDisplay(dpy);
return 0;
}
2011-11-10 19:15:05 +01:00