home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 242.lha / GenericLIBrarian / source / amiga-mach.c next >
Encoding:
C/C++ Source or Header  |  1989-04-07  |  12.4 KB  |  539 lines

  1. /*
  2.  * GLIB - a Generic LIBrarian and editor for synths
  3.  *
  4.  * Machine dependent stuff.
  5.  *
  6.  * Amiga version.
  7.  */
  8.  
  9. #include "glib.h"
  10. #include <stdio.h>
  11. #include <ctype.h>
  12.  
  13. #include <dos.h>
  14. #include <intuition/intuition.h>
  15. #include <exec/types.h>
  16. #include <exec/ports.h>
  17. #include <exec/io.h>
  18. #include <exec/devices.h>
  19. #include <exec/memory.h>
  20. #include <devices/serial.h>
  21.  
  22. #include <proto/exec.h>
  23. #include <proto/intuition.h>
  24.  
  25. struct IntuitionBase *IntuitionBase;
  26. struct GfxBase *GfxBase;
  27. struct Screen *S;
  28. struct Window *W;
  29. struct RastPort *R;
  30.  
  31. int Rows, Cols;
  32.  
  33. #define TOPEDGE 8       /* how much of screen bar to show */
  34. int WindowWidth = 640;
  35. int WindowHeight = 200 - TOPEDGE;
  36.  
  37. /* standard WB colors */
  38. #define BLUE 0
  39. #define WHITE 1
  40. #define BLACK 2
  41. #define ORANGE 3
  42.  
  43.  
  44. hello()
  45. {
  46.         windinit();
  47.         openmidi();
  48. }
  49.  
  50. bye()
  51. {
  52.         closemidi();
  53.         windexit(0);
  54. }
  55.  
  56. int MouseX, MouseY, MouseButtons;
  57. int Mouseok = 0;
  58.  
  59. /* getmouse - get current row and column of mouse */
  60. getmouse(amr,amc)
  61. int *amr;
  62. int *amc;
  63. {
  64.         *amr = MouseY / 8;
  65.         *amc = MouseX / 8;
  66. }
  67.  
  68. /* statmouse - return mouse button state (0=nothing pressed,1=left,2=right) */
  69. statmouse()
  70. {
  71.         nextevent(MOUSEBUTTONS, 0);
  72.         return MouseButtons;
  73. }
  74.  
  75. /* Return when either a console key or mouse button is pressed. */
  76. mouseorkey()
  77. {
  78.         return nextevent(MOUSEBUTTONS | VANILLAKEY, 1);
  79. }
  80.  
  81. flushconsole()
  82. {
  83.         while (nextevent(VANILLAKEY, 0) >= 0) ;
  84. }
  85.  
  86. getconsole()
  87. {
  88.         return nextevent(VANILLAKEY, 1);
  89. }
  90.  
  91. cursor(state)
  92. int state;
  93. {
  94.         int x, y;
  95.         x = R->cp_x;
  96.         y = R->cp_y - R->Font->tf_Baseline;
  97.         SetDrMd(R, COMPLEMENT);
  98.         RectFill(R, x, y, x+8, y+R->Font->tf_YSize-1);
  99.         SetDrMd(R, JAM2);
  100. }
  101.  
  102. nextevent(flags, wait)
  103. long flags;
  104. int wait;
  105. {
  106.         register int class, code;
  107.         register struct IntuiMessage *message;
  108.         int result;
  109.  
  110.         cursor(1);
  111.         ModifyIDCMP(W, CLOSEWINDOW | flags);
  112.         while (1)
  113.         {
  114.                 if (wait)
  115.                 {
  116.                     /* get next event, waiting if none are available */
  117.                     while ((message = (struct IntuiMessage *)GetMsg(W->UserPort)) == NULL)
  118.                     {
  119.                         Wait(1<<W->UserPort->mp_SigBit);
  120.                     }
  121.                 }
  122.                 else
  123.                 {
  124.                     /* get next event, but return if none are available */
  125.                     message = (struct IntuiMessage *)GetMsg(W->UserPort);
  126.                     if (message == NULL)
  127.                     {
  128.                         result = -1;
  129.                         break;
  130.                     }
  131.                 }
  132.                 class = message->Class;
  133.                 code = message->Code;
  134.                 MouseX = message->MouseX;
  135.                 MouseY = message->MouseY;
  136.                 ReplyMsg((struct Message *)message);
  137.  
  138.                 switch (class)
  139.                 {
  140.                 case VANILLAKEY:
  141.                         result = code;
  142.                         break;
  143.                 case CLOSEWINDOW:
  144.                         result = 'q';
  145.                         break;
  146.                 case MOUSEBUTTONS:
  147.                         switch (code)
  148.                         {
  149.                         case SELECTDOWN:
  150.                                 MouseButtons = 1;
  151.                                 break;
  152.                         case MENUDOWN:
  153.                                 MouseButtons = 2;
  154.                                 break;
  155.                         default:
  156.                                 MouseButtons = 0;
  157.                                 break;
  158.                         }
  159.                         result = MOUSE;
  160.                         break;
  161.                 default:
  162.                         continue;
  163.                 }
  164.                 break;
  165.         }
  166.         cursor(0);
  167.         return result;
  168. }
  169.  
  170. /*------------------------------------------------------------------------
  171.  * MIDI I/O Routines for Amiga.
  172.  *
  173.  * Uses low-level serial I/O for simultaneous reads and writes.
  174.  */
  175.  
  176. struct MsgPort  *MidiInPort, *MidiOutPort;
  177. struct IOExtSer *MidiIn, *MidiOut;
  178. int     SerOpen = 0;
  179.  
  180. char    MidiInBuf;
  181. int     MidiDataAvail = 0;
  182.  
  183. /*
  184.  * start an asynchronous read request from MIDI IN
  185.  */
  186.  
  187. startmidiread()
  188. {
  189.         MidiIn->IOSer.io_Data = (APTR) &MidiInBuf;
  190.         MidiIn->IOSer.io_Length = 1;
  191.         MidiIn->IOSer.io_Command = CMD_READ;
  192.         MidiIn->IOSer.io_Flags = IOF_QUICK;     /* use quick I/O */
  193.         BeginIO((struct IORequest *) MidiIn);
  194.         /* did I/O complete quickly? */
  195.         if ((MidiIn->IOSer.io_Flags & IOF_QUICK)) {
  196.                 /* wow, data's coming in fast! */
  197.                 MidiDataAvail = 1;
  198.         } else {
  199.                 /* no data this time, it'll arrive later */
  200.                 MidiDataAvail = 0;
  201.         }
  202. }
  203.  
  204. /*
  205.  * get a byte from MIDI IN.
  206.  * assumes startmidiread has been previously done.  if statmidi has been
  207.  * checked, this function will not wait.  otherwise, it will wait for
  208.  * a single byte to arrive if none is available.
  209.  */
  210.  
  211. getmidi()
  212. {
  213.         register struct Message *io;
  214.         int result;
  215.  
  216.         /* return previously-received data, if available */
  217.         if (MidiDataAvail) {
  218.                 result = MidiInBuf;
  219.                 /* start a new I/O */
  220.                 startmidiread();
  221.                 return result;
  222.         }
  223.  
  224.         /* read next available byte */
  225.         io = (struct Message *) CheckIO((struct IORequest *) MidiIn);
  226.         if (io == FALSE) {
  227.                 /* wait for next byte */
  228.                 WaitIO((struct IORequest *) MidiIn);
  229.                 io = &MidiIn->IOSer.io_Message;
  230.         }
  231.  
  232.         Remove(&io->mn_Node);
  233.         result = MidiInBuf;
  234.         startmidiread();        /* start I/O for next byte */
  235.         return result;
  236. }
  237.  
  238. /*
  239.  * write a byte to MIDI OUT
  240.  */
  241.  
  242. sendmidi(c)
  243. int c;
  244. {
  245.         char    buf = c;
  246.  
  247.         MidiOut->IOSer.io_Data = (APTR) &buf;
  248.         MidiOut->IOSer.io_Length = 1;
  249.         MidiOut->IOSer.io_Command = CMD_WRITE;
  250.         DoIO((struct IORequest *) MidiOut);     /* synchronous request */
  251. }
  252.  
  253. /*
  254.  * check if any midi data is waiting to be received
  255.  */
  256. statmidi()
  257. {
  258.         /* check if data has previously been received */
  259.         if (MidiDataAvail) return 1;
  260.  
  261.         /* check if i/o has completed */
  262.         return (CheckIO((struct IORequest *) MidiIn) == FALSE) ? 0 : 1;
  263. }
  264.  
  265. openmidi()
  266. {
  267.         /* create message port for serial device */
  268.         MidiInPort = (struct MsgPort *) CreatePort(SERIALNAME,0);
  269.         if (MidiInPort == NULL) fatal("Can't create MidiInPort");
  270.  
  271.         /* create i/o request block for serial device */
  272.         MidiIn = (struct IOExtSer *)
  273.                 CreateExtIO(MidiInPort, sizeof(struct IOExtSer));
  274.         if (MidiIn == NULL) fatal("Can't create MidiIn");
  275.  
  276.         /* open the serial device */
  277.         MidiIn->io_SerFlags = SERF_SHARED;
  278.         SerOpen = OpenDevice(SERIALNAME,0,(struct IORequest *) MidiIn,0) == 0 ? 1 : 0;
  279.         if (SerOpen == 0) fatal("Can't open serial.device");
  280.  
  281.         /* set serial device parameters */
  282.         MidiIn->io_Baud = 31250;
  283.         MidiIn->io_RBufLen = 8192;      /* large input buffer - if your synth
  284.                                          * sends dumps larger than this you
  285.                                          * will have to increase it. */
  286.         MidiIn->io_SerFlags = SERF_RAD_BOOGIE;
  287.         MidiIn->IOSer.io_Command = SDCMD_SETPARAMS;
  288.         DoIO((struct IORequest *) MidiIn);
  289.  
  290.         /* clone MidiIn into MidiOut to allow simultaneous i/o */
  291.         MidiOutPort = (struct MsgPort *) CreatePort("MidiOut",0);
  292.         if (MidiOutPort == NULL) fatal("Can't create MidiOutPort");
  293.  
  294.         MidiOut = (struct IOExtSer *)
  295.                         CreateExtIO(MidiOutPort, sizeof(struct IOExtSer));
  296.         *MidiOut = *MidiIn;
  297.         MidiOut->IOSer.io_Message.mn_ReplyPort = MidiOutPort;
  298.  
  299.         /* get the MIDI IN port started */
  300.         startmidiread();
  301. }
  302.  
  303. closemidi()
  304. {
  305.         if (SerOpen) CloseDevice((struct IORequest *)MidiIn);
  306.         if (MidiIn) DeleteExtIO((struct IORequest *)MidiIn);
  307.         if (MidiOut) DeleteExtIO((struct IORequest *)MidiOut);
  308.         if (MidiInPort) DeletePort(MidiInPort);
  309.         if (MidiOutPort) DeletePort(MidiOutPort);
  310. }
  311.  
  312. flushmidi()
  313. {
  314.         while ( STATMIDI )
  315.                 getmidi();
  316. }
  317.  
  318. long milliclock()
  319. {
  320.         unsigned int clock[2];
  321.         long milli;
  322.         timer(clock);
  323.         milli = clock[0] * 1000 + clock[1] / 1000;
  324.         return milli;
  325. }
  326.  
  327. millisleep(n)
  328. {
  329.         Delay(n/20);
  330. }
  331.  
  332. char *
  333. alloc(n)
  334. {
  335.         char *p;
  336.  
  337.         if ( (p=malloc((unsigned)n)) == (char *)NULL ) {
  338.                 fatal("GLIB is out of memory!");
  339.         }
  340.         return(p);
  341. }
  342.  
  343. struct TextAttr font =
  344. {
  345.         "topaz.font",TOPAZ_EIGHTY,FS_NORMAL,FPF_ROMFONT
  346. };
  347.  
  348. struct NewScreen ns =
  349. {
  350.         0,0,640,200,2,BLACK,WHITE,HIRES,CUSTOMSCREEN,&font,"",NULL,NULL
  351. };
  352.  
  353. windinit()
  354. {
  355.         struct NewWindow nw;
  356.  
  357.         if (S != NULL) return;
  358.  
  359.         IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 0);
  360.         GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",0);
  361.         if (IntuitionBase == NULL || GfxBase == NULL) exit(1);
  362.  
  363.         if ((S = (struct Screen *) OpenScreen(&ns)) == NULL) exit(1);
  364.  
  365.         nw.LeftEdge = 0;
  366.         nw.TopEdge = TOPEDGE;
  367.         nw.Width = WindowWidth;
  368.         nw.Height = WindowHeight;
  369.         nw.DetailPen = BLACK;
  370.         nw.BlockPen = WHITE;
  371.         nw.Title = NULL;
  372.         nw.Flags = SMART_REFRESH | ACTIVATE | BACKDROP |
  373.                    BORDERLESS | NOCAREREFRESH | RMBTRAP;
  374.         nw.IDCMPFlags = CLOSEWINDOW | VANILLAKEY;
  375.         nw.Type = CUSTOMSCREEN;
  376.         nw.FirstGadget = NULL;
  377.         nw.CheckMark = NULL;
  378.         nw.Screen = S;
  379.         nw.BitMap = NULL;
  380.         if ((W = (struct Window *) OpenWindow(&nw)) == NULL) exit(1);
  381.         R = W->RPort;
  382.         SetAPen(R, ORANGE);
  383.         SetBPen(R, BLACK);
  384.         ShowTitle(S, FALSE);
  385.         Cols=80;
  386.         Rows=24;
  387. }
  388.  
  389. windgoto(r,c)
  390. int r,c;
  391. {
  392.         Move(R, c*8, r*8 + R->Font->tf_Baseline);
  393. }
  394.  
  395. windeeol()
  396. {
  397.         int x, y;
  398.         x = R->cp_x;
  399.         y = R->cp_y  - R->Font->tf_Baseline;
  400.         SetAPen(R, BLACK);
  401.         RectFill(R, x, y, WindowWidth, y+R->Font->tf_YSize-1);
  402.         SetAPen(R, ORANGE);
  403. }
  404.  
  405. winderaserow(r)
  406. {
  407.         windgoto(r,0);
  408.         windeeol();
  409. }
  410.  
  411. windexit(r)
  412. int r;
  413. {
  414.         if (W) CloseWindow(W);
  415.         if (S) CloseScreen(S);
  416.         exit(r);
  417. }
  418.  
  419. windclear()
  420. {
  421.         SetAPen(R, BLACK);
  422.         RectFill(R, 0, 0, WindowWidth, WindowHeight);
  423.         SetAPen(R, ORANGE);
  424. }
  425.  
  426. /* windgets - get a line of input from the console, handling backspaces */
  427. windgets(s)
  428. char *s;
  429. {
  430.         char *origs = s;
  431.         int c;
  432.  
  433.         SetAPen(R, WHITE);
  434.         while ( (c=getconsole()) != '\n' && c!='\r' && c!= EOF ) {
  435.                 if ( c == '\b' ) {
  436.                         if ( s > origs ) {
  437.                                 wbackspace();
  438.                                 s--;
  439.                         }
  440.                 }
  441.                 else if (c == 24) {
  442.                         while (s > origs) {
  443.                                 wbackspace();
  444.                                 s--;
  445.                         }
  446.                 } else if (isprint(c)) {
  447.                         windputc(c);
  448.                         *s++ = c;
  449.                 }
  450.                 windrefresh();
  451.         }
  452.         *s = '\0';
  453.         SetAPen(R, ORANGE);
  454. }
  455.  
  456. windstr(s)
  457. char *s;
  458. {
  459.         Text(R, s, strlen(s));
  460. }
  461.  
  462. windputc(c)
  463. int c;
  464. {
  465.         char s = c;
  466.         Text(R, &s, 1);
  467. }
  468.  
  469. wbackspace()
  470. {
  471.         int x, y;
  472.         x = R->cp_x;
  473.         y = R->cp_y;
  474.         Move(R, x-8, y);
  475.         windputc(' ');
  476.         Move(R, x-8, y);
  477. }
  478.  
  479. windrefresh()
  480. {
  481. }
  482.  
  483. beep()
  484. {
  485.         DisplayBeep(S);
  486. }
  487.  
  488. windhigh()
  489. {
  490. }
  491.  
  492. windnorm()
  493. {
  494. }
  495.  
  496. struct IntuiText fataltext = { 1,0,COMPLEMENT,16,32,NULL,NULL,NULL };
  497. struct IntuiText canceltext = { 1,0,COMPLEMENT,2,2,NULL,"cancel",NULL };
  498.  
  499. fatal(text)
  500. char *text;
  501. {
  502.         fataltext.IText = text;
  503.         AutoRequest(W, &fataltext, NULL, &canceltext, 0, 0, 320, 90);
  504.         bye();
  505. }
  506.  
  507. /****************
  508.  * openls(), nextls(), and closels() are used to scan the current directory.
  509.  ***************/
  510.  
  511. struct FileInfoBlock *lsinfo = NULL;
  512.  
  513. openls()
  514. {
  515. }
  516.  
  517. char *
  518. nextls()
  519. {
  520.         int error;
  521.         if (lsinfo == NULL) {
  522.                 lsinfo = (struct FileInfoBlock *) alloc(sizeof(struct FileInfoBlock));
  523.                 error = dfind(lsinfo, "#?", 0);
  524.         } else {
  525.                 error = dnext(lsinfo);
  526.         }
  527.         if (error == 0) {
  528.                 return lsinfo->fib_FileName;
  529.         } else {
  530.                 return NULL;
  531.         }
  532. }
  533.  
  534. closels()
  535. {
  536.         free(lsinfo);
  537.         lsinfo = NULL;
  538. }
  539.