hotkeys.cpp

Go to the documentation of this file.
00001 /* $Id: hotkeys.cpp 20632 2010-08-26 22:01:16Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "openttd.h"
00014 #include "hotkeys.h"
00015 #include "ini_type.h"
00016 #include "string_func.h"
00017 #include "window_gui.h"
00018 
00019 char *_hotkeys_file;
00020 
00022 struct KeycodeNames {
00023   const char *name;       
00024   WindowKeyCodes keycode; 
00025 };
00026 
00028 static const KeycodeNames _keycode_to_name[] = {
00029   {"SHIFT", WKC_SHIFT},
00030   {"CTRL", WKC_CTRL},
00031   {"ALT", WKC_ALT},
00032   {"META", WKC_META},
00033   {"GLOBAL", WKC_GLOBAL_HOTKEY},
00034   {"ESC", WKC_ESC},
00035   {"DEL", WKC_DELETE},
00036   {"RETURN", WKC_RETURN},
00037   {"BACKQUOTE", WKC_BACKQUOTE},
00038   {"F1", WKC_F1},
00039   {"F2", WKC_F2},
00040   {"F3", WKC_F3},
00041   {"F4", WKC_F4},
00042   {"F5", WKC_F5},
00043   {"F6", WKC_F6},
00044   {"F7", WKC_F7},
00045   {"F8", WKC_F8},
00046   {"F9", WKC_F9},
00047   {"F10", WKC_F10},
00048   {"F11", WKC_F11},
00049   {"F12", WKC_F12},
00050   {"PAUSE", WKC_PAUSE},
00051   {"PLUS", (WindowKeyCodes)'+'},
00052   {"COMMA", (WindowKeyCodes)','},
00053   {"NUM_PLUS", WKC_NUM_PLUS},
00054   {"NUM_MINUS", WKC_NUM_MINUS},
00055   {"=", WKC_EQUALS},
00056   {"-", WKC_MINUS},
00057 };
00058 
00065 static uint16 ParseCode(const char *start, const char *end)
00066 {
00067   assert(start <= end);
00068   while (start < end && *start == ' ') start++;
00069   while (end > start && *end == ' ') end--;
00070   for (uint i = 0; i < lengthof(_keycode_to_name); i++) {
00071     if (strlen(_keycode_to_name[i].name) == (size_t)(end - start) && strncasecmp(start, _keycode_to_name[i].name, end - start) == 0) {
00072       return _keycode_to_name[i].keycode;
00073     }
00074   }
00075   if (end - start == 1) {
00076     if (*start >= 'a' && *start <= 'z') return *start - ('a' - 'A');
00077     return *start;
00078   }
00079   return 0;
00080 }
00081 
00088 static uint16 ParseKeycode(const char *start, const char *end)
00089 {
00090   assert(start <= end);
00091   uint16 keycode = 0;
00092   while (true) {
00093     const char *cur = start;
00094     while (*cur != '+' && cur != end) cur++;
00095     uint16 code = ParseCode(start, cur);
00096     if (code == 0) return 0;
00097     if (code & WKC_SPECIAL_KEYS) {
00098       keycode |= code;
00099     } else {
00100       /* Ignore the code if it has more then 1 letter. */
00101       if (keycode & ~WKC_SPECIAL_KEYS) return 0;
00102       keycode |= code;
00103     }
00104     if (cur == end) break;
00105     assert(cur < end);
00106     start = cur + 1;
00107   }
00108   return keycode;
00109 }
00110 
00116 template<class T>
00117 static void ParseHotkeys(Hotkey<T> *hotkey, const char *value)
00118 {
00119   const char *start = value;
00120   while (*start != '\0') {
00121     const char *end = start;
00122     while (*end != '\0' && *end != ',') end++;
00123     uint16 keycode = ParseKeycode(start, end);
00124     if (keycode != 0) hotkey->AddKeycode(keycode);
00125     start = (*end == ',') ? end + 1: end;
00126   }
00127 }
00128 
00138 static const char *KeycodeToString(uint16 keycode)
00139 {
00140   static char buf[32];
00141   buf[0] = '\0';
00142   bool first = true;
00143   if (keycode & WKC_GLOBAL_HOTKEY) {
00144     strecat(buf, "GLOBAL", lastof(buf));
00145     first = false;
00146   }
00147   if (keycode & WKC_SHIFT) {
00148     if (!first) strecat(buf, "+", lastof(buf));
00149     strecat(buf, "SHIFT", lastof(buf));
00150     first = false;
00151   }
00152   if (keycode & WKC_CTRL) {
00153     if (!first) strecat(buf, "+", lastof(buf));
00154     strecat(buf, "CTRL", lastof(buf));
00155     first = false;
00156   }
00157   if (keycode & WKC_ALT) {
00158     if (!first) strecat(buf, "+", lastof(buf));
00159     strecat(buf, "ALT", lastof(buf));
00160     first = false;
00161   }
00162   if (keycode & WKC_META) {
00163     if (!first) strecat(buf, "+", lastof(buf));
00164     strecat(buf, "META", lastof(buf));
00165     first = false;
00166   }
00167   if (!first) strecat(buf, "+", lastof(buf));
00168   keycode = keycode & ~WKC_SPECIAL_KEYS;
00169 
00170   for (uint i = 0; i < lengthof(_keycode_to_name); i++) {
00171     if (_keycode_to_name[i].keycode == keycode) {
00172       strecat(buf, _keycode_to_name[i].name, lastof(buf));
00173       return buf;
00174     }
00175   }
00176   assert(keycode < 128);
00177   char key[2];
00178   key[0] = keycode;
00179   key[1] = '\0';
00180   strecat(buf, key, lastof(buf));
00181   return buf;
00182 }
00183 
00192 template<class T>
00193 const char *SaveKeycodes(const Hotkey<T> *hotkey)
00194 {
00195   static char buf[128];
00196   buf[0] = '\0';
00197   for (uint i = 0; i < hotkey->keycodes.Length(); i++) {
00198     const char *str = KeycodeToString(hotkey->keycodes[i]);
00199     if (i > 0) strecat(buf, ",", lastof(buf));
00200     strecat(buf, str, lastof(buf));
00201   }
00202   return buf;
00203 }
00204 
00205 template<class T>
00206 void LoadHotkeyGroup(IniGroup *group, T *hotkey_list)
00207 {
00208   for (uint i = 0; hotkey_list[i].num != -1; i++) {
00209     T *hotkey = &hotkey_list[i];
00210     IniItem *item = group->GetItem(hotkey->name, false);
00211     if (item != NULL) {
00212       hotkey->keycodes.Clear();
00213       if (item->value != NULL) ParseHotkeys(hotkey, item->value);
00214     }
00215   }
00216 }
00217 
00218 template<class T>
00219 void SaveHotkeyGroup(IniGroup *group, T *hotkey_list)
00220 {
00221   for (uint i = 0; hotkey_list[i].num != -1; i++) {
00222     T *hotkey = &hotkey_list[i];
00223     IniItem *item = group->GetItem(hotkey->name, true);
00224     item->SetValue(SaveKeycodes(hotkey));
00225   }
00226 }
00227 
00228 template<class T>
00229 void SaveLoadHotkeyGroup(IniGroup *group, T *hotkey_list, bool save)
00230 {
00231   if (save) {
00232     SaveHotkeyGroup(group, hotkey_list);
00233   } else {
00234     LoadHotkeyGroup(group, hotkey_list);
00235   }
00236 }
00237 
00238 struct MainWindow;
00239 struct MainToolbarWindow;
00240 struct ScenarioEditorToolbarWindow;
00241 struct TerraformToolbarWindow;
00242 struct ScenarioEditorLandscapeGenerationWindow;
00243 struct OrdersWindow;
00244 struct BuildAirToolbarWindow;
00245 struct BuildDocksToolbarWindow;
00246 struct BuildRailToolbarWindow;
00247 struct BuildRoadToolbarWindow;
00248 struct SignListWindow;
00249 
00250 static void SaveLoadHotkeys(bool save)
00251 {
00252   IniFile *ini = new IniFile();
00253   ini->LoadFromDisk(_hotkeys_file);
00254 
00255   IniGroup *group;
00256 
00257 #define SL_HOTKEYS(name, window_name) \
00258   extern Hotkey<window_name> *_##name##_hotkeys;\
00259   group = ini->GetGroup(#name);\
00260   SaveLoadHotkeyGroup(group, _##name##_hotkeys, save);
00261 
00262   SL_HOTKEYS(global, MainWindow);
00263   SL_HOTKEYS(maintoolbar, MainToolbarWindow);
00264   SL_HOTKEYS(scenedit_maintoolbar, ScenarioEditorToolbarWindow);
00265   SL_HOTKEYS(terraform, TerraformToolbarWindow);
00266   SL_HOTKEYS(terraform_editor, ScenarioEditorLandscapeGenerationWindow);
00267   SL_HOTKEYS(order, OrdersWindow);
00268   SL_HOTKEYS(airtoolbar, BuildAirToolbarWindow);
00269   SL_HOTKEYS(dockstoolbar, BuildDocksToolbarWindow);
00270   SL_HOTKEYS(railtoolbar, BuildRailToolbarWindow);
00271   SL_HOTKEYS(roadtoolbar, BuildRoadToolbarWindow);
00272   SL_HOTKEYS(signlist, SignListWindow);
00273 
00274 
00275 #undef SL_HOTKEYS
00276   if (save) ini->SaveToDisk(_hotkeys_file);
00277   delete ini;
00278 }
00279 
00280 
00282 void LoadHotkeysFromConfig()
00283 {
00284   SaveLoadHotkeys(false);
00285 }
00286 
00288 void SaveHotkeysToConfig()
00289 {
00290   SaveLoadHotkeys(true);
00291 }
00292 
00293 typedef EventState GlobalHotkeyHandler(uint16, uint16);
00294 
00295 GlobalHotkeyHandler RailToolbarGlobalHotkeys;
00296 GlobalHotkeyHandler DockToolbarGlobalHotkeys;
00297 GlobalHotkeyHandler AirportToolbarGlobalHotkeys;
00298 GlobalHotkeyHandler TerraformToolbarGlobalHotkeys;
00299 GlobalHotkeyHandler TerraformToolbarEditorGlobalHotkeys;
00300 GlobalHotkeyHandler RoadToolbarGlobalHotkeys;
00301 GlobalHotkeyHandler RoadToolbarEditorGlobalHotkeys;
00302 GlobalHotkeyHandler SignListGlobalHotkeys;
00303 
00304 
00305 GlobalHotkeyHandler *_global_hotkey_handlers[] = {
00306   RailToolbarGlobalHotkeys,
00307   DockToolbarGlobalHotkeys,
00308   AirportToolbarGlobalHotkeys,
00309   TerraformToolbarGlobalHotkeys,
00310   RoadToolbarGlobalHotkeys,
00311   SignListGlobalHotkeys,
00312 };
00313 
00314 GlobalHotkeyHandler *_global_hotkey_handlers_editor[] = {
00315   TerraformToolbarEditorGlobalHotkeys,
00316   RoadToolbarEditorGlobalHotkeys,
00317 };
00318 
00319 
00320 void HandleGlobalHotkeys(uint16 key, uint16 keycode)
00321 {
00322   if (_game_mode == GM_NORMAL) {
00323     for (uint i = 0; i < lengthof(_global_hotkey_handlers); i++) {
00324       if (_global_hotkey_handlers[i](key, keycode) == ES_HANDLED) return;
00325     }
00326   } else if (_game_mode == GM_EDITOR) {
00327     for (uint i = 0; i < lengthof(_global_hotkey_handlers_editor); i++) {
00328       if (_global_hotkey_handlers_editor[i](key, keycode) == ES_HANDLED) return;
00329     }
00330   }
00331 }
00332 

Generated on Thu Jan 20 22:57:34 2011 for OpenTTD by  doxygen 1.6.1