diff options
-rw-r--r-- | libresourceqt/include/qt4/policy/audio-resource.h | 13 | ||||
-rw-r--r-- | libresourceqt/include/qt4/policy/resource-set.h | 6 | ||||
-rw-r--r-- | libresourceqt/src/audio-resource.cpp | 39 | ||||
-rw-r--r-- | libresourceqt/src/resource-engine.cpp | 55 | ||||
-rw-r--r-- | libresourceqt/src/resource-engine.h | 6 | ||||
-rw-r--r-- | libresourceqt/src/resource-set.cpp | 48 | ||||
-rw-r--r-- | resourceqt-client/client.cpp | 799 | ||||
-rw-r--r-- | resourceqt-client/resourceqt-client.cpp | 362 |
8 files changed, 646 insertions, 682 deletions
diff --git a/libresourceqt/include/qt4/policy/audio-resource.h b/libresourceqt/include/qt4/policy/audio-resource.h index 1736fdb..1ba5f5a 100644 --- a/libresourceqt/include/qt4/policy/audio-resource.h +++ b/libresourceqt/include/qt4/policy/audio-resource.h @@ -17,26 +17,29 @@ public: virtual ~AudioResource(); QString audioGroup() const; + bool audioGroupIsSet() const; void setAudioGroup(const QString & newGroup); quint32 processID() const; void setProcessID(quint32 newPID); - QString streamTag() const; - void setStreamTag(const QString & newStreamTag); + QString streamTagName() const; + QString streamTagValue() const; + bool streamTagIsSet() const; + void setStreamTag(const QString &name, const QString &value); virtual ResourceType type() const; virtual Resource * clone() const; private: QString group; quint32 pid; - QString stream; + QString streamName; + QString streamValue; signals: void pidChanged(quint32 newPid); void audioGroupChanged(const QString &newGroup); - void streamTagChanged(const QString &newTag); + void streamTagChanged(const QString &name, const QString &value); }; } #endif - diff --git a/libresourceqt/include/qt4/policy/resource-set.h b/libresourceqt/include/qt4/policy/resource-set.h index 8f0c7a6..fff63d1 100644 --- a/libresourceqt/include/qt4/policy/resource-set.h +++ b/libresourceqt/include/qt4/policy/resource-set.h @@ -34,7 +34,7 @@ * resources->addResource(videoResource); * \endcode * The resource set now has control over the Resource object pointers. You can - * drop them, but should NOT delete them. Instead call the ResourcePolicy::ResourceSet::deleteResource() + * drop them, but should NOT delete them. Instead call the ResourcePolicy::ResourceSet::deleteResource() * method. Then when you want to acquire the \ref ResourcePolicy::ResourceSet * you simply use the \ref ResourcePolicy::ResourceSetacquire() method, like this: * \code @@ -229,7 +229,7 @@ private: bool pendingAudioGroup; bool pendingAudioStream; bool pendingAudioPid; - + private slots: void connectedHandler(); void handleGranted(quint32); @@ -240,7 +240,7 @@ private slots: void handleAudioPidChange(quint32 newPid); void handleAudioGroupChange(const QString &newGroup); - void handleAudioStreamTagChanged(const QString &newGroup); + void handleAudioStreamTagChanged(const QString &name, const QString &value); }; } diff --git a/libresourceqt/src/audio-resource.cpp b/libresourceqt/src/audio-resource.cpp index 79587ff..b8c049f 100644 --- a/libresourceqt/src/audio-resource.cpp +++ b/libresourceqt/src/audio-resource.cpp @@ -3,12 +3,14 @@ using namespace ResourcePolicy; AudioResource::AudioResource(const QString &audioGroup) - : QObject(), Resource(), group(audioGroup), pid(0), stream(QString()) + : QObject(), Resource(), group(audioGroup), pid(0), + streamName(QString()), streamValue(QString()) { } AudioResource::AudioResource(const AudioResource &other) - : QObject(), Resource(other), group(other.group), pid(other.pid), stream(other.stream) + : QObject(), Resource(other), group(other.group), pid(other.pid), + streamName(other.streamName), streamValue(other.streamValue) { } @@ -26,6 +28,14 @@ QString AudioResource::audioGroup() const return group; } +bool AudioResource::audioGroupIsSet() const +{ + if (group.isEmpty() || group.isNull()) { + return false; + } + return true; +} + void AudioResource::setAudioGroup(const QString &newGroup) { qDebug("this = %p", this); @@ -45,15 +55,30 @@ void AudioResource::setProcessID(quint32 newPID) emit pidChanged(pid); } -QString AudioResource::streamTag() const +QString AudioResource::streamTagName() const +{ + return streamName; +} + +QString AudioResource::streamTagValue() const +{ + return streamValue; +} + +bool AudioResource::streamTagIsSet() const { - return stream; + if (streamName.isEmpty() || streamName.isNull() || + streamValue.isEmpty() || streamValue.isNull()) { + return false; + } + return true; } -void AudioResource::setStreamTag(const QString & newStreamTag) +void AudioResource::setStreamTag(const QString &name, const QString &value) { - stream = newStreamTag; - emit streamTagChanged(stream); + streamName = name; + streamValue = value; + emit streamTagChanged(name, value); } ResourceType AudioResource::type() const diff --git a/libresourceqt/src/resource-engine.cpp b/libresourceqt/src/resource-engine.cpp index 5c02b44..f788439 100644 --- a/libresourceqt/src/resource-engine.cpp +++ b/libresourceqt/src/resource-engine.cpp @@ -388,20 +388,42 @@ bool ResourceEngine::updateResources() return true; } -bool ResourceEngine::registerAudioPid(quint32) +bool ResourceEngine::registerAudioPid(quint32 pid) { - return false; + if(pid == 0) + return false; + + resmsg_t message; + memset(&message, 0, sizeof(resmsg_t)); + + message.audio.pid = pid; + + qDebug("audio pid %u", pid); + return sendAudioMessage(&message); } -bool ResourceEngine::registerAudioStreamTag(const QString &) +bool ResourceEngine::registerAudioStreamTag(const QString &name, const QString &value) { - return false; + if(name.isEmpty() || name.isNull() || value.isEmpty() || value.isNull()) + return false; + + resmsg_t message; + memset(&message, 0, sizeof(resmsg_t)); + + QByteArray nameBa = name.toLatin1(); + QByteArray valueBa = value.toLatin1(); + message.audio.property.name = nameBa.data(); + message.audio.property.match.method = resmsg_method_equals; + message.audio.property.match.pattern = valueBa.data(); + + qDebug() << "audio stream tag " << name << ":" << value; + return sendAudioMessage(&message); } bool ResourceEngine::registerAudioGroup(const QString &audioGroup) { - if((audioGroup == "") || (audioGroup == QString())) - return false; + if(audioGroup.isEmpty() || audioGroup.isNull()) + return false; resmsg_t message; memset(&message, 0, sizeof(resmsg_t)); @@ -409,28 +431,27 @@ bool ResourceEngine::registerAudioGroup(const QString &audioGroup) QByteArray ba = audioGroup.toLatin1(); message.audio.group = ba.data(); - message.audio.type = RESMSG_AUDIO; - message.audio.id = resourceSet->id(); - message.audio.reqno = ++requestId; + qDebug() << "audio group " << audioGroup; + return sendAudioMessage(&message); +} - message.audio.type = RESMSG_AUDIO; +bool ResourceEngine::sendAudioMessage(resmsg_t *message) +{ + message->audio.type = RESMSG_AUDIO; + message->audio.id = resourceSet->id(); + message->audio.reqno = ++requestId; -// msg.audio.pid = pid; -//stream tag is a name:value pair -// msg.audio.property.name = name; -// msg.audio.property.match.method = resmsg_method_equals; -// msg.audio.property.match.pattern = value; + message->audio.type = RESMSG_AUDIO; messageMap.insert(requestId, RESMSG_AUDIO); qDebug("audio %u:%u", resourceSet->id(), requestId); - int success = resproto_send_message(libresourceSet, &message, statusCallbackHandler); + int success = resproto_send_message(libresourceSet, message, statusCallbackHandler); if(!success) return false; else return true; - } static void connectionIsUp(resconn_t *connection) diff --git a/libresourceqt/src/resource-engine.h b/libresourceqt/src/resource-engine.h index b1fec70..594a20b 100644 --- a/libresourceqt/src/resource-engine.h +++ b/libresourceqt/src/resource-engine.h @@ -36,7 +36,7 @@ public: bool updateResources(); bool registerAudioPid(quint32 pid); - bool registerAudioStreamTag(const QString &streamName); + bool registerAudioStreamTag(const QString &name, const QString &value); bool registerAudioGroup(const QString &); void handleConnectionIsUp(); @@ -58,7 +58,7 @@ signals: void disconnectedFromManager(); private: - + bool connected; ResourceSet *resourceSet; DBusConnection *dbusConnection; @@ -68,6 +68,8 @@ private: quint32 requestId; QMap<quint32, resmsg_type_t> messageMap; quint32 connectionMode; + + bool sendAudioMessage(resmsg_t *message); }; } diff --git a/libresourceqt/src/resource-set.cpp b/libresourceqt/src/resource-set.cpp index bbb8d6e..00a0293 100644 --- a/libresourceqt/src/resource-set.cpp +++ b/libresourceqt/src/resource-set.cpp @@ -10,7 +10,7 @@ ResourceSet::ResourceSet(const QString &applicationClass, QObject * parent) pendingAudioGroup(true), pendingAudioStream(true), pendingAudioPid(true) { identifier = (quint32)this; - memset(resourceSet, 0, sizeof(QPointer<Resource *>)*NumberOfTypes); + memset(resourceSet, 0, sizeof(QPointer<Resource *>)*NumberOfTypes); } ResourceSet::~ResourceSet() @@ -19,7 +19,7 @@ ResourceSet::~ResourceSet() delete resourceSet[i]; } resourceEngine->disconnect(this); - resourceEngine->disconnectFromManager(); +// resourceEngine->disconnectFromManager(); delete resourceEngine; } @@ -29,7 +29,7 @@ bool ResourceSet::initialize() if (resourceEngine == NULL) { return false; } - QObject::connect(resourceEngine, SIGNAL(connectedToManager()), + QObject::connect(resourceEngine, SIGNAL(connectedToManager()), this, SLOT(connectedHandler())); QObject::connect(resourceEngine, SIGNAL(resourcesGranted(quint32)), this, SLOT(handleGranted(quint32))); @@ -58,14 +58,17 @@ void ResourceSet::addResource(Resource *resource) qDebug("audioResource... %p", resource); audioResource = static_cast<AudioResource *>(resource); QObject::connect(audioResource, SIGNAL(pidChanged(quint32)), - this, SLOT(handleAudioPidChange(quint32))); + this, SLOT(handleAudioPidChange(quint32))); QObject::connect(audioResource, SIGNAL(audioGroupChanged(const QString &)), - this, SLOT(handleAudioGroupChange(QString))); - QObject::connect(audioResource, SIGNAL(streamTagChanged(const QString &)), - this, SLOT(handleAudioStreamTagChanged(const QString &))); - pendingAudioStream = true; - pendingAudioGroup = true; - pendingAudioPid = true; + this, SLOT(handleAudioGroupChange(QString))); + QObject::connect(audioResource, SIGNAL(streamTagChanged(const QString &, const QString &)), + this, SLOT(handleAudioStreamTagChanged(const QString &, const QString &))); + if(audioResource->streamTagIsSet()) + pendingAudioStream = true; + if(audioResource->audioGroupIsSet()) + pendingAudioGroup = true; + if(audioResource->processID() > 0) + pendingAudioPid = true; } delete resourceSet[resource->type()]; resourceSet[resource->type()] = resource; @@ -126,7 +129,6 @@ QList<Resource *> ResourceSet::resources() const Resource * ResourceSet::resource(ResourceType type) const { - qDebug("returning %p (%p), audioResource = %p", resourceSet[type], &resourceSet[type], audioResource); return resourceSet[type]; } @@ -249,7 +251,7 @@ void ResourceSet::handleReleased() resourceSet[i]->unsetGranted(); } } - emit resourcesReleased(); + emit resourcesReleased(); } void ResourceSet::handleDeny() @@ -277,7 +279,7 @@ void ResourceSet::handleResourcesLost(quint32 lostResourcesBitmask) void ResourceSet::handleResourcesBecameAvailable(quint32 availableResources) { QList<ResourceType> listOfResources; - for(int i=0;i < NumberOfTypes; i++) { + for (int i=0;i < NumberOfTypes; i++) { ResourceType type = (ResourceType)i; quint32 bitmask = resourceTypeToLibresourceType(type); if ((bitmask & availableResources) == bitmask) { @@ -289,15 +291,21 @@ void ResourceSet::handleResourcesBecameAvailable(quint32 availableResources) void ResourceSet::handleAudioPidChange(quint32 newPid) { - if(initialized && resourceEngine->isConnectedToManager()) { + qDebug("Audio renderer PID = %u", newPid); + if (initialized && resourceEngine->isConnectedToManager()) { + qDebug("registering new PID"); resourceEngine->registerAudioPid(newPid); } + else { + qDebug("registering PID later..."); + pendingAudioPid = true; + } } void ResourceSet::handleAudioGroupChange(const QString &newGroup) { qDebug() << "Audio group changed to: " << newGroup; - if(initialized && resourceEngine->isConnectedToManager()) { + if (initialized && resourceEngine->isConnectedToManager()) { qDebug("registering new audio group"); resourceEngine->registerAudioGroup(newGroup); } @@ -307,10 +315,12 @@ void ResourceSet::handleAudioGroupChange(const QString &newGroup) } } -void ResourceSet::handleAudioStreamTagChanged(const QString &newTag) +void ResourceSet::handleAudioStreamTagChanged(const QString &name, const QString &value) { - if(initialized && resourceEngine->isConnectedToManager()) { - resourceEngine->registerAudioStreamTag(newTag); + if (initialized && resourceEngine->isConnectedToManager()) { + resourceEngine->registerAudioStreamTag(name, value); + } + else { + pendingAudioStream = true; } } - diff --git a/resourceqt-client/client.cpp b/resourceqt-client/client.cpp index c926ef0..1ac9519 100644 --- a/resourceqt-client/client.cpp +++ b/resourceqt-client/client.cpp @@ -9,39 +9,35 @@ Client::Client(QString appClass) : QObject() { - applicationClass = appClass; - resourceSet = NULL; + applicationClass = appClass; + resourceSet = NULL; standardInput = new QTextStream(stdin, QFile::ReadOnly); - mainTimerID = startTimer(0); + mainTimerID = startTimer(0); } Client::~Client() { - killTimer(mainTimerID); - - if( resourceSet != NULL ) - { - delete resourceSet; - resourceSet = NULL; - } - - if( standardInput != NULL ) - { - delete standardInput; - standardInput = NULL; - } + killTimer(mainTimerID); + + if (resourceSet != NULL) { + delete resourceSet; + resourceSet = NULL; + } + + if (standardInput != NULL) { + delete standardInput; + standardInput = NULL; + } } uint32_t Client::parseResourceList(QString resourceListStr) { - struct - { - uint32_t resourceType; - const char* resourceName; + struct { + uint32_t resourceType; + const char* resourceName; } - resourceDef[] = - { + resourceDef[] = { { RES_AUDIO_PLAYBACK , "AudioPlayback" }, { RES_VIDEO_PLAYBACK , "VideoPlayback" }, { RES_AUDIO_RECORDING, "AudioRecording" }, @@ -59,33 +55,27 @@ uint32_t Client::parseResourceList(QString resourceListStr) uint32_t resourceList = 0; - if( resourceListStr.isEmpty() || resourceListStr.isNull() ) - { + if (resourceListStr.isEmpty() || resourceListStr.isNull()) { return 0; } - else - { - QStringList resList = resourceListStr.split(",", QString::SkipEmptyParts); - - for( int i = 0; i < resList.count(); i++ ) - { - int pos = 0; - while( resourceDef[pos].resourceName != NULL ) - { - if( resList[i] == resourceDef[pos].resourceName ) + else { + QStringList resList = resourceListStr.split(",", QString::SkipEmptyParts); + + for (int i = 0; i < resList.count(); i++) { + int pos = 0; + while (resourceDef[pos].resourceName != NULL) { + if (resList[i] == resourceDef[pos].resourceName) break; pos++; } - if( !resourceDef[pos].resourceName ) - { - const char* res = qPrintable(resList[i]); - printf("Ignoring invalid resource name '%s'\n", res); + if (!resourceDef[pos].resourceName) { + const char* res = qPrintable(resList[i]); + printf("Ignoring invalid resource name '%s'\n", res); } - else - { - resourceList |= resourceDef[pos].resourceType; + else { + resourceList |= resourceDef[pos].resourceType; } } } @@ -95,448 +85,383 @@ uint32_t Client::parseResourceList(QString resourceListStr) void Client::showPrompt() { - printf("res-client> "); - fflush(stdout); + printf("res-client> "); + fflush(stdout); } void Client::updateSet(uint32_t list, uint32_t optional, bool remove) { - uint32_t resources[] = - { - RES_AUDIO_PLAYBACK, RES_VIDEO_PLAYBACK, RES_AUDIO_RECORDING, RES_VIDEO_RECORDING, - RES_VIBRA, RES_LEDS, RES_BACKLIGHT, RES_SYSTEM_BUTTON, RES_LOCK_BUTTON, - RES_SCALE_BUTTON, RES_SNAP_BUTTON, RES_LENS_COVER, 0 - }; - - int pos = 0; - while( resources[pos] ) - { - if( list & resources[pos] ) - { - Resource* resource = NULL; - ResourceType res = getResourceType(resources[pos]); - bool opt = (optional & resources[pos]) == resources[pos]; - - if( remove ) - { - if( !resourceSet->contains(res) ) - { - continue; - } - - if( optional ) - { - resource = resourceSet->resource(res); - resource->setOptional(false); - } - else - { - resourceSet->deleteResource(res); - } - } - else - { - if( resourceSet->contains(res) ) - { - resource = resourceSet->resource(res); - if( resource->isOptional() != opt ) - { - resource->setOptional(opt); - } - - continue; - } - - resource = allocateResource(res, opt); - if( resource ) - { - resourceSet->addResource(resource); - } - } - } - - pos++; - } + uint32_t resources[] = { + RES_AUDIO_PLAYBACK, RES_VIDEO_PLAYBACK, RES_AUDIO_RECORDING, RES_VIDEO_RECORDING, + RES_VIBRA, RES_LEDS, RES_BACKLIGHT, RES_SYSTEM_BUTTON, RES_LOCK_BUTTON, + RES_SCALE_BUTTON, RES_SNAP_BUTTON, RES_LENS_COVER, 0 + }; + + int pos = 0; + while (resources[pos]) { + if (list & resources[pos]) { + Resource* resource = NULL; + ResourceType res = getResourceType(resources[pos]); + bool opt = (optional & resources[pos]) == resources[pos]; + + if (remove) { + if (!resourceSet->contains(res)) { + continue; + } + + if (optional) { + resource = resourceSet->resource(res); + resource->setOptional(false); + } + else { + resourceSet->deleteResource(res); + } + } + else { + if (resourceSet->contains(res)) { + resource = resourceSet->resource(res); + if (resource->isOptional() != opt) { + resource->setOptional(opt); + } + + continue; + } + + resource = allocateResource(res, opt); + if (resource) { + resourceSet->addResource(resource); + } + } + } + + pos++; + } } bool Client::initialize(uint32_t all, uint32_t optional, bool alwaysReply, bool autoRelease) { - resourceSet = new ResourceSet(applicationClass); - if( resourceSet == NULL ) - { - return false; - } - - if( alwaysReply ) - { - qDebug("client: alwaysReply"); - resourceSet->setAlwaysReply(); - } - - if( autoRelease ) - { - qDebug("client: autoRelease"); - resourceSet->setAutoRelease(); - } - - updateSet(all, optional, false); - - if( !connect(resourceSet, SIGNAL(resourcesGranted(QList<ResourceType>)), this, SLOT(resourceAcquiredHandler(QList<ResourceType>))) ) - { - return false; - } - - if( !connect(resourceSet, SIGNAL(resourcesDenied()), this, SLOT(resourceDeniedHandler())) ) - { - return false; - } - - if( !connect(resourceSet, SIGNAL(lostResources()), this, SLOT(resourceLostHandler())) ) - { - return false; - } + resourceSet = new ResourceSet(applicationClass); + if (resourceSet == NULL) { + return false; + } + + if (alwaysReply) { + qDebug("client: alwaysReply"); + resourceSet->setAlwaysReply(); + } + + if (autoRelease) { + qDebug("client: autoRelease"); + resourceSet->setAutoRelease(); + } + + updateSet(all, optional, false); + + if (!connect(resourceSet, SIGNAL(resourcesGranted(QList<ResourceType>)), this, SLOT(resourceAcquiredHandler(QList<ResourceType>)))) { + return false; + } + + if (!connect(resourceSet, SIGNAL(resourcesDenied()), this, SLOT(resourceDeniedHandler()))) { + return false; + } + + if (!connect(resourceSet, SIGNAL(lostResources()), this, SLOT(resourceLostHandler()))) { + return false; + } connect(resourceSet, SIGNAL(resourcesReleased()), this, SLOT(resourceReleasedHandler())); - showPrompt(); + showPrompt(); - return true; + return true; } Resource* Client::allocateResource(ResourceType resource, bool optional) { - Resource* retValue = NULL; - - switch( resource ) - { - case AudioPlaybackType: - retValue = new AudioResource(); - break; - case VideoPlaybackType: - retValue = new VideoResource(); - break; - case AudioRecorderType: - retValue = new AudioRecorderResource(); - break; - case VideoRecorderType: - retValue = new VideoRecorderResource(); - break; - case VibraType: - retValue = new VibraResource(); - break; - case LedsType: - retValue = new LedsResource(); - break; - case BacklightType: - retValue = new BacklightResource(); - break; - case SystemButtonType: - retValue = new SystemButtonResource(); - break; - case LockButtonType: - retValue = new LockButtonResource(); - break; - case ScaleButtonType: - retValue = new ScaleButtonResource(); - break; - case SnapButtonType: - retValue = new SnapButtonResource(); - break; - case LensCoverType: - retValue = new LensCoverResource(); - break; - case NumberOfTypes: - return NULL; - } - - if( retValue ) - { - retValue->setOptional(optional); - } - else - { - printf("Unknown resource type - %d\n", resource); - } - - return retValue; + Resource* retValue = NULL; + + switch (resource) { + case AudioPlaybackType: + retValue = new AudioResource(); + break; + case VideoPlaybackType: + retValue = new VideoResource(); + break; + case AudioRecorderType: + retValue = new AudioRecorderResource(); + break; + case VideoRecorderType: + retValue = new VideoRecorderResource(); + break; + case VibraType: + retValue = new VibraResource(); + break; + case LedsType: + retValue = new LedsResource(); + break; + case BacklightType: + retValue = new BacklightResource(); + break; + case SystemButtonType: + retValue = new SystemButtonResource(); + break; + case LockButtonType: + retValue = new LockButtonResource(); + break; + case ScaleButtonType: + retValue = new ScaleButtonResource(); + break; + case SnapButtonType: + retValue = new SnapButtonResource(); + break; + case LensCoverType: + retValue = new LensCoverResource(); + break; + case NumberOfTypes: + return NULL; + } + + if (retValue) { + retValue->setOptional(optional); + } + else { + printf("Unknown resource type - %d\n", resource); + } + + return retValue; } ResourceType Client::getResourceType(uint32_t resource) { - switch( resource ) - { - case RES_AUDIO_PLAYBACK: - return AudioPlaybackType; - case RES_VIDEO_PLAYBACK: - return VideoPlaybackType; - case RES_AUDIO_RECORDING: - return AudioRecorderType; - case RES_VIDEO_RECORDING: - return VideoRecorderType; - case RES_VIBRA: - return VibraType; - case RES_LEDS: - return LedsType; - case RES_BACKLIGHT: - return BacklightType; - case RES_SYSTEM_BUTTON: - return SystemButtonType; - case RES_LOCK_BUTTON: - return LockButtonType; - case RES_SCALE_BUTTON: - return ScaleButtonType; - case RES_SNAP_BUTTON: - return SnapButtonType; - case RES_LENS_COVER: - return LensCoverType; - } - - return NumberOfTypes; + switch (resource) { + case RES_AUDIO_PLAYBACK: + return AudioPlaybackType; + case RES_VIDEO_PLAYBACK: + return VideoPlaybackType; + case RES_AUDIO_RECORDING: + return AudioRecorderType; + case RES_VIDEO_RECORDING: + return VideoRecorderType; + case RES_VIBRA: + return VibraType; + case RES_LEDS: + return LedsType; + case RES_BACKLIGHT: + return BacklightType; + case RES_SYSTEM_BUTTON: + return SystemButtonType; + case RES_LOCK_BUTTON: + return LockButtonType; + case RES_SCALE_BUTTON: + return ScaleButtonType; + case RES_SNAP_BUTTON: + return SnapButtonType; + case RES_LENS_COVER: + return LensCoverType; + } + + return NumberOfTypes; } void Client::showResources(const QList<ResourceType> resList) { - const char* resTypes[] = - { - "AudioPlayback", "VideoPlayback", "AudioRecorder", "VideoRecorder", "Vibra", - "Leds", "Backlight", "SystemButton", "LockButton", "ScaleButton", "SnapButton", - "LensCover" - }; - - for( int i = 0; i < resList.count(); i++ ) - { - printf("\t%s\n", resTypes[resList[i]]); - } + const char* resTypes[] = { + "AudioPlayback", "VideoPlayback", "AudioRecorder", "VideoRecorder", "Vibra", + "Leds", "Backlight", "SystemButton", "LockButton", "ScaleButton", "SnapButton", + "LensCover" + }; + + for (int i = 0; i < resList.count(); i++) { + printf("\t%s\n", resTypes[resList[i]]); + } } void Client::showResources(const QList<Resource*> resList) { - const char* resTypes[] = - { - "AudioPlayback", "VideoPlayback", "AudioRecorder", "VideoRecorder", "Vibra", - "Leds", "Backlight", "SystemButton", "LockButton", "ScaleButton", "SnapButton", - "LensCover" - }; - - for( int i = 0; i < resList.count(); i++ ) - { - Resource* r = resList[i]; - printf("\t%s%s%s\n", resTypes[r->type()], - r->isOptional() ? " (optional)" : "", - r->isGranted() ? " (granted)" : ""); - } + const char* resTypes[] = { + "AudioPlayback", "VideoPlayback", "AudioRecorder", "VideoRecorder", "Vibra", + "Leds", "Backlight", "SystemButton", "LockButton", "ScaleButton", "SnapButton", + "LensCover" + }; + + for (int i = 0; i < resList.count(); i++) { + Resource* r = resList[i]; + printf("\t%s%s%s\n", resTypes[r->type()], + r->isOptional() ? " (optional)" : "", + r->isGranted() ? " (granted)" : ""); + } } void Client::resourceAcquiredHandler(const QList<ResourceType>& /*grantedResList*/) { - QList<Resource*> list = resourceSet->resources(); - if( !list.count() ) - { - printf("\nGranted resource set is empty. Possible bug?\n"); - } - else - { - printf("\nManager grants access to these resources:\n"); - printf("Resource set:\n"); - showResources(list); - } - showPrompt(); + QList<Resource*> list = resourceSet->resources(); + if (!list.count()) { + printf("\nGranted resource set is empty. Possible bug?\n"); + } + else { + printf("\nManager grants access to these resources:\n"); + printf("Resource set:\n"); + showResources(list); + } + showPrompt(); } void Client::resourceDeniedHandler() { - printf("\nManager denies access to resources!\n"); - showPrompt(); + printf("\nManager denies access to resources!\n"); + showPrompt(); } void Client::resourceLostHandler() { - printf("\nLost resources from manager!\n"); - showPrompt(); + printf("\nLost resources from manager!\n"); + showPrompt(); } void Client::resourceReleasedHandler() { - printf("\nAll resources released\n"); - showPrompt(); + printf("\nAll resources released\n"); + showPrompt(); } void Client::timerEvent(QTimerEvent*) { - bool quitFlag = false; - - fd_set stdinfd; - FD_ZERO(&stdinfd); - FD_SET(0, &stdinfd); - timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 0; - - int ready = select(1, &stdinfd, NULL, NULL, &tv); - if( ready > 0 ) - { - QString line = standardInput->readLine(); - if( !line.isNull() ) - { - QStringList params = line.split(" "); - if( params[0] == "quit" ) - { - quitFlag = true; - QMetaObject::invokeMethod(QCoreApplication::instance(), "quit"); - } - else if( params[0] == "help" ) - { - printf("Available commands:\n"); - printf("\t help \tprint this help message\n"); - printf("\t quit \texit application\n"); - printf("\t acquire\tacquire required resources\n"); - printf("\t release\trelease resources\n"); - printf("\t update\tupdate modified resource set after add or remove command\n"); - printf("\t add reslist [-o]\tadd reosurce list, if -o provided, set as optional\n"); - printf("\t remove reslist [-o]\tremove reosurce list, if -o provided, removed only optional flag\n"); - printf("\t audio pid <pid>|group <audio group>|tag <name:value>\tset audio properties"); - printf("\t show \tshow resources\n"); - } - else if( params[0] == "show" ) - { - if( !resourceSet ) - { - printf("Show failed!\n"); - } - else - { - QList<Resource*> list = resourceSet->resources(); - if( !list.count() ) - { - printf("Resource set is empty, use add command to add some ...\n"); - } - else - { - printf("Resource set:\n"); - showResources(list); - } - } - } - else if( params[0] == "acquire" ) - { - if( !resourceSet || !resourceSet->acquire() ) - { - printf("Acquire failed!\n"); - } - } - else if( params[0] == "release" ) - { - if( !resourceSet || !resourceSet->release() ) - { - printf("Release failed!\n"); - } - } - else if( params[0] == "add" ) - { - if( !resourceSet ) - { - printf("Update failed!\n"); - } - else if( params.count() == 1 || params[1].isEmpty() || params[1].isNull() ) - { - printf("List of desired resources is missing. See help ...\n"); - } - else - { - uint32_t temp = Client::parseResourceList(params[1]); - if( params.count() > 2 && params[2] == "-o" ) - { - updateSet(temp, temp, false); - } - else - { - updateSet(temp, 0, false); - } - } - } - else if( params[0] == "remove" ) - { - if( !resourceSet || params.count() == 1 ) - { - printf("Update failed!\n"); - } - else if( params.count() == 1 || params[1].isEmpty() || params[1].isNull() ) - { - printf("List of desired resources is missing. See help ...\n"); - } - else - { - uint32_t temp = Client::parseResourceList(params[1]); - if( params.count() > 2 && params[2] == "-o" ) - { - updateSet(temp, temp, true); - } - else - { - updateSet(temp, 0, true); - } - } - } - else if( params[0] == "update" ) - { - if( !resourceSet || !resourceSet->update() ) - { - printf("Update failed!\n"); - } - } - else if( params[0] == "audio" ) - { - if( params.count() < 3 ) - { - printf("Not enough parameters! See help!\n"); - } - else - { - Resource *resource = resourceSet->resource(AudioPlaybackType); - AudioResource *audioResource = static_cast<AudioResource*>(resource); + bool quitFlag = false; + + fd_set stdinfd; + FD_ZERO(&stdinfd); + FD_SET(0, &stdinfd); + timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 0; + + int ready = select(1, &stdinfd, NULL, NULL, &tv); + if (ready > 0) { + QString line = standardInput->readLine(); + if (!line.isNull()) { + QStringList params = line.split(" "); + if (params[0] == "quit") { + quitFlag = true; + QMetaObject::invokeMethod(QCoreApplication::instance(), "quit"); + } + else if (params[0] == "help") { + printf("Available commands:\n"); + printf("\t help \tprint this help message\n"); + printf("\t quit \texit application\n"); + printf("\t acquire\tacquire required resources\n"); + printf("\t release\trelease resources\n"); + printf("\t update\tupdate modified resource set after add or remove command\n"); + printf("\t add reslist [-o]\tadd reosurce list, if -o provided, set as optional\n"); + printf("\t remove reslist [-o]\tremove reosurce list, if -o provided, removed only optional flag\n"); + printf("\t audio pid <pid>|group <audio group>|tag <name:value>\tset audio properties"); + printf("\t show \tshow resources\n"); + } + else if (params[0] == "show") { + if (!resourceSet) { + printf("Show failed!\n"); + } + else { + QList<Resource*> list = resourceSet->resources(); + if (!list.count()) { + printf("Resource set is empty, use add command to add some ...\n"); + } + else { + printf("Resource set:\n"); + showResources(list); + } + } + } + else if (params[0] == "acquire") { + if (!resourceSet || !resourceSet->acquire()) { + printf("Acquire failed!\n"); + } + } + else if (params[0] == "release") { + if (!resourceSet || !resourceSet->release()) { + printf("Release failed!\n"); + } + } + else if (params[0] == "add") { + if (!resourceSet) { + printf("Update failed!\n"); + } + else if (params.count() == 1 || params[1].isEmpty() || params[1].isNull()) { + printf("List of desired resources is missing. See help ...\n"); + } + else { + uint32_t temp = Client::parseResourceList(params[1]); + if (params.count() > 2 && params[2] == "-o") { + updateSet(temp, temp, false); + } + else { + updateSet(temp, 0, false); + } + } + } + else if (params[0] == "remove") { + if (!resourceSet || params.count() == 1) { + printf("Update failed!\n"); + } + else if (params.count() == 1 || params[1].isEmpty() || params[1].isNull()) { + printf("List of desired resources is missing. See help ...\n"); + } + else { + uint32_t temp = Client::parseResourceList(params[1]); + if (params.count() > 2 && params[2] == "-o") { + updateSet(temp, temp, true); + } + else { + updateSet(temp, 0, true); + } + } + } + else if (params[0] == "update") { + if (!resourceSet || !resourceSet->update()) { + printf("Update failed!\n"); + } + } + else if (params[0] == "audio") { + if (params.count() < 3) { + printf("Not enough parameters! See help!\n"); + } + else { + Resource *resource = resourceSet->resource(AudioPlaybackType); + AudioResource *audioResource = static_cast<AudioResource*>(resource); qDebug("resource = %p audioResource = %p", resource, audioResource); - if( audioResource == NULL ) - { - printf("No AudioResource available in set!\n"); - } - else - { - if( params[1] == "group" ) - { - audioResource->setAudioGroup(params[2]); - } - else if( params[1] == "pid" ) - { - bool ok; - quint32 pid = (quint32)params[2].toInt(&ok, 10); - if( ok && pid != 0 ) - { - audioResource->setProcessID(pid); - } - else - { - printf("Bad pid parameter!\n"); - } - } - else if( params[1] == "tag" ) { - audioResource->setStreamTag(params[2]); - } - else { - printf("Unknown audio command!\n"); - } - } - } - } - else if( !params[0].isEmpty() ) - { - QByteArray ba = line.toLatin1(); - const char *c_line = ba.data(); - printf("unknown command '%s'\n", c_line); - } - - if( !quitFlag ) - { - showPrompt(); - } - } - } + if (audioResource == NULL) { + printf("No AudioResource available in set!\n"); + } + else { + if (params[1] == "group") { + audioResource->setAudioGroup(params[2]); + } + else if (params[1] == "pid") { + bool ok; + quint32 pid = (quint32)params[2].toInt(&ok, 10); + if (ok && pid != 0) { + audioResource->setProcessID(pid); + } + else { + printf("Bad pid parameter!\n"); + } + } + else if (params[1] == "tag") { + audioResource->setStreamTag(params[2]); + } + else { + printf("Unknown audio command!\n"); + } + } + } + } + else if (!params[0].isEmpty()) { + QByteArray ba = line.toLatin1(); + const char *c_line = ba.data(); + printf("unknown command '%s'\n", c_line); + } + + if (!quitFlag) { + showPrompt(); + } + } + } } diff --git a/resourceqt-client/resourceqt-client.cpp b/resourceqt-client/resourceqt-client.cpp index 89aef76..49fa323 100644 --- a/resourceqt-client/resourceqt-client.cpp +++ b/resourceqt-client/resourceqt-client.cpp @@ -10,221 +10,199 @@ class CommandLineParser { public: - CommandLineParser(int argc, char** argv) - { - exitCode = 0; - exitFlag = false; - exeName = strdup(basename(argv[0])); - - resourcesAll = 0; - resourcesOptional = 0; - autoRelease = false; - alwaysReply = false; - - parseArguments(argc, argv); - } - - ~CommandLineParser() - { - if( exeName != NULL ) - { - delete exeName; - exeName = NULL; - } - } + CommandLineParser(int argc, char** argv) { + exitCode = 0; + exitFlag = false; + exeName = strdup(basename(argv[0])); + + resourcesAll = 0; + resourcesOptional = 0; + autoRelease = false; + alwaysReply = false; + + parseArguments(argc, argv); + } + + ~CommandLineParser() { + if (exeName != NULL) { + delete exeName; + exeName = NULL; + } + } public: - bool exitFlag; - int exitCode; + bool exitFlag; + int exitCode; - uint32_t resourcesAll; - uint32_t resourcesOptional; - QString applicationClass; - bool autoRelease; - bool alwaysReply; + uint32_t resourcesAll; + uint32_t resourcesOptional; + QString applicationClass; + bool autoRelease; + bool alwaysReply; - void printMessage(const char *fmt, ...) - { - va_list ap; - char fmtbuf[512]; + void printMessage(const char *fmt, ...) { + va_list ap; + char fmtbuf[512]; snprintf(fmtbuf, sizeof(fmtbuf), "%s\n", fmt); - va_start(ap, fmt); - vprintf(fmtbuf, ap); - va_end(ap); - } + va_start(ap, fmt); + vprintf(fmtbuf, ap); + va_end(ap); + } private: - char* exeName; + char* exeName; - void printError(const char *fmt, ...) - { - va_list ap; - char fmtbuf[512]; + void printError(const char *fmt, ...) { + va_list ap; + char fmtbuf[512]; snprintf(fmtbuf, sizeof(fmtbuf), "%s\n", fmt); - va_start(ap, fmt); - vprintf(fmtbuf, ap); - va_end(ap); - - exitCode = errno; - exitFlag = true; - } - - void parseArguments(int argc, char** argv) - { - int option; - - while( (option = getopt(argc, argv, "hm:o:")) != -1 ) - { - switch (option) - { - case 'h': - usage(0); - return; - case 'm': - parseModeValues(optarg); - break; - case 'o': - resourcesOptional = Client::parseResourceList(optarg); - break; - default: - usage(EINVAL); - return; - } - } - - if( (optind != argc - 2) && (optind != argc - 1) ) - { - usage(EINVAL); - return; - } - else - { - applicationClass = parseClassString(argv[optind]); - if( argc > optind + 1 ) - resourcesAll = Client::parseResourceList(argv[optind+1]); - else - resourcesAll = 0; - } - - if( !resourcesAll ) - { - printMessage("No resources found, use add command to add some ..."); - } - - if( (resourcesAll | resourcesOptional) != resourcesAll ) - { - printError("optional resources are not subset of all resources"); - } - } - - char* parseClassString(char *str) - { - if( strcmp(str, "call" ) && - strcmp(str, "camera" ) && - strcmp(str, "ringtone" ) && - strcmp(str, "alarm" ) && - strcmp(str, "navigator" ) && - strcmp(str, "game" ) && - strcmp(str, "player" ) && - strcmp(str, "event" ) && - strcmp(str, "background") ) - { - printError("invalid class '%s'", str); - return NULL; - } - - return str; - } - - void parseModeValues(QString modeListStr) - { - if( !modeListStr.isEmpty() && !modeListStr.isNull() ) - { - QStringList modeList = modeListStr.split(",", QString::SkipEmptyParts); - - for( int i = 0; i < modeList.count(); i++ ) - { - if( modeList[i] == "AutoRelease" ) - { - autoRelease = true; + va_start(ap, fmt); + vprintf(fmtbuf, ap); + va_end(ap); + + exitCode = errno; + exitFlag = true; + } + + void parseArguments(int argc, char** argv) { + int option; + + while ((option = getopt(argc, argv, "hm:o:")) != -1) { + switch (option) { + case 'h': + usage(0); + return; + case 'm': + parseModeValues(optarg); + break; + case 'o': + resourcesOptional = Client::parseResourceList(optarg); + break; + default: + usage(EINVAL); + return; + } + } + + if ((optind != argc - 2) && (optind != argc - 1)) { + usage(EINVAL); + return; + } + else { + applicationClass = parseClassString(argv[optind]); + if (argc > optind + 1) + resourcesAll = Client::parseResourceList(argv[optind+1]); + else + resourcesAll = 0; + } + + if (!resourcesAll) { + printMessage("No resources found, use add command to add some ..."); + } + + if ((resourcesAll | resourcesOptional) != resourcesAll) { + printError("optional resources are not subset of all resources"); + } + } + + char* parseClassString(char *str) { + if (strcmp(str, "call") && + strcmp(str, "camera") && + strcmp(str, "ringtone") && + strcmp(str, "alarm") && + strcmp(str, "navigator") && + strcmp(str, "game") && + strcmp(str, "player") && + strcmp(str, "event") && + strcmp(str, "background")) { + printError("invalid class '%s'", str); + return NULL; + } + + return str; + } + + void parseModeValues(QString modeListStr) { + if (!modeListStr.isEmpty() && !modeListStr.isNull()) { + QStringList modeList = modeListStr.split(",", QString::SkipEmptyParts); + + for (int i = 0; i < modeList.count(); i++) { + if (modeList[i] == "AutoRelease") { + autoRelease = true; } - else if( modeList[i] == "AlwaysReply" ) - { - alwaysReply = true; + else if (modeList[i] == "AlwaysReply") { + alwaysReply = true; } - else - { - const char* mode = qPrintable(modeList[i]); - printMessage("Ignoring unknown mode '%s'!", mode); + else { + const char* mode = qPrintable(modeList[i]); + printMessage("Ignoring unknown mode '%s'!", mode); } - } - } - } - - void usage(int theExitCode) - { - printf("usage: %s [-h] [-t] [-v] [-m mode-values]" - "[-o optional-resources] [-s shared-resources -m shared-mask] " - "class all-resources\n", - exeName); - printf("\toptions:\n"); - printf("\t h\tprint this help message and exit\n"); - printf("\t m\tmode values. See 'modes' below for the " - "\n\t\tsyntax of <mode-values>\n"); - printf("\t o\toptional resources. See 'resources' below for the " - "syntax of\n\t\t<optional-resources>\n"); - printf("\tclass:\n"); - printf("\t\tcall\t - for native or 3rd party telephony\n"); - printf("\t\tcamera\t - for photo applications\n"); - printf("\t\tringtone - for ringtones\n"); - printf("\t\talarm\t - for alarm clock\n"); - printf("\t\tnavigator - for mapping applications\n"); - printf("\t\tgame\t - for gaming\n"); - printf("\t\tplayer\t - for media playback/recording\n"); - printf("\t\tevent\t - for messaging and other event notifications\n"); - printf("\t\tbackground - for thumbnailing etc\n"); - printf("\tresources:\n"); - printf("\t comma separated list of the following resources\n"); - printf("\t\tAudioPlayback\n"); - printf("\t\tVideoPlayback\n"); - printf("\t\tAudioRecording\n"); - printf("\t\tVideoRecording\n"); - printf("\t\tVibra\n"); - printf("\t\tLeds\n"); - printf("\t\tBackLight\n"); - printf("\t\tSystemButton\n"); - printf("\t\tLockButton\n"); - printf("\t\tScaleButton\n"); - printf("\t\tSnapButton\n"); - printf("\t\tLensCover\n"); - printf("\t no whitespace allowed in the resource list.\n"); - printf("\tmodes:\n"); - printf("\t comma separated list of the following modes\n"); - printf("\t\tAutoRelease\n"); - printf("\t\tAlwaysReply\n"); - fflush(stdout); - - exitCode = theExitCode; - exitFlag = true; - } + } + } + } + + void usage(int theExitCode) { + printf("usage: %s [-h] [-t] [-v] [-m mode-values]" + "[-o optional-resources] [-s shared-resources -m shared-mask] " + "class all-resources\n", + exeName); + printf("\toptions:\n"); + printf("\t h\tprint this help message and exit\n"); + printf("\t m\tmode values. See 'modes' below for the " + "\n\t\tsyntax of <mode-values>\n"); + printf("\t o\toptional resources. See 'resources' below for the " + "syntax of\n\t\t<optional-resources>\n"); + printf("\tclass:\n"); + printf("\t\tcall\t - for native or 3rd party telephony\n"); + printf("\t\tcamera\t - for photo applications\n"); + printf("\t\tringtone - for ringtones\n"); + printf("\t\talarm\t - for alarm clock\n"); + printf("\t\tnavigator - for mapping applications\n"); + printf("\t\tgame\t - for gaming\n"); + printf("\t\tplayer\t - for media playback/recording\n"); + printf("\t\tevent\t - for messaging and other event notifications\n"); + printf("\t\tbackground - for thumbnailing etc\n"); + printf("\tresources:\n"); + printf("\t comma separated list of the following resources\n"); + printf("\t\tAudioPlayback\n"); + printf("\t\tVideoPlayback\n"); + printf("\t\tAudioRecording\n"); + printf("\t\tVideoRecording\n"); + printf("\t\tVibra\n"); + printf("\t\tLeds\n"); + printf("\t\tBackLight\n"); + printf("\t\tSystemButton\n"); + printf("\t\tLockButton\n"); + printf("\t\tScaleButton\n"); + printf("\t\tSnapButton\n"); + printf("\t\tLensCover\n"); + printf("\t no whitespace allowed in the resource list.\n"); + printf("\tmodes:\n"); + printf("\t comma separated list of the following modes\n"); + printf("\t\tAutoRelease\n"); + printf("\t\tAlwaysReply\n"); + fflush(stdout); + + exitCode = theExitCode; + exitFlag = true; + } }; int main(int argc, char *argv[]) { - CommandLineParser parser(argc, argv); + CommandLineParser parser(argc, argv); - if( parser.exitFlag ) - return parser.exitCode; + if (parser.exitFlag) + return parser.exitCode; QCoreApplication app(argc, argv); Client client(parser.applicationClass); - if( !client.initialize(parser.resourcesAll, parser.resourcesOptional, parser.alwaysReply, parser.autoRelease) ) - { - parser.printMessage("initialization failed!"); - return EINVAL; + if (!client.initialize(parser.resourcesAll, parser.resourcesOptional, parser.alwaysReply, parser.autoRelease)) { + parser.printMessage("initialization failed!"); + return EINVAL; } return app.exec(); |