Blob Blame Raw
#ifndef KEYBOARD_H
#define KEYBOARD_H


#include "graph.h"
#include "input.h"


#define K_LBL_SIZE LABEL_MAXLEN


// key flags
enum {
  KF_HIGHLIGHT = 1 << 0, // key will be hightlighted
  KF_HOLD      = 1 << 1, // key will be hold until it will pressed again or non-hold key will pressed
  KF_MOD       = 1 << 2, // visible value depends on modifier state (using optValue field)
  KF_LAYOUT    = 1 << 3, // key to switch layout (using optValue field);
  KF_MOVE      = 1 << 4, // handle to move the window
  KF_SIZE      = 1 << 5, // key to resize the window
  KF_CLOSE     = 1 << 6, // key to close the window
  
  KF_SPECIAL   = KF_HOLD | KF_MOD | KF_LAYOUT | KF_MOVE | KF_SIZE | KF_CLOSE,
};


// special layout indices
enum {
  LI_PREV   = -1,   // switch to previous layout in the list
  LI_NEXT   = -2,   // switch to next layout in the list
  LI_REVERT = -3 }; // switch to previously used layout

  
  
struct Key;
struct Layout;
struct Keyboard;
typedef struct Key Key;
typedef struct Layout Layout;
typedef struct Keyboard Keyboard;


struct Key {
  // keep these filds at the begining for easyest static initialization
  int ox, oy, ow, oh;
  unsigned int keySym;
  unsigned int keySym2;
  char label[K_LBL_SIZE];
  char label2[K_LBL_SIZE];
  unsigned int flags;
  int optValue;
  
  // these fields will be set while initialization
  Layout *l;
  int isLetter;
  int isKeypad;
  TextLayout tl;
  TextLayout tl2;
  
  // dynamic fields
  int x, y, w, h;
  int down;
  int longDown;
  int downKeycode;
  int mx, my; // values for move and size buttons
  Key *nextHeld, *prevHeld;
  Key *orig, *clone;
};


struct Layout {
  // keep these filds at the begining for easyest static initialization
  Key *keys;
  int keysCount;
  int ow, oh;
  
  // these fields will be set while initialization
  Keyboard *kbd;
  int hidden;

  // dynamic fields
  int w, h;
  Key *downKey; // key that received the last mouse down event
};


struct Keyboard {
  // keep these filds at the begining for easyest static initialization
  Layout *layouts;
  int layoutsCount;
  int ow, oh;
  
  // these fields will be set while initialization
  App *app;
  
  // dynamic fields
  unsigned int lastModifiers;
  Layout *current;
  Layout *lastVisible;
  Key *firstHeld;
};


int keyInit(Key *k, Layout *l);
void keyDeinit(Key *k);
void keyInvalidateRect(Key *k);
void keyDraw(Key *k, int cx, int cy, int cw, int ch);
void keyDown(Key *k, int x, int y);
void keyMotion(Key *k, int x, int y);
void keyLongDown(Key *k);
void keyUp(Key *k, int force);

int layoutInit(Layout *l, Keyboard *kbd);
void layoutDeinit(Layout *l);
void layoutResize(Layout *l);
void layoutDraw(Layout *l, int cx, int cy, int cw, int ch);
void layoutMouseDown(Layout *l, int x, int y);
void layoutMouseMotion(Layout *l, int x, int y);
void layoutMouseLongDown(Layout *l);
void layoutMouseUp(Layout *l);

int keyboardInit(Keyboard *kbd, App *app);
void keyboardDeinit(Keyboard *kbd);
void keyboardResize(Keyboard *kbd);
void keyboardUpdateModifiers(Keyboard *kbd);
void keyboardDraw(Keyboard *kbd, int cx, int cy, int cw, int ch);
void keyboardMouseDown(Keyboard *kbd, int x, int y);
void keyboardMouseMotion(Keyboard *kbd, int x, int y);
void keyboardMouseLongDown(Keyboard *kbd);
void keyboardMouseUp(Keyboard *kbd);
void keyboardSwitchLayout(Keyboard *kbd, int index);
void keyboardRelaseHeld(Keyboard *kbd);


#endif