[NTDLL]
[reactos.git] / base / applications / network / telnet / src / keytrans.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 //Telnet Win32 : an ANSI telnet client.
3 //Copyright (C) 1998-2000 Paul Brannan
4 //Copyright (C) 1998 I.Ioannou
5 //Copyright (C) 1997 Brad Johnson
6 //
7 //This program is free software; you can redistribute it and/or
8 //modify it under the terms of the GNU General Public License
9 //as published by the Free Software Foundation; either version 2
10 //of the License, or (at your option) any later version.
11 //
12 //This program is distributed in the hope that it will be useful,
13 //but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 //GNU General Public License for more details.
16 //
17 //You should have received a copy of the GNU General Public License
18 //along with this program; if not, write to the Free Software
19 //Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 //I.Ioannou
22 //roryt@hol.gr
23 //
24 ///////////////////////////////////////////////////////////////////////////
25
26 ///////////////////////////////////////////////////////////////////
27 // Key translations - I.Ioannou (roryt@hol.gr) //
28 // Athens - Greece December 18, 1996 02:56am //
29 // Reads a .cfg file and keeps the definitions //
30 // modified for alternate keymap swiching //
31 // by Andrey V. Smilianets (smile@head.aval.kiev.ua) //
32 // Kiev - Ukraine, December 1997. //
33 // modified to work with MSVC and the Standard Template //
34 // library by Paul Brannan <pbranna@clemson.edu>, //
35 // May 25, 1998 //
36 // updated June 7, 1998 by Paul Brannan to remove cout and //
37 // cerr statements //
38 // APP_KEY and APP2_Key added July 12, 1998 by Paul Brannan //
39 ///////////////////////////////////////////////////////////////////
40 // class KeyTranslator //
41 // Load : loads or replaces the keymap //
42 // TranslateKey : returns a char * to the key def //
43 // AddKeyDef : Changes or adds the key translation //
44 // DeleteKeyDef : Deletes a key def from the list //
45 ///////////////////////////////////////////////////////////////////
46
47 #include <windows.h>
48
49 // changed to make work with VC++ (Paul Brannan 5/25/98)
50 // FIX ME !!! Ioannou: This must be __BORLANDC__ && VERSION < 5
51 // but what is the directive for Borland version ????
52 // FIXED Sept. 31, 2000 (Bernard Badger)
53 //
54 #if defined(__BORLANDC__) && (__BORLANDC < 0x0500)
55 #include <mem.h>
56 #else
57 #include <memory.h>
58 #endif
59
60 #include "keytrans.h"
61 #include "tnerror.h"
62
63 /////////////////////////////////////////////////////////////
64 // class KeyTranslator //
65 // Load : loads or replaces the keymap //
66 // TranslateKey : returns a sz to the key def //
67 // AddKeyDef : Changes or adds the key translation //
68 // DeleteKeyDef : Deletes a key def from the list //
69 /////////////////////////////////////////////////////////////
70
71
72 KeyTranslator::KeyTranslator():
73 mapArray(0,0,sizeof(KeyMap)),
74 globals(0,0,sizeof(TKeyDef)) {
75 ext_mode = 0; // Paul Brannan 8/28/98
76 currentKeyMap = mainKeyMap = -1;
77 };
78
79 //AVS
80 // perform keymap switching
81 int KeyTranslator::switchMap(TKeyDef& tk) {
82 if ( mapArray.IsEmpty() ) {
83 return currentKeyMap = -1;
84 };
85 int i = mapArray.Find(KeyMap(tk));
86 if ( i != INT_MAX ) {
87 if (currentKeyMap == i)
88 currentKeyMap = mainKeyMap; // restore to default
89 else currentKeyMap = i;
90 return 1;
91 };
92 return 0;
93 };
94
95 // Let the calling function interpret the error code (Paul Brannan 12/17/98)
96 int KeyTranslator::SwitchTo(int to) {
97
98 int max = mapArray.GetItemsInContainer();
99 if (max == 0) return -1;
100 if (to < 0 || to > (max-1)) return 0;
101
102 currentKeyMap = to;
103 return 1;
104 };
105
106 //AVS
107 // rewrited to support multiple keymaps
108 const char *KeyTranslator::TranslateKey(WORD wVirtualKeyCode,
109 DWORD dwControlKeyState)
110 {
111 if ( mapArray.IsEmpty() ) return NULL;
112
113 TKeyDef ask(NULL, dwControlKeyState, wVirtualKeyCode);
114
115 // if a keymap switch pressed
116 if ( switchMap(ask) > 0 ) return "";
117
118 int i = mapArray[currentKeyMap].map.Find(ask);
119
120 if ( i != INT_MAX) return mapArray[currentKeyMap].map[i].GetszKey();
121
122 // if not found in current keymap
123 if ( currentKeyMap != mainKeyMap ) {
124 i = mapArray[mainKeyMap].map.Find(ask);
125 if ( i != INT_MAX) return mapArray[mainKeyMap].map[i].GetszKey();
126 };
127 return NULL;
128 };
129
130
131 //AVS
132 // rewrited to support multiple keymaps
133 int KeyTranslator::AddKeyDef(WORD wVirtualKeyCode, DWORD dwControlKeyState,
134 char*lpzKeyDef)
135 {
136 if ( ! mapArray[currentKeyMap].map.IsEmpty() ) {
137 int i = mapArray[currentKeyMap].map.Find(TKeyDef(NULL, dwControlKeyState, wVirtualKeyCode));
138 if ( i != INT_MAX) {
139 mapArray[currentKeyMap].map[i] = lpzKeyDef;
140 return 1;
141 }
142 };
143 return mapArray[currentKeyMap].map.Add( TKeyDef(lpzKeyDef, dwControlKeyState, wVirtualKeyCode));
144 }
145
146 // Paul Brannan Feb. 22, 1999
147 int KeyTranslator::AddKeyDef(WORD wVirtualKeyCode, DWORD dwControlKeyState,
148 tn_ops the_op)
149 {
150 optype op;
151 op.sendstr = 0;
152 op.the_op = the_op;
153 if ( ! mapArray[currentKeyMap].map.IsEmpty() ) {
154 int i = mapArray[currentKeyMap].map.Find(TKeyDef(NULL, dwControlKeyState, wVirtualKeyCode));
155 if ( i != INT_MAX) {
156 mapArray[currentKeyMap].map[i] = op;
157 return 1;
158 }
159 };
160 return mapArray[currentKeyMap].map.Add( TKeyDef(op, dwControlKeyState, wVirtualKeyCode));
161 }
162
163 // AVS
164 int KeyTranslator::LookOnGlobal(char* vkey) {
165 if ( ! globals.IsEmpty() ) {
166 int max = globals.GetItemsInContainer();
167 for ( int i = 0; i < max ; i++ )
168 if ( stricmp(globals[i].GetszKey(), vkey) == 0 )
169 return i;
170 };
171 return INT_MAX;
172 };
173
174 int KeyTranslator::AddGlobalDef(WORD wVirtualKeyCode, char*lpzKeyDef) {
175 if ( ! globals.IsEmpty() ) {
176 int max = globals.GetItemsInContainer();
177 for ( int i = 0; i < max ; i++ ) {
178 const char *s = globals[i].GetszKey();
179 if ( stricmp(s, lpzKeyDef) == 0 ) {
180 globals[i] = DWORD(wVirtualKeyCode);
181 return 1;
182 }
183 }
184 }
185 return globals.Add( TKeyDef(lpzKeyDef, 0, wVirtualKeyCode));
186 }
187
188
189 //AVS
190 // rewrited to support multiple keymaps
191 int KeyTranslator::DeleteKeyDef(WORD wVirtualKeyCode, DWORD dwControlKeyState)
192 {
193 if ( mapArray.IsEmpty() || mapArray[currentKeyMap].map.IsEmpty() )
194 return 0;
195
196 int i = mapArray[currentKeyMap].map.Find(TKeyDef(NULL, dwControlKeyState, wVirtualKeyCode));
197
198 if ( i != INT_MAX) {
199 mapArray[currentKeyMap].map.Destroy(i);
200 return 1;
201 };
202 return 0;
203 };
204
205 //AVS
206 // rewritten to support multiple keymaps
207 void KeyTranslator::DeleteAllDefs(void)
208 {
209 // This code wants to crash under the STL; Apparently the Destroy()
210 // function actually deletes the entry, rather than simply releasing
211 // memory. I think flush() should do the same thing, at least the
212 // way it is written with STL_BIDS (Paul Brannan 5/25/98).
213 int max;
214
215 max = mapArray.GetItemsInContainer();
216 if ( ! mapArray.IsEmpty() ) {
217 for ( int i = 0; i < max; i++ ) {
218 if ( !mapArray[i].map.IsEmpty() ) {
219 mapArray[i].map.Flush();
220 };
221 };
222 };
223 globals.Flush();
224 mapArray.Flush();
225 currentKeyMap = -1;
226 mainKeyMap = -1;
227 };