home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 287.lha / TY_v1.3 / src / win.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-07  |  10.1 KB  |  307 lines

  1. /*************************************************************************
  2.  ***                        win.c                        (JJB TEMPLAR) ***
  3.  *** Date begun: 8/8/89.                                               ***
  4.  *** Last modified: 26/8/89.                                           ***
  5.  *************************************************************************/
  6. /*** Intuition/Console device stuff for ty. These functions are        ***
  7.  *** currently used by io.c, ttyin.c, and output.c.                    ***
  8.  *** Most of the console code is lifted directly from the chapter on   ***
  9.  *** the console.device in the RKM:L&D.                                ***
  10.  *** Too easy to use IDCMP rather than console for input, so console   ***
  11.  *** input has been dropped. At the moment, this means multi-char      ***
  12.  *** commands have also been dropped.                                  ***
  13.  *************************************************************************/
  14.  
  15. #include <exec/types.h>
  16. #include <exec/io.h>
  17. #include <devices/conunit.h>
  18. #include <devices/inputevent.h>
  19. #include <intuition/intuition.h>
  20. #include <libraries/dos.h>
  21.  
  22. #include <proto/exec.h>
  23. #include <proto/intuition.h>
  24. #include <proto/graphics.h>
  25. #include <proto/dos.h>
  26.  
  27. #include "cmd.h"
  28. #include "gadg.h"
  29.  
  30. #define SHIFT       (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT)
  31. #define CONTROL     (IEQUALIFIER_CONTROL)
  32. #define MAXHORIZ    60          /* Max value for l_start */
  33.  
  34. extern char version[],sc_lower_left[];
  35. extern struct Gadget *fgadg;
  36. extern int  tty;                    /* Use as flag that cons is open */
  37. extern int  sc_width,sc_height;     /* Resize them here. */
  38. extern int  l_start;                /* Change this one here too. */
  39. extern int  has_resized;            /* Set if getc() notices NEWSIZE */
  40. extern int  maxim;
  41. extern BPTR file;
  42.  
  43. extern struct cmdmap uraw[];
  44. extern struct cmdmap lraw[];
  45. extern struct cmdmap lgad[];
  46. extern struct cmdmap rgad[];
  47.  
  48. extern void flush();
  49. extern void cleanup(char *,int);
  50. extern int  mapcmd(UBYTE,struct cmdmap *);
  51.  
  52. extern int  rbut(struct IntuiMessage *);
  53.  
  54. struct Window       *Window;
  55. struct NewWindow    NewWindow = {
  56.     1,1,639,199,0,1,CLOSEWINDOW|RAWKEY|GADGETUP|NEWSIZE|MOUSEBUTTONS,
  57.     WINDOWSIZING|WINDOWDEPTH|WINDOWCLOSE|WINDOWDRAG|SMART_REFRESH|ACTIVATE|RMBTRAP,
  58.     NULL,NULL,&version[0],NULL,NULL,250,109,1024,1024,WBENCHSCREEN};
  59.  
  60. struct IOStdReq *conWmsg;
  61. struct MsgPort  *conWport;
  62. struct ConUnit  *cunit;     /* Get at console parameters */
  63.  
  64. int     opencon();
  65. void    tOpen();
  66. void    tWrite(UBYTE *,int);
  67. void    tClose();
  68. void    texit(int,char *);
  69. int     getcmd();
  70. void    resize();
  71. void    doboom();
  72. void    movewin(int,int,int,int,int);
  73. void    horiz(int);
  74. int     linecount();
  75. int     conpos();           /* Return console x pos */
  76. int     getclick(struct IntuiMessage *);
  77.  
  78. int     opencon() /*=====================================================*/
  79. {
  80. register int    ret;
  81.     conWmsg->io_Data = (APTR)Window;
  82.     conWmsg->io_Length = sizeof(struct Window);
  83.     ret = OpenDevice("console.device",0,conWmsg,0);
  84.     return(ret);
  85. }
  86.  
  87. void    tOpen() /*=======================================================*/
  88. {
  89.     if (!(conWport = CreatePort("ty.conW",0)))
  90.                         texit(0,"ERROR: failed to open write port!\n");
  91.     if (!(conWmsg = CreateStdIO(conWport)))
  92.                         texit(1,"ERROR: failed to open write message!\n");
  93.     NewWindow.FirstGadget = fgadg;
  94.     if (!(Window = OpenWindow(&NewWindow)))
  95.                         texit(2,"ERROR: failed to open window!\n");
  96.     if (opencon())      texit(3,"ERROR: failed to open console.device!\n");
  97.  
  98.     cunit = (struct ConUnit *)conWmsg->io_Unit;
  99.     cunit->cu_Modes[2] = 0x30;      /* Turn off auto-wrap */
  100.  
  101.     tty = 123L;         /* Used as flag, until other code updated */
  102.     tWrite(" p",5); /* Turn off cursor */
  103.     SetWindowTitles(Window,(char *)-1L,version);
  104. }
  105.  
  106. void    tWrite(cp,sz) /*=================================================*/
  107. UBYTE   *cp;
  108. int     sz;
  109. {
  110.     conWmsg->io_Command = CMD_WRITE;
  111.     conWmsg->io_Data = (APTR)cp;
  112.     conWmsg->io_Length = sz;
  113.     DoIO(conWmsg);
  114. }
  115.  
  116. void    tClose() /*======================================================*/
  117. {
  118.     if (!tty) return;           /* Check flag */
  119.     tty = NULL;                 /* Reset flag. */
  120.     CloseDevice(conWmsg);       /* In reverse order to that in which     */
  121.     CloseWindow(Window);        /* they were procured. */
  122.     DeleteStdIO(conWmsg);
  123.     DeletePort(conWport);
  124. }
  125.  
  126. void    texit(bra,cp) /*=================================================*/
  127. int     bra;
  128. char    *cp;
  129. {
  130.     switch (bra) {
  131.         case (3): CloseWindow(Window);
  132.         case (2): DeleteStdIO(conWmsg);
  133.         case (1): DeletePort(conWport);
  134.         case (0): break;
  135.     }
  136.     cleanup(cp,20);
  137. }
  138.  
  139. int     getcmd() /*======================================================*/
  140. {                /* Get a command from the IDCMP.                        */
  141. struct IntuiMessage mvl;
  142. register struct IntuiMessage *msg;
  143. register int    ret = C_UNKNOWN;
  144. register int    loop = 1;
  145.  
  146.     if (maxim) {            /* Do here in case maxim set by file icon */
  147.         maxim = 0;          /* check instead of when ty started.      */
  148.         return(C_BOOM);
  149.     }
  150.  
  151.     flush();        /* Sync display */
  152.     while (loop) {
  153.         loop = 0;   /* Set back to one, to loop again. */
  154.         Wait(1 << Window->UserPort->mp_SigBit);
  155.         while (msg = (struct IntuiMessage *)GetMsg(Window->UserPort)) {
  156.             mvl = *msg;
  157.             ReplyMsg((struct Message *)msg);
  158.             switch (mvl.Class) {
  159.                 case (CLOSEWINDOW): ret = C_QUIT;   break;
  160.                 case (RAWKEY):
  161.                     if (mvl.Qualifier & CONTROL) switch (mvl.Code) {
  162.                         case (0x28): ret = C_RESIZE;    break;  /* L */
  163.                         default: loop = 1;
  164.                     }
  165.                     else if (mvl.Qualifier & SHIFT) {
  166.                         if ((ret = mapcmd((UBYTE)mvl.Code,uraw)) == C_UNKNOWN) loop = 1;
  167.                     }
  168.                     else {
  169.                         if ((ret = mapcmd((UBYTE)mvl.Code,lraw)) == C_UNKNOWN) loop = 1;
  170.                     }
  171.             break;
  172.                 case (GADGETUP):
  173.                     ret = mapcmd((UBYTE)(((struct Gadget *)(mvl.IAddress))->GadgetID),lgad);
  174.                     break;
  175.                 case (MOUSEBUTTONS): 
  176.                     if (mvl.Code == MENUUP) {
  177.                         if ((ret = mapcmd((UBYTE)rbut(&mvl),rgad)) == C_UNKNOWN) loop = 1;
  178.                     }
  179.                     else if (mvl.Code == SELECTDOWN) {
  180.                         ret = getclick(&mvl);
  181.                         if (ret == C_UNKNOWN) loop = 1;
  182.                     }
  183.                     else loop = 1;              /* REST */
  184.                     break;
  185.                 case (NEWSIZE): ret = C_RESIZE; break;
  186.             }
  187.         }
  188.     }
  189.  
  190.     return(ret);
  191. }
  192.  
  193. int     getc() /*========================================================*/
  194. {              /* Get a key from the IDCMP.                              */
  195. register struct IntuiMessage *msg;
  196. register int    ret = -1;
  197.  
  198.     while (ret < 0) {
  199.         Wait(1 << Window->UserPort->mp_SigBit);
  200.         while (msg = (struct IntuiMessage *)GetMsg(Window->UserPort)) {
  201.             if (msg->Class == RAWKEY) ret = msg->Code;
  202.           /* So clicking a gadget will be equiv to ... (press RETURN) */
  203.             else if (msg->Class & (GADGETUP|GADGETDOWN|CLOSEWINDOW)) ret = 0x44;
  204.             else if (msg->Class == NEWSIZE) has_resized = 1;
  205.             ReplyMsg((struct Message *)msg);
  206.         }
  207.         if ((ret >= 0x80) && (ret <= 0xf8)) ret = -1; /* Ignore up trans */
  208.     }
  209.     return(ret);
  210. }
  211.  
  212. void    resize() /*======================================================*/
  213. {
  214.     tWrite("tu",5);                   /* Get console to resize */
  215.  
  216.     sc_width = (int)cunit->cu_XMax + 1;     /* Then get from there */
  217.     sc_height = (int)cunit->cu_YMax + 2;
  218.  
  219.   /* Update control string to go to lower left. Much faster if it's updated
  220.    * only once every screen resize. */
  221.     sprintf(sc_lower_left,"%d;1H",sc_height);
  222.  
  223.     SetAPen(Window->RPort,0);
  224.     RectFill(Window->RPort,2,11,Window->Width - 17,Window->Height - 10);
  225. }
  226.  
  227. void    movewin(x,y,w,h,s) /*============================================*/
  228. int     x,y,w,h,s;
  229. {
  230. int     tx,ty,tw,th;
  231.  
  232.     tx = x - Window->LeftEdge;  ty = y - Window->TopEdge;
  233.     tw = w - Window->Width;     th = h - Window->Height;
  234.  
  235.     if (s) {
  236.         MoveWindow(Window,tx,ty);
  237.         SizeWindow(Window,tw,th);
  238.     }
  239.     else {
  240.         SizeWindow(Window,tw,th);
  241.         MoveWindow(Window,tx,ty);
  242.     }
  243. }
  244.  
  245. void    doboom() /*======================================================*/
  246. {
  247. static int  state = 0,x,y,w,h;
  248.  
  249.     if (!state) {
  250.         state = 1;
  251.         x = Window->LeftEdge;   y = Window->TopEdge;
  252.         w = Window->Width;      h = Window->Height;
  253.  
  254.         movewin(0,0,Window->WScreen->Width,Window->WScreen->Height,state);
  255.     }
  256.     else {
  257.         state = 0;
  258.         movewin(x,y,w,h,state);
  259.     }
  260. }
  261.  
  262. void    horiz(f) /*======================================================*/
  263. int     f;
  264. {
  265.     if (f) {        /* Move right. */
  266.         if (l_start < MAXHORIZ-10) l_start += 10;
  267.         else l_start = MAXHORIZ;
  268.     }
  269.     else {
  270.         if (l_start > 10) l_start -= 10;
  271.         else l_start = 0;
  272.     }
  273.     sethbar();
  274. }
  275.  
  276. int     linecount() /*===================================================*/
  277. {                   /* Return number of lines in "file".                 */
  278. char    buf[512];   /* Can I spare ½ K of stack at this point? Hope so.  */
  279. register int    ret = 0,j;
  280. register char   *cp,*e;
  281. ULONG   oldpos;
  282.     if (!file) return(0);
  283.     oldpos = Seek(file,0,OFFSET_BEGINNING);
  284.  
  285.     while ((j = Read(file,buf,512)) > 0)
  286.         for (cp = buf,e = buf + j; cp < e; cp++)
  287.             if (*cp == '\n') ret++;
  288.  
  289.     Seek(file,oldpos,OFFSET_BEGINNING);
  290.     return(ret);
  291. }
  292.  
  293. int     conpos() /*======================================================*/
  294. {
  295.     return((int)cunit->cu_XCCP);
  296. }
  297.  
  298. int     getclick(msg) /*=================================================*/
  299. struct IntuiMessage *msg;
  300. {
  301. register int    x,y;
  302.  
  303.     x = msg->MouseX;    y = msg->MouseY;
  304.     if ((x < 2) || (x > Window->Width-18) || (y < 11) || (y > Window->Height-10)) return(C_UNKNOWN);
  305.     return((y < (Window->Height >> 1))? C_BACK_PAGE: C_FORW_PAGE);
  306. }
  307.