hotkeys.cpp

Go to the documentation of this file.
00001 /* $Id: hotkeys.cpp 22099 2011-02-18 20:35:40Z 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       /* Some completely wrong keycode we don't support. */
00099       if (code & ~WKC_SPECIAL_KEYS) return 0;
00100       keycode |= code;
00101     } else {
00102       /* Ignore invalid keycodes */
00103       if (code >= 128) {
00104         return 0;
00105       }
00106       /* Ignore the code if it has more then 1 letter. */
00107       if (keycode & ~WKC_SPECIAL_KEYS) return 0;
00108       keycode |= code;
00109     }
00110     if (cur == end) break;
00111     assert(cur < end);
00112     start = cur + 1;
00113   }
00114   return keycode;
00115 }
00116 
00122 template<class T>
00123 static void ParseHotkeys(Hotkey<T> *hotkey, const char *value)
00124 {
00125   const char *start = value;
00126   while (*start != '\0') {
00127     const char *end = start;
00128     while (*end != '\0' && *end != ',') end++;
00129     uint16 keycode = ParseKeycode(start, end);
00130     if (keycode != 0) hotkey->AddKeycode(keycode);
00131     start = (*end == ',') ? end + 1: end;
00132   }
00133 }
00134 
00144 static const char *KeycodeToString(uint16 keycode)
00145 {
00146   static char buf[32];
00147   buf[0] = '\0';
00148   bool first = true;
00149   if (keycode & WKC_GLOBAL_HOTKEY) {
00150     strecat(buf, "GLOBAL", lastof(buf));
00151     first = false;
00152   }
00153   if (keycode & WKC_SHIFT) {
00154     if (!first) strecat(buf, "+", lastof(buf));
00155     strecat(buf, "SHIFT", lastof(buf));
00156     first = false;
00157   }
00158   if (keycode & WKC_CTRL) {
00159     if (!first) strecat(buf, "+", lastof(buf));
00160     strecat(buf, "CTRL", lastof(buf));
00161     first = false;
00162   }
00163   if (keycode & WKC_ALT) {
00164     if (!first) strecat(buf, "+", lastof(buf));
00165     strecat(buf, "ALT", lastof(buf));
00166     first = false;
00167   }
00168   if (keycode & WKC_META) {
00169     if (!first) strecat(buf, "+", lastof(buf));
00170     strecat(buf, "META", lastof(buf));
00171     first = false;
00172   }
00173   if (!first) strecat(buf, "+", lastof(buf));
00174   keycode = keycode & ~WKC_SPECIAL_KEYS;
00175 
00176   for (uint i = 0; i < lengthof(_keycode_to_name); i++) {
00177     if (_keycode_to_name[i].keycode == keycode) {
00178       strecat(buf, _keycode_to_name[i].name, lastof(buf));
00179       return buf;
00180     }
00181   }
00182   assert(keycode < 128);
00183   char key[2];
00184   key[0] = keycode;
00185   key[1] = '\0';
00186   strecat(buf, key, lastof(buf));
00187   return buf;
00188 }
00189 
00198 template<class T>
00199 const char *SaveKeycodes(const Hotkey<T> *hotkey)
00200 {
00201   static char buf[128];
00202   buf[0] = '\0';
00203   for (uint i = 0; i < hotkey->keycodes.Length(); i++) {
00204     const char *str = KeycodeToString(hotkey->keycodes[i]);
00205     if (i > 0) strecat(buf, ",", lastof(buf));
00206     strecat(buf, str, lastof(buf));
00207   }
00208   return buf;
00209 }
00210 
00211 template<class T>
00212 void LoadHotkeyGroup(IniGroup *group, T *hotkey_list)
00213 {
00214   for (uint i = 0; hotkey_list[i].num != -1; i++) {
00215     T *hotkey = &hotkey_list[i];
00216     IniItem *item = group->GetItem(hotkey->name, false);
00217     if (item != NULL) {
00218       hotkey->keycodes.Clear();
00219       if (item->value != NULL) ParseHotkeys(hotkey, item->value);
00220     }
00221   }
00222 }
00223 
00224 template<class T>
00225 void SaveHotkeyGroup(IniGroup *group, T *hotkey_list)
00226 {
00227   for (uint i = 0; hotkey_list[i].num != -1; i++) {
00228     T *hotkey = &hotkey_list[i];
00229     IniItem *item = group->GetItem(hotkey->name, true);
00230     item->SetValue(SaveKeycodes(hotkey));
00231   }
00232 }
00233 
00234 template<class T>
00235 void SaveLoadHotkeyGroup(IniGroup *group, T *hotkey_list, bool save)
00236 {
00237   if (save) {
00238     SaveHotkeyGroup(group, hotkey_list);
00239   } else {
00240     LoadHotkeyGroup(group, hotkey_list);
00241   }
00242 }
00243 
00244 struct MainWindow;
00245 struct MainToolbarWindow;
00246 struct ScenarioEditorToolbarWindow;
00247 struct TerraformToolbarWindow;
00248 struct ScenarioEditorLandscapeGenerationWindow;
00249 struct OrdersWindow;
00250 struct BuildAirToolbarWindow;
00251 struct BuildDocksToolbarWindow;
00252 struct BuildRailToolbarWindow;
00253 struct BuildRoadToolbarWindow;
00254 struct SignListWindow;
00255 
00256 static void SaveLoadHotkeys(bool save)
00257 {
00258   IniFile *ini = new IniFile();
00259   ini->LoadFromDisk(_hotkeys_file);
00260 
00261   IniGroup *group;
00262 
00263 #define SL_HOTKEYS(name, window_name) \
00264   extern Hotkey<window_name> *_##name##_hotkeys;\
00265   group = ini->GetGroup(#name);\
00266   SaveLoadHotkeyGroup(group, _##name##_hotkeys, save);
00267 
00268   SL_HOTKEYS(global, MainWindow);
00269   SL_HOTKEYS(maintoolbar, MainToolbarWindow);
00270   SL_HOTKEYS(scenedit_maintoolbar, ScenarioEditorToolbarWindow);
00271   SL_HOTKEYS(terraform, TerraformToolbarWindow);
00272   SL_HOTKEYS(terraform_editor, ScenarioEditorLandscapeGenerationWindow);
00273   SL_HOTKEYS(order, OrdersWindow);
00274   SL_HOTKEYS(airtoolbar, BuildAirToolbarWindow);
00275   SL_HOTKEYS(dockstoolbar, BuildDocksToolbarWindow);
00276   SL_HOTKEYS(railtoolbar, BuildRailToolbarWindow);
00277   SL_HOTKEYS(roadtoolbar, BuildRoadToolbarWindow);
00278   SL_HOTKEYS(signlist, SignListWindow);
00279 
00280 
00281 #undef SL_HOTKEYS
00282   if (save) ini->SaveToDisk(_hotkeys_file);
00283   delete ini;
00284 }
00285 
00286 
00288 void LoadHotkeysFromConfig()
00289 {
00290   SaveLoadHotkeys(false);
00291 }
00292 
00294 void SaveHotkeysToConfig()
00295 {
00296   SaveLoadHotkeys(true);
00297 }
00298 
00299 typedef EventState GlobalHotkeyHandler(uint16, uint16);
00300 
00301 GlobalHotkeyHandler RailToolbarGlobalHotkeys;
00302 GlobalHotkeyHandler DockToolbarGlobalHotkeys;
00303 GlobalHotkeyHandler AirportToolbarGlobalHotkeys;
00304 GlobalHotkeyHandler TerraformToolbarGlobalHotkeys;
00305 GlobalHotkeyHandler TerraformToolbarEditorGlobalHotkeys;
00306 GlobalHotkeyHandler RoadToolbarGlobalHotkeys;
00307 GlobalHotkeyHandler RoadToolbarEditorGlobalHotkeys;
00308 GlobalHotkeyHandler SignListGlobalHotkeys;
00309 
00310 
00311 GlobalHotkeyHandler *_global_hotkey_handlers[] = {
00312   RailToolbarGlobalHotkeys,
00313   DockToolbarGlobalHotkeys,
00314   AirportToolbarGlobalHotkeys,
00315   TerraformToolbarGlobalHotkeys,
00316   RoadToolbarGlobalHotkeys,
00317   SignListGlobalHotkeys,
00318 };
00319 
00320 GlobalHotkeyHandler *_global_hotkey_handlers_editor[] = {
00321   TerraformToolbarEditorGlobalHotkeys,
00322   RoadToolbarEditorGlobalHotkeys,
00323 };
00324 
00325 
00326 void HandleGlobalHotkeys(uint16 key, uint16 keycode)
00327 {
00328   if (_game_mode == GM_NORMAL) {
00329     for (uint i = 0; i < lengthof(_global_hotkey_handlers); i++) {
00330       if (_global_hotkey_handlers[i](key, keycode) == ES_HANDLED) return;
00331     }
00332   } else if (_game_mode == GM_EDITOR) {
00333     for (uint i = 0; i < lengthof(_global_hotkey_handlers_editor); i++) {
00334       if (_global_hotkey_handlers_editor[i](key, keycode) == ES_HANDLED) return;
00335     }
00336   }
00337 }
00338 

Generated on Fri Feb 18 21:53:39 2011 for OpenTTD by  doxygen 1.6.1