home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 152.lha / TWin.c < prev    next >
C/C++ Source or Header  |  1988-04-26  |  4KB  |  127 lines

  1. /* TWin.c - Text Server for debugging, by Talin. */
  2. /* to start:
  3.  
  4.         Run TWin.
  5.  
  6.     to print text:
  7.  
  8.         send a message to the message port "Text", where the ln_Name of the
  9.         message points to a null-terminated text string to be printed.
  10.  
  11.     to quit TWin - click on close box.
  12.  
  13.     example calling code:
  14.  
  15. TaskWrite(msg) char *msg;
  16. {    struct MsgPort *tport, *rport, *FindPort();
  17.     struct Message mymessage;
  18.     if (rport = CreatePort(NULL,0))
  19.     {    rport->mp_Flags = PA_SIGNAL;
  20.         Forbid();
  21.         if (tport = FindPort("Text"))
  22.         {    mymessage.mn_Node.ln_Name = msg;
  23.             mymessage.mn_Node.ln_Pri = 0;
  24.             mymessage.mn_Node.ln_Type = NT_MESSAGE;
  25.             mymessage.mn_ReplyPort = rport;
  26.             PutMsg(tport,&mymessage);
  27.             WaitPort(rport);
  28.         }
  29.         Permit();
  30.         DeletePort(rport);
  31.     }
  32. }
  33.  
  34. */
  35.  
  36.  
  37. #include "exec/types.h"
  38. #include "graphics/gfx.h"
  39. #include "graphics/gfxbase.h"
  40. #include "libraries/diskfont.h"
  41. #include "intuition/intuition.h"
  42.  
  43. struct Library             *IntuitionBase=NULL,
  44.                         *GfxBase=NULL,
  45.                         *OpenLibrary();
  46. struct Window             *Window=NULL,
  47.                         *OpenWindow();
  48. struct RastPort         *rp;
  49.  
  50. struct IOStdReq         write_req;                /* console io request */
  51. struct MsgPort            *con_port=NULL,            /* console message port */
  52.                         *task_port=NULL,        /* message port from other tasks */
  53.                         *CreatePort();
  54.  
  55. struct NewWindow text_window =
  56. {    50,50,400,100, 2,1,
  57.      CLOSEWINDOW,
  58.     NOCAREREFRESH | SIMPLE_REFRESH | WINDOWSIZING | WINDOWDEPTH | WINDOWCLOSE | WINDOWDRAG,
  59.     NULL,NULL,(UBYTE *)"Task Output Server",NULL,NULL,100,50,1000,1000,WBENCHSCREEN,
  60. };
  61.  
  62. #define SCAT goto exit_pgm                        /* standard escape function */
  63.  
  64. _main()
  65. {    short i;
  66.  
  67.     if ((IntuitionBase = OpenLibrary("intuition.library",0)) == NULL) SCAT;
  68.     if ((GfxBase = OpenLibrary("graphics.library",0)) == NULL) SCAT;
  69.  
  70.     if ((Window = OpenWindow(&text_window)) == NULL) SCAT;
  71.     rp = Window->RPort;
  72.     SetAPen(rp,3); SetBPen(rp,2);
  73.  
  74.     if (!(con_port = CreatePort("ConRead",0))) SCAT;
  75.     if (!(task_port = CreatePort("Text",0))) SCAT;
  76.     task_port->mp_Flags = PA_SIGNAL;
  77.  
  78.     write_req.io_Data = (APTR)Window;                            /* open console */
  79.     write_req.io_Length = sizeof(* Window);
  80.     write_req.io_Message.mn_Node.ln_Type = NT_MESSAGE;
  81.     write_req.io_Message.mn_Node.ln_Pri = 0;
  82.     write_req.io_Message.mn_ReplyPort = con_port;
  83.     if ( OpenDevice("console.device",0,&write_req,0) != 0) SCAT;
  84.  
  85.     while (TRUE)
  86.     {    LONG    dsig = 1 << task_port->mp_SigBit,                /* task signal */
  87.                 isig = 1 << Window->UserPort->mp_SigBit,        /* intuition sig */
  88.                 signals;                                        /* sigs recv'd */
  89.         void *GetMsg();
  90.  
  91.         signals = Wait(dsig | isig);                            /* wait for sigs */
  92.  
  93.         if (signals & isig)                                        /* if intuition */
  94.         {    struct IntuiMessage     *message;                    /* intuition msg */
  95.             long                    class;
  96.  
  97.             while (message = GetMsg(Window->UserPort))            /* get messages */
  98.             {    class = message->Class;                            /* get msg class */
  99.                 ReplyMsg(message);                                /* reply */
  100.  
  101.                 if (class == CLOSEWINDOW) SCAT;                    /* close the window */
  102.             }
  103.         }
  104.         
  105.         if (signals & dsig)                                        /* if another task */
  106.         {    struct Message *message;                            /* exec message */
  107.  
  108.             while (message = GetMsg(task_port))                    /* get message */
  109.             {    write_req.io_Command = CMD_WRITE;                /* write to console */
  110.                 write_req.io_Data = (APTR)(message->mn_Node.ln_Name);  /* string */
  111.                 write_req.io_Length = -1;                        /* null terminate */
  112.                 DoIO(&write_req);                                /* do it */
  113.  
  114.                 ReplyMsg(message);                                /* reply */
  115.             }
  116.         }
  117.     }
  118.  
  119. exit_pgm:                                                        /* clean up */
  120.     if (write_req.io_Device) CloseDevice(&write_req);
  121.     if (con_port) DeletePort (con_port);
  122.     if (task_port) DeletePort (task_port);
  123.     if (Window) CloseWindow(Window);
  124.     if (GfxBase) CloseLibrary(GfxBase);
  125.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  126. }
  127.