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 animate_carrier(_unused gpointer data)
86 switch(ani_counter++) {
88 icon = "network-transmit";
91 icon = "network-receive";
94 icon = "network-idle";
98 gtk_status_icon_set_from_icon_name(status_icon, icon);
103 animate_online(_unused gpointer data)
110 if (ani_counter++ > 6) {
116 if (ani_counter % 2 == 0)
117 icon = "network-idle";
119 icon = "network-transmit-receive";
120 gtk_status_icon_set_from_icon_name(status_icon, icon);
125 update_online(DHCPCD_CONNECTION *con, bool showif)
127 bool ison, iscarrier;
128 char *msg, *msgs, *tmp;
131 ison = iscarrier = false;
133 ifs = dhcpcd_interfaces(con);
134 for (i = ifs; i; i = i->next) {
135 if (g_strcmp0(i->type, "link") == 0) {
142 msg = dhcpcd_if_message(i, NULL);
145 g_message("%s", msg);
147 tmp = g_strconcat(msgs, "\n", msg, NULL);
154 g_message("%s: %s", i->ifname, i->reason);
157 if (online != ison || carrier != iscarrier) {
160 if (ani_timer != 0) {
161 g_source_remove(ani_timer);
166 animate_online(NULL);
167 ani_timer = g_timeout_add(300, animate_online, NULL);
168 } else if (iscarrier) {
169 animate_carrier(NULL);
170 ani_timer = g_timeout_add(500, animate_carrier, NULL);
172 gtk_status_icon_set_from_icon_name(status_icon,
176 gtk_status_icon_set_tooltip_text(status_icon, msgs);
185 notify_notification_close(nn, NULL);
190 static char *notify_last_msg;
199 notify(const char *title, const char *msg, const char *icon)
204 /* Don't spam the same message */
205 if (notify_last_msg) {
206 if (notify_last_msg && strcmp(msg, notify_last_msg) == 0)
208 g_free(notify_last_msg);
210 notify_last_msg = g_strdup(msg);
213 notify_notification_close(nn, NULL);
215 #if NOTIFY_CHECK_VERSION(0,7,0)
216 nn = notify_notification_new(title, msg, icon);
217 notify_notification_set_hint(nn, "transient",
218 g_variant_new_boolean(TRUE));
220 if (gtk_status_icon_get_visible(status_icon))
221 nn = notify_notification_new_with_status_icon(title,
222 msg, icon, status_icon);
224 nn = notify_notification_new(title, msg, icon, NULL);
227 notify_notification_set_timeout(nn, 5000);
228 g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
229 notify_notification_show(nn, NULL);
232 # define notify(a, b, c)
235 static struct watch *
236 dhcpcd_findwatch(int fd, gpointer data, struct watch **last)
242 for (w = watches; w; w = w->next) {
243 if (w->fd == fd || w->ref == data)
252 dhcpcd_unwatch(int fd, gpointer data)
256 if ((w = dhcpcd_findwatch(fd, data, &l))) {
261 g_source_remove(w->eventid);
262 g_io_channel_unref(w->gio);
269 gboolean (*cb)(GIOChannel *, GIOCondition, gpointer),
278 if ((w = dhcpcd_findwatch(fd, data, &l))) {
285 g_source_remove(w->eventid);
286 g_io_channel_unref(w->gio);
290 gio = g_io_channel_unix_new(fd);
292 g_warning(_("Error creating new GIO Channel\n"));
295 flags = G_IO_IN | G_IO_ERR | G_IO_HUP;
296 if ((eventid = g_io_add_watch(gio, flags, cb, data)) == 0) {
297 g_warning(_("Error creating watch\n"));
298 g_io_channel_unref(gio);
302 w = g_try_malloc(sizeof(*w));
304 g_warning(_("g_try_malloc\n"));
305 g_source_remove(eventid);
306 g_io_channel_unref(gio);
312 w->eventid = eventid;
321 dhcpcd_status_cb(DHCPCD_CONNECTION *con, const char *status,
324 static char *last = NULL;
329 g_message("Status changed to %s", status);
330 if (g_strcmp0(status, "down") == 0) {
332 "Connection to dhcpcd lost" : "dhcpcd not running");
333 if (ani_timer != 0) {
334 g_source_remove(ani_timer);
338 online = carrier = false;
339 gtk_status_icon_set_from_icon_name(status_icon,
341 gtk_status_icon_set_tooltip_text(status_icon, msg);
345 while ((w = TAILQ_FIRST(&wi_scans))) {
346 TAILQ_REMOVE(&wi_scans, w, next);
347 dhcpcd_wi_scans_free(w->scans);
350 dhcpcd_unwatch(-1, con);
351 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
353 if ((last == NULL || g_strcmp0(last, "down") == 0)) {
354 g_message(_("Connected to %s-%s"), "dhcpcd",
355 dhcpcd_version(con));
358 refresh = g_strcmp0(last, "opened") ? false : true;
359 update_online(con, refresh);
363 last = g_strdup(status);
367 dhcpcd_cb(_unused GIOChannel *gio, _unused GIOCondition c, gpointer data)
369 DHCPCD_CONNECTION *con;
371 con = (DHCPCD_CONNECTION *)data;
372 if (dhcpcd_get_fd(con) == -1) {
373 g_warning(_("dhcpcd connection lost"));
374 dhcpcd_unwatch(-1, con);
375 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
379 dhcpcd_dispatch(con);
384 dhcpcd_try_open(gpointer data)
386 DHCPCD_CONNECTION *con;
388 static int last_error;
390 con = (DHCPCD_CONNECTION *)data;
391 fd = dhcpcd_open(con, true);
393 if (errno == EACCES || errno == EPERM) {
394 if ((fd = dhcpcd_open(con, false)) != -1)
397 if (errno != last_error) {
398 g_critical("dhcpcd_open: %s", strerror(errno));
405 if (!dhcpcd_watch(fd, dhcpcd_cb, con)) {
410 /* Start listening to WPA events */
411 dhcpcd_wpa_start(con);
417 dhcpcd_if_cb(DHCPCD_IF *i, _unused void *data)
419 DHCPCD_CONNECTION *con;
424 /* We should ignore renew and stop so we don't annoy the user */
425 if (g_strcmp0(i->reason, "RENEW") &&
426 g_strcmp0(i->reason, "STOP") &&
427 g_strcmp0(i->reason, "STOPPED"))
429 msg = dhcpcd_if_message(i, &new_msg);
431 g_message("%s", msg);
434 icon = "network-transmit-receive";
436 // icon = "network-transmit";
438 icon = "network-offline";
439 notify(_("Network event"), msg, icon);
445 /* Update the tooltip with connection information */
446 con = dhcpcd_if_connection(i);
447 update_online(con, false);
451 dhcpcd_wpa_cb(_unused GIOChannel *gio, _unused GIOCondition c,
457 wpa = (DHCPCD_WPA *)data;
458 if (dhcpcd_wpa_get_fd(wpa) == -1) {
459 dhcpcd_unwatch(-1, wpa);
461 /* If the interface hasn't left, try re-opening */
462 i = dhcpcd_wpa_if(wpa);
464 g_strcmp0(i->reason, "DEPARTED") == 0 ||
465 g_strcmp0(i->reason, "STOPPED") == 0)
467 g_warning(_("dhcpcd WPA connection lost: %s"), i->ifname);
468 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_wpa_try_open, wpa);
472 dhcpcd_wpa_dispatch(wpa);
477 dhcpcd_wpa_try_open(gpointer data)
481 static int last_error;
483 wpa = (DHCPCD_WPA *)data;
484 fd = dhcpcd_wpa_open(wpa);
486 if (errno != last_error)
487 g_critical("dhcpcd_wpa_open: %s", strerror(errno));
492 if (!dhcpcd_watch(fd, dhcpcd_wpa_cb, wpa)) {
493 dhcpcd_wpa_close(wpa);
501 dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, _unused void *data)
505 DHCPCD_WI_SCAN *scans, *s1, *s2;
510 /* This could be a new WPA so watch it */
511 fd = dhcpcd_wpa_get_fd(wpa);
513 g_critical("No fd for WPA %p", wpa);
514 dhcpcd_unwatch(-1, wpa);
517 dhcpcd_watch(fd, dhcpcd_wpa_cb, wpa);
519 i = dhcpcd_wpa_if(wpa);
521 g_critical("No interface for WPA %p", wpa);
524 g_message(_("%s: Received scan results"), i->ifname);
527 scans = dhcpcd_wi_scans(i);
528 if (scans == NULL && errno)
529 g_warning("%s: %s", i->ifname, strerror(errno));
531 TAILQ_FOREACH(w, &wi_scans, next) {
532 if (w->interface == i)
536 w = g_malloc(sizeof(*w));
540 TAILQ_INIT(&w->menus);
541 TAILQ_INSERT_TAIL(&wi_scans, w, next);
544 msg = N_("New Access Point");
545 for (s1 = scans; s1; s1 = s1->next) {
546 for (s2 = w->scans; s2; s2 = s2->next)
547 if (g_strcmp0(s1->ssid, s2->ssid) == 0)
551 txt = g_strdup(s1->ssid);
553 msg = N_("New Access Points");
554 t = g_strconcat(txt, "\n",
562 notify(msg, txt, "network-wireless");
565 menu_update_scans(w, scans);
570 dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa, const char *status, _unused void *data)
574 i = dhcpcd_wpa_if(wpa);
575 g_message("%s: WPA status %s", i->ifname, status);
576 if (g_strcmp0(status, "down") == 0)
577 dhcpcd_unwatch(-1, wpa);
581 main(int argc, char *argv[])
583 DHCPCD_CONNECTION *con;
585 setlocale(LC_ALL, "");
586 bindtextdomain(PACKAGE, NULL);
587 bind_textdomain_codeset(PACKAGE, "UTF-8");
590 gtk_init(&argc, &argv);
591 g_set_application_name("Network Configurator");
592 gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(),
594 status_icon = gtk_status_icon_new_from_icon_name("network-offline");
596 gtk_status_icon_set_tooltip_text(status_icon,
597 _("Connecting to dhcpcd ..."));
598 gtk_status_icon_set_visible(status_icon, true);
601 notify_init(PACKAGE);
604 TAILQ_INIT(&wi_scans);
605 g_message(_("Connecting ..."));
608 g_critical("libdhcpcd: %s", strerror(errno));
611 dhcpcd_set_progname(con, "dhcpcd-gtk");
612 dhcpcd_set_status_callback(con, dhcpcd_status_cb, NULL);
613 dhcpcd_set_if_callback(con, dhcpcd_if_cb, NULL);
614 dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, NULL);
615 dhcpcd_wpa_set_status_callback(con, dhcpcd_wpa_status_cb, NULL);
616 if (dhcpcd_try_open(con))
617 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
619 menu_init(status_icon, con);