xref: /haiku/src/preferences/keymap/KeyboardLayout.h (revision adb0d19d561947362090081e81d90dde59142026)
1 /*
2  * Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef KEYBOARD_LAYOUT_H
6 #define KEYBOARD_LAYOUT_H
7 
8 
9 #include <map>
10 
11 #include <Entry.h>
12 #include <ObjectList.h>
13 #include <Point.h>
14 #include <Rect.h>
15 #include <String.h>
16 
17 
18 enum key_shape {
19 	kRectangleKeyShape,
20 	kCircleKeyShape,
21 	kEnterKeyShape
22 };
23 
24 struct Key {
25 	int32		code;
26 	int32		alternate_code[3];
27 	int32		alternate_modifier[3];
28 	key_shape	shape;
29 	BRect		frame;
30 	float		second_row;
31 		// this is the width of the second row of a kEnterKeyShape key
32 	bool		dark;
33 };
34 
35 struct Indicator {
36 	int32		modifier;
37 	BRect		frame;
38 };
39 
40 typedef std::map<BString, BString> VariableMap;
41 
42 class KeyboardLayout {
43 public:
44 							KeyboardLayout();
45 							~KeyboardLayout();
46 
47 			const char*		Name();
48 
49 			int32			CountKeys();
50 			Key*			KeyAt(int32 index);
51 
52 			int32			CountIndicators();
53 			Indicator*		IndicatorAt(int32 index);
54 
55 			BRect			Bounds();
56 			BSize			DefaultKeySize();
57 			int32			IndexForModifier(int32 modifier);
58 
59 			status_t		Load(const char* path);
60 			status_t		Load(entry_ref& ref);
61 
62 			void			SetDefault();
63 
64 private:
65 	enum parse_mode {
66 		kPairs,
67 		kSize,
68 		kRowStart,
69 		kKeyShape,
70 		kKeyCodes
71 	};
72 	struct parse_state {
73 		parse_mode	mode;
74 		int32		line;
75 	};
76 
77 			void			_FreeKeys();
78 			void			_Error(const parse_state& state,
79 								const char* reason, ...);
80 			void			_AddAlternateKeyCode(Key* key, int32 modifier,
81 								int32 code);
82 			bool			_AddKey(const Key& key);
83 			void			_SkipCommentsAndSpace(parse_state& state,
84 								const char*& data);
85 			void			_Trim(BString& string, bool stripComments);
86 			bool			_GetPair(const parse_state& state,
87 								const char*& data, BString& name,
88 								BString& value);
89 			bool			_AddKeyCodes(const parse_state& state,
90 								BPoint& rowLeftTop, Key& key, const char* data,
91 								int32& lastCount);
92 			bool			_GetSize(const parse_state& state, const char* data,
93 								float& x, float& y, float* _secondRow = NULL);
94 			bool			_GetShape(const parse_state& state,
95 								const char* data, Key& key);
96 			const char*		_Delimiter(parse_mode mode);
97 			bool			_GetTerm(const char*& data, const char* delimiter,
98 								BString& term, bool closingBracketAllowed);
99 			bool			_SubstituteVariables(BString& term,
100 								VariableMap& variables, BString& unknown);
101 			bool			_ParseTerm(const parse_state& state,
102 								const char*& data, BString& term,
103 								VariableMap& variables);
104 
105 			status_t		_InitFrom(const char* data);
106 
107 			BString			fName;
108 			Key*			fKeys;
109 			int32			fKeyCount;
110 			int32			fKeyCapacity;
111 			BRect			fBounds;
112 			BSize			fDefaultKeySize;
113 			int32			fAlternateIndex[3];
114 			BObjectList<Indicator> fIndicators;
115 };
116 
117 #endif	// KEYBOARD_LAYOUT_H
118