1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "backends/keymapper/input-watcher.h"
24
25 #include "backends/keymapper/action.h"
26 #include "backends/keymapper/keymapper.h"
27
28 namespace Common {
29
InputWatcher(EventDispatcher * eventDispatcher,Keymapper * keymapper)30 InputWatcher::InputWatcher(EventDispatcher *eventDispatcher, Keymapper *keymapper) :
31 _eventDispatcher(eventDispatcher),
32 _keymapper(keymapper),
33 _watching(false) {
34
35 }
36
startWatching()37 void InputWatcher::startWatching() {
38 assert(!_watching);
39 assert(_hwInput.type == kHardwareInputTypeInvalid);
40
41 _keymapper->setEnabled(false);
42 _eventDispatcher->registerObserver(this, EventManager::kEventRemapperPriority, false);
43 _watching = true;
44 }
45
stopWatching()46 void InputWatcher::stopWatching() {
47 _keymapper->setEnabled(true);
48 _eventDispatcher->unregisterObserver(this);
49 _watching = false;
50 }
51
isWatching() const52 bool InputWatcher::isWatching() const {
53 return _watching;
54 }
55
notifyEvent(const Event & event)56 bool InputWatcher::notifyEvent(const Event &event) {
57 assert(_watching);
58 assert(_hwInput.type == kHardwareInputTypeInvalid);
59
60 switch (event.type) {
61 case EVENT_KEYDOWN:
62 case EVENT_JOYBUTTON_DOWN:
63 case EVENT_LBUTTONDOWN:
64 case EVENT_RBUTTONDOWN:
65 case EVENT_MBUTTONDOWN:
66 case EVENT_X1BUTTONDOWN:
67 case EVENT_X2BUTTONDOWN:
68 return true;
69 case EVENT_KEYUP:
70 case EVENT_JOYBUTTON_UP:
71 case EVENT_JOYAXIS_MOTION:
72 case EVENT_LBUTTONUP:
73 case EVENT_RBUTTONUP:
74 case EVENT_MBUTTONUP:
75 case EVENT_WHEELUP:
76 case EVENT_WHEELDOWN:
77 case EVENT_X1BUTTONUP:
78 case EVENT_X2BUTTONUP:
79 case EVENT_CUSTOM_BACKEND_HARDWARE:
80 _hwInput = _keymapper->findHardwareInput(event);
81 if (_hwInput.type != kHardwareInputTypeInvalid) {
82 stopWatching();
83 }
84 return true;
85 default:
86 break;
87 }
88
89 return false;
90 }
91
checkForCapturedInput()92 HardwareInput InputWatcher::checkForCapturedInput() {
93 HardwareInput hwInput = _hwInput;
94 _hwInput = HardwareInput();
95 return hwInput;
96 }
97
98 } // End of namespace Common
99