summaryrefslogtreecommitdiffstats
path: root/tdecore/kkeyserver_x11.h
blob: 7632984e7180b13a814be2e1b06469290dd9673f (plain)
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
    Copyright (C) 2001 Ellis Whitehead <ellis@kde.org>

    Win32 port:
    Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#ifndef _KKEYSERVER_X11_H
#define _KKEYSERVER_X11_H

#include "tdeshortcut.h"
#include "kkeynative.h"

/**
 * A collection of functions for the conversion of key presses and
 * their modifiers from the window system specific format
 * to the generic format and vice-versa.
 */
namespace KKeyServer
{
	/**
	 * Supplement enum KKey::ModFlag
	 * @since 3.1
	 */
	enum ExtraModFlag { MODE_SWITCH = 0x2000 };

	/**
	 * Represents a key symbol.
	 * @see KKey
	 * @see KKeyServer
	 */
	struct TDECORE_EXPORT Sym
	{
	 public:
		/// the actual value of the symbol
		uint m_sym;

		/// Creates a null symbol.
		Sym()
			{ m_sym = 0; }
		/**
		 * Creates asymbol with the given value.
		 * @param sym the value
		 */
		Sym( uint sym )
			{ m_sym = sym; }
		/**
		 * Creates a symbol from the given string description.
		 * @param s the description of the symbol
		 * @see toString()
		 */
		Sym( const TQString& s )
			{ init( s ); }

		/**
		 * Initializes the symbol with the given Qt key code.
		 * @param keyQt the qt key code
		 * @return true if successful, false otherwise
		 * @see TQt::Key
		 */
		bool initQt( int keyQt );

		/**
		 * Initializes the key with the given string description.
		 * @param s the string description
		 * @return true if successful, false otherwise
		 * @see toString()
		 */
		bool init( const TQString &s );

		/**
		 * Returns the qt key code of the symbol.
		 * @return the qt key code
		 */
		int qt() const;

		/**
		 * @internal
		 */
		TQString toStringInternal() const;

		/**
		 * Returns the string representation of the symbol.
		 * @return the string representation of the symbol
		 */
		TQString toString() const;

		/**
		 * Returns the mods that are required for this symbol as
		 * ORed KKey::ModFlag's. For example, Break requires a
		 * Ctrl to be valid.
		 * @return the required KKey::ModFlag's
		 * @see KKey::ModFlag
		 */
		uint getModsRequired() const;

		/**
		 * TODO: please find out what this method does and document it
		 */
		uint getSymVariation() const;

		/**
		 * Casts the symbol to its integer representation.
		 */
		operator uint() const { return m_sym; }

		/**
		 * Overloaded operator to convert ints to Sym.
		 */
		Sym& operator =( uint sym ) { m_sym = sym; return *this; }

	 private:
		TQString toString( bool bUserSpace ) const;

		static void capitalizeKeyname( TQString& );
	};

	/**
	 * Represents a key press.
	 * @see KKey
	 */
	struct TDECORE_EXPORT Key
	{
		/// Code for native Keys in Qt
		enum { CODE_FOR_QT = 256 };

		/// The code of the key
		uint m_code;

		/// The modifiers of the key
		uint m_mod;

		/// The symbol of the key
		uint m_sym;

		/**
		 * Initializes the key with a KKey.
		 * @param key the key to get the data from
		 * @param bQt true to take the Qt keycode, false
		 *            for the native key code
		 * @see TQt::Key
		 * @see KKeyNative
		 */
		bool init( const KKey& key, bool bQt );

		/**
		 * Checks whether the key code is a native code.
		 * @return true if native code of the window system,
		 *         false if it is a Qt keycode
		 * @see TQt::Key
		 * @see KKeyNative
		 */
		bool isNative() const { return m_code != CODE_FOR_QT; }

		/**
		 * Returns the code of the key.
		 * @return the code of the key
		 */
		uint code() const { return m_code; }

		/**
		 * Returns the modifiers of the key.
		 * @return the modifiers of the key
		 */
		uint mod() const { return m_mod; }

		/**
		 * Returns the symbol of the key.
		 * @return the symbol of the key
		 */
		uint sym() const { return m_sym; }

		/**
		 * Returns the qt key code.
		 * @return the qt key code
		 */
		int keyCodeQt() const { return (int) m_sym; }

		/**
		 * Sets the qt key code.
		 * @param keyQt the qt key code
		 */
		void setKeycodeQt( int keyQt )
			{ m_code = CODE_FOR_QT; m_sym = keyQt; }

