home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 1.2 / amidev_cd_12.iso / reference_library / devices / dev_examples / key_reset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  6.8 KB  |  230 lines

  1. /*
  2.  * Key_Reset.c
  3.  *
  4.  * This is in two parts...
  5.  *
  6.  * Compile this C code with SAS C 5.10:
  7.  *      lc -b1 -cfistq -v -y Key_Reset
  8.  *
  9.  * Assemble the ASM code with Adapt
  10.  *  HX68 KeyHandler.a to KeyHandler.o
  11.  *
  12.  * Link with:
  13.  * Blink FROM LIB:c.o+Key_Reset.o+KeyHandler.o TO
  14.  *        Key_Reset LIB LIB:lc.lib LIB:amiga.lib
  15.  */
  16.  
  17. /*
  18.  * Keyboard device reset handler example...
  19.  */
  20. #include <exec/types.h>
  21. #include <exec/io.h>
  22. #include <exec/ports.h>
  23. #include <exec/memory.h>
  24. #include <devices/keyboard.h>
  25. #include <intuition/intuition.h>
  26. #include <exec/interrupts.h>
  27.  
  28. #include <clib/exec_protos.h>
  29. #include <clib/alib_protos.h>
  30. #include <clib/intuition_protos.h>
  31. #include <clib/dos_protos.h>
  32.  
  33. #include <stdio.h>
  34.  
  35. #ifdef LATTICE
  36. int CXBRK(void) { return(0); }     /* Disable SAS CTRL/C handling */
  37. int chkabort(void) { return(0); }  /* really */
  38. void main();
  39. #endif
  40.  
  41. extern VOID ResetHandler();
  42.  
  43. UBYTE NameString[]="Reset Handler Test";
  44.  
  45. struct NewWindow mywin={0,0,178,10,0,1,CLOSEWINDOW,
  46.                       WINDOWDRAG|WINDOWCLOSE|SIMPLE_REFRESH|NOCAREREFRESH
  47.                       NULL,NULL,NameString,NULL,NULL,0,0,0,0,WBENCHSCREEN};
  48.  
  49. extern struct IntuitionBase *IntuitionBase;
  50.  
  51. struct MyData
  52.     {
  53.     struct Task  *MyTask;
  54.            ULONG MySignal;
  55.     };
  56.  
  57. /*
  58.  * This routine opens a window and waits for the one event that
  59.  * can happen (CLOSEWINDOW)
  60.  */
  61. short WaitForUser(ULONG MySignal)
  62. {
  63. struct Window  *win;
  64.        short   ret=0;
  65.  
  66. if (IntuitionBase=(struct IntuitionBase *)
  67.                    OpenLibrary("intuition.library",0L))
  68.     {
  69.     if (win=(struct Window *)OpenWindow(&mywin))
  70.         {
  71.         ret=(MySignal==Wait(MySignal | (1L << win->UserPort->mp_SigBit)));
  72.         CloseWindow(win);
  73.         }
  74.     else
  75.         printf("Error: Could not open window\n");
  76.     CloseLibrary((struct Library *)IntuitionBase);
  77.     }
  78. else
  79.     printf("Error: Could not open intution.library\n");
  80. return(ret);
  81. }
  82.  
  83. VOID main(int argc, char *argv[])
  84. {
  85. struct IOStdReq  *KeyIO;
  86. struct MsgPort   *KeyMP;
  87. struct Interrupt *keyHandler;
  88. struct MyData    MyDataStuff;
  89.        ULONG     MySignal;
  90.  
  91. if ((MySignal=AllocSignal(-1L))!=-1)
  92.     {
  93.     MyDataStuff.MyTask=FindTask(NULL);
  94.     MyDataStuff.MySignal=1L << MySignal;
  95.  
  96.     if (KeyMP=CreatePort(NULL,NULL))
  97.         {
  98.         if (keyHandler =
  99.                 AllocMem(sizeof(struct Interrupt),MEMF_PUBLIC|MEMF_CLEAR))
  100.             {
  101.             if (KeyIO=(struct IOStdReq *)
  102.                       CreateExtIO(KeyMP,sizeof(struct IOStdReq)))
  103.                 {
  104.                 if (!OpenDevice("keyboard.device",NULL,(struct IORequest *)
  105.                                                         KeyIO,NULL))
  106.                     {
  107.                     keyHandler->is_Code=ResetHandler;
  108.                     keyHandler->is_Data=(APTR)&MyDataStuff;
  109.  
  110.                     /*
  111.                      * Note that only software interrupt priorities
  112.                      * can be used for the .ln_Pri on the reset
  113.                      * handler...
  114.                      */
  115.                     keyHandler->is_Node.ln_Pri=16;
  116.  
  117.                     keyHandler->is_Node.ln_Name=NameString;
  118.                     KeyIO->io_Data=(APTR)keyHandler;
  119.                     KeyIO->io_Command=KBD_ADDRESETHANDLER;
  120.                     DoIO((struct IORequest *)KeyIO);
  121.  
  122.                     if (WaitForUser(MyDataStuff.MySignal))
  123.                         {
  124.                         if (argc) /* Check for CLI */
  125.                             {
  126.                             printf("System going down\n");
  127.                             printf("Cleaning up...\n");
  128.                             /* Show a delay, like cleanup... */
  129.                             Delay(20);
  130.                             printf("*Poof*\n");
  131.                             }
  132.                         /* We are done with our cleanup */
  133.  
  134.                         KeyIO->io_Data=(APTR)keyHandler;
  135.                         KeyIO->io_Command=KBD_RESETHANDLERDONE;
  136.                         DoIO((struct IORequest *)KeyIO);
  137.                         /*
  138.                          * Note that since the above call
  139.                          * tells the system it is safe to reboot
  140.                          * and will cause the reboot if this
  141.                          * task was the last to say so, the call
  142.                          * never really returns...  The system
  143.                          * just reboots...
  144.                          */
  145.                         }
  146.  
  147.                     KeyIO->io_Data=(APTR)keyHandler;
  148.                     KeyIO->io_Command=KBD_REMRESETHANDLER;
  149.                     DoIO((struct IORequest *)KeyIO);
  150.  
  151.                     CloseDevice((struct IORequest *)KeyIO);
  152.                     }
  153.                 else
  154.                     printf("Error: Could not open keyboard.device\n");
  155.  
  156.                 DeleteExtIO((struct IORequest *)KeyIO);
  157.                 }
  158.             else
  159.                 printf("Error: Could not create I/O request\n");
  160.  
  161.             FreeMem(keyHandler,sizeof(struct Interrupt));
  162.             }
  163.         else
  164.             printf("Error: Could not allocate memory for interrupt\n");
  165.  
  166.         DeletePort(KeyMP);
  167.         }
  168.     else
  169.         printf("Error: Could not create message port\n");
  170.  
  171.     FreeSignal(MySignal);
  172.     }
  173. else
  174.     printf("Error: Could not allocate signal\n");
  175. }
  176. \kern -20pt
  177. ************************************************************************
  178. *       KeyHandler.a
  179. *
  180. * Keyboard reset handler that signals the task in the structure...
  181. *
  182. * See Key_Reset.c for details on how to compile/assemble/link...
  183. *
  184. ************************************************************************
  185. * Required includes...
  186. *
  187.         INCDIR  "include:"
  188.         INCLUDE "exec/types.i"
  189.         INCLUDE "exec/io.i"
  190.         INCLUDE "devices/keyboard.i"
  191. *
  192.         xref    _AbsExecBase    ; We get this from outside...
  193.         xref    _LVOSignal      ; We get this from outside...
  194. *
  195. ************************************************************************
  196. * Make the entry point external...
  197. *
  198.         xdef    _ResetHandler
  199. *
  200. ************************************************************************
  201. *
  202. * This is the input handler
  203. * The is_Data field is passed to you in a1.
  204. *
  205. * This is the structure that is passed in A1 in this example...
  206. *
  207.         STRUCTURE       MyData,0
  208.         APTR            MyTask
  209.         ULONG           MySignal
  210. *
  211. ************************************************************************
  212. * The handler gets called here...
  213. *
  214. _ResetHandler:  move.l  MySignal(a1),d0 ; Get signal to send
  215.                 move.l  MyTask(a1),a1           ; Get task
  216. *
  217. * Now signal the task...
  218. *
  219.                 move.l  a6,-(sp)        ; Save the stack...
  220.                 move.l  _AbsExecBase,a6 ; Get ExecBase
  221.                 jsr     _LVOSignal(a6)  ; Send the signal
  222.                 move.l  (sp)+,a6        ; Restore A6
  223. *
  224. * Return to let other handlers execute.
  225. *
  226.                 rts                     ; return from handler...
  227. *
  228.                 END
  229. ************************************************************************
  230.