silence dbus-send output
[harmattan/cameraplus] / lib / qtcamscanner.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "qtcamscanner.h"
22 #include "qtcamconfig.h"
23 #include <QDir>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <linux/videodev2.h>
28 #include <sys/ioctl.h>
29 #include <unistd.h>
30 #include <gst/gst.h>
31
32 class QtCamScannerPrivate {
33 public:
34   void scanEnum();
35   void scanV4l2();
36
37   QtCamConfig *conf;
38   QList<QPair<QString, QVariant> > devices;
39 };
40
41 void QtCamScannerPrivate::scanEnum() {
42   // Too bad there's no way to get the values of an enum without creating the element :(
43   GstElement *elem = gst_element_factory_make(conf->videoSource().toLatin1(), NULL);
44   if (!elem) {
45     return;
46   }
47
48   GParamSpec *spec = g_object_class_find_property(G_OBJECT_GET_CLASS(elem),
49                                                   conf->deviceScannerProperty().toLatin1());
50   if (!spec) {
51     gst_object_unref(elem);
52     return;
53   }
54
55   if (!G_IS_PARAM_SPEC_ENUM(spec)) {
56     gst_object_unref(elem);
57     return;
58   }
59
60   GParamSpecEnum *e = G_PARAM_SPEC_ENUM(spec);
61   // First add the default:
62   devices << qMakePair<QString, QVariant>(e->enum_class->values[e->default_value].value_name,
63                                           QByteArray::number(e->default_value));
64
65   for (int x = e->enum_class->minimum; x <= e->enum_class->maximum; x++) {
66     if (x != e->default_value) {
67       devices << qMakePair<QString, QVariant>(e->enum_class->values[x].value_name,
68                                               QByteArray::number(x));
69     }
70   }
71
72   gst_object_unref(elem);
73 }
74
75 void QtCamScannerPrivate::scanV4l2() {
76   QDir d("/dev/", "video?", QDir::Name | QDir::IgnoreCase, QDir::System);
77
78   QStringList entries = d.entryList();
79
80   foreach (const QString& dv, entries) {
81     QString dev = d.absoluteFilePath(dv);
82     struct v4l2_capability cap;
83     memset(&cap, 0x0, sizeof(cap));
84
85     int fd = open(dev.toLocal8Bit().constData(), O_RDONLY);
86     if (fd == -1) {
87       continue;
88     }
89
90     if (ioctl(fd, VIDIOC_QUERYCAP, &cap) != 0) {
91       close(fd);
92       continue;
93     }
94
95     close(fd);
96
97     if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) {
98       devices << qMakePair<QString, QVariant>((char *)cap.card, dev.toLocal8Bit());
99     }
100   }
101 }
102
103 QtCamScanner::QtCamScanner(QtCamConfig *conf, QObject *parent) :
104   QObject(parent), d_ptr(new QtCamScannerPrivate) {
105
106   d_ptr->conf = conf;
107 }
108
109 QtCamScanner::~QtCamScanner() {
110   delete d_ptr; d_ptr = 0;
111 }
112
113 void QtCamScanner::refresh() {
114   d_ptr->devices.clear();
115
116   if (d_ptr->conf->deviceScannerType() == SCANNER_TYPE_ENUM) {
117     d_ptr->scanEnum();
118   }
119   else {
120     d_ptr->scanV4l2();
121   }
122 }
123
124 QList<QPair<QString, QVariant> > QtCamScanner::devices() const {
125   return d_ptr->devices;
126 }