home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / dev / rkrm.lha / RKRM / Keyboard / Key_Reset.c next >
Encoding:
C/C++ Source or Header  |  1992-09-03  |  6.2 KB  |  184 lines

  1. /*
  2.  * Copyright (c) 1992 Commodore-Amiga, Inc.
  3.  * 
  4.  * This example is provided in electronic form by Commodore-Amiga, Inc. for 
  5.  * use with the "Amiga ROM Kernel Reference Manual: Devices", 3rd Edition, 
  6.  * published by Addison-Wesley (ISBN 0-201-56775-X).
  7.  * 
  8.  * The "Amiga ROM Kernel Reference Manual: Devices" contains additional 
  9.  * information on the correct usage of the techniques and operating system 
  10.  * functions presented in these examples.  The source and executable code 
  11.  * of these examples may only be distributed in free electronic form, via 
  12.  * bulletin board or as part of a fully non-commercial and freely 
  13.  * redistributable diskette.  Both the source and executable code (including 
  14.  * comments) must be included, without modification, in any copy.  This 
  15.  * example may not be published in printed form or distributed with any
  16.  * commercial product.  However, the programming techniques and support
  17.  * routines set forth in these examples may be used in the development
  18.  * of original executable software products for Commodore Amiga computers.
  19.  * 
  20.  * All other rights reserved.
  21.  * 
  22.  * This example is provided "as-is" and is subject to change; no
  23.  * warranties are made.  All use is at your own risk. No liability or
  24.  * responsibility is assumed.
  25.  *
  26.  *****************************************************************************
  27.  *
  28.  * Key_Reset.c
  29.  *
  30.  * This is in two parts...
  31.  *
  32.  * Compile this C code with SAS C 5.10:
  33.  *    lc -b1 -cfistq -v -y Key_Reset
  34.  *
  35.  * Assemble the ASM code with Adapt
  36.  *  HX68 KeyHandler.a to KeyHandler.o
  37.  *
  38.  * Link with:
  39.  *      Blink FROM LIB:c.o+Key_Reset.o+keyhandler.o TO Key_Reset LIB LIB:lc.lib LIB:amiga.lib
  40.  */
  41.  
  42. /*
  43.  * Keyboard device reset handler example...
  44.  */
  45.  
  46. #include <exec/types.h>
  47. #include <exec/io.h>
  48. #include <exec/ports.h>
  49. #include <exec/memory.h>
  50. #include <devices/keyboard.h>
  51. #include <intuition/intuition.h>
  52. #include <exec/interrupts.h>
  53.  
  54. #include <clib/exec_protos.h>
  55. #include <clib/alib_protos.h>
  56. #include <clib/intuition_protos.h>
  57. #include <clib/dos_protos.h>
  58.  
  59.  
  60. #include <stdio.h>
  61.  
  62. #ifdef LATTICE
  63. int CXBRK(void) { return(0); }     /* Disable SAS CTRL/C handling */
  64. int chkabort(void) { return(0); }  /* really */
  65. void main();
  66. #endif
  67.  
  68. extern VOID ResetHandler();
  69.  
  70. UBYTE NameString[]="Reset Handler Test";
  71.  
  72. struct NewWindow mywin={0,0,178,10,0,1,CLOSEWINDOW,
  73.                         WINDOWDRAG|WINDOWCLOSE|SIMPLE_REFRESH|NOCAREREFRESH,
  74.                         NULL,NULL,NameString,NULL,NULL,0,0,0,0,WBENCHSCREEN};
  75.  
  76. extern struct IntuitionBase *IntuitionBase;
  77.  
  78. struct MyData
  79.     {
  80.     struct Task  *MyTask;
  81.            ULONG MySignal;
  82.     };
  83.  
  84. /*
  85.  * This routine opens a window and waits for the one event that
  86.  * can happen (CLOSEWINDOW)
  87.  */
  88. short WaitForUser(ULONG MySignal)
  89. {
  90. struct Window  *win;
  91.        short   ret=0;
  92.  
  93.     if (IntuitionBase=(struct IntuitionBase *)
  94.                                     OpenLibrary("intuition.library",0L))
  95.     {
  96.         if (win=(struct Window *)OpenWindow(&mywin))
  97.         {
  98.             ret=(MySignal==Wait(MySignal | (1L << win->UserPort->mp_SigBit)));
  99.             CloseWindow(win);
  100.         }
  101.         CloseLibrary((struct Library *)IntuitionBase);
  102.     }
  103.     return(ret);
  104. }
  105.  
  106. VOID main(int argc, char *argv[])
  107. {
  108. struct IOStdReq  *KeyIO;
  109. struct MsgPort   *KeyMP;
  110. struct Interrupt *keyHandler;
  111. struct MyData    MyDataStuff;
  112.        ULONG     MySignal;
  113.  
  114.     if ((MySignal=AllocSignal(-1L))!=-1)
  115.     {
  116.         MyDataStuff.MyTask=FindTask(NULL);
  117.         MyDataStuff.MySignal=1L << MySignal;
  118.         if (KeyMP=CreatePort(NULL,NULL))
  119.         {
  120.             if (keyHandler=AllocMem(sizeof(struct Interrupt),
  121.                                        MEMF_PUBLIC|MEMF_CLEAR))
  122.             {
  123.                 if (KeyIO=(struct IOStdReq *)CreateExtIO(KeyMP,
  124.                                                      sizeof(struct IOStdReq)))
  125.                 {
  126.                     if (!OpenDevice("keyboard.device",NULL,
  127.                                      (struct IORequest *)KeyIO,NULL))
  128.                     {
  129.                         keyHandler->is_Code=ResetHandler;
  130.                         keyHandler->is_Data=(APTR)&MyDataStuff;
  131.  
  132.                         /*
  133.                          * Note that only software interrupt priorities
  134.                          * can be used for the .ln_Pri on the reset
  135.                          * handler...
  136.                          */
  137.                         keyHandler->is_Node.ln_Pri=16;
  138.  
  139.                         keyHandler->is_Node.ln_Name=NameString;
  140.                         KeyIO->io_Data=(APTR)keyHandler;
  141.                         KeyIO->io_Command=KBD_ADDRESETHANDLER;
  142.                         DoIO((struct IORequest *)KeyIO);
  143.  
  144.                         if (WaitForUser(MyDataStuff.MySignal))
  145.                         {
  146.                             if (argc) /* Check for CLI */
  147.                             {
  148.                                 printf("System going down\n");
  149.                                 printf("Cleaning up...\n");
  150.                                 /* Show a delay, like cleanup... */
  151.                                 Delay(20);
  152.                                 printf("*Poof*\n");
  153.                             }
  154.  
  155.                             /* We are done with our cleanup */
  156.                             KeyIO->io_Data=(APTR)keyHandler;
  157.                             KeyIO->io_Command=KBD_RESETHANDLERDONE;
  158.                             DoIO((struct IORequest *)KeyIO);
  159.                             /*
  160.                              * Note that since the above call
  161.                              * tells the system it is safe to reboot
  162.                              * and will cause the reboot if this
  163.                              * task was the last to say so, the call
  164.                              * never really returns...  The system
  165.                              * just reboots...
  166.                              */
  167.                         }
  168.  
  169.                         KeyIO->io_Data=(APTR)keyHandler;
  170.                         KeyIO->io_Command=KBD_REMRESETHANDLER;
  171.                         DoIO((struct IORequest *)KeyIO);
  172.  
  173.                         CloseDevice((struct IORequest *)KeyIO);
  174.                     }
  175.                     DeleteExtIO((struct IORequest *)KeyIO);
  176.                 }
  177.                 FreeMem(keyHandler,sizeof(struct Interrupt));
  178.             }
  179.             DeletePort(KeyMP);
  180.         }
  181.         FreeSignal(MySignal);
  182.     }
  183. }
  184.