Active Topics

 


Reply
Thread Tools
erendorn's Avatar
Posts: 738 | Thanked: 983 times | Joined on Apr 2010 @ London
#1
Basically, I'd like to try to code up a service that counts time and listen to dbus to trigger specific actions. (nothing fancy yet)

I am fairly familiar with c++, but I am completely lost in the "setting framework and pieces" phase that sadly comes first. There are a lot of documentation out there, but their not consistent between one another

I'm using the Qt SDK as I thought it would be useful to get familiar with it, and as Qt provides dbus abstraction. I've chosen "Qt Console Application" template, does that make sense?

I tried to inherit from a QtService class, but it does not seem to find the includes. How do I add include paths in a Qt SDK project?

Or is there a more straightforward way to create a service? (ie what do I need to implement?)
Targets would be maemo 5, meego, and linux desktop.

Any advice appreciated.. after that I should be good on my own.

thanks!
 
Posts: 435 | Thanked: 769 times | Joined on Apr 2010
#2
When you set a console only application, QtCreator creates a daemon. When in the end of the main function you return app.exec it means your application entered the event loop and won't exit by itself.
Subclass QObject and set your slots and connections, create a new istance of it in main.cpp and you're done.
 

The Following User Says Thank You to gionni88 For This Useful Post:
erendorn's Avatar
Posts: 738 | Thanked: 983 times | Joined on Apr 2010 @ London
#3
Thanks, less fog now

Do I need to do anything with the "app" object or will "Qt" statically interact with it in the background??
 
Posts: 435 | Thanked: 769 times | Joined on Apr 2010
#4
The QObject inherited class just need to be created in the main.cpp.

That class will have its own slots connected to your desidered dbus signals. After you create that object ( a "new MYAPP;" is enought if you can manage all functions within your defined class) all connections will be enstablished and there is nothing else you have to add.

Here a simple daemon that keeps backlight value at a set value. The only "event" triggering something is the fileChanged() signal from QFileSystemWatcher.

main.cpp
Code:
#include "backlighthandler.h"

#include <QtCore/QCoreApplication>



int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    new BacklightHandler;


    return a.exec();
}
backlighthandler.h
Code:
#ifndef BACKLIGHTHANDLER_H
#define BACKLIGHTHANDLER_H

#include <QObject>

#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QFileSystemWatcher>
#include <QStringList>

class BacklightHandler : public QObject
{
    Q_OBJECT
public:
    explicit BacklightHandler(QObject *parent = 0);

signals:

public slots:

private:
    int backlightValue;
    int defaultBacklightValue;

    QFileSystemWatcher *backlightFileWatcher;

    void setBacklight(int);
    int readBacklightValue();

private slots:
    void backlightValueChanged();

};

#endif // BACKLIGHTHANDLER_H
backlighthandler.cpp
Code:
#include "backlighthandler.h"

BacklightHandler::BacklightHandler(QObject *parent) :
    QObject(parent)
{
    defaultBacklightValue = readBacklightValue();
    backlightValue = qApp->arguments().at(1).toInt();

    backlightFileWatcher = new QFileSystemWatcher;
    backlightFileWatcher->addPath("/sys/class/backlight/acx565akm/brightness");
    connect(backlightFileWatcher, SIGNAL(fileChanged(QString)), this, SLOT(backlightValueChanged()));

    setBacklight(backlightValue);

}

void BacklightHandler::setBacklight(int value)
{
    QString program = QString("echo 'echo %1 > /sys/class/backlight/acx565akm/brightness' | root").arg(QString::number(value));
    system(program.toStdString().c_str());

}

int BacklightHandler::readBacklightValue()
{
    qDebug()<<"Reading backlight value.";
    QFile file("/sys/class/backlight/acx565akm/brightness");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream t( &file );
    QString value;
    value = t.readLine();
    qDebug()<<"Backlight value: "<<value<<endl;
    file.close();

    return value.toInt();
}

void BacklightHandler::backlightValueChanged()
{
    qDebug()<<"Backlight value changed.";
    int tempValue;
    tempValue = readBacklightValue();

    if (tempValue == backlightValue)
    {
        qDebug()<<"Value is the same, returning.";
        return;
    }

    qDebug()<<"Value is different, editing file."<<endl;

    setBacklight(backlightValue);

}
 

The Following 2 Users Say Thank You to gionni88 For This Useful Post:
erendorn's Avatar
Posts: 738 | Thanked: 983 times | Joined on Apr 2010 @ London
#5
That's very helpful! Thanks again!
 
Reply


 
Forum Jump


All times are GMT. The time now is 21:30.