| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include "gtkhelper.h" |
| 5 |
#include "widget-boolean.h" |
| 6 |
|
| 7 |
struct boolean { |
| 8 |
struct alsa_elem *elem; |
| 9 |
int backwards; |
| 10 |
GtkWidget *button; |
| 11 |
guint source; |
| 12 |
const char *text[2]; |
| 13 |
GtkWidget *icons[2]; |
| 14 |
}; |
| 15 |
|
| 16 |
static void button_clicked(GtkWidget *widget, struct boolean *data) { |
| 17 |
int value = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)); |
| 18 |
|
| 19 |
alsa_set_elem_value(data->elem, value ^ data->backwards); |
| 20 |
} |
| 21 |
|
| 22 |
static void toggle_button_set_text(struct boolean *data, int value) { |
| 23 |
const char *text = data->text[value]; |
| 24 |
|
| 25 |
if (!text) |
| 26 |
return; |
| 27 |
|
| 28 |
if (*text == '*') |
| 29 |
gtk_button_set_child(GTK_BUTTON(data->button), data->icons[value]); |
| 30 |
else |
| 31 |
gtk_button_set_label(GTK_BUTTON(data->button), text); |
| 32 |
} |
| 33 |
|
| 34 |
static void toggle_button_updated( |
| 35 |
struct alsa_elem *elem, |
| 36 |
void *private |
| 37 |
) { |
| 38 |
struct boolean *data = private; |
| 39 |
|
| 40 |
int is_writable = alsa_get_elem_writable(elem); |
| 41 |
gtk_widget_set_sensitive(data->button, is_writable); |
| 42 |
|
| 43 |
int value = !!alsa_get_elem_value(elem) ^ data->backwards; |
| 44 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(data->button), value); |
| 45 |
|
| 46 |
toggle_button_set_text(data, value); |
| 47 |
} |
| 48 |
|
| 49 |
static gboolean update_toggle_button(struct boolean *data) { |
| 50 |
toggle_button_updated(data->elem, data); |
| 51 |
|
| 52 |
return G_SOURCE_CONTINUE; |
| 53 |
} |
| 54 |
|
| 55 |
static void on_destroy(struct boolean *data) { |
| 56 |
if (data->source) |
| 57 |
g_source_remove(data->source); |
| 58 |
|
| 59 |
for (int i = 0; i < 2; i++) { |
| 60 |
if (data->icons[i]) |
| 61 |
g_object_unref(data->icons[i]); |
| 62 |
g_free((char *)data->text[i]); |
| 63 |
} |
| 64 |
|
| 65 |
g_free(data); |
| 66 |
} |
| 67 |
|
| 68 |
static void load_icons(struct boolean *data) { |
| 69 |
for (int i = 0; i < 2; i++) |
| 70 |
if (data->text[i] && *data->text[i] == '*') { |
| 71 |
char *path = g_strdup_printf( |
| 72 |
"/vu/b4/alsa-scarlett-gui/icons/%s.svg", data->text[i] + 1 |
| 73 |
); |
| 74 |
data->icons[i] = gtk_image_new_from_resource(path); |
| 75 |
gtk_widget_set_align(data->icons[i], GTK_ALIGN_CENTER, GTK_ALIGN_CENTER); |
| 76 |
g_object_ref(data->icons[i]); |
| 77 |
g_free(path); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
GtkWidget *make_boolean_alsa_elem( |
| 82 |
struct alsa_elem *elem, |
| 83 |
const char *disabled_text, |
| 84 |
const char *enabled_text |
| 85 |
) { |
| 86 |
struct boolean *data = g_malloc0(sizeof(struct boolean)); |
| 87 |
data->elem = elem; |
| 88 |
data->button = gtk_toggle_button_new(); |
| 89 |
|
| 90 |
if (strncmp(elem->name, "Master", 6) == 0 && |
| 91 |
strstr(elem->name, "Playback Switch")) |
| 92 |
data->backwards = 1; |
| 93 |
|
| 94 |
g_signal_connect( |
| 95 |
data->button, "clicked", G_CALLBACK(button_clicked), data |
| 96 |
); |
| 97 |
alsa_elem_add_callback(elem, toggle_button_updated, data, NULL); |
| 98 |
data->text[0] = g_strdup(disabled_text); |
| 99 |
data->text[1] = g_strdup(enabled_text); |
| 100 |
load_icons(data); |
| 101 |
|
| 102 |
|
| 103 |
int max_width = 0, max_height = 0; |
| 104 |
for (int i = 0; i < 2; i++) { |
| 105 |
toggle_button_set_text(data, i); |
| 106 |
|
| 107 |
GtkRequisition *size = gtk_requisition_new(); |
| 108 |
gtk_widget_get_preferred_size(data->button, size, NULL); |
| 109 |
|
| 110 |
if (size->width > max_width) |
| 111 |
max_width = size->width; |
| 112 |
if (size->height > max_height) |
| 113 |
max_height = size->height; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
gtk_widget_set_size_request(data->button, max_width, max_height); |
| 119 |
|
| 120 |
toggle_button_updated(elem, data); |
| 121 |
|
| 122 |
|
| 123 |
if (alsa_get_elem_volatile(elem)) |
| 124 |
data->source = |
| 125 |
g_timeout_add_seconds(1, (GSourceFunc)update_toggle_button, data); |
| 126 |
|
| 127 |
g_object_weak_ref(G_OBJECT(data->button), (GWeakNotify)on_destroy, data); |
| 128 |
|
| 129 |
|
| 130 |
g_object_set_data(G_OBJECT(data->button), "boolean_data", data); |
| 131 |
|
| 132 |
return data->button; |
| 133 |
} |
| 134 |
|
| 135 |
void boolean_widget_update_labels( |
| 136 |
GtkWidget *widget, |
| 137 |
const char *disabled_text, |
| 138 |
const char *enabled_text |
| 139 |
) { |
| 140 |
if (!widget) |
| 141 |
return; |
| 142 |
|
| 143 |
struct boolean *data = g_object_get_data(G_OBJECT(widget), "boolean_data"); |
| 144 |
if (!data) |
| 145 |
return; |
| 146 |
|
| 147 |
|
| 148 |
g_free((char *)data->text[0]); |
| 149 |
g_free((char *)data->text[1]); |
| 150 |
data->text[0] = g_strdup(disabled_text); |
| 151 |
data->text[1] = g_strdup(enabled_text); |
| 152 |
|
| 153 |
|
| 154 |
int value = !!alsa_get_elem_value(data->elem) ^ data->backwards; |
| 155 |
toggle_button_set_text(data, value); |
| 156 |
} |