Reworking resource policy and pipeline handling to use states and transitions
authorMohammed Sameer <msameer@foolab.org>
Thu, 13 Dec 2012 02:01:10 +0000 (04:01 +0200)
committerMohammed Sameer <msameer@foolab.org>
Thu, 13 Dec 2012 02:01:10 +0000 (04:01 +0200)
qml/CameraPage.qml
qml/PipelineManager.qml [new file with mode: 0644]
qml/main.qml

index a0620f0..24ba5e1 100644 (file)
@@ -47,98 +47,8 @@ Page {
         signal batteryLow
 
         function updatePolicy() {
-                if (!resourcePolicy.acquire(page.policyMode)) {
-                        cam.stop(true);
-                }
-                else {
-                        handlePipeline();
-                }
-        }
-
-        Component.onCompleted: {
-                if (Qt.application.active && needsPipeline) {
-                        updatePolicy();
-                }
-                else if (!needsPipeline) {
-                        cam.stop();
-                }
-        }
-
-        onStatusChanged: {
-                if (Qt.application.active && status == PageStatus.Activating) {
-                        updatePolicy();
-                }
-        }
-/*
-        onStatusChanged: {
-                if (status == PageStatus.Active && !page.needsPipeline) {
-                        cam.stop();
-                }
-        }
-*/
-
-        onPolicyModeChanged: {
-                if (Qt.application.active) {
-                        updatePolicy();
-                }
-        }
-
-        function handlePipeline() {
-                var acquired = resourcePolicy.acquired;
-                var hijacked = resourcePolicy.hijacked;
-
-                if (hijacked) {
-                        cam.stop(true);
-                        showError(qsTr("Resources lost. Stopping camera."));
-                }
-                else if (!Qt.application.active && !acquired) {
-                        // I don't think this is needed
-                        cam.stop(true);
-                }
-                else if (!Qt.application.active) {
-                        cam.stop();
-                }
-                else if (acquired && page.needsPipeline && !cam.running) {
-                        if (!cam.start()) {
-                                showError(qsTr("Failed to start camera. Please restart the application."));
-                        }
-                }
-        }
-
-        Connections {
-                target: resourcePolicy
-                onHijackedChanged: handlePipeline();
-                onAcquiredChanged: handlePipeline();
-        }
-
-        Connections {
-                target: Qt.application
-                onActiveChanged: {
-                        if (!Qt.application.active) {
-                                // This is a noop if camera is not
-                                // idle so calling it will not hurt
-                                if (cam.stop()) {
-                                        resourcePolicy.acquire(CameraResources.None);
-                                }
-                        }
-                        else if (page.needsPipeline) {
-                                updatePolicy();
-                        }
-                }
-        }
-
-        Connections {
-                target: cam
-                onIdleChanged: {
-                        if (cam.idle && !Qt.application.active) {
-                                cam.stop();
-                                resourcePolicy.acquire(CameraResources.None);
-                        }
-/*
-                        else if (cam.idle && !page.needsPipeline) {
-                                cam.stop();
-                        }
-*/
+                if (needsPipeline && Qt.application.active && status == PageStatus.Active) {
+                        pipelineManager.reset();
                 }
         }
 
@@ -148,7 +58,8 @@ Page {
                 id: standby
                 color: "black"
                 anchors.fill: parent
-                visible: standbyVisible && page.status == PageStatus.Active && (!Qt.application.active || !cam.running || !resourcePolicy.acquired)
+                visible: standbyVisible && page.status == PageStatus.Active && pipelineManager.showStandBy
+
                 Image {
                         source: "image://theme/icon-l-camera-standby"
                         anchors.centerIn: parent
diff --git a/qml/PipelineManager.qml b/qml/PipelineManager.qml
new file mode 100644 (file)
index 0000000..20b56c4
--- /dev/null
@@ -0,0 +1,135 @@
+// -*- qml -*-
+
+/*!
+ * This file is part of CameraPlus.
+ *
+ * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+import QtQuick 1.1
+import QtCamera 1.0
+import CameraPlus 1.0
+
+// TODO: cleanup debug output
+// TODO: losing resources will freeze camera somehow.
+
+Item {
+        id: handler
+
+        property bool showStandBy: state != "on"
+
+        property Camera camera: null
+        property Item currentPage: pageStack.currentPage
+
+        onCurrentPageChanged: {
+                if (state == "on") {
+                        startCamera();
+                }
+        }
+
+        CameraResources {
+                id: policy
+        }
+
+        function startCamera() {
+                console.log("start");
+                if (!policy.acquire(currentPage.policyMode)) {
+                        console.log("Failed to acquire policy resources");
+                        camera.stop(true);
+                        return;
+                }
+
+                if (!camera.start()) {
+                        showError(qsTr("Failed to start camera. Please restart the application."));
+                }
+        }
+
+        function stopCamera() {
+                console.log("stop");
+                camera.stop(false);
+                policy.acquire(CameraResources.None);
+        }
+
+        function forceStopCamera() {
+                // We don't release resources here so we can get them back when they become available
+                console.log("force");
+                camera.stop(true);
+                showError(qsTr("Resources lost. Stopping camera."));
+        }
+
+        state: "off"
+
+        onStateChanged: console.log("New state " + handler.state);
+
+        states: [
+        State {
+                name: "on"
+                when: Qt.application.active && currentPage && currentPage.needsPipeline && !policy.hijacked
+        },
+        State {
+                name: "off"
+                when: (!Qt.application.active && camera.idle) || (currentPage && !currentPage.needsPipeline)
+        },
+        State {
+                name: "policyLost"
+                when: policy.hijacked
+        },
+        State {
+                name: "error"
+        }
+        ]
+
+        transitions: [
+        Transition {
+                from: "on"
+                to: "off"
+                ScriptAction {
+                        script: stopCamera();
+                }
+        },
+        Transition {
+                from: "off"
+                to: "on"
+                ScriptAction {
+                        script: handler.startCamera();
+                }
+        },
+
+        Transition {
+                from: "on"
+                to: "policyLost"
+                ScriptAction {
+                        script: forceStopCamera();
+                }
+        },
+
+        Transition {
+                from: "policyLost"
+                to: "off"
+                ScriptAction {
+                        script: stopCamera();
+                }
+        },
+        Transition {
+                from: "policyLost"
+                to: "on"
+                ScriptAction {
+                        script: startCamera();
+                }
+        }
+        ]
+}
index 51be16a..5a54eef 100644 (file)
@@ -114,8 +114,9 @@ PageStackWindow {
                 active: cam.running && settings.useGps && settings.useGeotags
         }
 
-        CameraResources {
-                id: resourcePolicy
+        PipelineManager {
+                id: pipelineManager
+                camera: cam
         }
 
         DeviceInfo {