|
|
452870 |
#ifndef KEYBOARD_INC_C
|
|
|
452870 |
#define KEYBOARD_INC_C
|
|
|
452870 |
|
|
|
452870 |
|
|
|
452870 |
struct Keyboard;
|
|
|
452870 |
|
|
|
452870 |
|
|
|
452870 |
typedef struct Key {
|
|
|
452870 |
int x, y, w, h;
|
|
|
452870 |
const char *label;
|
|
|
452870 |
|
|
|
452870 |
struct Keyboard *kbd;
|
|
|
452870 |
int lx, ly;
|
|
|
452870 |
|
|
|
452870 |
int pressed;
|
|
|
452870 |
} Key;
|
|
|
452870 |
|
|
|
452870 |
|
|
|
452870 |
typedef struct Keyboard {
|
|
|
452870 |
Key *keys;
|
|
|
452870 |
int keysCount;
|
|
|
452870 |
|
|
|
452870 |
Display* dpy;
|
|
|
452870 |
Window win;
|
|
|
452870 |
GC gc;
|
|
|
452870 |
GContext gcid;
|
|
|
452870 |
XFontStruct *font;
|
|
|
452870 |
|
|
|
452870 |
Key *keyMousePressed;
|
|
|
452870 |
} Keyboard;
|
|
|
452870 |
|
|
|
452870 |
|
|
|
452870 |
|
|
|
452870 |
void keyInit(Key *k, Keyboard *kbd) {
|
|
|
452870 |
k->pressed = 0;
|
|
|
452870 |
k->kbd = kbd;
|
|
|
452870 |
XFontStruct *font = XQueryFont(k->kbd->dpy, k->kbd->gcid);
|
|
|
452870 |
XRectangle r = {};
|
|
|
452870 |
XTextExtents(font, k->label, strlen(k->label), NULL, &r);
|
|
|
452870 |
k->lx = k->x + (k->w - r.width)/2 - r.x;
|
|
|
452870 |
k->ly = k->y + (k->h - r.height)/2 - r.y;
|
|
|
452870 |
}
|
|
|
452870 |
|
|
|
452870 |
void keyDeinit(Key *k) {
|
|
|
452870 |
k->pressed = 0;
|
|
|
452870 |
}
|
|
|
452870 |
|
|
|
452870 |
void keyDraw(Key *k, int cx, int cy, int cw, int ch) {
|
|
|
452870 |
if ( k->x + k->w <= cx || k->x >= cx + cw
|
|
|
452870 |
|| k->y + k->h <= cy || k->y >= cy + ch ) return;
|
|
|
452870 |
if (k->pressed)
|
|
|
452870 |
XDrawRectangle(k->kbd->dpy, k->kbd->win, k->kbd->gc, k->x, k->y, k->w, k->h);
|
|
|
452870 |
XDrawString(k->kbd->dpy, k->kbd->win);
|
|
|
452870 |
}
|
|
|
452870 |
|
|
|
452870 |
void keyMouseDown(Key *k) {
|
|
|
452870 |
k->pressed = 1;
|
|
|
452870 |
}
|
|
|
452870 |
|
|
|
452870 |
void keyMouseUp(Key *k) {
|
|
|
452870 |
k->pressed = 0;
|
|
|
452870 |
}
|
|
|
452870 |
|
|
|
452870 |
|
|
|
452870 |
void keyboardInit(Keyboard *kbd, Display *dpy, Window win) {
|
|
|
452870 |
kbd->keyMousePressed = NULL;
|
|
|
452870 |
kbd->dpy = dpy;
|
|
|
452870 |
kbd->win = win;
|
|
|
452870 |
kbd->gc = DefaultGC(kbd->dpy, kbd->win);
|
|
|
452870 |
kbd->gcid = XGContextFromGC(kbd->gc);
|
|
|
452870 |
kbd->font = XQueryFont(k->kbd->dpy, k->kbd->gcid);
|
|
|
452870 |
for(int i = 0; i < kbd->keysCount; ++i)
|
|
|
452870 |
keyInit(&kbd->keys[i], kbd);
|
|
|
452870 |
}
|
|
|
452870 |
|
|
|
452870 |
void keyboardDeinit(Keyboard *kbd) {
|
|
|
452870 |
for(int i = 0; i < kbd->keysCount; ++i)
|
|
|
452870 |
keyDeinit(kbd->keys[i]);
|
|
|
452870 |
kbd->keyMousePressed = NULL;
|
|
|
452870 |
kbd->font = XFreeFont(kbd->dpy, kbd->font);
|
|
|
452870 |
kbd->gcid = NULL;
|
|
|
452870 |
kbd->gc = NULL;
|
|
|
452870 |
kbd->win = NULL;
|
|
|
452870 |
kbd->dpy = NULL;
|
|
|
452870 |
}
|
|
|
452870 |
|
|
|
452870 |
void keyboardDraw(Keyboard *kbd, int cx, int cy, int cw, int ch) {
|
|
|
452870 |
for(int i = 0; i < kbd->keysCount; ++i)
|
|
|
452870 |
keyDraw(&kbd->keys[i], cx, cy, cw, ch);
|
|
|
452870 |
}
|
|
|
452870 |
|
|
|
452870 |
void keyboardMouseDown(Keyboard *kbd, int x, int y) {
|
|
|
452870 |
for(int i = kbd->keysCount - 1; i >= 0; --i) {
|
|
|
452870 |
Key *k = &kbd->keys[i];
|
|
|
452870 |
if ( k->x <= x && x < k->x + k->w
|
|
|
452870 |
&& k->y <= y && y < k->y + k->h )
|
|
|
452870 |
{
|
|
|
452870 |
if (kbd->keyMousePressed != k) {
|
|
|
452870 |
keyboardMouseUp(kbd);
|
|
|
452870 |
kbd->keyMousePressed = k;
|
|
|
452870 |
keyMouseDown(k);
|
|
|
452870 |
}
|
|
|
452870 |
break;
|
|
|
452870 |
}
|
|
|
452870 |
}
|
|
|
452870 |
}
|
|
|
452870 |
|
|
|
452870 |
void keyboardMouseUp(Keyboard *kbd) {
|
|
|
452870 |
if (kbd->keyMousePressed) {
|
|
|
452870 |
kbd->keyMouseUp(kbd->keyMousePressed);
|
|
|
452870 |
kbd->keyMousePressed = NULL;
|
|
|
452870 |
}
|
|
|
452870 |
}
|
|
|
452870 |
|
|
|
452870 |
|
|
|
452870 |
|
|
|
452870 |
#endif
|