3 * Copyright 2009 Roy Marples <roy@marples.name>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 /* TODO: Animate the icon from carrier -> address
28 * maybe use network-idle -> network-transmit ->
29 * network-receive -> network-transmit-receive */
35 #include <libnotify/notify.h>
37 #include "dhcpcd-gtk.h"
40 DBusGProxy *dbus = NULL;
41 GSList *interfaces = NULL;
43 static GtkStatusIcon *status_icon;
44 static gint ani_timer;
45 static int ani_counter;
46 static gboolean online;
47 static gboolean carrier;
48 static char **interface_order;
49 static NotifyNotification *nn;
51 const char *const up_reasons[] = {
62 const char *const down_reasons[] = {
72 ignore_if_msg(const struct if_msg *ifm)
74 if (g_strcmp0(ifm->reason, "STOP") == 0 ||
75 g_strcmp0(ifm->reason, "RELEASE") == 0)
80 static struct if_msg *
81 find_if_msg(const char *iface)
86 for (gl = interfaces; gl; gl = gl->next) {
87 ifm = (struct if_msg *)gl->data;
88 if (g_strcmp0(ifm->ifname, iface) == 0)
95 free_if_ap(struct if_ap *ifa)
105 free_if_msg(struct if_msg *ifm)
112 for (gl = ifm->scan_results; gl; gl = gl->next)
113 free_if_ap((struct if_ap *)gl->data);
114 g_slist_free(ifm->scan_results);
119 error_exit(const char *msg, GError *error)
124 g_critical("%s: %s", msg, error->message);
125 dialog = gtk_message_dialog_new(NULL, 0,
126 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
127 "%s: %s", msg, error->message);
129 g_critical("%s", msg);
130 dialog = gtk_message_dialog_new(NULL, 0,
131 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", msg);
133 gtk_dialog_run(GTK_DIALOG(dialog));
134 gtk_widget_destroy(dialog);
135 if (gtk_main_level())
142 get_scan_results(struct if_msg *ifm)
153 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
154 otype = dbus_g_type_get_collection("GPtrArray", otype);
156 if (!dbus_g_proxy_call(dbus, "ScanResults", &error,
157 G_TYPE_STRING, ifm->ifname, G_TYPE_INVALID,
158 otype, &array, G_TYPE_INVALID))
159 error_exit(_("ScanResults"), error);
161 for (i = 0; i < array->len; i++) {
162 config = g_ptr_array_index(array, i);
163 val = g_hash_table_lookup(config, "BSSID");
166 ifa = g_malloc0(sizeof(*ifa));
167 ifa->ifname = g_strdup(ifm->ifname);
168 ifa->bssid = g_strdup(g_value_get_string(val));
169 val = g_hash_table_lookup(config, "Frequency");
171 ifa->frequency = g_value_get_int(val);
172 val = g_hash_table_lookup(config, "Quality");
174 ifa->quality = g_value_get_int(val);
175 val = g_hash_table_lookup(config, "Noise");
177 ifa->noise = g_value_get_int(val);
178 val = g_hash_table_lookup(config, "Level");
180 ifa->level = g_value_get_int(val);
181 val = g_hash_table_lookup(config, "Flags");
183 ifa->flags = g_strdup(g_value_get_string(val));
184 val = g_hash_table_lookup(config, "SSID");
186 ifa->ssid = g_strdup(g_value_get_string(val));
187 list = g_slist_append(list, ifa);
192 static struct if_msg *
193 make_if_msg(GHashTable *config)
198 val = g_hash_table_lookup(config, "Interface");
201 ifm = g_malloc0(sizeof(*ifm));
202 ifm->ifname = g_strdup(g_value_get_string(val));
203 val = g_hash_table_lookup(config, "Reason");
205 ifm->reason = g_strdup(g_value_get_string(val));
206 val = g_hash_table_lookup(config, "Wireless");
208 ifm->wireless = g_value_get_boolean(val);
210 val = g_hash_table_lookup(config, "SSID");
212 ifm->ssid = g_strdup(g_value_get_string(val));
214 val = g_hash_table_lookup(config, "IPAddress");
216 ifm->ip.s_addr = g_value_get_uint(val);
217 val = g_hash_table_lookup(config, "SubnetCIDR");
219 ifm->cidr = g_value_get_uchar(val);
220 val = g_hash_table_lookup(config, "InterfaceOrder");
222 g_strfreev(interface_order);
223 interface_order = g_strsplit(g_value_get_string(val), " ", 0);
229 if_up(const struct if_msg *ifm)
231 const char *const *r;
233 for (r = up_reasons; *r; r++)
234 if (g_strcmp0(*r, ifm->reason) == 0)
240 print_if_msg(const struct if_msg *ifm)
243 const char *reason = NULL;
245 gboolean showip, showssid;
250 reason = N_("Acquired address");
252 if (g_strcmp0(ifm->reason, "EXPIRE") == 0)
253 reason = N_("Failed to renew");
254 else if (g_strcmp0(ifm->reason, "CARRIER") == 0) {
256 reason = N_("Asssociated with");
257 if (ifm->ssid != NULL)
260 reason = N_("Cable plugged in");
262 } else if (g_strcmp0(ifm->reason, "NOCARRIER") == 0) {
264 if (ifm->ssid != NULL || ifm->ip.s_addr != 0) {
265 reason = N_("Disassociated from");
268 reason = N_("Not associated");
270 reason = N_("Cable unplugged");
275 reason = ifm->reason;
277 len = strlen(ifm->ifname) + 3;
278 len += strlen(reason) + 1;
279 if (ifm->ip.s_addr != 0) {
280 len += 16; /* 000. * 4 */
285 len += strlen(ifm->ssid) + 1;
286 msg = p = g_malloc(len);
287 p += g_snprintf(msg, len, "%s: %s", ifm->ifname, reason);
289 p += g_snprintf(p, len - (p - msg), " %s", ifm->ssid);
290 if (ifm->ip.s_addr != 0 && showip) {
291 p += g_snprintf(p, len - (p - msg), " %s", inet_ntoa(ifm->ip));
293 g_snprintf(p, len - (p - msg), "/%d", ifm->cidr);
299 if_msg_comparer(gconstpointer a, gconstpointer b)
301 const struct if_msg *ifa, *ifb;
302 const char *const *order;
304 ifa = (const struct if_msg *)a;
305 ifb = (const struct if_msg *)b;
306 for (order = (const char *const *)interface_order; *order; order++) {
307 if (g_strcmp0(*order, ifa->ifname) == 0)
309 if (g_strcmp0(*order, ifb->ifname) == 0)
316 animate_carrier(_unused gpointer data)
323 switch(ani_counter++) {
325 icon = "network-transmit";
328 icon = "network-receive";
331 icon = "network-idle";
335 gtk_status_icon_set_from_icon_name(status_icon, icon);
340 animate_online(_unused gpointer data)
347 if (ani_counter++ > 6) {
353 if (ani_counter % 2 == 0)
354 icon = "network-idle";
356 icon = "network-transmit-receive";
357 gtk_status_icon_set_from_icon_name(status_icon, icon);
364 gboolean ison, iscarrier;
365 char *msg, *msgs, *tmp;
367 const struct if_msg *ifm;
369 ison = iscarrier = FALSE;
371 for (gl = interfaces; gl; gl = gl->next) {
372 ifm = (const struct if_msg *)gl->data;
374 ison = iscarrier = TRUE;
375 if (!iscarrier && g_strcmp0(ifm->reason, "CARRIER") == 0)
377 msg = print_if_msg(ifm);
379 tmp = g_strconcat(msgs, "\n", msg, NULL);
387 if (online != ison || carrier != iscarrier) {
389 if (ani_timer != 0) {
390 g_source_remove(ani_timer);
395 animate_online(NULL);
396 ani_timer = g_timeout_add(300, animate_online, NULL);
397 } else if (iscarrier) {
398 animate_carrier(NULL);
399 ani_timer = g_timeout_add(500, animate_carrier, NULL);
401 gtk_status_icon_set_from_icon_name(status_icon,
405 gtk_status_icon_set_tooltip(status_icon, msgs);
413 notify_notification_close(nn, NULL);
423 notify(const char *title, const char *msg, const char *icon)
427 msgs = g_strsplit(msg, "\n", 0);
428 for (m = msgs; *m; m++)
432 notify_notification_close(nn, NULL);
433 if (gtk_status_icon_get_visible(status_icon))
434 nn = notify_notification_new_with_status_icon(title,
435 msg, icon, status_icon);
437 nn = notify_notification_new(title, msg, icon, NULL);
438 notify_notification_set_timeout(nn, 5000);
439 g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
440 notify_notification_show(nn, NULL);
444 dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
446 struct if_msg *ifm, *ifp;
450 const char *act, *net;
451 const char *const *r;
454 ifm = make_if_msg(config);
458 rem = ignore_if_msg(ifm);
460 for (gl = interfaces; gl; gl = gl->next) {
461 ifp = (struct if_msg *)gl->data;
462 if (g_strcmp0(ifp->ifname, ifm->ifname) == 0) {
463 ifm->scan_results = ifp->scan_results;
464 ifp->scan_results = NULL;
468 g_slist_delete_link(interfaces, gl);
474 if (ifp == NULL && !rem)
475 interfaces = g_slist_prepend(interfaces, ifm);
476 interfaces = g_slist_sort(interfaces, if_msg_comparer);
479 /* We should ignore renew and stop so we don't annoy the user */
480 if (g_strcmp0(ifm->reason, "RENEW") == 0 ||
481 g_strcmp0(ifm->reason, "STOP") == 0)
484 msg = print_if_msg(ifm);
487 act = N_("Connected to ");
490 for (r = down_reasons; *r; r++) {
491 if (g_strcmp0(*r, ifm->reason) == 0) {
492 act = N_("Disconnected from ");
496 if (act && ifm->ip.s_addr) {
497 ipn = htonl(ifm->ip.s_addr);
498 if (IN_LINKLOCAL(ipn))
499 net = N_("private network");
500 else if (IN_PRIVATE(ipn))
503 net = N_("internet");
504 title = g_strconcat(act, net, NULL);
508 notify(title, msg, GTK_STOCK_NETWORK);
511 notify(N_("Interface event"), msg, GTK_STOCK_NETWORK);
516 foreach_make_ifm(_unused gpointer key, gpointer value, _unused gpointer data)
520 ifm = make_if_msg((GHashTable *)value);
521 if (ignore_if_msg(ifm))
524 interfaces = g_slist_prepend(interfaces, ifm);
528 dhcpcd_get_interfaces()
531 GError *error = NULL;
537 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
538 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
539 if (!dbus_g_proxy_call(dbus, "GetInterfaces", &error,
541 otype, &ifs, G_TYPE_INVALID))
542 error_exit("GetInterfaces", error);
543 g_hash_table_foreach(ifs, foreach_make_ifm, NULL);
544 g_hash_table_unref(ifs);
546 /* Each interface config only remembers the last order when
547 * that interface was configured, so get the real order now. */
548 g_strfreev(interface_order);
549 interface_order = NULL;
550 if (!dbus_g_proxy_call(dbus, "ListInterfaces", &error,
552 G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
553 error_exit("ListInterfaces", error);
554 interfaces = g_slist_sort(interfaces, if_msg_comparer);
556 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
557 otype = dbus_g_type_get_collection("GPtrArray", otype);
558 for (gl = interfaces; gl; gl = gl->next) {
559 ifm = (struct if_msg *)gl->data;
562 if (!dbus_g_proxy_call(dbus, "ScanResults", &error,
563 G_TYPE_STRING, ifm->ifname, G_TYPE_INVALID,
564 otype, &array, G_TYPE_INVALID))
566 g_message("ScanResults: %s", error->message);
567 g_clear_error(&error);
570 for (gsl = ifm->scan_results; gsl; gsl = gsl->next)
572 g_slist_free(ifm->scan_results);
573 ifm->scan_results = get_scan_results(ifm);
580 check_status(const char *status)
582 static char *last = NULL;
587 GError *error = NULL;
589 g_message("Status changed to %s", status);
590 if (g_strcmp0(status, "down") == 0) {
591 for (gl = interfaces; gl; gl = gl->next)
592 free_if_msg((struct if_msg *)gl->data);
593 g_slist_free(interfaces);
597 "Connection to dhcpcd lost" : "dhcpcd not running");
598 gtk_status_icon_set_tooltip(status_icon, msg);
599 notify(_("No network"), msg, GTK_STOCK_NETWORK);
604 if (g_strcmp0(status, "down") != 0)
607 if (g_strcmp0(status, last) == 0)
609 if (g_strcmp0(last, "down") == 0)
613 last = g_strdup(status);
617 if (!dbus_g_proxy_call(dbus, "GetDhcpcdVersion", &error,
619 G_TYPE_STRING, &version, G_TYPE_INVALID))
620 error_exit(_("GetDhcpcdVersion"), error);
621 g_message(_("Connected to %s-%s"), "dhcpcd", version);
623 dhcpcd_get_interfaces();
627 dhcpcd_status(_unused DBusGProxy *proxy, const char *status,
630 check_status(status);
634 dhcpcd_scan_results(_unused DBusGProxy *proxy, const char *iface,
638 struct if_ap *ifa, *ifa2;
639 GSList *gl, *aps, *l;
642 ifm = find_if_msg(iface);
645 g_message(_("%s: Received scan results"), ifm->ifname);
646 aps = get_scan_results(ifm);
648 for (gl = aps; gl; gl = gl->next) {
649 ifa = (struct if_ap *)gl->data;
650 for (l = ifm->scan_results; l; l = l->next) {
651 ifa2 = (struct if_ap *)l->data;
652 if (g_strcmp0(ifa->ssid, ifa2->ssid) == 0)
657 txt = g_strdup(ifa->ssid);
659 ntxt = g_strconcat(txt, "\n", ifa->ssid, NULL);
665 for (gl = ifm->scan_results; gl; gl = gl->next)
666 free_if_ap((struct if_ap *)gl->data);
667 g_slist_free(ifm->scan_results);
668 ifm->scan_results = aps;
670 notify(N_("Found new AP"), txt, GTK_STOCK_NETWORK);
676 main(int argc, char *argv[])
678 DBusGConnection *bus;
679 GError *error = NULL;
680 char *version = NULL;
684 setlocale(LC_ALL, "");
685 bindtextdomain(PACKAGE, NULL);
686 bind_textdomain_codeset(PACKAGE, "UTF-8");
689 gtk_init(&argc, &argv);
690 g_set_application_name("dhcpcd Monitor");
691 status_icon = gtk_status_icon_new_from_icon_name("network-offline");
692 if (status_icon == NULL)
694 gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
695 //network_offline = gtk_status_icon_get_pixbuf(status_icon);
697 gtk_status_icon_set_tooltip(status_icon,
698 _("Connecting to dhcpcd ..."));
699 gtk_status_icon_set_visible(status_icon, TRUE);
701 notify_init(PACKAGE);
703 g_message(_("Connecting to dbus ..."));
704 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
705 if (bus == NULL || error != NULL)
706 error_exit(_("Could not connect to system bus"), error);
707 dbus = dbus_g_proxy_new_for_name(bus,
712 g_message(_("Connecting to dhcpcd-dbus ..."));
713 while (--tries > 0) {
714 g_clear_error(&error);
715 if (dbus_g_proxy_call_with_timeout(dbus, "GetVersion", 500,
716 &error, G_TYPE_INVALID,
717 G_TYPE_STRING, &version, G_TYPE_INVALID))
721 error_exit(_("GetVersion"), error);
722 g_message(_("Connected to %s-%s"), "dhcpcd-dbus", version);
725 gtk_status_icon_set_tooltip(status_icon, _("Triggering dhcpcd ..."));
727 menu_init(status_icon);
729 if (!dbus_g_proxy_call(dbus, "GetStatus", &error,
731 G_TYPE_STRING, &version, G_TYPE_INVALID))
732 error_exit(_("GetStatus"), error);
733 check_status(version);
736 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
737 dbus_g_proxy_add_signal(dbus, "Event",
738 otype, G_TYPE_INVALID);
739 dbus_g_proxy_connect_signal(dbus, "Event",
740 G_CALLBACK(dhcpcd_event), bus, NULL);
741 dbus_g_proxy_add_signal(dbus, "StatusChanged",
742 G_TYPE_STRING, G_TYPE_INVALID);
743 dbus_g_proxy_connect_signal(dbus, "StatusChanged",
744 G_CALLBACK(dhcpcd_status), bus, NULL);
745 dbus_g_proxy_add_signal(dbus, "ScanResults",
746 G_TYPE_STRING, G_TYPE_INVALID);
747 dbus_g_proxy_connect_signal(dbus, "ScanResults",
748 G_CALLBACK(dhcpcd_scan_results), bus, NULL);