deade4c762207efe12a650e7b9a9146280894c6a
[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 #include <QPointer>
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     if (dev && dev->listener()) {
67       dev->listener()->removeHandler(handler);
68       delete handler; handler = 0;
69     }
70
71     dev = 0;
72     q_ptr = 0;
73   }
74
75   bool setEnabled(bool enabled) {
76     if (!dev->d_ptr->videoSource) {
77       return false;
78     }
79
80     if (!GST_IS_PHOTOGRAPHY(dev->d_ptr->videoSource)) {
81       return false;
82     }
83
84     GstPhotography *photo = GST_PHOTOGRAPHY(dev->d_ptr->videoSource);
85     if (!photo) {
86       return false;
87     }
88
89     gst_photography_set_autofocus(photo, enabled ? TRUE : FALSE);
90
91     if (enabled) {
92       status = QtCamAutoFocus::Running;
93       emit q_ptr->statusChanged();
94     }
95     else {
96       status = QtCamAutoFocus::None;
97       emit q_ptr->statusChanged();
98     }
99
100     return true;
101   }
102
103   bool setStatus(QtCamAutoFocus::Status *status, GstMessage *message) {
104     const GstStructure *s = gst_message_get_structure(message);
105     int st = GST_PHOTOGRAPHY_FOCUS_STATUS_NONE;
106
107     if (gst_structure_get_int(s, "status", &st)) {
108       if (*status != st) {
109         *status = (QtCamAutoFocus::Status) st;
110         return true;
111       }
112     }
113
114     return false;
115   }
116
117 public slots:
118   void handleMessage(GstMessage *message) {
119     if (setStatus(&status, message)) {
120       // TODO: focus-window-rows, focus-window-columns, focus-windows and the rest
121       emit q_ptr->statusChanged();
122     }
123   }
124
125   void handleCafMessage(GstMessage *message) {
126     if (setStatus(&cafStatus, message)) {
127       emit q_ptr->cafStatusChanged();
128     }
129   }
130
131 public:
132   QPointer<QtCamDevice> dev;
133   QtCamAutoFocus *q_ptr;
134
135   QtCamAutoFocus::Status status;
136   QtCamAutoFocus::Status cafStatus;
137
138   QtCamGStreamerMessageHandler *handler;
139   QtCamGStreamerMessageHandler *cafHandler;
140 };
141
142 #endif /* QT_CAM_AUTO_FOCUS_P_H */