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->name, iface) == 0)
95 free_if_msg(struct if_msg *ifm)
102 for (gl = ifm->scan_results; gl; gl = gl->next)
104 g_slist_free(ifm->scan_results);
109 free_if_ap(struct if_ap *ifa)
118 error_exit(const char *msg, GError *error)
123 g_critical("%s: %s", msg, error->message);
124 dialog = gtk_message_dialog_new(NULL,
132 g_critical("%s", msg);
133 dialog = gtk_message_dialog_new(NULL,
140 gtk_dialog_run(GTK_DIALOG(dialog));
141 gtk_widget_destroy(dialog);
142 if (gtk_main_level())
149 get_scan_results(const GPtrArray *array)
157 for (i = 0; i < array->len; i++) {
158 config = g_ptr_array_index(array, i);
159 val = g_hash_table_lookup(config, "BSSID");
162 ifa = g_malloc0(sizeof(*ifa));
163 ifa->bssid = g_strdup(g_value_get_string(val));
164 val = g_hash_table_lookup(config, "Frequency");
166 ifa->freq = g_value_get_int(val);
167 val = g_hash_table_lookup(config, "Noise");
169 ifa->noise = g_value_get_int(val);
170 val = g_hash_table_lookup(config, "Level");
172 ifa->level = g_value_get_int(val);
173 val = g_hash_table_lookup(config, "Flags");
175 ifa->flags = g_strdup(g_value_get_string(val));
176 val = g_hash_table_lookup(config, "SSID");
178 ifa->ssid = g_strdup(g_value_get_string(val));
179 list = g_slist_append(list, ifa);
184 static struct if_msg *
185 make_if_msg(GHashTable *config)
190 val = g_hash_table_lookup(config, "Interface");
193 ifm = g_malloc0(sizeof(*ifm));
194 ifm->name = g_strdup(g_value_get_string(val));
195 val = g_hash_table_lookup(config, "Reason");
197 ifm->reason = g_strdup(g_value_get_string(val));
198 val = g_hash_table_lookup(config, "Wireless");
200 ifm->wireless = g_value_get_boolean(val);
202 val = g_hash_table_lookup(config, "SSID");
204 ifm->ssid = g_strdup(g_value_get_string(val));
206 val = g_hash_table_lookup(config, "IPAddress");
208 ifm->ip.s_addr = g_value_get_uint(val);
209 val = g_hash_table_lookup(config, "SubnetCIDR");
211 ifm->cidr = g_value_get_uchar(val);
212 val = g_hash_table_lookup(config, "InterfaceOrder");
214 g_strfreev(interface_order);
215 interface_order = g_strsplit(g_value_get_string(val), " ", 0);
221 if_up(const struct if_msg *ifm)
223 const char *const *r;
225 for (r = up_reasons; *r; r++)
226 if (g_strcmp0(*r, ifm->reason) == 0)
232 print_if_msg(const struct if_msg *ifm)
235 const char *reason = NULL;
237 gboolean showip, showssid;
242 reason = N_("Acquired address");
244 if (g_strcmp0(ifm->reason, "EXPIRE") == 0)
245 reason = N_("Failed to renew");
246 else if (g_strcmp0(ifm->reason, "CARRIER") == 0) {
248 reason = N_("Asssociated with");
249 if (ifm->ssid != NULL)
252 reason = N_("Cable plugged in");
254 } else if (g_strcmp0(ifm->reason, "NOCARRIER") == 0) {
256 if (ifm->ssid != NULL || ifm->ip.s_addr != 0) {
257 reason = N_("Disassociated from");
260 reason = N_("Not associated");
262 reason = N_("Cable unplugged");
267 reason = ifm->reason;
269 len = strlen(ifm->name) + 3;
270 len += strlen(reason) + 1;
271 if (ifm->ip.s_addr != 0) {
272 len += 16; /* 000. * 4 */
277 len += strlen(ifm->ssid) + 1;
278 msg = p = g_malloc(len);
279 p += g_snprintf(msg, len, "%s: %s", ifm->name, reason);
281 p += g_snprintf(p, len - (p - msg), " %s", ifm->ssid);
282 if (ifm->ip.s_addr != 0 && showip) {
283 p += g_snprintf(p, len - (p - msg), " %s", inet_ntoa(ifm->ip));
285 g_snprintf(p, len - (p - msg), "/%d", ifm->cidr);
291 if_msg_comparer(gconstpointer a, gconstpointer b)
293 const struct if_msg *ifa, *ifb;
294 const char *const *order;
296 ifa = (const struct if_msg *)a;
297 ifb = (const struct if_msg *)b;
298 for (order = (const char *const *)interface_order; *order; order++) {
299 if (g_strcmp0(*order, ifa->name) == 0)
301 if (g_strcmp0(*order, ifb->name) == 0)
308 animate_carrier(_unused gpointer data)
313 switch(ani_counter++) {
315 gtk_status_icon_set_from_icon_name(status_icon, "network-transmit");
318 gtk_status_icon_set_from_icon_name(status_icon, "network-receive");
321 gtk_status_icon_set_from_icon_name(status_icon, "network-idle");
329 animate_online(_unused gpointer data)
334 if (ani_counter++ > 6) {
340 if (ani_counter % 2 == 0)
341 gtk_status_icon_set_from_icon_name(status_icon, "network-idle");
343 gtk_status_icon_set_from_icon_name(status_icon, "network-transmit-receive");
348 update_online(char **buffer)
350 gboolean ison, iscarrier;
351 char *msg, *msgs, *tmp;
353 const struct if_msg *ifm;
355 ison = iscarrier = FALSE;
357 for (gl = interfaces; gl; gl = gl->next) {
358 ifm = (const struct if_msg *)gl->data;
360 ison = iscarrier = TRUE;
361 if (!iscarrier && g_strcmp0(ifm->reason, "CARRIER") == 0)
363 msg = print_if_msg(ifm);
365 tmp = g_strconcat(msgs, "\n", msg, NULL);
373 if (online != ison || carrier != iscarrier) {
375 if (ani_timer != 0) {
376 g_source_remove(ani_timer);
381 animate_online(NULL);
382 ani_timer = g_timeout_add(300, animate_online, NULL);
383 } else if (iscarrier) {
384 animate_carrier(NULL);
385 ani_timer = g_timeout_add(500, animate_carrier, NULL);
387 gtk_status_icon_set_from_icon_name(status_icon,
391 gtk_status_icon_set_tooltip(status_icon, msgs);
402 notify_notification_close(nn, NULL);
412 notify(const char *title, const char *msg, const char *icon)
416 msgs = g_strsplit(msg, "\n", 0);
417 for (m = msgs; *m; m++)
421 notify_notification_close(nn, NULL);
422 if (gtk_status_icon_get_visible(status_icon))
423 nn = notify_notification_new_with_status_icon(title,
428 nn = notify_notification_new(title, msg, icon, NULL);
429 notify_notification_set_timeout(nn, 5000);
430 g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
431 notify_notification_show(nn, NULL);
435 dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
437 struct if_msg *ifm, *ifp;
441 const char *act, *net;
442 const char *const *r;
445 ifm = make_if_msg(config);
449 rem = ignore_if_msg(ifm);
451 for (gl = interfaces; gl; gl = gl->next) {
452 ifp = (struct if_msg *)gl->data;
453 if (g_strcmp0(ifp->name, ifm->name) == 0) {
456 interfaces = g_list_delete_link(interfaces, gl);
462 if (ifp == NULL && !rem)
463 interfaces = g_list_prepend(interfaces, ifm);
464 interfaces = g_list_sort(interfaces, if_msg_comparer);
467 /* We should ignore renew and stop so we don't annoy the user */
468 if (g_strcmp0(ifm->reason, "RENEW") == 0 ||
469 g_strcmp0(ifm->reason, "STOP") == 0)
472 msg = print_if_msg(ifm);
475 act = N_("Connected to ");
478 for (r = down_reasons; *r; r++) {
479 if (g_strcmp0(*r, ifm->reason) == 0) {
480 act = N_("Disconnected from ");
484 if (act && ifm->ip.s_addr) {
485 ipn = htonl(ifm->ip.s_addr);
486 if (IN_LINKLOCAL(ipn))
487 net = N_("private network");
488 else if (IN_PRIVATE(ipn))
491 net = N_("internet");
492 title = g_strconcat(act, net, NULL);
496 notify(title, msg, GTK_STOCK_NETWORK);
499 notify(N_("Interface event"), msg, GTK_STOCK_NETWORK);
504 foreach_make_ifm(_unused gpointer key, gpointer value, _unused gpointer data)
508 ifm = make_if_msg((GHashTable *)value);
509 if (ignore_if_msg(ifm))
512 interfaces = g_list_prepend(interfaces, ifm);
516 dhcpcd_get_interfaces()
519 GError *error = NULL;
527 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
528 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
529 if (!dbus_g_proxy_call(dbus, "GetInterfaces", &error,
531 otype, &ifs, G_TYPE_INVALID))
532 error_exit("GetInterfaces", error);
533 g_hash_table_foreach(ifs, foreach_make_ifm, NULL);
534 g_hash_table_unref(ifs);
536 /* Each interface config only remembers the last order when
537 * that interface was configured, so get the real order now. */
538 g_strfreev(interface_order);
539 interface_order = NULL;
540 if (!dbus_g_proxy_call(dbus, "ListInterfaces", &error,
542 G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
543 error_exit("ListInterfaces", error);
544 interfaces = g_list_sort(interfaces, if_msg_comparer);
546 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
547 otype = dbus_g_type_get_collection("GPtrArray", otype);
548 for (gl = interfaces; gl; gl = gl->next) {
549 ifm = (struct if_msg *)gl->data;
552 if (!dbus_g_proxy_call(dbus, "GetScanResults", &error,
553 G_TYPE_STRING, ifm->name, G_TYPE_INVALID,
554 otype, &array, G_TYPE_INVALID))
556 g_message("GetScanResults: %s", error->message);
557 g_clear_error(&error);
560 for (gsl = ifm->scan_results; gsl; gsl = gsl->next)
562 g_slist_free(ifm->scan_results);
563 ifm->scan_results = get_scan_results(array);
568 // GTK+ 2.16 msg = gtk_status_icon_get_tooltip_text(status_icon);
570 notify("Interface status", msg, GTK_STOCK_NETWORK);
576 check_status(const char *status)
578 static char *last = NULL;
583 GError *error = NULL;
585 g_message("Status changed to %s", status);
586 if (g_strcmp0(status, "down") == 0) {
587 for (gl = interfaces; gl; gl = gl->next)
588 free_if_msg((struct if_msg *)gl->data);
589 g_list_free(interfaces);
592 msg = N_(last? "Connection to dhcpcd lost" : "dhcpcd not running");
593 gtk_status_icon_set_tooltip(status_icon, msg);
594 notify(_("No network"), msg, GTK_STOCK_NETWORK);
599 if (g_strcmp0(status, "down") != 0)
602 if (g_strcmp0(status, last) == 0)
604 if (g_strcmp0(last, "down") == 0)
608 last = g_strdup(status);
612 if (!dbus_g_proxy_call(dbus, "GetDhcpcdVersion", &error,
614 G_TYPE_STRING, &version, G_TYPE_INVALID))
615 error_exit(_("GetDhcpcdVersion"), error);
616 g_message(_("Connected to %s-%s"), "dhcpcd", version);
618 dhcpcd_get_interfaces();
622 dhcpcd_status(_unused DBusGProxy *proxy, const char *status, _unused void *data)
624 check_status(status);
628 dhcpcd_scan_results(_unused DBusGProxy *proxy, const char *iface, GPtrArray *array, _unused void *data)
633 ifm = find_if_msg(iface);
636 g_message(_("%s: Received scan results"), ifm->name);
637 for (gl = ifm->scan_results; gl; gl = gl->next)
638 free_if_ap((struct if_ap *)gl->data);
639 g_slist_free(ifm->scan_results);
640 ifm->scan_results = get_scan_results(array);
644 main(int argc, char *argv[])
646 DBusGConnection *bus;
647 GError *error = NULL;
648 char *version = NULL;
653 setlocale(LC_ALL, "");
654 bindtextdomain(PACKAGE, NULL);
655 bind_textdomain_codeset(PACKAGE, "UTF-8");
658 gtk_init(&argc, &argv);
659 g_set_application_name("dhcpcd Monitor");
660 status_icon = gtk_status_icon_new_from_icon_name("network-offline");
661 if (status_icon == NULL)
662 status_icon = gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
663 //network_offline = gtk_status_icon_get_pixbuf(status_icon);
665 gtk_status_icon_set_tooltip(status_icon, _("Connecting to dhcpcd ..."));
666 gtk_status_icon_set_visible(status_icon, TRUE);
668 notify_init(PACKAGE);
670 g_message(_("Connecting to dbus ..."));
671 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
672 if (bus == NULL || error != NULL)
673 error_exit(_("Could not connect to system bus"), error);
674 dbus = dbus_g_proxy_new_for_name(bus,
679 g_message(_("Connecting to dhcpcd-dbus ..."));
680 while (--tries > 0) {
681 g_clear_error(&error);
682 if (dbus_g_proxy_call_with_timeout(dbus,
693 error_exit(_("GetVersion"), error);
694 g_message(_("Connected to %s-%s"), "dhcpcd-dbus", version);
697 gtk_status_icon_set_tooltip(status_icon, _("Triggering dhcpcd ..."));
699 menu_init(status_icon);
701 if (!dbus_g_proxy_call(dbus, "GetStatus", &error,
703 G_TYPE_STRING, &version, G_TYPE_INVALID))
704 error_exit(_("GetStatus"), error);
705 check_status(version);
708 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
709 dbus_g_proxy_add_signal(dbus, "Event",
710 otype, G_TYPE_INVALID);
711 dbus_g_proxy_connect_signal(dbus, "Event",
712 G_CALLBACK(dhcpcd_event),
714 dbus_g_proxy_add_signal(dbus, "StatusChanged",
715 G_TYPE_STRING, G_TYPE_INVALID);
716 dbus_g_proxy_connect_signal(dbus, "StatusChanged",
717 G_CALLBACK(dhcpcd_status),
719 dbus_g_proxy_add_signal(dbus, "ScanResults",
720 G_TYPE_STRING, G_TYPE_INVALID);
721 dbus_g_proxy_connect_signal(dbus, "ScanResults",
722 G_CALLBACK(dhcpcd_scan_results),