| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include "stringhelper.h" |
| 5 |
#include "widget-input-select.h" |
| 6 |
|
| 7 |
struct input_select { |
| 8 |
struct alsa_elem *elem; |
| 9 |
GtkWidget *button; |
| 10 |
int line_num; |
| 11 |
}; |
| 12 |
|
| 13 |
static void input_select_clicked( |
| 14 |
GtkWidget *widget, |
| 15 |
struct input_select *data |
| 16 |
) { |
| 17 |
int count = alsa_get_item_count(data->elem); |
| 18 |
|
| 19 |
|
| 20 |
for (int i = 0; i < count; i++) { |
| 21 |
const char *text = alsa_get_item_name(data->elem, i); |
| 22 |
int a, b; |
| 23 |
get_two_num_from_string(text, &a, &b); |
| 24 |
|
| 25 |
if ((b == -1 && a == data->line_num) || |
| 26 |
(a <= data->line_num && b >= data->line_num)) { |
| 27 |
alsa_set_elem_value(data->elem, i); |
| 28 |
break; |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
static void input_select_updated( |
| 34 |
struct alsa_elem *elem, |
| 35 |
void *private |
| 36 |
) { |
| 37 |
struct input_select *data = private; |
| 38 |
int line_num = data->line_num; |
| 39 |
int is_writable = alsa_get_elem_writable(elem); |
| 40 |
|
| 41 |
int value = alsa_get_elem_value(elem); |
| 42 |
const char *text = alsa_get_item_name(elem, value); |
| 43 |
|
| 44 |
int a, b; |
| 45 |
get_two_num_from_string(text, &a, &b); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
int active = b == -1 |
| 50 |
? a == line_num |
| 51 |
: a <= line_num && b >= line_num; |
| 52 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(data->button), active); |
| 53 |
gtk_widget_set_sensitive(data->button, !active && is_writable); |
| 54 |
} |
| 55 |
|
| 56 |
GtkWidget *make_input_select_alsa_elem( |
| 57 |
struct alsa_elem *elem, |
| 58 |
int line_num |
| 59 |
) { |
| 60 |
struct input_select *data = malloc(sizeof(struct input_select)); |
| 61 |
data->elem = elem; |
| 62 |
data->button = gtk_toggle_button_new(); |
| 63 |
data->line_num = line_num; |
| 64 |
|
| 65 |
gtk_widget_add_css_class(data->button, "input-select"); |
| 66 |
|
| 67 |
char s[20]; |
| 68 |
snprintf(s, 20, "%d", line_num); |
| 69 |
gtk_button_set_label(GTK_BUTTON(data->button), s); |
| 70 |
|
| 71 |
g_signal_connect( |
| 72 |
data->button, "clicked", G_CALLBACK(input_select_clicked), data |
| 73 |
); |
| 74 |
alsa_elem_add_callback(elem, input_select_updated, data, NULL); |
| 75 |
|
| 76 |
input_select_updated(elem, data); |
| 77 |
|
| 78 |
return data->button; |
| 79 |
} |