Move Hedgewars key IDs to CSV file
authorWuzzy <Wuzzy2@mail.ru>
Sat, 27 Apr 2019 13:51:02 +0200
changeset 14844 9e8c201eae9d
parent 14843 9f6b650a6812
child 14845 6c51f74d80a8
Move Hedgewars key IDs to CSV file
hedgewars/CMakeLists.txt
hedgewars/uKeyNames.pas
hedgewars/uTypes.pas
hedgewars/uVariables.pas
share/hedgewars/Data/misc/CMakeLists.txt
share/hedgewars/Data/misc/keys.csv
--- a/hedgewars/CMakeLists.txt	Sat Apr 27 12:27:08 2019 +0200
+++ b/hedgewars/CMakeLists.txt	Sat Apr 27 13:51:02 2019 +0200
@@ -52,6 +52,7 @@
     uConsole.pas
     uCommands.pas
     uDebug.pas
+    uKeyNames.pas
     uInputHandler.pas
     uTextures.pas
     uRenderUtils.pas
--- a/hedgewars/uKeyNames.pas	Sat Apr 27 12:27:08 2019 +0200
+++ b/hedgewars/uKeyNames.pas	Sat Apr 27 13:51:02 2019 +0200
@@ -27,238 +27,61 @@
 
 implementation
 
+uses uPhysFSLayer, uUtils, uVariables, uTypes, uConsole;
+
 procedure populateKeyNames(var KeyArray: TKeyNames);
+var f: PfsFile;
+    l, keyname, tmp: shortstring;
+    i, scancode: LongInt;
 begin
 (*
- This is a mapping from SDL scancodes to Hedgewars key identifiers.
+ KeyArray is a mapping from SDL scancodes to Hedgewars key identifiers.
  Hedgewars key identifiers are strings with a maximum length of 15
  and are used internally to identify keys in the engine and in settings.ini.
 *)
