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;
41 #include "dhcpcd-gtk.h"
43 static GtkStatusIcon *status_icon;
44 static guint ani_timer;
45 static int ani_counter;
55 static struct watch *watches;
60 wi_scan_find(DHCPCD_WI_SCAN *scan)
65 for (w = wi_scans; w; w = w->next) {
66 for (dw = w->scans; dw; dw = dw->next)
76 animate_carrier(_unused gpointer data)
83 switch(ani_counter++) {
85 icon = "network-transmit";
88 icon = "network-receive";
91 icon = "network-idle";
95 gtk_status_icon_set_from_icon_name(status_icon, icon);
100 animate_online(_unused gpointer data)
107 if (ani_counter++ > 6) {
113 if (ani_counter % 2 == 0)
114 icon = "network-idle";
116 icon = "network-transmit-receive";
117 gtk_status_icon_set_from_icon_name(status_icon, icon);
122 update_online(DHCPCD_CONNECTION *con, bool showif)
124 bool ison, iscarrier, isstop;
125 char *msg, *msgs, *tmp;
126 DHCPCD_IF *ifs, *i, *j;
128 ison = iscarrier = false;
130 ifs = dhcpcd_interfaces(con);
131 for (i = ifs; i; i = i->next) {
133 g_message("%s: %s", i->ifname, i->reason);
134 if (strcmp(i->reason, "RELEASE") == 0 ||
135 strcmp(i->reason, "STOP") == 0)
137 if (strcmp(i->type, "ipv4") != 0) {
139 for (j = ifs; j; j = j->next)
140 if (strcmp(j->ifname, i->ifname) == 0 &&
141 strcmp(j->type, "ipv4") == 0)
143 if (strcmp(j->reason, "STOP") == 0)
151 ison = iscarrier = true;
152 if (!iscarrier && g_strcmp0(i->reason, "CARRIER") == 0)
154 msg = dhcpcd_if_message(i);
157 tmp = g_strconcat(msgs, "\n", msg, NULL);
166 if (online != ison || carrier != iscarrier) {
168 if (ani_timer != 0) {
169 g_source_remove(ani_timer);
174 animate_online(NULL);
175 ani_timer = g_timeout_add(300, animate_online, NULL);
176 } else if (iscarrier) {
177 animate_carrier(NULL);
178 ani_timer = g_timeout_add(500, animate_carrier, NULL);
180 gtk_status_icon_set_from_icon_name(status_icon,
184 gtk_status_icon_set_tooltip_text(status_icon, msgs);
193 notify_notification_close(nn, NULL);
198 static char *notify_last_msg;
207 notify(const char *title, const char *msg, const char *icon)
213 /* Don't spam the same message */
214 if (notify_last_msg) {
216 if (notify_last_msg && strcmp(msg, notify_last_msg) == 0)
218 g_free(notify_last_msg);
220 notify_last_msg = g_strdup(msg);
222 msgs = g_strsplit(msg, "\n", 0);
223 for (m = msgs; *m; m++)
228 notify_notification_close(nn, NULL);
230 #if NOTIFY_CHECK_VERSION(0,7,0)
231 nn = notify_notification_new(title, msg, icon);
232 notify_notification_set_hint(nn, "transient",
233 g_variant_new_boolean(TRUE));
235 if (gtk_status_icon_get_visible(status_icon))
236 nn = notify_notification_new_with_status_icon(title,
237 msg, icon, status_icon);
239 nn = notify_notification_new(title, msg, icon, NULL);
242 notify_notification_set_timeout(nn, 5000);
243 g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
244 notify_notification_show(nn, NULL);
247 # define notify(a, b, c)
251 event_cb(DHCPCD_CONNECTION *con, DHCPCD_IF *i, _unused void *data)
256 g_message("%s: %s", i->ifname, i->reason);
257 update_online(con, false);
259 /* We should ignore renew and stop so we don't annoy the user */
260 if (g_strcmp0(i->reason, "RENEW") == 0 ||
261 g_strcmp0(i->reason, "STOP") == 0)
264 msg = dhcpcd_if_message(i);
266 icon = "network-transmit-receive";
268 // icon = "network-transmit";
270 icon = "network-offline";
271 notify(_("Network event"), msg, icon);
276 status_cb(DHCPCD_CONNECTION *con, const char *status, _unused void *data)
278 static char *last = NULL;
284 g_message("Status changed to %s", status);
285 if (g_strcmp0(status, "down") == 0) {
287 "Connection to dhcpcd lost" : "dhcpcd not running");
288 if (ani_timer != 0) {
289 g_source_remove(ani_timer);
293 gtk_status_icon_set_from_icon_name(status_icon,
295 gtk_status_icon_set_tooltip_text(status_icon, msg);
296 notify(_("No network"), msg, "network-offline");
297 dhcpcd_prefs_abort();
300 dhcpcd_wi_scans_free(wi_scans->scans);
305 if ((last == NULL || g_strcmp0(last, "down") == 0) &&
306 dhcpcd_command(con, "GetDhcpcdVersion", NULL, &version))
308 g_message(_("Connected to %s-%s"), "dhcpcd", version);
313 update_online(con, refresh);
315 last = g_strdup(status);
319 scan_cb(DHCPCD_CONNECTION *con, DHCPCD_IF *i, _unused void *data)
322 DHCPCD_WI_SCAN *scans, *s1, *s2;
326 g_message(_("%s: Received scan results"), i->ifname);
327 scans = dhcpcd_wi_scans(con, i);
328 if (scans == NULL && dhcpcd_error(con) != NULL) {
329 g_warning("%s: %s", i->ifname, dhcpcd_error(con));
330 dhcpcd_error_clear(con);
332 for (w = wi_scans; w; w = w->next)
333 if (w->connection == con && w->interface == i)
336 w = g_malloc(sizeof(*w));
343 msg = N_("New Access Point");
344 for (s1 = scans; s1; s1 = s1->next) {
345 for (s2 = w->scans; s2; s2 = s2->next)
346 if (g_strcmp0(s1->ssid, s2->ssid) == 0)
350 txt = g_strdup(s1->ssid);
352 msg = N_("New Access Points");
353 t = g_strconcat(txt, "\n",
361 notify(msg, txt, "network-wireless");
364 dhcpcd_wi_scans_free(w->scans);
370 gio_callback(GIOChannel *gio, _unused GIOCondition c, _unused gpointer d)
374 fd = g_io_channel_unix_get_fd(gio);
380 delete_watch_cb(_unused DHCPCD_CONNECTION *con, const struct pollfd *fd,
386 for (w = watches; w; w = w->next) {
387 if (w->pollfd.fd == fd->fd) {
392 g_source_remove(w->eventid);
393 g_io_channel_unref(w->gio);
401 add_watch_cb(DHCPCD_CONNECTION *con, const struct pollfd *fd,
409 /* Remove any existing watch */
410 delete_watch_cb(con, fd, data);
412 gio = g_io_channel_unix_new(fd->fd);
414 g_warning(_("Error creating new GIO Channel\n"));
418 if (fd->events & POLLIN)
420 if (fd->events & POLLOUT)
422 if (fd->events & POLLERR)
424 if (fd->events & POLLHUP)
426 if ((eventid = g_io_add_watch(gio, flags, gio_callback, con)) == 0) {
427 g_io_channel_unref(gio);
428 g_warning(_("Error creating watch\n"));
431 w = g_malloc(sizeof(*w));
432 memcpy(&w->pollfd, fd, sizeof(w->pollfd));
433 w->eventid = eventid;
440 main(int argc, char *argv[])
443 char *version = NULL;
444 DHCPCD_CONNECTION *con;
446 setlocale(LC_ALL, "");
447 bindtextdomain(PACKAGE, NULL);
448 bind_textdomain_codeset(PACKAGE, "UTF-8");
451 gtk_init(&argc, &argv);
452 g_set_application_name("Network Configurator");
453 gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(),
455 status_icon = gtk_status_icon_new_from_icon_name("network-offline");
457 gtk_status_icon_set_tooltip_text(status_icon,
458 _("Connecting to dhcpcd ..."));
459 gtk_status_icon_set_visible(status_icon, true);
462 notify_init(PACKAGE);
465 g_message(_("Connecting ..."));
466 con = dhcpcd_open(&error);
468 g_critical("libdhcpcd: %s", error);
472 gtk_status_icon_set_tooltip_text(status_icon,
473 _("Triggering dhcpcd ..."));
476 if (!dhcpcd_command(con, "GetVersion", NULL, &version)) {
477 g_critical("libdhcpcd: GetVersion: %s", dhcpcd_error(con));
480 g_message(_("Connected to %s-%s"), "dhcpcd-dbus", version);
483 dhcpcd_set_watch_functions(con, add_watch_cb, delete_watch_cb, NULL);
484 dhcpcd_set_signal_functions(con, event_cb, status_cb, scan_cb, NULL);
485 if (dhcpcd_error(con)) {
486 g_critical("libdhcpcd: %s", dhcpcd_error(con));
490 menu_init(status_icon, con);