home *** CD-ROM | disk | FTP | other *** search
/ ST-Computer Leser-CD 2000 January / LCD_01_2000.iso / tools / clock237 / source / ehc_lib.c next >
Encoding:
C/C++ Source or Header  |  1998-03-19  |  3.1 KB  |  194 lines

  1. #include <stddef.h>
  2. #include <tos.h>
  3. #include "jclkcook.h"
  4.  
  5. #ifndef TRUE
  6. #define TRUE 1
  7. #define FALSE 0
  8. #endif
  9.  
  10. JCLKSTRUCT *clocky = NULL;
  11. char *hotkey = NULL;
  12. int ehc_id = 0;
  13.  
  14. int    cookie (long cookie, long *value);
  15.  
  16. /*
  17. ** Initialise hotkeys
  18. */
  19. int initHotkeys(void)
  20. {
  21.     int used, i;
  22.  
  23.     /* Required version of Clocky innstalled? */
  24.     if (cookie('JCLK', (void *)&clocky))
  25.     {
  26.         if (clocky->name != CLOCKY_IDENT || clocky->version < CLOCKY_VERSION)
  27.             return FALSE; /* Needs a newer version */
  28.     }
  29.     else
  30.         return FALSE;    /* Not installed at all */
  31.     
  32.     hotkey = (char *)(&clocky->actual_key);
  33.  
  34.     /* Get a EHC ID */
  35.     /* <Petr Stehlik> */
  36.     do {
  37.         ehc_id++;
  38.         used = FALSE;
  39.         for(i = 0; i < 128; i++)
  40.         {
  41.             if (clocky->ehc_table[i] == ehc_id)
  42.             {
  43.                 used = TRUE;    /* this id is already in use */
  44.                 break;            /* try higher mark */
  45.             }
  46.         }
  47.     } while(used && (ehc_id < 128));
  48.     /* </Petr Stehlik> */
  49.  
  50.     if (ehc_id > 127) /* Too many hotkey-clients */
  51.         return FALSE;
  52.  
  53.     /* OK, return the EHC ID */
  54.     return ehc_id;
  55. }
  56.  
  57.  
  58. /*
  59. ** Register a hotkey
  60. */
  61.  
  62. int registerHotkey(char key)
  63. {
  64.     /* Successfully initialised? */
  65.     if (!ehc_id)
  66.         return FALSE;
  67.  
  68.     /* Free key? */
  69.     if (clocky->ehc_table[key] > 0)
  70.         return FALSE;
  71.         
  72.     /* Register key */
  73.     clocky->ehc_table[key] = ehc_id;
  74.     return ehc_id;
  75. }
  76.  
  77.  
  78. /*
  79. ** Remove registered hotkeys
  80. **
  81. ** key == -1 -> Remove all registered hotkeys
  82. ** otherwise remove 'key'.
  83. */
  84.  
  85. void removeHotkeys(int key)
  86. {
  87.     int teller;
  88.  
  89.     if (ehc_id != 0)
  90.     {
  91.         if (key == -1) /* Remove all registered keys */
  92.         {
  93.             for (teller = 0; teller < 128; teller++)
  94.                 if (clocky->ehc_table[teller] == ehc_id)
  95.                     clocky->ehc_table[teller] = 0;
  96.         }
  97.         else    /* Remove 'key' */
  98.         {
  99.             if (clocky->ehc_table[key] == ehc_id)
  100.                 clocky->ehc_table[key] = 0;
  101.         }
  102.     }
  103. }
  104.  
  105.  
  106. /*
  107. ** Check hotkeys
  108. */
  109.  
  110. char checkHotkeys(void)
  111. {
  112.     char retValue;
  113.  
  114.     /* Return ASAP if no hotkey is pressed */
  115.     if (*hotkey == 0)
  116.         return 0;
  117.  
  118.     /* Hotkey pressed, but is it mine? */
  119.     if (clocky->ehc_table[*hotkey] == ehc_id)
  120.     {
  121.         retValue = *hotkey;
  122.         *hotkey = 0;
  123.         return retValue; /* Return hotkey */
  124.     }
  125.  
  126.     /* Nope */
  127.     return 0;
  128. }
  129.  
  130.  
  131. /*
  132. ** Scancode -> ASCII
  133. */
  134.  
  135. char scan2ascii(char scan)
  136. {
  137.     struct keytab {
  138.         unsigned char *unshift;
  139.         unsigned char *shift;
  140.         unsigned char *capslock;
  141.         } *ktab;
  142.  
  143.     ktab = (struct keytab *)Keytbl((char *)-1,(char *)-1,(char *)-1);
  144.     return ktab->unshift[scan];
  145. }
  146.  
  147.  
  148. /*
  149. ** ASCII -> Scancode
  150. */
  151.  
  152. char ascii2scan(char ascii)
  153. {
  154.     int teller;
  155.  
  156.     /* Test all scancodes until a match is found */
  157.     for (teller = 0; teller < 128; teller++)
  158.         if (scan2ascii(teller) == ascii)
  159.             return teller;
  160.  
  161.     /* Hmmm... It doesn't have a key */            
  162.     return 0;
  163. }
  164.  
  165.  
  166. /*
  167. ** Get the value of a cookie
  168. ** Stolen from E-Gem 2.10
  169. */
  170.  
  171. int    cookie (long cookie, long *value)
  172. {
  173.     register long *cookiejar, old_stack;
  174.     
  175.     old_stack = Super (NULL);
  176.     cookiejar = *((long **) 0x5a0l);
  177.     Super ((void *) old_stack);
  178.     
  179.     if (cookiejar)
  180.     {
  181.         while (*cookiejar)
  182.         {
  183.             if (*cookiejar==cookie)
  184.             {
  185.                 if (value)
  186.                     *value = *++cookiejar;
  187.                 return(TRUE);
  188.             }
  189.             cookiejar += 2;
  190.         }
  191.     }
  192.     return(FALSE);
  193. }
  194.