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 GList *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,
133 g_critical("%s", msg);
134 dialog = gtk_message_dialog_new(NULL,
141 gtk_dialog_run(GTK_DIALOG(dialog));
142 gtk_widget_destroy(dialog);
143 if (gtk_main_level())
150 get_scan_results(struct if_msg *ifm)
161 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
162 otype = dbus_g_type_get_collection("GPtrArray", otype);
164 if (!dbus_g_proxy_call(dbus, "ScanResults", &error,
165 G_TYPE_STRING, ifm->ifname, G_TYPE_INVALID,
166 otype, &array, G_TYPE_INVALID))
167 error_exit(_("ScanResults"), error);
169 for (i = 0; i < array->len; i++) {
170 config = g_ptr_array_index(array, i);
171 val = g_hash_table_lookup(config, "BSSID");
174 ifa = g_malloc0(sizeof(*ifa));
175 ifa->ifname = g_strdup(ifm->ifname);
176 ifa->bssid = g_strdup(g_value_get_string(val));
177 val = g_hash_table_lookup(config, "Frequency");
179 ifa->frequency = g_value_get_int(val);
180 val = g_hash_table_lookup(config, "Quality");
182 ifa->quality = g_value_get_int(val);
183 val = g_hash_table_lookup(config, "Noise");
185 ifa->noise = g_value_get_int(val);
186 val = g_hash_table_lookup(config, "Level");
188 ifa->level = g_value_get_int(val);
189 val = g_hash_table_lookup(config, "Flags");
191 ifa->flags = g_strdup(g_value_get_string(val));
192 val = g_hash_table_lookup(config, "SSID");
194 ifa->ssid = g_strdup(g_value_get_string(val));
195 list = g_slist_append(list, ifa);
200 static struct if_msg *
201 make_if_msg(GHashTable *config)
206 val = g_hash_table_lookup(config, "Interface");
209 ifm = g_malloc0(sizeof(*ifm));
210 ifm->ifname = g_strdup(g_value_get_string(val));
211 val = g_hash_table_lookup(config, "Reason");
213 ifm->reason = g_strdup(g_value_get_string(val));
214 val = g_hash_table_lookup(config, "Wireless");
216 ifm->wireless = g_value_get_boolean(val);
218 val = g_hash_table_lookup(config, "SSID");
220 ifm->ssid = g_strdup(g_value_get_string(val));
222 val = g_hash_table_lookup(config, "IPAddress");
224 ifm->ip.s_addr = g_value_get_uint(val);
225 val = g_hash_table_lookup(config, "SubnetCIDR");
227 ifm->cidr = g_value_get_uchar(val);
228 val = g_hash_table_lookup(config, "InterfaceOrder");
230 g_strfreev(interface_order);
231 interface_order = g_strsplit(g_value_get_string(val), " ", 0);
237 if_up(const struct if_msg *ifm)
239 const char *const *r;
241 for (r = up_reasons; *r; r++)
242 if (g_strcmp0(*r, ifm->reason) == 0)
248 print_if_msg(const struct if_msg *ifm)
251 const char *reason = NULL;
253 gboolean showip, showssid;
258 reason = N_("Acquired address");
260 if (g_strcmp0(ifm->reason, "EXPIRE") == 0)
261 reason = N_("Failed to renew");
262 else if (g_strcmp0(ifm->reason, "CARRIER") == 0) {
264 reason = N_("Asssociated with");
265 if (ifm->ssid != NULL)
268 reason = N_("Cable plugged in");
270 } else if (g_strcmp0(ifm->reason, "NOCARRIER") == 0) {
272 if (ifm->ssid != NULL || ifm->ip.s_addr != 0) {
273 reason = N_("Disassociated from");
276 reason = N_("Not associated");
278 reason = N_("Cable unplugged");
283 reason = ifm->reason;
285 len = strlen(ifm->ifname) + 3;
286 len += strlen(reason) + 1;
287 if (ifm->ip.s_addr != 0) {
288 len += 16; /* 000. * 4 */
293 len += strlen(ifm->ssid) + 1;
294 msg = p = g_malloc(len);
295 p += g_snprintf(msg, len, "%s: %s", ifm->ifname, reason);
297 p += g_snprintf(p, len - (p - msg), " %s", ifm->ssid);
298 if (ifm->ip.s_addr != 0 && showip) {
299 p += g_snprintf(p, len - (p - msg), " %s", inet_ntoa(ifm->ip));
301 g_snprintf(p, len - (p - msg), "/%d", ifm->cidr);
307 if_msg_comparer(gconstpointer a, gconstpointer b)
309 const struct if_msg *ifa, *ifb;
310 const char *const *order;
312 ifa = (const struct if_msg *)a;
313 ifb = (const struct if_msg *)b;
314 for (order = (const char *const *)interface_order; *order; order++) {
315 if (g_strcmp0(*order, ifa->ifname) == 0)
317 if (g_strcmp0(*order, ifb->ifname) == 0)
324 animate_carrier(_unused gpointer data)
329 switch(ani_counter++) {
331 gtk_status_icon_set_from_icon_name(status_icon, "network-transmit");
334 gtk_status_icon_set_from_icon_name(status_icon, "network-receive");
337 gtk_status_icon_set_from_icon_name(status_icon, "network-idle");
345 animate_online(_unused gpointer data)
350 if (ani_counter++ > 6) {
356 if (ani_counter % 2 == 0)
357 gtk_status_icon_set_from_icon_name(status_icon, "network-idle");
359 gtk_status_icon_set_from_icon_name(status_icon, "network-transmit-receive");
364 update_online(char **buffer)
366 gboolean 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);
418 notify_notification_close(nn, NULL);
428 notify(const char *title, const char *msg, const char *icon)
432 msgs = g_strsplit(msg, "\n", 0);
433 for (m = msgs; *m; m++)
437 notify_notification_close(nn, NULL);
438 if (gtk_status_icon_get_visible(status_icon))
439 nn = notify_notification_new_with_status_icon(title,
444 nn = notify_notification_new(title, msg, icon, NULL);
445 notify_notification_set_timeout(nn, 5000);
446 g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
447 notify_notification_show(nn, NULL);
451 dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
453 struct if_msg *ifm, *ifp;
457 const char *act, *net;
458 const char *const *r;
461 ifm = make_if_msg(config);
465 rem = ignore_if_msg(ifm);
467 for (gl = interfaces; gl; gl = gl->next) {
468 ifp = (struct if_msg *)gl->data;
469 if (g_strcmp0(ifp->ifname, ifm->ifname) == 0) {
470 ifm->scan_results = ifp->scan_results;
471 ifp->scan_results = NULL;
474 interfaces = g_list_delete_link(interfaces, gl);
480 if (ifp == NULL && !rem)
481 interfaces = g_list_prepend(interfaces, ifm);
482 interfaces = g_list_sort(interfaces, if_msg_comparer);
485 /* We should ignore renew and stop so we don't annoy the user */
486 if (g_strcmp0(ifm->reason, "RENEW") == 0 ||
487 g_strcmp0(ifm->reason, "STOP") == 0)
490 msg = print_if_msg(ifm);
493 act = N_("Connected to ");
496 for (r = down_reasons; *r; r++) {
497 if (g_strcmp0(*r, ifm->reason) == 0) {
498 act = N_("Disconnected from ");
502 if (act && ifm->ip.s_addr) {
503 ipn = htonl(ifm->ip.s_addr);
504 if (IN_LINKLOCAL(ipn))
505 net = N_("private network");
506 else if (IN_PRIVATE(ipn))
509 net = N_("internet");
510 title = g_strconcat(act, net, NULL);
514 notify(title, msg, GTK_STOCK_NETWORK);
517 notify(N_("Interface event"), msg, GTK_STOCK_NETWORK);
522 foreach_make_ifm(_unused gpointer key, gpointer value, _unused gpointer data)
526 ifm = make_if_msg((GHashTable *)value);
527 if (ignore_if_msg(ifm))
530 interfaces = g_list_prepend(interfaces, ifm);
534 dhcpcd_get_interfaces()
537 GError *error = NULL;
545 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
546 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
547 if (!dbus_g_proxy_call(dbus, "GetInterfaces", &error,
549 otype, &ifs, G_TYPE_INVALID))
550 error_exit("GetInterfaces", error);
551 g_hash_table_foreach(ifs, foreach_make_ifm, NULL);
552 g_hash_table_unref(ifs);
554 /* Each interface config only remembers the last order when
555 * that interface was configured, so get the real order now. */
556 g_strfreev(interface_order);
557 interface_order = NULL;
558 if (!dbus_g_proxy_call(dbus, "ListInterfaces", &error,
560 G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
561 error_exit("ListInterfaces", error);
562 interfaces = g_list_sort(interfaces, if_msg_comparer);
564 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
565 otype = dbus_g_type_get_collection("GPtrArray", otype);
566 for (gl = interfaces; gl; gl = gl->next) {
567 ifm = (struct if_msg *)gl->data;
570 if (!dbus_g_proxy_call(dbus, "ScanResults", &error,
571 G_TYPE_STRING, ifm->ifname, G_TYPE_INVALID,
572 otype, &array, G_TYPE_INVALID))
574 g_message("ScanResults: %s", error->message);
575 g_clear_error(&error);
578 for (gsl = ifm->scan_results; gsl; gsl = gsl->next)
580 g_slist_free(ifm->scan_results);
581 ifm->scan_results = get_scan_results(ifm);
586 // GTK+ 2.16 msg = gtk_status_icon_get_tooltip_text(status_icon);
588 notify(N_("Interface status"), msg, GTK_STOCK_NETWORK);
594 check_status(const char *status)
596 static char *last = NULL;
601 GError *error = NULL;
603 g_message("Status changed to %s", status);
604 if (g_strcmp0(status, "down") == 0) {
605 for (gl = interfaces; gl; gl = gl->next)
606 free_if_msg((struct if_msg *)gl->data);
607 g_list_free(interfaces);
610 msg = N_(last? "Connection to dhcpcd lost" : "dhcpcd not running");
611 gtk_status_icon_set_tooltip(status_icon, msg);
612 notify(_("No network"), msg, GTK_STOCK_NETWORK);
617 if (g_strcmp0(status, "down") != 0)
620 if (g_strcmp0(status, last) == 0)
622 if (g_strcmp0(last, "down") == 0)
626 last = g_strdup(status);
630 if (!dbus_g_proxy_call(dbus, "GetDhcpcdVersion", &error,
632 G_TYPE_STRING, &version, G_TYPE_INVALID))
633 error_exit(_("GetDhcpcdVersion"), error);
634 g_message(_("Connected to %s-%s"), "dhcpcd", version);
636 dhcpcd_get_interfaces();
640 dhcpcd_status(_unused DBusGProxy *proxy, const char *status, _unused void *data)
642 check_status(status);
646 dhcpcd_scan_results(_unused DBusGProxy *proxy, const char *iface, _unused void *data)
649 struct if_ap *ifa, *ifa2;
650 GSList *gl, *aps, *l;
653 ifm = find_if_msg(iface);
656 g_message(_("%s: Received scan results"), ifm->ifname);
657 aps = get_scan_results(ifm);
659 for (gl = aps; gl; gl = gl->next) {
660 ifa = (struct if_ap *)gl->data;
661 for (l = ifm->scan_results; l; l = l->next) {
662 ifa2 = (struct if_ap *)l->data;
663 if (g_strcmp0(ifa->ssid, ifa2->ssid) == 0)
668 txt = g_strdup(ifa->ssid);
670 ntxt = g_strconcat(txt, "\n", ifa->ssid, NULL);
676 for (gl = ifm->scan_results; gl; gl = gl->next)
677 free_if_ap((struct if_ap *)gl->data);
678 g_slist_free(ifm->scan_results);
679 ifm->scan_results = aps;
681 notify(N_("Found new AP"), txt, GTK_STOCK_NETWORK);
687 main(int argc, char *argv[])
689 DBusGConnection *bus;
690 GError *error = NULL;
691 char *version = NULL;
696 setlocale(LC_ALL, "");
697 bindtextdomain(PACKAGE, NULL);
698 bind_textdomain_codeset(PACKAGE, "UTF-8");
701 gtk_init(&argc, &argv);
702 g_set_application_name("dhcpcd Monitor");
703 status_icon = gtk_status_icon_new_from_icon_name("network-offline");
704 if (status_icon == NULL)
705 status_icon = gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
706 //network_offline = gtk_status_icon_get_pixbuf(status_icon);
708 gtk_status_icon_set_tooltip(status_icon, _("Connecting to dhcpcd ..."));
709 gtk_status_icon_set_visible(status_icon, TRUE);
711 notify_init(PACKAGE);
713 g_message(_("Connecting to dbus ..."));
714 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
715 if (bus == NULL || error != NULL)
716 error_exit(_("Could not connect to system bus"), error);
717 dbus = dbus_g_proxy_new_for_name(bus,
722 g_message(_("Connecting to dhcpcd-dbus ..."));
723 while (--tries > 0) {
724 g_clear_error(&error);
725 if (dbus_g_proxy_call_with_timeout(dbus,
736 error_exit(_("GetVersion"), error);
737 g_message(_("Connected to %s-%s"), "dhcpcd-dbus", version);
740 gtk_status_icon_set_tooltip(status_icon, _("Triggering dhcpcd ..."));
742 menu_init(status_icon);
744 if (!dbus_g_proxy_call(dbus, "GetStatus", &error,
746 G_TYPE_STRING, &version, G_TYPE_INVALID))
747 error_exit(_("GetStatus"), error);
748 check_status(version);
751 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
752 dbus_g_proxy_add_signal(dbus, "Event",
753 otype, G_TYPE_INVALID);
754 dbus_g_proxy_connect_signal(dbus, "Event",
755 G_CALLBACK(dhcpcd_event),
757 dbus_g_proxy_add_signal(dbus, "StatusChanged",
758 G_TYPE_STRING, G_TYPE_INVALID);
759 dbus_g_proxy_connect_signal(dbus, "StatusChanged",
760 G_CALLBACK(dhcpcd_status),
762 dbus_g_proxy_add_signal(dbus, "ScanResults",
763 G_TYPE_STRING, G_TYPE_INVALID);
764 dbus_g_proxy_connect_signal(dbus, "ScanResults",
765 G_CALLBACK(dhcpcd_scan_results),