<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- maemovibration.qdoc --> <title>Qt 4.8: Maemo Vibration Example</title> <link rel="stylesheet" type="text/css" href="style/style.css" /> <script src="scripts/jquery.js" type="text/javascript"></script> <script src="scripts/functions.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="style/superfish.css" /> <link rel="stylesheet" type="text/css" href="style/narrow.css" /> <!--[if IE]> <meta name="MSSmartTagsPreventParsing" content="true"> <meta http-equiv="imagetoolbar" content="no"> <![endif]--> <!--[if lt IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie6.css"> <![endif]--> <!--[if IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie7.css"> <![endif]--> <!--[if IE 8]> <link rel="stylesheet" type="text/css" href="style/style_ie8.css"> <![endif]--> <script src="scripts/superfish.js" type="text/javascript"></script> <script src="scripts/narrow.js" type="text/javascript"></script> </head> <body class="" onload="CheckEmptyAndLoadList();"> <div class="header" id="qtdocheader"> <div class="content"> <div id="nav-logo"> <a href="index.html">Home</a></div> <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a> <div id="narrowsearch"></div> <div id="nav-topright"> <ul> <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li> <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li> <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/"> DOC</a></li> <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li> </ul> </div> <div id="shortCut"> <ul> <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li> <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS </a></li> </ul> </div> <ul class="sf-menu" id="narrowmenu"> <li><a href="#">API Lookup</a> <ul> <li><a href="classes.html">Class index</a></li> <li><a href="functions.html">Function index</a></li> <li><a href="modules.html">Modules</a></li> <li><a href="namespaces.html">Namespaces</a></li> <li><a href="qtglobal.html">Global Declarations</a></li> <li><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </li> <li><a href="#">Qt Topics</a> <ul> <li><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li><a href="supported-platforms.html">Supported Platforms</a></li> <li><a href="technology-apis.html">Qt and Key Technologies</a></li> <li><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </li> <li><a href="#">Examples</a> <ul> <li><a href="all-examples.html">Examples</a></li> <li><a href="tutorials.html">Tutorials</a></li> <li><a href="demos.html">Demos</a></li> <li><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </li> </ul> </div> </div> <div class="wrapper"> <div class="hd"> <span></span> </div> <div class="bd group"> <div class="sidebar"> <div class="searchlabel"> Search index:</div> <div class="search" id="sidebarsearch"> <form id="qtdocsearch" action="" onsubmit="return false;"> <fieldset> <input type="text" name="searchstring" id="pageType" value="" /> <div id="resultdialog"> <a href="#" id="resultclose">Close</a> <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span> results:</p> <ul id="resultlist" class="all"> </ul> </div> </fieldset> </form> </div> <div class="box first bottombar" id="lookup"> <h2 title="API Lookup"><span></span> API Lookup</h2> <div id="list001" class="list"> <ul id="ul001" > <li class="defaultLink"><a href="classes.html">Class index</a></li> <li class="defaultLink"><a href="functions.html">Function index</a></li> <li class="defaultLink"><a href="modules.html">Modules</a></li> <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li> <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li> <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </div> </div> <div class="box bottombar" id="topics"> <h2 title="Qt Topics"><span></span> Qt Topics</h2> <div id="list002" class="list"> <ul id="ul002" > <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li class="defaultLink"><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li> <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </div> </div> <div class="box" id="examples"> <h2 title="Examples"><span></span> Examples</h2> <div id="list003" class="list"> <ul id="ul003"> <li class="defaultLink"><a href="all-examples.html">Examples</a></li> <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li> <li class="defaultLink"><a href="demos.html">Demos</a></li> <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </div> </div> </div> <div class="wrap"> <div class="toolbar"> <div class="breadcrumb toolblock"> <ul> <li class="first"><a href="index.html">Home</a></li> <!-- Breadcrumbs go here --> <li><a href="all-examples.html">Examples</a></li> <li>Maemo Vibration Example</li> </ul> </div> <div class="toolbuttons toolblock"> <ul> <li id="smallA" class="t_button">A</li> <li id="medA" class="t_button active">A</li> <li id="bigA" class="t_button">A</li> <li id="print" class="t_button"><a href="javascript:this.print();"> <span>Print</span></a></li> </ul> </div> </div> <div class="content mainContent"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#mcevibrator-class-definition">MceVibrator Class Definition</a></li> <li class="level1"><a href="#mcevibrator-class-implementation">MceVibrator Class Implementation</a></li> <li class="level1"><a href="#buttonwidget-class-definition">ButtonWidget Class Definition</a></li> <li class="level1"><a href="#buttonwidget-class-implementation">ButtonWidget Class Implementation</a></li> <li class="level1"><a href="#function"><tt>main()</tt> Function</a></li> </ul> </div> <h1 class="title">Maemo Vibration Example</h1> <span class="subtitle"></span> <!-- $$$widgets/maemovibration-description --> <div class="descr"> <a name="details"></a> <p>Files:</p> <ul> <li><a href="widgets-maemovibration-buttonwidget-cpp.html">widgets/maemovibration/buttonwidget.cpp</a></li> <li><a href="widgets-maemovibration-buttonwidget-h.html">widgets/maemovibration/buttonwidget.h</a></li> <li><a href="widgets-maemovibration-mcevibrator-cpp.html">widgets/maemovibration/mcevibrator.cpp</a></li> <li><a href="widgets-maemovibration-mcevibrator-h.html">widgets/maemovibration/mcevibrator.h</a></li> <li><a href="widgets-maemovibration-main-cpp.html">widgets/maemovibration/main.cpp</a></li> <li><a href="widgets-maemovibration-maemovibration-pro.html">widgets/maemovibration/maemovibration.pro</a></li> </ul> <p>The Maemo Vibration example shows how to tell the Maemo Mode Control Entity (MCE) to vibrate a maemo device.<p>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.</p> <p>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.</p> <p class="centerAlign"><img src="images/maemovibration-example.png" alt="Screenshot of the Maemo Vibration Example" /></p><p>The code makes use of two classes:</p> <ul> <li><tt>MceVibrator</tt> connects to the MCE service and can start a certain vibrator pattern. It also is responsible to parse the configuration file.</li> <li><tt>ButtonWidget</tt> provides a button for each pattern. Pressing the button activates the pattern in question.</li> </ul> <a name="mcevibrator-class-definition"></a> <h2>MceVibrator Class Definition</h2> <pre class="cpp"> <span class="keyword">class</span> MceVibrator : <span class="keyword">public</span> <span class="type"><a href="qobject.html">QObject</a></span> { Q_OBJECT <span class="keyword">public</span>: <span class="keyword">explicit</span> MceVibrator(<span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>); <span class="operator">~</span>MceVibrator(); <span class="keyword">static</span> <span class="keyword">const</span> <span class="type">char</span> defaultMceFilePath<span class="operator">[</span><span class="operator">]</span>; <span class="keyword">static</span> <span class="type"><a href="qstringlist.html">QStringList</a></span> parsePatternNames(<span class="type"><a href="qtextstream.html">QTextStream</a></span> <span class="operator">&</span>stream); <span class="keyword">public</span> <span class="keyword">slots</span>: <span class="type">void</span> vibrate(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>patternName); <span class="keyword">private</span>: <span class="type">void</span> deactivate(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>patternName); <span class="type"><a href="qdbusinterface.html">QDBusInterface</a></span> mceInterface; <span class="type"><a href="qstring.html">QString</a></span> lastPatternName; };</pre> <p>The <tt>MceVibrator</tt> class inherits from <a href="qobject.html">QObject</a> and provides a specialized and Qt friendly interface to the MCE vibration facilty. The slot <tt>vibrate()</tt> can be called to make the device vibrate according to a specific pattern name. We will connect it to a signal of a <tt>ButtonWidget</tt> object later. The static method <tt>ParsePatternNames()</tt> can be called to find out which patterns are available to us.</p> <ul> <li><tt>mceInterface</tt> is our D-Bus handle to the MCE service. We use it to invoke methods on the MCE request object.</li> <li><tt>lastPatternName</tt> 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.</li> </ul> <a name="mcevibrator-class-implementation"></a> <h2>MceVibrator Class Implementation</h2> <p>To connect to the service, we initialize the D-Bus interface handle. The system header <tt>"mce/dbus-names.h"</tt> 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.</p> <p>The MCE expects us to first enable the vibrator before we can use it. This is done with the call to the <tt>MCE_ENABLE_VIBRATOR</tt> D-Bus method.</p> <pre class="cpp"> MceVibrator<span class="operator">::</span>MceVibrator(<span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>parent) : <span class="type"><a href="qobject.html">QObject</a></span>(parent)<span class="operator">,</span> mceInterface(MCE_SERVICE<span class="operator">,</span> MCE_REQUEST_PATH<span class="operator">,</span> MCE_REQUEST_IF<span class="operator">,</span> <span class="type"><a href="qdbusconnection.html">QDBusConnection</a></span><span class="operator">::</span>systemBus()) { <span class="type"><a href="qdbusmessage.html">QDBusMessage</a></span> reply <span class="operator">=</span> mceInterface<span class="operator">.</span>call(MCE_ENABLE_VIBRATOR); checkError(reply); }</pre> <p>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.</p> <pre class="cpp"> <span class="type">void</span> MceVibrator<span class="operator">::</span>vibrate(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>patternName) { deactivate(lastPatternName); lastPatternName <span class="operator">=</span> patternName; <span class="type"><a href="qdbusmessage.html">QDBusMessage</a></span> reply <span class="operator">=</span> mceInterface<span class="operator">.</span>call(MCE_ACTIVATE_VIBRATOR_PATTERN<span class="operator">,</span> patternName); checkError(reply); }</pre> <p>The calls to the private method deactivate simply makes sure to deactivate the last pattern used, if there was one.</p> <pre class="cpp"> <span class="type">void</span> MceVibrator<span class="operator">::</span>deactivate(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>patternName) { <span class="keyword">if</span> (<span class="operator">!</span>patternName<span class="operator">.</span>isNull()) { <span class="type"><a href="qdbusmessage.html">QDBusMessage</a></span> reply <span class="operator">=</span> mceInterface<span class="operator">.</span>call(MCE_DEACTIVATE_VIBRATOR_PATTERN<span class="operator">,</span> patternName); checkError(reply); } }</pre> <p>Calling either the activate or deactivate MCE D-Bus method with invalid pattern names are ignored.</p> <p>Finally, the destructor disables the vibrator. When the destructor of the MCE interface handle is called, the connection is also closed.</p> <pre class="cpp"> MceVibrator<span class="operator">::</span><span class="operator">~</span>MceVibrator() { deactivate(lastPatternName); <span class="type"><a href="qdbusmessage.html">QDBusMessage</a></span> reply <span class="operator">=</span> mceInterface<span class="operator">.</span>call(MCE_DISABLE_VIBRATOR); checkError(reply); }</pre> <p>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:</p> <pre class="cpp"> VibratorPatterns<span class="operator">=</span>semicolon;separated;list;of;values</pre> <p>The static method <tt>ParsePatternNames</tt> looks for this line and returns a <a href="qstringlist.html">QStringList</a> containing the values, which are the pattern names we can use.</p> <pre class="cpp"> <span class="type"><a href="qstringlist.html">QStringList</a></span> MceVibrator<span class="operator">::</span>parsePatternNames(<span class="type"><a href="qtextstream.html">QTextStream</a></span> <span class="operator">&</span>stream) { <span class="type"><a href="qstringlist.html">QStringList</a></span> result; <span class="type"><a href="qstring.html">QString</a></span> line; <span class="keyword">do</span> { line <span class="operator">=</span> stream<span class="operator">.</span>readLine(); <span class="keyword">if</span> (line<span class="operator">.</span>startsWith(QLatin1String(<span class="string">"VibratorPatterns="</span>))) { <span class="type"><a href="qstring.html">QString</a></span> values <span class="operator">=</span> line<span class="operator">.</span>section(<span class="char">'='</span><span class="operator">,</span> <span class="number">1</span>); result <span class="operator">=</span> values<span class="operator">.</span>split(<span class="char">';'</span>); <span class="keyword">break</span>; } } <span class="keyword">while</span> (<span class="operator">!</span>line<span class="operator">.</span>isNull()); <span class="keyword">return</span> result; }</pre> <p>The helper function <tt>checkError()</tt> 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.</p> <pre class="cpp"> <span class="keyword">static</span> <span class="type">void</span> checkError(<span class="type"><a href="qdbusmessage.html">QDBusMessage</a></span> <span class="operator">&</span>msg) { <span class="keyword">if</span> (msg<span class="operator">.</span>type() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qdbusmessage.html">QDBusMessage</a></span><span class="operator">::</span>ErrorMessage) <a href="qtglobal.html#qDebug">qDebug</a>() <span class="operator"><</span><span class="operator"><</span> msg<span class="operator">.</span>errorName() <span class="operator"><</span><span class="operator"><</span> msg<span class="operator">.</span>errorMessage(); }</pre> <a name="buttonwidget-class-definition"></a> <h2>ButtonWidget Class Definition</h2> <pre class="cpp"> <span class="keyword">class</span> ButtonWidget : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span> { Q_OBJECT <span class="keyword">public</span>: ButtonWidget(<span class="type"><a href="qstringlist.html">QStringList</a></span> texts<span class="operator">,</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>); <span class="keyword">signals</span>: <span class="type">void</span> clicked(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>text); <span class="keyword">private</span>: <span class="type"><a href="qsignalmapper.html">QSignalMapper</a></span> <span class="operator">*</span>signalMapper; };</pre> <p>The <tt>ButtonWidget</tt> class inherits from <a href="qwidget.html">QWidget</a> 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 <tt>clicked()</tt> signal, where the string is the text of the button that was pressed.</p> <p>This class is taken from the <a href="qsignalmapper.html">QSignalMapper</a> documentation. The only change is the number of columns in the grid from three to two, to make the button labels fit.</p> <a name="buttonwidget-class-implementation"></a> <h2>ButtonWidget Class Implementation</h2> <pre class="cpp"> ButtonWidget<span class="operator">::</span>ButtonWidget(<span class="type"><a href="qstringlist.html">QStringList</a></span> texts<span class="operator">,</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent) : <span class="type"><a href="qwidget.html">QWidget</a></span>(parent) { signalMapper <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qsignalmapper.html">QSignalMapper</a></span>(<span class="keyword">this</span>); <span class="type"><a href="qgridlayout.html">QGridLayout</a></span> <span class="operator">*</span>gridLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgridlayout.html">QGridLayout</a></span>; <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator"><</span> texts<span class="operator">.</span>size(); <span class="operator">+</span><span class="operator">+</span>i) { <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>button <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(texts<span class="operator">[</span>i<span class="operator">]</span>); connect(button<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> signalMapper<span class="operator">,</span> SLOT(map())); signalMapper<span class="operator">-</span><span class="operator">></span>setMapping(button<span class="operator">,</span> texts<span class="operator">[</span>i<span class="operator">]</span>); gridLayout<span class="operator">-</span><span class="operator">></span>addWidget(button<span class="operator">,</span> i <span class="operator">/</span> <span class="number">2</span><span class="operator">,</span> i <span class="operator">%</span> <span class="number">2</span>); } connect(signalMapper<span class="operator">,</span> SIGNAL(mapped(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SIGNAL(clicked(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>))); setLayout(gridLayout); }</pre> <a name="function"></a> <h2><tt>main()</tt> Function</h2> <p>The main function begins with looking up the patterns available to us.</p> <pre class="cpp"> <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>) { <span class="type"><a href="qapplication.html">QApplication</a></span> a(argc<span class="operator">,</span> argv); <span class="type"><a href="qstring.html">QString</a></span> path <span class="operator">=</span> MceVibrator<span class="operator">::</span>defaultMceFilePath; <span class="type"><a href="qfile.html">QFile</a></span> file(path); <span class="type"><a href="qstringlist.html">QStringList</a></span> names; <span class="keyword">if</span> (file<span class="operator">.</span>open(<span class="type"><a href="qiodevice.html">QIODevice</a></span><span class="operator">::</span>ReadOnly <span class="operator">|</span> <span class="type"><a href="qiodevice.html">QIODevice</a></span><span class="operator">::</span>Text)) { <span class="type"><a href="qtextstream.html">QTextStream</a></span> stream(<span class="operator">&</span>file); names <span class="operator">=</span> MceVibrator<span class="operator">::</span>parsePatternNames(stream); file<span class="operator">.</span>close(); } <span class="keyword">if</span> (names<span class="operator">.</span>isEmpty()){ <a href="qtglobal.html#qDebug">qDebug</a>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Could not read vibration pattern names from "</span> <span class="operator"><</span><span class="operator"><</span> path; a<span class="operator">.</span>exit(<span class="operator">-</span><span class="number">1</span>); }</pre> <p>Then we create one instance of both classes, and connects the <tt>ButtonWidget</tt>'s clicked signal to the <tt>MceVibrator</tt>'s <tt>vibrate()</tt> slot. This works, since the button texts are the same as the pattern names.</p> <pre class="cpp"> ButtonWidget buttonWidget(names); MceVibrator vibrator; <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect(<span class="operator">&</span>buttonWidget<span class="operator">,</span> SIGNAL(clicked(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>))<span class="operator">,</span> <span class="operator">&</span>vibrator<span class="operator">,</span> SLOT(vibrate(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>))); buttonWidget<span class="operator">.</span>show(); <span class="keyword">return</span> a<span class="operator">.</span>exec(); }</pre> </div> <!-- @@@widgets/maemovibration --> </div> </div> </div> <div class="ft"> <span></span> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2015 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.</p> <br /> <p> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.</p> <p> Documentation sources may be obtained from <a href="http://www.qt-project.org"> www.qt-project.org</a>.</p> <br /> <p> 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. <a title="Privacy Policy" href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p> </div> <script src="scripts/functions.js" type="text/javascript"></script> </body> </html>