Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
in_kb.cpp File Reference
#include <queue>
#include <list>
#include "vegastrike.h"
#include "vs_globals.h"
#include "in_kb.h"
#include "in_handler.h"
#include "gldrv/winsys.h"
#include "in_kb_data.h"

Go to the source code of this file.

Classes

struct  HandlerCall
 

Functions

static void DefaultKBHandler (const KBData &, KBSTATE newState)
 
static void kbGetInput (int key, int modifiers, bool release, int x, int y)
 
static bool kbHasBinding (int key, int modifiers)
 
int shiftup (int ch)
 
int shiftdown (int ch)
 
void setActiveModifiers (unsigned int mask)
 
unsigned int getActiveModifiers ()
 
unsigned int pullActiveModifiers ()
 
unsigned int getModifier (const char *mod_name)
 
int getModifier (bool alton, bool cntrlon, bool shifton)
 
void glut_keyboard_cb (unsigned int ch, unsigned int mod, bool release, int x, int y)
 
void RestoreKB ()
 
void InitKB ()
 
void ProcessKB (unsigned int player)
 
void BindKey (int key, unsigned int mod, unsigned int player, KBHandler handler, const KBData &data)
 
void UnbindKey (int key, unsigned int mod)
 

Variables

static HandlerCall keyBindings [LAST_MODIFIER][WSK_LAST]
 
static unsigned int playerBindings [LAST_MODIFIER][WSK_LAST]
 
KBSTATE keyState [LAST_MODIFIER][WSK_LAST]
 
static const char _lomap [] = "0123456789-=\';/.,`\\"
 
static const char _himap [] = ")!@#$%^&*(_+\":?><~|"
 
static unsigned int _activeModifiers = 0
 

Function Documentation

void BindKey ( int  key,
unsigned int  mod,
unsigned int  player,
KBHandler  handler,
const KBData data 
)

Definition at line 213 of file in_kb.cpp.

References HandlerCall::data, HandlerCall::function, playerBindings, and RESET.

Referenced by createObjects(), and InitializeInput().

214 {
215  keyBindings[mod][key].function = handler;
216  keyBindings[mod][key].data = data;
217  playerBindings[mod][key] = player;
218  handler( std::string(), RESET ); //key is not used in handler
219 }
static void DefaultKBHandler ( const KBData ,
KBSTATE  newState 
)
static

Definition at line 31 of file in_kb.cpp.

Referenced by HandlerCall::HandlerCall().

32 {
33  //do nothing
34 }
unsigned int getActiveModifiers ( )

Definition at line 109 of file in_kb.cpp.

References _activeModifiers.

Referenced by pullActiveModifiers().

110 {
111  return _activeModifiers;
112 }
unsigned int getModifier ( const char *  mod_name)

Definition at line 120 of file in_kb.cpp.

References KB_MOD_ALT, KB_MOD_CTRL, and KB_MOD_SHIFT.

Referenced by glut_keyboard_cb().

121 {
122  if (mod_name[0] == '\0')
123  return 0;
124  unsigned int rv = 0;
125  if ( strstr( mod_name, "shift" ) || strstr( mod_name, "uppercase" ) || strstr( mod_name, "caps" ) )
126  rv |= KB_MOD_SHIFT;
127  if ( strstr( mod_name, "ctrl" ) || strstr( mod_name, "cntrl" ) || strstr( mod_name, "control" ) )
128  rv |= KB_MOD_CTRL;
129  if ( strstr( mod_name, "alt" ) || strstr( mod_name, "alternate" ) )
130  rv |= KB_MOD_ALT;
131  return rv;
132 }
int getModifier ( bool  alton,
bool  cntrlon,
bool  shifton 
)

Definition at line 133 of file in_kb.cpp.

References KB_MOD_ALT.

134 {
135  return cntrlon ? KB_MOD_CTRL : ( alton ? KB_MOD_ALT : (shifton ? KB_MOD_SHIFT : 0) );
136 }
void glut_keyboard_cb ( unsigned int  ch,
unsigned int  mod,
bool  release,
int  x,
int  y 
)

Definition at line 137 of file in_kb.cpp.

References DOWN, getModifier(), i, KB_MOD_ALT, KB_MOD_MASK, kbGetInput(), kbHasBinding(), keyState, LAST_MODIFIER, setActiveModifiers(), shiftdown(), shiftup(), WSK_MOD_LALT, WSK_MOD_LCTRL, WSK_MOD_LSHIFT, WSK_MOD_RALT, WSK_MOD_RCTRL, and WSK_MOD_RSHIFT.

