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 = NULL;
44 static NotifyNotification *nn;
45 static gboolean online;
46 static gboolean carrier;
47 static char **interface_order;
49 const char *const up_reasons[] = {
60 const char *const down_reasons[] = {
70 ignore_if_msg(const struct if_msg *ifm)
72 if (g_strcmp0(ifm->reason, "STOP") == 0 ||
73 g_strcmp0(ifm->reason, "RELEASE") == 0)
79 free_if_msg(struct if_msg *ifm)
88 error_exit(const char *msg, GError *error)
93 g_critical("%s: %s", msg, error->message);
94 dialog = gtk_message_dialog_new(NULL,
102 g_critical("%s", msg);
103 dialog = gtk_message_dialog_new(NULL,
110 gtk_dialog_run(GTK_DIALOG(dialog));
111 gtk_widget_destroy(dialog);
112 if (gtk_main_level())
119 get_scan_results(const char *iface)
122 GPtrArray *array = NULL;
124 GError *error = NULL;
130 /* Below code causes dbus to go belly up for some reason :/ */
132 otype = dbus_g_type_get_struct("GValueArray",
139 otype = dbus_g_type_get_collection("GPtrArray", otype);
140 if (!dbus_g_proxy_call(dbus, "GetScanResults", &error,
141 G_TYPE_STRING, &iface, G_TYPE_INVALID,
142 otype, &array, G_TYPE_INVALID)) {
143 g_message("GetScanResults: %s\n", error->message);
144 g_clear_error(&error);
148 printf ("got %d\n", array->len);
149 for (i = 0; i < array->len; i++) {
150 ifa = g_malloc(sizeof(*ifa));
151 item = g_ptr_array_index(array, i);
152 v = g_value_array_get_nth(item, 0);
153 ifa->bssid = g_strdup(g_value_get_string(v));
154 v = g_value_array_get_nth(item, 1);
155 ifa->freq = g_value_get_uint(v);
156 v = g_value_array_get_nth(item, 2);
157 ifa->level = g_value_get_uint(v);
158 v = g_value_array_get_nth(item, 3);
159 ifa->flags = g_strdup(g_value_get_string(v));
160 v = g_value_array_get_nth(item, 3);
161 ifa->ssid = g_strdup(g_value_get_string(v));
162 list = g_list_append(list, ifa);
164 g_ptr_array_free(array, TRUE);
168 static struct if_msg *
169 make_if_msg(GHashTable *config)
174 val = g_hash_table_lookup(config, "Interface");
177 ifm = g_malloc0(sizeof(*ifm));
178 ifm->name = g_strdup(g_value_get_string(val));
179 val = g_hash_table_lookup(config, "Reason");
181 ifm->reason = g_strdup(g_value_get_string(val));
182 val = g_hash_table_lookup(config, "Wireless");
184 ifm->wireless = g_value_get_boolean(val);
186 val = g_hash_table_lookup(config, "SSID");
188 ifm->ssid = g_strdup(g_value_get_string(val));
190 val = g_hash_table_lookup(config, "IPAddress");
192 ifm->ip.s_addr = g_value_get_uint(val);
193 val = g_hash_table_lookup(config, "SubnetCIDR");
195 ifm->cidr = g_value_get_uchar(val);
196 val = g_hash_table_lookup(config, "InterfaceOrder");
198 g_strfreev(interface_order);
199 interface_order = g_strsplit(g_value_get_string(val), " ", 0);
202 ifm->scan_results = get_scan_results(ifm->name);
207 if_up(const struct if_msg *ifm)
209 const char *const *r;
211 for (r = up_reasons; *r; r++)
212 if (g_strcmp0(*r, ifm->reason) == 0)
218 print_if_msg(const struct if_msg *ifm)
221 const char *reason = NULL;
223 gboolean showip, showssid;
228 reason = N_("Acquired address");
230 if (g_strcmp0(ifm->reason, "EXPIRE") == 0)
231 reason = N_("Failed to renew");
232 else if (g_strcmp0(ifm->reason, "CARRIER") == 0) {
234 reason = N_("Asssociated with");
235 if (ifm->ssid != NULL)
238 reason = N_("Cable plugged in");
240 } else if (g_strcmp0(ifm->reason, "NOCARRIER") == 0) {
242 if (ifm->ssid != NULL || ifm->ip.s_addr != 0) {
243 reason = N_("Lost association with");
246 reason = N_("Not associated");
248 reason = N_("Cable unplugged");
253 reason = ifm->reason;
255 len = strlen(ifm->name) + 3;
256 len += strlen(reason) + 1;
257 if (ifm->ip.s_addr != 0) {
258 len += 16; /* 000. * 4 */
263 len += strlen(ifm->ssid) + 1;
264 msg = p = g_malloc(len);
265 p += g_snprintf(msg, len, "%s: %s", ifm->name, reason);
267 p += g_snprintf(p, len - (p - msg), " %s", ifm->ssid);
268 if (ifm->ip.s_addr != 0 && showip) {
269 p += g_snprintf(p, len - (p - msg), " %s", inet_ntoa(ifm->ip));
271 g_snprintf(p, len - (p - msg), "/%d", ifm->cidr);
277 if_msg_comparer(gconstpointer a, gconstpointer b)
279 const struct if_msg *ifa, *ifb;
280 const char *const *order;
282 ifa = (const struct if_msg *)a;
283 ifb = (const struct if_msg *)b;
284 for (order = (const char *const *)interface_order; *order; order++) {
285 if (g_strcmp0(*order, ifa->name) == 0)
287 if (g_strcmp0(*order, ifb->name) == 0)
294 update_online(char **buffer)
296 gboolean ison, iscarrier;
297 char *msg, *msgs, *tmp;
300 const struct if_msg *ifm;
302 ison = iscarrier = FALSE;
304 for (gl = interfaces; gl; gl = gl->next) {
305 ifm = (const struct if_msg *)gl->data;
307 ison = iscarrier = TRUE;
308 if (!iscarrier && g_strcmp0(ifm->reason, "CARRIER") == 0)
310 msg = print_if_msg(ifm);
312 tmp = g_strconcat(msgs, "\n", msg, NULL);
320 if (online != ison || carrier != iscarrier) {
323 icon = "network-transmit-receive";
325 icon = "network-transmit";
327 icon = "network-offline";
328 gtk_status_icon_set_from_icon_name(status_icon, icon);
330 gtk_status_icon_set_tooltip(status_icon, msgs);
341 notify_notification_close(nn, NULL);
351 notify(const char *title, const char *msg, const char *icon)
355 msgs = g_strsplit(msg, "\n", 0);
356 for (m = msgs; *m; m++)
360 notify_notification_close(nn, NULL);
361 if (gtk_status_icon_get_visible(status_icon))
362 nn = notify_notification_new_with_status_icon(title,
367 nn = notify_notification_new(title, msg, icon, NULL);
368 notify_notification_set_timeout(nn, 5000);
369 g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
370 notify_notification_show(nn, NULL);
374 dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
376 struct if_msg *ifm, *ifp;
380 const char *act, *net;
381 const char *const *r;
384 ifm = make_if_msg(config);
388 rem = ignore_if_msg(ifm);
390 for (gl = interfaces; gl; gl = gl->next) {
391 ifp = (struct if_msg *)gl->data;
392 if (g_strcmp0(ifp->name, ifm->name) == 0) {
395 interfaces = g_list_delete_link(interfaces, gl);
401 if (ifp == NULL && !rem)
402 interfaces = g_list_prepend(interfaces, ifm);
403 interfaces = g_list_sort(interfaces, if_msg_comparer);
406 /* We should ignore renew and stop so we don't annoy the user */
407 if (g_strcmp0(ifm->reason, "RENEW") == 0 ||
408 g_strcmp0(ifm->reason, "STOP") == 0)
411 msg = print_if_msg(ifm);
414 act = N_("Connected to ");
417 for (r = down_reasons; *r; r++) {
418 if (g_strcmp0(*r, ifm->reason) == 0) {
419 act = N_("Disconnected from ");
423 if (act && ifm->ip.s_addr) {
424 ipn = htonl(ifm->ip.s_addr);
425 if (IN_LINKLOCAL(ipn))
426 net = N_("private network");
427 else if (IN_PRIVATE(ipn))
430 net = N_("internet");
431 title = g_strconcat(act, net, NULL);
435 notify(title, msg, GTK_STOCK_NETWORK);
438 notify("Interface event", msg, GTK_STOCK_NETWORK);
443 foreach_make_ifm(_unused gpointer key, gpointer value, _unused gpointer data)
447 ifm = make_if_msg((GHashTable *)value);
448 if (ignore_if_msg(ifm))
451 interfaces = g_list_prepend(interfaces, ifm);
455 dhcpcd_get_interfaces()
458 GError *error = NULL;
462 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
463 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
464 if (!dbus_g_proxy_call(dbus, "GetInterfaces", &error,
466 otype, &ifs, G_TYPE_INVALID))
467 error_exit("GetInterfaces", error);
468 g_hash_table_foreach(ifs, foreach_make_ifm, NULL);
469 g_hash_table_unref(ifs);
471 /* Each interface config only remembers the last order when
472 * that interface was configured, so get the real order now. */
473 g_strfreev(interface_order);
474 interface_order = NULL;
475 if (!dbus_g_proxy_call(dbus, "ListInterfaces", &error,
477 G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
478 error_exit("ListInterfaces", error);
479 interfaces = g_list_sort(interfaces, if_msg_comparer);
482 // GTK+ 2.16 msg = gtk_status_icon_get_tooltip_text(status_icon);
484 notify("Interface status", msg, GTK_STOCK_NETWORK);
490 check_status(const char *status)
492 static char *last = NULL;
497 GError *error = NULL;
499 g_message("Status changed to %s", status);
500 if (g_strcmp0(status, "down") == 0) {
501 for (gl = interfaces; gl; gl = gl->next)
502 free_if_msg((struct if_msg *)gl->data);
503 g_list_free(interfaces);
506 msg = N_(last? "Connection to dhcpcd lost" : "dhcpcd not running");
507 gtk_status_icon_set_tooltip(status_icon, msg);
508 notify(_("No network"), msg, GTK_STOCK_NETWORK);
513 if (g_strcmp0(status, "down") != 0)
516 if (g_strcmp0(status, last) == 0)
518 if (g_strcmp0(last, "down") == 0)
522 last = g_strdup(status);
526 if (!dbus_g_proxy_call(dbus, "GetDhcpcdVersion", &error,
528 G_TYPE_STRING, &version, G_TYPE_INVALID))
529 error_exit(_("GetDhcpcdVersion"), error);
530 g_message(_("Connected to %s-%s"), "dhcpcd", version);
532 dhcpcd_get_interfaces();
536 dhcpcd_status(_unused DBusGProxy *proxy, const char *status, _unused void *data)
538 check_status(status);
542 main(int argc, char *argv[])
544 DBusGConnection *bus;
545 GError *error = NULL;
546 char *version = NULL;
551 setlocale(LC_ALL, "");
552 bindtextdomain(PACKAGE, NULL);
553 bind_textdomain_codeset(PACKAGE, "UTF-8");
556 gtk_init(&argc, &argv);
557 g_set_application_name("dhcpcd Monitor");
558 status_icon = gtk_status_icon_new_from_icon_name("network-offline");
559 if (status_icon == NULL)
560 status_icon = gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
562 gtk_status_icon_set_tooltip(status_icon, _("Connecting to dhcpcd ..."));
563 gtk_status_icon_set_visible(status_icon, TRUE);
565 notify_init(PACKAGE);
567 g_message(_("Connecting to dbus ..."));
568 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
569 if (bus == NULL || error != NULL)
570 error_exit(_("Could not connect to system bus"), error);
571 dbus = dbus_g_proxy_new_for_name(bus,
576 g_message(_("Connecting to dhcpcd-dbus ..."));
577 while (--tries > 0) {
578 g_clear_error(&error);
579 if (dbus_g_proxy_call_with_timeout(dbus,
590 error_exit(_("GetVersion"), error);
591 g_message(_("Connected to %s-%s"), "dhcpcd-dbus", version);
594 gtk_status_icon_set_tooltip(status_icon, _("Triggering dhcpcd ..."));
596 menu_init(status_icon);
598 if (!dbus_g_proxy_call(dbus, "GetStatus", &error,
600 G_TYPE_STRING, &version, G_TYPE_INVALID))
601 error_exit(_("GetStatus"), error);
602 check_status(version);
605 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
606 dbus_g_proxy_add_signal(dbus, "Event",
607 otype, G_TYPE_INVALID);
608 dbus_g_proxy_connect_signal(dbus, "Event",
609 G_CALLBACK(dhcpcd_event),
611 dbus_g_proxy_add_signal(dbus, "StatusChanged",
612 G_TYPE_STRING, G_TYPE_INVALID);
613 dbus_g_proxy_connect_signal(dbus, "StatusChanged",
614 G_CALLBACK(dhcpcd_status),