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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
|
/*
* Copyright 2013 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Lars Uebernickel <lars.uebernickel@canonical.com>
*/
public class IndicatorSound.Service: Object {
DBusConnection bus;
public Service (MediaPlayerList playerlist, VolumeControl volume, AccountsServiceUser? accounts, Options options, VolumeWarning volume_warning) {
try {
bus = Bus.get_sync(GLib.BusType.SESSION);
} catch (GLib.Error e) {
error("Unable to get DBus session bus: %s", e.message);
}
_options = options;
info_notification = new Notify.Notification(_("Volume"), "", "audio-volume-muted");
_volume_warning = volume_warning;
_volume_warning.notify["active"].connect(() => {
this.increment_volume_sync_action();
this.update_notification();
});
BusWatcher.watch_namespace (GLib.BusType.SESSION,
"org.freedesktop.Notifications",
() => { debug("Notifications name appeared"); },
() => { debug("Notifications name vanshed"); notify_server_caps_checked = false; });
this.settings = new Settings ("com.canonical.indicator.sound");
this.settings.bind ("visible", this, "visible", SettingsBindFlags.GET);
this.notify["visible"].connect ( () => this.update_root_icon () );
this.volume_control = volume;
this.volume_control.notify["active-stream"].connect(() => {
var b = VolumeControl.Stream.MULTIMEDIA == volume_control.active_stream;
message("updating _volume_warning.multimedia_active to %d", (int)b);
_volume_warning.multimedia_active = b;
});
this.volume_control.active_output_changed.connect(() => {
bool headphones;
switch(volume_control.active_output) {
case VolumeControl.ActiveOutput.HEADPHONES:
case VolumeControl.ActiveOutput.USB_HEADPHONES:
case VolumeControl.ActiveOutput.HDMI_HEADPHONES:
case VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES:
headphones = true;
break;
default:
headphones = false;
break;
}
message("setting _volume_warning.headphones_active to %d", (int)headphones);
_volume_warning.headphones_active = headphones;
update_root_icon();
update_notification();
});
this.accounts_service = accounts;
/* If we're on the greeter, don't export */
if (this.accounts_service != null) {
this.accounts_service.notify["showDataOnGreeter"].connect(() => {
this.export_to_accounts_service = this.accounts_service.showDataOnGreeter;
eventually_update_player_actions();
});
this.export_to_accounts_service = this.accounts_service.showDataOnGreeter;
}
this.players = playerlist;
this.players.player_added.connect (this.player_added);
this.players.player_removed.connect (this.player_removed);
this.actions = new SimpleActionGroup ();
this.actions.add_action_entries (action_entries, this);
this.actions.add_action (this.create_silent_mode_action ());
this.actions.add_action (this.create_mute_action ());
this.actions.add_action (this.create_volume_action ());
this.actions.add_action (this.create_mic_volume_action ());
this.actions.add_action (this.create_high_volume_action ());
this.actions.add_action (this.create_volume_sync_action ());
this.menus = new HashTable<string, SoundMenu> (str_hash, str_equal);
this.menus.insert ("desktop_greeter", new SoundMenu (null, SoundMenu.DisplayFlags.SHOW_MUTE | SoundMenu.DisplayFlags.HIDE_PLAYERS | SoundMenu.DisplayFlags.GREETER_PLAYERS));
this.menus.insert ("phone_greeter", new SoundMenu (null, SoundMenu.DisplayFlags.SHOW_SILENT_MODE | SoundMenu.DisplayFlags.HIDE_INACTIVE_PLAYERS | SoundMenu.DisplayFlags.GREETER_PLAYERS));
this.menus.insert ("desktop", new SoundMenu ("indicator.desktop-settings", SoundMenu.DisplayFlags.SHOW_MUTE));
this.menus.insert ("phone", new SoundMenu ("indicator.phone-settings", SoundMenu.DisplayFlags.SHOW_SILENT_MODE | SoundMenu.DisplayFlags.HIDE_INACTIVE_PLAYERS));
this.menus.@foreach ( (profile, menu) => {
this.volume_control.bind_property ("active-mic", menu, "show-mic-volume", BindingFlags.SYNC_CREATE);
});
this.menus.@foreach ( (profile, menu) => {
_volume_warning.bind_property ("high-volume", menu, "show-high-volume-warning", BindingFlags.SYNC_CREATE);
});
this.menus.@foreach ( (profile, menu) => {
this.volume_control.active_output_changed.connect (menu.update_volume_slider);
});
this.sync_preferred_players ();
this.settings.changed["interested-media-players"].connect ( () => {
this.sync_preferred_players ();
});
/* Hide the notification when the menu is shown */
var shown_action = actions.lookup_action ("indicator-shown") as SimpleAction;
shown_action.change_state.connect ((state) => {
block_info_notifications = state.get_boolean();
if (block_info_notifications) {
debug("Indicator is shown");
close_notification(info_notification);
} else {
debug("Indicator is hidden");
}
});
/* Everything is built, let's put it on the bus */
try {
export_actions = bus.export_action_group ("/com/canonical/indicator/sound", this.actions);
} catch (Error e) {
critical ("%s", e.message);
}
this.menus.@foreach ( (profile, menu) => menu.export (bus, @"/com/canonical/indicator/sound/$profile"));
}
private void close_notification(Notify.Notification? n) {
return_if_fail (n != null);
if (n.id != 0) {
try {
GLib.message("calling n.close()");
n.close();
GLib.message("done calling n.close()");
} catch (GLib.Error e) {
warning("Unable to close notification: %s", e.message);
}
}
}
private void show_notification(Notify.Notification? n) {
return_if_fail (n != null);
try {
GLib.message("calling n.show()");
n.show ();
GLib.message("done calling n.show()");
} catch (GLib.Error e) {
warning ("Unable to show notification: %s", e.message);
}
}
~Service() {
debug("Destroying Service Object");
clear_acts_player();
if (this.player_action_update_id > 0) {
Source.remove (this.player_action_update_id);
this.player_action_update_id = 0;
}
if (this.sound_was_blocked_timeout_id > 0) {
Source.remove (this.sound_was_blocked_timeout_id);
this.sound_was_blocked_timeout_id = 0;
}
if (this.export_actions != 0) {
bus.unexport_action_group(this.export_actions);
this.export_actions = 0;
}
}
bool greeter_show_track () {
return export_to_accounts_service;
}
void clear_acts_player () {
/* NOTE: This is a bit of a hack to ensure that accounts service doesn't
continue to export the player by keeping a ref in the timer */
if (this.accounts_service != null)
this.accounts_service.player = null;
}
public bool visible { get; set; }
const ActionEntry[] action_entries = {
{ "root", null, null, "@a{sv} {}", null },
{ "scroll", activate_scroll_action, "i", null, null },
{ "desktop-settings", activate_desktop_settings, null, null, null },
{ "phone-settings", activate_phone_settings, null, null, null },
{ "indicator-shown", null, null, "@b false", null },
};
SimpleActionGroup actions;
HashTable<string, SoundMenu> menus;
Settings settings;
VolumeControl volume_control;
MediaPlayerList players;
uint player_action_update_id;
bool mute_blocks_sound;
uint sound_was_blocked_timeout_id;
bool syncing_preferred_players = false;
AccountsServiceUser? accounts_service = null;
bool export_to_accounts_service = false;
private Notify.Notification info_notification;
private Options _options;
private VolumeWarning _volume_warning;
const double volume_step_percentage = 0.06;
private void activate_scroll_action (SimpleAction action, Variant? param) {
int direction = param.get_int32(); // positive for up, negative for down
message("scroll: %d", direction);
if (_volume_warning.active) {
_volume_warning.user_keypress(direction>0
? VolumeWarning.Key.VOLUME_UP
: VolumeWarning.Key.VOLUME_DOWN);
} else {
double delta = volume_step_percentage * direction;
double v = volume_control.volume.volume + delta;
volume_control.set_volume_clamp (v, VolumeControl.VolumeReasons.USER_KEYPRESS);
}
}
void activate_desktop_settings (SimpleAction action, Variant? param) {
var env = Environment.get_variable ("DESKTOP_SESSION");
string cmd;
if (Environment.get_variable ("MIR_SOCKET") != null)
{
UrlDispatch.send ("settings:///system/sound");
return;
}
if (env == "xubuntu" || env == "ubuntustudio")
cmd = "pavucontrol";
else if (env == "mate")
cmd = "mate-volume-control";
else
{
if (Environment.get_variable ("XDG_CURRENT_DESKTOP") == "Unity" && Environment.find_program_in_path ("unity-control-center") != null)
cmd = "unity-control-center sound";
else
cmd = "gnome-control-center sound";
}
try {
Process.spawn_command_line_async (cmd);
} catch (Error e) {
warning ("unable to launch sound settings: %s", e.message);
}
}
void activate_phone_settings (SimpleAction action, Variant? param) {
UrlDispatch.send ("settings:///system/sound");
}
/* Returns a serialized version of @icon_name suited for the panel */
static Variant serialize_themed_icon (string icon_name)
{
var icon = new ThemedIcon.with_default_fallbacks (icon_name);
return icon.serialize ();
}
void update_root_icon () {
double volume = this.volume_control.volume.volume;
string icon = get_volume_root_icon (volume, this.volume_control.mute, volume_control.active_output);
string accessible_name;
if (this.volume_control.mute) {
accessible_name = _("Volume (muted)");
} else if (this.accounts_service != null && this.accounts_service.silentMode) {
int volume_int = (int)(volume * 100);
accessible_name = "%s (%s %d%%)".printf (_("Volume"), _("silent"), volume_int);
} else {
int volume_int = (int)(volume * 100);
accessible_name = "%s (%d%%)".printf (_("Volume"), volume_int);
}
var root_action = actions.lookup_action ("root") as SimpleAction;
var builder = new VariantBuilder (new VariantType ("a{sv}"));
builder.add ("{sv}", "title", new Variant.string (_("Sound")));
builder.add ("{sv}", "accessible-desc", new Variant.string (accessible_name));
builder.add ("{sv}", "icon", serialize_themed_icon (icon));
builder.add ("{sv}", "visible", new Variant.boolean (this.visible));
root_action.set_state (builder.end());
}
private bool notify_server_caps_checked = false;
private bool notify_server_supports_sync = false;
private bool block_info_notifications = false;
private string get_volume_icon (double volume, VolumeControl.ActiveOutput active_output)
{
string icon = "";
switch (active_output)
{
case VolumeControl.ActiveOutput.SPEAKERS:
if (volume <= 0.0)
icon = "audio-volume-muted";
else if (volume <= 0.3)
icon = "audio-volume-low";
else if (volume <= 0.7)
icon = "audio-volume-medium";
else
icon = "audio-volume-high";
break;
case VolumeControl.ActiveOutput.HEADPHONES:
if (volume <= 0.0)
icon = "audio-volume-muted";
else if (volume <= 0.3)
icon = "audio-volume-low";
else if (volume <= 0.7)
icon = "audio-volume-medium";
else
icon = "audio-volume-high";
break;
case VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES:
if (volume <= 0.0)
icon = "audio-volume-muted";
else if (volume <= 0.3)
icon = "audio-volume-low";
else if (volume <= 0.7)
icon = "audio-volume-medium";
else
icon = "audio-volume-high";
break;
case VolumeControl.ActiveOutput.BLUETOOTH_SPEAKER:
if (volume <= 0.0)
icon = "audio-volume-muted";
else if (volume <= 0.3)
icon = "audio-volume-low";
else if (volume <= 0.7)
icon = "audio-volume-medium";
else
icon = "audio-volume-high";
break;
case VolumeControl.ActiveOutput.USB_SPEAKER:
if (volume <= 0.0)
icon = "audio-volume-muted";
else if (volume <= 0.3)
icon = "audio-volume-low";
else if (volume <= 0.7)
icon = "audio-volume-medium";
else
icon = "audio-volume-high";
break;
case VolumeControl.ActiveOutput.USB_HEADPHONES:
if (volume <= 0.0)
icon = "audio-volume-muted";
else if (volume <= 0.3)
icon = "audio-volume-low";
else if (volume <= 0.7)
icon = "audio-volume-medium";
else
icon = "audio-volume-high";
break;
case VolumeControl.ActiveOutput.HDMI_SPEAKER:
if (volume <= 0.0)
icon = "audio-volume-muted";
else if (volume <= 0.3)
icon = "audio-volume-low";
else if (volume <= 0.7)
icon = "audio-volume-medium";
else
icon = "audio-volume-high";
break;
case VolumeControl.ActiveOutput.HDMI_HEADPHONES:
if (volume <= 0.0)
icon = "audio-volume-muted";
else if (volume <= 0.3)
icon = "audio-volume-low";
else if (volume <= 0.7)
icon = "audio-volume-medium";
else
icon = "audio-volume-high";
break;
}
return icon;
}
private string get_volume_root_icon_by_volume (double volume, VolumeControl.ActiveOutput active_output)
{
string icon = "";
switch (active_output)
{
case VolumeControl.ActiveOutput.SPEAKERS:
if (volume <= 0.0)
icon = "audio-volume-muted-panel";
else if (volume <= 0.3)
icon = "audio-volume-low-panel";
else if (volume <= 0.7)
icon = "audio-volume-medium-panel";
else
icon = "audio-volume-high-panel";
break;
case VolumeControl.ActiveOutput.HEADPHONES:
if (volume <= 0.0)
icon = "audio-volume-muted-panel";
else if (volume <= 0.3)
icon = "audio-volume-low-panel";
else if (volume <= 0.7)
icon = "audio-volume-medium-panel";
else
icon = "audio-volume-high-panel";
break;
case VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES:
if (volume <= 0.0)
icon = "audio-volume-muted-panel";
else if (volume <= 0.3)
icon = "audio-volume-low-panel";
else if (volume <= 0.7)
icon = "audio-volume-medium-panel";
else
icon = "audio-volume-high-panel";
break;
case VolumeControl.ActiveOutput.BLUETOOTH_SPEAKER:
if (volume <= 0.0)
icon = "audio-volume-muted-panel";
else if (volume <= 0.3)
icon = "audio-volume-low-panel";
else if (volume <= 0.7)
icon = "audio-volume-medium-panel";
else
icon = "audio-volume-high-panel";
break;
case VolumeControl.ActiveOutput.USB_SPEAKER:
if (volume <= 0.0)
icon = "audio-volume-muted-panel";
else if (volume <= 0.3)
icon = "audio-volume-low-panel";
else if (volume <= 0.7)
icon = "audio-volume-medium-panel";
else
icon = "audio-volume-high-panel";
break;
case VolumeControl.ActiveOutput.USB_HEADPHONES:
if (volume <= 0.0)
icon = "audio-volume-muted-panel";
else if (volume <= 0.3)
icon = "audio-volume-low-panel";
else if (volume <= 0.7)
icon = "audio-volume-medium-panel";
else
icon = "audio-volume-high-panel";
break;
case VolumeControl.ActiveOutput.HDMI_SPEAKER:
if (volume <= 0.0)
icon = "audio-volume-muted-panel";
else if (volume <= 0.3)
icon = "audio-volume-low-panel";
else if (volume <= 0.7)
icon = "audio-volume-medium-panel";
else
icon = "audio-volume-high-panel";
break;
case VolumeControl.ActiveOutput.HDMI_HEADPHONES:
if (volume <= 0.0)
icon = "audio-volume-muted-panel";
else if (volume <= 0.3)
icon = "audio-volume-low-panel";
else if (volume <= 0.7)
icon = "audio-volume-medium-panel";
else
icon = "audio-volume-high-panel";
break;
}
return icon;
}
private string get_volume_notification_icon (double volume, bool loud, VolumeControl.ActiveOutput active_output) {
string icon = "";
if (loud) {
switch (active_output)
{
case VolumeControl.ActiveOutput.SPEAKERS:
icon = "audio-volume-high";
break;
case VolumeControl.ActiveOutput.HEADPHONES:
icon = "audio-volume-high";
break;
case VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES:
icon = "audio-volume-high";
break;
case VolumeControl.ActiveOutput.BLUETOOTH_SPEAKER:
icon = "audio-volume-high";
break;
case VolumeControl.ActiveOutput.USB_SPEAKER:
icon = "audio-volume-high";
break;
case VolumeControl.ActiveOutput.USB_HEADPHONES:
icon = "audio-volume-high";
break;
case VolumeControl.ActiveOutput.HDMI_SPEAKER:
icon = "audio-volume-high";
break;
case VolumeControl.ActiveOutput.HDMI_HEADPHONES:
icon = "audio-volume-high";
break;
}
} else {
icon = get_volume_icon (volume, active_output);
}
return icon;
}
private string get_volume_root_icon (double volume, bool mute, VolumeControl.ActiveOutput active_output) {
string icon = "";
switch (active_output)
{
case VolumeControl.ActiveOutput.SPEAKERS:
if (mute || volume <= 0.0)
icon = this.mute_blocks_sound ? "audio-volume-muted-blocking-panel" : "audio-volume-muted-panel";
else if (this.accounts_service != null && this.accounts_service.silentMode)
icon = "audio-volume-muted-panel";
else
icon = get_volume_root_icon_by_volume (volume, active_output);
break;
case VolumeControl.ActiveOutput.HEADPHONES:
if (mute || volume <= 0.0)
icon = this.mute_blocks_sound ? "audio-volume-muted-blocking-panel" : "audio-volume-muted-panel";
else if (this.accounts_service != null && this.accounts_service.silentMode)
icon = "audio-volume-muted-panel";
else
icon = get_volume_root_icon_by_volume (volume, active_output);
break;
case VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES:
if (mute || volume <= 0.0)
icon = this.mute_blocks_sound ? "audio-volume-muted-blocking-panel" : "audio-volume-muted-panel";
else if (this.accounts_service != null && this.accounts_service.silentMode)
icon = "audio-volume-muted-panel";
else
icon = get_volume_root_icon_by_volume (volume, active_output);
break;
case VolumeControl.ActiveOutput.BLUETOOTH_SPEAKER:
if (mute || volume <= 0.0)
icon = this.mute_blocks_sound ? "audio-volume-muted-blocking-panel" : "audio-volume-muted-panel";
else if (this.accounts_service != null && this.accounts_service.silentMode)
icon = "audio-volume-muted-panel";
else
icon = get_volume_root_icon_by_volume (volume, active_output);
break;
case VolumeControl.ActiveOutput.USB_SPEAKER:
if (mute || volume <= 0.0)
icon = this.mute_blocks_sound ? "audio-volume-muted-blocking-panel" : "audio-volume-muted-panel";
else if (this.accounts_service != null && this.accounts_service.silentMode)
icon = "audio-volume-muted-panel";
else
icon = get_volume_root_icon_by_volume (volume, active_output);
break;
case VolumeControl.ActiveOutput.USB_HEADPHONES:
if (mute || volume <= 0.0)
icon = this.mute_blocks_sound ? "audio-volume-muted-blocking-panel" : "audio-volume-muted-panel";
else if (this.accounts_service != null && this.accounts_service.silentMode)
icon = "audio-volume-muted-panel";
else
icon = get_volume_root_icon_by_volume (volume, active_output);
break;
case VolumeControl.ActiveOutput.HDMI_SPEAKER:
if (mute || volume <= 0.0)
icon = this.mute_blocks_sound ? "audio-volume-muted-blocking-panel" : "audio-volume-muted-panel";
else if (this.accounts_service != null && this.accounts_service.silentMode)
icon = "audio-volume-muted-panel";
else
icon = get_volume_root_icon_by_volume (volume, active_output);
break;
case VolumeControl.ActiveOutput.HDMI_HEADPHONES:
if (mute || volume <= 0.0)
icon = this.mute_blocks_sound ? "audio-volume-muted-blocking-panel" : "audio-volume-muted-panel";
else if (this.accounts_service != null && this.accounts_service.silentMode)
icon = "audio-volume-muted-panel";
else
icon = get_volume_root_icon_by_volume (volume, active_output);
break;
}
return icon;
}
private string get_notification_label () {
string volume_label = "";
switch (volume_control.active_output)
{
case VolumeControl.ActiveOutput.SPEAKERS:
volume_label = _("Speakers");
break;
case VolumeControl.ActiveOutput.HEADPHONES:
volume_label = _("Headphones");
break;
case VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES:
volume_label = _("Bluetooth headphones");
break;
case VolumeControl.ActiveOutput.BLUETOOTH_SPEAKER:
volume_label = _("Bluetooth speaker");
break;
case VolumeControl.ActiveOutput.USB_SPEAKER:
volume_label = _("Usb speaker");
break;
case VolumeControl.ActiveOutput.USB_HEADPHONES:
volume_label = _("Usb headphones");
break;
case VolumeControl.ActiveOutput.HDMI_SPEAKER:
volume_label = _("HDMI speaker");
break;
case VolumeControl.ActiveOutput.HDMI_HEADPHONES:
volume_label = _("HDMI headphones");
break;
}
return volume_label;
}
private void update_notification () {
if (!notify_server_caps_checked)
{
GLib.message("service checking server caps");
List<string> caps = Notify.get_server_caps ();
notify_server_supports_sync = caps.find_custom ("x-canonical-private-synchronous", strcmp) != null;
notify_server_caps_checked = true;
GLib.message("service done checking server caps");
}
if (!_volume_warning.active && notify_server_supports_sync && !block_info_notifications)
{
bool is_loud = _volume_warning.high_volume;
string volume_label = get_notification_label ();
string icon = get_volume_notification_icon (volume_control.volume.volume, is_loud, volume_control.active_output);
/* Reset the notification */
GLib.message("service showing info notification");
var n = this.info_notification;
n.update (_("Volume"), volume_label, icon);
n.clear_hints();
n.set_hint ("x-canonical-non-shaped-icon", "true");
n.set_hint ("x-canonical-private-synchronous", "true");
n.set_hint ("x-canonical-value-bar-tint", is_loud ? "true" : "false");
n.set_hint ("value", (int32)Math.round(get_volume_percent() * 100.0));
show_notification(n);
GLib.message("service done showing info notification");
}
}
SimpleAction silent_action;
Action create_silent_mode_action () {
bool silentNow = false;
if (this.accounts_service != null) {
silentNow = this.accounts_service.silentMode;
}
silent_action = new SimpleAction.stateful ("silent-mode", null, new Variant.boolean (silentNow));
/* If we're not dealing with accounts service, we'll just always be out
of silent mode and that's cool. */
if (this.accounts_service == null) {
return silent_action;
}
this.accounts_service.notify["silentMode"].connect(() => {
silent_action.set_state(new Variant.boolean(this.accounts_service.silentMode));
this.update_root_icon ();
});
silent_action.activate.connect ((action, param) => {
action.change_state (new Variant.boolean (!action.get_state().get_boolean()));
});
silent_action.change_state.connect ((action, val) => {
this.accounts_service.silentMode = val.get_boolean();
});
return silent_action;
}
SimpleAction mute_action;
Action create_mute_action () {
mute_action = new SimpleAction.stateful ("mute", null, new Variant.boolean (this.volume_control.mute));
mute_action.activate.connect ( (action, param) => {
action.change_state (new Variant.boolean (!action.get_state ().get_boolean ()));
});
mute_action.change_state.connect ( (action, val) => {
volume_control.set_mute (val.get_boolean ());
});
this.volume_control.notify["mute"].connect ( () => {
mute_action.set_state (new Variant.boolean (this.volume_control.mute));
this.update_root_icon ();
});
this.volume_control.notify["is-playing"].connect( () => {
if (!this.volume_control.mute) {
this.mute_blocks_sound = false;
return;
}
if (this.volume_control.is_playing) {
this.mute_blocks_sound = true;
}
else if (this.mute_blocks_sound) {
/* Continue to show the blocking icon five seconds after a player has tried to play something */
if (this.sound_was_blocked_timeout_id > 0)
Source.remove (this.sound_was_blocked_timeout_id);
this.sound_was_blocked_timeout_id = Timeout.add_seconds (5, () => {
this.mute_blocks_sound = false;
this.sound_was_blocked_timeout_id = 0;
this.update_root_icon ();
return false;
});
}
this.update_root_icon ();
});
return mute_action;
}
/* return the current volume in the range of [0.0, 1.0] */
private double get_volume_percent() {
return volume_control.volume.volume / _options.max_volume;
}
/* volume control's range can vary depending on options.max_volume,
* but the action always needs to be in [0.0, 1.0]... */
private Variant create_volume_action_state() {
return new Variant.double (get_volume_percent());
}
private void update_volume_action_state() {
volume_action.set_state(create_volume_action_state());
}
private SimpleAction volume_action;
private Action create_volume_action () {
volume_action = new SimpleAction.stateful ("volume", VariantType.INT32, create_volume_action_state());
volume_action.change_state.connect ( (action, val) => {
double v = val.get_double () * _options.max_volume;
volume_control.set_volume_clamp (v, VolumeControl.VolumeReasons.USER_KEYPRESS);
});
/* activating this action changes the volume by the amount given in the parameter */
volume_action.activate.connect ((a,p) => activate_scroll_action(a,p));
_options.notify["max_volume"].connect(() => {
update_volume_action_state();
});
this.volume_control.notify["volume"].connect (() => {
update_volume_action_state();
this.update_root_icon ();
var reason = volume_control.volume.reason;
if (reason == VolumeControl.VolumeReasons.USER_KEYPRESS ||
reason == VolumeControl.VolumeReasons.DEVICE_OUTPUT_CHANGE)
this.update_notification ();
});
this.volume_control.bind_property ("ready", volume_action, "enabled", BindingFlags.SYNC_CREATE);
return volume_action;
}
SimpleAction mic_volume_action;
Action create_mic_volume_action () {
mic_volume_action = new SimpleAction.stateful ("mic-volume", null, new Variant.double (this.volume_control.mic_volume));
mic_volume_action.change_state.connect ( (action, val) => {
volume_control.mic_volume = val.get_double ();
});
this.volume_control.notify["mic-volume"].connect ( () => {
mic_volume_action.set_state (new Variant.double (this.volume_control.mic_volume));
});
this.volume_control.bind_property ("ready", mic_volume_action, "enabled", BindingFlags.SYNC_CREATE);
return mic_volume_action;
}
private Variant create_high_volume_action_state() {
return new Variant.boolean (_volume_warning.high_volume);
}
private void update_high_volume_action_state() {
high_volume_action.set_state(create_high_volume_action_state());
}
SimpleAction high_volume_action;
Action create_high_volume_action () {
high_volume_action = new SimpleAction.stateful("high-volume", null, create_high_volume_action_state());
_volume_warning.notify["high-volume"].connect( () => {
GLib.message("in service, due to high-volume change, updating high-volume-action-state");
update_high_volume_action_state();
GLib.message("in service, due to high-volume change, updating notification");
update_notification();
GLib.message("in service, finished handling high-volume notify");
});
return high_volume_action;
}
SimpleAction volume_sync_action;
uint64 volume_sync_number_ = 0;
Action create_volume_sync_action () {
volume_sync_action = new SimpleAction.stateful("volume-sync", VariantType.UINT64, new Variant.uint64 (volume_sync_number_));
return volume_sync_action;
}
void increment_volume_sync_action () {
volume_sync_action.set_state(new Variant.uint64 (++volume_sync_number_));
}
uint export_actions = 0;
Variant action_state_for_player (MediaPlayer player, bool show_track = true) {
var builder = new VariantBuilder (new VariantType ("a{sv}"));
builder.add ("{sv}", "running", new Variant ("b", player.is_running));
builder.add ("{sv}", "state", new Variant ("s", player.state));
if (player.current_track != null && show_track) {
builder.add ("{sv}", "title", new Variant ("s", player.current_track.title));
builder.add ("{sv}", "artist", new Variant ("s", player.current_track.artist));
builder.add ("{sv}", "album", new Variant ("s", player.current_track.album));
builder.add ("{sv}", "art-url", new Variant ("s", player.current_track.art_url));
}
return builder.end ();
}
bool update_player_actions () {
bool clear_accounts_player = true;
foreach (var player in this.players) {
SimpleAction? action = this.actions.lookup_action (player.id) as SimpleAction;
if (action != null) {
action.set_state (this.action_state_for_player (player));
action.set_enabled (player.can_raise);
}
SimpleAction? greeter_action = this.actions.lookup_action (player.id + ".greeter") as SimpleAction;
if (greeter_action != null) {
greeter_action.set_state (this.action_state_for_player (player, greeter_show_track()));
greeter_action.set_enabled (player.can_raise);
}
/* If we're playing then put that data in accounts service */
if (player.is_running && export_to_accounts_service && accounts_service != null) {
accounts_service.player = player;
clear_accounts_player = false;
}
}
if (clear_accounts_player)
clear_acts_player();
this.player_action_update_id = 0;
return false;
}
void eventually_update_player_actions () {
if (player_action_update_id == 0)
this.player_action_update_id = Idle.add (this.update_player_actions);
}
void sync_preferred_players () {
this.syncing_preferred_players = true;
this.players.sync (settings.get_strv ("interested-media-players"));
this.syncing_preferred_players = false;
}
void update_preferred_players () {
/* only write the key if we're not getting this call because we're syncing from the key right now */
if (!this.syncing_preferred_players) {
var builder = new VariantBuilder (VariantType.STRING_ARRAY);
foreach (var player in this.players)
builder.add ("s", player.id);
this.settings.set_value ("interested-media-players", builder.end ());
}
}
void player_added (MediaPlayer player) {
this.menus.@foreach ( (profile, menu) => menu.add_player (player));
SimpleAction action = new SimpleAction.stateful (player.id, null, this.action_state_for_player (player));
action.set_enabled (player.can_raise);
action.activate.connect ( () => { player.activate (); });
this.actions.add_action (action);
SimpleAction greeter_action = new SimpleAction.stateful (player.id + ".greeter", null, this.action_state_for_player (player, greeter_show_track()));
greeter_action.set_enabled (player.can_raise);
greeter_action.activate.connect ( () => { player.activate (); });
this.actions.add_action (greeter_action);
var play_action = new SimpleAction.stateful ("play." + player.id, null, player.state);
play_action.activate.connect ( () => player.play_pause () );
this.actions.add_action (play_action);
player.notify["state"].connect ( (object, pspec) => {
play_action.set_state (player.state);
});
var next_action = new SimpleAction ("next." + player.id, null);
next_action.activate.connect ( () => player.next () );
this.actions.add_action (next_action);
var prev_action = new SimpleAction ("previous." + player.id, null);
prev_action.activate.connect ( () => player.previous () );
this.actions.add_action (prev_action);
var playlist_action = new SimpleAction ("play-playlist." + player.id, VariantType.STRING);
playlist_action.activate.connect ( (parameter) => player.activate_playlist_by_name (parameter.get_string ()) );
this.actions.add_action (playlist_action);
player.notify.connect (this.eventually_update_player_actions);
this.update_preferred_players ();
}
void player_removed (MediaPlayer player) {
this.actions.remove_action (player.id);
this.actions.remove_action ("play." + player.id);
this.actions.remove_action ("next." + player.id);
this.actions.remove_action ("previous." + player.id);
this.actions.remove_action ("play-playlist." + player.id);
player.notify.disconnect (this.eventually_update_player_actions);
this.menus.@foreach ( (profile, menu) => menu.remove_player (player));
this.update_preferred_players ();
}
}
|