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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
|
/* Copyright (c) 2008, 2009
* Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
* Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
* Micah Cowan (micah@cowan.name)
* Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
* Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
* Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
* Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
* Copyright (c) 1987 Oliver Laumann
*
* 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, 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 (see the file COPYING); if not, see
* http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*
****************************************************************
*/
#include <sys/types.h>
#include <signal.h>
#ifndef sun
#include <sys/ioctl.h>
#endif
#ifdef ISC
# include <sys/tty.h>
# include <sys/sioctl.h>
# include <sys/pty.h>
#endif
#include "config.h"
#include "screen.h"
#include "extern.h"
static void CheckMaxSize __P((int));
static void FreeMline __P((struct mline *));
static int AllocMline __P((struct mline *ml, int));
static void MakeBlankLine __P((unsigned char *, int));
static void kaablamm __P((void));
static int BcopyMline __P((struct mline *, int, struct mline *, int, int, int));
static void SwapAltScreen __P((struct win *));
extern struct layer *flayer;
extern struct display *display, *displays;
extern unsigned char *blank, *null;
extern struct mline mline_blank, mline_null, mline_old;
extern struct win *windows;
extern int Z0width, Z1width;
extern int captionalways;
#if defined(TIOCGWINSZ) || defined(TIOCSWINSZ)
struct winsize glwz;
#endif
static struct mline mline_zero = {
(unsigned char *)0,
(unsigned char *)0
#ifdef FONT
,(unsigned char *)0
#endif
#ifdef COLOR
,(unsigned char *)0
# ifdef COLORS256
,(unsigned char *)0
# endif
#endif
};
/*
* ChangeFlag: 0: try to modify no window
* 1: modify fore (and try to modify no other) + redisplay
* 2: modify all windows
*
* Note: Activate() is only called if change_flag == 1
* i.e. on a WINCH event
*/
void
CheckScreenSize(change_flag)
int change_flag;
{
int wi, he;
if (display == 0)
{
debug("CheckScreenSize: No display -> no check.\n");
return;
}
#ifdef TIOCGWINSZ
if (ioctl(D_userfd, TIOCGWINSZ, (char *)&glwz) != 0)
{
debug2("CheckScreenSize: ioctl(%d, TIOCGWINSZ) errno %d\n", D_userfd, errno);
wi = D_CO;
he = D_LI;
}
else
{
wi = glwz.ws_col;
he = glwz.ws_row;
if (wi == 0)
wi = D_CO;
if (he == 0)
he = D_LI;
}
#else
wi = D_CO;
he = D_LI;
#endif
debug2("CheckScreenSize: screen is (%d,%d)\n", wi, he);
#if 0 /* XXX: Fixme */
if (change_flag == 2)
{
debug("Trying to adapt all windows (-A)\n");
for (p = windows; p; p = p->w_next)
if (p->w_display == 0 || p->w_display == display)
ChangeWindowSize(p, wi, he, p->w_histheight);
}
#endif
if (D_width == wi && D_height == he)
{
debug("CheckScreenSize: No change -> return.\n");
return;
}
#ifdef BLANKER_PRG
KillBlanker();
#endif
ResetIdle();
ChangeScreenSize(wi, he, change_flag);
/* XXX Redisplay logic */
#if 0
if (change_flag == 1)
Redisplay(D_fore ? D_fore->w_norefresh : 0);
#endif
}
void
ChangeScreenSize(wi, he, change_fore)
int wi, he;
int change_fore;
{
struct win *p;
struct canvas *cv;
int wwi;
debug2("ChangeScreenSize from (%d,%d) ", D_width, D_height);
debug3("to (%d,%d) (change_fore: %d)\n",wi, he, change_fore);
cv = &D_canvas;
cv->c_xe = wi - 1;
cv->c_ye = he - 1 - ((cv->c_slperp && cv->c_slperp->c_slnext) || captionalways) - (D_has_hstatus == HSTATUS_LASTLINE);
cv->c_blank.l_height = cv->c_ye - cv->c_ys + 1;
if (cv->c_slperp)
{
ResizeCanvas(cv);
RecreateCanvasChain();
RethinkDisplayViewports();
}
if (D_forecv == 0)
D_forecv = D_cvlist;
if (D_forecv)
D_fore = Layer2Window(D_forecv->c_layer);
D_width = wi;
D_height = he;
CheckMaxSize(wi);
if (D_CWS)
{
D_defwidth = D_CO;
D_defheight = D_LI;
}
else
{
if (D_CZ0 && (wi == Z0width || wi == Z1width) &&
(D_CO == Z0width || D_CO == Z1width))
D_defwidth = D_CO;
else
D_defwidth = wi;
D_defheight = he;
}
debug2("Default size: (%d,%d)\n", D_defwidth, D_defheight);
if (change_fore)
ResizeLayersToCanvases();
if (change_fore == 2 && D_CWS == NULL && displays->d_next == 0)
{
/* adapt all windows - to be removed ? */
for (p = windows; p; p = p->w_next)
{
debug1("Trying to change window %d.\n", p->w_number);
wwi = wi;
#if 0
if (D_CZ0 && p->w_width != wi && (wi == Z0width || wi == Z1width))
{
if (p->w_width > (Z0width + Z1width) / 2)
wwi = Z0width;
else
wwi = Z1width;
}
#endif
if (p->w_savelayer && p->w_savelayer->l_cvlist == 0)
ResizeLayer(p->w_savelayer, wwi, he, 0);
#if 0
ChangeWindowSize(p, wwi, he, p->w_histheight);
#endif
}
}
}
void
ResizeLayersToCanvases()
{
struct canvas *cv;
struct layer *l;
int lx, ly;
debug("ResizeLayersToCanvases\n");
D_kaablamm = 0;
for (cv = D_cvlist; cv; cv = cv->c_next)
{
l = cv->c_layer;
if (l == 0)
continue;
debug("Doing canvas: ");
if (l->l_width == cv->c_xe - cv->c_xs + 1 &&
l->l_height == cv->c_ye - cv->c_ys + 1)
{
debug("already fitting.\n");
continue;
}
if (!MayResizeLayer(l))
{
debug("may not resize.\n");
}
else
{
debug("doing resize.\n");
ResizeLayer(l, cv->c_xe - cv->c_xs + 1, cv->c_ye - cv->c_ys + 1, display);
}
/* normalize window, see screen.c */
lx = cv->c_layer->l_x;
ly = cv->c_layer->l_y;
if (ly + cv->c_yoff < cv->c_ys)
{
cv->c_yoff = cv->c_ys - ly;
RethinkViewportOffsets(cv);
}
else if (ly + cv->c_yoff > cv->c_ye)
{
cv->c_yoff = cv->c_ye - ly;
RethinkViewportOffsets(cv);
}
if (lx + cv->c_xoff < cv->c_xs)
{
int n = cv->c_xs - (lx + cv->c_xoff);
if (n < (cv->c_xe - cv->c_xs + 1) / 2)
n = (cv->c_xe - cv->c_xs + 1) / 2;
if (cv->c_xoff + n > cv->c_xs)
n = cv->c_xs - cv->c_xoff;
cv->c_xoff += n;
RethinkViewportOffsets(cv);
}
else if (lx + cv->c_xoff > cv->c_xe)
{
int n = lx + cv->c_xoff - cv->c_xe;
if (n < (cv->c_xe - cv->c_xs + 1) / 2)
n = (cv->c_xe - cv->c_xs + 1) / 2;
if (cv->c_xoff - n + cv->c_layer->l_width - 1 < cv->c_xe)
n = cv->c_xoff + cv->c_layer->l_width - 1 - cv->c_xe;
cv->c_xoff -= n;
RethinkViewportOffsets(cv);
}
}
Redisplay(0);
if (D_kaablamm)
{
kaablamm();
D_kaablamm = 0;
}
}
int
MayResizeLayer(l)
struct layer *l;
{
int cvs = 0;
debug("MayResizeLayer:\n");
for (; l; l = l->l_next)
{
if (l->l_cvlist)
if (++cvs > 1 || l->l_cvlist->c_lnext)
{
debug1("may not - cvs %d\n", cvs);
return 0;
}
}
debug("may resize\n");
return 1;
}
/*
* Easy implementation: rely on the fact that the only layers
* supporting resize are Win and Blank. So just kill all overlays.
*
* This is a lot harder if done the right way...
*/
static void
kaablamm()
{
Msg(0, "Aborted because of window size change.");
}
/* Kills non-resizable layers. */
#define RESIZE_OR_KILL_LAYERS(l, wi, he) do \
{ \
struct layer *_last = NULL; \
flayer = (l); \
while (flayer->l_next) \
{ \
if (LayResize(wi, he) == 0) \
{ \
_last = flayer; \
flayer = flayer->l_next; \
} \
else \
{ \
struct canvas *_cv; \
for (_cv = flayer->l_cvlist; _cv; _cv = _cv->c_lnext) \
_cv->c_display->d_kaablamm = 1; \
ExitOverlayPage(); \
if (_last) \
_last->l_next = flayer; \
} \
} \
/* We assume that the bottom-most layer, i.e. when flayer->l_next == 0, \
* is always resizable. Currently, WinLf and BlankLf can be the bottom-most layers. \
*/ \
LayResize(wi, he); \
} while (0)
void
ResizeLayer(l, wi, he, norefdisp)
struct layer *l;
int wi, he;
struct display *norefdisp;
{
struct win *p;
struct canvas *cv;
struct layer *oldflayer = flayer;
struct display *d, *olddisplay = display;
if (l->l_width == wi && l->l_height == he)
return;
p = Layer2Window(l);
/* If 'flayer' and 'l' are for the same window, then we will not
* restore 'flayer'. */
if (oldflayer && (l == oldflayer || Layer2Window(oldflayer) == p))
oldflayer = NULL;
flayer = l;
if (p)
{
/* It's a window layer. Kill the overlays on it in all displays. */
for (d = displays; d; d = d->d_next)
for (cv = d->d_cvlist; cv; cv = cv->c_next)
{
if (p == Layer2Window(cv->c_layer))
{
/* Canvas 'cv' on display 'd' shows this window. Remove any non-resizable
* layers over it. */
RESIZE_OR_KILL_LAYERS(cv->c_layer, wi, he);
}
}
}
else
{
/* It's a Blank layer. Just kill the non-resizable overlays over it. */
RESIZE_OR_KILL_LAYERS(flayer, wi, he);
}
for (display = displays; display; display = display->d_next)
{
if (display == norefdisp)
continue;
for (cv = D_cvlist; cv; cv = cv->c_next)
if (Layer2Window(cv->c_layer) == p)
{
CV_CALL(cv, LayRedisplayLine(-1, -1, -1, 0));
RefreshArea(cv->c_xs, cv->c_ys, cv->c_xe, cv->c_ye, 0);
}
if (D_kaablamm)
{
kaablamm();
D_kaablamm = 0;
}
}
/* If we started resizing a non-flayer layer, then restore the flayer.
* Otherwise, flayer should already be updated to the topmost foreground layer. */
if (oldflayer)
flayer = oldflayer;
display = olddisplay;
}
static void
FreeMline(ml)
struct mline *ml;
{
if (ml->image)
free(ml->image);
if (ml->attr && ml->attr != null)
free(ml->attr);
#ifdef FONT
if (ml->font && ml->font != null)
free(ml->font);
#endif
#ifdef COLOR
if (ml->color && ml->color != null)
free(ml->color);
# ifdef COLORS256
if (ml->colorx && ml->colorx != null)
free(ml->colorx);
# endif
#endif
*ml = mline_zero;
}
static int
AllocMline(ml, w)
struct mline *ml;
int w;
{
ml->image = malloc(w);
ml->attr = null;
#ifdef FONT
ml->font = null;
#endif
#ifdef COLOR
ml->color = null;
# ifdef COLORS256
ml->colorx = null;
# endif
#endif
if (ml->image == 0)
return -1;
return 0;
}
static int
BcopyMline(mlf, xf, mlt, xt, l, w)
struct mline *mlf, *mlt;
int xf, xt, l, w;
{
int r = 0;
bcopy((char *)mlf->image + xf, (char *)mlt->image + xt, l);
if (mlf->attr != null && mlt->attr == null)
{
if ((mlt->attr = (unsigned char *)calloc(w, 1)) == 0)
mlt->attr = null, r = -1;
}
if (mlt->attr != null)
bcopy((char *)mlf->attr + xf, (char *)mlt->attr + xt, l);
#ifdef FONT
if (mlf->font != null && mlt->font == null)
{
if ((mlt->font = (unsigned char *)calloc(w, 1)) == 0)
mlt->font = null, r = -1;
}
if (mlt->font != null)
bcopy((char *)mlf->font + xf, (char *)mlt->font + xt, l);
#endif
#ifdef COLOR
if (mlf->color != null && mlt->color == null)
{
if ((mlt->color = (unsigned char *)calloc(w, 1)) == 0)
mlt->color = null, r = -1;
}
if (mlt->color != null)
bcopy((char *)mlf->color + xf, (char *)mlt->color + xt, l);
# ifdef COLORS256
if (mlf->colorx != null && mlt->colorx == null)
{
if ((mlt->colorx = (unsigned char *)calloc(w, 1)) == 0)
mlt->colorx = null, r = -1;
}
if (mlt->colorx != null)
bcopy((char *)mlf->colorx + xf, (char *)mlt->colorx + xt, l);
# endif
#endif
return r;
}
static int maxwidth;
static void
CheckMaxSize(wi)
int wi;
{
unsigned char *oldnull = null;
unsigned char *oldblank = blank;
struct win *p;
int i;
struct mline *ml;
wi = ((wi + 1) + 255) & ~255;
if (wi <= maxwidth)
return;
maxwidth = wi;
debug1("New maxwidth: %d\n", maxwidth);
blank = (unsigned char *)xrealloc((char *)blank, maxwidth);
null = (unsigned char *)xrealloc((char *)null, maxwidth);
mline_old.image = (unsigned char *)xrealloc((char *)mline_old.image, maxwidth);
mline_old.attr = (unsigned char *)xrealloc((char *)mline_old.attr, maxwidth);
#ifdef FONT
mline_old.font = (unsigned char *)xrealloc((char *)mline_old.font, maxwidth);
#endif
#ifdef COLOR
mline_old.color = (unsigned char *)xrealloc((char *)mline_old.color, maxwidth);
# ifdef COLORS256
mline_old.colorx = (unsigned char *)xrealloc((char *)mline_old.colorx, maxwidth);
# endif
#endif
if (!(blank && null && mline_old.image && mline_old.attr IFFONT(&& mline_old.font) IFCOLOR(&& mline_old.color) IFCOLORX(&& mline_old.colorx)))
Panic(0, "%s", strnomem);
MakeBlankLine(blank, maxwidth);
bzero((char *)null, maxwidth);
mline_blank.image = blank;
mline_blank.attr = null;
mline_null.image = null;
mline_null.attr = null;
#ifdef FONT
mline_blank.font = null;
mline_null.font = null;
#endif
#ifdef COLOR
mline_blank.color = null;
mline_null.color = null;
# ifdef COLORS256
mline_blank.colorx = null;
mline_null.colorx = null;
# endif
#endif
#define RESET_AFC(x, bl) do { if (x == old##bl) x = bl; } while (0)
#define RESET_LINES(lines, count) \
do { \
ml = lines; \
for (i = 0; i < count; i++, ml++) \
{ \
RESET_AFC(ml->image, blank); \
RESET_AFC(ml->attr, null); \
IFFONT(RESET_AFC(ml->font, null)); \
IFCOLOR(RESET_AFC(ml->color, null)); \
IFCOLORX(RESET_AFC(ml->colorx, null)); \
} \
} while (0)
/* We have to run through all windows to substitute
* the null and blank references.
*/
for (p = windows; p; p = p->w_next)
{
RESET_LINES(p->w_mlines, p->w_height);
#ifdef COPY_PASTE
RESET_LINES(p->w_hlines, p->w_histheight);
RESET_LINES(p->w_alt.hlines, p->w_alt.histheight);
#endif
RESET_LINES(p->w_alt.mlines, p->w_alt.height);
}
}
char *
xrealloc(mem, len)
char *mem;
int len;
{
register char *nmem;
if (mem == 0)
return malloc(len);
if ((nmem = realloc(mem, len)))
return nmem;
free(mem);
return (char *)0;
}
static void
MakeBlankLine(p, n)
register unsigned char *p;
register int n;
{
while (n--)
*p++ = ' ';
}
#ifdef COPY_PASTE
#define OLDWIN(y) ((y < p->w_histheight) \
? &p->w_hlines[(p->w_histidx + y) % p->w_histheight] \
: &p->w_mlines[y - p->w_histheight])
#define NEWWIN(y) ((y < hi) ? &nhlines[y] : &nmlines[y - hi])
#else
#define OLDWIN(y) (&p->w_mlines[y])
#define NEWWIN(y) (&nmlines[y])
#endif
int
ChangeWindowSize(p, wi, he, hi)
struct win *p;
int wi, he, hi;
{
struct mline *mlf = 0, *mlt = 0, *ml, *nmlines, *nhlines;
int fy, ty, l, lx, lf, lt, yy, oty, addone;
int ncx, ncy, naka, t;
int y, shift;
if (wi <= 0 || he <= 0)
wi = he = hi = 0;
if (p->w_type == W_TYPE_GROUP)
return 0;
if (wi > 1000)
{
Msg(0, "Window width too large. Truncated to 1000.");
wi = 1000;
}
if (he > 1000)
{
Msg(0, "Window height too large. Truncated to 1000.");
he = 1000;
}
if (p->w_width == wi && p->w_height == he && p->w_histheight == hi)
{
debug("ChangeWindowSize: No change.\n");
return 0;
}
CheckMaxSize(wi);
/* XXX */
#if 0
/* just in case ... */
if (wi && (p->w_width != wi || p->w_height != he) && p->w_lay != &p->w_winlay)
{
debug("ChangeWindowSize: No resize because of overlay?\n");
return -1;
}
#endif
debug("ChangeWindowSize");
debug3(" from (%d,%d)+%d", p->w_width, p->w_height, p->w_histheight);
debug3(" to(%d,%d)+%d\n", wi, he, hi);
fy = p->w_histheight + p->w_height - 1;
ty = hi + he - 1;
nmlines = nhlines = 0;
ncx = 0;
ncy = 0;
naka = 0;
if (wi)
{
if (wi != p->w_width || he != p->w_height)
{
if ((nmlines = (struct mline *)calloc(he, sizeof(struct mline))) == 0)
{
KillWindow(p);
Msg(0, "%s", strnomem);
return -1;
}
}
else
{
debug1("image stays the same: %d lines\n", he);
nmlines = p->w_mlines;
fy -= he;
ty -= he;
ncx = p->w_x;
ncy = p->w_y;
naka = p->w_autoaka;
}
}
#ifdef COPY_PASTE
if (hi)
{
if ((nhlines = (struct mline *)calloc(hi, sizeof(struct mline))) == 0)
{
Msg(0, "No memory for history buffer - turned off");
hi = 0;
ty = he - 1;
}
}
#endif
/* special case: cursor is at magic margin position */
addone = 0;
if (p->w_width && p->w_x == p->w_width)
{
debug2("Special addone case: %d %d\n", p->w_x, p->w_y);
addone = 1;
p->w_x--;
}
/* handle the cursor and autoaka lines now if the widths are equal */
if (p->w_width == wi)
{
ncx = p->w_x + addone;
ncy = p->w_y + he - p->w_height;
/* never lose sight of the line with the cursor on it */
shift = -ncy;
for (yy = p->w_y + p->w_histheight - 1; yy >= 0 && ncy + shift < he; yy--)
{
ml = OLDWIN(yy);
if (!ml->image)
break;
if (ml->image[p->w_width] == ' ')
break;
shift++;
}
if (shift < 0)
shift = 0;
else
debug1("resize: cursor out of bounds, shifting %d\n", shift);
ncy += shift;
if (p->w_autoaka > 0)
{
naka = p->w_autoaka + he - p->w_height + shift;
if (naka < 1 || naka > he)
naka = 0;
}
while (shift-- > 0)
{
ml = OLDWIN(fy);
FreeMline(ml);
fy--;
}
}
debug2("fy %d ty %d\n", fy, ty);
if (fy >= 0)
mlf = OLDWIN(fy);
if (ty >= 0)
mlt = NEWWIN(ty);
while (fy >= 0 && ty >= 0)
{
if (p->w_width == wi)
{
/* here is a simple shortcut: just copy over */
*mlt = *mlf;
*mlf = mline_zero;
if (--fy >= 0)
mlf = OLDWIN(fy);
if (--ty >= 0)
mlt = NEWWIN(ty);
continue;
}
/* calculate lenght */
for (l = p->w_width - 1; l > 0; l--)
if (mlf->image[l] != ' ' || mlf->attr[l])
break;
if (fy == p->w_y + p->w_histheight && l < p->w_x)
l = p->w_x; /* cursor is non blank */
l++;
lf = l;
/* add wrapped lines to length */
for (yy = fy - 1; yy >= 0; yy--)
{
ml = OLDWIN(yy);
if (ml->image[p->w_width] == ' ')
break;
l += p->w_width;
}
/* rewrap lines */
lt = (l - 1) % wi + 1; /* lf is set above */
oty = ty;
while (l > 0 && fy >= 0 && ty >= 0)
{
lx = lt > lf ? lf : lt;
if (mlt->image == 0)
{
if (AllocMline(mlt, wi + 1))
goto nomem;
MakeBlankLine(mlt->image + lt, wi - lt);
mlt->image[wi] = ((oty == ty) ? ' ' : 0);
}
if (BcopyMline(mlf, lf - lx, mlt, lt - lx, lx, wi + 1))
goto nomem;
/* did we copy the cursor ? */
if (fy == p->w_y + p->w_histheight && lf - lx <= p->w_x && lf > p->w_x)
{
ncx = p->w_x + lt - lf + addone;
ncy = ty - hi;
shift = wi ? -ncy + (l - lx) / wi : 0;
if (ty + shift > hi + he - 1)
shift = hi + he - 1 - ty;
if (shift > 0)
{
debug3("resize: cursor out of bounds, shifting %d [%d/%d]\n", shift, lt - lx, wi);
for (y = hi + he - 1; y >= ty; y--)
{
mlt = NEWWIN(y);
FreeMline(mlt);
if (y - shift < ty)
continue;
ml = NEWWIN(y - shift);
*mlt = *ml;
*ml = mline_zero;
}
ncy += shift;
ty += shift;
mlt = NEWWIN(ty);
if (naka > 0)
naka = naka + shift > he ? 0 : naka + shift;
}
ASSERT(ncy >= 0);
}
/* did we copy autoaka line ? */
if (p->w_autoaka > 0 && fy == p->w_autoaka - 1 + p->w_histheight && lf - lx <= 0)
naka = ty - hi >= 0 ? 1 + ty - hi : 0;
lf -= lx;
lt -= lx;
l -= lx;
if (lf == 0)
{
FreeMline(mlf);
lf = p->w_width;
if (--fy >= 0)
mlf = OLDWIN(fy);
}
if (lt == 0)
{
lt = wi;
if (--ty >= 0)
mlt = NEWWIN(ty);
}
}
ASSERT(l != 0 || fy == yy);
}
while (fy >= 0)
{
FreeMline(mlf);
if (--fy >= 0)
mlf = OLDWIN(fy);
}
while (ty >= 0)
{
if (AllocMline(mlt, wi + 1))
goto nomem;
MakeBlankLine(mlt->image, wi + 1);
if (--ty >= 0)
mlt = NEWWIN(ty);
}
#ifdef DEBUG
if (nmlines != p->w_mlines)
for (fy = 0; fy < p->w_height + p->w_histheight; fy++)
{
ml = OLDWIN(fy);
ASSERT(ml->image == 0);
}
#endif
if (p->w_mlines && p->w_mlines != nmlines)
free((char *)p->w_mlines);
p->w_mlines = nmlines;
#ifdef COPY_PASTE
if (p->w_hlines && p->w_hlines != nhlines)
free((char *)p->w_hlines);
p->w_hlines = nhlines;
#endif
nmlines = nhlines = 0;
/* change tabs */
if (p->w_width != wi)
{
if (wi)
{
t = p->w_tabs ? p->w_width : 0;
p->w_tabs = xrealloc(p->w_tabs, wi + 1);
if (p->w_tabs == 0)
{
nomem:
if (nmlines)
{
for (ty = he + hi - 1; ty >= 0; ty--)
{
mlt = NEWWIN(ty);
FreeMline(mlt);
}
if (nmlines && p->w_mlines != nmlines)
free((char *)nmlines);
#ifdef COPY_PASTE
if (nhlines && p->w_hlines != nhlines)
free((char *)nhlines);
#endif
}
KillWindow(p);
Msg(0, "%s", strnomem);
return -1;
}
for (; t < wi; t++)
p->w_tabs[t] = t && !(t & 7) ? 1 : 0;
p->w_tabs[wi] = 0;
}
else
{
if (p->w_tabs)
free(p->w_tabs);
p->w_tabs = 0;
}
}
/* Change w_saved.y - this is only an estimate... */
p->w_saved.y += ncy - p->w_y;
p->w_x = ncx;
p->w_y = ncy;
if (p->w_autoaka > 0)
p->w_autoaka = naka;
/* do sanity checks */
if (p->w_x > wi)
p->w_x = wi;
if (p->w_y >= he)
p->w_y = he - 1;
if (p->w_saved.x > wi)
p->w_saved.x = wi;
if (p->w_saved.y < 0)
p->w_saved.y = 0;
if (p->w_saved.y >= he)
p->w_saved.y = he - 1;
if (p->w_alt.cursor.x > wi)
p->w_alt.cursor.x = wi;
if (p->w_alt.cursor.y >= he)
p->w_alt.cursor.y = he - 1;
/* reset scrolling region */
p->w_top = 0;
p->w_bot = he - 1;
/* signal new size to window */
#ifdef TIOCSWINSZ
if (wi && (p->w_width != wi || p->w_height != he)
&& p->w_width != 0 && p->w_height != 0 && p->w_ptyfd >= 0 && p->w_pid)
{
glwz.ws_col = wi;
glwz.ws_row = he;
debug("Setting pty winsize.\n");
if (ioctl(p->w_ptyfd, TIOCSWINSZ, (char *)&glwz))
debug2("SetPtySize: errno %d (fd:%d)\n", errno, p->w_ptyfd);
}
#endif /* TIOCSWINSZ */
/* store new size */
p->w_width = wi;
p->w_height = he;
#ifdef COPY_PASTE
p->w_histidx = 0;
p->w_histheight = hi;
#endif
#ifdef BUILTIN_TELNET
if (p->w_type == W_TYPE_TELNET)
TelWindowSize(p);
#endif
#ifdef DEBUG
/* Test if everything was ok */
for (fy = 0; fy < p->w_height + p->w_histheight; fy++)
{
ml = OLDWIN(fy);
ASSERT(ml->image);
# ifdef UTF8
if (p->w_encoding == UTF8)
{
for (l = 0; l < p->w_width; l++)
ASSERT(ml->image[l] >= ' ' || ml->font[l]);
}
else
#endif
for (l = 0; l < p->w_width; l++)
ASSERT(ml->image[l] >= ' ');
}
#endif
return 0;
}
void
FreeAltScreen(p)
struct win *p;
{
int i;
if (p->w_alt.mlines)
{
for (i = 0; i < p->w_alt.height; i++)
FreeMline(p->w_alt.mlines + i);
free(p->w_alt.mlines);
}
p->w_alt.mlines = 0;
p->w_alt.width = 0;
p->w_alt.height = 0;
#ifdef COPY_PASTE
if (p->w_alt.hlines)
{
for (i = 0; i < p->w_alt.histheight; i++)
FreeMline(p->w_alt.hlines + i);
free(p->w_alt.hlines);
}
p->w_alt.hlines = 0;
p->w_alt.histidx = 0;
p->w_alt.histheight = 0;
#endif
}
static void
SwapAltScreen(p)
struct win *p;
{
struct mline *ml;
int t;
#define SWAP(item, t) do { (t) = p->w_alt. item; p->w_alt. item = p->w_##item; p->w_##item = (t); } while (0)
SWAP(mlines, ml);
SWAP(width, t);
SWAP(height, t);
#ifdef COPY_PASTE
SWAP(histheight, t);
SWAP(hlines, ml);
SWAP(histidx, t);
#endif
#undef SWAP
}
void
EnterAltScreen(p)
struct win *p;
{
if (!p->w_alt.on)
{
/* If not already using the alternate screen buffer, then create
a new one and swap it with the 'real' screen buffer. */
FreeAltScreen(p);
SwapAltScreen(p);
}
else
{
/* Already using the alternate buffer. Just clear the screen. To do so, it
is only necessary to reset the height(s) without resetting the width. */
p->w_height = 0;
p->w_histheight = 0;
}
ChangeWindowSize(p, p->w_alt.width, p->w_alt.height, p->w_alt.histheight);
p->w_alt.on = 1;
}
void
LeaveAltScreen(p)
struct win *p;
{
if (!p->w_alt.on)
return;
SwapAltScreen(p);
ChangeWindowSize(p, p->w_alt.width, p->w_alt.height, p->w_alt.histheight);
FreeAltScreen(p);
p->w_alt.on = 0;
}
|