OpenTTD
hotkeys.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "stdafx.h"
11 #include "openttd.h"
12 #include "hotkeys.h"
13 #include "ini_type.h"
14 #include "string_func.h"
15 #include "window_gui.h"
16 
17 #include "safeguards.h"
18 
19 char *_hotkeys_file;
20 
25 static std::vector<HotkeyList*> *_hotkey_lists = nullptr;
26 
28 struct KeycodeNames {
29  const char *name;
31 };
32 
34 static const KeycodeNames _keycode_to_name[] = {
35  {"SHIFT", WKC_SHIFT},
36  {"CTRL", WKC_CTRL},
37  {"ALT", WKC_ALT},
38  {"META", WKC_META},
39  {"GLOBAL", WKC_GLOBAL_HOTKEY},
40  {"ESC", WKC_ESC},
41  {"DEL", WKC_DELETE},
42  {"BACKSPACE", WKC_BACKSPACE},
43  {"RETURN", WKC_RETURN},
44  {"BACKQUOTE", WKC_BACKQUOTE},
45  {"F1", WKC_F1},
46  {"F2", WKC_F2},
47  {"F3", WKC_F3},
48  {"F4", WKC_F4},
49  {"F5", WKC_F5},
50  {"F6", WKC_F6},
51  {"F7", WKC_F7},
52  {"F8", WKC_F8},
53  {"F9", WKC_F9},
54  {"F10", WKC_F10},
55  {"F11", WKC_F11},
56  {"F12", WKC_F12},
57  {"PAUSE", WKC_PAUSE},
58  {"COMMA", WKC_COMMA},
59  {"NUM_PLUS", WKC_NUM_PLUS},
60  {"NUM_MINUS", WKC_NUM_MINUS},
61  {"=", WKC_EQUALS},
62  {"-", WKC_MINUS},
63 };
64 
71 static uint16 ParseCode(const char *start, const char *end)
72 {
73  assert(start <= end);
74  while (start < end && *start == ' ') start++;
75  while (end > start && *end == ' ') end--;
76  for (uint i = 0; i < lengthof(_keycode_to_name); i++) {
77  if (strlen(_keycode_to_name[i].name) == (size_t)(end - start) && strncasecmp(start, _keycode_to_name[i].name, end - start) == 0) {
78  return _keycode_to_name[i].keycode;
79  }
80  }
81  if (end - start == 1) {
82  if (*start >= 'a' && *start <= 'z') return *start - ('a'-'A');
83  /* Ignore invalid keycodes */
84  if (*(const uint8 *)start < 128) return *start;
85  }
86  return 0;
87 }
88 
95 static uint16 ParseKeycode(const char *start, const char *end)
96 {
97  assert(start <= end);
98  uint16 keycode = 0;
99  for (;;) {
100  const char *cur = start;
101  while (*cur != '+' && cur != end) cur++;
102  uint16 code = ParseCode(start, cur);
103  if (code == 0) return 0;
104  if (code & WKC_SPECIAL_KEYS) {
105  /* Some completely wrong keycode we don't support. */
106  if (code & ~WKC_SPECIAL_KEYS) return 0;
107  keycode |= code;
108  } else {
109  /* Ignore the code if it has more then 1 letter. */
110  if (keycode & ~WKC_SPECIAL_KEYS) return 0;
111  keycode |= code;
112  }
113  if (cur == end) break;
114  assert(cur < end);
115  start = cur + 1;
116  }
117  return keycode;
118 }
119 
125 static void ParseHotkeys(Hotkey *hotkey, const char *value)
126 {
127  const char *start = value;
128  while (*start != '\0') {
129  const char *end = start;
130  while (*end != '\0' && *end != ',') end++;
131  uint16 keycode = ParseKeycode(start, end);
132  if (keycode != 0) hotkey->AddKeycode(keycode);
133  start = (*end == ',') ? end + 1: end;
134  }
135 }
136 
146 static const char *KeycodeToString(uint16 keycode)
147 {
148  static char buf[32];
149  buf[0] = '\0';
150  bool first = true;
151  if (keycode & WKC_GLOBAL_HOTKEY) {
152  strecat(buf, "GLOBAL", lastof(buf));
153  first = false;
154  }
155  if (keycode & WKC_SHIFT) {
156  if (!first) strecat(buf, "+", lastof(buf));
157  strecat(buf, "SHIFT", lastof(buf));
158  first = false;
159  }
160  if (keycode & WKC_CTRL) {
161  if (!first) strecat(buf, "+", lastof(buf));
162  strecat(buf, "CTRL", lastof(buf));
163  first = false;
164  }
165  if (keycode & WKC_ALT) {
166  if (!first) strecat(buf, "+", lastof(buf));
167  strecat(buf, "ALT", lastof(buf));
168  first = false;
169  }
170  if (keycode & WKC_META) {
171  if (!first) strecat(buf, "+", lastof(buf));
172  strecat(buf, "META", lastof(buf));
173  first = false;
174  }
175  if (!first) strecat(buf, "+", lastof(buf));
176  keycode = keycode & ~WKC_SPECIAL_KEYS;
177 
178  for (uint i = 0; i < lengthof(_keycode_to_name); i++) {
179  if (_keycode_to_name[i].keycode == keycode) {
180  strecat(buf, _keycode_to_name[i].name, lastof(buf));
181  return buf;
182  }
183  }
184  assert(keycode < 128);
185  char key[2];
186  key[0] = keycode;
187  key[1] = '\0';
188  strecat(buf, key, lastof(buf));
189  return buf;
190 }
191 
200 const char *SaveKeycodes(const Hotkey *hotkey)
201 {
202  static char buf[128];
203  buf[0] = '\0';
204  for (uint i = 0; i < hotkey->keycodes.size(); i++) {
205  const char *str = KeycodeToString(hotkey->keycodes[i]);
206  if (i > 0) strecat(buf, ",", lastof(buf));
207  strecat(buf, str, lastof(buf));
208  }
209  return buf;
210 }
211 
218 Hotkey::Hotkey(uint16 default_keycode, const char *name, int num) :
219  name(name),
220  num(num)
221 {
222  if (default_keycode != 0) this->AddKeycode(default_keycode);
223 }
224 
231 Hotkey::Hotkey(const uint16 *default_keycodes, const char *name, int num) :
232  name(name),
233  num(num)
234 {
235  const uint16 *keycode = default_keycodes;
236  while (*keycode != 0) {
237  this->AddKeycode(*keycode);
238  keycode++;
239  }
240 }
241 
247 void Hotkey::AddKeycode(uint16 keycode)
248 {
249  include(this->keycodes, keycode);
250 }
251 
252 HotkeyList::HotkeyList(const char *ini_group, Hotkey *items, GlobalHotkeyHandlerFunc global_hotkey_handler) :
253  global_hotkey_handler(global_hotkey_handler), ini_group(ini_group), items(items)
254 {
255  if (_hotkey_lists == nullptr) _hotkey_lists = new std::vector<HotkeyList*>();
256  _hotkey_lists->push_back(this);
257 }
258 
259 HotkeyList::~HotkeyList()
260 {
261  _hotkey_lists->erase(std::find(_hotkey_lists->begin(), _hotkey_lists->end(), this));
262 }
263 
269 {
270  IniGroup *group = ini->GetGroup(this->ini_group);
271  for (Hotkey *hotkey = this->items; hotkey->name != nullptr; ++hotkey) {
272  IniItem *item = group->GetItem(hotkey->name, false);
273  if (item != nullptr) {
274  hotkey->keycodes.clear();
275  if (item->value != nullptr) ParseHotkeys(hotkey, item->value);
276  }
277  }
278 }
279 
284 void HotkeyList::Save(IniFile *ini) const
285 {
286  IniGroup *group = ini->GetGroup(this->ini_group);
287  for (const Hotkey *hotkey = this->items; hotkey->name != nullptr; ++hotkey) {
288  IniItem *item = group->GetItem(hotkey->name, true);
289  item->SetValue(SaveKeycodes(hotkey));
290  }
291 }
292 
299 int HotkeyList::CheckMatch(uint16 keycode, bool global_only) const
300 {
301  for (const Hotkey *list = this->items; list->name != nullptr; ++list) {
302  auto begin = list->keycodes.begin();
303  auto end = list->keycodes.end();
304  if (std::find(begin, end, keycode | WKC_GLOBAL_HOTKEY) != end || (!global_only && std::find(begin, end, keycode) != end)) {
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) {
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) {
345  if (list->global_hotkey_handler == nullptr) 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:29
A group within an ini file.
Definition: ini_type.h:36
static char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: depend.cpp:97
WindowKeyCodes
Definition: gfx_type.h:27
All data for a single hotkey.
Definition: hotkeys.h:22
Hotkey(uint16 default_keycode, const char *name, int num)
Create a new Hotkey object with a single default keycode.
Definition: hotkeys.cpp:218
, Comma
Definition: gfx_type.h:102
Hotkey related functions.
= Equals
Definition: gfx_type.h:97
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
IniGroup * GetGroup(const char *name, size_t len=0, bool create_new=true)
Get the group with the given name.
Definition: ini_load.cpp:154
Functions, definitions and such used only by the GUI.
A single "line" in an ini file.
Definition: ini_type.h:23
static void ParseHotkeys(Hotkey *hotkey, const char *value)
Parse a string to the keycodes it represents.
Definition: hotkeys.cpp:125
Functions related to low-level strings.
void Load(IniFile *ini)
Load HotkeyList from IniFile.
Definition: hotkeys.cpp:268
WindowKeyCodes keycode
The keycode.
Definition: hotkeys.cpp:30
static uint16 ParseKeycode(const char *start, const char *end)
Parse a string representation of a keycode.
Definition: hotkeys.cpp:95
void Save(IniFile *ini) const
Save HotkeyList to IniFile.
Definition: hotkeys.cpp:284
bool SaveToDisk(const char *filename)
Save the Ini file&#39;s data to the disk.
Definition: ini.cpp:41
void SetValue(const char *value)
Replace the current value with another value.
Definition: ini_load.cpp:47
void LoadFromDisk(const char *filename, Subdirectory subdir)
Load the Ini file&#39;s data from the disk.
Definition: ini_load.cpp:210
A path without any base directory.
Definition: fileio_type.h:125
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:40
char * value
The value of this item.
Definition: ini_type.h:26
Some generic types.
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:40
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:247
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:86
bool include(std::vector< T > &vec, const T &item)
Helper function to append an item to a vector if it is not already contained Consider using std::set...
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:103
static uint16 ParseCode(const char *start, const char *end)
Try to parse a single part of a keycode.
Definition: hotkeys.cpp:71
static const KeycodeNames _keycode_to_name[]
Array of non-standard keycodes that can be used in the hotkeys config file.
Definition: hotkeys.cpp:34
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:299
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:146
The passed event is handled.
Definition: window_type.h:712
String representation of a keycode.
Definition: hotkeys.cpp:28
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:200
Fake keycode bit to indicate global hotkeys.
Definition: gfx_type.h:33
static std::vector< HotkeyList * > * _hotkey_lists
List of all HotkeyLists.
Definition: hotkeys.cpp:25