File: SiCodes.cs

package info (click to toggle)
keepass2 2.57%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 14,516 kB
  • sloc: cs: 120,930; xml: 6,271; cpp: 322; sh: 53; makefile: 49
file content (458 lines) | stat: -rw-r--r-- 16,089 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2024 Dominik Reichl <dominik.reichl@t-online.de>

  This program 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; either version 2 of the License, or
  (at your option) any later version.

  This program 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.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using System.Windows.Forms;

namespace KeePass.Util.SendInputExt
{
	internal sealed class SiCode
	{
		public readonly string Code;
		public readonly int VKey;
		public readonly bool? ExtKey = null; // Currently all have default ext state
		public readonly string XKeySym;

		public SiCode(string strCode, int iVKey, string strXKeySym)
		{
			if(string.IsNullOrEmpty(strCode)) { Debug.Assert(false); strCode = " "; }

			this.Code = strCode;
			this.VKey = iVKey;
			this.XKeySym = strXKeySym;
		}

		public SiCode(string strCode, Keys k, string strXKeySym)
		{
			if(string.IsNullOrEmpty(strCode)) { Debug.Assert(false); strCode = " "; }

			this.Code = strCode;
			this.VKey = (int)k;
			this.XKeySym = strXKeySym;
		}
	}

	internal enum SiDiacritic
	{
		None = 0,
		Grave,
		Acute,
		Circumflex,
		Tilde,
		Diaeresis, // Umlaut
		Ring, // Ring above
		Cedilla,
		Macron,
		Breve,
		Ogonek,
		DotAbove,
		Caron,
		AcuteDouble
	}

	internal static class SiCodes
	{
		private static List<SiCode> m_l = null;
		public static List<SiCode> KeyCodes
		{
			get
			{
				if(m_l != null) return m_l;

				List<SiCode> l = new List<SiCode>();

				// XKeySym codes from 'keysymdef.h'

				l.Add(new SiCode("BACKSPACE", Keys.Back, "BackSpace"));
				l.Add(new SiCode("BKSP", Keys.Back, "BackSpace"));
				l.Add(new SiCode("BS", Keys.Back, "BackSpace"));
				l.Add(new SiCode("BREAK", Keys.Cancel, "Cancel")); // See SendKeys class
				l.Add(new SiCode("CAPSLOCK", Keys.CapsLock, "Caps_Lock"));
				l.Add(new SiCode("CLEAR", Keys.Clear, "Clear"));
				l.Add(new SiCode("DEL", Keys.Delete, "Delete"));
				l.Add(new SiCode("DELETE", Keys.Delete, "Delete"));
				l.Add(new SiCode("END", Keys.End, "End"));
				l.Add(new SiCode("ENTER", Keys.Enter, "Return"));
				l.Add(new SiCode("ESC", Keys.Escape, "Escape"));
				l.Add(new SiCode("ESCAPE", Keys.Escape, "Escape"));
				l.Add(new SiCode("HELP", Keys.Help, "Help"));
				l.Add(new SiCode("HOME", Keys.Home, "Home"));
				l.Add(new SiCode("INS", Keys.Insert, "Insert"));
				l.Add(new SiCode("INSERT", Keys.Insert, "Insert"));
				l.Add(new SiCode("NUMLOCK", Keys.NumLock, "Num_Lock"));
				l.Add(new SiCode("PGDN", Keys.PageDown, "Page_Down"));
				l.Add(new SiCode("PGUP", Keys.PageUp, "Page_Up"));
				l.Add(new SiCode("PRTSC", Keys.PrintScreen, "Print"));
				l.Add(new SiCode("SCROLLLOCK", Keys.Scroll, "Scroll_Lock"));
				l.Add(new SiCode("SPACE", Keys.Space, "space"));
				l.Add(new SiCode("TAB", Keys.Tab, "Tab"));

				l.Add(new SiCode("UP", Keys.Up, "Up"));
				l.Add(new SiCode("DOWN", Keys.Down, "Down"));
				l.Add(new SiCode("LEFT", Keys.Left, "Left"));
				l.Add(new SiCode("RIGHT", Keys.Right, "Right"));

				for(int i = 1; i <= 24; ++i)
				{
					string strF = "F" + i.ToString(NumberFormatInfo.InvariantInfo);
					l.Add(new SiCode(strF, (int)Keys.F1 + i - 1, strF));

					Debug.Assert(Enum.IsDefined(typeof(Keys), (int)Keys.F1 + i - 1) &&
						Enum.GetName(typeof(Keys), (int)Keys.F1 + i - 1) == strF);
				}

				l.Add(new SiCode("ADD", Keys.Add, "KP_Add"));
				l.Add(new SiCode("SUBTRACT", Keys.Subtract, "KP_Subtract"));
				l.Add(new SiCode("MULTIPLY", Keys.Multiply, "KP_Multiply"));
				l.Add(new SiCode("DIVIDE", Keys.Divide, "KP_Divide"));

				for(int i = 0; i < 10; ++i)
				{
					string strI = i.ToString(NumberFormatInfo.InvariantInfo);
					l.Add(new SiCode("NUMPAD" + strI, (int)Keys.NumPad0 + i,
						"KP_" + strI));

					Debug.Assert(((int)Keys.NumPad9 - (int)Keys.NumPad0) == 9);
				}

				l.Add(new SiCode("WIN", Keys.LWin, "Super_L"));
				l.Add(new SiCode("LWIN", Keys.LWin, "Super_L"));
				l.Add(new SiCode("RWIN", Keys.RWin, "Super_R"));
				l.Add(new SiCode("APPS", Keys.Apps, "Menu"));

#if DEBUG
				foreach(SiCode si in l)
				{
					// All key codes must be upper-case (for 'Get' method)
					Debug.Assert(si.Code == si.Code.ToUpperInvariant());
				}
#endif

				m_l = l;
				return l;
			}
		}