		/**
		 * Initializes this key with a KKeyNative.
		 * @return this key
		 */
		Key& operator =( const KKeyNative& key );

		/**
		 * Compares this key with the given Key object. Returns a
		 * negative number if the given Key is larger, 0 if they
		 * are equal and a positive number this Key is larger. The
		 * returned value is the difference between the symbol, modifier
		 * or code, whatever is non-zero first.
		 *
		 * @param key the key to compare with this key
		 * @return a negative number if the given Key is larger, 0 if
		 * they are equal and a positive number this Key is larger
		 */
		int compare( const Key& key ) const;

		/**
		 * Compares the symbol, modifiers and code of both keys.
		 * @see compare()
		 */
		bool operator ==( const Key& b ) const
			{ return compare( b ) == 0; }

		/**
		 * Compares the symbol, modifiers and code of both keys.
		 * @see compare()
		 */
		bool operator <( const Key& b ) const
			{ return compare( b ) < 0; }

		/**
		 * Converts this Key to a KKey.
		 * @return the KKey
		 */
		KKey key() const;
	};

	/**
	 * TODO: please document this class
	 */
	struct TDECORE_EXPORT Variations
	{
		enum { MAX_VARIATIONS = 4 };

		Key m_rgkey[MAX_VARIATIONS];
		uint m_nVariations;

		Variations() { m_nVariations = 0; }

		void init( const KKey&, bool bQt );

		uint count() const { return m_nVariations; }
		const Key& key( uint i ) const { return m_rgkey[i]; }
	};

	/// TODO: please document
	TDECORE_EXPORT bool initializeMods();

	/**
	 * Returns the equivalent X modifier mask of the given modifier flag.
	 * @param modFlag the generic flags to check
	 * @return the window system specific flags
	 */
	TDECORE_EXPORT uint modX( KKey::ModFlag modFlag );

	/**
	 * Returns true if the current keyboard layout supports the Win key.
	 * Specifically, whether the Super or Meta keys are assigned to an X modifier.
	 * @return true if the keyboard has a Win key
	 * @see modXWin()
	 */
	TDECORE_EXPORT bool keyboardHasWinKey();

	/**
	 * Returns the X11 Shift modifier mask/flag.
	 * @return the X11 Shift modifier mask/flag.
	 * @see accelModMaskX()
	 */
	TDECORE_EXPORT uint modXShift();

	/**
	 * Returns the X11 Lock modifier mask/flag.
	 * @return the X11 Lock modifier mask/flag.
	 * @see accelModMaskX()
	 */
	TDECORE_EXPORT uint modXLock();

	/**
	 * Returns the X11 Ctrl modifier mask/flag.
	 * @return the X11 Ctrl modifier mask/flag.
	 * @see accelModMaskX()
	 */
	TDECORE_EXPORT uint modXCtrl();

	/**
	 * Returns the X11 Alt (Mod1) modifier mask/flag.
	 * @return the X11 Alt (Mod1) modifier mask/flag.
	 * @see accelModMaskX()
	 */
	TDECORE_EXPORT uint modXAlt();

	/**
	 * Returns the X11 NumLock modifier mask/flag.
	 * @return the X11 NumLock modifier mask/flag.
	 * @see accelModMaskX()
	 */
	TDECORE_EXPORT uint modXNumLock();

	/**
	 * Returns the X11 Win (Mod3) modifier mask/flag.
	 * @return the X11 Win (Mod3) modifier mask/flag.
	 * @see keyboardHasWinKey()
	 * @see accelModMaskX()
	 */
	TDECORE_EXPORT uint modXWin();

	/**
	 * Returns the X11 ScrollLock modifier mask/flag.
	 * @return the X11 ScrollLock modifier mask/flag.
	 * @see accelModMaskX()
	 */
	TDECORE_EXPORT uint modXScrollLock();

	/**
	 * Returns the X11 Mode_switch modifier mask/flag. 
	 * @return the X11 Mode_switch modifier mask/flag. 
	 * @see accelModMaskX()
	 * @since 3.5
	 */
	TDECORE_EXPORT uint modXModeSwitch();

	/**
	 * Returns bitwise OR'ed mask containing Shift, Ctrl, Alt, and
	 * Win (if available).
	 * @see modXShift()
	 * @see modXLock()
	 * @see modXCtrl()
	 * @see modXAlt()
	 * @see modXNumLock()
	 * @see modXWin()
	 * @see modXScrollLock()
	 */
	TDECORE_EXPORT uint accelModMaskX();

