Sunday, June 27, 2010

Using vibration API in Maemo

Recently I was creating some simple application that give notification when some new information is available in one site that I visit often.

This application play sound and vibrate when new information is available. In this blog I will share code required to enable vibration support in Maemo application and I am using Qt framework to implement my application.

In Maemo, Vibration API is based on DBUS call, with proper DBUS call you can enable disable vibration and apply certain vibration pattern.

To be able to use DBUS api, Qt required to enable DBUS support, which can be done by putting this line "Qt += dbus" in to .pro file.

Now we can use DBUS API to enable and apply vibration. API are from following headers.

#include <QtDBus>

#ifdef Q_WS_MAEMO_5
#include <mce mode-names.h>
#include <mce dbus-names.h>
#endif

by sending MCE_ENABLE_VIBRATOR dbus call, we tell OS to enable vibration support.

void Sample::enableVibration()
{
    #ifdef Q_WS_MAEMO_5
    mDbusInterface = new QDBusInterface(MCE_SERVICE, MCE_REQUEST_PATH,
                                     MCE_REQUEST_IF, QDBusConnection::systemBus(),
                                     this);

    mDbusInterface->call(MCE_ENABLE_VIBRATOR);

    #endif
}

Now that, vibration is enabled, we can apply certain vibration pattern. For my application is am enabling "PatternIncomingCall", After this call vibration will be on until we deactivate applied vibration patter.

void Sample::vibrate(int aTimeout)
{
    #ifdef Q_WS_MAEMO_5

    mDbusInterface->call(MCE_ACTIVATE_VIBRATOR_PATTERN, "PatternIncomingCall");
    QTimer::singleShot(aTimeout,this,SLOT(stopVibration()));
    #endif
}

And finally "MCE_DEACTIVATE_VIBRATOR_PATTERN" dbus call will deactivate vibration patter.

void Sample::stopVibration()
{
    #ifdef Q_WS_MAEMO_5
    mDbusInterface->call(MCE_DEACTIVATE_VIBRATOR_PATTERN, "PatternIncomingCall");
    #endif
} 

So this was all that required to enable vibration support for Maemo application.

Saturday, June 5, 2010

Snake game for Maemo

Past few weeks I was working on Snake game implementation for maemo. I tried to implement classic snake game in Qt as learning exercise. To make game more interesting, game is using Accelerometer to direct snake and there is also support for keyboard to direct snake.

Source code is already in gitorous. and installation file is also uploaded on maemo extra-testing repository. To install game on n900 enable either extra-devel or extra-testing and then from terminal type apt-get install snake.

Here are fews snaps from game for overview.

Friday, May 28, 2010

Trying out MeeGo SDK

Yesterday first release of MeeGo OS for netbook and N900 was out and with this SDK for MeeGo was also out.

I tried to install MeeGO SDK on my computer. Installation is easy and dose not required much user intreaction.

I also tried to develop helloworld application and with no effort I was able to create my first application for MeeGo device.

MeeGo use Qt as Application development framework and QtCreator is it's primary IDE.

MeeGo's site has vary good instruction for installation of SDK and getting started with development.

Here is link for SDK setup and Here is link for tutorial for getting started.

Finally here are few snaps from my SDK installation and my fist application for MeeGo device.

Tuesday, May 25, 2010

Enum value to String in Qt

Update:

it looks like that Qt framework made some changes that made staticQtMetaObject as protected.

Now to get staticQtMetaObject you need to use following code.
class StaticQtMetaObject : public QObject
{
public:
    static inline const QMetaObject& get() {return staticQtMetaObject;}
};
then use it like following
QString KeyboardOptionWidget::getKeyString( int aKeyCode )
{
    int index = StaticQtMetaObject::get().indexOfEnumerator("Key");
    QMetaEnum metaEnum = StaticQtMetaObject::get().enumerator( index);
    QString keyString = metaEnum.valueToKey( aKeyCode );
    return keyString.replace("Key_","");
    return QString();
}
--------------
Some time we might need to convert Enum value to string for various reason like logging or debugging. I also needed similar thing for one of my project.

I found one interesting code that uses Qt's meta-object system to convert Enum to String. I have created one similar function that convert key board code to string.

QString KeyboardOptionWidget::getKeyString( int aKeyCode )
{
    int index = QApplication::staticQtMetaObject.indexOfEnumerator("Key");
    QMetaEnum metaEnum = QApplication::staticQtMetaObject.enumerator( index);
 
    QString keyString = metaEnum.valueToKey( aKeyCode );
    return keyString.replace("Key_","");
} 

Hope this helps.

Tuesday, May 18, 2010

How to disable Blank display on Mameo from Qt application

I have created a game for N900 which take input from accelerometer and move game object around. In this case user don't have to touch on screen and platform was assuming that user it idle and is making display off.

To avoid display off, Maemo is providing DBUS API using which you can disable display off.

Following is Qt code to send dbus call to disable display off. You need to call this method at least once in every 60 seconds to avoid display off.

QDBusConnection::systemBus().call(QDBusMessage::createMethodCall(MCE_SERVICE, 
MCE_REQUEST_PATH,MCE_REQUEST_IF, MCE_PREVENT_BLANK_REQ));

You will also need to include "mce/mode-names.h","mce/dbus-names.h" and "QtDBus" headers and add dbus support in Qt's pro file.

I hope this will help to someone facing similar problem.