1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
/*
* Copyright (C) 2011 Collabora Ltd.
*
* 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors: Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
*/
#include <config.h>
#include <string.h>
#include <telepathy-glib/util.h>
#include "empathy-camera-monitor.h"
#include "cheese-camera-device-monitor.h"
#include "empathy-utils.h"
#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
#include "empathy-debug.h"
struct _EmpathyCameraMonitorPrivate
{
CheeseCameraDeviceMonitor *cheese_monitor;
GQueue *cameras;
gint num_cameras;
};
enum
{
PROP_0,
PROP_AVAILABLE,
};
enum
{
CAMERA_ADDED,
CAMERA_REMOVED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL];
G_DEFINE_TYPE (EmpathyCameraMonitor, empathy_camera_monitor, G_TYPE_OBJECT);
static EmpathyCameraMonitor *manager_singleton = NULL;
static EmpathyCamera *
empathy_camera_new (const gchar *id,
const gchar *device,
const gchar *name)
{
EmpathyCamera *camera = g_slice_new (EmpathyCamera);
camera->id = g_strdup (id);
camera->device = g_strdup (device);
camera->name = g_strdup (name);
return camera;
}
static EmpathyCamera *
empathy_camera_copy (EmpathyCamera *camera)
{
return empathy_camera_new (camera->id, camera->device, camera->name);
}
static void
empathy_camera_free (EmpathyCamera *camera)
{
g_free (camera->id);
g_free (camera->device);
g_free (camera->name);
g_slice_free (EmpathyCamera, camera);
}
G_DEFINE_BOXED_TYPE (EmpathyCamera, empathy_camera,
empathy_camera_copy, empathy_camera_free)
static gint
empathy_camera_find (gconstpointer a,
gconstpointer b)
{
const EmpathyCamera *camera = a;
const gchar *id = b;
return g_strcmp0 (camera->id, id);
}
static void
empathy_camera_monitor_free_camera_foreach (gpointer data,
gpointer user_data)
{
empathy_camera_free (data);
}
static void
on_camera_added (CheeseCameraDeviceMonitor *device,
gchar *id,
gchar *filename,
gchar *product_name,
gint api_version,
EmpathyCameraMonitor *self)
{
EmpathyCamera *camera = empathy_camera_new (id, filename, product_name);
if (self->priv->cameras == NULL)
return;
g_queue_push_tail (self->priv->cameras, camera);
self->priv->num_cameras++;
if (self->priv->num_cameras == 1)
g_object_notify (G_OBJECT (self), "available");
g_signal_emit (self, signals[CAMERA_ADDED], 0, camera);
}
static void
on_camera_removed (CheeseCameraDeviceMonitor *device,
gchar *id,
EmpathyCameraMonitor *self)
{
EmpathyCamera *camera;
GList *l;
if (self->priv->cameras == NULL)
return;
l = g_queue_find_custom (self->priv->cameras, id, empathy_camera_find);
g_return_if_fail (l != NULL);
camera = l->data;
g_queue_delete_link (self->priv->cameras, l);
self->priv->num_cameras--;
if (self->priv->num_cameras == 0)
g_object_notify (G_OBJECT (self), "available");
g_signal_emit (self, signals[CAMERA_REMOVED], 0, camera);
empathy_camera_free (camera);
}
const GList *
empathy_camera_monitor_get_cameras (EmpathyCameraMonitor *self)
{
if (self->priv->cameras != NULL)
return self->priv->cameras->head;
else
return NULL;
}
static void
empathy_camera_monitor_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
EmpathyCameraMonitor *self = (EmpathyCameraMonitor *) object;
switch (prop_id)
{
case PROP_AVAILABLE:
g_value_set_boolean (value, self->priv->num_cameras > 0);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
empathy_camera_monitor_dispose (GObject *object)
{
EmpathyCameraMonitor *self = EMPATHY_CAMERA_MONITOR (object);
tp_clear_object (&self->priv->cheese_monitor);
g_queue_foreach (self->priv->cameras,
empathy_camera_monitor_free_camera_foreach, NULL);
tp_clear_pointer (&self->priv->cameras, g_queue_free);
G_OBJECT_CLASS (empathy_camera_monitor_parent_class)->dispose (object);
}
static void
empathy_camera_monitor_constructed (GObject *object)
{
EmpathyCameraMonitor *self = (EmpathyCameraMonitor *) object;
G_OBJECT_CLASS (empathy_camera_monitor_parent_class)->constructed (object);
cheese_camera_device_monitor_coldplug (self->priv->cheese_monitor);
}
static void
empathy_camera_monitor_class_init (EmpathyCameraMonitorClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->dispose = empathy_camera_monitor_dispose;
object_class->constructed = empathy_camera_monitor_constructed;
object_class->get_property = empathy_camera_monitor_get_property;
g_object_class_install_property (object_class, PROP_AVAILABLE,
g_param_spec_boolean ("available", "Available",
"Camera available", TRUE, G_PARAM_READABLE));
signals[CAMERA_ADDED] =
g_signal_new ("added", G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
0, NULL, NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE, 1, EMPATHY_TYPE_CAMERA);
signals[CAMERA_REMOVED] =
g_signal_new ("removed", G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
0, NULL, NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE, 1, EMPATHY_TYPE_CAMERA);
g_type_class_add_private (object_class,
sizeof (EmpathyCameraMonitorPrivate));
}
static void
empathy_camera_monitor_init (EmpathyCameraMonitor *self)
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
EMPATHY_TYPE_CAMERA_MONITOR, EmpathyCameraMonitorPrivate);
self->priv->cameras = g_queue_new ();
self->priv->cheese_monitor = cheese_camera_device_monitor_new ();
g_signal_connect (self->priv->cheese_monitor, "added",
G_CALLBACK (on_camera_added), self);
g_signal_connect (self->priv->cheese_monitor, "removed",
G_CALLBACK (on_camera_removed), self);
#ifndef HAVE_UDEV
/* No udev, assume there are cameras present */
self->priv->num_cameras = 1;
#endif
}
EmpathyCameraMonitor *
empathy_camera_monitor_dup_singleton (void)
{
GObject *retval;
if (manager_singleton)
{
retval = g_object_ref (manager_singleton);
}
else
{
retval = g_object_new (EMPATHY_TYPE_CAMERA_MONITOR, NULL);
manager_singleton = EMPATHY_CAMERA_MONITOR (retval);
g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
}
return EMPATHY_CAMERA_MONITOR (retval);
}
EmpathyCameraMonitor *
empathy_camera_monitor_new (void)
{
return EMPATHY_CAMERA_MONITOR (
g_object_new (EMPATHY_TYPE_CAMERA_MONITOR, NULL));
}
gboolean empathy_camera_monitor_get_available (EmpathyCameraMonitor *self)
{
g_return_val_if_fail (EMPATHY_IS_CAMERA_MONITOR (self), FALSE);
return self->priv->num_cameras > 0;
}
|