-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwutui.c
More file actions
1820 lines (1502 loc) · 45.3 KB
/
wutui.c
File metadata and controls
1820 lines (1502 loc) · 45.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2025, Muhammad Saheed <saheed@FreeBSD.org>
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/event.h>
#include <sys/queue.h>
#include <sys/sbuf.h>
#include <sys/socket.h>
#include <netlink/netlink_route.h>
#include <netlink/netlink_snl.h>
#include <netlink/netlink_snl_route.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <locale.h>
#include <math.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <wchar.h>
#include "ctrl_seq.h"
#include "usage.h"
#include "utils.h"
#include "wifi.h"
#include "wpa_ctrl.h"
#define WRAPPED_INCR(var, max) ((var) = ((var) + 1) % (max))
#define WRAPPED_DECR(var, max) ((var) = ((var) - 1 + (max)) % (max))
#define CLAMP(val, minval, maxval) MAX((minval), MIN((val), (maxval)))
#define SUB_CLAMP_ZERO(a, b) ((a) < (b) ? 0 : (a) - (b))
enum handler_return { HANDLER_CONTINUE, HANDLER_BREAK };
typedef enum handler_return (*handler_f)(void *udata);
struct event_handler {
uintptr_t ident;
handler_f handler;
void *udata;
SLIST_ENTRY(event_handler) next;
};
SLIST_HEAD(event_handlers, event_handler);
struct notification {
char *msg;
TAILQ_ENTRY(notification) next;
};
TAILQ_HEAD(notifications, notification);
struct wutui {
int tty, wpa_fd, kq;
bool show_help, is_window_small, show_dialog, hide_dialog_text;
const char *dialog_title, *dialog_text;
bool searching;
char search_scan_results[IEEE80211_NWID_LEN + 1],
search_known_networks[IEEE80211_NWID_LEN + 1];
struct termios cooked;
struct wpa_ctrl *ctrl;
struct winsize winsize;
enum { SECTION_KN = 0, SECTION_NS } section;
size_t selected_kn, selected_sr;
size_t horizontal_pos;
size_t kn_offset, sr_offset;
struct supplicant_status *status;
struct scan_results *srs;
struct known_networks *kns;
struct event_handlers *handlers;
struct notifications *notifications;
size_t max_rows;
size_t kn_entries, sr_entries;
struct snl_state *rtnl;
};
enum wutui_key {
BACKSPACE = 127,
DEL_KEY = 1000,
ARROW_UP,
ARROW_DOWN,
ARROW_RIGHT,
ARROW_LEFT,
HOME_KEY,
END_KEY,
PAGE_UP,
PAGE_DOWN
};
enum kqueue_timer { TIMER_NOTIFICATION_CLEANUP, TIMER_PERIODIC_SCAN };
struct keybinding {
const char *keys;
const char *desc;
};
static const int timers[] = {
[TIMER_NOTIFICATION_CLEANUP] = 5 /* seconds */,
[TIMER_PERIODIC_SCAN] = 30,
};
static struct wutui wutui;
static const int MAX_COLS = 80;
static const int MAX_ROWS = 34;
static const int MAX_ENTRIES = 13;
static const int MIN_COLS = 44;
static const int MIN_ROWS = 22;
#define MARGIN (MAX((wutui.winsize.ws_col - MAX_COLS) / 2, 0))
static void parse_args(int argc, char *argv[], const char **ctrl_path);
static int sbuf_sliding_printf(struct sbuf *sb, size_t start, size_t width,
const wchar_t *fmt, ...);
static void register_handler(struct event_handlers *ehs, int ident,
handler_f handler, void *udata);
static void free_handlers(struct event_handlers *ehs);
static void register_events(void);
static void pop_notification(struct notifications *ns);
static void push_notification(struct notifications *ns, const char *msg);
static void push_notificationf(struct notifications *ns, const char *fmt, ...);
static void clear_notifactions(struct notifications *);
static void free_notifactions(struct notifications *);
static void init_wutui(const char *ctrl_path);
static void deinit_wutui(void);
static void event_loop(void);
static void wait_kq(struct kevent *tevent);
static void render_tui(void);
static void render_wifi_info(struct sbuf *sb);
static void render_known_networks(struct sbuf *sb);
static void render_network_scan(struct sbuf *sb);
static void render_help(struct sbuf *sb);
static void render_dialog(struct sbuf *sb);
static int render_notification(struct sbuf *sb, const char *msg, int pos);
static void render_notifications(struct sbuf *sb);
static char *read_dialog_input(const char *title, int min, int max,
bool hide_text);
static void heading(struct sbuf *sb, const char *text, const char *text_color,
int extra_utf8_width, bool rounded, bool search_icon, int margin,
int max_cols);
static int word_wrap(struct sbuf *sb, const char *text, int width,
int start_col, int pos);
static void divider(struct sbuf *sb, int margin, int max_cols, bool rounded,
bool scrollbar);
static void draw_margin(struct sbuf *sb, int margin);
static const wchar_t *get_signal_bars(int dbm);
static const char *right_corner_block(int pos, int max_entries, int scrollbar);
static const char *divider_block(int pos, int max_entries, int scrollbar);
static int get_scrollbar_pos(int offset, int entries, int max_entries,
int arrows);
static int fetch_cursor_position(unsigned short *row, unsigned short *col);
static int fetch_winsize(void);
static void disable_raw_mode(void);
static void enter_raw_mode(void);
static void enter_alt_buffer(void);
static void leave_alt_buffer(void);
static void quit(void);
static int wutui_configure_network(struct scan_result *selected_sr);
static void connect_scan_result(void);
static void search_by_ssid(void);
static void die(const char *, ...);
static void diex(const char *, ...);
static int read_key(void);
static enum handler_return handle_notification_cleanup(void *);
static enum handler_return handle_periodic_scan(void *);
static enum handler_return handle_input(void *);
static enum handler_return handle_dialog_input(void *);
static enum handler_return handle_wpa_event(void *);
static enum handler_return handle_sigwinch(void *);
static enum handler_return handle_search_input(void *);
static enum handler_return handle_rtnl_ip_change(void *);
static void update_scan_results(void);
static void update_known_networks(void);
static void update_supplicant_status(void);
int
main(int argc, char *argv[])
{
const char *ctrl_path = NULL;
if (setlocale(LC_ALL, "") == NULL)
err(EXIT_FAILURE, "setlocale");
if (!isatty(STDIN_FILENO))
errx(EXIT_FAILURE, "not a TTY");
ctrl_path = wpa_ctrl_default_path();
parse_args(argc, argv, &ctrl_path);
if (ctrl_path == NULL) {
errx(EXIT_FAILURE,
"no wpa ctrl interface on default path, provide --ctrl-interface");
}
init_wutui(ctrl_path);
enter_raw_mode();
enter_alt_buffer();
event_loop();
return (EXIT_SUCCESS);
}
static void
parse_args(int argc, char *argv[], const char **ctrl_path)
{
int opt;
struct option opts[] = {
{ "ctrl-interface", required_argument, NULL, 'c' },
{ "help", no_argument, NULL, 'h' },
{ NULL, 0, NULL, 0 },
};
while ((opt = getopt_long(argc, argv, "+c:h", opts, NULL)) != -1) {
switch (opt) {
case 'c':
*ctrl_path = optarg;
break;
case 'h':
usage_tui(stdout);
exit(EXIT_SUCCESS);
case '?':
default:
usage_tui(stderr);
exit(EXIT_FAILURE);
}
}
if (argc - optind != 0) {
warnx("wrong number of arguments");
usage_tui(stderr);
exit(EXIT_FAILURE);
}
}
static int
sbuf_sliding_printf(struct sbuf *sb, size_t start, size_t width,
const wchar_t *fmt, ...)
{
va_list ap;
wchar_t *wbuf = NULL;
size_t wlen = 0;
const wchar_t *window = L"";
size_t window_len = 0;
char *mbbuf = NULL;
size_t mblen = 0;
FILE *stream = open_wmemstream(&wbuf, &wlen);
if (stream == NULL)
return (1);
va_start(ap, fmt);
vfwprintf(stream, fmt, ap);
va_end(ap);
fflush(stream);
fclose(stream);
if (start < wlen) {
window = &wbuf[start];
window_len = wlen - start;
if (window_len > width) {
wbuf[start + width] = L'\0';
window_len = width;
}
}
mblen = wcstombs(NULL, window, 0);
if (mblen == (size_t)-1) {
free(wbuf);
return (1);
}
mbbuf = malloc(mblen + 1);
if (mbbuf == NULL) {
free(wbuf);
return (1);
}
wcstombs(mbbuf, window, mblen + 1);
free(wbuf);
sbuf_bcat(sb, mbbuf, mblen);
if (window_len < width)
sbuf_printf(sb, "%-*.*s", (int)(window_len - width),
(int)(window_len - width), "");
free(mbbuf);
return (0);
}
static void
register_handler(struct event_handlers *ehs, int ident, handler_f handler,
void *udata)
{
struct event_handler *eh = malloc(sizeof(*eh));
if (eh == NULL)
err(EXIT_FAILURE, "malloc");
eh->ident = ident;
eh->handler = handler;
eh->udata = udata;
SLIST_INSERT_HEAD(ehs, eh, next);
}
static void
free_handlers(struct event_handlers *ehs)
{
struct event_handler *eh, *eh_tmp;
if (ehs == NULL)
return;
SLIST_FOREACH_SAFE(eh, ehs, next, eh_tmp)
free(eh);
free(ehs);
}
static void
register_events(void)
{
struct kevent events[6];
EV_SET(&events[0], wutui.tty, EVFILT_READ, EV_ADD, 0, 0, NULL);
register_handler(wutui.handlers, wutui.tty, handle_input, NULL);
EV_SET(&events[1], wutui.wpa_fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
register_handler(wutui.handlers, wutui.wpa_fd, handle_wpa_event, NULL);
EV_SET(&events[2], SIGWINCH, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
register_handler(wutui.handlers, SIGWINCH, handle_sigwinch, NULL);
EV_SET(&events[3], TIMER_NOTIFICATION_CLEANUP, EVFILT_TIMER, EV_ADD,
NOTE_SECONDS, timers[TIMER_NOTIFICATION_CLEANUP], NULL);
register_handler(wutui.handlers, TIMER_NOTIFICATION_CLEANUP,
handle_notification_cleanup, NULL);
EV_SET(&events[4], TIMER_PERIODIC_SCAN, EVFILT_TIMER, EV_ADD,
NOTE_SECONDS, timers[TIMER_PERIODIC_SCAN], NULL);
register_handler(wutui.handlers, TIMER_PERIODIC_SCAN,
handle_periodic_scan, NULL);
EV_SET(&events[5], wutui.rtnl->fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
register_handler(wutui.handlers, wutui.rtnl->fd, handle_rtnl_ip_change,
NULL);
if (kevent(wutui.kq, events, nitems(events), NULL, 0, NULL) == -1)
err(EXIT_FAILURE, "kevent register");
}
static void
pop_notification(struct notifications *ns)
{
struct notification *first = TAILQ_FIRST(ns);
if (first == NULL)
return;
TAILQ_REMOVE(ns, first, next);
free(first->msg);
free(first);
}
static void
push_notification(struct notifications *ns, const char *msg)
{
struct notification *notification = NULL;
if ((notification = malloc(sizeof(*notification))) == NULL)
die("malloc");
if ((notification->msg = strdup(msg)) == NULL) {
free(notification);
die("strdup");
}
TAILQ_INSERT_TAIL(ns, notification, next);
}
static void
push_notificationf(struct notifications *ns, const char *fmt, ...)
{
struct sbuf *sb = sbuf_new_auto();
if (sb == NULL)
die("sbuf_new_auto()");
va_list ap;
va_start(ap, fmt);
sbuf_vprintf(sb, fmt, ap);
va_end(ap);
if (sbuf_finish(sb) != 0)
die("sbuf failed");
push_notification(ns, sbuf_data(sb));
sbuf_delete(sb);
}
static void
clear_notifactions(struct notifications *ns)
{
struct notification *n, *n_tmp;
TAILQ_FOREACH_SAFE(n, ns, next, n_tmp) {
TAILQ_REMOVE(ns, n, next);
free(n->msg);
free(n);
}
}
static void
free_notifactions(struct notifications *ns)
{
if (ns == NULL)
return;
clear_notifactions(ns);
free(ns);
}
static void
init_wutui(const char *ctrl_path)
{
wutui.wpa_fd = wutui.tty = wutui.kq = -1;
if (atexit(deinit_wutui) != 0)
err(EXIT_FAILURE, "atexit");
wutui.notifications = malloc(sizeof(struct notifications));
if (wutui.notifications == NULL)
err(EXIT_FAILURE, "malloc");
TAILQ_INIT(wutui.notifications);
if ((wutui.ctrl = wpa_ctrl_open(ctrl_path)) == NULL) {
err(EXIT_FAILURE,
"failed to open wpa_supplicant ctrl_interface, %s",
ctrl_path);
}
if ((wutui.status = get_supplicant_status(wutui.ctrl)) == NULL)
errx(EXIT_FAILURE, "failed to retrieve wpa_supplicant status");
if ((wutui.kns = get_known_networks(wutui.ctrl)) == NULL)
errx(EXIT_FAILURE, "failed to retrieve known networks");
sort_known_networks(wutui.kns);
if ((wutui.srs = get_scan_results(wutui.ctrl)) == NULL)
errx(EXIT_FAILURE, "failed to retrieve scan results");
sort_scan_results(wutui.srs);
if (wpa_ctrl_attach(wutui.ctrl) != 0) {
err(EXIT_FAILURE,
"failed to register to wpa_ctrl event monitor");
}
if ((wutui.wpa_fd = wpa_ctrl_get_fd(wutui.ctrl)) == -1)
err(EXIT_FAILURE, "invalid wpa_ctrl socket");
if ((wutui.tty = open("/dev/tty", O_RDWR)) == -1)
err(EXIT_FAILURE, "open(/dev/tty)");
if (tcgetattr(wutui.tty, &wutui.cooked) == -1)
err(EXIT_FAILURE, "tcgetattr()");
if (fetch_winsize() == -1)
err(EXIT_FAILURE, "failed to fetch terminal winsize");
wutui.rtnl = malloc(sizeof(*wutui.rtnl));
if (wutui.rtnl == NULL)
err(EXIT_FAILURE, "malloc");
if (!snl_init(wutui.rtnl, NETLINK_ROUTE))
err(EXIT_FAILURE, "snl_init(NETLINK_ROUTE)");
if (bind(wutui.rtnl->fd,
(struct sockaddr *)&((struct sockaddr_nl) {
.nl_family = NETLINK_ROUTE,
.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR,
}),
sizeof(struct sockaddr_nl)) == -1) {
err(EXIT_FAILURE, "bind(wutui.rtnl)");
}
if ((wutui.kq = kqueue()) == -1)
err(EXIT_FAILURE, "kqueue()");
wutui.handlers = malloc(sizeof(struct event_handlers));
if (wutui.handlers == NULL)
err(EXIT_FAILURE, "malloc");
SLIST_INIT(wutui.handlers);
register_events();
}
static void
deinit_wutui(void)
{
if (wutui.tty != -1)
disable_raw_mode();
free_handlers(wutui.handlers);
free_notifactions(wutui.notifications);
close(wutui.tty);
close(wutui.kq);
snl_free(wutui.rtnl);
free_supplicant_status(wutui.status);
free_known_networks(wutui.kns);
free_scan_results(wutui.srs);
if (wutui.ctrl != NULL) {
wpa_ctrl_detach(wutui.ctrl);
wpa_ctrl_close(wutui.ctrl);
}
}
static void
event_loop(void)
{
struct kevent tevent;
for (;;) {
struct event_handler *eh;
render_tui();
wait_kq(&tevent);
SLIST_FOREACH(eh, wutui.handlers, next) {
if (tevent.ident == eh->ident) {
if (eh->handler(eh->udata) == HANDLER_BREAK)
return;
break;
}
}
}
}
static void
wait_kq(struct kevent *tevent)
{
int nev = kevent(wutui.kq, NULL, 0, tevent, 1, NULL);
if (nev == -1)
die("kevent wait");
if (nev > 0 && tevent->flags & EV_ERROR)
diex("event error: %s", strerror(tevent->data));
}
static void
render_tui(void)
{
struct sbuf *sb = sbuf_new_auto();
if (sb == NULL)
die("sbuf_new_auto()");
sbuf_cat(sb, ERASE_IN_DISPLAY(ERASE_ENTIRE) CURSOR_MOVE(1, 1));
if (wutui.is_window_small) {
const char msg[] = "Terminal size too small";
int msg_len = sizeof(msg) - 1;
int vertical_offset = MAX((wutui.winsize.ws_row - 1) / 2, 0);
int msg_offset = MAX((wutui.winsize.ws_col - msg_len) / 2, 0);
sbuf_printf(sb, CURSOR_DOWN_FMT, vertical_offset);
sbuf_printf(sb, "%*s" BOLD "%s" NORMAL_INTENSITY, msg_offset,
"", msg);
} else {
int vertical_offset = (wutui.winsize.ws_row - wutui.max_rows) /
2;
vertical_offset = MAX(vertical_offset, 0);
sbuf_printf(sb, CURSOR_DOWN_FMT, vertical_offset);
render_wifi_info(sb);
render_known_networks(sb);
render_network_scan(sb);
divider(sb, MARGIN, MIN(MAX_COLS, wutui.winsize.ws_col), true,
wutui.winsize.ws_col < MAX_COLS);
render_notifications(sb);
if (wutui.show_help)
render_help(sb);
if (wutui.dialog_title != NULL)
render_dialog(sb);
}
if (sbuf_finish(sb) != 0)
die("sbuf failed");
if (write(wutui.tty, sbuf_data(sb), sbuf_len(sb)) != sbuf_len(sb))
die("write");
sbuf_delete(sb);
}
static void
render_wifi_info(struct sbuf *sb)
{
const size_t ssid_extra = wutui.status->ssid != NULL ?
ssid_extra_width(wutui.status->ssid) :
0;
const int WPA_STATE_LEN = sizeof("INTERFACE_DISABLED") - 1;
const int IP_LEN = sizeof("255.255.255.255") - 1;
const int left_key_width = MAX(sizeof("SSID"), sizeof("WPA State")) - 1;
const int left_val_width = MAX(IEEE80211_NWID_LEN, WPA_STATE_LEN);
const int right_key_width = MAX(sizeof("Frequency"),
sizeof("IP Address")) -
1;
char freq_buf[64];
int freq_str_len = snprintf(freq_buf, sizeof(freq_buf), "%d MHz",
wutui.status->freq);
const int right_val_width = MAX(IP_LEN, freq_str_len);
const int cols = MIN(wutui.winsize.ws_col, MAX_COLS);
const char *ip_address = wutui.status->ssid == NULL ||
wutui.status->ip_address == NULL ?
"N/A" :
wutui.status->ip_address;
if (wutui.status->ssid != NULL && wutui.status->ip_address == NULL)
ip_address = "Acquiring...";
heading(sb, "WiFi Info", NULL, 0, true, false, MARGIN, cols);
sbuf_printf(sb, "%*s│", MARGIN, "");
sbuf_sliding_printf(sb, wutui.horizontal_pos,
MAX(cols - 2 - ssid_extra, 0), L" %-*.*s: %-*.*s %-*.*s: %-*.*s",
left_key_width, left_key_width, "SSID", left_val_width - ssid_extra,
left_val_width - ssid_extra,
wutui.status->ssid == NULL ? "N/A" : wutui.status->ssid,
right_key_width, right_key_width, "Frequency", right_val_width,
right_val_width, freq_buf);
sbuf_printf(sb, "│\r\n");
sbuf_printf(sb, "%*s│", MARGIN, "");
sbuf_sliding_printf(sb, wutui.horizontal_pos, MAX(cols - 2, 0),
L" %-*.*s: %-*.*s %-*.*s: %-*.*s", left_key_width, left_key_width,
"WPA State", left_val_width, left_val_width,
wutui.status->state == NULL ? "N/A" : wutui.status->state,
right_key_width, right_key_width, "IP Address", right_val_width,
right_val_width, ip_address);
sbuf_printf(sb, "│\r\n");
}
static void
render_known_networks(struct sbuf *sb)
{
size_t i = 0;
int scrollbar = -1;
const int SECURITY_LEN = sizeof("Security") - 1;
const int HIDDEN_LEN = sizeof("Hidden") - 1;
const int PRIORITY_LEN = sizeof("Priority") - 1;
const int AUTO_CONNECT_LEN = sizeof("Auto Connect") - 1;
const int COLS = MIN(wutui.winsize.ws_col, MAX_COLS);
int search_len = strlen(wutui.search_known_networks);
bool is_filtered = search_len != 0;
bool is_searching = wutui.section == SECTION_KN && wutui.searching;
if (wutui.selected_kn < wutui.kn_offset)
wutui.kn_offset = wutui.selected_kn;
if (wutui.selected_kn >= wutui.kn_offset + wutui.kn_entries)
wutui.kn_offset = wutui.selected_kn - wutui.kn_entries + 1;
scrollbar = get_scrollbar_pos(wutui.kn_offset, wutui.kns->len,
wutui.kn_entries, 1);
heading(sb,
is_filtered || is_searching ? wutui.search_known_networks :
"Known Networks",
is_searching ? COLOR(FG, CYAN) : NULL,
search_len - display_width(wutui.search_known_networks), false,
is_filtered || is_searching, MARGIN, COLS);
sbuf_printf(sb, "%*s│" BOLD COLOR(FG, BLUE), MARGIN, "");
sbuf_sliding_printf(sb, wutui.horizontal_pos, MAX(COLS - 2, 0),
L" %-*s Security Hidden Priority Auto Connect ",
IEEE80211_NWID_LEN, "SSID");
sbuf_printf(sb, NORMAL_INTENSITY COLOR(FG, DEFAULT_COLOR) "%s\r\n",
right_corner_block(-1, wutui.kn_entries, scrollbar));
for (i = wutui.kn_offset;
i < wutui.kns->len && i < wutui.kn_entries + wutui.kn_offset; i++) {
struct known_network *kn = &wutui.kns->items[i];
const size_t ssid_extra = ssid_extra_width(kn->ssid);
sbuf_printf(sb, "%*s│ %s", MARGIN, "",
wutui.section == SECTION_KN && i == wutui.selected_kn &&
!wutui.searching ?
INVERT :
"");
sbuf_sliding_printf(sb, wutui.horizontal_pos,
MAX(COLS - 4 - ssid_extra, 0),
L"%s%-*s %-*s %-*s %*d %-*s",
kn->state == KN_CURRENT ? ">" : " ",
IEEE80211_NWID_LEN - ssid_extra, kn->ssid, SECURITY_LEN,
security_to_string[kn->security], HIDDEN_LEN,
kn->hidden ? "Yes" : "No", PRIORITY_LEN, kn->priority,
AUTO_CONNECT_LEN,
kn->state == KN_ENABLED ? "Yes" :
kn->state == KN_CURRENT ? "Current" :
"No");
sbuf_printf(sb, REMOVE_INVERT " %s\r\n",
right_corner_block(i - wutui.kn_offset, wutui.kn_entries,
scrollbar));
}
for (; i < wutui.kn_entries + wutui.kn_offset; i++)
sbuf_printf(sb, "%*s│%*s%s\r\n", MARGIN, "", COLS - 2, "",
right_corner_block(i - wutui.kn_offset, wutui.kn_entries,
scrollbar));
}
/* TODO: add a way to connect to hidden networks */
static void
render_network_scan(struct sbuf *sb)
{
size_t i = 0;
int scrollbar = -1;
const int SECURITY_LEN = sizeof("Security") - 1;
const int SIGNAL_LEN = sizeof("Signal") - 1;
const int FREQ_LEN = sizeof("5180") - 1;
const int COLS = MIN(wutui.winsize.ws_col, MAX_COLS);
int search_len = strlen(wutui.search_scan_results);
bool is_filtered = strlen(wutui.search_scan_results) != 0;
bool is_searching = wutui.section == SECTION_NS && wutui.searching;
if (wutui.selected_sr < wutui.sr_offset)
wutui.sr_offset = wutui.selected_sr;
if (wutui.selected_sr >= wutui.sr_offset + wutui.sr_entries)
wutui.sr_offset = wutui.selected_sr - wutui.sr_entries + 1;
scrollbar = get_scrollbar_pos(wutui.sr_offset, wutui.srs->len,
wutui.sr_entries, 1);
heading(sb,
is_filtered || is_searching ? wutui.search_scan_results :
"Network Scan",
is_searching ? COLOR(FG, CYAN) : NULL,
search_len - display_width(wutui.search_scan_results), false,
is_filtered || is_searching, MARGIN, COLS);
sbuf_printf(sb, "%*s│" BOLD COLOR(FG, BLUE), MARGIN, "");
sbuf_sliding_printf(sb, wutui.horizontal_pos, MAX(COLS - 2, 0),
L" %-*s Security Signal Frequency ",
IEEE80211_NWID_LEN, "SSID");
sbuf_printf(sb, NORMAL_INTENSITY COLOR(FG, DEFAULT_COLOR) "%s\r\n",
right_corner_block(-1, wutui.sr_entries, scrollbar));
for (i = wutui.sr_offset;
i < wutui.srs->len && i < wutui.sr_entries + wutui.sr_offset; i++) {
struct scan_result *sr = &wutui.srs->items[i];
const wchar_t *signal_bars = get_signal_bars(sr->signal);
const size_t ssid_extra = ssid_extra_width(sr->ssid);
sbuf_printf(sb, "%*s│ %s", MARGIN, "",
wutui.section == SECTION_NS && i == wutui.selected_sr &&
!wutui.searching ?
INVERT :
"");
sbuf_sliding_printf(sb, wutui.horizontal_pos,
MAX(COLS - 4 - ssid_extra, 0),
L" %-*s %-*s %-*ls %-*d MHz ",
IEEE80211_NWID_LEN - ssid_extra, sr->ssid, SECURITY_LEN,
security_to_string[sr->security], SIGNAL_LEN, signal_bars,
FREQ_LEN, sr->freq);
sbuf_printf(sb, REMOVE_INVERT " %s\r\n",
right_corner_block(i - wutui.sr_offset, wutui.sr_entries,
scrollbar));
}
for (; i < wutui.sr_entries + wutui.sr_offset; i++)
sbuf_printf(sb, "%*s│%*s%s\r\n", MARGIN, "", COLS - 2, "",
right_corner_block(i - wutui.sr_offset, wutui.sr_entries,
scrollbar));
}
static void
render_help(struct sbuf *sb)
{
struct keybinding general_keys[] = {
{ "d", "Disconnect current AP" },
{ "h", "Toggle help" },
{ "[/<Left>", "Scroll left" },
{ "]/<Right>", "Scroll right" },
{ "j/<Down>", "Move down" },
{ "k/<Up>", "Move up" },
{ "q", "Quit" },
{ "r", "Reconnect to known AP" },
{ "s", "Trigger Scan" },
{ "Tab", "Switch between sections" },
{ "/", "Search by SSID" },
{ "<C-l>", "Clear notifications" },
};
struct keybinding kn_keys[] = {
{ "a", "Toggle auto connect" },
{ "f", "Forget network" },
{ "<C-a>", "Increase priority" },
{ "<C-x>", "Decrease priority" },
};
struct keybinding ns_keys[] = {
{ "c", "Connect to network" },
};
int max_key_len = sizeof("]/<Right>") - 1;
int max_desc_len = sizeof("Switch between sections") - 1;
int max_help_rows = nitems(general_keys) + nitems(kn_keys) +
nitems(ns_keys) + 2 /* top and bottom */ + 2 /* section headers*/;
int max_help_cols = max_key_len + 1 /* space */ + max_desc_len +
6 /* 2 * strlen("│ ") */;
int vertical_offset = MAX((wutui.winsize.ws_row - max_help_rows) / 2,
0);
int help_margin = MAX((wutui.winsize.ws_col - max_help_cols) / 2, 0);
sbuf_cat(sb, CURSOR_MOVE(1, 1));
sbuf_printf(sb, CURSOR_DOWN_FMT, vertical_offset);
sbuf_cat(sb, COLOR(FG, MAGENTA));
heading(sb, "Help", NULL, 0, true, false, help_margin, max_help_cols);
for (size_t i = 0; i < nitems(general_keys); i++) {
draw_margin(sb, help_margin);
sbuf_printf(sb, "│ %-*s %-*s │\r\n", max_key_len,
general_keys[i].keys, max_desc_len, general_keys[i].desc);
}
heading(sb, "Known Networks", NULL, 0, false, false, help_margin,
max_help_cols);
for (size_t i = 0; i < nitems(kn_keys); i++) {
draw_margin(sb, help_margin);
sbuf_printf(sb, "│ %-*s %-*s │\r\n", max_key_len,
kn_keys[i].keys, max_desc_len, kn_keys[i].desc);
}
heading(sb, "Network Scan", NULL, 0, false, false, help_margin,
max_help_cols);
for (size_t i = 0; i < nitems(ns_keys); i++) {
draw_margin(sb, help_margin);
sbuf_printf(sb, "│ %-*s %-*s │\r\n", max_key_len,
ns_keys[i].keys, max_desc_len, ns_keys[i].desc);
}
divider(sb, help_margin, max_help_cols, true, false);
sbuf_cat(sb, COLOR(FG, DEFAULT_COLOR));
}
static void
render_dialog(struct sbuf *sb)
{
int dialog_rows = 3 + 2;
int dialog_cols = MAX_COLS / 2;
int vertical_offset = MAX((wutui.winsize.ws_row - dialog_rows) / 2, 0);
int dialog_margin = MAX((wutui.winsize.ws_col - dialog_cols) / 2, 0);
int text_width = MAX(dialog_cols - 4, 1);
int text_len = wutui.dialog_text != NULL ? strlen(wutui.dialog_text) :
0;
char stars[text_width];
const char *text = wutui.dialog_text == NULL || wutui.hide_dialog_text ?
stars :
(wutui.dialog_text + MAX(0, text_len - text_width));
memset(stars, 0, text_width);
if (wutui.dialog_text != NULL && wutui.hide_dialog_text)
memset(stars, '*', MIN(text_width, text_len));
sbuf_cat(sb, CURSOR_MOVE(1, 1));
sbuf_printf(sb, CURSOR_DOWN_FMT, vertical_offset);
sbuf_cat(sb, COLOR(FG, GREEN));
heading(sb, wutui.dialog_title, NULL, 0, true, false, dialog_margin,
dialog_cols);
draw_margin(sb, dialog_margin);
sbuf_printf(sb, "│ %*s │\r\n", text_width, "");
draw_margin(sb, dialog_margin);
sbuf_printf(sb,
"│ " COLOR(BG_BRIGHT, BLACK) "%-*.*s" COLOR(BG,
DEFAULT_COLOR) " │\r\n",
text_width, text_width, text);
draw_margin(sb, dialog_margin);
sbuf_printf(sb, "│ %*s │\r\n", text_width, "");
divider(sb, dialog_margin, dialog_cols, true, false);
sbuf_cat(sb, COLOR(FG, DEFAULT_COLOR));
}
static int
render_notification(struct sbuf *sb, const char *msg, int pos)
{
int len = strlen(msg);
int box_width = MIN(len + 4, MAX_COLS / 2);
int start_col = wutui.winsize.ws_col - box_width + 1;
sbuf_cat(sb, COLOR(FG, YELLOW));
sbuf_cat(sb, CURSOR_MOVE(2, 1));
sbuf_printf(sb, CURSOR_MOVE_FMT "╭", pos, start_col);
for (int i = 0; i < box_width - 2; i++)
sbuf_cat(sb, "─");
sbuf_cat(sb, "╮\r\n");
pos = word_wrap(sb, msg, box_width - 4, start_col, pos);
sbuf_printf(sb, CURSOR_MOVE_FMT "╰", ++pos, start_col);
for (int i = 0; i < box_width - 2; i++)
sbuf_cat(sb, "─");
sbuf_cat(sb, "╯\r\n");
sbuf_cat(sb, COLOR(FG, DEFAULT_COLOR));
return (pos + 1);
}
static void
render_notifications(struct sbuf *sb)
{
struct notification *notif, *notif_tmp;
size_t pos = 1;
TAILQ_FOREACH_REVERSE_SAFE(notif, wutui.notifications, notifications,
next, notif_tmp) {
pos = render_notification(sb, notif->msg, pos);
if (pos * 3 >= wutui.max_rows * 2)
break;
}
}
static char *
read_dialog_input(const char *title, int min, int max, bool hide_text)
{
char *input = NULL;
struct sbuf *input_sb = sbuf_new_auto();
struct event_handler *eh = NULL;
handler_f old_handler = NULL;
void *old_udata = NULL;
struct {
struct sbuf *input_sb;
int min, max;
} udata = { input_sb, min, max };
if (input_sb == NULL)
die("sbuf_new_auto()");
wutui.dialog_title = title;
wutui.hide_dialog_text = hide_text;
SLIST_FOREACH(eh, wutui.handlers, next) {
if (eh->ident == (uintptr_t)wutui.tty)
break;
}
assert(eh != NULL);
old_handler = eh->handler;
old_udata = eh->udata;
eh->handler = handle_dialog_input;
eh->udata = &udata;
event_loop();
eh->handler = old_handler;
eh->udata = old_udata;
if (sbuf_len(input_sb) != 0) {
if ((input = strdup(sbuf_data(input_sb))) == NULL)
die("strdup");