diff options
| author | Tomas Chvatal (scarabeus) <tomas.chvatal@gmail.com> | 2008-10-22 22:50:54 +0200 |
|---|---|---|
| committer | Tomas Chvatal (scarabeus) <tomas.chvatal@gmail.com> | 2008-10-22 22:50:54 +0200 |
| commit | e12753d2ec9db83ec08bb9db63902a82fb245cdd (patch) | |
| tree | 6e4a63b790d6335f969f88b797a927e0fb25f37a /src/client/hotKey.c~ | |
| parent | 0b4d9821c7333b8c4bdf7e0eeb750aef502a1137 (diff) | |
X
Diffstat (limited to 'src/client/hotKey.c~')
| -rw-r--r-- | src/client/hotKey.c~ | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/src/client/hotKey.c~ b/src/client/hotKey.c~ new file mode 100644 index 0000000..43a13ca --- /dev/null +++ b/src/client/hotKey.c~ @@ -0,0 +1,158 @@ + +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> + +#include "main.h" +#include "list.h" +#include "myTimer.h" + +#include "interface.h" +#include "hotKey.h" + +typedef struct hotKey_struct +{ + SDLKey key; + bool_t active; + void (*handler)(); +} hotKey_t; + +static list_t *listHotKey; +static my_time_t lastActive; + +static hotKey_t* newHotKey(SDLKey key, void (*handler)()) +{ + hotKey_t *new; + + new = malloc( sizeof(hotKey_t) ); + new->key = key; + new->active = TRUE; + new->handler = handler; + + return new; +} + +static void destroyHotKey(hotKey_t *hotkey) +{ + free(hotkey); +} + +static hotKey_t* findHotkey(SDLKey key) +{ + int i; + + for( i = 0 ; i < listHotKey->count ; i++ ) + { + hotKey_t *this; + + this = (hotKey_t *)listHotKey->list[i]; + + if( this->key == key ) + { + return this; + } + } + + return NULL; +} + +void initHotKey() +{ + listHotKey = newList(); + lastActive = getMyTime(); +} + +void registerHotKey(SDLKey key, void (*handler)()) +{ + hotKey_t *hotkey; + + if( findHotkey(key) != NULL ) + { + assert( ! "Conflict with register key"); + } + + hotkey = newHotKey(key, handler); + addList(listHotKey, hotkey); +} + +void unregisterHotKey(SDLKey key) +{ + hotKey_t *hotkey; + int index; + + hotkey = findHotkey(key); + + if( hotkey == NULL ) + { + assert( ! "This hotkey not register"); + } + + index = searchListItem(listHotKey, hotkey); + assert( index != -1 ); + delListItem(listHotKey, index, destroyHotKey); +} + +void enableHotKey(SDLKey key) +{ + hotKey_t *hotkey; + + hotkey = findHotkey(key); + + if( hotkey == NULL ) + { + assert( ! "This hotkey not register"); + } + + lastActive = getMyTime(); + hotkey->active = TRUE; +} + +void disableHotKey(SDLKey key) +{ + hotKey_t *hotkey; + + hotkey = findHotkey(key); + + if( hotkey == NULL ) + { + assert( ! "This hotkey not register"); + } + + lastActive = getMyTime(); + hotkey->active = FALSE; +} + +void eventHotKey() +{ + my_time_t currentTime; + Uint8 *mapa; + int i; + + currentTime = getMyTime(); + + if( currentTime - lastActive < HOTKEY_ACTIVE_INTERVAL ) + { + return; + } + + mapa = SDL_GetKeyState(NULL); + + for( i = 0 ; i < listHotKey->count ; i++ ) + { + hotKey_t *this; + + this = (hotKey_t *)listHotKey->list[i]; + + if( mapa[this->key] == SDL_PRESSED && this->active == TRUE ) + { + lastActive = getMyTime(); + //printf("hotKey = %d\n", this->key); + this->handler(); + } + } +} + +void quitHotKey() +{ + destroyListItem(listHotKey, destroyHotKey); +} |