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;
45 static int ani_counter;
48 static char **interface_order;
49 static NotifyNotification *nn;
51 const char *const up_reasons[] = {
63 const char *const down_reasons[] = {
73 ignore_if_msg(const struct if_msg *ifm)
75 if (g_strcmp0(ifm->reason, "STOP") == 0 ||
76 g_strcmp0(ifm->reason, "RELEASE") == 0)
81 static struct if_msg *
82 find_if_msg(const char *iface)
87 for (gl = interfaces; gl; gl = gl->next) {
88 ifm = (struct if_msg *)gl->data;
89 if (g_strcmp0(ifm->ifname, iface) == 0)
96 free_if_ap(struct if_ap *ifa)
106 free_if_msg(struct if_msg *ifm)
113 for (gl = ifm->scan_results; gl; gl = gl->next)
114 free_if_ap((struct if_ap *)gl->data);
115 g_slist_free(ifm->scan_results);
120 error_exit(const char *msg, GError *error)
125 g_critical("%s: %s", msg, error->message);
126 dialog = gtk_message_dialog_new(NULL, 0,
127 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
128 "%s: %s", msg, error->message);
130 g_critical("%s", msg);
131 dialog = gtk_message_dialog_new(NULL, 0,
132 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", msg);
134 gtk_dialog_run(GTK_DIALOG(dialog));
135 gtk_widget_destroy(dialog);
136 if (gtk_main_level())
143 get_scan_results(struct if_msg *ifm)
154 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
155 otype = dbus_g_type_get_collection("GPtrArray", otype);
158 if (!dbus_g_proxy_call(dbus, "ScanResults", &error,
159 G_TYPE_STRING, ifm->ifname, G_TYPE_INVALID,
160 otype, &array, G_TYPE_INVALID))
161 error_exit(_("ScanResults"), error);
163 for (i = 0; i < array->len; i++) {
164 config = g_ptr_array_index(array, i);
165 val = g_hash_table_lookup(config, "BSSID");
168 ifa = g_malloc0(sizeof(*ifa));
169 ifa->ifname = g_strdup(ifm->ifname);
170 ifa->bssid = g_strdup(g_value_get_string(val));
171 val = g_hash_table_lookup(config, "Frequency");
173 ifa->frequency = g_value_get_int(val);
174 val = g_hash_table_lookup(config, "Quality");
176 ifa->quality = g_value_get_int(val);
177 val = g_hash_table_lookup(config, "Noise");
179 ifa->noise = g_value_get_int(val);
180 val = g_hash_table_lookup(config, "Level");
182 ifa->level = g_value_get_int(val);
183 val = g_hash_table_lookup(config, "Flags");
185 ifa->flags = g_strdup(g_value_get_string(val));
186 val = g_hash_table_lookup(config, "SSID");
188 ifa->ssid = g_strdup(g_value_get_string(val));
189 list = g_slist_append(list, ifa);
194 static struct if_msg *
195 make_if_msg(GHashTable *config)
200 val = g_hash_table_lookup(config, "Interface");
203 ifm = g_malloc0(sizeof(*ifm));
204 ifm->ifname = g_strdup(g_value_get_string(val));
205 val = g_hash_table_lookup(config, "Reason");
207 ifm->reason = g_strdup(g_value_get_string(val));
208 val = g_hash_table_lookup(config, "Wireless");
210 ifm->wireless = g_value_get_boolean(val);
212 val = g_hash_table_lookup(config, "SSID");
214 ifm->ssid = g_strdup(g_value_get_string(val));
216 val = g_hash_table_lookup(config, "IPAddress");
218 ifm->ip.s_addr = g_value_get_uint(val);
219 val = g_hash_table_lookup(config, "SubnetCIDR");
221 ifm->cidr = g_value_get_uchar(val);
222 val = g_hash_table_lookup(config, "InterfaceOrder");
224 g_strfreev(interface_order);
225 interface_order = g_strsplit(g_value_get_string(val), " ", 0);
231 if_up(const struct if_msg *ifm)
233 const char *const *r;
235 for (r = up_reasons; *r; r++)
236 if (g_strcmp0(*r, ifm->reason) == 0)
242 print_if_msg(const struct if_msg *ifm)
245 const char *reason = NULL;
247 bool showip, showssid;
252 reason = N_("Acquired address");
254 if (g_strcmp0(ifm->reason, "EXPIRE") == 0)
255 reason = N_("Failed to renew");
256 else if (g_strcmp0(ifm->reason, "CARRIER") == 0) {
258 reason = N_("Asssociated with");
259 if (ifm->ssid != NULL)
262 reason = N_("Cable plugged in");
264 } else if (g_strcmp0(ifm->reason, "NOCARRIER") == 0) {
266 if (ifm->ssid != NULL || ifm->ip.s_addr != 0) {
267 reason = N_("Disassociated from");
270 reason = N_("Not associated");
272 reason = N_("Cable unplugged");
277 reason = ifm->reason;
279 len = strlen(ifm->ifname) + 3;
280 len += strlen(reason) + 1;
281 if (ifm->ip.s_addr != 0) {
282 len += 16; /* 000. * 4 */
287 len += strlen(ifm->ssid) + 1;
288 msg = p = g_malloc(len);
289 p += g_snprintf(msg, len, "%s: %s", ifm->ifname, reason);
291 p += g_snprintf(p, len - (p - msg), " %s", ifm->ssid);
292 if (ifm->ip.s_addr != 0 && showip) {
293 p += g_snprintf(p, len - (p - msg), " %s", inet_ntoa(ifm->ip));
295 g_snprintf(p, len - (p - msg), "/%d", ifm->cidr);
301 if_msg_comparer(gconstpointer a, gconstpointer b)
303 const struct if_msg *ifa, *ifb;
304 const char *const *order;
306 ifa = (const struct if_msg *)a;
307 ifb = (const struct if_msg *)b;
308 for (order = (const char *const *)interface_order; *order; order++) {
309 if (g_strcmp0(*order, ifa->ifname) == 0)
311 if (g_strcmp0(*order, ifb->ifname) == 0)
318 animate_carrier(_unused gpointer data)
325 switch(ani_counter++) {
327 icon = "network-transmit";
330 icon = "network-receive";
333 icon = "network-idle";
337 gtk_status_icon_set_from_icon_name(status_icon, icon);
342 animate_online(_unused gpointer data)
349 if (ani_counter++ > 6) {
355 if (ani_counter % 2 == 0)
356 icon = "network-idle";
358 icon = "network-transmit-receive";
359 gtk_status_icon_set_from_icon_name(status_icon, icon);
366 bool ison, iscarrier;
367 char *msg, *msgs, *tmp;
369 const struct if_msg *ifm;
371 ison = iscarrier = false;
373 for (gl = interfaces; gl; gl = gl->next) {
374 ifm = (const struct if_msg *)gl->data;
376 ison = iscarrier = true;
377 if (!iscarrier && g_strcmp0(ifm->reason, "CARRIER") == 0)
379 msg = print_if_msg(ifm);
381 tmp = g_strconcat(msgs, "\n", msg, NULL);
389 if (online != ison || carrier != iscarrier) {
391 if (ani_timer != 0) {
392 g_source_remove(ani_timer);
397 animate_online(NULL);
398 ani_timer = g_timeout_add(300, animate_online, NULL);
399 } else if (iscarrier) {
400 animate_carrier(NULL);
401 ani_timer = g_timeout_add(500, animate_carrier, NULL);
403 gtk_status_icon_set_from_icon_name(status_icon,
407 gtk_status_icon_set_tooltip(status_icon, msgs);
415 notify_notification_close(nn, NULL);
425 notify(const char *title, const char *msg, const char *icon)
429 msgs = g_strsplit(msg, "\n", 0);
430 for (m = msgs; *m; m++)
434 notify_notification_close(nn, NULL);
435 if (gtk_status_icon_get_visible(status_icon))
436 nn = notify_notification_new_with_status_icon(title,
437 msg, icon, status_icon);
439 nn = notify_notification_new(title, msg, icon, NULL);
440 notify_notification_set_timeout(nn, 5000);
441 g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
442 notify_notification_show(nn, NULL);
446 dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
448 struct if_msg *ifm, *ifp;
452 const char *act, *net;
453 const char *const *r;
456 ifm = make_if_msg(config);
460 rem = ignore_if_msg(ifm);
462 for (gl = interfaces; gl; gl = gl->next) {
463 ifp = (struct if_msg *)gl->data;
464 if (g_strcmp0(ifp->ifname, ifm->ifname) == 0) {
465 ifm->scan_results = ifp->scan_results;
466 ifp->scan_results = NULL;
470 g_slist_delete_link(interfaces, gl);
476 if (ifp == NULL && !rem)
477 interfaces = g_slist_prepend(interfaces, ifm);
478 interfaces = g_slist_sort(interfaces, if_msg_comparer);
481 /* We should ignore renew and stop so we don't annoy the user */
482 if (g_strcmp0(ifm->reason, "RENEW") == 0 ||
483 g_strcmp0(ifm->reason, "STOP") == 0)
486 msg = print_if_msg(ifm);
489 act = N_("Connected to ");
492 for (r = down_reasons; *r; r++) {
493 if (g_strcmp0(*r, ifm->reason) == 0) {
494 act = N_("Disconnected from ");
498 if (act && ifm->ip.s_addr) {
499 ipn = htonl(ifm->ip.s_addr);
500 if (IN_LINKLOCAL(ipn))
501 net = N_("private network");
502 else if (IN_PRIVATE(ipn))
505 net = N_("internet");
506 title = g_strconcat(act, net, NULL);
510 notify(title, msg, GTK_STOCK_NETWORK);
513 notify(N_("Interface event"), msg, GTK_STOCK_NETWORK);
518 foreach_make_ifm(_unused gpointer key, gpointer value, _unused gpointer data)
522 ifm = make_if_msg((GHashTable *)value);
523 if (ignore_if_msg(ifm))
526 interfaces = g_slist_prepend(interfaces, ifm);
530 dhcpcd_get_interfaces()
533 GError *error = NULL;
539 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
540 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
541 if (!dbus_g_proxy_call(dbus, "GetInterfaces", &error,
543 otype, &ifs, G_TYPE_INVALID))
544 error_exit("GetInterfaces", error);
545 g_hash_table_foreach(ifs, foreach_make_ifm, NULL);
546 g_hash_table_unref(ifs);
548 /* Each interface config only remembers the last order when
549 * that interface was configured, so get the real order now. */
550 g_strfreev(interface_order);
551 interface_order = NULL;
552 if (!dbus_g_proxy_call(dbus, "ListInterfaces", &error,
554 G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
555 error_exit("ListInterfaces", error);
556 interfaces = g_slist_sort(interfaces, if_msg_comparer);
558 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
559 otype = dbus_g_type_get_collection("GPtrArray", otype);
560 for (gl = interfaces; gl; gl = gl->next) {
561 ifm = (struct if_msg *)gl->data;
564 if (!dbus_g_proxy_call(dbus, "ScanResults", &error,
565 G_TYPE_STRING, ifm->ifname, G_TYPE_INVALID,
566 otype, &array, G_TYPE_INVALID))
568 g_message("ScanResults: %s", error->message);
569 g_clear_error(&error);
572 for (gsl = ifm->scan_results; gsl; gsl = gsl->next)
574 g_slist_free(ifm->scan_results);
575 ifm->scan_results = get_scan_results(ifm);
582 check_status(const char *status)
584 static char *last = NULL;
589 GError *error = NULL;
591 g_message("Status changed to %s", status);
592 if (g_strcmp0(status, "down") == 0) {
593 for (gl = interfaces; gl; gl = gl->next)
594 free_if_msg((struct if_msg *)gl->data);
595 g_slist_free(interfaces);
599 "Connection to dhcpcd lost" : "dhcpcd not running");
600 gtk_status_icon_set_tooltip(status_icon, msg);
601 notify(_("No network"), msg, GTK_STOCK_NETWORK);
606 if (g_strcmp0(status, "down") != 0)
609 if (g_strcmp0(status, last) == 0)
611 if (g_strcmp0(last, "down") == 0)
615 last = g_strdup(status);
619 if (!dbus_g_proxy_call(dbus, "GetDhcpcdVersion", &error,
621 G_TYPE_STRING, &version, G_TYPE_INVALID))
622 error_exit(_("GetDhcpcdVersion"), error);
623 g_message(_("Connected to %s-%s"), "dhcpcd", version);
625 dhcpcd_get_interfaces();
629 dhcpcd_status(_unused DBusGProxy *proxy, const char *status,
632 check_status(status);
636 dhcpcd_scan_results(_unused DBusGProxy *proxy, const char *iface,
640 struct if_ap *ifa, *ifa2;
641 GSList *gl, *aps, *l;
644 ifm = find_if_msg(iface);
647 g_message(_("%s: Received scan results"), ifm->ifname);
648 aps = get_scan_results(ifm);
650 for (gl = aps; gl; gl = gl->next) {
651 ifa = (struct if_ap *)gl->data;
652 for (l = ifm->scan_results; l; l = l->next) {
653 ifa2 = (struct if_ap *)l->data;
654 if (g_strcmp0(ifa->ssid, ifa2->ssid) == 0)
659 txt = g_strdup(ifa->ssid);
661 ntxt = g_strconcat(txt, "\n", ifa->ssid, NULL);
667 for (gl = ifm->scan_results; gl; gl = gl->next)
668 free_if_ap((struct if_ap *)gl->data);
669 g_slist_free(ifm->scan_results);
670 ifm->scan_results = aps;
672 notify(N_("Found new AP"), txt, GTK_STOCK_NETWORK);
678 main(int argc, char *argv[])
680 DBusGConnection *bus;
681 GError *error = NULL;
682 char *version = NULL;
686 setlocale(LC_ALL, "");
687 bindtextdomain(PACKAGE, NULL);
688 bind_textdomain_codeset(PACKAGE, "UTF-8");
691 gtk_init(&argc, &argv);
692 g_set_application_name("dhcpcd Monitor");
693 status_icon = gtk_status_icon_new_from_icon_name("network-offline");
694 if (status_icon == NULL)
696 gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
697 //network_offline = gtk_status_icon_get_pixbuf(status_icon);
699 gtk_status_icon_set_tooltip(status_icon,
700 _("Connecting to dhcpcd ..."));
701 gtk_status_icon_set_visible(status_icon, true);
703 notify_init(PACKAGE);
705 g_message(_("Connecting to dbus ..."));
706 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
707 if (bus == NULL || error != NULL)
708 error_exit(_("Could not connect to system bus"), error);
709 dbus = dbus_g_proxy_new_for_name(bus,
714 g_message(_("Connecting to dhcpcd-dbus ..."));
715 while (--tries > 0) {
716 g_clear_error(&error);
717 if (dbus_g_proxy_call_with_timeout(dbus, "GetVersion", 500,
718 &error, G_TYPE_INVALID,
719 G_TYPE_STRING, &version, G_TYPE_INVALID))
723 error_exit(_("GetVersion"), error);
724 g_message(_("Connected to %s-%s"), "dhcpcd-dbus", version);
727 gtk_status_icon_set_tooltip(status_icon, _("Triggering dhcpcd ..."));
729 menu_init(status_icon);
731 if (!dbus_g_proxy_call(dbus, "GetStatus", &error,
733 G_TYPE_STRING, &version, G_TYPE_INVALID))
734 error_exit(_("GetStatus"), error);
735 check_status(version);
738 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
739 dbus_g_proxy_add_signal(dbus, "Event",
740 otype, G_TYPE_INVALID);
741 dbus_g_proxy_connect_signal(dbus, "Event",
742 G_CALLBACK(dhcpcd_event), bus, NULL);
743 dbus_g_proxy_add_signal(dbus, "StatusChanged",
744 G_TYPE_STRING, G_TYPE_INVALID);
745 dbus_g_proxy_connect_signal(dbus, "StatusChanged",
746 G_CALLBACK(dhcpcd_status), bus, NULL);
747 dbus_g_proxy_add_signal(dbus, "ScanResults",
748 G_TYPE_STRING, G_TYPE_INVALID);
749 dbus_g_proxy_connect_signal(dbus, "ScanResults",
750 G_CALLBACK(dhcpcd_scan_results), bus, NULL);