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, NULL);
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) {
162 if (ani_timer != 0) {
163 g_source_remove(ani_timer);
168 animate_online(NULL);
169 ani_timer = g_timeout_add(300, animate_online, NULL);
170 } else if (iscarrier) {
171 animate_carrier(NULL);
172 ani_timer = g_timeout_add(500, animate_carrier, NULL);
174 gtk_status_icon_set_from_icon_name(status_icon,
178 gtk_status_icon_set_tooltip_text(status_icon, msgs);
187 notify_notification_close(nn, NULL);
192 static char *notify_last_msg;
201 notify(const char *title, const char *msg, const char *icon)
206 /* Don't spam the same message */
207 if (notify_last_msg) {
208 if (notify_last_msg && strcmp(msg, notify_last_msg) == 0)
210 g_free(notify_last_msg);
212 notify_last_msg = g_strdup(msg);
215 notify_notification_close(nn, NULL);
217 #if NOTIFY_CHECK_VERSION(0,7,0)
218 nn = notify_notification_new(title, msg, icon);
219 notify_notification_set_hint(nn, "transient",
220 g_variant_new_boolean(TRUE));
222 if (gtk_status_icon_get_visible(status_icon))
223 nn = notify_notification_new_with_status_icon(title,
224 msg, icon, status_icon);
226 nn = notify_notification_new(title, msg, icon, NULL);
229 notify_notification_set_timeout(nn, 5000);
230 g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
231 notify_notification_show(nn, NULL);
234 # define notify(a, b, c)
237 static struct watch *
238 dhcpcd_findwatch(int fd, gpointer data, struct watch **last)
244 for (w = watches; w; w = w->next) {
245 if (w->fd == fd || w->ref == data)
254 dhcpcd_unwatch(int fd, gpointer data)
258 if ((w = dhcpcd_findwatch(fd, data, &l))) {
263 g_source_remove(w->eventid);
264 g_io_channel_unref(w->gio);
271 gboolean (*cb)(GIOChannel *, GIOCondition, gpointer),
280 if ((w = dhcpcd_findwatch(fd, data, &l))) {
287 g_source_remove(w->eventid);
288 g_io_channel_unref(w->gio);
292 gio = g_io_channel_unix_new(fd);
294 g_warning(_("Error creating new GIO Channel\n"));
297 flags = G_IO_IN | G_IO_ERR | G_IO_HUP;
298 if ((eventid = g_io_add_watch(gio, flags, cb, data)) == 0) {
299 g_warning(_("Error creating watch\n"));
300 g_io_channel_unref(gio);
304 w = g_try_malloc(sizeof(*w));
306 g_warning(_("g_try_malloc\n"));
307 g_source_remove(eventid);
308 g_io_channel_unref(gio);
314 w->eventid = eventid;
323 dhcpcd_status_cb(DHCPCD_CONNECTION *con, const char *status,
326 static char *last = NULL;
331 g_message("Status changed to %s", status);
332 if (g_strcmp0(status, "down") == 0) {
334 "Connection to dhcpcd lost" : "dhcpcd not running");
335 if (ani_timer != 0) {
336 g_source_remove(ani_timer);
340 online = carrier = false;
341 gtk_status_icon_set_from_icon_name(status_icon,
343 gtk_status_icon_set_tooltip_text(status_icon, msg);
344 dhcpcd_prefs_abort();
348 dhcpcd_wi_scans_free(wi_scans->scans);
352 dhcpcd_unwatch(-1, con);
353 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
355 if ((last == NULL || g_strcmp0(last, "down") == 0)) {
356 g_message(_("Connected to %s-%s"), "dhcpcd",
357 dhcpcd_version(con));
360 refresh = g_strcmp0(last, "opened") ? false : true;
361 update_online(con, refresh);
365 last = g_strdup(status);
369 dhcpcd_cb(_unused GIOChannel *gio, _unused GIOCondition c, gpointer data)
371 DHCPCD_CONNECTION *con;
373 con = (DHCPCD_CONNECTION *)data;
374 if (dhcpcd_get_fd(con) == -1) {
375 g_warning(_("dhcpcd connection lost"));
376 dhcpcd_unwatch(-1, con);
377 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
381 dhcpcd_dispatch(con);
386 dhcpcd_try_open(gpointer data)
388 DHCPCD_CONNECTION *con;
390 static int last_error;
392 con = (DHCPCD_CONNECTION *)data;
393 fd = dhcpcd_open(con, true);
395 if (errno == EACCES || errno == EPERM) {
396 if ((fd = dhcpcd_open(con, false)) != -1)
399 if (errno != last_error) {
400 g_critical("dhcpcd_open: %s", strerror(errno));
407 if (!dhcpcd_watch(fd, dhcpcd_cb, con)) {
412 /* Start listening to WPA events */
413 dhcpcd_wpa_start(con);
419 dhcpcd_if_cb(DHCPCD_IF *i, _unused void *data)
421 DHCPCD_CONNECTION *con;
426 /* We should ignore renew and stop so we don't annoy the user */
427 if (g_strcmp0(i->reason, "RENEW") &&
428 g_strcmp0(i->reason, "STOP") &&
429 g_strcmp0(i->reason, "STOPPED"))
431 msg = dhcpcd_if_message(i, &new_msg);
433 g_message("%s", msg);
436 icon = "network-transmit-receive";
438 // icon = "network-transmit";
440 icon = "network-offline";
441 notify(_("Network event"), msg, icon);
447 /* Update the tooltip with connection information */
448 con = dhcpcd_if_connection(i);
449 update_online(con, false);
453 dhcpcd_wpa_cb(_unused GIOChannel *gio, _unused GIOCondition c,
459 wpa = (DHCPCD_WPA *)data;
460 if (dhcpcd_wpa_get_fd(wpa) == -1) {
461 dhcpcd_unwatch(-1, wpa);
463 /* If the interface hasn't left, try re-opening */
464 i = dhcpcd_wpa_if(wpa);
466 g_strcmp0(i->reason, "DEPARTED") == 0 ||
467 g_strcmp0(i->reason, "STOPPED") == 0)
469 g_warning(_("dhcpcd WPA connection lost: %s"), i->ifname);
470 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_wpa_try_open, wpa);
474 dhcpcd_wpa_dispatch(wpa);
479 dhcpcd_wpa_try_open(gpointer data)
483 static int last_error;
485 wpa = (DHCPCD_WPA *)data;
486 fd = dhcpcd_wpa_open(wpa);
488 if (errno != last_error)
489 g_critical("dhcpcd_wpa_open: %s", strerror(errno));
494 if (!dhcpcd_watch(fd, dhcpcd_wpa_cb, wpa)) {
495 dhcpcd_wpa_close(wpa);
503 dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, _unused void *data)
507 DHCPCD_WI_SCAN *scans, *s1, *s2;
512 /* This could be a new WPA so watch it */
513 fd = dhcpcd_wpa_get_fd(wpa);
515 g_critical("No fd for WPA %p", wpa);
516 dhcpcd_unwatch(-1, wpa);
519 dhcpcd_watch(fd, dhcpcd_wpa_cb, wpa);
521 i = dhcpcd_wpa_if(wpa);
523 g_critical("No interface for WPA %p", wpa);
526 g_message(_("%s: Received scan results"), i->ifname);
529 scans = dhcpcd_wi_scans(i);
530 if (scans == NULL && errno)
531 g_warning("%s: %s", i->ifname, strerror(errno));
533 for (w = wi_scans; w; w = w->next)
534 if (w->interface == i)
537 w = g_malloc(sizeof(*w));
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);
566 dhcpcd_wi_scans_free(w->scans);
572 dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa, const char *status, _unused void *data)
576 i = dhcpcd_wpa_if(wpa);
577 g_message("%s: WPA status %s", i->ifname, status);
578 if (g_strcmp0(status, "down") == 0)
579 dhcpcd_unwatch(-1, wpa);
583 main(int argc, char *argv[])
585 DHCPCD_CONNECTION *con;
587 setlocale(LC_ALL, "");
588 bindtextdomain(PACKAGE, NULL);
589 bind_textdomain_codeset(PACKAGE, "UTF-8");
592 gtk_init(&argc, &argv);
593 g_set_application_name("Network Configurator");
594 gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(),
596 status_icon = gtk_status_icon_new_from_icon_name("network-offline");
598 gtk_status_icon_set_tooltip_text(status_icon,
599 _("Connecting to dhcpcd ..."));
600 gtk_status_icon_set_visible(status_icon, true);
603 notify_init(PACKAGE);
606 g_message(_("Connecting ..."));
609 g_critical("libdhcpcd: %s", strerror(errno));
612 dhcpcd_set_progname(con, "dhcpcd-gtk");
613 dhcpcd_set_status_callback(con, dhcpcd_status_cb, NULL);
614 dhcpcd_set_if_callback(con, dhcpcd_if_cb, NULL);
615 dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, NULL);
616 dhcpcd_wpa_set_status_callback(con, dhcpcd_wpa_status_cb, NULL);
617 if (dhcpcd_try_open(con))
618 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
620 menu_init(status_icon, con);