From 0d8b5446e880298d89511dd0bc0dbe328ccfce97 Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Sun, 11 Sep 2011 03:31:14 +0200 Subject: Try to revert to previous state on SIGINT/SIGTERM. --- fakeproperty.cpp | 33 ++++++++++++++++++++++++---- fakeproperty.h | 14 ++++++++---- fakepropertyadaptor.cpp | 15 ++++++++++--- fakepropertyadaptor.h | 5 ++++- faker.cpp | 25 ++++++++++++++++++++++ faker.h | 27 +++++++++++++++++++++++ glol.pro | 16 ++++++++------ main.cpp | 57 +++++++++++++++++++++++++++++++++++++++++-------- 8 files changed, 165 insertions(+), 27 deletions(-) create mode 100644 faker.cpp create mode 100644 faker.h diff --git a/fakeproperty.cpp b/fakeproperty.cpp index 38d9040..8eea148 100644 --- a/fakeproperty.cpp +++ b/fakeproperty.cpp @@ -1,16 +1,41 @@ #include "fakeproperty.h" -FakeProperty::FakeProperty(const QString& key, const QVariant& value, QObject *parent) : - QObject(parent), _key(key), _value(value) +FakeProperty::FakeProperty(const QString& key, QObject* parent) : + QObject(parent), _key(key), _value(), _timestamp(0) { } -const QString& FakeProperty::key() const +QString FakeProperty::key() const { return _key; } -const QVariant& FakeProperty::value() const +QVariant FakeProperty::value() const { return _value; } + +quint64 FakeProperty::timestamp() const +{ + return _timestamp; +} + +void FakeProperty::setValue(const QVariant &value) +{ + _value = value; + if (_timestamp) { + emit valueChanged(); + } +} + +void FakeProperty::startFaking() +{ + _timestamp = Q_INT64_C(-1); /* To have priority over other providers */ + emit valueChanged(); +} + +void FakeProperty::stopFaking() +{ + _timestamp = 0; /* To stop having priority over other providers */ + emit valueChanged(); +} diff --git a/fakeproperty.h b/fakeproperty.h index 4509b83..3f920ad 100644 --- a/fakeproperty.h +++ b/fakeproperty.h @@ -7,17 +7,23 @@ class FakeProperty : public QObject { Q_OBJECT public: - explicit FakeProperty(const QString& key, const QVariant& value, QObject *parent = 0); - const QString& key() const; - const QVariant& value() const; + explicit FakeProperty(const QString& key, QObject *parent = 0); + QString key() const; + QVariant value() const; + quint64 timestamp() const; signals: + void valueChanged(); public slots: + void setValue(const QVariant& value); + void startFaking(); + void stopFaking(); -private: +protected: QString _key; QVariant _value; + quint64 _timestamp; }; diff --git a/fakepropertyadaptor.cpp b/fakepropertyadaptor.cpp index 0a65c31..369c1c1 100644 --- a/fakepropertyadaptor.cpp +++ b/fakepropertyadaptor.cpp @@ -7,6 +7,7 @@ FakePropertyAdaptor::FakePropertyAdaptor(FakeProperty* property) : QDBusAbstractAdaptor(property), _property(property) { + connect(_property, SIGNAL(valueChanged()), SLOT(propertyChanged())); } QString FakePropertyAdaptor::objectPath() const @@ -20,17 +21,25 @@ QString FakePropertyAdaptor::objectPath() const void FakePropertyAdaptor::Subscribe(const QDBusMessage& msg, QVariantList& values, quint64& timestamp) { - qDebug() << "subscribe from " << msg.service(); + Q_UNUSED(msg); Get(values, timestamp); } void FakePropertyAdaptor::Unsubscribe(const QDBusMessage& msg) { - qDebug() << "unsubscribe from " << msg.service(); + Q_UNUSED(msg); } void FakePropertyAdaptor::Get(QVariantList& values, quint64& timestamp) { values << _property->value(); - timestamp = -1LL; + timestamp = _property->timestamp(); +} + +void FakePropertyAdaptor::propertyChanged() +{ + QVariantList values; + quint64 timestamp; + Get(values, timestamp); + emit ValueChanged(values, timestamp); } diff --git a/fakepropertyadaptor.h b/fakepropertyadaptor.h index 8293042..6c70d46 100644 --- a/fakepropertyadaptor.h +++ b/fakepropertyadaptor.h @@ -23,8 +23,11 @@ public slots: void Unsubscribe(const QDBusMessage& msg); void Get(QVariantList& values, quint64& timestamp); -private: +protected: FakeProperty* _property; + +protected slots: + void propertyChanged(); }; #endif // FAKEPROPERTYADAPTOR_H diff --git a/faker.cpp b/faker.cpp new file mode 100644 index 0000000..b512439 --- /dev/null +++ b/faker.cpp @@ -0,0 +1,25 @@ +#include "faker.h" + +#define SERVICE_NAME "com.javispedro.glol" + +Faker::Faker(const QString& val, QObject *parent) : + QObject(parent), _bus(QDBusConnection::sessionBus()), + _property(new FakeProperty("Screen.TopEdge")), + _adaptor(new FakePropertyAdaptor(_property)) +{ + _property->setValue(val); +} + +void Faker::start() +{ + _bus.registerObject(_adaptor->objectPath(), _property); + _bus.registerService(SERVICE_NAME); + _property->startFaking(); +} + +void Faker::stop() +{ + _property->stopFaking(); + _bus.unregisterService(SERVICE_NAME); + _bus.unregisterObject(_adaptor->objectPath()); +} diff --git a/faker.h b/faker.h new file mode 100644 index 0000000..7c2c149 --- /dev/null +++ b/faker.h @@ -0,0 +1,27 @@ +#ifndef FAKER_H +#define FAKER_H + +#include + +#include "fakeproperty.h" +#include "fakepropertyadaptor.h" + +class Faker : public QObject +{ + Q_OBJECT +public: + explicit Faker(const QString& val, QObject *parent = 0); + +signals: + +public slots: + void start(); + void stop(); + +protected: + QDBusConnection _bus; + FakeProperty *_property; + FakePropertyAdaptor *_adaptor; +}; + +#endif // FAKER_H diff --git a/glol.pro b/glol.pro index 0f1033c..2cb225d 100644 --- a/glol.pro +++ b/glol.pro @@ -14,11 +14,13 @@ TEMPLATE = app SOURCES += main.cpp \ fakepropertyadaptor.cpp \ - fakeproperty.cpp + fakeproperty.cpp \ + faker.cpp HEADERS += \ fakepropertyadaptor.h \ - fakeproperty.h + fakeproperty.h \ + faker.h OTHER_FILES += \ com.javispedro.glol.context \ @@ -30,9 +32,11 @@ OTHER_FILES += \ qtc_packaging/debian_harmattan/changelog unix:!symbian:!maemo5 { - target.path = /usr/bin + target.path = /usr/bin INSTALLS += target - contextfiles.path += /usr/share/contextkit/providers - contextfiles.files = com.javispedro.glol.context - INSTALLS += contextfiles + contextfiles.path += /usr/share/contextkit/providers + contextfiles.files = com.javispedro.glol.context + INSTALLS += contextfiles } + + diff --git a/main.cpp b/main.cpp index d112d28..26a1171 100644 --- a/main.cpp +++ b/main.cpp @@ -1,19 +1,58 @@ +#include +#include + #include -#include -#include +#include + +#include "faker.h" -#include "fakeproperty.h" -#include "fakepropertyadaptor.h" +static Faker* faker; + +static void printHelp() +{ + printf("glol -- temporarily lock orientation in all applications\n"); + printf("Use: glol \n"); +} + +static void handleSignal(int signal) +{ + Q_UNUSED(signal); + // Potentially unsafe. But... + faker->stop(); + QCoreApplication::instance()->quit(); +} int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); - QDBusConnection bus = QDBusConnection::sessionBus(); - FakeProperty *property = new FakeProperty("Screen.TopEdge", QString("top")); - FakePropertyAdaptor *adaptor = new FakePropertyAdaptor(property); - bus.registerObject(adaptor->objectPath(), property); - bus.registerService("com.javispedro.glol"); + QMap map; + QString val; + + if (a.arguments().count() != 2) { + printHelp(); + return 1; + } + + map["p"] = "left"; + map["portrait"] = "left"; + map["l"] = "top"; + map["landscape"] = "top"; + map["ip"] = "right"; + map["il"] = "bottom"; + + val = map[a.arguments().at(1).toLower()]; + if (val.isEmpty()) { + printHelp(); + return 1; + } + + signal(SIGINT, handleSignal); + signal(SIGTERM, handleSignal); + signal(SIGHUP, handleSignal); + + faker = new Faker(val); + faker->start(); return a.exec(); } -- cgit v1.2.3