implemented most (all?) of MinUI plus required API

removed simple mode and version screen (for now at least)
This commit is contained in:
Shaun Inman 2023-01-13 20:19:25 -05:00
parent 4c5b216c2a
commit cf44917afa
4 changed files with 575 additions and 226 deletions

View file

@ -123,8 +123,33 @@ static struct GFX_Context {
struct fb_fix_screeninfo finfo;
SDL_Surface* screen;
SDL_Surface* assets;
} gfx;
static SDL_Rect asset_rects[] = {
[ASSET_WHITE_PILL] = (SDL_Rect){SCALE4( 1, 1,30,30)},
[ASSET_BLACK_PILL] = (SDL_Rect){SCALE4(33, 1,30,30)},
[ASSET_DARK_GRAY_PILL] = (SDL_Rect){SCALE4(65, 1,30,30)},
[ASSET_BUTTON] = (SDL_Rect){SCALE4( 1,33,20,20)},
[ASSET_PAGE_BG] = (SDL_Rect){SCALE4(64,33,15,15)},
[ASSET_STATE_BG] = (SDL_Rect){SCALE4(23,54, 8, 8)},
[ASSET_PAGE] = (SDL_Rect){SCALE4(39,54, 6, 6)},
[ASSET_BAR] = (SDL_Rect){SCALE4(33,58, 4, 4)},
[ASSET_BAR_BG] = (SDL_Rect){SCALE4(15,55, 4, 4)},
[ASSET_DOT] = (SDL_Rect){SCALE4(33,54, 2, 2)},
[ASSET_BRIGHTNESS] = (SDL_Rect){SCALE4(23,33,19,19)},
[ASSET_VOLUME_MUTE] = (SDL_Rect){SCALE4(44,33,10,16)},
[ASSET_VOLUME] = (SDL_Rect){SCALE4(44,33,18,16)},
[ASSET_BATTERY] = (SDL_Rect){SCALE4(47,51,17,10)},
[ASSET_BATTERY_LOW] = (SDL_Rect){SCALE4(66,51,17,10)},
[ASSET_BATTERY_FILL] = (SDL_Rect){SCALE4(81,33,12, 6)},
[ASSET_BATTERY_FILL_LOW]= (SDL_Rect){SCALE4( 1,55,12, 6)},
[ASSET_BATTERY_BOLT] = (SDL_Rect){SCALE4(81,41,12, 6)},
};
static uint32_t asset_rgbs[ASSET_COLORS];
GFX_Fonts font;
///////////////////////////////
SDL_Surface* GFX_init(void) {
@ -183,8 +208,47 @@ SDL_Surface* GFX_init(void) {
// return screen
gfx.screen = SDL_CreateRGBSurfaceFrom(gfx.map + gfx.buffer_size * gfx.buffer, SCREEN_WIDTH,SCREEN_HEIGHT, SCREEN_DEPTH,SCREEN_PITCH, 0,0,0,0);
RGB_WHITE = SDL_MapRGB(gfx.screen->format, TRIAD_WHITE);
RGB_BLACK = SDL_MapRGB(gfx.screen->format, TRIAD_BLACK);
RGB_LIGHT_GRAY = SDL_MapRGB(gfx.screen->format, TRIAD_LIGHT_GRAY);
RGB_DARK_GRAY = SDL_MapRGB(gfx.screen->format, TRIAD_DARK_GRAY);
asset_rgbs[ASSET_WHITE_PILL] = RGB_WHITE;
asset_rgbs[ASSET_BLACK_PILL] = RGB_BLACK;
asset_rgbs[ASSET_DARK_GRAY_PILL]= RGB_DARK_GRAY;
asset_rgbs[ASSET_BUTTON] = RGB_WHITE;
asset_rgbs[ASSET_PAGE_BG] = RGB_WHITE;
asset_rgbs[ASSET_STATE_BG] = RGB_WHITE;
asset_rgbs[ASSET_PAGE] = RGB_BLACK;
asset_rgbs[ASSET_BAR] = RGB_WHITE;
asset_rgbs[ASSET_BAR_BG] = RGB_BLACK;
asset_rgbs[ASSET_DOT] = RGB_LIGHT_GRAY;
char asset_path[MAX_PATH];
sprintf(asset_path, RES_PATH "/assets@%ix.png", SCREEN_SCALE);
gfx.assets = IMG_Load(asset_path);
font.large = TTF_OpenFont(FONT_PATH, SCALE1(FONT_LARGE));
font.medium = TTF_OpenFont(FONT_PATH, SCALE1(FONT_MEDIUM));
font.small = TTF_OpenFont(FONT_PATH, SCALE1(FONT_SMALL));
font.tiny = TTF_OpenFont(FONT_PATH, SCALE1(FONT_TINY));
return gfx.screen;
}
void GFX_quit(void) {
TTF_CloseFont(font.large);
TTF_CloseFont(font.medium);
TTF_CloseFont(font.small);
TTF_CloseFont(font.tiny);
SDL_FreeSurface(gfx.assets);
GFX_clearAll();
munmap(gfx.map, gfx.map_size);
close(gfx.fb);
SDL_Quit();
}
void GFX_clear(SDL_Surface* screen) {
memset(screen->pixels, 0, gfx.buffer_size);
}
@ -216,11 +280,116 @@ void GFX_flip(SDL_Surface* screen) {
screen->pixels = gfx.map + (gfx.buffer * gfx.buffer_size);
#endif
}
void GFX_quit(void) {
GFX_clearAll();
munmap(gfx.map, gfx.map_size);
close(gfx.fb);
SDL_Quit();
void GFX_blitAsset(int asset, SDL_Rect* src_rect, SDL_Surface* dst, SDL_Rect* dst_rect) {
SDL_Rect* rect = &asset_rects[asset];
SDL_Rect adj_rect = {
.x = rect->x,
.y = rect->y,
.w = rect->w,
.h = rect->h,
};
if (src_rect) {
adj_rect.x += src_rect->x;
adj_rect.y += src_rect->y;
adj_rect.w = src_rect->w;
adj_rect.h = src_rect->h;
}
SDL_BlitSurface(gfx.assets, &adj_rect, dst, dst_rect);
}
void GFX_blitPill(int asset, SDL_Surface* dst, SDL_Rect* dst_rect) {
int x = dst_rect->x;
int y = dst_rect->y;
int w = dst_rect->w;
int h = dst_rect->h;
int r = h / 2;
if (w < h) w = h;
w -= h;
GFX_blitAsset(asset, &(SDL_Rect){0,0,r,h}, dst, &(SDL_Rect){x,y});
x += r;
if (w>0) {
SDL_FillRect(dst, &(SDL_Rect){x,y,w,h}, asset_rgbs[asset]);
x += w;
}
GFX_blitAsset(asset, &(SDL_Rect){r,0,r,h}, dst, &(SDL_Rect){x,y});
}
void GFX_blitBattery(SDL_Surface* dst, SDL_Rect* dst_rect) {
SDL_Rect rect = asset_rects[ASSET_BATTERY];
int x = dst_rect->x;
int y = dst_rect->y;
x += (SCALE1(PILL_SIZE) - (rect.w + SCREEN_SCALE)) / 2;
y += (SCALE1(PILL_SIZE) - rect.h) / 2;
if (POW_isCharging()) {
GFX_blitAsset(ASSET_BATTERY, NULL, dst, &(SDL_Rect){x,y});
GFX_blitAsset(ASSET_BATTERY_BOLT, NULL, dst, &(SDL_Rect){x+SCALE1(3),y+SCALE1(2)});
}
else {
int percent = POW_getBattery();
GFX_blitAsset(percent<=10?ASSET_BATTERY_LOW:ASSET_BATTERY, NULL, dst, &(SDL_Rect){x,y});
rect = asset_rects[ASSET_BATTERY_FILL];
SDL_Rect clip = rect;
clip.w *= percent;
clip.w /= 100;
if (clip.w<=0) return;
clip.x = rect.w - clip.w;
clip.y = 0;
GFX_blitAsset(percent<=20?ASSET_BATTERY_FILL_LOW:ASSET_BATTERY_FILL, &clip, dst, &(SDL_Rect){x+SCALE1(3)+clip.x,y+SCALE1(2)});
}
}
int GFX_getButtonWidth(char* hint, char* button) {
int button_width = 0;
int width;
if (strlen(button)==1) {
button_width += SCALE1(BUTTON_SIZE);
}
else {
button_width += SCALE1(BUTTON_SIZE) / 2;
TTF_SizeUTF8(font.tiny, button, &width, NULL);
button_width += width;
}
button_width += SCALE1(BUTTON_MARGIN);
TTF_SizeUTF8(font.small, hint, &width, NULL);
button_width += width + SCALE1(BUTTON_MARGIN);
return button_width;
}
void GFX_blitButton(char* hint, char*button, SDL_Surface* dst, SDL_Rect* dst_rect) {
SDL_Surface* text;
int ox = 0;
// button
if (strlen(button)==1) {
GFX_blitAsset(ASSET_BUTTON, NULL, dst, dst_rect);
// label
text = TTF_RenderUTF8_Blended(font.medium, button, COLOR_BUTTON_TEXT);
SDL_BlitSurface(text, NULL, dst, &(SDL_Rect){dst_rect->x+(SCALE1(BUTTON_SIZE)-text->w)/2,dst_rect->y+(SCALE1(BUTTON_SIZE)-text->h)/2});
ox += SCALE1(BUTTON_SIZE);
SDL_FreeSurface(text);
}
else {
text = TTF_RenderUTF8_Blended(font.tiny, button, COLOR_BUTTON_TEXT);
GFX_blitPill(ASSET_BUTTON, dst, &(SDL_Rect){dst_rect->x,dst_rect->y,SCALE1(BUTTON_SIZE)/2+text->w,SCALE1(BUTTON_SIZE)});
ox += SCALE1(BUTTON_SIZE)/4;
SDL_BlitSurface(text, NULL, dst, &(SDL_Rect){ox+dst_rect->x,dst_rect->y+(SCALE1(BUTTON_SIZE)-text->h)/2,text->w,text->h});
ox += text->w;
ox += SCALE1(BUTTON_SIZE)/4;
SDL_FreeSurface(text);
}
ox += SCALE1(BUTTON_MARGIN);
// hint text
text = TTF_RenderUTF8_Blended(font.small, hint, COLOR_WHITE);
SDL_BlitSurface(text, NULL, dst, &(SDL_Rect){ox+dst_rect->x,dst_rect->y+(SCALE1(BUTTON_SIZE)-text->h)/2,text->w,text->h});
SDL_FreeSurface(text);
}
///////////////////////////////
@ -507,9 +676,9 @@ void POW_fauxSleep(void) {
GFX_clear(gfx.screen);
PAD_reset();
POW_enterSleep();
// TODO: pause keymon
system("killall -STOP keymon.elf");
POW_waitForWake();
// TODO: resume keymon
system("killall -CONT keymon.elf");
POW_exitSleep();
}
int POW_preventAutosleep(void) {
@ -517,4 +686,19 @@ int POW_preventAutosleep(void) {
}
int POW_isCharging(void) {
return getInt("/sys/class/power_supply/battery/charger_online");
}
int POW_getBattery(void) {
// return getInt("/sys/class/power_supply/battery/capacity"); // this is really inaccurate
int i = getInt("/sys/class/power_supply/battery/voltage_now") / 10000; // ~320-420
i = MIN(MAX(0, i-320), 100); // TODO: smooth this value before returning ala Mini
// eh
// if (i>80) return 100;
// else if (i>60) return 80;
// else if (i>40) return 60;
// else if (i>20) return 40;
// else if (i>10) return 20;
// else if (i>5) return 10;
return i;
}

View file

@ -25,6 +25,43 @@ void LOG_note(int level, const char* fmt, ...);
///////////////////////////////
uint32_t RGB_WHITE;
uint32_t RGB_BLACK;
uint32_t RGB_LIGHT_GRAY;
uint32_t RGB_DARK_GRAY;
enum {
ASSET_WHITE_PILL,
ASSET_BLACK_PILL,
ASSET_DARK_GRAY_PILL,
ASSET_BUTTON,
ASSET_PAGE_BG,
ASSET_STATE_BG,
ASSET_PAGE,
ASSET_BAR,
ASSET_BAR_BG,
ASSET_DOT,
ASSET_COLORS,
ASSET_BRIGHTNESS,
ASSET_VOLUME_MUTE,
ASSET_VOLUME,
ASSET_BATTERY,
ASSET_BATTERY_LOW,
ASSET_BATTERY_FILL,
ASSET_BATTERY_FILL_LOW,
ASSET_BATTERY_BOLT,
};
typedef struct GFX_Fonts {
TTF_Font* large; // menu
TTF_Font* medium; // single char button label
TTF_Font* small; // button hint
TTF_Font* tiny; // multi char button label
} GFX_Fonts;
extern GFX_Fonts font;
SDL_Surface* GFX_init(void);
void GFX_clear(SDL_Surface* screen);
void GFX_clearAll(void);
@ -32,6 +69,13 @@ void GFX_startFrame(void);
void GFX_flip(SDL_Surface* screen);
void GFX_quit(void);
// NOTE: all dimensions should be pre-scaled
void GFX_blitAsset(int asset, SDL_Rect* src_rect, SDL_Surface* dst, SDL_Rect* dst_rect);
void GFX_blitPill(int asset, SDL_Surface* dst, SDL_Rect* dst_rect);
void GFX_blitBattery(SDL_Surface* dst, SDL_Rect* dst_rect);
int GFX_getButtonWidth(char* hint, char* button);
void GFX_blitButton(char* hint, char*button, SDL_Surface* dst, SDL_Rect* dst_rect);
///////////////////////////////
typedef struct SND_Frame {
@ -85,6 +129,7 @@ void POW_powerOff(void);
void POW_fauxSleep(void);
int POW_preventAutosleep(void);
int POW_isCharging(void);
int POW_getBattery(void);
///////////////////////////////

View file

@ -43,10 +43,28 @@
#define RESUME_SLOT_PATH "/tmp/mmenu_slot.txt"
#define AUTO_RESUME_PATH USERDATA_PATH "/.miniui/auto_resume.txt"
#define AUTO_RESUME_SLOT "9"
#define ENABLE_SIMPLE_MODE_PATH USERDATA_PATH "/enable-simple-mode"
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define TRIAD_WHITE 0xff,0xff,0xff
#define TRIAD_BLACK 0x00,0x00,0x00
#define TRIAD_LIGHT_GRAY 0x7f,0x7f,0x7f
#define TRIAD_DARK_GRAY 0x26,0x26,0x26
#define TRIAD_LIGHT_TEXT 0xcc,0xcc,0xcc
#define TRIAD_DARK_TEXT 0x66,0x66,0x66
#define TRIAD_BUTTON_TEXT 0x99,0x99,0x99
#define COLOR_WHITE (SDL_Color){TRIAD_WHITE}
#define COLOR_BLACK (SDL_Color){TRIAD_BLACK}
#define COLOR_LIGHT_TEXT (SDL_Color){TRIAD_LIGHT_TEXT}
#define COLOR_DARK_TEXT (SDL_Color){TRIAD_DARK_TEXT}
#define COLOR_BUTTON_TEXT (SDL_Color){TRIAD_BUTTON_TEXT}
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define SCREEN_SCALE 2 // SCREEN_HEIGHT / 240
#define BASE_WIDTH 320
#define BASE_HEIGHT 240
// SNES (stretched to 4:3)
// #define SCREEN_WIDTH 1024
@ -60,11 +78,24 @@
// #define SCREEN_WIDTH 768
// #define SCREEN_HEIGHT 576
#define SCREEN_DEPTH 16
#define SCREEN_BPP 2
#define SCREEN_PITCH SCREEN_WIDTH * SCREEN_BPP
#define SCREEN_DEPTH 16
#define SCREEN_BPP 2
#define SCREEN_PITCH (SCREEN_WIDTH * SCREEN_BPP)
#define MAIN_ROW_COUNT 7
// all before scale
#define PILL_SIZE 30
#define BUTTON_SIZE 20
#define BUTTON_MARGIN ((PILL_SIZE - BUTTON_SIZE) / 2)
#define SETTINGS_SIZE 4
#define SETTINGS_WIDTH 80
#define MAIN_ROW_COUNT 6 // SCREEN_HEIGHT / (PILL_SIZE * SCREEN_SCALE) - 2 (floor and subtract 1 if not an integer)
#define PADDING 10 // PILL_SIZE / 3 (or non-integer part of the previous calculatiom divided by three)
#define FONT_LARGE 16 // menu
#define FONT_MEDIUM 14 // single char button label
#define FONT_SMALL 12 // button hint
#define FONT_TINY 10 // multi char button label
///////////////////////////////
@ -74,4 +105,9 @@
#define MAX(a, b) (a) > (b) ? (a) : (b)
#define MIN(a, b) (a) < (b) ? (a) : (b)
#define SCALE1(a) ((a)*SCREEN_SCALE)
#define SCALE2(a,b) ((a)*SCREEN_SCALE),((b)*SCREEN_SCALE)
#define SCALE4(a,b,c,d) ((a)*SCREEN_SCALE),((b)*SCREEN_SCALE),((c)*SCREEN_SCALE),((d)*SCREEN_SCALE)
#endif