home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377b.lha / devices / keyboard / keyreset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-02-03  |  5.8 KB  |  168 lines

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