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);
157 if (!dbus_g_proxy_call(dbus, "ScanResults", &error,
158 G_TYPE_STRING, ifm->ifname, G_TYPE_INVALID,
159 otype, &array, G_TYPE_INVALID))
160 error_exit(_("ScanResults"), error);
162 for (i = 0; i < array->len; i++) {
163 config = g_ptr_array_index(array, i);
164 val = g_hash_table_lookup(config, "BSSID");
167 ifa = g_malloc0(sizeof(*ifa));
168 ifa->ifname = g_strdup(ifm->ifname);
169 ifa->bssid = g_strdup(g_value_get_string(val));
170 val = g_hash_table_lookup(config, "Frequency");
172 ifa->frequency = g_value_get_int(val);
173 val = g_hash_table_lookup(config, "Quality");
175 ifa->quality = g_value_get_int(val);
176 val = g_hash_table_lookup(config, "Noise");
178 ifa->noise = g_value_get_int(val);
179 val = g_hash_table_lookup(config, "Level");
181 ifa->level = g_value_get_int(val);
182 val = g_hash_table_lookup(config, "Flags");
184 ifa->flags = g_strdup(g_value_get_string(val));
185 val = g_hash_table_lookup(config, "SSID");
187 ifa->ssid = g_strdup(g_value_get_string(val));
188 list = g_slist_append(list, ifa);
193 static struct if_msg *
194 make_if_msg(GHashTable *config)
199 val = g_hash_table_lookup(config, "Interface");
202 ifm = g_malloc0(sizeof(*ifm));
203 ifm->ifname = g_strdup(g_value_get_string(val));
204 val = g_hash_table_lookup(config, "Reason");
206 ifm->reason = g_strdup(g_value_get_string(val));
207 val = g_hash_table_lookup(config, "Wireless");
209 ifm->wireless = g_value_get_boolean(val);
211 val = g_hash_table_lookup(config, "SSID");
213 ifm->ssid = g_strdup(g_value_get_string(val));
215 val = g_hash_table_lookup(config, "IPAddress");
217 ifm->ip.s_addr = g_value_get_uint(val);
218 val = g_hash_table_lookup(config, "SubnetCIDR");
220 ifm->cidr = g_value_get_uchar(val);
221 val = g_hash_table_lookup(config, "InterfaceOrder");
223 g_strfreev(interface_order);
224 interface_order = g_strsplit(g_value_get_string(val), " ", 0);
230 if_up(const struct if_msg *ifm)
232 const char *const *r;
234 for (r = up_reasons; *r; r++)
235 if (g_strcmp0(*r, ifm->reason) == 0)
241 print_if_msg(const struct if_msg *ifm)
244 const char *reason = NULL;
246 gboolean showip, showssid;
251 reason = N_("Acquired address");
253 if (g_strcmp0(ifm->reason, "EXPIRE") == 0)
254 reason = N_("Failed to renew");
255 else if (g_strcmp0(ifm->reason, "CARRIER") == 0) {
257 reason = N_("Asssociated with");
258 if (ifm->ssid != NULL)
261 reason = N_("Cable plugged in");
263 } else if (g_strcmp0(ifm->reason, "NOCARRIER") == 0) {
265 if (ifm->ssid != NULL || ifm->ip.s_addr != 0) {
266 reason = N_("Disassociated from");
269 reason = N_("Not associated");
271 reason = N_("Cable unplugged");
276 reason = ifm->reason;
278 len = strlen(ifm->ifname) + 3;
279 len += strlen(reason) + 1;
280 if (ifm->ip.s_addr != 0) {
281 len += 16; /* 000. * 4 */
286 len += strlen(ifm->ssid) + 1;
287 msg = p = g_malloc(len);
288 p += g_snprintf(msg, len, "%s: %s", ifm->ifname, reason);
290 p += g_snprintf(p, len - (p - msg), " %s", ifm->ssid);
291 if (ifm->ip.s_addr != 0 && showip) {
292 p += g_snprintf(p, len - (p - msg), " %s", inet_ntoa(ifm->ip));
294 g_snprintf(p, len - (p - msg), "/%d", ifm->cidr);
300 if_msg_comparer(gconstpointer a, gconstpointer b)
302 const struct if_msg *ifa, *ifb;
303 const char *const *order;
305 ifa = (const struct if_msg *)a;
306 ifb = (const struct if_msg *)b;
307 for (order = (const char *const *)interface_order; *order; order++) {
308 if (g_strcmp0(*order, ifa->ifname) == 0)
310 if (g_strcmp0(*order, ifb->ifname) == 0)
317 animate_carrier(_unused gpointer data)
324 switch(ani_counter++) {
326 icon = "network-transmit";
329 icon = "network-receive";
332 icon = "network-idle";
336 gtk_status_icon_set_from_icon_name(status_icon, icon);
341 animate_online(_unused gpointer data)
348 if (ani_counter++ > 6) {
354 if (ani_counter % 2 == 0)
355 icon = "network-idle";
357 icon = "network-transmit-receive";
358 gtk_status_icon_set_from_icon_name(status_icon, icon);
365 gboolean ison, iscarrier;
366 char *msg, *msgs, *tmp;
368 const struct if_msg *ifm;
370 ison = iscarrier = FALSE;
372 for (gl = interfaces; gl; gl = gl->next) {
373 ifm = (const struct if_msg *)gl->data;
375 ison = iscarrier = TRUE;
376 if (!iscarrier && g_strcmp0(ifm->reason, "CARRIER") == 0)
378 msg = print_if_msg(ifm);
380 tmp = g_strconcat(msgs, "\n", msg, NULL);
388 if (online != ison || carrier != iscarrier) {
390 if (ani_timer != 0) {
391 g_source_remove(ani_timer);
396 animate_online(NULL);
397 ani_timer = g_timeout_add(300, animate_online, NULL);
398 } else if (iscarrier) {
399 animate_carrier(NULL);
400 ani_timer = g_timeout_add(500, animate_carrier, NULL);
402 gtk_status_icon_set_from_icon_name(status_icon,
406 gtk_status_icon_set_tooltip(status_icon, msgs);
414 notify_notification_close(nn, NULL);
424 notify(const char *title, const char *msg, const char *icon)
428 msgs = g_strsplit(msg, "\n", 0);
429 for (m = msgs; *m; m++)
433 notify_notification_close(nn, NULL);
434 if (gtk_status_icon_get_visible(status_icon))
435 nn = notify_notification_new_with_status_icon(title,
436 msg, icon, status_icon);
438 nn = notify_notification_new(title, msg, icon, NULL);
439 notify_notification_set_timeout(nn, 5000);
440 g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
441 notify_notification_show(nn, NULL);
445 dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
447 struct if_msg *ifm, *ifp;
451 const char *act, *net;
452 const char *const *r;
455 ifm = make_if_msg(config);
459 rem = ignore_if_msg(ifm);
461 for (gl = interfaces; gl; gl = gl->next) {
462 ifp = (struct if_msg *)gl->data;
463 if (g_strcmp0(ifp->ifname, ifm->ifname) == 0) {
464 ifm->scan_results = ifp->scan_results;
465 ifp->scan_results = NULL;
469 g_slist_delete_link(interfaces, gl);
475 if (ifp == NULL && !rem)
476 interfaces = g_slist_prepend(interfaces, ifm);
477 interfaces = g_slist_sort(interfaces, if_msg_comparer);
480 /* We should ignore renew and stop so we don't annoy the user */
481 if (g_strcmp0(ifm->reason, "RENEW") == 0 ||
482 g_strcmp0(ifm->reason, "STOP") == 0)
485 msg = print_if_msg(ifm);
488 act = N_("Connected to ");
491 for (r = down_reasons; *r; r++) {
492 if (g_strcmp0(*r, ifm->reason) == 0) {
493 act = N_("Disconnected from ");
497 if (act && ifm->ip.s_addr) {
498 ipn = htonl(ifm->ip.s_addr);
499 if (IN_LINKLOCAL(ipn))
500 net = N_("private network");
501 else if (IN_PRIVATE(ipn))
504 net = N_("internet");
505 title = g_strconcat(act, net, NULL);
509 notify(title, msg, GTK_STOCK_NETWORK);
512 notify(N_("Interface event"), msg, GTK_STOCK_NETWORK);
517 foreach_make_ifm(_unused gpointer key, gpointer value, _unused gpointer data)
521 ifm = make_if_msg((GHashTable *)value);
522 if (ignore_if_msg(ifm))
525 interfaces = g_slist_prepend(interfaces, ifm);
529 dhcpcd_get_interfaces()
532 GError *error = NULL;
538 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
539 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
540 if (!dbus_g_proxy_call(dbus, "GetInterfaces", &error,
542 otype, &ifs, G_TYPE_INVALID))
543 error_exit("GetInterfaces", error);
544 g_hash_table_foreach(ifs, foreach_make_ifm, NULL);
545 g_hash_table_unref(ifs);
547 /* Each interface config only remembers the last order when
548 * that interface was configured, so get the real order now. */
549 g_strfreev(interface_order);
550 interface_order = NULL;
551 if (!dbus_g_proxy_call(dbus, "ListInterfaces", &error,
553 G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
554 error_exit("ListInterfaces", error);
555 interfaces = g_slist_sort(interfaces, if_msg_comparer);
557 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
558 otype = dbus_g_type_get_collection("GPtrArray", otype);
559 for (gl = interfaces; gl; gl = gl->next) {
560 ifm = (struct if_msg *)gl->data;
563 if (!dbus_g_proxy_call(dbus, "ScanResults", &error,
564 G_TYPE_STRING, ifm->ifname, G_TYPE_INVALID,
565 otype, &array, G_TYPE_INVALID))
567 g_message("ScanResults: %s", error->message);
568 g_clear_error(&error);
571 for (gsl = ifm->scan_results; gsl; gsl = gsl->next)
573 g_slist_free(ifm->scan_results);
574 ifm->scan_results = get_scan_results(ifm);
581 check_status(const char *status)
583 static char *last = NULL;
588 GError *error = NULL;
590 g_message("Status changed to %s", status);
591 if (g_strcmp0(status, "down") == 0) {
592 for (gl = interfaces; gl; gl = gl->next)
593 free_if_msg((struct if_msg *)gl->data);
594 g_slist_free(interfaces);
598 "Connection to dhcpcd lost" : "dhcpcd not running");
599 gtk_status_icon_set_tooltip(status_icon, msg);
600 notify(_("No network"), msg, GTK_STOCK_NETWORK);
605 if (g_strcmp0(status, "down") != 0)
608 if (g_strcmp0(status, last) == 0)
610 if (g_strcmp0(last, "down") == 0)
614 last = g_strdup(status);
618 if (!dbus_g_proxy_call(dbus, "GetDhcpcdVersion", &error,
620 G_TYPE_STRING, &version, G_TYPE_INVALID))
621 error_exit(_("GetDhcpcdVersion"), error);
622 g_message(_("Connected to %s-%s"), "dhcpcd", version);
624 dhcpcd_get_interfaces();
628 dhcpcd_status(_unused DBusGProxy *proxy, const char *status,
631 check_status(status);
635 dhcpcd_scan_results(_unused DBusGProxy *proxy, const char *iface,
639 struct if_ap *ifa, *ifa2;
640 GSList *gl, *aps, *l;
643 ifm = find_if_msg(iface);
646 g_message(_("%s: Received scan results"), ifm->ifname);
647 aps = get_scan_results(ifm);
649 for (gl = aps; gl; gl = gl->next) {
650 ifa = (struct if_ap *)gl->data;
651 for (l = ifm->scan_results; l; l = l->next) {
652 ifa2 = (struct if_ap *)l->data;
653 if (g_strcmp0(ifa->ssid, ifa2->ssid) == 0)
658 txt = g_strdup(ifa->ssid);
660 ntxt = g_strconcat(txt, "\n", ifa->ssid, NULL);
666 for (gl = ifm->scan_results; gl; gl = gl->next)
667 free_if_ap((struct if_ap *)gl->data);
668 g_slist_free(ifm->scan_results);
669 ifm->scan_results = aps;
671 notify(N_("Found new AP"), txt, GTK_STOCK_NETWORK);
677 main(int argc, char *argv[])
679 DBusGConnection *bus;
680 GError *error = NULL;
681 char *version = NULL;
685 setlocale(LC_ALL, "");
686 bindtextdomain(PACKAGE, NULL);
687 bind_textdomain_codeset(PACKAGE, "UTF-8");
690 gtk_init(&argc, &argv);
691 g_set_application_name("dhcpcd Monitor");
692 status_icon = gtk_status_icon_new_from_icon_name("network-offline");
693 if (status_icon == NULL)
695 gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
696 //network_offline = gtk_status_icon_get_pixbuf(status_icon);
698 gtk_status_icon_set_tooltip(status_icon,
699 _("Connecting to dhcpcd ..."));
700 gtk_status_icon_set_visible(status_icon, TRUE);
702 notify_init(PACKAGE);
704 g_message(_("Connecting to dbus ..."));
705 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
706 if (bus == NULL || error != NULL)
707 error_exit(_("Could not connect to system bus"), error);
708 dbus = dbus_g_proxy_new_for_name(bus,
713 g_message(_("Connecting to dhcpcd-dbus ..."));
714 while (--tries > 0) {
715 g_clear_error(&error);
716 if (dbus_g_proxy_call_with_timeout(dbus, "GetVersion", 500,
717 &error, G_TYPE_INVALID,
718 G_TYPE_STRING, &version, G_TYPE_INVALID))
722 error_exit(_("GetVersion"), error);
723 g_message(_("Connected to %s-%s"), "dhcpcd-dbus", version);
726 gtk_status_icon_set_tooltip(status_icon, _("Triggering dhcpcd ..."));
728 menu_init(status_icon);
730 if (!dbus_g_proxy_call(dbus, "GetStatus", &error,
732 G_TYPE_STRING, &version, G_TYPE_INVALID))
733 error_exit(_("GetStatus"), error);
734 check_status(version);
737 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
738 dbus_g_proxy_add_signal(dbus, "Event",
739 otype, G_TYPE_INVALID);
740 dbus_g_proxy_connect_signal(dbus, "Event",
741 G_CALLBACK(dhcpcd_event), bus, NULL);
742 dbus_g_proxy_add_signal(dbus, "StatusChanged",
743 G_TYPE_STRING, G_TYPE_INVALID);
744 dbus_g_proxy_connect_signal(dbus, "StatusChanged",
745 G_CALLBACK(dhcpcd_status), bus, NULL);
746 dbus_g_proxy_add_signal(dbus, "ScanResults",
747 G_TYPE_STRING, G_TYPE_INVALID);
748 dbus_g_proxy_connect_signal(dbus, "ScanResults",
749 G_CALLBACK(dhcpcd_scan_results), bus, NULL);