]> git.meshlink.io Git - catta/blobdiff - avahi-discover-standalone/main.c
Replace libglade with GtkBuilder
[catta] / avahi-discover-standalone / main.c
index c6e9cc47e1a43bec4c919fac9c9e630f5c68ee88..6f5a65797985a95382a0ecb631b9950feadc8190 100644 (file)
 
 #include <sys/ioctl.h>
 #include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
 #include <net/if.h>
 #include <unistd.h>
 
 #include <gtk/gtk.h>
-#include <glade/glade.h>
+
 #include <avahi-core/core.h>
 #include <avahi-core/lookup.h>
+
 #include <avahi-common/strlst.h>
 #include <avahi-common/domain.h>
+#include <avahi-common/error.h>
+
 #include <avahi-glib/glib-watch.h>
 #include <avahi-glib/glib-malloc.h>
 
@@ -68,34 +73,6 @@ static GHashTable *service_type_hash_table = NULL;
 static AvahiSServiceResolver *service_resolver = NULL;
 static struct Service *current_service = NULL;
 
-static const char* getifname(int idx) {
-    static char t[256];
-    static struct ifreq ifreq;
-    int fd = -1;
-
-    memset(&ifreq, 0, sizeof(ifreq));
-
-    if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
-        if ((fd = socket(PF_INET6, SOCK_DGRAM, 0)) < 0)
-            goto fail;
-    
-    ifreq.ifr_ifindex = idx;
-    if (ioctl(fd, SIOCGIFNAME, &ifreq) < 0)
-        goto fail;
-
-    close(fd);
-    
-    return ifreq.ifr_name;
-
-    
-fail:
-    if (fd >= 0)
-        close(fd);
-
-    snprintf(t, sizeof(t), "#%i", idx);
-    return t;
-}
-
 static struct Service *get_service(const gchar *service_type, const gchar *service_name, const gchar*domain_name, AvahiIfIndex interface, AvahiProtocol protocol) {
     struct ServiceType *st;
     GList *l;
@@ -148,21 +125,22 @@ static void free_service(struct Service *s) {
 }
 
 static void service_browser_callback(
-    AvahiSServiceBrowser *b,
+    AVAHI_GCC_UNUSED AvahiSServiceBrowser *b,
     AvahiIfIndex interface,
     AvahiProtocol protocol,
     AvahiBrowserEvent event,
     const char *service_name,
     const char *service_type,
     const char *domain_name,
-    AvahiLookupResultFlags flags,
-    void* userdata) {
+    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
+    AVAHI_GCC_UNUSED void* userdata) {
 
     if (event == AVAHI_BROWSER_NEW) {
         struct Service *s;
         GtkTreeIter iter, piter;
         GtkTreePath *path, *ppath;
         gchar iface[256];
+       char name[IF_NAMESIZE];
         
         s = g_new(struct Service, 1);
         s->service_name = g_strdup(service_name);
@@ -177,7 +155,7 @@ static void service_browser_callback(
         ppath = gtk_tree_row_reference_get_path(s->service_type->tree_ref);
         gtk_tree_model_get_iter(GTK_TREE_MODEL(tree_store), &piter, ppath);
 
-        snprintf(iface, sizeof(iface), "%s %s", getifname(interface), avahi_proto_to_string(protocol));
+        snprintf(iface, sizeof(iface), "%s %s", if_indextoname(interface, name), avahi_proto_to_string(protocol));
 
         gtk_tree_store_append(tree_store, &iter, &piter);
         gtk_tree_store_set(tree_store, &iter, 0, s->service_name, 1, iface, 2, s, -1);
@@ -199,14 +177,14 @@ static void service_browser_callback(
 }
 
 static void service_type_browser_callback(
-    AvahiSServiceTypeBrowser *b,
-    AvahiIfIndex interface,
-    AvahiProtocol protocol,
+    AVAHI_GCC_UNUSED AvahiSServiceTypeBrowser *b,
+    AVAHI_GCC_UNUSED AvahiIfIndex interface,
+    AVAHI_GCC_UNUSED AvahiProtocol protocol,
     AvahiBrowserEvent event,
     const char *service_type,
     const char *domain,
-    AvahiLookupResultFlags flags,
-    void * userdata) {
+    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
+    AVAHI_GCC_UNUSED void * userdata) {
     
     struct ServiceType *st;
     GtkTreePath *path;
@@ -236,9 +214,10 @@ static void service_type_browser_callback(
 
 static void update_label(struct Service *s, const gchar *hostname, const AvahiAddress *a, guint16 port, AvahiStringList *txt) {
     gchar t[512], address[64], *txt_s;
+    char name[IF_NAMESIZE];
 
     if (a && hostname) {
-        char na[256];
+        char na[AVAHI_ADDRESS_STR_MAX];
         avahi_address_snprint(na, sizeof(na), a);
         snprintf(address, sizeof(address), "%s/%s:%u", hostname, na, port);
 
@@ -261,7 +240,7 @@ static void update_label(struct Service *s, const gchar *hostname, const AvahiAd
              s->service_type->service_type,
              s->service_name,
              s->domain_name,
-             getifname(s->interface), avahi_proto_to_string(s->protocol),
+             if_indextoname(s->interface,name), avahi_proto_to_string(s->protocol),
              address,
              txt_s);
 
@@ -289,17 +268,17 @@ static struct Service *get_service_on_cursor(void) {
 
 static void service_resolver_callback(
     AvahiSServiceResolver *r,
-    AvahiIfIndex interface,
-    AvahiProtocol protocol,
+    AVAHI_GCC_UNUSED AvahiIfIndex interface,
+    AVAHI_GCC_UNUSED AvahiProtocol protocol,
     AvahiResolverEvent event,
-    const char *name,
-    const char *type,
-    const char *domain,
+    AVAHI_GCC_UNUSED const char *name,
+    AVAHI_GCC_UNUSED const char *type,
+    AVAHI_GCC_UNUSED const char *domain,
     const char *host_name,
     const AvahiAddress *a,
     uint16_t port,
     AvahiStringList *txt,
-    AvahiLookupResultFlags flags, 
+    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags, 
     void* userdata) {
     
     struct Service *s;
@@ -312,17 +291,15 @@ static void service_resolver_callback(
         return;
     }
 
-    if (event == AVAHI_RESOLVER_TIMEOUT)
-        gtk_label_set_markup(info_label, "<i>Failed to resolve: Timeout.</i>");
-    else if (event == AVAHI_RESOLVER_FAILURE)
-        gtk_label_set_markup(info_label, "<i>Failed to resolve: Failure.</i>");
-    else if (event == AVAHI_RESOLVER_NOT_FOUND)
-        gtk_label_set_markup(info_label, "<i>Failed to resolve: Not found.</i>");
-    else if (event == AVAHI_RESOLVER_FOUND)
+    if (event == AVAHI_RESOLVER_FAILURE) {
+        char t[256];
+        snprintf(t, sizeof(t), "<i>Failed to resolve: %s.</i>", avahi_strerror(avahi_server_errno(server)));
+        gtk_label_set_markup(info_label, t);
+    } else if (event == AVAHI_RESOLVER_FOUND)
         update_label(s, host_name, a, port, txt);
 }
 
-static void tree_view_on_cursor_changed(GtkTreeView *tv, gpointer userdata) {
+static void tree_view_on_cursor_changed(AVAHI_GCC_UNUSED GtkTreeView *tv, AVAHI_GCC_UNUSED gpointer userdata) {
     struct Service *s;
     
     if (!(s = get_service_on_cursor()))
@@ -336,33 +313,33 @@ static void tree_view_on_cursor_changed(GtkTreeView *tv, gpointer userdata) {
     service_resolver = avahi_s_service_resolver_new(server, s->interface, s->protocol, s->service_name, s->service_type->service_type, s->domain_name, AVAHI_PROTO_UNSPEC, 0, service_resolver_callback, s);
 }
 
-static gboolean main_window_on_delete_event(GtkWidget *widget, GdkEvent *event, gpointer user_data) {
+static gboolean main_window_on_delete_event(AVAHI_GCC_UNUSED GtkWidget *widget, AVAHI_GCC_UNUSED GdkEvent *event, AVAHI_GCC_UNUSED gpointer user_data) {
     gtk_main_quit();
     return FALSE;
 }
 
 int main(int argc, char *argv[]) {
-    GladeXML *xml;
+    GtkBuilder *ui;
     AvahiServerConfig config;
     GtkTreeViewColumn *c;
     gint error;
     AvahiGLibPoll *poll_api;
 
     gtk_init(&argc, &argv);
-    glade_init();
 
     avahi_set_allocator(avahi_glib_allocator());
 
     poll_api = avahi_glib_poll_new(NULL, G_PRIORITY_DEFAULT);
 
-    xml = glade_xml_new(AVAHI_INTERFACES_DIR"avahi-discover.glade", NULL, NULL);
-    main_window = glade_xml_get_widget(xml, "main_window");
+    ui = gtk_builder_new();
+    gtk_builder_add_from_file(ui, AVAHI_INTERFACES_DIR"avahi-discover.ui", NULL);
+    main_window = GTK_WIDGET(gtk_builder_get_object(ui, "main_window"));
     g_signal_connect(main_window, "delete-event", (GCallback) main_window_on_delete_event, NULL);
-    
-    tree_view = GTK_TREE_VIEW(glade_xml_get_widget(xml, "tree_view"));
+
+    tree_view = GTK_TREE_VIEW(gtk_builder_get_object(ui, "tree_view"));
     g_signal_connect(GTK_WIDGET(tree_view), "cursor-changed", (GCallback) tree_view_on_cursor_changed, NULL);
 
-    info_label = GTK_LABEL(glade_xml_get_widget(xml, "info_label"));
+    info_label = GTK_LABEL(gtk_builder_get_object(ui, "info_label"));
 
     tree_store = gtk_tree_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
     gtk_tree_view_set_model(tree_view, GTK_TREE_MODEL(tree_store));