home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff238a.lzh / DMouse / blanker.c < prev    next >
C/C++ Source or Header  |  1989-08-24  |  5KB  |  233 lines

  1.  
  2. /*
  3.  *  BLANKER.C
  4.  *
  5.  *  Example external screen blanker for DMouse.
  6.  */
  7.  
  8. #include <local/typedefs.h>
  9. #include <local/ipc.h>
  10. #include <local/xmisc.h>
  11.  
  12. #ifdef LATTICE
  13. #include <dos.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18. int __stdargs CXBRK(void);
  19.  
  20. __stdargs    /*  bug in lcr.lib  */
  21. CXBRK()
  22. {
  23.     return(0);
  24. }
  25.  
  26. #else
  27. extern int Enable_Abort;    /*    CLI break enable    */
  28. #endif
  29.  
  30.  
  31. #define RATE    1000000
  32.  
  33. typedef struct IORequest IORequest;
  34.  
  35. static SCR *Scr;
  36. static TA   Ta = {  (ubyte *)"topaz.font", 8 };
  37. static NS   Ns = {  0, 0, 320, -1, 1, -1, -1, 0, CUSTOMSCREEN|SCREENQUIET, &Ta };
  38.  
  39. static IOT Iot;
  40. static char Ioip;
  41. static char *TStr = "Blank";
  42. static char *TSpa = "     ";
  43. static long TLen = 5;
  44.  
  45. void main ARGS((int, char **));
  46. void screenon ARGS((void));
  47. void screenoff ARGS((void));
  48. void screengraphics ARGS((void));
  49. long myrand ARGS((void));
  50.  
  51. void
  52. main(ac,av)
  53. char **av;
  54. {
  55.     PORT *dmport;
  56.     PORT *ipport;
  57.     PORT *tp;
  58.     IORequest    AddReq;
  59.     IORequest    RemReq;
  60.     char foo;    /*  dummy   */
  61.     char notdone = 1;
  62.  
  63. #ifndef LATTICE
  64.     Enable_Abort = 0;
  65. #endif
  66.  
  67.     tp       = CreatePort(NULL, 0);
  68.     ipport = CreatePort(NULL, 0);
  69.  
  70.     AddReq.io_Message.mn_ReplyPort = ipport;
  71.     AddReq.io_Command = 0x84;
  72.     AddReq.io_Unit = (struct Unit *)&foo;
  73.     AddReq.io_Flags = 0x0C;    /* %1100 (screen blanker, no mouse blanker) */
  74.  
  75.     RemReq.io_Message.mn_ReplyPort = ipport;
  76.     RemReq.io_Command = 0x85;
  77.     RemReq.io_Unit = (struct Unit *)&foo;
  78.  
  79.     if (openlibs(INTUITION_LIB|GRAPHICS_LIB) == 0)
  80.     goto fail;
  81.     if (OpenDevice("timer.device", UNIT_VBLANK, &Iot, 0)) {
  82.     Write(Output(), "Unable to open timer.device\n", 28);
  83.     goto fail;
  84.     }
  85.     Iot.tr_node.io_Message.mn_ReplyPort = tp;
  86.     Iot.tr_node.io_Command = TR_ADDREQUEST;
  87.  
  88.     SetSignal(0, SIGBREAKF_CTRL_C);
  89.     Forbid();
  90.     if (dmport = FindPort("DMouse.ipc"))
  91.     PutMsg(dmport, &AddReq.io_Message);
  92.     Permit();
  93.     if (dmport == NULL) {
  94.     puts("DMouse not running or <V1.20");
  95.     goto fail;
  96.     }
  97.  
  98.     while (notdone) {
  99.     long mask = (1 << tp->mp_SigBit) | (1 << ipport->mp_SigBit) | SIGBREAKF_CTRL_C;
  100.     mask = Wait(mask);
  101.  
  102.     if (mask & SIGBREAKF_CTRL_C) {
  103.         notdone = 0;
  104.     }
  105.     if ((mask & (1 << tp->mp_SigBit)) && Scr && Ioip && CheckIO(&Iot)) {
  106.         WaitIO(&Iot);
  107.         Iot.tr_time.tv_secs  = RATE / 1000000;
  108.         Iot.tr_time.tv_micro = RATE % 1000000;
  109.         SendIO(&Iot);
  110.         screengraphics();
  111.     }
  112.     if (mask & (1 << ipport->mp_SigBit)) {
  113.         IORequest *ior;
  114.         while (ior = (IORequest *)GetMsg(ipport)) {
  115.         if (ior->io_Message.mn_Node.ln_Type == NT_REPLYMSG) {   /* my AddHand req */
  116.             notdone = 0;
  117.             continue;
  118.         }
  119.         switch(ior->io_Command) {
  120.         case 0x80:
  121.         case 0x81:
  122.             break;
  123.         case 0x82:
  124.             screenon();
  125.             break;
  126.         case 0x83:
  127.             screenoff();
  128.             break;
  129.         case 0x86:
  130.             notdone = 0;
  131.             break;
  132.         }
  133.         ReplyMsg(&ior->io_Message);
  134.         }
  135.     }
  136.     }
  137.     PutMsg(dmport, &RemReq.io_Message);
  138.     WaitMsg(&RemReq);
  139.     {
  140.     register IORequest *ior = NULL;
  141.     while (ior != &AddReq) {    /*  last msg will be AddReq */
  142.         WaitPort(ipport);
  143.         ior = (IORequest *)GetMsg(ipport);
  144.         if (ior->io_Message.mn_Node.ln_Type == NT_MESSAGE)
  145.         ReplyMsg(&ior->io_Message);
  146.     }
  147.     }
  148. fail:
  149.     if (Ioip) {
  150.     AbortIO(&Iot);
  151.     WaitIO(&Iot);
  152.     }
  153.     if (Iot.tr_node.io_Device)
  154.     CloseDevice(&Iot);
  155.     if (tp)
  156.     DeletePort(tp);
  157.     if (ipport)
  158.     DeletePort(ipport);
  159.     closelibs(-1);
  160. }
  161.  
  162. void
  163. screenoff()
  164. {
  165.     if (Scr)
  166.     ScreenToFront(Scr);
  167.     else if (Scr = OpenScreen(&Ns)) {
  168.     if (!Ioip) {
  169.         Iot.tr_time.tv_secs  = 3;
  170.         Iot.tr_time.tv_micro = 0;
  171.         SendIO(&Iot);
  172.         Ioip = 1;
  173.     }
  174.     }
  175. }
  176.  
  177. void
  178. screenon()
  179. {
  180.     if (Scr)
  181.     CloseScreen(Scr);
  182.     Scr = NULL;
  183.     if (Ioip) {
  184.     AbortIO(&Iot);
  185.     WaitIO(&Iot);
  186.     Ioip = 0;
  187.     }
  188. }
  189.  
  190. /*
  191.  *  Simple Stupid
  192.  *
  193.  *  Warning:  Note that no clipping is done when using the screen's
  194.  *          rastport, all rendering must be IN BOUNDS!
  195.  */
  196.  
  197. void
  198. screengraphics()
  199. {
  200.     static short oldx, oldy;
  201.     short x, y;
  202.  
  203.     x = (myrand() & 4095) % (Scr->Width - TextLength(&Scr->RastPort, TStr, TLen) - 8);
  204.     y = (myrand() & 4095) % (Scr->Height- Scr->RastPort.TxHeight - Scr->RastPort.TxBaseline - 8);
  205.     if (x < 0 || y < 0)
  206.     return;
  207.     y += Scr->RastPort.TxBaseline + 4;
  208.  
  209.     SetAPen(&Scr->RastPort, 0);
  210.     Move(&Scr->RastPort, oldx, oldy);
  211.     Text(&Scr->RastPort, TSpa, TLen);
  212.  
  213.     oldx = x;
  214.     oldy = y;
  215.     SetRGB4(&Scr->ViewPort, 1, myrand()&15, myrand()&15, myrand()&15);
  216.  
  217.     SetAPen(&Scr->RastPort, 1);
  218.     Move(&Scr->RastPort, oldx, oldy);
  219.     Text(&Scr->RastPort, TStr, TLen);
  220. }
  221.  
  222.  
  223. long
  224. myrand()
  225. {
  226.     static long rv = 34987;
  227.     rv = (rv * 13 + 1) ^ (rv >> 13);
  228.     return(rv);
  229. }
  230.  
  231.  
  232.  
  233.