2 * This file is part of CameraPlus.
4 * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "cameraresources.h"
22 #include <dbusconnectioneventloop.h>
25 #define APPLICATION_CLASS "camera"
27 CameraResources::CameraResources(QObject *parent) :
29 m_worker(new CameraResourcesWorker) {
31 m_worker->moveToThread(&m_thread);
32 DBUSConnectionEventLoop::getInstance().moveToThread(&m_thread);
34 QObject::connect(&m_thread, SIGNAL(started()), m_worker, SLOT(init()));
37 qRegisterMetaType<CameraResources::Mode>("CameraResources::Mode");
38 qRegisterMetaType<bool *>("bool *");
40 QObject::connect(m_worker, SIGNAL(acquiredChanged()), this, SIGNAL(acquiredChanged()));
41 QObject::connect(m_worker, SIGNAL(hijackedChanged()), this, SIGNAL(hijackedChanged()));
42 QObject::connect(m_worker, SIGNAL(updated()), this, SIGNAL(updated()));
43 QObject::connect(m_worker, SIGNAL(acquiredChanged()), this, SIGNAL(scaleAcquisitionChanged()));
44 QObject::connect(m_worker, SIGNAL(hijackedChanged()), this, SIGNAL(scaleAcquisitionChanged()));
45 QObject::connect(m_worker, SIGNAL(updated()), this, SIGNAL(scaleAcquisitionChanged()));
48 CameraResources::~CameraResources() {
49 acquire(CameraResources::None);
52 while (m_thread.isRunning()) {
60 bool CameraResources::isResourceGranted(const ResourcePolicy::ResourceType& resource) const {
63 QMetaObject::invokeMethod(m_worker, "isResourceGranted", Qt::BlockingQueuedConnection,
64 Q_ARG(bool *, &ok), Q_ARG(int, resource));
69 bool CameraResources::acquire(const Mode& mode) {
72 QMetaObject::invokeMethod(m_worker, "acquire", Qt::BlockingQueuedConnection,
73 Q_ARG(bool *, &ok), Q_ARG(CameraResources::Mode, mode));
78 bool CameraResources::acquired() const {
81 QMetaObject::invokeMethod(m_worker, "acquired", Qt::BlockingQueuedConnection,
87 bool CameraResources::hijacked() const {
90 QMetaObject::invokeMethod(m_worker, "hijacked", Qt::BlockingQueuedConnection,
96 bool CameraResources::isScaleAcquired() const {
97 return isResourceGranted(ResourcePolicy::ScaleButtonType);
100 CameraResourcesWorker::CameraResourcesWorker(QObject *parent) :
103 m_mode(CameraResources::None),
110 CameraResourcesWorker::~CameraResourcesWorker() {
114 void CameraResourcesWorker::init() {
115 m_set = new ResourcePolicy::ResourceSet(APPLICATION_CLASS, this);
116 m_set->setAlwaysReply();
118 QObject::connect(m_set, SIGNAL(resourcesReleased()), this, SLOT(resourcesReleased()));
119 QObject::connect(m_set, SIGNAL(lostResources()), this, SLOT(lostResources()));
120 QObject::connect(m_set, SIGNAL(resourcesGranted(const QList<ResourcePolicy::ResourceType>&)),
121 this, SLOT(resourcesGranted(const QList<ResourcePolicy::ResourceType>&)));
122 QObject::connect(m_set, SIGNAL(resourcesDenied()), this, SLOT(resourcesDenied()));
124 if (!m_set->initAndConnect()) {
125 qCritical() << "Failed to connect to resource policy engine";
129 void CameraResourcesWorker::resourcesReleased() {
136 void CameraResourcesWorker::lostResources() {
143 void CameraResourcesWorker::resourcesGranted(const QList<ResourcePolicy::ResourceType>& optional) {
147 // This can happen when:
148 // 1) We lose/gain optional resources.
149 // 2) A higher priority application releases resources back.
159 void CameraResourcesWorker::resourcesDenied() {
166 QList<ResourcePolicy::ResourceType> CameraResourcesWorker::listSet() {
167 QList<ResourcePolicy::Resource *> resources = m_set->resources();
169 QList<ResourcePolicy::ResourceType> set;
171 foreach (ResourcePolicy::Resource *r, resources) {
178 void CameraResourcesWorker::acquire(bool *ok, const CameraResources::Mode& mode) {
179 if (mode == m_mode) {
189 case CameraResources::None:
193 case CameraResources::Image:
194 *ok = updateSet(QList<ResourcePolicy::ResourceType>()
195 << ResourcePolicy::VideoPlaybackType
196 << ResourcePolicy::VideoRecorderType,
197 QList<ResourcePolicy::ResourceType>()
198 << ResourcePolicy::ScaleButtonType);
201 case CameraResources::Video:
202 *ok = updateSet(QList<ResourcePolicy::ResourceType>()
203 << ResourcePolicy::VideoPlaybackType
204 << ResourcePolicy::VideoRecorderType,
205 QList<ResourcePolicy::ResourceType>()
206 << ResourcePolicy::ScaleButtonType);
209 case CameraResources::Recording:
210 *ok = updateSet(QList<ResourcePolicy::ResourceType>()
211 << ResourcePolicy::VideoPlaybackType
212 << ResourcePolicy::VideoRecorderType,
213 QList<ResourcePolicy::ResourceType>()
214 << ResourcePolicy::ScaleButtonType
215 << ResourcePolicy::AudioRecorderType
216 << ResourcePolicy::AudioPlaybackType);
219 case CameraResources::Player:
220 *ok = updateSet(QList<ResourcePolicy::ResourceType>()
221 << ResourcePolicy::VideoPlaybackType
222 << ResourcePolicy::AudioPlaybackType,
223 QList<ResourcePolicy::ResourceType>());
227 qWarning() << "Unknown mode" << mode;
233 void CameraResourcesWorker::acquired(bool *ok) {
237 void CameraResourcesWorker::hijacked(bool *ok) {
241 bool CameraResourcesWorker::updateSet(const QList<ResourcePolicy::ResourceType>& required,
242 const QList<ResourcePolicy::ResourceType>& optional) {
245 QList<ResourcePolicy::ResourceType> set = listSet();
247 foreach (ResourcePolicy::ResourceType r, set) {
248 if (required.indexOf(r) != -1) {
249 m_set->resource(r)->setOptional(false);
251 else if (optional.indexOf(r) != -1) {
252 m_set->resource(r)->setOptional(true);
255 m_set->deleteResource(r);
259 foreach (ResourcePolicy::ResourceType r, required) {
260 m_set->addResource(r);
263 foreach (ResourcePolicy::ResourceType r, optional) {
264 m_set->addResource(r);
265 ResourcePolicy::Resource *res = m_set->resource(r);
267 res->setOptional(true);
271 if (m_set->contains(ResourcePolicy::AudioPlaybackType)) {
272 bool isOptional = m_set->resource(ResourcePolicy::AudioPlaybackType)->isOptional();
274 ResourcePolicy::AudioResource *audio = new ResourcePolicy::AudioResource(APPLICATION_CLASS);
275 audio->setProcessID(QCoreApplication::applicationPid());
276 audio->setOptional(isOptional);
277 m_set->addResourceObject(audio);
285 while (m_acquiring) {
286 QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
292 bool CameraResourcesWorker::release() {
297 while (m_acquiring) {
298 QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
301 m_mode = CameraResources::None;
306 void CameraResourcesWorker::setAcquired(bool acquired) {
307 if (m_acquired != acquired) {
308 m_acquired = acquired;
309 emit acquiredChanged();
313 void CameraResourcesWorker::setHijacked(bool hijacked) {
314 if (m_hijacked != hijacked) {
315 m_hijacked = hijacked;
316 emit hijackedChanged();
320 void CameraResourcesWorker::isResourceGranted(bool *ok, int resource) {
322 ResourcePolicy::ResourceType rt = (ResourcePolicy::ResourceType)resource;
324 ResourcePolicy::Resource *r = m_set->resource(rt);
326 *ok = r->isGranted();