File: keyboard.cpp

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (358 lines) | stat: -rw-r--r-- 10,759 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************
 * Keyboard management.
 *****************************************************************************/

#include "game/game.h"
#include "interface/keyboard.h"
#include "network/chat.h"
#ifdef DEBUG
#include "network/randomsync.h"
#endif
#include "tool/xml_document.h"

#include <SDL_events.h>
#include <libxml/tree.h>

#define MODIFIER_OFFSET (SDLK_LAST + 1)
#define SHIFT_BIT 0x1
#define ALT_BIT 0x2
#define CONTROL_BIT 0x4
#define SHIFT_OFFSET (MODIFIER_OFFSET * SHIFT_BIT)
#define CONTROL_OFFSET (MODIFIER_OFFSET * CONTROL_BIT)
#define ALT_OFFSET (MODIFIER_OFFSET * ALT_BIT)

#if SDL_MINOR_VERSION != 2
#  define SDLK_LAST  SDL_NUM_SCANCODES
#endif


int  Keyboard::GetRawKeyCode(int key_code) const
{
  return key_code % MODIFIER_OFFSET;
}

bool Keyboard::HasShiftModifier(int key_code) const
{
  return (key_code/SHIFT_OFFSET)&1;
}

bool Keyboard::HasAltModifier(int key_code) const
{
  return (key_code/ALT_OFFSET)&1;
}

bool Keyboard::HasControlModifier(int key_code) const
{
  return (key_code/CONTROL_OFFSET)&1;
}

ManMachineInterface::Key_t
Keyboard::GetRegisteredAction(int raw_key_code, bool ctrl, bool alt, bool shift) const
{
  int key_code = raw_key_code + shift*SHIFT_OFFSET +
                 alt*ALT_OFFSET + ctrl*CONTROL_OFFSET;

  // If the map doesn't have such key, exit immediately
  std::map<int, std::vector<Key_t> >::const_iterator it = layout.find(key_code);
  if (it == layout.end())
    return KEY_NONE;

  // Must use at because [] has side effect of inserting a new key,
  // despite our previous verification
  return ((*it).second.size()) ?(*it).second[0] : KEY_NONE;
}

bool Keyboard::SaveKeyEvent(Key_t at, int raw_key_code,
                            bool ctrl, bool alt, bool shift)
{
  int key_code = raw_key_code + shift*SHIFT_OFFSET
               + alt*ALT_OFFSET + ctrl*CONTROL_OFFSET;
  SetKeyAction(key_code, at);
  return true;
}


Keyboard::Keyboard()
  : ManMachineInterface()
  , modifier_bits(0)
  , modifier_only_bits(0)
{
  //Disable repeated events when a key is kept down
  SDL_EnableKeyRepeat(0,0);
  SetDefaultConfig();

  // Registring SDL event
  RegisterEvent(SDL_KEYDOWN);
  RegisterEvent(SDL_KEYUP);
}

void Keyboard::SetDefaultConfig()
{
  SetKeyAction(SDLK_ESCAPE, ManMachineInterface::KEY_QUIT);
}

void Keyboard::SetConfig(const xmlNode *node)
{
  ASSERT(node);

  //Remove old key configuration
  ClearKeyBindings();

  xmlNodeArray list = XmlReader::GetNamedChildren(node, "bind");
  for (xmlNodeArray::iterator it = list.begin(); it != list.end(); ++it) {
    std::string key_name, action_name;
    bool shift = false, control = false, alt = false;

    //Extract XML config
    XmlReader::ReadStringAttr(*it, "key", key_name);
    XmlReader::ReadStringAttr(*it, "action", action_name);
    XmlReader::ReadBoolAttr(*it, "shift", shift);
    XmlReader::ReadBoolAttr(*it, "control", control);
    XmlReader::ReadBoolAttr(*it, "alt", alt);

    //Generate key and action value
    int key;
    Key_t action;

    // Set internal key
    key = GetKeyFromKeyName(key_name);
    // Reject config set for other values
    if (SDLK_BACKSPACE == key || SDLK_DELETE == key)
      continue;

    if (shift) {
      key += SHIFT_OFFSET;
    }
    if (control) {
      key += CONTROL_OFFSET;
    }
    if (alt) {
      key += ALT_OFFSET;
    }

    action = GetActionFromActionName(action_name);

    //Set association
    SetKeyAction(key, action);
  }
}

