Stop viewfinder after the pipeline reaches idle state.
[harmattan/cameraplus] / lib / qtcamautofocus_p.h
1 // -*- c++ -*-
2
3 /*!
4  * This file is part of CameraPlus.
5  *
6  * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #ifndef QT_CAM_AUTO_FOCUS_P_H
24 #define QT_CAM_AUTO_FOCUS_P_H
25
26 #include "qtcamautofocus.h"
27 #include "qtcamgstreamermessagehandler.h"
28 #include "qtcamgstreamermessagelistener.h"
29 #include "qtcamdevice.h"
30 #include "qtcamdevice_p.h"
31
32 #include <QDebug>
33
34 #ifndef GST_USE_UNSTABLE_API
35 #define GST_USE_UNSTABLE_API
36 #endif /* GST_USE_UNSTABLE_API */
37 #include <gst/interfaces/photography.h>
38
39 class QtCamAutoFocusPrivate : public QObject {
40   Q_OBJECT
41
42 public:
43   QtCamAutoFocusPrivate(QtCamDevice *device, QtCamAutoFocus *q, QObject *parent = 0) :
44     QObject(parent),
45     dev(device),
46     q_ptr(q),
47     status(QtCamAutoFocus::None),
48     cafStatus(QtCamAutoFocus::None) {
49
50     handler = new QtCamGStreamerMessageHandler(GST_PHOTOGRAPHY_AUTOFOCUS_DONE, this);
51
52     QObject::connect(handler, SIGNAL(messageSent(GstMessage *)),
53                      this, SLOT(handleMessage(GstMessage *)));
54
55     dev->listener()->addHandler(handler);
56
57     cafHandler = new QtCamGStreamerMessageHandler("caf-update", this);
58
59     QObject::connect(cafHandler, SIGNAL(messageSent(GstMessage *)),
60                      this, SLOT(handleCafMessage(GstMessage *)));
61
62     dev->listener()->addHandler(cafHandler);
63   }
64
65   ~QtCamAutoFocusPrivate() {
66     dev->listener()->removeHandler(handler);
67     delete handler; handler = 0;
68     dev = 0;
69     q_ptr = 0;
70   }
71
72   bool setEnabled(bool enabled) {
73     if (!dev->d_ptr->videoSource) {
74       return false;
75     }
76
77     if (!GST_IS_PHOTOGRAPHY(dev->d_ptr->videoSource)) {
78       return false;
79     }
80
81     GstPhotography *photo = GST_PHOTOGRAPHY(dev->d_ptr->videoSource);
82     if (!photo) {
83       return false;
84     }
85
86     gst_photography_set_autofocus(photo, enabled ? TRUE : FALSE);
87
88     return true;
89   }
90
91   bool setStatus(QtCamAutoFocus::Status *status, GstMessage *message) {
92     const GstStructure *s = gst_message_get_structure(message);
93     int st = GST_PHOTOGRAPHY_FOCUS_STATUS_NONE;
94
95     if (gst_structure_get_int(s, "status", &st)) {
96       if (*status != st) {
97         *status = (QtCamAutoFocus::Status) st;
98         return true;
99       }
100     }
101
102     return false;
103   }
104
105 public slots:
106   void handleMessage(GstMessage *message) {
107     if (setStatus(&status, message)) {
108       // TODO: focus-window-rows, focus-window-columns, focus-windows and the rest
109       emit q_ptr->statusChanged();
110     }
111   }
112
113   void handleCafMessage(GstMessage *message) {
114     if (setStatus(&cafStatus, message)) {
115       emit q_ptr->cafStatusChanged();
116     }
117   }
118
119 public:
120   QtCamDevice *dev;
121   QtCamAutoFocus *q_ptr;
122
123   QtCamAutoFocus::Status status;
124   QtCamAutoFocus::Status cafStatus;
125
126   QtCamGStreamerMessageHandler *handler;
127   QtCamGStreamerMessageHandler *cafHandler;
128 };
129
130 #endif /* QT_CAM_AUTO_FOCUS_P_H */