?login_element?

Subversion Repositories NedoOS

Rev

Rev 2200 | Blame | Compare with Previous | Last modification | View Log | Download

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <intrz80.h>
  4. #include <stdlib.h>
  5. #include <oscalls.h>
  6. #include <../common/terminal.c>
  7. #include <tcp.h>
  8. #include <osfs.h>
  9. #include <ctype.h>
  10. #include <math.h>
  11.  
  12. #define true 1
  13. #define false 0
  14. #define COMMANDLINE 0x0080
  15.  
  16. unsigned int RBR_THR = 0xf8ef;
  17. unsigned int IER = 0xf9ef;
  18. unsigned int IIR_FCR = 0xfaef;
  19. unsigned int LCR = 0xfbef;
  20. unsigned int MCR = 0xfcef;
  21. unsigned int LSR = 0xfdef;
  22. unsigned int MSR = 0xfeef;
  23. unsigned int SR = 0xffef;
  24. unsigned int divider = 1;
  25. unsigned char comType = 0;
  26. unsigned int espType = 32;
  27.  
  28. const unsigned char sendOk[] = "SEND OK";
  29. const unsigned char gotWiFi[] = "WIFI GOT IP";
  30. unsigned char minRating[] = "0000000000";
  31. const unsigned char userAgent[] = " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS; Radio)\r\n\r\n\0";
  32. const unsigned char cmdlist1[] = "GET /file/id:";
  33. unsigned char userQuery[256] = "/api/export:zxMusic/limit:10/filter:zxMusicId=44816";
  34. unsigned char fileName[] = "radio/player.ovl";
  35. unsigned char appCmd[128] = "player.com ";
  36. unsigned char curPath[128];
  37.  
  38. unsigned char ver[] = "3.1";
  39.  
  40. unsigned char queryType[64];
  41. unsigned char netbuf[4096];
  42. unsigned char dataBuffer[8192];
  43. unsigned char crlf[2] = {13, 10};
  44. unsigned char formats[4][4] = {"pt3", "pt2", "tfc", "ts"};
  45. unsigned char interfaces[2][8] = {"NedoNET", "ESP-COM"};
  46. unsigned char cmd[256];
  47. unsigned char link[512];
  48. unsigned char toLog[256];
  49. unsigned char queryNum;
  50.  
  51. struct sockaddr_in targetadr;
  52. struct readstructure readStruct;
  53. struct sockaddr_in dnsaddress;
  54.  
  55. unsigned long contLen;
  56. long count;
  57. unsigned char saveFlag, saveBak, rptFlag, netDriver;
  58. unsigned char status, key, curFormat;
  59. union APP_PAGES main_pg;
  60. union APP_PAGES player_pg;
  61. unsigned int loaded;
  62. unsigned int headlng;
  63. unsigned char cutOff = 1;
  64. int remainTime;
  65.  
  66. struct fileStruct
  67. {
  68.   long picId;
  69.   unsigned long fileSize;
  70.   unsigned int picYear;
  71.   unsigned long totalAmount;
  72.   unsigned int curPos;
  73.   unsigned int startBar;
  74.   unsigned int trackInSeconds;
  75.   unsigned char time[16];
  76.   unsigned char picRating[8];
  77.   unsigned char trackName[256];
  78.   unsigned char fileName[256];
  79.   unsigned char authorIds[64];
  80.   unsigned char authorTitle[64];
  81.   unsigned char authorRealName[64];
  82.   unsigned char afn[64];
  83.   unsigned char tfn[64];
  84.   unsigned char fileName2[256];
  85. } curFileStruct;
  86.  
  87. struct window
  88. {
  89.   unsigned char x;
  90.   unsigned char y;
  91.   unsigned char w;
  92.   unsigned char h;
  93.   unsigned char text;
  94.   unsigned char back;
  95.   unsigned char tittle[80];
  96. } curWin;
  97.  
  98. void writeLog(char *logline)
  99. {
  100.   FILE *LogFile;
  101.   unsigned long fileSize;
  102.  
  103.   LogFile = OS_OPENHANDLE("m:/bin/radio/radio.log", 0x80);
  104.   if (((int)LogFile) & 0xff)
  105.   {
  106.     LogFile = OS_CREATEHANDLE("m:/bin/radio/radio.log", 0x80);
  107.     OS_CLOSEHANDLE(LogFile);
  108.     LogFile = OS_OPENHANDLE("m:/bin/radio/radio.log", 0x80);
  109.   }
  110.  
  111.   fileSize = OS_GETFILESIZE(LogFile);
  112.   OS_SEEKHANDLE(LogFile, fileSize);
  113.   OS_WRITEHANDLE(logline, LogFile, strlen(logline));
  114.   OS_CLOSEHANDLE(LogFile);
  115. }
  116.  
  117. void delay(unsigned long counter)
  118. {
  119.   unsigned long start, finish;
  120.   counter = counter / 20;
  121.   if (counter < 1)
  122.   {
  123.     counter = 1;
  124.   }
  125.   start = time();
  126.   finish = start + counter;
  127.  
  128.   while (start < finish)
  129.   {
  130.     start = time();
  131.   }
  132. }
  133.  
  134. void spaces(unsigned char number)
  135. {
  136.   while (number > 0)
  137.   {
  138.     putchar(' ');
  139.     number--;
  140.   }
  141. }
  142.  
  143. void clearStatus(void)
  144. {
  145.   OS_SETCOLOR(5);
  146.   OS_SETXY(0, 24);
  147.   spaces(79);
  148.   putchar('\r');
  149. }
  150.  
  151. void printProgress(unsigned char type)
  152. {
  153.   unsigned char bar, minutes, seconds;
  154.   unsigned char *position;
  155.   long barLenght;
  156.   int timer;
  157.   switch (type)
  158.   {
  159.   case 0: // print empty bar
  160.     OS_SETXY(5, 10);
  161.     OS_SETCOLOR(70);
  162.     printf("%02u:%02u", 0, 0);
  163.     OS_SETXY(14, 10);
  164.     OS_SETCOLOR(71);
  165.     for (bar = 0; bar < 50; bar++)
  166.     {
  167.       putchar(176);
  168.     }
  169.     putchar(' ');
  170.     putchar(' ');
  171.     minutes = atoi(curFileStruct.time);
  172.     position = (strstr(curFileStruct.time, ":")) + 1;
  173.     seconds = atoi(position);
  174.     curFileStruct.trackInSeconds = minutes * 60 + seconds;
  175.     curFileStruct.curPos = 0;
  176.     curFileStruct.startBar = 0;
  177.     break;
  178.   case 1: // print progress bar
  179.  
  180.     OS_SETXY(5, 10);
  181.     OS_SETCOLOR(70);
  182.     timer = floor(curFileStruct.curPos / 60);
  183.     printf("%02u:%02u", timer, (curFileStruct.curPos - (timer * 60)));
  184.  
  185.     barLenght = (curFileStruct.curPos * 50 / curFileStruct.trackInSeconds);
  186.     if (barLenght > 49)
  187.     {
  188.       barLenght = 50;
  189.     }
  190.     OS_SETXY(14 + curFileStruct.startBar, 10);
  191.     OS_SETCOLOR(71);
  192.     for (bar = 0; bar < barLenght - curFileStruct.startBar; bar++)
  193.     {
  194.       putchar(178);
  195.     }
  196.     OS_SETXY(0, 0);
  197.     curFileStruct.startBar = bar;
  198.     break;
  199.   case 2: // print full bar
  200.     OS_SETXY(14, 10);
  201.     OS_SETCOLOR(71);
  202.     for (bar = 0; bar < 50; bar++)
  203.     {
  204.       putchar(178);
  205.     }
  206.     break;
  207.   }
  208. }
  209.  
  210. void printHelp(void)
  211. {
  212.   OS_SETXY(0, 14);
  213.   OS_SETCOLOR(71);
  214.   printf(" [<-] [B] Previous track          [->] [ ] Next track      \r\n");
  215.   printf(" [S]  Stop player                 [R]  Repeat track mode   \r\n");
  216.   printf(" [K]  Toggle saving tracks        [D]  Download track      \r\n");
  217.   printf(" [Q]  Select Query type           [F]  Select tracks format\r\n");
  218.   printf(" [I]  Interface ZXNETUSB/ESP32    [J]  Jump to NNNN file   \r\n");
  219.   printf(" [ESC] Exit to OS                 [M]  Minimal Rating(Q:2,3)\r\n");
  220.   printf("                                                           \r\n");
  221. }
  222.  
  223. void printStatus(void)
  224. {
  225.   OS_SETXY(0, 8);
  226.   OS_SETCOLOR(70);
  227.   printf(" [Q]Query : ");
  228.   OS_SETCOLOR(71);
  229.   printf("%s", queryType);
  230.   printf("  ");
  231.   OS_SETXY(0, 23);
  232.   OS_SETCOLOR(95);
  233.   printf("                                                                                ");
  234.   OS_SETXY(1, 23);
  235.   printf(" [F]Format: ");
  236.   OS_SETCOLOR(94);
  237.   printf("%s", formats[curFormat]);
  238.   OS_SETCOLOR(95);
  239.   printf(" [K]Keep files: ");
  240.   OS_SETCOLOR(94);
  241.   printf("%u", saveFlag);
  242.   OS_SETCOLOR(95);
  243.   printf(" [R]Repeat: ");
  244.   OS_SETCOLOR(94);
  245.   printf("%u", rptFlag);
  246.   OS_SETCOLOR(95);
  247.   printf(" [J]Jump to ");
  248.   printf(" [E]Exit        [%s]", ver);
  249.  
  250.   OS_SETCOLOR(71);
  251.   YIELD();
  252. }
  253.  
  254. void printInfo(void)
  255. {
  256.   BDBOX(30, 2, 50, 6, 71, ' ');
  257.   OS_SETXY(0, 1);
  258.   OS_SETCOLOR(70);
  259.   printf(" #: ");
  260.   OS_SETCOLOR(71);
  261.   printf("%lu", count);
  262.   OS_SETCOLOR(70);
  263.   printf(" ID: ");
  264.   OS_SETCOLOR(71);
  265.   printf("%lu", curFileStruct.picId);
  266.   OS_SETCOLOR(70);
  267.   printf(" Total Tracks: ");
  268.   OS_SETCOLOR(71);
  269.   printf("%lu               \r\n", curFileStruct.totalAmount);
  270.   OS_SETCOLOR(70);
  271.   printf(" RATING: ");
  272.   OS_SETCOLOR(71);
  273.   printf("%s", curFileStruct.picRating);
  274.   OS_SETCOLOR(70);
  275.   printf(" YEAR: ");
  276.   OS_SETCOLOR(71);
  277.   printf("%u", curFileStruct.picYear);
  278.   OS_SETCOLOR(70);
  279.   printf(" DURATION: ");
  280.   OS_SETCOLOR(71);
  281.   printf("%s", curFileStruct.time);
  282.   printf(" \r\n\r\n");
  283.   OS_SETCOLOR(70);
  284.   printf(" AuthorsIDs ");
  285.   OS_SETCOLOR(71);
  286.   printf("%s", curFileStruct.authorIds);
  287.   OS_SETCOLOR(70);
  288.   printf(" Author: ");
  289.   OS_SETCOLOR(71);
  290.   printf("%s", curFileStruct.authorTitle);
  291.   OS_SETCOLOR(70);
  292.   printf(" Real name: ");
  293.   OS_SETCOLOR(71);
  294.   printf("%s", curFileStruct.authorRealName);
  295.   printf(" \r\n\r\n");
  296.   OS_SETCOLOR(69);
  297.   printf("                                                                           \r");
  298.   printf("   TITLE: %s\r\n", curFileStruct.trackName);
  299. }
  300.  
  301. void refreshScreen(void)
  302. {
  303.   OS_CLS(0);
  304.   printInfo();
  305.   printProgress(0);
  306.   printProgress(1);
  307.   printHelp();
  308.   printStatus();
  309. }
  310.  
  311. int pos(unsigned char *s, unsigned char *c, unsigned int n, unsigned int startPos)
  312. {
  313.   unsigned int i, j;
  314.   unsigned int lenC, lenS;
  315.  
  316.   for (lenC = 0; c[lenC]; lenC++)
  317.     ;
  318.   for (lenS = 0; s[lenS]; lenS++)
  319.     ;
  320.  
  321.   for (i = startPos; i <= lenS - lenC; i++)
  322.   {
  323.     for (j = 0; s[i + j] == c[j]; j++)
  324.       ;
  325.  
  326.     if (j - lenC == 1 && i == lenS - lenC && !(n - 1))
  327.       return i;
  328.     if (j == lenC)
  329.       if (n - 1)
  330.         n--;
  331.       else
  332.         return i;
  333.   }
  334.   return -1;
  335. }
  336.  
  337. ///////////////////////////
  338. #include <../common/esp-com.c>
  339. #include <../common/network.c>
  340. //////////////////////////
  341.  
  342. int cutHeader(unsigned int todo)
  343. {
  344.   unsigned int err;
  345.   unsigned char *count1;
  346.  
  347.   err = httpError();
  348.   if (err != 200)
  349.   {
  350.  
  351.     OS_CLS(0);
  352.     puts(netbuf);
  353.     puts("^^^^^^^^^^^^^^^^^^^^^");
  354.     printf("HTTP response:[%u]\r\n", err);
  355.     getchar();
  356.     refreshScreen();
  357.   }
  358.   count1 = strstr(netbuf, "Content-Length:");
  359.   if (count1 == NULL)
  360.   {
  361.     OS_CLS(0);
  362.     puts(netbuf);
  363.     puts("^^^^^^^^^^^^^^^^^^^^^");
  364.     printf("contLen  not found \r\n");
  365.     contLen = 0;
  366.     getchar();
  367.     refreshScreen();
  368.   }
  369.   else
  370.   {
  371.     contLen = atol(count1 + 15);
  372.     // printf("Content-Length: %lu \n\r", contLen);
  373.   }
  374.  
  375.   count1 = strstr(netbuf, "\r\n\r\n");
  376.   if (count1 == NULL)
  377.   {
  378.     clearStatus();
  379.     printf("end of header not found\r\n");
  380.   }
  381.   else
  382.   {
  383.     headlng = ((unsigned int)count1 - (unsigned int)netbuf + 4);
  384.     // printf("header %u bytes\r\n", headlng);
  385.   }
  386.   return todo - headlng;
  387. }
  388.  
  389. unsigned char inputBox(struct window w, unsigned char *prefilled)
  390. {
  391.   unsigned char wcount, tempx, tittleStart;
  392.   unsigned char byte, counter;
  393.   w.h++;
  394.   OS_SETXY(w.x, w.y - 1);
  395.   BDBOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
  396.   OS_SETXY(w.x, w.y);
  397.   OS_SETCOLOR(w.text);
  398.   putchar(201);
  399.   for (wcount = 0; wcount < w.w; wcount++)
  400.   {
  401.     putchar(205);
  402.   }
  403.   putchar(187);
  404.   OS_SETXY(w.x, w.y + w.h);
  405.   putchar(200);
  406.   for (wcount = 0; wcount < w.w; wcount++)
  407.   {
  408.     putchar(205);
  409.   }
  410.   putchar(188);
  411.  
  412.   tempx = w.x + w.w + 1;
  413.   for (wcount = 1; wcount < w.h; wcount++)
  414.   {
  415.     OS_SETXY(w.x, w.y + wcount);
  416.     putchar(186);
  417.     OS_SETXY(tempx, w.y + wcount);
  418.     putchar(186);
  419.   }
  420.   tittleStart = w.x + (w.w / 2) - (strlen(w.tittle) / 2);
  421.   OS_SETXY(tittleStart, w.y);
  422.   printf("[%s]", w.tittle);
  423.   OS_SETXY(w.x + 1, w.y + 1);
  424.   OS_SETCOLOR(w.back);
  425.   putchar(219);
  426.  
  427.   cmd[0] = 0;
  428.  
  429.   counter = strlen(prefilled);
  430.   if (counter != 0)
  431.   {
  432.     strcpy(cmd, prefilled);
  433.     goto skipKeys;
  434.   }
  435.  
  436.   do
  437.   {
  438.     byte = OS_GETKEY();
  439.     if (byte != 0)
  440.     {
  441.       switch (byte)
  442.       {
  443.       case 0x08:
  444.         if (counter > 0)
  445.         {
  446.           counter--;
  447.           cmd[counter] = 0;
  448.         }
  449.         break;
  450.       case 0x0d:
  451.  
  452.         if (counter == 0)
  453.         {
  454.           return false;
  455.         }
  456.         else
  457.         {
  458.           return true;
  459.         }
  460.  
  461.       case 31:
  462.         break;
  463.       case 250:
  464.         break;
  465.       case 249:
  466.         break;
  467.       case 248:
  468.         break;
  469.       case 251: // Right
  470.         break;
  471.       case 252: // Del
  472.         OS_SETXY(w.x + 1, w.y + 1);
  473.         spaces(counter + 1);
  474.         cmd[0] = 0;
  475.         counter = 0;
  476.         break;
  477.       case 27:
  478.         cmd[0] = 0;
  479.         return false;
  480.       default:
  481.         if (counter < w.w - 1)
  482.         {
  483.           cmd[counter] = byte;
  484.           counter++;
  485.           cmd[counter] = 0;
  486.         }
  487.         break;
  488.       }
  489.     skipKeys:
  490.       OS_SETXY(w.x + 1, w.y + 1);
  491.       printf("%s", cmd);
  492.       putchar(219);
  493.       if (byte == 0x08)
  494.       {
  495.         putchar(' ');
  496.       }
  497.     }
  498.     YIELD();
  499.   } while (42);
  500.   return false;
  501. }
  502.  
  503. char *str_replace(char *dst, int num, const char *str, const char *orig, const char *rep)
  504. {
  505.   const char *ptr;
  506.   size_t len1 = strlen(orig);
  507.   size_t len2 = strlen(rep);
  508.   char *tmp = dst;
  509.  
  510.   num -= 1;
  511.   while ((ptr = strstr(str, orig)) != NULL)
  512.   {
  513.     num -= (ptr - str) + len2;
  514.     if (num < 1)
  515.       break;
  516.  
  517.     strncpy(dst, str, (size_t)(ptr - str));
  518.     dst += ptr - str;
  519.     strncpy(dst, rep, len2);
  520.     dst += len2;
  521.     str = ptr + len1;
  522.   }
  523.  
  524.   for (; (*dst = *str) && (num > 0); --num)
  525.   {
  526.     ++dst;
  527.     ++str;
  528.   }
  529.   return tmp;
  530. }
  531.  
  532. const char *parseJson(unsigned char *property)
  533. {
  534.   unsigned int w, lng, lngp1, findEnd, listPos;
  535.   unsigned char terminator;
  536.   int n;
  537.   n = -1;
  538.   netbuf[0] = 0;
  539.   n = pos(dataBuffer, property, 1, 0);
  540.   if (n == -1)
  541.   {
  542.     strcpy(netbuf, "-");
  543.     return netbuf;
  544.   }
  545.   lng = n - 1 + strlen(property);
  546.   if (dataBuffer[lng] == ':')
  547.   {
  548.     terminator = '\0';
  549.   }
  550.   if (dataBuffer[lng] == '\"')
  551.   {
  552.     terminator = '\"';
  553.   }
  554.   if (dataBuffer[lng] == '[')
  555.   {
  556.     terminator = ']';
  557.   }
  558.  
  559.   findEnd = 1;
  560.   lngp1 = lng + 1;
  561.  
  562.   while (42)
  563.   {
  564.  
  565.     if ((dataBuffer[lngp1 + findEnd] == ','))
  566.     {
  567.       if (terminator == '\0')
  568.       {
  569.         break;
  570.       }
  571.       if ((dataBuffer[lng + findEnd] == terminator))
  572.       {
  573.         findEnd--;
  574.         break;
  575.       }
  576.     }
  577.     findEnd++;
  578.   }
  579.   listPos = 0;
  580.   for (w = lngp1; w < findEnd + lngp1; w++)
  581.   {
  582.     netbuf[listPos] = dataBuffer[w];
  583.     listPos++;
  584.   }
  585.   netbuf[listPos] = 0;
  586.   return netbuf;
  587. }
  588.  
  589. void convert866(void)
  590. {
  591.   unsigned int lng, targetPos, w, q = 0;
  592.   unsigned char buffer[8], one, two;
  593.   unsigned int decVal;
  594.   lng = strlen(netbuf);
  595.   targetPos = lng + 1;
  596.  
  597.   while (q < lng)
  598.   {
  599.     one = netbuf[q];
  600.     two = netbuf[q + 1];
  601.     if (one == 92 && two == 117)
  602.     {
  603.       q = q + 2;
  604.       for (w = 0; w < 4; w++)
  605.       {
  606.         buffer[w] = netbuf[q + w];
  607.       }
  608.       q = q + 4;
  609.       buffer[4] = '\0';
  610.       decVal = (unsigned int)strtol(buffer, NULL, 16);
  611.  
  612.       if (decVal < 1088)
  613.       {
  614.         decVal = decVal - 912;
  615.       }
  616.       if (decVal > 1087)
  617.       {
  618.         decVal = decVal - 864;
  619.       }
  620.       if (decVal == 1025)
  621.       {
  622.         decVal = 240;
  623.       }
  624.       if (decVal == 1105)
  625.       {
  626.         decVal = 241;
  627.       }
  628.       netbuf[targetPos] = decVal;
  629.     }
  630.     else
  631.     {
  632.       netbuf[targetPos] = netbuf[q];
  633.       q++;
  634.     }
  635.     targetPos++;
  636.   }
  637.   netbuf[targetPos] = '\0';
  638.  
  639.   for (w = lng + 1; w < targetPos + 1; w++)
  640.   {
  641.     netbuf[w - lng - 1] = netbuf[w];
  642.   }
  643. }
  644.  
  645. void nameRepair(unsigned char *pfn, unsigned int tfnSize)
  646. {
  647.   str_replace(pfn, tfnSize, pfn, "\\", "_");
  648.   str_replace(pfn, tfnSize, pfn, "/", "_");
  649.   str_replace(pfn, tfnSize, pfn, ":", "_");
  650.   str_replace(pfn, tfnSize, pfn, "*", "_");
  651.   str_replace(pfn, tfnSize, pfn, "?", "_");
  652.   str_replace(pfn, tfnSize, pfn, "<", "_");
  653.   str_replace(pfn, tfnSize, pfn, ">", "_");
  654.   str_replace(pfn, tfnSize, pfn, "|", "_");
  655.   str_replace(pfn, tfnSize, pfn, " ", "_");
  656.   str_replace(pfn, tfnSize, pfn, "&#039;", "'");
  657.   str_replace(pfn, tfnSize, pfn, "&amp;", "&");
  658.   str_replace(pfn, tfnSize, pfn, "&quot;", "'");
  659.   str_replace(pfn, tfnSize, pfn, "&gt;", ")");
  660.   str_replace(pfn, tfnSize, pfn, "&lt;", "(");
  661.   str_replace(pfn, tfnSize, pfn, "\"", "'");
  662. }
  663.  
  664. void stringRepair(unsigned char *pfn, unsigned int tSize)
  665. {
  666.   str_replace(pfn, tSize, pfn, "&#039;", "'");
  667.   str_replace(pfn, tSize, pfn, "&amp;", "&");
  668.   str_replace(pfn, tSize, pfn, "&gt;", ">");
  669.   str_replace(pfn, tSize, pfn, "&lt;", "<");
  670.   str_replace(pfn, tSize, pfn, "&quot;", "\"");
  671.   str_replace(pfn, tSize, pfn, "\\/", "/");
  672. }
  673.  
  674. void ncReplace(void)
  675. {
  676.   unsigned char len;
  677.   for (len = 0; len < strlen(curFileStruct.afn); len++)
  678.   {
  679.     if (curFileStruct.afn[len] < ' ')
  680.     {
  681.       curFileStruct.afn[len] = '_';
  682.     }
  683.   }
  684.  
  685.   for (len = 0; len < strlen(curFileStruct.tfn); len++)
  686.   {
  687.     if (curFileStruct.tfn[len] < ' ')
  688.     {
  689.       curFileStruct.tfn[len] = '_';
  690.     }
  691.   }
  692. }
  693.  
  694. unsigned char saveBuf(unsigned long fileId, unsigned char operation, unsigned int sizeOfBuf)
  695. {
  696.   FILE *fp2;
  697.   unsigned long fileSize;
  698.   unsigned char afnSize, tfnSize;
  699.   unsigned char fileIdChar[10];
  700.  
  701.   if (operation == 00)
  702.   {
  703.  
  704.     if (saveFlag == 0)
  705.     {
  706.       sprintf(curFileStruct.fileName, "temp.%s", formats[curFormat]);
  707.     }
  708.     else
  709.     {
  710.       afnSize = sizeof(curFileStruct.afn) - 1;
  711.       tfnSize = sizeof(curFileStruct.tfn) - 1;
  712.  
  713.       strcpy(curFileStruct.afn, curFileStruct.authorTitle);
  714.       nameRepair(curFileStruct.afn, afnSize);
  715.       strcpy(curFileStruct.tfn, curFileStruct.trackName);
  716.       nameRepair(curFileStruct.tfn, tfnSize);
  717.       sprintf(curFileStruct.fileName, "%s-%s.%s", curFileStruct.afn, curFileStruct.tfn, formats[curFormat]);
  718.       ncReplace();
  719.  
  720.       if (strlen(curFileStruct.fileName) > 63)
  721.       {
  722.         sprintf(fileIdChar, "-%ld", fileId);
  723.         str_replace(curFileStruct.fileName, sizeof(curFileStruct.fileName) - 1, curFileStruct.fileName, fileIdChar, "");
  724.         curFileStruct.fileName[50] = '\0';
  725.         strcat(curFileStruct.fileName, fileIdChar);
  726.         strcat(curFileStruct.fileName, formats[curFormat]);
  727.       }
  728.     }
  729.     OS_SETSYSDRV();
  730.     OS_MKDIR("../downloads/radio"); // Create if not exist
  731.     OS_CHDIR("../downloads/radio");
  732.     fp2 = OS_CREATEHANDLE(curFileStruct.fileName, 0x80);
  733.     if (((int)fp2) & 0xff)
  734.     {
  735.       clearStatus();
  736.       printf("%s creating error. Check for  downloads\\radio folder.", curFileStruct.fileName);
  737.       getchar();
  738.       exit(0);
  739.     }
  740.     OS_CLOSEHANDLE(fp2);
  741.     return 0;
  742.   }
  743.  
  744.   if (operation == 01)
  745.   {
  746.     fp2 = OS_OPENHANDLE(curFileStruct.fileName, 0x80);
  747.     if (((int)fp2) & 0xff)
  748.     {
  749.  
  750.       clearStatus();
  751.       printf("%s opening error.", curFileStruct.fileName);
  752.       exit(0);
  753.     }
  754.     fileSize = OS_GETFILESIZE(fp2);
  755.     OS_SEEKHANDLE(fp2, fileSize);
  756.     OS_WRITEHANDLE(netbuf + headlng, fp2, sizeOfBuf);
  757.     OS_CLOSEHANDLE(fp2);
  758.     return 0;
  759.   }
  760.  
  761.   if (operation == 02)
  762.   {
  763.     OS_CLOSEHANDLE(fp2);
  764.     return 0;
  765.   }
  766.  
  767.   return 0;
  768. }
  769.  
  770. char getDataNet(void)
  771. {
  772.   unsigned int todo, downloaded;
  773.   unsigned char socket;
  774.   clearStatus();
  775.   socket = OpenSock(AF_INET, SOCK_STREAM);
  776.   testOperation("OS_NETSOCKET", socket);
  777.  
  778.   todo = netConnect(socket, 2);
  779.   testOperation("OS_NETCONNECT", todo);
  780.  
  781.   todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf), 2);
  782.   testOperation("OS_WIZNETWRITE", todo);
  783.  
  784.   downloaded = 0;
  785.   do
  786.   {
  787.     headlng = 0;
  788.     todo = tcpRead(socket, 2);
  789.     clearStatus();
  790.     testOperation("OS_WIZNETREAD", todo);
  791.     if (todo == 0)
  792.     {
  793.       break;
  794.     }
  795.     if (downloaded == 0)
  796.     {
  797.       todo = cutHeader(todo);
  798.     }
  799.  
  800.     if (downloaded + todo > sizeof(dataBuffer))
  801.     {
  802.       clearStatus();
  803.       printf("dataBuffer overrun...");
  804.       break;
  805.     }
  806.     memcpy(dataBuffer + downloaded, netbuf + headlng, todo);
  807.     downloaded = downloaded + todo;
  808.   } while (downloaded < contLen);
  809.   netShutDown(socket, 1);
  810.   return true;
  811. }
  812.  
  813. unsigned int getDataEsp(void)
  814. {
  815.   unsigned char sizeLink;
  816.   unsigned long downloaded;
  817.   unsigned char byte, count = 0;
  818.   unsigned int todo;
  819.   unsigned char *count1;
  820.  
  821.   strcpy(link, netbuf);
  822.   sizeLink = strlen(link);
  823.   do
  824.   {
  825.     sendcommand("AT+CIPSTART=\"TCP\",\"zxart.ee\",80");
  826.     getAnswer2(); // CONNECT or ERROR or link is not valid
  827.     count1 = strstr(netbuf, "CONNECT");
  828.   } while (count1 == NULL);
  829.  
  830.   getAnswer2();                                   // OK
  831.   sprintf(netbuf, "AT+CIPSEND=%u", sizeLink + 2); // second CRLF in send command
  832.   sendcommand(netbuf);
  833.   getAnswer2();
  834.   do
  835.   {
  836.     byte = uart_readBlock();
  837.     // putchar(byte);
  838.   } while (byte != '>');
  839.   sendcommand(link);
  840.   count = 0;
  841.   do
  842.   {
  843.     byte = uart_readBlock();
  844.     if (byte == sendOk[count])
  845.     {
  846.       count++;
  847.     }
  848.     else
  849.     {
  850.       count = 0;
  851.     }
  852.   } while (count < strlen(sendOk));
  853.   uart_readBlock(); // CR
  854.   uart_readBlock(); // LF
  855.   downloaded = 0;
  856.   do
  857.   {
  858.     headlng = 0;
  859.     todo = recvHead();
  860.     getdataEsp(todo); // Requested size
  861.     if (downloaded == 0)
  862.     {
  863.       todo = cutHeader(todo);
  864.     }
  865.     memcpy(dataBuffer + downloaded, netbuf + headlng, todo);
  866.     downloaded = downloaded + todo;
  867.   } while (downloaded < contLen);
  868.   sendcommand("AT+CIPCLOSE");
  869.   getAnswer2(); // CLOSED
  870.   getAnswer2(); // OK
  871.   return 0;
  872. }
  873.  
  874. long processJson(unsigned long startPos, unsigned char limit, unsigned char queryNum)
  875. {
  876.   FILE *fp3;
  877.   unsigned int tSize;
  878.   unsigned char *count, result;
  879.   clearStatus();
  880.   printf("Getting data(%u)...", queryNum);
  881.  
  882.   switch (queryNum)
  883.   {
  884.   case 0:
  885.     sprintf(netbuf, "GET /api/export:zxMusic/limit:%u/start:%lu/filter:zxMusicFormat=%s/order:date,desc%s", limit, startPos, formats[curFormat], userAgent);
  886.     break;
  887.   case 1:
  888.     startPos = 0;
  889.     sprintf(netbuf, "GET /api/types:zxMusic/export:zxMusic/language:eng/limit:%u/start:%lu/order:votes,rand/filter:zxMusicMinRating=%s;zxMusicFormat=%s%s", limit, startPos, minRating, formats[curFormat], userAgent);
  890.     break;
  891.   case 2:
  892.     startPos = 0;
  893.     sprintf(netbuf, "GET /api/types:zxMusic/export:zxMusic/language:eng/limit:%u/start:%lu/order:rand/filter:zxMusicMinRating=%s;zxMusicFormat=%s%s", limit, startPos, minRating, formats[curFormat], userAgent);
  894.     break;
  895.  
  896.   case 3:
  897.     fp3 = OS_OPENHANDLE("../ini/user.que", 0x80);
  898.     if (((int)fp3) & 0xff)
  899.     {
  900.       fp3 = OS_CREATEHANDLE("../ini/user.que", 0x80);
  901.       OS_WRITEHANDLE(userQuery, fp3, sizeof(userQuery));
  902.       OS_CLOSEHANDLE(fp3);
  903.       fp3 = OS_OPENHANDLE("../ini/user.que", 0x80);
  904.     }
  905.     OS_READHANDLE(userQuery, fp3, sizeof(userQuery));
  906.     OS_CLOSEHANDLE(fp3);
  907.     sprintf(netbuf, "GET /api/limit:%u/start:%lu%s%s", limit, startPos, userQuery, userAgent);
  908.     break;
  909.   case 99:
  910.     sprintf(netbuf, "GET /jsonElementData/elementId:%lu%s", startPos, userAgent);
  911.     break;
  912.   }
  913.  
  914.   switch (netDriver)
  915.   {
  916.   case 0:
  917.     result = getDataNet();
  918.     break;
  919.   case 1:
  920.     result = getDataEsp();
  921.     break;
  922.   }
  923.   clearStatus();
  924.   printf("Processing data (%u)...", queryNum);
  925.  
  926.   count = strstr(dataBuffer, "responseStatus\":\"success");
  927.   if (count == NULL)
  928.   {
  929.     OS_CLS(0);
  930.     OS_SETCOLOR(66);
  931.     puts("dataBuffer[]:");
  932.     puts(dataBuffer);
  933.     puts("---------------");
  934.     printf("PROCESS JSON: [ERROR: Bad responseStatus.] [Query:%u] [Track:%lu]\r\n", queryNum, startPos);
  935.     YIELD();
  936.     getchar();
  937.     return -1;
  938.   }
  939.   count = strstr(dataBuffer, "\"id\":");
  940.   if (count == NULL)
  941.   {
  942.     parseJson("\"totalAmount\":");
  943.  
  944.     if (atol(netbuf) == 0)
  945.     {
  946.       return -3;
  947.     }
  948.  
  949.     if (netbuf[0] != '-')
  950.     {
  951.       return -4;
  952.     }
  953.  
  954.     OS_CLS(0);
  955.     OS_SETCOLOR(66);
  956.     puts("dataBuffer[]:");
  957.     puts(dataBuffer);
  958.     puts("---------------");
  959.     printf("PROCESS JSON: [ERROR: ID not found] [Query:%u] [Track:%lu]", queryNum, startPos);
  960.     YIELD();
  961.     getchar();
  962.     return -2;
  963.   }
  964.   if (queryNum < 4)
  965.   {
  966.     netbuf[0] = '\0';
  967.     parseJson("\"id\":");
  968.     curFileStruct.picId = atol(netbuf);
  969.     parseJson(",\"title\":\"");
  970.     convert866();
  971.     strcpy(curFileStruct.trackName, netbuf);
  972.  
  973.     tSize = sizeof(curFileStruct.trackName);
  974.     stringRepair(curFileStruct.trackName, tSize);
  975.  
  976.     parseJson("\"rating\":\"");
  977.     strcpy(curFileStruct.picRating, netbuf);
  978.     parseJson("\"year\":\"");
  979.     curFileStruct.picYear = atoi(netbuf);
  980.     parseJson("\"totalAmount\":");
  981.     curFileStruct.totalAmount = atol(netbuf);
  982.     parseJson("\"time\":\"");
  983.     strcpy(curFileStruct.time, netbuf);
  984.     parseJson("\"authorIds\":[");
  985.     strcpy(curFileStruct.authorIds, netbuf);
  986.     parseJson("\"authorIds\":[");
  987.     strcpy(curFileStruct.fileName2, netbuf);
  988.   }
  989.   if (queryNum == 99)
  990.   {
  991.     parseJson(",\"title\":\"");
  992.     convert866();
  993.     strcpy(curFileStruct.authorTitle, netbuf);
  994.     parseJson(",\"realName\":\"");
  995.     convert866();
  996.     strcpy(curFileStruct.authorRealName, netbuf);
  997.   }
  998.   return curFileStruct.picId;
  999. }
  1000.  
  1001. unsigned char getTrack2(unsigned long fileId)
  1002. {
  1003.   int todo;
  1004.   char socket;
  1005.   unsigned int packSize = 2000;
  1006.   unsigned long downloaded;
  1007.   unsigned char try = 0, byte = 0;
  1008.   unsigned int count;
  1009.   unsigned char *count1;
  1010.   clearStatus();
  1011.   printf("Getting track...");
  1012.   sprintf(netbuf, "GET /file/id:%lu%s", fileId, userAgent);
  1013.   if (netDriver == 0)
  1014.   {
  1015.     socket = OpenSock(AF_INET, SOCK_STREAM);
  1016.     clearStatus();
  1017.     testOperation("OS_NETSOCKET", socket);
  1018.  
  1019.     todo = netConnect(socket, 10);
  1020.     testOperation("OS_NETCONNECT", todo);
  1021.  
  1022.     todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf), 10);
  1023.     testOperation("OS_WIZNETWRITE", todo);
  1024.     saveBuf(curFileStruct.picId, 00, 0);
  1025.     downloaded = 0;
  1026.     do
  1027.     {
  1028.       headlng = 0;
  1029.       todo = tcpRead(socket, 10);
  1030.       testOperation("OS_WIZNETREAD", todo);
  1031.  
  1032.       if (todo == 0)
  1033.       {
  1034.         break;
  1035.       }
  1036.       if (downloaded == 0)
  1037.       {
  1038.         todo = cutHeader(todo);
  1039.       }
  1040.       saveBuf(curFileStruct.picId, 01, todo);
  1041.       downloaded = downloaded + todo;
  1042.     } while (downloaded < contLen);
  1043.     netShutDown(socket, 0);
  1044.   }
  1045.   else
  1046.   {
  1047.     strcpy(link, netbuf);
  1048.     saveBuf(curFileStruct.picId, 00, 0);
  1049.     do
  1050.     {
  1051.       sendcommand("AT+CIPSTART=\"TCP\",\"zxart.ee\",80");
  1052.       getAnswer2(); // CONNECT or ERROR or link is not valid
  1053.       count1 = strstr(netbuf, "CONNECT");
  1054.     } while (count1 == NULL);
  1055.  
  1056.     getAnswer2(); // OK
  1057.  
  1058.     sprintf(cmd, "AT+CIPSEND=%u", strlen(link) + 2); // second CRLF in send command
  1059.     sendcommand(cmd);
  1060.     getAnswer2();
  1061.  
  1062.     do
  1063.     {
  1064.       byte = uart_readBlock();
  1065.       // putchar(byte);
  1066.     } while (byte != '>');
  1067.     sendcommand(link);
  1068.     count = 0;
  1069.  
  1070.     do
  1071.     {
  1072.       byte = uart_readBlock();
  1073.       if (byte == sendOk[count])
  1074.       {
  1075.         count++;
  1076.       }
  1077.       else
  1078.       {
  1079.         count = 0;
  1080.       }
  1081.     } while (count < strlen(sendOk));
  1082.     uart_readBlock(); // CR
  1083.     uart_readBlock(); // LF
  1084.     downloaded = 0;
  1085.     do
  1086.     {
  1087.       headlng = 0;
  1088.       todo = recvHead();
  1089.       getdataEsp(todo); // Requested size
  1090.       if (downloaded == 0)
  1091.       {
  1092.         todo = cutHeader(todo);
  1093.       }
  1094.       downloaded = downloaded + todo;
  1095.       saveBuf(curFileStruct.picId, 01, todo);
  1096.     } while (downloaded < contLen);
  1097.     sendcommand("AT+CIPCLOSE");
  1098.     getAnswer2(); // CLOSED
  1099.     getAnswer2(); // OK
  1100.     saveBuf(curFileStruct.picId, 02, 0);
  1101.   }
  1102.   return 0;
  1103. }
  1104.  
  1105. unsigned char runPlayer(void)
  1106. {
  1107.   FILE *fp2;
  1108.   unsigned long playerSize, loaded, loop;
  1109.   unsigned char pgbak;
  1110.   clearStatus();
  1111.   printf("Running player...");
  1112.   sprintf(appCmd, "player.com %s", curFileStruct.fileName);
  1113.   player_pg.l = OS_GETMAINPAGES();
  1114.   pgbak = main_pg.pgs.window_3;
  1115.   loaded = 0;
  1116.   loop = 0;
  1117.   OS_GETPATH((unsigned int)&curPath);
  1118.   OS_SETSYSDRV();
  1119.   fp2 = OS_OPENHANDLE(fileName, 0x80);
  1120.   if (((int)fp2) & 0xff)
  1121.   {
  1122.     clearStatus();
  1123.     printf("%s", fileName);
  1124.     printf(" not found.");
  1125.     exit(0);
  1126.   }
  1127.   playerSize = OS_GETFILESIZE(fp2);
  1128.   OS_CHDIR(curPath);
  1129.   OS_NEWAPP((unsigned int)&player_pg);
  1130.   SETPG32KHIGH(player_pg.pgs.window_3);
  1131.   memcpy((char *)(0xC080), &appCmd, sizeof(appCmd));
  1132.  
  1133.   do
  1134.   {
  1135.     loaded = OS_READHANDLE(dataBuffer, fp2, sizeof(dataBuffer));
  1136.     memcpy((char *)(0xC100 + loop), &dataBuffer, loaded);
  1137.     loop = loop + loaded;
  1138.   } while (loop < playerSize);
  1139.  
  1140.   OS_CLOSEHANDLE(fp2);
  1141.   SETPG32KHIGH(pgbak);
  1142.   OS_RUNAPP(player_pg.pgs.pId);
  1143.  
  1144.   return player_pg.pgs.pId;
  1145. }
  1146.  
  1147. long trackSelector(unsigned char mode)
  1148. {
  1149.   switch (mode)
  1150.   {
  1151.   case 0: // Next track
  1152.     count++;
  1153.     if (count > curFileStruct.totalAmount - 1)
  1154.     {
  1155.       count = 0;
  1156.     }
  1157.     break;
  1158.   case 1: // Prev. track
  1159.     count--;
  1160.     if (count < 0)
  1161.     {
  1162.       count = curFileStruct.totalAmount - 1;
  1163.     }
  1164.     break;
  1165.   }
  1166.   return count;
  1167. }
  1168.  
  1169. unsigned char testPlayer(void)
  1170. {
  1171.   union APP_PAGES player2_pg;
  1172.   player2_pg.l = OS_GETAPPMAINPAGES(player_pg.pgs.pId);
  1173.   if (errno == 0)
  1174.   {
  1175.     return 1;
  1176.   }
  1177.   else
  1178.   {
  1179.     return 0;
  1180.   }
  1181. }
  1182.  
  1183. void infoBox(struct window w, unsigned char *message)
  1184. {
  1185.   unsigned char wcount, tempx, tittleStart;
  1186.  
  1187.   w.h++;
  1188.   OS_SETXY(w.x, w.y - 1);
  1189.   BDBOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
  1190.   OS_SETXY(w.x, w.y);
  1191.   OS_SETCOLOR(w.text);
  1192.   putchar(201);
  1193.   for (wcount = 0; wcount < w.w; wcount++)
  1194.   {
  1195.     putchar(205);
  1196.   }
  1197.   putchar(187);
  1198.   OS_SETXY(w.x, w.y + w.h);
  1199.   putchar(200);
  1200.   for (wcount = 0; wcount < w.w; wcount++)
  1201.   {
  1202.     putchar(205);
  1203.   }
  1204.   putchar(188);
  1205.  
  1206.   tempx = w.x + w.w + 1;
  1207.   for (wcount = 1; wcount < w.h; wcount++)
  1208.   {
  1209.     OS_SETXY(w.x, w.y + wcount);
  1210.     putchar(186);
  1211.     OS_SETXY(tempx, w.y + wcount);
  1212.     putchar(186);
  1213.   }
  1214.   tittleStart = w.x + (w.w / 2) - (strlen(w.tittle) / 2);
  1215.   OS_SETXY(tittleStart, w.y);
  1216.   printf("[%s]", w.tittle);
  1217.  
  1218.   OS_SETXY(w.x + 1, w.y + 1);
  1219.   OS_SETCOLOR(w.back);
  1220.   tittleStart = w.x + (w.w / 2) - (strlen(message) / 2);
  1221.   OS_SETXY(tittleStart, w.y + 1);
  1222.   printf("%s", message);
  1223. }
  1224.  
  1225. /*
  1226. char optionsMenu(void)
  1227. {
  1228.   unsigned char options[7][16] = {"Music format", "Plaing queue", "Net interface", "Keep files", "Minimal rating", "", ""};
  1229.   char line = 0;
  1230.  
  1231.   //  curFormat++;
  1232.   //  queryNum++; //  скопировать строки
  1233.   //  netDriver++;
  1234.   //  saveFlag++;
  1235.   //  minRating++;
  1236.  
  1237.   strcpy(curWin.tittle, "Radio options");
  1238.   curWin.w = 22;
  1239.   curWin.x = 39 - curWin.w / 2;
  1240.   curWin.y = 7;
  1241.   curWin.h = 7;
  1242.   curWin.text = 95;
  1243.   curWin.back = 95;
  1244.   infoBox(curWin, "");
  1245.   curWin.x++;
  1246.   curWin.y++;
  1247.   OS_SETCOLOR(95);
  1248.   while (strlen(options[line]) != 0)
  1249.   {
  1250.     OS_SETXY(curWin.x, curWin.y + line);
  1251.     printf("%s", options[line]);
  1252.     line++;
  1253.   }
  1254.  
  1255.   getchar();
  1256.   return true;
  1257. }
  1258. */
  1259. void refreshQueryNames(int queryNum)
  1260. {
  1261.   switch (queryNum)
  1262.   {
  1263.   case 0:
  1264.     strcpy(queryType, "from newest to oldest                   ");
  1265.     break;
  1266.   case 1:
  1267.     sprintf(queryType, "Random most voted tracks with rating %s+    ", minRating);
  1268.     break;
  1269.   case 2:
  1270.     sprintf(queryType, "Random play with rating %s+                  ", minRating);
  1271.     break;
  1272.   case 3:
  1273.     strcpy(queryType, "User defined query from \"user.que\"     ");
  1274.     break;
  1275.   }
  1276. }
  1277.  
  1278. C_task main(int argc, char *argv[])
  1279. {
  1280.   unsigned char errn, keypress, pId, changedFormat;
  1281.   long iddqd, idkfa;
  1282.   unsigned long curTimer, startTimer, oldTimer;
  1283.   srand(time());
  1284.  
  1285.   OS_HIDEFROMPARENT();
  1286.   OS_SETGFX(0x86);
  1287.   OS_CLS(0);
  1288.  
  1289.   count = 0;
  1290.   saveFlag = 0;
  1291.   queryNum = 0;
  1292.   curFormat = 0;
  1293.   changedFormat = 0;
  1294.   rptFlag = 0;
  1295.   netDriver = 0;
  1296.   strcpy(minRating, "4.0");
  1297.  
  1298.   targetadr.family = AF_INET;
  1299.   targetadr.porth = 00;
  1300.   targetadr.portl = 80;
  1301.   targetadr.b1 = 217; // D9
  1302.   targetadr.b2 = 146; // 92
  1303.   targetadr.b3 = 69;  // 45
  1304.   targetadr.b4 = 13;  // 0D
  1305.  
  1306.   if (argc > 1)
  1307.   {
  1308.     if ((argv[1][0] == 'e') || (argv[1][0] == 'E'))
  1309.     {
  1310.       netDriver = 1;
  1311.       clearStatus();
  1312.       printf("    ESP-COM mode enabled...");
  1313.       BDBOX(1, 14, 80, 8, 71, ' ');
  1314.       OS_SETXY(0, 14);
  1315.       loadEspConfig();
  1316.       uart_init(divider);
  1317.       espReBoot();
  1318.       printHelp();
  1319.     }
  1320.   }
  1321.  
  1322.   strcpy(queryType, "from newest to oldest");
  1323.   OS_CLS(0);
  1324.   OS_SETCOLOR(71);
  1325.   OS_SETCOLOR(95);
  1326.   printf("                           ZXART.EE radio for %s                           ", interfaces[netDriver]);
  1327.   OS_SETCOLOR(6);
  1328.   printStatus();
  1329.  
  1330.   if (netDriver == 0)
  1331.   {
  1332.     get_dns();
  1333.     clearStatus();
  1334.     dnsResolve("zxart.ee");
  1335.   }
  1336. start:
  1337.   OS_SETSYSDRV();
  1338.   printHelp();
  1339.   curFileStruct.fileSize = 0;
  1340.  
  1341.   iddqd = processJson(count, 1, queryNum); // Query for track info
  1342.  
  1343.   switch (iddqd)
  1344.   {
  1345.   case -3: // return 0 pictures
  1346.     strcpy(minRating, "1.0");
  1347.     refreshQueryNames(queryNum);
  1348.     clearStatus();
  1349.     printf("No picture is returned in query. Minimal rating is set to %s", minRating);
  1350.     refreshScreen();
  1351.     goto start;
  1352.   case -4: // return xxxx picture, but empty body.
  1353.     clearStatus();
  1354.     printf("Empty body is returned for %ld. Next picture, please.", count);
  1355.     count++;
  1356.     refreshScreen();
  1357.     goto start;
  1358.   }
  1359.  
  1360.   if (iddqd < 0)
  1361.   {
  1362.     {
  1363.       clearStatus();
  1364.       printf("Error getting track info, next please(%ld)...", iddqd);
  1365.       /*
  1366.         count = trackSelector(0);
  1367.         goto start;
  1368.    */
  1369.  
  1370.       OS_DROPAPP(pId);
  1371.       changedFormat = 1;
  1372.       refreshScreen();
  1373.       goto rekey;
  1374.     }
  1375.   }
  1376.  
  1377.   idkfa = processJson(atol(curFileStruct.authorIds), 0, 99); // Query for AuthorID
  1378.  
  1379.   if (idkfa < 0)
  1380.   {
  1381.     clearStatus();
  1382.     printf("Error getting author %lu", atol(curFileStruct.authorIds));
  1383.     strcpy(curFileStruct.authorTitle, "-");
  1384.     strcpy(curFileStruct.authorRealName, "-");
  1385.   }
  1386.  
  1387.   /////////////////////////////////////////////////
  1388.   // optionsMenu();
  1389.  
  1390. replay:
  1391.  
  1392.   errn = getTrack2(iddqd); // Downloading the track
  1393.  
  1394. resume:
  1395.   startTimer = time();
  1396.   printProgress(0);
  1397.   pId = runPlayer(); // Start the Player!
  1398.   printStatus();
  1399.   printInfo();
  1400. rekey:
  1401.   keypress = OS_GETKEY();
  1402.  
  1403.   switch (keypress)
  1404.   {
  1405.   case 27:
  1406.   case 'e':
  1407.   case 'E':
  1408.     OS_DROPAPP(pId);
  1409.     OS_CLS(0);
  1410.     printf("Good bye...\r\n");
  1411.     OS_SETCOLOR(7);
  1412.     exit(0);
  1413.   case 248:
  1414.   case 'b':
  1415.   case 'B':
  1416.     changedFormat = 0;
  1417.     OS_DROPAPP(pId);
  1418.     clearStatus();
  1419.     printf("Player stopped...");
  1420.     count = trackSelector(1);
  1421.     goto start;
  1422.   case 251:
  1423.   case 32:
  1424.   case 'n':
  1425.   case 'N':
  1426.     changedFormat = 0;
  1427.     OS_DROPAPP(pId);
  1428.     clearStatus();
  1429.     printf("Player stopped...");
  1430.     count = trackSelector(0);
  1431.     goto start;
  1432.   case 'k':
  1433.   case 'K':
  1434.     OS_DROPAPP(pId);
  1435.     clearStatus();
  1436.     printf("Player stopped...");
  1437.     saveFlag = !saveFlag;
  1438.     printStatus();
  1439.     changedFormat = 0;
  1440.     goto replay;
  1441.   case 'q':
  1442.   case 'Q':
  1443.     OS_DROPAPP(pId);
  1444.     clearStatus();
  1445.     printf("Player stopped...");
  1446.     queryNum++;
  1447.     if (queryNum > 3)
  1448.     {
  1449.       queryNum = 0;
  1450.     }
  1451.     refreshQueryNames(queryNum);
  1452.     count = 0;
  1453.     changedFormat = 0;
  1454.     printStatus();
  1455.     goto start;
  1456.   case 'j':
  1457.   case 'J':
  1458.     curWin.w = 22;
  1459.     curWin.x = 80 / 2 - curWin.w / 2 - 2;
  1460.     curWin.y = 14;
  1461.     curWin.h = 1;
  1462.     curWin.text = 103;
  1463.     curWin.back = 103;
  1464.     strcpy(curWin.tittle, "Track number:");
  1465.     if (inputBox(curWin, ""))
  1466.     {
  1467.       sscanf(cmd, "%lu", &count);
  1468.       OS_DROPAPP(pId);
  1469.       if (count > curFileStruct.totalAmount - 1)
  1470.       {
  1471.         count = curFileStruct.totalAmount - 1;
  1472.       }
  1473.       changedFormat = 0;
  1474.       goto start;
  1475.     }
  1476.     printHelp();
  1477.     break;
  1478.   case 'm':
  1479.   case 'M':
  1480.     curWin.w = 22;
  1481.     curWin.x = 80 / 2 - curWin.w / 2 - 2;
  1482.     curWin.y = 14;
  1483.     curWin.h = 1;
  1484.     curWin.text = 103;
  1485.     curWin.back = 103;
  1486.     strcpy(curWin.tittle, "Minimal rating:");
  1487.  
  1488.     if (inputBox(curWin, ""))
  1489.     {
  1490.       char counter;
  1491.       for (counter = 0; counter < strlen(cmd); counter++)
  1492.       {
  1493.         if ((((cmd[counter] < '0') || (cmd[counter] > '9'))) && cmd[counter] != '.')
  1494.         {
  1495.           clearStatus();
  1496.           printf("Wrong input.[%s]", cmd);
  1497.           counter = 0;
  1498.           break;
  1499.         }
  1500.       }
  1501.  
  1502.       if (counter != 0)
  1503.       {
  1504.         strncpy(minRating, cmd, 5);
  1505.         refreshQueryNames(queryNum);
  1506.         count = 0;
  1507.       }
  1508.       refreshScreen();
  1509.     }
  1510.     break;
  1511.   case 'f':
  1512.   case 'F':
  1513.     OS_DROPAPP(pId);
  1514.     clearStatus();
  1515.     printf("Player stopped...");
  1516.     curFormat++;
  1517.     count = -1;
  1518.     if (curFormat > 3)
  1519.     {
  1520.       curFormat = 0;
  1521.     }
  1522.     changedFormat = 1;
  1523.     curFileStruct.totalAmount = 1;
  1524.     if (strstr(formats[curFormat], "tfc") != NULL)
  1525.     {
  1526.       cutOff = 0;
  1527.     }
  1528.     else
  1529.     {
  1530.       cutOff = 0;
  1531.     }
  1532.  
  1533.     printStatus();
  1534.     printProgress(0);
  1535.     BDBOX(1, 2, 80, 6, 71, ' ');
  1536.     goto rekey;
  1537.   case 's':
  1538.   case 'S':
  1539.     OS_DROPAPP(pId);
  1540.     clearStatus();
  1541.     printf("Player stopped...");
  1542.     printProgress(0);
  1543.     changedFormat = 0;
  1544.     getchar();
  1545.     goto resume;
  1546.   case 'r':
  1547.   case 'R':
  1548.     rptFlag = !rptFlag;
  1549.     clearStatus();
  1550.     printStatus();
  1551.     goto rekey;
  1552.   case 'd':
  1553.   case 'D':
  1554.     saveBak = saveFlag;
  1555.     saveFlag = 1;
  1556.  
  1557.     errn = getTrack2(iddqd); // Downloading the track
  1558.  
  1559.     saveFlag = saveBak;
  1560.     clearStatus();
  1561.     printf("File saved: [%s]...", curFileStruct.fileName);
  1562.     goto rekey;
  1563.   case 'i':
  1564.   case 'I':
  1565.     netDriver = !netDriver;
  1566.     if (netDriver == 1)
  1567.     {
  1568.       clearStatus();
  1569.       printf("    ESP-COM mode enabled...");
  1570.       BDBOX(1, 14, 80, 8, 71, ' ');
  1571.       OS_SETXY(0, 14);
  1572.       loadEspConfig();
  1573.       uart_init(divider);
  1574.       espReBoot();
  1575.       printHelp();
  1576.     }
  1577.     else
  1578.     {
  1579.       clearStatus();
  1580.       printf("    ZXNETUSB mode enabled...");
  1581.     }
  1582.     OS_SETXY(0, 0);
  1583.     OS_SETCOLOR(71);
  1584.     OS_SETCOLOR(95);
  1585.     printf("                           ZXART.EE radio for %s                    ", interfaces[netDriver]);
  1586.     OS_SETCOLOR(6);
  1587.     break;
  1588.   case 31:
  1589.     refreshScreen();
  1590.     break;
  1591.   default:
  1592.     break;
  1593.   }
  1594.   curTimer = time();
  1595.   curFileStruct.curPos = (curTimer - startTimer) / 50;
  1596.   /*
  1597.     if ((curTimer - oldTimer) > 49)
  1598.     {
  1599.       alive = testPlayer();
  1600.       sprintf(toLog, ",%u", alive);
  1601.       //writeLog(toLog);
  1602.     }
  1603.     if (alive == 0 && !changedFormat)
  1604.     {
  1605.       if (rptFlag == 1)
  1606.       {
  1607.         goto resume;
  1608.       }
  1609.       //writeLog("\r\nalive == 0. Next track please.\r\n");
  1610.       printProgress(2);
  1611.       count = trackSelector(0);
  1612.       goto start;
  1613.     }
  1614.  
  1615.     if (alive == 1 && ((curTimer - oldTimer) > 49))
  1616.     {
  1617.       sprintf(toLog, ".%u", curFileStruct.trackInSeconds - curFileStruct.curPos);
  1618.       //writeLog(toLog);
  1619.       printProgress(1);
  1620.       oldTimer = curTimer;
  1621.     }
  1622.   */
  1623.  
  1624.   remainTime = curFileStruct.trackInSeconds - curFileStruct.curPos;
  1625.  
  1626.   if ((remainTime < cutOff) && !changedFormat)
  1627.   {
  1628.  
  1629.     OS_DROPAPP(pId);
  1630.     clearStatus();
  1631.     printf("Player stopped...");
  1632.  
  1633.     if (rptFlag == 1)
  1634.     {
  1635.       goto resume;
  1636.     }
  1637.     printProgress(2);
  1638.     count = trackSelector(0);
  1639.     goto start;
  1640.   }
  1641.   if ((curTimer - oldTimer) > 49 && !changedFormat)
  1642.   {
  1643.     printProgress(1);
  1644.     oldTimer = curTimer;
  1645.   }
  1646.   YIELD();
  1647.   goto rekey;
  1648. }
  1649.