Qt Reference Documentation

Contents

Maemo Vibration Example

Files:

The Maemo Vibration example shows how to tell the Maemo Mode Control Entity (MCE) to vibrate a maemo device.

The MCE is a system service on Maemo that, among other things, provides an D-Bus interface to trigger vibrations. The vibrations are specified as patterns and are defined in a system configuration file.

The example program reads the configuration file to look for possible vibration patterns and display a button for each. Pressing a button will make the device vibrate accordingly, until the application closes, or another pattern is started.

Screenshot of the Maemo Vibration Example

The code makes use of two classes:

MceVibrator Class Definition

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

     static const char defaultMceFilePath[];
     static QStringList parsePatternNames(QTextStream &stream);

 public slots:
     void vibrate(const QString &patternName);

 private:
     void deactivate(const QString &patternName);

     QDBusInterface mceInterface;
     QString lastPatternName;
 };

The MceVibrator class inherits from QObject and provides a specialized and Qt friendly interface to the MCE vibration facilty. The slot vibrate() can be called to make the device vibrate according to a specific pattern name. We will connect it to a signal of a ButtonWidget object later. The static method ParsePatternNames() can be called to find out which patterns are available to us.

MceVibrator Class Implementation

To connect to the service, we initialize the D-Bus interface handle. The system header "mce/dbus-names.h" contains definitions of the D-Bus service name and request object path and interface. These are passed to the constructor of the handle, and Qt will automatically establish a connection to it, if it is possible.

The MCE expects us to first enable the vibrator before we can use it. This is done with the call to the MCE_ENABLE_VIBRATOR D-Bus method.

 MceVibrator::MceVibrator(QObject *parent) :
     QObject(parent),
     mceInterface(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF,
                    QDBusConnection::systemBus())
 {
     QDBusMessage reply = mceInterface.call(MCE_ENABLE_VIBRATOR);
     checkError(reply);
 }

From now on we can activate vibration patterns. Each time a vibration pattern is activated, the last pattern has to be deactivated first. In the vibrate slot we use the MCE interface to call the activation method.

 void MceVibrator::vibrate(const QString &patternName)
 {
     deactivate(lastPatternName);
     lastPatternName = patternName;
     QDBusMessage reply = mceInterface.call(MCE_ACTIVATE_VIBRATOR_PATTERN, patternName);
     checkError(reply);
 }

The calls to the private method deactivate simply makes sure to deactivate the last pattern used, if there was one.

 void MceVibrator::deactivate(const QString &patternName)
 {
     if (!patternName.isNull()) {
         QDBusMessage reply = mceInterface.call(MCE_DEACTIVATE_VIBRATOR_PATTERN, patternName);
         checkError(reply);
     }
 }

Calling either the activate or deactivate MCE D-Bus method with invalid pattern names are ignored.

Finally, the destructor disables the vibrator. When the destructor of the MCE interface handle is called, the connection is also closed.

 MceVibrator::~MceVibrator()
 {
     deactivate(lastPatternName);
     QDBusMessage reply = mceInterface.call(MCE_DISABLE_VIBRATOR);
     checkError(reply);
 }

The MCE configuration file contains options for many different things. We are only interested in one line that contains the vibration patterns. It has the following format:

 VibratorPatterns=semicolon;separated;list;of;values

The static method ParsePatternNames looks for this line and returns a QStringList containing the values, which are the pattern names we can use.

 QStringList MceVibrator::parsePatternNames(QTextStream &stream)
 {
     QStringList result;
     QString line;

     do {
         line = stream.readLine();
         if (line.startsWith(QLatin1String("VibratorPatterns="))) {
             QString values = line.section('=', 1);
             result = values.split(';');
             break;
         }
     } while (!line.isNull());

     return result;
 }

The helper function checkError() saves us some code duplication. None of the called methods return anything of use to us, so we're only interested in getting error messages for debugging.

 static void checkError(QDBusMessage &msg)
 {
     if (msg.type() == QDBusMessage::ErrorMessage)
         qDebug() << msg.errorName() << msg.errorMessage();
 }

ButtonWidget Class Definition

 class ButtonWidget : public QWidget
 {
     Q_OBJECT

 public:
     ButtonWidget(QStringList texts, QWidget *parent = 0);

 signals:
     void clicked(const QString &text);

 private:
     QSignalMapper *signalMapper;
 };

The ButtonWidget class inherits from QWidget and provides the main user interface for the application. It creates a grid of buttons, one for each string in the stringlist passed to the constructor. Pressing a button emits the clicked() signal, where the string is the text of the button that was pressed.

This class is taken from the QSignalMapper documentation. The only change is the number of columns in the grid from three to two, to make the button labels fit.

ButtonWidget Class Implementation

 ButtonWidget::ButtonWidget(QStringList texts, QWidget *parent)
     : QWidget(parent)
 {
     signalMapper = new QSignalMapper(this);

     QGridLayout *gridLayout = new QGridLayout;
     for (int i = 0; i < texts.size(); ++i) {
         QPushButton *button = new QPushButton(texts[i]);
         connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
         signalMapper->setMapping(button, texts[i]);
         gridLayout->addWidget(button, i / 2, i % 2);
     }

     connect(signalMapper, SIGNAL(mapped(const QString &)),
             this, SIGNAL(clicked(const QString &)));

     setLayout(gridLayout);
 }

main() Function

The main function begins with looking up the patterns available to us.

 int main(int argc, char *argv[])
 {
     QApplication a(argc, argv);
         QString path = MceVibrator::defaultMceFilePath;

     QFile file(path);
     QStringList names;
     if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
         QTextStream stream(&file);
         names = MceVibrator::parsePatternNames(stream);
         file.close();
     }

     if (names.isEmpty()){
         qDebug() << "Could not read vibration pattern names from " << path;
         a.exit(-1);
     }

Then we create one instance of both classes, and connects the ButtonWidget's clicked signal to the MceVibrator's vibrate() slot. This works, since the button texts are the same as the pattern names.

     ButtonWidget buttonWidget(names);
     MceVibrator vibrator;
     QObject::connect(&buttonWidget, SIGNAL(clicked(const QString &)),
                      &vibrator, SLOT(vibrate(const QString &)));
     buttonWidget.show();

     return a.exec();
 }