1 /*
2 XorGramana Copyright 2009 James W. Morris, james@jwm-art.net
3
4 This file is part of XorGramana.
5
6 XorGramana is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 XorGramana is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with XorGramana. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "input.h"
20 #include "gfx.h"
21
22 struct xg_input xginput={
23 0,
24 SDL_DEFAULT_REPEAT_DELAY/3,
25 SDL_DEFAULT_REPEAT_INTERVAL*2,
26 0,0
27 };
28
create_key_list_array(int count)29 struct xg_key** create_key_list_array(int count)
30 {
31 int n;
32 struct xg_key** keylist;
33 if(!(keylist=calloc(count+1,sizeof(struct xg_key)))){
34 fprintf(stderr,"Failed to allocate list for %d key data items\n",count);
35 return 0;
36 }
37 for(n=0;n<count;n++){
38 if(!(keylist[n]=calloc(1,sizeof(struct xg_key)))){
39 fprintf(stderr,
40 "Failed to allocate list for %d key data items\n",
41 count);
42 free(keylist);
43 return 0;
44 }
45 }
46 keylist[count]=0;
47 return keylist;
48 }
49
destroy_key_list_array(struct xg_key ** keylist)50 void destroy_key_list_array(struct xg_key** keylist)
51 {
52 struct xg_key* key=0;
53 int n;
54 if(!keylist)
55 return;
56 n=0;
57 #ifdef INPUT_DEBUG
58 printf("Freeing keylistarray:%lx",(unsigned long)keylist);
59 #endif
60 while((key=keylist[n])!=0){
61 #ifdef INPUT_DEBUG
62 printf("freeing key: %c\n",key->key);
63 #endif
64 free(key);
65 n++;
66 }
67 #ifdef INPUT_DEBUG
68 printf("that list done.\n");
69 #endif
70 free(keylist);
71 }
72
set_keys(struct xg_key ** keys)73 void set_keys(struct xg_key** keys)
74 {
75 xginput.keys=keys;
76 }
77
key_repeat_on()78 void key_repeat_on()
79 {
80 SDL_EnableKeyRepeat(xginput.r_delay,xginput.r_interval);
81 }
82
key_repeat_off()83 void key_repeat_off()
84 {
85 SDL_EnableKeyRepeat(0,0);
86 }
87
simple_poll_event()88 bool simple_poll_event()
89 {
90 SDL_Event event;
91 SDLKey sym;
92 struct xg_key* key=0;
93 int n;
94 if(!xginput.keys)
95 return FALSE;
96 xginput.quit=0;
97 xginput.exit=0;
98 for(n=0;(key=xginput.keys[n])!=0;n++)
99 key->pressed=0;
100 while(SDL_PollEvent(&event)){
101 switch(event.type){
102 case SDL_QUIT:
103 xginput.exit=1;
104 return TRUE;
105 case SDL_KEYDOWN:
106 sym=event.key.keysym.sym;
107 if(sym==SDLK_ESCAPE){
108 xginput.quit=1;
109 return TRUE;
110 }
111 else if(sym==SDLK_F11){
112 SDL_WM_ToggleFullScreen(screen);
113 return TRUE;
114 }
115 else{
116 for(n=0;(key=xginput.keys[n])!=0;n++){
117 if(sym==key->key){
118 key->pressed=1;
119 return TRUE;
120 }
121 }
122 }
123 break;
124 default:
125 break;
126 }
127 }
128 return FALSE;
129 }
130
wait_till_pressed()131 void wait_till_pressed()
132 {
133 unsigned int flimit=SDL_GetTicks()+TICK_COUNT;
134 while(!simple_poll_event()){
135 delay(flimit);
136 flimit=SDL_GetTicks()+TICK_COUNT;
137 }
138 }
139