diff options
Diffstat (limited to 'libresourceqt/src/resource-set.cpp')
-rw-r--r-- | libresourceqt/src/resource-set.cpp | 152 |
1 files changed, 134 insertions, 18 deletions
diff --git a/libresourceqt/src/resource-set.cpp b/libresourceqt/src/resource-set.cpp index 7960fad..3652939 100644 --- a/libresourceqt/src/resource-set.cpp +++ b/libresourceqt/src/resource-set.cpp @@ -5,11 +5,12 @@ using namespace ResourcePolicy; ResourceSet::ResourceSet(const QString &applicationClass, QObject * parent) : QObject(parent), resourceClass(applicationClass), resourceEngine(NULL), - autoRelease(false), alwaysReply(false), initialized(false), - pendingAcquire(false) + audioResource(NULL), autoRelease(false), alwaysReply(false), + initialized(false), pendingAcquire(false), pendingUpdate(false), + 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() @@ -17,6 +18,9 @@ ResourceSet::~ResourceSet() for (int i = 0;i < NumberOfTypes;i++) { delete resourceSet[i]; } + resourceEngine->disconnect(this); + resourceEngine->disconnectFromManager(); + delete resourceEngine; } bool ResourceSet::initialize() @@ -27,14 +31,20 @@ bool ResourceSet::initialize() } QObject::connect(resourceEngine, SIGNAL(connectedToManager()), this, SLOT(connectedHandler())); - QObject::connect(resourceEngine, SIGNAL(resourcesAcquired(quint32)), - this, SLOT(handleAcquire(quint32))); + QObject::connect(resourceEngine, SIGNAL(resourcesGranted(quint32)), + this, SLOT(handleGranted(quint32))); QObject::connect(resourceEngine, SIGNAL(resourcesDenied()), this, SLOT(handleDeny())); + QObject::connect(resourceEngine, SIGNAL(resourcesReleased()), + this, SLOT(handleReleased())); + QObject::connect(resourceEngine, SIGNAL(resourcesLost(quint32)), + this, SLOT(handleResourcesLost(quint32))); + QObject::connect(resourceEngine, SIGNAL(resourcesBecameAvailable(quint32)), + this, SLOT(handleResourcesBecameAvailable(quint32))); if (!resourceEngine->initialize()) { return false; } - if (!resourceEngine->connect()) { + if (!resourceEngine->connectToManager()) { return false; } qDebug("ResourceSet is initialized engine=%p", resourceEngine); @@ -42,13 +52,23 @@ bool ResourceSet::initialize() return true; } -void ResourceSet::addResource(const Resource *resource) +void ResourceSet::addResource(Resource *resource) { - if ((resource->type() == AudioPlaybackType) || (resource->type() == AudioRecorderType)) { - qDebug("audioResource..."); + if (resource->type() == AudioPlaybackType) { + qDebug("audioResource... %p", resource); + audioResource = static_cast<AudioResource *>(resource); + QObject::connect(audioResource, SIGNAL(pidChanged(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; } delete resourceSet[resource->type()]; - resourceSet[resource->type()] = resource->clone(); + resourceSet[resource->type()] = resource; } void ResourceSet::addResources(const QList<Resource *>resources) @@ -60,6 +80,10 @@ void ResourceSet::addResources(const QList<Resource *>resources) void ResourceSet::deleteResource(ResourceType type) { + if(type == AudioPlaybackType) { + audioResource->disconnect(); + audioResource = NULL; + } delete resourceSet[type]; resourceSet[type] = NULL; } @@ -102,18 +126,20 @@ 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]; } bool ResourceSet::acquire() { - if(!initialized) { + if (!initialized) { pendingAcquire = true; return initialize(); } - else if (!resourceEngine->isConnected()) { + + if (!resourceEngine->isConnectedToManager()) { pendingAcquire = true; - resourceEngine->connect(); + resourceEngine->connectToManager(); return true; } else { @@ -123,12 +149,24 @@ bool ResourceSet::acquire() bool ResourceSet::release() { - return false; + if (!initialized || !resourceEngine->isConnectedToManager()) { + return true; + } + + return resourceEngine->releaseResources(); } bool ResourceSet::update() { - return false; + if (!initialized) { + return true; + } + + if (!resourceEngine->isConnectedToManager()) { + pendingUpdate = true; + return true; + } + return resourceEngine->updateResources(); } QString ResourceSet::applicationClass() @@ -148,31 +186,56 @@ void ResourceSet::setAlwaysReply() void ResourceSet::connectedHandler() { + if (pendingAudioGroup) { + qDebug() << "registering audio group: " << audioResource->audioGroup(); + resourceEngine->registerAudioGroup(audioResource->audioGroup()); + pendingAudioGroup = false; + } + if (pendingUpdate) { + resourceEngine->updateResources(); + pendingUpdate = false; + } if (pendingAcquire) { resourceEngine->acquireResources(); + pendingAcquire = false; } } -void ResourceSet::handleAcquire(quint32 bitmaskOfGrantedResources) +void ResourceSet::handleGranted(quint32 bitmaskOfGrantedResources) { qDebug("in %s",__FUNCTION__); QList<ResourceType> optionalResources; qDebug("Acquired resources: 0x%04x", bitmaskOfGrantedResources); for(int i=0;i < NumberOfTypes; i++) { + if(resourceSet[i] == NULL) + continue; ResourceType type = (ResourceType)i; quint32 bitmask = resourceTypeToLibresourceType(type); - qDebug("Checking if resource %x(%x) is in the set", i, bitmask); + qDebug("Checking if resource 0x%04x is in the set", bitmask); if ((bitmask & bitmaskOfGrantedResources) == bitmask) { if (resourceSet[i]->isOptional()) { optionalResources << type; } resourceSet[i]->setGranted(); - qDebug("Resource %02x is now granted", i); + qDebug("Resource 0x%04x is now granted", i); + } + else { + resourceSet[i]->unsetGranted(); } } emit resourcesGranted(optionalResources); } +void ResourceSet::handleReleased() +{ + for(int i=0;i < NumberOfTypes; i++) { + if(resourceSet[i] != NULL) { + resourceSet[i]->unsetGranted(); + } + } + emit resourcesReleased(); +} + void ResourceSet::handleDeny() { for(int i=0;i < NumberOfTypes; i++) { @@ -180,5 +243,58 @@ void ResourceSet::handleDeny() resourceSet[i]->unsetGranted(); } } + emit resourcesDenied(); +} + +void ResourceSet::handleResourcesLost(quint32 lostResourcesBitmask) +{ + for(int i=0;i < NumberOfTypes; i++) { + quint32 bitmask = resourceTypeToLibresourceType((ResourceType)i); + if ((bitmask & lostResourcesBitmask) == bitmask) { + resourceSet[i]->unsetGranted(); + qDebug("Resource %04x is now lost", bitmask); + } + } + emit lostResources(); +} + +void ResourceSet::handleResourcesBecameAvailable(quint32 availableResources) +{ + QList<ResourceType> listOfResources; + for(int i=0;i < NumberOfTypes; i++) { + ResourceType type = (ResourceType)i; + quint32 bitmask = resourceTypeToLibresourceType(type); + if ((bitmask & availableResources) == bitmask) { + listOfResources.append(type); + } + } + emit resourcesBecameAvailable(listOfResources); +} + +void ResourceSet::handleAudioPidChange(quint32 newPid) +{ + if(initialized && resourceEngine->isConnectedToManager()) { + resourceEngine->registerAudioPid(newPid); + } +} + +void ResourceSet::handleAudioGroupChange(const QString &newGroup) +{ + qDebug() << "Audio group changed to: " << newGroup; + if(initialized && resourceEngine->isConnectedToManager()) { + qDebug("registering new audio group"); + resourceEngine->registerAudioGroup(newGroup); + } + else { + qDebug("registering new audio group LATER"); + pendingAudioGroup = true; + } +} + +void ResourceSet::handleAudioStreamTagChanged(const QString &newTag) +{ + if(initialized && resourceEngine->isConnectedToManager()) { + resourceEngine->registerAudioStreamTag(newTag); + } } |