home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 283.lha / DMouse_v1.20 / src / blanker.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-07  |  4.5 KB  |  237 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.  
  40. static IOT Iot;
  41. static char Ioip;
  42. static char *TStr = "Blank";
  43. static char *TSpa = "     ";
  44. static long TLen = 5;
  45.  
  46. void main ARGS((int, char **));
  47. void screenon ARGS((void));
  48. void screenoff ARGS((void));
  49. void screengraphics ARGS((void));
  50. long myrand ARGS((void));
  51.  
  52. void
  53. main(ac,av)
  54. char **av;
  55. {
  56.     PORT *dmport;
  57.     PORT *ipport;
  58.     PORT *tp;
  59.     IORequest    AddReq;
  60.     IORequest    RemReq;
  61.     char foo;    /*  dummy   */
  62.     char notdone = 1;
  63.  
  64. #ifndef LATTICE
  65.     Enable_Abort = 0;
  66. #endif
  67.  
  68.     tp       = CreatePort(NULL, 0);
  69.     ipport = CreatePort(NULL, 0);
  70.  
  71.     AddReq.io_Message.mn_ReplyPort = ipport;
  72.     AddReq.io_Command = 0x84;
  73.     AddReq.io_Unit = (struct Unit *)&foo;
  74.     AddReq.io_Flags = 0x0C;    /* %1100 (screen blanker, no mouse blanker) */
  75.  
  76.     RemReq.io_Message.mn_ReplyPort = ipport;
  77.     RemReq.io_Command = 0x85;
  78.     RemReq.io_Unit = (struct Unit *)&foo;
  79.  
  80.     if (openlibs(INTUITION_LIB|GRAPHICS_LIB) == 0)
  81.     goto fail;
  82.     if (OpenDevice("timer.device", UNIT_VBLANK, &Iot, 0)) {
  83.     Write(Output(), "Unable to open timer.device\n", 28);
  84.     goto fail;
  85.     }
  86.     Iot.tr_node.io_Message.mn_ReplyPort = tp;
  87.     Iot.tr_node.io_Command = TR_ADDREQUEST;
  88.  
  89.     SetSignal(0, SIGBREAKF_CTRL_C);
  90.     Forbid();
  91.     if (dmport = FindPort("DMouse.ipc"))
  92.     PutMsg(dmport, &AddReq.io_Message);
  93.     Permit();
  94.     if (dmport == NULL) {
  95.     puts("DMouse not running or <V1.20");
  96.     goto fail;
  97.     }
  98.  
  99.     while (notdone) {
  100.     long mask = (1 << tp->mp_SigBit) | (1 << ipport->mp_SigBit) | SIGBREAKF_CTRL_C
  101.  
  102.     mask = Wait(mask);
  103.  
  104.     if (mask & SIGBREAKF_CTRL_C) {
  105.         notdone = 0;
  106.     }
  107.     if ((mask & (1 << tp->mp_SigBit)) && Scr && Ioip && CheckIO(&Iot)) {
  108.         WaitIO(&Iot);
  109.         Iot.tr_time.tv_secs  = RATE / 1000000;
  110.         Iot.tr_time.tv_micro = RATE % 1000000;
  111.         SendIO(&Iot);
  112.         screengraphics();
  113.     }
  114.     if (mask & (1 << ipport->mp_SigBit)) {
  115.         IORequest *ior;
  116.         while (ior = (IORequest *)GetMsg(ipport)) {
  117.         if (ior->io_Message.mn_Node.ln_Type == NT_REPLYMSG) {   /* my AddHand req */
  118.             notdone = 0;
  119.             continue;
  120.         }
  121.         switch(ior->io_Command) {
  122.         case 0x80:
  123.         case 0x81:
  124.             break;
  125.         case 0x82:
  126.             screenon();
  127.             break;
  128.         case 0x83:
  129.             screenoff();
  130.             break;
  131.         case 0x86:
  132.             notdone = 0;
  133.             break;
  134.         }
  135.         ReplyMsg(&ior->io_Message);
  136.         }
  137.     }
  138.     }
  139.     PutMsg(dmport, &RemReq.io_Message);
  140.     WaitMsg(&RemReq);
  141.     {
  142.     register IORequest *ior = NULL;
  143.     while (ior != &AddReq) {    /*  last msg will be AddReq */
  144.         WaitPort(ipport);
  145.         ior = (IORequest *)GetMsg(ipport);
  146.         if (ior->io_Message.mn_Node.ln_Type == NT_MESSAGE)
  147.         ReplyMsg(&ior->io_Message);
  148.     }
  149.     }
  150. fail:
  151.     if (Ioip) {
  152.     AbortIO(&Iot);
  153.     WaitIO(&Iot);
  154.     }
  155.     if (Iot.tr_node.io_Device)
  156.     CloseDevice(&Iot);
  157.     if (tp)
  158.     DeletePort(tp);
  159.     if (ipport)
  160.     DeletePort(ipport);
  161.     closelibs(-1);
  162. }
  163.  
  164. void
  165. screenoff()
  166. {
  167.     if (Scr)
  168.     ScreenToFront(Scr);
  169.     else if (Scr = OpenScreen(&Ns)) {
  170.     if (!Ioip) {
  171.         Iot.tr_time.tv_secs  = 3;
  172.         Iot.tr_time.tv_micro = 0;
  173.         SendIO(&Iot);
  174.         Ioip = 1;
  175.     }
  176.     }
  177. }
  178.  
  179. void
  180. screenon()
  181. {
  182.     if (Scr)
  183.     CloseScreen(Scr);
  184.     Scr = NULL;
  185.     if (Ioip) {
  186.     AbortIO(&Iot);
  187.     WaitIO(&Iot);
  188.     Ioip = 0;
  189.     }
  190. }
  191.  
  192. /*
  193.  *  Simple Stupid
  194.  *
  195.  *  Warning:  Note that no clipping is done when using the screen's
  196.  *          rastport, all rendering must be IN BOUNDS!
  197.  */
  198.  
  199. void
  200. screengraphics()
  201. {
  202.     static short oldx, oldy;
  203.     short x, y;
  204.  
  205.     x = (myrand() & 4095) % (Scr->Width - TextLength(&Scr->RastPort, TStr, TLen
  206.  - 8);
  207.     y = (myrand() & 4095) % (Scr->Height- Scr->RastPort.TxHeight - Scr->RastPor
  208. .TxBaseline - 8);
  209.     if (x < 0 || y < 0)
  210.     return;
  211.     y += Scr->RastPort.TxBaseline + 4;
  212.  
  213.     SetAPen(&Scr->RastPort, 0);
  214.     Move(&Scr->RastPort, oldx, oldy);
  215.     Text(&Scr->RastPort, TSpa, TLen);
  216.  
  217.     oldx = x;
  218.     oldy = y;
  219.     SetRGB4(&Scr->ViewPort, 1, myrand()&15, myrand()&15, myrand()&15);
  220.  
  221.     SetAPen(&Scr->RastPort, 1);
  222.     Move(&Scr->RastPort, oldx, oldy);
  223.     Text(&Scr->RastPort, TStr, TLen);
  224. }
  225.  
  226.  
  227. long
  228. myrand()
  229. {
  230.     static long rv = 34987;
  231.     rv = (rv * 13 + 1) ^ (rv >> 13);
  232.     return(rv);
  233. }
  234.  
  235.  
  236.  
  237.