1 /*
2 * OpenSCAD (www.openscad.org)
3 * Copyright (C) 2009-2015 Clifford Wolf <clifford@clifford.at> and
4 * Marius Kintel <marius@kintel.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * As a special exception, you have permission to link this program
12 * with the CGAL library and distribute executables, as long as you
13 * follow the requirements of the GNU GPL in regard to all of the
14 * software in the executable aside from CGAL.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26 #include "InputDriverManager.h"
27
28 #include "printutils.h"
29
30 InputDriverManager * InputDriverManager::self = 0;
31
32 /**
33 * This can be called from non-GUI context, so no Qt initialization is done
34 * at this point.
35 */
InputDriverManager(void)36 InputDriverManager::InputDriverManager(void) : currentWindow(nullptr), timer(nullptr)
37 {
38 }
39
~InputDriverManager(void)40 InputDriverManager::~InputDriverManager(void)
41 {
42
43 }
44
instance()45 InputDriverManager * InputDriverManager::instance()
46 {
47 if (!self) {
48 self = new InputDriverManager();
49 }
50 return self;
51 }
52
registerDriver(InputDriver * driver)53 void InputDriverManager::registerDriver(InputDriver *driver)
54 {
55 this->drivers.push_back(driver);
56 }
57
unregisterDriver(InputDriver * driver)58 void InputDriverManager::unregisterDriver(InputDriver *driver)
59 {
60 this->drivers.remove(driver);
61 }
62
registerActions(const QList<QAction * > & actions,const QString parent)63 void InputDriverManager::registerActions(const QList<QAction *> &actions, const QString parent)
64 {
65 for (const auto action : actions) {
66 const auto description = parent + action->text();
67 if (!action->objectName().isEmpty()) {
68 this->actions.push_back({action->objectName(), description, action->icon()});
69 }
70 if (action->menu()) {
71 registerActions(action->menu()->actions(), description + QString::fromUtf8(u8" \u2192 "));
72 }
73 }
74 }
75
init()76 void InputDriverManager::init()
77 {
78 timer = new QTimer(this);
79 connect(QApplication::instance(), SIGNAL(focusChanged(QWidget *, QWidget *)), this, SLOT(onFocusChanged(QWidget *, QWidget *)));
80
81 doOpen(true);
82 connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
83 timer->start(10 * 1000);
84 }
85
onTimeout()86 void InputDriverManager::onTimeout()
87 {
88 for (auto driver : drivers) {
89 if (driver->openOnce()) {
90 continue;
91 }
92 if (driver->isOpen()) {
93 return;
94 }
95 }
96 doOpen(false);
97 }
98
doOpen(bool firstOpen)99 void InputDriverManager::doOpen(bool firstOpen)
100 {
101 for (auto driver : drivers) {
102 if (driver->openOnce()) {
103 continue;
104 }
105 if (driver->open()) {
106 break;
107 }
108 }
109
110 if (firstOpen) {
111 for (auto driver : drivers) {
112 if (driver->openOnce()) {
113 driver->open();
114 }
115 }
116 }
117 }
118
listDrivers() const119 std::string InputDriverManager::listDrivers() const
120 {
121 std::ostringstream stream;
122 const char *sep = "";
123 for (auto driver : drivers) {
124 stream << sep << driver->get_name();
125 if (driver->isOpen()) {
126 stream << "*";
127 }
128 sep = ", ";
129 }
130 return stream.str();
131 }
132
listDriverInfos() const133 std::string InputDriverManager::listDriverInfos() const
134 {
135 std::ostringstream stream;
136 const char *sep = "";
137 for (auto driver : drivers) {
138 stream << sep << driver->get_info();
139 sep = "\n";
140 }
141 return stream.str();
142 }
143
closeDrivers()144 void InputDriverManager::closeDrivers()
145 {
146 if (timer != nullptr) {
147 timer->stop();
148 }
149 InputEventMapper::instance()->stop();
150
151 for (auto driver : drivers) {
152 driver->close();
153 }
154 }
155
sendEvent(InputEvent * event)156 void InputDriverManager::sendEvent(InputEvent *event)
157 {
158 event->deliver(&mapper);
159 }
160
postEvent(InputEvent * event)161 void InputDriverManager::postEvent(InputEvent *event)
162 {
163 QWidget *window = event->activeOnly ? QApplication::activeWindow() : currentWindow;
164 if (window) {
165 QCoreApplication::postEvent(window, event);
166 }
167 }
168
getActions() const169 const std::list<ActionStruct> & InputDriverManager::getActions() const
170 {
171 return actions;
172 }
173
getTranslation() const174 QList<double> InputDriverManager::getTranslation() const
175 {
176 const MainWindow *window = currentWindow;
177 if (window) {
178 return window->getTranslation();
179 }
180 return QList<double>({0.0, 0.0, 0.0});
181 }
182
getRotation() const183 QList<double> InputDriverManager::getRotation() const
184 {
185 const MainWindow *window = currentWindow;
186 if (window) {
187 return window->getRotation();
188 }
189 return QList<double>({0.0, 0.0, 0.0});
190 }
191
onFocusChanged(QWidget *,QWidget * current)192 void InputDriverManager::onFocusChanged(QWidget *, QWidget *current)
193 {
194 if (current) {
195 currentWindow = dynamic_cast<MainWindow *>(current->window());
196 }
197 }
198
onInputMappingUpdated()199 void InputDriverManager::onInputMappingUpdated()
200 {
201 mapper.onInputMappingUpdated();
202 }
203
onInputCalibrationUpdated()204 void InputDriverManager::onInputCalibrationUpdated()
205 {
206 mapper.onInputCalibrationUpdated();
207 }
208
onInputGainUpdated()209 void InputDriverManager::onInputGainUpdated()
210 {
211 mapper.onInputGainUpdated();
212 }
213
getButtonCount()214 int InputDriverManager::getButtonCount(){
215 int max=0;
216 for (auto driver : drivers) {
217 if(driver->isOpen()){
218 max = std::max(max, driver->getButtonCount());
219 }
220 }
221 return max;
222 }
223
getAxisCount()224 int InputDriverManager::getAxisCount(){
225 int max=0;
226 for (auto driver : drivers) {
227 if(driver->isOpen()){
228 max = std::max(max, driver->getAxisCount());
229 }
230 }
231 return max;
232 }
233