home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 138.lha / Blanker / blanker2.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  9KB  |  204 lines

  1. From joe@vixen.UUCP Fri Feb 12 23:14:23 1988
  2. Posted-Date: Fri, 12 Feb 88 21:53:03 CST
  3. Received-Date: Fri, 12 Feb 88 23:14:16 CST
  4. Received: by sally.utexas.edu (5.54/5.51)
  5.         id AA24065; Fri, 12 Feb 88 23:14:16 CST
  6. Received: by vixen.uucp (3.2/SMI-3.2)
  7.         id AA00152; Fri, 12 Feb 88 21:53:03 CST
  8. Date: Fri, 12 Feb 88 21:53:03 CST
  9. From: joe@vixen.uucp (Joe Hitchens)
  10. Message-Id: <8802130353.AA00152@vixen.uucp>
  11. To: joe@sally.UUCP
  12. Subject: blanker2.c
  13. Status: R
  14. /* 
  15.         Blanker2 -- by Joe Hitchens
  16.         v1.27.88
  17.         A screen blanking program that turns the screen black after 90 seconds
  18.         of keyboard and mouse inactivity.
  19.         This program is PUBLIC DOMAIN.
  20.         Do me a favor though, and leave my name on it.
  21.         Usage: blanker2 [n]
  22.                 n = number of seconds of inactivity after which blanking occurs.                if n is not given, a default of 90 is used.
  23.                 NOTE: blanker2 need not be 'run'.  It will install an event
  24.                       handler and leave it behind when exiting.
  25.                       Also, the CLI window it may have been run from can be 
  26.                       closed after installation.
  27.         Exit codes
  28.         0       Normal exit, everything went fine.
  29.         1       CreatePort() failed.
  30.         2       CreateStdIO() failed.
  31.         3       OpenDevice() on "input.device" failed.
  32.         4       Memory Allocation for Interrupt structure failed.
  33.         5       Memory Allocation for Blanker structure failed.
  34.         6       Memory Allocation for event handler code space failed.
  35.         Please report any bugs you may find to me, so that I can try to
  36.         fix them.
  37.         j.h.                                      joe@sally.utexas.edu  
  38. */
  39. #include <stdio.h>
  40. #include <exec/types.h>
  41. #include <exec/ports.h>
  42. #include <exec/memory.h>
  43. #include <exec/io.h>
  44. #include <exec/interrupts.h>
  45. #include <devices/input.h>
  46. #include <exec/devices.h>
  47. #include <devices/inputevent.h>
  48. #include <graphics/gfxmacros.h>
  49. #include <hardware/custom.h>
  50. extern struct MsgPort   *CreatePort();
  51. extern struct IOStdReq  *CreateStdIO();
  52. extern char                             *AllocMem();
  53. extern int                              HandlerInterface();
  54. #define DEFAULT_BLANKPOINT      900             /* This is 1/10's of seconds */
  55. struct blanker {
  56.         long    Class;
  57.         long    Ticks;
  58.         long    BlankPoint;
  59.         long    Dark;
  60.         char    Name[8];
  61.         };
  62. /*      ----------------------------------------
  63.         Actual entry point for event handler 
  64.         ---------------------------------------- */
  65. #asm
  66.         public  _HandlerInterface
  67. _HandlerInterface:
  68.         movem.l a0/a1,-(a7)     ;
  69.         bsr     _myhandler      ;go to the C language routine we provided
  70.         addq.l  #8,a7           ;
  71.         rts
  72. #endasm
  73. struct InputEvent *
  74. myhandler(ev, mydata)
  75.         register struct InputEvent      *ev;
  76.         register struct blanker         *mydata;
  77.         {
  78.         mydata->Class = ev->ie_Class;
  79.         if (mydata->Class == IECLASS_TIMER)
  80.                 {
  81.                         mydata->Ticks++;
  82.                         if (mydata->Ticks >= mydata->BlankPoint)
  83.                                 {
  84.                                 custom.dmacon = BITCLR | DMAF_RASTER | DMAF_COPPER;
  85.                                 custom.color[0] = (UWORD) 0;    /* turn out the lights !  */
  86.                                 mydata->Ticks = 0;                              /* reset counter                  */
  87.                                 mydata->Dark = 1;                               /* yes the lights are out */
  88.                                 }
  89.                 return(ev);
  90.                 }
  91.         if (mydata->Class == IECLASS_RAWMOUSE)
  92.                 {
  93.                 mydata->Ticks = 0;
  94.                 if (mydata->Dark)
  95.                         {
  96.                         custom.dmacon = BITSET | DMAF_RASTER | DMAF_COPPER; /* lightson */
  97.                         mydata->Dark = 0;
  98.                         }
  99.                 return(ev);
  100.                 }
  101.         if (mydata->Class == IECLASS_RAWKEY)
  102.                 {
  103.                 mydata->Ticks = 0;
  104.                 if (mydata->Dark)
  105.                         {
  106.                         custom.dmacon = BITSET | DMAF_RASTER | DMAF_COPPER; /* lightson */
  107.                         mydata->Dark = 0;
  108.                         }
  109.                 return(ev);
  110.                 }
  111.         return(ev);
  112.         }
  113. /*      ----------------------------------------
  114.         End of event handler code
  115.         ---------------------------------------- */
  116. main(argc, argv)
  117.         int             argc;
  118.         char    **argv;
  119.         {
  120.         struct MsgPort          *inputDevPort;          /* for opening input.device */
  121.         struct IOStdReq         *inputRequestBlock;     /* for opening input.device */
  122.         struct Interrupt        *handlerStuff;          /* for adding event handler */
  123.         long                            n;                                      /* for calc'ing handler size */
  124.         long                            s;                                      /* ticks till blanking */
  125.         struct blanker          *p;                                     /* ptr to handler's variables */
  126.         char                            *code;                          /* prt to handler's entry point */
  127.         if (argc > 1)
  128.                 {
  129.                 s = 10 * atoi(argv[1]);
  130.                 if (s < 10)
  131.                         {
  132.                         s = 10;
  133.                         }
  134.                 }
  135.         else
  136.                 {
  137.                 s = DEFAULT_BLANKPOINT;
  138.                 }
  139.         inputDevPort = CreatePort("Blanker-Loader", 0);
  140.         if (inputDevPort == NULL) 
  141.                 exit (1);
  142.         inputRequestBlock = CreateStdIO(inputDevPort);
  143.         if (inputRequestBlock == 0)
  144.                 {
  145.                 DeletePort(inputDevPort);
  146.                 exit (2);
  147.                 }
  148.         if (OpenDevice("input.device", 0, inputRequestBlock, 0))
  149.                 {
  150.                 DeleteStdIO(inputRequestBlock);
  151.                 DeletePort(inputDevPort);
  152.                 exit (3);
  153.                 }
  154.         handlerStuff = (struct Interrupt *)
  155.                                                         AllocMem (sizeof (struct Interrupt), MEMF_CLEAR);
  156.         if (handlerStuff == (struct Interrupt *) 0)
  157.                 {
  158.                 DeleteStdIO(inputRequestBlock);
  159.                 DeletePort(inputDevPort);
  160.                 exit (4);
  161.                 }
  162.         /* alloc space for the private variable space used by event handler */
  163.         p = (struct blanker *)AllocMem (sizeof (struct blanker), MEMF_CLEAR); 
  164.         if (p == 0)
  165.                 {
  166.                 DeleteStdIO(inputRequestBlock);
  167.                 DeletePort(inputDevPort);
  168.                 FreeMem (handlerStuff, sizeof (struct Interrupt));
  169.                 exit (5);
  170.                 }
  171.         p->Class = 0;
  172.         p->Ticks = 0;
  173.         p->BlankPoint = s;
  174.         p->Dark = 0;
  175.         strncpy (p->Name, "Blanker", 7);
  176.         n = (char *)main - (char *)HandlerInterface; /* size of handler code */
  177.         code = AllocMem (n, MEMF_CLEAR); 
  178.         if (code == 0)
  179.                 {
  180.                 DeleteStdIO(inputRequestBlock);
  181.                 DeletePort(inputDevPort);
  182.                 FreeMem (handlerStuff, sizeof (struct Interrupt));
  183.                 FreeMem (p, sizeof (struct blanker));
  184.                 exit (6);
  185.                 }
  186.         CopyMem (HandlerInterface, code, n); /* move handler code to safe spot */
  187.         handlerStuff->is_Data = (APTR) p;                /* ptr to our variable space */
  188.         handlerStuff->is_Code = (VOID (*)())code; /* ptr to event handler code */
  189.         handlerStuff->is_Node.ln_Pri = 51;      /* priority just above Intuition */
  190.         handlerStuff->is_Node.ln_Name = p->Name;
  191.         inputRequestBlock->io_Command = IND_ADDHANDLER;
  192.         inputRequestBlock->io_Data = (APTR) handlerStuff;
  193.         DoIO (inputRequestBlock);                       /* install our event handler */ 
  194.         CloseDevice (inputRequestBlock);        /* close device and leave event */
  195.         DeleteStdIO (inputRequestBlock);        /* handler in memory                    */
  196.         DeletePort (inputDevPort);
  197.         if (argc > 0)
  198.                 {
  199.                 printf(" -- Blanker2 -- by Joe Hitchens -- V1.27.88 --\n");
  200.                 printf("Screen Blanks after %d sec's of mouse/key inactivity.\n",
  201.                                                                                                         s / 10);
  202.                 }
  203.         }
  204.