OpenTTD
hotkeys.cpp
Go to the documentation of this file.
1 /* $Id: hotkeys.cpp 27816 2017-03-20 19:30:49Z peter1138 $ */
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  {"RETURN", WKC_RETURN},
45  {"BACKQUOTE", WKC_BACKQUOTE},
46  {"F1", WKC_F1},
47  {"F2", WKC_F2},
48  {"F3", WKC_F3},
49  {"F4", WKC_F4},
50  {"F5", WKC_F5},
51  {"F6", WKC_F6},
52  {"F7", WKC_F7},
53  {"F8", WKC_F8},
54  {"F9", WKC_F9},
55  {"F10", WKC_F10},
56  {"F11", WKC_F11},
57  {"F12", WKC_F12},
58  {"PAUSE", WKC_PAUSE},
59  {"COMMA", WKC_COMMA},
60  {"NUM_PLUS", WKC_NUM_PLUS},
61  {"NUM_MINUS", WKC_NUM_MINUS},
62  {"=", WKC_EQUALS},
63  {"-", WKC_MINUS},
64 };
65 
72 static uint16 ParseCode(const char *start, const char *end)
73 {
74  assert(start <= end);
75  while (start < end && *start == ' ') start++;
76  while (end > start && *end == ' ') end--;
77  for (uint i = 0; i < lengthof(_keycode_to_name); i++) {
78  if (strlen(_keycode_to_name[i].name) == (size_t)(end - start) && strncasecmp(start, _keycode_to_name[i].name, end - start) == 0) {
79  return _keycode_to_name[i].keycode;
80  }
81  }
82  if (end - start == 1) {
83  if (*start >= 'a' && *start <= 'z') return *start - ('a'-'A');
84  /* Ignore invalid keycodes */
85  if (*(const uint8 *)start < 128) return *start;
86  }
87  return 0;
88 }
89 
96 static uint16 ParseKeycode(const char *start, const char *end)
97 {
98  assert(start <= end);
99  uint16 keycode = 0;
100  for (;;) {
101  const char *cur = start;
102  while (*cur != '+' && cur != end) cur++;
103  uint16 code = ParseCode(start, cur);
104  if (code == 0) return 0;
105  if (code & WKC_SPECIAL_KEYS) {
106  /* Some completely wrong keycode we don't support. */
107  if (code & ~WKC_SPECIAL_KEYS) return 0;
108  keycode |= code;
109  } else {
110  /* Ignore the code if it has more then 1 letter. */
111  if (keycode & ~WKC_SPECIAL_KEYS) return 0;
112  keycode |= code;
113  }
114  if (cur == end) break;
115  assert(cur < end);
116  start = cur + 1;
117  }
118  return keycode;
119 }
120 
126 static void ParseHotkeys(Hotkey *hotkey, const char *value)
127 {
128  const char *start = value;
129  while (*start != '\0') {
130  const char *end = start;
131  while (*end != '\0' && *end != ',') end++;
132  uint16 keycode = ParseKeycode(start, end);
133  if (keycode != 0) hotkey->AddKeycode(keycode);
134  start = (*end == ',') ? end + 1: end;
135  }
136 }
137 
147 static const char *KeycodeToString(uint16 keycode)
148 {
149  static char buf[32];
150  buf[0] = '\0';
151  bool first = true;
152  if (keycode & WKC_GLOBAL_HOTKEY) {
153  strecat(buf, "GLOBAL", lastof(buf));
154  first = false;
155  }
156  if (keycode & WKC_SHIFT) {
157  if (!first) strecat(buf, "+", lastof(buf));
158  strecat(buf, "SHIFT", lastof(buf));
159  first = false;
160  }
161  if (keycode & WKC_CTRL) {
162  if (!first) strecat(buf, "+", lastof(buf));
163  strecat(buf, "CTRL", lastof(buf));
164  first = false;
165  }
166  if (keycode & WKC_ALT) {
167  if (!first) strecat(buf, "+", lastof(buf));
168  strecat(buf, "ALT", lastof(buf));
169  first = false;
170  }
171  if (keycode & WKC_META) {
172  if (!first) strecat(buf, "+", lastof(buf));
173  strecat(buf, "META", lastof(buf));
174  first = false;
175  }
176  if (!first) strecat(buf, "+", lastof(buf));
177  keycode = keycode & ~WKC_SPECIAL_KEYS;
178 
179  for (uint i = 0; i < lengthof(_keycode_to_name); i++) {
180  if (_keycode_to_name[i].keycode == keycode) {
181  strecat(buf, _keycode_to_name[i].name, lastof(buf));
182  return buf;
183  }
184  }
185  assert(keycode < 128);
186  char key[2];
187  key[0] = keycode;
188  key[1] = '\0';
189  strecat(buf, key, lastof(buf));
190  return buf;
191 }
192 
201 const char *SaveKeycodes(const Hotkey *hotkey)
202 {
203  static char buf[128];
204  buf[0] = '\0';
205  for (uint i = 0; i < hotkey->keycodes.Length(); i++) {
206  const char *str = KeycodeToString(hotkey->keycodes[i]);
207  if (i > 0) strecat(buf, ",", lastof(buf));
208  strecat(buf, str, lastof(buf));
209  }
210  return buf;
211 }
212 
219 Hotkey::Hotkey(uint16 default_keycode, const char *name, int num) :
220  name(name),
221  num(num)
222 {
223  if (default_keycode != 0) this->AddKeycode(default_keycode);
224 }
225 
232 Hotkey::Hotkey(const uint16 *default_keycodes, const char *name, int num) :
233  name(name),
234  num(num)
235 {
236  const uint16 *keycode = default_keycodes;
237  while (*keycode != 0) {
238  this->AddKeycode(*keycode);
239  keycode++;
240  }
241 }
242 
248 void Hotkey::AddKeycode(uint16 keycode)
249 {
250  this->keycodes.Include(keycode);
251 }
252 
253 HotkeyList::HotkeyList(const char *ini_group, Hotkey *items, GlobalHotkeyHandlerFunc global_hotkey_handler) :
254  global_hotkey_handler(global_hotkey_handler), ini_group(ini_group), items(items)
255 {
256  if (_hotkey_lists == NULL) _hotkey_lists = new SmallVector<HotkeyList*, 16>();
257  *_hotkey_lists->Append() = this;
258 }
259 
260 HotkeyList::~HotkeyList()
261 {
262  _hotkey_lists->Erase(_hotkey_lists->Find(this));
263 }
264 
270 {
271  IniGroup *group = ini->GetGroup(this->ini_group);
272  for (Hotkey *hotkey = this->items; hotkey->name != NULL; ++hotkey) {
273  IniItem *item = group->GetItem(hotkey->name, false);
274  if (item != NULL) {
275  hotkey->keycodes.Clear();
276  if (item->value != NULL) ParseHotkeys(hotkey, item->value);
277  }
278  }
279 }
280 
285 void HotkeyList::Save(IniFile *ini) const
286 {
287  IniGroup *group = ini->GetGroup(this->ini_group);
288  for (const Hotkey *hotkey = this->items; hotkey->name != NULL; ++hotkey) {
289  IniItem *item = group->GetItem(hotkey->name, true);
290  item->SetValue(SaveKeycodes(hotkey));
291  }
292 }
293 
300 int HotkeyList::CheckMatch(uint16 keycode, bool global_only) const
301 {
302  for (const Hotkey *list = this->items; list->name != NULL; ++list) {
303  if (list->keycodes.Contains(keycode | WKC_GLOBAL_HOTKEY) || (!global_only && list->keycodes.Contains(keycode))) {
304  return list->num;
305  }
306  }
307  return -1;
308 }
309 
310 
311 static void SaveLoadHotkeys(bool save)
312 {
313  IniFile *ini = new IniFile();
314  ini->LoadFromDisk(_hotkeys_file, NO_DIRECTORY);
315 
316  for (HotkeyList **list = _hotkey_lists->Begin(); list != _hotkey_lists->End(); ++list) {
317  if (save) {
318  (*list)->Save(ini);
319  } else {
320  (*list)->Load(ini);
321  }
322  }
323 
324  if (save) ini->SaveToDisk(_hotkeys_file);
325  delete ini;
326 }
327 
328 
331 {
332  SaveLoadHotkeys(false);
333 }
334 
337 {
338  SaveLoadHotkeys(true);
339 }
340 
341 void HandleGlobalHotkeys(WChar key, uint16 keycode)
342 {
343  for (HotkeyList **list = _hotkey_lists->Begin(); list != _hotkey_lists->End(); ++list) {
344  if ((*list)->global_hotkey_handler == NULL) continue;
345 
346  int hotkey = (*list)->CheckMatch(keycode, true);
347  if (hotkey >= 0 && ((*list)->global_hotkey_handler(hotkey) == ES_HANDLED)) return;
348  }
349 }
350 
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:219
, 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:126
Functions related to low-level strings.
void Load(IniFile *ini)
Load HotkeyList from IniFile.
Definition: hotkeys.cpp:269
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:96
uint Length() const
Get the number of items in the list.
void Save(IniFile *ini) const
Save HotkeyList to IniFile.
Definition: hotkeys.cpp:285
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:248
Types related to reading/writing &#39;*.ini&#39; files.
void LoadHotkeysFromConfig()
Load the hotkeys from the config file.
Definition: hotkeys.cpp:330
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:72
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:336
int CheckMatch(uint16 keycode, bool global_only=false) const
Check if a keycode is bound to something.
Definition: hotkeys.cpp:300
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:147
The passed event is handled.
Definition: window_type.h:702
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:201
Fake keycode bit to indicate global hotkeys.
Definition: gfx_type.h:35