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
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
|
/*
* kmscon - Console Management
*
* Copyright (c) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
* Copyright (c) 2011 University of Tuebingen
*
* 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 AUTHORS OR COPYRIGHT HOLDERS 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.
*/
/*
* Console Management
* This provides the console drawing and manipulation functions. It does not
* provide the terminal emulation. It is just an abstraction layer to draw text
* to a framebuffer as used by terminals and consoles.
*/
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "console.h"
#include "font.h"
#include "gl.h"
#include "log.h"
#include "unicode.h"
#define LOG_SUBSYSTEM "console"
#define DEFAULT_WIDTH 80
#define DEFAULT_HEIGHT 24
struct cell {
kmscon_symbol_t ch;
};
struct line {
struct line *next;
struct line *prev;
unsigned int size;
struct cell *cells;
};
struct kmscon_buffer {
/* scroll-back buffer */
unsigned int sb_count; /* number of lines in sb */
struct line *sb_first; /* first line; was moved first */
struct line *sb_last; /* last line; was moved last*/
unsigned int sb_max; /* max-limit of lines in sb */
/* current position in sb; if NULL, the main screen is shown */
struct line *position;
/* fixed=true means that if the current focus is on the sb, then the
* focus will stay on the line even if new lines are added to the sb or
* removed. fixed=false means the distance from the current position to
* the main-buffer is kept constant. In most situations this means that
* the sb seems to scroll-up while staying on the line
*/
bool fixed_position;
/* current cell/row count of the main screen */
unsigned int size_x; /* cell count */
unsigned int size_y; /* row count */
/* main scroll buffer */
unsigned int scroll_y; /* number of rows in this buffer */
unsigned int scroll_fill; /* current fill; last line pushed */
struct line **scroll_buf; /* lines of the buffer */
/* margin buffers */
unsigned int mtop_y; /* number of rows in top margin */
struct line **mtop_buf; /* lines of the top margin */
unsigned int mbottom_y; /* number of rows in bottom margin */
struct line **mbottom_buf; /* lines of the bottom margin */
};
struct kmscon_console {
size_t ref;
/* console cells */
struct kmscon_buffer *cells;
bool rel_addr; /* is relative addressing used? */
bool auto_wrap; /* auto wrap on end of line? */
/* cursor */
unsigned int cursor_x;
unsigned int cursor_y;
};
/* Console Buffer and Cell Objects
* A console buffer maintains an array of lines of the current screen buffer
* and a list of lines in the scrollback buffer. The screen buffer can be
* modified, the scrollback buffer is constant.
*
* Buffers:
* The buffer object consists of three buffers. The top margin, the bottom
* margin and the scroll buffer. The margins are non-existent by default. The
* scroll buffer is the main buffer. If the terminal is rotated then the lines
* of this buffer go to the scroll-back buffer and vice versa.
* The margin buffers are static. They can be modified but they are never
* rotated. If the margins are created, the lines are copied from the scroll
* buffer and then removed from the scroll buffer. If the margins are removed,
* the lines are linked back into the scroll buffer at the current position.
* Each buffer is an array of lines. The lines can be modified very fast and
* rotations do not require heavy memory-moves. If a line is NULL we consider
* this line empty so we can resize the buffer without reallocating new lines.
*
* Scrollback buffer:
* The scrollback buffer contains all lines that were pushed out of the current
* screen. It's a linked list of lines which cannot be accessed by the
* application. It has an upper bound so we do not consume too much memory.
* If the current buffer is resized to a bigger size, then lines from the
* scrollback buffer may get back into the current buffer to fill the screen.
*
* Lines:
* A single line is represented by a "struct line". It has an array of cells
* which can be accessed directly. The length of each line may vary and for
* faster resizing we also keep a \size member.
* Lines may be shorter than the current buffer width. We do not resize them to
* speed up the buffer operations. If a line is printed which is longer or
* shorted than the screen width, it is simply filled with spaces or truncated
* to screen width.
* If such a line is accessed outside the bounds, the line is resized to the
* current screen width to allow access.
*
* Screen position:
* The current screen position may be any line inside the scrollback buffer. If
* it is NULL, the current position is set to the current screen buffer.
* If it is non-NULL it will stick to the given line and will not scroll back
* on new input.
*
* Cells
* A single cell describes a single character that is printed in that cell. The
* character itself is a kmscon_char unicode character. The cell also contains
* the color of the character and some other metadata.
*/
static void destroy_cell(struct cell *cell)
{
if (!cell)
return;
}
static int init_cell(struct cell *cell)
{
if (!cell)
return -EINVAL;
memset(cell, 0, sizeof(*cell));
return 0;
}
static void reset_cell(struct cell *cell)
{
if (!cell)
return;
memset(cell, 0, sizeof(*cell));
}
static void free_line(struct line *line)
{
unsigned int i;
if (!line)
return;
for (i = 0; i < line->size; ++i)
destroy_cell(&line->cells[i]);
free(line->cells);
free(line);
}
static int new_line(struct line **out)
{
struct line *line;
if (!out)
return -EINVAL;
line = malloc(sizeof(*line));
if (!line)
return -ENOMEM;
memset(line, 0, sizeof(*line));
*out = line;
return 0;
}
static int resize_line(struct line *line, unsigned int width)
{
struct cell *tmp;
int ret;
if (!line)
return -EINVAL;
if (!width)
width = DEFAULT_WIDTH;
if (line->size < width) {
tmp = realloc(line->cells, width * sizeof(struct cell));
if (!tmp)
return -ENOMEM;
line->cells = tmp;
while (line->size < width) {
ret = init_cell(&line->cells[line->size]);
if (ret)
return ret;
line->size++;
}
} else if (line->size > width) {
while (line->size > width) {
line->size--;
destroy_cell(&line->cells[line->size]);
}
}
return 0;
}
static struct line *get_line(struct kmscon_buffer *buf, unsigned int y)
{
if (y < buf->mtop_y) {
return buf->mtop_buf[y];
} else if (y < buf->mtop_y + buf->scroll_y) {
y -= buf->mtop_y;
return buf->scroll_buf[y];
} else if (y < buf->mtop_y + buf->scroll_y + buf->mbottom_y) {
y = y - buf->mtop_y - buf->scroll_y;
return buf->mbottom_buf[y];
}
return NULL;
}
/* This links the given line into the scrollback-buffer. This always succeeds.
* If \line is NULL then an empty line is created that is pushed to the
* scrollback buffer.
*/
static void link_to_scrollback(struct kmscon_buffer *buf, struct line *line)
{
struct line *tmp;
int ret;
if (!buf)
return;
if (buf->sb_max == 0) {
free_line(line);
return;
}
/* line==NULL means the line is empty. The scrollback buffer cannot
* contain such lines, though. Therefore, explicitely allocate a new
* empty line with line!=NULL but size=0.
*/
if (!line) {
ret = new_line(&line);
if (ret) {
log_warn("cannot allocate line (%d); dropping scrollback-buffer line", ret);
return;
}
}
/* Remove a line from the scrollback buffer if it reaches its maximum.
* We must take care to correctly keep the current position as the new
* line is linked in after we remove the top-most line here.
* sb_max == 0 is tested earlier so we can assume sb_max > 0 here. In
* other words, buf->sb_first is a valid line if sb_count >= sb_max.
*/
if (buf->sb_count >= buf->sb_max) {
tmp = buf->sb_first;
buf->sb_first = tmp->next;
if (tmp->next)
tmp->next->prev = NULL;
else
buf->sb_last = NULL;
buf->sb_count--;
/* (position==tmp && !next) means we have sb_max=1 so set
* position to the new line. Otherwise, set to new first line.
* If position!=tmp and we have a fixed_position then nothing
* needs to be done because we can stay at the same line. If we
* have no fixed_position, we need to set the position to the
* next inserted line, which can be "line", too.
*/
if (buf->position) {
if (buf->position == tmp || !buf->fixed_position) {
if (buf->position->next)
buf->position = buf->position->next;
else
buf->position = line;
}
}
free_line(tmp);
}
line->next = NULL;
line->prev = buf->sb_last;
if (buf->sb_last)
buf->sb_last->next = line;
else
buf->sb_first = line;
buf->sb_last = line;
buf->sb_count++;
}
/* Unlinks last line from the scrollback buffer, Returns NULL if it is empty */
static struct line *get_from_scrollback(struct kmscon_buffer *buf)
{
struct line *line;
if (!buf || !buf->sb_last)
return NULL;
line = buf->sb_last;
buf->sb_last = line->prev;
if (line->prev)
line->prev->next = NULL;
else
buf->sb_first = NULL;
buf->sb_count--;
/* correctly move the current position if it is set in the sb */
if (buf->position) {
if (buf->fixed_position) {
if (buf->position == line)
buf->position = NULL;
} else if (!buf->position->prev) {
if (buf->position == line)
buf->position = NULL;
} else {
buf->position = buf->position->prev;
}
}
line->next = NULL;
line->prev = NULL;
return line;
}
/* Resize scroll buffer. Despite being used for scroll region only, it is kept
* big enough to hold both margins too. We do this to allow fast merges of
* margins and scroll buffer.
*/
static int resize_scrollbuf(struct kmscon_buffer *buf, unsigned int y)
{
unsigned int fill, i, siz;
struct line *iter, **cache;
/* Resize y size by adjusting the scroll-buffer size */
if (y < buf->scroll_y) {
/* Shrink scroll-buffer. First move enough elements from the
* scroll-buffer into the scroll-back buffer so we can shrink
* it without loosing data.
* Then reallocate the buffer (we shrink it so we never fail
* here) and correctly set values in \buf. If the buffer has
* unused lines, we can shrink it down without moving lines
* into the scrollback-buffer so first calculate the current
* fill of the buffer and then move appropriate amount of
* elements to the scrollback buffer.
*/
if (buf->scroll_fill > y) {
for (i = y; i < buf->scroll_fill; ++i)
link_to_scrollback(buf, buf->scroll_buf[i - y]);
memmove(buf->scroll_buf,
&buf->scroll_buf[buf->scroll_fill - y],
y * sizeof(struct line*));
}
siz = buf->mtop_y + buf->mbottom_y + y;
buf->scroll_buf = realloc(buf->scroll_buf,
siz * sizeof(struct line*));
buf->scroll_y = y;
if (buf->scroll_fill > y)
buf->scroll_fill = y;
} else if (y > buf->scroll_y) {
/* Increase scroll-buffer to new size. Reset all new elements
* to NULL so they are empty. Copy existing buffer into new
* buffer and correctly set values in \buf.
* If we have more space in the buffer, we simply move lines
* from the scroll-back buffer into our scroll-buffer if they
* are available. Otherwise, we simply add NULL lines.
*/
siz = buf->mtop_y + buf->mbottom_y + y;
cache = malloc(sizeof(struct line*) * siz);
if (!cache)
return -ENOMEM;
memset(cache, 0, sizeof(struct line*) * siz);
fill = y - buf->scroll_y;
for (i = 0; i < fill; ++i) {
iter = get_from_scrollback(buf);
if (!iter)
break;
cache[y - i - 1] = iter;
}
buf->scroll_fill += i;
memmove(cache, &cache[y - i], i * sizeof(struct line*));
memset(&cache[i + buf->scroll_y], 0,
(fill - i) * sizeof(struct line*));
if (buf->scroll_y)
memcpy(&cache[i], buf->scroll_buf,
sizeof(struct line*) * buf->scroll_y);
free(buf->scroll_buf);
buf->scroll_buf = cache;
buf->scroll_y = y;
}
return 0;
}
static int resize_mtop(struct kmscon_buffer *buf, unsigned int y)
{
unsigned int mv;
struct line **cache;
if (y == buf->mtop_y)
return 0;
if (y < buf->mtop_y) {
mv = buf->mtop_y - y;
memmove(&buf->scroll_buf[mv], buf->scroll_buf,
buf->scroll_y * sizeof(struct line*));
memcpy(buf->scroll_buf, &buf->mtop_buf[y],
mv * sizeof(struct line*));
buf->scroll_fill += mv;
buf->scroll_y += mv;
buf->mtop_y -= mv;
} else {
mv = y - buf->mtop_y;
if (mv >= buf->scroll_y) {
log_debug("setting margin size above buffer size; trimming margin");
if (buf->scroll_y <= 1)
return 0;
mv = buf->scroll_y - 1;
y = buf->mtop_y + mv;
}
cache = malloc(y * sizeof(struct line*));
if (!cache)
return -ENOMEM;
memcpy(cache, buf->mtop_buf,
buf->mtop_y * sizeof(struct line*));
memcpy(&cache[buf->mtop_y], buf->scroll_buf,
mv * sizeof(struct line*));
memmove(buf->scroll_buf, &buf->scroll_buf[mv],
(buf->scroll_y - mv) * sizeof(struct line*));
if (buf->scroll_fill > mv)
buf->scroll_fill -= mv;
else
buf->scroll_fill = 0;
buf->scroll_y -= mv;
buf->mtop_y += mv;
free(buf->mtop_buf);
buf->mtop_buf = cache;
}
return 0;
}
static int resize_mbottom(struct kmscon_buffer *buf, unsigned int y)
{
unsigned int mv;
struct line **cache;
if (y == buf->mbottom_y)
return 0;
if (y < buf->mbottom_y) {
mv = buf->mbottom_y - y;
memcpy(&buf->scroll_buf[buf->scroll_y], buf->mbottom_buf,
mv * sizeof(struct line*));
memmove(buf->mbottom_buf, &buf->mbottom_buf[mv],
(buf->mbottom_y - mv) * sizeof(struct line*));
buf->scroll_y += mv;
buf->scroll_fill = buf->scroll_y;
buf->mbottom_y -= mv;
} else {
mv = y - buf->mbottom_y;
if (mv >= buf->scroll_y) {
log_debug("setting margin size above buffer size; trimming margin");
if (buf->scroll_y <= 1)
return 0;
mv = buf->scroll_y - 1;
y = buf->mbottom_y + mv;
}
cache = malloc(y * sizeof(struct line*));
if (!cache)
return -ENOMEM;
memcpy(&cache[mv], buf->mbottom_buf,
buf->mbottom_y * sizeof(struct line*));
memcpy(cache, &buf->scroll_buf[buf->scroll_y - mv],
mv * sizeof(struct line*));
buf->scroll_y -= mv;
if (buf->scroll_fill > buf->scroll_y)
buf->scroll_fill = buf->scroll_y;
buf->mbottom_y += mv;
free(buf->mbottom_buf);
buf->mbottom_buf = cache;
}
return 0;
}
/* Resize the current console buffer
* This resizes the current buffer. We do not resize the lines or modify them
* in any way. This would take too long if multiple resize-operations are
* performed.
*/
static int kmscon_buffer_resize(struct kmscon_buffer *buf, unsigned int x,
unsigned int y)
{
int ret;
unsigned int margin;
if (!buf)
return -EINVAL;
if (!x)
x = DEFAULT_WIDTH;
if (!y)
y = DEFAULT_HEIGHT;
if (buf->size_x == x && buf->size_y == y)
return 0;
margin = buf->mtop_y + buf->mbottom_y;
if (y <= margin) {
log_debug("reducing buffer size below margin size; destroying margins");
resize_mtop(buf, 0);
resize_mbottom(buf, 0);
}
ret = resize_scrollbuf(buf, buf->scroll_y + (y - (int)buf->size_y));
if (ret)
return ret;
buf->size_y = y;
/* Adjust x size by simply setting the new value */
buf->size_x = x;
log_debug("resize buffer to %ux%u", x, y);
return 0;
}
/* set maximum scrollback buffer size */
static void kmscon_buffer_set_max_sb(struct kmscon_buffer *buf,
unsigned int max)
{
struct line *line;
if (!buf)
return;
while (buf->sb_count > max) {
line = buf->sb_first;
if (!line)
break;
buf->sb_first = line->next;
if (line->next)
line->next->prev = NULL;
else
buf->sb_last = NULL;
buf->sb_count--;
/* We treat fixed/unfixed position the same here because we
* remove lines from the TOP of the scrollback buffer.
*/
if (buf->position == line) {
if (buf->sb_first)
buf->position = buf->sb_first;
else
buf->position = NULL;
}
free_line(line);
}
buf->sb_max = max;
}
/* clear scrollback buffer */
static void kmscon_buffer_clear_sb(struct kmscon_buffer *buf)
{
struct line *iter, *tmp;
if (!buf)
return;
for (iter = buf->sb_first; iter; ) {
tmp = iter;
iter = iter->next;
free_line(tmp);
}
buf->sb_first = NULL;
buf->sb_last = NULL;
buf->sb_count = 0;
buf->position = NULL;
}
static int kmscon_buffer_new(struct kmscon_buffer **out, unsigned int x,
unsigned int y)
{
struct kmscon_buffer *buf;
int ret;
if (!out)
return -EINVAL;
buf = malloc(sizeof(*buf));
if (!buf)
return -ENOMEM;
memset(buf, 0, sizeof(*buf));
ret = kmscon_buffer_resize(buf, x, y);
if (ret)
goto err_free;
log_debug("new buffer object");
*out = buf;
return 0;
err_free:
free(buf);
return ret;
}
static void kmscon_buffer_free(struct kmscon_buffer *buf)
{
unsigned int i;
if (!buf)
return;
log_debug("destroying buffer object");
kmscon_buffer_clear_sb(buf);
for (i = 0; i < buf->scroll_y; ++i)
free_line(buf->scroll_buf[i]);
for (i = 0; i < buf->mtop_y; ++i)
free_line(buf->mtop_buf[i]);
for (i = 0; i < buf->mbottom_y; ++i)
free_line(buf->mbottom_buf[i]);
free(buf->scroll_buf);
free(buf->mtop_buf);
free(buf->mbottom_buf);
free(buf);
}
static int kmscon_buffer_set_margins(struct kmscon_buffer *buf,
unsigned int top,
unsigned int bottom)
{
int ret;
if (!buf)
return -EINVAL;
if (top < buf->mtop_y) {
ret = resize_mtop(buf, top);
if (ret)
return ret;
return resize_mbottom(buf, bottom);
} else {
ret = resize_mbottom(buf, bottom);
if (ret)
return ret;
return resize_mtop(buf, top);
}
}
static void kmscon_buffer_draw(struct kmscon_buffer *buf,
struct font_screen *fscr)
{
unsigned int i, j, k, num;
struct line *iter, *line = NULL;
struct cell *cell;
int idx;
float m[16];
if (!buf || !fscr)
return;
font_screen_draw_start(fscr);
iter = buf->position;
k = 0;
idx = 0;
for (i = 0; i < buf->size_y; ++i) {
if (iter) {
line = iter;
iter = iter->next;
} else {
if (idx == 0) {
if (k < buf->mtop_y) {
line = buf->mtop_buf[k];
} else {
k = 0;
idx = 1;
}
}
if (idx == 1) {
if (k < buf->scroll_y) {
line = buf->scroll_buf[k];
} else {
k = 0;
idx = 2;
}
}
if (idx == 2) {
if (k < buf->mbottom_y)
line = buf->mbottom_buf[k];
else
break;
}
k++;
}
if (!line)
continue;
if (line->size < buf->size_x)
num = line->size;
else
num = buf->size_x;
for (j = 0; j < num; ++j) {
cell = &line->cells[j];
font_screen_draw_char(fscr, cell->ch, j, i, 1, 1);
}
}
gl_m4_identity(m);
font_screen_draw_perform(fscr, m);
}
static void kmscon_buffer_write(struct kmscon_buffer *buf, unsigned int x,
unsigned int y, kmscon_symbol_t ch)
{
struct line *line, **slot;
int ret;
bool scroll = false;
if (!buf)
return;
if (x >= buf->size_x || y >= buf->size_y) {
log_warn("writing beyond buffer boundary");
return;
}
if (y < buf->mtop_y) {
slot = &buf->mtop_buf[y];
} else if (y < buf->mtop_y + buf->scroll_y) {
y -= buf->mtop_y;
slot = &buf->scroll_buf[y];
scroll = true;
} else if (y < buf->mtop_y + buf->scroll_y + buf->mbottom_y) {
y = y - buf->mtop_y - buf->scroll_y;
slot = &buf->mbottom_buf[y];
} else {
log_warn("writing to invalid buffer space");
return;
}
line = *slot;
if (!line) {
ret = new_line(&line);
if (ret) {
log_warn("cannot allocate line (%d); dropping input", ret);
return;
}
*slot = line;
if (scroll && buf->scroll_fill <= y)
buf->scroll_fill = y + 1;
}
if (x >= line->size) {
ret = resize_line(line, buf->size_x);
if (ret) {
log_warn("cannot resize line (%d); dropping input", ret);
return;
}
}
line->cells[x].ch = ch;
}
static kmscon_symbol_t kmscon_buffer_read(struct kmscon_buffer *buf,
unsigned int x,
unsigned int y)
{
struct line *line;
if (!buf)
return kmscon_symbol_default;
if (x >= buf->size_x || y >= buf->size_y) {
log_warn("reading out of buffer bounds");
return kmscon_symbol_default;
}
if (y < buf->mtop_y) {
line = buf->mtop_buf[y];
} else if (y < buf->mtop_y + buf->scroll_y) {
y -= buf->mtop_y;
line = buf->scroll_buf[y];
} else if (y < buf->mtop_y + buf->scroll_y + buf->mbottom_y) {
y = y - buf->mtop_y - buf->scroll_y;
line = buf->mbottom_buf[y];
} else {
log_warn("reading from invalid buffer space");
return kmscon_symbol_default;
}
if (!line)
return kmscon_symbol_default;
if (x >= line->size)
return kmscon_symbol_default;
return line->cells[x].ch;
}
static void kmscon_buffer_scroll_down(struct kmscon_buffer *buf,
unsigned int num)
{
unsigned int i;
if (!buf || !num)
return;
if (num > buf->scroll_y)
num = buf->scroll_y;
for (i = 0; i < num; ++i)
free_line(buf->scroll_buf[buf->scroll_y - i - 1]);
memmove(&buf->scroll_buf[num], buf->scroll_buf,
(buf->scroll_y - num) * sizeof(struct line*));
memset(buf->scroll_buf, 0, num * sizeof(struct line*));
buf->scroll_fill = buf->scroll_y;
}
static void kmscon_buffer_scroll_up(struct kmscon_buffer *buf,
unsigned int num)
{
unsigned int i;
if (!buf || !num)
return;
if (num > buf->scroll_y)
num = buf->scroll_y;
for (i = 0; i < num; ++i)
link_to_scrollback(buf, buf->scroll_buf[i]);
memmove(buf->scroll_buf, &buf->scroll_buf[num],
(buf->scroll_y - num) * sizeof(struct line*));
memset(&buf->scroll_buf[buf->scroll_y - num], 0,
num * sizeof(struct line*));
buf->scroll_fill = buf->scroll_y;
}
static void kmscon_buffer_erase_region(struct kmscon_buffer *buf,
unsigned int x_from,
unsigned int y_from,
unsigned int x_to,
unsigned int y_to)
{
unsigned int to;
struct line *line;
if (!buf)
return;
if (y_to >= buf->size_y)
y_to = buf->size_y - 1;
if (x_to >= buf->size_x)
x_to = buf->size_x - 1;
for ( ; y_from <= y_to; ++y_from) {
line = get_line(buf, y_from);
if (!line) {
x_from = 0;
continue;
}
if (y_from == y_to)
to = x_to;
else
to = buf->size_x - 1;
for ( ; x_from <= to; ++x_from) {
if (x_from >= line->size)
break;
reset_cell(&line->cells[x_from]);
}
x_from = 0;
}
}
/* Console Object
* The console object is a state-machine that represents the current state of
* the whole console. Besides managing the buffer it also controls the current
* cursor position on similar.
* The functions that are provided by this console are exactly the ones that
* are needed to emulate the classic VTs. Therefore, they are designed to have
* the desired behavior and configuration-options.
*/
static inline unsigned int to_abs_x(struct kmscon_console *con, unsigned int x)
{
return x;
}
static inline unsigned int to_abs_y(struct kmscon_console *con, unsigned int y)
{
if (!con->rel_addr)
return y;
return con->cells->mtop_y + y;
}
int kmscon_console_new(struct kmscon_console **out)
{
struct kmscon_console *con;
int ret;
if (!out)
return -EINVAL;
con = malloc(sizeof(*con));
if (!con)
return -ENOMEM;
memset(con, 0, sizeof(*con));
con->ref = 1;
con->auto_wrap = true;
ret = kmscon_buffer_new(&con->cells, 0, 0);
if (ret)
goto err_free;
log_debug("new console");
*out = con;
return 0;
err_free:
free(con);
return ret;
}
void kmscon_console_ref(struct kmscon_console *con)
{
if (!con)
return;
++con->ref;
}
/*
* Drops one reference. If this is the last reference, the whole console is
* freed and the associated render-images are destroyed.
*/
void kmscon_console_unref(struct kmscon_console *con)
{
if (!con || !con->ref)
return;
if (--con->ref)
return;
log_debug("destroying console");
kmscon_buffer_free(con->cells);
free(con);
}
unsigned int kmscon_console_get_width(struct kmscon_console *con)
{
if (!con)
return 0;
return con->cells->size_x;
}
unsigned int kmscon_console_get_height(struct kmscon_console *con)
{
if (!con)
return 0;
return con->cells->size_y;
}
void kmscon_console_draw(struct kmscon_console *con, struct font_screen *fscr)
{
if (!con)
return;
kmscon_buffer_draw(con->cells, fscr);
}
void kmscon_console_write(struct kmscon_console *con, kmscon_symbol_t ch)
{
unsigned int last;
if (!con)
return;
last = con->cells->scroll_y + con->cells->mtop_y;
if (con->cursor_x >= con->cells->size_x) {
if (con->auto_wrap) {
con->cursor_x = 0;
con->cursor_y++;
if (con->cursor_y >= last) {
con->cursor_y--;
kmscon_buffer_scroll_up(con->cells, 1);
}
} else {
con->cursor_x = con->cells->size_x - 1;
}
}
kmscon_buffer_write(con->cells, con->cursor_x, con->cursor_y, ch);
con->cursor_x++;
}
void kmscon_console_newline(struct kmscon_console *con)
{
unsigned int last;
if (!con)
return;
last = con->cells->scroll_y + con->cells->mtop_y;
con->cursor_x = 0;
con->cursor_y++;
if (con->cursor_y >= last) {
con->cursor_y--;
kmscon_buffer_scroll_up(con->cells, 1);
}
}
void kmscon_console_backspace(struct kmscon_console *con)
{
if (!con)
return;
if (con->cursor_x >= con->cells->size_x) {
con->cursor_x = con->cells->size_x - 2;
} else if (con->cursor_x > 0) {
con->cursor_x--;
} else if (con->auto_wrap) {
con->cursor_x = con->cells->size_x - 1;
kmscon_console_move_up(con, 1, true);
}
}
void kmscon_console_move_to(struct kmscon_console *con, unsigned int x,
unsigned int y)
{
unsigned int last;
if (!con)
return;
last = con->cells->scroll_y + con->cells->mtop_y;
con->cursor_x = to_abs_x(con, x);
if (con->cursor_x >= con->cells->size_x)
con->cursor_x = con->cells->size_x - 1;
con->cursor_y = to_abs_y(con, y);
if (con->cursor_y >= last) {
if (con->rel_addr)
con->cursor_y = last - 1;
else if (con->cursor_y >= con->cells->size_y)
con->cursor_y = con->cells->size_y - 1;
}
}
void kmscon_console_move_up(struct kmscon_console *con, unsigned int num,
bool scroll)
{
unsigned int diff;
if (!con || !num)
return;
if (num > con->cells->size_y)
num = con->cells->size_y;
if (con->rel_addr) {
diff = con->cursor_y - con->cells->mtop_y;
if (num > diff) {
num -= diff;
if (scroll)
kmscon_buffer_scroll_down(con->cells, num);
con->cursor_y = con->cells->mtop_y;
} else {
con->cursor_y -= num;
}
} else {
if (num > con->cursor_y) {
num -= con->cursor_y;
if (scroll)
kmscon_buffer_scroll_down(con->cells, num);
con->cursor_y = 0;
} else {
con->cursor_y -= num;
}
}
}
void kmscon_console_move_down(struct kmscon_console *con, unsigned int num,
bool scroll)
{
unsigned int diff, last;
if (!con || !num)
return;
last = con->cells->scroll_y + con->cells->mtop_y;
if (num > con->cells->size_y)
num = con->cells->size_y;
if (con->rel_addr) {
diff = last - 1 - con->cursor_y;
if (num > diff) {
num -= diff;
if (scroll)
kmscon_buffer_scroll_up(con->cells, num);
con->cursor_y = last - 1;
} else {
con->cursor_y += num;
}
} else {
diff = con->cells->size_y - con->cursor_y - 1;
if (num > diff) {
num -= diff;
if (scroll)
kmscon_buffer_scroll_up(con->cells, num);
con->cursor_y = con->cells->size_y - 1;
} else {
con->cursor_y += num;
}
}
}
void kmscon_console_move_left(struct kmscon_console *con, unsigned int num)
{
if (!con || !num)
return;
if (num > con->cells->size_x)
num = con->cells->size_x;
if (con->cursor_x >= con->cells->size_x)
con->cursor_x = con->cells->size_x - 1;
if (num > con->cursor_x)
con->cursor_x = 0;
else
con->cursor_x -= num;
}
void kmscon_console_move_right(struct kmscon_console *con, unsigned int num)
{
if (!con || !num)
return;
if (num > con->cells->size_x)
num = con->cells->size_x;
if (num + con->cursor_x >= con->cells->size_x)
con->cursor_x = con->cells->size_x - 1;
else
con->cursor_x += num;
}
void kmscon_console_move_line_end(struct kmscon_console *con)
{
if (!con)
return;
con->cursor_x = con->cells->size_x - 1;
}
void kmscon_console_move_line_home(struct kmscon_console *con)
{
if (!con)
return;
con->cursor_x = 0;
}
void kmscon_console_erase_cursor(struct kmscon_console *con)
{
unsigned int x;
if (!con)
return;
if (con->cursor_x >= con->cells->size_x)
x = con->cells->size_x - 1;
else
x = con->cursor_x;
kmscon_buffer_erase_region(con->cells, x, con->cursor_y,
x, con->cursor_y);
}
void kmscon_console_erase_cursor_to_end(struct kmscon_console *con)
{
unsigned int x;
if (!con)
return;
if (con->cursor_x >= con->cells->size_x)
x = con->cells->size_x - 1;
else
x = con->cursor_x;
kmscon_buffer_erase_region(con->cells, x, con->cursor_y,
con->cells->size_x - 1, con->cursor_y);
}
void kmscon_console_erase_home_to_cursor(struct kmscon_console *con)
{
if (!con)
return;
kmscon_buffer_erase_region(con->cells, 0, con->cursor_y,
con->cursor_x, con->cursor_y);
}
void kmscon_console_erase_current_line(struct kmscon_console *con)
{
if (!con)
return;
kmscon_buffer_erase_region(con->cells, 0, con->cursor_y,
con->cells->size_x - 1, con->cursor_y);
}
void kmscon_console_erase_screen_to_cursor(struct kmscon_console *con)
{
if (!con)
return;
kmscon_buffer_erase_region(con->cells, 0, 0,
con->cursor_x, con->cursor_y);
}
void kmscon_console_erase_cursor_to_screen(struct kmscon_console *con)
{
unsigned int x;
if (!con)
return;
if (con->cursor_x >= con->cells->size_x)
x = con->cells->size_x - 1;
else
x = con->cursor_x;
kmscon_buffer_erase_region(con->cells, x, con->cursor_y,
con->cells->size_x - 1, con->cells->size_y - 1);
}
void kmscon_console_erase_screen(struct kmscon_console *con)
{
if (!con)
return;
kmscon_buffer_erase_region(con->cells, 0, 0,
con->cells->size_x - 1, con->cells->size_y - 1);
}
|