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())
118 static struct if_msg *
119 make_if_msg(GHashTable *config)
124 val = g_hash_table_lookup(config, "Interface");
127 ifm = g_malloc0(sizeof(*ifm));
128 ifm->name = g_strdup(g_value_get_string(val));
129 val = g_hash_table_lookup(config, "Reason");
131 ifm->reason = g_strdup(g_value_get_string(val));
132 val = g_hash_table_lookup(config, "Wireless");
134 ifm->wireless = g_value_get_boolean(val);
136 val = g_hash_table_lookup(config, "SSID");
138 ifm->ssid = g_strdup(g_value_get_string(val));
140 val = g_hash_table_lookup(config, "IPAddress");
142 ifm->ip.s_addr = g_value_get_uint(val);
143 val = g_hash_table_lookup(config, "SubnetCIDR");
145 ifm->cidr = g_value_get_uchar(val);
146 val = g_hash_table_lookup(config, "InterfaceOrder");
148 g_strfreev(interface_order);
149 interface_order = g_strsplit(g_value_get_string(val), " ", 0);
155 if_up(const struct if_msg *ifm)
157 const char *const *r;
159 for (r = up_reasons; *r; r++)
160 if (g_strcmp0(*r, ifm->reason) == 0)
166 print_if_msg(const struct if_msg *ifm)
169 const char *reason = NULL;
171 gboolean showip, showssid;
176 reason = N_("Acquired address");
178 if (g_strcmp0(ifm->reason, "EXPIRE") == 0)
179 reason = N_("Failed to renew");
180 else if (g_strcmp0(ifm->reason, "CARRIER") == 0) {
182 reason = N_("Asssociated with");
183 if (ifm->ssid != NULL)
186 reason = N_("Cable plugged in");
188 } else if (g_strcmp0(ifm->reason, "NOCARRIER") == 0) {
190 if (ifm->ssid != NULL || ifm->ip.s_addr != 0) {
191 reason = N_("Lost association with");
194 reason = N_("Not associated");
196 reason = N_("Cable unplugged");
201 reason = ifm->reason;
203 len = strlen(ifm->name) + 3;
204 len += strlen(reason) + 1;
205 if (ifm->ip.s_addr != 0) {
206 len += 16; /* 000. * 4 */
211 len += strlen(ifm->ssid) + 1;
212 msg = p = g_malloc(len);
213 p += g_snprintf(msg, len, "%s: %s", ifm->name, reason);
215 p += g_snprintf(p, len - (p - msg), " %s", ifm->ssid);
216 if (ifm->ip.s_addr != 0 && showip) {
217 p += g_snprintf(p, len - (p - msg), " %s", inet_ntoa(ifm->ip));
219 g_snprintf(p, len - (p - msg), "/%d", ifm->cidr);
225 if_msg_comparer(gconstpointer a, gconstpointer b)
227 const struct if_msg *ifa, *ifb;
228 const char *const *order;
230 ifa = (const struct if_msg *)a;
231 ifb = (const struct if_msg *)b;
232 for (order = (const char *const *)interface_order; *order; order++) {
233 if (g_strcmp0(*order, ifa->name) == 0)
235 if (g_strcmp0(*order, ifb->name) == 0)
242 update_online(char **buffer)
244 gboolean ison, iscarrier;
245 char *msg, *msgs, *tmp;
248 const struct if_msg *ifm;
250 ison = iscarrier = FALSE;
252 for (gl = interfaces; gl; gl = gl->next) {
253 ifm = (const struct if_msg *)gl->data;
255 ison = iscarrier = TRUE;
256 if (!iscarrier && g_strcmp0(ifm->reason, "CARRIER") == 0)
258 msg = print_if_msg(ifm);
260 tmp = g_strconcat(msgs, "\n", msg, NULL);
268 if (online != ison || carrier != iscarrier) {
271 icon = "network-transmit-receive";
273 icon = "network-transmit";
275 icon = "network-offline";
276 gtk_status_icon_set_from_icon_name(status_icon, icon);
278 gtk_status_icon_set_tooltip(status_icon, msgs);
289 notify_notification_close(nn, NULL);
299 notify(const char *title, const char *msg, const char *icon)
303 msgs = g_strsplit(msg, "\n", 0);
304 for (m = msgs; *m; m++)
308 notify_notification_close(nn, NULL);
309 if (gtk_status_icon_get_visible(status_icon))
310 nn = notify_notification_new_with_status_icon(title,
315 nn = notify_notification_new(title, msg, icon, NULL);
316 notify_notification_set_timeout(nn, 5000);
317 g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
318 notify_notification_show(nn, NULL);
322 dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
324 struct if_msg *ifm, *ifp;
328 const char *act, *net;
329 const char *const *r;
332 ifm = make_if_msg(config);
336 rem = ignore_if_msg(ifm);
338 for (gl = interfaces; gl; gl = gl->next) {
339 ifp = (struct if_msg *)gl->data;
340 if (g_strcmp0(ifp->name, ifm->name) == 0) {
343 interfaces = g_list_delete_link(interfaces, gl);
349 if (ifp == NULL && !rem)
350 interfaces = g_list_prepend(interfaces, ifm);
351 interfaces = g_list_sort(interfaces, if_msg_comparer);
354 /* We should ignore renew and stop so we don't annoy the user */
355 if (g_strcmp0(ifm->reason, "RENEW") == 0 ||
356 g_strcmp0(ifm->reason, "STOP") == 0)
359 msg = print_if_msg(ifm);
362 act = N_("Connected to ");
365 for (r = down_reasons; *r; r++) {
366 if (g_strcmp0(*r, ifm->reason) == 0) {
367 act = N_("Disconnected from ");
371 if (act && ifm->ip.s_addr) {
372 ipn = htonl(ifm->ip.s_addr);
373 if (IN_LINKLOCAL(ipn))
374 net = N_("private network");
375 else if (IN_PRIVATE(ipn))
378 net = N_("internet");
379 title = g_strconcat(act, net, NULL);
383 notify(title, msg, GTK_STOCK_NETWORK);
386 notify("Interface event", msg, GTK_STOCK_NETWORK);
391 foreach_make_ifm(_unused gpointer key, gpointer value, _unused gpointer data)
395 ifm = make_if_msg((GHashTable *)value);
396 if (ignore_if_msg(ifm))
399 interfaces = g_list_prepend(interfaces, ifm);
403 dhcpcd_get_interfaces()
406 GError *error = NULL;
410 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
411 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
412 if (!dbus_g_proxy_call(dbus, "GetInterfaces", &error,
414 otype, &ifs, G_TYPE_INVALID))
415 error_exit("GetInterfaces", error);
416 g_hash_table_foreach(ifs, foreach_make_ifm, NULL);
417 g_hash_table_unref(ifs);
419 /* Each interface config only remembers the last order when
420 * that interface was configured, so get the real order now. */
421 g_strfreev(interface_order);
422 interface_order = NULL;
423 if (!dbus_g_proxy_call(dbus, "ListInterfaces", &error,
425 G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
426 error_exit("ListInterfaces", error);
427 interfaces = g_list_sort(interfaces, if_msg_comparer);
430 // GTK+ 2.16 msg = gtk_status_icon_get_tooltip_text(status_icon);
432 notify("Interface status", msg, GTK_STOCK_NETWORK);
438 check_status(const char *status)
440 static char *last = NULL;
445 GError *error = NULL;
447 g_message("Status changed to %s", status);
448 if (g_strcmp0(status, "down") == 0) {
449 for (gl = interfaces; gl; gl = gl->next)
450 free_if_msg((struct if_msg *)gl->data);
451 g_list_free(interfaces);
454 msg = N_(last? "Connection to dhcpcd lost" : "dhcpcd not running");
455 gtk_status_icon_set_tooltip(status_icon, msg);
456 notify(_("No network"), msg, GTK_STOCK_NETWORK);
461 if (g_strcmp0(status, "down") != 0)
464 if (g_strcmp0(status, last) == 0)
466 if (g_strcmp0(last, "down") == 0)
470 last = g_strdup(status);
474 if (!dbus_g_proxy_call(dbus, "GetDhcpcdVersion", &error,
476 G_TYPE_STRING, &version, G_TYPE_INVALID))
477 error_exit(_("GetDhcpcdVersion"), error);
478 g_message(_("Connected to %s-%s"), "dhcpcd", version);
480 dhcpcd_get_interfaces();
484 dhcpcd_status(_unused DBusGProxy *proxy, const char *status, _unused void *data)
486 check_status(status);
490 main(int argc, char *argv[])
492 DBusGConnection *bus;
493 GError *error = NULL;
494 char *version = NULL;
499 setlocale(LC_ALL, "");
500 bindtextdomain(PACKAGE, NULL);
501 bind_textdomain_codeset(PACKAGE, "UTF-8");
504 gtk_init(&argc, &argv);
505 g_set_application_name("dhcpcd Monitor");
506 status_icon = gtk_status_icon_new_from_icon_name("network-offline");
507 if (status_icon == NULL)
508 status_icon = gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
510 gtk_status_icon_set_tooltip(status_icon, _("Connecting to dhcpcd ..."));
511 gtk_status_icon_set_visible(status_icon, TRUE);
513 notify_init(PACKAGE);
515 g_message(_("Connecting to dbus ..."));
516 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
517 if (bus == NULL || error != NULL)
518 error_exit(_("Could not connect to system bus"), error);
519 dbus = dbus_g_proxy_new_for_name(bus,
524 g_message(_("Connecting to dhcpcd-dbus ..."));
525 while (--tries > 0) {
526 g_clear_error(&error);
527 if (dbus_g_proxy_call_with_timeout(dbus,
538 error_exit(_("GetVersion"), error);
539 g_message(_("Connected to %s-%s"), "dhcpcd-dbus", version);
542 gtk_status_icon_set_tooltip(status_icon, _("Triggering dhcpcd ..."));
544 menu_init(status_icon);
546 if (!dbus_g_proxy_call(dbus, "GetStatus", &error,
548 G_TYPE_STRING, &version, G_TYPE_INVALID))
549 error_exit(_("GetStatus"), error);
550 check_status(version);
553 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
554 dbus_g_proxy_add_signal(dbus, "Event",
555 otype, G_TYPE_INVALID);
556 dbus_g_proxy_connect_signal(dbus, "Event",
557 G_CALLBACK(dhcpcd_event),
559 dbus_g_proxy_add_signal(dbus, "StatusChanged",
560 G_TYPE_STRING, G_TYPE_INVALID);
561 dbus_g_proxy_connect_signal(dbus, "StatusChanged",
562 G_CALLBACK(dhcpcd_status),