-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathconsole.c
More file actions
725 lines (571 loc) · 20.2 KB
/
console.c
File metadata and controls
725 lines (571 loc) · 20.2 KB
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
/*---------------------------------------------------------------------------------
Copyright (C) 2005
Michael Noland (joat)
Jason Rogers (dovoto)
Dave Murphy (WinterMute)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
---------------------------------------------------------------------------------*/
#include <nds/ndstypes.h>
#include <nds/memory.h>
#include <nds/arm9/console.h>
#include <nds/arm9/video.h>
#include <nds/arm9/background.h>
#include <nds/debug.h>
#include <default_font_bin.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/iosupport.h>
PrintConsole defaultConsole =
{
//Font:
{
(u16*)default_font_bin, //font gfx
0, //font palette
0, //font color count
1, //bpp
0, //first ascii character in the set
256, //number of characters in the font set
true //convert single color
},
0, //font background map
0, //font background gfx
22, //map base
3, //char base
0, //bg layer in use
-1, //bg id
0,0, //cursorX cursorY
0,0, //prevcursorX prevcursorY
32, //console width
24, //console height
0, //window x
0, //window y
32, //window width
24, //window height
3, //tab size
0, //font character offset
0, //selected palette
0, //print callback
false, //console initialized
true, //load graphics
};
PrintConsole currentCopy;
PrintConsole* currentConsole = ¤tCopy;
PrintConsole* consoleGetDefault(void){return &defaultConsole;}
void consolePrintChar(char c);
//---------------------------------------------------------------------------------
static void consoleCls(char mode) {
//---------------------------------------------------------------------------------
int i = 0;
int colTemp,rowTemp;
switch (mode)
{
case '[':
case '0':
{
colTemp = currentConsole->cursorX ;
rowTemp = currentConsole->cursorY ;
while(i++ < ((currentConsole->windowHeight * currentConsole->windowWidth) - (rowTemp * currentConsole->consoleWidth + colTemp)))
consolePrintChar(' ');
currentConsole->cursorX = colTemp;
currentConsole->cursorY = rowTemp;
break;
}
case '1':
{
colTemp = currentConsole->cursorX ;
rowTemp = currentConsole->cursorY ;
currentConsole->cursorY = 0;
currentConsole->cursorX = 0;
while (i++ < (rowTemp * currentConsole->windowWidth + colTemp))
consolePrintChar(' ');
currentConsole->cursorX = colTemp;
currentConsole->cursorY = rowTemp;
break;
}
case '2':
{
currentConsole->cursorY = 0;
currentConsole->cursorX = 0;
while(i++ < currentConsole->windowHeight * currentConsole->windowWidth)
consolePrintChar(' ');
currentConsole->cursorY = 0;
currentConsole->cursorX = 0;
break;
}
}
}
//---------------------------------------------------------------------------------
static void consoleClearLine(char mode) {
//---------------------------------------------------------------------------------
int i = 0;
int colTemp;
switch (mode)
{
case '[':
case '0':
{
colTemp = currentConsole->cursorX ;
while(i++ < (currentConsole->windowWidth - colTemp)) {
consolePrintChar(' ');
}
currentConsole->cursorX = colTemp;
break;
}
case '1':
{
colTemp = currentConsole->cursorX ;
currentConsole->cursorX = 0;
while(i++ < ((currentConsole->windowWidth - colTemp)-2)) {
consolePrintChar(' ');
}
currentConsole->cursorX = colTemp;
break;
}
case '2':
{
colTemp = currentConsole->cursorX ;
currentConsole->cursorX = 0;
while(i++ < currentConsole->windowWidth) {
consolePrintChar(' ');
}
currentConsole->cursorX = colTemp;
break;
}
default:
{
colTemp = currentConsole->cursorX ;
while(i++ < (currentConsole->windowWidth - colTemp)) {
consolePrintChar(' ');
}
currentConsole->cursorX = colTemp;
break;
}
}
}
//---------------------------------------------------------------------------------
ssize_t nocash_write(struct _reent *r, void *fd, const char *ptr, size_t len) {
//---------------------------------------------------------------------------------
nocashWrite(ptr,len);
return len;
}
//---------------------------------------------------------------------------------
ssize_t con_write(struct _reent *r,void *fd,const char *ptr, size_t len) {
//---------------------------------------------------------------------------------
PrintConsole *tmpConsole = NULL;
if (r->deviceData) {
// save currentConsole
tmpConsole = currentConsole;
currentConsole = r->deviceData;
}
char chr;
int i, count = 0;
char *tmp = (char*)ptr;
int intensity = 0;
if(!tmp || len<=0) return -1;
i = 0;
while(i<len) {
chr = *(tmp++);
i++; count++;
if ( chr == 0x1b && *tmp == '[' ) {
bool escaping = true;
char *escapeseq = tmp;
int escapelen = 0;
do {
chr = *(tmp++);
i++; count++; escapelen++;
int parameter;
switch (chr) {
/////////////////////////////////////////
// Cursor directional movement
/////////////////////////////////////////
case 'A':
siscanf(escapeseq,"[%dA", ¶meter);
currentConsole->cursorY = (currentConsole->cursorY - parameter) < 0 ? 0 : currentConsole->cursorY - parameter;
escaping = false;
break;
case 'B':
siscanf(escapeseq,"[%dB", ¶meter);
currentConsole->cursorY = (currentConsole->cursorY + parameter) > currentConsole->windowHeight - 1 ? currentConsole->windowHeight - 1 : currentConsole->cursorY + parameter;
escaping = false;
break;
case 'C':
siscanf(escapeseq,"[%dC", ¶meter);
currentConsole->cursorX = (currentConsole->cursorX + parameter) > currentConsole->windowWidth - 1 ? currentConsole->windowWidth - 1 : currentConsole->cursorX + parameter;
escaping = false;
break;
case 'D':
siscanf(escapeseq,"[%dD", ¶meter);
currentConsole->cursorX = (currentConsole->cursorX - parameter) < 0 ? 0 : currentConsole->cursorX - parameter;
escaping = false;
break;
/////////////////////////////////////////
// Cursor position movement
/////////////////////////////////////////
case 'H':
case 'f':
siscanf(escapeseq,"[%d;%df", ¤tConsole->cursorY , ¤tConsole->cursorX );
escaping = false;
break;
/////////////////////////////////////////
// Screen clear
/////////////////////////////////////////
case 'J':
consoleCls(escapeseq[escapelen-2]);
escaping = false;
break;
/////////////////////////////////////////
// Line clear
/////////////////////////////////////////
case 'K':
consoleClearLine(escapeseq[escapelen-2]);
escaping = false;
break;
/////////////////////////////////////////
// Save cursor position
/////////////////////////////////////////
case 's':
currentConsole->prevCursorX = currentConsole->cursorX ;
currentConsole->prevCursorY = currentConsole->cursorY ;
escaping = false;
break;
/////////////////////////////////////////
// Load cursor position
/////////////////////////////////////////
case 'u':
currentConsole->cursorX = currentConsole->prevCursorX ;
currentConsole->cursorY = currentConsole->prevCursorY ;
escaping = false;
break;
/////////////////////////////////////////
// Color scan codes
/////////////////////////////////////////
case 'm':
siscanf(escapeseq,"[%d;%dm", ¶meter, &intensity);
//only handle 30-37,39 and intensity for the color changes
parameter -= 30;
//39 is the reset code
if(parameter == 9){
parameter = 15;
}
else if(parameter > 8){
parameter -= 2;
}
else if(intensity){
parameter += 8;
}
if(parameter < 16 && parameter >= 0){
currentConsole->fontCurPal = parameter << 12;
}
escaping = false;
break;
}
} while (escaping);
continue;
}
consolePrintChar(chr);
}
if (tmpConsole) {
// restore currentConsole
currentConsole = tmpConsole;
}
return count;
}
int con_open(struct _reent *r, void *fileStruct, const char *path, int flags, int mode) {
return 0;
}
int consoleCount = 1; // Since con0 already exists
static devoptab_t dotab_stdout = {
.name = "con0",
.write_r = con_write,
.open_r = con_open,
};
static const devoptab_t dotab_nocash = {
.name = "nocash",
.write_r = nocash_write,
};
extern const devoptab_t dotab_stdnull; // from newlib/libgloss/libsysbase/iosupport.c
//---------------------------------------------------------------------------------
void consoleLoadFont(PrintConsole* console) {
//---------------------------------------------------------------------------------
int i;
u16* palette = BG_PALETTE_SUB;
//check which display is being utilized
if(console->fontBgGfx < BG_GFX_SUB){
palette = BG_PALETTE;
}
if (console->font.bpp == 1) {
const u8* in = (const u8*)console->font.gfx;
u32* out = (u32*)console->fontBgGfx;
for (i = 0; i < console->font.numChars * 8; i ++) {
unsigned cur = *in++;
int j;
u32 temp = 0;
for (j = 0; j < 8; j ++) {
temp |= ((cur&1) * 0xf) << (j*4);
cur >>= 1;
}
*out++ = temp;
}
goto _setUpPalette;
} else if (console->font.bpp == 4) {
if(!console->font.convertSingleColor) {
if(console->font.gfx)
dmaCopy(console->font.gfx, console->fontBgGfx, console->font.numChars * 64 / 2);
if(console->font.pal)
dmaCopy(console->font.pal, palette + console->fontCurPal * 16, console->font.numColors*2);
console->fontCurPal <<= 12;
} else {
for (i = 0; i < console->font.numChars * 16; i++) {
u16 temp = 0;
if(console->font.gfx[i] & 0xF)
temp |= 0xF;
if(console->font.gfx[i] & 0xF0)
temp |= 0xF0;
if(console->font.gfx[i] & 0xF00)
temp |= 0xF00;
if(console->font.gfx[i] & 0xF000)
temp |= 0xF000;
console->fontBgGfx[i] = temp;
}
_setUpPalette:
//set up the palette for color printing
palette[1 * 16 - 1] = RGB15(0,0,0); //30 normal black
palette[2 * 16 - 1] = RGB15(15,0,0); //31 normal red
palette[3 * 16 - 1] = RGB15(0,15,0); //32 normal green
palette[4 * 16 - 1] = RGB15(15,15,0); //33 normal yellow
palette[5 * 16 - 1] = RGB15(0,0,15); //34 normal blue
palette[6 * 16 - 1] = RGB15(15,0,15); //35 normal magenta
palette[7 * 16 - 1] = RGB15(0,15,15); //36 normal cyan
palette[8 * 16 - 1] = RGB15(24,24,24); //37 normal white
palette[9 * 16 - 1 ] = RGB15(15,15,15); //40 bright black
palette[10 * 16 - 1] = RGB15(31,0,0); //41 bright red
palette[11 * 16 - 1] = RGB15(0,31,0); //42 bright green
palette[12 * 16 - 1] = RGB15(31,31,0); //43 bright yellow
palette[13 * 16 - 1] = RGB15(0,0,31); //44 bright blue
palette[14 * 16 - 1] = RGB15(31,0,31); //45 bright magenta
palette[15 * 16 - 1] = RGB15(0,31,31); //46 bright cyan
palette[16 * 16 - 1] = RGB15(31,31,31); //47 & 39 bright white
console->fontCurPal = 15 << 12;
}
} else if(console->font.bpp == 8) {
console->fontCurPal = 0;
if(!console->font.convertSingleColor) {
if(console->font.gfx)
dmaCopy(console->font.gfx, console->fontBgGfx, console->font.numChars * 64);
if(console->font.pal)
dmaCopy(console->font.pal, palette, console->font.numColors*2);
} else {
for(i = 0; i < console->font.numChars * 16; i++) {
u32 temp = 0;
if(console->font.gfx[i] & 0xF)
temp = 255;
if(console->font.gfx[i] & 0xF0)
temp |= 255 << 8;
if(console->font.gfx[i] & 0xF00)
temp |= 255 << 16;
if(console->font.gfx[i] & 0xF000)
temp |= 255 << 24;
((u32*)console->fontBgGfx)[i] = temp;
}
palette[255] = RGB15(31,31,31);
}
}
palette[0] = RGB15(0,0,0);
PrintConsole* tmp = consoleSelect(console);
consoleCls('2');
consoleSelect(tmp);
}
//---------------------------------------------------------------------------------
PrintConsole* consoleInit(PrintConsole* console, int layer,
BgType type, BgSize size,
int mapBase, int tileBase,
bool mainDisplay, bool loadGraphics){
//---------------------------------------------------------------------------------
static bool firstConsoleInit = true;
if(firstConsoleInit) {
devoptab_list[STD_OUT] = &dotab_stdout;
devoptab_list[STD_ERR] = &dotab_stdout;
// make sure con0 also has the new behavior, in case the application
// consoleSelects a different console
dotab_stdout.deviceData = console ? console : currentConsole;
setvbuf(stdout, NULL , _IONBF, 0);
setvbuf(stderr, NULL , _IONBF, 0);
firstConsoleInit = false;
// WARNING: Only 16 devoptab_list slots are set to dotab_stdnull, but AddDevice() iterates
// through to STD_MAX without null-checking... A PR should be put in for this...
// https://github.com/devkitPro/newlib/blob/4cc8767a6067786cbb2da969baff3481bd71461c/libgloss/libsysbase/iosupport.c#L107
// Temporarily, just to be safe, I'll fix that problem here.
for (int i = 16; i < STD_MAX; ++i)
devoptab_list[i] = &dotab_stdnull;
} else if (console) {
// Make a new device for each new console, so that applications can write
// to consoles on different bg layers, just like the Linux VTs.
// The firstConsoleInit code above won't be changed as to not break existing code.
devoptab_t *const dot = malloc(sizeof(devoptab_t));
dot->write_r = con_write;
// Set the name to be `con${consoleNumber}`.
const int consoleNumber = consoleCount++;
dot->name = strdup("con\0\0");
sprintf((char *)dot->name + 3, "%d", consoleNumber);
// Must set an open() callback. newlib returns ENOSYS if not set:
// https://github.com/devkitPro/newlib/blob/4cc8767a6067786cbb2da969baff3481bd71461c/libgloss/libsysbase/open.c#L12
dot->open_r = con_open;
// The important part
dot->deviceData = console;
AddDevice(dot);
}
if(console) {
currentConsole = console;
} else {
console = currentConsole;
}
*currentConsole = defaultConsole;
if(mainDisplay) {
console->bgId = bgInit(layer, type, size, mapBase, tileBase);
} else {
console->bgId = bgInitSub(layer, type, size, mapBase, tileBase);
}
console->fontBgGfx = (u16*)bgGetGfxPtr(console->bgId);
console->fontBgMap = (u16*)bgGetMapPtr(console->bgId);
console->consoleInitialised = 1;
consoleCls('2');
if(loadGraphics)
consoleLoadFont(console);
return currentConsole;
}
//---------------------------------------------------------------------------------
PrintConsole *consoleSelect(PrintConsole* console){
//---------------------------------------------------------------------------------
PrintConsole *tmp = currentConsole;
currentConsole = console;
return tmp;
}
//---------------------------------------------------------------------------------
void consoleSetFont(PrintConsole* console, ConsoleFont* font){
//---------------------------------------------------------------------------------
if(!console) console = currentConsole;
console->font = *font;
consoleLoadFont(console);
}
//---------------------------------------------------------------------------------
void consoleDebugInit(DebugDevice device){
//---------------------------------------------------------------------------------
int buffertype = _IONBF;
switch(device) {
case DebugDevice_NOCASH:
devoptab_list[STD_ERR] = &dotab_nocash;
buffertype = _IOLBF;
break;
case DebugDevice_CONSOLE:
devoptab_list[STD_ERR] = &dotab_stdout;
break;
case DebugDevice_NULL:
devoptab_list[STD_ERR] = &dotab_stdnull;
break;
}
setvbuf(stderr, NULL , buffertype, 0);
}
//---------------------------------------------------------------------------------
// Places the console in a default mode using bg0 of the sub display, and vram c for
// font and map..this is provided for rapid prototyping and nothing more
PrintConsole* consoleDemoInit(void) {
//---------------------------------------------------------------------------------
videoSetModeSub(MODE_0_2D);
vramSetBankC(VRAM_C_SUB_BG);
return consoleInit(NULL, defaultConsole.bgLayer, BgType_Text4bpp, BgSize_T_256x256, defaultConsole.mapBase, defaultConsole.gfxBase, false, true);
}
//---------------------------------------------------------------------------------
static void newRow() {
//---------------------------------------------------------------------------------
currentConsole->cursorY ++;
if(currentConsole->cursorY >= currentConsole->windowHeight) {
int rowCount;
int colCount;
currentConsole->cursorY --;
for(rowCount = 0; rowCount < currentConsole->windowHeight - 1; rowCount++)
for(colCount = 0; colCount < currentConsole->windowWidth; colCount++)
currentConsole->fontBgMap[(colCount + currentConsole->windowX) + (rowCount + currentConsole->windowY) * currentConsole->consoleWidth] =
currentConsole->fontBgMap[(colCount + currentConsole->windowX) + (rowCount + currentConsole->windowY + 1) * currentConsole->consoleWidth];
for(colCount = 0; colCount < currentConsole->windowWidth; colCount++)
currentConsole->fontBgMap[(colCount + currentConsole->windowX) + (rowCount + currentConsole->windowY) * currentConsole->consoleWidth] =
(' ' + currentConsole->fontCharOffset - currentConsole->font.asciiOffset);
}
}
//---------------------------------------------------------------------------------
void consolePrintChar(char c) {
//---------------------------------------------------------------------------------
if (c==0) return;
if(currentConsole->fontBgMap == 0) return;
if(currentConsole->PrintChar)
if(currentConsole->PrintChar(currentConsole, c))
return;
if(currentConsole->cursorX >= currentConsole->windowWidth) {
currentConsole->cursorX = 0;
newRow();
}
switch(c) {
/*
The only special characters we will handle are tab (\t), carriage return (\r), line feed (\n)
and backspace (\b).
Carriage return & line feed will function the same: go to next line and put cursor at the beginning.
For everything else, use VT sequences.
Reason: VT sequences are more specific to the task of cursor placement.
The special escape sequences \b \f & \v are archaic and non-portable.
*/
case 8:
currentConsole->cursorX--;
if(currentConsole->cursorX < 0) {
if(currentConsole->cursorY > 0) {
currentConsole->cursorX = currentConsole->windowX - 1;
currentConsole->cursorY--;
} else {
currentConsole->cursorX = 0;
}
}
currentConsole->fontBgMap[currentConsole->cursorX + currentConsole->windowX + (currentConsole->cursorY + currentConsole->windowY) * currentConsole->consoleWidth] = currentConsole->fontCurPal | (u16)(' ' + currentConsole->fontCharOffset - currentConsole->font.asciiOffset);
break;
case 9:
currentConsole->cursorX += currentConsole->tabSize - ((currentConsole->cursorX)%(currentConsole->tabSize));
break;
case 10:
newRow();
case 13:
currentConsole->cursorX = 0;
break;
default:
currentConsole->fontBgMap[currentConsole->cursorX + currentConsole->windowX + (currentConsole->cursorY + currentConsole->windowY) * currentConsole->consoleWidth] = currentConsole->fontCurPal | (u16)(c + currentConsole->fontCharOffset - currentConsole->font.asciiOffset);
++currentConsole->cursorX ;
break;
}
}
//---------------------------------------------------------------------------------
void consoleClear(void) {
//---------------------------------------------------------------------------------
iprintf("\x1b[2J");
}
//---------------------------------------------------------------------------------
void consoleSetWindow(PrintConsole* console, int x, int y, int width, int height){
//---------------------------------------------------------------------------------
if(!console) console = currentConsole;
console->windowWidth = width;
console->windowHeight = height;
console->windowX = x;
console->windowY = y;
console->cursorX = 0;
console->cursorY = 0;
}