From 35e42dc65351ae4079338f915ce75c0baa868f80 Mon Sep 17 00:00:00 2001 From: Tim LaBerge Date: Mon, 26 Mar 2012 16:17:03 +0200 Subject: [PATCH] Detect DigiTech devices on startup. If more than one device is found, ask user which one to use. --- TODO | 1 - gdigi.c | 29 +++++++++++++++++++---------- gui.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ gui.h | 1 + 4 files changed, 73 insertions(+), 11 deletions(-) diff --git a/TODO b/TODO index 6a1e154..0dee0f4 100644 --- a/TODO +++ b/TODO @@ -2,5 +2,4 @@ -handling presets (saving, exporting to xml patches) -buildsystem (install knob.png to share dir, don't use inline knob pixbuf) -various fixes to MIDI IN messages handling --ask user which device to use if more than one DigiTech device is found -optimizations diff --git a/gdigi.c b/gdigi.c index 7fa2647..0275adc 100644 --- a/gdigi.c +++ b/gdigi.c @@ -1058,6 +1058,8 @@ static gboolean request_who_am_i(unsigned char *device_id, unsigned char *family *device_id = data->str[8]; *family_id = data->str[9]; *product_id = data->str[10]; + g_message("I am device id %d family %d product id %d.", + *device_id, *family_id, *product_id); g_string_free(data, TRUE); return TRUE; } @@ -1122,18 +1124,14 @@ static gint get_digitech_devices(GList **devices) { gint card_num = -1; gint number = 0; - snd_card_next(&card_num); - while (card_num > -1) { + while (!snd_card_next(&card_num) && (card_num > -1)) { char* name; snd_card_get_longname(card_num, &name); - gint count = strspn(name,"DigiTech"); - if (count > 0) - { + if (strspn(name,"DigiTech") > 0) { number++; *devices = g_list_append(*devices, GINT_TO_POINTER(card_num)); } - snd_card_next(&card_num); } return number; @@ -1161,13 +1159,24 @@ int main(int argc, char *argv[]) { if (device_port == NULL) { /* port not given explicitly in commandline - search for devices */ - GList *devices = NULL; - if (get_digitech_devices(&devices) <= 0) { - g_message("Couldn't find DigiTech devices!"); + GList *devices = NULL; + GList *device = NULL; + int num_devices = 0; + int chosen_device = 0; + if ((num_devices = get_digitech_devices(&devices)) <= 0) { + g_message("Couldn't find any DigiTech devices!"); exit(EXIT_FAILURE); } + if (num_devices > 1) { + chosen_device = select_device_dialog(devices); + if (chosen_device < 0) { + show_error_message(NULL, "No device chosen"); + exit(EXIT_FAILURE); + } + } + device = g_list_nth(devices, chosen_device); device_port = g_strdup_printf("hw:%d,0,0", - GPOINTER_TO_INT(devices->data)); + GPOINTER_TO_INT(device->data)); g_list_free(devices); g_message("Found device %s", device_port); } else { diff --git a/gui.c b/gui.c index 923f2c1..8709fad 100644 --- a/gui.c +++ b/gui.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "gdigi.h" #include "gui.h" #include "effects.h" @@ -1339,3 +1340,55 @@ gboolean unsupported_device_dialog(Device **device) gtk_widget_destroy(dialog); return FALSE; } + +/** + * \param devices List containing the available Digitech devices. + * + * Displays dialogbox for choosing a device. + * + * \return Index of the selected device or -1 on failure. + **/ +gint select_device_dialog (GList *devices) +{ + + GtkWidget *dialog; + GtkWidget *label; + GtkWidget *combo_box; + GtkWidget *vbox; + GList *device; + + dialog = gtk_dialog_new_with_buttons("Select Digitech device", + NULL, GTK_DIALOG_MODAL, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, + NULL); + + vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); + + label = gtk_label_new("You have multiple Digitech devices, select one.\n"); + gtk_container_add(GTK_CONTAINER(vbox), label); + + combo_box = gtk_combo_box_text_new(); + device = g_list_first(devices); + do { + char *name; + + snd_card_get_longname(GPOINTER_TO_INT(device->data), &name); + gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(combo_box), NULL, name); + + } while ((device = g_list_next(device))); + + gtk_container_add(GTK_CONTAINER(vbox), combo_box); + + gtk_widget_show_all(vbox); + if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { + gint number = gtk_combo_box_get_active(GTK_COMBO_BOX(combo_box)); + if (number != -1) { + gtk_widget_destroy(dialog); + return (number); + } + } + + gtk_widget_destroy(dialog); + return -1; +} diff --git a/gui.h b/gui.h index 99e95f7..5555227 100644 --- a/gui.h +++ b/gui.h @@ -28,5 +28,6 @@ gboolean apply_current_preset_to_gui(gpointer data); void gui_create(Device *device); void gui_free(); gboolean unsupported_device_dialog(Device **device); +gint select_device_dialog (GList *devices); #endif /* GDIGI_GUI_H */