forked from WayfireWM/wf-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvolume.cpp
More file actions
247 lines (209 loc) · 7.31 KB
/
volume.cpp
File metadata and controls
247 lines (209 loc) · 7.31 KB
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
#include <gtkmm.h>
#include <glibmm.h>
#include "volume.hpp"
#include "volume-level.hpp"
void WayfireVolume::update_icon()
{
if (gvc_stream && gvc_mixer_stream_get_is_muted(gvc_stream))
{
main_image.set_from_icon_name(volume_icon_for(0)); // mute
return;
}
main_image.set_from_icon_name(volume_icon_for(volume_scale.get_target_value() / (double)max_norm));
}
bool WayfireVolume::on_popover_timeout(int timer)
{
popover.popdown();
return false;
}
void WayfireVolume::check_set_popover_timeout()
{
popover_timeout.disconnect();
popover_timeout = Glib::signal_timeout().connect(sigc::bind(sigc::mem_fun(*this,
&WayfireVolume::on_popover_timeout), 0), timeout * 1000);
}
void WayfireVolume::set_volume(pa_volume_t volume, set_volume_flags_t flags)
{
volume_scale.set_target_value(volume);
if (gvc_stream && (flags & VOLUME_FLAG_UPDATE_GVC))
{
gvc_mixer_stream_set_volume(gvc_stream, volume);
gvc_mixer_stream_push_volume(gvc_stream);
}
if (flags & VOLUME_FLAG_SHOW_POPOVER)
{
if (!popover.is_visible())
{
popover.popup();
} else
{
check_set_popover_timeout();
}
}
update_icon();
}
void WayfireVolume::on_volume_changed_external()
{
auto volume = gvc_mixer_stream_get_volume(gvc_stream);
if (volume != (pa_volume_t)this->volume_scale.get_target_value())
{
set_volume(volume, VOLUME_FLAG_SHOW_POPOVER);
}
check_set_popover_timeout();
}
static void notify_volume(GvcMixerControl *gvc_control,
guint id, gpointer user_data)
{
WayfireVolume *wf_volume = (WayfireVolume*)user_data;
wf_volume->on_volume_changed_external();
}
static void notify_is_muted(GvcMixerControl *gvc_control,
guint id, gpointer user_data)
{
WayfireVolume *wf_volume = (WayfireVolume*)user_data;
wf_volume->update_icon();
}
void WayfireVolume::disconnect_gvc_stream_signals()
{
if (notify_volume_signal)
{
g_signal_handler_disconnect(gvc_stream, notify_volume_signal);
}
notify_volume_signal = 0;
if (notify_is_muted_signal)
{
g_signal_handler_disconnect(gvc_stream, notify_is_muted_signal);
}
notify_is_muted_signal = 0;
}
void WayfireVolume::on_default_sink_changed()
{
gvc_stream = gvc_mixer_control_get_default_sink(gvc_control);
if (!gvc_stream)
{
printf("GVC: Failed to get default sink\n");
return;
}
/* Reconnect signals to new sink */
disconnect_gvc_stream_signals();
notify_volume_signal = g_signal_connect(gvc_stream, "notify::volume",
G_CALLBACK(notify_volume), this);
notify_is_muted_signal = g_signal_connect(gvc_stream, "notify::is-muted",
G_CALLBACK(notify_is_muted), this);
/* Update the scale attributes */
max_norm = gvc_mixer_control_get_vol_max_norm(gvc_control);
volume_scale.set_range(0.0, max_norm);
volume_scale.set_increments(max_norm * scroll_sensitivity,
max_norm * scroll_sensitivity * 2);
/* Finally, update the displayed volume. However, do not show the popup */
set_volume(gvc_mixer_stream_get_volume(gvc_stream), VOLUME_FLAG_NO_ACTION);
}
static void default_sink_changed(GvcMixerControl *gvc_control,
guint id, gpointer user_data)
{
WayfireVolume *wf_volume = (WayfireVolume*)user_data;
wf_volume->on_default_sink_changed();
}
void WayfireVolume::on_volume_value_changed()
{
/* User manually changed volume */
set_volume(volume_scale.get_target_value());
}
void WayfireVolume::init(Gtk::Box *container)
{
/* Setup button */
signals.push_back(button.signal_clicked().connect([=]
{
if (!popover.is_visible())
{
popover.popup();
} else
{
popover.popdown();
}
}));
auto style = button.get_style_context();
style->add_class("volume");
style->add_class("flat");
gtk_widget_set_parent(GTK_WIDGET(popover.gobj()), GTK_WIDGET(button.gobj()));
auto scroll_gesture = Gtk::EventControllerScroll::create();
scroll_gesture->signal_scroll().connect([=] (double dx, double dy)
{
int change = 0;
if (scroll_gesture->get_unit() == Gdk::ScrollUnit::WHEEL)
{
// +- number of clicks.
change = dy * max_norm * scroll_sensitivity;
} else
{
// Number of pixels expected to have scrolled. usually in 100s
change = (dy / 100.0) * max_norm * scroll_sensitivity;
}
set_volume(std::clamp(volume_scale.get_target_value() - change,
0.0, max_norm));
return true;
}, true);
scroll_gesture->set_flags(Gtk::EventControllerScroll::Flags::VERTICAL);
scroll_gesture->set_propagation_phase(Gtk::PropagationPhase::CAPTURE);
button.add_controller(scroll_gesture);
/* Setup popover */
popover.set_autohide(false);
popover.set_child(volume_scale);
// popover->set_modal(false);
popover.get_style_context()->add_class("volume-popover");
auto scroll_gesture2 = Gtk::EventControllerScroll::create();
signals.push_back(scroll_gesture2->signal_scroll().connect([=] (double dx, double dy)
{
int change = 0;
if (scroll_gesture->get_unit() == Gdk::ScrollUnit::WHEEL)
{
// +- number of clicks.
change = dy * max_norm * scroll_sensitivity;
} else
{
// Number of pixels expected to have scrolled. usually in 100s
change = (dy / 100.0) * max_norm * scroll_sensitivity;
}
set_volume(std::clamp(volume_scale.get_target_value() - change,
0.0, max_norm));
return true;
}, false));
scroll_gesture2->set_flags(Gtk::EventControllerScroll::Flags::VERTICAL);
scroll_gesture2->set_propagation_phase(Gtk::PropagationPhase::CAPTURE);
volume_scale.add_controller(scroll_gesture2);
volume_scale.set_draw_value(false);
volume_scale.set_size_request(300, 0);
volume_scale.set_user_changed_callback([=] () { on_volume_value_changed(); });
signals.push_back(volume_scale.signal_state_flags_changed().connect(
[=] (Gtk::StateFlags) { check_set_popover_timeout(); }));
/* Setup gvc control */
gvc_control = gvc_mixer_control_new("Wayfire Volume Control");
notify_default_sink_changed = g_signal_connect(gvc_control,
"default-sink-changed", G_CALLBACK(default_sink_changed), this);
gvc_mixer_control_open(gvc_control);
/* Middle click toggle mute */
auto middle_click_gesture = Gtk::GestureClick::create();
middle_click_gesture->set_button(2);
signals.push_back(middle_click_gesture->signal_pressed().connect([=] (int count, double x, double y)
{
bool muted = !(gvc_stream && gvc_mixer_stream_get_is_muted(gvc_stream));
gvc_mixer_stream_change_is_muted(gvc_stream, muted);
gvc_mixer_stream_push_volume(gvc_stream);
}));
button.add_controller(middle_click_gesture);
/* Setup layout */
container->append(button);
button.set_child(main_image);
}
WayfireVolume::~WayfireVolume()
{
gtk_widget_unparent(GTK_WIDGET(popover.gobj()));
disconnect_gvc_stream_signals();
if (notify_default_sink_changed)
{
g_signal_handler_disconnect(gvc_control, notify_default_sink_changed);
}
gvc_mixer_control_close(gvc_control);
g_object_unref(gvc_control);
popover_timeout.disconnect();
}