3 * Copyright 2009-2014 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
33 # include <libnotify/notify.h>
34 #ifndef NOTIFY_CHECK_VERSION
35 # define NOTIFY_CHECK_VERSION(a,b,c) 0
37 static NotifyNotification *nn;
42 #include "dhcpcd-gtk.h"
44 static GtkStatusIcon *status_icon;
45 static guint ani_timer;
46 static int ani_counter;
57 static struct watch *watches;
61 static gboolean dhcpcd_try_open(gpointer data);
62 static gboolean dhcpcd_wpa_try_open(gpointer data);
65 wi_scan_find(DHCPCD_WI_SCAN *scan)
70 TAILQ_FOREACH(w, &wi_scans, next) {
71 for (dw = w->scans; dw; dw = dw->next)
79 get_strength_icon_name(int strength)
83 return "network-wireless-connected-100";
84 else if (strength > 55)
85 return "network-wireless-connected-75";
86 else if (strength > 30)
87 return "network-wireless-connected-50";
88 else if (strength > 5)
89 return "network-wireless-connected-25";
91 return "network-wireless-connected-00";
94 static DHCPCD_WI_SCAN *
98 DHCPCD_WI_SCAN *scan, *s;
101 TAILQ_FOREACH(w, &wi_scans, next) {
102 for (s = w->scans; s; s = s->next) {
103 if (dhcpcd_wi_associated(w->interface, s) &&
105 s->strength.value > scan->strength.value))
113 animate_carrier(_unused gpointer data)
116 DHCPCD_WI_SCAN *scan;
121 scan = get_strongest_scan();
123 switch(ani_counter++) {
125 icon = "network-wireless-connected-00";
128 icon = "network-wireless-connected-25";
131 icon = "network-wireless-connected-50";
134 icon = "network-wireless-connected-75";
137 icon = "network-wireless-connected-100";
142 switch(ani_counter++) {
144 icon = "network-transmit";
147 icon = "network-receive";
150 icon = "network-idle";
155 gtk_status_icon_set_from_icon_name(status_icon, icon);
160 animate_online(_unused gpointer data)
163 DHCPCD_WI_SCAN *scan;
168 if (ani_counter++ > 6) {
174 scan = get_strongest_scan();
175 if (ani_counter % 2 == 0)
176 icon = scan ? "network-wireless-connected-00" :
179 icon = scan ? get_strength_icon_name(scan->strength.value) :
180 "network-transmit-receive";
181 gtk_status_icon_set_from_icon_name(status_icon, icon);
186 update_online(DHCPCD_CONNECTION *con, bool showif)
188 bool ison, iscarrier;
189 char *msg, *msgs, *tmp;
192 ison = iscarrier = false;
194 ifs = dhcpcd_interfaces(con);
195 for (i = ifs; i; i = i->next) {
196 if (g_strcmp0(i->type, "link") == 0) {
203 msg = dhcpcd_if_message(i, NULL);
206 g_message("%s", msg);
208 tmp = g_strconcat(msgs, "\n", msg, NULL);
215 g_message("%s: %s", i->ifname, i->reason);
218 if (online != ison || carrier != iscarrier) {
221 if (ani_timer != 0) {
222 g_source_remove(ani_timer);
227 animate_online(NULL);
228 ani_timer = g_timeout_add(300, animate_online, NULL);
229 } else if (iscarrier) {
230 animate_carrier(NULL);
231 ani_timer = g_timeout_add(500, animate_carrier, NULL);
233 gtk_status_icon_set_from_icon_name(status_icon,
237 gtk_status_icon_set_tooltip_text(status_icon, msgs);
246 notify_notification_close(nn, NULL);
251 static char *notify_last_msg;
260 notify(const char *title, const char *msg, const char *icon)
265 /* Don't spam the same message */
266 if (notify_last_msg) {
267 if (notify_last_msg && strcmp(msg, notify_last_msg) == 0)
269 g_free(notify_last_msg);
271 notify_last_msg = g_strdup(msg);
274 notify_notification_close(nn, NULL);
276 #if NOTIFY_CHECK_VERSION(0,7,0)
277 nn = notify_notification_new(title, msg, icon);
278 notify_notification_set_hint(nn, "transient",
279 g_variant_new_boolean(TRUE));
281 if (gtk_status_icon_get_visible(status_icon))
282 nn = notify_notification_new_with_status_icon(title,
283 msg, icon, status_icon);
285 nn = notify_notification_new(title, msg, icon, NULL);
288 notify_notification_set_timeout(nn, 5000);
289 g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
290 notify_notification_show(nn, NULL);
293 # define notify(a, b, c)
296 static struct watch *
297 dhcpcd_findwatch(int fd, gpointer data, struct watch **last)
303 for (w = watches; w; w = w->next) {
304 if (w->fd == fd || w->ref == data)
313 dhcpcd_unwatch(int fd, gpointer data)
317 if ((w = dhcpcd_findwatch(fd, data, &l))) {
322 g_source_remove(w->eventid);
323 g_io_channel_unref(w->gio);
330 gboolean (*cb)(GIOChannel *, GIOCondition, gpointer),
339 if ((w = dhcpcd_findwatch(fd, data, &l))) {
346 g_source_remove(w->eventid);
347 g_io_channel_unref(w->gio);
351 gio = g_io_channel_unix_new(fd);
353 g_warning(_("Error creating new GIO Channel\n"));
356 flags = G_IO_IN | G_IO_ERR | G_IO_HUP;
357 if ((eventid = g_io_add_watch(gio, flags, cb, data)) == 0) {
358 g_warning(_("Error creating watch\n"));
359 g_io_channel_unref(gio);
363 w = g_try_malloc(sizeof(*w));
365 g_warning(_("g_try_malloc\n"));
366 g_source_remove(eventid);
367 g_io_channel_unref(gio);
373 w->eventid = eventid;
382 dhcpcd_status_cb(DHCPCD_CONNECTION *con, const char *status,
385 static char *last = NULL;
390 g_message("Status changed to %s", status);
391 if (g_strcmp0(status, "down") == 0) {
393 "Connection to dhcpcd lost" : "dhcpcd not running");
394 if (ani_timer != 0) {
395 g_source_remove(ani_timer);
399 online = carrier = false;
400 gtk_status_icon_set_from_icon_name(status_icon,
402 gtk_status_icon_set_tooltip_text(status_icon, msg);
406 while ((w = TAILQ_FIRST(&wi_scans))) {
407 TAILQ_REMOVE(&wi_scans, w, next);
408 dhcpcd_wi_scans_free(w->scans);
411 dhcpcd_unwatch(-1, con);
412 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
414 if ((last == NULL || g_strcmp0(last, "down") == 0)) {
415 g_message(_("Connected to %s-%s"), "dhcpcd",
416 dhcpcd_version(con));
419 refresh = g_strcmp0(last, "opened") ? false : true;
420 update_online(con, refresh);
424 last = g_strdup(status);
428 dhcpcd_cb(_unused GIOChannel *gio, _unused GIOCondition c, gpointer data)
430 DHCPCD_CONNECTION *con;
432 con = (DHCPCD_CONNECTION *)data;
433 if (dhcpcd_get_fd(con) == -1) {
434 g_warning(_("dhcpcd connection lost"));
435 dhcpcd_unwatch(-1, con);
436 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
440 dhcpcd_dispatch(con);
445 dhcpcd_try_open(gpointer data)
447 DHCPCD_CONNECTION *con;
449 static int last_error;
451 con = (DHCPCD_CONNECTION *)data;
452 fd = dhcpcd_open(con, true);
454 if (errno == EACCES || errno == EPERM) {
455 if ((fd = dhcpcd_open(con, false)) != -1)
458 if (errno != last_error) {
459 g_critical("dhcpcd_open: %s", strerror(errno));
466 if (!dhcpcd_watch(fd, dhcpcd_cb, con)) {
471 /* Start listening to WPA events */
472 dhcpcd_wpa_start(con);
478 dhcpcd_if_cb(DHCPCD_IF *i, _unused void *data)
480 DHCPCD_CONNECTION *con;
485 /* We should ignore renew and stop so we don't annoy the user */
486 if (g_strcmp0(i->reason, "RENEW") &&
487 g_strcmp0(i->reason, "STOP") &&
488 g_strcmp0(i->reason, "STOPPED"))
490 msg = dhcpcd_if_message(i, &new_msg);
492 g_message("%s", msg);
495 icon = "network-transmit-receive";
497 // icon = "network-transmit";
499 icon = "network-offline";
500 notify(_("Network event"), msg, icon);
506 /* Update the tooltip with connection information */
507 con = dhcpcd_if_connection(i);
508 update_online(con, false);
511 DHCPCD_WI_SCAN *scans;
514 TAILQ_FOREACH(w, &wi_scans, next) {
515 if (w->interface == i)
519 scans = dhcpcd_wi_scans(i);
520 menu_update_scans(w, scans);
526 dhcpcd_wpa_cb(_unused GIOChannel *gio, _unused GIOCondition c,
532 wpa = (DHCPCD_WPA *)data;
533 if (dhcpcd_wpa_get_fd(wpa) == -1) {
534 dhcpcd_unwatch(-1, wpa);
536 /* If the interface hasn't left, try re-opening */
537 i = dhcpcd_wpa_if(wpa);
539 g_strcmp0(i->reason, "DEPARTED") == 0 ||
540 g_strcmp0(i->reason, "STOPPED") == 0)
542 g_warning(_("dhcpcd WPA connection lost: %s"), i->ifname);
543 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_wpa_try_open, wpa);
547 dhcpcd_wpa_dispatch(wpa);
552 dhcpcd_wpa_try_open(gpointer data)
556 static int last_error;
558 wpa = (DHCPCD_WPA *)data;
559 fd = dhcpcd_wpa_open(wpa);
561 if (errno != last_error)
562 g_critical("dhcpcd_wpa_open: %s", strerror(errno));
567 if (!dhcpcd_watch(fd, dhcpcd_wpa_cb, wpa)) {
568 dhcpcd_wpa_close(wpa);
576 dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, _unused void *data)
580 DHCPCD_WI_SCAN *scans, *s1, *s2;
585 /* This could be a new WPA so watch it */
586 fd = dhcpcd_wpa_get_fd(wpa);
588 g_critical("No fd for WPA %p", wpa);
589 dhcpcd_unwatch(-1, wpa);
592 dhcpcd_watch(fd, dhcpcd_wpa_cb, wpa);
594 i = dhcpcd_wpa_if(wpa);
596 g_critical("No interface for WPA %p", wpa);
599 g_message(_("%s: Received scan results"), i->ifname);
602 scans = dhcpcd_wi_scans(i);
603 if (scans == NULL && errno)
604 g_warning("%s: %s", i->ifname, strerror(errno));
606 TAILQ_FOREACH(w, &wi_scans, next) {
607 if (w->interface == i)
611 w = g_malloc(sizeof(*w));
615 TAILQ_INIT(&w->menus);
616 TAILQ_INSERT_TAIL(&wi_scans, w, next);
619 msg = N_("New Access Point");
620 for (s1 = scans; s1; s1 = s1->next) {
621 for (s2 = w->scans; s2; s2 = s2->next)
622 if (g_strcmp0(s1->ssid, s2->ssid) == 0)
626 txt = g_strdup(s1->ssid);
628 msg = N_("New Access Points");
629 t = g_strconcat(txt, "\n",
637 notify(msg, txt, "network-wireless");
640 menu_update_scans(w, scans);
645 dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa, const char *status, _unused void *data)
650 i = dhcpcd_wpa_if(wpa);
651 g_message("%s: WPA status %s", i->ifname, status);
652 if (g_strcmp0(status, "down") == 0) {
653 dhcpcd_unwatch(-1, wpa);
654 TAILQ_FOREACH_SAFE(w, &wi_scans, next, wn) {
655 if (w->interface == i) {
656 TAILQ_REMOVE(&wi_scans, w, next);
658 dhcpcd_wi_scans_free(w->scans);
667 bgscan(gpointer data)
670 DHCPCD_CONNECTION *con;
673 con = (DHCPCD_CONNECTION *)data;
674 TAILQ_FOREACH(w, &wi_scans, next) {
675 if (w->interface->wireless && w->interface->up) {
676 wpa = dhcpcd_wpa_find(con, w->interface->ifname);
678 dhcpcd_wpa_scan(wpa);
687 main(int argc, char *argv[])
689 DHCPCD_CONNECTION *con;
691 setlocale(LC_ALL, "");
692 bindtextdomain(PACKAGE, NULL);
693 bind_textdomain_codeset(PACKAGE, "UTF-8");
696 gtk_init(&argc, &argv);
697 g_set_application_name("Network Configurator");
698 gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(),
700 status_icon = gtk_status_icon_new_from_icon_name("network-offline");
702 gtk_status_icon_set_tooltip_text(status_icon,
703 _("Connecting to dhcpcd ..."));
704 gtk_status_icon_set_visible(status_icon, true);
707 notify_init(PACKAGE);
710 TAILQ_INIT(&wi_scans);
711 g_message(_("Connecting ..."));
714 g_critical("libdhcpcd: %s", strerror(errno));
717 dhcpcd_set_progname(con, "dhcpcd-gtk");
718 dhcpcd_set_status_callback(con, dhcpcd_status_cb, NULL);
719 dhcpcd_set_if_callback(con, dhcpcd_if_cb, NULL);
720 dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, NULL);
721 dhcpcd_wpa_set_status_callback(con, dhcpcd_wpa_status_cb, NULL);
722 if (dhcpcd_try_open(con))
723 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
725 menu_init(status_icon, con);
727 g_timeout_add(DHCPCD_WPA_SCAN_LONG, bgscan, con);