Merge lp:~cjcurran/indicator-sound/pulse-refactor into lp:~indicator-applet-developers/indicator-sound/trunk_3
- pulse-refactor
- Merge into trunk_3
Proposed by
Conor Curran
Status: | Merged | ||||
---|---|---|---|---|---|
Merged at revision: | 125 | ||||
Proposed branch: | lp:~cjcurran/indicator-sound/pulse-refactor | ||||
Merge into: | lp:~indicator-applet-developers/indicator-sound/trunk_3 | ||||
Diff against target: |
2001 lines (+865/-832) 13 files modified
src/Makefile.am (+5/-4) src/active-sink.c (+293/-0) src/active-sink.h (+72/-0) src/mute-menu-item.c (+8/-4) src/pulse-manager.c (+0/-589) src/pulse-manager.h (+0/-45) src/pulseaudio-mgr.c (+404/-0) src/pulseaudio-mgr.h (+31/-0) src/slider-menu-item.c (+23/-23) src/slider-menu-item.h (+2/-2) src/sound-service-dbus.c (+22/-157) src/sound-service-dbus.h (+4/-6) src/sound-service.c (+1/-2) |
||||
To merge this branch: | bzr merge lp:~cjcurran/indicator-sound/pulse-refactor | ||||
Related bugs: |
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Ted Gould (community) | Approve | ||
Review via email: mp+48781@code.launchpad.net |
Commit message
Description of the change
Final stage in this code tidy up, pulsemanager now totally refactored, more efficient and coherent.
To post a comment you must log in.
Revision history for this message
Conor Curran (cjcurran) wrote : | # |
It will try to reconnect through the pulse state callback, refactored though as you suggested to reuse the reconnect method on both instances (initial connection and reconnections).
well spotted on the typo in the mute-menuitem.c.
Fixed and committed.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'src/Makefile.am' |
2 | --- src/Makefile.am 2011-01-26 22:08:46 +0000 |
3 | +++ src/Makefile.am 2011-02-07 18:12:56 +0000 |
4 | @@ -72,7 +72,6 @@ |
5 | --pkg gdk-pixbuf-2.0 \ |
6 | --pkg libxml-2.0 |
7 | |
8 | - |
9 | $(MAINTAINER_VALAFLAGS) |
10 | |
11 | music_bridge_APIFILES = \ |
12 | @@ -89,8 +88,10 @@ |
13 | common-defs.h \ |
14 | sound-service.h \ |
15 | sound-service.c \ |
16 | - pulse-manager.h \ |
17 | - pulse-manager.c \ |
18 | + pulseaudio-mgr.h \ |
19 | + pulseaudio-mgr.c \ |
20 | + active-sink.c \ |
21 | + active-sink.h \ |
22 | sound-service-dbus.h \ |
23 | sound-service-dbus.c \ |
24 | slider-menu-item.h \ |
25 | @@ -108,7 +109,7 @@ |
26 | # Service xml compilation |
27 | ######################### |
28 | DBUS_SPECS = \ |
29 | - sound-service.xml |
30 | + sound-service.xml |
31 | |
32 | gen-%.xml.h: %.xml |
33 | @echo "Building $@ from $<" |
34 | |
35 | === added file 'src/active-sink.c' |
36 | --- src/active-sink.c 1970-01-01 00:00:00 +0000 |
37 | +++ src/active-sink.c 2011-02-07 18:12:56 +0000 |
38 | @@ -0,0 +1,293 @@ |
39 | +/* |
40 | +Copyright 2011 Canonical Ltd. |
41 | + |
42 | +Authors: |
43 | + Conor Curran <conor.curran@canonical.com> |
44 | + |
45 | +This program is free software: you can redistribute it and/or modify it |
46 | +under the terms of the GNU General Public License version 3, as published |
47 | +by the Free Software Foundation. |
48 | + |
49 | +This program is distributed in the hope that it will be useful, but |
50 | +WITHOUT ANY WARRANTY; without even the implied warranties of |
51 | +MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
52 | +PURPOSE. See the GNU General Public License for more details. |
53 | + |
54 | +You should have received a copy of the GNU General Public License along |
55 | +with this program. If not, see <http://www.gnu.org/licenses/>. |
56 | +*/ |
57 | +#include <libdbusmenu-glib/menuitem.h> |
58 | + |
59 | +#include "active-sink.h" |
60 | +#include "slider-menu-item.h" |
61 | +#include "mute-menu-item.h" |
62 | + |
63 | +#include "pulseaudio-mgr.h" |
64 | + |
65 | +typedef struct _ActiveSinkPrivate ActiveSinkPrivate; |
66 | + |
67 | +struct _ActiveSinkPrivate |
68 | +{ |
69 | + SliderMenuItem* volume_slider_menuitem; |
70 | + MuteMenuItem* mute_menuitem; |
71 | + SoundState current_sound_state; |
72 | + SoundServiceDbus* service; |
73 | + gint index; |
74 | + gchar* name; |
75 | + pa_cvolume volume; |
76 | + pa_channel_map channel_map; |
77 | + pa_volume_t base_volume; |
78 | +}; |
79 | + |
80 | +#define ACTIVE_SINK_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), ACTIVE_SINK_TYPE, ActiveSinkPrivate)) |
81 | + |
82 | +/* Prototypes */ |
83 | +static void active_sink_class_init (ActiveSinkClass *klass); |
84 | +static void active_sink_init (ActiveSink *self); |
85 | +static void active_sink_dispose (GObject *object); |
86 | +static void active_sink_finalize (GObject *object); |
87 | + |
88 | +static SoundState active_sink_get_state_from_volume (ActiveSink* self); |
89 | +static pa_cvolume active_sink_construct_mono_volume (const pa_cvolume* vol); |
90 | +static void active_sink_volume_update (ActiveSink* self, gdouble percent); |
91 | +static void active_sink_mute_update (ActiveSink* self, gboolean muted); |
92 | + |
93 | + |
94 | +G_DEFINE_TYPE (ActiveSink, active_sink, G_TYPE_OBJECT); |
95 | + |
96 | +static void |
97 | +active_sink_class_init (ActiveSinkClass *klass) |
98 | +{ |
99 | + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
100 | + |
101 | + g_type_class_add_private (klass, sizeof (ActiveSinkPrivate)); |
102 | + |
103 | + gobject_class->dispose = active_sink_dispose; |
104 | + gobject_class->finalize = active_sink_finalize; |
105 | +} |
106 | + |
107 | +static void |
108 | +active_sink_init (ActiveSink *self) |
109 | +{ |
110 | + ActiveSinkPrivate* priv = ACTIVE_SINK_GET_PRIVATE (self); |
111 | + priv->mute_menuitem = NULL; |
112 | + priv->volume_slider_menuitem = NULL; |
113 | + priv->current_sound_state = UNAVAILABLE; |
114 | + priv->index = -1; |
115 | + priv->name = NULL; |
116 | + priv->service = NULL; |
117 | + |
118 | + // Init our menu items. |
119 | + priv->mute_menuitem = g_object_new (MUTE_MENU_ITEM_TYPE, NULL); |
120 | + priv->volume_slider_menuitem = slider_menu_item_new (self); |
121 | + mute_menu_item_enable (priv->mute_menuitem, FALSE); |
122 | + slider_menu_item_enable (priv->volume_slider_menuitem, FALSE); |
123 | +} |
124 | + |
125 | +static void |
126 | +active_sink_dispose (GObject *object) |
127 | +{ |
128 | + G_OBJECT_CLASS (active_sink_parent_class)->dispose (object); |
129 | +} |
130 | + |
131 | +static void |
132 | +active_sink_finalize (GObject *object) |
133 | +{ |
134 | + G_OBJECT_CLASS (active_sink_parent_class)->finalize (object); |
135 | +} |
136 | + |
137 | +void |
138 | +active_sink_populate (ActiveSink* sink, |
139 | + const pa_sink_info* update) |
140 | +{ |
141 | + ActiveSinkPrivate* priv = ACTIVE_SINK_GET_PRIVATE(sink); |
142 | + |
143 | + priv->name = g_strdup (update->name); |
144 | + priv->index = update->index; |
145 | + active_sink_mute_update (sink, update->mute); |
146 | + priv->volume = active_sink_construct_mono_volume (&update->volume); |
147 | + priv->base_volume = update->base_volume; |
148 | + priv->channel_map = update->channel_map; |
149 | + |
150 | + pa_volume_t vol = pa_cvolume_max (&update->volume); |
151 | + gdouble volume_percent = ((gdouble) vol * 100) / PA_VOLUME_NORM; |
152 | + |
153 | + active_sink_volume_update (sink, volume_percent); |
154 | + active_sink_mute_update (sink, update->mute); |
155 | + mute_menu_item_enable (priv->mute_menuitem, TRUE); |
156 | + slider_menu_item_enable (priv->volume_slider_menuitem, TRUE); |
157 | + |
158 | + g_debug ("Active sink has been populated - volume %f", volume_percent); |
159 | +} |
160 | + |
161 | +void |
162 | +active_sink_update (ActiveSink* sink, |
163 | + const pa_sink_info* update) |
164 | +{ |
165 | + ActiveSinkPrivate* priv = ACTIVE_SINK_GET_PRIVATE (sink); |
166 | + active_sink_mute_update (sink, update->mute); |
167 | + priv->volume = active_sink_construct_mono_volume (&update->volume); |
168 | + priv->base_volume = update->base_volume; |
169 | + priv->channel_map = update->channel_map; |
170 | + |
171 | + pa_volume_t vol = pa_cvolume_max (&update->volume); |
172 | + gdouble volume_percent = ((gdouble) vol * 100) / PA_VOLUME_NORM; |
173 | + |
174 | + active_sink_volume_update (sink, volume_percent); |
175 | + active_sink_mute_update (sink, update->mute); |
176 | +} |
177 | + |
178 | +// To the UI |
179 | +static void |
180 | +active_sink_volume_update (ActiveSink* self, gdouble percent) |
181 | +{ |
182 | + ActiveSinkPrivate* priv = ACTIVE_SINK_GET_PRIVATE (self); |
183 | + slider_menu_item_update (priv->volume_slider_menuitem, percent); |
184 | + SoundState state = active_sink_get_state_from_volume (self); |
185 | + if (priv->current_sound_state != state){ |
186 | + priv->current_sound_state = state; |
187 | + sound_service_dbus_update_sound_state (priv->service, |
188 | + priv->current_sound_state); |
189 | + } |
190 | +} |
191 | + |
192 | +// From the UI |
193 | +void |
194 | +active_sink_update_volume (ActiveSink* self, gdouble percent) |
195 | +{ |
196 | + pa_cvolume new_volume; |
197 | + pa_cvolume_init(&new_volume); |
198 | + new_volume.channels = 1; |
199 | + pa_volume_t new_volume_value = (pa_volume_t) ((percent * PA_VOLUME_NORM) / 100); |
200 | + pa_cvolume_set(&new_volume, 1, new_volume_value); |
201 | + |
202 | + ActiveSinkPrivate* priv = ACTIVE_SINK_GET_PRIVATE (self); |
203 | + |
204 | + pa_cvolume_set(&priv->volume, priv->channel_map.channels, new_volume_value); |
205 | + pm_update_volume (priv->index, new_volume); |
206 | +} |
207 | + |
208 | + |
209 | +static void |
210 | +active_sink_mute_update (ActiveSink* self, gboolean muted) |
211 | +{ |
212 | + ActiveSinkPrivate* priv = ACTIVE_SINK_GET_PRIVATE (self); |
213 | + mute_menu_item_update (priv->mute_menuitem, muted); |
214 | + SoundState state = active_sink_get_state_from_volume (self); |
215 | + |
216 | + if (muted == TRUE){ |
217 | + state = MUTED; |
218 | + } |
219 | + if (priv->current_sound_state != state){ |
220 | + priv->current_sound_state = state; |
221 | + sound_service_dbus_update_sound_state (priv->service, state); |
222 | + } |
223 | +} |
224 | + |
225 | +void |
226 | +active_sink_ensure_sink_is_unmuted (ActiveSink* self) |
227 | +{ |
228 | + ActiveSinkPrivate* priv = ACTIVE_SINK_GET_PRIVATE (self); |
229 | + if (mute_menu_item_is_muted (priv->mute_menuitem)){ |
230 | + pm_update_mute (FALSE); |
231 | + } |
232 | +} |
233 | + |
234 | + |
235 | +static SoundState |
236 | +active_sink_get_state_from_volume (ActiveSink* self) |
237 | +{ |
238 | + ActiveSinkPrivate* priv = ACTIVE_SINK_GET_PRIVATE (self); |
239 | + GVariant* v = dbusmenu_menuitem_property_get_variant (DBUSMENU_MENUITEM(priv->volume_slider_menuitem), |
240 | + DBUSMENU_VOLUME_MENUITEM_LEVEL); |
241 | + gdouble volume_percent = g_variant_get_double (v); |
242 | + |
243 | + SoundState state = LOW_LEVEL; |
244 | + |
245 | + if (volume_percent < 30.0 && volume_percent > 0) { |
246 | + state = LOW_LEVEL; |
247 | + } |
248 | + else if (volume_percent < 70.0 && volume_percent >= 30.0) { |
249 | + state = MEDIUM_LEVEL; |
250 | + } |
251 | + else if (volume_percent >= 70.0) { |
252 | + state = HIGH_LEVEL; |
253 | + } |
254 | + else if (volume_percent == 0.0) { |
255 | + state = ZERO_LEVEL; |
256 | + } |
257 | + return state; |
258 | +} |
259 | + |
260 | +static pa_cvolume |
261 | +active_sink_construct_mono_volume (const pa_cvolume* vol) |
262 | +{ |
263 | + pa_cvolume new_volume; |
264 | + pa_cvolume_init(&new_volume); |
265 | + new_volume.channels = 1; |
266 | + pa_volume_t max_vol = pa_cvolume_max(vol); |
267 | + pa_cvolume_set(&new_volume, 1, max_vol); |
268 | + return new_volume; |
269 | +} |
270 | + |
271 | +void |
272 | +active_sink_determine_blocking_state (ActiveSink* self) |
273 | +{ |
274 | + ActiveSinkPrivate* priv = ACTIVE_SINK_GET_PRIVATE (self); |
275 | + if (mute_menu_item_is_muted (priv->mute_menuitem)){ |
276 | + /** |
277 | + We don't want to set the current state to blocking |
278 | + as this is a fire and forget event. |
279 | + */ |
280 | + sound_service_dbus_update_sound_state (priv->service, |
281 | + BLOCKED); |
282 | + } |
283 | +} |
284 | + |
285 | +gint |
286 | +active_sink_get_index (ActiveSink* self) |
287 | +{ |
288 | + ActiveSinkPrivate* priv = ACTIVE_SINK_GET_PRIVATE (self); |
289 | + return priv->index; |
290 | +} |
291 | + |
292 | +gboolean |
293 | +active_sink_is_populated (ActiveSink* sink) |
294 | +{ |
295 | + ActiveSinkPrivate* priv = ACTIVE_SINK_GET_PRIVATE (sink); |
296 | + return (priv->index != -1); |
297 | +} |
298 | + |
299 | +void |
300 | +active_sink_deactivate (ActiveSink* self) |
301 | +{ |
302 | + ActiveSinkPrivate* priv = ACTIVE_SINK_GET_PRIVATE (self); |
303 | + priv->current_sound_state = UNAVAILABLE; |
304 | + sound_service_dbus_update_sound_state (priv->service, |
305 | + priv->current_sound_state); |
306 | + mute_menu_item_enable (priv->mute_menuitem, FALSE); |
307 | + slider_menu_item_enable (priv->volume_slider_menuitem, FALSE); |
308 | + priv->index = -1; |
309 | + g_free(priv->name); |
310 | + priv->name = NULL; |
311 | +} |
312 | + |
313 | +SoundState |
314 | +active_sink_get_state (ActiveSink* self) |
315 | +{ |
316 | + ActiveSinkPrivate* priv = ACTIVE_SINK_GET_PRIVATE (self); |
317 | + return priv->current_sound_state; |
318 | +} |
319 | + |
320 | +ActiveSink* |
321 | +active_sink_new (SoundServiceDbus* service) |
322 | +{ |
323 | + ActiveSink* sink = g_object_new (ACTIVE_SINK_TYPE, NULL); |
324 | + ActiveSinkPrivate* priv = ACTIVE_SINK_GET_PRIVATE (sink); |
325 | + priv->service = service; |
326 | + sound_service_dbus_build_sound_menu (service, |
327 | + mute_menu_item_get_button (priv->mute_menuitem), |
328 | + DBUSMENU_MENUITEM (priv->volume_slider_menuitem)); |
329 | + pm_establish_pulse_connection (sink); |
330 | + return sink; |
331 | +} |
332 | |
333 | === added file 'src/active-sink.h' |
334 | --- src/active-sink.h 1970-01-01 00:00:00 +0000 |
335 | +++ src/active-sink.h 2011-02-07 18:12:56 +0000 |
336 | @@ -0,0 +1,72 @@ |
337 | +/* |
338 | + * Copyright 2010 Canonical Ltd. |
339 | + * |
340 | + * Authors: |
341 | + * Conor Curran <conor.curran@canonical.com> |
342 | + * |
343 | + * This program is free software: you can redistribute it and/or modify it |
344 | + * under the terms of the GNU General Public License version 3, as published |
345 | + * by the Free Software Foundation. |
346 | + * |
347 | + * This program is distributed in the hope that it will be useful, but |
348 | + * WITHOUT ANY WARRANTY; without even the implied warranties of |
349 | + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
350 | + * PURPOSE. See the GNU General Public License for more details. |
351 | + * |
352 | + * You should have received a copy of the GNU General Public License along |
353 | + * with this program. If not, see <http://www.gnu.org/licenses/>. |
354 | + */ |
355 | + |
356 | +#ifndef __ACTIVE_SINK_H__ |
357 | +#define __ACTIVE_SINK_H__ |
358 | + |
359 | +#include <glib.h> |
360 | +#include <glib-object.h> |
361 | + |
362 | +#include "common-defs.h" |
363 | +#include "sound-service-dbus.h" |
364 | + |
365 | +#include <pulse/pulseaudio.h> |
366 | + |
367 | +G_BEGIN_DECLS |
368 | + |
369 | +#define ACTIVE_SINK_TYPE (active_sink_get_type ()) |
370 | +#define ACTIVE_SINK(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), ACTIVE_SINK_TYPE, ActiveSink)) |
371 | +#define ACTIVE_SINK_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), ACTIVE_SINK_TYPE, ActiveSinkClass)) |
372 | +#define IS_ACTIVE_SINK(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), ACTIVE_SINK_TYPE)) |
373 | +#define IS_ACTIVE_SINK_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), ACTIVE_SINK_TYPE)) |
374 | +#define ACTIVE_SINK_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), ACTIVE_SINK_TYPE, ActiveSinkClass)) |
375 | + |
376 | +typedef struct _ActiveSink ActiveSink; |
377 | +typedef struct _ActiveSinkClass ActiveSinkClass; |
378 | + |
379 | +struct _ActiveSink { |
380 | + GObject parent; |
381 | +}; |
382 | + |
383 | +struct _ActiveSinkClass { |
384 | + GObjectClass parent_class; |
385 | +}; |
386 | + |
387 | +GType active_sink_get_type (void) G_GNUC_CONST; |
388 | + |
389 | +void active_sink_populate (ActiveSink* sink, const pa_sink_info* update); |
390 | +void active_sink_update (ActiveSink* sink, const pa_sink_info* update); |
391 | + |
392 | +gboolean active_sink_is_populated (ActiveSink* sink); |
393 | +void active_sink_determine_blocking_state (ActiveSink* self); |
394 | + |
395 | +gint active_sink_get_index (ActiveSink* self); |
396 | +SoundState active_sink_get_state (ActiveSink* self); |
397 | + |
398 | +void active_sink_deactivate (ActiveSink* self); |
399 | + |
400 | +void active_sink_update_mute (ActiveSink* self, gboolean mute_update); |
401 | +void active_sink_update_volume (ActiveSink* self, gdouble percent); |
402 | +void active_sink_ensure_sink_is_unmuted (ActiveSink* self); |
403 | + |
404 | +ActiveSink* active_sink_new (SoundServiceDbus* service); |
405 | + |
406 | +G_END_DECLS |
407 | + |
408 | +#endif |
409 | |
410 | === modified file 'src/mute-menu-item.c' |
411 | --- src/mute-menu-item.c 2011-01-27 22:10:48 +0000 |
412 | +++ src/mute-menu-item.c 2011-02-07 18:12:56 +0000 |
413 | @@ -24,7 +24,7 @@ |
414 | |
415 | #include "common-defs.h" |
416 | #include "mute-menu-item.h" |
417 | -#include "pulse-manager.h" |
418 | +#include "pulseaudio-mgr.h" |
419 | |
420 | typedef struct _MuteMenuItemPrivate MuteMenuItemPrivate; |
421 | |
422 | @@ -62,6 +62,9 @@ |
423 | g_debug("Building new Mute Menu Item"); |
424 | MuteMenuItemPrivate* priv = MUTE_MENU_ITEM_GET_PRIVATE(self); |
425 | priv->button = dbusmenu_menuitem_new(); |
426 | + dbusmenu_menuitem_property_set_bool (priv->button, |
427 | + DBUSMENU_MENUITEM_PROP_VISIBLE, |
428 | + TRUE); |
429 | |
430 | g_signal_connect (G_OBJECT (priv->button), |
431 | DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED, |
432 | @@ -90,10 +93,8 @@ |
433 | DbusmenuMenuitem* button = DBUSMENU_MENUITEM (user_data); |
434 | gboolean current_value = dbusmenu_menuitem_property_get_bool (button, |
435 | DBUSMENU_MUTE_MENUITEM_VALUE); |
436 | - |
437 | gboolean new_value = !current_value; |
438 | - // pa manager api - to be refactored |
439 | - toggle_global_mute (new_value); |
440 | + pm_update_mute (new_value); |
441 | } |
442 | |
443 | void |
444 | @@ -113,6 +114,9 @@ |
445 | mute_menu_item_enable (MuteMenuItem* item, gboolean active) |
446 | { |
447 | MuteMenuItemPrivate* priv = MUTE_MENU_ITEM_GET_PRIVATE (item); |
448 | + dbusmenu_menuitem_property_set_bool (priv->button, |
449 | + DBUSMENU_MENUITEM_PROP_VISIBLE, |
450 | + TRUE); |
451 | |
452 | dbusmenu_menuitem_property_set_bool (priv->button, |
453 | DBUSMENU_MENUITEM_PROP_ENABLED, |
454 | |
455 | === removed file 'src/pulse-manager.c' |
456 | --- src/pulse-manager.c 2011-01-27 22:10:48 +0000 |
457 | +++ src/pulse-manager.c 1970-01-01 00:00:00 +0000 |
458 | @@ -1,589 +0,0 @@ |
459 | -/* |
460 | -A small wrapper utility to load indicators and put them as menu items |
461 | -into the gnome-panel using it's applet interface. |
462 | - |
463 | -Copyright 2010 Canonical Ltd. |
464 | - |
465 | -Authors: |
466 | - Conor Curran <conor.curran@canonical.com> |
467 | - |
468 | -This program is free software: you can redistribute it and/or modify it |
469 | -under the terms of the GNU General Public License version 3, as published |
470 | -by the Free Software Foundation. |
471 | - |
472 | -This program is distributed in the hope that it will be useful, but |
473 | -WITHOUT ANY WARRANTY; without even the implied warranties of |
474 | -MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
475 | -PURPOSE. See the GNU General Public License for more details. |
476 | - |
477 | -You should have received a copy of the GNU General Public License along |
478 | -with this program. If not, see <http://www.gnu.org/licenses/>. |
479 | -*/ |
480 | - |
481 | -#include <pulse/glib-mainloop.h> |
482 | -#include <pulse/error.h> |
483 | -#include <pulse/gccmacro.h> |
484 | - |
485 | -#include "pulse-manager.h" |
486 | - |
487 | -#define RECONNECT_DELAY 5 |
488 | - |
489 | -static GHashTable *sink_hash = NULL; |
490 | -static SoundServiceDbus *dbus_service = NULL; |
491 | -static gint DEFAULT_SINK_INDEX = -1; |
492 | -static gboolean pa_server_available = FALSE; |
493 | -static gint reconnect_idle_id = 0; |
494 | -static pa_context *pulse_context = NULL; |
495 | -static pa_glib_mainloop *pa_main_loop = NULL; |
496 | - |
497 | -static void context_state_callback(pa_context *c, void *userdata); |
498 | -static gboolean reconnect_to_pulse(); |
499 | -static void pulse_sink_info_callback(pa_context *c, const pa_sink_info *sink_info, int eol, void *userdata); |
500 | -static void context_success_callback(pa_context *c, int success, void *userdata); |
501 | -static void pulse_sink_input_info_callback(pa_context *c, const pa_sink_input_info *info, int eol, void *userdata); |
502 | -static void pulse_server_info_callback(pa_context *c, const pa_server_info *info, void *userdata); |
503 | -static void update_sink_info(pa_context *c, const pa_sink_info *info, int eol, void *userdata); |
504 | -static void destroy_sink_info(void *value); |
505 | -static gboolean determine_sink_availability(); |
506 | - |
507 | -static gboolean has_volume_changed(const pa_sink_info* new_sink, sink_info* cached_sink); |
508 | -static pa_cvolume construct_mono_volume(const pa_cvolume* vol); |
509 | - |
510 | -/** |
511 | -Future Refactoring notes |
512 | - - rewrite in vala. |
513 | - - make sure all state is kept in the service for volume icon switching. |
514 | -**/ |
515 | - |
516 | -/** |
517 | -Entry point |
518 | -**/ |
519 | -void establish_pulse_activities(SoundServiceDbus *service) |
520 | -{ |
521 | - dbus_service = service; |
522 | - pa_main_loop = pa_glib_mainloop_new(g_main_context_default()); |
523 | - g_assert(pa_main_loop); |
524 | - pulse_context = pa_context_new(pa_glib_mainloop_get_api(pa_main_loop), |
525 | - "com.canonical.indicators.sound"); |
526 | - g_assert(pulse_context); |
527 | - |
528 | - sink_hash = g_hash_table_new_full(g_direct_hash, |
529 | - g_direct_equal, |
530 | - NULL, |
531 | - destroy_sink_info); |
532 | - |
533 | - // Establish event callback registration |
534 | - pa_context_set_state_callback (pulse_context, context_state_callback, NULL); |
535 | - sound_service_dbus_update_pa_state (dbus_service, FALSE, FALSE, 0); |
536 | - pa_context_connect (pulse_context, NULL, PA_CONTEXT_NOFAIL, NULL); |
537 | -} |
538 | - |
539 | -/** |
540 | -get_context() |
541 | -Needed for testing - bah! |
542 | -**/ |
543 | -pa_context* get_context() |
544 | -{ |
545 | - return pulse_context; |
546 | -} |
547 | - |
548 | -static gboolean |
549 | -reconnect_to_pulse() |
550 | -{ |
551 | - g_debug("Attempt to reconnect to pulse"); |
552 | - // reset |
553 | - if (pulse_context != NULL) { |
554 | - pa_context_unref(pulse_context); |
555 | - pulse_context = NULL; |
556 | - } |
557 | - |
558 | - if (sink_hash != NULL) { |
559 | - g_hash_table_destroy(sink_hash); |
560 | - sink_hash = NULL; |
561 | - } |
562 | - pulse_context = pa_context_new( pa_glib_mainloop_get_api( pa_main_loop ), |
563 | - "com.canonical.indicators.sound" ); |
564 | - g_assert(pulse_context); |
565 | - sink_hash = g_hash_table_new_full( g_direct_hash, g_direct_equal, |
566 | - NULL, |
567 | - destroy_sink_info ); |
568 | - // Establish event callback registration |
569 | - pa_context_set_state_callback (pulse_context, context_state_callback, NULL); |
570 | - int result = pa_context_connect (pulse_context, NULL, PA_CONTEXT_NOFAIL, NULL); |
571 | - |
572 | - if (result < 0) { |
573 | - g_warning ("Failed to connect context: %s", |
574 | - pa_strerror (pa_context_errno (pulse_context))); |
575 | - } |
576 | - // we always want to cancel any continious callbacks with the existing timeout |
577 | - // if the connection failed the new context created above will catch any updates |
578 | - // to do with the state of pulse and thus take care of business. |
579 | - reconnect_idle_id = 0; |
580 | - return FALSE; |
581 | -} |
582 | - |
583 | -/** |
584 | -close_pulse_activites() |
585 | -Gracefully close our connection with the Pulse async library. |
586 | -**/ |
587 | -void close_pulse_activites() |
588 | -{ |
589 | - if (pulse_context != NULL) { |
590 | - /* g_debug("freeing the pulse context");*/ |
591 | - pa_context_unref(pulse_context); |
592 | - pulse_context = NULL; |
593 | - } |
594 | - g_hash_table_destroy(sink_hash); |
595 | - pa_glib_mainloop_free(pa_main_loop); |
596 | - pa_main_loop = NULL; |
597 | - /* g_debug("I just closed communication with Pulse");*/ |
598 | -} |
599 | - |
600 | -/** |
601 | -destroy_sink_info() |
602 | -item destructor method for the sink_info hash |
603 | -**/ |
604 | -static void destroy_sink_info(void *value) |
605 | -{ |
606 | - sink_info *sink = (sink_info*)value; |
607 | - g_free(sink->name); |
608 | - g_free(sink); |
609 | -} |
610 | - |
611 | -/* |
612 | -Controllers & Utilities |
613 | -*/ |
614 | -static gboolean determine_sink_availability() |
615 | -{ |
616 | - // Firstly check to see if we have any sinks |
617 | - // if not get the hell out of here ! |
618 | - if (g_hash_table_size(sink_hash) < 1) { |
619 | - g_debug("Sink_available returning false because sinks_hash is empty !!!"); |
620 | - DEFAULT_SINK_INDEX = -1; |
621 | - return FALSE; |
622 | - } |
623 | - // Secondly, make sure the default sink index is set |
624 | - // If the default sink index has not been set |
625 | - // (via the server or has been reset because default sink has been removed), |
626 | - // it will attempt to set it to the value of the first |
627 | - // index in the array of keys from the sink_hash. |
628 | - GList* keys = g_hash_table_get_keys(sink_hash); |
629 | - GList* key = g_list_first(keys); |
630 | - |
631 | - DEFAULT_SINK_INDEX = (DEFAULT_SINK_INDEX < 0) ? GPOINTER_TO_INT(key->data) : DEFAULT_SINK_INDEX; |
632 | - |
633 | - // Thirdly ensure the default sink index does not have the name "auto_null" |
634 | - sink_info* s = g_hash_table_lookup(sink_hash, GINT_TO_POINTER(DEFAULT_SINK_INDEX)); |
635 | - // Up until now the most robust method to test this is to manually remove the available sink device |
636 | - // kernel module and then reload (rmmod & modprobe). |
637 | - // TODO: Edge case of dynamic loading and unloading of sinks should be handled also. |
638 | - /* g_debug("About to test for to see if the available sink is null - s->name = %s", s->name);*/ |
639 | - gboolean available = g_ascii_strncasecmp("auto_null", s->name, 9) != 0; |
640 | - /* g_debug("PA_Manager -> determine_sink_availability: %i", available);*/ |
641 | - return available; |
642 | -} |
643 | - |
644 | -gboolean default_sink_is_muted() |
645 | -{ |
646 | - if (DEFAULT_SINK_INDEX < 0) |
647 | - return FALSE; |
648 | - if (g_hash_table_size(sink_hash) < 1) |
649 | - return FALSE; |
650 | - sink_info *s = g_hash_table_lookup(sink_hash, GINT_TO_POINTER(DEFAULT_SINK_INDEX)); |
651 | - return s->mute; |
652 | -} |
653 | - |
654 | -static void check_sink_input_while_muted_event(gint sink_index) |
655 | -{ |
656 | - /* g_debug("SINKINPUTWHILEMUTED SIGNAL EVENT TO BE SENT FROM PA MANAGER - check trace for value");*/ |
657 | - |
658 | - if (default_sink_is_muted(sink_index) == TRUE) { |
659 | - sound_service_dbus_update_sound_state(dbus_service, BLOCKED); |
660 | - } |
661 | -// Why do you need to send a false for a blocked event, it times out after 5 secs anyway |
662 | -//} else { |
663 | - // sound_service_dbus_sink_input_while_muted(dbus_service, FALSE); |
664 | - //} |
665 | -} |
666 | - |
667 | -static gdouble get_default_sink_volume() |
668 | -{ |
669 | - if (DEFAULT_SINK_INDEX < 0) |
670 | - return 0; |
671 | - sink_info *s = g_hash_table_lookup(sink_hash, GINT_TO_POINTER(DEFAULT_SINK_INDEX)); |
672 | - pa_volume_t vol = pa_cvolume_avg(&s->volume); |
673 | - gdouble volume_percent = ((gdouble) vol * 100) / PA_VOLUME_NORM; |
674 | - /* g_debug("software volume = %f", volume_percent);*/ |
675 | - return volume_percent; |
676 | -} |
677 | - |
678 | -static void mute_each_sink(gpointer key, gpointer value, gpointer user_data) |
679 | -{ |
680 | - sink_info *info = (sink_info*)value; |
681 | - pa_operation_unref(pa_context_set_sink_mute_by_index(pulse_context, info->index, GPOINTER_TO_INT(user_data), context_success_callback, NULL)); |
682 | - if (GPOINTER_TO_INT(user_data) == 1) { |
683 | - sound_service_dbus_update_sink_mute(dbus_service, TRUE); |
684 | - } else { |
685 | - sound_service_dbus_update_volume(dbus_service, get_default_sink_volume()); |
686 | - } |
687 | -} |
688 | - |
689 | -void toggle_global_mute(gboolean mute_value) |
690 | -{ |
691 | - g_hash_table_foreach(sink_hash, mute_each_sink, GINT_TO_POINTER(mute_value)); |
692 | - /* g_debug("in the pulse manager: toggle global mute value %i", mute_value);*/ |
693 | -} |
694 | - |
695 | - |
696 | -/* |
697 | -Refine the resolution of the slider or binary scale it to achieve a more subtle volume control. |
698 | -Use the base volume stored in the sink struct to calculate actual linear volumes. |
699 | -*/ |
700 | -void set_sink_volume(gdouble percent) |
701 | -{ |
702 | - if (pa_server_available == FALSE) |
703 | - return; |
704 | - /* g_debug("in the pulse manager:set_sink_volume with percent %f", percent);*/ |
705 | - |
706 | - if (DEFAULT_SINK_INDEX < 0) { |
707 | - g_warning("We have no default sink !!! - returning after not attempting to set any volume of any sink"); |
708 | - return; |
709 | - } |
710 | - |
711 | - sink_info *cached_sink = g_hash_table_lookup(sink_hash, GINT_TO_POINTER(DEFAULT_SINK_INDEX)); |
712 | - |
713 | - pa_cvolume new_volume; |
714 | - pa_cvolume_init(&new_volume); |
715 | - new_volume.channels = 1; |
716 | - pa_volume_t new_volume_value = (pa_volume_t) ((percent * PA_VOLUME_NORM) / 100); |
717 | - pa_cvolume_set(&new_volume, 1, new_volume_value); |
718 | - pa_cvolume_set(&cached_sink->volume, cached_sink->channel_map.channels, new_volume_value); |
719 | - pa_operation_unref(pa_context_set_sink_volume_by_index(pulse_context, DEFAULT_SINK_INDEX, &new_volume, NULL, NULL)); |
720 | -} |
721 | - |
722 | - |
723 | -static pa_cvolume construct_mono_volume(const pa_cvolume* vol) |
724 | -{ |
725 | - pa_cvolume new_volume; |
726 | - pa_cvolume_init(&new_volume); |
727 | - new_volume.channels = 1; |
728 | - pa_volume_t max_vol = pa_cvolume_max(vol); |
729 | - pa_cvolume_set(&new_volume, 1, max_vol); |
730 | - return new_volume; |
731 | -} |
732 | - |
733 | -/**********************************************************************************************************************/ |
734 | -// Pulse-Audio asychronous call-backs |
735 | -/**********************************************************************************************************************/ |
736 | - |
737 | -static void gather_pulse_information(pa_context *c, void *userdata) |
738 | -{ |
739 | - pa_operation *operation; |
740 | - if (!(operation = pa_context_get_server_info(c, pulse_server_info_callback, userdata))) { |
741 | - g_warning("pa_context_get_server_info failed"); |
742 | - if (!(operation = pa_context_get_sink_info_list(c, pulse_sink_info_callback, NULL))) { |
743 | - g_warning("pa_context_get_sink_info_list() failed - cannot fetch server or sink info - leaving . . ."); |
744 | - return; |
745 | - } |
746 | - } |
747 | - pa_operation_unref(operation); |
748 | - return; |
749 | -} |
750 | - |
751 | - |
752 | -static void context_success_callback(pa_context *c, int success, void *userdata) |
753 | -{ |
754 | - /* g_debug("Context Success Callback - result = %i", success);*/ |
755 | -} |
756 | - |
757 | -/** |
758 | -On Service startup this callback will be called multiple times resulting our sinks_hash container to be filled with the |
759 | -available sinks. |
760 | -For now this callback assumes it only used at startup. It may be necessary to use if sinks become available after startup. |
761 | -Major candidate for refactoring. |
762 | -**/ |
763 | -static void pulse_sink_info_callback(pa_context *c, const pa_sink_info *sink, int eol, void *userdata) |
764 | -{ |
765 | - if (eol > 0) { |
766 | - |
767 | - gboolean device_available = determine_sink_availability(); |
768 | - if (device_available == TRUE) { |
769 | - sound_service_dbus_update_pa_state( dbus_service, |
770 | - device_available, |
771 | - default_sink_is_muted(), |
772 | - get_default_sink_volume() ); |
773 | - } else { |
774 | - //Update the indicator to show PA either is not ready or has no available sink |
775 | - g_warning("Cannot find a suitable default sink ..."); |
776 | - sound_service_dbus_update_pa_state( dbus_service, |
777 | - device_available, |
778 | - default_sink_is_muted(), |
779 | - get_default_sink_volume() ); |
780 | - } |
781 | - } else { |
782 | - /* g_debug("About to add an item to our hash");*/ |
783 | - sink_info *value; |
784 | - value = g_new0(sink_info, 1); |
785 | - value->index = sink->index; |
786 | - value->name = g_strdup(sink->name); |
787 | - value->mute = !!sink->mute; |
788 | - value->volume = construct_mono_volume(&sink->volume); |
789 | - value->base_volume = sink->base_volume; |
790 | - value->channel_map = sink->channel_map; |
791 | - g_hash_table_insert(sink_hash, GINT_TO_POINTER(sink->index), value); |
792 | - /* g_debug("After adding an item to our hash");*/ |
793 | - } |
794 | -} |
795 | - |
796 | -static void pulse_default_sink_info_callback(pa_context *c, const pa_sink_info *info, int eol, void *userdata) |
797 | -{ |
798 | - if (eol > 0) { |
799 | - return; |
800 | - } else { |
801 | - DEFAULT_SINK_INDEX = info->index; |
802 | - /* g_debug("Just set the default sink index to %i", DEFAULT_SINK_INDEX); */ |
803 | - GList *keys = g_hash_table_get_keys(sink_hash); |
804 | - gint position = g_list_index(keys, GINT_TO_POINTER(info->index)); |
805 | - // Only update sink-list if the index is not in our already fetched list. |
806 | - if (position < 0) { |
807 | - pa_operation_unref(pa_context_get_sink_info_list(c, pulse_sink_info_callback, NULL)); |
808 | - } else { |
809 | - sound_service_dbus_update_pa_state(dbus_service, |
810 | - determine_sink_availability(), |
811 | - default_sink_is_muted(), |
812 | - get_default_sink_volume()); |
813 | - } |
814 | - } |
815 | -} |
816 | - |
817 | -static void pulse_sink_input_info_callback(pa_context *c, const pa_sink_input_info *info, int eol, void *userdata) |
818 | -{ |
819 | - if (eol > 0) { |
820 | - if (pa_context_errno(c) == PA_ERR_NOENTITY) |
821 | - return; |
822 | - /* g_warning("Sink INPUT info callback failure");*/ |
823 | - return; |
824 | - } else { |
825 | - if (info == NULL) { |
826 | - // TODO: watch this carefully - PA async api should not be doing this . . . |
827 | - /* g_warning("\n Sink input info callback : SINK INPUT INFO IS NULL BUT EOL was not POSITIVE!!!");*/ |
828 | - return; |
829 | - } |
830 | - /* g_debug("\n SINK INPUT INFO sink index : %d \n", info->sink);*/ |
831 | - check_sink_input_while_muted_event(info->sink); |
832 | - } |
833 | -} |
834 | - |
835 | -static void update_sink_info(pa_context *c, const pa_sink_info *info, int eol, void *userdata) |
836 | -{ |
837 | - if (eol > 0) { |
838 | - if (pa_context_errno(c) == PA_ERR_NOENTITY) |
839 | - return; |
840 | - /* g_warning("Sink INPUT info callback failure");*/ |
841 | - return; |
842 | - } |
843 | - gint position = -1; |
844 | - GList *keys = g_hash_table_get_keys(sink_hash); |
845 | - |
846 | - if (info == NULL) |
847 | - return; |
848 | - |
849 | - position = g_list_index(keys, GINT_TO_POINTER(info->index)); |
850 | - |
851 | - if (position >= 0) { // => index is within the keys of the hash. |
852 | - sink_info *s = g_hash_table_lookup(sink_hash, GINT_TO_POINTER(info->index)); |
853 | - s->name = g_strdup(info->name); |
854 | - gboolean mute_changed = s->mute != !!info->mute; |
855 | - s->mute = !!info->mute; |
856 | - gboolean volume_changed = has_volume_changed(info, s); |
857 | - |
858 | - /* g_debug("new balance : %i", (int)(pa_cvolume_get_balance(&info->volume, &info->channel_map) * 100));*/ |
859 | - /* g_debug("cached balance : %i", (int)(pa_cvolume_get_balance(&s->volume, &s->channel_map) * 100));*/ |
860 | - /* g_debug("update_sink_info: new_volume input : %f", (gdouble)(pa_cvolume_max(&info->volume)));*/ |
861 | - /* g_debug("update sink info: cached volume is at: %f", (gdouble)(pa_cvolume_max(&s->volume)));*/ |
862 | - /* g_debug("update sink info : volume changed = %i", volume_changed);*/ |
863 | - /* g_debug("update sink info : compatibility = %i", pa_cvolume_compatible_with_channel_map(&info->volume, &s->channel_map));*/ |
864 | - |
865 | - s->volume = construct_mono_volume(&info->volume); |
866 | - s->channel_map = info->channel_map; |
867 | - |
868 | - if (DEFAULT_SINK_INDEX == s->index) { |
869 | - //update the UI |
870 | - if (volume_changed == TRUE && s->mute == FALSE) { |
871 | - pa_volume_t vol = pa_cvolume_max(&s->volume); |
872 | - gdouble volume_percent = ((gdouble) vol * 100) / PA_VOLUME_NORM; |
873 | - /* g_debug("Updating volume from PA manager with volume = %f", volume_percent);*/ |
874 | - sound_service_dbus_update_volume(dbus_service, volume_percent); |
875 | - } |
876 | - |
877 | - if (mute_changed == TRUE) { |
878 | - /* g_debug("Updating Mute from PA manager with mute = %i", s->mute);*/ |
879 | - sound_service_dbus_update_sink_mute(dbus_service, s->mute); |
880 | - if (s->mute == FALSE) { |
881 | - pa_volume_t vol = pa_cvolume_max(&s->volume); |
882 | - gdouble volume_percent = ((gdouble) vol * 100) / PA_VOLUME_NORM; |
883 | - /* g_debug("Updating volume from PA manager with volume = %f", volume_percent);*/ |
884 | - sound_service_dbus_update_volume(dbus_service, volume_percent); |
885 | - } |
886 | - } |
887 | - } |
888 | - } else { |
889 | - sink_info *value; |
890 | - value = g_new0(sink_info, 1); |
891 | - value->index = info->index; |
892 | - value->name = g_strdup(info->name); |
893 | - value->mute = !!info->mute; |
894 | - value->volume = construct_mono_volume(&info->volume); |
895 | - value->channel_map = info->channel_map; |
896 | - value->base_volume = info->base_volume; |
897 | - g_hash_table_insert(sink_hash, GINT_TO_POINTER(value->index), value); |
898 | - /* g_debug("pulse-manager:update_sink_info -> After adding a new sink to our hash");*/ |
899 | - sound_service_dbus_update_sound_state(dbus_service, AVAILABLE); |
900 | - } |
901 | -} |
902 | - |
903 | - |
904 | -static gboolean has_volume_changed(const pa_sink_info* new_sink, sink_info* cached_sink) |
905 | -{ |
906 | - if (pa_cvolume_compatible_with_channel_map(&new_sink->volume, &cached_sink->channel_map) == FALSE) |
907 | - return FALSE; |
908 | - |
909 | - pa_cvolume new_vol = construct_mono_volume(&new_sink->volume); |
910 | - |
911 | - if (pa_cvolume_equal(&new_vol, &(cached_sink->volume)) == TRUE) { |
912 | - /* g_debug("has_volume_changed: volumes appear to be equal? no change triggered!"); */ |
913 | - return FALSE; |
914 | - } |
915 | - |
916 | - return TRUE; |
917 | -} |
918 | - |
919 | - |
920 | -static void pulse_server_info_callback(pa_context *c, |
921 | - const pa_server_info *info, |
922 | - void *userdata) |
923 | -{ |
924 | - /* g_debug("server info callback");*/ |
925 | - pa_operation *operation; |
926 | - if (info == NULL) { |
927 | - g_warning("No server - get the hell out of here"); |
928 | - sound_service_dbus_update_pa_state(dbus_service, FALSE, TRUE, 0); |
929 | - pa_server_available = FALSE; |
930 | - return; |
931 | - } |
932 | - pa_server_available = TRUE; |
933 | - if (info->default_sink_name != NULL) { |
934 | - if (!(operation = pa_context_get_sink_info_by_name(c, |
935 | - info->default_sink_name, |
936 | - pulse_default_sink_info_callback, |
937 | - userdata))) { |
938 | - g_warning("pa_context_get_sink_info_by_name() failed"); |
939 | - } else { |
940 | - pa_operation_unref(operation); |
941 | - return; |
942 | - } |
943 | - } |
944 | - if (!(operation = pa_context_get_sink_info_list(c, pulse_sink_info_callback, NULL))) { |
945 | - g_warning("pa_context_get_sink_info_list() failed"); |
946 | - return; |
947 | - } |
948 | - pa_operation_unref(operation); |
949 | -} |
950 | - |
951 | - |
952 | -static void subscribed_events_callback(pa_context *c, enum pa_subscription_event_type t, uint32_t index, void *userdata) |
953 | -{ |
954 | - switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) { |
955 | - case PA_SUBSCRIPTION_EVENT_SINK: |
956 | - if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) { |
957 | - if (index == DEFAULT_SINK_INDEX) |
958 | - sound_service_dbus_update_sound_state(dbus_service, UNAVAILABLE); |
959 | - |
960 | - /* g_debug("Subscribed_events_callback - removing sink of index %i from our sink hash - keep the cache tidy !", index);*/ |
961 | - g_hash_table_remove(sink_hash, GINT_TO_POINTER(index)); |
962 | - |
963 | - if (index == DEFAULT_SINK_INDEX) { |
964 | - /* g_debug("subscribed_events_callback - PA_SUBSCRIPTION_EVENT_SINK REMOVAL: default sink %i has been removed.", DEFAULT_SINK_INDEX); */ |
965 | - DEFAULT_SINK_INDEX = -1; |
966 | - determine_sink_availability(); |
967 | - } |
968 | - /* g_debug("subscribed_events_callback - Now what is our default sink : %i", DEFAULT_SINK_INDEX); */ |
969 | - } else { |
970 | - /* g_debug("subscribed_events_callback - PA_SUBSCRIPTION_EVENT_SINK: a generic sink event - will trigger an update"); */ |
971 | - pa_operation_unref(pa_context_get_sink_info_by_index(c, index, update_sink_info, userdata)); |
972 | - } |
973 | - break; |
974 | - case PA_SUBSCRIPTION_EVENT_SINK_INPUT: |
975 | - /* g_debug("subscribed_events_callback - PA_SUBSCRIPTION_EVENT_SINK_INPUT event triggered!!");*/ |
976 | - if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) { |
977 | - //handle the sink input remove event - not relevant for current design |
978 | - } else { |
979 | - pa_operation_unref(pa_context_get_sink_input_info(c, index, pulse_sink_input_info_callback, userdata)); |
980 | - } |
981 | - break; |
982 | - case PA_SUBSCRIPTION_EVENT_SERVER: |
983 | - g_debug("subscribed_events_callback - PA_SUBSCRIPTION_EVENT_SERVER change of some description ???"); |
984 | - pa_operation *o; |
985 | - if (!(o = pa_context_get_server_info(c, pulse_server_info_callback, userdata))) { |
986 | - g_warning("subscribed_events_callback - pa_context_get_server_info() failed"); |
987 | - return; |
988 | - } |
989 | - pa_operation_unref(o); |
990 | - break; |
991 | - } |
992 | -} |
993 | - |
994 | - |
995 | -static void context_state_callback(pa_context *c, void *userdata) |
996 | -{ |
997 | - switch (pa_context_get_state(c)) { |
998 | - case PA_CONTEXT_UNCONNECTED: |
999 | - g_debug("unconnected"); |
1000 | - break; |
1001 | - case PA_CONTEXT_CONNECTING: |
1002 | - g_debug("connecting - waiting for the server to become available"); |
1003 | - break; |
1004 | - case PA_CONTEXT_AUTHORIZING: |
1005 | - /* g_debug("authorizing");*/ |
1006 | - break; |
1007 | - case PA_CONTEXT_SETTING_NAME: |
1008 | - /* g_debug("context setting name");*/ |
1009 | - break; |
1010 | - case PA_CONTEXT_FAILED: |
1011 | - g_warning("PA_CONTEXT_FAILED - Is PulseAudio Daemon running ?"); |
1012 | - pa_server_available = FALSE; |
1013 | - sound_service_dbus_update_pa_state( dbus_service, |
1014 | - pa_server_available, |
1015 | - default_sink_is_muted(), |
1016 | - get_default_sink_volume() ); |
1017 | - |
1018 | - if (reconnect_idle_id == 0){ |
1019 | - reconnect_idle_id = g_timeout_add_seconds (RECONNECT_DELAY, |
1020 | - reconnect_to_pulse, |
1021 | - NULL); |
1022 | - } |
1023 | - break; |
1024 | - case PA_CONTEXT_TERMINATED: |
1025 | - /* g_debug("context terminated");*/ |
1026 | - break; |
1027 | - case PA_CONTEXT_READY: |
1028 | - g_debug("PA_CONTEXT_READY"); |
1029 | - pa_operation *o; |
1030 | - |
1031 | - pa_context_set_subscribe_callback(c, subscribed_events_callback, userdata); |
1032 | - |
1033 | - if (!(o = pa_context_subscribe(c, (pa_subscription_mask_t) |
1034 | - (PA_SUBSCRIPTION_MASK_SINK| |
1035 | - PA_SUBSCRIPTION_MASK_SINK_INPUT| |
1036 | - PA_SUBSCRIPTION_MASK_SERVER), NULL, NULL))) { |
1037 | - g_warning("pa_context_subscribe() failed"); |
1038 | - return; |
1039 | - } |
1040 | - pa_operation_unref(o); |
1041 | - |
1042 | - gather_pulse_information(c, userdata); |
1043 | - |
1044 | - break; |
1045 | - } |
1046 | -} |
1047 | - |
1048 | |
1049 | === removed file 'src/pulse-manager.h' |
1050 | --- src/pulse-manager.h 2011-01-27 22:10:48 +0000 |
1051 | +++ src/pulse-manager.h 1970-01-01 00:00:00 +0000 |
1052 | @@ -1,45 +0,0 @@ |
1053 | -#ifndef __INCLUDE_PULSE_MANAGER_H__ |
1054 | -#define __INCLUDE_PULSE_MANAGER_H__ |
1055 | -/* |
1056 | -Copyright 2010 Canonical Ltd. |
1057 | - |
1058 | -Authors: |
1059 | - Conor Curran <conor.curran@canonical.com> |
1060 | - |
1061 | -This program is free software: you can redistribute it and/or modify it |
1062 | -under the terms of the GNU General Public License version 3, as published |
1063 | -by the Free Software Foundation. |
1064 | - |
1065 | -This program is distributed in the hope that it will be useful, but |
1066 | -WITHOUT ANY WARRANTY; without even the implied warranties of |
1067 | -MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
1068 | -PURPOSE. See the GNU General Public License for more details. |
1069 | - |
1070 | -You should have received a copy of the GNU General Public License along |
1071 | -with this program. If not, see <http://www.gnu.org/licenses/>. |
1072 | -*/ |
1073 | - |
1074 | - |
1075 | -#include <pulse/pulseaudio.h> |
1076 | -#include <glib.h> |
1077 | -#include "sound-service-dbus.h" |
1078 | - |
1079 | - |
1080 | -typedef struct { |
1081 | - gchar* name; |
1082 | - gint index; |
1083 | - pa_cvolume volume; |
1084 | - pa_channel_map channel_map; |
1085 | - gboolean mute; |
1086 | - pa_volume_t base_volume; |
1087 | -} sink_info; |
1088 | - |
1089 | - |
1090 | -pa_context* get_context(void); |
1091 | -void establish_pulse_activities(SoundServiceDbus *service); |
1092 | -void set_sink_volume(gdouble percent); |
1093 | -void toggle_global_mute(gboolean mute_value); |
1094 | -void close_pulse_activites(); |
1095 | -gboolean default_sink_is_muted(); |
1096 | -#endif |
1097 | - |
1098 | |
1099 | === added file 'src/pulseaudio-mgr.c' |
1100 | --- src/pulseaudio-mgr.c 1970-01-01 00:00:00 +0000 |
1101 | +++ src/pulseaudio-mgr.c 2011-02-07 18:12:56 +0000 |
1102 | @@ -0,0 +1,404 @@ |
1103 | +/* |
1104 | +Copyright 2011 Canonical Ltd. |
1105 | + |
1106 | +Authors: |
1107 | + Conor Curran <conor.curran@canonical.com> |
1108 | + |
1109 | +This program is free software: you can redistribute it and/or modify it |
1110 | +under the terms of the GNU General Public License version 3, as published |
1111 | +by the Free Software Foundation. |
1112 | + |
1113 | +This program is distributed in the hope that it will be useful, but |
1114 | +WITHOUT ANY WARRANTY; without even the implied warranties of |
1115 | +MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
1116 | +PURPOSE. See the GNU General Public License for more details. |
1117 | + |
1118 | +You should have received a copy of the GNU General Public License along |
1119 | +with this program. If not, see <http://www.gnu.org/licenses/>. |
1120 | +*/ |
1121 | + |
1122 | +/**Notes |
1123 | + * |
1124 | + * Approach now is to set up the communication channels then query the server |
1125 | + * fetch its default sink. If this fails then fetch the list of sinks and take |
1126 | + * the first one which is not the auto-null sink. |
1127 | + * TODO: need to handle the situation where one chink in this linear chain breaks |
1128 | + * i.e. start off the process again and count the attempts (note different to |
1129 | + reconnect attempts) |
1130 | + */ |
1131 | +#include <pulse/gccmacro.h> |
1132 | +#include <pulse/glib-mainloop.h> |
1133 | +#include <pulse/error.h> |
1134 | + |
1135 | +#include "pulseaudio-mgr.h" |
1136 | + |
1137 | +#define RECONNECT_DELAY 5 |
1138 | + |
1139 | + |
1140 | +static void pm_context_state_callback(pa_context *c, void *userdata); |
1141 | +static void pm_subscribed_events_callback (pa_context *c, |
1142 | + enum pa_subscription_event_type t, |
1143 | + uint32_t index, |
1144 | + void* userdata); |
1145 | +static void pm_server_info_callback (pa_context *c, |
1146 | + const pa_server_info *info, |
1147 | + void *userdata); |
1148 | +static void pm_default_sink_info_callback (pa_context *c, |
1149 | + const pa_sink_info *info, |
1150 | + int eol, |
1151 | + void *userdata); |
1152 | +static void pm_sink_info_callback (pa_context *c, |
1153 | + const pa_sink_info *sink, |
1154 | + int eol, |
1155 | + void *userdata); |
1156 | +static void pm_sink_input_info_callback (pa_context *c, |
1157 | + const pa_sink_input_info *info, |
1158 | + int eol, |
1159 | + void *userdata); |
1160 | +static void pm_update_active_sink (pa_context *c, |
1161 | + const pa_sink_info *info, |
1162 | + int eol, |
1163 | + void *userdata); |
1164 | +static void pm_toggle_mute_for_every_sink_callback (pa_context *c, |
1165 | + const pa_sink_info *sink, |
1166 | + int eol, |
1167 | + void* userdata); |
1168 | + |
1169 | +static gboolean reconnect_to_pulse (gpointer user_data); |
1170 | + |
1171 | +static gint connection_attempts = 0; |
1172 | +static gint reconnect_idle_id = 0; |
1173 | +static pa_context *pulse_context = NULL; |
1174 | +static pa_glib_mainloop *pa_main_loop = NULL; |
1175 | + |
1176 | +/** |
1177 | + Entry Point |
1178 | + **/ |
1179 | +void |
1180 | +pm_establish_pulse_connection (ActiveSink* active_sink) |
1181 | +{ |
1182 | + pa_main_loop = pa_glib_mainloop_new (g_main_context_default ()); |
1183 | + g_assert (pa_main_loop); |
1184 | + reconnect_to_pulse ((gpointer)active_sink); |
1185 | +} |
1186 | + |
1187 | +/** |
1188 | +close_pulse_activites() |
1189 | +Gracefully close our connection with the Pulse async library. |
1190 | +**/ |
1191 | +void close_pulse_activites() |
1192 | +{ |
1193 | + if (pulse_context != NULL) { |
1194 | + pa_context_unref(pulse_context); |
1195 | + pulse_context = NULL; |
1196 | + } |
1197 | + pa_glib_mainloop_free(pa_main_loop); |
1198 | + pa_main_loop = NULL; |
1199 | +} |
1200 | + |
1201 | +/** |
1202 | +reconnect_to_pulse (gpointer user_data) |
1203 | +Method which connects to the pulse server and is used to track reconnects. |
1204 | + */ |
1205 | +static gboolean |
1206 | +reconnect_to_pulse (gpointer user_data) |
1207 | +{ |
1208 | + g_debug("Attempt to reconnect to pulse"); |
1209 | + // reset |
1210 | + connection_attempts += 1; |
1211 | + if (pulse_context != NULL) { |
1212 | + pa_context_unref(pulse_context); |
1213 | + pulse_context = NULL; |
1214 | + } |
1215 | + |
1216 | + pulse_context = pa_context_new( pa_glib_mainloop_get_api( pa_main_loop ), |
1217 | + "com.canonical.indicators.sound" ); |
1218 | + g_assert(pulse_context); |
1219 | + pa_context_set_state_callback (pulse_context, |
1220 | + pm_context_state_callback, |
1221 | + user_data); |
1222 | + int result = pa_context_connect (pulse_context, |
1223 | + NULL, |
1224 | + PA_CONTEXT_NOFAIL, |
1225 | + user_data); |
1226 | + |
1227 | + if (result < 0) { |
1228 | + g_warning ("Failed to connect context: %s", |
1229 | + pa_strerror (pa_context_errno (pulse_context))); |
1230 | + } |
1231 | + |
1232 | + reconnect_idle_id = 0; |
1233 | + if (connection_attempts > 5){ |
1234 | + return FALSE; |
1235 | + } |
1236 | + else{ |
1237 | + return TRUE; |
1238 | + } |
1239 | +} |
1240 | + |
1241 | +void |
1242 | +pm_update_volume (gint sink_index, pa_cvolume new_volume) |
1243 | +{ |
1244 | + pa_operation_unref (pa_context_set_sink_volume_by_index (pulse_context, |
1245 | + sink_index, |
1246 | + &new_volume, |
1247 | + NULL, |
1248 | + NULL) ); |
1249 | +} |
1250 | + |
1251 | +void |
1252 | +pm_update_mute (gboolean update) |
1253 | +{ |
1254 | + pa_operation_unref (pa_context_get_sink_info_list (pulse_context, |
1255 | + pm_toggle_mute_for_every_sink_callback, |
1256 | + GINT_TO_POINTER (update))); |
1257 | +} |
1258 | + |
1259 | +/**********************************************************************************************************************/ |
1260 | +// Pulse-Audio asychronous call-backs |
1261 | +/**********************************************************************************************************************/ |
1262 | + |
1263 | + |
1264 | +static void |
1265 | +pm_subscribed_events_callback (pa_context *c, |
1266 | + enum pa_subscription_event_type t, |
1267 | + uint32_t index, |
1268 | + void* userdata) |
1269 | +{ |
1270 | + switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) { |
1271 | + case PA_SUBSCRIPTION_EVENT_SINK: |
1272 | + if (IS_ACTIVE_SINK (userdata) == FALSE){ |
1273 | + g_warning ("subscribed events callback - our userdata is not what we think it should be"); |
1274 | + return; |
1275 | + } |
1276 | + ActiveSink* sink = ACTIVE_SINK (userdata); |
1277 | + // We don't care about any other sink other than the active one. |
1278 | + if (index != active_sink_get_index (sink)) |
1279 | + return; |
1280 | + |
1281 | + if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) { |
1282 | + active_sink_deactivate (ACTIVE_SINK (userdata)); |
1283 | + |
1284 | + } |
1285 | + else{ |
1286 | + pa_operation_unref (pa_context_get_sink_info_by_index (c, |
1287 | + index, |
1288 | + pm_update_active_sink, |
1289 | + userdata) ); |
1290 | + } |
1291 | + break; |
1292 | + case PA_SUBSCRIPTION_EVENT_SINK_INPUT: |
1293 | + // We don't care about sink input removals. |
1294 | + if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_REMOVE) { |
1295 | + pa_operation_unref (pa_context_get_sink_input_info (c, |
1296 | + index, |
1297 | + pm_sink_input_info_callback, userdata)); |
1298 | + } |
1299 | + break; |
1300 | + case PA_SUBSCRIPTION_EVENT_SERVER: |
1301 | + g_debug("PA_SUBSCRIPTION_EVENT_SERVER event triggered."); |
1302 | + pa_operation *o; |
1303 | + if (!(o = pa_context_get_server_info (c, pm_server_info_callback, userdata))) { |
1304 | + g_warning("subscribed_events_callback - pa_context_get_server_info() failed"); |
1305 | + return; |
1306 | + } |
1307 | + pa_operation_unref(o); |
1308 | + break; |
1309 | + } |
1310 | +} |
1311 | + |
1312 | + |
1313 | +static void |
1314 | +pm_context_state_callback (pa_context *c, void *userdata) |
1315 | +{ |
1316 | + switch (pa_context_get_state(c)) { |
1317 | + case PA_CONTEXT_UNCONNECTED: |
1318 | + g_debug("unconnected"); |
1319 | + break; |
1320 | + case PA_CONTEXT_CONNECTING: |
1321 | + g_debug("connecting - waiting for the server to become available"); |
1322 | + break; |
1323 | + case PA_CONTEXT_AUTHORIZING: |
1324 | + break; |
1325 | + case PA_CONTEXT_SETTING_NAME: |
1326 | + break; |
1327 | + case PA_CONTEXT_FAILED: |
1328 | + g_warning("PA_CONTEXT_FAILED - Is PulseAudio Daemon running ?"); |
1329 | + active_sink_deactivate (ACTIVE_SINK (userdata)); |
1330 | + if (reconnect_idle_id == 0){ |
1331 | + reconnect_idle_id = g_timeout_add_seconds (RECONNECT_DELAY, |
1332 | + reconnect_to_pulse, |
1333 | + userdata); |
1334 | + } |
1335 | + break; |
1336 | + case PA_CONTEXT_TERMINATED: |
1337 | + break; |
1338 | + case PA_CONTEXT_READY: |
1339 | + connection_attempts = 0; |
1340 | + g_debug("PA_CONTEXT_READY"); |
1341 | + pa_operation *o; |
1342 | + |
1343 | + pa_context_set_subscribe_callback(c, pm_subscribed_events_callback, userdata); |
1344 | + |
1345 | + if (!(o = pa_context_subscribe (c, (pa_subscription_mask_t) |
1346 | + (PA_SUBSCRIPTION_MASK_SINK| |
1347 | + PA_SUBSCRIPTION_MASK_SINK_INPUT| |
1348 | + PA_SUBSCRIPTION_MASK_SERVER), NULL, NULL))) { |
1349 | + g_warning("pa_context_subscribe() failed"); |
1350 | + |
1351 | + } |
1352 | + |
1353 | + if (!(o = pa_context_get_server_info (c, pm_server_info_callback, userdata))) { |
1354 | + g_warning("Initial - pa_context_get_server_info() failed"); |
1355 | + } |
1356 | + pa_operation_unref(o); |
1357 | + |
1358 | + break; |
1359 | + } |
1360 | +} |
1361 | + |
1362 | +/** |
1363 | + After startup we go straight for the server info to see if it has details of |
1364 | + the default sink. If so it makes things much easier. |
1365 | + **/ |
1366 | +static void |
1367 | +pm_server_info_callback (pa_context *c, |
1368 | + const pa_server_info *info, |
1369 | + void *userdata) |
1370 | +{ |
1371 | + pa_operation *operation; |
1372 | + g_debug ("server info callback"); |
1373 | + |
1374 | + if (info == NULL) { |
1375 | + g_warning("No PA server - get the hell out of here"); |
1376 | + active_sink_deactivate (ACTIVE_SINK (userdata)); |
1377 | + return; |
1378 | + } |
1379 | + if (info->default_sink_name != NULL) { |
1380 | + g_debug ("default sink name from the server ain't null'"); |
1381 | + if (!(operation = pa_context_get_sink_info_by_name (c, |
1382 | + info->default_sink_name, |
1383 | + pm_default_sink_info_callback, |
1384 | + userdata) )) { |
1385 | + } |
1386 | + else{ |
1387 | + pa_operation_unref(operation); |
1388 | + return; |
1389 | + } |
1390 | + } |
1391 | + else if (!(operation = pa_context_get_sink_info_list(c, |
1392 | + pm_sink_info_callback, |
1393 | + NULL))) { |
1394 | + g_warning("pa_context_get_sink_info_list() failed"); |
1395 | + return; |
1396 | + } |
1397 | + pa_operation_unref(operation); |
1398 | +} |
1399 | + |
1400 | +// If the server doesn't have a default sink to give us |
1401 | +// we should attempt to pick up the first of the list of sinks which doesn't have |
1402 | +// the name 'auto_null' (that was all really I was doing before) |
1403 | +static void |
1404 | +pm_sink_info_callback (pa_context *c, |
1405 | + const pa_sink_info *sink, |
1406 | + int eol, |
1407 | + void* userdata) |
1408 | +{ |
1409 | + if (eol > 0) { |
1410 | + return; |
1411 | + } |
1412 | + else { |
1413 | + if (IS_ACTIVE_SINK (userdata) == FALSE){ |
1414 | + g_warning ("sink info callback - our user data is not what we think it should be"); |
1415 | + return; |
1416 | + } |
1417 | + ActiveSink* a_sink = ACTIVE_SINK (userdata); |
1418 | + if (active_sink_is_populated (a_sink) == FALSE && |
1419 | + g_ascii_strncasecmp("auto_null", sink->name, 9) != 0){ |
1420 | + active_sink_populate (a_sink, sink); |
1421 | + } |
1422 | + } |
1423 | +} |
1424 | + |
1425 | +static void |
1426 | +pm_default_sink_info_callback (pa_context *c, |
1427 | + const pa_sink_info *info, |
1428 | + int eol, |
1429 | + void *userdata) |
1430 | +{ |
1431 | + if (eol > 0) { |
1432 | + return; |
1433 | + } |
1434 | + else { |
1435 | + if (IS_ACTIVE_SINK (userdata) == FALSE){ |
1436 | + g_warning ("Default sink info callback - our user data is not what we think it should be"); |
1437 | + return; |
1438 | + } |
1439 | + g_debug ("server has handed us a default sink"); |
1440 | + active_sink_populate (ACTIVE_SINK (userdata), info); |
1441 | + } |
1442 | +} |
1443 | + |
1444 | +static void |
1445 | +pm_sink_input_info_callback (pa_context *c, |
1446 | + const pa_sink_input_info *info, |
1447 | + int eol, |
1448 | + void *userdata) |
1449 | +{ |
1450 | + if (eol > 0) { |
1451 | + return; |
1452 | + } |
1453 | + else { |
1454 | + if (info == NULL) { |
1455 | + // TODO: watch this carefully - PA async api should not be doing this . . . |
1456 | + g_warning("\n Sink input info callback : SINK INPUT INFO IS NULL BUT EOL was not POSITIVE!!!"); |
1457 | + return; |
1458 | + } |
1459 | + if (IS_ACTIVE_SINK (userdata) == FALSE){ |
1460 | + g_warning ("sink input info callback - our user data is not what we think it should be"); |
1461 | + return; |
1462 | + } |
1463 | + |
1464 | + ActiveSink* a_sink = ACTIVE_SINK (userdata); |
1465 | + if (active_sink_get_index (a_sink) == info->sink){ |
1466 | + active_sink_determine_blocking_state (a_sink); |
1467 | + } |
1468 | + } |
1469 | +} |
1470 | + |
1471 | +static void |
1472 | +pm_update_active_sink (pa_context *c, |
1473 | + const pa_sink_info *info, |
1474 | + int eol, |
1475 | + void *userdata) |
1476 | +{ |
1477 | + if (eol > 0) { |
1478 | + return; |
1479 | + } |
1480 | + else{ |
1481 | + if (IS_ACTIVE_SINK (userdata) == FALSE){ |
1482 | + g_warning ("update_active_sink - our user data is not what we think it should be"); |
1483 | + return; |
1484 | + } |
1485 | + active_sink_update (ACTIVE_SINK(userdata), info); |
1486 | + } |
1487 | +} |
1488 | + |
1489 | +static void |
1490 | +pm_toggle_mute_for_every_sink_callback (pa_context *c, |
1491 | + const pa_sink_info *sink, |
1492 | + int eol, |
1493 | + void* userdata) |
1494 | +{ |
1495 | + if (eol > 0) { |
1496 | + return; |
1497 | + } |
1498 | + else { |
1499 | + pa_operation_unref (pa_context_set_sink_mute_by_index (c, |
1500 | + sink->index, |
1501 | + GPOINTER_TO_INT(userdata), |
1502 | + NULL, |
1503 | + NULL)); |
1504 | + } |
1505 | +} |
1506 | + |
1507 | |
1508 | === added file 'src/pulseaudio-mgr.h' |
1509 | --- src/pulseaudio-mgr.h 1970-01-01 00:00:00 +0000 |
1510 | +++ src/pulseaudio-mgr.h 2011-02-07 18:12:56 +0000 |
1511 | @@ -0,0 +1,31 @@ |
1512 | +/* |
1513 | +Copyright 2011 Canonical Ltd. |
1514 | + |
1515 | +Authors: |
1516 | + Conor Curran <conor.curran@canonical.com> |
1517 | + |
1518 | +This program is free software: you can redistribute it and/or modify it |
1519 | +under the terms of the GNU General Public License version 3, as published |
1520 | +by the Free Software Foundation. |
1521 | + |
1522 | +This program is distributed in the hope that it will be useful, but |
1523 | +WITHOUT ANY WARRANTY; without even the implied warranties of |
1524 | +MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
1525 | +PURPOSE. See the GNU General Public License for more details. |
1526 | + |
1527 | +You should have received a copy of the GNU General Public License along |
1528 | +with this program. If not, see <http://www.gnu.org/licenses/>. |
1529 | +*/ |
1530 | + |
1531 | +#include "active-sink.h" |
1532 | + |
1533 | +void pm_establish_pulse_connection (ActiveSink* active_sink); |
1534 | +void close_pulse_activites(); |
1535 | +void pm_update_volume (gint sink_index, pa_cvolume new_volume); |
1536 | +void pm_update_mute (gboolean update); |
1537 | + |
1538 | + |
1539 | + |
1540 | + |
1541 | + |
1542 | + |
1543 | |
1544 | === modified file 'src/slider-menu-item.c' |
1545 | --- src/slider-menu-item.c 2011-01-27 22:10:48 +0000 |
1546 | +++ src/slider-menu-item.c 2011-02-07 18:12:56 +0000 |
1547 | @@ -22,13 +22,12 @@ |
1548 | |
1549 | #include <glib/gi18n.h> |
1550 | #include "slider-menu-item.h" |
1551 | -#include "pulse-manager.h" |
1552 | #include "common-defs.h" |
1553 | |
1554 | - |
1555 | typedef struct _SliderMenuItemPrivate SliderMenuItemPrivate; |
1556 | |
1557 | struct _SliderMenuItemPrivate { |
1558 | + ActiveSink* a_sink; |
1559 | }; |
1560 | |
1561 | #define SLIDER_MENU_ITEM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SLIDER_MENU_ITEM_TYPE, SliderMenuItemPrivate)) |
1562 | @@ -43,7 +42,8 @@ |
1563 | |
1564 | G_DEFINE_TYPE (SliderMenuItem, slider_menu_item, DBUSMENU_TYPE_MENUITEM); |
1565 | |
1566 | -static void slider_menu_item_class_init (SliderMenuItemClass *klass) |
1567 | +static void |
1568 | +slider_menu_item_class_init (SliderMenuItemClass *klass) |
1569 | { |
1570 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
1571 | |
1572 | @@ -57,13 +57,18 @@ |
1573 | return; |
1574 | } |
1575 | |
1576 | -static void slider_menu_item_init (SliderMenuItem *self) |
1577 | +static void |
1578 | +slider_menu_item_init (SliderMenuItem *self) |
1579 | { |
1580 | g_debug("Building new Slider Menu Item"); |
1581 | + dbusmenu_menuitem_property_set( DBUSMENU_MENUITEM(self), |
1582 | + DBUSMENU_MENUITEM_PROP_TYPE, |
1583 | + DBUSMENU_VOLUME_MENUITEM_TYPE ); |
1584 | return; |
1585 | } |
1586 | |
1587 | -static void slider_menu_item_dispose (GObject *object) |
1588 | +static void |
1589 | +slider_menu_item_dispose (GObject *object) |
1590 | { |
1591 | G_OBJECT_CLASS (slider_menu_item_parent_class)->dispose (object); |
1592 | return; |
1593 | @@ -89,27 +94,26 @@ |
1594 | |
1595 | gboolean volume_input = g_variant_get_double(input); |
1596 | if (value != NULL){ |
1597 | - set_sink_volume(volume_input); |
1598 | - // TODO -when the ACTIVESINK instance exists this will be handled nicely |
1599 | - // PA MANAGER will be refactored first. |
1600 | - if (default_sink_is_muted () == TRUE){ |
1601 | - toggle_global_mute (FALSE); |
1602 | - } |
1603 | + if (IS_SLIDER_MENU_ITEM (mi)) { |
1604 | + SliderMenuItemPrivate* priv = SLIDER_MENU_ITEM_GET_PRIVATE (SLIDER_MENU_ITEM (mi)); |
1605 | + active_sink_update_volume (priv->a_sink, volume_input); |
1606 | + active_sink_ensure_sink_is_unmuted (priv->a_sink); |
1607 | + } |
1608 | } |
1609 | } |
1610 | |
1611 | -void slider_menu_item_update (SliderMenuItem* item, |
1612 | +void |
1613 | +slider_menu_item_update (SliderMenuItem* item, |
1614 | gdouble update) |
1615 | { |
1616 | - // TODO |
1617 | - // Check if that variant below will leak !!! |
1618 | GVariant* new_volume = g_variant_new_double(update); |
1619 | dbusmenu_menuitem_property_set_variant(DBUSMENU_MENUITEM(item), |
1620 | DBUSMENU_VOLUME_MENUITEM_LEVEL, |
1621 | new_volume); |
1622 | } |
1623 | |
1624 | -void slider_menu_item_enable (SliderMenuItem* item, |
1625 | +void |
1626 | +slider_menu_item_enable (SliderMenuItem* item, |
1627 | gboolean active) |
1628 | { |
1629 | dbusmenu_menuitem_property_set_bool( DBUSMENU_MENUITEM(item), |
1630 | @@ -117,15 +121,11 @@ |
1631 | active ); |
1632 | } |
1633 | |
1634 | -SliderMenuItem* slider_menu_item_new (gboolean sinks_available, |
1635 | - gdouble start_volume) |
1636 | +SliderMenuItem* |
1637 | +slider_menu_item_new (ActiveSink* sink) |
1638 | { |
1639 | SliderMenuItem *self = g_object_new(SLIDER_MENU_ITEM_TYPE, NULL); |
1640 | - dbusmenu_menuitem_property_set( DBUSMENU_MENUITEM(self), |
1641 | - DBUSMENU_MENUITEM_PROP_TYPE, |
1642 | - DBUSMENU_VOLUME_MENUITEM_TYPE ); |
1643 | - slider_menu_item_update (self, start_volume); |
1644 | - slider_menu_item_enable (self, sinks_available); |
1645 | - |
1646 | + SliderMenuItemPrivate* priv = SLIDER_MENU_ITEM_GET_PRIVATE (self); |
1647 | + priv->a_sink = sink; |
1648 | return self; |
1649 | } |
1650 | \ No newline at end of file |
1651 | |
1652 | === modified file 'src/slider-menu-item.h' |
1653 | --- src/slider-menu-item.h 2011-01-12 23:55:05 +0000 |
1654 | +++ src/slider-menu-item.h 2011-02-07 18:12:56 +0000 |
1655 | @@ -23,6 +23,7 @@ |
1656 | #include <glib-object.h> |
1657 | |
1658 | #include <libdbusmenu-glib/menuitem.h> |
1659 | +#include "active-sink.h" |
1660 | |
1661 | G_BEGIN_DECLS |
1662 | |
1663 | @@ -49,8 +50,7 @@ |
1664 | void slider_menu_item_update(SliderMenuItem* item, gdouble update); |
1665 | void slider_menu_item_enable(SliderMenuItem* item, gboolean active); |
1666 | |
1667 | -SliderMenuItem* slider_menu_item_new (gboolean sinks_available, |
1668 | - gdouble current_vol); |
1669 | +SliderMenuItem* slider_menu_item_new (ActiveSink* sink); |
1670 | |
1671 | G_END_DECLS |
1672 | |
1673 | |
1674 | === modified file 'src/sound-service-dbus.c' |
1675 | --- src/sound-service-dbus.c 2011-02-01 20:40:51 +0000 |
1676 | +++ src/sound-service-dbus.c 2011-02-07 18:12:56 +0000 |
1677 | @@ -29,13 +29,9 @@ |
1678 | #include <libdbusmenu-glib/client.h> |
1679 | |
1680 | #include "sound-service-dbus.h" |
1681 | - |
1682 | +#include "active-sink.h" |
1683 | #include "gen-sound-service.xml.h" |
1684 | #include "dbus-shared-names.h" |
1685 | -#include "pulse-manager.h" |
1686 | -#include "slider-menu-item.h" |
1687 | -#include "mute-menu-item.h" |
1688 | -#include "pulse-manager.h" |
1689 | |
1690 | // DBUS methods |
1691 | static void bus_method_call (GDBusConnection * connection, |
1692 | @@ -57,16 +53,14 @@ |
1693 | typedef struct _SoundServiceDbusPrivate SoundServiceDbusPrivate; |
1694 | |
1695 | struct _SoundServiceDbusPrivate { |
1696 | - GDBusConnection* connection; |
1697 | - DbusmenuMenuitem* root_menuitem; |
1698 | - SliderMenuItem* volume_slider_menuitem; |
1699 | - MuteMenuItem* mute_menuitem; |
1700 | - SoundState current_sound_state; |
1701 | + GDBusConnection* connection; |
1702 | + DbusmenuMenuitem* root_menuitem; |
1703 | + ActiveSink* active_sink; |
1704 | }; |
1705 | |
1706 | static GDBusNodeInfo * node_info = NULL; |
1707 | static GDBusInterfaceInfo * interface_info = NULL; |
1708 | -static gboolean b_startup = TRUE; |
1709 | + |
1710 | #define SOUND_SERVICE_DBUS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUND_SERVICE_DBUS_TYPE, SoundServiceDbusPrivate)) |
1711 | |
1712 | static void sound_service_dbus_class_init (SoundServiceDbusClass *klass); |
1713 | @@ -74,17 +68,8 @@ |
1714 | static void sound_service_dbus_dispose (GObject *object); |
1715 | static void sound_service_dbus_finalize (GObject *object); |
1716 | |
1717 | -static void sound_service_dbus_build_sound_menu ( SoundServiceDbus* root, |
1718 | - gboolean mute_update, |
1719 | - gboolean availability, |
1720 | - gdouble volume ); |
1721 | static void show_sound_settings_dialog (DbusmenuMenuitem *mi, |
1722 | gpointer user_data); |
1723 | -static SoundState sound_service_dbus_get_state_from_volume (SoundServiceDbus* self); |
1724 | -static void sound_service_dbus_determine_state (SoundServiceDbus* self, |
1725 | - gboolean availability, |
1726 | - gboolean mute, |
1727 | - gdouble volume); |
1728 | static gboolean sound_service_dbus_blacklist_player (SoundServiceDbus* self, |
1729 | gchar* player_name, |
1730 | gboolean blacklist); |
1731 | @@ -133,8 +118,6 @@ |
1732 | |
1733 | priv->connection = NULL; |
1734 | |
1735 | - priv->current_sound_state = UNAVAILABLE; |
1736 | - |
1737 | /* Fetch the session bus */ |
1738 | priv->connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error); |
1739 | |
1740 | @@ -167,32 +150,26 @@ |
1741 | DbusmenuServer *server = dbusmenu_server_new(INDICATOR_SOUND_MENU_DBUS_OBJECT_PATH); |
1742 | dbusmenu_server_set_root (server, priv->root_menuitem); |
1743 | g_object_unref (priv->root_menuitem); |
1744 | - establish_pulse_activities (self); |
1745 | + priv->active_sink = active_sink_new (self); |
1746 | return priv->root_menuitem; |
1747 | } |
1748 | |
1749 | -static void |
1750 | +void |
1751 | sound_service_dbus_build_sound_menu ( SoundServiceDbus* self, |
1752 | - gboolean mute_update, |
1753 | - gboolean availability, |
1754 | - gdouble volume ) |
1755 | + DbusmenuMenuitem* mute_item, |
1756 | + DbusmenuMenuitem* slider_item) |
1757 | { |
1758 | SoundServiceDbusPrivate * priv = SOUND_SERVICE_DBUS_GET_PRIVATE(self); |
1759 | |
1760 | // Mute button |
1761 | - priv->mute_menuitem = mute_menu_item_new ( mute_update, availability); |
1762 | - dbusmenu_menuitem_child_append (priv->root_menuitem, |
1763 | - mute_menu_item_get_button (priv->mute_menuitem)); |
1764 | - |
1765 | - // Slider |
1766 | - priv->volume_slider_menuitem = slider_menu_item_new ( availability, volume ); |
1767 | - dbusmenu_menuitem_child_append (priv->root_menuitem, DBUSMENU_MENUITEM ( priv->volume_slider_menuitem )); |
1768 | + dbusmenu_menuitem_child_append (priv->root_menuitem, mute_item); |
1769 | + g_debug ("just about to add the slider %i", DBUSMENU_IS_MENUITEM(slider_item)); |
1770 | + dbusmenu_menuitem_child_append (priv->root_menuitem, slider_item); |
1771 | |
1772 | // Separator |
1773 | - |
1774 | DbusmenuMenuitem* separator = dbusmenu_menuitem_new(); |
1775 | |
1776 | - dbusmenu_menuitem_property_set( separator, |
1777 | + dbusmenu_menuitem_property_set (separator, |
1778 | DBUSMENU_MENUITEM_PROP_TYPE, |
1779 | DBUSMENU_CLIENT_TYPES_SEPARATOR); |
1780 | dbusmenu_menuitem_child_append(priv->root_menuitem, separator); |
1781 | @@ -207,9 +184,7 @@ |
1782 | dbusmenu_menuitem_child_append(priv->root_menuitem, settings_mi); |
1783 | g_object_unref (settings_mi); |
1784 | g_signal_connect(G_OBJECT(settings_mi), DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED, |
1785 | - G_CALLBACK(show_sound_settings_dialog), NULL); |
1786 | - |
1787 | - sound_service_dbus_determine_state (self, availability, mute_update, volume); |
1788 | + G_CALLBACK(show_sound_settings_dialog), NULL); |
1789 | } |
1790 | |
1791 | /** |
1792 | @@ -229,41 +204,11 @@ |
1793 | } |
1794 | } |
1795 | |
1796 | -void |
1797 | -sound_service_dbus_update_pa_state ( SoundServiceDbus* self, |
1798 | - gboolean availability, |
1799 | - gboolean mute_update, |
1800 | - gdouble volume ) |
1801 | -{ |
1802 | - g_debug("update pa state with availability of %i, mute value of %i and a volume percent is %f", availability, mute_update, volume); |
1803 | - SoundServiceDbusPrivate * priv = SOUND_SERVICE_DBUS_GET_PRIVATE(self); |
1804 | - |
1805 | - if (b_startup == TRUE) { |
1806 | - sound_service_dbus_build_sound_menu ( self, |
1807 | - mute_update, |
1808 | - availability, |
1809 | - volume ); |
1810 | - b_startup = FALSE; |
1811 | - return; |
1812 | - } |
1813 | - |
1814 | - mute_menu_item_update ( priv->mute_menuitem, |
1815 | - mute_update ); |
1816 | - slider_menu_item_update ( priv->volume_slider_menuitem, |
1817 | - volume ); |
1818 | - |
1819 | - mute_menu_item_enable ( priv->mute_menuitem, availability); |
1820 | - slider_menu_item_enable ( priv->volume_slider_menuitem, |
1821 | - availability ); |
1822 | - sound_service_dbus_determine_state (self, availability, mute_update, volume); |
1823 | - |
1824 | -} |
1825 | - |
1826 | - |
1827 | static void |
1828 | sound_service_dbus_dispose (GObject *object) |
1829 | { |
1830 | G_OBJECT_CLASS (sound_service_dbus_parent_class)->dispose (object); |
1831 | + //TODO dispose of the active sink instance ! |
1832 | return; |
1833 | } |
1834 | |
1835 | @@ -274,101 +219,19 @@ |
1836 | return; |
1837 | } |
1838 | |
1839 | -// UNTIL PA-MANAGER IS REFACTORED AND THE ACTIVESINK CLASS IS CREATED LEAVE |
1840 | -// THE UI ELEMENTS SEPARATELY HANDLED LIKE THIS. |
1841 | -void |
1842 | -sound_service_dbus_update_volume (SoundServiceDbus* self, |
1843 | - gdouble volume) |
1844 | -{ |
1845 | - SoundServiceDbusPrivate *priv = SOUND_SERVICE_DBUS_GET_PRIVATE (self); |
1846 | - slider_menu_item_update (priv->volume_slider_menuitem, volume); |
1847 | - sound_service_dbus_update_sound_state (self, |
1848 | - sound_service_dbus_get_state_from_volume (self)); |
1849 | -} |
1850 | - |
1851 | -void |
1852 | -sound_service_dbus_update_sink_mute (SoundServiceDbus* self, |
1853 | - gboolean mute_update) |
1854 | -{ |
1855 | - SoundServiceDbusPrivate *priv = SOUND_SERVICE_DBUS_GET_PRIVATE (self); |
1856 | - mute_menu_item_update (priv->mute_menuitem, mute_update); |
1857 | - SoundState state = sound_service_dbus_get_state_from_volume (self); |
1858 | - if (mute_update == TRUE){ |
1859 | - state = MUTED; |
1860 | - } |
1861 | - sound_service_dbus_update_sound_state (self, state); |
1862 | -} |
1863 | - |
1864 | -/*------- State calculators ------------------*/ |
1865 | -static SoundState |
1866 | -sound_service_dbus_get_state_from_volume (SoundServiceDbus* self) |
1867 | -{ |
1868 | - SoundServiceDbusPrivate *priv = SOUND_SERVICE_DBUS_GET_PRIVATE (self); |
1869 | - GVariant* v = dbusmenu_menuitem_property_get_variant (DBUSMENU_MENUITEM(priv->volume_slider_menuitem), |
1870 | - DBUSMENU_VOLUME_MENUITEM_LEVEL); |
1871 | - gdouble volume_percent = g_variant_get_double (v); |
1872 | - |
1873 | - SoundState state = LOW_LEVEL; |
1874 | - |
1875 | - if (volume_percent < 30.0 && volume_percent > 0) { |
1876 | - state = LOW_LEVEL; |
1877 | - } |
1878 | - else if (volume_percent < 70.0 && volume_percent >= 30.0) { |
1879 | - state = MEDIUM_LEVEL; |
1880 | - } |
1881 | - else if (volume_percent >= 70.0) { |
1882 | - state = HIGH_LEVEL; |
1883 | - } |
1884 | - else if (volume_percent == 0.0) { |
1885 | - state = ZERO_LEVEL; |
1886 | - } |
1887 | - return state; |
1888 | -} |
1889 | - |
1890 | -static void |
1891 | -sound_service_dbus_determine_state (SoundServiceDbus* self, |
1892 | - gboolean availability, |
1893 | - gboolean mute, |
1894 | - gdouble volume) |
1895 | -{ |
1896 | - SoundState update; |
1897 | - if (availability == FALSE) { |
1898 | - update = UNAVAILABLE; |
1899 | - } |
1900 | - else if (mute == TRUE) { |
1901 | - update = MUTED; |
1902 | - } |
1903 | - else{ |
1904 | - update = sound_service_dbus_get_state_from_volume (self); |
1905 | - } |
1906 | - sound_service_dbus_update_sound_state (self, update); |
1907 | -} |
1908 | - |
1909 | |
1910 | // EMIT STATE SIGNAL |
1911 | - |
1912 | -// TODO: this will be a bit messy until the pa_manager is sorted. |
1913 | -// And we figure out all of the edge cases. |
1914 | void |
1915 | sound_service_dbus_update_sound_state (SoundServiceDbus* self, |
1916 | SoundState new_state) |
1917 | { |
1918 | SoundServiceDbusPrivate *priv = SOUND_SERVICE_DBUS_GET_PRIVATE (self); |
1919 | - SoundState update = new_state; |
1920 | - // Ensure that after it has become available update the state with the current volume level |
1921 | - if (new_state == AVAILABLE && |
1922 | - mute_menu_item_is_muted (priv->mute_menuitem) == FALSE){ |
1923 | - update = sound_service_dbus_get_state_from_volume (self); |
1924 | - } |
1925 | - if (update != BLOCKED){ |
1926 | - priv->current_sound_state = update; |
1927 | - } |
1928 | |
1929 | - GVariant* v_output = g_variant_new("(i)", (int)update); |
1930 | + GVariant* v_output = g_variant_new("(i)", (int)new_state); |
1931 | |
1932 | GError * error = NULL; |
1933 | |
1934 | - g_debug ("emitting signal with value %i", (int)update); |
1935 | + g_debug ("emitting state signal with value %i", (int)new_state); |
1936 | g_dbus_connection_emit_signal( priv->connection, |
1937 | NULL, |
1938 | INDICATOR_SOUND_SERVICE_DBUS_OBJECT_PATH, |
1939 | @@ -384,7 +247,6 @@ |
1940 | } |
1941 | |
1942 | //HANDLE DBUS METHOD CALLS |
1943 | -// TODO we will need to implement the black_list method. |
1944 | static void |
1945 | bus_method_call (GDBusConnection * connection, |
1946 | const gchar * sender, |
1947 | @@ -401,8 +263,8 @@ |
1948 | SoundServiceDbusPrivate *priv = SOUND_SERVICE_DBUS_GET_PRIVATE (service); |
1949 | |
1950 | if (g_strcmp0(method, "GetSoundState") == 0) { |
1951 | - g_debug("Get state - %i", priv->current_sound_state ); |
1952 | - retval = g_variant_new ( "(i)", priv->current_sound_state); |
1953 | + g_debug("Get state - %i", active_sink_get_state (priv->active_sink)); |
1954 | + retval = g_variant_new ( "(i)", active_sink_get_state (priv->active_sink)); |
1955 | } |
1956 | else if (g_strcmp0(method, "BlacklistMediaPlayer") == 0) { |
1957 | gboolean blacklist; |
1958 | @@ -422,6 +284,9 @@ |
1959 | g_dbus_method_invocation_return_value (invocation, retval); |
1960 | } |
1961 | |
1962 | +/** |
1963 | + TODO - Works nicely but refactor into at least two different methods |
1964 | +**/ |
1965 | static gboolean sound_service_dbus_blacklist_player (SoundServiceDbus* self, |
1966 | gchar* player_name, |
1967 | gboolean blacklist) |
1968 | |
1969 | === modified file 'src/sound-service-dbus.h' |
1970 | --- src/sound-service-dbus.h 2011-01-23 05:37:45 +0000 |
1971 | +++ src/sound-service-dbus.h 2011-02-07 18:12:56 +0000 |
1972 | @@ -55,12 +55,10 @@ |
1973 | |
1974 | DbusmenuMenuitem* sound_service_dbus_create_root_item (SoundServiceDbus* self); |
1975 | void sound_service_dbus_update_sound_state (SoundServiceDbus* self, SoundState new_state); |
1976 | -void sound_service_dbus_update_sink_mute(SoundServiceDbus* self, gboolean sink_mute); |
1977 | -void sound_service_dbus_update_volume(SoundServiceDbus* self, gdouble volume); |
1978 | -void sound_service_dbus_update_pa_state ( SoundServiceDbus* root, |
1979 | - gboolean availability, |
1980 | - gboolean mute_update, |
1981 | - gdouble volume ); |
1982 | +void sound_service_dbus_build_sound_menu ( SoundServiceDbus* self, |
1983 | + DbusmenuMenuitem* mute_item, |
1984 | + DbusmenuMenuitem* slider_item); |
1985 | + |
1986 | |
1987 | G_END_DECLS |
1988 | |
1989 | |
1990 | === modified file 'src/sound-service.c' |
1991 | --- src/sound-service.c 2011-02-02 00:07:58 +0000 |
1992 | +++ src/sound-service.c 2011-02-07 18:12:56 +0000 |
1993 | @@ -18,8 +18,7 @@ |
1994 | */ |
1995 | |
1996 | #include "sound-service.h" |
1997 | - |
1998 | -#include "pulse-manager.h" |
1999 | +#include "pulseaudio-mgr.h" |
2000 | #include "sound-service-dbus.h" |
2001 | #include "music-player-bridge.h" |
2002 |
I think this is a typo:
414 -/*
415 +#/*
It seems like pm_establish_ pulse_connectio n() should call reconnect_ to_pulse( ) as they have very similar code and that way there's just one set of code doing it.
I think as a result of the above if we can't connect the first time we won't reconnect to pulse. I'm not sure though, please check.