		private static Dictionary<string, SiCode> m_dictS = null;
		public static SiCode Get(string strCode)
		{
			if(strCode == null) { Debug.Assert(false); return null; }

			if(m_dictS == null)
			{
				Dictionary<string, SiCode> d = new Dictionary<string, SiCode>();
				foreach(SiCode siCp in SiCodes.KeyCodes)
				{
					d[siCp.Code] = siCp;
				}
				Debug.Assert(d.Count == SiCodes.KeyCodes.Count);

				m_dictS = d;
			}

			string strUp = strCode.ToUpperInvariant();
			SiCode si;
			if(m_dictS.TryGetValue(strUp, out si)) return si;
			return null;
		}

		public static SiCode Get(int iVKey, bool? obExtKey)
		{
			foreach(SiCode si in SiCodes.KeyCodes)
			{
				if(si.VKey == iVKey)
				{
					if(!si.ExtKey.HasValue || !obExtKey.HasValue) return si;
					if(si.ExtKey.Value == obExtKey.Value) return si;
				}
			}

			return null;
		}

		// Characters that should be converted to VKeys in special
		// situations (e.g. when a key modifier is active)
		private static Dictionary<char, int> m_dChToVk = null;

		// Characters that should always be converted to VKeys
		// (independent of e.g. whether key modifier is active or not)
		private static Dictionary<char, int> m_dChToVkAlways = null;

		private static void EnsureChToVkDicts()
		{
			if(m_dChToVk != null) return;

			Dictionary<char, int> d = new Dictionary<char, int>();

			// The following characters should *always* be converted
			d['\u0008'] = (int)Keys.Back;
			d['\t'] = (int)Keys.Tab;
			d['\n'] = (int)Keys.LineFeed;
			d['\r'] = (int)Keys.Return;
			d['\u001B'] = (int)Keys.Escape;
			d[' '] = (int)Keys.Space; // For toggling checkboxes
			d['\u007F'] = (int)Keys.Delete; // Different values

			m_dChToVkAlways = new Dictionary<char, int>(d);

			Debug.Assert((int)Keys.D0 == (int)'0');
			for(char ch = '0'; ch <= '9'; ++ch)
				d[ch] = (int)ch - (int)'0' + (int)Keys.D0;
			Debug.Assert(d['9'] == (int)Keys.D9);

			Debug.Assert((int)Keys.A == (int)'A');
			// Do not translate upper-case letters;
			// on Windows, sending VK_A results in 'a', and 'A' with Shift;
			// on some Linux systems, only Ctrl+'v' pastes, not Ctrl+'V':
			// https://sourceforge.net/p/keepass/discussion/329220/thread/bce61102/
			// for(char ch = 'A'; ch <= 'Z'; ++ch)
			//	d[ch] = (int)ch - (int)'A' + (int)Keys.A;
			for(char ch = 'a'; ch <= 'z'; ++ch)
				d[ch] = (int)ch - (int)'a' + (int)Keys.A;
			Debug.Assert(d['z'] == (int)Keys.Z);

			m_dChToVk = d;
			Debug.Assert(m_dChToVk.Count > m_dChToVkAlways.Count); // Independency
		}

