Blame keyboard.c

452870
452870
#include "app.h"
452870
452870
452870
452870
int keyInit(Key *k, Keyboard *kbd) {
452870
  k->kbd = kbd;
452870
  k->down = 0;
452870
  
452870
  int lx = k->x + k->w/2;
452870
  int ly = k->y + k->h/2;
452870
  if (k->label && !textLayoutInit(&k->tl, &k->kbd->app->graph, lx, ly, k->label))
452870
    return 0;
452870
  
452870
  int d = k->h/4;
452870
  lx = k->x + k->w - d;
452870
  ly = k->y + d;
452870
  if (k->label2 && !textLayoutInit(&k->tl2, &k->kbd->app->graph, lx, ly, k->label2)) {
452870
    if (k->label) textLayoutDeinit(&k->tl);
452870
    return 0;
452870
  }
452870
  
452870
  return 1;
452870
}
452870
452870
452870
void keyDeinit(Key *k) {
452870
  if (k->label) textLayoutDeinit(&k->tl);
452870
  if (k->label2) textLayoutDeinit(&k->tl2);
452870
}
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
  graphDrawButton(&k->kbd->app->graph, k->x, k->y, k->w, k->h, k->down);
452870
  textLayoutDraw(&k->tl, k->down);
452870
}
452870
452870
452870
void keyMouseDown(Key *k) {
452870
  k->down = 1;
452870
  inputEvent(&k->kbd->app->input, k->keySym, 1);
452870
  appInvalidateRect(k->kbd->app, k->x, k->y, k->w, k->h);
452870
}
452870
452870
452870
void keyMouseUp(Key *k) {
452870
  k->down = 0;
452870
  inputEvent(&k->kbd->app->input, k->keySym, 0);
452870
  appInvalidateRect(k->kbd->app, k->x, k->y, k->w, k->h);
452870
}
452870
452870
452870
452870
int keyboardInit(Keyboard *kbd, App *app) {
452870
  LOGDBG("keyboard: init");
452870
  kbd->app = app;
452870
  kbd->downKey = NULL;
452870
  for(int i = 0; i < kbd->keysCount; ++i)
452870
    if (!keyInit(&kbd->keys[i], kbd)) {
452870
      for(--i; i >= 0; --i) keyDeinit(&kbd->keys[i]);
452870
      return 0;
452870
    }
452870
  return 1;
452870
}
452870
452870
452870
void keyboardDeinit(Keyboard *kbd) {
452870
  LOGDBG("keyboard: deinit");
452870
  keyboardMouseUp(kbd);
452870
  for(int i = 0; i < kbd->keysCount; ++i)
452870
    keyDeinit(&kbd->keys[i]);
452870
}
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
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->downKey != k) {
452870
        keyboardMouseUp(kbd);
452870
        kbd->downKey = k;
452870
        keyMouseDown(k);
452870
      }
452870
      break;
452870
    }
452870
  }
452870
}
452870
452870
452870
void keyboardMouseUp(Keyboard *kbd) {
452870
  if (kbd->downKey) {
452870
    keyMouseUp(kbd->downKey);
452870
    kbd->downKey = NULL;
452870
  }
452870
}