Referenced by RestoreKB().

138 {
139  bool shifton = false;
140  int alton = false;
141  int ctrlon = false;
142 
143  unsigned int modmask = KB_MOD_MASK;
144  if ( ( WSK_MOD_LSHIFT == (mod&WSK_MOD_LSHIFT) ) || ( WSK_MOD_RSHIFT == (mod&WSK_MOD_RSHIFT) ) ) {
145  //This is ugly, but we have to support legacy config files...
146  //...maybe add config option to disable this soooo ugly thing...
147  if ( !kbHasBinding( ch, KB_MOD_SHIFT ) ) {
148  ch = shiftup( ch );
149  modmask &= ~KB_MOD_SHIFT;
150  }
151  shifton = true;
152  }
153  if ( ( WSK_MOD_LALT == (mod&WSK_MOD_LALT) ) || ( WSK_MOD_RALT == (mod&WSK_MOD_RALT) ) )
154  alton = true;
155  if ( ( WSK_MOD_LCTRL == (mod&WSK_MOD_LCTRL) ) || ( WSK_MOD_RCTRL == (mod&WSK_MOD_RCTRL) ) )
156  ctrlon = true;
157  //Polling state
159  (shifton ? KB_MOD_SHIFT : 0)
160  |(alton ? KB_MOD_ALT : 0)
161  |(ctrlon ? KB_MOD_CTRL : 0) );
162 
163  int curmod = getModifier( alton, ctrlon, shifton )&modmask;
164  kbGetInput( ch, curmod, release, x, y );
165  if (release) {
166  for (int i = 0; i < LAST_MODIFIER; ++i) {
167  if (i != curmod) {
168  if (keyState[i][shiftdown( ch )] == DOWN)
169  kbGetInput( shiftdown( ch ), i, release, x, y );
170  if (keyState[i][shiftup( ch )] == DOWN)
171  kbGetInput( shiftup( ch ), i, release, x, y );
172  } else {
173  if (shifton) {
174  if ( ( (unsigned int) shiftdown( ch ) ) != ch && keyState[i][shiftdown( ch )] == DOWN )
175  kbGetInput( shiftdown( ch ), i, release, x, y );
176  } else if ( ( (unsigned int) shiftup( ch ) ) != ch && keyState[i][shiftup( ch )] == DOWN ) {
177  kbGetInput( shiftup( ch ), i, release, x, y );
178  }
179  }
180  }
181  }
182 }
void InitKB ( )

Definition at line 195 of file in_kb.cpp.

References a, i, KEYMAP_SIZE, keyState, LAST_MODIFIER, RestoreKB(), UnbindKey(), and UP.

Referenced by InitInput().

196 {
197  for (int i = 0; i < LAST_MODIFIER; ++i)
198  for (int a = 0; a < KEYMAP_SIZE; a++) {
199  keyState[i][a] = UP;
200  UnbindKey( a, i );
201  }
202  RestoreKB();
203 }
static void kbGetInput ( int  key,
int  modifiers,
bool  release,
int  x,
int  y 
)
static

FIXME If key is out of array index range, do nothing. This is a quick hack, the underlying cause of invalid parameters ever being given should probably be fixed instead

Definition at line 48 of file in_kb.cpp.

References _Universe, Universe::CurrentCockpit(), DOWN, HandlerCall::function, i, keyState, playerBindings, PRESS, RELEASE, RESET, Universe::SetActiveCockpit(), UP, and WSK_LAST.

Referenced by glut_keyboard_cb().

49 {
51  if (key < 0 || key >= WSK_LAST)
52  return;
53  int i = _Universe->CurrentCockpit();
54  _Universe->SetActiveCockpit( playerBindings[modifiers][key] );
55  if ( (keyState[modifiers][key] == RESET || keyState[modifiers][key] == UP) && !release )
56  keyBindings[modifiers][key].function( keyBindings[modifiers][key].data, PRESS );
57  if ( (keyState[modifiers][key] == DOWN || keyState[modifiers][key] == RESET) && release )
58  keyBindings[modifiers][key].function( keyBindings[modifiers][key].data, RELEASE );
59  keyState[modifiers][key] = release ? UP : DOWN;
61 }
static bool kbHasBinding ( int  key,
int  modifiers 
)
static

