GCC Code Coverage Report


Directory: ./
File: panels/sound/cc-balance-slider.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 48 0.0%
Functions: 0 9 0.0%
Branches: 0 23 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2018 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "cc-sound-resources.h"
19 #include "cc-balance-slider.h"
20 #include "gvc-channel-map-private.h"
21
22 struct _CcBalanceSlider
23 {
24 GtkWidget parent_instance;
25
26 GtkWidget *scale;
27 GtkAdjustment *adjustment;
28
29 GvcChannelMap *channel_map;
30 guint volume_changed_handler_id;
31 };
32
33 G_DEFINE_TYPE (CcBalanceSlider, cc_balance_slider, GTK_TYPE_WIDGET)
34
35 static void
36 changed_cb (CcBalanceSlider *self)
37 {
38 gdouble value;
39 const pa_channel_map *pa_map;
40 pa_cvolume pa_volume;
41
42 if (self->channel_map == NULL)
43 return;
44
45 value = gtk_adjustment_get_value (self->adjustment);
46 pa_map = gvc_channel_map_get_pa_channel_map (self->channel_map);
47 pa_volume = *gvc_channel_map_get_cvolume (self->channel_map);
48 pa_cvolume_set_balance (&pa_volume, pa_map, value);
49 gvc_channel_map_volume_changed (self->channel_map, &pa_volume, TRUE);
50 }
51
52 static void
53 volume_changed_cb (CcBalanceSlider *self)
54 {
55 const gdouble *volumes;
56
57 volumes = gvc_channel_map_get_volume (self->channel_map);
58 g_signal_handlers_block_by_func (self->adjustment, volume_changed_cb, self);
59
60 /* If close to 0, set to 0 to also snap while moving with arrow keys */
61 if (fabs (volumes[BALANCE]) < 0.01)
62 gtk_adjustment_set_value (self->adjustment, 0);
63 else
64 gtk_adjustment_set_value (self->adjustment, volumes[BALANCE]);
65
66 g_signal_handlers_unblock_by_func (self->adjustment, volume_changed_cb, self);
67 }
68
69 static void
70 cc_balance_slider_dispose (GObject *object)
71 {
72 CcBalanceSlider *self = CC_BALANCE_SLIDER (object);
73
74 g_clear_pointer (&self->scale, gtk_widget_unparent);
75
76 g_clear_object (&self->channel_map);
77
78 G_OBJECT_CLASS (cc_balance_slider_parent_class)->dispose (object);
79 }
80
81 void
82 cc_balance_slider_class_init (CcBalanceSliderClass *klass)
83 {
84 GObjectClass *object_class = G_OBJECT_CLASS (klass);
85 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
86
87 object_class->dispose = cc_balance_slider_dispose;
88
89 gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/sound/cc-balance-slider.ui");
90
91 gtk_widget_class_bind_template_child (widget_class, CcBalanceSlider, scale);
92 gtk_widget_class_bind_template_child (widget_class, CcBalanceSlider, adjustment);
93
94 gtk_widget_class_bind_template_callback (widget_class, changed_cb);
95
96 gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
97 }
98
99 void
100 cc_balance_slider_init (CcBalanceSlider *self)
101 {
102 g_resources_register (cc_sound_get_resource ());
103
104 gtk_widget_init_template (GTK_WIDGET (self));
105
106 // Don't flip the slider with RTL locale
107 gtk_widget_set_direction (self->scale, GTK_TEXT_DIR_LTR);
108 }
109
110 void
111 cc_balance_slider_set_channel_map (CcBalanceSlider *self,
112 GvcChannelMap *channel_map)
113 {
114 g_return_if_fail (CC_IS_BALANCE_SLIDER (self));
115
116 if (self->channel_map != NULL)
117 {
118 g_signal_handler_disconnect (self->channel_map, self->volume_changed_handler_id);
119 self->volume_changed_handler_id = 0;
120 }
121 g_clear_object (&self->channel_map);
122
123 if (channel_map != NULL)
124 {
125 self->channel_map = g_object_ref (channel_map);
126
127 self->volume_changed_handler_id = g_signal_connect_object (channel_map,
128 "volume-changed",
129 G_CALLBACK (volume_changed_cb),
130 self, G_CONNECT_SWAPPED);
131 volume_changed_cb (self);
132 }
133 }
134