Skip to content

Instantly share code, notes, and snippets.

@ZeroQI
Created July 10, 2018 07:19
Show Gist options
  • Save ZeroQI/510ff01b740aa6420099d61953a2d342 to your computer and use it in GitHub Desktop.
Save ZeroQI/510ff01b740aa6420099d61953a2d342 to your computer and use it in GitHub Desktop.
i-station V43 loader
#include <stdlib.h>
#include <string.h>
#include <iconv.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_rotozoom.h"
#include "ac97_codec.h"
#include "audio_api.h"
#include "ac97_drv_ioctl.h"
#define MAX_ITEM 256
#define MAX_LENGTH 64
#define MAX_GROUP 7
#define COLS 4
#define ROWS 2
#define EXIT 64
#define RUN 66
const char* version = "Launcher v0.26 flat by ZeroQI";
const SDL_Color TRANSPARENT = {0xFF,0x0F,0xFF,0x00};
const SDL_Color White = {0xFF,0xFF,0xFF,0x00};
const SDL_Color Black = {0x00,0x00,0x00,0x00};
const SDL_Color LEFT = {0xFB,0xFB,0xFF,0x00};
SDL_Surface * screen, * icon[MAX_GROUP][MAX_ITEM], * backround;
TTF_Font * font;
SDL_Event event;
char groups[MAX_GROUP][MAX_LENGTH];
char path[MAX_GROUP][MAX_ITEM][MAX_LENGTH];
char names[MAX_GROUP][MAX_ITEM][MAX_LENGTH];
int total[MAX_GROUP]= {0,0,0,0,0};
int audio_output = 0;
int volume = 25;
int brightness = 50;
void error(char *message)
{ SDL_Surface * surfaceTitle=TTF_RenderUTF8_Blended(font, message, White);
SDL_Rect rect= {abs(480-surfaceTitle->w)/2,100,surfaceTitle->w,surfaceTitle->h};
SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, LEFT.r, LEFT.g, LEFT.b));
SDL_BlitSurface(surfaceTitle, NULL, screen, &rect);
SDL_Flip(screen); //SDL_UpdateRect(sfScreen, 0, 0, 0, 0);
SDL_Delay(5000);
SDL_FreeSurface(surfaceTitle);
}
void init_video()
{ SDL_InitSubSystem(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(480,272,16,SDL_HWSURFACE|SDL_DOUBLEBUF);
SDL_ShowCursor(SDL_DISABLE);
SDL_EventState(SDL_MOUSEBUTTONDOWN,SDL_ENABLE);
SDL_EventState(SDL_KEYDOWN,SDL_ENABLE);
while(SDL_PollEvent(&event));
//sprintf(es,"/mai/fbio brightness %d",brightness);
}
int init()
{ /*** Audio ***/
//audio_api_init();
//audio_device_input_select(INPUT_DAC);
//audio_device_output_ctrl(OUTPUT_EARPHONE, ON); // Earphone on
//audio_device_output_ctrl(OUTPUT_SPEAKER, OFF); // Speaker off
//audio_device_set_volume(25);
//AHInit();
//AHSetAudioPath(PATH_EAR_PHONE);
//AHSetVolume(volume);
//AHSetAudioPath(audio_output);
//AHSetVolume(volume);
//iWriteConfig(&fl, audio_output, volume, brightness, current_tab);
//AHClose();
/*** USB ***/
int iocont = open("/dev/keydrv",O_RDWR|O_NDELAY);// open LED/USB power driver
unsigned char iodata = 0xF8; //Turn on USB host power and turn off LEDs
write(iocont, &iodata, 1); sync(); close(iocont);
init_video();
/*** Video fonts ***/
TTF_Init(); atexit(TTF_Quit);
font=TTF_OpenFont("/mnt/hd/user/gulim.ttf",16);
TF_SetFontStyle(font,TTF_STYLE_BOLD);
/*** Background ***/
backround = SDL_LoadBMP("launcher.bmp");
backround = SDL_DisplayFormat(backround);
}
void free_video()
{ SDL_FreeSurface(screen); screen = NULL;
SDL_QuitSubSystem(SDL_INIT_VIDEO);
}
void free_all()
{ int group;
for(group=0;strcmp(groups[group],"")!=0;group++)
for(--total[group];total[group]>=0;total[group]--)
SDL_FreeSurface(icon[group][ total[group] ]);
SDL_FreeSurface(backround);
free_video();
}
void text(TTF_Font * font, char * text, int x, int y, int w, int h)
{ SDL_Surface * surfaceTitle=TTF_RenderUTF8_Blended(font, ">", Black);
SDL_Rect rect={x, y, w, h};
SDL_BlitSurface(surfaceTitle, NULL, screen, &rect);
SDL_FreeSurface(surfaceTitle);
}
int GenerateProgramList (char *arg) // Find ".run" extension in the folder
{ DIR *dp;
struct dirent *dirp;
char name[MAX_LENGTH], temp[MAX_LENGTH],filename[MAX_LENGTH], group[MAX_LENGTH], *ptr;
int x, group_nb=0;
dp = opendir(arg);
if(dp == NULL) { error("Opendir failed.\n"); return 1; }
strcpy(groups[0],"misc");
while(dirp = readdir(dp))
{ if((strstr(dirp->d_name, ".run")!=NULL))
{ strncpy(filename, dirp->d_name, strlen(dirp->d_name)-4);
filename[strlen(dirp->d_name)-4]='\0';
strcpy(name, filename);
ptr=strchr(filename, '.');
if(ptr==NULL) { strcpy(name, filename);
strcpy(group,"misc");
}
else { strcpy (name, ptr+1);
strncpy(group,filename, ptr-filename);
group[ptr-filename]='\0';
}
x=0; group_nb=-1;
while(group_nb<0)
{ if (strcmp(groups[x],group)==0) group_nb=x;
else if (strcmp(groups[x],"")==0)
{ group_nb=x;
strcpy(groups[x], group);
}
x++;
}
strcpy(path[group_nb][total[group_nb]], filename);
strcpy(names[group_nb][total[group_nb]], name);
sprintf(temp, "_icons/%s.bmp",name);
icon[group_nb][total[group_nb]]=SDL_LoadBMP(temp);
if(icon[group_nb][total[group_nb]]==NULL) icon[group_nb][total[group_nb]]=SDL_LoadBMP("_icons/_default.bmp");
if(icon[group_nb][total[group_nb]]!=NULL) SDL_SetColorKey(icon[group_nb][total[group_nb]], SDL_SRCCOLORKEY, SDL_MapRGB(icon[group_nb][total[group_nb]]->format, TRANSPARENT.r, TRANSPARENT.g, TRANSPARENT.b));
total[group_nb]++;
} }
closedir(dp);
return 0;
}
void DrawCats(int group, int view)
{ SDL_Surface * surfaceTitle, *image;
SDL_Rect rect;
int x;
char temp[MAX_LENGTH];
rect.x= 0;rect.y= 0; rect.w=480;rect.h= 272;
SDL_BlitSurface(backround, NULL, screen, &rect);
surfaceTitle=TTF_RenderUTF8_Blended(font, version, White);
rect.x= 5;rect.y= 2; rect.w=surfaceTitle->w;rect.h=surfaceTitle->h;
SDL_BlitSurface(surfaceTitle, NULL, screen, &rect);
SDL_FreeSurface(surfaceTitle);
for(x=0;strcmp(groups[x],"")!=0;x++)
{ rect.x=0; rect.y=24+31*x; rect.w=80;rect.h=36;
if(x==group)
{ SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, LEFT.r, LEFT.g, LEFT.b));
if(view==0)
{ surfaceTitle=TTF_RenderUTF8_Blended(font, ">", Black);
rect.x= 62;rect.y= 24+31*group+(31-surfaceTitle->h)/2;
rect.w=surfaceTitle->w;rect.h=surfaceTitle->h;
SDL_BlitSurface(surfaceTitle, NULL, screen, &rect);
SDL_FreeSurface(surfaceTitle);
} }
surfaceTitle=TTF_RenderUTF8_Blended(font, groups[x], Black);
rect.x=2; rect.y=24+31*x+(31-surfaceTitle->h)/2; rect.w=80;rect.h=36;
SDL_BlitSurface(surfaceTitle, NULL, screen, &rect);
SDL_FreeSurface(surfaceTitle);
} }
void DrawIcons(int group, int start, int item)
{ int count;
SDL_Surface * image, * surfaceTitle;
SDL_Rect rect;
char temp[MAX_LENGTH];
for(count=start; count<total[group] && count<start+COLS*ROWS; count++)
{ if(icon[group][count]==NULL) { printf("NULL image! \n"); return; }
image = SDL_DisplayFormat(icon[group][count]);
if(count==item)
{ SDL_SetAlpha(image, SDL_SRCALPHA, 80);
rect.x= 94;rect.y= 250; rect.w=470;rect.h=18;
sprintf(temp, "%s.run", path[group][item]);
surfaceTitle=TTF_RenderUTF8_Blended(font, temp, Black);
SDL_BlitSurface(surfaceTitle, NULL, screen, &rect);
SDL_FreeSurface(surfaceTitle);
}
rect.x=(count%COLS)*94+88;rect.y=(count/COLS-(start/COLS))*96+24+6;
rect.w=image->w;rect.h=image->h;
SDL_BlitSurface(image, NULL, screen, &rect);
SDL_FreeSurface(image);
} }
//marlett
void menu_group(int key, int *start, int *item, int *group, int *view, int *done )
{ char temp[MAX_LENGTH];
switch(key)
{ case SDLK_LEFT: break;
case SDLK_RIGHT:(*view)=1; (*item)=0; break;
case SDLK_UP: if(*group-1>=0) (*group)--;
(*item)=-1; (*start)=0; break;
case SDLK_DOWN: if(strcmp(groups[*group+1],"")!=0) (*group)++;
(*item)=-1; (*start)=0; break;
case SDLK_RETURN:(*view)=1; (*item)=0; break;
case SDLK_SPACE:free_video();
system("/mnt/hd/user/launcher2");
init_video(); break;
case SDLK_TAB: (*done)=EXIT; break;
} }
void menu_icons(int key, int *start, int *item, int *group, int *view, int *done)
{ char link_cmd[1024],file[80];
switch(key)
{ case SDLK_UP: if((*item)-COLS>=0) (*item)-=COLS;
if((*item)<(*start)) (*start)-=COLS; break;
case SDLK_DOWN: if((*item)+COLS<total[*group]) (*item)+=COLS;
if((*item)>=(*start)+COLS*ROWS) (*start)+=COLS; break;
case SDLK_LEFT: if((*item)-1>=0) (*item)-=1;
if((*item)<(*start)) (*start)-=COLS; break;
case SDLK_RIGHT:if((*item)+1<total[*group]) (*item)+=1;
if((*item)>=(*start)+COLS*ROWS) (*start)+=COLS; break;
case SDLK_RETURN:free_video();
sprintf(file, "/mnt/hd/user/%s.run", path[*group][*item]);
system(file);
init_video(); break;
case SDLK_SPACE:(*view)=0; (*item)=-1; break;
case SDLK_TAB: (*done=1); break;
} }
void menu_touchpad(int x, int y, int *start, int *item, int *group, int *view, int *done)
{ int count;
/*** Categories ***/
for(count=0;strcmp(groups[count],"")!=0;count++)
{ if(x>=0 && x<80 && y>=24+31*count && y<24+31*count+36)
{ (*group)=count;
(*item)=-1;
} }
/*** Icons ***/
for(count=*start; count<total[*group] && count<(*start)+COLS*ROWS; count++)
{ if( x>= ((count%COLS)*94+88) && x < ((count%COLS)*94+88+94)
&& y >= ((count/COLS-(*start/COLS))*96+24+6)
&& y < ((count/COLS-(*start/COLS))*96+24+6+96) )
{ if(*item==count)
{ event.type=SDL_KEYDOWN;
event.key.keysym.sym=SDLK_RETURN;
}
else
{ (*item)=count; (*view)=1;
} } } }
int main(int argc, char *argv[])
{ int start=0, item=-1, group=0, view=0, done=0;
init();
GenerateProgramList("/mnt/hd/user");
while(done == 0)
{ DrawCats (group, view);
DrawIcons(group, start, item);
SDL_Flip(screen);
while(SDL_PollEvent(&event))
{ if(event.type==SDL_MOUSEBUTTONDOWN) menu_touchpad(event.motion.x, event.motion.y, &start, &item, &group, &view, &done);
if(event.type==SDL_KEYDOWN)
if(view==0) menu_group(event.key.keysym.sym, &start, &item, &group, &view, &done);
else menu_icons(event.key.keysym.sym, &start, &item, &group, &view, &done);
} }
free_all();
return done;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment