-
Notifications
You must be signed in to change notification settings - Fork 2
/
window.c
1990 lines (1769 loc) · 39.7 KB
/
window.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
/*
########################################################################
# This file is part of the minicom communications package for WRAMP.
#
# Copyright 1991-1995 Miquel van Smoorenburg.
# Copyright (C) 2019 The University of Waikato, Hamilton, New Zealand.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
########################################################################
*/
#include "port.h"
#include "window.h"
#include "charmap.h"
/* Status line code on/off. */
#define ST_LINE 1
/* fmg 2/20/94 macros - Length of Macros */
#ifndef MAC_LEN
#define MAC_LEN 257
#endif
/* Don't want to include all header stuff for three prototypes from sysdep.c */
#if __STDC__
int setcbreak(int);
int wxgetch(void);
void getrowcols(int *rows, int *cols);
#else
int setcbreak();
int wxgetch();
void getrowcols();
#endif
#ifndef BBS
#include "config.h"
#endif
#define BUFFERSIZE 2048
#define swap(x, y) { int d = (x); (x) = (y); (y) = d; }
/* Terminal capabilities */
static char *CM, *IS, *RS, *AC, *EA;
static char *ME, *SE, *UE, *AE;
static char *AS, *MB, *MD, *MR, *SO, *US;
static char *CE, *Al, *Dl, *AL, *DL;
static char *CS, *SF, *SR, *VB, *BL;
static char *VE, *VI, *KS, *KE;
static char *CD, *CL, *IC, *DC;
char *BC;
static char *CR, *NL;
#if ST_LINE
static char *TS, *FS, *DS;
#endif
/* Special characters */
static char D_UL;
static char D_HOR;
static char D_UR;
static char D_LL;
static char D_VER;
static char D_LR;
static char S_UL;
static char S_HOR;
static char S_UR;
static char S_LL;
static char S_VER;
static char S_LR;
static char _bufstart[BUFFERSIZE];
static char *_bufpos = _bufstart;
static char *_buffend;
static ELM *gmap;
static char curattr = -1;
static char curcolor = -1;
static int curx = -1;
static int cury = -1;
static int _intern = 0;
static int _curstype = CNORMAL;
static int _has_am = 0;
static int _mv_standout = 0;
static ELM oldc;
static int sflag = 0;
/*
* Smooth is only defined for slow machines running Remote.
* With this defined, Remote will buffer only per-line
* and the output will look much less 'jerky'. (I hope :-)
*/
#ifdef SMOOTH
static WIN *curwin = NIL_WIN;
extern WIN *us;
#endif
int useattr = 1;
int dirflush = 1;
int LINES, COLS;
int usecolor = 0;
WIN *stdwin;
char *_tptr = CNULL;
int screen_ibmpc = 0;
int screen_iso = 0;
int w_init = 0;
#if ST_LINE
int use_status = 1;
#else
int use_status = 0;
#endif
/* Standard vt100 map (ac cpability) */
static char *def_ac = "+\273,\253aaffggjjkkllmmnnooqqssttuuvvwwxx";
#if DEBUG
/*
* Debug to stdout
*/
int debug(s, a1, a2, a3, a4)
char *s;
int a1, a2, a3, a4;
{
char lala[80];
sprintf(lala, s, a1, a2, a3, a4);
write(2, lala, strlen(lala));
return(0);
}
#endif
/* ===== Low level routines ===== */
/*
* Flush the screen buffer
*/
void wflush()
{
int todo, done;
todo = _bufpos - _bufstart;
_bufpos = _bufstart;
while(todo > 0) {
done = write(1, _bufpos, todo);
if (done > 0) {
todo -= done;
_bufpos += done;
}
if (done < 0 && errno != EINTR) break;
}
_bufpos = _bufstart;
}
/*
* Output a raw character to the screen
*/
static int outchar(c)
int c;
{
*_bufpos++ = c;
if (_bufpos >= _buffend) wflush();
#if defined(SMOOTH)
if (curwin == us && (c == '\n' || c == '\r')) wflush();
#endif
return(0);
}
/*
* Output a raw string to the screen.
*/
static void outstr(s)
char *s;
{
tputs(s, 1, outchar);
}
/*
* Turn off all attributes
*/
static void _attroff()
{
if (ME != CNULL)
outstr(ME);
else {
if (SE != CNULL) outstr(SE);
if (UE != CNULL) outstr(UE);
}
if (AE != CNULL) outstr(AE);
}
/*
* Turn some attributes on
*/
static void _attron(attr)
char attr;
{
if (!usecolor || (attr & XA_REVERSE) == 0) {
/* Reverse standout does not look too good.. */
if (attr & XA_BOLD && MD != CNULL) outstr(MD);
if (attr & XA_STANDOUT && SO != CNULL) outstr(SO);
if (attr & XA_UNDERLINE && US != CNULL) outstr(US);
}
if (attr & XA_REVERSE && MR != CNULL) outstr(MR);
if (attr & XA_BLINK && MB != CNULL) outstr(MB);
if (attr & XA_ALTCHARSET && AS != CNULL) outstr(AS);
}
/*
* Set the colors
*/
static void _colson(color)
char color;
{
char buf[12];
sprintf(buf, "\033[%d;%dm", COLFG(color) + 30, COLBG(color) + 40);
outstr(buf);
}
/*
* Set global attributes, if different.
*/
static void _setattr(attr, color)
char attr, color;
{
if (!useattr) return;
if (!usecolor) {
curcolor = color;
if (attr == curattr) return;
curattr = attr;
_attroff();
_attron(attr);
return;
}
if (attr == curattr && color == curcolor) return;
_attroff();
_colson(color);
_attron(attr);
curattr = attr;
curcolor = color;
}
/*
* Goto (x, y) in stdwin
*/
static void _gotoxy(x, y)
int x, y;
{
int oldattr = -1;
#if ST_LINE
int tmp;
/* Sanity check. */
if (x >= COLS || y > LINES || (x == curx && y == cury)) return;
if (use_status) {
/* Leaving status line? */
if (cury == LINES && y < cury) {
outstr(FS);
/* Re-set attributes. */
tmp = curattr;
curattr = -1;
_setattr(tmp, curcolor);
outstr(tgoto(CM, x, y));
curx = x; cury = y;
return;
}
/* Writing on status line? */
else if (y == LINES) {
/* From normal screen? */
if (cury < y) {
outstr(tgoto(TS, x, x));
curx = x;
cury = y;
/* Set the right attributes. */
tmp = curattr;
curattr = -1;
_setattr(tmp, curcolor);
return;
}
}
}
#else
/* Sanity check. */
if (x >= COLS || y >= LINES || (x == curx && y == cury)) {
# if 0
if (x >= COLS || y >= LINES)
fprintf(stderr, "OOPS: (x, y) == (%d, %d)\n",
COLS, LINES);
# endif
return;
}
#endif
if (!_mv_standout && curattr != XA_NORMAL) {
oldattr = curattr;
_setattr(XA_NORMAL, curcolor);
}
if (CR != CNULL && y == cury && x == 0)
outstr(CR);
#if 0 /* Hmm, sometimes NL only works in the first column */
else if (NL != CNULL && x == curx && y == cury + 1)
outstr(NL);
#else
else if (NL != CNULL && x == 0 && x == curx && y == cury + 1)
outstr(NL);
#endif
else if (BC != CNULL && y == cury && x == curx - 1)
outstr(BC);
else
outstr(tgoto(CM, x, y));
curx = x;
cury = y;
if (oldattr != -1) _setattr(oldattr, curcolor);
}
/*
* Write a character in stdwin at x, y with attr & color
* 'doit' can be -1: only write to screen, not to memory
* 0: only write to memory, not to screen
* 1: write to both screen and memory
*/
static void _win_write(c, doit, x, y,attr, color)
int c, doit;
int x, y;
char attr, color;
{
ELM *e;
/* If the terminal has automatic margins, we can't write to the
* last line, last character. After scrolling, this "invisible"
* character is automatically restored.
*/
if (_has_am && y >= LINES - 1 && x >= COLS - 1) {
doit = 0;
sflag = 1;
oldc.value = c;
oldc.attr = attr;
oldc.color = color;
}
#if ST_LINE
if (x < COLS && y <= LINES) {
#else
if (x < COLS && y < LINES) {
#endif
if (doit != 0) {
_gotoxy(x, y);
_setattr(attr, color);
(void) outchar((screen_ibmpc || (attr & XA_ALTCHARSET)) ? c : wcharmap[(unsigned char)c]);
curx++;
}
if (doit >= 0) {
e = &gmap[x + y * COLS];
e->value = c;
e->attr = attr;
e->color = color;
}
}
}
/*
* Set cursor type.
*/
static void _cursor(type)
int type;
{
_curstype = type;
if (type == CNORMAL && VE != CNULL) outstr(VE);
if (type == CNONE && VE != CNULL && VI != CNULL) outstr(VI);
}
/* ==== High level routines ==== */
#if 0
/* This code is functional, but not yet used.
* It might be one day....
*/
/*
* Resize a window
*/
void wresize(win, lines, cols)
WIN *win;
int lines, cols;
{
int x, y;
ELM *oldmap, *newmap, *e, *n;
if ((newmap = (ELM *)malloc((lines + 1) * cols * sizeof(ELM))) == (ELM *)NULL)
return;
if (win == stdwin)
oldmap = gmap;
else
oldmap = win->map;
for(y = 0; y < lines; y++)
for(x = 0; x < cols; x++) {
n = &newmap[y + x * cols];
if (x < win->xs && y < win->ys) {
e = &oldmap[y + x * COLS];
n->value = e->value;
n->color = e->color;
n->attr = e->attr;
} else {
n->value = ' ';
n->color = win->color;
n->attr = win->attr;
}
}
if (win->sy2 == win->y2) win->sy2 = win->y1 + lines - 1;
win->y2 = win->y1 + lines - 1;
win->ys = lines;
win->xs = cols;
free(oldmap);
if (win == stdwin) {
gmap = newmap;
LINES = lines;
COLS = cols;
} else
win->map = newmap;
}
#endif
/*
* Create a new window.
*/
/*ARGSUSED*/
WIN *wopen(x1, y1, x2, y2, border, attr, fg, bg, direct, histlines, doclr)
int x1, y1, x2, y2;
int border;
int attr, fg, bg, direct;
int histlines;
int doclr;
{
WIN *w;
ELM *e;
int bytes;
int x, y;
int color;
int offs;
int xattr;
#ifdef SMOOTH
curwin = NIL_WIN;
#endif
if ((w = (WIN *)malloc(sizeof(WIN))) == (WIN *)0) return(w);
offs = (border != BNONE);
if (!screen_ibmpc && AS)
xattr = attr | XA_ALTCHARSET;
else
xattr = attr;
if (x1 < offs) x1 = offs;
if (y1 < offs) y1 = offs;
#if 0
if (x2 >= COLS - offs) x2 = COLS - offs - 1;
if (y2 >= LINES - offs) y2 = LINES - offs - 1;
#endif
if (x1 > x2) swap(x1, x2);
if (y1 > y2) swap(y1, y2);
w->xs = x2 - x1 + 1;
w->ys = y2 - y1 + 1;
w->x1 = x1;
w->x2 = x2;
w->y1 = w->sy1 = y1;
w->y2 = w->sy2 = y2;
w->doscroll = 1;
w->border = border;
w->cursor = CNORMAL;
w->attr = attr;
w->autocr = 1;
w->wrap = 1;
color = w->color = COLATTR(fg, bg);
w->curx = 0;
w->cury = 0;
w->o_curx = curx;
w->o_cury = cury;
w->o_attr = curattr;
w->o_color = curcolor;
w->o_cursor = _curstype;
w->direct = direct;
if (border != BNONE) {
x1--; x2++;
y1--; y2++;
}
/* Store whatever we are overlapping */
bytes = (y2 - y1 + 1) * (x2 - x1 + 1) * sizeof(ELM) + 100;
if ((e = (ELM *)malloc(bytes)) == (ELM *)0) {
free(w);
return((WIN *)0);
}
w->map = e;
/* How many bytes is one line */
bytes = (x2 - x1 + 1) * sizeof(ELM);
/* Loop */
for(y = y1; y <= y2; y++) {
memcpy(e, gmap + COLS * y + x1, bytes);
e += (x2 - x1 + 1);
}
#if HISTORY
/* Do we want history? */
w->histline = w->histlines = 0;
w->histbuf = (ELM *)0;
if (histlines) {
/* Reserve some memory. */
bytes = w->xs * histlines * sizeof(ELM);
if ((w->histbuf = (ELM *)malloc(bytes)) == NULL) {
free(w->map);
free(w);
return((WIN *)0);
}
w->histlines = histlines;
/* Clear the history buf. */
e = w->histbuf;
for(y = 0; y < w->xs * histlines; y++) {
e->value = ' ';
e->attr = attr;
e->color = color;
e++;
}
}
#endif
/* And draw the window */
if (border) {
_win_write(border == BSINGLE ? S_UL : D_UL, w->direct, x1, y1,
xattr, color);
for(x = x1 + 1; x < x2; x++)
_win_write(border == BSINGLE ? S_HOR : D_HOR, w->direct, x, y1,
xattr, color);
_win_write(border == BSINGLE ? S_UR : D_UR, w->direct, x2, y1,
xattr, color);
for(y = y1 + 1; y < y2; y++) {
_win_write(border == BSINGLE ? S_VER : D_VER, w->direct, x1, y,
xattr, color);
for(x = x1 + 1; x < x2; x++)
_win_write(' ', w->direct, x, y, attr, color);
_win_write(border == BSINGLE ? S_VER : D_VER, w->direct, x2, y,
xattr, color);
}
_win_write(border == BSINGLE ? S_LL : D_LL, w->direct, x1, y2,
xattr, color);
for(x = x1 + 1; x < x2; x++)
_win_write(border == BSINGLE ? S_HOR : D_HOR, w->direct,
x, y2, xattr, color);
_win_write(border == BSINGLE ? S_LR : D_LR, w->direct, x2, y2,
xattr, color);
if (w->direct) _gotoxy(x1 + 1, y1 + 1);
} else
if (doclr) winclr(w);
wcursor(w, CNORMAL);
if (w->direct) wflush();
return(w);
}
/*
* Close a window.
*/
void wclose(win, replace)
WIN *win;
int replace;
{
ELM *e;
int x, y;
#ifdef SMOOTH
curwin = NIL_WIN;
#endif
if (!win) return;
if (win == stdwin) {
win_end();
return;
}
e = win->map;
if (win->border) {
win->x1--; win->x2++;
win->y1--; win->y2++;
}
wcursor(win, win->o_cursor);
if (replace) {
for(y = win->y1; y <= win->y2; y++) {
for(x = win->x1; x <= win->x2; x++) {
_win_write(e->value, 1, x, y, e->attr, e->color);
e++;
}
}
_gotoxy(win->o_curx, win->o_cury);
_setattr(win->o_attr, win->o_color);
}
free(win->map);
free(win);
#if HISTORY
if (win->histbuf) free(win->histbuf);
#endif
wflush();
}
static int oldx, oldy;
static int ocursor;
/*
* Clear screen & restore keyboard modes
*/
void wleave()
{
oldx = curx;
oldy = cury;
ocursor = _curstype;
(void) setcbreak(0); /* Normal */
_gotoxy(0, LINES - 1);
_setattr(XA_NORMAL, COLATTR(WHITE, BLACK));
_cursor(CNORMAL);
if (CL != CNULL)
outstr(CL);
else
outstr("\n");
#if ST_LINE
if (DS) outstr(DS);
#endif
if (KE != CNULL) outstr(KE);
if (RS != CNULL) outstr(RS);
wflush();
}
void wreturn()
{
int x, y;
ELM *e;
#ifdef SMOOTH
curwin = NIL_WIN;
#endif
curattr = -1;
curcolor = -1;
(void) setcbreak(1); /* Cbreak, no echo */
if (IS != CNULL) outstr(IS); /* Initialization string */
if (EA != CNULL) outstr(EA); /* Graphics init. */
if (KS != CNULL) outstr(KS); /* Keypad mode */
_gotoxy(0, 0);
_cursor(ocursor);
e = gmap;
for(y = 0; y <LINES; y++) {
for(x = 0; x < COLS; x++) {
_win_write(e->value, -1, x, y, e->attr, e->color);
e++;
}
}
_gotoxy(oldx, oldy);
wflush();
}
/*
* Redraw the whole window.
*/
void wredraw(w, newdirect)
WIN *w;
int newdirect;
{
int minx, maxx, miny, maxy;
ELM *e;
int x, y;
int addcnt;
minx = w->x1;
maxx = w->x2;
miny = w->y1;
maxy = w->y2;
addcnt = stdwin->xs - w->xs;
if (w->border) {
minx--;
maxx++;
miny--;
maxy++;
addcnt -= 2;
}
_gotoxy(minx, miny);
_cursor(CNONE);
e = gmap + (miny * stdwin->xs) + minx;
for(y = miny; y <= maxy; y++) {
for(x = minx; x <= maxx; x++) {
_win_write(e->value, -1, x, y, e->attr, e->color);
e++;
}
e += addcnt;
}
_gotoxy(w->x1 + w->curx, w->y1 + w->cury);
_cursor(w->cursor);
wflush();
w->direct = newdirect;
}
/*
* Clear to end of line, low level.
*/
static int _wclreol(w)
WIN *w;
{
int x;
int doit = 1;
int y;
#ifdef SMOOTH
curwin = w;
#endif
y = w->cury + w->y1;
if (w->direct && (w->x2 == COLS - 1) && CE) {
_gotoxy(w->curx + w->x1, y);
_setattr(w->attr, w->color);
outstr(CE);
doit = 0;
}
for(x = w->curx + w->x1; x <= w->x2; x++) {
_win_write(' ', (w->direct && doit) ? 1 : 0, x, y, w->attr, w->color);
}
return(doit);
}
/*
* Scroll a window.
*/
void wscroll(win, dir)
WIN *win;
int dir;
{
ELM *e, *f;
char *src, *dst;
int x, y;
int doit = 1;
int ocurx, fs = 0, len;
int phys_scr = 0;
#ifdef SMOOTH
curwin = win;
#endif
/*
* If the window *is* the physical screen, we can scroll very simple.
* This improves performance on slow screens (eg ATARI ST) dramatically.
*/
if (win->direct && SF != CNULL &&
(dir == S_UP || SR != CNULL) && (LINES == win->sy2 - win->sy1 + 1)) {
doit = 0;
phys_scr = 1;
_setattr(win->attr, win->color);
if (dir == S_UP) {
_gotoxy(0, LINES - 1);
outstr(SF);
} else {
_gotoxy(0, 0);
outstr(SR);
}
}
/*
* If the window is as wide as the physical screen, we can
* scroll it with insert/delete line (or set scroll region - vt100!)
*/
else if (win->direct && win->xs == COLS &&
((CS != CNULL && SF != CNULL && SR != CNULL)
|| (Dl != CNULL && Al != CNULL))) {
doit = 0;
phys_scr = 1;
_setattr(win->attr, win->color);
if (CS != CNULL && SF != CNULL && SR != CNULL) { /* Scrolling Region */
/* If the scroll region we want to initialize already is as
* big as the physical screen, we don't _have_ to
* initialize it.
*/
if (win->sy2 == LINES - 1 && win->sy1 == 0) fs = 1;
if (!fs) {
outstr(tgoto(CS, win->sy2, win->sy1));
cury = 0;
}
if (dir == S_UP) {
_gotoxy(0, win->sy2);
outstr(SF);
} else {
_gotoxy(0, win->sy1);
outstr(SR);
}
if (!fs) {
outstr(tgoto(CS, LINES - 1, 0));
cury = 0;
}
_gotoxy(0, win->sy2);
} else { /* Use insert/delete line */
if (dir == S_UP) {
_gotoxy(0, win->sy1);
outstr(Dl);
_gotoxy(0, win->sy2);
outstr(Al);
} else {
_gotoxy(0, win->sy2);
outstr(Dl);
_gotoxy(0, win->sy1);
outstr(Al);
}
}
}
/* If a terminal has automatic margins, we can't write
* to the lower right. After scrolling we have to restore
* the non-visible character that is now visible.
*/
if (sflag && win->sy2 == (LINES - 1) && win->sy1 != win->sy2) {
if (dir == S_UP) {
_win_write(oldc.value, 1, COLS - 1, LINES - 2,
oldc.attr, oldc.color);
}
sflag = 0;
}
ocurx = win->curx;
#if HISTORY
/* If this window has a history buf, see if we want to use it. */
if (win->histbuf && dir == S_UP &&
win->sy2 == win->y2 && win->sy1 == win->y1) {
/* Calculate screen buffer */
e = gmap + win->y1 * COLS + win->x1;
/* Calculate history buffer */
f = win->histbuf + (win->xs * win->histline);
/* Copy line from screen to history buffer */
memcpy((char *)f, (char *)e, win->xs * sizeof(ELM));
/* Postion the next line in the history buffer */
win->histline++;
if (win->histline >= win->histlines) win->histline = 0;
}
#endif
/* If the window is screen-wide and has no border, there
* is a much simpler & FASTER way of scrolling the memory image !!
*/
if (phys_scr) {
len = (win->sy2 - win->sy1) * win->xs * sizeof(ELM);
if (dir == S_UP) {
dst = (char *)&gmap[0]; /* First line */
src = (char *)&gmap[win->xs]; /* Second line */
win->cury = win->sy2 - win->y1;
} else {
src = (char *)&gmap[0]; /* First line */
dst = (char *)&gmap[win->xs]; /* Second line */
win->cury = win->sy1 - win->y1;
}
/* memmove copies len bytes from src to dst, even if the
* objects overlap.
*/
fflush(stdout);
#ifdef _SYSV
memcpy((char *)dst, (char *)src, len);
#else
# ifdef _BSD43
bcopy((char *)src, (char *)dst, len);
# else
memmove((char *)dst, (char *)src, len);
# endif
#endif
} else {
/* Now scroll the memory image. */
if (dir == S_UP) {
for(y = win->sy1 + 1; y <= win->sy2; y++) {
e = gmap + y * COLS + win->x1;
for(x = win->x1; x <= win->x2; x++) {
_win_write(e->value, win->direct && doit,
x, y - 1, e->attr, e->color);
e++;
}
}
win->curx = 0;
win->cury = win->sy2 - win->y1;
if (doit) (void) _wclreol(win);
} else {
for(y = win->sy2 - 1; y >= win->sy1; y--) {
e = gmap + y * COLS + win->x1;
for(x = win->x1; x <= win->x2; x++) {
_win_write(e->value, win->direct && doit,
x, y + 1, e->attr, e->color);
e++;
}
}
win->curx = 0;
win->cury = win->sy1 - win->y1;
if (doit) (void) _wclreol(win);
}
}
win->curx = ocurx;
if (!doit) for(x = win->x1; x <= win->x2; x++)
_win_write(' ', 0, x, win->y1 + win->cury, win->attr, win->color);
if (!_intern && win->direct)
_gotoxy(win->x1 + win->curx, win->y1 + win->cury);
if (dirflush && !_intern && win->direct) wflush();
}
/*
* Locate the cursor in a window.
*/
void wlocate(win, x, y)
WIN *win;
int x, y;
{
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x >= win->xs) x = win->xs - 1;
if (y >= win->ys) y = win->ys - 1;
win->curx = x;
win->cury = y;
if (win->direct) _gotoxy(win->x1 + x, win->y1 + y);
if (dirflush) wflush();
}
/*
* Print a character in a window.
*/
void wputc(win, c)
WIN *win;
int c;
{
int mv = 0;
#ifdef SMOOTH
curwin = win;
#endif
switch(c) {
case '\r':
win->curx = 0;
mv++;
break;
case '\b':
if (win->curx == 0) break;
win->curx--;
mv++;
break;
case '\007':
wbell();
break;
case '\t':
do {
wputc(win, ' '); /* Recursion! */
} while(win->curx % 8);
break;
case '\n':
if (win->autocr) win->curx = 0;
/*FALLTHRU*/
default:
/* See if we need to scroll/move. (vt100 behaviour!) */
if (c == '\n' || (win->curx >= win->xs && win->wrap)) {
if (c != '\n') win->curx = 0;
win->cury++;
mv++;
if (win->cury == win->sy2 - win->y1 + 1) {
if (win->doscroll)
wscroll(win, S_UP);
else
win->cury = win->sy1 - win->y1;