Initial commit. corresponds to 1.0-1 release
[flashstrobe] / src / camera.h
1 /*
2   Copyright (C) 2010 by Juan Carlos Torres <jucato@kdemail.net>
3
4   This program is free software; you can redistribute it and/or
5   modify it under the terms of the GNU General Public License as
6   published by the Free Software Foundation; either version 2 of
7   the License or (at your option) version 3 or any later version
8   accepted by the membership of KDE e.V. (or its successor appro-
9   ved by the membership of KDE e.V.), which shall act as a proxy
10   defined in Section 14 of version 3 of the license.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program. If not, see http://www.gnu.org/licenses/.
19 */
20
21
22 #ifndef CAMERA_H
23 #define CAMERA_H
24
25 #include <QtCore/QObject>
26 #include <QtCore/QString>
27 #include <QtCore/QList>
28 #include <QtDBus/QDBusArgument>
29
30 struct buffer
31 {
32     void* start;
33     size_t length;
34 };
35
36 struct Property
37 {
38     QString name;
39     bool added;
40     bool removed;
41 };
42
43 const QDBusArgument& operator<<(QDBusArgument& arg, const Property& change);
44 const QDBusArgument& operator>>(QDBusArgument& arg, const Property& change);
45
46 /**
47  * @brief Qt-based class for accessing the N900 camera and flash LEDs
48  *
49  * This class provides easy access to the N900's camera and flash LED
50  * functionality, specifically initializing the camera and controlling
51  * the LED torch and flash modes. It also reports on the status of
52  * the camera shutter.
53  *
54  */
55 //  TODO: Implement torch functionality
56 class Camera : public QObject
57 {
58     Q_OBJECT
59
60     public:
61         /**
62          * Error codes for easier error message handling
63          */
64         enum ErrorCode
65         {
66             NoError = 0,        /**< No error */
67             GenericError = -1   /**< Generic Error */
68         };
69
70         Camera(QObject* parent = 0);
71         ~Camera();
72
73         /**
74          * Opens a character device connected to the camera
75          * and gets a file descriptor.
76          * 
77          * @param device Device name, normally /dev/video0
78          * @return @c int Identifying the file descriptor
79          * @return @c int -1 Of opening the device was unsuccessful
80          */
81         static int open(char* device);
82
83         /**
84          * Closes the character device connected to the camera.
85          *
86          * @return @c int 0 If closing the device was successful
87          * @return @c int -1 If closing the device was unsuccessful
88          */
89         int close();
90
91         /**
92          * Initializes the camera. This specifically does the following:
93          * @li Get and set the cropping properties
94          * @li Set the camera data format
95          * @li Allocate memory-mapped buffers
96          *
97          * @return @c int 0 If initialization was successful
98          * @return @c int -1 If initialization was unsuccessful
99          */
100         int init();
101
102         /**
103          * Deinitializes the camera, freeing any allocated memory
104          * used during initialization.
105          *
106          * @return @c int 0 If deinitialization was successful
107          * @return @c int -1 If deinitialization was unsuccessful
108          */
109         int deinit();
110
111         /**
112          * Returns the current set flash intensity. This
113          * is different from the torch intensity used in
114          * torch modes.
115          *
116          * @return @c int The current flash intensity
117          */
118         int flashIntensity() const;
119
120         /**
121          * Returns the strobe timeout. Used only in strobe mode.
122          *
123          * @return @c quint32 Current timeout in microseconds
124          */
125         quint32 timeout() const;
126
127         /**
128          * Convenience function to create a D-Bus connection
129          * to listen for changes in the camera shutter status.
130          */
131         void registerDbusWatcher();
132
133         /**
134          * Function to manually query the status of the camera
135          * shutter using D-Bus and HAL.
136          *
137          * @return @c true If the camera shutter is open
138          * @return @c false If the camera shutter is closed
139          */
140         bool isShutterOpen() const;
141
142     public slots:
143
144         /**
145          * Starts or stops camera data streaming. Used to block
146          * and unblock the camera while using the flash.
147          *
148          * @param stream Whether streaming is on (@c true) or off (@c false)
149          *
150          * @return @c int 0 If starting or stopping the stream was successful
151          * @return @c int 1 If starting or stopping the stream was unsuccessful
152          */
153         int stream(bool stream);
154
155         /**
156          * Flash the LED in strobe mode. This will only flash
157          * the LED once. Repetition must be controlled by the
158          * client of this class.
159          *
160          * @return @c int 0 If strobing was successful
161          * @return @c int -1 If strobing was unsuccessful
162          */
163         int strobe();
164
165         /**
166          * Sets the intensity of the LED in flash mode.
167          *
168          * @param intensity intensity in mA. Valid values range
169          *                  from 215 to 500 only
170          *
171          * @return @c int 0 If setting the flash intensity was successful
172          * @return @c int -1 If setting the flash intensity was unsuccessful
173          */
174         int setFlashIntensity(int intensity);
175
176         /**
177          * Sets the strobe timeout. Used only in strobe mode.
178          *
179          * @param timeout Timeout in microseconds. Valid values range
180          *                 from 54600 to 820000 only
181          */
182         int setTimeout(quint32 timeout);
183
184         /**
185          * Slot that is called when the camera shutter status
186          * changes, that is, when it is opened or closed. Internally
187          * calls @ref isShutterOpen() to get the current shutter
188          * status and emits the @ref shutterStateChanged() signal.
189          *
190          * @param numUpdates Unused parameter to match the signal signature
191          * @param updates Unused parameter to match the signal signature
192          */
193         void shutterPropertyModified(int numUpdates, QList<Property> updates);
194
195     signals:
196         /**
197          * Signal to notify change in the camera shutter status.
198          * Users of this class should connect to this to monitor
199          * the shutter state.
200          * 
201          * @param open Determines whether the shutter is open (@c true)
202          *             or closed (@c false).
203          */
204         void shutterStateChanged(bool open);
205
206     private:
207         static char m_deviceName[15];
208         static int m_fd;
209
210         quint32 m_numBuffers;
211         struct buffer* m_buffers;
212
213 };
214
215 #endif