Definition at line 63 of file in_kb.cpp.

References HandlerCall::function.

Referenced by glut_keyboard_cb().

64 {
65  static HandlerCall defaultHandler;
66  return keyBindings[modifiers][key].function != defaultHandler.function;
67 }
void ProcessKB ( unsigned int  player)

Definition at line 205 of file in_kb.cpp.

References a, HandlerCall::function, KEYMAP_SIZE, keyState, LAST_MODIFIER, and playerBindings.

Referenced by ProcessInput().

206 {
207  for (int mod = 0; mod < LAST_MODIFIER; mod++)
208  for (int a = 0; a < KEYMAP_SIZE; a++)
209  if (playerBindings[mod][a] == player)
210  keyBindings[mod][a].function( keyBindings[mod][a].data, keyState[mod][a] );
211 }
unsigned int pullActiveModifiers ( )

Definition at line 113 of file in_kb.cpp.

References getActiveModifiers().

Referenced by BaseUtil::SetKeyStatusEventData().

114 {
115 #ifdef SDL_WINDOWING
116  setActiveModifiersSDL( SDL_GetModState() );
117 #endif
118  return getActiveModifiers();
119 }
void RestoreKB ( )

Definition at line 184 of file in_kb.cpp.

References a, DOWN, HandlerCall::function, glut_keyboard_cb(), i, KEYMAP_SIZE, keyState, LAST_MODIFIER, RELEASE, UP, and winsys_set_keyboard_func().

Referenced by GameCockpit::Draw(), InitKB(), restore_main_loop(), CockpitKeys::TextMessageCallback(), and NavComputer::toggleVisibility().

185 {
186  for (int i = 0; i < LAST_MODIFIER; ++i)
187  for (int a = 0; a < KEYMAP_SIZE; a++)
188  if (keyState[i][a] == DOWN) {
189  keyBindings[i][a].function( keyBindings[i][a].data, RELEASE );
190  keyState[i][a] = UP;
191  }
193 }
void setActiveModifiers ( unsigned int  mask)

Definition at line 96 of file in_kb.cpp.

References _activeModifiers.

Referenced by base_keyboard_cb(), gamemenu_keyboard_handler(), and glut_keyboard_cb().

97 {
98  _activeModifiers = mask;
99 }
int shiftdown ( int  ch)

Definition at line 83 of file in_kb.cpp.

References _himap, _lomap, and c.

Referenced by glut_keyboard_cb().

84 {
85  if ( ch == (ch&0xFF) ) {
86  const char *c = strchr( _himap, ch );
87  if (c)
88  return _lomap[c-_himap];
89 
90  else
91  return tolower( ch );
92  } else {return ch; }}
int shiftup ( int  ch)

Definition at line 72 of file in_kb.cpp.

References _himap, _lomap, and c.

Referenced by base_keyboard_cb(), gamemenu_keyboard_handler(), glut_keyboard_cb(), nav_keyboard_cb(), and CockpitKeys::TextMessageCallback().

73 {
74  if ( ch == (ch&0xFF) ) {
75  const char *c = strchr( _lomap, ch );
76  if (c)
77  return _himap[c-_lomap];
78 
79  else
80  return toupper( ch );
81  } else {return ch; }}
void UnbindKey ( int  key,
unsigned int  mod 
)

Definition at line 221 of file in_kb.cpp.

Referenced by InitKB().

222 {
223  keyBindings[mod][key] = HandlerCall();
224 }

Variable Documentation

unsigned int _activeModifiers = 0
static

Definition at line 94 of file in_kb.cpp.

Referenced by getActiveModifiers(), and setActiveModifiers().

const char _himap[] = ")!@#$%^&*(_+\":?><~|"
static

Definition at line 70 of file in_kb.cpp.

Referenced by shiftdown(), and shiftup().

const char _lomap[] = "0123456789-=\';/.,`\\"
static

Definition at line 69 of file in_kb.cpp.

Referenced by shiftdown(), and shiftup().

HandlerCall keyBindings[LAST_MODIFIER][WSK_LAST]
static

Definition at line 44 of file in_kb.cpp.

unsigned int playerBindings[LAST_MODIFIER][WSK_LAST]
static

Definition at line 45 of file in_kb.cpp.

Referenced by BindKey(), kbGetInput(), and ProcessKB().