This repository has been archived by the owner on Jul 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
screen.c
2916 lines (2549 loc) · 73.6 KB
/
screen.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
/* $XTermId: screen.c,v 1.475 2013/02/13 00:42:30 tom Exp $ */
/*
* Copyright 1999-2012,2013 by Thomas E. Dickey
*
* All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name(s) of the above copyright
* holders shall not be used in advertising or otherwise to promote the
* sale, use or other dealings in this Software without prior written
* authorization.
*
*
* Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
*
* All Rights Reserved
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation, and that the name of Digital Equipment
* Corporation not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior permission.
*
*
* DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
* DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
* ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/* screen.c */
#include <stdio.h>
#include <xterm.h>
#include <error.h>
#include <data.h>
#include <xcharmouse.h>
#include <xterm_io.h>
#include <X11/Xatom.h>
#if OPT_WIDE_CHARS
#include <fontutils.h>
#endif
#include <menu.h>
#include <assert.h>
#include <signal.h>
#ifndef _Xconst
#define _Xconst const /* Solaris 7 workaround */
#endif /* _Xconst */
#define inSaveBuf(screen, buf, inx) \
((buf) == (screen)->saveBuf_index && \
((inx) < (screen)->savelines || (screen)->savelines == 0))
#define getMinRow(screen) ((xw->flags & ORIGIN) ? (screen)->top_marg : 0)
#define getMaxRow(screen) ((xw->flags & ORIGIN) ? (screen)->bot_marg : (screen)->max_row)
#define getMinCol(screen) ((xw->flags & ORIGIN) ? (screen)->lft_marg : 0)
#define getMaxCol(screen) ((xw->flags & ORIGIN) ? (screen)->rgt_marg : (screen)->max_col)
#define MoveLineData(base, dst, src, len) \
memmove(scrnHeadAddr(screen, base, (unsigned) (dst)), \
scrnHeadAddr(screen, base, (unsigned) (src)), \
(size_t) scrnHeadSize(screen, (unsigned) (len)))
#define SaveLineData(base, src, len) \
(void) ScrnPointers(screen, len); \
memcpy (screen->save_ptr, \
scrnHeadAddr(screen, base, src), \
(size_t) scrnHeadSize(screen, (unsigned) (len)))
#define RestoreLineData(base, dst, len) \
memcpy (scrnHeadAddr(screen, base, dst), \
screen->save_ptr, \
(size_t) scrnHeadSize(screen, (unsigned) (len)))
#if OPT_SAVE_LINES
#define VisBuf(screen) screen->editBuf_index[screen->whichBuf]
#else
#define VisBuf(screen) scrnHeadAddr(screen, screen->saveBuf_index, (unsigned) savelines)
#endif
/*
* ScrnPtr's can point to different types of data.
*/
#define SizeofScrnPtr(name) \
(unsigned) sizeof(*((LineData *)0)->name)
/*
* The pointers in LineData point into a block of text allocated as a single
* chunk for the given number of rows. Ensure that these pointers are aligned
* at least to int-boundaries.
*/
#define AlignMask() (sizeof(int) - 1)
#define IsAligned(value) (((unsigned long) (value) & AlignMask()) == 0)
#define AlignValue(value) \
if (!IsAligned(value)) \
value = (value | (unsigned) AlignMask()) + 1
#define SetupScrnPtr(dst,src,type) \
dst = (type *) (void *) src; \
assert(IsAligned(dst)); \
src += skipNcol##type
#define ScrnBufAddr(ptrs, offset) (ScrnBuf) ((void *) ((char *) (ptrs) + (offset)))
#define LineDataAddr(ptrs, offset) (LineData *) ((void *) ((char *) (ptrs) + (offset)))
#if OPT_TRACE > 1
static void
traceScrnBuf(const char *tag, TScreen * screen, ScrnBuf sb, unsigned len)
{
unsigned j;
TRACE(("traceScrnBuf %s\n", tag));
for (j = 0; j < len; ++j) {
LineData *src = (LineData *) scrnHeadAddr(screen, sb, j);
TRACE(("%p %s%3d:%s\n",
src, ((int) j >= screen->savelines) ? "*" : " ",
j, visibleIChars(src->charData, src->lineSize)));
}
TRACE(("...traceScrnBuf %s\n", tag));
}
#define TRACE_SCRNBUF(tag, screen, sb, len) traceScrnBuf(tag, screen, sb, len)
#else
#define TRACE_SCRNBUF(tag, screen, sb, len) /*nothing */
#endif
static unsigned
scrnHeadSize(TScreen * screen, unsigned count)
{
unsigned result = SizeOfLineData;
(void) screen;
#if OPT_WIDE_CHARS
if (screen->wide_chars) {
result += (unsigned) screen->lineExtra;
}
#endif
result *= count;
return result;
}
ScrnBuf
scrnHeadAddr(TScreen * screen, ScrnBuf base, unsigned offset)
{
unsigned size = scrnHeadSize(screen, offset);
ScrnBuf result = ScrnBufAddr(base, size);
assert((int) offset >= 0);
return result;
}
/*
* Given a block of data, build index to it in the 'base' parameter.
*/
void
setupLineData(TScreen * screen, ScrnBuf base, Char * data, unsigned nrow, unsigned ncol)
{
unsigned i;
unsigned offset = 0;
unsigned jump = scrnHeadSize(screen, 1);
LineData *ptr;
#if OPT_WIDE_CHARS
unsigned j;
#endif
/* these names are based on types */
unsigned skipNcolChar;
unsigned skipNcolCharData;
#if OPT_ISO_COLORS
unsigned skipNcolCellColor;
#endif
AlignValue(ncol);
skipNcolChar = (ncol * SizeofScrnPtr(attribs));
skipNcolCharData = (ncol * SizeofScrnPtr(charData));
#if OPT_ISO_COLORS
skipNcolCellColor = (ncol * SizeofScrnPtr(color));
#endif
for (i = 0; i < nrow; i++, offset += jump) {
ptr = LineDataAddr(base, offset);
ptr->lineSize = (Dimension) ncol;
ptr->bufHead = 0;
#if OPT_DEC_CHRSET
SetLineDblCS(ptr, 0);
#endif
SetupScrnPtr(ptr->attribs, data, Char);
#if OPT_ISO_COLORS
SetupScrnPtr(ptr->color, data, CellColor);
#endif
SetupScrnPtr(ptr->charData, data, CharData);
#if OPT_WIDE_CHARS
if (screen->wide_chars) {
unsigned extra = (unsigned) screen->max_combining;
ptr->combSize = (Char) extra;
for (j = 0; j < extra; ++j) {
SetupScrnPtr(ptr->combData[j], data, CharData);
}
}
#endif
}
}
#define ExtractScrnData(name) \
memcpy(dstPtrs->name, \
((LineData *) srcPtrs)->name,\
dstCols * sizeof(dstPtrs->name[0])); \
nextPtr += (srcCols * sizeof(dstPtrs->name[0]))
/*
* As part of reallocating the screen buffer when resizing, extract from
* the old copy of the screen buffer the data which will be used in the
* new copy of the screen buffer.
*/
static void
extractScrnData(TScreen * screen,
ScrnBuf dstPtrs,
ScrnBuf srcPtrs,
unsigned nrows,
unsigned move_down)
{
unsigned j;
TRACE(("extractScrnData(nrows %d)\n", nrows));
TRACE_SCRNBUF("extract from", screen, srcPtrs, nrows);
for (j = 0; j < nrows; j++) {
LineData *dst = (LineData *) scrnHeadAddr(screen,
dstPtrs, j + move_down);
LineData *src = (LineData *) scrnHeadAddr(screen,
srcPtrs, j);
copyLineData(dst, src);
}
}
static ScrnPtr *
allocScrnHead(TScreen * screen, unsigned nrow)
{
ScrnPtr *result;
unsigned size = scrnHeadSize(screen, 1);
result = (ScrnPtr *) calloc((size_t) nrow, (size_t) size);
if (result == 0)
SysError(ERROR_SCALLOC);
TRACE(("allocScrnHead %d -> %d -> %p..%p\n", nrow, nrow * size,
(void *) result,
(char *) result + (nrow * size) - 1));
return result;
}
/*
* Return the size of a line's data.
*/
static unsigned
sizeofScrnRow(TScreen * screen, unsigned ncol)
{
unsigned result;
unsigned sizeAttribs;
#if OPT_ISO_COLORS
unsigned sizeColors;
#endif
(void) screen;
result = (ncol * (unsigned) sizeof(CharData));
AlignValue(result);
#if OPT_WIDE_CHARS
if (screen->wide_chars) {
result *= (unsigned) (1 + screen->max_combining);
}
#endif
sizeAttribs = (ncol * SizeofScrnPtr(attribs));
AlignValue(sizeAttribs);
result += sizeAttribs;
#if OPT_ISO_COLORS
sizeColors = (ncol * SizeofScrnPtr(color));
AlignValue(sizeColors);
result += sizeColors;
#endif
return result;
}
Char *
allocScrnData(TScreen * screen, unsigned nrow, unsigned ncol)
{
Char *result;
size_t length;
AlignValue(ncol);
length = ((nrow + 1) * sizeofScrnRow(screen, ncol));
if (length == 0
|| (result = (Char *) calloc(length, sizeof(Char))) == 0)
SysError(ERROR_SCALLOC2);
TRACE(("allocScrnData %ux%u -> %lu -> %p..%p\n",
nrow, ncol, (unsigned long) length, result, result + length - 1));
return result;
}
/*
* Allocates memory for a 2-dimensional array of chars and returns a pointer
* thereto. Each line is formed from a set of char arrays, with an index
* (i.e., the ScrnBuf type). The first pointer in the index is reserved for
* per-line flags, and does not point to data.
*
* After the per-line flags, we have a series of pointers to char arrays: The
* first one is the actual character array, the second one is the attributes,
* the third is the foreground and background colors, and the fourth denotes
* the character set.
*
* We store it all as pointers, because of alignment considerations.
*/
ScrnBuf
allocScrnBuf(XtermWidget xw, unsigned nrow, unsigned ncol, Char ** addr)
{
TScreen *screen = TScreenOf(xw);
ScrnBuf base = 0;
if (nrow != 0) {
base = allocScrnHead(screen, nrow);
*addr = allocScrnData(screen, nrow, ncol);
setupLineData(screen, base, *addr, nrow, ncol);
}
TRACE(("allocScrnBuf %dx%d ->%p\n", nrow, ncol, (void *) base));
return (base);
}
#if OPT_SAVE_LINES
/*
* Copy line-data from the visible (edit) buffer to the save-lines buffer.
*/
static void
saveEditBufLines(TScreen * screen, ScrnBuf sb, unsigned n)
{
unsigned j;
TRACE(("...copying %d lines from editBuf to saveBuf\n", n));
#if OPT_FIFO_LINES
(void) sb;
#endif
for (j = 0; j < n; ++j) {
#if OPT_FIFO_LINES
LineData *dst = addScrollback(screen);
#else
unsigned k = (screen->savelines + j - n);
LineData *dst = (LineData *) scrnHeadAddr(screen, sb, k);
#endif
LineData *src = getLineData(screen, (int) j);
copyLineData(dst, src);
}
}
/*
* Copy line-data from the save-lines buffer to the visible (edit) buffer.
*/
static void
unsaveEditBufLines(TScreen * screen, ScrnBuf sb, unsigned n)
{
unsigned j;
TRACE(("...copying %d lines from saveBuf to editBuf\n", n));
for (j = 0; j < n; ++j) {
int extra = (int) (n - j);
LineData *dst = (LineData *) scrnHeadAddr(screen, sb, j);
#if OPT_FIFO_LINES
LineData *src;
if ((screen->saved_fifo - extra) <= 0) {
TRACE(("...FIXME: must clear text!\n"));
continue;
}
src = getScrollback(screen, -extra);
#else
unsigned k = (screen->savelines - extra);
LineData *src = (LineData *) scrnHeadAddr(screen,
screen->saveBuf_index, k);
#endif
copyLineData(dst, src);
}
}
#endif
/*
* This is called when the screen is resized.
* Returns the number of lines the text was moved down (neg for up).
* (Return value only necessary with SouthWestGravity.)
*/
static int
Reallocate(XtermWidget xw,
ScrnBuf * sbuf,
Char ** sbufaddr,
unsigned nrow,
unsigned ncol,
unsigned oldrow)
{
TScreen *screen = TScreenOf(xw);
ScrnBuf oldBufHead;
ScrnBuf newBufHead;
Char *newBufData;
unsigned minrows;
Char *oldBufData;
int move_down = 0, move_up = 0;
if (sbuf == NULL || *sbuf == NULL) {
return 0;
}
oldBufData = *sbufaddr;
TRACE(("Reallocate %dx%d -> %dx%d\n", oldrow, MaxCols(screen), nrow, ncol));
/*
* realloc sbuf, the pointers to all the lines.
* If the screen shrinks, remove lines off the top of the buffer
* if resizeGravity resource says to do so.
*/
TRACE(("Check move_up, nrow %d vs oldrow %d (resizeGravity %s)\n",
nrow, oldrow,
BtoS(GravityIsSouthWest(xw))));
if (GravityIsSouthWest(xw)) {
if (nrow < oldrow) {
/* Remove lines off the top of the buffer if necessary. */
move_up = (int) (oldrow - nrow)
- (TScreenOf(xw)->max_row - TScreenOf(xw)->cur_row);
if (move_up < 0)
move_up = 0;
/* Overlapping move here! */
TRACE(("move_up %d\n", move_up));
if (move_up) {
ScrnBuf dst = *sbuf;
unsigned len = (unsigned) ((int) oldrow - move_up);
TRACE_SCRNBUF("before move_up", screen, dst, oldrow);
SaveLineData(dst, 0, (size_t) move_up);
MoveLineData(dst, 0, (size_t) move_up, len);
RestoreLineData(dst, len, (size_t) move_up);
TRACE_SCRNBUF("after move_up", screen, dst, oldrow);
}
}
}
oldBufHead = *sbuf;
*sbuf = allocScrnHead(screen, (unsigned) nrow);
newBufHead = *sbuf;
/*
* Create the new buffer space and copy old buffer contents there, line by
* line.
*/
newBufData = allocScrnData(screen, nrow, ncol);
*sbufaddr = newBufData;
minrows = (oldrow < nrow) ? oldrow : nrow;
if (GravityIsSouthWest(xw)) {
if (nrow > oldrow) {
/* move data down to bottom of expanded screen */
move_down = Min((int) (nrow - oldrow), TScreenOf(xw)->savedlines);
}
}
setupLineData(screen, newBufHead, *sbufaddr, nrow, ncol);
extractScrnData(screen, newBufHead, oldBufHead, minrows,
#if OPT_SAVE_LINES
0
#else
(unsigned) move_down
#endif
);
free(oldBufHead);
/* Now free the old data */
free(oldBufData);
TRACE(("...Reallocate %dx%d ->%p\n", nrow, ncol, (void *) newBufHead));
return move_down ? move_down : -move_up; /* convert to rows */
}
#if OPT_WIDE_CHARS
/*
* This function reallocates memory if changing the number of Buf offsets.
* The code is based on Reallocate().
*/
static void
ReallocateBufOffsets(XtermWidget xw,
ScrnBuf * sbuf,
Char ** sbufaddr,
unsigned nrow,
unsigned ncol)
{
TScreen *screen = TScreenOf(xw);
unsigned i;
ScrnBuf newBufHead;
Char *oldBufData;
ScrnBuf oldBufHead;
unsigned old_jump = scrnHeadSize(screen, 1);
unsigned new_jump;
unsigned new_ptrs = 1 + (unsigned) (screen->max_combining);
unsigned dstCols = ncol;
unsigned srcCols = ncol;
LineData *dstPtrs;
LineData *srcPtrs;
Char *nextPtr;
assert(nrow != 0);
assert(ncol != 0);
oldBufData = *sbufaddr;
oldBufHead = *sbuf;
/*
* Allocate a new LineData array, retain the old one until we've copied
* the data that it points to, as well as non-pointer data, e.g., bufHead.
*
* Turn on wide-chars temporarily when constructing pointers, since that is
* used to decide whether to address the combData[] array, which affects
* the length of the LineData structure.
*/
screen->wide_chars = True;
new_jump = scrnHeadSize(screen, 1);
newBufHead = allocScrnHead(screen, nrow);
*sbufaddr = allocScrnData(screen, nrow, ncol);
setupLineData(screen, newBufHead, *sbufaddr, nrow, ncol);
screen->wide_chars = False;
nextPtr = *sbufaddr;
srcPtrs = (LineData *) oldBufHead;
dstPtrs = (LineData *) newBufHead;
for (i = 0; i < nrow; i++) {
dstPtrs->bufHead = srcPtrs->bufHead;
ExtractScrnData(attribs);
#if OPT_ISO_COLORS
ExtractScrnData(color);
#endif
ExtractScrnData(charData);
nextPtr += ncol * new_ptrs;
srcPtrs = LineDataAddr(srcPtrs, old_jump);
dstPtrs = LineDataAddr(dstPtrs, new_jump);
}
/* Now free the old data */
free(oldBufData);
free(oldBufHead);
*sbuf = newBufHead;
TRACE(("ReallocateBufOffsets %dx%d ->%p\n", nrow, ncol, *sbufaddr));
}
#if OPT_FIFO_LINES
/*
* Allocate a new FIFO index.
*/
static void
ReallocateFifoIndex(XtermWidget xw)
{
TScreen *screen = TScreenOf(xw);
if (screen->savelines > 0 && screen->saveBuf_index != 0) {
ScrnBuf newBufHead;
LineData *dstPtrs;
LineData *srcPtrs;
unsigned i;
unsigned old_jump = scrnHeadSize(screen, 1);
unsigned new_jump;
screen->wide_chars = True;
newBufHead = allocScrnHead(screen, (unsigned) screen->savelines);
new_jump = scrnHeadSize(screen, 1);
srcPtrs = (LineData *) screen->saveBuf_index;
dstPtrs = (LineData *) newBufHead;
for (i = 0; i < (unsigned) screen->savelines; ++i) {
memcpy(dstPtrs, srcPtrs, SizeOfLineData);
srcPtrs = LineDataAddr(srcPtrs, old_jump);
dstPtrs = LineDataAddr(dstPtrs, new_jump);
}
screen->wide_chars = False;
free(screen->saveBuf_index);
screen->saveBuf_index = newBufHead;
}
}
#endif
/*
* This function dynamically adds support for wide-characters.
*/
void
ChangeToWide(XtermWidget xw)
{
TScreen *screen = TScreenOf(xw);
if (screen->wide_chars)
return;
TRACE(("ChangeToWide\n"));
if (xtermLoadWideFonts(xw, True)) {
int whichBuf = screen->whichBuf;
#if !OPT_FIFO_LINES || !OPT_SAVE_LINES
int savelines = screen->scrollWidget ? screen->savelines : 0;
if (savelines < 0)
savelines = 0;
#endif
/*
* If we're displaying the alternate screen, switch the pointers back
* temporarily so ReallocateBufOffsets() will operate on the proper
* data in the alternate buffer.
*/
if (screen->whichBuf)
SwitchBufPtrs(screen, 0);
#if OPT_SAVE_LINES
#if OPT_FIFO_LINES
ReallocateFifoIndex(xw);
#else
ReallocateBufOffsets(xw,
&screen->saveBuf_index,
&screen->saveBuf_data,
(unsigned) savelines,
(unsigned) MaxCols(screen));
#endif
if (screen->editBuf_index[0]) {
ReallocateBufOffsets(xw,
&screen->editBuf_index[0],
&screen->editBuf_data[0],
(unsigned) MaxRows(screen),
(unsigned) MaxCols(screen));
}
#else
ReallocateBufOffsets(xw,
&screen->saveBuf_index,
&screen->saveBuf_data,
(unsigned) (MaxRows(screen) + savelines),
(unsigned) MaxCols(screen));
#endif
if (screen->editBuf_index[1]) {
ReallocateBufOffsets(xw,
&screen->editBuf_index[1],
&screen->editBuf_data[1],
(unsigned) MaxRows(screen),
(unsigned) MaxCols(screen));
}
screen->wide_chars = True;
screen->visbuf = VisBuf(screen);
/*
* Switch the pointers back before we start painting on the screen.
*/
if (whichBuf)
SwitchBufPtrs(screen, whichBuf);
update_font_utf8_mode();
SetVTFont(xw, screen->menu_font_number, True, NULL);
}
TRACE(("...ChangeToWide\n"));
}
#endif
/*
* Clear cells, no side-effects.
*/
void
CopyCells(TScreen * screen, LineData * src, LineData * dst, int col, int len)
{
if (len > 0) {
int n;
int last = col + len;
for (n = col; n < last; ++n) {
dst->charData[n] = src->charData[n];
dst->attribs[n] = src->attribs[n];
}
if_OPT_ISO_COLORS(screen, {
for (n = col; n < last; ++n) {
dst->color[n] = src->color[n];
}
});
if_OPT_WIDE_CHARS(screen, {
size_t off;
for (n = col; n < last; ++n) {
for_each_combData(off, src) {
dst->combData[off][n] = src->combData[off][n];
}
}
});
}
}
/*
* Clear cells, no side-effects.
*/
void
ClearCells(XtermWidget xw, int flags, unsigned len, int row, int col)
{
if (len != 0) {
TScreen *screen = TScreenOf(xw);
LineData *ld;
unsigned n;
ld = getLineData(screen, row);
flags = (int) ((unsigned) flags | TERM_COLOR_FLAGS(xw));
for (n = 0; n < len; ++n)
ld->charData[(unsigned) col + n] = (CharData) ' ';
memset(ld->attribs + col, flags, (size_t) len);
if_OPT_ISO_COLORS(screen, {
CellColor p = xtermColorPair(xw);
for (n = 0; n < len; ++n) {
ld->color[(unsigned) col + n] = p;
}
});
if_OPT_WIDE_CHARS(screen, {
size_t off;
for_each_combData(off, ld) {
memset(ld->combData[off] + col, 0, (size_t) len * sizeof(CharData));
}
});
}
}
/*
* Clear data in the screen-structure (no I/O).
* Check for wide-character damage as well, clearing the damaged cells.
*/
void
ScrnClearCells(XtermWidget xw, int row, int col, unsigned len)
{
#if OPT_WIDE_CHARS
TScreen *screen = TScreenOf(xw);
#endif
int flags = 0;
if_OPT_WIDE_CHARS(screen, {
int kl;
int kr;
if (DamagedCells(screen, len, &kl, &kr, row, col)
&& kr >= kl) {
ClearCells(xw, flags, (unsigned) (kr - kl + 1), row, kl);
}
});
ClearCells(xw, flags, len, row, col);
}
/*
* Disown the selection and repaint the area that is highlighted so it is no
* longer highlighted.
*/
void
ScrnDisownSelection(XtermWidget xw)
{
if (ScrnHaveSelection(TScreenOf(xw))) {
if (TScreenOf(xw)->keepSelection) {
UnhiliteSelection(xw);
} else {
DisownSelection(xw);
}
}
}
/*
* Writes str into buf at screen's current row and column. Characters are set
* to match flags.
*/
void
ScrnWriteText(XtermWidget xw,
IChar * str,
unsigned flags,
unsigned cur_fg_bg,
unsigned length)
{
TScreen *screen = TScreenOf(xw);
LineData *ld;
Char *attrs;
int avail = MaxCols(screen) - screen->cur_col;
IChar *chars;
#if OPT_WIDE_CHARS
IChar starcol1;
#endif
unsigned n;
unsigned real_width = visual_width(str, length);
(void) cur_fg_bg;
if (real_width + (unsigned) screen->cur_col > (unsigned) MaxCols(screen)) {
real_width = (unsigned) (MaxCols(screen) - screen->cur_col);
}
if (avail <= 0)
return;
if (length > (unsigned) avail)
length = (unsigned) avail;
if (length == 0 || real_width == 0)
return;
ld = getLineData(screen, screen->cur_row);
chars = ld->charData + screen->cur_col;
attrs = ld->attribs + screen->cur_col;
#if OPT_WIDE_CHARS
starcol1 = *chars;
#endif
/* write blanks if we're writing invisible text */
for (n = 0; n < length; ++n) {
if ((flags & INVISIBLE))
chars[n] = ' ';
else
chars[n] = str[n];
}
#if OPT_BLINK_TEXT
if ((flags & BLINK) && !(screen->blink_as_bold)) {
LineSetBlinked(ld);
}
#endif
if_OPT_WIDE_CHARS(screen, {
if (real_width != length) {
IChar *char1 = chars;
if (screen->cur_col
&& starcol1 == HIDDEN_CHAR
&& isWide((int) char1[-1])) {
char1[-1] = (CharData) ' ';
}
/* if we are overwriting the right hand half of a
wide character, make the other half vanish */
while (length) {
int ch = (int) str[0];
*char1++ = *str++;
length--;
if (isWide(ch)) {
*char1++ = (CharData) HIDDEN_CHAR;
}
}
if (*char1 == HIDDEN_CHAR
&& char1[-1] == HIDDEN_CHAR) {
*char1 = (CharData) ' ';
}
/* if we are overwriting the left hand half of a
wide character, make the other half vanish */
} else {
if (screen->cur_col
&& starcol1 == HIDDEN_CHAR
&& isWide((int) chars[-1])) {
chars[-1] = (CharData) ' ';
}
/* if we are overwriting the right hand half of a
wide character, make the other half vanish */
if (chars[length] == HIDDEN_CHAR
&& isWide((int) chars[length - 1])) {
chars[length] = (CharData) ' ';
}
}
});
flags &= ATTRIBUTES;
flags |= CHARDRAWN;
memset(attrs, (Char) flags, (size_t) real_width);
if_OPT_WIDE_CHARS(screen, {
size_t off;
for_each_combData(off, ld) {
memset(ld->combData[off] + screen->cur_col,
0,
real_width * sizeof(CharData));
}
});
if_OPT_ISO_COLORS(screen, {
unsigned j;
for (j = 0; j < real_width; ++j)
ld->color[screen->cur_col + (int) j] = (CellColor) cur_fg_bg;
});
#if OPT_WIDE_CHARS
screen->last_written_col = screen->cur_col + (int) real_width - 1;
screen->last_written_row = screen->cur_row;
#endif
if_OPT_XMC_GLITCH(screen, {
Resolve_XMC(xw);
});
return;
}
/*
* Saves pointers to the n lines beginning at sb + where, and clears the lines
*/
static void
ScrnClearLines(XtermWidget xw, ScrnBuf sb, int where, unsigned n, unsigned size)
{
TScreen *screen = TScreenOf(xw);
ScrnPtr *base;
unsigned jump = scrnHeadSize(screen, 1);
unsigned i;
LineData *work;
unsigned flags = TERM_COLOR_FLAGS(xw);
#if OPT_ISO_COLORS
unsigned j;
#endif
TRACE(("ScrnClearLines(%s:where %d, n %d, size %d)\n",
(sb == screen->saveBuf_index) ? "save" : "edit",
where, n, size));
assert(n != 0);
assert(size != 0);
/* save n lines at where */
SaveLineData(sb, (unsigned) where, (size_t) n);
/* clear contents of old rows */
base = screen->save_ptr;
for (i = 0; i < n; ++i) {
work = (LineData *) base;
work->bufHead = 0;
#if OPT_DEC_CHRSET
SetLineDblCS(work, 0);
#endif
memset(work->charData, 0, size * sizeof(CharData));
if (TERM_COLOR_FLAGS(xw)) {
memset(work->attribs, (int) flags, (size_t) size);
#if OPT_ISO_COLORS
{
CellColor p = xtermColorPair(xw);
for (j = 0; j < size; ++j) {
work->color[j] = p;
}
}
#endif
} else {
memset(work->attribs, 0, (size_t) size);
#if OPT_ISO_COLORS
memset(work->color, 0, size * sizeof(work->color[0]));
#endif
}
#if OPT_WIDE_CHARS
if (screen->wide_chars) {