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 for (w = wi_scans; w; w = w->next) {
71 for (dw = w->scans; dw; dw = dw->next)
81 animate_carrier(_unused gpointer data)
88 switch(ani_counter++) {
90 icon = "network-transmit";
93 icon = "network-receive";
96 icon = "network-idle";
100 gtk_status_icon_set_from_icon_name(status_icon, icon);
105 animate_online(_unused gpointer data)
112 if (ani_counter++ > 6) {
118 if (ani_counter % 2 == 0)
119 icon = "network-idle";
121 icon = "network-transmit-receive";
122 gtk_status_icon_set_from_icon_name(status_icon, icon);
127 update_online(DHCPCD_CONNECTION *con, bool showif)
129 bool ison, iscarrier;
130 char *msg, *msgs, *tmp;
133 ison = iscarrier = false;
135 ifs = dhcpcd_interfaces(con);
136 for (i = ifs; i; i = i->next) {
137 if (g_strcmp0(i->type, "link") == 0) {
144 msg = dhcpcd_if_message(i);
147 g_message("%s", msg);
149 tmp = g_strconcat(msgs, "\n", msg, NULL);
156 g_message("%s: %s", i->ifname, i->reason);
159 if (online != ison || carrier != iscarrier) {
161 if (ani_timer != 0) {
162 g_source_remove(ani_timer);
167 animate_online(NULL);
168 ani_timer = g_timeout_add(300, animate_online, NULL);
169 } else if (iscarrier) {
170 animate_carrier(NULL);
171 ani_timer = g_timeout_add(500, animate_carrier, NULL);
173 gtk_status_icon_set_from_icon_name(status_icon,
177 gtk_status_icon_set_tooltip_text(status_icon, msgs);
186 notify_notification_close(nn, NULL);
191 static char *notify_last_msg;
200 notify(const char *title, const char *msg, const char *icon)
205 /* Don't spam the same message */
206 if (notify_last_msg) {
207 if (notify_last_msg && strcmp(msg, notify_last_msg) == 0)
209 g_free(notify_last_msg);
211 notify_last_msg = g_strdup(msg);
214 notify_notification_close(nn, NULL);
216 #if NOTIFY_CHECK_VERSION(0,7,0)
217 nn = notify_notification_new(title, msg, icon);
218 notify_notification_set_hint(nn, "transient",
219 g_variant_new_boolean(TRUE));
221 if (gtk_status_icon_get_visible(status_icon))
222 nn = notify_notification_new_with_status_icon(title,
223 msg, icon, status_icon);
225 nn = notify_notification_new(title, msg, icon, NULL);
228 notify_notification_set_timeout(nn, 5000);
229 g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
230 notify_notification_show(nn, NULL);
233 # define notify(a, b, c)
237 dhcpcd_status_cb(DHCPCD_CONNECTION *con, const char *status, _unused void *data)
239 static char *last = NULL;
244 g_message("Status changed to %s", status);
245 if (g_strcmp0(status, "down") == 0) {
247 "Connection to dhcpcd lost" : "dhcpcd not running");
248 if (ani_timer != 0) {
249 g_source_remove(ani_timer);
253 gtk_status_icon_set_from_icon_name(status_icon,
255 gtk_status_icon_set_tooltip_text(status_icon, msg);
256 notify(_("No network"), msg, "network-offline");
257 dhcpcd_prefs_abort();
260 dhcpcd_wi_scans_free(wi_scans->scans);
265 if ((last == NULL || g_strcmp0(last, "down") == 0)) {
266 g_message(_("Connected to %s-%s"), "dhcpcd",
267 dhcpcd_version(con));
271 update_online(con, refresh);
275 last = g_strdup(status);
278 static struct watch *
279 dhcpcd_findwatch(int fd, gpointer data, struct watch **last)
285 for (w = watches; w; w = w->next) {
286 if (w->fd == fd || w->ref == data)
295 dhcpcd_unwatch(int fd, gpointer data)
299 if ((w = dhcpcd_findwatch(fd, data, &l))) {
304 g_source_remove(w->eventid);
305 g_io_channel_unref(w->gio);
312 gboolean (*cb)(GIOChannel *, GIOCondition, gpointer),
321 if ((w = dhcpcd_findwatch(fd, data, &l))) {
328 g_source_remove(w->eventid);
329 g_io_channel_unref(w->gio);
333 gio = g_io_channel_unix_new(fd);
335 g_warning(_("Error creating new GIO Channel\n"));
338 flags = G_IO_IN | G_IO_ERR | G_IO_HUP;
339 if ((eventid = g_io_add_watch(gio, flags, cb, data)) == 0) {
340 g_warning(_("Error creating watch\n"));
341 g_io_channel_unref(gio);
345 w = g_try_malloc(sizeof(*w));
347 g_warning(_("g_try_malloc\n"));
348 g_source_remove(eventid);
349 g_io_channel_unref(gio);
355 w->eventid = eventid;
364 dhcpcd_cb(_unused GIOChannel *gio, _unused GIOCondition c, gpointer data)
366 DHCPCD_CONNECTION *con;
368 con = (DHCPCD_CONNECTION *)data;
369 if (dhcpcd_get_fd(con) == -1) {
370 g_warning(_("dhcpcd connection lost"));
371 dhcpcd_unwatch(-1, con);
372 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
376 dhcpcd_dispatch(con);
381 dhcpcd_try_open(gpointer data)
383 DHCPCD_CONNECTION *con;
385 static int last_error;
387 con = (DHCPCD_CONNECTION *)data;
388 fd = dhcpcd_open(con);
390 if (errno != last_error)
391 g_critical("dhcpcd_open: %s", strerror(errno));
396 if (!dhcpcd_watch(fd, dhcpcd_cb, con)) {
405 dhcpcd_if_cb(DHCPCD_IF *i, _unused void *data)
407 DHCPCD_CONNECTION *con;
411 /* Update the tooltip with connection information */
412 con = dhcpcd_if_connection(i);
413 update_online(con, false);
415 /* We should ignore renew and stop so we don't annoy the user */
416 if (g_strcmp0(i->reason, "RENEW") == 0 ||
417 g_strcmp0(i->reason, "STOP") == 0 ||
418 g_strcmp0(i->reason, "STOPPED") == 0)
421 msg = dhcpcd_if_message(i);
423 g_message("%s", msg);
425 icon = "network-transmit-receive";
427 // icon = "network-transmit";
429 icon = "network-offline";
430 notify(_("Network event"), msg, icon);
436 dhcpcd_wpa_cb(_unused GIOChannel *gio, _unused GIOCondition c,
442 wpa = (DHCPCD_WPA *)data;
443 if (dhcpcd_wpa_get_fd(wpa) == -1) {
444 dhcpcd_unwatch(-1, wpa);
446 /* If the interface hasn't left, try re-opening */
447 i = dhcpcd_wpa_if(wpa);
449 g_strcmp0(i->reason, "DEPARTED") == 0 ||
450 g_strcmp0(i->reason, "STOPPED") == 0)
452 g_warning(_("dhcpcd WPA connection lost: %s"), i->ifname);
453 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_wpa_try_open, wpa);
457 dhcpcd_wpa_dispatch(wpa);
462 dhcpcd_wpa_try_open(gpointer data)
466 static int last_error;
468 wpa = (DHCPCD_WPA *)data;
469 fd = dhcpcd_wpa_open(wpa);
471 if (errno != last_error)
472 g_critical("dhcpcd_wpa_open: %s", strerror(errno));
477 if (!dhcpcd_watch(fd, dhcpcd_wpa_cb, wpa)) {
478 dhcpcd_wpa_close(wpa);
486 dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, _unused void *data)
490 DHCPCD_WI_SCAN *scans, *s1, *s2;
495 /* This could be a new WPA so watch it */
496 fd = dhcpcd_wpa_get_fd(wpa);
498 g_critical("No fd for WPA %p", wpa);
499 dhcpcd_unwatch(-1, wpa);
502 dhcpcd_watch(fd, dhcpcd_wpa_cb, wpa);
504 i = dhcpcd_wpa_if(wpa);
506 g_critical("No interface for WPA %p", wpa);
509 g_message(_("%s: Received scan results"), i->ifname);
512 scans = dhcpcd_wi_scans(i);
513 if (scans == NULL && errno)
514 g_warning("%s: %s", i->ifname, strerror(errno));
516 for (w = wi_scans; w; w = w->next)
517 if (w->interface == i)
520 w = g_malloc(sizeof(*w));
526 msg = N_("New Access Point");
527 for (s1 = scans; s1; s1 = s1->next) {
528 for (s2 = w->scans; s2; s2 = s2->next)
529 if (g_strcmp0(s1->ssid, s2->ssid) == 0)
533 txt = g_strdup(s1->ssid);
535 msg = N_("New Access Points");
536 t = g_strconcat(txt, "\n",
544 notify(msg, txt, "network-wireless");
547 dhcpcd_wi_scans_free(w->scans);
553 main(int argc, char *argv[])
555 DHCPCD_CONNECTION *con;
557 setlocale(LC_ALL, "");
558 bindtextdomain(PACKAGE, NULL);
559 bind_textdomain_codeset(PACKAGE, "UTF-8");
562 gtk_init(&argc, &argv);
563 g_set_application_name("Network Configurator");
564 gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(),
566 status_icon = gtk_status_icon_new_from_icon_name("network-offline");
568 gtk_status_icon_set_tooltip_text(status_icon,
569 _("Connecting to dhcpcd ..."));
570 gtk_status_icon_set_visible(status_icon, true);
573 notify_init(PACKAGE);
576 g_message(_("Connecting ..."));
579 g_critical("libdhcpcd: %s", strerror(errno));
582 dhcpcd_set_status_callback(con, dhcpcd_status_cb, NULL);
583 dhcpcd_set_if_callback(con, dhcpcd_if_cb, NULL);
584 dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, NULL);
585 //dhcpcd_wpa_set_status_callback(con, dhcpcd_wpa_status_cb, NULL);
586 if (dhcpcd_try_open(con))
587 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
589 menu_init(status_icon, con);