home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 500-599 / ff599.lzh / InputView / Source.lha / IHandler.c < prev    next >
C/C++ Source or Header  |  1992-01-31  |  8KB  |  251 lines

  1. /*----------------------------------------------------------------------------
  2.    File   :    IHandler.c
  3.    Projekt:    --
  4.    Inhalt :    InitIHandler  (initialisiert Speicher & Devices)
  5.                CloseIHandler (macht alles rückgängig)
  6.                InputHandler (der eigentliche Handler)
  7.                LaunchIHandler (startet den da oben..)
  8.                CatchIHandler (.. und fängt ihn wieder ein)
  9.  
  10.    Version:    0.12
  11.    Datum  :    18.April 1991
  12.  
  13.    Autor  :    Uwe Röhm
  14.    Adresse:    Auber Str. 25,  W-6209 Hohenstein 4
  15.     (Semester) Wörthstr. 18    W-8390 Passau
  16.    Bemerkung:
  17.    Support-Routinen zur einfachen Programmierung eines eigenen InputHandlers.
  18. ----------------------------------------------------------------------------*/
  19.  
  20. #include <exec/Types.h>
  21. #include <exec/io.h>
  22. #include <exec/ports.h>
  23. #include <exec/interrupts.h>
  24. #include <devices/input.h>
  25. #include <devices/InputEvent.h>
  26. #include "IHandler.h"
  27.  
  28. #ifndef LATTICE
  29.    #include <functions.h>
  30. #else
  31.    #include <proto/exec.h>
  32. #endif
  33.  
  34. /*  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --
  35.    Funktion  :  InitIHandler
  36.    Parameter :  struct MsgPort* (Zeiger auf einen Port oder NULL, für neuen)
  37.                 int IENr (Anzahl der InputEvents, für die ich Platz hole)
  38.                 int Time (Zahl der Ticks zwischen den Signals vom Handler)
  39.    Rückgabe  :  struct IHandCom * (Zeiger auf fertige Struktur oder NULL)
  40.  
  41.    Aufruf von:  -?-
  42.    UnterFunks:  CloseIHandler (s.u.)
  43.    Autor     :  Floyd
  44.    Datum     :  14.03.1991
  45.    Bemerkung:
  46.    Die gemeinsame Datenstruktur des Handlers und des PRGs wird initialisiert.
  47. --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  */
  48. struct IHandCom *InitIHandler (struct MsgPort *PPtr, int IENr, int Time)
  49. {
  50.    struct IHandCom *ret = NULL;
  51.  
  52.    ret = (struct IHandCom *) AllocMem (sizeof (*ret), IHANDMEMOPT);
  53.    if (ret)
  54.    {
  55.       if (PPtr)
  56.          ret->Port = PPtr;
  57.       else
  58.          ret->Port = (struct MsgPort*) CreatePort (IHANDPORTNAME, 0);
  59.  
  60.       if (ret->Port)
  61.       {
  62.          ret->InpDev = (struct IOStdReq *) CreateExtIO (ret->Port, sizeof (struct IOStdReq));
  63.          if (ret->InpDev)
  64.          {
  65.             if( OpenDevice ("input.device", NULL,(struct IORequest *) ret->InpDev, NULL) )
  66.             {
  67.                DeleteExtIO ((struct IORequest *) ret->InpDev);
  68.                ret->InpDev = NULL;
  69.             }
  70.             else
  71.             {
  72.                ret->Inter = (struct Interrupt *) AllocMem (sizeof (*ret->Inter), IHANDMEMOPT);
  73.                ret->BufStart = (struct InputEvent*) AllocMem (sizeof (struct InputEvent) * IENr, IHANDMEMOPT);
  74.                if (ret->BufStart && ret->Inter)
  75.                {
  76.                   ret->BufEnde = ret->BufStart + IENr;
  77.                   ret->BufMarke= ret->BufStart;
  78.                   ret->BufWrite= ret->BufStart;
  79.                   ret->SigTask = ret->Port->mp_SigTask;
  80.                   ret->TimerMax= Time;
  81.                   ret->is_launched = FALSE;
  82.                   ret->SigBit  = AllocSignal (-1);
  83.                   if (ret->SigBit != -1)
  84.                       return ret;
  85.                }
  86.             }
  87.          }
  88.       }
  89.    }
  90.  
  91.    CloseIHandler (ret);
  92.    return NULL;
  93. }
  94.  
  95.  
  96. /*  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --
  97.    Funktion  :  CloseIHandler
  98.    Parameter :  struct IHandCom * (Zeiger auf freizugebende Struktur)
  99.    Rückgabe  :  --
  100.  
  101.    Aufruf von:  -?-
  102.    UnterFunks:  CatchIHandler (s.u.)
  103.    Autor     :  Floyd
  104.    Datum     :  14.03.1991
  105.    Bemerkung:
  106.    Die gemeinsame Datenstruktur des Handlers und des PRGs wird freigegeben.
  107. --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  */
  108. VOID CloseIHandler (struct IHandCom *ihc)
  109. {
  110.    if (ihc)
  111.    {
  112.       CatchIHandler (ihc);
  113.  
  114.       if (ihc->InpDev)
  115.       {  DeleteExtIO ((struct IORequest *) ihc->InpDev);
  116.          ihc->InpDev = NULL;
  117.       }
  118.  
  119.       if (ihc->Port)
  120.          DeletePort (ihc->Port);
  121.  
  122.       if (ihc->BufStart)
  123.       {  FreeMem (ihc->BufStart, (ihc->BufEnde - ihc->BufStart) * sizeof (struct InputEvent));
  124.          ihc->BufStart = NULL;
  125.       }
  126.  
  127.       if (ihc->Inter)
  128.       {  FreeMem (ihc->Inter, sizeof (struct Interrupt));
  129.          ihc->Inter = NULL;
  130.       }
  131.  
  132.       if (ihc->SigBit)
  133.          FreeSignal (ihc->SigBit);
  134.  
  135.       FreeMem (ihc, sizeof (*ihc));
  136.    }
  137. }
  138.  
  139.  
  140. /*  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --
  141.    Funktion  :  InputHandler
  142.    Parameter :  struct InputEvent *ie1 (Zeiger auf Events...)
  143.                 struct IHandCom *ihc (Zeiger auf eigene Datenstruktur...)
  144.    Rückgabe  :  struct InputEvent *(wie genommen, so zeronnen)
  145.  
  146.    Aufruf von:  InputDevice (im ROM !)
  147.    UnterFunks:  --
  148.    Autor     :  Uwe Röhm
  149.    Datum     :  18.04.1991
  150.    Bemerkung:
  151.    ein kleiner Beispiels-Inputhandler ...
  152. --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  */
  153. struct InputEvent* __regargs InputHandler(struct InputEvent *ie1,struct IHandCom *ihc)
  154. {
  155. struct InputEvent *iez;
  156.  
  157.    iez = ie1;
  158.    do
  159.    {
  160.       switch (iez->ie_Class)
  161.       {
  162.          case IECLASS_RAWKEY:
  163.          case IECLASS_RAWMOUSE:
  164.             if (ihc->does_control)
  165.             {
  166.                CopyMem (iez, ihc->BufMarke, sizeof (struct InputEvent));
  167.                if (iez->ie_Class == IECLASS_RAWKEY)
  168.                   ihc->BufMarke->ie_Class = IECLASS_GADGETDOWN;
  169.                else
  170.                   ihc->BufMarke->ie_Class = IECLASS_GADGETUP;
  171.                ihc->BufMarke->ie_NextEvent = NULL;
  172.                ihc->BufMarke++;
  173.                if (ihc->BufMarke == ihc->BufEnde)
  174.                   ihc->BufMarke = ihc->BufStart;
  175.             }
  176.             break;
  177.          case IECLASS_TIMER:
  178.             ihc->Timer++;
  179.             if (ihc->Timer > ihc->TimerMax)
  180.             {
  181.                ihc->Timer = 0;
  182.                Signal (ihc->SigTask, (1 << ihc->SigBit));
  183.             }
  184.             break;
  185.          default:
  186.             break;
  187.       }
  188.    }
  189.    while (iez = iez->ie_NextEvent);
  190.  
  191.    return ie1;
  192. }
  193.  
  194.  
  195. /*  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --
  196.    Funktion  :  LaunchIHandler
  197.    Parameter :  struct IHandCom *ihc (Zeiger auf gemeinsame Struktur)
  198.                 BYTE Priority (Priorität des InputHandlers)
  199.                 char *Handlername (eben dieser)
  200.    Rückgabe  :  BOOL (TRUE -> gestartet, sonst Fehler: FALSE)
  201.  
  202.    Aufruf von:  -?-
  203.    UnterFunks:  --
  204.    Autor     :  Floyd
  205.    Datum     :  14.03.91
  206.    Bemerkung:
  207.    startet obrigen InputHandler...
  208. --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  */
  209. BOOL LaunchIHandler (struct IHandCom *ihc, BYTE Priority, char *Handlername)
  210. {
  211.    if (ihc->is_launched)
  212.       return FALSE;
  213.    else
  214.    {  ihc->Inter->is_Code        = (VOID *) InputHandler;
  215.       ihc->Inter->is_Data        = (APTR) ihc;
  216.       ihc->Inter->is_Node.ln_Pri = Priority;
  217.       ihc->Inter->is_Node.ln_Name= Handlername;
  218.  
  219.       ihc->InpDev->io_Data    = (APTR) ihc->Inter;
  220.       ihc->InpDev->io_Command = IND_ADDHANDLER;
  221.       DoIO ((struct IORequest *) ihc->InpDev);
  222.       ihc->is_launched = TRUE;
  223.    }
  224.    return TRUE;
  225. }
  226.  
  227.  
  228. /*  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --
  229.    Funktion  :  CatchIHandler
  230.    Parameter :  struct IHandCOm *ihc (Zeiger auf .... <gähn>)
  231.    Rückgabe  :  --
  232.  
  233.    Aufruf von:  -?-
  234.    UnterFunks:  --
  235.    Autor     :  Floyd (ich)
  236.    Datum     :  14.03.1991
  237.    Bemerkung:
  238.    Holt den InputHandler wieder aus dem System...
  239. --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  */
  240. VOID CatchIHandler (struct IHandCom *ihc)
  241. {
  242.    if (ihc->is_launched)
  243.    {
  244.       ihc->InpDev->io_Data    = (APTR) ihc->Inter;
  245.       ihc->InpDev->io_Command = IND_REMHANDLER;
  246.       DoIO ((struct IORequest *) ihc->InpDev);
  247.       ihc->is_launched = FALSE;
  248.    }
  249. }
  250.  
  251.