Maemo Vibration Example

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 connects to the MCE service and can start a certain vibrator pattern. It also is responsible to parse the configuration file.
  • ButtonWidget provides a button for each pattern. Pressing the button activates the pattern in question.

MceVibrator Class Definition

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

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

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

    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.

  • mceInterface is our D-Bus handle to the MCE service. We use it to invoke methods on the MCE request object.
  • lastPatternName contains the pattern that was activated last time. We have to keep track of this, because the last pattern has to be deactivated before activating a new pattern.

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) :
    QDBusMessage 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)
    lastPatternName = patternName;
    QDBusMessage reply =, patternName);

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 =, patternName);

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.

    QDBusMessage 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:


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

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

    void clicked(const QString &text);

    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 &)));


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 ( | QIODevice::Text)) {
        QTextStream stream(&file);
        names = MceVibrator::parsePatternNames(stream);

    if (names.isEmpty()){
        qDebug() << "Could not read vibration pattern names from " << path;

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 &)));;

    return a.exec();


© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.