* Add "less" target to view make output through less.
[matthijs/ABM2.git] / ABM2 / Amaltheia / Input.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dimitris Saougos & Filippos Papadopoulos   *
3  *   <psybases@gmail.com>                                                             *
4  *                                                                                                       *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU Library General Public License as       *
7  *   published by the Free Software Foundation; either version 2 of the    *
8  *   License, or (at your option) any later version.                                    *
9  *                                                                                                           *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU Library General Public     *
16  *   License along with this program; if not, write to the                 *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "Input.h"
22 #include <SDL/SDL.h>
23 #include <ctype.h>
24
25
26 const int LEFT_BUTTON = SDL_BUTTON_LEFT;
27 const int MIDDLE_BUTTON =  SDL_BUTTON_MIDDLE;
28 const int RIGHT_BUTTON   =   SDL_BUTTON_RIGHT;
29
30 const int KEY_ESC = SDLK_ESCAPE;
31 const int KEY_ESCAPE = SDLK_ESCAPE;
32
33 const int KEY_MINUS     =      SDLK_MINUS;
34 const int KEY_EQUALS =         SDLK_EQUALS;
35
36 const int KEY_BACKSPACE =      SDLK_BACKSPACE;
37
38 const int KEY_TAB     =        SDLK_TAB;
39
40 const int KEY_LBRACKET =       SDLK_LEFTBRACKET;
41 const int KEY_RBRACKET   =     SDLK_RIGHTBRACKET;
42
43 const int KEY_ENTER       =    SDLK_RETURN;
44 const int KEY_RETURN       =   SDLK_RETURN;
45
46 const int KEY_CTRL         =   SDLK_LCTRL;
47 const int KEY_LCTRL         =  SDLK_LCTRL;
48 const int KEY_RCTRL          = SDLK_RCTRL;
49
50 const int KEY_SHIFT         =  SDLK_LSHIFT;
51 const int KEY_LSHIFT       =   SDLK_LSHIFT;
52 const int KEY_RSHIFT      =    SDLK_RSHIFT;
53
54 const int KEY_ALT         =    SDLK_LSUPER;
55 const int KEY_LALT        =    SDLK_LSUPER;
56 const int KEY_RALT     =       SDLK_RSUPER;
57
58 const int KEY_0             =  SDLK_0;
59 const int KEY_1            =   SDLK_1;
60 const int KEY_2            =   SDLK_2;
61 const int KEY_3            =   SDLK_3;
62 const int KEY_4            =   SDLK_4;
63 const int KEY_5            =   SDLK_5;
64 const int KEY_6           =    SDLK_6;
65 const int KEY_7          =     SDLK_7;
66 const int KEY_8            =   SDLK_8;
67 const int KEY_9              = SDLK_9;
68
69 const int KEY_A              = SDLK_a;
70 const int KEY_B              = SDLK_b;
71 const int KEY_C           =    SDLK_c;
72 const int KEY_D        =       SDLK_d;
73 const int KEY_E         =      SDLK_e;
74 const int KEY_F          =     SDLK_f;
75 const int KEY_G           =    SDLK_g;
76 const int KEY_H       =        SDLK_h;
77 const int KEY_I          =     SDLK_i;
78 const int KEY_J             =  SDLK_j;
79 const int KEY_K             =  SDLK_k;
80 const int KEY_L      =         SDLK_l;
81 const int KEY_M       =        SDLK_m;
82 const int KEY_N          =     SDLK_n;
83 const int KEY_O             =  SDLK_o;
84 const int KEY_P           =    SDLK_p;
85 const int KEY_Q      =         SDLK_q;
86 const int KEY_R         =      SDLK_r;
87 const int KEY_S            =   SDLK_s;
88 const int KEY_T            =   SDLK_t;
89 const int KEY_U             =  SDLK_u;
90 const int KEY_V       =        SDLK_v;
91 const int KEY_W        =       SDLK_w;
92 const int KEY_X           =    SDLK_x;
93 const int KEY_Y             =  SDLK_y;
94 const int KEY_Z              = SDLK_z;
95
96 const int KEY_SEMICOLON  =     SDLK_SEMICOLON;
97 const int KEY_APOSTROPHE   =   SDLK_QUOTE;
98
99 const int KEY_TILDE       =    SDLK_UNKNOWN;
100 const int KEY_GRAVE      =     SDLK_BACKQUOTE;
101
102 const int KEY_BACKSLASH  =     SDLK_BACKSLASH;
103
104 const int KEY_COMMA       =    SDLK_COMMA;
105 const int KEY_PERIOD         = SDLK_PERIOD;
106 const int KEY_FORWARDSLASH  =  SDLK_SLASH;
107 const int KEY_SLASH       =    SDLK_SLASH;
108
109 const int KEY_SPACE     =      SDLK_SPACE;
110
111 const int KEY_CAPSLOCK      =  SDLK_CAPSLOCK;
112 const int KEY_CAPITAL      =   SDLK_CAPSLOCK;
113
114 const int KEY_F1        =      SDLK_F1;
115 const int KEY_F2         =     SDLK_F2;
116 const int KEY_F3          =    SDLK_F3;
117 const int KEY_F4           =   SDLK_F4;
118 const int KEY_F5              = SDLK_F5;
119 const int KEY_F6        =      SDLK_F6;
120 const int KEY_F7            =  SDLK_F7;
121 const int KEY_F8          =    SDLK_F8;
122 const int KEY_F9         =     SDLK_F9;
123 const int KEY_F10         =    SDLK_F10;
124 const int KEY_F11          =   SDLK_F11;
125 const int KEY_F12          =   SDLK_F12;
126
127 const int KEY_SYSRQ      =     SDLK_SYSREQ;
128 const int KEY_SCROLLLOCK  =    SDLK_SCROLLOCK;
129 const int KEY_PAUSE      =     SDLK_PAUSE;
130
131 const int KEY_NUMLOCK   =      SDLK_NUMLOCK;
132 const int KEY_NUMPAD0     =    SDLK_KP0;
133 const int KEY_NUMPAD1      =   SDLK_KP1;
134 const int KEY_NUMPAD2     =    SDLK_KP2;
135 const int KEY_NUMPAD3      =   SDLK_KP3;
136 const int KEY_NUMPAD4      =   SDLK_KP4;
137 const int KEY_NUMPAD5       =  SDLK_KP5;
138 const int KEY_NUMPAD6      =   SDLK_KP6;
139 const int KEY_NUMPAD7     =    SDLK_KP7;
140 const int KEY_NUMPAD8      =   SDLK_KP8;
141 const int KEY_NUMPAD9       =  SDLK_KP9;
142 const int KEY_ADD        =     SDLK_PLUS;
143 const int KEY_SUBTRACT  =      SDLK_MINUS;
144 const int KEY_DIVIDE    =      SDLK_SLASH;
145 const int KEY_MULTIPLY   =     SDLK_ASTERISK;
146 const int KEY_DECIMAL     =    SDLK_PERIOD;
147 const int KEY_NUMPADENTER  =   SDLK_UNKNOWN;
148
149 const int KEY_INSERT    =      SDLK_INSERT;
150 const int KEY_DELETE     =     SDLK_DELETE;
151 const int KEY_HOME        =    SDLK_HOME;
152 const int KEY_END       =      SDLK_END;
153 const int KEY_PAGEUP    =      SDLK_PAGEUP;
154 const int KEY_PAGEDOWN  =      SDLK_PAGEDOWN;
155
156 const int KEY_UP      =        SDLK_UP;
157 const int KEY_DOWN   =         SDLK_DOWN;
158 const int KEY_LEFT       =     SDLK_LEFT;
159 const int KEY_RIGHT        =   SDLK_RIGHT;
160
161 const int KEY_LWIN     =       SDLK_LSUPER;
162 const int KEY_RWIN      =      SDLK_RSUPER;
163 const int KEY_APPS         =   SDLK_MENU;
164
165
166
167
168
169 Input::Input(bool keyb,bool mous)
170 {
171     int i;
172     lockBuffer = 0;
173
174     for (i=0;i<4;i++)
175         mouseLockBuffer[i]=-1;
176
177     symbol[0] = ')';
178     symbol[1] = '!';
179     symbol[2] = '@';
180     symbol[3] = '#';
181     symbol[4] = '$';
182     symbol[5] = '%';
183     symbol[6] = '^';
184     symbol[7] = '&';
185     symbol[8] = '*';
186     symbol[9] = '(';
187
188     keyLockDuration = D;
189     return ;
190 }
191
192 Input::~Input()
193 {
194     return ;
195 }
196
197
198
199 bool Input::keyboardGetKeyState(int key)
200 {
201
202     static long tm = getTime();
203     bool retValue = false;
204
205     tm = getTime();
206     SDL_PumpEvents();
207     SDLMod mod = SDL_GetModState();
208
209     if(key == KEY_CTRL && (mod & KMOD_CTRL))
210         retValue = true;
211     else
212         if(key == KEY_LCTRL && (mod & KMOD_LCTRL))
213             retValue = true;
214         else
215             if(key == KEY_RCTRL && (mod & KMOD_RCTRL))
216                 retValue = true;
217             else
218                 if(key == KEY_SHIFT      && (mod & KMOD_SHIFT))
219                     retValue = true;
220                 else
221                     if(key == KEY_LSHIFT && (mod & KMOD_LSHIFT))
222                         retValue = true;
223                     else
224                         if(key == KEY_RSHIFT && (mod & KMOD_RSHIFT))
225                             retValue = true;
226                         else
227                             if(key == KEY_ALT && (mod & KMOD_ALT))
228                                 retValue = true;
229                             else
230                                 if(key == KEY_LALT  && (mod & KMOD_LALT))
231                                     retValue = true;
232                                 else
233                                     if(key == KEY_RALT && (mod & KMOD_RALT))
234                                         retValue = true;
235                                     else
236                                         if((key == KEY_CAPSLOCK || key == KEY_CAPITAL ) && (mod & KMOD_CAPS))
237                                             retValue = true;
238                                         else
239                                             if(key == KEY_NUMLOCK && (mod & KMOD_NUM))
240                                                 retValue = true;
241                                             else
242                                             {
243                                                 Uint8 *keystate = SDL_GetKeyState(NULL);
244                                                 if( keystate[key])
245                                                 {
246                                                     retValue = true;
247                                                     key_val = 0;
248                                                 }
249                                             }
250
251
252     return retValue;
253 }
254
255
256 /*
257 bool Input::keyboardGetKeyState(int key)
258 {
259         bool retValue = false;
260         
261         if (getTime() - lockBuffer[key] > keyLockDuration)
262                 lockBuffer[key]=-1;
263  
264 //      printf("key = %d\n", key);
265         if(lockBuffer[key]==-1)
266
267                 if(key_press && key_val == key)
268                         retValue = true;
269 }
270  
271         return retValue;
272 }
273 */
274
275
276 void Input::mousePrepare()
277 {
278     SDL_PumpEvents();
279     SDL_GetRelativeMouseState(&mx, &my);
280     return;
281 }
282
283 long Input::mouseGetYDelta(void)
284 {
285         return my;
286 }
287
288
289 long Input::mouseGetXDelta(void)
290 {
291     return mx;
292 }
293
294
295 bool Input::mouseGetButtonState(int button)
296 {
297     bool retValue = false;
298
299     if (getTime()-mouseLockBuffer[button]>buttonLockDuration)
300         mouseLockBuffer[button]=-1;
301
302     /*
303        while(SDL_PollEvent(&event))
304        {
305           if(event.type == SDL_MOUSEBUTTONDOWN)
306           {
307              if((&event.button)->button == button)
308                 retValue = true;
309              else
310              {
311                 if(SDL_PushEvent(&event) == -1)
312                    std::cerr << "push event failed\n";
313                 retValue = false;
314              }
315              break;
316           }
317           else
318                 if(event.type == SDL_MOUSEBUTTONUP)
319                    continue;
320           else
321                 if(event.type == SDL_KEYDOWN || event.type == SDL_MOUSEMOTION)
322                 {
323                    if(SDL_PushEvent(&event) == -1)
324                       std::cerr << "push event failed\n";
325                    break;
326                 }
327            else
328                break;
329      
330        } // while
331     */
332     if (mouseLockBuffer[button]==-1)
333     {
334         SDL_PumpEvents();
335         if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(button))
336             retValue = true;
337     }
338
339     return retValue;
340 }
341
342
343 void Input::lockKey(int key, int duration)
344 {
345     if (key<0 || key>255)
346         return ;
347     keyLockDuration=duration;
348     return ;
349 }
350
351 void Input::lockButton(int button, int duration)
352 {
353     if (button<0 || button>3)
354         return ;
355
356     //if (mouseLockBuffer[button]!=-1) return ;
357
358     mouseLockBuffer[button]=(int)(getTime());
359
360     buttonLockDuration=duration;
361
362     return ;
363 }
364
365
366 char Input::getChar(void)
367 {
368
369     static int oldKey = 0;
370     static long delay = getTime();
371     static long repeat = getTime();
372
373
374     if(key_val == oldKey)
375     {
376         repeat = getTime();
377         if(getTime() - delay < D)
378             return 0;
379         else
380             delay = getTime();
381     }
382     else
383     {
384         delay = getTime();
385         if(getTime() - repeat < R)
386             return 0;
387         else
388             repeat = getTime();
389     }
390
391     oldKey = key_val;
392
393     if(key_press)
394     {
395         if(key_val == 13)
396             return '\n';
397         else
398         {
399             SDL_PumpEvents();
400             SDLMod mod = SDL_GetModState();
401
402             if((mod & KMOD_CAPS) && !(mod & KMOD_SHIFT))
403                 key_val = toupper(key_val);
404
405             if((mod & KMOD_SHIFT))
406             {
407                 if(isdigit(key_val))
408                     return symbol[key_val - 48];
409                 if(key_val == '\'')
410                     return '\"';
411                 if(key_val == ';')
412                     return ':';
413                 if(key_val == ',')
414                     return '<';
415                 if(key_val == '.')
416                     return '>';
417                 if(key_val == '[')
418                     return '{';
419                 if(key_val == ']')
420                     return '}';
421                 if(key_val == '-')
422                     return '_';
423                 if(key_val == '=')
424                     return '+';
425                 if(key_val == '\\')
426                     return '|';
427                 if(key_val == '/')
428                     return '?';
429
430                 if(!(mod & KMOD_CAPS))
431                     return toupper(key_val);
432             }
433             return key_val;
434         }
435     }
436
437     return 0;
438     /*
439         for (i=0;i<256;i++)
440                 if(keyboardGetKeyState(i))
441                 {
442                         lockKey(i, 300);
443                 
444                         if(i ==13)
445                                 return '\n';
446                 
447                         return i;
448                 }
449     */
450 }
451