author | alwin <alwin> | 2005-02-28 09:40:30 (UTC) |
---|---|---|
committer | alwin <alwin> | 2005-02-28 09:40:30 (UTC) |
commit | 2b64a84d39eeed5681d0ee5068c7d11a01527750 (patch) (unidiff) | |
tree | c8693340dbc5ef5e2f9afa90b690829ddff2c4bd /noncore/applets/keyhelper/keyhelperapplet/misc/KeyModifiers.cpp | |
parent | 61fa699140c5efbb6ba0bf2a62f7e8fbf62976be (diff) | |
download | opie-2b64a84d39eeed5681d0ee5068c7d11a01527750.zip opie-2b64a84d39eeed5681d0ee5068c7d11a01527750.tar.gz opie-2b64a84d39eeed5681d0ee5068c7d11a01527750.tar.bz2 |
other keymapping tool - not working this moment, I have to check it out
- the reason is that the config file is somewhat easier to understand than
from zkbapplet and has a nice config tool.
Please don't put it into any repositories this moment.
Diffstat (limited to 'noncore/applets/keyhelper/keyhelperapplet/misc/KeyModifiers.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/applets/keyhelper/keyhelperapplet/misc/KeyModifiers.cpp | 285 |
1 files changed, 285 insertions, 0 deletions
diff --git a/noncore/applets/keyhelper/keyhelperapplet/misc/KeyModifiers.cpp b/noncore/applets/keyhelper/keyhelperapplet/misc/KeyModifiers.cpp new file mode 100644 index 0000000..4699b11 --- a/dev/null +++ b/noncore/applets/keyhelper/keyhelperapplet/misc/KeyModifiers.cpp | |||
@@ -0,0 +1,285 @@ | |||
1 | #include "KeyModifiers.h" | ||
2 | |||
3 | KeyModifiers::KeyModifiers() | ||
4 | { | ||
5 | qDebug("KeyModifiers::KeyModifiers()"); | ||
6 | m_pTimer = new QTimer(this); | ||
7 | connect(m_pTimer, SIGNAL(timeout()), | ||
8 | this, SLOT(resetToggles())); | ||
9 | init(); | ||
10 | } | ||
11 | |||
12 | KeyModifiers::~KeyModifiers() | ||
13 | { | ||
14 | qDebug("KeyModifiers::~KeyModifiers()"); | ||
15 | delete m_pTimer; | ||
16 | clear(); | ||
17 | } | ||
18 | |||
19 | void KeyModifiers::addType(const QString& type) | ||
20 | { | ||
21 | if(m_types.contains(type) == false){ | ||
22 | qDebug("addType[%s][%x]", type.latin1(), m_bitmask); | ||
23 | m_types.insert(type, m_bitmask); | ||
24 | m_bitmask = m_bitmask << 1; | ||
25 | } | ||
26 | } | ||
27 | |||
28 | ModifierInfo* KeyModifiers::assign(const QString& type, int keycode, | ||
29 | int keymask, bool mode) | ||
30 | { | ||
31 | m_info = new ModifierInfo(type, keycode, keymask, mode); | ||
32 | addType(type); | ||
33 | m_modifiers.append(m_info); | ||
34 | if(mode){ | ||
35 | m_togglekeys.append(m_info); | ||
36 | } | ||
37 | assignRelease(m_info, keycode); | ||
38 | return(m_info); | ||
39 | } | ||
40 | |||
41 | void KeyModifiers::assignRelease(int keycode) | ||
42 | { | ||
43 | assignRelease(m_info, keycode); | ||
44 | } | ||
45 | |||
46 | void KeyModifiers::assignRelease(ModifierInfo* info, int keycode) | ||
47 | { | ||
48 | if(m_releasekeys.contains(keycode) == false){ | ||
49 | m_releasekeys.insert(keycode, new ModifierList); | ||
50 | } | ||
51 | m_releasekeys[keycode]->append(info); | ||
52 | } | ||
53 | |||
54 | void KeyModifiers::setToggle() | ||
55 | { | ||
56 | setToggle(m_info); | ||
57 | } | ||
58 | |||
59 | void KeyModifiers::setToggle(ModifierInfo* info) | ||
60 | { | ||
61 | info->toggle_mode = true; | ||
62 | m_togglekeys.append(info); | ||
63 | } | ||
64 | |||
65 | void KeyModifiers::keepToggles() | ||
66 | { | ||
67 | if(m_timeout > 0){ | ||
68 | m_pTimer->start(m_timeout, true); | ||
69 | } | ||
70 | } | ||
71 | |||
72 | bool KeyModifiers::pressKey(int keycode, int modifiers) | ||
73 | { | ||
74 | int keymask; | ||
75 | |||
76 | keymask = getState(modifiers, true); | ||
77 | |||
78 | for(ModifierList::Iterator it=m_modifiers.begin(); | ||
79 | it!=m_modifiers.end(); ++it){ | ||
80 | if((*it)->keycode == keycode | ||
81 | && ((*it)->keymask & keymask) == (*it)->keymask){ | ||
82 | (*it)->pressed = true; | ||
83 | if((*it)->toggle_mode){ | ||
84 | /* change toggle state */ | ||
85 | (*it)->toggled = !((*it)->toggled); | ||
86 | if((*it)->toggled){ | ||
87 | keepToggles(); | ||
88 | } else { | ||
89 | m_pTimer->stop(); | ||
90 | } | ||
91 | } | ||
92 | return(true); | ||
93 | } | ||
94 | } | ||
95 | return(false); | ||
96 | } | ||
97 | |||
98 | bool KeyModifiers::isModifier(int keycode) | ||
99 | { | ||
100 | if(keycode == Qt::Key_Shift | ||
101 | || keycode == Qt::Key_Control | ||
102 | || keycode == Qt::Key_Alt | ||
103 | || keycode == Qt::Key_Meta | ||
104 | || keycode == Qt::Key_F22){ | ||
105 | return(true); | ||
106 | } else { | ||
107 | return(false); | ||
108 | } | ||
109 | } | ||
110 | |||
111 | void KeyModifiers::releaseKey(int keycode) | ||
112 | { | ||
113 | if(m_releasekeys.contains(keycode)){ | ||
114 | ModifierList* list = m_releasekeys[keycode]; | ||
115 | for(ModifierList::Iterator it=(*list).begin(); | ||
116 | it!=(*list).end(); ++it){ | ||
117 | (*it)->pressed = false; | ||
118 | } | ||
119 | } | ||
120 | } | ||
121 | |||
122 | int KeyModifiers::getState() | ||
123 | { | ||
124 | int state = 0; | ||
125 | for(ModifierList::Iterator it=m_modifiers.begin(); | ||
126 | it!=m_modifiers.end(); ++it){ | ||
127 | if((*it)->pressed || (*it)->toggled){ | ||
128 | state |= m_types[(*it)->type]; | ||
129 | } | ||
130 | } | ||
131 | return(state); | ||
132 | } | ||
133 | |||
134 | int KeyModifiers::getState(int modifiers, bool reset) | ||
135 | { | ||
136 | int state = getState(); | ||
137 | int mask; | ||
138 | |||
139 | mask = getMask("Shift"); | ||
140 | if(modifiers & Qt::ShiftButton){ | ||
141 | state |= mask; | ||
142 | } else { | ||
143 | if(reset){ | ||
144 | state &= ~mask; | ||
145 | } | ||
146 | } | ||
147 | mask = getMask("Control"); | ||
148 | if(modifiers & Qt::ControlButton){ | ||
149 | state |= mask; | ||
150 | } else { | ||
151 | if(reset){ | ||
152 | state &= ~mask; | ||
153 | } | ||
154 | } | ||
155 | mask = getMask("Alt"); | ||
156 | if(modifiers & Qt::AltButton){ | ||
157 | state |= mask; | ||
158 | } else { | ||
159 | if(reset){ | ||
160 | state &= ~mask; | ||
161 | } | ||
162 | } | ||
163 | |||
164 | return(state); | ||
165 | } | ||
166 | |||
167 | int KeyModifiers::getModifiers(int modifiers) | ||
168 | { | ||
169 | int state = getState(); | ||
170 | |||
171 | if(state & getMask("Shift")){ | ||
172 | modifiers |= Qt::ShiftButton; | ||
173 | } | ||
174 | if(state & getMask("Control")){ | ||
175 | modifiers |= Qt::ControlButton; | ||
176 | } | ||
177 | if(state & getMask("Alt")){ | ||
178 | modifiers |= Qt::AltButton; | ||
179 | } | ||
180 | |||
181 | return(modifiers); | ||
182 | } | ||
183 | |||
184 | int KeyModifiers::getMask(const QString& type) | ||
185 | { | ||
186 | if(m_types.contains(type)){ | ||
187 | return(m_types[type]); | ||
188 | } else { | ||
189 | return(0); | ||
190 | } | ||
191 | } | ||
192 | |||
193 | void KeyModifiers::clear() | ||
194 | { | ||
195 | m_types.clear(); | ||
196 | |||
197 | m_togglekeys.clear(); | ||
198 | |||
199 | for(ModifierList::Iterator it=m_modifiers.begin(); | ||
200 | it!=m_modifiers.end(); ++it){ | ||
201 | delete *it; | ||
202 | } | ||
203 | m_modifiers.clear(); | ||
204 | |||
205 | for(QMap<int, ModifierList*>::Iterator it=m_releasekeys.begin(); | ||
206 | it!=m_releasekeys.end(); ++it){ | ||
207 | delete *it; | ||
208 | } | ||
209 | m_releasekeys.clear(); | ||
210 | } | ||
211 | |||
212 | void KeyModifiers::init() | ||
213 | { | ||
214 | m_bitmask = 0x00000001; | ||
215 | addType("Shift"); | ||
216 | addType("Control"); | ||
217 | addType("Alt"); | ||
218 | |||
219 | ConfigEx& cfg = ConfigEx::getInstance("keyhelper"); | ||
220 | |||
221 | cfg.setGroup("Global"); | ||
222 | |||
223 | QString timeout = cfg.readEntry("ToggleTimeout", "10"); | ||
224 | int msec = timeout.toInt(); | ||
225 | msec *= 1000; /* sec to msec */ | ||
226 | m_timeout = msec; | ||
227 | } | ||
228 | |||
229 | void KeyModifiers::reset() | ||
230 | { | ||
231 | clear(); | ||
232 | init(); | ||
233 | } | ||
234 | |||
235 | bool KeyModifiers::isToggled() | ||
236 | { | ||
237 | for(ModifierList::Iterator it=m_togglekeys.begin(); | ||
238 | it!=m_togglekeys.end(); ++it){ | ||
239 | if((*it)->toggled){ | ||
240 | return(true); | ||
241 | } | ||
242 | } | ||
243 | return(false); | ||
244 | } | ||
245 | |||
246 | void KeyModifiers::resetToggles() | ||
247 | { | ||
248 | for(ModifierList::Iterator it=m_togglekeys.begin(); | ||
249 | it!=m_togglekeys.end(); ++it){ | ||
250 | (*it)->toggled = false; | ||
251 | } | ||
252 | m_pTimer->stop(); | ||
253 | } | ||
254 | |||
255 | void KeyModifiers::resetStates() | ||
256 | { | ||
257 | for(ModifierList::Iterator it=m_modifiers.begin(); | ||
258 | it!=m_modifiers.end(); ++it){ | ||
259 | (*it)->pressed = false; | ||
260 | } | ||
261 | resetToggles(); | ||
262 | } | ||
263 | |||
264 | void KeyModifiers::statistics() | ||
265 | { | ||
266 | qWarning("KeyModifiers::statistics()"); | ||
267 | for(ModifierList::Iterator it=m_modifiers.begin(); | ||
268 | it!=m_modifiers.end(); ++it){ | ||
269 | qWarning(" [%s][%x][%x][%d][%d]", | ||
270 | (*it)->type.latin1(), | ||
271 | (*it)->keycode, | ||
272 | (*it)->keymask, | ||
273 | (*it)->pressed, | ||
274 | (*it)->toggled); | ||
275 | for(QMap<int, ModifierList*>::Iterator it2=m_releasekeys.begin(); | ||
276 | it2!=m_releasekeys.end(); ++it2){ | ||
277 | for(ModifierList::Iterator it3=(*it2)->begin(); | ||
278 | it3!=(*it2)->end(); ++it3){ | ||
279 | if(*it == *it3){ | ||
280 | qWarning(" release[%x]", it2.key()); | ||
281 | } | ||
282 | } | ||
283 | } | ||
284 | } | ||
285 | } | ||