	/**
	 * Extracts the symbol from the given Qt key and
	 * converts it to a symbol.
	 * @param keyQt the qt key code
	 * @param sym if successful, the symbol will be written here
	 * @return true if successful, false otherwise
	 * @see TQt::Key
	 * @see Sym
	 */
	TDECORE_EXPORT bool keyQtToSym( int keyQt, uint& sym );

	/**
	 * Extracts the modifiers from the given Qt key and
	 * converts them in a mask of ORed KKey::ModFlag modifiers.
	 * @param keyQt the qt key code
	 * @param mod if successful, the modifiers will be written here
	 * @return true if successful, false otherwise
	 * @see TQt::Key
	 */
	TDECORE_EXPORT bool keyQtToMod( int keyQt, uint& mod );

	/**
	 * Converts the given symbol to a Qt key code.
	 * @param sym the symbol
	 * @param keyQt if successful, the qt key code will be written here
	 * @return true if successful, false otherwise
	 * @see TQt::Key
	 * @see Sym
	 */
	TDECORE_EXPORT bool symToKeyQt( uint sym, int& keyQt );

	/**
	 * Converts the mask of ORed KKey::ModFlag modifiers to
	 * a mask of ORed Qt key code modifiers.
	 * @param mod the mask of KKey::ModFlag modifiers
	 * @param modQt the mask of Qt key code modifiers will be written here,
	 *        if successful
	 * @return true if successful, false otherwise
	 * @see TQt::Key
	 * @see KKey
	 */
	TDECORE_EXPORT bool modToModQt( uint mod, int& modQt );

	/**
	 * Converts the mask of ORed KKey::ModFlag modifiers to
	 * a mask of ORed X11 modifiers.
	 * @param mod the mask of KKey::ModFlag modifiers
	 * @param modX the mask of X11 modifiers will be written here,
	 *        if successful
	 * @return true if successful, false otherwise
	 * @see KKey
	 */
	TDECORE_EXPORT bool modToModX( uint mod, uint& modX );

	/**
	 * Converts the mask of ORed X11 modifiers to
	 * a mask of ORed Qt key code modifiers.
	 * @param modX the mask of X11 modifiers
	 * @param modQt the mask of Qt key code modifiers will be written here
	 *        if successful
	 * @return true if successful, false otherwise
	 * @see TQt::Key
	 */
	//wrapped for win32
	TDECORE_EXPORT bool modXToModQt( uint modX, int& modQt );

	/**
	 * Converts the Qt-compatible button state to x11 modifier. 
	 */
	TDECORE_EXPORT int qtButtonStateToMod( TQt::ButtonState s );

	/**
	 * Converts the mask of ORed X11 modifiers to
	 * a mask of ORed KKey::ModFlag modifiers.
	 * @param modX the mask of X11 modifiers
	 * @param mod the mask of KKey::ModFlag modifiers will be written here,
	 *        if successful
	 * @return true if successful, false otherwise
	 * @see KKey
	 */
	TDECORE_EXPORT bool modXToMod( uint modX, uint& mod );

	/**
	 * Converts a X11 key code and a mask of ORed X11 modifiers
	 * into a X11 symbol.
	 * converts it to a symbol.
	 * @param codeX the X11 key code
	 * @param modX the mask of ORed X11 modifiers
	 * @param symX if successful, the X11 symbol will be written here
	 * @return true if successful, false otherwise
	 * @see TQt::Key
	 * @see Sym
	 */
	TDECORE_EXPORT bool codeXToSym( uchar codeX, uint modX, uint& symX );

	/**
	 * @internal
	 */
	TDECORE_EXPORT TQString modToStringInternal( uint mod );

	/**
	 * Converts the mask of ORed KKey::ModFlag modifiers to a
	 * user-readable string.
	 * @param mod the mask of ORed KKey::ModFlag modifiers
	 * @return the user-readable string
	 */
	TDECORE_EXPORT TQString modToStringUser( uint mod );

	/**
	 * Converts the modifier given as user-readable string
         * to KKey::ModFlag modifier, or 0.
         * @internal
         * @since 3.5
	 */
	TDECORE_EXPORT uint stringUserToMod( const TQString& mod );

	/**
	 * @internal
	 * Unimplemented?
	 */
	TDECORE_EXPORT bool stringToSymMod( const TQString&, uint& sym, uint& mod );

	/**
	 * @internal
	 * Unimplemented?
	 */
	TDECORE_EXPORT void keyQtToKeyX( uint keyCombQt, unsigned char *pKeyCodeX, uint *pKeySymX, uint *pKeyModX );
}

#endif // !_KKEYSERVER_X11_H