void Keyboard::SaveConfig(xmlNode *node) const
{
  xmlNode *keyboard_node = xmlAddChild(node, xmlNewNode(NULL /* empty prefix */, (const xmlChar*)"keyboard"));
  std::map<int, std::vector<Key_t> >::const_iterator it;

  for (it = layout.begin(); it != layout.end(); it++) {
    const std::vector<Key_t>& actions = it->second;
    for (uint i = 0; i < actions.size(); i++) {
      int key = it->first;

      if (key == SDLK_UNKNOWN)
        continue;

      bool shift = false, control = false, alt = false;

      if (key > CONTROL_OFFSET) {
        key -= CONTROL_OFFSET;
        control = true;
      }

      if (key > ALT_OFFSET) {
        key -= ALT_OFFSET;
        alt = true;
      }

      if (key > SHIFT_OFFSET) {
        key -= SHIFT_OFFSET;
        shift = true;
      }

      //Generate node
      xmlNode *bind = xmlAddChild(keyboard_node, xmlNewNode(NULL /* empty prefix */, (const xmlChar*)"bind"));
      xmlSetProp(bind, (const xmlChar*)"key", (const xmlChar*)GetKeyNameFromKey(key).c_str());
      xmlSetProp(bind, (const xmlChar*)"action", (const xmlChar*)GetActionNameFromAction(actions.at(i)).c_str());
      if (shift) xmlSetProp(bind, (const xmlChar*)"shift", (const xmlChar*)"true");
      if (control) xmlSetProp(bind, (const xmlChar*)"control", (const xmlChar*)"true");
      if (alt) xmlSetProp(bind, (const xmlChar*)"alt", (const xmlChar*)"true");
    }
  }
}

void Keyboard::HandleKeyComboEvent(int key_code, Key_Event_t event_type)
{
  MSG_DEBUG("keyboard", "%s %s%s%s%d",
            event_type == KEY_PRESSED ?"pressed":"released",
            ((key_code / MODIFIER_OFFSET) & CONTROL_BIT)?"[control] + ": "",
            ((key_code / MODIFIER_OFFSET) & ALT_BIT)?"[alt] + ": "",
            ((key_code / MODIFIER_OFFSET) & SHIFT_BIT)?"[shift] + ": "",
            key_code % MODIFIER_OFFSET);
  std::map<int, std::vector<Key_t> >::iterator it = layout.find(key_code);

  if (it == layout.end())
    return;


  const std::vector<Key_t>& keys = it->second;
  std::vector<Key_t>::const_iterator itv;

  for (itv = keys.begin(); itv != keys.end() ; itv++) {
    //While player writes, it cannot control the game but PAUSE.
    if (Game::GetInstance()->chatsession.CheckInput()) {
      switch (*itv) {
      case KEY_PAUSE:
        break;
      default:
        return;
      }
    }

    if (event_type == KEY_PRESSED) {
      HandleKeyPressed(*itv);
      return;
    }

    if (event_type == KEY_RELEASED) {
      HandleKeyReleased(*itv);
      return;
    }
  }
}

static int GetModifierBitsFromSDL() {
  SDLMod sdl_modifier_bits = SDL_GetModState();
  int result = 0;
  if (sdl_modifier_bits & KMOD_SHIFT)
    result |= SHIFT_BIT;

  if (sdl_modifier_bits & KMOD_ALT)
    result |= ALT_BIT;

  if (sdl_modifier_bits & KMOD_CTRL)
    result |= CONTROL_BIT;

  return result;
}

bool Keyboard::IsModifier(int raw_key_code)
{
  return raw_key_code>=SDLK_NUMLOCK && raw_key_code<=SDLK_COMPOSE;
}

