-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
1635 lines (1324 loc) · 37.2 KB
/
main.c
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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#include <limits.h>
#include <getopt.h>
#include <syslog.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <math.h>
#include "types.h"
#include "misc.h"
#include "message.h"
#include "platform.h"
#include "keycodes.h"
#include "cfg-parse.tab.h"
/* Default config values are zero for all but a few things. */
static struct config global_cfg = {
.log.level = LL_INFO,
.reconnect = {
.max_tries = 10,
.max_interval = 30 * 1000 * 1000,
},
};
static struct config* config = &global_cfg;
struct node* focused_node;
static struct node* last_focused_node;
opmode_t opmode;
static char* progname;
static int orig_argc;
static char** orig_argv;
static FILE* logfile;
/* iterate over all remotes, regardless of whether or not they're enabled */
#define for_each_defined_remote(r) for (r = config->remotes; r; r = r->next)
/* iterate over only enabled remotes */
#define for_each_remote(r) for_each_defined_remote(r) if (r->enabled)
static void focus_master(void);
static void setup_remote(struct remote* rmt);
static void handle_message(struct remote* rmt, const struct message* msg);
#define SYSLOG_FACILITY LOG_USER
static void init_logfile(void)
{
switch (config->log.file.type) {
case LF_NONE:
break;
case LF_STDERR:
logfile = stderr;
break;
case LF_FILE:
logfile = fopen(config->log.file.path, "a");
if (!logfile) {
fprintf(stderr, "Failed to open log file %s: %s\n",
config->log.file.path, strerror(errno));
exit(1);
}
setlinebuf(logfile);
break;
case LF_SYSLOG:
openlog(progname, LOG_PID, SYSLOG_FACILITY);
break;
default:
fprintf(stderr, "Bad log file type %d\n", config->log.file.type);
abort();
}
}
/* Small hack to let remote.c set the log level... */
void set_loglevel(unsigned int level)
{
static const char* levelnames[] = {
[0] = "[NONE]",
[LL_BUG] = "BUG",
[LL_ERROR] = "ERROR",
[LL_WARN] = "WARN",
[LL_INFO] = "INFO",
[LL_VERBOSE] = "VERBOSE",
[LL_DEBUG] = "DEBUG",
[LL_DEBUG2] = "DEBUG2",
};
if (level >= ARR_LEN(levelnames)) {
bug("Attempted to set loglevel to bogus level %u\n", level);
level = LL_DEBUG2;
}
config->log.level = level;
mlog(0, "Log level set to %s\n", levelnames[level]);
}
__printf(1, 2) void initerr(const char* fmt, ...)
{
va_list va;
va_start(va, fmt);
vfprintf(stderr, fmt, va);
va_end(va);
}
static void vlog(const char* fmt, va_list va)
{
char datestr[128];
time_t now;
struct tm tm;
switch (config->log.file.type) {
case LF_NONE:
break;
case LF_SYSLOG:
/* TODO: maybe carry different log levels through to syslog? */
vsyslog(SYSLOG_FACILITY|LOG_NOTICE, fmt, va);
break;
case LF_FILE:
case LF_STDERR:
now = time(NULL);
if (!localtime_r(&now, &tm)) {
fprintf(stderr, "localtime_r() failed\n");
abort();
}
if (!strftime(datestr, sizeof(datestr), "%F %T", &tm)) {
fprintf(stderr, "strftime() failed\n");
abort();
}
fprintf(logfile, "[%d] %s: ", getpid(), datestr);
vfprintf(logfile, fmt, va);
break;
default:
fprintf(stderr, "bad logfile type %d\n", config->log.file.type);
abort();
}
}
static void log_direct(const char* fmt, ...)
{
va_list va;
va_start(va, fmt);
vlog(fmt, va);
va_end(va);
}
__printf(2, 3) void mlog(unsigned int level, const char* fmt, ...)
{
va_list va;
struct message* msg;
if (config->log.level < level)
return;
va_start(va, fmt);
if (opmode == MASTER) {
vlog(fmt, va);
} else {
msg = new_message(MT_LOGMSG);
MB(msg, logmsg).msg = xvasprintf(fmt, va);
mc_enqueue_message(&stdio_msgchan, msg);
}
va_end(va);
}
static void disconnect_remote(struct remote* rmt)
{
pid_t pid;
int status;
/* Close fds and reset send & receive queues/buffers */
mc_close(&rmt->msgchan);
/*
* A note on signal choice here: initially this used SIGTERM (which
* seemed more appropriate), but it appears ssh has a tendency to
* (under certain connection-failure conditions) block for long
* periods of time with SIGTERM blocked/ignored, meaning we end up
* blocking in wait(). So instead we just skip straight to the big
* gun here. I don't think it's likely to have any terribly important
* cleanup to do anyway (at least in this case).
*/
if (rmt->sshpid > 0) {
if (kill(rmt->sshpid, SIGKILL) && errno != ESRCH)
perror("failed to kill remote shell");
pid = waitpid(rmt->sshpid, &status, 0);
if (pid != rmt->sshpid)
perror("wait() on remote shell");
}
rmt->sshpid = -1;
if (rmt == focused_node->remote)
focus_master();
}
/*
* Reconnection time-interval computations are done scaled by this factor to
* avoid potential overflows
*/
#define RECONNECT_INTERVAL_UNIT (500 * 1000) /* half a second */
static void reconnect_remote_cb(void* arg)
{
struct remote* rmt = arg;
rmt->reconnect_timer = NULL;
setup_remote(rmt);
}
static void fail_remote(struct remote* rmt, const char* reason)
{
uint64_t tmp, lshift, next_reconnect_delay;
errlog("disconnecting remote '%s': %s\n", rmt->node.name, reason);
disconnect_remote(rmt);
rmt->failcount += 1;
if (rmt->failcount > config->reconnect.max_tries) {
errlog("remote '%s' exceeds failure limits, permfailing.\n",
rmt->node.name);
rmt->state = CS_PERMFAILED;
return;
}
rmt->state = CS_FAILED;
/* 0.5s, 1s, 2s, 4s, 8s...capped at config->reconnect.max_interval */
lshift = rmt->failcount - 1;
if (lshift > (CHAR_BIT * sizeof(uint64_t) - 1))
lshift = (CHAR_BIT * sizeof(uint64_t)) - 1;
tmp = (1ULL << lshift);
if (tmp > (config->reconnect.max_interval / RECONNECT_INTERVAL_UNIT))
tmp = config->reconnect.max_interval / RECONNECT_INTERVAL_UNIT;
next_reconnect_delay = tmp * RECONNECT_INTERVAL_UNIT;
rmt->reconnect_timer = schedule_call(reconnect_remote_cb, rmt, NULL,
next_reconnect_delay);
}
static void enqueue_message(struct remote* rmt, struct message* msg)
{
if (mc_enqueue_message(&rmt->msgchan, msg))
fail_remote(rmt, "send backlog exceeded");
}
void send_keyevent(struct remote* rmt, keycode_t kc, pressrel_t pr)
{
struct message* msg;
if (!rmt)
return;
msg = new_message(MT_KEYEVENT);
MB(msg, keyevent).keycode = kc;
MB(msg, keyevent).pressrel = pr;
enqueue_message(rmt, msg);
}
void send_moverel(struct remote* rmt, int32_t dx, int32_t dy)
{
struct message* msg;
if (!rmt)
return;
msg = new_message(MT_MOVEREL);
MB(msg, moverel).dx = dx;
MB(msg, moverel).dy = dy;
enqueue_message(rmt, msg);
}
void send_clickevent(struct remote* rmt, mousebutton_t button, pressrel_t pr)
{
struct message* msg;
unsigned int i, count = 1;
if (!rmt)
return;
if (button == MB_SCROLLUP || button == MB_SCROLLDOWN) {
count = abs(rmt->scrollmult);
if (rmt->scrollmult < 0)
button = (button == MB_SCROLLUP) ? MB_SCROLLDOWN : MB_SCROLLUP;
}
for (i = 0; i < count; i++) {
msg = new_message(MT_CLICKEVENT);
MB(msg, clickevent).button = button;
MB(msg, clickevent).pressrel = pr;
enqueue_message(rmt, msg);
}
}
void send_setbrightness(struct remote* rmt, float f)
{
struct message* msg;
if (!rmt)
return;
msg = new_message(MT_SETBRIGHTNESS);
MB(msg, setbrightness).brightness = f;
enqueue_message(rmt, msg);
}
void send_setclipboard(struct remote* rmt, char* text)
{
struct message* msg;
if (!rmt)
return;
msg = new_message(MT_SETCLIPBOARD);
MB(msg, setclipboard).text = text;
enqueue_message(rmt, msg);
}
void send_setloglevel(struct remote* rmt, unsigned int level)
{
struct message* msg;
if (!rmt)
return;
msg = new_message(MT_SETLOGLEVEL);
MB(msg, setloglevel).loglevel = level;
enqueue_message(rmt, msg);
}
#define SSH_DEFAULT(type, name) \
static inline type get_##name(const struct remote* rmt) \
{ \
return rmt->sshcfg.name ? rmt->sshcfg.name \
: config->ssh_defaults.name; \
}
SSH_DEFAULT(char*, remoteshell)
SSH_DEFAULT(int, port)
SSH_DEFAULT(char*, bindaddr)
SSH_DEFAULT(char*, identityfile)
SSH_DEFAULT(char*, username)
SSH_DEFAULT(char*, remotecmd)
static void exec_remote_shell(const struct remote* rmt)
{
int nargs;
char* remote_shell = get_remoteshell(rmt) ? get_remoteshell(rmt) : "ssh";
char* argv[] = {
remote_shell,
"-oBatchMode=yes",
"-oServerAliveInterval=2",
"-oServerAliveCountMax=3",
"-oConnectTimeout=2",
/* placeholders */
NULL, /* -q */
NULL, /* -E */
NULL, /* logfile */
NULL, /* -b */
NULL, /* bind address */
NULL, /* -oIdentitiesOnly=yes */
NULL, /* -i */
NULL, /* identity file */
NULL, /* -p */
NULL, /* port */
NULL, /* -l */
NULL, /* username */
NULL, /* hostname */
NULL, /* remote command */
NULL, /* argv terminator */
};
for (nargs = 0; argv[nargs]; nargs++) /* just find first NULL entry */;
if (config->log.level < LL_WARN)
argv[nargs++] = "-q";
if (config->log.file.type == LF_FILE) {
argv[nargs++] = "-E";
argv[nargs++] = config->log.file.path;
} else if (config->log.file.type == LF_SYSLOG || config->log.file.type == LF_NONE) {
/*
* TODO: fork a logger(1) and attach its stdin to ssh's stderr
* for the LF_SYSLOG case?
*/
argv[nargs++] = "-E";
argv[nargs++] = "/dev/null";
}
if (get_port(rmt)) {
argv[nargs++] = "-p";
argv[nargs++] = xasprintf("%d", get_port(rmt));
}
if (get_bindaddr(rmt)) {
argv[nargs++] = "-b";
argv[nargs++] = get_bindaddr(rmt);
}
if (get_identityfile(rmt)) {
argv[nargs++] = "-oIdentitiesOnly=yes";
argv[nargs++] = "-i";
argv[nargs++] = get_identityfile(rmt);
}
if (get_username(rmt)) {
argv[nargs++] = "-l";
argv[nargs++] = get_username(rmt);
}
argv[nargs++] = rmt->hostname;
argv[nargs++] = get_remotecmd(rmt) ? get_remotecmd(rmt) : progname;
assert(nargs < ARR_LEN(argv));
execvp(remote_shell, argv);
perror("execvp");
exit(1);
}
static void rmt_mc_read_cb(struct msgchan* mc, struct message* msg, void* arg)
{
struct remote* rmt = arg;
if (msg->body.type != MT_MOUSEPOS && msg->body.type != MT_LOGMSG)
debug2("received %s from %s\n", msgtype_name(msg->body.type),
rmt->node.name);
handle_message(rmt, msg);
}
static void rmt_mc_err_cb(struct msgchan* mc, void* arg, int err)
{
struct remote* rmt = arg;
char* msg = xasprintf("msgchan error: %s", strerror(err));
fail_remote(rmt, msg);
xfree(msg);
}
static void setup_remote(struct remote* rmt)
{
int sockfds[2];
struct message* setupmsg;
int sndbuf_sz;
info("initiating connection attempt to remote %s...\n", rmt->node.name);
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds)) {
perror("socketpair");
exit(1);
}
/*
* If a remote goes offline, we want to detect it sooner rather than
* later (which happens via ssh getting backed up, thus allowing our
* send backlog to reach its limit), so we shrink our send-buffer size
* on the socket we'll be sending messages through. Granted, ssh's
* network-facing socket probably still has a much larger send buffer,
* so the effectiveness of this is likely to be pretty limited, but we
* might as well try.
*/
sndbuf_sz = 1024;
if (setsockopt(sockfds[0], SOL_SOCKET, SO_SNDBUF, &sndbuf_sz,
sizeof(sndbuf_sz)))
warn("setsockopt(SO_SNDBUF) failed: %s\n", strerror(errno));
rmt->sshpid = fork();
if (rmt->sshpid < 0) {
perror("fork");
exit(1);
}
rmt->state = CS_SETTINGUP;
if (!rmt->sshpid) {
/* ssh child */
if (dup2(sockfds[1], STDIN_FILENO) < 0
|| dup2(sockfds[1], STDOUT_FILENO) < 0) {
perror("dup2");
exit(1);
}
if (close(sockfds[0]))
perror("close");
if (close(sockfds[1]))
perror("close");
exec_remote_shell(rmt);
}
set_fd_nonblock(sockfds[0], 1);
set_fd_cloexec(sockfds[0], 1);
mc_init(&rmt->msgchan, sockfds[0], sockfds[0], rmt_mc_read_cb,
rmt_mc_err_cb, rmt);
if (close(sockfds[1]))
perror("close");
setupmsg = new_message(MT_SETUP);
setupmsg->body.type = MT_SETUP;
MB(setupmsg, setup).prot_vers = PROT_VERSION;
MB(setupmsg, setup).loglevel = config->log.level;
MB(setupmsg, setup).params.params_val = flatten_kvmap(rmt->params,
&MB(setupmsg, setup).params.params_len);
enqueue_message(rmt, setupmsg);
}
static struct remote* find_remote(const char* name)
{
struct remote* rmt;
/* First search by alias */
for_each_defined_remote (rmt) {
if (!strcmp(name, rmt->node.name))
return rmt;
}
/* if that fails, try hostnames */
for_each_defined_remote (rmt) {
if (!strcmp(name, rmt->hostname))
return rmt;
}
return NULL;
}
static struct node* find_node(const char* name)
{
struct remote* rmt;
if (!name || !strcmp(name, config->master.name))
return &config->master;
rmt = find_remote(name);
return rmt ? &rmt->node : NULL;
}
static void resolve_noderef(struct noderef* n)
{
char* name;
struct node* node;
if (n->type == NT_TMPNAME) {
name = n->name;
node = find_node(name);
if (!node)
initdie("No such remote: '%s'\n", n->name);
n->type = NT_NODE;
n->node = node;
xfree(name);
}
}
static const char* dirnames[] = {
[LEFT] = "left",
[RIGHT] = "right",
[UP] = "up",
[DOWN] = "down",
};
static void apply_link(struct link* ln)
{
assert(ln->a.dir != NO_DIR);
if (ln->a.nr.node->neighbors[ln->a.dir])
initerr("Warning: %s %s neighbor already specified\n",
ln->a.nr.node->name, dirnames[ln->a.dir]);
ln->a.nr.node->neighbors[ln->a.dir] = ln->b.nr.node;
if (ln->b.dir != NO_DIR) {
if (ln->b.nr.node->neighbors[ln->b.dir])
initerr("Warning: %s %s neighbor already specified\n",
ln->b.nr.node->name, dirnames[ln->b.dir]);
ln->b.nr.node->neighbors[ln->b.dir] = ln->a.nr.node;
}
}
static int node_enabled(struct node* n)
{
struct remote* r = n->remote;
return !r || r->enabled;
}
static void apply_topology(void)
{
struct link* ln;
for (ln = config->topology; ln; ln = ln->next) {
resolve_noderef(&ln->a.nr);
resolve_noderef(&ln->b.nr);
/* ignore links to disabled remotes */
if (node_enabled(ln->a.nr.node) && node_enabled(ln->b.nr.node))
apply_link(ln);
}
}
struct remote_enable {
const char* name;
int enable;
};
static void set_enabled_remotes(const struct remote_enable* settings, unsigned num_settings)
{
struct remote* rmt;
/*
* First apply the default for any remotes that aren't explicitly
* specified (disabled if -e was last, enabled if -d was last or if
* neither was passed)
*/
int dflt = num_settings ? !settings[num_settings - 1].enable : 1;
for_each_defined_remote (rmt) {
rmt->enabled = dflt;
}
for (unsigned i = 0; i < num_settings; i++) {
rmt = find_remote(settings[i].name);
if (rmt)
rmt->enabled = settings[i].enable;
else
initdie("Error: remote '%s' not defined\n", settings[i].name);
}
}
static void mark_reachable(struct node* n)
{
int seen;
direction_t dir;
if (!n || is_master(n))
return;
seen = n->remote->reachable;
n->remote->reachable = 1;
if (!seen) {
for_each_direction (dir)
mark_reachable(n->neighbors[dir]);
}
}
static void check_remotes(void)
{
direction_t dir;
struct remote* rmt;
int num_neighbors;
for_each_direction (dir)
mark_reachable(config->master.neighbors[dir]);
for_each_remote (rmt) {
if (!rmt->reachable)
initerr("Warning: remote '%s' is not reachable\n",
rmt->node.name);
num_neighbors = 0;
for_each_direction (dir) {
if (rmt->node.neighbors[dir])
num_neighbors += 1;
}
if (!num_neighbors)
initerr("Warning: remote '%s' has no neighbors\n",
rmt->node.name);
}
}
static void transfer_clipboard(struct node* from, struct node* to)
{
if (is_master(from) && is_master(to)) {
vinfo("switching from master to master??\n");
return;
}
if (is_remote(from))
enqueue_message(from->remote, new_message(MT_GETCLIPBOARD));
else if (is_remote(to))
send_setclipboard(to->remote, get_clipboard_text());
}
static void transfer_modifiers(struct node* from, struct node* to,
const keycode_t* modkeys)
{
int i;
if (is_remote(from)) {
for (i = 0; modkeys[i] != ET_null; i++)
send_keyevent(from->remote, modkeys[i], PR_RELEASE);
}
if (is_remote(to)) {
for (i = 0; modkeys[i] != ET_null; i++)
send_keyevent(to->remote, modkeys[i], PR_PRESS);
}
}
static void set_node_display_brightness(struct node* node, float f)
{
if (is_master(node))
set_display_brightness(f);
else
send_setbrightness(node->remote, f);
}
struct setbrightness_cb_args {
struct node* node;
float brightness;
};
static void set_brightness_cb(void* arg)
{
struct setbrightness_cb_args* args = arg;
/*
* There's a chance this can be called after a remote has been
* disconnected, in which case we need to not try to send the
* brightness-change message to avoid a use-after-free.
*/
if (!(is_remote(args->node) && args->node->remote->state != CS_CONNECTED))
set_node_display_brightness(args->node, args->brightness);
}
static void schedule_brightness_change(struct node* node, float f, uint64_t delay)
{
struct setbrightness_cb_args* args = xmalloc(sizeof(*args));
args->node = node;
args->brightness = f;
schedule_call(set_brightness_cb, args, xfree, delay);
}
static void transition_brightness(struct node* node, float from, float to,
uint64_t duration, int steps)
{
int i;
float frac, level;
uint64_t delay;
set_node_display_brightness(node, from);
for (i = 1; i < steps; i++) {
frac = (float)i / (float)steps;
delay = (uint64_t)(frac * (float)duration);
level = from + (frac * (to - from));
schedule_brightness_change(node, level, delay);
}
schedule_brightness_change(node, to, duration);
}
static void indicate_switch(struct node* from, struct node* to)
{
struct focus_hint* fh = &config->focus_hint;
switch (fh->type) {
case FH_NONE:
break;
case FH_DIM_INACTIVE:
if (from && from != to)
transition_brightness(from, 1.0, fh->brightness, fh->duration,
fh->fade_steps);
transition_brightness(to, fh->brightness, 1.0, fh->duration,
fh->fade_steps);
break;
case FH_FLASH_ACTIVE:
transition_brightness(to, fh->brightness, 1.0, fh->duration,
fh->fade_steps);
break;
default:
errlog("unknown focus_hint type %d\n", fh->type);
break;
}
}
/*
* If the given remote is connected, do nothing and return 0. Otherwise
* initiate a reconnection attempt and return 1.
*/
static int reconnect_remote(struct remote* rmt)
{
if (rmt->state == CS_CONNECTED)
return 0;
if (rmt->reconnect_timer) {
if (!cancel_call(rmt->reconnect_timer))
warn("Failed to cancel reconnect_timer for remote %s\n",
rmt->node.name);
}
if (rmt->state == CS_SETTINGUP)
disconnect_remote(rmt);
rmt->failcount = 0;
setup_remote(rmt);
return 1;
}
/*
* A special focus-switch for when the focused remote fails; in this case we
* just revert focus directly to the master.
*/
static void focus_master(void)
{
ungrab_inputs(1);
last_focused_node = focused_node;
focused_node = &config->master;
indicate_switch(NULL, &config->master);
}
/*
* Returns non-zero on a successful "real" switch, or zero if no actual switch
* was performed (i.e. the switched-to node is the same as the current node,
* or the remote we tried to switch to is currently disconnected).
*/
static int focus_node(struct node* n, keycode_t* modkeys, int via_hotkey)
{
struct node* to;
struct node* from;
if (!n) {
to = focused_node;
} else if (is_remote(n) && n->remote->state != CS_CONNECTED) {
info("Remote %s not connected, attempting to reconnect...\n", n->name);
reconnect_remote(n->remote);
to = focused_node;
} else {
to = n;
}
from = focused_node;
/*
* If configured to do so, give visual indication even if no actual
* switch is performed.
*/
if (to != from
|| config->show_nullswitch == NS_YES
|| (config->show_nullswitch == NS_HOTKEYONLY && via_hotkey))
indicate_switch(from, to);
if (to == from)
return 0;
debug2("focus switch: %s -> %s\n", from->name, to->name);
if (is_remote(from) && is_master(to))
ungrab_inputs(via_hotkey);
else if (is_master(from) && is_remote(to))
grab_inputs();
transfer_clipboard(from, to);
transfer_modifiers(from, to, modkeys);
last_focused_node = focused_node;
focused_node = to;
return 1;
}
static int focus_neighbor(direction_t dir, keycode_t* modkeys, int via_hotkey)
{
return focus_node(focused_node->neighbors[dir], modkeys, via_hotkey);
}
static void clear_ssh_config(struct ssh_config* c)
{
xfree(c->remoteshell);
xfree(c->bindaddr);
xfree(c->identityfile);
xfree(c->username);
xfree(c->remotecmd);
memset(c, 0, sizeof(*c));
}
static void free_remote(struct remote* rmt)
{
xfree(rmt->node.name);
xfree(rmt->hostname);
destroy_kvmap(rmt->params);
clear_ssh_config(&rmt->sshcfg);
xfree(rmt);
}
static int run_command(const char* cmd, int must_succeed)
{
int status = system(cmd);
if (must_succeed)
return status == -1 || WEXITSTATUS(status);
else
return 0;
}
/*
* The environment variable used to indicate that we've re-execed ourselves
* under a new ssh-agent.
*/
#define ENTHRALL_AGENT_ENV_VAR "__enthrall_private_agent__"
static void shutdown_master(void)
{
struct remote* rmt;
struct hotkey* hk;
struct link* ln;
while (config->remotes) {
rmt = config->remotes;
config->remotes = rmt->next;
if (rmt->state == CS_CONNECTED || rmt->state == CS_SETTINGUP)
disconnect_remote(rmt);
free_remote(rmt);
}
while (config->hotkeys) {
hk = config->hotkeys;
config->hotkeys = hk->next;
xfree(hk->key_string);
xfree(hk);
}
while (config->topology) {
ln = config->topology;
config->topology = ln->next;
xfree(ln);
}
clear_ssh_config(&config->ssh_defaults);
xfree(config->master.name);
platform_exit();
/* If we re-execed under a private agent, unload keys & kill it now. */
if (getenv(ENTHRALL_AGENT_ENV_VAR))
run_command("ssh-add -D 2>/dev/null; ssh-agent -k >/dev/null", 0);
if (config->log.file.type == LF_SYSLOG)
closelog();
}
static int reconnect_remotes(void)
{
struct remote* rmt;
int count = 0;
for_each_remote (rmt)
count += reconnect_remote(rmt);
return count;
}
static int halt_reconnects(void)
{