-    KeyArray[4] := 'a';
-    KeyArray[5] := 'b';
-    KeyArray[6] := 'c';
-    KeyArray[7] := 'd';
-    KeyArray[8] := 'e';
-    KeyArray[9] := 'f';
-    KeyArray[10] := 'g';
-    KeyArray[11] := 'h';
-    KeyArray[12] := 'i';
-    KeyArray[13] := 'j';
-    KeyArray[14] := 'k';
-    KeyArray[15] := 'l';
-    KeyArray[16] := 'm';
-    KeyArray[17] := 'n';
-    KeyArray[18] := 'o';
-    KeyArray[19] := 'p';
-    KeyArray[20] := 'q';
-    KeyArray[21] := 'r';
-    KeyArray[22] := 's';
-    KeyArray[23] := 't';
-    KeyArray[24] := 'u';
-    KeyArray[25] := 'v';
-    KeyArray[26] := 'w';
-    KeyArray[27] := 'x';
-    KeyArray[28] := 'y';
-    KeyArray[29] := 'z';
-    KeyArray[30] := '1';
-    KeyArray[31] := '2';
-    KeyArray[32] := '3';
-    KeyArray[33] := '4';
-    KeyArray[34] := '5';
-    KeyArray[35] := '6';
-    KeyArray[36] := '7';
-    KeyArray[37] := '8';
-    KeyArray[38] := '9';
-    KeyArray[39] := '0';
-    KeyArray[40] := 'return';
-    KeyArray[41] := 'escape';
-    KeyArray[42] := 'backspace';
-    KeyArray[43] := 'tab';
-    KeyArray[44] := 'space';
-    KeyArray[45] := '-';
-    KeyArray[46] := '=';
-    KeyArray[47] := '[';
-    KeyArray[48] := ']';
-    KeyArray[49] := '\';
-    KeyArray[50] := '#';
-    KeyArray[51] := ';';
-    KeyArray[52] := '''';
-    KeyArray[53] := '`';
-    KeyArray[54] := ',';
-    KeyArray[55] := '.';
-    KeyArray[56] := '/';
-    KeyArray[57] := 'capslock';
-    KeyArray[58] := 'f1';
-    KeyArray[59] := 'f2';
-    KeyArray[60] := 'f3';
-    KeyArray[61] := 'f4';
-    KeyArray[62] := 'f5';
-    KeyArray[63] := 'f6';
-    KeyArray[64] := 'f7';
-    KeyArray[65] := 'f8';
-    KeyArray[66] := 'f9';
-    KeyArray[67] := 'f10';
-    KeyArray[68] := 'f11';
-    KeyArray[69] := 'f12';
-    KeyArray[70] := 'printscreen';
-    KeyArray[71] := 'scrolllock';
-    KeyArray[72] := 'pause';
-    KeyArray[73] := 'insert';
-    KeyArray[74] := 'home';
-    KeyArray[75] := 'pageup';
-    KeyArray[76] := 'delete';
-    KeyArray[77] := 'end';
-    KeyArray[78] := 'pagedown';
-    KeyArray[79] := 'right';
-    KeyArray[80] := 'left';
-    KeyArray[81] := 'down';
-    KeyArray[82] := 'up';
-    KeyArray[83] := 'numlock';
-    KeyArray[84] := 'keypad_/';
-    KeyArray[85] := 'keypad_*';
-    KeyArray[86] := 'keypad_-';
-    KeyArray[87] := 'keypad_+';
-    KeyArray[88] := 'keypad_enter';
-    KeyArray[89] := 'keypad_1';
-    KeyArray[90] := 'keypad_2';
-    KeyArray[91] := 'keypad_3';
-    KeyArray[92] := 'keypad_4';
-    KeyArray[93] := 'keypad_5';
-    KeyArray[94] := 'keypad_6';
-    KeyArray[95] := 'keypad_7';
-    KeyArray[96] := 'keypad_8';
-    KeyArray[97] := 'keypad_9';
-    KeyArray[98] := 'keypad_0';
-    KeyArray[99] := 'keypad_.';
-    KeyArray[101] := 'menu';
-    KeyArray[102] := 'power';
-    KeyArray[103] := 'keypad_=';
-    KeyArray[104] := 'f13';
-    KeyArray[105] := 'f14';
-    KeyArray[106] := 'f15';
-    KeyArray[107] := 'f16';
-    KeyArray[108] := 'f17';
-    KeyArray[109] := 'f18';
-    KeyArray[110] := 'f19';
-    KeyArray[111] := 'f20';
-    KeyArray[112] := 'f21';
-    KeyArray[113] := 'f22';
-    KeyArray[114] := 'f23';
-    KeyArray[115] := 'f24';
-    KeyArray[116] := 'execute';
-    KeyArray[117] := 'help';
-    KeyArray[118] := 'menu';
-    KeyArray[119] := 'select';
-    KeyArray[120] := 'stop';
-    KeyArray[121] := 'again';
-    KeyArray[122] := 'undo';
-    KeyArray[123] := 'cut';
-    KeyArray[124] := 'copy';
-    KeyArray[125] := 'paste';
-    KeyArray[126] := 'find';
-    KeyArray[127] := 'mute';
-    KeyArray[128] := 'volumeup';
-    KeyArray[129] := 'volumedown';
-    KeyArray[133] := 'keypad_,';
-    KeyArray[134] := 'keypad_=_(as400';
-    KeyArray[153] := 'alterase';
-    KeyArray[154] := 'sysreq';
-    KeyArray[155] := 'cancel';
-    KeyArray[156] := 'clear';
-    KeyArray[157] := 'prior';
-    KeyArray[158] := 'return2';
-    KeyArray[159] := 'separator';
-    KeyArray[160] := 'out';
-    KeyArray[161] := 'oper';
-    KeyArray[162] := 'clear_/_again';
-    KeyArray[163] := 'crsel';
-    KeyArray[164] := 'exsel';
-    KeyArray[176] := 'keypad_00';
-    KeyArray[177] := 'keypad_000';
-    KeyArray[178] := 'thousandssepara';
-    KeyArray[179] := 'decimalseparato';
-    KeyArray[180] := 'currencyunit';
-    KeyArray[181] := 'currencysubunit';
-    KeyArray[182] := 'keypad_(';
-    KeyArray[183] := 'keypad_)';
-    KeyArray[184] := 'keypad_{';
-    KeyArray[185] := 'keypad_}';
-    KeyArray[186] := 'keypad_tab';
-    KeyArray[187] := 'keypad_backspac';
-    KeyArray[188] := 'keypad_a';
-    KeyArray[189] := 'keypad_b';
-    KeyArray[190] := 'keypad_c';
-    KeyArray[191] := 'keypad_d';
-    KeyArray[192] := 'keypad_e';
-    KeyArray[193] := 'keypad_f';
-    KeyArray[194] := 'keypad_xor';
-    KeyArray[195] := 'keypad_^';
-    KeyArray[196] := 'keypad_%';
-    KeyArray[197] := 'keypad_<';
-    KeyArray[198] := 'keypad_>';
-    KeyArray[199] := 'keypad_&';
-    KeyArray[200] := 'keypad_&&';
-    KeyArray[201] := 'keypad_|';
-    KeyArray[202] := 'keypad_||';
-    KeyArray[203] := 'keypad_:';
-    KeyArray[204] := 'keypad_#';
-    KeyArray[205] := 'keypad_space';
-    KeyArray[206] := 'keypad_@';
-    KeyArray[207] := 'keypad_!';
-    KeyArray[208] := 'keypad_memstore';
-    KeyArray[209] := 'keypad_memrecal';
-    KeyArray[210] := 'keypad_memclear';
-    KeyArray[211] := 'keypad_memadd';
-    KeyArray[212] := 'keypad_memsubtr';
-    KeyArray[213] := 'keypad_memmulti';
-    KeyArray[214] := 'keypad_memdivid';
-    KeyArray[215] := 'keypad_+/-';
-    KeyArray[216] := 'keypad_clear';
-    KeyArray[217] := 'keypad_clearent';
-    KeyArray[218] := 'keypad_binary';
-    KeyArray[219] := 'keypad_octal';
-    KeyArray[220] := 'keypad_decimal';
-    KeyArray[221] := 'keypad_hexadeci';
-    KeyArray[224] := 'left_ctrl';
-    KeyArray[225] := 'left_shift';
-    KeyArray[226] := 'left_alt';
-    KeyArray[227] := 'left_gui';
-    KeyArray[228] := 'right_ctrl';
-    KeyArray[229] := 'right_shift';
-    KeyArray[230] := 'right_alt';
-    KeyArray[231] := 'right_gui';
-    KeyArray[257] := 'modeswitch';
-    KeyArray[258] := 'audionext';
-    KeyArray[259] := 'audioprev';
-    KeyArray[260] := 'audiostop';
-    KeyArray[261] := 'audioplay';
-    KeyArray[262] := 'audiomute';
-    KeyArray[263] := 'mediaselect';
-    KeyArray[264] := 'www';
-    KeyArray[265] := 'mail';
-    KeyArray[266] := 'calculator';
-    KeyArray[267] := 'computer';
-    KeyArray[268] := 'ac_search';
-    KeyArray[269] := 'ac_home';
-    KeyArray[270] := 'ac_back';
-    KeyArray[271] := 'ac_forward';
-    KeyArray[272] := 'ac_stop';
-    KeyArray[273] := 'ac_refresh';
-    KeyArray[274] := 'ac_bookmarks';
-    KeyArray[275] := 'brightnessdown';
-    KeyArray[276] := 'brightnessup';
-    KeyArray[277] := 'displayswitch';
-    KeyArray[278] := 'kbdillumtoggle';
-    KeyArray[279] := 'kbdillumdown';
-    KeyArray[280] := 'kbdillumup';
-    KeyArray[281] := 'eject';
-    KeyArray[282] := 'sleep';
-    KeyArray[283] := 'app1';
-    KeyArray[284] := 'app2';
-    KeyArray[285] := 'audiorewind';
-    KeyArray[286] := 'audiofastforwar';
+
+(* Key identifiers are read from an RFC 4180-compliant CSV file.
+- 1st column: SDL scancode
+- 2nd column: Hedgewars key ID *)
+if pfsExists(cPathz[ptMisc]+'/keys.csv') then
+    begin
+    f:= pfsOpenRead(cPathz[ptMisc]+'/keys.csv');
+    l:= '';
+    pfsReadLn(f, l);
+    while (not pfsEOF(f)) and (l <> '') do
+        begin
+        tmp:= '';
+        i:= 1;
+        while (i <= length(l)) and (l[i] <> ',') do
+            begin
+            tmp:= tmp + l[i];
+            inc(i)
+            end;
+        scancode:= StrToInt(tmp);
 
-    // generateKeyNames(); // DEBUG (see below)
+        if i < length(l) then
+            begin
+            keyname:= copy(l, i + 1, length(l) - i);
+            if (keyname[1] = '"') and (keyname[length(keyname)] = '"') then
+                keyname:= copy(keyname, 2, length(keyname) - 2)
+            else
+                keyname:= copy(keyname, 1, length(keyname) - 1);
+            end;
+
+        pfsReadLn(f, l);
+        KeyArray[scancode]:= keyname;
+        end;
+
+    pfsClose(f)
+    end
+else
+    begin
+    WriteLnToConsole('misc/keys.csv file not found');
+    AddFileLog('misc/keys.csv file not found');
+    halt(haltStartupError);
+    end;
+
+// generateKeyNames(); // DEBUG (see below)
 end;
 
 (*
--- a/hedgewars/uTypes.pas	Sat Apr 27 12:27:08 2019 +0200
+++ b/hedgewars/uTypes.pas	Sat Apr 27 13:51:02 2019 +0200
@@ -45,7 +45,7 @@
     TPathType = (ptNone, ptData, ptGraphics, ptThemes, ptCurrTheme, ptConfig, ptTeams, ptMaps,
             ptMapCurrent, ptDemos, ptSounds, ptGraves, ptFonts, ptForts, ptLocale,
             ptAmmoMenu, ptHedgehog, ptVoices, ptHats, ptFlags, ptMissionMaps,
-            ptSuddenDeath, ptButtons, ptShaders, ptDefaultVoice);
+            ptSuddenDeath, ptButtons, ptShaders, ptDefaultVoice, ptMisc);
 
     // Available sprites for displaying stuff
     TSprite = (sprWater, sprCloud, sprBomb, sprBigDigit, sprBigDigitGray, sprBigDigitGreen,
--- a/hedgewars/uVariables.pas	Sat Apr 27 12:27:08 2019 +0200
+++ b/hedgewars/uVariables.pas	Sat Apr 27 13:51:02 2019 +0200
@@ -331,7 +331,8 @@
         '/Graphics/SuddenDeath',         // ptSuddenDeath
         '/Graphics/Buttons',             // ptButton
         '/Shaders',                      // ptShaders
-        '/Sounds/voices/Default'         // ptDefaultVoice
+        '/Sounds/voices/Default',        // ptDefaultVoice
+        '/misc'                          // ptMisc
     );
 
 var
--- a/share/hedgewars/Data/misc/CMakeLists.txt	Sat Apr 27 12:27:08 2019 +0200
+++ b/share/hedgewars/Data/misc/CMakeLists.txt	Sat Apr 27 13:51:02 2019 +0200
@@ -2,7 +2,7 @@
 if(UNIX AND NOT APPLE)
     configure_file(hwengine.desktop.in hwengine.desktop)
 
-    file(GLOB miscfiles *.svg *.xml *.desktop)
+    file(GLOB miscfiles *.svg *.xml *.desktop *.csv)
 
     install(FILES ${miscfiles} DESTINATION ${SHAREPATH}/Data/misc)
     install(FILES hedgewars.xpm DESTINATION ${CMAKE_INSTALL_PREFIX}/share/pixmaps)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/share/hedgewars/Data/misc/keys.csv	Sat Apr 27 13:51:02 2019 +0200
@@ -0,0 +1,223 @@
+4,"a"
+5,"b"
+6,"c"
+7,"d"
+8,"e"
+9,"f"
+10,"g"
+11,"h"
+12,"i"
+13,"j"
+14,"k"
+15,"l"
+16,"m"
+17,"n"
+18,"o"
+19,"p"
+20,"q"
+21,"r"
+22,"s"
+23,"t"
+24,"u"
+25,"v"
+26,"w"
+27,"x"
+28,"y"
+29,"z"
+30,"1"
+31,"2"
+32,"3"
+33,"4"
+34,"5"
+35,"6"
+36,"7"
+37,"8"
+38,"9"
+39,"0"
+40,"return"
+41,"escape"
+42,"backspace"
+43,"tab"
+44,"space"
+45,"-"
+46,"="
+47,"["
+48,"]"
+49,"\"
+50,"#"
+51,";"
+52,"'"
+53,"`"
+54,","
+55,"."
+56,"/"
+57,"capslock"
+58,"f1"
+59,"f2"
+60,"f3"
+61,"f4"
+62,"f5"
+63,"f6"
+64,"f7"
+65,"f8"
+66,"f9"
+67,"f10"
+68,"f11"
+69,"f12"
+70,"printscreen"
+71,"scrolllock"
+72,"pause"
+73,"insert"
+74,"home"
+75,"pageup"
+76,"delete"
+77,"end"
+78,"pagedown"
+79,"right"
+80,"left"
+81,"down"
+82,"up"
+83,"numlock"
+84,"keypad_/"
+85,"keypad_*"
+86,"keypad_-"
+87,"keypad_+"
+88,"keypad_enter"
+89,"keypad_1"
+90,"keypad_2"
+91,"keypad_3"
+92,"keypad_4"
+93,"keypad_5"
+94,"keypad_6"
+95,"keypad_7"
+96,"keypad_8"
+97,"keypad_9"
+98,"keypad_0"
+99,"keypad_."
+101,"menu"
+102,"power"
+103,"keypad_="
+104,"f13"
+105,"f14"
+106,"f15"
+107,"f16"
+108,"f17"
+109,"f18"
+110,"f19"
+111,"f20"
+112,"f21"
+113,"f22"
+114,"f23"
+115,"f24"
+116,"execute"
+117,"help"
+118,"menu"
+119,"select"
+120,"stop"
+121,"again"
+122,"undo"
+123,"cut"
+124,"copy"
+125,"paste"
+126,"find"
+127,"mute"
+128,"volumeup"
+129,"volumedown"
+133,"keypad_,"
+134,"keypad_=_(as400"
+153,"alterase"
+154,"sysreq"
+155,"cancel"
+156,"clear"
+157,"prior"
+158,"return2"
+159,"separator"
+160,"out"
+161,"oper"
+162,"clear_/_again"
+163,"crsel"
+164,"exsel"
+176,"keypad_00"
+177,"keypad_000"
+178,"thousandssepara"
+179,"decimalseparato"
+180,"currencyunit"
+181,"currencysubunit"
+182,"keypad_("
+183,"keypad_)"
+184,"keypad_{"
+185,"keypad_}"
+186,"keypad_tab"
+187,"keypad_backspac"
+188,"keypad_a"
+189,"keypad_b"
+190,"keypad_c"
+191,"keypad_d"
+192,"keypad_e"
+193,"keypad_f"
+194,"keypad_xor"
+195,"keypad_^"
+196,"keypad_%"
+197,"keypad_<"
+198,"keypad_>"
+199,"keypad_&"
+200,"keypad_&&"
+201,"keypad_|"
+202,"keypad_||"
+203,"keypad_:"
+204,"keypad_#"
+205,"keypad_space"
+206,"keypad_@"
+207,"keypad_!"
+208,"keypad_memstore"
+209,"keypad_memrecal"
+210,"keypad_memclear"
+211,"keypad_memadd"
+212,"keypad_memsubtr"
+213,"keypad_memmulti"
+214,"keypad_memdivid"
+215,"keypad_+/-"
+216,"keypad_clear"
+217,"keypad_clearent"
+218,"keypad_binary"
+219,"keypad_octal"
+220,"keypad_decimal"
+221,"keypad_hexadeci"
+224,"left_ctrl"
+225,"left_shift"
+226,"left_alt"
+227,"left_gui"
+228,"right_ctrl"
+229,"right_shift"
+230,"right_alt"
+231,"right_gui"
+257,"modeswitch"
+258,"audionext"
+259,"audioprev"
+260,"audiostop"
+261,"audioplay"
+262,"audiomute"
+263,"mediaselect"
+264,"www"
+265,"mail"
+266,"calculator"
+267,"computer"
+268,"ac_search"
+269,"ac_home"
+270,"ac_back"
+271,"ac_forward"
+272,"ac_stop"
+273,"ac_refresh"
+274,"ac_bookmarks"
+275,"brightnessdown"
+276,"brightnessup"
+277,"displayswitch"
+278,"kbdillumtoggle"
+279,"kbdillumdown"
+280,"kbdillumup"
+281,"eject"
+282,"sleep"
+283,"app1"
+284,"app2"
+285,"audiorewind"
+286,"audiofastforwar"
\ No newline at end of file