Allow simple re-open after the active driver closes.

This commit is contained in:
Torsten Paul 2015-07-19 18:09:36 +02:00
parent 4ba2f1aa4b
commit 7dc2a35f09
8 changed files with 93 additions and 11 deletions

View file

@ -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;
}

View file

@ -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;

View file

@ -36,3 +36,13 @@ InputDriver::~InputDriver()
{
}
bool InputDriver::isOpen()
{
return isRunning();
}
bool InputDriver::openOnce()
{
return false;
}

View file

@ -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();
};

View file

@ -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();

View file

@ -27,6 +27,7 @@
#include <QWidget>
#include <QThread>
#include <QTimer>
#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 *);
};

View file

@ -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()

View file

@ -42,5 +42,5 @@ public:
virtual const std::string & get_name() const;
private:
void spnav_input();
bool spnav_input();
};