diff --git a/src/input/DBusInputDriver.cc b/src/input/DBusInputDriver.cc index 2bf58201..8ef5009c 100644 --- a/src/input/DBusInputDriver.cc +++ b/src/input/DBusInputDriver.cc @@ -34,7 +34,7 @@ void DBusInputDriver::run() } -DBusInputDriver::DBusInputDriver() +DBusInputDriver::DBusInputDriver() : is_open(false) { } @@ -44,8 +44,22 @@ DBusInputDriver::~DBusInputDriver() } +bool DBusInputDriver::openOnce() +{ + return true; +} + +bool DBusInputDriver::isOpen() +{ + +} + bool DBusInputDriver::open() { + if (is_open) { + return true; + } + if (!QDBusConnection::sessionBus().isConnected()) { return false; } @@ -62,6 +76,7 @@ bool DBusInputDriver::open() connect(iface, SIGNAL(rotateTo(double, double, double)), this, SLOT(rotateTo(double, double, double))); connect(iface, SIGNAL(translate(double, double, double)), this, SLOT(translate(double, double, double))); connect(iface, SIGNAL(translateTo(double, double, double)), this, SLOT(translateTo(double, double, double))); + is_open = true; return true; } diff --git a/src/input/DBusInputDriver.h b/src/input/DBusInputDriver.h index 211adb60..f280d20a 100644 --- a/src/input/DBusInputDriver.h +++ b/src/input/DBusInputDriver.h @@ -31,12 +31,16 @@ class DBusInputDriver : public InputDriver { Q_OBJECT + bool is_open; + public: DBusInputDriver(); virtual ~DBusInputDriver(); virtual void run(); virtual bool open(); virtual void close(); + virtual bool isOpen(); + virtual bool openOnce(); virtual const std::string & get_name() const; diff --git a/src/input/InputDriver.cc b/src/input/InputDriver.cc index a77b6139..4056fd73 100644 --- a/src/input/InputDriver.cc +++ b/src/input/InputDriver.cc @@ -36,3 +36,13 @@ InputDriver::~InputDriver() { } + +bool InputDriver::isOpen() +{ + return isRunning(); +} + +bool InputDriver::openOnce() +{ + return false; +} diff --git a/src/input/InputDriver.h b/src/input/InputDriver.h index 892030b0..d3aeed1b 100644 --- a/src/input/InputDriver.h +++ b/src/input/InputDriver.h @@ -123,4 +123,16 @@ public: virtual bool open() = 0; virtual void close() = 0; + + /* + * Return if the driver is currently opened. The default implementation + * simply returns the {@link #isRunning()} status of the thread. + */ + virtual bool isOpen(); + + /* + * Drivers that are not connected to a device and can be opened on + * application start. No attempt to re-open is made. + */ + virtual bool openOnce(); }; diff --git a/src/input/InputDriverManager.cc b/src/input/InputDriverManager.cc index 15459506..34a91b3b 100644 --- a/src/input/InputDriverManager.cc +++ b/src/input/InputDriverManager.cc @@ -33,6 +33,7 @@ InputDriverManager::InputDriverManager() { currentWindow = 0; connect(QApplication::instance(), SIGNAL(focusChanged(QWidget *, QWidget *)), this, SLOT(onFocusChanged(QWidget *, QWidget *))); + timer = new QTimer(this); } InputDriverManager::~InputDriverManager() @@ -59,14 +60,46 @@ void InputDriverManager::unregisterDriver(InputDriver *driver) } void InputDriverManager::init() +{ + doOpen(true); + connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout())); + timer->start(10 * 1000); +} + +void InputDriverManager::onTimeout() +{ + for (drivers_t::iterator it = drivers.begin(); it != drivers.end(); it++) { + InputDriver *driver = (*it); + if (driver->openOnce()) { + continue; + } + if (driver->isOpen()) { + return; + } + } + doOpen(false); +} + +void InputDriverManager::doOpen(bool firstOpen) { for (drivers_t::iterator it = drivers.begin();it != drivers.end();it++) { - PRINTB("trying to open %s\n", (*it)->get_name().c_str()); - if ((*it)->open()) { - PRINTB("using %s\n", (*it)->get_name().c_str()); + InputDriver *driver = (*it); + if (driver->openOnce()) { + continue; + } + if (driver->open()) { break; } } + + if (firstOpen) { + for (drivers_t::iterator it = drivers.begin();it != drivers.end();it++) { + InputDriver *driver = (*it); + if (driver->openOnce()) { + driver->open(); + } + } + } } std::string InputDriverManager::listDrivers() @@ -74,7 +107,11 @@ std::string InputDriverManager::listDrivers() std::stringstream stream; const char *sep = ""; for (drivers_t::iterator it = drivers.begin();it != drivers.end();it++) { - stream << sep << (*it)->get_name(); + InputDriver *driver = (*it); + stream << sep << driver->get_name(); + if (driver->isOpen()) { + stream << "*"; + } sep = ", "; } return stream.str(); diff --git a/src/input/InputDriverManager.h b/src/input/InputDriverManager.h index 5f1443ba..98cc922a 100644 --- a/src/input/InputDriverManager.h +++ b/src/input/InputDriverManager.h @@ -27,6 +27,7 @@ #include #include +#include #include "InputDriver.h" #include "MainWindow.h" @@ -41,6 +42,8 @@ private: MainWindow *currentWindow; + QTimer *timer; + static InputDriverManager *self; public: @@ -57,5 +60,7 @@ public: static InputDriverManager * instance(); private slots: + void onTimeout(); + void doOpen(bool firstOpen); void onFocusChanged(QWidget *, QWidget *); }; diff --git a/src/input/SpaceNavInputDriver.cc b/src/input/SpaceNavInputDriver.cc index ec302388..02c08084 100644 --- a/src/input/SpaceNavInputDriver.cc +++ b/src/input/SpaceNavInputDriver.cc @@ -47,11 +47,9 @@ SpaceNavInputDriver::~SpaceNavInputDriver() void SpaceNavInputDriver::run() { - while (true) { + while (spnav_input()) { QThread::msleep(20); spnav_remove_events(SPNAV_EVENT_MOTION); - - spnav_input(); } } @@ -60,12 +58,12 @@ void SpaceNavInputDriver::run() * one event is available and then processes all events until the queue is * empty. */ -void SpaceNavInputDriver::spnav_input(void) +bool SpaceNavInputDriver::spnav_input(void) { spnav_event ev; if (spnav_wait_event(&ev) == 0) { - return; + return false; } do { @@ -100,6 +98,7 @@ void SpaceNavInputDriver::spnav_input(void) InputDriverManager::instance()->postEvent(event); } } while (spnav_poll_event(&ev)); + return true; } bool SpaceNavInputDriver::open() diff --git a/src/input/SpaceNavInputDriver.h b/src/input/SpaceNavInputDriver.h index 31d0c98a..3c90812f 100644 --- a/src/input/SpaceNavInputDriver.h +++ b/src/input/SpaceNavInputDriver.h @@ -42,5 +42,5 @@ public: virtual const std::string & get_name() const; private: - void spnav_input(); + bool spnav_input(); };