home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 001-099 / ff093.lzh / MicroEmacs / source / src.arc / console.c < prev    next >
C/C++ Source or Header  |  1987-08-16  |  9KB  |  379 lines

  1. /*
  2.  * Misc. console.device/window functions 
  3.  *
  4.  *      Feel free to use this code anywhere you please...Just keep
  5.  *        "this" information in the source. 
  6.  *                                              Phillip Lindsay
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include "estruct.h"
  11. #include "edef.h"
  12.  
  13. #if AMIGA
  14. #undef TRUE
  15. #undef FALSE
  16. #include <exec/types.h>
  17. #include <exec/nodes.h>
  18. #include <exec/lists.h>
  19. #include <exec/tasks.h>
  20. #include <exec/ports.h>
  21. #include <exec/io.h>
  22. #include <devices/console.h>
  23. #include <devices/conunit.h>
  24. #include <libraries/dos.h>
  25. #include <libraries/dosextens.h>
  26. #include <graphics/clip.h>
  27. #include <graphics/view.h>
  28. #include <graphics/rastport.h>
  29. #include <graphics/layers.h>
  30. #include <graphics/text.h>
  31. #include <intuition/intuition.h>
  32.  
  33. LONG                OpenConsole();
  34. VOID                cleanup();
  35. extern  struct  Screen        *OpenScreen();
  36. extern    struct    Window        *OpenWindow();
  37. extern    struct    MsgPort        *CreatePort();
  38. extern    struct    IOStdReq    *CreateStdIO();
  39. extern    LONG            OpenDevice();
  40. extern  struct  Task            *FindTask();
  41. extern    LONG            DoIO();
  42.  
  43. #define CONSOLENAME        "console.device"
  44.  
  45. #define WINFLAGS1    WINDOWSIZING | WINDOWDRAG | WINDOWDEPTH
  46. #define WINFLAGS2       SMART_REFRESH | ACTIVATE | NOCAREREFRESH
  47. #define WINFLAGS      (WINFLAGS1 | WINFLAGS2)
  48.  
  49. #define SCRWINFLAGS1    BORDERLESS | BACKDROP | NOCAREREFRESH | ACTIVATE
  50. #define SCRWINFLAGS    SCRWINFLAGS1 | SMART_REFRESH
  51.  
  52. /*
  53.  * Intuition global variables
  54.  */
  55.  
  56. struct TextAttr SafeFont =
  57.       {
  58.       (UBYTE *)"topaz.font",
  59.       TOPAZ_EIGHTY,
  60.       0,
  61.       FS_NORMAL
  62.       };
  63.  
  64. struct    IntuitionBase    *IntuitionBase=NULL;    /* library bases    */
  65. struct  GfxBase        *GfxBase=NULL;
  66. /* struct  DiskfontBase     *DiskfontBase=NULL; */
  67. struct    Window        *EmacsWindow=NULL;    /* Our window        */
  68. struct    MsgPort        *consoleport=NULL;    /* I/O port         */
  69. struct    IOStdReq    *consoleWriteMsg=NULL;     /* I/O messages        */
  70. struct    IOStdReq    *consoleReadMsg=NULL;
  71. struct  Window          *savewindow=-1L;
  72.  
  73. char     screenkey[] =   "EmacsScreen";
  74.  
  75. /*
  76.  * Open a console device, given a read request
  77.  * and a write request message.
  78.  */
  79.  
  80. LONG OpenConsole(writerequest,readrequest,window)
  81. struct IOStdReq *writerequest;
  82. struct IOStdReq *readrequest;
  83. struct Window *window;
  84. {
  85.     LONG error; 
  86.     writerequest->io_Data = (APTR) window;
  87.     writerequest->io_Length = (ULONG) sizeof(*window);
  88.     error = OpenDevice(CONSOLENAME, 0L, writerequest, 0L);
  89.  
  90.     /* clone required parts of the request */
  91.     readrequest->io_Device = writerequest->io_Device;
  92.     readrequest->io_Unit   = writerequest->io_Unit;
  93.     return( (LONG) error);
  94. }
  95.  
  96. /*
  97.  * Output a single character    
  98.  * to a specified console
  99.  */ 
  100.  
  101. LONG ConPutChar(request,character)
  102. struct IOStdReq *request;
  103. char character;
  104. {
  105.     request->io_Command = CMD_WRITE;
  106.     request->io_Data = (APTR)&character;
  107.     request->io_Length = (ULONG)1;
  108.     return( (LONG) DoIO(request) );
  109. }
  110.  
  111. /*
  112.  * Output a NULL-terminated string of
  113.  * characters to a console
  114.  */ 
  115.  
  116. LONG ConPutStr(request,string)
  117. struct IOStdReq *request;
  118. char *string;
  119. {
  120.     request->io_Command = CMD_WRITE;
  121.     request->io_Data = (APTR)string;
  122.     request->io_Length = (LONG)-1;
  123.     return( (LONG) DoIO(request) );
  124. }
  125.  
  126. /*
  127.  * Write out a string of predetermined
  128.  * length to the console
  129.  */
  130.  
  131. LONG ConWrite(request,string,len)
  132. struct IOStdReq *request;
  133. char *string;
  134. LONG len;
  135. {
  136.     request->io_Command = CMD_WRITE;
  137.     request->io_Data = (APTR)string;
  138.     request->io_Length = (ULONG)len;
  139.     return( (LONG) DoIO(request) );
  140. }
  141.  
  142. /*
  143.  * read a character.
  144.  */
  145.  
  146. LONG GetChar(request,whereto)
  147. struct IOStdReq *request;
  148. char *whereto;
  149. {
  150.     request->io_Command = CMD_READ;
  151.     request->io_Data = (APTR)whereto;
  152.     request->io_Length = (LONG)1;
  153.     return((LONG)DoIO(request));
  154. }
  155.  
  156. VOID CursorOff(request)
  157. struct IOStdReq *request;
  158. {
  159.     register struct IOStdReq *req=request;
  160.     
  161.     req->io_Command = CMD_WRITE;
  162.     req->io_Data    = (APTR) "\X9B0 p";
  163.     req->io_Length  =  -1L;
  164.     DoIO(req);
  165. }
  166.  
  167. VOID CursorOn(request)
  168. struct IOStdReq *request;
  169. {
  170.     register struct IOStdReq *req=request;
  171.     
  172.     req->io_Command = CMD_WRITE;
  173.     req->io_Data    = (APTR) "\X9B p";
  174.     req->io_Length  =  -1L;
  175.     DoIO(req);
  176. }
  177.  
  178. /*
  179.  * memfill() - fill memory with supplied byte value for "size" bytes
  180.  */
  181. void memfill(source,size,value)
  182. UBYTE *source;
  183. ULONG size;
  184. UBYTE value;    
  185. {
  186.  register UBYTE *msource=source,
  187.         mvalue=value,
  188.         *mend = &source[size];
  189.  
  190.  while( msource < mend ) *msource++ = mvalue;  
  191.  
  192. }
  193.  
  194. LONG screendefaults(width,height,modes)
  195. SHORT *width, *height;
  196. UWORD *modes;
  197. {
  198.  struct Screen      wbscreen;
  199.  
  200.  if(!(GetScreenData(&wbscreen,(ULONG)sizeof(struct Screen),WBENCHSCREEN,0L)))
  201.     return( (LONG) FALSE);    
  202.  
  203.  *width  = wbscreen.Width;
  204.  *height = wbscreen.Height;
  205.  *modes  = wbscreen.ViewPort.Modes;   
  206.  
  207.  return( (LONG) TRUE);
  208. }
  209.  
  210. struct Screen *InitScreen(title,width,height,modes)
  211. UBYTE *title;
  212. SHORT width,height;
  213. UWORD modes;
  214. {
  215.  struct NewScreen newscreen;
  216.  
  217.  memfill(&newscreen,(ULONG)sizeof(struct NewScreen),0L);
  218.  
  219. #ifdef DEBUG
  220.   kprintf("Opening Screen..w:%d h:%d m:%d\n",width,height,modes);
  221. #endif
  222.  
  223.  newscreen.Width        = width;
  224.  newscreen.Height       = height;
  225.  newscreen.Depth        = 1; 
  226.  newscreen.BlockPen     = 1;
  227.  newscreen.ViewModes    = modes;
  228.  newscreen.Type         = CUSTOMSCREEN;
  229.  newscreen.Font         = &SafeFont;
  230.  newscreen.DefaultTitle = title;
  231.  
  232.  return((struct Screen *)OpenScreen(&newscreen));
  233. }
  234.  
  235. /* 
  236.     Basically this function opens a window, but depending on what is
  237.     passed as arguments a window will be opened in their own screen.
  238.     winx = -1, opens a window on a workbench size/mode screen
  239.     winx = -1, winy = -1 
  240.         opens a window on a interlace screen based on workbench screen
  241. */
  242. struct Window *InitWindow(winx,winy,wwidth,wheight,title)
  243. WORD  winx,winy,wwidth,wheight;
  244. UBYTE *title;
  245. {
  246.  struct NewWindow   newwindow;
  247.  SHORT    width,height;
  248.  UWORD    modes;
  249.  
  250.  if(!(screendefaults(&width,&height,&modes)))
  251.   return( (struct Window *) 0L);    
  252.  
  253. #ifdef DEBUG
  254. kprintf("InitWindow: x=%d,y=%d,wx=%d,wy=%d,title=%s\n",
  255.     winx,winy,wwidth,wheight,title); 
  256. #endif
  257.  
  258.  memfill(&newwindow,(ULONG)sizeof(struct NewWindow),0L);
  259.  if(winx == -1)
  260.   {
  261.    if(winy == -1 && !(modes & LACE))
  262.     {
  263.         modes  |= LACE;             /* here we make a magic assumption */
  264.         height *= 2;
  265.     }
  266.    if(!(newwindow.Screen = InitScreen(title,width,height,modes)))
  267.     return( (struct Window *) 0L);
  268.     
  269.    ShowTitle(newwindow.Screen,(LONG) FALSE);
  270.    newwindow.Screen->UserData = (UBYTE *) screenkey;    
  271.    winx               = 0;
  272.    winy               = (SHORT) newwindow.Screen->BarHeight;
  273.    wwidth             = width; 
  274.    wheight            = height - newwindow.Screen->BarHeight;
  275.    newwindow.Type     = CUSTOMSCREEN;
  276.    newwindow.Flags    = SCRWINFLAGS;
  277.    title              = NULL; /* so we don't get a title in window */
  278.   } 
  279.   
  280.  newwindow.LeftEdge     = winx;
  281.  newwindow.TopEdge      = winy;
  282.  newwindow.Width           = (!wwidth  ? width : wwidth);
  283.  newwindow.Height         = (!wheight ? height : wheight);
  284.  newwindow.DetailPen      = -1L;
  285.  newwindow.BlockPen       = -1L;
  286.  
  287.  if(newwindow.Type != CUSTOMSCREEN)
  288.   {
  289.    newwindow.Title         = title; 
  290.    newwindow.Flags         = WINFLAGS;
  291.    newwindow.MinWidth      = 71L;
  292.    newwindow.MinHeight     = 22L; 
  293.    newwindow.MaxWidth      = -1L;
  294.    newwindow.MaxHeight     = -1L;
  295.    newwindow.Type          = WBENCHSCREEN;
  296.   }
  297.  
  298.  return((struct Window *)OpenWindow(&newwindow));
  299.  
  300. }
  301.  
  302. /*
  303.     this function close a window and if the window is attached to a 
  304.     screen "we opened" that screen will also be closed
  305. */
  306.     
  307. void CloseScrWin(win)
  308. struct Window *win;
  309. {
  310.  struct Screen *scr;
  311.  
  312.  scr = win->WScreen;
  313.  
  314.  CloseWindow(win);
  315.  
  316. /* make sure this is OUR screen */
  317.  if(((scr->Flags & SCREENTYPE) != WBENCHSCREEN) &&
  318.      !(strcmp(scr->UserData,screenkey))) 
  319.     CloseScreen(scr);
  320. }
  321.  
  322. /*
  323.     This function determines the number of rows/cols available on a given
  324.     window. If the window type becomes more complicated these routines
  325.     might need some additional logic. (ie. worry about borders and such )
  326. */    
  327.  
  328. UWORD getrows(Win,Font)
  329. struct Window   *Win;
  330. struct TextFont *Font;
  331. {
  332.     register struct TextFont *font=Font;
  333.     register struct Window   *win=Win; 
  334.       
  335.     return( (UWORD) win->Height/font->tf_YSize);   
  336. }
  337.  
  338. UWORD getcols(Win,Font)
  339. struct Window   *Win;
  340. struct TextFont *Font;
  341. {
  342.     register struct TextFont *font=Font;
  343.     register struct Window   *win=Win; 
  344.     
  345.     return( (UWORD) win->Width/font->tf_XSize);   
  346. }
  347.  
  348. /*
  349.     here is the Amiga specific cleanup code for MicroEMACS.
  350. */
  351.     
  352. void cleanup()
  353. {
  354.     struct Process *myproc;
  355.     
  356.         if(savewindow != -1L)
  357.          {
  358.           myproc = (struct Process *) FindTask(0L);        
  359.        Forbid();
  360.        myproc->pr_WindowPtr = (APTR) savewindow;
  361.        Permit();
  362.       }
  363.     if(consoleReadMsg)
  364.      {
  365.      if(consoleReadMsg->io_Device)
  366.         CloseDevice(consoleReadMsg);    
  367.            DeleteStdIO(consoleReadMsg);
  368.          }
  369.     if(consoleWriteMsg)    DeleteStdIO(consoleWriteMsg);
  370.     if(consoleport)        DeletePort(consoleport);
  371.     if(EmacsWindow)     CloseScrWin(EmacsWindow);
  372.     if(GfxBase)         CloseLibrary(GfxBase);
  373.     if(IntuitionBase)    CloseLibrary(IntuitionBase);
  374.  
  375. }
  376.  
  377. /* end of console.c */
  378. #endif
  379.