| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include "optional-state.h" |
| 5 |
#include "window-levels.h" |
| 6 |
#include "window-mixer.h" |
| 7 |
#include "window-preferences.h" |
| 8 |
|
| 9 |
s) |
| 10 |
static const struct { |
| 11 |
const char *label; |
| 12 |
int hz; |
| 13 |
int ms; |
| 14 |
} levels_rates[] = { |
| 15 |
{ "5 Hz", 5, 200 }, |
| 16 |
{ "10 Hz", 10, 100 }, |
| 17 |
{ "20 Hz", 20, 50 }, |
| 18 |
}; |
| 19 |
|
| 20 |
#define LEVELS_RATE_COUNT \ |
| 21 |
((int)(sizeof(levels_rates) / sizeof(levels_rates[0]))) |
| 22 |
|
| 23 |
int levels_hz_to_ms(int hz) { |
| 24 |
for (int i = 0; i < LEVELS_RATE_COUNT; i++) |
| 25 |
if (levels_rates[i].hz == hz) |
| 26 |
return levels_rates[i].ms; |
| 27 |
return DEFAULT_LEVELS_INTERVAL_MS; |
| 28 |
} |
| 29 |
|
| 30 |
static int ms_to_rate_index(int ms) { |
| 31 |
for (int i = 0; i < LEVELS_RATE_COUNT; i++) |
| 32 |
if (levels_rates[i].ms == ms) |
| 33 |
return i; |
| 34 |
for (int i = 0; i < LEVELS_RATE_COUNT; i++) |
| 35 |
if (levels_rates[i].ms == DEFAULT_LEVELS_INTERVAL_MS) |
| 36 |
return i; |
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
static int parse_bool_pref( |
| 41 |
GHashTable *state, |
| 42 |
const char *key, |
| 43 |
int fallback |
| 44 |
) { |
| 45 |
if (!state) |
| 46 |
return fallback; |
| 47 |
const char *val = g_hash_table_lookup(state, key); |
| 48 |
if (!val) |
| 49 |
return fallback; |
| 50 |
if (strcmp(val, "true") == 0) |
| 51 |
return 1; |
| 52 |
if (strcmp(val, "false") == 0) |
| 53 |
return 0; |
| 54 |
return fallback; |
| 55 |
} |
| 56 |
|
| 57 |
void load_preferences(struct alsa_card *card) { |
| 58 |
GHashTable *state = optional_state_load( |
| 59 |
card, CONFIG_SECTION_UI |
| 60 |
); |
| 61 |
|
| 62 |
card->pref_show_bottom_right_labels = parse_bool_pref( |
| 63 |
state, "mixer-show-bottom-right-labels", 0 |
| 64 |
); |
| 65 |
|
| 66 |
const char *val = state |
| 67 |
? g_hash_table_lookup(state, "levels-update-rate") |
| 68 |
: NULL; |
| 69 |
card->pref_levels_interval_ms = |
| 70 |
levels_hz_to_ms(val ? atoi(val) : 0); |
| 71 |
|
| 72 |
if (state) |
| 73 |
g_hash_table_destroy(state); |
| 74 |
} |
| 75 |
|
| 76 |
struct rate_buttons { |
| 77 |
struct alsa_card *card; |
| 78 |
GtkWidget *buttons[LEVELS_RATE_COUNT]; |
| 79 |
}; |
| 80 |
|
| 81 |
static void on_bottom_right_labels_changed( |
| 82 |
GObject *sw, |
| 83 |
GParamSpec *pspec, |
| 84 |
gpointer data |
| 85 |
) { |
| 86 |
struct alsa_card *card = data; |
| 87 |
|
| 88 |
card->pref_show_bottom_right_labels = |
| 89 |
gtk_switch_get_active(GTK_SWITCH(sw)); |
| 90 |
optional_state_save( |
| 91 |
card, CONFIG_SECTION_UI, |
| 92 |
"mixer-show-bottom-right-labels", |
| 93 |
card->pref_show_bottom_right_labels ? "true" : "false" |
| 94 |
); |
| 95 |
rebuild_mixer_grid(card); |
| 96 |
} |
| 97 |
|
| 98 |
static void on_rate_button_toggled( |
| 99 |
GtkToggleButton *button, |
| 100 |
gpointer data |
| 101 |
) { |
| 102 |
struct rate_buttons *rb = data; |
| 103 |
|
| 104 |
if (!gtk_toggle_button_get_active(button)) |
| 105 |
return; |
| 106 |
|
| 107 |
int idx = GPOINTER_TO_INT( |
| 108 |
g_object_get_data(G_OBJECT(button), "rate-index") |
| 109 |
); |
| 110 |
|
| 111 |
rb->card->pref_levels_interval_ms = levels_rates[idx].ms; |
| 112 |
|
| 113 |
char buf[8]; |
| 114 |
snprintf(buf, sizeof(buf), "%d", levels_rates[idx].hz); |
| 115 |
optional_state_save( |
| 116 |
rb->card, CONFIG_SECTION_UI, "levels-update-rate", buf |
| 117 |
); |
| 118 |
|
| 119 |
restart_levels_timer(rb->card); |
| 120 |
} |
| 121 |
|
| 122 |
static GtkWidget *make_pref_row( |
| 123 |
const char *label_text, |
| 124 |
GtkWidget *control |
| 125 |
) { |
| 126 |
GtkWidget *row = gtk_box_new( |
| 127 |
GTK_ORIENTATION_HORIZONTAL, 10 |
| 128 |
); |
| 129 |
gtk_widget_set_hexpand(row, TRUE); |
| 130 |
|
| 131 |
GtkWidget *label = gtk_label_new(label_text); |
| 132 |
gtk_widget_set_hexpand(label, TRUE); |
| 133 |
gtk_widget_set_halign(label, GTK_ALIGN_START); |
| 134 |
gtk_box_append(GTK_BOX(row), label); |
| 135 |
|
| 136 |
gtk_widget_set_halign(control, GTK_ALIGN_END); |
| 137 |
gtk_box_append(GTK_BOX(row), control); |
| 138 |
|
| 139 |
return row; |
| 140 |
} |
| 141 |
|
| 142 |
static GtkWidget *make_section_label(const char *text) { |
| 143 |
GtkWidget *label = gtk_label_new(NULL); |
| 144 |
char *markup = g_strdup_printf("<b>%s</b>", text); |
| 145 |
gtk_label_set_markup(GTK_LABEL(label), markup); |
| 146 |
g_free(markup); |
| 147 |
gtk_widget_set_halign(label, GTK_ALIGN_START); |
| 148 |
return label; |
| 149 |
} |
| 150 |
|
| 151 |
GtkWidget *create_preferences_controls( |
| 152 |
struct alsa_card *card |
| 153 |
) { |
| 154 |
GtkWidget *top = gtk_frame_new(NULL); |
| 155 |
gtk_widget_add_css_class(top, "window-frame"); |
| 156 |
|
| 157 |
GtkWidget *content = gtk_box_new( |
| 158 |
GTK_ORIENTATION_VERTICAL, 10 |
| 159 |
); |
| 160 |
gtk_widget_add_css_class(content, "window-content"); |
| 161 |
gtk_widget_add_css_class(content, "top-level-content"); |
| 162 |
gtk_frame_set_child(GTK_FRAME(top), content); |
| 163 |
|
| 164 |
|
| 165 |
gtk_box_append( |
| 166 |
GTK_BOX(content), make_section_label("Mixer") |
| 167 |
); |
| 168 |
|
| 169 |
GtkWidget *sw = gtk_switch_new(); |
| 170 |
gtk_switch_set_active( |
| 171 |
GTK_SWITCH(sw), card->pref_show_bottom_right_labels |
| 172 |
); |
| 173 |
g_signal_connect( |
| 174 |
sw, "notify::active", |
| 175 |
G_CALLBACK(on_bottom_right_labels_changed), card |
| 176 |
); |
| 177 |
gtk_box_append( |
| 178 |
GTK_BOX(content), |
| 179 |
make_pref_row("Show Bottom/Right Labels", sw) |
| 180 |
); |
| 181 |
|
| 182 |
|
| 183 |
if (card->has_levels) { |
| 184 |
gtk_box_append( |
| 185 |
GTK_BOX(content), make_section_label("Levels") |
| 186 |
); |
| 187 |
|
| 188 |
struct rate_buttons *rb = g_malloc0( |
| 189 |
sizeof(struct rate_buttons) |
| 190 |
); |
| 191 |
rb->card = card; |
| 192 |
|
| 193 |
int active = ms_to_rate_index( |
| 194 |
card->pref_levels_interval_ms |
| 195 |
); |
| 196 |
|
| 197 |
GtkWidget *rate_box = gtk_box_new( |
| 198 |
GTK_ORIENTATION_HORIZONTAL, 5 |
| 199 |
); |
| 200 |
gtk_widget_set_halign(rate_box, GTK_ALIGN_END); |
| 201 |
|
| 202 |
for (int i = 0; i < LEVELS_RATE_COUNT; i++) { |
| 203 |
GtkWidget *btn = gtk_toggle_button_new_with_label( |
| 204 |
levels_rates[i].label |
| 205 |
); |
| 206 |
gtk_toggle_button_set_active( |
| 207 |
GTK_TOGGLE_BUTTON(btn), i == active |
| 208 |
); |
| 209 |
if (i > 0) |
| 210 |
gtk_toggle_button_set_group( |
| 211 |
GTK_TOGGLE_BUTTON(btn), |
| 212 |
GTK_TOGGLE_BUTTON(rb->buttons[0]) |
| 213 |
); |
| 214 |
|
| 215 |
g_object_set_data( |
| 216 |
G_OBJECT(btn), "rate-index", |
| 217 |
GINT_TO_POINTER(i) |
| 218 |
); |
| 219 |
g_signal_connect( |
| 220 |
btn, "toggled", |
| 221 |
G_CALLBACK(on_rate_button_toggled), rb |
| 222 |
); |
| 223 |
|
| 224 |
rb->buttons[i] = btn; |
| 225 |
gtk_box_append(GTK_BOX(rate_box), btn); |
| 226 |
} |
| 227 |
|
| 228 |
gtk_box_append( |
| 229 |
GTK_BOX(content), |
| 230 |
make_pref_row("Update Rate", rate_box) |
| 231 |
); |
| 232 |
|
| 233 |
g_object_weak_ref( |
| 234 |
G_OBJECT(top), (GWeakNotify)g_free, rb |
| 235 |
); |
| 236 |
} |
| 237 |
|
| 238 |
return top; |
| 239 |
} |