		public static int CharToVKey(char ch, bool bLightConv)
		{
			EnsureChToVkDicts();

			Dictionary<char, int> d = (bLightConv ? m_dChToVkAlways : m_dChToVk);

			int iVKey;
			if(d.TryGetValue(ch, out iVKey)) return iVKey;
			return 0;
		}

		public static char VKeyToChar(int iVKey)
		{
			EnsureChToVkDicts();

			foreach(KeyValuePair<char, int> kvp in m_dChToVk)
			{
				if(kvp.Value == iVKey) return kvp.Key;
			}

			return char.MinValue;
		}

		private static Dictionary<char, string> m_dChToXKeySym = null;
		public static string CharToXKeySym(char ch)
		{
			if(m_dChToXKeySym == null)
			{
				Dictionary<char, string> d = new Dictionary<char, string>();

				// XDoTool sends some characters in the wrong case;
				// a workaround is to specify the keypress as an
				// XKeySym combination;
				// https://sourceforge.net/p/keepass/bugs/1532/
				// https://github.com/jordansissel/xdotool/issues/41

				AddDiacritic(d, 'A', '\u00C0', '\u00E0', SiDiacritic.Grave);
				AddDiacritic(d, 'A', '\u00C1', '\u00E1', SiDiacritic.Acute);
				AddDiacritic(d, 'A', '\u00C2', '\u00E2', SiDiacritic.Circumflex);
				AddDiacritic(d, 'A', '\u00C3', '\u00E3', SiDiacritic.Tilde);
				AddDiacritic(d, 'A', '\u00C4', '\u00E4', SiDiacritic.Diaeresis);
				AddDiacritic(d, 'A', '\u00C5', '\u00E5', SiDiacritic.Ring);

				AddDiacritic(d, 'C', '\u00C7', '\u00E7', SiDiacritic.Cedilla);

				AddDiacritic(d, 'E', '\u00C8', '\u00E8', SiDiacritic.Grave);
				AddDiacritic(d, 'E', '\u00C9', '\u00E9', SiDiacritic.Acute);
				AddDiacritic(d, 'E', '\u00CA', '\u00EA', SiDiacritic.Circumflex);
				AddDiacritic(d, 'E', '\u00CB', '\u00EB', SiDiacritic.Diaeresis);

				AddDiacritic(d, 'I', '\u00CC', '\u00EC', SiDiacritic.Grave);
				AddDiacritic(d, 'I', '\u00CD', '\u00ED', SiDiacritic.Acute);
				AddDiacritic(d, 'I', '\u00CE', '\u00EE', SiDiacritic.Circumflex);
				AddDiacritic(d, 'I', '\u00CF', '\u00EF', SiDiacritic.Diaeresis);

				AddDiacritic(d, 'N', '\u00D1', '\u00F1', SiDiacritic.Tilde);

				AddDiacritic(d, 'O', '\u00D2', '\u00F2', SiDiacritic.Grave);
				AddDiacritic(d, 'O', '\u00D3', '\u00F3', SiDiacritic.Acute);
				AddDiacritic(d, 'O', '\u00D4', '\u00F4', SiDiacritic.Circumflex);
				AddDiacritic(d, 'O', '\u00D5', '\u00F5', SiDiacritic.Tilde);
				AddDiacritic(d, 'O', '\u00D6', '\u00F6', SiDiacritic.Diaeresis);

				AddDiacritic(d, 'U', '\u00D9', '\u00F9', SiDiacritic.Grave);
				AddDiacritic(d, 'U', '\u00DA', '\u00FA', SiDiacritic.Acute);
				AddDiacritic(d, 'U', '\u00DB', '\u00FB', SiDiacritic.Circumflex);
				AddDiacritic(d, 'U', '\u00DC', '\u00FC', SiDiacritic.Diaeresis);

				AddDiacritic(d, 'Y', '\u00DD', '\u00FD', SiDiacritic.Acute);
				AddDiacritic(d, 'Y', '\u0178', '\u00FF', SiDiacritic.Diaeresis);

				AddDiacritic(d, 'A', '\u0100', '\u0101', SiDiacritic.Macron);
				AddDiacritic(d, 'A', '\u0102', '\u0103', SiDiacritic.Breve);
				AddDiacritic(d, 'A', '\u0104', '\u0105', SiDiacritic.Ogonek);

				AddDiacritic(d, 'C', '\u0106', '\u0107', SiDiacritic.Acute);
				AddDiacritic(d, 'C', '\u0108', '\u0109', SiDiacritic.Circumflex);
				AddDiacritic(d, 'C', '\u010A', '\u010B', SiDiacritic.DotAbove);
				AddDiacritic(d, 'C', '\u010C', '\u010D', SiDiacritic.Caron);

				AddDiacritic(d, 'D', '\u010E', '\u010F', SiDiacritic.Caron);

				AddDiacritic(d, 'E', '\u0112', '\u0113', SiDiacritic.Macron);
				AddDiacritic(d, 'E', '\u0114', '\u0115', SiDiacritic.Breve);
				AddDiacritic(d, 'E', '\u0116', '\u0117', SiDiacritic.DotAbove);
				AddDiacritic(d, 'E', '\u0118', '\u0119', SiDiacritic.Ogonek);
				AddDiacritic(d, 'E', '\u011A', '\u011B', SiDiacritic.Caron);

				AddDiacritic(d, 'G', '\u011C', '\u011D', SiDiacritic.Circumflex);
				AddDiacritic(d, 'G', '\u011E', '\u011F', SiDiacritic.Breve);
				AddDiacritic(d, 'G', '\u0120', '\u0121', SiDiacritic.DotAbove);
				AddDiacritic(d, 'G', '\u0122', '\u0123', SiDiacritic.Cedilla);

				AddDiacritic(d, 'H', '\u0124', '\u0125', SiDiacritic.Circumflex);

				AddDiacritic(d, 'I', '\u0128', '\u0129', SiDiacritic.Tilde);
				AddDiacritic(d, 'I', '\u012A', '\u012B', SiDiacritic.Macron);
				AddDiacritic(d, 'I', '\u012C', '\u012D', SiDiacritic.Breve);
				AddDiacritic(d, 'I', '\u012E', '\u012F', SiDiacritic.Ogonek);
				AddDiacritic(d, 'I', '\u0130', '\0', SiDiacritic.DotAbove);

				AddDiacritic(d, 'J', '\u0134', '\u0135', SiDiacritic.Circumflex);

				AddDiacritic(d, 'K', '\u0136', '\u0137', SiDiacritic.Cedilla);

				AddDiacritic(d, 'L', '\u0139', '\u013A', SiDiacritic.Acute);
				AddDiacritic(d, 'L', '\u013B', '\u013C', SiDiacritic.Cedilla);
				AddDiacritic(d, 'L', '\u013D', '\u013E', SiDiacritic.Caron);

				AddDiacritic(d, 'N', '\u0143', '\u0144', SiDiacritic.Acute);
				AddDiacritic(d, 'N', '\u0145', '\u0146', SiDiacritic.Cedilla);
				AddDiacritic(d, 'N', '\u0147', '\u0148', SiDiacritic.Caron);

				AddDiacritic(d, 'O', '\u014C', '\u014D', SiDiacritic.Macron);
				AddDiacritic(d, 'O', '\u014E', '\u014F', SiDiacritic.Breve);
				AddDiacritic(d, 'O', '\u0150', '\u0151', SiDiacritic.AcuteDouble);

				AddDiacritic(d, 'R', '\u0154', '\u0155', SiDiacritic.Acute);
				AddDiacritic(d, 'R', '\u0156', '\u0157', SiDiacritic.Cedilla);
				AddDiacritic(d, 'R', '\u0158', '\u0159', SiDiacritic.Caron);

				AddDiacritic(d, 'S', '\u015A', '\u015B', SiDiacritic.Acute);
				AddDiacritic(d, 'S', '\u015C', '\u015D', SiDiacritic.Circumflex);
				AddDiacritic(d, 'S', '\u015E', '\u015F', SiDiacritic.Cedilla);
				AddDiacritic(d, 'S', '\u0160', '\u0161', SiDiacritic.Caron);

				AddDiacritic(d, 'T', '\u0162', '\u0163', SiDiacritic.Cedilla);
				AddDiacritic(d, 'T', '\u0164', '\u0165', SiDiacritic.Caron);

				AddDiacritic(d, 'U', '\u0168', '\u0169', SiDiacritic.Tilde);
				AddDiacritic(d, 'U', '\u016A', '\u016B', SiDiacritic.Macron);
				AddDiacritic(d, 'U', '\u016C', '\u016D', SiDiacritic.Breve);
				AddDiacritic(d, 'U', '\u016E', '\u016F', SiDiacritic.Ring);
				AddDiacritic(d, 'U', '\u0170', '\u0171', SiDiacritic.AcuteDouble);
				AddDiacritic(d, 'U', '\u0172', '\u0173', SiDiacritic.Ogonek);

				AddDiacritic(d, 'W', '\u0174', '\u0175', SiDiacritic.Circumflex);

				AddDiacritic(d, 'Y', '\u0176', '\u0177', SiDiacritic.Circumflex);

				AddDiacritic(d, 'Z', '\u0179', '\u017A', SiDiacritic.Acute);
				AddDiacritic(d, 'Z', '\u017B', '\u017C', SiDiacritic.DotAbove);
				AddDiacritic(d, 'Z', '\u017D', '\u017E', SiDiacritic.Caron);

				m_dChToXKeySym = d;
			}

			string str;
			if(m_dChToXKeySym.TryGetValue(ch, out str)) return str;

			// Unicode is supported; codes are 'UHHHH' with 'HHHH' being
			// the Unicode value; see header of 'keysymdef.h'
			return ("U" + ((int)ch).ToString("X4", NumberFormatInfo.InvariantInfo));
		}

