home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / system / alock / source.lha / alock.c < prev    next >
C/C++ Source or Header  |  1993-09-02  |  10KB  |  444 lines

  1. /*
  2.  * alock.c
  3.  * based on several pieces of software (C) Commodore-Amiga Inc.
  4.  *
  5.  * written by Michael Kaiser
  6.  *
  7.  * This information is provided "as is"; no warranties are made.  All
  8.  * use is at your own risk. No liability or responsibility is assumed.
  9.  *
  10.  * Compiles without warnings under SAS/C 6.0 :-)
  11.  *
  12. */
  13.  
  14. #include <exec/types.h>
  15. #include <exec/memory.h>
  16. #include <exec/interrupts.h>
  17. #include <devices/input.h>
  18. #include <graphics/displayinfo.h>
  19. #include <intuition/intuition.h>
  20.  
  21. #include <clib/alib_protos.h>
  22. #include <clib/dos_protos.h>
  23. #include <clib/exec_protos.h>
  24. #include <clib/keymap_protos.h>
  25. #include <clib/graphics_protos.h>
  26. #include <clib/intuition_protos.h>
  27. #include <math.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31.  
  32. #include "alock.rev.h"
  33. #include "abrush.h"
  34. #include "alogo.h"
  35.  
  36. /* This is the function will grab all the inputs and the common pointer */
  37.  
  38. extern void Grab();
  39. struct InputEvent *eventbuf = 0;
  40.  
  41. /* SAS/C & stuff */
  42.  
  43. extern struct IntuitionBase *IntuitionBase;
  44.  
  45. /* Password stuff */
  46.  
  47. static char NameString[] = "AmigaLock";
  48. static char password[128] = "alock";
  49. static int  passlen = 5;
  50. static char passbuf[128] = "";
  51. static int  buflen = 0;
  52.  
  53. /* A collection of routines that might be processed while the screen is locked. */
  54.  
  55. /* ------------- This shows the standard (internal) picture ------------- */
  56.  
  57. void Lock_ShowScreen(void) 
  58.  
  59. {
  60.   struct Screen *S;
  61.   int    done;
  62.   char   buffer[32];
  63.   register int i;
  64.   int counter;
  65.   
  66.   if (IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",37L))
  67.     {
  68.       if (S = OpenScreenTags(0L,
  69.                  SA_Depth,4,
  70.                  SA_DisplayID,HIRESLACE_KEY|DEFAULT_MONITOR_ID,
  71.                  SA_Type,CUSTOMSCREEN,
  72.                  SA_ShowTitle,FALSE,
  73.                  SA_Quiet,TRUE,
  74.                  TAG_DONE))
  75.     {
  76.       
  77.       
  78.       for (i = 0; i < 16; i++)
  79.         {
  80.           SetRGB4(&S->ViewPort,(LONG)i,
  81.               (big_cmap[i] & 0x0F00) >> 8,
  82.               (big_cmap[i] & 0x00F0) >> 4,
  83.               (big_cmap[i] & 0x000F));
  84.         }
  85.       
  86.       DrawImage(&S->RastPort,&big_img,(S->Width-big_img.Width)/2,(S->Height-big_img.Height)/2);
  87.       
  88.       counter = 0;
  89.       done = 0;
  90.       while (!done)
  91.         {
  92.           if (eventbuf)
  93.         {
  94.           MapRawKey(eventbuf,buffer,31,0);
  95.           passbuf[buflen] = buffer[0]; buflen ++; passbuf[buflen] = 0;
  96.           if (strncmp(passbuf,password,buflen) == 0)
  97.             {
  98.               if (strncmp(passbuf,password,passlen) == 0)
  99.             done = 1;
  100.             }
  101.           else
  102.             {
  103.               passbuf[0] = buffer[0]; 
  104.               buflen = 1; 
  105.               passbuf[1] = 0;
  106.             }
  107.           eventbuf = 0;
  108.           Delay(3);
  109.           eventbuf = 0;
  110.         }
  111.           else
  112.         Delay(3);
  113.  
  114.           counter ++;
  115.           for (i = 8; i < 16; i++)
  116.         {
  117.           SetRGB4(&S->ViewPort,(LONG)i,
  118.               (big_cmap[(i+counter) & 0x0f] & 0x0F00) >> 8,
  119.               (big_cmap[(i+counter) & 0x0f] & 0x00F0) >> 4,
  120.               (big_cmap[(i+counter) & 0x0f] & 0x000F));
  121.         }
  122.           if (counter >= 16)
  123.         counter = 0;
  124.         }
  125.       CloseScreen(S);
  126.     }
  127.       CloseLibrary((struct Library *)IntuitionBase);
  128.     }
  129. }
  130.  
  131. /* -------------------------------- Game of life with C= logos ------------------------------- */
  132.  
  133. #define BOARD_X   12
  134. #define BOARD_Y   10
  135. #define SET_LIMIT 0.9
  136.  
  137. static int life_neighbours(int *board, int x, int y)
  138.  
  139. {
  140.   register int i,j,k;
  141.  
  142.   k = 0;
  143.   for (i = -1; i < 2; i++)
  144.     for (j = -1; j < 2; j++)
  145.       if ((x + i >= 0) && (x + i < BOARD_X) &&
  146.       (y + j >= 0) && (y + j < BOARD_Y))
  147.     k += board[x+i + BOARD_Y * (y+j)];
  148.  
  149.   return(k);
  150. }
  151.  
  152.  
  153. static void life_run(int *old_board, int *new_board)
  154.  
  155. {
  156.   register int i,j,k;
  157.  
  158.   for (i = 0; i < BOARD_X; i++)
  159.     for (j = 0; j < BOARD_Y; j++)
  160.       {
  161.     k = life_neighbours(old_board,i,j);
  162.     if (k <= 1)
  163.       new_board[i + BOARD_Y * j] = old_board[i + BOARD_Y * j];
  164.     else
  165.       if (k <= 2)
  166.         new_board[i + BOARD_Y * j] = 1;
  167.       else
  168.         new_board[i + BOARD_Y * j] = 0;
  169.       }
  170. }
  171.  
  172. static void life_display(struct Screen *screen, 
  173.              struct Image *pos_img, 
  174.              struct Image *neg_img, 
  175.              int *board)
  176.  
  177. {
  178.   int x,y;
  179.  
  180.   for (x = 0; x < BOARD_X; x++)
  181.     for (y = 0; y < BOARD_Y; y++)
  182.       if (board[x + BOARD_Y * y])
  183.     DrawImage(&screen->RastPort,pos_img,
  184.           x * pos_img->Width,
  185.           y * pos_img->Height);
  186.       else
  187.     DrawImage(&screen->RastPort,neg_img,
  188.           x * neg_img->Width,
  189.           y * neg_img->Height);
  190. }
  191.  
  192.  
  193. void Lock_ShowLife(void) 
  194.  
  195. {
  196.   struct Screen *S;
  197.   int    done;
  198.  
  199.   char   buffer[32];
  200.   register int i;
  201.   int counter;
  202.  
  203.   int old_board[BOARD_X * BOARD_Y];
  204.   int new_board[BOARD_X * BOARD_Y];
  205.  
  206.   counter = 0;
  207.   for (i = 0; i < BOARD_X * BOARD_Y; i++)
  208.     {
  209.       old_board[i] = 0;
  210.       if (drand48() > SET_LIMIT)
  211.     {
  212.       old_board[i] = 1;
  213.       counter ++;
  214.     }
  215.     }
  216.   
  217.   while (counter < (BOARD_X * BOARD_Y)/16)
  218.     {
  219.       for (i = 0; i < BOARD_X * BOARD_Y; i++)
  220.     {
  221.       if (old_board[i])
  222.         if (drand48() > SET_LIMIT)
  223.           {
  224.         old_board[i] = 1;
  225.         counter ++;
  226.           }
  227.     }
  228.     }
  229.   
  230.   if (IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",37L))
  231.     {
  232.       if (S = OpenScreenTags(0L,
  233.                  SA_Depth,2,
  234.                  SA_DisplayID,HIRESLACE_KEY|DEFAULT_MONITOR_ID,
  235.                  SA_Type,CUSTOMSCREEN,
  236.                  SA_ShowTitle,FALSE,
  237.                  SA_Quiet,TRUE,
  238.                  TAG_DONE))
  239.     {
  240.       
  241.       
  242.       for (i = 0; i < 4; i++)
  243.         {
  244.           SetRGB4(&S->ViewPort,(LONG)i,
  245.               (pos_cmap[i] & 0x0F00) >> 8,
  246.               (pos_cmap[i] & 0x00F0) >> 4,
  247.               (pos_cmap[i] & 0x000F));
  248.         }
  249.       
  250.       life_display(S,&pos_img,&neg_img,old_board);
  251.  
  252.       counter = 0;
  253.       done = 0;
  254.       while (!done)
  255.         {
  256.           if (eventbuf)
  257.         {
  258.           MapRawKey(eventbuf,buffer,31,0);
  259.           passbuf[buflen] = buffer[0]; buflen ++; passbuf[buflen] = 0;
  260.           if (strncmp(passbuf,password,buflen) == 0)
  261.             {
  262.               if (strncmp(passbuf,password,passlen) == 0)
  263.             done = 1;
  264.             }
  265.           else
  266.             {
  267.               passbuf[0] = buffer[0]; 
  268.               buflen = 1; 
  269.               passbuf[1] = 0;
  270.             }
  271.           eventbuf = 0;
  272.           Delay(3);
  273.           eventbuf = 0;
  274.         }
  275.           else
  276.         Delay(3);
  277.  
  278.           counter ++;
  279.           if ((counter % 20) == 0)
  280.         {
  281.           life_run(old_board,new_board);
  282.           for (i = 0; i < BOARD_X * BOARD_Y; i++)
  283.             old_board[i] = new_board[i];
  284.           life_display(S,&pos_img,&neg_img,old_board);
  285.           counter = 0;
  286.         }
  287.         }
  288.       CloseScreen(S);
  289.     }
  290.       CloseLibrary((struct Library *)IntuitionBase);
  291.     }
  292. }
  293.  
  294. /* ------------------------------- Load a picture & display it --------------------------------- */
  295.  
  296. void Lock_DoAction(char *actionname)
  297.  
  298. {
  299.   int    done;
  300.   char   buffer[32];
  301.  
  302.   done = system(actionname);
  303.  
  304.   while (!done)
  305.     {
  306.       if (eventbuf)
  307.     {
  308.       MapRawKey(eventbuf,buffer,31,0);
  309.       passbuf[buflen] = buffer[0]; buflen ++; passbuf[buflen] = 0;
  310.       if (strncmp(passbuf,password,buflen) == 0)
  311.         {
  312.           if (strncmp(passbuf,password,passlen) == 0)
  313.         done = 1;
  314.         }
  315.       else
  316.         {
  317.           passbuf[0] = buffer[0]; 
  318.           buflen = 1; 
  319.           passbuf[1] = 0;
  320.         }
  321.       eventbuf = 0;
  322.       Delay(3);
  323.       eventbuf = 0;
  324.     }
  325.       else
  326.     Delay(3);
  327.     }
  328. }
  329.  
  330. /* The main procedure */
  331.  
  332. int main(int argc, char **argv)
  333.  
  334. {
  335.   char            **ttypes;
  336.   char             *param;
  337.   char              actionname[512] = " ";
  338.   struct IOStdReq  *inputReqBlk;
  339.   struct MsgPort   *inputPort;
  340.   struct Interrupt *inputHandler;
  341.   int               result = 1;  /* bad result */
  342.   int               mode   = 0;  /* INTERNAL   */
  343.  
  344.   if (argc > 0)
  345.     {
  346.       printf("%s (%s) by Michael Kaiser.\n",VERS,DATE);
  347.       printf("Usage: %s PASSWORD=<password> ACTION=INTERNAL|LIFE|<command string>\n",argv[0]);
  348.     }
  349.  
  350.   ttypes = ArgArrayInit( argc, argv );
  351.   if (ttypes)
  352.     {
  353.       param = ArgString(ttypes,"PASSWORD","alock");
  354.       if (param)
  355.     strcpy(password,param);
  356.       
  357.       param = ArgString(ttypes,"ACTION","INTERNAL");
  358.       if (param)
  359.     {
  360.       if (strcmp(param,"INTERNAL") == 0)
  361.         mode = 0;
  362.       else
  363.         if (strcmp(param,"LIFE") == 0)
  364.           mode = 1;
  365.         else
  366.           {
  367.         strcpy(actionname,param);
  368.         mode = 2;
  369.           }
  370.     }
  371.     }
  372.  
  373.   ArgArrayDone();
  374.  
  375.   if (inputPort=CreatePort(NULL,NULL))
  376.     {
  377.       if (inputHandler=AllocMem(sizeof(struct Interrupt),
  378.                 MEMF_PUBLIC|MEMF_CLEAR))
  379.         {
  380.       if (inputReqBlk=(struct IOStdReq *)CreateExtIO(inputPort,
  381.                              sizeof(struct IOStdReq)))
  382.             {
  383.           if (!OpenDevice("input.device",NULL,
  384.                   (struct IORequest *)inputReqBlk,NULL))
  385.                 {
  386.           inputHandler->is_Code=Grab;
  387.           inputHandler->is_Data=NULL;
  388.           inputHandler->is_Node.ln_Pri=100;
  389.           inputHandler->is_Node.ln_Name=NameString;
  390.           inputReqBlk->io_Data=(APTR)inputHandler;
  391.           inputReqBlk->io_Command=IND_ADDHANDLER;
  392.           DoIO((struct IORequest *)inputReqBlk);
  393.           
  394.           result = 0;
  395.           
  396.           switch(mode) {
  397.           case 0:
  398.             Lock_ShowScreen();
  399.             break;
  400.  
  401.           case 1:
  402.             Lock_ShowLife();
  403.             break;
  404.  
  405.           case 2:
  406.             Lock_DoAction(actionname);
  407.             break;
  408.  
  409.           default:
  410.             break;
  411.           }
  412.  
  413.           inputReqBlk->io_Data=(APTR)inputHandler;
  414.           inputReqBlk->io_Command=IND_REMHANDLER;
  415.           DoIO((struct IORequest *)inputReqBlk);
  416.           
  417.           CloseDevice((struct IORequest *)inputReqBlk);
  418.           if (IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0L))
  419.             {
  420.               DisplayBeep(0L);
  421.               CloseLibrary((struct Library*)IntuitionBase);
  422.             }
  423.                 }
  424.           else
  425.                 printf("Error: Could not open input.device\n");
  426.           
  427.           DeleteExtIO((struct IORequest *)inputReqBlk);
  428.             }
  429.       else
  430.             printf("Error: Could not create I/O request\n");
  431.       
  432.       FreeMem(inputHandler,sizeof(struct Interrupt));
  433.         }
  434.       else
  435.         printf("Error: Could not allocate interrupt struct memory\n");
  436.       
  437.       DeletePort(inputPort);
  438.     }
  439.   else
  440.     printf("Error: Could not create message port\n");
  441.   
  442.   return(result);
  443. }
  444.