bool Keyboard::HandleKeyEvent(const SDL_Event& evnt)
{
  // Not a registred event
  if (!IsRegistredEvent(evnt.type))
    return false;

  Key_Event_t event_type;
  switch(evnt.type) {
  case SDL_KEYDOWN:
    event_type = KEY_PRESSED;
    break;
  case SDL_KEYUP:
    event_type = KEY_RELEASED;
    break;
  default:
    return false;
  }

  //Handle input text for Chat session in Network game
  if (Game::GetInstance()->chatsession.CheckInput()) {
    if (event_type == KEY_PRESSED)
      Game::GetInstance()->chatsession.HandleKeyPressed(evnt);
    else if (event_type == KEY_RELEASED)
      Game::GetInstance()->chatsession.HandleKeyReleased(evnt);
    return false;
  }

  SDLKey basic_key_code = evnt.key.keysym.sym;

#ifdef DEBUG
  if (IsLOGGING("killsynchro")
      && basic_key_code == SDLK_k
      && event_type == KEY_RELEASED) {
    fprintf(stderr, "\n\nKILLING NETWORK SYNCHRONISATION!\n\n");
    RandomSync().SetSeed(0);
  }
#endif

  // Also ignore real key code of a modifier, fix bug #15238
  if (IsModifier(basic_key_code)) {
    int modifier_changed = modifier_only_bits ^ GetModifierBitsFromSDL();
    if (event_type == KEY_RELEASED) {
      for (std::set<SDLKey>::const_iterator it = pressed_keys.begin(); it != pressed_keys.end(); it++ ) {
        int key_code = *it + MODIFIER_OFFSET * modifier_changed;
        HandleKeyComboEvent(key_code, KEY_RELEASED);
      }
    } else if (modifier_only_bits && event_type == KEY_PRESSED) {
      for (std::set<SDLKey>::const_iterator it = pressed_keys.begin(); it != pressed_keys.end(); it++ ) {
        int key_code = *it + MODIFIER_OFFSET * modifier_only_bits;
        HandleKeyComboEvent(key_code, KEY_RELEASED);
      }
    }
    modifier_only_bits = GetModifierBitsFromSDL();
    return false;
  }
#ifdef MAEMO
  if (SDL_GetModState() & KMOD_MODE) {
    if (basic_key_code == SDLK_LEFT) basic_key_code = SDLK_UP;
    if (basic_key_code == SDLK_RIGHT) basic_key_code = SDLK_DOWN;
  }
#endif

  int key_code;
  int previous_modifier_bits = modifier_bits;
  modifier_bits = GetModifierBitsFromSDL();

  if (modifier_bits != previous_modifier_bits) {
    std::set<SDLKey>::const_iterator it = pressed_keys.find(basic_key_code);
    if (it !=  pressed_keys.end()) {
      key_code = basic_key_code + MODIFIER_OFFSET * previous_modifier_bits;
      HandleKeyComboEvent(key_code, KEY_RELEASED);
      if (key_code != basic_key_code)
        HandleKeyComboEvent(basic_key_code, KEY_RELEASED);
      pressed_keys.erase(basic_key_code);
      if (event_type == KEY_PRESSED) {
        key_code = basic_key_code + MODIFIER_OFFSET * modifier_bits;
        HandleKeyComboEvent(key_code, KEY_PRESSED);
        pressed_keys.insert(basic_key_code);
      }
      return true;
    }
  }

  if (event_type == KEY_PRESSED) {
    key_code = basic_key_code + (MODIFIER_OFFSET * modifier_bits);
    HandleKeyComboEvent(key_code, KEY_PRESSED);
    if (previous_modifier_bits ^ modifier_bits) {
      key_code = basic_key_code + (MODIFIER_OFFSET * previous_modifier_bits);
      HandleKeyComboEvent(key_code, KEY_RELEASED);
    }
    pressed_keys.insert(basic_key_code);
  } else {
    ASSERT(event_type == KEY_RELEASED);
    key_code = basic_key_code + (MODIFIER_OFFSET * previous_modifier_bits);
    HandleKeyComboEvent(key_code, KEY_RELEASED);
    if (key_code != basic_key_code)
      HandleKeyComboEvent(basic_key_code, KEY_RELEASED);
    pressed_keys.erase(basic_key_code);
  }
  return true;
}