#include "app.h"
int keyInit(Key *k, Layout *l) {
k->l = l;
k->down = 0;
k->downKeycode = 0;
k->mx = k->my = 0;
k->isLetter = k->isKeypad = 0;
k->x = k->y = k->w = k->h = 0;
inputPrepareKeysyms(&k->keySym, &k->keySym2, &k->isLetter, &k->isKeypad);
if (!k->label[0]) keysymString(k->keySym, k->label);
if (!k->label2[0]) keysymString(k->keySym2, k->label2);
textLayoutInit(&k->tl, &k->l->kbd->app->graph, k->label);
textLayoutInit(&k->tl2, &k->l->kbd->app->graph, k->label2);
return 1;
}
void keyDeinit(Key *k) {
keyUp(k, 1);
textLayoutDeinit(&k->tl);
textLayoutDeinit(&k->tl2);
}
void keyDraw(Key *k, int cx, int cy, int cw, int ch) {
if ( k->x + k->w <= cx || k->x >= cx + cw
|| k->y + k->h <= cy || k->y >= cy + ch ) return;
App *app = k->l->kbd->app;
int active = k->down;
if (k->flags & KF_MOD)
active = (app->input.modifiers & (unsigned int)k->optValue);
graphDrawButton(&k->l->kbd->app->graph, k->x, k->y, k->w, k->h, active);
TextLayout *tl = &k->tl;
TextLayout *tl2 = &k->tl2;
if (inputChooseKeysym(app->input.modifiers, k->keySym, k->keySym2, k->isLetter, k->isKeypad))
tl = tl2, tl2 = &k->tl; // swap labels
int lx, lx2, ly, ly2;
lx = lx2 = k->x + k->w/2;
ly = ly2 = k->y + k->h/2;
if (!k->isLetter && k->label2[0]) {
int dx = k->w/6;
int dy = k->h/6;
lx -= dx;
ly += dy;
lx2 += dx;
ly2 -= dy;
}
textLayoutDraw(tl, lx, ly, active);
if (!k->isLetter)
textLayoutDraw(tl2, lx2, ly2, active);
}
void keyDown(Key *k, int x, int y) {
if (k->down == 2) k->down = 1;
if (k->down) return;
Keyboard *kbd = k->l->kbd;
k->down = 1;
if (k->flags & KF_MOVE) {
k->mx = x;
k->my = y;
} else
if (k->flags & KF_SIZE) {
k->mx = kbd->app->w - x;
k->my = kbd->app->h - y;
}
if (k->flags & KF_HOLD) {
k->down = 2;
// insert to held list
if (!k->prevHeld && !k->nextHeld && kbd->firstHeld != k) {
k->nextHeld = kbd->firstHeld;
if (k->nextHeld) k->nextHeld->prevHeld = k;
kbd->firstHeld = k;
}
}
// choose keySym
unsigned int keySym = inputChooseKeysym(kbd->app->input.modifiers, k->keySym, k->keySym2, k->isLetter, k->isKeypad)
? k->keySym2 : k->keySym;
// press
if (keySym) {
k->downKeycode = inputKeycode(&kbd->app->input, keySym);
if (k->downKeycode) inputEvent(&kbd->app->input, k->downKeycode, 1);
}
// redraw
appInvalidateRect(kbd->app, k->x, k->y, k->w, k->h);
}
void keyMotion(Key *k, int x, int y) {
App *app = k->l->kbd->app;
if (k->flags & KF_MOVE) {
appMove(app, app->x + x - k->mx, app->y + y - k->my, app->w, app->h);
} else
if (k->flags & KF_SIZE) {
appMove(app, app->x, app->y, x + k->mx, y + k->my);
}
}
void keyUp(Key *k, int force) {
if (!force && k->down != 1) return;
Keyboard *kbd = k->l->kbd;
int wasDown = k->down;
k->down = 0;
// release key
if (k->downKeycode) {
inputEvent(&kbd->app->input, k->downKeycode, 0);
k->downKeycode = 0;
}
// remove from held list
if (k->prevHeld || k->nextHeld || kbd->firstHeld == k) {
if (k->nextHeld) k->nextHeld->prevHeld = k->prevHeld;
if (k->prevHeld) k->prevHeld->nextHeld = k; else kbd->firstHeld = k->nextHeld;
k->nextHeld = k->prevHeld = NULL;
}
// following action should be done only for real button up
if (!wasDown)
return;
if (k->flags & KF_CLOSE)
appStop(kbd->app, 0);
if (k->flags & KF_LAYOUT)
keyboardSwitchLayout(kbd, k->optValue);
// on releasing a regular key release all held special keys
if (!force && !k->flags)
keyboardRelaseHeld(kbd);
// redraw
appInvalidateRect(kbd->app, k->x, k->y, k->w, k->h);
}
int layoutInit(Layout *l, Keyboard *kbd) {
LOGDBG("layout: init");
l->kbd = kbd;
l->prev = NULL;
l->downKey = NULL;
l->w = l->h = 0;
if (!l->keysCount)
return 1;
// fix keys coords
Key *k = &l->keys[0];
int x0 = k->ox;
int y0 = k->oy;
int x1 = x0 + k->ow;
int y1 = y0 + k->oh;
for(int i = 1; i < l->keysCount; ++i) {
k = &l->keys[i];
Key *p = k - 1;
// coord < 0 means relative offset from previos key -1
// zero coord of size means copy value from previous key
if (!k->ox) k->ox = p->ox; else
if (k->ox < 0) k->ox = p->ox + p->ow - k->ox - 1;
if (!k->oy) k->oy = p->oy; else
if (k->oy < 0) k->oy = p->oy + p->oh - k->oy - 1;
if (!k->ow) k->ow = p->ow;
if (!k->oh) k->oh = p->oh;
// calc bounds
if (x0 > k->ox) x0 = k->ox;
if (y0 > k->oy) y0 = k->oy;
if (x1 < k->ox + k->ow) x1 = k->ox + k->ow;
if (y1 < k->oy + k->oh) y1 = k->oy + k->oh;
}
// fix layout size
if (!l->ow) l->ow = x1 + x0;
if (!l->oh) l->oh = y1 + y0;
LOGDBG("layout: init: size x0=%d y0=%d x1=%d y1=%d w=%d h=%d", x0, y0, x1, y1, l->ow, l->oh);
// init keys
for(int i = 0; i < l->keysCount; ++i) {
if (!keyInit(&l->keys[i], l)) {
for(--i; i >= 0; --i) keyDeinit(&l->keys[i]);
return 0;
}
}
return 1;
}
void layoutDeinit(Layout *l) {
LOGDBG("layout: deinit");
layoutMouseUp(l);
for(int i = 0; i < l->keysCount; ++i)
keyDeinit(&l->keys[i]);
}
void layoutResize(Layout *l) {
if (l->w == l->kbd->app->w && l->h == l->kbd->app->h) return;
LOGDBG("layout: resize w=%d h=%d", l->kbd->app->w, l->kbd->app->h);
l->w = l->kbd->app->w;
l->h = l->kbd->app->h;
float kx = l->w/(float)l->ow;
float ky = l->h/(float)l->oh;
for(int i = 0; i < l->keysCount; ++i) {
Key *k = &l->keys[i];
k->x = (int)(k->ox*kx + 0.5f);
k->y = (int)(k->oy*ky + 0.5f);
k->w = (int)(k->ow*kx + 0.5f);
k->h = (int)(k->oh*ky + 0.5f);
}
}
void layoutDraw(Layout *l, int cx, int cy, int cw, int ch) {
for(int i = 0; i < l->keysCount; ++i)
keyDraw(&l->keys[i], cx, cy, cw, ch);
}
void layoutMouseDown(Layout *l, int x, int y) {
LOGDBG("layout: mouse down: x=%d y=%d", x, y);
for(int i = l->keysCount - 1; i >= 0; --i) {
Key *k = &l->keys[i];
if ( k->x <= x && x < k->x + k->w
&& k->y <= y && y < k->y + k->h )
{
if (l->downKey != k) {
layoutMouseUp(l);
l->downKey = k;
keyDown(k, x, y);
}
break;
}
}
}
void layoutMouseMotion(Layout *l, int x, int y) {
LOGDBG("layout: mouse motion: x=%d y=%d", x, y);
if (!l->downKey) return;
keyMotion(l->downKey, x, y);
}
void layoutMouseUp(Layout *l) {
LOGDBG("layout: mouse up");
if (!l->downKey) return;
keyUp(l->downKey, 0);
l->downKey = NULL;
}
int keyboardInit(Keyboard *kbd, App *app) {
LOGDBG("keyboard: init");
kbd->app = app;
kbd->firstHeld = NULL;
kbd->lastModifiers = kbd->app->input.modifiers;
kbd->current = kbd->layoutsCount > 0 ? kbd->layouts : NULL;
for(int i = 0; i < kbd->layoutsCount; ++i) {
if (!layoutInit(&kbd->layouts[i], kbd)) {
for(--i; i >= 0; --i) layoutDeinit(&kbd->layouts[i]);
return 0;
}
}
// fix size
for(int i = 0; i < kbd->layoutsCount; ++i) {
if (kbd->ow < kbd->layouts[i].ow) kbd->ow = kbd->layouts[i].ow;
if (kbd->oh < kbd->layouts[i].oh) kbd->oh = kbd->layouts[i].oh;
}
LOGDBG("keyboard: init: size ow=%d oh=%d", kbd->ow, kbd->oh);
//appMove(app, app->x, app->y, kbd->ow, kbd->oh);
return 1;
}
void keyboardDeinit(Keyboard *kbd) {
LOGDBG("keyboard: deinit");
keyboardMouseUp(kbd);
for(int i = 0; i < kbd->layoutsCount; ++i)
layoutDeinit(&kbd->layouts[i]);
}
void keyboardResize(Keyboard *kbd) {
if (kbd->current)
layoutResize(kbd->current);
}
void keyboardUpdateModifiers(Keyboard* kbd) {
if (kbd->lastModifiers == kbd->app->input.modifiers) return;
LOGDBG("keyboard: update modifiers");
kbd->lastModifiers = kbd->app->input.modifiers;
appInvalidateRect(kbd->app, 0, 0, kbd->app->w, kbd->app->h);
}
void keyboardDraw(Keyboard *kbd, int cx, int cy, int cw, int ch) {
if (!kbd->current) return;
layoutDraw(kbd->current, cx, cy, cw, ch);
}
void keyboardMouseDown(Keyboard *kbd, int x, int y) {
LOGDBG("keyboard: mouse down: x=%d y=%d", x, y);
if (!kbd->current) return;
layoutMouseDown(kbd->current, x, y);
}
void keyboardMouseMotion(Keyboard *kbd, int x, int y) {
LOGDBG("keyboard: mouse motion: x=%d y=%d", x, y);
if (!kbd->current) return;
layoutMouseMotion(kbd->current, x, y);
}
void keyboardMouseUp(Keyboard *kbd) {
LOGDBG("keyboard: mouse up");
if (!kbd->current) return;
layoutMouseUp(kbd->current);
}
void keyboardRelaseHeld(Keyboard *kbd) {
LOGDBG("keyboard: release held");
while(kbd->firstHeld) keyUp(kbd->firstHeld, 1);
}
void keyboardSwitchLayout(Keyboard *kbd, int index) {
LOGDBG("keyboard: switch layout");
if (!kbd->layoutsCount) return;
if (index == LI_PREV) {
index = kbd->current ? kbd->current - kbd->layouts - 1: 0;
if (index < 0) index = kbd->layoutsCount - 1;
} else
if (index == LI_NEXT) {
index = kbd->current ? kbd->current - kbd->layouts + 1: 0;
if (index >= kbd->layoutsCount) index = 0;
} else
if (index == LI_REVERT) {
if (kbd->current && kbd->current->prev)
kbd->current = kbd->current->prev;
keyboardResize(kbd);
appInvalidateRect(kbd->app, 0, 0, kbd->app->w, kbd->app->h);
return;
}
if (index < 0 || index >= kbd->layoutsCount) {
LOGWRN("keyboard: switch layout: bad index %d", index);
return;
}
kbd->layouts[index].prev = kbd->current;
kbd->current = &kbd->layouts[index];
keyboardResize(kbd);
appInvalidateRect(kbd->app, 0, 0, kbd->app->w, kbd->app->h);
}