54090fe18e731b4a0bfd488e7828e79b1278bbbc
[dhcpcd-ui] / src / dhcpcd-gtk / main.c
1 /*
2  * dhcpcd-gtk
3  * Copyright 2009-2014 Roy Marples <roy@marples.name>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 #include <errno.h>
28 #include <locale.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #ifdef NOTIFY
33 #  include <libnotify/notify.h>
34 #ifndef NOTIFY_CHECK_VERSION
35 #  define NOTIFY_CHECK_VERSION(a,b,c) 0
36 #endif
37 static NotifyNotification *nn;
38 #endif
39
40 #include "config.h"
41 #include "dhcpcd.h"
42 #include "dhcpcd-gtk.h"
43
44 static GtkStatusIcon *status_icon;
45 static guint ani_timer;
46 static int ani_counter;
47 static bool online;
48 static bool carrier;
49
50 struct watch {
51         gpointer ref;
52         int fd;
53         guint eventid;
54         GIOChannel *gio;
55         struct watch *next;
56 };
57 static struct watch *watches;
58
59 WI_SCAN *wi_scans;
60
61 static gboolean dhcpcd_try_open(gpointer data);
62 static gboolean dhcpcd_wpa_try_open(gpointer data);
63
64 WI_SCAN *
65 wi_scan_find(DHCPCD_WI_SCAN *scan)
66 {
67         WI_SCAN *w;
68         DHCPCD_WI_SCAN *dw;
69
70         for (w = wi_scans; w; w = w->next) {
71                 for (dw = w->scans; dw; dw = dw->next)
72                         if (dw == scan)
73                                 break;
74                 if (dw)
75                         return w;
76         }
77         return NULL;
78 }
79
80 static gboolean
81 animate_carrier(_unused gpointer data)
82 {
83         const char *icon;
84
85         if (ani_timer == 0)
86                 return false;
87
88         switch(ani_counter++) {
89         case 0:
90                 icon = "network-transmit";
91                 break;
92         case 1:
93                 icon = "network-receive";
94                 break;
95         default:
96                 icon = "network-idle";
97                 ani_counter = 0;
98                 break;
99         }
100         gtk_status_icon_set_from_icon_name(status_icon, icon);
101         return true;
102 }
103
104 static gboolean
105 animate_online(_unused gpointer data)
106 {
107         const char *icon;
108
109         if (ani_timer == 0)
110                 return false;
111
112         if (ani_counter++ > 6) {
113                 ani_timer = 0;
114                 ani_counter = 0;
115                 return false;
116         }
117
118         if (ani_counter % 2 == 0)
119                 icon = "network-idle";
120         else
121                 icon = "network-transmit-receive";
122         gtk_status_icon_set_from_icon_name(status_icon, icon);
123         return true;
124 }
125
126 static void
127 update_online(DHCPCD_CONNECTION *con, bool showif)
128 {
129         bool ison, iscarrier;
130         char *msg, *msgs, *tmp;
131         DHCPCD_IF *ifs, *i;
132
133         ison = iscarrier = false;
134         msgs = NULL;
135         ifs = dhcpcd_interfaces(con);
136         for (i = ifs; i; i = i->next) {
137                 if (g_strcmp0(i->type, "link") == 0) {
138                         if (i->up)
139                                 iscarrier = true;
140                 } else {
141                         if (i->up)
142                                 ison = true;
143                 }
144                 msg = dhcpcd_if_message(i, NULL);
145                 if (msg) {
146                         if (showif)
147                                 g_message("%s", msg);
148                         if (msgs) {
149                                 tmp = g_strconcat(msgs, "\n", msg, NULL);
150                                 g_free(msgs);
151                                 g_free(msg);
152                                 msgs = tmp;
153                         } else
154                                 msgs = msg;
155                 } else if (showif)
156                         g_message("%s: %s", i->ifname, i->reason);
157         }
158
159         if (online != ison || carrier != iscarrier) {
160                 online = ison;
161                 if (ani_timer != 0) {
162                         g_source_remove(ani_timer);
163                         ani_timer = 0;
164                         ani_counter = 0;
165                 }
166                 if (ison) {
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);
172                 } else {
173                         gtk_status_icon_set_from_icon_name(status_icon,
174                             "network-offline");
175                 }
176         }
177         gtk_status_icon_set_tooltip_text(status_icon, msgs);
178         g_free(msgs);
179 }
180
181 void
182 notify_close(void)
183 {
184 #ifdef NOTIFY
185         if (nn != NULL)
186                 notify_notification_close(nn, NULL);
187 #endif
188 }
189
190 #ifdef NOTIFY
191 static char *notify_last_msg;
192
193 static void
194 notify_closed(void)
195 {
196         nn = NULL;
197 }
198
199 static void
200 notify(const char *title, const char *msg, const char *icon)
201 {
202
203         if (msg == NULL)
204                 return;
205         /* Don't spam the same message */
206         if (notify_last_msg) {
207                 if (notify_last_msg && strcmp(msg, notify_last_msg) == 0)
208                         return;
209                 g_free(notify_last_msg);
210         }
211         notify_last_msg = g_strdup(msg);
212
213         if (nn != NULL)
214                 notify_notification_close(nn, NULL);
215
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));
220 #else
221         if (gtk_status_icon_get_visible(status_icon))
222                 nn = notify_notification_new_with_status_icon(title,
223                     msg, icon, status_icon);
224         else
225                 nn = notify_notification_new(title, msg, icon, NULL);
226 #endif
227
228         notify_notification_set_timeout(nn, 5000);
229         g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
230         notify_notification_show(nn, NULL);
231 }
232 #else
233 #  define notify(a, b, c)
234 #endif
235
236 static struct watch *
237 dhcpcd_findwatch(int fd, gpointer data, struct watch **last)
238 {
239         struct watch *w;
240
241         if (last)
242                 *last = NULL;
243         for (w = watches; w; w = w->next) {
244                 if (w->fd == fd || w->ref == data)
245                         return w;
246                 if (last)
247                         *last = w;
248         }
249         return NULL;
250 }
251
252 static void
253 dhcpcd_unwatch(int fd, gpointer data)
254 {
255         struct watch *w, *l;
256
257         if ((w = dhcpcd_findwatch(fd, data, &l))) {
258                 if (l)
259                         l->next = w->next;
260                 else
261                         watches = w->next;
262                 g_source_remove(w->eventid);
263                 g_io_channel_unref(w->gio);
264                 g_free(w);
265         }
266 }
267
268 static gboolean
269 dhcpcd_watch(int fd,
270     gboolean (*cb)(GIOChannel *, GIOCondition, gpointer),
271     gpointer data)
272 {
273         struct watch *w, *l;
274         GIOChannel *gio;
275         GIOCondition flags;
276         guint eventid;
277
278         /* Sanity */
279         if ((w = dhcpcd_findwatch(fd, data, &l))) {
280                 if (w->fd == fd)
281                         return TRUE;
282                 if (l)
283                         l->next = w->next;
284                 else
285                         watches = w->next;
286                 g_source_remove(w->eventid);
287                 g_io_channel_unref(w->gio);
288                 g_free(w);
289         }
290
291         gio = g_io_channel_unix_new(fd);
292         if (gio == NULL) {
293                 g_warning(_("Error creating new GIO Channel\n"));
294                 return FALSE;
295         }
296         flags = G_IO_IN | G_IO_ERR | G_IO_HUP;
297         if ((eventid = g_io_add_watch(gio, flags, cb, data)) == 0) {
298                 g_warning(_("Error creating watch\n"));
299                 g_io_channel_unref(gio);
300                 return FALSE;
301         }
302
303         w = g_try_malloc(sizeof(*w));
304         if (w == NULL) {
305                 g_warning(_("g_try_malloc\n"));
306                 g_source_remove(eventid);
307                 g_io_channel_unref(gio);
308                 return FALSE;
309         }
310
311         w->ref = data;
312         w->fd = fd;
313         w->eventid = eventid;
314         w->gio = gio;
315         w->next = watches;
316         watches = w;
317
318         return TRUE;
319 }
320
321 static void
322 dhcpcd_status_cb(DHCPCD_CONNECTION *con, const char *status, _unused void *data)
323 {
324         static char *last = NULL;
325         const char *msg;
326         bool refresh;
327         WI_SCAN *w;
328
329         g_message("Status changed to %s", status);
330         if (g_strcmp0(status, "down") == 0) {
331                 msg = N_(last ?
332                     "Connection to dhcpcd lost" : "dhcpcd not running");
333                 if (ani_timer != 0) {
334                         g_source_remove(ani_timer);
335                         ani_timer = 0;
336                         ani_counter = 0;
337                 }
338                 gtk_status_icon_set_from_icon_name(status_icon,
339                     "network-offline");
340                 gtk_status_icon_set_tooltip_text(status_icon, msg);
341                 notify(_("No network"), msg, "network-offline");
342                 dhcpcd_prefs_abort();
343                 while (wi_scans) {
344                         w = wi_scans->next;
345                         dhcpcd_wi_scans_free(wi_scans->scans);
346                         g_free(wi_scans);
347                         wi_scans = w;
348                 }
349                 dhcpcd_unwatch(-1, con);
350                 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
351         } else {
352                 if ((last == NULL || g_strcmp0(last, "down") == 0)) {
353                         g_message(_("Connected to %s-%s"), "dhcpcd",
354                             dhcpcd_version(con));
355                         refresh = true;
356                 } else
357                         refresh = g_strcmp0(last, "opened") ? false : true;
358                 update_online(con, refresh);
359         }
360
361         g_free(last);
362         last = g_strdup(status);
363 }
364
365 static gboolean
366 dhcpcd_cb(_unused GIOChannel *gio, _unused GIOCondition c, gpointer data)
367 {
368         DHCPCD_CONNECTION *con;
369
370         con = (DHCPCD_CONNECTION *)data;
371         if (dhcpcd_get_fd(con) == -1) {
372                 g_warning(_("dhcpcd connection lost"));
373                 dhcpcd_unwatch(-1, con);
374                 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
375                 return FALSE;
376         }
377
378         dhcpcd_dispatch(con);
379         return TRUE;
380 }
381
382 static gboolean
383 dhcpcd_try_open(gpointer data)
384 {
385         DHCPCD_CONNECTION *con;
386         int fd;
387         static int last_error;
388
389         con = (DHCPCD_CONNECTION *)data;
390         fd = dhcpcd_open(con);
391         if (fd == -1) {
392                 if (errno != last_error) {
393                         g_critical("dhcpcd_open: %s", strerror(errno));
394                         last_error = errno;
395                 }
396                 return TRUE;
397         }
398
399         if (!dhcpcd_watch(fd, dhcpcd_cb, con)) {
400                 dhcpcd_close(con);
401                 return TRUE;
402         }
403
404         /* Start listening to WPA events */
405         dhcpcd_wpa_start(con);
406
407         return FALSE;
408 }
409
410 static void
411 dhcpcd_if_cb(DHCPCD_IF *i, _unused void *data)
412 {
413         DHCPCD_CONNECTION *con;
414         char *msg;
415         const char *icon;
416         bool new_msg;
417
418         /* Update the tooltip with connection information */
419         con = dhcpcd_if_connection(i);
420         update_online(con, false);
421
422         /* We should ignore renew and stop so we don't annoy the user */
423         if (g_strcmp0(i->reason, "RENEW") == 0 ||
424             g_strcmp0(i->reason, "STOP") == 0 ||
425             g_strcmp0(i->reason, "STOPPED") == 0)
426                 return;
427
428         msg = dhcpcd_if_message(i, &new_msg);
429         if (msg) {
430                 g_message("%s", msg);
431                 if (new_msg) {
432                         if (i->up)
433                                 icon = "network-transmit-receive";
434                         //else
435                         //      icon = "network-transmit";
436                         if (!i->up)
437                                 icon = "network-offline";
438                         notify(_("Network event"), msg, icon);
439                 }
440                 g_free(msg);
441         }
442 }
443
444 static gboolean
445 dhcpcd_wpa_cb(_unused GIOChannel *gio, _unused GIOCondition c,
446     gpointer data)
447 {
448         DHCPCD_WPA *wpa;
449         DHCPCD_IF *i;
450
451         wpa = (DHCPCD_WPA *)data;
452         if (dhcpcd_wpa_get_fd(wpa) == -1) {
453                 dhcpcd_unwatch(-1, wpa);
454
455                 /* If the interface hasn't left, try re-opening */
456                 i = dhcpcd_wpa_if(wpa);
457                 if (i == NULL ||
458                     g_strcmp0(i->reason, "DEPARTED") == 0 ||
459                     g_strcmp0(i->reason, "STOPPED") == 0)
460                         return TRUE;
461                 g_warning(_("dhcpcd WPA connection lost: %s"), i->ifname);
462                 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_wpa_try_open, wpa);
463                 return FALSE;
464         }
465
466         dhcpcd_wpa_dispatch(wpa);
467         return TRUE;
468 }
469
470 static gboolean
471 dhcpcd_wpa_try_open(gpointer data)
472 {
473         DHCPCD_WPA *wpa;
474         int fd;
475         static int last_error;
476
477         wpa = (DHCPCD_WPA *)data;
478         fd = dhcpcd_wpa_open(wpa);
479         if (fd == -1) {
480                 if (errno != last_error)
481                         g_critical("dhcpcd_wpa_open: %s", strerror(errno));
482                 last_error = errno;
483                 return TRUE;
484         }
485
486         if (!dhcpcd_watch(fd, dhcpcd_wpa_cb, wpa)) {
487                 dhcpcd_wpa_close(wpa);
488                 return TRUE;
489         }
490
491         return FALSE;
492 }
493
494 static void
495 dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, _unused void *data)
496 {
497         DHCPCD_IF *i;
498         WI_SCAN *w;
499         DHCPCD_WI_SCAN *scans, *s1, *s2;
500         char *txt, *t;
501         int lerrno, fd;
502         const char *msg;
503
504         /* This could be a new WPA so watch it */
505         fd = dhcpcd_wpa_get_fd(wpa);
506         if (fd == -1) {
507                 g_critical("No fd for WPA %p", wpa);
508                 dhcpcd_unwatch(-1, wpa);
509                 return;
510         }
511         dhcpcd_watch(fd, dhcpcd_wpa_cb, wpa);
512
513         i = dhcpcd_wpa_if(wpa);
514         if (i == NULL) {
515                 g_critical("No interface for WPA %p", wpa);
516                 return;
517         }
518         g_message(_("%s: Received scan results"), i->ifname);
519         lerrno = errno;
520         errno = 0;
521         scans = dhcpcd_wi_scans(i);
522         if (scans == NULL && errno)
523                 g_warning("%s: %s", i->ifname, strerror(errno));
524         errno = lerrno;
525         for (w = wi_scans; w; w = w->next)
526                 if (w->interface == i)
527                         break;
528         if (w == NULL) {
529                 w = g_malloc(sizeof(*w));
530                 w->interface = i;
531                 w->next = wi_scans;
532                 wi_scans = w;
533         } else {
534                 txt = NULL;
535                 msg = N_("New Access Point");
536                 for (s1 = scans; s1; s1 = s1->next) {
537                         for (s2 = w->scans; s2; s2 = s2->next)
538                                 if (g_strcmp0(s1->ssid, s2->ssid) == 0)
539                                         break;
540                         if (s2 == NULL) {
541                                 if (txt == NULL)
542                                         txt = g_strdup(s1->ssid);
543                                 else {
544                                         msg = N_("New Access Points");
545                                         t = g_strconcat(txt, "\n",
546                                             s1->ssid, NULL);
547                                         g_free(txt);
548                                         txt = t;
549                                 }
550                         }
551                 }
552                 if (txt) {
553                         notify(msg, txt, "network-wireless");
554                         g_free(txt);
555                 }
556                 dhcpcd_wi_scans_free(w->scans);
557         }
558         w->scans = scans;
559 }
560
561 int
562 main(int argc, char *argv[])
563 {
564         DHCPCD_CONNECTION *con;
565
566         setlocale(LC_ALL, "");
567         bindtextdomain(PACKAGE, NULL);
568         bind_textdomain_codeset(PACKAGE, "UTF-8");
569         textdomain(PACKAGE);
570
571         gtk_init(&argc, &argv);
572         g_set_application_name("Network Configurator");
573         gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(),
574             ICONDIR);
575         status_icon = gtk_status_icon_new_from_icon_name("network-offline");
576
577         gtk_status_icon_set_tooltip_text(status_icon,
578             _("Connecting to dhcpcd ..."));
579         gtk_status_icon_set_visible(status_icon, true);
580         online = false;
581 #ifdef NOTIFY
582         notify_init(PACKAGE);
583 #endif
584
585         g_message(_("Connecting ..."));
586         con = dhcpcd_new();
587         if (con ==  NULL) {
588                 g_critical("libdhcpcd: %s", strerror(errno));
589                 exit(EXIT_FAILURE);
590         }
591         dhcpcd_set_status_callback(con, dhcpcd_status_cb, NULL);
592         dhcpcd_set_if_callback(con, dhcpcd_if_cb, NULL);
593         dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, NULL);
594         //dhcpcd_wpa_set_status_callback(con, dhcpcd_wpa_status_cb, NULL);
595         if (dhcpcd_try_open(con))
596                 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
597
598         menu_init(status_icon, con);
599
600         gtk_main();
601         dhcpcd_close(con);
602         dhcpcd_free(con);
603         return 0;
604 }