Advertisement
Guest User

Wiireader desastre

a guest
Mar 13th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 65.93 KB | None | 0 0
  1. /*  Wiireader v1.1
  2.     Copyright (C) 2008 Francisco Muñoz "Hermes" (www.entuwii.net)
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  17.  
  18. */
  19.  
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <malloc.h>
  26. #include <math.h>
  27.  
  28. #include "libs/jpeglib/jpeglib.h"
  29. #include "libs/libpng/pngu/pngu.c"
  30.  
  31. #include <setjmp.h>
  32.  
  33. #include <ogcsys.h>
  34. #include <gccore.h>
  35. #include "ogc/lwp_threads.h"
  36. #include <wiiuse/wpad.h>
  37. #include <fat.h>
  38. #include <fcntl.h>
  39. #include <dirent.h>
  40. #include <sys/stat.h>
  41. #include <sys/statvfs.h>
  42. #include "unistd.h"
  43. #include <ogc/conf.h>
  44.  
  45. #include "screen.h"
  46. #include "textreader.h"
  47. //
  48. //#include "asnd.h"
  49. #include "audioplayer.h"
  50. #include "unzip.h"
  51.  
  52. #include "icons.h"
  53.  
  54.  
  55.  
  56. #define ticks_to_msecs(ticks)     ((u32)((ticks)/(TB_TIMER_CLOCK)))
  57.  
  58. extern unsigned gettick();
  59.  
  60. #define THREAD_PRIORITY() LWP_SetThreadPriority(LWP_GetSelf(),40)
  61.  
  62. int is_16_9=0; // screen format 16:9 ?
  63.  
  64. /*
  65. int fprintf(FILE *a,const char *s, ...)
  66. {
  67.     exit(0);
  68. }
  69.  
  70. int printf(const char *s, ...)
  71. {
  72.     exit(0);
  73. }
  74. */
  75.  
  76.  
  77. //extern char *debug_str;
  78.  
  79. char path_file[258]="sd:/";
  80. char path_txt_cfg[258]="sd:/apps/wiireader/textreader.cfg";
  81. char path_cfg[258]="sd:/apps/wiireader/textreader.cfg";
  82.  
  83. static char extern_char_set[28672];
  84.  
  85. unsigned char temp_buffer[32768]; // buffer temporal
  86.  
  87. unsigned new_key=0,old_key=0;
  88.  
  89.  
  90. int exit_by_reset=0;
  91.  
  92. int return_reset=2;
  93.  
  94. void reset_call() {exit_by_reset=return_reset;}
  95. void power_call() {exit_by_reset=3;}
  96.  
  97.  
  98. #define WII_A 16
  99. #define WII_B 32
  100.  
  101. #define WII_MINUS 64
  102. #define WII_PLUS 128
  103.  
  104. #define WII_HOME 256
  105. #define WII_START 512
  106.  
  107. #define WII_ONE 1024
  108. #define WII_TWO 2048
  109.  
  110.  
  111. #define WII_RIGHT 2
  112. #define WII_LEFT  1
  113. #define WII_UP    4
  114. #define WII_DOWN  8
  115.  
  116.  
  117. #define QTDEN(a,b)  ((a & (b))==(b))
  118.  
  119.  
  120. unsigned wii_nKeys=0;
  121.  
  122.  
  123. static int valid_keys=0;
  124.  
  125. void wpad_callback(s32 chan, const WPADData *data)
  126. {
  127. if(WPAD_ReadEvent(chan, (WPADData * )data)<0)  valid_keys=0;
  128.  
  129. }
  130.  
  131. unsigned wii_joystick_read(int sel)
  132. {
  133. u32 type[4]={0,0,0,0};
  134. u32 butt=0;
  135. s32 retj[4]={-1,-1,-1,-1};
  136.  
  137. int n;
  138.  
  139. unsigned ret=0;
  140. static expansion_t w_exp;
  141.  
  142. int is_readed=0;
  143.  
  144.  
  145.        
  146.     //debug_str="WPAD_ScanPads()";
  147.  
  148.     DI_AudioThread(1);
  149.     valid_keys=1;
  150.     n=WPAD_ReadPending(WPAD_CHAN_ALL, wpad_callback);//WPAD_ScanPads();
  151.     DI_AudioThread(0);
  152.  
  153. if(n<WPAD_ERR_NONE || !valid_keys) {return 0;}
  154.    
  155.     //debug_str="wii_joystick_read()";
  156.  
  157.     if(exit_by_reset)
  158.         {
  159.         exit(0);
  160.         }
  161.  
  162. DI_AudioThread(1);
  163. // lee los 4 wiimotes
  164. for(n=0;n<4;n++)
  165.     retj[n]=WPAD_Probe(n, &type[n]);
  166.  
  167.  
  168.    
  169.     for(n=0;n<4;n++)
  170.     {
  171.     if(retj[n]>=0)
  172.         {
  173.         butt=WPAD_ButtonsHeld(n);
  174.         is_readed=1;
  175.  
  176.         if(butt & WPAD_BUTTON_HOME) ret|=WII_HOME;
  177.  
  178.    
  179.  
  180.             if(type[n]==WPAD_EXP_NUNCHUK)
  181.                 {
  182.                 WPAD_Expansion(n,&w_exp);
  183.                 if(w_exp.nunchuk.js.pos.y<(128-48)) ret|=WII_DOWN;
  184.                 if(w_exp.nunchuk.js.pos.y>(128+48)) ret|=WII_UP;
  185.                 if(w_exp.nunchuk.js.pos.x<(128-48)) ret|=WII_LEFT;
  186.                 if(w_exp.nunchuk.js.pos.x>(128+48)) ret|=WII_RIGHT;
  187.  
  188.                 if(butt & WPAD_NUNCHUK_BUTTON_C) ret|=WII_A;
  189.                 if(butt & WPAD_NUNCHUK_BUTTON_Z) ret|=WII_B;
  190.                 }
  191.  
  192.             if(butt & WPAD_BUTTON_A) ret|=WII_A;
  193.             if(butt & WPAD_BUTTON_B) ret|=WII_B;
  194.  
  195.             if(butt & WPAD_BUTTON_1) ret|=WII_ONE;
  196.             if(butt & WPAD_BUTTON_2) ret|=WII_TWO;
  197.  
  198.             if(butt & WPAD_BUTTON_UP)   ret|=WII_UP;
  199.             if(butt & WPAD_BUTTON_DOWN) ret|=WII_DOWN;
  200.  
  201.             if(butt & WPAD_BUTTON_RIGHT) ret|=WII_RIGHT;
  202.             if(butt &  WPAD_BUTTON_LEFT) ret|=WII_LEFT;
  203.            
  204.  
  205.             if(butt & WPAD_BUTTON_MINUS) ret|=WII_MINUS;
  206.             if(butt & WPAD_BUTTON_PLUS) ret|=WII_PLUS;
  207.            
  208.        
  209.         }
  210.     }
  211. DI_AudioThread(0);
  212. if(!is_readed) return wii_nKeys;   
  213.  
  214. //debug_str="Undef";
  215. return ret;
  216. }
  217.  
  218.  
  219.  
  220. char * get_name(char *name);
  221.  
  222.  
  223.  
  224.  
  225. int volume=204;
  226.  
  227.  
  228.  
  229. ////////////////////////////////////////////////////////////////////////
  230.  
  231.  
  232. /***************************************************************************/
  233. // seleccion de ficheros
  234. /***************************************************************************/
  235.  
  236.  
  237.  
  238. int nfiles=0;
  239. int ndirs=0;
  240.  
  241. #define MAX_ENTRY 512
  242.  
  243. struct _files
  244. {
  245.     int size;
  246.     char name[258];
  247.     char is_directory;
  248.     char is_selected;
  249. } files[MAX_ENTRY],swap_file;
  250.  
  251.  
  252. int njpg_files=0,npng_files=0,nmusic_files=0,play_file=-1,current_jpg=-1,current_png=-1;
  253. int music_playing=0, music_loop=0;
  254.  
  255. struct _play_files
  256. {
  257.     char name[258];
  258. } jpg_files[MAX_ENTRY], png_files[MAX_ENTRY], music_files[MAX_ENTRY], txt_file, swap_music;
  259.  
  260.  
  261.  
  262. // textreader
  263.  
  264. char *txt_file_mem=NULL;
  265. int txt_file_len=0;
  266.  
  267.  
  268. void music_shuffle()
  269. {
  270. int n,k;
  271. int s;
  272.     if(nmusic_files<=1) return;
  273.  
  274. for(s=0;s<10;s++)
  275.     for(n=0;n<nmusic_files;n++)
  276.         {
  277.         k=((rand() & 511)* nmusic_files)/512;
  278.         swap_music= music_files[n];
  279.         music_files[n]=music_files[k];
  280.         music_files[k]= swap_music;
  281.         }
  282.  
  283.     StopAudio();music_playing=0;play_file=-1;
  284.  
  285. }
  286.  
  287. // test_music_file: mode 0 -> Activa flag si fichero está en la lista; mode 1-> añade o borra fichero
  288.  
  289. void test_music_file(int index, int mode)
  290. {
  291. int n;
  292. int test;
  293.  
  294. //if(index>=nfiles) return;
  295. if(files[index].is_directory) {files[index].is_selected=0;return;}
  296.  
  297.     test=0;
  298.     for(n=0;n<nmusic_files;n++)
  299.         {
  300.         if(!strcmp(files[index].name, music_files[n].name))
  301.             {
  302.             test=1;break;
  303.             }
  304.         }
  305.     if(mode==0)
  306.         {
  307.         if(test)
  308.             files[index].is_selected=1;
  309.         else
  310.             files[index].is_selected=0;
  311.         }
  312.     if(mode==1)
  313.         {
  314.         if(!test)
  315.             {
  316.             if(nmusic_files<MAX_ENTRY) {files[index].is_selected=1;memcpy(music_files[nmusic_files].name,files[index].name,258);nmusic_files++;}
  317.             }
  318.         else
  319.             {
  320.             files[index].is_selected=0;
  321.             if(nmusic_files>0) nmusic_files--;
  322.             for(;n<nmusic_files;n++) {music_files[n]=music_files[n+1];}
  323.             }
  324.         }
  325. }
  326.  
  327. int loop_music_playing()
  328. {
  329. int n;
  330.  
  331.     if(play_file>=0 && music_playing)
  332.         {
  333.         n=StatusAudio();
  334.         if(n!=AUDIO_STATUS_RUNNING && n!=AUDIO_STATUS_PAUSED)
  335.             {
  336.             play_file++;
  337.             if(music_loop) if(play_file>=nmusic_files) play_file=0;
  338.             if(play_file<nmusic_files) {music_playing=1;if(PlayAudio(music_files[play_file].name, 0, 0)<0) music_playing=0; return 1;}
  339.             else {StopAudio();music_playing=0;play_file=-1;}
  340.             }
  341.         }
  342. return 0;
  343. }
  344.  
  345. int is_ext(char *a,char *b) // compara extensiones
  346. {
  347. int n,m;
  348.  
  349.     m=0;n=0;
  350.     while(a[n]!=0) {if(a[n]=='.') m=n; n++;}
  351.  
  352.     n=0;
  353.     while(b[n]!=0) {
  354.                    unsigned char t1,t2;
  355.                    t1=a[m];t2=b[n];
  356.                    if(t1>='A' && t1<='Z') t1+=32;
  357.                    if(t2>='A' && t2<='Z') t2+=32;
  358.                    if(t1!=t2) return 0; m++; n++;
  359.                    }
  360.  
  361.     if(a[m]==0) return 1;
  362.  
  363. return 0;
  364. }
  365.  
  366. char * get_name_short(char *name) // devuelve el nombre del fichero recortado a 36 caracteres
  367. {
  368. #define MAX_STR 36
  369. #define BRK_STR 17
  370.  
  371. static unsigned char name2[MAX_STR+4];
  372.  
  373. int n,m,l;
  374.  
  375.  
  376. m=0;n=-1;
  377. while(name[m]!=0) {if(name[m]=='/') n=m;m++;} // elimina la parte de directorio
  378. n++;
  379.  
  380. l=n;
  381. while(name[l]!=0) l++;
  382.  
  383. l-=n;
  384. if(l>=MAX_STR)
  385.     {
  386.     memcpy(name2,&name[n],BRK_STR);
  387.     name2[BRK_STR]='.';
  388.     name2[BRK_STR+1]='.';
  389.     n+=(l-MAX_STR)+(BRK_STR+2);
  390.  
  391.     for(m=BRK_STR+2;m<MAX_STR;m++)
  392.         {
  393.         name2[m]=name[n];
  394.         if(name[n]==0) break;
  395.         n++;
  396.         }
  397.     }
  398. else
  399.     {
  400.     for(m=0;m<MAX_STR;m++)
  401.         {
  402.         name2[m]=name[n];
  403.         if(name[n]==0) break;
  404.         n++;
  405.         }
  406.     }
  407.     name2[m]=0;
  408.  
  409. #undef MAX_STR
  410. #undef BRK_STR
  411.  
  412. return ((char *)name2);
  413. }
  414.  
  415. char * get_name_short_fromUTF8(char *name) // devuelve el nombre del fichero recortado a 36 caracteres
  416. {
  417. #define MAX_STR 36
  418. #define BRK_STR 17
  419.  
  420. static unsigned char name2[MAX_STR+4];
  421.  
  422. int n,m,l,s;
  423.  
  424.  
  425. m=0;n=-1;
  426. while(name[m]!=0) {if(name[m]=='/') n=m;m++;} // elimina la parte de directorio
  427. n++;
  428.  
  429. l=n;
  430. while(name[l]!=0) l++;
  431.  
  432. l-=n;
  433. if(l>=MAX_STR)
  434.     {
  435.     s=n;
  436.     for(m=0;m<BRK_STR;m++)
  437.         {
  438.         if((name[s] & 0xc0)==0xc0 && (name[s+1] & 0xc0)==0x80)
  439.             {
  440.             name2[m]= (((name[s] & 3)<<6) | (name[s+1] & 63));
  441.             s++;
  442.             }
  443.         else name2[m]=name[s];
  444.         s++;
  445.         }
  446.    
  447.     //memcpy(name2,&name[n],BRK_STR);
  448.     name2[m]='.';
  449.     name2[m+1]='.';
  450.     m+=2;
  451.     n+=(l-MAX_STR)+(m);
  452.  
  453.     for(;m<MAX_STR;m++)
  454.         {
  455.         if((name[n] & 0xc0)==0xc0 && (name[n+1] & 0xc0)==0x80)
  456.             {
  457.             name2[m]= (((name[n] & 3)<<6) | (name[n+1] & 63));
  458.             n++;
  459.             }
  460.         else name2[m]=name[n];
  461.  
  462.         if(name[n]==0) break;
  463.         n++;
  464.         }
  465.     }
  466. else
  467.     {
  468.     for(m=0;m<MAX_STR;m++)
  469.         {
  470.         if((name[n] & 0xc0)==0xc0 && (name[n+1] & 0xc0)==0x80)
  471.             {
  472.             name2[m]= (((name[n] & 3)<<6) | (name[n+1] & 63));
  473.             n++;
  474.             }
  475.         else name2[m]=name[n];
  476.         if(name[n]==0) break;
  477.         n++;
  478.         }
  479.     }
  480.     name2[m]=0;
  481.  
  482. #undef MAX_STR
  483. #undef BRK_STR
  484.  
  485. return ((char *)name2);
  486. }
  487.  
  488. void char_to_utf8(char *s, char *d)
  489. {
  490. u8 *c= (u8 *) s;
  491.  
  492. while(*c!=0)
  493.     {
  494.     if((*c & 0xc0)==0xc0 && (*(c+1) & 0xc0)==0x80) // esto es UTF-8!
  495.         {
  496.         *d++=*c++;
  497.         *d++=*c;
  498.         }
  499.     else
  500.     if(*c>=0x80) // convierte a UTF-8
  501.         {
  502.         *d++= 192 | (*c>>6);
  503.         *d++= 128 | (*c & 63);
  504.         }
  505.     else *d++=*c;
  506.  
  507.     c++;
  508.     }
  509. *d=0;
  510. }
  511.  
  512. void utf8_to_char(char *s, char *d)
  513. {
  514. u8 *c= (u8 *) s;
  515.  
  516. while(*c!=0)
  517.     {
  518.    
  519.     if((*c & 0xc0)==0xc0 && (*(c+1) & 0xc0)==0x80)
  520.         {
  521.         *d++= (((*c & 3)<<6) | (*(c+1) & 63));
  522.         c++;
  523.         }
  524.     else *d++=*c;
  525.  
  526.     c++;
  527.     }
  528. *d=0;
  529. }
  530.  
  531. char * get_name(char *name) // devuelve el nombre del fichero completo
  532. {
  533. static unsigned char name2[256];
  534.  
  535. int n,m;
  536.  
  537.     m=0;n=-1;
  538.  
  539.     while(name[m]!=0) {if(name[m]=='/') n=m;m++;}
  540.     n++;
  541.  
  542.     for(m=0;m<255;m++)
  543.         {
  544.         name2[m]=name[n];
  545.         if(name[n]==0) break;
  546.         n++;
  547.         }
  548.  
  549.     name2[m]=0;
  550.  
  551. return ((char *) name2);
  552. }
  553.  
  554. char * get_name_from_UTF8(char *name) // devuelve el nombre del fichero completo
  555. {
  556. static unsigned char name2[256];
  557.  
  558. int n,m;
  559.  
  560.     m=0;n=-1;
  561.  
  562.     while(name[m]!=0) {if(name[m]=='/') n=m;m++;}
  563.     n++;
  564.  
  565.     for(m=0;m<255;m++)
  566.         {
  567.         if((name[n] & 0xc0)==0xc0 && (name[n+1] & 0xc0)==0x80)
  568.             {
  569.             name2[m]= (((name[n] & 3)<<6) | (name[n+1] & 63));
  570.             n++;
  571.             }
  572.         else
  573.             name2[m]=name[n];
  574.  
  575.         if(name[n]==0) break;
  576.         n++;
  577.         }
  578.  
  579.     name2[m]=0;
  580.  
  581. return ((char *) name2);
  582. }
  583.  
  584. char * LoadZipTxt(char *name, int *fsize)
  585. {
  586. unz_global_info gi;
  587. unzFile uf;
  588. int error;
  589. int n;
  590. char zip_file[256];
  591. unz_file_info zip_file_info;
  592.  
  593. char *mem=NULL;
  594.  
  595. *fsize=0;
  596.  
  597.     uf = unzOpen (name);
  598.  
  599.     if (uf == NULL) return NULL;
  600.  
  601.     error = unzGetGlobalInfo (uf, &gi);
  602.     if(error != UNZ_OK)  {unzClose(uf);return NULL;}
  603.  
  604.     for (n=0; n<gi.number_entry; n++)
  605.         {
  606.           error = unzGetCurrentFileInfo (uf, &zip_file_info, zip_file,
  607.                        sizeof (zip_file), NULL, 0, NULL, 0);
  608.  
  609.           if(error != UNZ_OK)  {unzClose(uf);return NULL;}
  610.  
  611.           if (is_ext(zip_file, ".txt")) break;
  612.            
  613.           if (n < (gi.number_entry-1))
  614.             {
  615.             error = unzGoToNextFile (uf);
  616.             if(error != UNZ_OK)  {unzClose(uf);return NULL;}
  617.             }
  618.         }
  619.     //
  620.  
  621.     mem =malloc(zip_file_info.uncompressed_size+0x2000);
  622.     if (mem == NULL) {unzClose(uf);return NULL;}
  623.  
  624.     error = unzOpenCurrentFile (uf);
  625.     if(error != UNZ_OK)  {free(mem);mem=0;unzClose(uf);return NULL;}
  626.  
  627.     *fsize=zip_file_info.uncompressed_size;
  628.     n=0;
  629.     while(n<zip_file_info.uncompressed_size)
  630.         {
  631.         error = unzReadCurrentFile (uf, mem+n, zip_file_info.uncompressed_size-n);
  632.         if (error <= 0) break;
  633.  
  634.         n+=error;
  635.         }
  636.      
  637.  
  638.      if(error <0)  {*fsize=0;free(mem);mem=0;unzCloseCurrentFile(uf);unzClose(uf);return NULL;}
  639.  
  640.      error = unzCloseCurrentFile (uf);
  641.      if(error != UNZ_OK)  {*fsize=0;unzClose(uf);free(mem);mem=0;return NULL;}
  642.  
  643. return mem;
  644. }
  645.  
  646.  
  647.  
  648. int have_device=0;
  649.  
  650. int current_device=0;
  651.  
  652.  
  653. void read_list_file(unsigned char *dirname, int flag)
  654. {
  655. DIR *fd;
  656.  
  657. static char namefile[256*4];
  658.  
  659. static struct stat filestat;
  660. //struct dirent* temp = NULL;
  661.  
  662.  
  663. static char swap[257];
  664.  
  665. int n,m,f;
  666.  
  667. if(!dirname) sprintf(swap, "%s", path_file);
  668. else
  669.     {
  670.     sprintf(path_file, "%s", dirname);
  671.  
  672.     n=0;while(dirname[n]!=0) n++;
  673.     if(dirname[n-1]=='/') // esto solo pasa si es directorio raiz
  674.         {sprintf(swap, "%s", dirname);dirname=0;}
  675.     else  
  676.         {
  677.         sprintf(swap, "%s/", dirname);
  678.         if(swap[n-1]=='.')
  679.             {n--;while(swap[n]!='/') n--;n--;while(swap[n]!='/') n--; if(swap[n-1]==':') dirname=0; n++;swap[n]=0;} // para volver al directorio anterior
  680.         }
  681.     }
  682.  
  683. ndirs=0;
  684. nfiles = 0;
  685. files[nfiles].is_selected=0;
  686. if(!dirname)
  687.     {
  688.    
  689.     if(current_device==0 && (have_device & 2)!=0)
  690.         {
  691.         sprintf(files[nfiles].name, ".[USB]");
  692.         ndirs++;files[nfiles].is_directory=1;
  693.         files[nfiles].is_selected=0;
  694.         nfiles++;
  695.         }
  696.     if(current_device==1 && (have_device & 1)!=0)
  697.         {
  698.         sprintf(files[nfiles].name, ".[SD]");
  699.         ndirs++;files[nfiles].is_directory=1;
  700.         files[nfiles].is_selected=0;
  701.         nfiles++;
  702.         }
  703.     }
  704.  
  705.     fd = opendir(swap);
  706.  
  707.     if(fd != NULL)
  708.         {
  709.  
  710.    
  711.         while(nfiles < MAX_ENTRY)
  712.             {
  713.             files[nfiles].is_directory=0;
  714.             files[nfiles].is_selected=0;
  715.  
  716.             //if(readdir(fd, namefile, &filestat)!=0) break;
  717.              /*snprintf(fullpath, sizeof(fullpath), "%s/%s", path, temp->d_name);
  718.     if(stat( fullpath, &filestat) == 0) {
  719.             if(dirnext( fd, namefile, &filestat) != 0) break;*/
  720.             if(namefile[0]=='.' && namefile[1]==0) continue;
  721.            
  722.             sprintf(files[nfiles].name, "%s%s", swap, namefile);
  723.        
  724.             if((filestat.st_mode & S_IFDIR))
  725.                 {
  726.                 files[nfiles].size=0;
  727.                 ndirs++;files[nfiles].is_directory=1;
  728.                 }
  729.             else
  730.                 {
  731.                 if(flag==0)
  732.                     {
  733.                     if(!(is_ext(files[nfiles].name,".mp3") || is_ext(files[nfiles].name,".ogg") || is_ext(files[nfiles].name,".m3u") )) continue;
  734.                     if(!is_ext(files[nfiles].name,".m3u")) test_music_file(nfiles, 0);
  735.                     }
  736.  
  737.                 if(flag==1)
  738.                     if(!(is_ext(files[nfiles].name,".jpg") || is_ext(files[nfiles].name,".jpeg") || is_ext(files[nfiles].name,".png"))) continue;
  739.  
  740.                 if(flag==2)
  741.                     if(!(is_ext(files[nfiles].name,".txt") || is_ext(files[nfiles].name,".zip"))) continue;
  742.  
  743.                 files[nfiles].size=filestat.st_size;
  744.                 }
  745.            
  746.             nfiles++;
  747.             }
  748.    
  749.     closedir(fd);  
  750.     }
  751.    
  752.   if(!nfiles)
  753.         {
  754.        
  755.         if(current_device==0 && (have_device & 2)!=0)
  756.             {
  757.             sprintf(files[nfiles].name, ".[USB]");
  758.             ndirs++;files[nfiles].is_directory=1;
  759.             files[nfiles].is_selected=0;
  760.             nfiles++;
  761.             }
  762.         if(current_device==1 && (have_device & 1)!=0)
  763.             {
  764.             sprintf(files[nfiles].name, ".[SD]");
  765.             ndirs++;files[nfiles].is_directory=1;
  766.             files[nfiles].is_selected=0;
  767.             nfiles++;
  768.             }
  769.            
  770.         }
  771.  
  772.     for(n=0;n<(nfiles);n++) //ordena
  773.     for(m=n+1;m<nfiles;m++)
  774.         {
  775.         f=0;
  776.            
  777.         if(files[m].is_directory==1 && files[n].is_directory==0) f=1;
  778.         if(f==0)
  779.             if(files[m].is_directory==files[n].is_directory)
  780.                 if(strcmp(files[m].name,files[n].name)<0) f=1;
  781.         if(f)
  782.             {
  783.             f=0;
  784.             swap_file=files[m];
  785.             files[m]=files[n];
  786.             files[n]=swap_file;
  787.             }
  788.         }
  789.  
  790. }
  791.  
  792.  
  793. void fun_exit()
  794. {
  795. int m=0;
  796.  
  797.     Screen_flip();
  798.     autocenter=0;
  799.     sizeletter=1;
  800.  
  801.         m=SCR_HEIGHT/2;
  802.  
  803.         SetTexture(MOSAIC_PATTERN);
  804.         DrawRoundFillBox(32,m-128, 640-64, 256, 0,0xff803000);
  805.         SetTexture(NULL);
  806.         DrawRoundBox(32,m-128, 640-64, 256, 0, 6, 0xfff08020);
  807.  
  808.         autocenter=1;
  809.         PX=0;PY=m-16;
  810.         s_printf("Closing All");
  811.         autocenter=0;
  812.  
  813.     Screen_flip();
  814.     StopAudio();
  815.  
  816.     write_cfg(path_txt_cfg);
  817.  
  818.     WPAD_Shutdown();
  819.     ASND_End();
  820.  
  821.     if(have_device & 1) fatUnmount("sd");
  822.     if(have_device & 2) fatUnmount("usb");
  823.  
  824.     VIDEO_WaitVSync();
  825.     VIDEO_WaitVSync();
  826.  
  827.     sleep(2);
  828.  
  829.     if(exit_by_reset==2)
  830.         SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0);
  831.     if(exit_by_reset==3)
  832.         SYS_ResetSystem(SYS_POWEROFF_STANDBY, 0, 0);
  833. }
  834.  
  835.  
  836.  
  837. struct my_error_mgr {
  838.   struct jpeg_error_mgr pub;    /* "public" fields */
  839.  
  840.   jmp_buf setjmp_buffer;    /* for return to caller */
  841. };
  842.  
  843. typedef struct my_error_mgr * my_error_ptr;
  844.  
  845. char jpg_error_str[JMSG_LENGTH_MAX];
  846.  
  847. void  my_error_exit (j_common_ptr cinfo)
  848. {
  849.   /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
  850.   my_error_ptr myerr = (my_error_ptr) cinfo->err;
  851.  
  852.   /* Always display the message. */
  853.   /* We could postpone this until after returning, if we chose. */
  854.  // (*cinfo->err->output_message) (cinfo);
  855. (*cinfo->err->format_message) (cinfo, jpg_error_str);
  856.   /* Return control to the setjmp point */
  857.   longjmp(myerr->setjmp_buffer, 1);
  858. }
  859.  
  860. unsigned *jpeg_buf=NULL;
  861.  
  862. int jpeg_tdx,jpeg_tdy; // texel w-h
  863.  
  864. int jpeg_w,jpeg_h; // pixel w-h
  865.  
  866.  
  867. int read_JPEG_file (char * filename)
  868. {
  869. /* This struct contains the JPEG decompression parameters and pointers to
  870. * working space (which is allocated as needed by the JPEG library).
  871. */
  872. struct jpeg_decompress_struct cinfo;
  873. /* We use our private extension JPEG error handler.
  874. * Note that this struct must live as long as the main JPEG parameter
  875. * struct, to avoid dangling-pointer problems.
  876. */
  877. struct my_error_mgr jerr;
  878. /* More stuff */
  879. FILE * infile;      /* source file */
  880. JSAMPARRAY buffer;      /* Output row buffer */
  881. int row_stride;     /* physical row width in output buffer */
  882.  
  883. int n,m;
  884.  
  885. int a,b,k,l,same_w;
  886. int rr,gg,bb;
  887.  
  888. u8 *buf;
  889.  
  890. /* In this example we want to open the input file before doing anything else,
  891. * so that the setjmp() error recovery below can assume the file is open.
  892. * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
  893. * requires it in order to read binary files.
  894. */
  895.     jpg_error_str[0]=0;
  896.     if ((infile = fopen(filename, "rb")) == NULL) {
  897.  
  898.     sprintf(jpg_error_str,"Can't open file");
  899.     return 0;
  900.     }
  901.  
  902.     /* Step 1: allocate and initialize JPEG decompression object */
  903.  
  904.     /* We set up the normal JPEG error routines, then override error_exit. */
  905.     cinfo.err = jpeg_std_error(&jerr.pub);
  906.     jerr.pub.error_exit = my_error_exit;
  907.     /* Establish the setjmp return context for my_error_exit to use. */
  908.     if (setjmp(jerr.setjmp_buffer)) {
  909.     /* If we get here, the JPEG code has signaled an error.
  910.      * We need to clean up the JPEG object, close the input file, and return.
  911.      */
  912.  
  913.     //printf("Error!!!\n");
  914.     jpeg_destroy_decompress(&cinfo);
  915.     fclose(infile);
  916.     return 0;
  917.     }
  918.     /* Now we can initialize the JPEG decompression object. */
  919.     jpeg_create_decompress(&cinfo);
  920.  
  921.     /* Step 2: specify data source (eg, a file) */
  922.  
  923.     jpeg_stdio_src(&cinfo, infile);
  924.  
  925.     /* Step 3: read file parameters with jpeg_read_header() */
  926.  
  927.     (void) jpeg_read_header(&cinfo, TRUE);
  928.     /* We can ignore the return value from jpeg_read_header since
  929.     *   (a) suspension is not possible with the stdio data source, and
  930.     *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
  931.     * See libjpeg.doc for more info.
  932.     */
  933.  
  934.     /* Step 4: set parameters for decompression */
  935.  
  936.     /* In this example, we don't need to change any of the defaults set by
  937.     * jpeg_read_header(), so we do nothing here.
  938.     */
  939.  
  940.     /* Step 5: Start decompressor */
  941.  
  942.     (void) jpeg_start_decompress(&cinfo);
  943.     /* We can ignore the return value since suspension is not possible
  944.     * with the stdio data source.
  945.     */
  946.  
  947.     /* We may need to do some setup of our own at this point before reading
  948.     * the data.  After jpeg_start_decompress() we have the correct scaled
  949.     * output image dimensions available, as well as the output colormap
  950.     * if we asked for color quantization.
  951.     * In this example, we need to make an output work buffer of the right size.
  952.     */
  953.     /* JSAMPLEs per row in output buffer */
  954.     row_stride = cinfo.output_width * cinfo.output_components;
  955.  
  956.     // printf("width %i %i\n",cinfo.output_width, cinfo.output_components);
  957.     /* Make a one-row-high sample array that will go away when done with image */
  958.     buffer = (*cinfo.mem->alloc_sarray)
  959.         ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
  960.  
  961.  
  962.  
  963.     jpeg_w=cinfo.output_width;
  964.     jpeg_h=cinfo.output_height;
  965.  
  966.  
  967.     if(!buffer || jpg_error_str[0]!=0)
  968.     {
  969.     jpeg_destroy_decompress(&cinfo);
  970.     fclose(infile);
  971.     return 0;
  972.     }
  973.     /* Step 6: while (scan lines remain to be read) */
  974.     /*           jpeg_read_scanlines(...); */
  975.  
  976.     /* Here we use the library's state variable cinfo.output_scanline as the
  977.     * loop counter, so that we don't have to keep track ourselves.
  978.     */
  979.     n=0;
  980.  
  981.     jpeg_tdx=(jpeg_w>>3)<<3; if(jpeg_tdx>1024) jpeg_tdx=1024;
  982.     jpeg_tdy=(jpeg_h>>3)<<3; if(jpeg_tdy>1024) jpeg_tdy=1024;
  983.     a=0;k=0;
  984.     while (cinfo.output_scanline < cinfo.output_height) {
  985.     /* jpeg_read_scanlines expects an array of pointers to scanlines.
  986.      * Here the array is only one element long, but you could ask for
  987.      * more than one scanline at a time if that's more convenient.
  988.      */
  989.     (void) jpeg_read_scanlines(&cinfo, buffer, 1);
  990.     /* Assume put_scanline_someplace wants a pointer and sample count. */
  991.     // put_scanline_someplace(buffer[0], row_stride);
  992.     if(jpg_error_str[0]!=0)
  993.     {
  994.     jpeg_destroy_decompress(&cinfo);
  995.     fclose(infile);
  996.     return 0;
  997.     }
  998.  
  999.     buf=(u8 *) buffer[0];
  1000.     rr=buf[2];
  1001.     gg=buf[1];
  1002.     bb=buf[0];
  1003.  
  1004.         b=0;l=0;
  1005.         same_w=0;
  1006.         for(m=0;m<jpeg_w;m++) // re-scale to the max 1024x1024
  1007.             {
  1008.             if(!same_w)
  1009.                 {
  1010.                 jpeg_buf[(k*jpeg_tdx)+l]=0xff000000 | (bb<<16) | (gg<<8) |  (rr);
  1011.                 }
  1012.             else
  1013.                 {
  1014.                 rr+=buf[2];
  1015.                 gg+=buf[1];
  1016.                 bb+=buf[0];
  1017.                 }
  1018.            
  1019.             buf+=3;
  1020.             b+=jpeg_tdx;if(b>=jpeg_w)
  1021.                 {
  1022.                 if(same_w)
  1023.                   {
  1024.                   same_w++;
  1025.                   rr/=same_w;
  1026.                   gg/=same_w;
  1027.                   bb/=same_w;
  1028.                   jpeg_buf[(k*jpeg_tdx)+l]=0xff000000 | (bb<<16) | (gg<<8) |  (rr);
  1029.                   }
  1030.                 same_w=0;b-=jpeg_w;l++;if(l>=jpeg_tdx) break;
  1031.                 rr=buf[2];
  1032.                 gg=buf[1];
  1033.                 bb=buf[0];
  1034.                 } else same_w++;
  1035.             }
  1036.         a+=jpeg_tdy;if(a>=jpeg_h) {a-=jpeg_h;k++;if(k>=jpeg_tdy) break;}
  1037.        
  1038.    
  1039.     n++;
  1040.     }
  1041.  
  1042.     /* Step 7: Finish decompression */
  1043.  
  1044.     (void) jpeg_finish_decompress(&cinfo);
  1045.     /* We can ignore the return value since suspension is not possible
  1046.     * with the stdio data source.
  1047.     */
  1048.  
  1049.     /* Step 8: Release JPEG decompression object */
  1050.  
  1051.     /* This is an important step since it will release a good deal of memory. */
  1052.     jpeg_destroy_decompress(&cinfo);
  1053.  
  1054.     /* After finish_decompress, we can close the input file.
  1055.     * Here we postpone it until after no more JPEG errors are possible,
  1056.     * so as to simplify the setjmp error logic above.  (Actually, I don't
  1057.     * think that jpeg_destroy can do an error exit, but why assume anything...)
  1058.     */
  1059.     fclose(infile);
  1060.  
  1061.     /* At this point you may want to check to see whether any corrupt-data
  1062.     * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
  1063.     */
  1064.     if(jpg_error_str[0]!=0)
  1065.     {
  1066.     return 0;
  1067.     }
  1068.     /* And we're done! */
  1069. return 1;
  1070. }
  1071.  
  1072.  
  1073.  
  1074. int read_PNG_file (char * filename)
  1075. {
  1076.    PNGUPROP imgProp;
  1077.    IMGCTX ctx;
  1078.    
  1079.    char *texture_buff;
  1080.  
  1081.    //ctx = PNGU_SelectImageFromDevice (filename);
  1082.    if ( !(ctx = PNGU_SelectImageFromDevice (filename)) ){
  1083.    return 0;
  1084.    }
  1085.    PNGU_GetImageProperties (ctx, &imgProp);
  1086.    texture_buff = memalign (32, imgProp.imgWidth * imgProp.imgHeight * 4);
  1087.    PNGU_DecodeTo4x4RGBA8 (ctx, imgProp.imgWidth, imgProp.imgHeight, texture_buff, 255);
  1088.    PNGU_ReleaseImageContext (ctx);
  1089.    DCFlushRange (texture_buff, imgProp.imgWidth * imgProp.imgHeight * 4);
  1090.  return 1;  
  1091. }
  1092.  
  1093.  
  1094.  
  1095.  
  1096. GXTexObj scr_tex;
  1097. GXTexObj icon_tex[icons_num_sprites];
  1098.  
  1099. u16 *animate_mem=NULL;
  1100.  
  1101. void animate_background()
  1102. {
  1103. int n,m,k;
  1104. static int l=0;
  1105.  
  1106. n=icons_num_sprites-2;
  1107.  
  1108. if(!animate_mem) animate_mem=memalign(32, icons_sprites[n].sx*icons_sprites[n].sy*2);
  1109. if(!animate_mem) return;
  1110. l++;
  1111.  
  1112. k=l>>2;
  1113. for(m=0;m<icons_sprites[n].sy;m++)
  1114.     {
  1115.     k=(k % icons_sprites[n].sy);
  1116.     memcpy(animate_mem+k*icons_sprites[n].sx, ((u16 *)icons_sprites[n].ptr)+m*icons_sprites[n].sx, icons_sprites[n].sx*2);
  1117.     k++;
  1118.     }
  1119. CreateTexture(&icon_tex[n], TLUT_SRGB5A1, animate_mem, icons_sprites[n].sx, icons_sprites[n].sy, 0);
  1120. }
  1121.  
  1122.  
  1123. int audio_pause=0;
  1124.  
  1125.  
  1126. void select_file(int flag)
  1127. {
  1128. int n,m,l,f=0,len,max_entry;
  1129. static  int posfile=0;
  1130. static int old_flag=-1;
  1131. int flash=0;
  1132.  
  1133. read_list_file((void *) path_file, flag);
  1134.  
  1135. if(flag!=old_flag)
  1136.     {
  1137.     posfile=0;
  1138.     old_flag=flag;
  1139.     }
  1140.  
  1141. while(1)
  1142.     {
  1143.    
  1144.  
  1145.     flash++;
  1146.  
  1147.     if(exit_by_reset) break;
  1148.  
  1149.     loop_music_playing();
  1150.  
  1151.     m=0;
  1152.    
  1153.     sizeletter=4;
  1154.  
  1155.    // SetTexture(MOSAIC_PATTERN);DrawFillBox(0,0, 640, SCR_HEIGHT, 0,0xff603000);
  1156.     animate_background();
  1157.     SetTexture(&icon_tex[icons_num_sprites-2]);
  1158.     DrawFillBox(0,0, 640, SCR_HEIGHT, 0,0xff906000);
  1159.    
  1160.  
  1161.     // header
  1162.     SetTexture(CROSS_PATTERN);
  1163.     DrawRoundFillBox(8,8, 640-16, 32, 0, 0xff302000);
  1164.     SetTexture(NULL_PATTERN);
  1165.     DrawRoundBox(8,8, 640-16, 32, 0, 2, 0xff804000);
  1166.     PX=0;PY=16;color=0xffffffff;autocenter=1;
  1167.    
  1168.     if(flag==0) s_printf("SELECT MUSIC FILES");
  1169.     if(flag==1) s_printf("SELECT IMAGE FILES");
  1170.     if(flag==2) s_printf("SELECT TEXT FILES");
  1171.  
  1172.     autocenter=0;
  1173.    
  1174.     // file list
  1175.    
  1176.  
  1177.     SetTexture(CROSS_PATTERN);
  1178.     DrawRoundFillBox(8,48, 640-16, SCR_HEIGHT-32-48, 0, 0xff302000);
  1179.  
  1180.     SetTexture(NULL_PATTERN);
  1181.     DrawRoundBox(8, 48, 640-16, SCR_HEIGHT-32-48, 0, 2, 0xff804000);
  1182.  
  1183.    
  1184.     max_entry=(SCR_HEIGHT-32-48)/20;
  1185.  
  1186.     SetTexture(NULL);
  1187.     DrawRoundFillBox(8,SCR_HEIGHT-28, 640-16, 24, 0, 0xff201010);
  1188.     SetTexture(NULL_PATTERN);
  1189.     DrawRoundBox(8,SCR_HEIGHT-28, 640-16, 24, 0, 2, 0xff804000);
  1190.    
  1191.     PX=24;PY=SCR_HEIGHT-24;color=0xffffffff;autocenter=1;
  1192.     sizeletter=3;
  1193.     if(flag==0)
  1194.         s_printf("Use A to select one file and PLUS to select All");
  1195.     if(flag==1)
  1196.         s_printf("Use A to select one file and PLUS to select All");
  1197.     if(flag==2)
  1198.         s_printf("Use A to select one file");
  1199.     autocenter=0;
  1200.     sizeletter=4;
  1201.  
  1202.     for(n=(posfile-max_entry/2)*(nfiles>=max_entry);n<posfile+max_entry;n++)
  1203.             {
  1204.             if(n<0) continue;
  1205.             if(n>=nfiles) break;
  1206.             if(m>=max_entry) break;
  1207.            
  1208.            
  1209.             PX=16;PY=48+2+m*20;
  1210.             color=0xffffffff;
  1211.  
  1212.             if(n==posfile)
  1213.                 {
  1214.                 len=38;
  1215.  
  1216.                 //if(flash & 8)
  1217.                 DrawRoundFillBox(PX,PY-2, len*16, 20, 0, 0xff00004f);
  1218.                
  1219.                 DrawRoundBox(PX,PY-2, len*16, 20, 0, 2, 0xff30ffff);
  1220.  
  1221.                 }
  1222.  
  1223.             if(files[n].is_directory)
  1224.                 {
  1225.                 if(files[n].name[0]=='.' && files[n].name[1]=='[')
  1226.                     {
  1227.                     color=0xffffff00;
  1228.                     s_printf("%s",&files[n].name[1]);
  1229.                     }
  1230.                 else
  1231.                     {
  1232.                     color=0xff00ffff;
  1233.                
  1234.                     s_printf("<%s>",get_name_short_fromUTF8(&files[n].name[0]));
  1235.                     }
  1236.                 }
  1237.             else
  1238.                 {
  1239.                 color=0xffffffff;
  1240.                 if(files[n].is_selected) color=0xff6fff6f;
  1241.                
  1242.                 if(flag==2)
  1243.                     if(!strcmp(files[n].name,txt_file.name)) color=0xff6fff6f;
  1244.  
  1245.                 s_printf("%s", (char *) get_name_short_fromUTF8(&files[n].name[0]));
  1246.                 }
  1247.            
  1248.             bkcolor=0;color=0xffffffff;
  1249.             m++;
  1250.             }
  1251.    
  1252.      wii_nKeys=wii_joystick_read(0);
  1253.    
  1254.      new_key=wii_nKeys & (~old_key);
  1255.      old_key=wii_nKeys;
  1256.  
  1257.      if(!(old_key  & (WII_UP | WII_DOWN))) f=0;
  1258.            
  1259.             if(old_key  & WII_UP)
  1260.                 {
  1261.                 if(f==0) f=2;
  1262.                 else if(f & 1) f=2;
  1263.                 else {f+=2;if(f>40) {f=34;new_key|=WII_UP;}}
  1264.                 }
  1265.             if(old_key  & WII_DOWN)
  1266.                 {
  1267.                 if(f==0) f=1;
  1268.                 else if(!(f & 1)) f=1;
  1269.                 else {f+=2;if(f>41) {f=35;new_key|=WII_DOWN;}}
  1270.                 }
  1271.             if(new_key & (WII_HOME | WII_ONE | WII_TWO)) break;
  1272.            
  1273.        
  1274.  
  1275.             if(nfiles>0) if(new_key & WII_PLUS)
  1276.                 {
  1277.                
  1278.                         if(flag==1)
  1279.                             {
  1280.                             current_jpg=-1;
  1281.                             njpg_files=0;
  1282.                             for(n=0;n<nfiles;n++)
  1283.                                 {
  1284.                                 if(!files[n].is_directory)
  1285.                                 if(is_ext(files[n].name,".jpg") || is_ext(files[n].name,".jpeg") || is_ext(files[n].name,".png"))
  1286.                                     {
  1287.                                     memcpy(jpg_files[njpg_files].name,files[n].name,258);
  1288.                                     current_jpg=0;
  1289.                                     njpg_files++;
  1290.                                     if(njpg_files>=MAX_ENTRY) break;
  1291.                                     }
  1292.                                 }
  1293.                             break;
  1294.                             }
  1295.                         else
  1296.                         if(flag==0)
  1297.                             {
  1298.                             for(n=0;n<nfiles;n++)
  1299.                                 {
  1300.                                 if(!files[n].is_directory)
  1301.                                 if(is_ext(files[n].name,".mp3") || is_ext(files[n].name,".ogg"))
  1302.                                     {
  1303.                                     test_music_file(n, 1);
  1304.                                    
  1305.                                     }
  1306.                                 }
  1307.                            
  1308.                             }
  1309.                            
  1310.            
  1311.                 }
  1312.  
  1313.             if(nfiles>0) if(new_key & WII_A)
  1314.                 {
  1315.                
  1316.                
  1317.                     if(!files[posfile].is_directory)
  1318.                         {
  1319.                        
  1320.                         if(is_ext(files[posfile].name,".txt") || is_ext(files[posfile].name,".zip"))
  1321.                             {
  1322.                             FILE *fp;
  1323.                            
  1324.                             memcpy(txt_file.name,files[posfile].name,258);
  1325.                        
  1326.                             if(txt_file_mem) free(txt_file_mem);txt_file_mem=0;
  1327.  
  1328.                             txt_file_len=0;
  1329.  
  1330.                             if(is_ext(files[posfile].name,".zip"))
  1331.                                 txt_file_mem = LoadZipTxt(txt_file.name, &txt_file_len);
  1332.                             else
  1333.                                 {
  1334.                                 fp=fopen(txt_file.name,"r"); // lee el fichero de texto
  1335.                                 if(fp!=0)
  1336.                                     {
  1337.                                     fseek(fp,0,SEEK_END);
  1338.                                     txt_file_len=ftell(fp);
  1339.                                     fseek(fp,0,SEEK_SET);
  1340.                                     txt_file_mem=malloc(txt_file_len+128);
  1341.                                     if(txt_file_mem)
  1342.                                         {n=fread(txt_file_mem,1,txt_file_len,fp);
  1343.                                         if(n<0) {txt_file_len=0;free(txt_file_mem);txt_file_mem=0;} else txt_file_len=n;
  1344.                                         }
  1345.                                     else txt_file_len=0;
  1346.                                     fclose(fp);
  1347.                                     }
  1348.                                 }
  1349.  
  1350.                             if(txt_file_len!=0)
  1351.                                 {
  1352.                                 read_txt_cfg(path_txt_cfg);
  1353.                                 text_init(txt_file_mem, txt_file_len, txt_file.name);
  1354.                                 break;
  1355.                                 }
  1356.                             }
  1357.                         else
  1358.                         if(is_ext(files[posfile].name,".jpg") || is_ext(files[posfile].name,".jpeg") || is_ext(files[posfile].name,".png"))
  1359.                             {
  1360.                             current_jpg=-1;
  1361.                             njpg_files=0;
  1362.                             for(n=0;n<nfiles;n++)
  1363.                                 {
  1364.                                 if(!files[n].is_directory)
  1365.                                 if(is_ext(files[n].name,".jpg") || is_ext(files[n].name,".jpeg") || is_ext(files[n].name,".png"))
  1366.                                     {
  1367.                                     memcpy(jpg_files[njpg_files].name,files[n].name,258);
  1368.                                     if(n==posfile) current_jpg=njpg_files;
  1369.                                     njpg_files++;
  1370.                                     if(njpg_files>=MAX_ENTRY) break;
  1371.                                     }
  1372.                                 }
  1373.                             break;
  1374.                             }
  1375.                         else
  1376.                             {
  1377.                             #if 1
  1378.                             if(nmusic_files<MAX_ENTRY)
  1379.                                 {
  1380.                                 if(is_ext(files[posfile].name,".mp3") || is_ext(files[posfile].name,".ogg"))
  1381.                                     {
  1382.                                     test_music_file(posfile, 1);
  1383.                                     }
  1384.                                  else
  1385.                                  if(is_ext(files[posfile].name,".m3u"))
  1386.                                     {
  1387.                                     FILE *fp;
  1388.  
  1389.                                     memcpy(temp_buffer,files[posfile].name,258); // copia el nombre al archivo temporal
  1390.                                     m=n=0;
  1391.                                     while(temp_buffer[n]!=0) {if(temp_buffer[n]=='/') m=n+1; n++;}
  1392.                                     l=m;
  1393.                                     temp_buffer[l]=0;
  1394.  
  1395.                                     fp=fopen(files[posfile].name,"r"); // lee el fichero m3u
  1396.  
  1397.                                     if(fp!=0)
  1398.                                         {
  1399.                                         nmusic_files=0;
  1400.  
  1401.                                         while(fgets((void *) &temp_buffer[512], 256, fp))
  1402.                                             {
  1403.                                            
  1404.                                             n=512;
  1405.                                             while(temp_buffer[n]!=0) {if(temp_buffer[n]<32) {temp_buffer[n]=0;break;} n++;}
  1406.                                            
  1407.                                             if(is_ext((void *) &temp_buffer[512],".mp3") || is_ext((void *) &temp_buffer[512],".ogg"))
  1408.                                                 {
  1409.                                                
  1410.                                                 if(nmusic_files<MAX_ENTRY)
  1411.                                                     {
  1412.                                                     m=n=512;
  1413.                                                     while(temp_buffer[n]!=0) {if(temp_buffer[n]=='/' || temp_buffer[n]=='\\') m=n+1; n++;}
  1414.                                                     n-=512;
  1415.                                                     if((l+n)>258) {n=257-l;temp_buffer[n+512]=0;}
  1416.                                                     sprintf((void *) &temp_buffer[1024],"%s%s",&temp_buffer[0],&temp_buffer[m]);
  1417.                                                     char_to_utf8((void *) &temp_buffer[1024],music_files[nmusic_files].name);
  1418.  
  1419.                                                     nmusic_files++;
  1420.                                                     }
  1421.                                                 else break;
  1422.                                                
  1423.                                                 }
  1424.                                            
  1425.                                             }
  1426.                                        
  1427.                                         fclose(fp);
  1428.  
  1429.                                         if(nmusic_files)
  1430.                                             {
  1431.                                             play_file=0;
  1432.                                             music_playing=1;if(PlayAudio(music_files[play_file].name, 0, 0)<0) music_playing=0;
  1433.                                             break; // retorna al player
  1434.                                             }
  1435.                                         } //clos
  1436.                                     }// m3u
  1437.                                 }
  1438.                             #endif
  1439.                             }
  1440.                
  1441.                         }
  1442.                       else
  1443.                         {
  1444.                          // cambio de device
  1445.                          if(files[posfile].name[0]=='.' && files[posfile].name[1]=='[')
  1446.                             {
  1447.                            
  1448.                             if(files[posfile].name[2]=='U')
  1449.                                 {
  1450.                                
  1451.                                 current_device=1;
  1452.                                 sprintf(path_file, "fat%i:/", "usb");
  1453.                                 read_list_file(NULL, flag);
  1454.                                 posfile=0;
  1455.                            
  1456.                                 }
  1457.                             else
  1458.                             if(files[posfile].name[2]=='S')
  1459.                                 {
  1460.  
  1461.                                 current_device=0;
  1462.                                 sprintf(path_file, "fat%i:/", "sd");
  1463.                                 read_list_file(NULL, flag);
  1464.                                 posfile=0;
  1465.                                
  1466.                                 }
  1467.                             }
  1468.                          else
  1469.                             {read_list_file((void *) &files[posfile].name[0], flag);posfile=0;}
  1470.                          
  1471.                          
  1472.                         }
  1473.                        
  1474.                 }
  1475.  
  1476.  
  1477.            
  1478.  
  1479.                 if((new_key & WII_UP)) {posfile--;if(posfile<0) posfile=nfiles-1;}
  1480.                 if((new_key & WII_DOWN)){posfile++;if(posfile>=nfiles) posfile=0;}
  1481.            
  1482.        
  1483.    
  1484.     Screen_flip();
  1485.    
  1486.     }
  1487. Screen_flip();
  1488. }
  1489.  
  1490. void music_player();
  1491.  
  1492. void jpeg_player()
  1493. {
  1494. int dx,dy;
  1495.  
  1496. int posx=0,posy=0;
  1497. static int mode=0;
  1498. static int amp=100;
  1499.  
  1500. int n;
  1501. int osd_time=0;
  1502.  
  1503. unsigned time;
  1504.  
  1505. if(current_jpg<0) return;
  1506.  
  1507. while(1) // proteccion contra pulsaciones
  1508.     {
  1509.     if(exit_by_reset) break;
  1510.  
  1511.     wii_nKeys=wii_joystick_read(1);
  1512.    
  1513.     new_key=wii_nKeys & (~old_key);
  1514.     old_key=wii_nKeys;
  1515.  
  1516.     if(!(old_key &
  1517.         (WII_LEFT | WII_RIGHT | WII_UP | WII_DOWN ))) break;
  1518.     }
  1519.  
  1520. Next_jpg:
  1521.  
  1522. posx=0;posy=0;
  1523. osd_time=0;
  1524.  
  1525. time=ticks_to_msecs(gettick());
  1526.  
  1527. if(is_ext(jpg_files[current_jpg].name,".jpg") || is_ext(jpg_files[current_jpg].name,".jpeg")){
  1528.  
  1529. if(read_JPEG_file(jpg_files[current_jpg].name))
  1530.     {
  1531.     CreateTexture(&scr_tex, TILE_RGBA8, (void *) jpeg_buf, jpeg_tdx, jpeg_tdy, 0);
  1532.     }
  1533.  else
  1534.     {
  1535.     jpeg_tdx=jpeg_tdy=64;
  1536.     jpeg_w=256;jpeg_h=256;
  1537.     memset((void *) jpeg_buf,0,jpeg_tdx*jpeg_tdy*4);
  1538.    
  1539.     for(n=0;n<jpeg_tdx;n++)
  1540.         {
  1541.         jpeg_buf[jpeg_tdx*n+n]=0xffff0000;
  1542.         if((n+1)<jpeg_tdx) jpeg_buf[jpeg_tdx*n+n+1]=0xffff0000;
  1543.         if((n+2)<jpeg_tdx) jpeg_buf[jpeg_tdx*n+n+2]=0xffff0000;
  1544.         if((n+3)<jpeg_tdx) jpeg_buf[jpeg_tdx*n+n+3]=0xffff0000;
  1545.  
  1546.         jpeg_buf[jpeg_tdx*n+jpeg_tdx-n-1]=0xffff0000;
  1547.         if((jpeg_tdx-n-2)>0) jpeg_buf[jpeg_tdx*n+jpeg_tdx-n-2]=0xffff0000;
  1548.         if((jpeg_tdx-n-3)>0) jpeg_buf[jpeg_tdx*n+jpeg_tdx-n-3]=0xffff0000;
  1549.         if((jpeg_tdx-n-4)>0) jpeg_buf[jpeg_tdx*n+jpeg_tdx-n-4]=0xffff0000;
  1550.         }
  1551.     CreateTexture(&scr_tex, TILE_RGBA8, (void *) jpeg_buf, jpeg_tdx, jpeg_tdy, 0);
  1552.     }
  1553. }else if(is_ext(jpg_files[current_jpg].name,".png")){
  1554.  
  1555. if(read_PNG_file(jpg_files[current_jpg].name))
  1556.     {
  1557.     CreateTexture(&scr_tex, TILE_RGBA8, (void *) jpeg_buf, jpeg_tdx, jpeg_tdy, 0);
  1558.     }
  1559.  else
  1560.     {
  1561.     jpeg_tdx=jpeg_tdy=64;
  1562.     jpeg_w=256;jpeg_h=256;
  1563.     memset((void *) jpeg_buf,0,jpeg_tdx*jpeg_tdy*4);
  1564.    
  1565.     for(n=0;n<jpeg_tdx;n++)
  1566.         {
  1567.         jpeg_buf[jpeg_tdx*n+n]=0xffff0000;
  1568.         if((n+1)<jpeg_tdx) jpeg_buf[jpeg_tdx*n+n+1]=0xffff0000;
  1569.         if((n+2)<jpeg_tdx) jpeg_buf[jpeg_tdx*n+n+2]=0xffff0000;
  1570.         if((n+3)<jpeg_tdx) jpeg_buf[jpeg_tdx*n+n+3]=0xffff0000;
  1571.  
  1572.         jpeg_buf[jpeg_tdx*n+jpeg_tdx-n-1]=0xffff0000;
  1573.         if((jpeg_tdx-n-2)>0) jpeg_buf[jpeg_tdx*n+jpeg_tdx-n-2]=0xffff0000;
  1574.         if((jpeg_tdx-n-3)>0) jpeg_buf[jpeg_tdx*n+jpeg_tdx-n-3]=0xffff0000;
  1575.         if((jpeg_tdx-n-4)>0) jpeg_buf[jpeg_tdx*n+jpeg_tdx-n-4]=0xffff0000;
  1576.         }
  1577.     CreateTexture(&scr_tex, TILE_RGBA8, (void *) jpeg_buf, jpeg_tdx, jpeg_tdy, 0);
  1578.     }
  1579.  
  1580. }
  1581.  
  1582. if((ticks_to_msecs(gettick())-time)<500)  // espera al menos medio segundo
  1583.                 usleep(500*1000);
  1584.  
  1585.  
  1586. while(1)
  1587.     {
  1588.            
  1589.     int flag;
  1590.  
  1591.     if(exit_by_reset) break;
  1592.    
  1593.     loop_music_playing();
  1594.    
  1595.     sizeletter=4;
  1596.    
  1597.     PX=24;PY=SCR_HEIGHT-24;color=0xffffffff;
  1598.  
  1599.    
  1600.  
  1601.     SetTexture(&scr_tex);
  1602.  
  1603.     //DrawFillBox(0,0, 640, SCR_HEIGHT, 0,0xffffffff);
  1604.  
  1605.     wii_nKeys=wii_joystick_read(1);
  1606.    
  1607.     new_key=wii_nKeys & (~old_key);
  1608.     old_key=wii_nKeys;
  1609.  
  1610.     flag=0;
  1611.     if(new_key & WII_PLUS) {flag=1;if(mode==0) {mode=1;posx=posy=0;if(jpeg_w>=jpeg_h) amp=125;} else {if(amp==100) {posx=posy=0;} amp+=25;if(amp>400) amp=400;}}
  1612.     if(new_key & WII_MINUS) {flag=1;amp-=25;if(amp<100) {mode=0;amp=100;}}
  1613.  
  1614.    
  1615.     if(new_key & WII_B)
  1616.         {
  1617.        
  1618.         current_jpg++;
  1619.        
  1620.         if(current_jpg>=njpg_files)
  1621.             {
  1622.             current_jpg--;
  1623.             amp=100;mode=0;
  1624.             }
  1625.  
  1626.         Screen_flip();
  1627.         SetTexture(NULL);
  1628.  
  1629.         goto Next_jpg;
  1630.         }
  1631.  
  1632.     if(new_key & WII_A)
  1633.         {
  1634.        
  1635.         current_jpg--;
  1636.        
  1637.         if(current_jpg<0)
  1638.             {
  1639.             current_jpg++;
  1640.             amp=100;mode=0;
  1641.             }
  1642.  
  1643.         Screen_flip();
  1644.         SetTexture(NULL);
  1645.    
  1646.         goto Next_jpg;
  1647.         }
  1648.    
  1649.     if(new_key & WII_HOME) break;
  1650.  
  1651.    
  1652.  
  1653.    
  1654.  
  1655.     if(amp==100 && mode==0)
  1656.         {
  1657.             if(!is_16_9)
  1658.                 {
  1659.                 dx=640;
  1660.                 dy=(640*jpeg_h)/jpeg_w;
  1661.  
  1662.                 if(dy>480)
  1663.                     {
  1664.                     dy=480;
  1665.                     dx=(480*jpeg_w)/jpeg_h;
  1666.                     }
  1667.                 dy=SCR_HEIGHT*dy/480;
  1668.                 }
  1669.             else
  1670.                 {
  1671.                 dx=853;
  1672.                 dy=(853*jpeg_h)/jpeg_w;
  1673.  
  1674.                 if(dy>480)
  1675.                     {
  1676.                     dy=480;
  1677.                     dx=(480*jpeg_w)/jpeg_h;
  1678.                     }
  1679.                 dy=SCR_HEIGHT*dy/480;
  1680.                 dx=dx*640/853;
  1681.                 }
  1682.  
  1683.             posx=(640-dx)/2;
  1684.            
  1685.             posy=(SCR_HEIGHT-dy)/2;if(posy<0) posy=0;
  1686.  
  1687.        
  1688.         }
  1689.      else
  1690.         {
  1691.         if(is_16_9) dy=(((SCR_HEIGHT)*jpeg_h)/jpeg_w)*16/9;
  1692.         else dy=((SCR_HEIGHT/3)*4*jpeg_h)/jpeg_w;
  1693.         dx=640;
  1694.         dx=dx*amp/100;
  1695.         dy=dy*amp/100;
  1696.        
  1697.         #if 0
  1698.         if(new_key & WII_LEFT) {posx+=128/*dx/3*/;flag=1;}
  1699.         if(new_key & WII_RIGHT) {posx-=128/*dx/3*/;flag=1;}
  1700.         if(new_key & WII_UP) {posy+=64;flag=1;}
  1701.         if(new_key & WII_DOWN) {posy-=64;flag=1;}
  1702.         #endif
  1703.  
  1704.         if(old_key & WII_LEFT) {posx+=16;flag=1;}
  1705.         if(old_key & WII_RIGHT) {posx-=16;flag=1;}
  1706.         if(old_key & WII_UP) {posy+=16;flag=1;}
  1707.         if(old_key & WII_DOWN) {posy-=16;flag=1;}
  1708.        
  1709.         }
  1710.  
  1711.     if(flag)
  1712.         {
  1713.         if(posx>0 && flag!=2) posx=0;
  1714.         if(posy>0) posy=0;
  1715.         if(posx<-(dx-640)) posx=-(dx-640);
  1716.         if(posy<-(dy-(SCR_HEIGHT))) posy=-(dy-(SCR_HEIGHT));
  1717.  
  1718.         osd_time=0;
  1719.         }
  1720.  
  1721.     if(amp==100 && jpeg_w>=jpeg_h && jpeg_h<=SCR_HEIGHT && (!is_16_9 || mode==0)) {posy=(SCR_HEIGHT-dy)/2;if(posy<0) posy=0;}
  1722.     //DrawFillBox(0,0, 640, dy, 0,0xffffffff);
  1723.     if(mode==1 && amp>=100)
  1724.         {
  1725.         DrawFillBox(posx,posy, dx, dy, 100,0xffffffff);
  1726.        
  1727.  
  1728.         if(osd_time<120)
  1729.             {
  1730.             osd_time++;
  1731.             SetTexture(CROSS_PATTERN);
  1732.             DrawRoundFillBox(8,SCR_HEIGHT-24, 640-16, 24, 0, 0xff302000);
  1733.             SetTexture(NULL_PATTERN);
  1734.             DrawRoundBox(8,SCR_HEIGHT-24, 640-16, 24, 0, 4, 0xffff7f2f);
  1735.             PX=16;PY=SCR_HEIGHT-20;autocenter=1;s_printf("ZOOM: %i%% Img: (%i/%i)",amp, current_jpg+1, njpg_files);autocenter=0;
  1736.             }
  1737.         }
  1738.     else
  1739.         {
  1740.         DrawFillBox(posx,posy, dx, dy, 100,0xffffffff);
  1741.         if(osd_time<120)
  1742.             {
  1743.             osd_time++;
  1744.             SetTexture(CROSS_PATTERN);
  1745.             DrawRoundFillBox(8,SCR_HEIGHT-24, 640-16, 24, 0, 0xff302000);
  1746.             SetTexture(NULL_PATTERN);
  1747.             DrawRoundBox(8,SCR_HEIGHT-24, 640-16, 24, 0, 4, 0xffff7f2f);
  1748.             PX=16;PY=SCR_HEIGHT-20;autocenter=1;s_printf("Img: (%i/%i) Res: %ix%i",current_jpg+1, njpg_files, jpeg_w, jpeg_h);autocenter=0;
  1749.             }
  1750.         }
  1751.     if(jpg_error_str[0]!=0)
  1752.         {
  1753.         SetTexture(CROSS_PATTERN);
  1754.         DrawRoundFillBox(8,SCR_HEIGHT-24, 640-16, 24, 0, 0xff302000);
  1755.         SetTexture(NULL_PATTERN);
  1756.         DrawRoundBox(8,SCR_HEIGHT-24, 640-16, 24, 0, 4, 0xffff7f2f);
  1757.         autocenter=1;
  1758.         PX=16;PY=SCR_HEIGHT-20;s_printf("%s",jpg_error_str);
  1759.         autocenter=0;
  1760.         }
  1761.        
  1762.     Screen_flip();
  1763.  
  1764.     if(new_key & WII_ONE) {music_player();}
  1765.    
  1766.    
  1767.     }
  1768.  
  1769. Screen_flip();
  1770. SetTexture(NULL);
  1771.  
  1772. }
  1773. void music_gfx();
  1774.  
  1775. void music_player()
  1776. {
  1777. int n,m;
  1778.  
  1779. int f=0;
  1780.  
  1781. int mode=0;
  1782. int select=0;
  1783.  
  1784. int select2=2;
  1785.  
  1786. int time_playing=0;
  1787. int time_playing_old=0;
  1788. unsigned time_total=0;
  1789.  
  1790. int left_count=0,right_count=0;
  1791.  
  1792. int ang_suma=0;
  1793.  
  1794. int status;
  1795.  
  1796. int flash=0;
  1797.  
  1798. int osd_volume=0;
  1799.  
  1800. int fast=0;
  1801.  
  1802. if(play_file>=0 && music_playing) select=play_file;
  1803.  
  1804. wii_nKeys=wii_joystick_read(1);
  1805.    
  1806. new_key=wii_nKeys & (~old_key);
  1807. old_key=wii_nKeys;
  1808.  
  1809.  
  1810. while(1)
  1811.     {
  1812.     int max_entry;
  1813.  
  1814.     if(exit_by_reset) break;
  1815.  
  1816.     flash++;
  1817.  
  1818.     status=StatusAudio();
  1819.  
  1820.     if(status==AUDIO_STATUS_RUNNING || fast) {ang_suma++;ang_suma %=360;}
  1821.  
  1822.     //SetTexture(MOSAIC_PATTERN);DrawFillBox(0,0, 640, SCR_HEIGHT, 0,0xff603000);
  1823.     animate_background();
  1824.     SetTexture(&icon_tex[icons_num_sprites-2]);
  1825.     DrawFillBox(0,0, 640, SCR_HEIGHT, 0,0xff906000);
  1826.  
  1827.    
  1828.     for(n=0;n<9;n++)
  1829.         {
  1830.         SetTexture(NULL_PATTERN);
  1831.         switch(n)
  1832.             {
  1833.             case 1:
  1834.                 if(left_count)
  1835.                     DrawFillBox(140+40*n,48, 32, 32, 0,0xff000000);
  1836.                 else
  1837.                     DrawFillBox(140+40*n,48, 32, 32, 0,0xff808080);
  1838.                 break;
  1839.             case 2:
  1840.                 if(status==AUDIO_STATUS_RUNNING && left_count==0 && right_count==0)
  1841.                     DrawFillBox(140+40*n,48, 32, 32, 0,0xff000000);
  1842.                 else
  1843.                     DrawFillBox(140+40*n,48, 32, 32, 0,0xff808080);
  1844.                 break;
  1845.             case 3:
  1846.                 if(status==AUDIO_STATUS_PAUSED && (flash & 16)!=0 && fast==0)
  1847.                     DrawFillBox(140+40*n,48, 32, 32, 0,0xff000000);
  1848.                 else
  1849.                     DrawFillBox(140+40*n,48, 32, 32, 0,0xff808080);
  1850.                 break;
  1851.             case 4:
  1852.                 if(!music_playing)
  1853.                     DrawFillBox(140+40*n,48, 32, 32, 0,0xff000000);
  1854.                 else
  1855.                     DrawFillBox(140+40*n,48, 32, 32, 0,0xff808080);
  1856.                 break;
  1857.             case 5:
  1858.                 if(right_count)
  1859.                     DrawFillBox(140+40*n,48, 32, 32, 0,0xff000000);
  1860.                 else
  1861.                     DrawFillBox(140+40*n,48, 32, 32, 0,0xff808080);
  1862.                 break;
  1863.             case 6:
  1864.                 DrawFillBox(140+40*n,48, 32, 32, 0,0xff000000);
  1865.                 break;
  1866.             case 7:
  1867.                 if(music_loop)
  1868.                     DrawFillBox(140+40*n,48, 32, 32, 0,0xff0000ff);
  1869.                 else
  1870.                     DrawFillBox(140+40*n,48, 32, 32, 0,0xff808080);
  1871.                 break;
  1872.             }
  1873.         SetTexture(&icon_tex[n]);
  1874.  
  1875.         if(select2==n && mode==0) DrawFillBox(140+40*n,48, 32, 32, 0,0xff40ff40);
  1876.             else DrawFillBox(140+40*n,48, 32, 32, 0,0xffffffff);
  1877.         }
  1878.  
  1879.     SetTexture(CROSS_PATTERN);
  1880.     DrawRoundFillBox(8, 88, 640-16, SCR_HEIGHT-16-88+8*(SCR_HEIGHT>480), 0, 0xff302000);
  1881.     SetTexture(NULL_PATTERN);
  1882.     DrawRoundBox(8, 88, 640-16, SCR_HEIGHT-16-88+8*(SCR_HEIGHT>480), 0, 2, 0xff804000);
  1883.  
  1884.     sizeletter=2;
  1885.     autocenter=1;
  1886.     PX=0;PY=12;
  1887.     color=0xfff08020;
  1888.     SetTexture(NULL_PATTERN);
  1889.     DrawRoundBox(208,8, 640-416, 32, 0, 2, 0xfff08020);
  1890.     s_printf("Music Player");
  1891.     autocenter=0;
  1892.  
  1893.     sizeletter=4;
  1894.  
  1895.     if(!fast) time_playing=GetTimeAudio()/1000; else time_playing=time_playing_old;
  1896.     time_total=GetTotalTimeAudio()/1000;
  1897.    
  1898.     if(!music_playing) {time_playing_old=0;time_playing=0;}
  1899.         else
  1900.             if(time_playing>0) time_playing_old=time_playing;
  1901.  
  1902.     if(status==AUDIO_STATUS_RUNNING || status==AUDIO_STATUS_PAUSED || fast)
  1903.         {
  1904.        
  1905.        
  1906.         SetTexture(MOSAIC_PATTERN);
  1907.         DrawFillSlice(320,88+(SCR_HEIGHT-16-88)/2, 120, 120, 0, 0, 360, 0xff603000);
  1908.         SetTexture(NULL_PATTERN);
  1909.         DrawSlice(320,88+(SCR_HEIGHT-16-88)/2, 120, 120, 0, 4, 0, 360, 0xff804000);
  1910.         SetTexture(STRIPED_PATTERN);
  1911.         DrawFillSlice(320,88+(SCR_HEIGHT-16-88)/2, 116, 116, 0, ang_suma, ang_suma+time_playing_old*360/time_total, 0x80000040);
  1912.        
  1913.         }
  1914.        
  1915.         SetTexture(NULL_PATTERN);
  1916.  
  1917.        
  1918.         PX=80-32-16;
  1919.         PY=32;
  1920.         color=0xff00f0f0;s_printf("Time");
  1921.         PX=600-48-32;
  1922.         s_printf("Length");
  1923.  
  1924.         color=0xffffffff;
  1925.        
  1926.        
  1927.         PX=16;
  1928.         PY=48;
  1929.         {
  1930.         unsigned color=0xff00ff00;
  1931.         unsigned temp=time_playing_old/60;
  1932.         int lh=20+4*(SCR_HEIGHT<=480);
  1933.      
  1934.         SetTexture(CROSS_PATTERN);
  1935.         DrawRoundFillBox(PX-8, PY, 120, 32, 0, 0xff302000);
  1936.         SetTexture(NULL_PATTERN);
  1937.         DrawRoundBox(PX-8, PY, 120, 32, 0, 2, 0xff804000);
  1938.         if((status==AUDIO_STATUS_PAUSED && (flash & 16)!=0) || status!=AUDIO_STATUS_PAUSED || fast)
  1939.             {
  1940.             if(temp/100)
  1941.                 {
  1942.                 SetTexture(&icon_tex[9+temp/100]);
  1943.                 DrawFillBox(PX,PY+4, 20, lh, 0,color);PX+=20;
  1944.                 }
  1945.             else PX+=10;
  1946.  
  1947.             temp %=100;
  1948.             SetTexture(&icon_tex[9]+temp/10);
  1949.             DrawFillBox(PX,PY+4, 20, lh, 0,color);PX+=20;temp %=10;
  1950.             SetTexture(&icon_tex[9]+temp);
  1951.             DrawFillBox(PX,PY+4, 20, lh, 0,color);PX+=16;
  1952.  
  1953.             SetTexture(&icon_tex[19]);
  1954.             DrawFillBox(PX,PY+4, 20, lh, 0,color);PX+=16;
  1955.  
  1956.             temp=time_playing_old % 60;
  1957.             SetTexture(&icon_tex[9+temp/10]);
  1958.             DrawFillBox(PX,PY+4, 20, lh, 0,color);PX+=20;temp %=10;
  1959.             SetTexture(&icon_tex[9+temp]);
  1960.             DrawFillBox(PX,PY+4, 20, lh, 0,color);
  1961.             }
  1962.  
  1963.         // total time
  1964.         PX=548-32;
  1965.  
  1966.         temp=time_total/60;
  1967.  
  1968.         SetTexture(CROSS_PATTERN);
  1969.         DrawRoundFillBox(PX-8, PY, 120, 32, 0, 0xff302000);
  1970.         SetTexture(NULL_PATTERN);
  1971.         DrawRoundBox(PX-8, PY, 120, 32, 0, 2, 0xff804000);
  1972.        
  1973.         if(temp/100)
  1974.             {
  1975.             SetTexture(&icon_tex[9+temp/100]);
  1976.             DrawFillBox(PX,PY+4, 20, lh, 0,color);PX+=20;
  1977.             }
  1978.         else PX+=10;
  1979.  
  1980.         temp %=100;
  1981.         SetTexture(&icon_tex[9]+temp/10);
  1982.         DrawFillBox(PX,PY+4, 20, lh, 0,color);PX+=20;temp %=10;
  1983.         SetTexture(&icon_tex[9]+temp);
  1984.         DrawFillBox(PX,PY+4, 20, lh, 0,color);PX+=16;
  1985.  
  1986.         SetTexture(&icon_tex[19]);
  1987.         DrawFillBox(PX,PY+4, 20, lh, 0,color);PX+=16;
  1988.  
  1989.         temp=time_total % 60;
  1990.         SetTexture(&icon_tex[9+temp/10]);
  1991.         DrawFillBox(PX,PY+4, 20, lh, 0,color);PX+=20;temp %=10;
  1992.         SetTexture(&icon_tex[9+temp]);
  1993.         DrawFillBox(PX,PY+4, 20, lh, 0,color);
  1994.        
  1995.         SetTexture(NULL_PATTERN);
  1996.         }
  1997.         //s_printf("%2.2u:%2.2u ",time_playing/60, time_playing % 60);
  1998.  
  1999.         sizeletter=4;
  2000.        /* color=0xff00f080;s_printf("Length: ");
  2001.         color=0xffffffff;
  2002.        
  2003.         s_printf("%2.2u:%2.2u",time_total/60, time_total % 60);*/
  2004.        
  2005.  
  2006.     max_entry=(SCR_HEIGHT-16-88)/20;
  2007.  
  2008.     PX=24;PY=SCR_HEIGHT-24;color=0xffffffff;
  2009.  
  2010.     m=0;
  2011.    
  2012.     if(mode==0) if(play_file>=0) select=play_file;
  2013.  
  2014.     if(nmusic_files>0)
  2015.     for(n=(select-max_entry/2)*(nmusic_files>=max_entry);n<select+max_entry;n++)
  2016.             {
  2017.             if(n<0) continue;
  2018.             if(n>=nmusic_files) break;
  2019.             if(m>=max_entry) break;
  2020.            
  2021.            
  2022.             PX=16;PY=88+8+2+m*20;
  2023.             color=0xffffffff;
  2024.            
  2025.             if(n==play_file)
  2026.                 {
  2027.  
  2028.                 DrawRoundFillBox(PX,PY-2, 38*16, 20, 0, 0xff004f00);
  2029.                
  2030.                 //DrawRoundBox(PX,PY-2, 38*16, 20, 0, 2, 0xff30ffff);
  2031.  
  2032.                 }
  2033.  
  2034.             if(mode!=0)
  2035.             if(n==select)
  2036.                 {
  2037.  
  2038.                 DrawRoundFillBox(PX,PY-2, 38*16, 20, 0, 0xff00004f);
  2039.                
  2040.                 DrawRoundBox(PX,PY-2, 38*16, 20, 0, 2, 0xff30ffff);
  2041.  
  2042.                 }
  2043.  
  2044.    
  2045.                 s_printf("%s", (char *) get_name_short_fromUTF8(&music_files[n].name[0]));
  2046.        
  2047.            
  2048.             bkcolor=0;color=0xffffffff;
  2049.             m++;
  2050.             }
  2051.  
  2052.     if(osd_volume>0)
  2053.         {
  2054.         osd_volume--;
  2055.        
  2056.         SetTexture(MOSAIC_PATTERN);
  2057.         DrawBox(8,SCR_HEIGHT-16, 640-16, 16, 0, 2, 0xff00c000);
  2058.         SetTexture(MOSAIC_PATTERN);
  2059.         DrawFillBox(12,SCR_HEIGHT-16+4, (640-24)*volume/255, 8, 0, 0xff00c000);
  2060.         }
  2061.  
  2062.     wii_nKeys=wii_joystick_read(1);
  2063.    
  2064.     new_key=wii_nKeys & (~old_key);
  2065.     old_key=wii_nKeys;
  2066.  
  2067.     if(new_key & WII_HOME) break;
  2068.  
  2069.     if(old_key & WII_MINUS) {volume--;if(volume<0) volume=0;SetVolumeAudio(volume);osd_volume=60;}
  2070.     if(old_key & WII_PLUS) {volume++;if(volume>255) volume=255;SetVolumeAudio(volume);osd_volume=60;}
  2071.  
  2072.     if(!(old_key  & (WII_UP | WII_DOWN))) f=0;
  2073.            
  2074.         if(old_key  & WII_UP)
  2075.             {
  2076.             if(f==0) f=2;
  2077.             else if(f & 1) f=2;
  2078.             else {f+=2;if(f>40) {f=34;new_key|=WII_UP;}}
  2079.             }
  2080.         if(old_key  & WII_DOWN)
  2081.             {
  2082.             if(f==0) f=1;
  2083.             else if(!(f & 1)) f=1;
  2084.             else {f+=2;if(f>41) {f=35;new_key|=WII_DOWN;}}
  2085.             }
  2086.  
  2087.     if(mode==0)
  2088.         {
  2089.  
  2090.         if(new_key & WII_LEFT) {select2--;if(select2<0) select2=8;}
  2091.         if(new_key & WII_RIGHT) {select2++;if(select2>8) select2=0;}
  2092.        
  2093.  
  2094.         if(new_key & WII_B)
  2095.             {
  2096.             if(music_playing)
  2097.                 {
  2098.                 n=StatusAudio();
  2099.                
  2100.                 if(n==AUDIO_STATUS_RUNNING) PauseAudio(1);
  2101.                 else if(n==AUDIO_STATUS_PAUSED) PauseAudio(0);
  2102.                
  2103.                 }
  2104.            
  2105.             }
  2106.        
  2107.         if(new_key & WII_A)
  2108.             {
  2109.             if(select2==0) // getfiles
  2110.                 {
  2111.                 select_file(0);
  2112.                 new_key=0;
  2113.                 }
  2114.             else
  2115.             if(select2==1 && left_count==0) // <<
  2116.                 {
  2117.                 left_count=1;
  2118.                 }
  2119.             else
  2120.             if(select2==2 && nmusic_files>0 && right_count==0) // play
  2121.                 {
  2122.                 if(music_playing)
  2123.                     {
  2124.                     n=StatusAudio();
  2125.                     if(play_file==select && n==AUDIO_STATUS_PAUSED)
  2126.                         {
  2127.                         PauseAudio(0);
  2128.                         }
  2129.                     else {time_playing_old=0;music_playing=1;play_file=select;if(PlayAudio(music_files[play_file].name, 0, 0)<0) music_playing=0;}
  2130.                     }
  2131.                 else {time_playing_old=0;music_playing=1;play_file=select;if(PlayAudio(music_files[play_file].name, 0, 0)<0) music_playing=0;}
  2132.                 }
  2133.             else
  2134.             if(select2==3) // pause
  2135.                 {
  2136.                 if(music_playing)
  2137.                     {
  2138.                     n=StatusAudio();
  2139.                    
  2140.                     if(n==AUDIO_STATUS_RUNNING) PauseAudio(1);
  2141.                     else if(n==AUDIO_STATUS_PAUSED) PauseAudio(0);
  2142.                    
  2143.                     }
  2144.                
  2145.                 }
  2146.             else
  2147.             if(select2==4) // stop
  2148.                 {  
  2149.                 StopAudio();fast=0;music_playing=0;
  2150.                 }
  2151.             else
  2152.             if(select2==5) // >>
  2153.                 {  
  2154.                 right_count=1;
  2155.                 }
  2156.             else
  2157.             if(select2==6) // shuffle
  2158.                 {
  2159.                 music_shuffle();
  2160.                 }
  2161.             else
  2162.             if(select2==7) // loop
  2163.                 {
  2164.                 music_loop^=1;
  2165.                 }
  2166.             else
  2167.             if(select2==8) // trash
  2168.                 {
  2169.                 nmusic_files=0;
  2170.                 play_file=-1;
  2171.                 }
  2172.            
  2173.             } // wii_a
  2174.         else
  2175.             if(old_key & WII_A)
  2176.                 {
  2177.                 if(select2==1) // <<
  2178.                     {
  2179.                     if(time_playing>0)
  2180.                     if(left_count>42) {fast=1;PauseAudio(1);left_count=40;time_playing_old-=5;if(time_playing_old<0) time_playing_old=0;time_playing=time_playing_old;/*SetTimeAudio(time_playing_old*1000);*/}
  2181.                     left_count++;
  2182.                     }
  2183.                 if(select2==5) // >>
  2184.                     {
  2185.                     if(time_playing>0)
  2186.                     if(right_count>42) {fast=1;PauseAudio(1);right_count=40;time_playing_old+=5;if(time_playing_old>=time_total) time_playing_old=time_total;time_playing=time_playing_old;/*SetTimeAudio(time_playing_old*1000);*/}
  2187.                     right_count++;
  2188.                     }
  2189.                 }
  2190.             else // << >> liberado
  2191.                 {
  2192.                 if(fast)
  2193.                         {
  2194.                         fast=0;
  2195.                        
  2196.                         SetTimeAudio(time_playing_old*1000);PauseAudio(0);
  2197.                         }
  2198.                 if(left_count>=1 && left_count<40)
  2199.                         {
  2200.                         fast=0;
  2201.                         play_file--;
  2202.                         if(music_loop) if(play_file<0) play_file=nmusic_files-1;
  2203.                         if(play_file>=0 && nmusic_files>0) {time_playing_old=0;music_playing=1;if(PlayAudio(music_files[play_file].name, 0, 0)<0) music_playing=0;}
  2204.                         else {StopAudio();music_playing=0;play_file=-1;}
  2205.                         }
  2206.                
  2207.                 left_count=0;
  2208.                 if(right_count>=1 && right_count<40)
  2209.                         {
  2210.                         fast=0;
  2211.                         play_file++;
  2212.                         if(music_loop) if(play_file>=nmusic_files) play_file=0;
  2213.                         if(play_file<nmusic_files) {time_playing_old=0;music_playing=1;if(PlayAudio(music_files[play_file].name, 0, 0)<0) music_playing=0;}
  2214.                         else {StopAudio();music_playing=0;play_file=-1;}
  2215.                         }
  2216.                
  2217.                 right_count=0;
  2218.                 }
  2219.                
  2220.         }
  2221.     else
  2222.     if(mode==1)
  2223.         {
  2224.         if(new_key & WII_UP) {select--;if(select<0) select=nmusic_files-1;}
  2225.         if(new_key & WII_DOWN) {select++;if(select>=nmusic_files) select=0;}
  2226.  
  2227.         if(music_playing)
  2228.             {
  2229.             if(new_key & WII_RIGHT) right_count=1;
  2230.             else
  2231.                 {
  2232.                 if(old_key & WII_RIGHT)
  2233.                     {
  2234.                     if(time_playing>0)
  2235.                     if(right_count>42) {fast=1;PauseAudio(1);right_count=40;time_playing_old+=5;if(time_playing_old>=time_total) time_playing_old=time_total; time_playing=time_playing_old;/*SetTimeAudio(time_playing_old*1000);*/}
  2236.                     right_count++;
  2237.                     }
  2238.                 else
  2239.                     {
  2240.                     if(fast && right_count)
  2241.                         {
  2242.                         fast=0;
  2243.                    
  2244.                         SetTimeAudio(time_playing_old*1000);PauseAudio(0);
  2245.                         }
  2246.                     if(right_count>=1 && right_count<40)
  2247.                         {
  2248.                         fast=0;
  2249.                         play_file++;
  2250.                         if(music_loop) if(play_file>=nmusic_files) play_file=0;
  2251.                         if(play_file<nmusic_files) {time_playing_old=0;music_playing=1;if(PlayAudio(music_files[play_file].name, 0, 0)<0) music_playing=0;}
  2252.                         else {StopAudio();music_playing=0;play_file=-1;}
  2253.                         }
  2254.                     right_count=0;
  2255.                     }
  2256.                 }
  2257.  
  2258.             if(new_key & WII_LEFT) left_count=1;
  2259.             else
  2260.                 {
  2261.                 if(old_key & WII_LEFT)
  2262.                     {
  2263.                     if(time_playing>0)
  2264.                     if(left_count>42) {fast=1;PauseAudio(1);left_count=40;time_playing_old-=5;if(time_playing_old<0) time_playing_old=0; time_playing=time_playing_old;/*SetTimeAudio(time_playing_old*1000);*/}
  2265.                     left_count++;
  2266.                     }
  2267.                 else
  2268.                     {
  2269.                     if(fast && left_count)
  2270.                         {
  2271.                         fast=0;
  2272.                         SetTimeAudio(time_playing_old*1000);PauseAudio(0);
  2273.                         }
  2274.                     if(left_count>=1 && left_count<40)
  2275.                         {
  2276.                         play_file--;
  2277.                         if(music_loop) if(play_file<0) play_file=nmusic_files-1;
  2278.                         if(play_file>=0 && nmusic_files>0) {time_playing_old=0;music_playing=1;if(PlayAudio(music_files[play_file].name, 0, 0)<0) music_playing=0;}
  2279.                         else {StopAudio();music_playing=0;play_file=-1;}
  2280.                         }
  2281.                     left_count=0;
  2282.                     }
  2283.                 }
  2284.            
  2285.             }
  2286.         if(new_key & WII_B)
  2287.             {
  2288.             if(music_playing)
  2289.                 {
  2290.                 n=StatusAudio();
  2291.                
  2292.                 if(n==AUDIO_STATUS_RUNNING) PauseAudio(1);
  2293.                 else if(n==AUDIO_STATUS_PAUSED) PauseAudio(0);
  2294.                
  2295.                 }
  2296.            
  2297.             }
  2298.  
  2299.         if(new_key & WII_A)
  2300.             {
  2301.             if(fast)
  2302.                 {
  2303.                 fast=0;
  2304.            
  2305.                 SetTimeAudio(time_playing_old*1000);PauseAudio(0);
  2306.                 }
  2307.             if(music_playing)
  2308.                 {
  2309.                 n=StatusAudio();
  2310.                 if(play_file==select && n==AUDIO_STATUS_PAUSED)
  2311.                     {
  2312.                     PauseAudio(0);
  2313.                     }
  2314.                 else
  2315.                 if(play_file==select && n==AUDIO_STATUS_RUNNING)
  2316.                     {
  2317.                     StopAudio();music_playing=0;
  2318.                     }
  2319.                 else if(nmusic_files>0){time_playing_old=0;music_playing=1;play_file=select;if(PlayAudio(music_files[play_file].name, 0, 0)<0) music_playing=0;}
  2320.                 }
  2321.             else if(nmusic_files>0){time_playing_old=0;music_playing=1;play_file=select;if(PlayAudio(music_files[play_file].name, 0, 0)<0) music_playing=0;}
  2322.             }
  2323.         } // mode==1
  2324.  
  2325.     if(mode==0 && nmusic_files>0)
  2326.         {
  2327.         if(new_key & (WII_ONE | WII_DOWN)) mode=1;
  2328.         if(new_key & WII_TWO)
  2329.             {if(fast)
  2330.                 {
  2331.                 fast=0;
  2332.  
  2333.                 SetTimeAudio(time_playing_old*1000);PauseAudio(0);
  2334.                 }
  2335.             music_gfx();
  2336.             }
  2337.         }
  2338.     else
  2339.     if(mode==1)
  2340.         {
  2341.         if(new_key & WII_ONE) mode=0;
  2342.         if(new_key & WII_TWO)
  2343.             {if(fast)
  2344.                 {
  2345.                 fast=0;
  2346.  
  2347.                 SetTimeAudio(time_playing_old*1000);PauseAudio(0);
  2348.                 }
  2349.             music_gfx();
  2350.             }//mode=1; // to do: mode=2 visual effects
  2351.         }
  2352.    
  2353.     Screen_flip();
  2354.    
  2355.     if(loop_music_playing()) {select=play_file;time_playing_old=0;}
  2356.    
  2357.     }
  2358. if(fast)
  2359.     {
  2360.     fast=0;
  2361.  
  2362.     SetTimeAudio(time_playing_old*1000);PauseAudio(0);
  2363.     }
  2364. Screen_flip();
  2365. SetTexture(NULL);
  2366. }
  2367.  
  2368.  
  2369.  
  2370. void txt_player()
  2371. {
  2372. unsigned time1,time2;
  2373.  
  2374.  
  2375. if(txt_file_len==0) return;
  2376.  
  2377.  
  2378. time1=time2=ticks_to_msecs(gettick());
  2379.  
  2380.  
  2381.  
  2382. while(1)
  2383.     {
  2384.     if(exit_by_reset) break;
  2385.  
  2386.     loop_music_playing();
  2387.  
  2388.     SetTexture(MOSAIC_PATTERN);
  2389.     DrawFillBox(0,0, 640, SCR_HEIGHT, 0,0xff603000);
  2390.  
  2391.     wii_nKeys=wii_joystick_read(0);
  2392.  
  2393.     new_key=wii_nKeys & (~old_key);
  2394.     old_key=wii_nKeys;
  2395.  
  2396.     if(new_key & WII_HOME) break;
  2397.    
  2398.     time2=ticks_to_msecs(gettick());
  2399.  
  2400.     SelectFontTexture(1);
  2401.     if(time1>time2) time1=time2;
  2402.     if((time2-time1)>60000) text_main(128, new_key, old_key);
  2403.     else text_main(0, new_key, old_key);
  2404.  
  2405.     SelectFontTexture(0);
  2406.     Screen_flip();
  2407.    
  2408.     if((time2-time1)>60000) {write_cfg(path_txt_cfg);time1=time2=ticks_to_msecs(gettick());}
  2409.  
  2410.     if(new_key & WII_ONE) {Screen_flip();write_cfg(path_txt_cfg);music_player();time1=time2=ticks_to_msecs(gettick());}
  2411.  
  2412.     }
  2413. Screen_flip();
  2414. write_cfg(path_txt_cfg);
  2415. }
  2416.  
  2417. void exit_menu()
  2418. {
  2419. int n, m;
  2420. int select=0;
  2421. int loop=1;
  2422.  
  2423. while(loop)
  2424.     {
  2425.     if(exit_by_reset) break;
  2426.  
  2427.     loop_music_playing();
  2428.  
  2429.     //SetTexture(CROSS_PATTERN);
  2430.     //DrawFillBox(0,0, 640, SCR_HEIGHT, 0,0xff603000);
  2431.  
  2432.     animate_background();
  2433.     SetTexture(&icon_tex[icons_num_sprites-2]);
  2434.     DrawFillBox(0,0, 640, SCR_HEIGHT, 0,0xff906000);
  2435.  
  2436.     sizeletter=1;
  2437.     autocenter=1;
  2438.     PX=0;PY=12;
  2439.     color=0xfff08020;
  2440.     SetTexture(NULL_PATTERN);
  2441.     DrawRoundBox(144,8, 640-256-32, 40, 0, 2, 0xfff08020);
  2442.     s_printf("WiiReader");
  2443.    
  2444.     color=0xffffffff;
  2445.     sizeletter=1;
  2446.  
  2447.     for(n=0;n<3;n++)
  2448.         {
  2449.         m=SCR_HEIGHT/2-110+n*110;//48+n*110+50+20;
  2450.  
  2451.         SetTexture(MOSAIC_PATTERN);
  2452.         DrawRoundFillBox(32,m-32, 640-64, 64, 0,0xff803000);
  2453.  
  2454.         SetTexture(NULL_PATTERN);
  2455.         if(n==select)
  2456.             DrawRoundBox(32,m-32, 640-64, 64, 0, 10, 0xff00f0f0);
  2457.         else
  2458.             DrawRoundBox(32,m-32, 640-64, 64, 0, 6, 0xfff08020);
  2459.  
  2460.         switch(n)
  2461.             {
  2462.             case 0:
  2463.                 autocenter=1;
  2464.                 PX=0;PY=m-16;
  2465.                 if(return_reset==1)
  2466.                     s_printf("Homebrew Channel");
  2467.                 else
  2468.                     s_printf("Reset");
  2469.  
  2470.                 autocenter=0;break;
  2471.             case 1:
  2472.                 autocenter=1;
  2473.                 PX=0;PY=m-16;
  2474.                 s_printf("Power Off");
  2475.                 autocenter=0;break;
  2476.             case 2:
  2477.                 autocenter=1;
  2478.                 PX=0;PY=m-16;
  2479.                 s_printf("Abort");
  2480.                 autocenter=0;break;
  2481.             }
  2482.         }
  2483.  
  2484.  
  2485.     sizeletter=4;
  2486.     PX=24;PY=SCR_HEIGHT-24;color=0xffffffff;
  2487.  
  2488.     wii_nKeys=wii_joystick_read(1);
  2489.    
  2490.     new_key=wii_nKeys & (~old_key);
  2491.     old_key=wii_nKeys;
  2492.  
  2493.     if(new_key & WII_UP) {select--;if(select<0) select=2;}
  2494.     if(new_key & WII_DOWN) {select++;if(select>2) select=0;}
  2495.  
  2496.  
  2497.     Screen_flip();
  2498.    
  2499.     if(new_key & WII_A)
  2500.         {
  2501.         switch(select)
  2502.             {
  2503.             case 0:
  2504.                 exit_by_reset=return_reset;
  2505.                 exit(0);
  2506.                 break;
  2507.             case 1:
  2508.                 exit_by_reset=3;
  2509.                 exit(0);
  2510.                 break;
  2511.             case 2:
  2512.                 loop=0;
  2513.                 break;
  2514.             }
  2515.         }
  2516.    
  2517.     if(new_key & WII_B) break;
  2518.  
  2519.     }
  2520. Screen_flip();
  2521. SetTexture(NULL);
  2522. }
  2523.  
  2524. void main_menu()
  2525. {
  2526. int n,m;
  2527.  
  2528. int last_menu=-1;
  2529.  
  2530. static int select=0;
  2531.  
  2532. while(1)
  2533.     {
  2534.     if(exit_by_reset) break;
  2535.  
  2536.     loop_music_playing();
  2537.  
  2538.     //SetTexture(CROSS_PATTERN);
  2539.    
  2540.     animate_background();
  2541.     SetTexture(&icon_tex[icons_num_sprites-2]);
  2542.     DrawFillBox(0,0, 640, SCR_HEIGHT, 0,0xff906000);
  2543.  
  2544.     sizeletter=1;
  2545.     autocenter=1;
  2546.     PX=0;PY=12;
  2547.     color=0xfff08020;
  2548.     SetTexture(NULL_PATTERN);
  2549.     DrawRoundBox(144,8, 640-256-32, 40, 0, 2, 0xfff08020);
  2550.     s_printf("WiiReader");
  2551.     sizeletter=2;
  2552.  
  2553.     PX=0;PY=48+3*100+50+48;color=0xffffffff;
  2554.     s_printf("%s","\251 Francisco Muñoz 'Hermes' - v1.1");
  2555.     autocenter=0;
  2556.     sizeletter=1;
  2557.     for(n=0;n<4;n++)
  2558.         {
  2559.         m=48+n*100+50;
  2560.  
  2561.         SetTexture(MOSAIC_PATTERN);
  2562.         DrawRoundFillBox(32,m-32, 640-64, 64, 0,0xff803000);
  2563.  
  2564.         SetTexture(NULL_PATTERN);
  2565.         if(n==select)
  2566.             DrawRoundBox(32,m-32, 640-64, 64, 0, 10, 0xff00f0f0);
  2567.         else
  2568.             DrawRoundBox(32,m-32, 640-64, 64, 0, 6, 0xfff08020);
  2569.  
  2570.         switch(n)
  2571.             {
  2572.             case 0:
  2573.                 autocenter=1;
  2574.                 PX=0;PY=m-16;
  2575.                 s_printf("JPEG Viewer");
  2576.                 autocenter=0;break;
  2577.             case 1:
  2578.                 autocenter=1;
  2579.                 PX=0;PY=m-16;
  2580.                 s_printf("Music Player");
  2581.                 autocenter=0;break;
  2582.             case 2:
  2583.                 autocenter=1;
  2584.                 PX=0;PY=m-16;
  2585.                 s_printf("TXT Reader");
  2586.                 autocenter=0;break;
  2587.             case 3:
  2588.                 autocenter=1;
  2589.                 PX=0;PY=m-16;
  2590.                 s_printf("Exit");
  2591.                 autocenter=0;break;
  2592.             }
  2593.         }
  2594.  
  2595.  
  2596.     sizeletter=4;
  2597.     PX=24;PY=SCR_HEIGHT-24;color=0xffffffff;
  2598.  
  2599.     wii_nKeys=wii_joystick_read(1);
  2600.    
  2601.     new_key=wii_nKeys & (~old_key);
  2602.     old_key=wii_nKeys;
  2603.  
  2604.     if(new_key & WII_UP) {select--;if(select<0) select=3;}
  2605.     if(new_key & WII_DOWN) {select++;if(select>3) select=0;}
  2606.  
  2607.  
  2608.     Screen_flip();
  2609.  
  2610.     if(new_key & WII_B)
  2611.         {
  2612.         switch(last_menu)
  2613.             {
  2614.             case 0:
  2615.                 last_menu=0;
  2616.                 jpeg_player();
  2617.                 break;
  2618.             case 1:
  2619.                 last_menu=1;
  2620.                 music_player();
  2621.                 break;
  2622.             case 2:
  2623.                 last_menu=2;
  2624.                 txt_player();
  2625.                 break;
  2626.             }
  2627.         }
  2628.    
  2629.     if(new_key & WII_A)
  2630.         {
  2631.         switch(select)
  2632.             {
  2633.             case 0:
  2634.                 last_menu=0;
  2635.                 select_file(1);
  2636.                 jpeg_player();
  2637.                 break;
  2638.             case 1:
  2639.                 last_menu=1;
  2640.                 music_player();
  2641.                 break;
  2642.             case 2:
  2643.                 last_menu=2;
  2644.                 select_file(2);
  2645.                 txt_player();
  2646.                 break;
  2647.             case 3:
  2648.                 exit_menu();
  2649.                 break;
  2650.             }
  2651.         }
  2652.  
  2653.    
  2654.     }
  2655. Screen_flip();
  2656. SetTexture(NULL);
  2657. }
  2658.  
  2659. void credits()
  2660. {
  2661. int n;
  2662.  
  2663. for(n=0;n<6*60;n++)
  2664.     {
  2665.     animate_background();
  2666.     SetTexture(&icon_tex[icons_num_sprites-2]);
  2667.     DrawFillBox(0,0, 640, SCR_HEIGHT, 0,0xff906000);
  2668.  
  2669.     sizeletter=1;
  2670.     autocenter=1;
  2671.     PX=0;PY=12;
  2672.     color=0xfff08020;
  2673.     SetTexture(NULL_PATTERN);
  2674.     DrawRoundBox(144,8, 640-256-32, 40, 0, 2, 0xfff08020);
  2675.     s_printf("WiiReader");
  2676.  
  2677.     sizeletter=2;
  2678.     color=0xffffffff;
  2679.     PY=60;
  2680.     PX=0;s_printf("%s","\251 Francisco Muñoz 'Hermes' - v1.1");PY+=24;
  2681.     PX=0;s_printf("Under General Public License (GPL) Terms");PY+=60;
  2682.    
  2683.     color=0xff00ffff;
  2684.     PX=0;s_printf("This software is based in part on the");PY+=24;
  2685.     PX=0;s_printf("work of the Independent JPEG Group");PY+=24;
  2686.     PX=0;s_printf("Copyright (C) 1994-1998, Thomas G. Lane");PY+=32;
  2687.  
  2688.     PX=0;s_printf("Also based in libmpg123 copyrighted by ");PY+=24;
  2689.     PX=0;s_printf("Michael Hipp under LGPL 2.1");PY+=32;
  2690.  
  2691.     PX=0;s_printf("Also based in the 'Tremor' lib");PY+=24;
  2692.     PX=0;s_printf("Copyright (c) 2002, Xiph.org Foundation");PY+=32;
  2693.  
  2694.     PX=0;s_printf("Also based in libogc/DevkitPro");PY+=24;
  2695.     PX=0;s_printf("Copyright: Michael Wiedenbauer (shagkur)");PY+=24;
  2696.     PX=0;s_printf("and Dave Murphy (WinterMute)");PY+=32;
  2697.  
  2698.     PX=0;s_printf("Also based in unzip lib");PY+=24;
  2699.     PX=0;s_printf("Copyright (C) 1998 Gilles Vollant");
  2700.  
  2701.  
  2702.    
  2703.     autocenter=0;
  2704.    
  2705.     Screen_flip();
  2706.     }
  2707.  
  2708. }
  2709.  
  2710. int main(int argc, char *argv[])
  2711. {
  2712.  
  2713. int n;
  2714.  
  2715. DIR *dir;
  2716.  
  2717.  
  2718. return_reset=1;
  2719.  
  2720. if(argc<1) return_reset=2;
  2721.  
  2722. CONF_Init();
  2723.  
  2724. is_16_9=CONF_GetAspectRatio();
  2725.  
  2726.     InitScreen();
  2727.  
  2728.     srand(gettick());
  2729.  
  2730.    //console_init(frameBuffer[0],20,20,screenMode->fbWidth,screenMode->xfbHeight,screenMode->fbWidth*VI_DISPLAY_PIX_SZ);  
  2731.                          
  2732.  
  2733.     SYS_SetResetCallback(reset_call); // esto es para que puedas salir al pulsar boton de RESET
  2734.     SYS_SetPowerCallback(power_call); // esto para apagar con power
  2735.  
  2736.     PAD_Init();
  2737.     WPAD_Init();
  2738.     WPAD_SetIdleTimeout(60*5); // 5 minutes
  2739.    
  2740.     //WPAD_SetVRes(WPAD_CHAN_ALL, 640, SCR_HEIGHT);
  2741.        
  2742.  
  2743.     THREAD_PRIORITY();
  2744.  
  2745.     ASND_Init(/*INIT_RATE_48000*/); // Initialize the Sound Lib
  2746.  
  2747.     ASND_Pause(0);
  2748.     ASND_PauseVoice(0, 0);
  2749.  
  2750.  
  2751.     atexit(fun_exit);
  2752.  
  2753.  
  2754.     VIDEO_WaitVSync();
  2755.    
  2756.     fatInit(8, false);
  2757.  
  2758.     sleep(2);
  2759.  
  2760.     fatInitDefault();
  2761.    
  2762.     have_device=0;
  2763.  
  2764.     {
  2765.     path_file[3]=48+3;
  2766.     path_txt_cfg[3]=48+3;
  2767.    
  2768.     for(n=0;n<5;n++)
  2769.         {
  2770.         dir = opendir(path_file);
  2771.         if (dir) {closedir(dir); have_device|=1;fatInitDefault();break;}
  2772.         usleep(200*1000);
  2773.         }
  2774.  
  2775.     path_file[3]=48+3;
  2776.  
  2777.     for(n=0;n<5;n++)
  2778.         {
  2779.         dir = opendir(path_file);
  2780.         if (dir) {closedir(dir);  have_device|=2;fatInitDefault();break;}
  2781.         usleep(200*1000);
  2782.         }
  2783.     }
  2784.    
  2785.    
  2786.     current_device=0;
  2787.     if(have_device & 1) path_file[3]=48+3;
  2788.     else if(have_device & 2) {current_device=1; path_file[3]=48+4;}
  2789.  
  2790.     for(n=0;n<icons_num_sprites-2;n++)
  2791.     {
  2792.     CreateTexture(&icon_tex[n], TLUT_SRGB5A1, icons_sprites[n].ptr, icons_sprites[n].sx, icons_sprites[n].sy, 0);
  2793.     }
  2794.  
  2795.     credits();
  2796.     //sleep(1);
  2797.  
  2798.  
  2799. jpeg_buf=memalign(32,1024*1024*4);
  2800.  
  2801.  
  2802. sprintf(path_txt_cfg,"%sapps", path_file);
  2803. if(have_device & 1)
  2804.     mkdir(path_txt_cfg, S_IREAD | S_IWRITE);
  2805.  
  2806. //usleep(1000*500);
  2807. sprintf(path_txt_cfg,"%sapps/wiireader", path_file);
  2808. if(have_device & 1)
  2809.     mkdir(path_txt_cfg, S_IREAD | S_IWRITE);
  2810. sprintf(path_txt_cfg,"%sapps/wiireader/textreader.cfg", path_file);
  2811. usleep(1000*500);
  2812. sprintf(path_cfg,"%sapps/wiireader/font.bin", path_file);
  2813.  
  2814. {
  2815. FILE *fp;
  2816. fp=fopen(path_cfg,"r"); // lee el fichero de texto
  2817. if(fp!=0)
  2818.     {
  2819.     n=fread(extern_char_set,1, 28672 ,fp);
  2820.     if(n==28672) {UploadFontTextureExt((void *) extern_char_set);}
  2821.     fclose(fp);
  2822.     }
  2823. }
  2824.  
  2825. sprintf(path_cfg,"%sapps/wiireader/wiireader.cfg", path_file);
  2826.  
  2827. SetVolumeAudio(volume);
  2828. main_menu();
  2829.  
  2830. Screen_flip();
  2831. StopAudio();
  2832. return(0);
  2833.  
  2834. }
  2835.  
  2836. static GXTexObj gfx_tex;
  2837.  
  2838. static u32 gfx_texture[256*128] ATTRIBUTE_ALIGN(32);
  2839. static u32 gfx_buff[256*128] ATTRIBUTE_ALIGN(8);
  2840.  
  2841. static void inline set_gfx_pix(u32 x,u32 y, u32 c)
  2842. {
  2843.     if(x>=256 || y>=128) return;
  2844.  
  2845.     gfx_buff[(y<<8)+x]=c;
  2846. }
  2847.  
  2848. static u32 inline get_gfx_pix(u32 x,u32 y)
  2849. {
  2850.     if(x>=256 || y>=128) return 0xff400040;
  2851.  
  2852. return gfx_buff[(y<<8)+x];
  2853. }
  2854.  
  2855. static void line_gfx(int x1,int y1,int x2,int y2, unsigned color)
  2856. {
  2857. int n,m;
  2858. int a,b,a2,b2;
  2859.  
  2860.     x2-=x1;
  2861.     y2-=y1;
  2862.  
  2863.     a2=x2;if(a2<0) {a2=-a2;x2=-1;} else x2=1;
  2864.     b2=y2;if(b2<0) {b2=-b2;y2=-1;} else y2=1;
  2865.  
  2866.     m=a2;if(b2>m) m=b2;
  2867.  
  2868.     a=0;b=0;
  2869.  
  2870.     for(n=0;n<m;n++)
  2871.         {
  2872.         set_gfx_pix(x1, y1, color);
  2873.         a+=a2;if(a>=m) {a-=m;x1+=x2;}
  2874.         b+=b2;if(b>=m) {b-=m;y1+=y2;}
  2875.         }
  2876. }
  2877.  
  2878. static void gfx_fun()
  2879. {
  2880.  
  2881. int n,m,s;
  2882.  
  2883. int r,g,b;
  2884.  
  2885. for(n=0;n<128;n++)
  2886.     {
  2887.     s=1;if(n>64) s=-1;
  2888.  
  2889.     for(m=0;m<256;m++)
  2890.         {
  2891.         register u32 color;
  2892.  
  2893.         color=get_gfx_pix(m,n);
  2894.  
  2895.         r=(color) & 255;
  2896.         g=(color>>8) & 255;
  2897.         b=(color>>16) & 255;
  2898.  
  2899.    
  2900.  
  2901.         color=get_gfx_pix(m,n-s);
  2902.  
  2903.         r+=(color) & 255;
  2904.         g+=(color>>8) & 255;
  2905.         b+=(color>>16) & 255;
  2906.  
  2907.         color=get_gfx_pix(m-1,n+s);
  2908.  
  2909.         r+=(color) & 255;
  2910.         g+=(color>>8) & 255;
  2911.         b+=(color>>16) & 255;
  2912.  
  2913.         color=get_gfx_pix(m+1,n+s);
  2914.  
  2915.         r+=(color) & 255;
  2916.         g+=(color>>8) & 255;
  2917.         b+=(color>>16) & 255;
  2918.  
  2919.  
  2920.         r>>=2;g>>=2;b>>=2;
  2921.        
  2922.         if(r<32 && g<32 && b<32) b=32;
  2923.  
  2924.         color=((0xff00+b)<<16) | (g<<8) | r;
  2925.  
  2926.  
  2927.         set_gfx_pix(m, n, color);
  2928.        
  2929.         }
  2930.     }
  2931.  
  2932. }
  2933.  
  2934.  
  2935. static void gfx_fun2()
  2936. {
  2937.  
  2938. int n,m,s;
  2939.  
  2940. int r,g,b;
  2941.  
  2942. for(n=0;n<128;n++)
  2943.     {
  2944.     s=1;//if(n>128) s=-1;
  2945.  
  2946.     for(m=0;m<256;m++)
  2947.         {
  2948.         register u32 color;
  2949.  
  2950.         color=get_gfx_pix(m,n);
  2951.  
  2952.         r=(color) & 255;
  2953.         g=(color>>8) & 255;
  2954.         b=(color>>16) & 255;
  2955.  
  2956.    
  2957.  
  2958.         color=get_gfx_pix(m,n-s);
  2959.  
  2960.         r+=(color) & 255;
  2961.         g+=(color>>8) & 255;
  2962.         b+=(color>>16) & 255;
  2963.  
  2964.         color=get_gfx_pix(m-1,n);
  2965.  
  2966.         r+=(color) & 255;
  2967.         g+=(color>>8) & 255;
  2968.         b+=(color>>16) & 255;
  2969.  
  2970.         color=get_gfx_pix(m+1,n);
  2971.  
  2972.         r+=(color) & 255;
  2973.         g+=(color>>8) & 255;
  2974.         b+=(color>>16) & 255;
  2975.  
  2976.         color=get_gfx_pix(m,n+s);
  2977.  
  2978.         r+=(color) & 255;
  2979.         g+=(color>>8) & 255;
  2980.         b+=(color>>16) & 255;
  2981.  
  2982.        
  2983.         r/=5;g/=5;b/=5;
  2984.  
  2985.        
  2986.        
  2987.         color=((0xff00+b)<<16) | (g<<8) | r;
  2988.  
  2989.  
  2990.         set_gfx_pix(m, n, color);
  2991.        
  2992.         }
  2993.     }
  2994.  
  2995. }
  2996.  
  2997.  
  2998.  
  2999. void music_gfx()
  3000. {
  3001.  
  3002. int n,m;
  3003. short l,r,oldl=0,oldr=0;
  3004. u16 *punt;
  3005. int hahaha=0;
  3006.  
  3007. memset(gfx_buff, 0, 256*256/2*4);
  3008.  
  3009. memset(pcm_outsample,0,512*2);
  3010.  
  3011. while(1)
  3012.     {
  3013.     if(exit_by_reset) break;
  3014.     wii_nKeys=wii_joystick_read(1);
  3015.    
  3016.     new_key=wii_nKeys & (~old_key);
  3017.     old_key=wii_nKeys;
  3018.  
  3019.     if(new_key & (WII_HOME | WII_ONE | WII_TWO | WII_A | WII_B)) break;
  3020.  
  3021.    
  3022.    
  3023.  
  3024.     for(n=0;n<256;n+=4)
  3025.         {
  3026.         l=(pcm_outsample[n<<1]>>10)+64;
  3027.         r=(pcm_outsample[(n<<1)+1]>>10)+64;
  3028.        
  3029.         if(hahaha & 128)
  3030.             {
  3031.             set_gfx_pix(n,l,0xff008fff);
  3032.             set_gfx_pix(n,r,0xff00ff8f);
  3033.             }
  3034.         else
  3035.             {
  3036.             line_gfx(n-4,oldl, n, l, 0xff008fff);
  3037.             line_gfx(n-4,oldr, n, r, 0xff008fff);
  3038.             }
  3039.  
  3040.         set_gfx_pix(n+2, -(l<<12), 0xff8f00ff+((l & 255)<<8));
  3041.         set_gfx_pix(n+2, -(r<<12), 0xffff008f+((r & 255)<<8));
  3042.  
  3043.         oldl=l;oldr=r;
  3044.         }
  3045.    
  3046.  
  3047.     for(n=0;n<256;n++)
  3048.         {
  3049.        
  3050.        
  3051.         l=(pcm_outsample[n<<1]>>10)+64;
  3052.         r=(pcm_outsample[(n<<1)+1]>>10)+64;
  3053.        
  3054.    
  3055.         line_gfx(n-1,128-oldl, n, 128-l, 0xff400000+(l<<1));
  3056.         line_gfx(n-1,128-oldr, n, 128-r, 0xffff0000+(r<<1));
  3057.         oldl=l;oldr=r;
  3058.         }
  3059.  
  3060.     hahaha++;
  3061.     if((hahaha & 0xffff)>3600)
  3062.         {
  3063.         if(hahaha>=(65536+3603)) hahaha=0;
  3064.         else
  3065.             if(hahaha>3605 && hahaha<65536) hahaha=65536+3500;
  3066.  
  3067.         punt= (u16 *) icons_sprites[icons_num_sprites-1].ptr;
  3068.  
  3069.         for(n=0;n<96;n++)
  3070.         for(m=0;m<128;m++)
  3071.             {
  3072.             if(punt[((n/3)<<5) | (m>>2)]==0x8000)
  3073.                 set_gfx_pix(64+m, 16+n, 0xff000000);
  3074.             else if(punt[((n>>1)<<5) | (m>>2)])
  3075.                     set_gfx_pix(64+m, 16+n, 0xffffffff);
  3076.             }
  3077.         }
  3078.  
  3079.     gfx_fun();
  3080.  
  3081.     for(n=0;n<256;n+=8)
  3082.         {
  3083.        
  3084.        
  3085.         l=(pcm_outsample[n<<1]>>10)+64;
  3086.         r=(pcm_outsample[(n<<1)+1]>>10)+64;
  3087.        
  3088.    
  3089.         line_gfx(n-8,oldl, n, l, 0xffff8f00+(l<<1));
  3090.         line_gfx(n-8,oldr, n, r, 0xffff008f+((127-r)<<9));
  3091.         oldl=l;oldr=r;
  3092.         }
  3093.  
  3094.    
  3095.     gfx_fun2();
  3096.  
  3097.    
  3098.    
  3099.     memcpy(gfx_texture,gfx_buff, 256*256/2*4);
  3100.    
  3101.     CreateTexture(&gfx_tex, TILE_SRGBA8, (void *) gfx_texture, 256, 256/2, 0);
  3102.     SetTexture(&gfx_tex);
  3103.  
  3104.     DrawFillBox(-40,-40, 640+80, SCR_HEIGHT+80, 0,0xffffffff);
  3105.  
  3106.     DrawFillBox(0,0, 640, SCR_HEIGHT, 0,0x80ffffff);
  3107.  
  3108.  
  3109.     Screen_flip();
  3110.    
  3111.     loop_music_playing();
  3112.  
  3113.  
  3114.    
  3115.     }
  3116. Screen_flip();
  3117. SetTexture(NULL);
  3118. }
  3119.  
  3120. /* end */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement