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 */
34 #include <libnotify/notify.h>
36 #include "dhcpcd-gtk.h"
39 DBusGProxy *dbus = NULL;
40 GList *interfaces = NULL;
42 static GtkStatusIcon *status_icon = NULL;
43 static NotifyNotification *nn;
44 static gboolean online;
45 static gboolean carrier;
46 static char **interface_order;
48 const char *const up_reasons[] = {
59 const char *const down_reasons[] = {
69 ignore_if_msg(const struct if_msg *ifm)
71 if (g_strcmp0(ifm->reason, "STOP") == 0 ||
72 g_strcmp0(ifm->reason, "RELEASE") == 0)
78 free_if_msg(struct if_msg *ifm)
87 error_exit(const char *msg, GError *error)
92 g_critical("%s: %s", msg, error->message);
93 dialog = gtk_message_dialog_new(NULL,
101 g_critical("%s", msg);
102 dialog = gtk_message_dialog_new(NULL,
109 gtk_dialog_run(GTK_DIALOG(dialog));
110 gtk_widget_destroy(dialog);
111 if (gtk_main_level())
117 static struct if_msg *
118 make_if_msg(GHashTable *config)
123 val = g_hash_table_lookup(config, "Interface");
126 ifm = g_malloc0(sizeof(*ifm));
127 ifm->name = g_strdup(g_value_get_string(val));
128 val = g_hash_table_lookup(config, "Reason");
130 ifm->reason = g_strdup(g_value_get_string(val));
131 val = g_hash_table_lookup(config, "Wireless");
133 ifm->wireless = g_value_get_boolean(val);
135 val = g_hash_table_lookup(config, "SSID");
137 ifm->ssid = g_strdup(g_value_get_string(val));
139 val = g_hash_table_lookup(config, "IPAddress");
141 ifm->ip.s_addr = g_value_get_uint(val);
142 val = g_hash_table_lookup(config, "SubnetCIDR");
144 ifm->cidr = g_value_get_uchar(val);
145 val = g_hash_table_lookup(config, "InterfaceOrder");
147 g_strfreev(interface_order);
148 interface_order = g_strsplit(g_value_get_string(val), " ", 0);
154 if_up(const struct if_msg *ifm)
156 const char *const *r;
158 for (r = up_reasons; *r; r++)
159 if (g_strcmp0(*r, ifm->reason) == 0)
165 print_if_msg(const struct if_msg *ifm)
168 const char *reason = NULL;
170 gboolean showip, showssid;
175 reason = "Acquired address";
177 if (g_strcmp0(ifm->reason, "EXPIRE") == 0)
178 reason = "Failed to renew";
179 else if (g_strcmp0(ifm->reason, "CARRIER") == 0) {
181 reason = "Asssociated with";
182 if (ifm->ssid != NULL)
185 reason = "Cable plugged in";
187 } else if (g_strcmp0(ifm->reason, "NOCARRIER") == 0) {
189 if (ifm->ssid != NULL || ifm->ip.s_addr != 0) {
190 reason = "Lost association with";
193 reason = "Not associated";
195 reason = "Cable unplugged";
200 reason = ifm->reason;
202 len = strlen(ifm->name) + 3;
203 len += strlen(reason) + 1;
204 if (ifm->ip.s_addr != 0) {
205 len += 16; /* 000. * 4 */
210 len += strlen(ifm->ssid) + 1;
211 msg = p = g_malloc(len);
212 p += g_snprintf(msg, len, "%s: %s", ifm->name, reason);
214 p += g_snprintf(p, len - (p - msg), " %s", ifm->ssid);
215 if (ifm->ip.s_addr != 0 && showip) {
216 p += g_snprintf(p, len - (p - msg), " %s", inet_ntoa(ifm->ip));
218 g_snprintf(p, len - (p - msg), "/%d", ifm->cidr);
224 if_msg_comparer(gconstpointer a, gconstpointer b)
226 const struct if_msg *ifa, *ifb;
227 const char *const *order;
229 ifa = (const struct if_msg *)a;
230 ifb = (const struct if_msg *)b;
231 for (order = (const char *const *)interface_order; *order; order++) {
232 if (g_strcmp0(*order, ifa->name) == 0)
234 if (g_strcmp0(*order, ifb->name) == 0)
241 update_online(char **buffer)
243 gboolean ison, iscarrier;
244 char *msg, *msgs, *tmp;
247 const struct if_msg *ifm;
249 ison = iscarrier = FALSE;
251 for (gl = interfaces; gl; gl = gl->next) {
252 ifm = (const struct if_msg *)gl->data;
254 ison = iscarrier = TRUE;
255 if (!iscarrier && g_strcmp0(ifm->reason, "CARRIER") == 0)
257 msg = print_if_msg(ifm);
259 tmp = g_strconcat(msgs, "\n", msg, NULL);
267 if (online != ison || carrier != iscarrier) {
270 icon = "network-transmit-receive";
272 icon = "network-transmit";
274 icon = "network-offline";
275 gtk_status_icon_set_from_icon_name(status_icon, icon);
277 gtk_status_icon_set_tooltip(status_icon, msgs);
288 notify_notification_close(nn, NULL);
298 notify(const char *title, const char *msg, const char *icon)
302 msgs = g_strsplit(msg, "\n", 0);
303 for (m = msgs; *m; m++)
307 notify_notification_close(nn, NULL);
308 if (gtk_status_icon_get_visible(status_icon))
309 nn = notify_notification_new_with_status_icon(title,
314 nn = notify_notification_new(title, msg, icon, NULL);
315 notify_notification_set_timeout(nn, 5000);
316 g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
317 notify_notification_show(nn, NULL);
321 dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
323 struct if_msg *ifm, *ifp;
327 const char *act, *net;
328 const char *const *r;
331 ifm = make_if_msg(config);
335 rem = ignore_if_msg(ifm);
337 for (gl = interfaces; gl; gl = gl->next) {
338 ifp = (struct if_msg *)gl->data;
339 if (g_strcmp0(ifp->name, ifm->name) == 0) {
342 interfaces = g_list_delete_link(interfaces, gl);
348 if (ifp == NULL && !rem)
349 interfaces = g_list_prepend(interfaces, ifm);
350 interfaces = g_list_sort(interfaces, if_msg_comparer);
353 /* We should ignore renew and stop so we don't annoy the user */
354 if (g_strcmp0(ifm->reason, "RENEW") == 0 ||
355 g_strcmp0(ifm->reason, "STOP") == 0)
358 msg = print_if_msg(ifm);
361 act = "Connected to ";
364 for (r = down_reasons; *r; r++) {
365 if (g_strcmp0(*r, ifm->reason) == 0) {
366 act = "Disconnected from ";
370 if (act && ifm->ip.s_addr) {
371 ipn = htonl(ifm->ip.s_addr);
372 if (IN_LINKLOCAL(ipn))
373 net = "private network";
374 else if (IN_PRIVATE(ipn))
378 title = g_strconcat(act, net, NULL);
382 notify(title, msg, GTK_STOCK_NETWORK);
385 notify("Interface event", msg, GTK_STOCK_NETWORK);
390 foreach_make_ifm(_unused gpointer key, gpointer value, _unused gpointer data)
394 ifm = make_if_msg((GHashTable *)value);
395 if (ignore_if_msg(ifm))
398 interfaces = g_list_prepend(interfaces, ifm);
402 dhcpcd_get_interfaces()
405 GError *error = NULL;
409 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
410 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
411 if (!dbus_g_proxy_call(dbus, "GetInterfaces", &error,
413 otype, &ifs, G_TYPE_INVALID))
414 error_exit("GetInterfaces", error);
415 g_hash_table_foreach(ifs, foreach_make_ifm, NULL);
416 g_hash_table_unref(ifs);
418 /* Each interface config only remembers the last order when
419 * that interface was configured, so get the real order now. */
420 g_strfreev(interface_order);
421 interface_order = NULL;
422 if (!dbus_g_proxy_call(dbus, "ListInterfaces", &error,
424 G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
425 error_exit("ListInterfaces", error);
426 interfaces = g_list_sort(interfaces, if_msg_comparer);
429 // GTK+ 2.16 msg = gtk_status_icon_get_tooltip_text(status_icon);
431 notify("Interface status", msg, GTK_STOCK_NETWORK);
437 check_status(const char *status)
439 static char *last = NULL;
444 GError *error = NULL;
446 g_message("Status changed to %s", status);
447 if (g_strcmp0(status, "down") == 0) {
448 for (gl = interfaces; gl; gl = gl->next)
449 free_if_msg((struct if_msg *)gl->data);
450 g_list_free(interfaces);
453 msg = last? "Connection to dhcpcd lost" : "dhcpcd not running";
454 gtk_status_icon_set_tooltip(status_icon, msg);
455 notify("No network", msg, GTK_STOCK_NETWORK);
460 if (g_strcmp0(status, "down") != 0)
463 if (g_strcmp0(status, last) == 0)
465 if (g_strcmp0(last, "down") == 0)
469 last = g_strdup(status);
473 if (!dbus_g_proxy_call(dbus, "GetDhcpcdVersion", &error,
475 G_TYPE_STRING, &version, G_TYPE_INVALID))
476 error_exit("GetDhcpcdVersion", error);
477 g_message("Connected to dhcpcd-%s", version);
479 dhcpcd_get_interfaces();
483 dhcpcd_status(_unused DBusGProxy *proxy, const char *status, _unused void *data)
485 check_status(status);
489 main(int argc, char *argv[])
491 DBusGConnection *bus;
492 GError *error = NULL;
493 char *version = NULL;
497 gtk_init(&argc, &argv);
498 g_set_application_name("dhcpcd Monitor");
499 status_icon = gtk_status_icon_new_from_icon_name("network-offline");
500 if (status_icon == NULL)
501 status_icon = gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
503 gtk_status_icon_set_tooltip(status_icon, "Connecting to dhcpcd ...");
504 gtk_status_icon_set_visible(status_icon, TRUE);
506 notify_init(PACKAGE);
508 g_message("Connecting to dbus ...");
509 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
510 if (bus == NULL || error != NULL)
511 error_exit("Could not connect to system bus", error);
512 dbus = dbus_g_proxy_new_for_name(bus,
517 g_message("Connecting to dhcpcd-dbus ...");
518 while (--tries > 0) {
519 g_clear_error(&error);
520 if (dbus_g_proxy_call_with_timeout(dbus,
531 error_exit("GetVersion", error);
532 g_message("Connected to dhcpcd-dbus-%s", version);
535 gtk_status_icon_set_tooltip(status_icon, "Triggering dhcpcd ...");
537 menu_init(status_icon);
539 if (!dbus_g_proxy_call(dbus, "GetStatus", &error,
541 G_TYPE_STRING, &version, G_TYPE_INVALID))
542 error_exit("GetStatus", error);
543 check_status(version);
546 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
547 dbus_g_proxy_add_signal(dbus, "Event",
548 otype, G_TYPE_INVALID);
549 dbus_g_proxy_connect_signal(dbus, "Event",
550 G_CALLBACK(dhcpcd_event),
552 dbus_g_proxy_add_signal(dbus, "StatusChanged",
553 G_TYPE_STRING, G_TYPE_INVALID);
554 dbus_g_proxy_connect_signal(dbus, "StatusChanged",
555 G_CALLBACK(dhcpcd_status),