Include and link physfs properly.
[tuxanci.git] / src / client / hotKey.c
blob5471101d6388fa97577eddad4d46b65b4fbfad6b
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
5 #include "main.h"
6 #include "list.h"
7 #include "myTimer.h"
9 #include "interface.h"
10 #include "hotKey.h"
12 typedef struct hotKey_struct {
13 SDLKey key;
14 bool_t active;
15 void (*handler) ();
16 } hotKey_t;
18 static list_t *listHotKey;
19 static my_time_t lastActive;
21 static hotKey_t *newHotKey(SDLKey key, void (*handler) ())
23 hotKey_t *new;
25 new = malloc(sizeof(hotKey_t));
26 new->key = key;
27 new->active = TRUE;
28 new->handler = handler;
30 return new;
33 static void destroyHotKey(hotKey_t *hotkey)
35 free(hotkey);
38 static hotKey_t *findHotkey(SDLKey key)
40 int i;
42 for (i = 0; i < listHotKey->count; i++) {
43 hotKey_t *this;
45 this = (hotKey_t *) listHotKey->list[i];
47 if (this->key == key) {
48 return this;
52 return NULL;
55 void hot_key_init()
57 listHotKey = list_new();
58 lastActive = timer_get_current_time();
61 void hot_key_register(SDLKey key, void (*handler) ())
63 hotKey_t *hotkey;
65 if (findHotkey(key) != NULL) {
66 assert(!"Conflict with register key");
69 hotkey = newHotKey(key, handler);
70 list_ins(listHotKey, 0, hotkey);
73 void hot_key_unregister(SDLKey key)
75 hotKey_t *hotkey;
76 int my_index;
78 hotkey = findHotkey(key);
80 if (hotkey == NULL) {
81 fatal("Unregistering not registered hotkey");
84 my_index = list_search(listHotKey, hotkey);
85 assert(my_index != -1);
86 list_del_item(listHotKey, my_index, destroyHotKey);
89 void hot_key_enable(SDLKey key)
91 hotKey_t *hotkey;
93 hotkey = findHotkey(key);
95 if (hotkey == NULL) {
96 fatal("Enabling not registered hotkey");
99 lastActive = timer_get_current_time();
100 hotkey->active = TRUE;
103 void hot_key_disable(SDLKey key)
105 hotKey_t *hotkey;
107 hotkey = findHotkey(key);
109 if (hotkey == NULL) {
110 fatal("Disabling not registered hotkey");
113 lastActive = timer_get_current_time();
114 hotkey->active = FALSE;
117 void hot_key_event()
119 my_time_t currentTime;
120 Uint8 *mapa;
121 int i;
123 currentTime = timer_get_current_time();
125 if (currentTime - lastActive < HOTKEY_ACTIVE_INTERVAL) {
126 return;
129 mapa = SDL_GetKeyState(NULL);
131 for (i = 0; i < listHotKey->count; i++) {
132 hotKey_t *this;
134 this = (hotKey_t *) listHotKey->list[i];
136 if (mapa[this->key] == SDL_PRESSED && this->active == TRUE) {
137 lastActive = timer_get_current_time();
138 /*printf("hotKey = %d\n", this->key);*/
139 this->handler();
141 return;
146 void hot_key_quit()
148 list_destroy_item(listHotKey, destroyHotKey);