-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipbt.c
1953 lines (1741 loc) · 48.6 KB
/
ipbt.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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <math.h>
#include <limits.h>
#include <ctype.h>
#include <locale.h>
#include <ncurses.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include "putty.h"
#include "terminal.h"
#include "misc.h"
const char usagemsg[] =
"usage: ipbt [ [ -w width ] [ -h height ] | -u ] [ -T | -N ]\n"
" [ -f frame ] [ -P ] file [file...]\n"
"where: -w width specify width of emulated terminal screen (default 80)\n"
" -h height specify height of emulated terminal screen (default 24)\n"
" -l lazy parsing (immediate playback)\n"
" -u use size of real terminal for emulated terminal\n"
" -T assume input files to be ttyrec format\n"
" -N assume input files to be nh-recorder format\n"
" -f frame start viewing at a particular frame number\n"
" -P terminate immediately after reading input\n"
" also: ipbt --version report version number\n"
" ipbt --help display this help text\n"
" ipbt --licence display the (MIT) licence text\n"
;
void usage(void) {
fputs(usagemsg, stdout);
}
const char licencemsg[] =
"IPBT is an application derived from the PuTTY source base by Simon\n"
"Tatham.\n"
"\n"
"The PuTTY copyright notice is reproduced below. The long list of\n"
"copyright holders are not all actually relevant, since some of them\n"
"contributed code to PuTTY which is not included in IPBT.\n"
"\n"
"| PuTTY is copyright 1997-2007 Simon Tatham.\n"
"|\n"
"| Portions copyright Robert de Bath, Joris van Rantwijk, Delian\n"
"| Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,\n"
"| Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus\n"
"| Kuhn, and CORE SDI S.A.\n"
"\n"
"Those portions of IPBT which are not part of PuTTY are copyright\n"
"2005-2007 Simon Tatham. The same licence terms apply to them as to\n"
"PuTTY:\n"
"\n"
"| Permission is hereby granted, free of charge, to any person\n"
"| obtaining a copy of this software and associated documentation files\n"
"| (the \"Software\"), to deal in the Software without restriction,\n"
"| including without limitation the rights to use, copy, modify, merge,\n"
"| publish, distribute, sublicense, and/or sell copies of the Software,\n"
"| and to permit persons to whom the Software is furnished to do so,\n"
"| subject to the following conditions:\n"
"|\n"
"| The above copyright notice and this permission notice shall be\n"
"| included in all copies or substantial portions of the Software.\n"
"|\n"
"| THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n"
"| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n"
"| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n"
"| NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE\n"
"| FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF\n"
"| CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\n"
"| WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n"
;
void licence(void) {
fputs(licencemsg, stdout);
}
void version(void) {
#define SVN_REV "$Revision$"
char rev[sizeof(SVN_REV)];
char *p, *q;
strcpy(rev, SVN_REV);
for (p = rev; *p && *p != ':'; p++);
if (*p) {
p++;
while (*p && isspace((unsigned char)*p)) p++;
for (q = p; *q && !isspace((unsigned char)*q) && *q != '$'; q++);
if (*q) *q = '\0';
printf("ipbt revision %s", p);
} else {
printf("ipbt: unknown version");
}
putchar('\n');
}
int curses_active;
void cleanup_exit(int code)
{
exit(code);
}
void fatalbox(char *p, ...)
{
va_list ap;
if (curses_active)
endwin();
fprintf(stderr, "FATAL ERROR: ");
va_start(ap, p);
vfprintf(stderr, p, ap);
va_end(ap);
fputc('\n', stderr);
cleanup_exit(1);
}
void modalfatalbox(char *p, ...)
{
va_list ap;
if (curses_active)
endwin();
fprintf(stderr, "FATAL ERROR: ");
va_start(ap, p);
vfprintf(stderr, p, ap);
va_end(ap);
fputc('\n', stderr);
cleanup_exit(1);
}
#define FG 0x000F0000
#define BG 0x00F00000
#define FGBG 0x00FF0000
#define BOLD 0x01000000
#define UNDER 0x02000000
#define REV 0x04000000
#define BLINK 0x08000000
#define FGSHIFT 16
#define BGSHIFT 20
#define FGBGSHIFT FGSHIFT
#define NODEFAULT -1
#define TTYREC 0
#define NHRECORDER 1
#define NTYPES 2
static const char *const typenames[] = { "ttyrec", "nh-recorder" };
struct filename {
char *name;
int type;
};
struct reader {
int i;
char *p;
FILE *fp;
char hdrbuf[12];
char nhrbuf[4096];
char *termdata;
int termdatasize, nframes;
unsigned long long timestamp, oldtimestamp;
unsigned long long frametime;
int type, nhrstate;
long fileoff;
int totalsize;
};
struct parray;
struct inst {
Terminal *term;
struct unicode_data ucsdata;
Config cfg;
int *playscreen, *screen, *oldscreen, w, h, screenlen;
struct parray **parrays;
int frames;
unsigned long long movietime;
struct filename *filenames;
int nfiles, filesize;
struct reader *reader;
int cpairs[(FGBG >> FGSHIFT) + 1];
int pairsused, nines;
int number, osd;
int playing;
int logmod;
double speedmod;
char *searchstr;
int searchback;
char *pname;
};
/*
* Our notional screen data structure is simply an array of 32-bit
* integers: W*H screen positions, plus a magic one for the cursor
* position.
*
* This rather simplistic and flat architecture is because a lot of
* the time we won't be directly storing these. Instead, we'll be
* storing a list of how each integer changed over time. Our
* _movie_ data structure will be a collection of pseudo-`arrays',
* one for each of the integers in our screen array, containing
* elements of the form (frame number in which the integer changed,
* what it changed to). This means that we can determine the value
* of integer I at frame F by a binary search of the array. This
* enables us to play back and forth through the entire movie with
* arbitrary rewind. Hence the need to have the entire terminal
* state encoded as an unstructured list of integers: if I had to
* give separate treatment to the cursor position and any other
* future enhancements such as line attributes, it would all get
* more complicated.
*
* To prevent memory wastage by repeatedly reallocing several
* actual arrays, we instead use the concept of a `pseudo-array',
* which is structured much like an ext2fs file: initially the
* array is a single block of memory in the obvious format, but
* once it overflows that block we move to a two-layer structure
* containing an index block with (frame, sub-block) records each
* indexing a block of real array. When the index block overflows,
* we move to a three-layer structure with a second-level index
* block indexing the first-level ones, and so on.
*/
struct parray_block;
struct parray_level0 {
int frame;
int data;
};
struct parray_level1 {
int frame;
struct parray_block *subblock;
};
#ifdef PARRAY_TEST
#define PARRAY_L0COUNT 5
#define PARRAY_L1COUNT 3
#else
#define PARRAY_BLKSIZE 16384
#define PARRAY_L0COUNT (PARRAY_BLKSIZE / sizeof(struct parray_level0))
#define PARRAY_L1COUNT (PARRAY_BLKSIZE / sizeof(struct parray_level1))
#endif
struct parray {
int toplevel;
int items;
int memusage;
struct parray_block *root;
};
struct parray_block {
union {
struct parray_level0 level0[PARRAY_L0COUNT];
struct parray_level1 level1[PARRAY_L1COUNT];
} u;
};
struct parray *parray_new(void)
{
struct parray *pa = snew(struct parray);
pa->toplevel = -1;
pa->items = 0;
pa->memusage = sizeof(struct parray);
pa->root = NULL;
return pa;
}
void parray_append(struct parray *pa, int frame, int data)
{
struct parray_block *pb, *pb2;
int i, n, index, count;
/*
* Special case: the very first item.
*/
if (!pa->items) {
pb = snew(struct parray_block);
pa->memusage += sizeof(struct parray_block);
for (i = 0; i < (int) PARRAY_L0COUNT; i++) {
pb->u.level0[i].frame = INT_MAX;
pb->u.level0[i].data = 0;
}
pb->u.level0[0].frame = frame;
pb->u.level0[0].data = data;
pa->items++;
pa->toplevel = 0;
pa->root = pb;
return;
}
/*
* Figure out how many items are covered by a single block at
* the parray's current top level.
*/
count = PARRAY_L0COUNT;
for (i = 1; i <= pa->toplevel; i++)
count *= PARRAY_L1COUNT;
/*
* If this is equal to the parray's current total item count,
* we must create a new top-level block.
*/
assert(pa->items <= count);
if (pa->items == count) {
pb = snew(struct parray_block);
pa->memusage += sizeof(struct parray_block);
/*
* pa->root->u.level0[0].frame and
* pa->root->u.level1[0].frame overlap exactly (guaranteed
* by the C standard), so we don't need to worry about
* which one to access through.
*/
pb->u.level1[0].frame = pa->root->u.level1[0].frame;
pb->u.level1[0].subblock = pa->root;
pa->toplevel++;
pa->root = pb;
count *= PARRAY_L1COUNT; /* we've moved up a level */
}
/*
* Now work down the tree. At each level, create a new block
* and descend to it if necessary, otherwise descend to the
* last existing block if it's not completely full.
*/
pb = pa->root;
index = pa->items;
for (i = pa->toplevel; i-- > 0 ;) {
count /= PARRAY_L1COUNT;
n = index / count;
assert(n < (int) PARRAY_L1COUNT);
index %= count;
if (!index) {
/*
* Create a new empty block at the next level down.
*/
pb2 = snew(struct parray_block);
pb->u.level1[n].frame = frame;
pb->u.level1[n].subblock = pb2;
}
/*
* Descend to the partially filled end block, whether or
* not we just had to create it.
*/
pb = pb->u.level1[n].subblock;
}
/*
* Now we're sitting on a level-0 block which is known to have
* spare space. Add our entry.
*/
pb->u.level0[index].frame = frame;
pb->u.level0[index].data = data;
pa->items++;
}
int parray_search(struct parray *pa, int frame, int *index_out)
{
struct parray_block *pb;
int count, total, i, n, top, bot, mid, index;
assert(pa->root);
assert(pa->items > 0);
assert(frame >= pa->root->u.level1[0].frame);
/*
* Figure out how many items are covered by a single block at
* the parray's current top level. This will tell us how many
* blocks to check at each level of the parray.
*/
count = PARRAY_L0COUNT;
for (i = 1; i <= pa->toplevel; i++)
count *= PARRAY_L1COUNT;
index = 0;
/*
* Binary search each block on the way down.
*/
pb = pa->root;
total = pa->items;
for (i = pa->toplevel; i-- > 0 ;) {
count /= PARRAY_L1COUNT;
n = (total + count - 1) / count;
bot = 0;
top = n;
while (top - bot > 1) {
mid = (top + bot) / 2;
if (pb->u.level1[mid].frame > frame)
top = mid;
else
bot = mid;
}
total -= bot * count;
index += bot * count;
if (total > count)
total = count;
pb = pb->u.level1[bot].subblock;
}
/*
* And binary-search the bottom block.
*/
bot = 0;
top = total;
while (top - bot > 1) {
mid = (top + bot) / 2;
if (pb->u.level0[mid].frame > frame)
top = mid;
else
bot = mid;
}
index += bot;
if (index_out)
*index_out = index;
return pb->u.level0[bot].data;
}
int parray_retrieve(struct parray *pa, int index, int *frame)
{
struct parray_block *pb;
int count, total, i, n;
assert(pa->root);
assert(index >= 0 && index < pa->items);
/*
* Figure out how many items are covered by a single block at
* the parray's current top level.
*/
count = PARRAY_L0COUNT;
for (i = 1; i <= pa->toplevel; i++)
count *= PARRAY_L1COUNT;
/*
* Search down the tree.
*/
pb = pa->root;
total = pa->items;
for (i = pa->toplevel; i-- > 0 ;) {
count /= PARRAY_L1COUNT;
n = index / count;
index -= n * count;
pb = pb->u.level1[n].subblock;
}
if (frame)
*frame = pb->u.level0[index].frame;
return pb->u.level0[index].data;
}
#define CURSOR (inst->w * inst->h)
#define TIMETOP (inst->w * inst->h + 1)
#define TIMEBOT (inst->w * inst->h + 2)
#define FILENO (inst->w * inst->h + 3)
#define OFFSET (inst->w * inst->h + 4)
#define TOTAL (inst->w * inst->h + 5)
void sys_cursor(void *frontend, int x, int y)
{
struct inst *inst = (struct inst *)frontend;
inst->screen[CURSOR] = y * inst->w + x;
}
Context get_ctx(void *frontend)
{
return (Context)frontend;
}
void do_text(Context ctx, int x, int y, wchar_t *text, int len,
unsigned long attr, int lattr)
{
struct inst *inst = (struct inst *)ctx;
int i, index;
unsigned int fg, bg, val;
(void) lattr; /* unused */
for (i = 0; i < len; i++) {
assert(y >= 0 && y < inst->h);
assert(x+i >= 0 && x+i < inst->w);
index = y * inst->w + (x+i);
val = text[i] & 0xFF;
if (text[i] >= 0xD95F && text[i] < 0xD97F)
val += 0x100;
if (attr & ATTR_BOLD)
val |= BOLD;
if (attr & ATTR_UNDER)
val |= UNDER;
if (attr & ATTR_REVERSE)
val |= REV;
if (attr & ATTR_BLINK)
val |= BLINK;
fg = (attr & ATTR_FGMASK) >> ATTR_FGSHIFT;
bg = (attr & ATTR_BGMASK) >> ATTR_BGSHIFT;
if (fg >= 8)
fg = 9;
if (bg >= 8)
bg = 9;
val |= (fg << FGSHIFT) | (bg << BGSHIFT);
inst->screen[index] = val;
}
}
void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
unsigned long attr, int lattr)
{
do_text(ctx, x, y, text, len, attr, lattr);
}
void store_frame(struct inst *inst, unsigned long long delay,
int fileno, long fileoff)
{
int i, n;
/*
* Force the terminal to refresh, so that our data is up to
* date.
*/
term_invalidate(inst->term);
term_update(inst->term);
/*
* Now see which terminal integers have changed, and write
* movie records for the ones that have.
*/
inst->movietime += delay;
inst->frames++;
inst->screen[TIMETOP] = (unsigned long long)inst->movietime >> 32;
inst->screen[TIMEBOT] = (unsigned long long)inst->movietime & 0xFFFFFFFF;
inst->screen[FILENO] = fileno;
inst->screen[OFFSET] = fileoff;
n = 0;
for (i = 0; i < inst->screenlen; i++) {
/*
* 0xFFFFFFFF is an invalid value for _any_ integer, which
* enables us to use it as the `not initialised yet'
* setting for oldscreen.
*/
assert(inst->screen[i] != (int) 0xFFFFFFFF);
if (inst->screen[i] != inst->oldscreen[i])
n++;
}
for (i = 0; i < inst->screenlen; i++) {
if (inst->screen[i] != inst->oldscreen[i]) {
parray_append(inst->parrays[i], inst->frames-1, inst->screen[i]);
inst->oldscreen[i] = inst->screen[i];
}
}
}
void start_player(struct inst *inst)
{
int i;
setlocale(LC_CTYPE, ""); /* arrange that curses can query the charset */
initscr();
noecho();
move(0,0);
refresh();
if (has_colors()) {
start_color();
for (i = 0; i < (int) lenof(inst->cpairs); i++)
inst->cpairs[i] = -1;
inst->pairsused = 1;
inst->nines = (use_default_colors() == OK);
} else {
inst->pairsused = -1;
}
curses_active = TRUE;
}
void end_player(void)
{
if (!curses_active)
return;
endwin();
curses_active = FALSE;
}
unsigned int_for_frame(struct inst *inst, int i, int f)
{
return parray_search(inst->parrays[i], f, NULL);
}
void set_cpair(struct inst *inst, int col)
{
int fg, bg;
if (!inst->nines) {
/*
* If default fg and bg are not supported, fall back to
* white on black as a default.
*/
fg = ((col << FGBGSHIFT) & FG) >> FGSHIFT;
bg = ((col << FGBGSHIFT) & BG) >> BGSHIFT;
if (fg == 9)
fg = 7;
if (bg == 9)
bg = 0;
col = ((fg << FGSHIFT) | (bg << BGSHIFT)) >> FGBGSHIFT;
}
if (col != 0x99) {
if (inst->cpairs[col] == -1) {
inst->cpairs[col] = inst->pairsused++;
fg = ((col << FGBGSHIFT) & FG) >> FGSHIFT;
bg = ((col << FGBGSHIFT) & BG) >> BGSHIFT;
init_pair(inst->cpairs[col],
(fg < 8 ? fg : -1),
(bg < 8 ? bg : -1));
}
wattron(stdscr, COLOR_PAIR(inst->cpairs[col]));
}
}
void display_frame(struct inst *inst, int f)
{
int i, x, y;
/*
* Fetch the screen state in this frame.
*/
for (i = 0; i < inst->screenlen; i++)
inst->playscreen[i] = int_for_frame(inst, i, f);
/*
* Now display it.
*/
for (y = 0; y < inst->h; y++)
for (x = 0; x < inst->w; x++) {
unsigned val = inst->playscreen[y*inst->w + x];
int col, ch;
wattrset(stdscr, A_NORMAL);
if (val & BOLD)
wattron(stdscr, A_BOLD);
if (val & UNDER)
wattron(stdscr, A_UNDERLINE);
if (val & REV)
wattron(stdscr, A_REVERSE);
if (val & BLINK)
wattron(stdscr, A_BLINK);
if (inst->pairsused >= 0) {
col = (val & FGBG) >> FGBGSHIFT;
set_cpair(inst, col);
}
wmove(stdscr, y, x);
if (val & 0x100) {
switch (val & 0xFF) {
/*
* Use the ncurses codes for the VT100 line
* drawing characters where available. We can't
* do all of them: the control character
* representations such as HT and VT are not
* encoded by ncurses. We replace missing
* characters with ACS_BLOCK, on the grounds
* that they've got to be _something_.
*/
case 0x5f:
ch = ' ';
break;
case 0x60:
ch = ACS_DIAMOND;
break;
case 0x61:
ch = ACS_CKBOARD;
break;
case 0x66:
ch = ACS_DEGREE;
break;
case 0x67:
ch = ACS_PLMINUS;
break;
case 0x6a:
ch = ACS_LRCORNER;
break;
case 0x6b:
ch = ACS_URCORNER;
break;
case 0x6c:
ch = ACS_ULCORNER;
break;
case 0x6d:
ch = ACS_LLCORNER;
break;
case 0x6e:
ch = ACS_PLUS;
break;
case 0x6f:
ch = ACS_S1;
break;
case 0x70:
ch = ACS_S3;
break;
case 0x71:
ch = ACS_HLINE;
break;
case 0x72:
ch = ACS_S7;
break;
case 0x73:
ch = ACS_S9;
break;
case 0x74:
ch = ACS_LTEE;
break;
case 0x75:
ch = ACS_RTEE;
break;
case 0x76:
ch = ACS_BTEE;
break;
case 0x77:
ch = ACS_TTEE;
break;
case 0x78:
ch = ACS_VLINE;
break;
case 0x79:
ch = ACS_LEQUAL;
break;
case 0x7a:
ch = ACS_GEQUAL;
break;
case 0x7b:
ch = ACS_PI;
break;
case 0x7c:
ch = ACS_NEQUAL;
break;
case 0x7d:
ch = ACS_STERLING;
break;
case 0x7e:
ch = ACS_BULLET;
break;
default:
ch = ACS_BLOCK;
break;
}
} else {
ch = val & 0xFF;
}
waddch(stdscr, ch);
}
/*
* Draw the OSD and the numeric count, if any.
*/
if (inst->number) {
char buf[40];
int len = sprintf(buf, " %d ", inst->number);
wmove(stdscr, 1, inst->w - len - 1);
wattrset(stdscr, A_NORMAL);
wattron(stdscr, A_BOLD);
set_cpair(inst, 0x47); /* white on blue */
waddstr(stdscr, buf);
}
if (inst->osd) {
char buf1[80], buf2[80], buf3[80], buf4[80];
long long t;
t = int_for_frame(inst, TIMETOP, f);
t = (t << 32) + int_for_frame(inst, TIMEBOT, f);
sprintf(buf2, "%s x %g", inst->logmod ? "LOG" : "", inst->speedmod);
if (inst->logmod || inst->speedmod != 1.0)
sprintf(buf4, " Speed:%20s ", buf2);
else
buf4[0] = '\0';
if (inst->reader) sprintf(buf2, "%d / %d*", f, inst->frames);
else sprintf(buf2, "%d / %d", f, inst->frames);
sprintf(buf1, " Frame:%20s ", buf2);
sprintf(buf2, " Time:%21.3f ", t / 1000000.0);
sprintf(buf3, " Mode:%21s ",
(inst->playing ? "PLAY" : "PAUSE"));
wattrset(stdscr, A_NORMAL);
wattron(stdscr, A_BOLD);
set_cpair(inst, 0x47); /* white on blue */
wmove(stdscr, 1, 1);
waddstr(stdscr, buf1);
wmove(stdscr, 2, 1);
waddstr(stdscr, buf2);
wmove(stdscr, 3, 1);
waddstr(stdscr, buf3);
wmove(stdscr, 4, 1);
waddstr(stdscr, buf4);
}
/*
* Position the cursor.
*/
x = inst->playscreen[CURSOR];
y = x / inst->w;
x %= inst->w;
wmove(stdscr, y, x);
}
/*
* Search the movie array for a frame containing a given piece of
* text. Returns the frame in which the text was found, or <0 if
* not.
*/
int search(struct inst *inst, char *string, int start_frame, int backwards)
{
int f = start_frame;
int i, j, k, len;
int *searchlines;
int *indices, *nextframes;
char *scrbuf;
/*
* Check the bounds.
*/
if (start_frame >= inst->frames || start_frame < 0)
return -1; /* not found */
/*
* We track which lines of the display actually changed between
* frames, in order to avoid repeatedly searching an unchanged
* line. Initially, of course, we set all these flags to TRUE
* because the first frame must be searched in full.
*/
searchlines = snewn(inst->h, int);
for (i = 0; i < inst->h; i++)
searchlines[i] = TRUE;
/*
* Allocate space for tracking indices, and the next frame in
* which each integer changes, in the display parrays.
*/
indices = snewn(inst->w * inst->h, int);
nextframes = snewn(inst->w * inst->h, int);
for (i = 0; i < inst->w * inst->h; i++)
indices[i] = -1;
/*
* And allocate space for the actual display buffer.
*/
scrbuf = snewn(inst->w * inst->h, char);
memset(scrbuf, 0, inst->w * inst->h);
len = strlen(string);
while (1) {
/*
* Retrieve the current frame.
*/
for (i = 0; i < inst->w * inst->h; i++) {
int integer, nextframe = -1;
int changed = FALSE;
if (indices[i] < 0) {
/*
* This is the first time we've retrieved this
* integer, so we need to do a conventional
* retrieve operation and set up our index.
*/
integer = parray_search(inst->parrays[i], f, &indices[i]);
changed = TRUE;
} else if (backwards && f < nextframes[i]) {
/*
* This integer has changed in this frame (reverse
* search version).
*/
indices[i]--;
integer = parray_retrieve(inst->parrays[i], indices[i],
&nextframe);
changed = TRUE;
} else if (!backwards && f >= nextframes[i]) {
/*
* This integer has changed in this frame (forward
* search version).
*/
indices[i]++;
integer = parray_retrieve(inst->parrays[i], indices[i], NULL);
changed = TRUE;
}
if (changed) {
char bufval;
/*
* Update the screen buffer and mark this line as
* changed.
*/
if (integer & 0x100)
bufval = 0; /* ignore line drawing characters */
else
bufval = integer;
if (scrbuf[i] != bufval) {
scrbuf[i] = bufval;
searchlines[i / inst->w] = TRUE;
}
/*
* Find the next frame in which this integer
* changes.
*/
if (nextframe < 0) {
if (backwards)
parray_retrieve(inst->parrays[i], indices[i],
&nextframe);
else {
if (indices[i]+1 < inst->parrays[i]->items)
parray_retrieve(inst->parrays[i], indices[i]+1,
&nextframe);
else
nextframe = inst->frames;
}
}
nextframes[i] = nextframe;
}
}
/*
* Search whatever lines of the current frame we need to.
*/
for (i = 0; i < inst->h; i++)
if (searchlines[i]) {
int found;
searchlines[i] = FALSE;
/*
* FIXME: for the moment we'll just do a naive
* string search.
*/
found = FALSE;
for (j = 0; j <= inst->w - len; j++) {
for (k = 0; k < len; k++)
if (scrbuf[i * inst->w + j + k] != string[k])
break;
if (k == len) {
found = TRUE;
break;
}
}
if (found)
goto found_it;
}
/*
* Not found, so move to next frame.
*/
if (backwards) {
f--;
if (f < 0) {
f = -1;
goto found_it;
}
} else {
f++;
if (f >= inst->frames) {
f = -1;
goto found_it;
}
}
}
found_it: