1-
21#include <stdlib.h>
32#include <string.h>
43#include <reent.h>
@@ -296,7 +295,7 @@ static void __console_clear_line(int line, int from, int to )
296295 if ( !(con = currentConsole ) ) return ;
297296
298297 // Each character is FONT_XSIZE * VI_DISPLAY_PIX_SZ wide
299- const u32 line_width = (to - from + 1 ) * ( FONT_XSIZE * VI_DISPLAY_PIX_SZ ) ;
298+ const u32 line_width = (to - from + 1 ) * FONT_XSIZE * VI_DISPLAY_PIX_SZ ;
300299
301300 unsigned int bgcolor = con -> bg ;
302301
@@ -307,15 +306,15 @@ static void __console_clear_line(int line, int from, int to )
307306 //add the given row & column to the offset & assign the pointer
308307 const u32 offset = __console_get_offset () \
309308 + ((line - 1 + con -> windowY - 1 ) * con -> con_stride * FONT_YSIZE ) \
310- + (( from - 1 + con -> windowX - 1 ) * FONT_XSIZE * VI_DISPLAY_PIX_SZ ) ;
309+ + (from - 1 + con -> windowX - 1 ) * FONT_XSIZE * VI_DISPLAY_PIX_SZ ;
311310
312- p = (u8 * )((u32 )con -> destbuffer + offset );
311+ p = (u8 * )((uintptr_t )con -> destbuffer + offset );
313312
314313 // Clears 1 line of pixels at a time
315314 for (u16 ycnt = 0 ; ycnt < FONT_YSIZE ; ycnt ++ )
316315 {
317316 for (u32 xcnt = 0 ; xcnt < line_width ; xcnt += 4 )
318- * (u32 * )((u32 )p + xcnt ) = bgcolor ;
317+ * (u32 * )((uintptr_t )p + xcnt ) = bgcolor ;
319318
320319 p += con -> con_stride ;
321320 }
@@ -334,13 +333,14 @@ static void __console_clear(void)
334333 }
335334
336335 //get console pointer
337- p = (u8 * )((u32 )con -> destbuffer + __console_get_offset ()) + ((con -> windowY - 1 ) * con -> con_stride * FONT_YSIZE );
336+ p = (u8 * )((uintptr_t )con -> destbuffer + __console_get_offset ()) \
337+ + ((con -> windowY - 1 ) * con -> con_stride * FONT_YSIZE );
338338
339339 // Clears 1 line of pixels at a time
340- for (u16 ycnt = con -> windowY * 16 ; ycnt < ( con -> windowHeight + con -> windowY ) * 16 ; ycnt ++ )
340+ for (u16 ycnt = 0 ; ycnt < con -> windowHeight * 16 ; ycnt ++ )
341341 {
342342 for (u32 xcnt = (con -> windowX - 1 ) * 8 * VI_DISPLAY_PIX_SZ ; xcnt < (con -> windowWidth + con -> windowX - 1 ) * 8 * VI_DISPLAY_PIX_SZ ; xcnt += 4 )
343- * (u32 * )((u32 )p + xcnt ) = bgcolor ;
343+ * (u32 * )((uintptr_t )p + xcnt ) = bgcolor ;
344344
345345 p += con -> con_stride ;
346346 }
@@ -407,7 +407,7 @@ static void __set_default_console(void *destbuffer,int xstart,int ystart, int tg
407407 con -> bg = CONSOLE_COLOR_BLACK ;
408408
409409 con -> flags = 0 ;
410- con -> tabSize = 4 ;
410+ con -> tabSize = 3 ;
411411
412412 currentConsole = con ;
413413
@@ -646,6 +646,10 @@ static void consoleSetColorState(int code)
646646
647647static void consoleHandleColorEsc (int argCount )
648648{
649+ escapeSeq .color .bg = currentConsole -> bg ;
650+ escapeSeq .color .fg = currentConsole -> fg ;
651+ escapeSeq .color .flags = currentConsole -> flags ;
652+
649653 for (int arg = 0 ; arg < argCount ; arg ++ )
650654 {
651655 int code = escapeSeq .args [arg ];
@@ -724,21 +728,19 @@ static void consoleHandleColorEsc(int argCount)
724728 if (escapeSeq .colorArgCount == 3 )
725729 {
726730 escapeSeq .color .bg = single_RGB8_to_YCbCr (escapeSeq .colorArgs [0 ], escapeSeq .colorArgs [1 ], escapeSeq .colorArgs [2 ]);
727- escapeSeq .color .flags |= CONSOLE_FG_CUSTOM ;
731+ escapeSeq .color .flags |= CONSOLE_BG_CUSTOM ;
728732 escapeSeq .state = ESC_BUILDING_UNKNOWN ;
729733 }
730734 default :
731735 break ;
732736 }
733737 }
734738 escapeSeq .argIdx = 0 ;
735- }
736739
737- static void consoleColorApply (void )
738- {
739740 currentConsole -> bg = escapeSeq .color .bg ;
740741 currentConsole -> fg = escapeSeq .color .fg ;
741742 currentConsole -> flags = escapeSeq .color .flags ;
743+
742744}
743745
744746void newRow ()
@@ -748,11 +750,10 @@ void newRow()
748750 /* if bottom border reached, scroll */
749751 if ( currentConsole -> cursorY > currentConsole -> windowHeight )
750752 {
751- const u8 * console = (u8 * )((u32 )currentConsole -> destbuffer + __console_get_offset ()) \
753+ u8 * ptr = (u8 * )((u32 )currentConsole -> destbuffer + __console_get_offset ()) \
754+ + (currentConsole -> windowX - 1 ) * FONT_XSIZE * VI_DISPLAY_PIX_SZ \
752755 + ((currentConsole -> windowY - 1 ) * currentConsole -> con_stride * FONT_YSIZE );
753756
754- //copy lines upwards
755- u8 * ptr = (u8 * )console ;
756757 for (u32 ycnt = 0 ; ycnt < ((currentConsole -> windowHeight - 1 ) * FONT_YSIZE ); ycnt ++ )
757758 {
758759 memmove (ptr , ptr + currentConsole -> con_stride * FONT_YSIZE , currentConsole -> windowWidth * FONT_XSIZE * VI_DISPLAY_PIX_SZ );
@@ -767,6 +768,8 @@ void newRow()
767768
768769void consolePrintChar (int c )
769770{
771+ int tabspaces ;
772+
770773 if (c == 0 ) return ;
771774
772775 switch (c ) {
@@ -784,7 +787,7 @@ void consolePrintChar(int c)
784787
785788 if (currentConsole -> cursorX < 1 ) {
786789 if (currentConsole -> cursorY > 1 ) {
787- currentConsole -> cursorX = currentConsole -> windowWidth - 1 ;
790+ currentConsole -> cursorX = currentConsole -> windowWidth ;
788791 currentConsole -> cursorY -- ;
789792 } else {
790793 currentConsole -> cursorX = 1 ;
@@ -795,7 +798,10 @@ void consolePrintChar(int c)
795798 break ;
796799
797800 case 9 :
798- currentConsole -> cursorX += currentConsole -> tabSize - ((currentConsole -> cursorX )%(currentConsole -> tabSize ));
801+ tabspaces = currentConsole -> tabSize - ((currentConsole -> cursorX - 1 ) % currentConsole -> tabSize );
802+ if (currentConsole -> cursorX + tabspaces > currentConsole -> windowWidth )
803+ tabspaces = currentConsole -> windowWidth - currentConsole -> cursorX ;
804+ for (int i = 0 ; i < tabspaces ; i ++ ) consolePrintChar (' ' );
799805 break ;
800806 case 10 :
801807 newRow ();
@@ -975,7 +981,6 @@ ssize_t __console_write(struct _reent *r,void *fd,const char *ptr, size_t len)
975981 if (escapeSeq .argIdx == 0 && !escapeSeq .hasArg ) escapeSeq .args [escapeSeq .argIdx ++ ] = 0 ;
976982 if (escapeSeq .hasArg ) escapeSeq .argIdx ++ ;
977983 consoleHandleColorEsc (escapeSeq .argIdx );
978- consoleColorApply ();
979984 escapeSeq .state = ESC_NONE ;
980985 break ;
981986 default :
0 commit comments