OpenTTD
hotkeys.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD 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, version 2.
6  * OpenTTD 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.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #include "stdafx.h"
13 #include "openttd.h"
14 #include "hotkeys.h"
15 #include "ini_type.h"
16 #include "string_func.h"
17 #include "window_gui.h"
18 
19 #include "safeguards.h"
20 
21 char *_hotkeys_file;
22 
28 
30 struct KeycodeNames {
31  const char *name;
33 };
34 
36 static const KeycodeNames _keycode_to_name[] = {
37  {"SHIFT", WKC_SHIFT},
38  {"CTRL", WKC_CTRL},
39  {"ALT", WKC_ALT},
40  {"META", WKC_META},
41  {"GLOBAL", WKC_GLOBAL_HOTKEY},
42  {"ESC", WKC_ESC},
43  {"DEL", WKC_DELETE},
44  {"BACKSPACE", WKC_BACKSPACE},
45  {"RETURN", WKC_RETURN},
46  {"BACKQUOTE", WKC_BACKQUOTE},
47  {"F1", WKC_F1},
48  {"F2", WKC_F2},
49  {"F3", WKC_F3},
50  {"F4", WKC_F4},
51  {"F5", WKC_F5},
52  {"F6", WKC_F6},
53  {"F7", WKC_F7},
54  {"F8", WKC_F8},
55  {"F9", WKC_F9},
56  {"F10", WKC_F10},
57  {"F11", WKC_F11},
58  {"F12", WKC_F12},
59  {"PAUSE", WKC_PAUSE},
60  {"COMMA", WKC_COMMA},
61  {"NUM_PLUS", WKC_NUM_PLUS},
62  {"NUM_MINUS", WKC_NUM_MINUS},
63  {"=", WKC_EQUALS},
64  {"-", WKC_MINUS},
65 };
66 
73 static uint16 ParseCode(const char *start, const char *end)
74 {
75  assert(start <= end);
76  while (start < end && *start == ' ') start++;
77  while (end > start && *end == ' ') end--;
78  for (uint i = 0; i < lengthof(_keycode_to_name); i++) {
79  if (strlen(_keycode_to_name[i].name) == (size_t)(end - start) && strncasecmp(start, _keycode_to_name[i].name, end - start) == 0) {
80  return _keycode_to_name[i].keycode;
81  }
82  }
83  if (end - start == 1) {
84  if (*start >= 'a' && *start <= 'z') return *start - ('a'-'A');
85  /* Ignore invalid keycodes */
86  if (*(const uint8 *)start < 128) return *start;
87  }
88  return 0;
89 }
90 
97 static uint16 ParseKeycode(const char *start, const char *end)
98 {
99  assert(start <= end);
100  uint16 keycode = 0;
101  for (;;) {
102  const char *cur = start;
103  while (*cur != '+' && cur != end) cur++;
104  uint16 code = ParseCode(start, cur);
105  if (code == 0) return 0;
106  if (code & WKC_SPECIAL_KEYS) {
107  /* Some completely wrong keycode we don't support. */
108  if (code & ~WKC_SPECIAL_KEYS) return 0;
109  keycode |= code;
110  } else {
111  /* Ignore the code if it has more then 1 letter. */
112  if (keycode & ~WKC_SPECIAL_KEYS) return 0;
113  keycode |= code;
114  }
115  if (cur == end) break;
116  assert(cur < end);
117  start = cur + 1;
118  }
119  return keycode;
120 }
121 
127 static void ParseHotkeys(Hotkey *hotkey, const char *value)
128 {
129  const char *start = value;
130  while (*start != '\0') {
131  const char *end = start;
132  while (*end != '\0' && *end != ',') end++;
133  uint16 keycode = ParseKeycode(start, end);
134  if (keycode != 0) hotkey->AddKeycode(keycode);
135  start = (*end == ',') ? end + 1: end;
136  }
137 }
138 
148 static const char *KeycodeToString(uint16 keycode)
149 {
150  static char buf[32];
151  buf[0] = '\0';
152  bool first = true;
153  if (keycode & WKC_GLOBAL_HOTKEY) {
154  strecat(buf, "GLOBAL", lastof(buf));
155  first = false;
156  }
157  if (keycode & WKC_SHIFT) {
158  if (!first) strecat(buf, "+", lastof(buf));
159  strecat(buf, "SHIFT", lastof(buf));
160  first = false;
161  }
162  if (keycode & WKC_CTRL) {
163  if (!first) strecat(buf, "+", lastof(buf));
164  strecat(buf, "CTRL", lastof(buf));
165  first = false;
166  }
167  if (keycode & WKC_ALT) {
168  if (!first) strecat(buf, "+", lastof(buf));
169  strecat(buf, "ALT", lastof(buf));
170  first = false;
171  }
172  if (keycode & WKC_META) {
173  if (!first) strecat(buf, "+", lastof(buf));
174  strecat(buf, "META", lastof(buf));
175  first = false;
176  }
177  if (!first) strecat(buf, "+", lastof(buf));
178  keycode = keycode & ~WKC_SPECIAL_KEYS;
179 
180  for (uint i = 0; i < lengthof(_keycode_to_name); i++) {
181  if (_keycode_to_name[i].keycode == keycode) {
182  strecat(buf, _keycode_to_name[i].name, lastof(buf));
183  return buf;
184  }
185  }
186  assert(keycode < 128);
187  char key[2];
188  key[0] = keycode;
189  key[1] = '\0';
190  strecat(buf, key, lastof(buf));
191  return buf;
192 }
193 
202 const char *SaveKeycodes(const Hotkey *hotkey)
203 {
204  static char buf[128];
205  buf[0] = '\0';
206  for (uint i = 0; i < hotkey->keycodes.Length(); i++) {
207  const char *str = KeycodeToString(hotkey->keycodes[i]);
208  if (i > 0) strecat(buf, ",", lastof(buf));
209  strecat(buf, str, lastof(buf));
210  }
211  return buf;
212 }
213 
220 Hotkey::Hotkey(uint16 default_keycode, const char *name, int num) :
221  name(name),
222  num(num)
223 {
224  if (default_keycode != 0) this->AddKeycode(default_keycode);
225 }
226 
233 Hotkey::Hotkey(const uint16 *default_keycodes, const char *name, int num) :
234  name(name),
235  num(num)
236 {
237  const uint16 *keycode = default_keycodes;
238  while (*keycode != 0) {
239  this->AddKeycode(*keycode);
240  keycode++;
241  }
242 }
243 
249 void Hotkey::AddKeycode(uint16 keycode)
250 {
251  this->keycodes.Include(keycode);
252 }
253 
254 HotkeyList::HotkeyList(const char *ini_group, Hotkey *items, GlobalHotkeyHandlerFunc global_hotkey_handler) :
255  global_hotkey_handler(global_hotkey_handler), ini_group(ini_group), items(items)
256 {
257  if (_hotkey_lists == NULL) _hotkey_lists = new SmallVector<HotkeyList*, 16>();
258  *_hotkey_lists->Append() = this;
259 }
260 
261 HotkeyList::~HotkeyList()
262 {
263  _hotkey_lists->Erase(_hotkey_lists->Find(this));
264 }
265 
271 {
272  IniGroup *group = ini->GetGroup(this->ini_group);
273  for (Hotkey *hotkey = this->items; hotkey->name != NULL; ++hotkey) {
274  IniItem *item = group->GetItem(hotkey->name, false);
275  if (item != NULL) {
276  hotkey->keycodes.Clear();
277  if (item->value != NULL) ParseHotkeys(hotkey, item->value);
278  }
279  }
280 }
281 
286 void HotkeyList::Save(IniFile *ini) const
287 {
288  IniGroup *group = ini->GetGroup(this->ini_group);
289  for (const Hotkey *hotkey = this->items; hotkey->name != NULL; ++hotkey) {
290  IniItem *item = group->GetItem(hotkey->name, true);
291  item->SetValue(SaveKeycodes(hotkey));
292  }
293 }
294 
301 int HotkeyList::CheckMatch(uint16 keycode, bool global_only) const
302 {
303  for (const Hotkey *list = this->items; list->name != NULL; ++list) {
304  if (list->keycodes.Contains(keycode | WKC_GLOBAL_HOTKEY) || (!global_only && list->keycodes.Contains(keycode))) {
305  return list->num;
306  }
307  }
308  return -1;
309 }
310 
311 
312 static void SaveLoadHotkeys(bool save)
313 {
314  IniFile *ini = new IniFile();
315  ini->LoadFromDisk(_hotkeys_file, NO_DIRECTORY);
316 
317  for (HotkeyList **list = _hotkey_lists->Begin(); list != _hotkey_lists->End(); ++list) {
318  if (save) {
319  (*list)->Save(ini);
320  } else {
321  (*list)->Load(ini);
322  }
323  }
324 
325  if (save) ini->SaveToDisk(_hotkeys_file);
326  delete ini;
327 }
328 
329 
332 {
333  SaveLoadHotkeys(false);
334 }
335 
338 {
339  SaveLoadHotkeys(true);
340 }
341 
342 void HandleGlobalHotkeys(WChar key, uint16 keycode)
343 {
344  for (HotkeyList **list = _hotkey_lists->Begin(); list != _hotkey_lists->End(); ++list) {
345  if ((*list)->global_hotkey_handler == NULL) continue;
346 
347  int hotkey = (*list)->CheckMatch(keycode, true);
348  if (hotkey >= 0 && ((*list)->global_hotkey_handler(hotkey) == ES_HANDLED)) return;
349  }
350 }
351 
const char * name
Name of the keycode.
Definition: hotkeys.cpp:31
A group within an ini file.
Definition: ini_type.h:38
static char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: depend.cpp:99
WindowKeyCodes
Definition: gfx_type.h:29
All data for a single hotkey.
Definition: hotkeys.h:24
Hotkey(uint16 default_keycode, const char *name, int num)
Create a new Hotkey object with a single default keycode.
Definition: hotkeys.cpp:220
, Comma
Definition: gfx_type.h:104
Hotkey related functions.
= Equals
Definition: gfx_type.h:99
const T * Begin() const
Get the pointer to the first item (const)
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
Simple vector template class.
IniGroup * GetGroup(const char *name, size_t len=0, bool create_new=true)
Get the group with the given name.
Definition: ini_load.cpp:156
const T * End() const
Get the pointer behind the last valid item (const)
Functions, definitions and such used only by the GUI.
A single "line" in an ini file.
Definition: ini_type.h:25
T * Append(uint to_add=1)
Append an item and return it.
static void ParseHotkeys(Hotkey *hotkey, const char *value)
Parse a string to the keycodes it represents.
Definition: hotkeys.cpp:127
Functions related to low-level strings.
void Load(IniFile *ini)
Load HotkeyList from IniFile.
Definition: hotkeys.cpp:270
WindowKeyCodes keycode
The keycode.
Definition: hotkeys.cpp:32
static uint16 ParseKeycode(const char *start, const char *end)
Parse a string representation of a keycode.
Definition: hotkeys.cpp:97
uint Length() const
Get the number of items in the list.
void Save(IniFile *ini) const
Save HotkeyList to IniFile.
Definition: hotkeys.cpp:286
bool SaveToDisk(const char *filename)
Save the Ini file&#39;s data to the disk.
Definition: ini.cpp:43
void SetValue(const char *value)
Replace the current value with another value.
Definition: ini_load.cpp:49
void LoadFromDisk(const char *filename, Subdirectory subdir)
Load the Ini file&#39;s data from the disk.
Definition: ini_load.cpp:212
A path without any base directory.
Definition: fileio_type.h:127
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
List of hotkeys for a window.
Definition: hotkeys.h:42
char * value
The value of this item.
Definition: ini_type.h:28
const T * Find(const T &item) const
Search for the first occurrence of an item.
Some generic types.
static SmallVector< HotkeyList *, 16 > * _hotkey_lists
List of all HotkeyLists.
Definition: hotkeys.cpp:27
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
void AddKeycode(uint16 keycode)
Add a keycode to this hotkey, from now that keycode will be matched in addition to any previously add...
Definition: hotkeys.cpp:249
Types related to reading/writing &#39;*.ini&#39; files.
void LoadHotkeysFromConfig()
Load the hotkeys from the config file.
Definition: hotkeys.cpp:331
Ini file that supports both loading and saving.
Definition: ini_type.h:88
IniItem * GetItem(const char *name, bool create)
Get the item with the given name, and if it doesn&#39;t exist and create is true it creates a new item...
Definition: ini_load.cpp:105
bool Include(const T &item)
Tests whether a item is present in the vector, and appends it to the end if not.
void Erase(T *item)
Removes given item from this vector.
static uint16 ParseCode(const char *start, const char *end)
Try to parse a single part of a keycode.
Definition: hotkeys.cpp:73
static const KeycodeNames _keycode_to_name[]
Array of non-standard keycodes that can be used in the hotkeys config file.
Definition: hotkeys.cpp:36
void SaveHotkeysToConfig()
Save the hotkeys to the config file.
Definition: hotkeys.cpp:337
int CheckMatch(uint16 keycode, bool global_only=false) const
Check if a keycode is bound to something.
Definition: hotkeys.cpp:301
static const char * KeycodeToString(uint16 keycode)
Convert a hotkey to it&#39;s string representation so it can be written to the config file...
Definition: hotkeys.cpp:148
The passed event is handled.
Definition: window_type.h:714
String representation of a keycode.
Definition: hotkeys.cpp:30
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:35
const char * SaveKeycodes(const Hotkey *hotkey)
Convert all keycodes attached to a hotkey to a single string.
Definition: hotkeys.cpp:202
Fake keycode bit to indicate global hotkeys.
Definition: gfx_type.h:35