home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.67.zip / src / goattrk2.c < prev    next >
C/C++ Source or Header  |  2008-07-21  |  33KB  |  1,303 lines

  1. //
  2. // GOATTRACKER v2.67
  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17. //
  18.  
  19. #define GOATTRK2_C
  20.  
  21. #ifdef __WIN32__
  22. #include <windows.h>
  23. #endif
  24.  
  25. #include "goattrk2.h"
  26. #include "bme.h"
  27.  
  28. int menu = 0;
  29. int editmode = EDIT_PATTERN;
  30. int recordmode = 1;
  31. int followplay = 0;
  32. int hexnybble = -1;
  33. int stepsize = 4;
  34. int autoadvance = 0;
  35. int defaultpatternlength = 64;
  36. int cursorflash = 0;
  37. int cursorcolortable[] = {1,2,7,2};
  38. int exitprogram = 0;
  39. int eacolumn = 0;
  40. int eamode = 0;
  41.  
  42. unsigned keypreset = KEY_TRACKER;
  43. unsigned playerversion = 0;
  44. int fileformat = FORMAT_PRG;
  45. int zeropageadr = 0xfc;
  46. int playeradr = 0x1000;
  47. unsigned sidmodel = 0;
  48. unsigned multiplier = 1;
  49. unsigned adparam = 0x0f00;
  50. unsigned ntsc = 0;
  51. unsigned patternhex = 0;
  52. unsigned sidaddress = 0xd400;
  53. unsigned finevibrato = 1;
  54. unsigned optimizepulse = 1;
  55. unsigned optimizerealtime = 1;
  56. unsigned customclockrate = 0;
  57. unsigned usefinevib = 0;
  58. unsigned b = DEFAULTBUF;
  59. unsigned mr = DEFAULTMIXRATE;
  60. unsigned writer = 0;
  61. unsigned hardsid = 0;
  62. unsigned catweasel = 0;
  63. unsigned interpolate = 0;
  64. unsigned residdelay = 0;
  65. unsigned hardsidbufinteractive = 20;
  66. unsigned hardsidbufplayback = 400;
  67.  
  68. char *configptr;
  69. char configbuf[MAX_PATHNAME];
  70. char loadedsongfilename[MAX_FILENAME];
  71. char songfilename[MAX_FILENAME];
  72. char songfilter[MAX_FILENAME];
  73. char songpath[MAX_PATHNAME];
  74. char instrfilename[MAX_FILENAME];
  75. char instrfilter[MAX_FILENAME];
  76. char instrpath[MAX_PATHNAME];
  77. char packedpath[MAX_PATHNAME];
  78.  
  79. char *programname = "GoatTracker v2.67";
  80.                                       
  81. char textbuffer[MAX_PATHNAME];
  82.  
  83. unsigned char hexkeytbl[] = {'0', '1', '2', '3', '4', '5', '6', '7',
  84.   '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  85.  
  86. extern unsigned char datafile[];
  87.  
  88. int main(int argc, char **argv)
  89. {
  90.   char filename[MAX_PATHNAME];
  91.   FILE *configfile;
  92.   int c,d;
  93.  
  94.   // Open datafile
  95.   io_openlinkeddatafile(datafile);
  96.  
  97.   // Load configuration
  98.   #ifdef __WIN32__
  99.   GetModuleFileName(NULL, filename, MAX_PATHNAME);
  100.   filename[strlen(filename)-3] = 'c';
  101.   filename[strlen(filename)-2] = 'f';
  102.   filename[strlen(filename)-1] = 'g';
  103.   #else
  104.   strcpy(filename, getenv("HOME"));
  105.   strcat(filename, "/.goattrk/goattrk2.cfg");
  106.   #endif
  107.   configfile = fopen(filename, "rt");
  108.   configptr = NULL;
  109.   if (configfile)
  110.   {
  111.     getparam(configfile, &b);
  112.     getparam(configfile, &mr);
  113.     getparam(configfile, &hardsid);
  114.     getparam(configfile, &sidmodel);
  115.     getparam(configfile, &ntsc);
  116.     getparam(configfile, (unsigned *)&fileformat);
  117.     getparam(configfile, (unsigned *)&playeradr);
  118.     getparam(configfile, (unsigned *)&zeropageadr);
  119.     getparam(configfile, &playerversion);
  120.     getparam(configfile, &keypreset);
  121.     getparam(configfile, (unsigned *)&stepsize);
  122.     getparam(configfile, &multiplier);
  123.     getparam(configfile, &catweasel);
  124.     getparam(configfile, &adparam);
  125.     getparam(configfile, &interpolate);
  126.     getparam(configfile, &patternhex);
  127.     getparam(configfile, &sidaddress);
  128.     getparam(configfile, &finevibrato);
  129.     getparam(configfile, &optimizepulse);
  130.     getparam(configfile, &optimizerealtime);
  131.     getparam(configfile, &residdelay);
  132.     getparam(configfile, &customclockrate);
  133.     getparam(configfile, &hardsidbufinteractive);
  134.     getparam(configfile, &hardsidbufplayback);
  135.     fclose(configfile);
  136.   }
  137.   zeropageadr &= 0xff;
  138.   playeradr &= 0xff00;
  139.   sidaddress &= 0xffff;
  140.   if (!stepsize) stepsize = 4;
  141.   if ((sidaddress < 1) ||(sidaddress > 0xffff)) sidaddress = 0xd400;
  142.  
  143.   if (!initscreen())
  144.   {
  145.     return 1;
  146.   }
  147.  
  148.   // Init pathnames
  149.   initpaths();
  150.  
  151.   // Scan command line
  152.   for (c = 1; c < argc; c++)
  153.   {
  154.     if ((argv[c][0] == '-') || (argv[c][0] == '/'))
  155.     {
  156.       int y = 0;
  157.       switch(toupper(argv[c][1]))
  158.       {
  159.         case '?':
  160.         printtext(0,y++,15,"Usage: GOATTRK2 [songname] [options]");
  161.         printtext(0,y++,15,"Options:");
  162.         printtext(0,y++,15,"/Axx Set ADSR parameter for hardrestart in hex. DEFAULT=0F00");
  163.         printtext(0,y++,15,"/Bxx Set sound buffer length in milliseconds DEFAULT=100");
  164.         printtext(0,y++,15,"/Cxx Use CatWeasel MK3 PCI SID (0 = off, 1 = on)");
  165.         printtext(0,y++,15,"/Dxx Pattern row display (0 = decimal, 1 = hexadecimal)");
  166.         printtext(0,y++,15,"/Exx Set emulated SID model (0 = 6581 1 = 8580) DEFAULT=6581");
  167.         printtext(0,y++,15,"/Fxx Set custom SID clock cycles per second (0 = use PAL/NTSC default)");
  168.         printtext(0,y++,15,"/Hxx Use HardSID (0 = off, 1 = HardSID ID0 2 = HardSID ID1 etc.)");
  169.         printtext(0,y++,15,"/Ixx Set reSID interpolation (0 = off, 1 = on) DEFAULT=off");
  170.         printtext(0,y++,15,"/Kxx Note-entry mode (0 = PROTRACKER 1 = DMC) DEFAULT=PROTRK.");
  171.         printtext(0,y++,15,"/Lxx SID memory location in hex. DEFAULT=D400");
  172.         printtext(0,y++,15,"/Mxx Set sound mixing rate DEFAULT=44100");
  173.         printtext(0,y++,15,"/Oxx Set pulseoptimization/skipping (0 = off, 1 = on) DEFAULT=on");
  174.         printtext(0,y++,15,"/Rxx Set realtime-effect optimization/skipping (0 = off, 1 = on) DEFAULT=on");
  175.         printtext(0,y++,15,"/Sxx Set speed multiplier (0 for 25Hz, 1 for 1x, 2 for 2x etc.)");
  176.         printtext(0,y++,15,"/Txx Set HardSID interactive mode sound buffer length in milliseconds DEFAULT=20, max.buffering=0");
  177.         printtext(0,y++,15,"/Uxx Set HardSID playback mode sound buffer length in milliseconds DEFAULT=400, max.buffering=0");
  178.         printtext(0,y++,15,"/Vxx Set finevibrato conversion (0 = off, 1 = on) DEFAULT=1");
  179.         printtext(0,y++,15,"/Zxx Set random reSID write delay in cycles (0 = off) DEFAULT=off");
  180.         printtext(0,y++,15,"/N   Use NTSC timing");
  181.         printtext(0,y++,15,"/P   Use PAL timing (DEFAULT)");
  182.         printtext(0,y++,15,"/W   Write sound output to a file SIDAUDIO.RAW");
  183.         printtext(0,y++,15,"/?   Show this info again");
  184.         waitkeynoupdate();
  185.         return 0;
  186.  
  187.         case 'Z':
  188.         sscanf(&argv[c][2], "%u", &residdelay);
  189.         break;
  190.  
  191.         case 'A':
  192.         sscanf(&argv[c][2], "%x", &adparam);
  193.         break;
  194.  
  195.         case 'S':
  196.         sscanf(&argv[c][2], "%u", &multiplier);
  197.         break;
  198.  
  199.         case 'B':
  200.         sscanf(&argv[c][2], "%u", &b);
  201.         break;
  202.  
  203.         case 'D':
  204.         sscanf(&argv[c][2], "%u", &patternhex);
  205.         break;
  206.  
  207.         case 'E':
  208.         sscanf(&argv[c][2], "%u", &sidmodel);
  209.         break;
  210.  
  211.         case 'I':
  212.         sscanf(&argv[c][2], "%u", &interpolate);
  213.         break;
  214.  
  215.         case 'K':
  216.         sscanf(&argv[c][2], "%u", &keypreset);
  217.         break;
  218.  
  219.         case 'L':
  220.         sscanf(&argv[c][2], "%x", &sidaddress);
  221.         break;
  222.  
  223.         case 'N':
  224.         ntsc = 1;
  225.         customclockrate = 0;
  226.         break;
  227.  
  228.         case 'P':
  229.         ntsc = 0;
  230.         customclockrate = 0;
  231.         break;
  232.  
  233.         case 'F':
  234.         sscanf(&argv[c][2], "%u", &customclockrate);
  235.         break;
  236.  
  237.         case 'M':
  238.         sscanf(&argv[c][2], "%u", &mr);
  239.         break;
  240.  
  241.         case 'O':
  242.         sscanf(&argv[c][2], "%u", &optimizepulse);
  243.         break;
  244.  
  245.         case 'R':
  246.         sscanf(&argv[c][2], "%u", &optimizerealtime);
  247.         break;
  248.  
  249.         case 'H':
  250.         sscanf(&argv[c][2], "%u", &hardsid);
  251.         break;
  252.  
  253.         case 'V':
  254.         sscanf(&argv[c][2], "%u", &finevibrato);
  255.         break;
  256.         
  257.         case 'T':
  258.         sscanf(&argv[c][2], "%u", &hardsidbufinteractive);
  259.         break;
  260.  
  261.         case 'U':
  262.         sscanf(&argv[c][2], "%u", &hardsidbufplayback);
  263.         break;
  264.  
  265.         case 'W':
  266.         writer = 1;
  267.         break;
  268.  
  269.         case 'C':
  270.         sscanf(&argv[c][2], "%u", &catweasel);
  271.         break;
  272.       }
  273.     }
  274.     else
  275.     {
  276.       char startpath[MAX_PATHNAME];
  277.  
  278.       strcpy(songfilename, argv[c]);
  279.       for (d = strlen(argv[c])-1; d >= 0; d--)
  280.       {
  281.         if ((argv[c][d] == '/') || (argv[c][d] == '\\'))
  282.         {
  283.           strcpy(startpath, argv[c]);
  284.           startpath[d+1] = 0;
  285.           chdir(startpath);
  286.           initpaths();
  287.           strcpy(songfilename, &argv[c][d+1]);
  288.           break;
  289.         }
  290.       }
  291.     }
  292.   }
  293.  
  294.   // Validate parameters
  295.   sidmodel &= 1;
  296.   adparam &= 0xffff;
  297.   if (multiplier > 16) multiplier = 16;
  298.   if ((sidaddress < 1) ||(sidaddress > 0xffff)) sidaddress = 0xd400;
  299.   if (keypreset > 2) keypreset = 0;
  300.   if ((finevibrato == 1) && (multiplier < 2)) usefinevib = 1;
  301.   if (finevibrato > 1) usefinevib = 1;
  302.   if (optimizepulse > 1) optimizepulse = 1;
  303.   if (optimizerealtime > 1) optimizerealtime = 1;
  304.   if (residdelay > 63) residdelay = 63;
  305.   if (customclockrate < 100) customclockrate = 0;
  306.  
  307.   // Reset channels/song
  308.   initchannels();
  309.   clearsong(1,1,1,1,1);
  310.  
  311.   // Init sound
  312.   if (!sound_init(b, mr, writer, hardsid, sidmodel, ntsc, multiplier, catweasel, interpolate, customclockrate))
  313.   {
  314.     printtextc(MAX_ROWS/2-1,15,"Sound init failed. Press any key to run without sound (notice that song timer won't start)");
  315.     waitkeynoupdate();
  316.   }
  317.  
  318.   // Load song if applicable
  319.   if (strlen(songfilename)) loadsong();
  320.  
  321.   // Start editor mainloop
  322.   printmainscreen();
  323.   while (!exitprogram)
  324.   {
  325.     waitkeymouse();
  326.     docommand();
  327.   }
  328.  
  329.   // Save configuration
  330.   #ifndef __WIN32__
  331.   strcpy(filename, getenv("HOME"));
  332.   strcat(filename, "/.goattrk");
  333.   mkdir(filename, S_IRUSR | S_IWUSR | S_IXUSR);
  334.   strcat(filename, "/goattrk2.cfg");
  335.   #endif
  336.   configfile = fopen(filename, "wt");
  337.   if (configfile)
  338.   {
  339.     fprintf(configfile, ";------------------------------------------------------------------------------\n"
  340.                         ";GT2 config file. Rows starting with ; are comments. Hexadecimal parameters are\n"
  341.                         ";to be preceded with $ and decimal parameters with nothing.                    \n"
  342.                         ";------------------------------------------------------------------------------\n"
  343.                         "\n"
  344.                         ";reSID buffer length (in milliseconds)\n%d\n\n"
  345.                         ";reSID mixing rate (in Hz)\n%d\n\n"
  346.                         ";Hardsid device number (0 = off)\n%d\n\n"
  347.                         ";reSID model (0 = 6581, 1 = 8580)\n%d\n\n"
  348.                         ";Timing mode (0 = PAL, 1 = NTSC)\n%d\n\n"
  349.                         ";Packer/relocator fileformat (0 = SID, 1 = PRG, 2 = BIN)\n%d\n\n"
  350.                         ";Packer/relocator player address\n$%04x\n\n"
  351.                         ";Packer/relocator zeropage baseaddress\n$%02x\n\n"
  352.                         ";Packer/relocator player type (0 = standard ... 3 = minimal)\n%d\n\n"
  353.                         ";Key entry mode (0 = Protracker, 1 = DMC)\n%d\n\n"
  354.                         ";Pattern highlight step size\n%d\n\n"
  355.                         ";Speed multiplier (0 = 25Hz, 1 = 1X, 2 = 2X etc.)\n%d\n\n"
  356.                         ";Use CatWeasel SID (0 = off, 1 = on)\n%d\n\n"
  357.                         ";Hardrestart ADSR parameter\n$%04x\n\n"
  358.                         ";reSID interpolation (0 = off, 1 = on)\n%d\n\n"
  359.                         ";Hexadecimal pattern display (0 = off, 1 = on)\n%d\n\n"
  360.                         ";SID baseaddress\n$%04x\n\n"
  361.                         ";Finevibrato mode (0 = off, 1 = on)\n%d\n\n"
  362.                         ";Pulseskipping (0 = off, 1 = on)\n%d\n\n"
  363.                         ";Realtime effect skipping (0 = off, 1 = on)\n%d\n\n"
  364.                         ";Random reSID write delay in cycles (0 = off)\n%d\n\n"
  365.                         ";Custom SID clock cycles per second (0 = use PAL/NTSC default)\n%d\n\n"
  366.                         ";HardSID interactive mode buffer size (in milliseconds, 0 = maximum/no flush)\n%d\n\n"
  367.                         ";HardSID playback mode buffer size (in milliseconds, 0 = maximum/no flush)\n%d\n\n",
  368.     b,
  369.     mr,
  370.     hardsid,
  371.     sidmodel,
  372.     ntsc,
  373.     fileformat,
  374.     playeradr,
  375.     zeropageadr,
  376.     playerversion,
  377.     keypreset,
  378.     stepsize,
  379.     multiplier,
  380.     catweasel,
  381.     adparam,
  382.     interpolate,
  383.     patternhex,
  384.     sidaddress,
  385.     finevibrato,
  386.     optimizepulse,
  387.     optimizerealtime,
  388.     residdelay,
  389.     customclockrate,
  390.     hardsidbufinteractive,
  391.     hardsidbufplayback);
  392.  
  393.     fclose(configfile);
  394.   }
  395.  
  396.   // Exit
  397.   return 0;
  398. }
  399.  
  400. void waitkey(void)
  401. {
  402.   for (;;)
  403.   {
  404.     displayupdate();
  405.     getkey();
  406.     if ((rawkey) || (key)) break;
  407.     if (win_quitted) break;
  408.   }
  409.  
  410.   converthex();
  411. }
  412.  
  413. void waitkeymouse(void)
  414. {
  415.   for (;;)
  416.   {
  417.     displayupdate();
  418.     getkey();
  419.     if ((rawkey) || (key)) break;
  420.     if (win_quitted) break;
  421.     if (mouseb) break;
  422.   }
  423.  
  424.   converthex();
  425. }
  426.  
  427. void waitkeymousenoupdate(void)
  428. {
  429.   for (;;)
  430.   {
  431.       fliptoscreen();
  432.     getkey();
  433.     if ((rawkey) || (key)) break;
  434.     if (win_quitted) break;
  435.     if (mouseb) break;
  436.   }
  437.  
  438.   converthex();
  439. }
  440.  
  441. void waitkeynoupdate(void)
  442. {
  443.   for (;;)
  444.   {
  445.       fliptoscreen();
  446.     getkey();
  447.     if ((rawkey) || (key)) break;
  448.     if ((mouseb) && (!prevmouseb)) break;
  449.     if (win_quitted) break;
  450.   }
  451. }
  452.  
  453. void converthex()
  454. {
  455.   int c;
  456.  
  457.   hexnybble = -1;
  458.   for (c = 0; c < 16; c++)
  459.   {
  460.     if (tolower(key) == hexkeytbl[c])
  461.     {
  462.       if (c >= 10)
  463.       {
  464.         if (!shiftpressed) hexnybble = c;
  465.       }
  466.       else
  467.       {
  468.         hexnybble = c;
  469.       }
  470.     }
  471.   }
  472. }
  473.  
  474.  
  475. void docommand(void)
  476. {
  477.   // "GUI" operation :)
  478.   mousecommands();
  479.  
  480.   // Mode-specific commands
  481.   switch(editmode)
  482.   {
  483.     case EDIT_ORDERLIST:
  484.     orderlistcommands();
  485.     break;
  486.  
  487.     case EDIT_INSTRUMENT:
  488.     instrumentcommands();
  489.     break;
  490.  
  491.     case EDIT_TABLES:
  492.     tablecommands();
  493.     break;
  494.  
  495.     case EDIT_PATTERN:
  496.     patterncommands();
  497.     break;
  498.  
  499.     case EDIT_NAMES:
  500.     namecommands();
  501.     break;
  502.   }
  503.  
  504.   // General commands
  505.   generalcommands();
  506. }
  507.  
  508. void mousecommands(void)
  509. {
  510.   int c;
  511.  
  512.   if (!mouseb) return;
  513.  
  514.   // Pattern editpos & pattern number selection
  515.   for (c = 0; c < MAX_CHN; c++)
  516.   {
  517.     if ((mousey == 2) && (mousex >= 13 + c*15) && (mousex <= 14 + c*15))
  518.     {
  519.         if ((!prevmouseb) || (mouseheld > HOLDDELAY))
  520.         {
  521.         if (mouseb & MOUSEB_LEFT) 
  522.         {
  523.           epchn = c;
  524.           nextpattern();
  525.         }
  526.         if (mouseb & MOUSEB_RIGHT)
  527.         {
  528.           epchn = c;
  529.           prevpattern();
  530.         }
  531.       }
  532.     }
  533.     else
  534.     {
  535.       if ((mousey >= 2) && (mousey <= 34) && (mousex >= 6 + c*15) && (mousex <= 14 + c*15))
  536.       {
  537.         int x = mousex-6-c*15;
  538.         int newpos = mousey-3+epview;
  539.         if (newpos < 0) newpos = 0;
  540.         if (newpos > pattlen[epnum[epchn]]) newpos = pattlen[epnum[epchn]];
  541.  
  542.         editmode = EDIT_PATTERN;
  543.  
  544.         if ((mouseb & (MOUSEB_RIGHT|MOUSEB_MIDDLE)) && (!prevmouseb))
  545.         {
  546.           if ((epmarkchn != c) || (newpos != epmarkend))
  547.           {
  548.             epmarkchn = c;
  549.             epmarkstart = epmarkend = newpos;
  550.           }
  551.         }
  552.  
  553.         if (mouseb & MOUSEB_LEFT)
  554.         {
  555.           epchn = c;
  556.           if (x < 4) epcolumn = 0;
  557.           if (x >= 4) epcolumn = x-3;
  558.         }
  559.  
  560.         if (!prevmouseb)
  561.         {
  562.           if (mouseb & MOUSEB_LEFT)
  563.             eppos = newpos;
  564.         }
  565.         else
  566.         {
  567.             if (mouseb & MOUSEB_LEFT)
  568.             {
  569.             if (mousey == 2) eppos--;
  570.             if (mousey == 34) eppos++;
  571.           }
  572.         }
  573.         if (eppos < 0) eppos = 0;
  574.         if (eppos > pattlen[epnum[epchn]]) eppos = pattlen[epnum[epchn]];
  575.  
  576.         if (mouseb & (MOUSEB_RIGHT|MOUSEB_MIDDLE)) epmarkend = newpos;
  577.       }
  578.     }
  579.   }
  580.  
  581.   // Song editpos & songnumber selection
  582.   if ((mousey >= 3) && (mousey <= 8) && (mousex >= 40+10))
  583.   {
  584.     int newpos = esview + (mousex-44-10) / 3;
  585.     int newcolumn = (mousex-44-10) % 3;
  586.     int newchn = mousey - 3;
  587.     if (newcolumn < 0) newcolumn = 0;
  588.     if (newcolumn > 1) newcolumn = 1;
  589.     if (newpos < 0)
  590.     {
  591.       newpos = 0;
  592.       newcolumn = 0;
  593.     }
  594.     if (newpos == songlen[esnum][eschn])
  595.     {
  596.       newpos++;
  597.       newcolumn = 0;
  598.     }
  599.     if (newpos > songlen[esnum][eschn]+1)
  600.     {
  601.       newpos = songlen[esnum][eschn] + 1;
  602.       newcolumn = 1;
  603.     }
  604.  
  605.     editmode = EDIT_ORDERLIST;
  606.  
  607.     if ((mouseb & (MOUSEB_RIGHT|MOUSEB_MIDDLE)) && (!prevmouseb) && (newpos < songlen[esnum][eschn]))
  608.     {
  609.       if ((esmarkchn != newchn) || (newpos != esmarkend))
  610.       {
  611.         esmarkchn = newchn;
  612.         esmarkstart = esmarkend = newpos;
  613.       }
  614.     }
  615.  
  616.     if (mouseb & MOUSEB_LEFT)
  617.     {
  618.       eschn = newchn;
  619.       eseditpos = newpos;
  620.       escolumn = newcolumn;
  621.     }
  622.  
  623.     if ((mouseb & (MOUSEB_RIGHT|MOUSEB_MIDDLE)) && (newpos < songlen[esnum][eschn])) esmarkend = newpos;
  624.   }
  625.   if (((!prevmouseb) || (mouseheld > HOLDDELAY)) && (mousey == 2) && (mousex >= 63+10) && (mousex <= 64+10))
  626.   {
  627.     if (mouseb & MOUSEB_LEFT) nextsong();
  628.     if (mouseb & MOUSEB_RIGHT) prevsong();
  629.   }
  630.  
  631.   // Instrument editpos & instrument number selection
  632.   if ((mousey >= 8) && (mousey <= 12) && (mousex >= 56+10) && (mousex <= 57+10))
  633.   {
  634.     editmode = EDIT_INSTRUMENT;
  635.     eipos = mousey-8;
  636.     eicolumn = mousex-56-10;
  637.   }
  638.   if ((mousey >= 8) && (mousey <= 11) && (mousex >= 76+10) && (mousex <= 77+10))
  639.   {
  640.     editmode = EDIT_INSTRUMENT;
  641.     eipos = mousey-8+5;
  642.     eicolumn = mousex-76-10;
  643.   }
  644.   if ((mousey == 7) && (mousex >= 60+10))
  645.   {
  646.     editmode = EDIT_INSTRUMENT;
  647.     eipos = 9;
  648.   }
  649.   if (((!prevmouseb) || (mouseheld > HOLDDELAY)) && (mousey == 7) && (mousex >= 56+10) && (mousex <= 57+10))
  650.   {
  651.     if (mouseb & MOUSEB_LEFT) nextinstr();
  652.     if (mouseb & MOUSEB_RIGHT) previnstr();
  653.   }
  654.  
  655.  
  656.   // Table editpos
  657.   for (c = 0; c < MAX_TABLES; c++)
  658.   {
  659.     if ((mousey >= 14) && (mousey <= 30) && (mousex >= 43+10+c*10) && (mousex <= 47+10+c*10))
  660.     {
  661.       int newpos = mousey-15+etview[etnum];
  662.       if (newpos < 0) newpos = 0;
  663.       if (newpos >= MAX_TABLELEN) newpos = MAX_TABLELEN-1;
  664.  
  665.       editmode = EDIT_TABLES;
  666.  
  667.       if ((mouseb & (MOUSEB_RIGHT|MOUSEB_MIDDLE)) && (!prevmouseb))
  668.       {
  669.         if ((etmarknum != etnum) || (newpos != etmarkend))
  670.         {
  671.           etmarknum = c;
  672.           etmarkstart = etmarkend = newpos;
  673.         }
  674.       }
  675.       if (mouseb & MOUSEB_LEFT)
  676.       {
  677.         etnum = c;
  678.         etpos = mousey-15+etview[etnum];
  679.         etcolumn = mousex-43-10-c*10;
  680.       }
  681.       if (etcolumn >= 2) etcolumn--;
  682.       if (etpos < 0) etpos = 0;
  683.       if (etpos > MAX_TABLELEN-1) etpos = MAX_TABLELEN-1;
  684.  
  685.       if (mouseb & (MOUSEB_RIGHT|MOUSEB_MIDDLE)) etmarkend = newpos;
  686.     }
  687.   }
  688.  
  689.   // Name editpos
  690.   if ((mousey >= 31) && (mousey <= 33) && (mousex >= 47+10))
  691.   {
  692.     editmode = EDIT_NAMES;
  693.     enpos = mousey - 31;
  694.   }
  695.  
  696.   // Status panel
  697.   if ((!prevmouseb) && (mousex == 7) && (mousey == 23+3+9))
  698.   {
  699.     if (mouseb & (MOUSEB_LEFT))
  700.       if (epoctave < 7) epoctave++;
  701.     if (mouseb & (MOUSEB_RIGHT))
  702.       if (epoctave > 0) epoctave--;
  703.   }
  704.   if ((!prevmouseb) && (mousex <= 7) && (mousey == 24+3+9))
  705.   {
  706.     recordmode ^= 1;
  707.   }
  708.   for (c = 0; c < MAX_CHN; c++)
  709.   {
  710.     if ((!prevmouseb) && (mousey >= 23+3+9) && (mousex >= 80 + 7*c) && (mousex <= 85 + 7*c))
  711.       mutechannel(c);
  712.   }
  713.  
  714.   // Titlebar actions
  715.   if (!menu)
  716.   {
  717.     if ((mousey == 0) && (!prevmouseb) && (mouseb == MOUSEB_LEFT))
  718.     {
  719.       if ((mousex >= 40+10) && (mousex <= 41+10))
  720.       {
  721.         usefinevib ^= 1;
  722.       }
  723.       if ((mousex >= 43+10) && (mousex <= 44+10))
  724.       {
  725.         optimizepulse ^= 1;
  726.       }
  727.       if ((mousex >= 46+10) && (mousex <= 47+10))
  728.       {
  729.         optimizerealtime ^= 1;
  730.       }
  731.       if ((mousex >= 49+10) && (mousex <= 52+10))
  732.       {
  733.         ntsc ^= 1;
  734.         sound_init(b, mr, writer, hardsid, sidmodel, ntsc, multiplier, catweasel, interpolate, customclockrate);
  735.       }
  736.       if ((mousex >= 54+10) && (mousex <= 57+10))
  737.       {
  738.         sidmodel ^= 1;
  739.         sound_init(b, mr, writer, hardsid, sidmodel, ntsc, multiplier, catweasel, interpolate, customclockrate);
  740.       }
  741.       if ((mousex >= 62+10) && (mousex <= 65+10)) editadsr();
  742.       if ((mousex >= 67+10) && (mousex <= 68+10)) prevmultiplier();
  743.       if ((mousex >= 69+10) && (mousex <= 70+10)) nextmultiplier();
  744.     }
  745.   }
  746.   else
  747.   {
  748.     if ((!mousey) && (mouseb & MOUSEB_LEFT) && (!(prevmouseb & MOUSEB_LEFT)))
  749.     {
  750.       if ((mousex >= 0) && (mousex <= 5))
  751.       {
  752.         initsong(esnum, PLAY_BEGINNING);
  753.         followplay = shiftpressed;
  754.       }
  755.       if ((mousex >= 7) && (mousex <= 15))
  756.       {
  757.         initsong(esnum, PLAY_POS);
  758.         followplay = shiftpressed;
  759.       }
  760.       if ((mousex >= 17) && (mousex <= 26))
  761.       {
  762.         initsong(esnum, PLAY_PATTERN);
  763.         followplay = shiftpressed;
  764.       }
  765.       if ((mousex >= 28) && (mousex <= 33))
  766.         stopsong();
  767.       if ((mousex >= 35) && (mousex <= 40))
  768.         load();
  769.       if ((mousex >= 42) && (mousex <= 47))
  770.         save();
  771.       if ((mousex >= 49) && (mousex <= 57))
  772.         relocator();
  773.       if ((mousex >= 59) && (mousex <= 64))
  774.         onlinehelp();
  775.       if ((mousex >= 66) && (mousex <= 72))
  776.         clear();
  777.       if ((mousex >= 74) && (mousex <= 79))
  778.         quit();
  779.     }
  780.   }
  781. }
  782.  
  783. void generalcommands(void)
  784. {
  785.   int c;
  786.  
  787.   switch(key)
  788.   {
  789.     case '?':
  790.     case '-':
  791.     if ((editmode != EDIT_NAMES) && (editmode != EDIT_ORDERLIST))
  792.     {
  793.       if (!((editmode == EDIT_INSTRUMENT) && (eipos == 9))) previnstr();
  794.     }
  795.     break;
  796.  
  797.     case '+':
  798.     case '_':
  799.     if ((editmode != EDIT_NAMES) && (editmode != EDIT_ORDERLIST))
  800.     {
  801.       if (!((editmode == EDIT_INSTRUMENT) && (eipos >= 9))) nextinstr();
  802.  
  803.     }
  804.     break;
  805.  
  806.     case '*':
  807.     if (editmode != EDIT_NAMES)
  808.     {
  809.       if (!((editmode == EDIT_INSTRUMENT) && (eipos >= 9)))
  810.       {
  811.         if (epoctave < 7) epoctave++;
  812.       }
  813.     }
  814.     break;
  815.  
  816.     case '/':
  817.     case '\'':
  818.     if (editmode != EDIT_NAMES)
  819.     {
  820.       if (!((editmode == EDIT_INSTRUMENT) && (eipos >= 9)))
  821.       {
  822.         if (epoctave > 0) epoctave--;
  823.       }
  824.     }
  825.     break;
  826.  
  827.     case '<':
  828.     if (((editmode == EDIT_INSTRUMENT) && (eipos != 9)) || (editmode == EDIT_TABLES))
  829.       previnstr();
  830.     break;
  831.  
  832.     case '>':
  833.     if (((editmode == EDIT_INSTRUMENT) && (eipos != 9)) || (editmode == EDIT_TABLES))
  834.       nextinstr();
  835.     break;
  836.  
  837.     case ';':
  838.     for (c = 0; c < MAX_CHN; c++)
  839.     {
  840.       if (espos[c]) espos[c]--;
  841.       if (espos[c] < esview)
  842.       {
  843.         esview = espos[c];
  844.         eseditpos = espos[c];
  845.       }
  846.     }
  847.     updateviewtopos();
  848.     rewindsong();
  849.     break;
  850.  
  851.     case ':':
  852.     for (c = 0; c < MAX_CHN; c++)
  853.     {
  854.       if (espos[c] < songlen[esnum][c]-1)
  855.         espos[c]++;
  856.       if (espos[c] - esview >= VISIBLEORDERLIST)
  857.       {
  858.         esview = espos[c] - VISIBLEORDERLIST + 1;
  859.         eseditpos = espos[c];
  860.       }
  861.     }
  862.     updateviewtopos();
  863.     rewindsong();
  864.     break;
  865.  
  866.   }
  867.   if (win_quitted) exitprogram = 1;
  868.   switch(rawkey)
  869.   {
  870.     case KEY_ESC:
  871.     if (!shiftpressed)
  872.       quit();
  873.     else
  874.       clear();
  875.     break;
  876.  
  877.     case KEY_KPMULTIPLY:
  878.     if ((editmode != EDIT_NAMES) && (!key))
  879.     {
  880.       if (!((editmode == EDIT_INSTRUMENT) && (eipos >= 9)))
  881.       {
  882.         if (epoctave < 7) epoctave++;
  883.       }
  884.     }
  885.     break;
  886.  
  887.     case KEY_KPDIVIDE:
  888.     if ((editmode != EDIT_NAMES) && (!key))
  889.     {
  890.       if (!((editmode == EDIT_INSTRUMENT) && (eipos >= 9)))
  891.       {
  892.         if (epoctave > 0) epoctave--;
  893.       }
  894.     }
  895.     break;
  896.  
  897.     case KEY_F12:
  898.     onlinehelp();
  899.     break;
  900.  
  901.     case KEY_TAB:
  902.     if (!shiftpressed) editmode++;
  903.     else editmode--;
  904.     if (editmode > EDIT_NAMES) editmode = EDIT_PATTERN;
  905.     if (editmode < EDIT_PATTERN) editmode = EDIT_NAMES;
  906.     break;
  907.  
  908.     case KEY_F1:
  909.     initsong(esnum, PLAY_BEGINNING);
  910.     followplay = shiftpressed;
  911.     break;
  912.  
  913.     case KEY_F2:
  914.     initsong(esnum, PLAY_POS);
  915.     followplay = shiftpressed;
  916.     break;
  917.  
  918.     case KEY_F3:
  919.     initsong(esnum, PLAY_PATTERN);
  920.     followplay = shiftpressed;
  921.     break;
  922.  
  923.     case KEY_F4:
  924.     if (shiftpressed) 
  925.       mutechannel(epchn);
  926.     else
  927.     stopsong();
  928.     break;
  929.  
  930.     case KEY_F5:
  931.     if (!shiftpressed)
  932.       editmode = EDIT_PATTERN;
  933.     else prevmultiplier();
  934.     break;
  935.  
  936.     case KEY_F6:
  937.     if (!shiftpressed)
  938.       editmode = EDIT_ORDERLIST;
  939.     else nextmultiplier();
  940.     break;
  941.  
  942.     case KEY_F7:
  943.     if (!shiftpressed)
  944.     {
  945.       if (editmode == EDIT_INSTRUMENT)
  946.         editmode = EDIT_TABLES;
  947.       else
  948.         editmode = EDIT_INSTRUMENT;
  949.     }
  950.     else editadsr();
  951.     break;
  952.  
  953.     case KEY_F8:
  954.     if (!shiftpressed)
  955.       editmode = EDIT_NAMES;
  956.     else
  957.     {
  958.       sidmodel ^= 1;
  959.       sound_init(b, mr, writer, hardsid, sidmodel, ntsc, multiplier, catweasel, interpolate, customclockrate);
  960.     }
  961.     break;
  962.  
  963.     case KEY_F9:
  964.     relocator();
  965.     break;
  966.  
  967.     case KEY_F10:
  968.     load();
  969.     break;
  970.  
  971.     case KEY_F11:
  972.     save();
  973.     break;
  974.   }
  975. }
  976.  
  977. void load(void)
  978. {
  979.   if ((editmode != EDIT_INSTRUMENT) && (editmode != EDIT_TABLES))
  980.   {
  981.     if (fileselector(songfilename, songpath, songfilter, "LOAD SONG", 0))
  982.       loadsong();
  983.   }
  984.   else
  985.   {
  986.     if (einum)
  987.     {
  988.       if (fileselector(instrfilename, instrpath, instrfilter, "LOAD INSTRUMENT", 0))
  989.         loadinstrument();
  990.     }
  991.   }
  992.   key = 0;
  993.   rawkey = 0;
  994. }
  995.  
  996. void save(void)
  997. {
  998.   if ((editmode != EDIT_INSTRUMENT) && (editmode != EDIT_TABLES))
  999.   {
  1000.     int done = 0;
  1001.  
  1002.     // Repeat until quit or save successful
  1003.     while (!done)
  1004.     {
  1005.       if (strlen(loadedsongfilename)) strcpy(songfilename, loadedsongfilename);
  1006.       if (fileselector(songfilename, songpath, songfilter, "SAVE SONG", 3))
  1007.         done = savesong();
  1008.       else done = 1;
  1009.     }
  1010.   }
  1011.   else
  1012.   {
  1013.     if (einum)
  1014.     {
  1015.       int done = 0;
  1016.       int useinstrname = 0;
  1017.       char tempfilename[MAX_FILENAME];
  1018.  
  1019.       // Repeat until quit or save successful
  1020.       while (!done)
  1021.       {
  1022.         if ((!strlen(instrfilename)) && (strlen(instr[einum].name)))
  1023.         {
  1024.           useinstrname = 1;
  1025.           strcpy(instrfilename, instr[einum].name);
  1026.           strcat(instrfilename, ".ins");
  1027.           strcpy(tempfilename, instrfilename);
  1028.         }
  1029.  
  1030.         if (fileselector(instrfilename, instrpath, instrfilter, "SAVE INSTRUMENT", 3))
  1031.           done = saveinstrument();
  1032.         else done = 1;
  1033.  
  1034.         if (useinstrname)
  1035.         {
  1036.           if (!strcmp(tempfilename, instrfilename))
  1037.             memset(instrfilename, 0, sizeof instrfilename);
  1038.         }
  1039.       }
  1040.     }
  1041.   }
  1042.   key = 0;
  1043.   rawkey = 0;
  1044. }
  1045.  
  1046. void quit(void)
  1047. {
  1048.   if ((!shiftpressed) || (mouseb))
  1049.   {
  1050.     printtextcp(49, 36, 15, "Really Quit (y/n)?");
  1051.     waitkey();
  1052.     printblank(20, 36, 58);
  1053.     if ((key == 'y') || (key == 'Y')) exitprogram = 1;
  1054.   }
  1055.   key = 0;
  1056.   rawkey = 0;
  1057. }
  1058.  
  1059. void clear(void)
  1060. {
  1061.   int cs = 0;
  1062.   int cp = 0;
  1063.   int ci = 0;
  1064.   int ct = 0;
  1065.   int cn = 0;
  1066.  
  1067.   printtextcp(49, 36, 15, "Optimize everything (y/n)?");
  1068.   waitkey();
  1069.   printblank(20, 36, 58);
  1070.   if ((key == 'y') || (key == 'Y'))
  1071.   {
  1072.     optimizeeverything(1, 1);
  1073.     key = 0;
  1074.     rawkey = 0;
  1075.     return;
  1076.   }
  1077.  
  1078.   printtextcp(49, 36, 15, "Clear orderlists (y/n)?");
  1079.   waitkey();
  1080.   printblank(20, 36, 58);
  1081.   if ((key == 'y') || (key == 'Y')) cs = 1;
  1082.  
  1083.   printtextcp(49, 36, 15, "Clear patterns (y/n)?");
  1084.   waitkey();
  1085.   printblank(20, 36, 58);
  1086.   if ((key == 'y') || (key == 'Y')) cp = 1;
  1087.  
  1088.   printtextcp(49, 36, 15, "Clear instruments (y/n)?");
  1089.   waitkey();
  1090.   printblank(20, 36, 58);
  1091.   if ((key == 'y') || (key == 'Y')) ci = 1;
  1092.  
  1093.   printtextcp(49, 36, 15, "Clear tables (y/n)?");
  1094.   waitkey();
  1095.   printblank(20, 36, 58);
  1096.   if ((key == 'y') || (key == 'Y')) ct = 1;
  1097.  
  1098.   printtextcp(49, 36, 15, "Clear songname (y/n)?");
  1099.   waitkey();
  1100.   printblank(20, 36, 58);
  1101.   if ((key == 'y') || (key == 'Y')) cn = 1;
  1102.  
  1103.   if (cp == 1)
  1104.   {
  1105.     int selectdone = 0;
  1106.     int olddpl = defaultpatternlength;
  1107.  
  1108.     printtext(40, 36, 15,"Pattern length:");
  1109.     while (!selectdone)
  1110.     {
  1111.       sprintf(textbuffer, "%02d ", defaultpatternlength);
  1112.       printtext(55, 36, 15, textbuffer);
  1113.       waitkey();
  1114.       switch(rawkey)
  1115.       {
  1116.         case KEY_LEFT:
  1117.         defaultpatternlength -= 7;
  1118.         case KEY_DOWN:
  1119.         defaultpatternlength--;
  1120.         if (defaultpatternlength < 1) defaultpatternlength = 1;
  1121.         break;
  1122.  
  1123.         case KEY_RIGHT:
  1124.         defaultpatternlength += 7;
  1125.         case KEY_UP:
  1126.         defaultpatternlength++;
  1127.         if (defaultpatternlength > MAX_PATTROWS) defaultpatternlength = MAX_PATTROWS;
  1128.         break;
  1129.  
  1130.         case KEY_ESC:
  1131.         defaultpatternlength = olddpl;
  1132.         selectdone = 1;
  1133.         break;
  1134.  
  1135.         case KEY_ENTER:
  1136.         selectdone = 1;
  1137.         break;
  1138.       }
  1139.     }
  1140.     printblank(20, 36, 58);
  1141.   }
  1142.  
  1143.   if (cs | cp | ci | ct | cn)
  1144.     memset(songfilename, 0, sizeof songfilename);
  1145.   clearsong(cs, cp, ci, ct, cn);
  1146.  
  1147.   key = 0;
  1148.   rawkey = 0;
  1149. }
  1150.  
  1151. void editadsr(void)
  1152. {
  1153.   eamode = 1;
  1154.   eacolumn = 0;
  1155.  
  1156.   for (;;)
  1157.   {
  1158.     waitkeymouse();
  1159.  
  1160.     if (win_quitted)
  1161.     {
  1162.       exitprogram = 1;
  1163.       key = 0;
  1164.       rawkey = 0;
  1165.       return;
  1166.     }
  1167.  
  1168.     if (hexnybble >= 0)
  1169.     {
  1170.       switch(eacolumn)
  1171.       {
  1172.         case 0:
  1173.         adparam &= 0x0fff;
  1174.         adparam |= hexnybble << 12;
  1175.         break;
  1176.  
  1177.         case 1:
  1178.         adparam &= 0xf0ff;
  1179.         adparam |= hexnybble << 8;
  1180.         break;
  1181.  
  1182.         case 2:
  1183.         adparam &= 0xff0f;
  1184.         adparam |= hexnybble << 4;
  1185.         break;
  1186.  
  1187.         case 3:
  1188.         adparam &= 0xfff0;
  1189.         adparam |= hexnybble;
  1190.         break;
  1191.       }
  1192.       eacolumn++;
  1193.     }
  1194.  
  1195.     switch(rawkey)
  1196.     {
  1197.       case KEY_F7:
  1198.       if (!shiftpressed) break;
  1199.  
  1200.       case KEY_ESC:
  1201.       case KEY_ENTER:
  1202.       case KEY_TAB:
  1203.       eamode = 0;
  1204.       key = 0;
  1205.       rawkey = 0;
  1206.       return;
  1207.  
  1208.       case KEY_BACKSPACE:
  1209.       if (!eacolumn) break;
  1210.       case KEY_LEFT:
  1211.       eacolumn--;
  1212.       break;
  1213.  
  1214.       case KEY_RIGHT:
  1215.       eacolumn++;
  1216.     }
  1217.     eacolumn &= 3;
  1218.  
  1219.     if ((mouseb) && (!prevmouseb))
  1220.     {
  1221.       eamode = 0;
  1222.       return;
  1223.     }
  1224.   }
  1225. }
  1226.  
  1227. void getparam(FILE *handle, unsigned *value)
  1228. {
  1229.   for (;;)
  1230.   {
  1231.     while (!configptr)
  1232.     {
  1233.       if (feof(handle)) return;
  1234.       fgets(configbuf, MAX_PATHNAME, handle);
  1235.       if ((configbuf[0]) && (configbuf[0] != ';')) configptr = configbuf;
  1236.     }
  1237.  
  1238.     if ((*configptr == '$') || ((*configptr >= '0') && (*configptr <= '9'))) break;
  1239.     if (!(*configptr)) configptr = NULL;
  1240.     else configptr++;
  1241.   }
  1242.  
  1243.   if (*configptr == '$')
  1244.   {
  1245.     *value = 0;
  1246.     configptr++;
  1247.     for (;;)
  1248.     {
  1249.       char c = tolower(*configptr++);
  1250.       int h = -1;
  1251.  
  1252.       if ((c >= 'a') && (c <= 'f')) h = c - 'a' + 10;
  1253.       if ((c >= '0') && (c <= '9')) h = c - '0';
  1254.  
  1255.       if (h >= 0)
  1256.       {
  1257.         *value *= 16;
  1258.         *value += h;
  1259.       }
  1260.       else break;
  1261.     }
  1262.   }
  1263.   else
  1264.   {
  1265.     *value = 0;
  1266.     for (;;)
  1267.     {
  1268.       char c = tolower(*configptr++);
  1269.       int d = -1;
  1270.  
  1271.       if ((c >= '0') && (c <= '9')) d = c - '0';
  1272.  
  1273.       if (d >= 0)
  1274.       {
  1275.         *value *= 10;
  1276.         *value += d;
  1277.       }
  1278.       else break;
  1279.     }
  1280.   }
  1281.  
  1282.   if (!(*configptr)) configptr = NULL;
  1283. }
  1284.  
  1285.  
  1286. void prevmultiplier(void)
  1287. {
  1288.   if (multiplier > 0)
  1289.   {
  1290.     multiplier--;
  1291.     sound_init(b, mr, writer, hardsid, sidmodel, ntsc, multiplier, catweasel, interpolate, customclockrate);
  1292.   }
  1293. }
  1294.  
  1295. void nextmultiplier(void)
  1296. {
  1297.   if (multiplier < 16)
  1298.   {
  1299.     multiplier++;
  1300.     sound_init(b, mr, writer, hardsid, sidmodel, ntsc, multiplier, catweasel, interpolate, customclockrate);
  1301.   }
  1302. }
  1303.