3939#include <cbm.h>
4040#include <errno.h>
4141#include <joystick.h>
42+ #include "ui_colors.h"
4243#include "screen.h"
4344#include "dir.h"
4445#include "base.h"
@@ -69,6 +70,79 @@ static void showMessage(const char *text, uint8_t color);
6970/* definitions */
7071#define EF3_USB_CMD_LEN 12
7172
73+ #define UI_COLOR_TAIL_BYTES 5 /* bytes at the end of dir->name reserved for color IDs */
74+
75+ /* define globals declared in header */
76+ unsigned char g_color1_id = UI_COL_WHITE ;
77+ unsigned char g_color2_id = UI_COL_WHITE ;
78+ unsigned char g_color3_id = UI_COL_PURPLE ;
79+ unsigned char g_color4_id = UI_COL_BLACK ;
80+ unsigned char g_color5_id = UI_COL_BLACK ;
81+
82+ unsigned char ui_color_to_conio (unsigned char id ) {
83+ static const unsigned char map [UI_COL_MAX ] = {
84+ COLOR_BLACK , // 0
85+ COLOR_WHITE , // 1
86+ COLOR_RED , // 2
87+ COLOR_CYAN , // 3
88+ COLOR_PURPLE , // 4
89+ COLOR_GREEN , // 5
90+ COLOR_BLUE , // 6
91+ COLOR_YELLOW , // 7
92+ COLOR_ORANGE , // 8
93+ COLOR_BROWN , // 9
94+ COLOR_LIGHTRED , // 10
95+ COLOR_GRAY1 , // 11
96+ COLOR_GRAY2 , // 12
97+ COLOR_LIGHTGREEN , // 13
98+ COLOR_LIGHTBLUE , // 14
99+ COLOR_GRAY3 // 15
100+ };
101+ return (id < UI_COL_MAX ) ? map [id ] : COLOR_CYAN ;
102+ }
103+
104+
105+ /* Build a printable title string from a Directory, stripping color tail */
106+ static void build_dir_title (const Directory * d , char * out , size_t outsz )
107+ {
108+ /* d->name layout:
109+ [0] = flag (NORMAL_DIR / SEARCH_SUPPORTED / CLEAR_SEARCH)
110+ [1 .. DIR_NAME_LENGTH-UI_COLOR_TAIL_BYTES-1] = visible chars (padded)
111+ [DIR_NAME_LENGTH-UI_COLOR_TAIL_BYTES .. DIR_NAME_LENGTH-1] = color IDs
112+ */
113+ size_t visible_len = (DIR_NAME_LENGTH - UI_COLOR_TAIL_BYTES ) - 1 ; /* skip flag at [0] */
114+ size_t n = visible_len ;
115+ if (outsz == 0 ) return ;
116+ if (n >= outsz ) n = outsz - 1 ;
117+
118+ memcpy (out , d -> name + 1 , n );
119+ out [n ] = 0 ;
120+
121+ /* trim trailing spaces */
122+ while (n && out [n - 1 ] == ' ' ) {
123+ out [-- n ] = 0 ;
124+ }
125+ }
126+
127+
128+ /* pull IDs out of the dir-name tail that firmware filled */
129+ static void ui_sync_from_dirname_tail (const Directory * d ) {
130+ const unsigned char * name = (const unsigned char * )d -> name ;
131+
132+ unsigned char c1 = name [DIR_NAME_LENGTH - 5 ];
133+ unsigned char c2 = name [DIR_NAME_LENGTH - 4 ];
134+ unsigned char c3 = name [DIR_NAME_LENGTH - 3 ];
135+ unsigned char c4 = name [DIR_NAME_LENGTH - 2 ];
136+ unsigned char c5 = name [DIR_NAME_LENGTH - 1 ];
137+
138+ if (c1 < UI_COL_MAX ) g_color1_id = c1 ;
139+ if (c2 < UI_COL_MAX ) g_color2_id = c2 ;
140+ if (c3 < UI_COL_MAX ) g_color3_id = c3 ;
141+ if (c4 < UI_COL_MAX ) g_color4_id = c4 ;
142+ if (c5 < UI_COL_MAX ) g_color5_id = c5 ;
143+ }
144+
145+
72146static bool isC128 = false;
73147
74148static char linebuffer [SCREENW + 1 ];
@@ -93,8 +167,7 @@ static const char menuBar[] = {" " KUNG_FU_FLASH_VER " <F1> Help"};
93167int main (void )
94168{
95169 uint8_t i , tmp , tst ;
96- initScreen (COLOR_BLACK , COLOR_BLACK , TEXTC );
97-
170+ initScreen (COLOR_BLACK , COLOR_BLACK , USER_COLOR2 );
98171 dir = (Directory * )malloc (sizeof (Directory ));
99172 bigBuffer = (uint8_t * )dir ;
100173 pageBuffer = (uint16_t * )dir ;
@@ -282,7 +355,8 @@ static void showTextPage(uint16_t page)
282355
283356 clrscr ();
284357 revers (1 );
285- textcolor (BACKC );
358+
359+ textcolor (USER_COLOR1 );
286360 gotoxy (0 , 0 );
287361 KFF_READ_PTR = 0 ;
288362 for (i = 0 ; i < DIR_NAME_LENGTH ; i ++ )
@@ -292,7 +366,8 @@ static void showTextPage(uint16_t page)
292366 cputc (' ' );
293367 cputsxy (0 , BOTTOM , programBar );
294368 revers (0 );
295- textcolor (TEXTC );
369+
370+ textcolor (USER_COLOR1 );
296371 gotoxy (0 , 1 );
297372
298373 KFF_READ_PTR = pageBuffer [page ];
@@ -387,7 +462,7 @@ static void updateScreen(void)
387462{
388463 clrscr ();
389464 revers (0 );
390- textcolor (BACKC );
465+ textcolor (USER_COLOR1 );
391466 drawFrame ();
392467 revers (1 );
393468 cputsxy (0 , BOTTOM , menuBar );
@@ -396,17 +471,46 @@ static void updateScreen(void)
396471 showDir ();
397472}
398473
474+
399475static uint8_t menuLoop (void )
400476{
401477 uint8_t c , last_selected = 0 ;
402478 uint8_t data , cmd , reply ;
403479
404480 memset (dir , 0 , sizeof (Directory ));
481+
482+ // Send current search buffer first (as before)
483+ kff_send_size_data (searchBuffer , searchLen );
484+
485+ // ---- PREFETCH ONE DIR PAGE TO SYNC COLORS BEFORE DRAWING ----
486+ // Ask firmware for a directory page
487+ cmd = kff_send_reply_progress (REPLY_DIR );
488+
489+ // Handle only the very first response here, enough to sync colors
490+ if (cmd == CMD_READ_DIR ) {
491+ readDir (dir ); // fills dir->name[0..38]
492+ ui_sync_from_dirname_tail (dir ); // <-- get g_color*_id from name tail
493+ } else if (cmd == CMD_READ_DIR_PAGE ) {
494+ // very unlikely on first request, but still safe:
495+ if (!readDirPage (dir )) {
496+ dir -> selected = last_selected ;
497+ }
498+ ui_sync_from_dirname_tail (dir );
499+ } else {
500+ // If firmware sent something else, just proceed (colors stay defaults)
501+ }
502+
503+ // Now that colors are known, draw UI for the first time
405504 updateScreen ();
505+ bgcolor (USER_COLOR4 );
506+ bordercolor (USER_COLOR5 );
507+ showDir ();
508+ waitRelease ();
406509
407510 cmd = CMD_NONE ;
408- kff_send_size_data (searchBuffer , searchLen );
409- reply = REPLY_DIR ;
511+ reply = REPLY_OK ;
512+
513+
410514
411515 while (true)
412516 {
@@ -423,6 +527,9 @@ static uint8_t menuLoop(void)
423527
424528 case CMD_READ_DIR :
425529 readDir (dir );
530+
531+ ui_sync_from_dirname_tail (dir );
532+
426533 showDir ();
427534 waitRelease ();
428535 continue ;
@@ -444,6 +551,8 @@ static uint8_t menuLoop(void)
444551 dir -> selected = dir -> text_elements ;
445552 }
446553
554+ ui_sync_from_dirname_tail (dir );
555+
447556 showDir ();
448557 continue ;
449558
@@ -649,7 +758,7 @@ static uint8_t search(uint8_t c)
649758 printElement (last_selected );
650759 dir -> selected = last_selected ;
651760
652- textcolor (TEXTC );
761+ textcolor (USER_COLOR1 );
653762 revers (1 );
654763 memcpy (inputBuffer , searchBuffer , searchLen );
655764
@@ -846,10 +955,13 @@ static void showDir(void)
846955 }
847956 else
848957 {
849- sprintf (linebuffer , " %-38s " , dir -> name + 1 );
958+ char title [DIR_NAME_LENGTH ];
959+ build_dir_title (dir , title , sizeof (title )); // <-- pass dir here
960+ sprintf (linebuffer , " %-38s " , title );
850961 }
851962
852- textcolor (BACKC );
963+
964+ textcolor (USER_COLOR1 );
853965 revers (1 );
854966 cputsxy (0 , 0 , linebuffer );
855967
@@ -868,10 +980,28 @@ static void help(void)
868980
869981 cputsxy (2 , 2 , "github.com/KimJorgensen/KungFuFlash2" );
870982
871- textcolor (TEXTC );
983+
984+ textcolor (COLOR_GREEN );
985+ cputsxy (10 , 3 , "color mod AtomicRPM" );
986+ textcolor (COLOR_CYAN );
987+ cputsxy (11 , 3 , "o" );
988+ textcolor (COLOR_RED );
989+ cputsxy (12 , 3 , "l" );
990+ textcolor (COLOR_YELLOW );
991+ cputsxy (13 , 3 , "o" );
992+ textcolor (COLOR_ORANGE );
993+ cputsxy (14 , 3 , "r" );
994+ textcolor (COLOR_GREEN );
995+ cputsxy (16 , 3 , "mod AtomicRPM" );
996+
997+
998+ textcolor (BACKC );
999+ cputsxy (20 , 4 , "\xa3\xa3\xa3\xa3\xa3\xa3\xa3\xa3\xa3" );
1000+
1001+ textcolor (COLOR_YELLOW );
8721002 cputsxy (0 , 5 , "<CRSR> or Joy Change selection" );
8731003 cputsxy (0 , 6 , "<RETURN> or Fire Run/Change Dir" );
874- cputsxy (0 , 7 , " + <SHIFT> or Hold Options" );
1004+ cputsxy (0 , 7 , "<RETURN> + <SHIFT> Options" );
8751005 cputsxy (0 , 8 , "<HOME> Root Dir" );
8761006 cputsxy (0 , 9 , "<DEL> or Fire left Dir Up" );
8771007 cputsxy (0 , 10 , "<A-Z> or Fire up Search" );
@@ -884,7 +1014,7 @@ static void help(void)
8841014
8851015 cputsxy (0 , 18 , "Use joystick in port 2" );
8861016
887- textcolor (COLOR_RED );
1017+ textcolor (COLOR_CYAN );
8881018 cputsxy (0 , 20 , "KUNG FU FLASH IS PROVIDED WITH NO" );
8891019 cputsxy (0 , 21 , "WARRANTY OF ANY KIND." );
8901020 textcolor (COLOR_LIGHTRED );
@@ -902,6 +1032,8 @@ static void updateDir(uint8_t last_selected)
9021032 printElement (dir -> selected );
9031033}
9041034
1035+
1036+
9051037static void printDirPage (void )
9061038{
9071039 uint8_t element_no = 0 ;
@@ -918,18 +1050,19 @@ static void printDirPage(void)
9181050 }
9191051}
9201052
1053+
9211054static void printElement (uint8_t element_no )
9221055{
9231056 char * element ;
9241057
9251058 element = dir -> elements [element_no ];
9261059 if (* element ++ == TEXT_ELEMENT )
9271060 {
928- textcolor (WARNC );
1061+ textcolor (WARNC ); // yellow
9291062 }
9301063 else
9311064 {
932- textcolor (TEXTC );
1065+ textcolor (( element_no & 1u ) ? USER_COLOR3 : USER_COLOR2 );
9331066 }
9341067
9351068 if (element_no == dir -> selected )
@@ -950,7 +1083,7 @@ static void showMessage(const char *text, uint8_t color)
9501083 clrscr ();
9511084
9521085 revers (1 );
953- textcolor (BACKC );
1086+ textcolor (BACKC ); // purple
9541087 cputsxy (0 , 0 , " " );
9551088 cputsxy (0 , BOTTOM , programBar );
9561089 revers (0 );
0 commit comments