home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / c / curses / src / _ansi.c next >
Encoding:
C/C++ Source or Header  |  1992-06-30  |  5.1 KB  |  209 lines

  1. /* -*-C-*-
  2.  *
  3.  *
  4.  * Filename : _ansi.c
  5.  *
  6.  * Author   : Simon J Raybould.    (sie@fulcrum.bt.co.uk).
  7.  *
  8.  * Date     : Thursday 28th May 1992.
  9.  *
  10.  * Desc     : ANSI support for curses.
  11.  *
  12.  *
  13.  * THIS CODE IS NOT PUBLIC DOMAIN
  14.  * ==============================
  15.  * 
  16.  * This code is copyright Simon J Raybould 1992, all rights are reserved.
  17.  * All code, ideas, data structures and algorithms remain the property of the
  18.  * author. Neither the whole nor sections of this code may be used as part
  19.  * of other code without the authors consent. If you wish to use some of this
  20.  * code then please email me at (sie@fulcrum.bt.co.uk).
  21.  *
  22.  * This source is not public domain, so you do not have any right to alter it
  23.  * or sell it for personal gain. The source is provided purely for reference
  24.  * purposes. You may re-compile the source with any compiler you choose.
  25.  * You must not distribute altered copies without the authors consent. My
  26.  * intention is that the source will help people isolate any bugs much more
  27.  * effectively.
  28.  *
  29.  * Disclaimer
  30.  * ==========
  31.  *
  32.  * No implication is made as to this code being fit for any purpose at all.
  33.  * I (the author) shall not be held responsible for any loss of data or damage 
  34.  * to property that may result from its use or misuse.
  35.  *
  36.  *
  37.  * Revision History
  38.  * ================
  39.  *
  40.  * $Log:    _ansi.c,v $
  41.  * Revision 1.1  92/06/10  23:46:04  sie
  42.  * Initial revision
  43.  * 
  44.  *
  45.  */
  46.  
  47. #include <libraries/dosextens.h>
  48. #include <errno.h>
  49. #include <exec/memory.h>
  50. #include <fcntl.h>
  51. #include "acurses.h"
  52.  
  53. static char *rcsid = "$Header: SRC:lib/curses/src/RCS/_ansi.c,v 1.1 92/06/10 23:46:04 sie Exp $";
  54.  
  55.  
  56. void
  57. ANSIClear()
  58. {
  59.   char *clrstr = "\033[H\033[J";
  60.   write(1, clrstr, 6);
  61. }
  62.  
  63. void
  64. ANSIMove(int line, int col)
  65. {
  66.   char buf[30];
  67.   
  68.   sprintf(buf, "\033[%d;%dH", line+1, col+1);
  69.   write(1, buf, strlen(buf));
  70. }
  71.  
  72. void
  73. ANSIFlash()
  74. {
  75.   write(1, "\007", 1);
  76. }
  77.  
  78. void
  79. ANSIClearRect(int line, int col, int height, int width)
  80. {
  81.   char buf[BUFSIZ], *ptr;
  82.   int i;
  83.   
  84.   if(!line && !col && height>=LINES-1 && width>=COLS-1) {
  85.     ANSIClear();
  86.     return;
  87.   }
  88.   if(!(ptr = malloc(width+1)))    /* 1 for the NULL */
  89.     return;
  90.   memset(ptr, ' ', width);
  91.   ptr[width] = '\0';        /* terminate  */
  92.   for(i=line; i<line+height; i++) {
  93.     sprintf(buf, "\033[%d;%dH%s", i+1, col+1, ptr);
  94.     write(1, buf, strlen(buf));
  95.   }
  96.   free(ptr);
  97. }
  98.  
  99.  
  100. /*
  101.  *
  102.  * Author   : Chuck McManis.
  103.  *
  104.  * Date     : 18th June 1987.
  105.  *
  106.  * Desc     : These are routines for setting a given stream to RawMode or
  107.  *            CanonMode mode on the Amiga.
  108.  *
  109.  */
  110.  
  111. /*
  112.  * Function RawMode() - Convert the specified File Handle to 'RawMode' mode. This
  113.  * only works on TTY's and essentially keeps DOS from translating keys for
  114.  * you.
  115.  */
  116.  
  117. long
  118. RawMode(BPTR afh)
  119. {
  120.     struct MsgPort *mp;        /* The File Handle message port */
  121.     long            Arg[1], res;
  122.  
  123.     mp = ((struct FileHandle *) (BADDR(afh)))->fh_Type;
  124.     Arg[0] = -1L;
  125.     res = send_packet(mp, ACTION_SCREEN_MODE, Arg, 1);
  126.     if (res == 0) {
  127.     errno = ENXIO;
  128.     return (-1);
  129.     }
  130.     return (0);
  131. }
  132.  
  133. /*
  134.  * Function - CanonMode() this function returns the designate file pointer to
  135.  * it's normal, wait for a <CR> mode. This is exactly like RawMode() except that
  136.  * it sends a 0 to the console to make it back into a CON: from a RAWMODE: 
  137.  */
  138.  
  139. long
  140. CanonMode(BPTR afh)
  141. {
  142.     struct MsgPort *mp;        /* The File Handle message port */
  143.     long            Arg[1], res;
  144.  
  145.     mp = ((struct FileHandle *) (BADDR(afh)))->fh_Type;
  146.     Arg[0] = 0L;
  147.     res = send_packet(mp, ACTION_SCREEN_MODE, Arg, 1);
  148.     if (res == 0) {
  149.     errno = ENXIO;
  150.     return (-1);
  151.     }
  152.     return (0);
  153. }
  154.  
  155. /*
  156.  * send_packet() - written by Phil Lindsay, Carolyn Scheppner, and Andy
  157.  * Finkel. This function will send a packet of the given type to the Message
  158.  * Port supplied. 
  159.  */
  160.  
  161. static long
  162. send_packet(pid, action, args, nargs)
  163.     struct MsgPort *pid;    /* process indentifier ... (handlers message
  164.                  * port ) */
  165.     long            action,    /* packet type ... (what you want handler to
  166.                  * do )   */
  167.                     args[],    /* a pointer to a argument list */
  168.                     nargs;    /* number of arguments in list  */
  169. {
  170.     struct MsgPort *replyport;
  171.     struct StandardPacket *packet;
  172.  
  173.     long            count, *pargs, res1;
  174.  
  175.     replyport = (struct MsgPort *) CreatePort(NULL, 0);
  176.     if (!replyport)
  177.     return (0);
  178.  
  179.     /* Allocate space for a packet, make it public and clear it */
  180.     packet = (struct StandardPacket *)
  181.     AllocMem((long) sizeof(struct StandardPacket),
  182.          MEMF_PUBLIC | MEMF_CLEAR);
  183.     if (!packet) {
  184.     DeletePort(replyport);
  185.     return (0);
  186.     }
  187.     packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt);
  188.     packet->sp_Pkt.dp_Link = &(packet->sp_Msg);
  189.     packet->sp_Pkt.dp_Port = replyport;
  190.     packet->sp_Pkt.dp_Type = action;
  191.  
  192.     /* copy the args into the packet */
  193.     pargs = &(packet->sp_Pkt.dp_Arg1);    /* address of first argument */
  194.     for (count = 0; count < nargs; count++)
  195.     pargs[count] = args[count];
  196.  
  197.     PutMsg(pid, packet);    /* send packet */
  198.  
  199.     WaitPort(replyport);
  200.     GetMsg(replyport);
  201.  
  202.     res1 = packet->sp_Pkt.dp_Res1;
  203.  
  204.     FreeMem(packet, (long) sizeof(struct StandardPacket));
  205.     DeletePort(replyport);
  206.  
  207.     return (res1);
  208. }
  209.