		private static void AddDiacritic(Dictionary<char, string> d,
			char chBase, char chDiaU, char chDiaL, SiDiacritic dc)
		{
			if(d == null) { Debug.Assert(false); return; }

			string strPrefix = string.Empty;
			switch(dc)
			{
				case SiDiacritic.Grave:
					strPrefix = "dead_grave ";
					break;
				case SiDiacritic.Acute:
					strPrefix = "dead_acute ";
					break;
				case SiDiacritic.Circumflex:
					strPrefix = "dead_circumflex ";
					break;
				case SiDiacritic.Tilde:
					strPrefix = "dead_tilde ";
					break;
				case SiDiacritic.Diaeresis:
					strPrefix = "dead_diaeresis ";
					break;
				case SiDiacritic.Ring:
					strPrefix = "dead_abovering ";
					break;
				case SiDiacritic.Cedilla:
					strPrefix = "dead_cedilla ";
					break;
				case SiDiacritic.Macron:
					strPrefix = "dead_macron ";
					break;
				case SiDiacritic.Breve:
					strPrefix = "dead_breve ";
					break;
				case SiDiacritic.Ogonek:
					strPrefix = "dead_ogonek ";
					break;
				case SiDiacritic.DotAbove:
					strPrefix = "dead_abovedot ";
					break;
				case SiDiacritic.Caron:
					strPrefix = "dead_caron ";
					break;
				case SiDiacritic.AcuteDouble:
					strPrefix = "dead_doubleacute ";
					break;
				default:
					Debug.Assert(dc == SiDiacritic.None);
					break;
			}
			Debug.Assert((strPrefix.Length == 0) || strPrefix.EndsWith(" "));

			if(chDiaU != '\0')
			{
				Debug.Assert(!d.ContainsKey(chDiaU));
				d[chDiaU] = (strPrefix + char.ToUpperInvariant(chBase));
			}
			if(chDiaL != '\0')
			{
				Debug.Assert(!d.ContainsKey(chDiaL));
				d[chDiaL] = (strPrefix + char.ToLowerInvariant(chBase));
			}
		}
	}
}