From 34f44418156bb77f54ffff6208f381e767a9236c Mon Sep 17 00:00:00 2001 From: "Brian J. Tarricone" Date: Sun, 4 Oct 2020 04:02:02 -0700 Subject: [PATCH] Populate a combo box with known network interfaces Use getifaddrs() to enumerate all addresses associated with all network interfaces, discarding duplicate interface names, and sorting. For OSes where getifaddrs() is not available, an interface name can still be manually typed in. --- panel-plugin/netload.c | 89 +++++++++++++++++++++++++++--------------- panel-plugin/os.h | 4 ++ 2 files changed, 62 insertions(+), 31 deletions(-) diff --git a/panel-plugin/netload.c b/panel-plugin/netload.c index 43344e2..67888d0 100644 --- a/panel-plugin/netload.c +++ b/panel-plugin/netload.c @@ -109,7 +109,7 @@ typedef struct GtkWidget *update_spinner; /* Network device */ - GtkWidget *net_entry; + GtkWidget *net_combo; /* Maximum */ GtkWidget *max_use_label; @@ -791,7 +791,7 @@ static void monitor_apply_options(t_global_monitor *global) g_free(global->monitor->options.network_device); } global->monitor->options.network_device = - g_strdup(gtk_entry_get_text(GTK_ENTRY(global->monitor->net_entry))); + gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(global->monitor->net_combo)); for( i = 0; i < SUM; i++ ) { @@ -839,22 +839,6 @@ static void max_label_changed(GtkWidget *button, t_global_monitor *global) } -/* ---------------------------------------------------------------------------------------------- */ -static void network_changed(GtkWidget *button, t_global_monitor *global) -{ - if (global->monitor->options.network_device) - { - g_free(global->monitor->options.network_device); - } - - global->monitor->options.network_device = - g_strdup(gtk_entry_get_text(GTK_ENTRY(global->monitor->net_entry))); - - setup_monitor(global, FALSE); - DBG("network_changed"); -} - - /* ---------------------------------------------------------------------------------------------- */ static void label_toggled(GtkWidget *check_button, t_global_monitor *global) { @@ -963,6 +947,48 @@ monitor_dialog_response (GtkWidget *dlg, int response, t_global_monitor *global) monitor_write_config (global->plugin, global); } +static gboolean add_interface(const gchar *name, gpointer ignore, t_global_monitor *global) +{ + gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(global->monitor->net_combo), name); + if (!strcmp(name, global->monitor->options.network_device)) { + GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(global->monitor->net_combo)); + gint n_items = gtk_tree_model_iter_n_children(model, NULL); + gtk_combo_box_set_active(GTK_COMBO_BOX(global->monitor->net_combo), n_items - 1); + } + + return FALSE; +} + +static gint monitor_populate_interface_list(t_global_monitor *global) +{ + gint count = 0; +#if defined(__linux__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) + struct ifaddrs *addrs = NULL, *addr; + + if (getifaddrs(&addrs) == 0) + { + GTree *found_ifaces = g_tree_new((GCompareFunc) strcmp); + + for (addr = addrs; addr != NULL; addr = addr->ifa_next) + { + if (addr->ifa_name != NULL && g_tree_lookup(found_ifaces, addr->ifa_name) == NULL) + { + DBG("Found interface: %s", addr->ifa_name); + g_tree_insert(found_ifaces, addr->ifa_name, GUINT_TO_POINTER(TRUE)); + ++count; + } + } + + g_tree_foreach(found_ifaces, (GTraverseFunc) add_interface, global); + + g_tree_unref(found_ifaces); + freeifaddrs(addrs); + } +#endif /* __linux__ || __OpenBSD__ || __FreeBSD__ || __NetBSD__ */ + + return count; +} + static void monitor_create_options(XfcePanelPlugin *plugin, t_global_monitor *global) { GtkWidget *dlg; @@ -975,7 +1001,7 @@ static void monitor_create_options(XfcePanelPlugin *plugin, t_global_monitor *gl GtkWidget *color_label[SUM]; gint present_data_active; GtkSizeGroup *sg; - gint i; + gint i, n_interfaces; gchar buffer[BUFSIZ]; gchar *color_text[] = { N_("Bar color (i_ncoming):"), @@ -1055,18 +1081,21 @@ static void monitor_create_options(XfcePanelPlugin *plugin, t_global_monitor *gl gtk_box_pack_start(GTK_BOX(net_hbox), GTK_WIDGET(device_label), FALSE, FALSE, 0); - global->monitor->net_entry = gtk_entry_new(); + global->monitor->net_combo = gtk_combo_box_text_new_with_entry(); + n_interfaces = monitor_populate_interface_list(global); gtk_label_set_mnemonic_widget(GTK_LABEL(device_label), - global->monitor->net_entry); - gtk_entry_set_max_length(GTK_ENTRY(global->monitor->net_entry), - MAX_LENGTH); - gtk_entry_set_text(GTK_ENTRY(global->monitor->net_entry), - global->monitor->options.network_device); - gtk_widget_show(global->monitor->opt_entry); - - gtk_box_pack_start(GTK_BOX(net_hbox), GTK_WIDGET(global->monitor->net_entry), + global->monitor->net_combo); + if (gtk_combo_box_get_active(GTK_COMBO_BOX(global->monitor->net_combo)) == -1 && + global->monitor->options.network_device[0] != '\0') + { + gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(global->monitor->net_combo), + global->monitor->options.network_device); + gtk_combo_box_set_active(GTK_COMBO_BOX(global->monitor->net_combo), n_interfaces); + } + gtk_widget_show(global->monitor->net_combo); + gtk_box_pack_start(GTK_BOX(net_hbox), GTK_WIDGET(global->monitor->net_combo), FALSE, FALSE, 0); - + gtk_size_group_add_widget(sg, device_label); gtk_widget_show_all(GTK_WIDGET(net_hbox)); @@ -1267,8 +1296,6 @@ static void monitor_create_options(XfcePanelPlugin *plugin, t_global_monitor *gl G_CALLBACK(label_changed), global); g_signal_connect(GTK_WIDGET(global->monitor->opt_present_data_combobox), "changed", G_CALLBACK(present_data_combobox_changed), global); - g_signal_connect(GTK_WIDGET(global->monitor->net_entry), "activate", - G_CALLBACK(network_changed), global); g_signal_connect(GTK_WIDGET(global->monitor->opt_as_bits), "toggled", G_CALLBACK(as_bits_toggled), global); diff --git a/panel-plugin/os.h b/panel-plugin/os.h index 9a5c544..2e88f59 100644 --- a/panel-plugin/os.h +++ b/panel-plugin/os.h @@ -120,6 +120,7 @@ # include # include # include +# include #elif __NetBSD__ /* N E T B S D */ # include # include @@ -144,6 +145,7 @@ # include # include # include +# include #elif __OpenBSD__ || __MicroBSD__ /* O P E N B S D */ # include # include @@ -167,6 +169,7 @@ # include # include # include +# include #elif __linux__ /* L I N U X */ # include # include @@ -186,6 +189,7 @@ # include # include # include +# include #elif __Solaris__ /* S O L A R I S */ # include # define _WIDEC_H