-
Notifications
You must be signed in to change notification settings - Fork 45
/
monitor-display.c
909 lines (830 loc) · 23.7 KB
/
monitor-display.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
// Control plane section of the multicast monitor program
// Moved out of monitor.c when it was getting way too big
// Copyright Aug 2024 Phil Karn, KA9Q
#define _GNU_SOURCE 1
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <sys/stat.h>
#include <opus/opus.h>
#include <portaudio.h>
#include <ncurses.h>
#include <locale.h>
#include <signal.h>
#include <getopt.h>
#include <iniparser/iniparser.h>
#if __linux__
#include <bsd/string.h>
#include <alsa/asoundlib.h>
#else
#include <string.h>
#endif
#include <sysexits.h>
#include <poll.h>
#include "conf.h"
#include "config.h"
#include "misc.h"
#include "multicast.h"
#include "radio.h"
#include "iir.h"
#include "morse.h"
#include "status.h"
#include "monitor.h"
bool Auto_sort = false;
int Update_interval = 100; // Default time in ms between display updates
// Simple hack database of identifiers for specific frequencies
// Return an ascii string identifier indexed by ssrc
// Database in /usr/share/ka9q-radio/id.txt
// Really should be rewritten with something much better
struct idtable {
double freq;
char id[128];
};
#define IDSIZE 1024
static int Nid;
static struct idtable Idtable[IDSIZE];
static struct stat Last_stat;
static void update_monitor_display(void);
static void process_keyboard(void);
static int First_session = 0;
static int Sessions_per_screen = 0;
static int Current = -1; // No current session
static bool help = false;
// Versions of ncurses routines that truncate at EOL
// Remaining problem: if I use the last column, the position will still
// wrap to the first column of the next row, and I can't stop that except by not using the last column.
int mvprintwt(int y,int x,char const *fmt,...){
if(x < 0)
return ERR;
int space = COLS - x - 1; // leave last column open
if(space <= 0)
return ERR;
va_list ap;
va_start(ap,fmt);
char string[COLS+1]; // Maximum line length plus null
int r = vsnprintf(string,space,fmt,ap); // write only 'space' characters, space <= COLS
va_end(ap);
if(r < 0)
return ERR;
mvaddstr(y,x,string);
return OK;
}
// Same for printw() - truncate at EOL
int printwt(char const *fmt,...){
int y,x;
getyx(stdscr,y,x);
if(x < 0)
return ERR;
int space = COLS - x - 1; // leave last column open
if(space <= 0)
return ERR;
va_list ap;
va_start(ap,fmt);
char string[COLS+1]; // Maximum line length plus null
int r = vsnprintf(string,space,fmt,ap); // write only 'space' characters, space <= COLS
va_end(ap);
if(r < 0)
return ERR;
mvaddstr(y,x,string);
return OK;
}
// Same for mvaddstr() and addstr()
int mvaddstrt(int y,int x,char const *str){
int space = COLS - x - 1; // Leave last column open
if(strlen(str) <= space)
return mvaddstr(y,x,str); // fits
char temp[space+1];
strlcpy(temp,str,sizeof(temp)); // truncate
return mvaddstr(y,x,temp);
}
int addstrt(char const *str){
int y,x;
getyx(stdscr,y,x);
int space = COLS - x - 1; // Leave last column open
if(strlen(str) <= space)
return mvaddstr(y,x,str); // fits
char temp[space+1];
strlcpy(temp,str,sizeof(temp)); // truncate
return mvaddstr(y,x,temp);
}
// Use ncurses to display streams
void *display(void *arg){
pthread_setname("display");
if(initscr() == NULL){
fprintf(stderr,"initscr() failed, disabling control/display thread\n");
pthread_exit(NULL);
}
keypad(stdscr,TRUE);
timeout(Update_interval);
cbreak();
noecho();
while(!Terminate){
assert(First_session >= 0);
assert(First_session == 0 || First_session < Nsessions);
assert(Current >= -1);
assert(Current == -1 || Current < Nsessions); // in case Nsessions is 0
// Start screen update
move(0,0);
clrtobot();
addstrt("KA9Q Multicast Audio Monitor:");
for(int i=0;i<Nfds;i++)
printwt(" %s",Mcast_address_text[i]);
addstrt("\n");
if(help){
char path [PATH_MAX];
dist_path(path,sizeof(path),"monitor-help.txt");
FILE *fp = fopen(path,"r");
if(fp != NULL){
size_t size = 0;
char *line = NULL;
while(getline(&line,&size,fp) > 0)
addstrt(line);
FREE(line);
fclose(fp);
fp = NULL;
}
}
if(Nsessions == 0)
Current = -1;
if(Nsessions > 0 && Current == -1)
Current = 0;
if(Quiet_mode){
addstrt("Hit 'q' to resume screen updates\n");
} else
update_monitor_display();
process_keyboard();
vote(); // mainly in case a session was muted or unmuted
}
return NULL;
}
// sort callback for sort_session_active() for comparing sessions by most recently active (or currently longest active)
static int scompare(void const *a, void const *b){
struct session const * const s1 = *(struct session **)a;
struct session const * const s2 = *(struct session **)b;
if(s1->now_active){
if(s2->now_active){
// Both active. Fuzz needed because active sessions are updated when packets arrive
if(fabsf(s1->active - s2->active) < 0.5) {
return 0; // Equal within 1/2 sec
} else if(s1->active > s2->active){
return -1; // s1 Longer active
} else {
return +1; // s2 longer
}
} else
return -1; // s1 active, s2 inactive. Active always lower than inactive
} else { // s1 inctive
if(s2->now_active){
return +1;
} else { // Both inactive, sort by last active times
if(s1->last_active > s2->last_active){
return -1;
} else {
return +1;
}
// last_active is in nanoseconds so chances of equality are nil
}
}
}
// sort callback for sort_session() for comparing sessions by total time
static int tcompare(void const *a, void const *b){
struct session const * const s1 = *(struct session **)a;
struct session const * const s2 = *(struct session **)b;
#define FUZZ 1
#ifdef FUZZ
if(fabsf(s1->tot_active - s2->tot_active) < 0.1) // equal within margin
return 0;
#endif
if(s1->tot_active > s2->tot_active)
return -1;
return +1;
}
// Sort session list in increasing order of age
static int sort_session_active(void){
pthread_mutex_lock(&Sess_mutex);
qsort(Sessions,Nsessions,sizeof(Sessions[0]),scompare);
pthread_mutex_unlock(&Sess_mutex);
return 0;
}
static int sort_session_total(void){
pthread_mutex_lock(&Sess_mutex);
qsort(Sessions,Nsessions,sizeof(Sessions[0]),tcompare);
pthread_mutex_unlock(&Sess_mutex);
return 0;
}
void load_id(void){
char filename[PATH_MAX];
dist_path(filename,sizeof(filename),ID);
struct stat statbuf;
stat(filename,&statbuf);
if(statbuf.st_mtime != Last_stat.st_mtime)
Nid = 0; // Force reload
if(Nid != 0)
return;
// Load table
FILE * const fp = fopen(filename,"r");
if(fp == NULL)
return;
char *line = NULL;
size_t linesize = 0;
while(getline(&line,&linesize,fp) > 0){
chomp(line);
if(line[0] == '#' || strlen(line) == 0)
continue; // Comment
assert(Nid < IDSIZE);
char *ptr = NULL;
Idtable[Nid].freq = strtod(line,&ptr);
if(ptr == line)
continue; // no parseable number
while(*ptr == ' ' || *ptr == '\t')
ptr++;
int const len = strlen(ptr); // Length of ID field
if(len > 0){ // Not null
strlcpy(Idtable[Nid].id,ptr,sizeof(Idtable[Nid].id));
}
Nid++;
if(Nid == IDSIZE){
fprintf(stderr,"ID table overlow, size %d\n",Nid);
break;
}
}
FREE(line);
fclose(fp);
}
char const *lookupid(double freq){
for(int i=0; i < Nid; i++){
if(Idtable[i].freq == freq)
return Idtable[i].id;
}
return NULL;
}
static void update_monitor_display(void){
// First header line
if(Repeater_tail != 0){
if(Last_id_time != 0)
printwt("Last ID: %lld sec",(long long)((gps_time_ns() - Last_id_time) / BILLION));
if(PTT_state)
addstrt(" PTT On");
else if(Last_xmit_time != 0)
printwt(" PTT Off; Last xmit: %lld sec",(long long)((gps_time_ns() - Last_xmit_time) / BILLION));
printwt("\n");
}
if(Constant_delay)
printwt("Constant delay ");
if(Start_muted)
printwt("**Starting new sessions muted** ");
if(Voting)
printwt("SNR Voting enabled\n");
int y,x;
getyx(stdscr,y,x);
if(x != 0)
printwt("\n");
if(Auto_sort)
sort_session_active();
Sessions_per_screen = LINES - getcury(stdscr) - 1;
// This mutex protects Sessions[] and Nsessions. Instead of holding the
// lock for the entire display loop, we make a copy.
pthread_mutex_lock(&Sess_mutex);
int const Nsessions_copy = Nsessions;
struct session *Sessions_copy[Nsessions_copy];
memcpy(Sessions_copy,Sessions,Nsessions_copy * sizeof(Sessions_copy[0]));
pthread_mutex_unlock(&Sess_mutex);
assert(Nsessions_copy <= NSESSIONS);
if(Verbose){
// Measure skew between sampling clock and UNIX real time (hopefully NTP synched)
double const pa_seconds = Pa_GetStreamTime(Pa_Stream) - Start_pa_time;
int const q = modsub(Wptr,Rptr,BUFFERSIZE);
double const qd = (double) q / DAC_samprate;
double const rate = Audio_frames / pa_seconds;
printwt("Playout %.0f ms, latency %d ms, queue %.3lf sec, D/A rate %'.3lf Hz,",Playout,Portaudio_delay,qd,rate);
printwt(" (%+.3lf ppm),",1e6 * (rate / DAC_samprate - 1));
// Time since last packet drop on any channel
printwt(" Error-free sec %'.1lf\n",(1e-9*(gps_time_ns() - Last_error_time)));
}
// Show channel statuses
getyx(stdscr,y,x);
int row_save = y;
int col_save = x;
// dB column
int width = 4;
mvprintwt(y++,x,"%*s",width,"dB");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp != NULL)
mvprintwt(y,x,"%+*.0lf",width,sp->muted ? -INFINITY : voltage2dB(sp->gain));
}
x += width;
y = row_save;
if(Auto_position){
if(x >= COLS)
goto done;
// Pan column
width = 4;
mvprintwt(y++,x," pan");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp != NULL)
mvprintwt(y,x,"%*d",width,(int)roundf(100*sp->pan));
}
x += width;
y = row_save;
}
// SSRC
if(x >= COLS)
goto done;
width = 9;
mvprintwt(y++,x,"%*s",width,"ssrc");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp != NULL)
mvprintwt(y,x,"%*d",width,sp->ssrc);
}
x += width;
y = row_save;
if(Notch){
if(x >= COLS)
goto done;
width = 7;
mvprintwt(y++,x,"%*s",width,"tone");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp != NULL && (!sp->notch_enable || sp->notch_tone == 0))
continue;
mvprintwt(y,x,"%*.1f%c",width-1,sp->notch_tone,sp->current_tone == sp->notch_tone ? '*' : ' ');
}
x += width;
y = row_save;
}
if(x >= COLS)
goto done;
width = 12;
mvprintwt(y++,x,"%*s",width,"freq");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp != NULL)
mvprintwt(y,x,"%'*.0lf",width,sp->chan.tune.freq);
}
x += width;
y = row_save;
if(x >= COLS)
goto done;
width = 5;
mvprintwt(y++,x,"%*s",width,"mode");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp != NULL)
mvprintwt(y,x,"%*s",width,sp->chan.preset);
}
x += width;
y = row_save;
if(x >= COLS)
goto done;
width = 6;
mvprintwt(y++,x,"%*s",width-1,"s/n");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp != NULL && !isnan(sp->snr))
mvprintwt(y,x,"%*.1f%c",width-1,sp->snr,(Voting && sp == Best_session) ? '*' : ' ');
}
x += width;
y = row_save;
x++; // ID is left justified, add a leading space
if(x >= COLS)
goto done;
width = 0;
mvprintwt(y++,x,"%s","id");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp == NULL)
continue;
int len = strlen(sp->id);
if(len > width)
width = len;
mvprintwt(y,x,"%s",sp->id);
}
x += width;
y = row_save;
if(x >= COLS)
goto done;
width = 10;
mvprintwt(y++,x,"%*s",width,"total");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp == NULL)
continue;
char total_buf[100];
mvprintwt(y,x,"%*s",width,ftime(total_buf,sizeof(total_buf),sp->tot_active));
}
x += width;
y = row_save;
if(x >= COLS)
goto done;
width = 10;
mvprintwt(y++,x,"%*s",width,"cur/idle");
{
long long time = gps_time_ns();
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session *sp = Sessions_copy[session];
if(sp == NULL)
continue;
char buf[100];
if(sp->now_active)
mvprintwt(y,x,"%*s",width,ftime(buf,sizeof(buf),sp->active));
else {
float idle_sec = (time - sp->last_active) / BILLION;
mvprintwt(y,x,"%*s",width,ftime(buf,sizeof(buf),idle_sec)); // Time idle since last transmission
}
}
}
x += width;
y = row_save;
if(x >= COLS)
goto done;
width = 6;
mvprintwt(y++,x,"%*s",width,"queue");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp == NULL || !sp->now_active || sp->muted || (Voting && Best_session != NULL && Best_session != sp))
continue;
int const d = modsub(sp->wptr,Rptr,BUFFERSIZE); // Unplayed samples on queue
int const queue_ms = d > 0 ? 1000 * d / DAC_samprate : 0; // milliseconds
mvprintwt(y,x,"%*d",width,queue_ms); // Time idle since last transmission
}
x += width;
y = row_save;
// Opus/pcm
x++; // Left justified, add a space
if(x >= COLS)
goto done;
width = 6;
mvprintwt(y++,x,"%-*s",width,"type");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp != NULL)
mvprintwt(y,x,"%-*s",width,encoding_string(sp->pt_table[sp->type].encoding));
}
x += width;
y = row_save;
// frame size, ms
if(x >= COLS)
goto done;
width = 3;
mvprintwt(y++,x,"%*s",width,"ms");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp != NULL && sp->samprate != 0)
mvprintwt(y,x,"%*d",width,(1000 * sp->frame_size/sp->samprate)); // frame size, ms
}
x += width;
y = row_save;
// channels
if(x >= COLS)
goto done;
width = 2;
mvprintwt(y++,x,"%*s",width,"c");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp != NULL)
mvprintwt(y,x,"%*d",width,sp->channels);
}
x += width;
y = row_save;
// BW
if(x >= COLS)
goto done;
width = 3;
mvprintwt(y++,x,"%*s",width,"bw");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp != NULL)
mvprintwt(y,x,"%*d",width,sp->bandwidth);
}
x += width;
y = row_save;
// RTP payload type
if(x >= COLS)
goto done;
width = 4;
mvprintwt(y++,x,"%*s",width,"pt");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp != NULL)
mvprintwt(y,x,"%*d",width,sp->type);
}
x += width;
y = row_save;
// Packets
if(x >= COLS)
goto done;
width = 12;
mvprintwt(y++,x,"%*s",width,"packets");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp != NULL)
mvprintwt(y,x,"%*lu",width,sp->packets);
}
x += width;
y = row_save;
// Resets
if(x >= COLS)
goto done;
width = 7;
mvprintwt(y++,x,"%*s",width,"resets");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp != NULL)
mvprintwt(y,x,"%*lu",width,sp->resets);
}
x += width;
y = row_save;
// BW
if(x >= COLS)
goto done;
width = 6;
mvprintwt(y++,x,"%*s",width,"drops");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp != NULL)
mvprintwt(y,x,"%'*llu",width,(unsigned long long)sp->rtp_state.drops);
}
x += width;
y = row_save;
// Lates
if(x >= COLS)
goto done;
width = 6;
mvprintwt(y++,x,"%*s",width,"lates");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp != NULL)
mvprintwt(y,x,"%*lu",width,sp->lates);
}
x += width;
y = row_save;
// BW
if(x >= COLS)
goto done;
width = 6;
mvprintwt(y++,x,"%*s",width,"reseq");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp != NULL)
mvprintwt(y,x,"%*lu",width,sp->reseqs);
}
x += width;
y = row_save;
// Sockets
x++; // Left justified
mvprintwt(y++,x,"%s","sockets");
for(int session = First_session; session < Nsessions_copy; session++,y++){
struct session const *sp = Sessions_copy[session];
if(sp != NULL)
mvprintwt(y,x,"%s -> %s",formatsock(&sp->sender),sp->dest);
}
done:;
// Embolden the active lines
attr_t attrs = 0;
short pair = 0;
attr_get(&attrs, &pair, NULL);
for(int session = First_session; session < Nsessions_copy; session++){
struct session const *sp = Sessions_copy[session];
if(sp == NULL)
continue;
attr_t attr = A_NORMAL;
if(sp->now_active)
attr |= A_BOLD;
// 1 adjusts for the titles
// only underscore to just before the socket entry since it's variable length
mvchgat(1 + row_save + session,col_save,x,attr,pair,NULL);
}
if(Current != -1)
move(1 + row_save + Current,col_save); // Cursor on current line
// End of display writing
}
static void process_keyboard(void){
int const c = getch(); // Waits for 'update interval' ms if no input
if(c == EOF)
return; // No key hit
// These commands don't require session locking
bool serviced = true;
switch(c){
case 'Q': // quit program
Terminate = true;
break;
case 'v':
Verbose = !Verbose;
break;
case 'C':
Constant_delay = !Constant_delay;
break;
case 'A': // Start all new sessions muted
Start_muted = !Start_muted;
break;
case 'q':
Quiet_mode = !Quiet_mode;
break;
case '\f': // Screen repaint (formfeed, aka control-L)
clearok(curscr,TRUE);
break;
case 'h': // Help screen
help = !help;
break;
case 's': // Sort sessions by most recently active (or longest active)
sort_session_active(); // locks Sess_mutex internally
break;
case 't': // Sort sessions by most total activity
sort_session_total();
break;
case 'S':
Auto_sort = !Auto_sort;
break;
case KEY_RESIZE:
case EOF:
break;
default:
serviced = false; // Not handled in this switch(), so fall through and handle in the next with the lock taken
break;
}
if(serviced)
return;
// Commands below this point require session locking
serviced = true;
pthread_mutex_lock(&Sess_mutex);
// Commands manipulating the current session index
switch(c){
case KEY_NPAGE:
if(First_session + Sessions_per_screen < Nsessions){
First_session += Sessions_per_screen;
Current += Sessions_per_screen;
if(Current > Nsessions-1)
Current = Nsessions - 1;
}
break;
case KEY_PPAGE:
if(First_session - Sessions_per_screen >= 0){
First_session -= Sessions_per_screen;
Current -= Sessions_per_screen;
}
break;
case KEY_HOME: // first session
if(Nsessions > 0){
Current = 0;
First_session = 0;
}
break;
case KEY_END: // last session
if(Nsessions > 0){
Current = Nsessions-1;
First_session = max(0,Nsessions - Sessions_per_screen);
}
break;
case '\t':
case KEY_DOWN:
if(Current >= 0 && Current < Nsessions-1){
Current++;
if(Current >= First_session + Sessions_per_screen - 1)
First_session++;
}
break;
case KEY_BTAB:
case KEY_UP:
if(Current > 0){
Current--;
if(Current < First_session)
First_session--;
}
break;
default:
serviced = false;
}
if(serviced){
pthread_mutex_unlock(&Sess_mutex);
return;
}
// Commands operating on all sessions
serviced = true;
switch(c){
case 'U': // Unmute all sessions, resetting any that were muted
for(int i = 0; i < Nsessions; i++){
struct session *sp = sptr(i);
if(sp != NULL && sp->muted){
sp->reset = true; // Resynchronize playout buffer (output callback may have paused)
sp->muted = false;
}
}
break;
case 'M': // Mute all sessions
for(int i = 0; i < Nsessions; i++){
struct session *sp = sptr(i);
if(sp != NULL)
sp->muted = true;
}
break;
case 'N':
Notch = true;
for(int i=0; i < Nsessions; i++){
struct session *sp = sptr(i);
if(sp != NULL){
sp->notch_enable = true;
}
}
break;
case 'R': // Reset all sessions
for(int i=0; i < Nsessions;i++){
struct session *sp = sptr(i);
if(sp != NULL)
sp->reset = true;
}
break;
case 'F':
Notch = false;
for(int i=0; i < Nsessions; i++){
struct session *sp = sptr(i);
if(sp != NULL)
sp->notch_enable = false;
}
break;
default:
serviced = false; // Not handled by this switch
break;
}
if(serviced){
pthread_mutex_unlock(&Sess_mutex);
return;
}
// Commands operating on the current session
// Check validity of current session pointer so individual cases don't have to
// Do this last
// Lock still held!
serviced = true;
struct session *sp = sptr(Current);
if(sp == NULL){
// Current index not valid
pthread_mutex_unlock(&Sess_mutex);
beep();
return;
}
switch(c){
case 'f': // Turn off tone notching
sp->notch_enable = false;
break;
case 'n':
Notch = true;
sp->notch_enable = true;
break;
case '=': // If the user doesn't hit the shift key (on a US keyboard) take it as a '+'
case '+':
sp->gain *= 1.122018454; // +1 dB
break;
case '_': // Underscore is shifted minus
case '-':
sp->gain /= 1.122018454; // -1 dB
break;
case KEY_LEFT:
sp->pan = max(sp->pan - .01,-1.0);
break;
case KEY_RIGHT:
sp->pan = min(sp->pan + .01,+1.0);
break;
case KEY_SLEFT: // Shifted left - decrease playout buffer 10 ms
if(Playout >= -100){
Playout -= 1;
sp->reset = true;
}
break;
case KEY_SRIGHT: // Shifted right - increase playout buffer 10 ms
Playout += 1;
sp->reset = true;
break;
case 'u': // Unmute and reset Current session
if(sp->muted){
sp->reset = true; // Resynchronize playout buffer (output callback may have paused)
sp->muted = false;
}
break;
case 'm': // Mute Current session
sp->muted = true;
break;
case 'r': // Manually reset playout queue
sp->reset = true;
break;
case KEY_DC: // Delete
case KEY_BACKSPACE:
case 'd': // Delete current session
sp->terminate = true; // Also keeps it from being found again by sptr()
pthread_mutex_unlock(&Sess_mutex); // close_session will need the lock, at least
// We have to wait for it to clean up before we close and remove its session
pthread_join(sp->task,NULL);
sp->task = (pthread_t)0;
close_session(&sp); // Decrements Nsessions
if(Current >= Nsessions)
Current = Nsessions-1; // -1 when no sessions
return; // Avoid unlocking again
default:
serviced = false;
break;
}
pthread_mutex_unlock(&Sess_mutex);
if(!serviced)
beep(); // Not serviced by anything
}