forked from elementary/wingpanel-indicator-sound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeviceManagerWidget.vala
147 lines (127 loc) · 4.8 KB
/
DeviceManagerWidget.vala
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
// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
/*-
* Copyright 2021 elementary, Inc. (https://elementary.io)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
* Authored by: Sean Davis <sean@bluesabre.org>
*/
public class Sound.Widgets.DeviceManagerWidget : Gtk.Grid {
private Gtk.ListBox device_list;
private Gtk.ScrolledWindow scrolled_box;
public bool is_input_manager;
private unowned PulseAudioManager pam;
construct {
pam = PulseAudioManager.get_default ();
pam.new_device.connect (add_device);
pam.notify["default-output"].connect (default_output_changed);
pam.notify["default-input"].connect (default_input_changed);
pam.start ();
device_list = new Gtk.ListBox () {
activate_on_single_click = true,
visible = true
};
scrolled_box = new Gtk.ScrolledWindow (null, null) {
hscrollbar_policy = Gtk.PolicyType.NEVER,
propagate_natural_height = true,
max_content_height = 256,
no_show_all = true
};
scrolled_box.add (device_list);
attach (scrolled_box, 0, 1, 1);
update_showable ();
}
private void add_device (Device device) {
if (device.input != is_input_manager) {
return;
}
Gtk.ListBoxRow? row = device_list.get_row_at_index (0);
var device_item = new DeviceItem (device.display_name,
device.is_default,
device.is_priority,
device.get_nice_icon (),
row);
device_list.add (device_item);
device_item.activated.connect (() => {
pam.set_default_device.begin (device);
update_preferred_devices (device);
});
device.removed.connect (() => {
device_list.remove (device_item);
device_list.show_all ();
update_showable ();
});
device.defaulted.connect (() => {
device_item.set_default ();
update_preferred_devices (device);
update_showable ();
});
update_showable ();
}
/**
* Preferred devices are stored as:
* {
* device_a: last_used_unix_timestamp,
* device_b: last_used_unix_timestamp
* }
* If a device hasn't been selected in 7 days, it is removed from preferred devices.
* Device selection happens when the user selects the device, and when the plugin
* is initialized.
*/
private void update_preferred_devices (Device device) {
VariantBuilder builder = new VariantBuilder (new VariantType ("a{si}"));
var preferred_devices = Sound.Indicator.settings.get_value ("preferred-devices");
int32 now = (int32)(GLib.get_real_time () / 1000000);
int32 preferred_expiry = now - (86400 * 7); // Expire unused after 7 days
builder.add ("{si}", device.id, now);
foreach (var dev in preferred_devices) {
var name = dev.get_child_value (0).get_string ();
var last_used = dev.get_child_value (1).get_int32 ();
if (name == device.id) {
continue;
}
if (last_used < preferred_expiry) {
continue;
}
builder.add ("{si}", name, last_used);
}
Variant dictionary = builder.end ();
device.is_priority = true;
Sound.Indicator.settings.set_value ("preferred-devices", dictionary);
}
private uint n_visible_items () {
uint n = 0;
foreach (unowned var device in device_list.get_children ()) {
if (device.visible) {
n++;
}
}
return n;
}
private void update_showable () {
if (n_visible_items () <= 1) {
scrolled_box.hide ();
} else {
scrolled_box.show ();
}
}
private void default_output_changed () {
pam.default_output.defaulted ();
}
private void default_input_changed () {
pam.default_input.defaulted ();
}
}