Debian Codegraph @

src/gui/sdl_mapper.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Copyright (C) 2002-2014 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: sdl_mapper.cpp,v 1.60 2009-06-01 10:25:51 qbix79 Exp $ */
20
21 #include <vector>
22 #include <list>
23 #include <string.h>
24 #include <ctype.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <assert.h>
28
29
30 #include "SDL.h"
31 #include "SDL_thread.h"
32
33 #include "dosbox.h"
34 #include "video.h"
35 #include "keyboard.h"
36 #include "joystick.h"
37 #include "support.h"
38 #include "mapper.h"
39 #include "setup.h"
40
41 enum {
42 CLR_BLACK=0,
43 CLR_WHITE=1,
44 CLR_RED=2,
45 CLR_BLUE=3,
46 CLR_GREEN=4
47 };
48
49 enum BB_Types {
50 BB_Next,BB_Add,BB_Del,
51 BB_Save,BB_Exit
52 };
53
54 enum BC_Types {
55 BC_Mod1,BC_Mod2,BC_Mod3,
56 BC_Hold
57 };
58
59 #define BMOD_Mod1 0x0001
60 #define BMOD_Mod2 0x0002
61 #define BMOD_Mod3 0x0004
62
63 #define BFLG_Hold 0x0001
64 #define BFLG_Repeat 0x0004
65
66
67 #define MAXSTICKS 8
68 #define MAXACTIVE 16
69 #define MAXBUTTON 32
70 #define MAXBUTTON_CAP 16
71
72 class CEvent;
73 class CHandlerEvent;
74 class CButton;
75 class CBind;
76 class CBindGroup;
77
78 static void SetActiveEvent(CEvent * event);
79 static void SetActiveBind(CBind * _bind);
80 extern Bit8u int10_font_14[256 * 14];
81
82 static std::vector<CEvent *> events;
83 static std::vector<CButton *> buttons;
84 static std::vector<CBindGroup *> bindgroups;
85 static std::vector<CHandlerEvent *> handlergroup;
86 typedef std::list<CBind *> CBindList;
87 typedef std::list<CEvent *>::iterator CEventList_it;
88 typedef std::list<CBind *>::iterator CBindList_it;
89 typedef std::vector<CButton *>::iterator CButton_it;
90 typedef std::vector<CEvent *>::iterator CEventVector_it;
91 typedef std::vector<CHandlerEvent *>::iterator CHandlerEventVector_it;
92 typedef std::vector<CBindGroup *>::iterator CBindGroup_it;
93
94 static CBindList holdlist;
95
96
97 class CEvent {
98 public:
99 CEvent(char const * const _entry) {
100 safe_strncpy(entry,_entry,16);
101 events.push_back(this);
102 bindlist.clear();
103 activity=0;
104 current_value=0;
105 }
106 void AddBind(CBind * bind);
107 virtual ~CEvent() {}
108 virtual void Active(bool yesno)=0;
109 virtual void ActivateEvent(bool ev_trigger,bool skip_action)=0;
110 virtual void DeActivateEvent(bool ev_trigger)=0;
111 void DeActivateAll(void);
112 void SetValue(Bits value){
113 current_value=value;
114 }
115 Bits GetValue(void) {
116 return current_value;
117 }
118 char * GetName(void) { return entry; }
119 virtual bool IsTrigger(void)=0;
120 CBindList bindlist;
121 protected:
122 Bitu activity;
123 char entry[16];
124 Bits current_value;
125 };
126
127 /* class for events which can be ON/OFF only: key presses, joystick buttons, joystick hat */
128 class CTriggeredEvent : public CEvent {
129 public:
130 CTriggeredEvent(char const * const _entry) : CEvent(_entry) {}
131 virtual bool IsTrigger(void) {
132 return true;
133 }
134 void ActivateEvent(bool ev_trigger,bool skip_action) {
135 if (current_value>25000) {
136 /* value exceeds boundary, trigger event if not active */
137 if (!activity && !skip_action) Active(true);
138 if (activity<32767) activity++;
139 } else {
140 if (activity>0) {
141 /* untrigger event if it is fully inactive */
142 DeActivateEvent(ev_trigger);
143 activity=0;
144 }
145 }
146 }
147 void DeActivateEvent(bool /*ev_trigger*/) {
148 activity--;
149 if (!activity) Active(false);
150 }
151 };
152
153 /* class for events which have a non-boolean state: joystick axis movement */
154 class CContinuousEvent : public CEvent {
155 public:
156 CContinuousEvent(char const * const _entry) : CEvent(_entry) {}
157 virtual bool IsTrigger(void) {
158 return false;
159 }
160 void ActivateEvent(bool ev_trigger,bool skip_action) {
161 if (ev_trigger) {
162 activity++;
163 if (!skip_action) Active(true);
164 } else {
165 /* test if no trigger-activity is present, this cares especially
166 about activity of the opposite-direction joystick axis for example */
167 if (!GetActivityCount()) Active(true);
168 }
169 }
170 void DeActivateEvent(bool ev_trigger) {
171 if (ev_trigger) {
172 if (activity>0) activity--;
173 if (activity==0) {
174 /* test if still some trigger-activity is present,
175 adjust the state in this case accordingly */
176 if (GetActivityCount()) RepostActivity();
177 else Active(false);
178 }
179 } else {
180 if (!GetActivityCount()) Active(false);
181 }
182 }
183 virtual Bitu GetActivityCount(void) {
184 return activity;
185 }
186 virtual void RepostActivity(void) {}
187 };
188
189 class CBind {
190 public:
191 virtual ~CBind () {
192 list->remove(this);
193 // event->bindlist.remove(this);
194 }
195 CBind(CBindList * _list) {
196 list=_list;
197 _list->push_back(this);
198 mods=flags=0;
199 event=0;
200 active=holding=false;
201 }
202 void AddFlags(char * buf) {
203 if (mods & BMOD_Mod1) strcat(buf," mod1");
204 if (mods & BMOD_Mod2) strcat(buf," mod2");
205 if (mods & BMOD_Mod3) strcat(buf," mod3");
206 if (flags & BFLG_Hold) strcat(buf," hold");
207 }
208 void SetFlags(char * buf) {
209 char * word;
210 while (*(word=StripWord(buf))) {
211 if (!strcasecmp(word,"mod1")) mods|=BMOD_Mod1;
212 if (!strcasecmp(word,"mod2")) mods|=BMOD_Mod2;
213 if (!strcasecmp(word,"mod3")) mods|=BMOD_Mod3;
214 if (!strcasecmp(word,"hold")) flags|=BFLG_Hold;
215 }
216 }
217 void ActivateBind(Bits _value,bool ev_trigger,bool skip_action=false) {
218 if (event->IsTrigger()) {
219 /* use value-boundary for on/off events */
220 if (_value>25000) {
221 event->SetValue(_value);
222 if (active) return;
223 event->ActivateEvent(ev_trigger,skip_action);
224 active=true;
225 } else {
226 if (active) {
227 event->DeActivateEvent(ev_trigger);
228 active=false;
229 }
230 }
231 } else {
232 /* store value for possible later use in the activated event */
233 event->SetValue(_value);
234 event->ActivateEvent(ev_trigger,false);
235 }
236 }
237 void DeActivateBind(bool ev_trigger) {
238 if (event->IsTrigger()) {
239 if (!active) return;
240 active=false;
241 if (flags & BFLG_Hold) {
242 if (!holding) {
243 holdlist.push_back(this);
244 holding=true;
245 return;
246 } else {
247 holdlist.remove(this);
248 holding=false;
249 }
250 }
251 event->DeActivateEvent(ev_trigger);
252 } else {
253 /* store value for possible later use in the activated event */
254 event->SetValue(0);
255 event->DeActivateEvent(ev_trigger);
256 }
257 }
258 virtual void ConfigName(char * buf)=0;
259 virtual void BindName(char * buf)=0;
260
261 Bitu mods,flags;
262 Bit16s value;
263 CEvent * event;
264 CBindList * list;
265 bool active,holding;
266 };
267
268
269 void CEvent::AddBind(CBind * bind) {
270 bindlist.push_front(bind);
271 bind->event=this;
272 }
273 void CEvent::DeActivateAll(void) {
274 for (CBindList_it bit=bindlist.begin();bit!=bindlist.end();bit++) {
275 (*bit)->DeActivateBind(true);
276 }
277 }
278
279
280
281 class CBindGroup {
282 public:
283 CBindGroup() {
284 bindgroups.push_back(this);
285 }
286 void ActivateBindList(CBindList * list,Bits value,bool ev_trigger);
287 void DeactivateBindList(CBindList * list,bool ev_trigger);
288 virtual CBind * CreateConfigBind(char *&buf)=0;
289 virtual CBind * CreateEventBind(SDL_Event * event)=0;
290
291 virtual bool CheckEvent(SDL_Event * event)=0;
292 virtual const char * ConfigStart(void)=0;
293 virtual const char * BindStart(void)=0;
294 virtual ~CBindGroup (void) { }
295
296 protected:
297
298 };
299
300
301 #define MAX_SDLKEYS 323
302
303 static bool usescancodes;
304 static Bit8u scancode_map[MAX_SDLKEYS];
305
306 #define Z SDLK_UNKNOWN
307
308 #if defined (MACOSX)
309 static SDLKey sdlkey_map[]={
310 /* Main block printables */
311 /*00-05*/ SDLK_a, SDLK_s, SDLK_d, SDLK_f, SDLK_h, SDLK_g,
312 /*06-0B*/ SDLK_z, SDLK_x, SDLK_c, SDLK_v, SDLK_WORLD_0, SDLK_b,
313 /*0C-11*/ SDLK_q, SDLK_w, SDLK_e, SDLK_r, SDLK_y, SDLK_t,
314 /*12-17*/ SDLK_1, SDLK_2, SDLK_3, SDLK_4, SDLK_6, SDLK_5,
315 /*18-1D*/ SDLK_EQUALS, SDLK_9, SDLK_7, SDLK_MINUS, SDLK_8, SDLK_0,
316 /*1E-21*/ SDLK_RIGHTBRACKET, SDLK_o, SDLK_u, SDLK_LEFTBRACKET,
317 /*22-23*/ SDLK_i, SDLK_p,
318 /*24-29*/ SDLK_RETURN, SDLK_l, SDLK_j, SDLK_QUOTE, SDLK_k, SDLK_SEMICOLON,
319 /*2A-29*/ SDLK_BACKSLASH, SDLK_COMMA, SDLK_SLASH, SDLK_n, SDLK_m,
320 /*2F-2F*/ SDLK_PERIOD,
321
322 /* Spaces, controls, modifiers (dosbox uses LMETA only for
323 * hotkeys, it's not really mapped to an emulated key) */
324 /*30-33*/ SDLK_TAB, SDLK_SPACE, SDLK_BACKQUOTE, SDLK_BACKSPACE,
325 /*34-37*/ Z, SDLK_ESCAPE, Z, SDLK_LMETA,
326 /*38-3B*/ SDLK_LSHIFT, SDLK_CAPSLOCK, SDLK_LALT, SDLK_LCTRL,
327
328 /*3C-40*/ Z, Z, Z, Z, Z,
329
330 /* Keypad (KP_EQUALS not supported, NUMLOCK used on what is CLEAR
331 * in Mac OS X) */
332 /*41-46*/ SDLK_KP_PERIOD, Z, SDLK_KP_MULTIPLY, Z, SDLK_KP_PLUS, Z,
333 /*47-4A*/ SDLK_NUMLOCK /*==SDLK_CLEAR*/, Z, Z, Z,
334 /*4B-4D*/ SDLK_KP_DIVIDE, SDLK_KP_ENTER, Z,
335 /*4E-51*/ SDLK_KP_MINUS, Z, Z, SDLK_KP_EQUALS,
336 /*52-57*/ SDLK_KP0, SDLK_KP1, SDLK_KP2, SDLK_KP3, SDLK_KP4, SDLK_KP5,
337 /*58-5C*/ SDLK_KP6, SDLK_KP7, Z, SDLK_KP8, SDLK_KP9,
338
339 /*5D-5F*/ Z, Z, Z,
340
341 /* Function keys and cursor blocks (F13 not supported, F14 =>
342 * PRINT[SCREEN], F15 => SCROLLOCK, F16 => PAUSE, HELP => INSERT) */
343 /*60-64*/ SDLK_F5, SDLK_F6, SDLK_F7, SDLK_F3, SDLK_F8,
344 /*65-6A*/ SDLK_F9, Z, SDLK_F11, Z, SDLK_F13, SDLK_PAUSE /*==SDLK_F16*/,
345 /*6B-70*/ SDLK_PRINT /*==SDLK_F14*/, Z, SDLK_F10, Z, SDLK_F12, Z,
346 /*71-72*/ SDLK_SCROLLOCK /*==SDLK_F15*/, SDLK_INSERT /*==SDLK_HELP*/,
347 /*73-77*/ SDLK_HOME, SDLK_PAGEUP, SDLK_DELETE, SDLK_F4, SDLK_END,
348 /*78-7C*/ SDLK_F2, SDLK_PAGEDOWN, SDLK_F1, SDLK_LEFT, SDLK_RIGHT,
349 /*7D-7E*/ SDLK_DOWN, SDLK_UP,
350
351 /*7F-7F*/ Z,
352
353 /* 4 extra keys that don't really exist, but are needed for
354 * round-trip mapping (dosbox uses RMETA only for hotkeys, it's
355 * not really mapped to an emulated key) */
356 SDLK_RMETA, SDLK_RSHIFT, SDLK_RALT, SDLK_RCTRL,
357 };
358 #define MAX_SCANCODES (0x80+4)
359 /* Make sure that the table above has the expected size. This
360 expression will raise a compiler error if the condition is false. */
361 typedef char assert_right_size [MAX_SCANCODES == (sizeof(sdlkey_map)/sizeof(sdlkey_map[0])) ? 1 : -1];
362
363 #else // !MACOSX
364
365 #define MAX_SCANCODES 212
366 static SDLKey sdlkey_map[MAX_SCANCODES]={SDLK_UNKNOWN,SDLK_ESCAPE,
367 SDLK_1,SDLK_2,SDLK_3,SDLK_4,SDLK_5,SDLK_6,SDLK_7,SDLK_8,SDLK_9,SDLK_0,
368 /* 0x0c: */
369 SDLK_MINUS,SDLK_EQUALS,SDLK_BACKSPACE,SDLK_TAB,
370 SDLK_q,SDLK_w,SDLK_e,SDLK_r,SDLK_t,SDLK_y,SDLK_u,SDLK_i,SDLK_o,SDLK_p,
371 SDLK_LEFTBRACKET,SDLK_RIGHTBRACKET,SDLK_RETURN,SDLK_LCTRL,
372 SDLK_a,SDLK_s,SDLK_d,SDLK_f,SDLK_g,SDLK_h,SDLK_j,SDLK_k,SDLK_l,
373 SDLK_SEMICOLON,SDLK_QUOTE,SDLK_BACKQUOTE,SDLK_LSHIFT,SDLK_BACKSLASH,
374 SDLK_z,SDLK_x,SDLK_c,SDLK_v,SDLK_b,SDLK_n,SDLK_m,
375 /* 0x33: */
376 SDLK_COMMA,SDLK_PERIOD,SDLK_SLASH,SDLK_RSHIFT,SDLK_KP_MULTIPLY,
377 SDLK_LALT,SDLK_SPACE,SDLK_CAPSLOCK,
378 SDLK_F1,SDLK_F2,SDLK_F3,SDLK_F4,SDLK_F5,SDLK_F6,SDLK_F7,SDLK_F8,SDLK_F9,SDLK_F10,
379 /* 0x45: */
380 SDLK_NUMLOCK,SDLK_SCROLLOCK,
381 SDLK_KP7,SDLK_KP8,SDLK_KP9,SDLK_KP_MINUS,SDLK_KP4,SDLK_KP5,SDLK_KP6,SDLK_KP_PLUS,
382 SDLK_KP1,SDLK_KP2,SDLK_KP3,SDLK_KP0,SDLK_KP_PERIOD,
383 SDLK_UNKNOWN,SDLK_UNKNOWN,
384 SDLK_LESS,SDLK_F11,SDLK_F12,
385 Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,
386 Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,
387 Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,
388 /* 0xb7: */
389 Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z
390 /* 0xd4: ... */
391 };
392 #endif
393
394 #undef Z
395
396
397 SDLKey MapSDLCode(Bitu skey) {
398 if (usescancodes) {
399 if (skey<MAX_SCANCODES) return sdlkey_map[skey];
400 else return SDLK_UNKNOWN;
401 } else return (SDLKey)skey;
402 }
403
404 Bitu GetKeyCode(SDL_keysym keysym) {
405 if (usescancodes) {
406 Bitu key=(Bitu)keysym.scancode;
407 if (key==0
408 #if defined (MACOSX)
409 /* On Mac on US keyboards, scancode 0 is actually the 'a'
410 * key. For good measure exclude all printables from this
411 * condition. */
412 && (keysym.sym < SDLK_SPACE || keysym.sym > SDLK_WORLD_95)
413 #endif
414 ) {
415 /* try to retrieve key from symbolic key as scancode is zero */
416 if (keysym.sym<MAX_SDLKEYS) key=scancode_map[(Bitu)keysym.sym];
417 }
418 #if !defined (WIN32) && !defined (MACOSX) && !defined(OS2)
419 /* Linux adds 8 to all scancodes */
420 else key-=8;
421 #endif
422 #if defined (WIN32)
423 switch (key) {
424 case 0x1c: // ENTER
425 case 0x1d: // CONTROL
426 case 0x35: // SLASH
427 case 0x37: // PRINTSCREEN
428 case 0x38: // ALT
429 case 0x45: // PAUSE
430 case 0x47: // HOME
431 case 0x48: // cursor UP
432 case 0x49: // PAGE UP
433 case 0x4b: // cursor LEFT
434 case 0x4d: // cursor RIGHT
435 case 0x4f: // END
436 case 0x50: // cursor DOWN
437 case 0x51: // PAGE DOWN
438 case 0x52: // INSERT
439 case 0x53: // DELETE
440 if (GFX_SDLUsingWinDIB()) key=scancode_map[(Bitu)keysym.sym];
441 break;
442 }
443 #endif
444 return key;
445 } else {
446 #if defined (WIN32)
447 /* special handling of 102-key under windows */
448 if ((keysym.sym==SDLK_BACKSLASH) && (keysym.scancode==0x56)) return (Bitu)SDLK_LESS;
449 #endif
450 return (Bitu)keysym.sym;
451 }
452 }
453
454
455 class CKeyBind;
456 class CKeyBindGroup;
457
458 class CKeyBind : public CBind {
459 public:
460 CKeyBind(CBindList * _list,SDLKey _key) : CBind(_list) {
461 key = _key;
462 }
463 void BindName(char * buf) {
464 sprintf(buf,"Key %s",SDL_GetKeyName(MapSDLCode((Bitu)key)));
465 }
466 void ConfigName(char * buf) {
467 sprintf(buf,"key %d",MapSDLCode((Bitu)key));
468 }
469 public:
470 SDLKey key;
471 };
472
473 class CKeyBindGroup : public CBindGroup {
474 public:
475 CKeyBindGroup(Bitu _keys) : CBindGroup (){
476 lists=new CBindList[_keys];
477 for (Bitu i=0;i<_keys;i++) lists[i].clear();
478 keys=_keys;
479 configname="key";
480 }
481 ~CKeyBindGroup() { delete[] lists; }
482 CBind * CreateConfigBind(char *& buf) {
483 if (strncasecmp(buf,configname,strlen(configname))) return 0;
484 StripWord(buf);char * num=StripWord(buf);
485 Bitu code=ConvDecWord(num);
486 if (usescancodes) {
487 if (code<MAX_SDLKEYS) code=scancode_map[code];
488 else code=0;
489 }
490 CBind * bind=CreateKeyBind((SDLKey)code);
491 return bind;
492 }
493 CBind * CreateEventBind(SDL_Event * event) {
494 if (event->type!=SDL_KEYDOWN) return 0;
495 return CreateKeyBind((SDLKey)GetKeyCode(event->key.keysym));
496 };
497 bool CheckEvent(SDL_Event * event) {
498 if (event->type!=SDL_KEYDOWN && event->type!=SDL_KEYUP) return false;
499 Bitu key=GetKeyCode(event->key.keysym);
500 // LOG_MSG("key type %i is %x [%x %x]",event->type,key,event->key.keysym.sym,event->key.keysym.scancode);
501 assert(Bitu(event->key.keysym.sym)<keys);
502 if (event->type==SDL_KEYDOWN) ActivateBindList(&lists[key],0x7fff,true);
503 else DeactivateBindList(&lists[key],true);
504 return 0;
505 }
506 CBind * CreateKeyBind(SDLKey _key) {
507 if (!usescancodes) assert((Bitu)_key<keys);
508 return new CKeyBind(&lists[(Bitu)_key],_key);
509 }
510 private:
511 const char * ConfigStart(void) {
512 return configname;
513 }
514 const char * BindStart(void) {
515 return "Key";
516 }
517 protected:
518 const char * configname;
519 CBindList * lists;
520 Bitu keys;
521 };
522
523 #define MAX_VJOY_BUTTONS 8
524
525 static struct {
526 bool button_pressed[MAX_VJOY_BUTTONS];
527 Bit16s axis_pos[8];
528 bool hat_pressed[16];
529 } virtual_joysticks[2];
530
531
532 class CJAxisBind;
533 class CJButtonBind;
534 class CJHatBind;
535
536 class CJAxisBind : public CBind {
537 public:
538 CJAxisBind(CBindList * _list,CBindGroup * _group,Bitu _axis,bool _positive) : CBind(_list){
539 group = _group;
540 axis = _axis;
541 positive = _positive;
542 }
543 void ConfigName(char * buf) {
544 sprintf(buf,"%s axis %d %d",group->ConfigStart(),axis,positive ? 1 : 0);
545 }
546 void BindName(char * buf) {
547 sprintf(buf,"%s Axis %d%s",group->BindStart(),axis,positive ? "+" : "-");
548 }
549 protected:
550 CBindGroup * group;
551 Bitu axis;
552 bool positive;
553 };
554
555 class CJButtonBind : public CBind {
556 public:
557 CJButtonBind(CBindList * _list,CBindGroup * _group,Bitu _button) : CBind(_list) {
558 group = _group;
559 button=_button;
560 }
561 void ConfigName(char * buf) {
562 sprintf(buf,"%s button %d",group->ConfigStart(),button);
563 }
564 void BindName(char * buf) {
565 sprintf(buf,"%s Button %d",group->BindStart(),button);
566 }
567 protected:
568 CBindGroup * group;
569 Bitu button;
570 };
571
572 class CJHatBind : public CBind {
573 public:
574 CJHatBind(CBindList * _list,CBindGroup * _group,Bitu _hat,Bit8u _dir) : CBind(_list) {
575 group = _group;
576 hat = _hat;
577 dir = _dir;
578 /* allow only one hat position */
579 if (dir&SDL_HAT_UP) dir=SDL_HAT_UP;
580 else if (dir&SDL_HAT_RIGHT) dir=SDL_HAT_RIGHT;
581 else if (dir&SDL_HAT_DOWN) dir=SDL_HAT_DOWN;
582 else if (dir&SDL_HAT_LEFT) dir=SDL_HAT_LEFT;
583 else E_Exit("MAPPER:JOYSTICK:Invalid hat position");
584 }
585 void ConfigName(char * buf) {
586 sprintf(buf,"%s hat %d %d",group->ConfigStart(),hat,dir);
587 }
588 void BindName(char * buf) {
589 sprintf(buf,"%s Hat %d %s",group->BindStart(),hat,(dir==SDL_HAT_UP)?"up":
590 ((dir==SDL_HAT_RIGHT)?"right":
591 ((dir==SDL_HAT_DOWN)?"down":"left")));
592 }
593 protected:
594 CBindGroup * group;
595 Bitu hat;
596 Bit8u dir;
597 };
598
599 bool autofire = false;
600
601 class CStickBindGroup : public CBindGroup {
602 public:
603 CStickBindGroup(Bitu _stick,Bitu _emustick,bool _dummy=false) : CBindGroup (){
604 stick=_stick; // the number of the physical device (SDL numbering|)
605 emustick=_emustick; // the number of the emulated device
606 sprintf(configname,"stick_%d",emustick);
607
608 sdl_joystick=NULL;
609 axes=0; buttons=0; hats=0;
610 button_wrap=0;
611 button_cap=0; axes_cap=0; hats_cap=0;
612 emulated_buttons=0; emulated_axes=0; emulated_hats=0;
613
614 is_dummy=_dummy;
615 if (_dummy) return;
616
617 // initialize binding lists and position data
618 pos_axis_lists=new CBindList[4];
619 neg_axis_lists=new CBindList[4];
620 button_lists=new CBindList[MAXBUTTON];
621 hat_lists=new CBindList[4];
622 Bitu i;
623 for (i=0; i<MAXBUTTON; i++) {
624 button_autofire[i]=0;
625 old_button_state[i]=0;
626 }
627 for(i=0;i<16;i++) old_hat_state[i]=0;
628 for (i=0; i<4; i++) {
629 old_pos_axis_state[i]=false;
630 old_neg_axis_state[i]=false;
631 }
632
633 // initialize emulated joystick state
634 emulated_axes=2;
635 emulated_buttons=2;
636 emulated_hats=0;
637 JOYSTICK_Enable(emustick,true);
638
639 sdl_joystick=SDL_JoystickOpen(_stick);
640 if (sdl_joystick==NULL) {
641 button_wrap=emulated_buttons;
642 return;
643 }
644
645 axes=SDL_JoystickNumAxes(sdl_joystick);
646 buttons=SDL_JoystickNumButtons(sdl_joystick);
647 hats=SDL_JoystickNumHats(sdl_joystick);
648 button_wrap=buttons;
649 button_cap=buttons;
650 if (button_wrapping_enabled) {
651 button_wrap=emulated_buttons;
652 if (buttons>MAXBUTTON_CAP) button_cap = MAXBUTTON_CAP;
653 }
654 if (button_wrap > MAXBUTTON) button_wrap = MAXBUTTON;
655 axes_cap=emulated_axes;
656 if (axes_cap>axes) axes_cap=axes;
657 hats_cap=emulated_hats;
658 if (hats_cap>hats) hats_cap=hats;
659 LOG_MSG("Using joystick %s with %d axes, %d buttons and %d hat(s)",SDL_JoystickName(stick),axes,buttons,hats);
660 }
661 ~CStickBindGroup() {
662 SDL_JoystickClose(sdl_joystick);
663 delete[] pos_axis_lists;
664 delete[] neg_axis_lists;
665 delete[] button_lists;
666 delete[] hat_lists;
667 }
668
669 CBind * CreateConfigBind(char *& buf) {
670 if (strncasecmp(configname,buf,strlen(configname))) return 0;
671 StripWord(buf);char * type=StripWord(buf);
672 CBind * bind=0;
673 if (!strcasecmp(type,"axis")) {
674 Bitu ax=ConvDecWord(StripWord(buf));
675 bool pos=ConvDecWord(StripWord(buf)) > 0;
676 bind=CreateAxisBind(ax,pos);
677 } else if (!strcasecmp(type,"button")) {
678 Bitu but=ConvDecWord(StripWord(buf));
679 bind=CreateButtonBind(but);
680 } else if (!strcasecmp(type,"hat")) {
681 Bitu hat=ConvDecWord(StripWord(buf));
682 Bit8u dir=(Bit8u)ConvDecWord(StripWord(buf));
683 bind=CreateHatBind(hat,dir);
684 }
685 return bind;
686 }
687 CBind * CreateEventBind(SDL_Event * event) {
688 if (event->type==SDL_JOYAXISMOTION) {
689 if (event->jaxis.which!=stick) return 0;
690 #if defined (REDUCE_JOYSTICK_POLLING)
691 if (event->jaxis.axis>=emulated_axes) return 0;
692 #endif
693 if (abs(event->jaxis.value)<25000) return 0;
694 return CreateAxisBind(event->jaxis.axis,event->jaxis.value>0);
695 } else if (event->type==SDL_JOYBUTTONDOWN) {
696 if (event->jbutton.which!=stick) return 0;
697 #if defined (REDUCE_JOYSTICK_POLLING)
698 return CreateButtonBind(event->jbutton.button%button_wrap);
699 #else
700 return CreateButtonBind(event->jbutton.button);
701 #endif
702 } else if (event->type==SDL_JOYHATMOTION) {
703 if (event->jhat.which!=stick) return 0;
704 if (event->jhat.value==0) return 0;
705 if (event->jhat.value>(SDL_HAT_UP|SDL_HAT_RIGHT|SDL_HAT_DOWN|SDL_HAT_LEFT)) return 0;
706 return CreateHatBind(event->jhat.hat,event->jhat.value);
707 } else return 0;
708 }
709
710 virtual bool CheckEvent(SDL_Event * event) {
711 SDL_JoyAxisEvent * jaxis = NULL;
712 SDL_JoyButtonEvent * jbutton = NULL;
713 Bitu but = 0;
714
715 switch(event->type) {
716 case SDL_JOYAXISMOTION:
717 jaxis = &event->jaxis;
718 if(jaxis->which == stick) {
719 if(jaxis->axis == 0)
720 JOYSTICK_Move_X(emustick,(float)(jaxis->value/32768.0));
721 else if(jaxis->axis == 1)
722 JOYSTICK_Move_Y(emustick,(float)(jaxis->value/32768.0));
723 }
724 break;
725 case SDL_JOYBUTTONDOWN:
726 case SDL_JOYBUTTONUP:
727 jbutton = &event->jbutton;
728 bool state;
729 state=jbutton->type==SDL_JOYBUTTONDOWN;
730 but = jbutton->button % emulated_buttons;
731 if (jbutton->which == stick) {
732 JOYSTICK_Button(emustick,but,state);
733 }
734 break;
735 }
736 return false;
737 }
738
739 virtual void UpdateJoystick() {
740 if (is_dummy) return;
741 /* query SDL joystick and activate bindings */
742 ActivateJoystickBoundEvents();
743
744 bool button_pressed[MAXBUTTON];
745 Bitu i;
746 for (i=0; i<MAXBUTTON; i++) button_pressed[i]=false;
747 for (i=0; i<MAX_VJOY_BUTTONS; i++) {
748 if (virtual_joysticks[emustick].button_pressed[i])
749 button_pressed[i % button_wrap]=true;
750 }
751 for (i=0; i<emulated_buttons; i++) {
752 if (autofire && (button_pressed[i]))
753 JOYSTICK_Button(emustick,i,(++button_autofire[i])&1);
754 else
755 JOYSTICK_Button(emustick,i,button_pressed[i]);
756 }
757
758 JOYSTICK_Move_X(emustick,((float)virtual_joysticks[emustick].axis_pos[0])/32768.0f);
759 JOYSTICK_Move_Y(emustick,((float)virtual_joysticks[emustick].axis_pos[1])/32768.0f);
760 }
761
762 void ActivateJoystickBoundEvents() {
763 if (GCC_UNLIKELY(sdl_joystick==NULL)) return;
764
765 Bitu i;
766
767 bool button_pressed[MAXBUTTON];
768 for (i=0; i<MAXBUTTON; i++) button_pressed[i]=false;
769 /* read button states */
770 for (i=0; i<button_cap; i++) {
771 if (SDL_JoystickGetButton(sdl_joystick,i))
772 button_pressed[i % button_wrap]=true;
773 }
774 for (i=0; i<button_wrap; i++) {
775 /* activate binding if button state has changed */
776 if (button_pressed[i]!=old_button_state[i]) {
777 if (button_pressed[i]) ActivateBindList(&button_lists[i],32767,true);
778 else DeactivateBindList(&button_lists[i],true);
779 old_button_state[i]=button_pressed[i];
780 }
781 }
782
783 for (i=0; i<axes_cap; i++) {
784 Sint16 caxis_pos=SDL_JoystickGetAxis(sdl_joystick,i);
785 /* activate bindings for joystick position */
786 if (caxis_pos>1) {
787 if (old_neg_axis_state[i]) {
788 DeactivateBindList(&neg_axis_lists[i],false);
789 old_neg_axis_state[i] = false;
790 }
791 ActivateBindList(&pos_axis_lists[i],caxis_pos,false);
792 old_pos_axis_state[i] = true;
793 } else if (caxis_pos<-1) {
794 if (old_pos_axis_state[i]) {
795 DeactivateBindList(&pos_axis_lists[i],false);
796 old_pos_axis_state[i] = false;
797 }
798 if (caxis_pos!=-32768) caxis_pos=(Sint16)abs(caxis_pos);
799 else caxis_pos=32767;
800 ActivateBindList(&neg_axis_lists[i],caxis_pos,false);
801 old_neg_axis_state[i] = true;
802 } else {
803 /* center */
804 if (old_pos_axis_state[i]) {
805 DeactivateBindList(&pos_axis_lists[i],false);
806 old_pos_axis_state[i] = false;
807 }
808 if (old_neg_axis_state[i]) {
809 DeactivateBindList(&neg_axis_lists[i],false);
810 old_neg_axis_state[i] = false;
811 }
812 }
813 }
814
815 for (i=0; i<hats_cap; i++) {
816 Uint8 chat_state=SDL_JoystickGetHat(sdl_joystick,i);
817
818 /* activate binding if hat state has changed */
819 if ((chat_state & SDL_HAT_UP) != (old_hat_state[i] & SDL_HAT_UP)) {
820 if (chat_state & SDL_HAT_UP) ActivateBindList(&hat_lists[(i<<2)+0],32767,true);
821 else DeactivateBindList(&hat_lists[(i<<2)+0],true);
822 }
823 if ((chat_state & SDL_HAT_RIGHT) != (old_hat_state[i] & SDL_HAT_RIGHT)) {
824 if (chat_state & SDL_HAT_RIGHT) ActivateBindList(&hat_lists[(i<<2)+1],32767,true);
825 else DeactivateBindList(&hat_lists[(i<<2)+1],true);
826 }
827 if ((chat_state & SDL_HAT_DOWN) != (old_hat_state[i] & SDL_HAT_DOWN)) {
828 if (chat_state & SDL_HAT_DOWN) ActivateBindList(&hat_lists[(i<<2)+2],32767,true);
829 else DeactivateBindList(&hat_lists[(i<<2)+2],true);
830 }
831 if ((chat_state & SDL_HAT_LEFT) != (old_hat_state[i] & SDL_HAT_LEFT)) {
832 if (chat_state & SDL_HAT_LEFT) ActivateBindList(&hat_lists[(i<<2)+3],32767,true);
833 else DeactivateBindList(&hat_lists[(i<<2)+3],true);
834 }
835 old_hat_state[i]=chat_state;
836 }
837 }
838
839 private:
840 CBind * CreateAxisBind(Bitu axis,bool positive) {
841 if (axis<emulated_axes) {
842 if (positive) return new CJAxisBind(&pos_axis_lists[axis],this,axis,positive);
843 else return new CJAxisBind(&neg_axis_lists[axis],this,axis,positive);
844 }
845 return NULL;
846 }
847 CBind * CreateButtonBind(Bitu button) {
848 if (button<button_wrap)
849 return new CJButtonBind(&button_lists[button],this,button);
850 return NULL;
851 }
852 CBind * CreateHatBind(Bitu hat,Bit8u value) {
853 Bitu hat_dir;
854 if (value&SDL_HAT_UP) hat_dir=0;
855 else if (value&SDL_HAT_RIGHT) hat_dir=1;
856 else if (value&SDL_HAT_DOWN) hat_dir=2;
857 else if (value&SDL_HAT_LEFT) hat_dir=3;
858 else return NULL;
859 return new CJHatBind(&hat_lists[(hat<<2)+hat_dir],this,hat,value);
860 }
861 const char * ConfigStart(void) {
862 return configname;
863 }
864 const char * BindStart(void) {
865 if (sdl_joystick!=NULL) return SDL_JoystickName(stick);
866 else return "[missing joystick]";
867 }
868
869 protected:
870 CBindList * pos_axis_lists;
871 CBindList * neg_axis_lists;
872 CBindList * button_lists;
873 CBindList * hat_lists;
874 Bitu stick,emustick,axes,buttons,hats,emulated_axes,emulated_buttons,emulated_hats;
875 Bitu button_wrap,button_cap,axes_cap,hats_cap;
876 SDL_Joystick * sdl_joystick;
877 char configname[10];
878 Bitu button_autofire[MAXBUTTON];
879 bool old_button_state[MAXBUTTON];
880 bool old_pos_axis_state[16];
881 bool old_neg_axis_state[16];
882 Uint8 old_hat_state[16];
883 bool is_dummy;
884 };
885
886 class C4AxisBindGroup : public CStickBindGroup {
887 public:
888 C4AxisBindGroup(Bitu _stick,Bitu _emustick) : CStickBindGroup (_stick,_emustick){
889 emulated_axes=4;
890 emulated_buttons=4;
891 if (button_wrapping_enabled) button_wrap=emulated_buttons;
892
893 axes_cap=emulated_axes;
894 if (axes_cap>axes) axes_cap=axes;
895 hats_cap=emulated_hats;
896 if (hats_cap>hats) hats_cap=hats;
897
898 JOYSTICK_Enable(1,true);
899 }
900
901 bool CheckEvent(SDL_Event * event) {
902 SDL_JoyAxisEvent * jaxis = NULL;
903 SDL_JoyButtonEvent * jbutton = NULL;
904 Bitu but = 0;
905
906 switch(event->type) {
907 case SDL_JOYAXISMOTION:
908 jaxis = &event->jaxis;
909 if(jaxis->which == stick && jaxis->axis < 4) {
910 if(jaxis->axis & 1)
911 JOYSTICK_Move_Y(jaxis->axis>>1 & 1,(float)(jaxis->value/32768.0));
912 else
913 JOYSTICK_Move_X(jaxis->axis>>1 & 1,(float)(jaxis->value/32768.0));
914 }
915 break;
916 case SDL_JOYBUTTONDOWN:
917 case SDL_JOYBUTTONUP:
918 jbutton = &event->jbutton;
919 bool state;
920 state=jbutton->type==SDL_JOYBUTTONDOWN;
921 but = jbutton->button % emulated_buttons;
922 if (jbutton->which == stick) {
923 JOYSTICK_Button((but >> 1),(but & 1),state);
924 }
925 break;
926 }
927 return false;
928 }
929
930 virtual void UpdateJoystick() {
931 /* query SDL joystick and activate bindings */
932 ActivateJoystickBoundEvents();
933
934 bool button_pressed[MAXBUTTON];
935 Bitu i;
936 for (i=0; i<MAXBUTTON; i++) button_pressed[i]=false;
937 for (i=0; i<MAX_VJOY_BUTTONS; i++) {
938 if (virtual_joysticks[0].button_pressed[i])
939 button_pressed[i % button_wrap]=true;
940
941 }
942 for (i=0; i<emulated_buttons; i++) {
943 if (autofire && (button_pressed[i]))
944 JOYSTICK_Button(i>>1,i&1,(++button_autofire[i])&1);
945 else
946 JOYSTICK_Button(i>>1,i&1,button_pressed[i]);
947 }
948
949 JOYSTICK_Move_X(0,((float)virtual_joysticks[0].axis_pos[0])/32768.0f);
950 JOYSTICK_Move_Y(0,((float)virtual_joysticks[0].axis_pos[1])/32768.0f);
951 JOYSTICK_Move_X(1,((float)virtual_joysticks[0].axis_pos[2])/32768.0f);
952 JOYSTICK_Move_Y(1,((float)virtual_joysticks[0].axis_pos[3])/32768.0f);
953 }
954 };
955
956 class CFCSBindGroup : public CStickBindGroup {
957 public:
958 CFCSBindGroup(Bitu _stick,Bitu _emustick) : CStickBindGroup (_stick,_emustick){
959 emulated_axes=4;
960 emulated_buttons=4;
961 old_hat_position=0;
962 emulated_hats=1;
963 if (button_wrapping_enabled) button_wrap=emulated_buttons;
964
965 axes_cap=emulated_axes;
966 if (axes_cap>axes) axes_cap=axes;
967 hats_cap=emulated_hats;
968 if (hats_cap>hats) hats_cap=hats;
969
970 JOYSTICK_Enable(1,true);
971 JOYSTICK_Move_Y(1,1.0);
972 }
973
974 bool CheckEvent(SDL_Event * event) {
975 SDL_JoyAxisEvent * jaxis = NULL;
976 SDL_JoyButtonEvent * jbutton = NULL;
977 SDL_JoyHatEvent * jhat = NULL;
978 Bitu but = 0;
979
980 switch(event->type) {
981 case SDL_JOYAXISMOTION:
982 jaxis = &event->jaxis;
983 if(jaxis->which == stick) {
984 if(jaxis->axis == 0)
985 JOYSTICK_Move_X(0,(float)(jaxis->value/32768.0));
986 else if(jaxis->axis == 1)
987 JOYSTICK_Move_Y(0,(float)(jaxis->value/32768.0));
988 else if(jaxis->axis == 2)
989 JOYSTICK_Move_X(1,(float)(jaxis->value/32768.0));
990 }
991 break;
992 case SDL_JOYHATMOTION:
993 jhat = &event->jhat;
994 if(jhat->which == stick) DecodeHatPosition(jhat->value);
995 break;
996 case SDL_JOYBUTTONDOWN:
997 case SDL_JOYBUTTONUP:
998 jbutton = &event->jbutton;
999 bool state;
1000 state=jbutton->type==SDL_JOYBUTTONDOWN;
1001 but = jbutton->button % emulated_buttons;
1002 if (jbutton->which == stick) {
1003 JOYSTICK_Button((but >> 1),(but & 1),state);
1004 }
1005 break;
1006 }
1007 return false;
1008 }
1009
1010 virtual void UpdateJoystick() {
1011 /* query SDL joystick and activate bindings */
1012 ActivateJoystickBoundEvents();
1013
1014 bool button_pressed[MAXBUTTON];
1015 Bitu i;
1016 for (i=0; i<MAXBUTTON; i++) button_pressed[i]=false;
1017 for (i=0; i<MAX_VJOY_BUTTONS; i++) {
1018 if (virtual_joysticks[0].button_pressed[i])
1019 button_pressed[i % button_wrap]=true;
1020
1021 }
1022 for (i=0; i<emulated_buttons; i++) {
1023 if (autofire && (button_pressed[i]))
1024 JOYSTICK_Button(i>>1,i&1,(++button_autofire[i])&1);
1025 else
1026 JOYSTICK_Button(i>>1,i&1,button_pressed[i]);
1027 }
1028
1029 JOYSTICK_Move_X(0,((float)virtual_joysticks[0].axis_pos[0])/32768.0f);
1030 JOYSTICK_Move_Y(0,((float)virtual_joysticks[0].axis_pos[1])/32768.0f);
1031 JOYSTICK_Move_X(1,((float)virtual_joysticks[0].axis_pos[2])/32768.0f);
1032
1033 Uint8 hat_pos=0;
1034 if (virtual_joysticks[0].hat_pressed[0]) hat_pos|=SDL_HAT_UP;
1035 else if (virtual_joysticks[0].hat_pressed[2]) hat_pos|=SDL_HAT_DOWN;
1036 if (virtual_joysticks[0].hat_pressed[3]) hat_pos|=SDL_HAT_LEFT;
1037 else if (virtual_joysticks[0].hat_pressed[1]) hat_pos|=SDL_HAT_RIGHT;
1038
1039 if (hat_pos!=old_hat_position) {
1040 DecodeHatPosition(hat_pos);
1041 old_hat_position=hat_pos;
1042 }
1043 }
1044
1045 private:
1046 Uint8 old_hat_position;
1047
1048 void DecodeHatPosition(Uint8 hat_pos) {
1049 switch(hat_pos) {
1050 case SDL_HAT_CENTERED:
1051 JOYSTICK_Move_Y(1,1.0);
1052 break;
1053 case SDL_HAT_UP:
1054 JOYSTICK_Move_Y(1,-1.0);
1055 break;
1056 case SDL_HAT_RIGHT:
1057 JOYSTICK_Move_Y(1,-0.5);
1058 break;
1059 case SDL_HAT_DOWN:
1060 JOYSTICK_Move_Y(1,0.0);
1061 break;
1062 case SDL_HAT_LEFT:
1063 JOYSTICK_Move_Y(1,0.5);
1064 break;
1065 case SDL_HAT_LEFTUP:
1066 if(JOYSTICK_GetMove_Y(1) < 0)
1067 JOYSTICK_Move_Y(1,0.5);
1068 else
1069 JOYSTICK_Move_Y(1,-1.0);
1070 break;
1071 case SDL_HAT_RIGHTUP:
1072 if(JOYSTICK_GetMove_Y(1) < -0.7)
1073 JOYSTICK_Move_Y(1,-0.5);
1074 else
1075 JOYSTICK_Move_Y(1,-1.0);
1076 break;
1077 case SDL_HAT_RIGHTDOWN:
1078 if(JOYSTICK_GetMove_Y(1) < -0.2)
1079 JOYSTICK_Move_Y(1,0.0);
1080 else
1081 JOYSTICK_Move_Y(1,-0.5);
1082 break;
1083 case SDL_HAT_LEFTDOWN:
1084 if(JOYSTICK_GetMove_Y(1) > 0.2)
1085 JOYSTICK_Move_Y(1,0.0);
1086 else
1087 JOYSTICK_Move_Y(1,0.5);
1088 break;
1089 }
1090 }
1091 };
1092
1093 class CCHBindGroup : public CStickBindGroup {
1094 public:
1095 CCHBindGroup(Bitu _stick,Bitu _emustick) : CStickBindGroup (_stick,_emustick){
1096 emulated_axes=4;
1097 emulated_buttons=6;
1098 emulated_hats=1;
1099 if (button_wrapping_enabled) button_wrap=emulated_buttons;
1100
1101 axes_cap=emulated_axes;
1102 if (axes_cap>axes) axes_cap=axes;
1103 hats_cap=emulated_hats;
1104 if (hats_cap>hats) hats_cap=hats;
1105
1106 JOYSTICK_Enable(1,true);
1107 button_state=0;
1108 }
1109
1110 bool CheckEvent(SDL_Event * event) {
1111 SDL_JoyAxisEvent * jaxis = NULL;
1112 SDL_JoyButtonEvent * jbutton = NULL;
1113 SDL_JoyHatEvent * jhat = NULL;
1114 Bitu but = 0;
1115 static unsigned const button_magic[6]={0x02,0x04,0x10,0x100,0x20,0x200};
1116 static unsigned const hat_magic[2][5]={{0x8888,0x8000,0x800,0x80,0x08},
1117 {0x5440,0x4000,0x400,0x40,0x1000}};
1118 switch(event->type) {
1119 case SDL_JOYAXISMOTION:
1120 jaxis = &event->jaxis;
1121 if(jaxis->which == stick && jaxis->axis < 4) {
1122 if(jaxis->axis & 1)
1123 JOYSTICK_Move_Y(jaxis->axis>>1 & 1,(float)(jaxis->value/32768.0));
1124 else
1125 JOYSTICK_Move_X(jaxis->axis>>1 & 1,(float)(jaxis->value/32768.0));
1126 }
1127 break;
1128 case SDL_JOYHATMOTION:
1129 jhat = &event->jhat;
1130 if(jhat->which == stick && jhat->hat < 2) {
1131 if(jhat->value == SDL_HAT_CENTERED)
1132 button_state&=~hat_magic[jhat->hat][0];
1133 if(jhat->value & SDL_HAT_UP)
1134 button_state|=hat_magic[jhat->hat][1];
1135 if(jhat->value & SDL_HAT_RIGHT)
1136 button_state|=hat_magic[jhat->hat][2];
1137 if(jhat->value & SDL_HAT_DOWN)
1138 button_state|=hat_magic[jhat->hat][3];
1139 if(jhat->value & SDL_HAT_LEFT)
1140 button_state|=hat_magic[jhat->hat][4];
1141 }
1142 break;
1143 case SDL_JOYBUTTONDOWN:
1144 jbutton = &event->jbutton;
1145 but = jbutton->button % emulated_buttons;
1146 if (jbutton->which == stick)
1147 button_state|=button_magic[but];
1148 break;
1149 case SDL_JOYBUTTONUP:
1150 jbutton = &event->jbutton;
1151 but = jbutton->button % emulated_buttons;
1152 if (jbutton->which == stick)
1153 button_state&=~button_magic[but];
1154 break;
1155 }
1156
1157 unsigned i;
1158 Bit16u j;
1159 j=button_state;
1160 for(i=0;i<16;i++) if (j & 1) break; else j>>=1;
1161 JOYSTICK_Button(0,0,i&1);
1162 JOYSTICK_Button(0,1,(i>>1)&1);
1163 JOYSTICK_Button(1,0,(i>>2)&1);
1164 JOYSTICK_Button(1,1,(i>>3)&1);
1165 return false;
1166 }
1167
1168 void UpdateJoystick() {
1169 static unsigned const button_priority[6]={7,11,13,14,5,6};
1170 static unsigned const hat_priority[2][4]={{0,1,2,3},{8,9,10,12}};
1171
1172 /* query SDL joystick and activate bindings */
1173 ActivateJoystickBoundEvents();
1174
1175 JOYSTICK_Move_X(0,((float)virtual_joysticks[0].axis_pos[0])/32768.0f);
1176 JOYSTICK_Move_Y(0,((float)virtual_joysticks[0].axis_pos[1])/32768.0f);
1177 JOYSTICK_Move_X(1,((float)virtual_joysticks[0].axis_pos[2])/32768.0f);
1178 JOYSTICK_Move_Y(1,((float)virtual_joysticks[0].axis_pos[3])/32768.0f);
1179
1180 Bitu bt_state=15;
1181
1182 Bitu i;
1183 for (i=0; i<(hats<2?hats:2); i++) {
1184 Uint8 hat_pos=0;
1185 if (virtual_joysticks[0].hat_pressed[(i<<2)+0]) hat_pos|=SDL_HAT_UP;
1186 else if (virtual_joysticks[0].hat_pressed[(i<<2)+2]) hat_pos|=SDL_HAT_DOWN;
1187 if (virtual_joysticks[0].hat_pressed[(i<<2)+3]) hat_pos|=SDL_HAT_LEFT;
1188 else if (virtual_joysticks[0].hat_pressed[(i<<2)+1]) hat_pos|=SDL_HAT_RIGHT;
1189
1190 if (hat_pos & SDL_HAT_UP)
1191 if (bt_state>hat_priority[i][0]) bt_state=hat_priority[i][0];
1192 if (hat_pos & SDL_HAT_DOWN)
1193 if (bt_state>hat_priority[i][1]) bt_state=hat_priority[i][1];
1194 if (hat_pos & SDL_HAT_RIGHT)
1195 if (bt_state>hat_priority[i][2]) bt_state=hat_priority[i][2];
1196 if (hat_pos & SDL_HAT_LEFT)
1197 if (bt_state>hat_priority[i][3]) bt_state=hat_priority[i][3];
1198 }
1199
1200 bool button_pressed[MAXBUTTON];
1201 for (i=0; i<MAXBUTTON; i++) button_pressed[i]=false;
1202 for (i=0; i<MAX_VJOY_BUTTONS; i++) {
1203 if (virtual_joysticks[0].button_pressed[i])
1204 button_pressed[i % button_wrap]=true;
1205
1206 }
1207 for (i=0; i<6; i++) {
1208 if ((button_pressed[i]) && (bt_state>button_priority[i]))
1209 bt_state=button_priority[i];
1210 }
1211
1212 if (bt_state>15) bt_state=15;
1213 JOYSTICK_Button(0,0,(bt_state&8)==0);
1214 JOYSTICK_Button(0,1,(bt_state&4)==0);
1215 JOYSTICK_Button(1,0,(bt_state&2)==0);
1216 JOYSTICK_Button(1,1,(bt_state&1)==0);
1217 }
1218
1219 protected:
1220 Bit16u button_state;
1221 };
1222
1223 static struct CMapper {
1224 SDL_Surface * surface;
1225 SDL_Surface * draw_surface;
1226 bool exit;
1227 CEvent * aevent; //Active Event
1228 CBind * abind; //Active Bind
1229 CBindList_it abindit; //Location of active bind in list
1230 bool redraw;
1231 bool addbind;
1232 Bitu mods;
1233 struct {
1234 Bitu num_groups,num;
1235 CStickBindGroup * stick[MAXSTICKS];
1236 } sticks;
1237 std::string filename;
1238 } mapper;
1239
1240 void CBindGroup::ActivateBindList(CBindList * list,Bits value,bool ev_trigger) {
1241 Bitu validmod=0;
1242 CBindList_it it;
1243 for (it=list->begin();it!=list->end();it++) {
1244 if (((*it)->mods & mapper.mods) == (*it)->mods) {
1245 if (validmod<(*it)->mods) validmod=(*it)->mods;
1246 }
1247 }
1248 for (it=list->begin();it!=list->end();it++) {
1249 /*BUG:CRASH if keymapper key is removed*/
1250 if (validmod==(*it)->mods) (*it)->ActivateBind(value,ev_trigger);
1251 }
1252 }
1253
1254 void CBindGroup::DeactivateBindList(CBindList * list,bool ev_trigger) {
1255 CBindList_it it;
1256 for (it=list->begin();it!=list->end();it++) {
1257 (*it)->DeActivateBind(ev_trigger);
1258 }
1259 }
1260
1261 static void DrawText(Bitu x,Bitu y,const char * text,Bit8u color) {
1262 Bit8u * draw=((Bit8u *)mapper.surface->pixels)+(y*mapper.surface->pitch)+x;
1263 while (*text) {
1264 Bit8u * font=&int10_font_14[(*text)*14];
1265 Bitu i,j;Bit8u * draw_line=draw;
1266 for (i=0;i<14;i++) {
1267 Bit8u map=*font++;
1268 for (j=0;j<8;j++) {
1269 if (map & 0x80) *(draw_line+j)=color;
1270 else *(draw_line+j)=CLR_BLACK;
1271 map<<=1;
1272 }
1273 draw_line+=mapper.surface->pitch;
1274 }
1275 text++;draw+=8;
1276 }
1277 }
1278
1279 class CButton {
1280 public:
1281 virtual ~CButton(){};
1282 CButton(Bitu _x,Bitu _y,Bitu _dx,Bitu _dy) {
1283 x=_x;y=_y;dx=_dx;dy=_dy;
1284 buttons.push_back(this);
1285 color=CLR_WHITE;
1286 enabled=true;
1287 }
1288 virtual void Draw(void) {
1289 if (!enabled) return;
1290 Bit8u * point=((Bit8u *)mapper.surface->pixels)+(y*mapper.surface->pitch)+x;
1291 for (Bitu lines=0;lines<dy;lines++) {
1292 if (lines==0 || lines==(dy-1)) {
1293 for (Bitu cols=0;cols<dx;cols++) *(point+cols)=color;
1294 } else {
1295 *point=color;*(point+dx-1)=color;
1296 }
1297 point+=mapper.surface->pitch;
1298 }
1299 }
1300 virtual bool OnTop(Bitu _x,Bitu _y) {
1301 return ( enabled && (_x>=x) && (_x<x+dx) && (_y>=y) && (_y<y+dy));
1302 }
1303 virtual void Click(void) {}
1304 void Enable(bool yes) {
1305 enabled=yes;
1306 mapper.redraw=true;
1307 }
1308 void SetColor(Bit8u _col) { color=_col; }
1309 protected:
1310 Bitu x,y,dx,dy;
1311 Bit8u color;
1312 bool enabled;
1313 };
1314
1315 class CTextButton : public CButton {
1316 public:
1317 CTextButton(Bitu _x,Bitu _y,Bitu _dx,Bitu _dy,const char * _text) : CButton(_x,_y,_dx,_dy) { text=_text;}
1318 void Draw(void) {
1319 if (!enabled) return;
1320 CButton::Draw();
1321 DrawText(x+2,y+2,text,color);
1322 }
1323 protected:
1324 const char * text;
1325 };
1326
1327 class CEventButton;
1328 static CEventButton * last_clicked = NULL;
1329
1330 class CEventButton : public CTextButton {
1331 public:
1332 CEventButton(Bitu _x,Bitu _y,Bitu _dx,Bitu _dy,const char * _text,CEvent * _event)
1333 : CTextButton(_x,_y,_dx,_dy,_text) {
1334 event=_event;
1335 }
1336 void Click(void) {
1337 if (last_clicked) last_clicked->SetColor(CLR_WHITE);
1338 this->SetColor(CLR_GREEN);
1339 SetActiveEvent(event);
1340 last_clicked=this;
1341 }
1342 protected:
1343 CEvent * event;
1344 };
1345
1346 class CCaptionButton : public CButton {
1347 public:
1348 CCaptionButton(Bitu _x,Bitu _y,Bitu _dx,Bitu _dy) : CButton(_x,_y,_dx,_dy){
1349 caption[0]=0;
1350 }
1351 void Change(const char * format,...) GCC_ATTRIBUTE(__format__(__printf__,2,3));
1352
1353 void Draw(void) {
1354 if (!enabled) return;
1355 DrawText(x+2,y+2,caption,color);
1356 }
1357 protected:
1358 char caption[128];
1359 };
1360
1361 void CCaptionButton::Change(const char * format,...) {
1362 va_list msg;
1363 va_start(msg,format);
1364 vsprintf(caption,format,msg);
1365 va_end(msg);
1366 mapper.redraw=true;
1367 }
1368
1369 static void change_action_text(const char* text,Bit8u col);
1370
1371 static void MAPPER_SaveBinds(void);
1372 class CBindButton : public CTextButton {
1373 public:
1374 CBindButton(Bitu _x,Bitu _y,Bitu _dx,Bitu _dy,const char * _text,BB_Types _type)
1375 : CTextButton(_x,_y,_dx,_dy,_text) {
1376 type=_type;
1377 }
1378 void Click(void) {
1379 switch (type) {
1380 case BB_Add:
1381 mapper.addbind=true;
1382 SetActiveBind(0);
1383 change_action_text("Press a key/joystick button or move the joystick.",CLR_RED);
1384 break;
1385 case BB_Del:
1386 if (mapper.abindit!=mapper.aevent->bindlist.end()) {
1387 delete (*mapper.abindit);
1388 mapper.abindit=mapper.aevent->bindlist.erase(mapper.abindit);
1389 if (mapper.abindit==mapper.aevent->bindlist.end())
1390 mapper.abindit=mapper.aevent->bindlist.begin();
1391 }
1392 if (mapper.abindit!=mapper.aevent->bindlist.end()) SetActiveBind(*(mapper.abindit));
1393 else SetActiveBind(0);
1394 break;
1395 case BB_Next:
1396 if (mapper.abindit!=mapper.aevent->bindlist.end())
1397 mapper.abindit++;
1398 if (mapper.abindit==mapper.aevent->bindlist.end())
1399 mapper.abindit=mapper.aevent->bindlist.begin();
1400 SetActiveBind(*(mapper.abindit));
1401 break;
1402 case BB_Save:
1403 MAPPER_SaveBinds();
1404 break;
1405 case BB_Exit:
1406 mapper.exit=true;
1407 break;
1408 }
1409 }
1410 protected:
1411 BB_Types type;
1412 };
1413
1414 class CCheckButton : public CTextButton {
1415 public:
1416 CCheckButton(Bitu _x,Bitu _y,Bitu _dx,Bitu _dy,const char * _text,BC_Types _type)
1417 : CTextButton(_x,_y,_dx,_dy,_text) {
1418 type=_type;
1419 }
1420 void Draw(void) {
1421 if (!enabled) return;
1422 bool checked=false;
1423 switch (type) {
1424 case BC_Mod1:
1425 checked=(mapper.abind->mods&BMOD_Mod1)>0;
1426 break;
1427 case BC_Mod2:
1428 checked=(mapper.abind->mods&BMOD_Mod2)>0;
1429 break;
1430 case BC_Mod3:
1431 checked=(mapper.abind->mods&BMOD_Mod3)>0;
1432 break;
1433 case BC_Hold:
1434 checked=(mapper.abind->flags&BFLG_Hold)>0;
1435 break;
1436 }
1437 if (checked) {
1438 Bit8u * point=((Bit8u *)mapper.surface->pixels)+((y+2)*mapper.surface->pitch)+x+dx-dy+2;
1439 for (Bitu lines=0;lines<(dy-4);lines++) {
1440 memset(point,color,dy-4);
1441 point+=mapper.surface->pitch;
1442 }
1443 }
1444 CTextButton::Draw();
1445 }
1446 void Click(void) {
1447 switch (type) {
1448 case BC_Mod1:
1449 mapper.abind->mods^=BMOD_Mod1;
1450 break;
1451 case BC_Mod2:
1452 mapper.abind->mods^=BMOD_Mod2;
1453 break;
1454 case BC_Mod3:
1455 mapper.abind->mods^=BMOD_Mod3;
1456 break;
1457 case BC_Hold:
1458 mapper.abind->flags^=BFLG_Hold;
1459 break;
1460 }
1461 mapper.redraw=true;
1462 }
1463 protected:
1464 BC_Types type;
1465 };
1466
1467 class CKeyEvent : public CTriggeredEvent {
1468 public:
1469 CKeyEvent(char const * const _entry,KBD_KEYS _key) : CTriggeredEvent(_entry) {
1470 key=_key;
1471 }
1472 void Active(bool yesno) {
1473 KEYBOARD_AddKey(key,yesno);
1474 };
1475 KBD_KEYS key;
1476 };
1477
1478 class CJAxisEvent : public CContinuousEvent {
1479 public:
1480 CJAxisEvent(char const * const _entry,Bitu _stick,Bitu _axis,bool _positive,CJAxisEvent * _opposite_axis) : CContinuousEvent(_entry) {
1481 stick=_stick;
1482 axis=_axis;
1483 positive=_positive;
1484 opposite_axis=_opposite_axis;
1485 if (_opposite_axis) {
1486 _opposite_axis->SetOppositeAxis(this);
1487 }
1488 }
1489 void Active(bool /*moved*/) {
1490 virtual_joysticks[stick].axis_pos[axis]=(Bit16s)(GetValue()*(positive?1:-1));
1491 }
1492 virtual Bitu GetActivityCount(void) {
1493 return activity|opposite_axis->activity;
1494 }
1495 virtual void RepostActivity(void) {
1496 /* caring for joystick movement into the opposite direction */
1497 opposite_axis->Active(true);
1498 }
1499 protected:
1500 void SetOppositeAxis(CJAxisEvent * _opposite_axis) {
1501 opposite_axis=_opposite_axis;
1502 }
1503 Bitu stick,axis;
1504 bool positive;
1505 CJAxisEvent * opposite_axis;
1506 };
1507
1508 class CJButtonEvent : public CTriggeredEvent {
1509 public:
1510 CJButtonEvent(char const * const _entry,Bitu _stick,Bitu _button) : CTriggeredEvent(_entry) {
1511 stick=_stick;
1512 button=_button;
1513 }
1514 void Active(bool pressed) {
1515 virtual_joysticks[stick].button_pressed[button]=pressed;
1516 }
1517 protected:
1518 Bitu stick,button;
1519 };
1520
1521 class CJHatEvent : public CTriggeredEvent {
1522 public:
1523 CJHatEvent(char const * const _entry,Bitu _stick,Bitu _hat,Bitu _dir) : CTriggeredEvent(_entry) {
1524 stick=_stick;
1525 hat=_hat;
1526 dir=_dir;
1527 }
1528 void Active(bool pressed) {
1529 virtual_joysticks[stick].hat_pressed[(hat<<2)+dir]=pressed;
1530 }
1531 protected:
1532 Bitu stick,hat,dir;
1533 };
1534
1535
1536 class CModEvent : public CTriggeredEvent {
1537 public:
1538 CModEvent(char const * const _entry,Bitu _wmod) : CTriggeredEvent(_entry) {
1539 wmod=_wmod;
1540 }
1541 void Active(bool yesno) {
1542 if (yesno) mapper.mods|=(1 << (wmod-1));
1543 else mapper.mods&=~(1 << (wmod-1));
1544 };
1545 protected:
1546 Bitu wmod;
1547 };
1548
1549 class CHandlerEvent : public CTriggeredEvent {
1550 public:
1551 CHandlerEvent(char const * const _entry,MAPPER_Handler * _handler,MapKeys _key,Bitu _mod,char const * const _buttonname) : CTriggeredEvent(_entry) {
1552 handler=_handler;
1553 defmod=_mod;
1554 defkey=_key;
1555 buttonname=_buttonname;
1556 handlergroup.push_back(this);
1557 }
1558 void Active(bool yesno) {
1559 (*handler)(yesno);
1560 };
1561 const char * ButtonName(void) {
1562 return buttonname;
1563 }
1564 void MakeDefaultBind(char * buf) {
1565 Bitu key=0;
1566 switch (defkey) {
1567 case MK_f1:case MK_f2:case MK_f3:case MK_f4:
1568 case MK_f5:case MK_f6:case MK_f7:case MK_f8:
1569 case MK_f9:case MK_f10:case MK_f11:case MK_f12:
1570 key=SDLK_F1+(defkey-MK_f1);
1571 break;
1572 case MK_return:
1573 key=SDLK_RETURN;
1574 break;
1575 case MK_kpminus:
1576 key=SDLK_KP_MINUS;
1577 break;
1578 case MK_scrolllock:
1579 key=SDLK_SCROLLOCK;
1580 break;
1581 case MK_pause:
1582 key=SDLK_PAUSE;
1583 break;
1584 case MK_printscreen:
1585 key=SDLK_PRINT;
1586 break;
1587 }
1588 sprintf(buf,"%s \"key %d%s%s%s\"",
1589 entry,
1590 key,
1591 defmod & 1 ? " mod1" : "",
1592 defmod & 2 ? " mod2" : "",
1593 defmod & 4 ? " mod3" : ""
1594 );
1595 }
1596 protected:
1597 MapKeys defkey;
1598 Bitu defmod;
1599 MAPPER_Handler * handler;
1600 public:
1601 const char * buttonname;
1602 };
1603
1604
1605 static struct {
1606 CCaptionButton * event_title;
1607 CCaptionButton * bind_title;
1608 CCaptionButton * selected;
1609 CCaptionButton * action;
1610 CBindButton * save;
1611 CBindButton * exit;
1612 CBindButton * add;
1613 CBindButton * del;
1614 CBindButton * next;
1615 CCheckButton * mod1,* mod2,* mod3,* hold;
1616 } bind_but;
1617
1618
1619 static void change_action_text(const char* text,Bit8u col) {
1620 bind_but.action->Change(text,"");
1621 bind_but.action->SetColor(col);
1622 }
1623
1624
1625 static void SetActiveBind(CBind * _bind) {
1626 mapper.abind=_bind;
1627 if (_bind) {
1628 bind_but.bind_title->Enable(true);
1629 char buf[256];_bind->BindName(buf);
1630 bind_but.bind_title->Change("BIND:%s",buf);
1631 bind_but.del->Enable(true);
1632 bind_but.next->Enable(true);
1633 bind_but.mod1->Enable(true);
1634 bind_but.mod2->Enable(true);
1635 bind_but.mod3->Enable(true);
1636 bind_but.hold->Enable(true);
1637 } else {
1638 bind_but.bind_title->Enable(false);
1639 bind_but.del->Enable(false);
1640 bind_but.next->Enable(false);
1641 bind_but.mod1->Enable(false);
1642 bind_but.mod2->Enable(false);
1643 bind_but.mod3->Enable(false);
1644 bind_but.hold->Enable(false);
1645 }
1646 }
1647
1648 static void SetActiveEvent(CEvent * event) {
1649 mapper.aevent=event;
1650 mapper.redraw=true;
1651 mapper.addbind=false;
1652 bind_but.event_title->Change("EVENT:%s",event ? event->GetName(): "none");
1653 if (!event) {
1654 change_action_text("Select an event to change.",CLR_WHITE);
1655 bind_but.add->Enable(false);
1656 SetActiveBind(0);
1657 } else {
1658 change_action_text("Select a different event or hit the Add/Del/Next buttons.",CLR_WHITE);
1659 mapper.abindit=event->bindlist.begin();
1660 if (mapper.abindit!=event->bindlist.end()) {
1661 SetActiveBind(*(mapper.abindit));
1662 } else SetActiveBind(0);
1663 bind_but.add->Enable(true);
1664 }
1665 }
1666
1667 static void DrawButtons(void) {
1668 SDL_FillRect(mapper.surface,0,CLR_BLACK);
1669 SDL_LockSurface(mapper.surface);
1670 for (CButton_it but_it = buttons.begin();but_it!=buttons.end();but_it++) {
1671 (*but_it)->Draw();
1672 }
1673 SDL_UnlockSurface(mapper.surface);
1674 SDL_Flip(mapper.surface);
1675 }
1676
1677 static CKeyEvent * AddKeyButtonEvent(Bitu x,Bitu y,Bitu dx,Bitu dy,char const * const title,char const * const entry,KBD_KEYS key) {
1678 char buf[64];
1679 strcpy(buf,"key_");
1680 strcat(buf,entry);
1681 CKeyEvent * event=new CKeyEvent(buf,key);
1682 new CEventButton(x,y,dx,dy,title,event);
1683 return event;
1684 }
1685
1686 static CJAxisEvent * AddJAxisButton(Bitu x,Bitu y,Bitu dx,Bitu dy,char const * const title,Bitu stick,Bitu axis,bool positive,CJAxisEvent * opposite_axis) {
1687 char buf[64];
1688 sprintf(buf,"jaxis_%d_%d%s",stick,axis,positive ? "+" : "-");
1689 CJAxisEvent * event=new CJAxisEvent(buf,stick,axis,positive,opposite_axis);
1690 new CEventButton(x,y,dx,dy,title,event);
1691 return event;
1692 }
1693 static CJAxisEvent * AddJAxisButton_hidden(Bitu stick,Bitu axis,bool positive,CJAxisEvent * opposite_axis) {
1694 char buf[64];
1695 sprintf(buf,"jaxis_%d_%d%s",stick,axis,positive ? "+" : "-");
1696 return new CJAxisEvent(buf,stick,axis,positive,opposite_axis);
1697 }
1698
1699 static void AddJButtonButton(Bitu x,Bitu y,Bitu dx,Bitu dy,char const * const title,Bitu stick,Bitu button) {
1700 char buf[64];
1701 sprintf(buf,"jbutton_%d_%d",stick,button);
1702 CJButtonEvent * event=new CJButtonEvent(buf,stick,button);
1703 new CEventButton(x,y,dx,dy,title,event);
1704 }
1705 static void AddJButtonButton_hidden(Bitu stick,Bitu button) {
1706 char buf[64];
1707 sprintf(buf,"jbutton_%d_%d",stick,button);
1708 new CJButtonEvent(buf,stick,button);
1709 }
1710
1711 static void AddJHatButton(Bitu x,Bitu y,Bitu dx,Bitu dy,char const * const title,Bitu _stick,Bitu _hat,Bitu _dir) {
1712 char buf[64];
1713 sprintf(buf,"jhat_%d_%d_%d",_stick,_hat,_dir);
1714 CJHatEvent * event=new CJHatEvent(buf,_stick,_hat,_dir);
1715 new CEventButton(x,y,dx,dy,title,event);
1716 }
1717
1718 static void AddModButton(Bitu x,Bitu y,Bitu dx,Bitu dy,char const * const title,Bitu _mod) {
1719 char buf[64];
1720 sprintf(buf,"mod_%d",_mod);
1721 CModEvent * event=new CModEvent(buf,_mod);
1722 new CEventButton(x,y,dx,dy,title,event);
1723 }
1724
1725 struct KeyBlock {
1726 const char * title;
1727 const char * entry;
1728 KBD_KEYS key;
1729 };
1730 static KeyBlock combo_f[12]={
1731 {"F1","f1",KBD_f1}, {"F2","f2",KBD_f2}, {"F3","f3",KBD_f3},
1732 {"F4","f4",KBD_f4}, {"F5","f5",KBD_f5}, {"F6","f6",KBD_f6},
1733 {"F7","f7",KBD_f7}, {"F8","f8",KBD_f8}, {"F9","f9",KBD_f9},
1734 {"F10","f10",KBD_f10}, {"F11","f11",KBD_f11}, {"F12","f12",KBD_f12},
1735 };
1736
1737 static KeyBlock combo_1[14]={
1738 {"`~","grave",KBD_grave}, {"1!","1",KBD_1}, {"2@","2",KBD_2},
1739 {"3#","3",KBD_3}, {"4$","4",KBD_4}, {"5%","5",KBD_5},
1740 {"6^","6",KBD_6}, {"7&","7",KBD_7}, {"8*","8",KBD_8},
1741 {"9(","9",KBD_9}, {"0)","0",KBD_0}, {"-_","minus",KBD_minus},
1742 {"=+","equals",KBD_equals}, {"\x1B","bspace",KBD_backspace},
1743 };
1744
1745 static KeyBlock combo_2[12]={
1746 {"q","q",KBD_q}, {"w","w",KBD_w}, {"e","e",KBD_e},
1747 {"r","r",KBD_r}, {"t","t",KBD_t}, {"y","y",KBD_y},
1748 {"u","u",KBD_u}, {"i","i",KBD_i}, {"o","o",KBD_o},
1749 {"p","p",KBD_p}, {"[","lbracket",KBD_leftbracket},
1750 {"]","rbracket",KBD_rightbracket},
1751 };
1752
1753 static KeyBlock combo_3[12]={
1754 {"a","a",KBD_a}, {"s","s",KBD_s}, {"d","d",KBD_d},
1755 {"f","f",KBD_f}, {"g","g",KBD_g}, {"h","h",KBD_h},
1756 {"j","j",KBD_j}, {"k","k",KBD_k}, {"l","l",KBD_l},
1757 {";","semicolon",KBD_semicolon}, {"'","quote",KBD_quote},
1758 {"\\","backslash",KBD_backslash},
1759 };
1760
1761 static KeyBlock combo_4[11]={
1762 {"<","lessthan",KBD_extra_lt_gt},
1763 {"z","z",KBD_z}, {"x","x",KBD_x}, {"c","c",KBD_c},
1764 {"v","v",KBD_v}, {"b","b",KBD_b}, {"n","n",KBD_n},
1765 {"m","m",KBD_m}, {",","comma",KBD_comma},
1766 {".","period",KBD_period}, {"/","slash",KBD_slash},
1767 };
1768
1769 static CKeyEvent * caps_lock_event=NULL;
1770 static CKeyEvent * num_lock_event=NULL;
1771
1772 static void CreateLayout(void) {
1773 Bitu i;
1774 /* Create the buttons for the Keyboard */
1775 #define BW 28
1776 #define BH 20
1777 #define DX 5
1778 #define PX(_X_) ((_X_)*BW + DX)
1779 #define PY(_Y_) (10+(_Y_)*BH)
1780 AddKeyButtonEvent(PX(0),PY(0),BW,BH,"ESC","esc",KBD_esc);
1781 for (i=0;i<12;i++) AddKeyButtonEvent(PX(2+i),PY(0),BW,BH,combo_f[i].title,combo_f[i].entry,combo_f[i].key);
1782 for (i=0;i<14;i++) AddKeyButtonEvent(PX( i),PY(1),BW,BH,combo_1[i].title,combo_1[i].entry,combo_1[i].key);
1783
1784 AddKeyButtonEvent(PX(0),PY(2),BW*2,BH,"TAB","tab",KBD_tab);
1785 for (i=0;i<12;i++) AddKeyButtonEvent(PX(2+i),PY(2),BW,BH,combo_2[i].title,combo_2[i].entry,combo_2[i].key);
1786
1787 AddKeyButtonEvent(PX(14),PY(2),BW*2,BH*2,"ENTER","enter",KBD_enter);
1788
1789 caps_lock_event=AddKeyButtonEvent(PX(0),PY(3),BW*2,BH,"CLCK","capslock",KBD_capslock);
1790 for (i=0;i<12;i++) AddKeyButtonEvent(PX(2+i),PY(3),BW,BH,combo_3[i].title,combo_3[i].entry,combo_3[i].key);
1791
1792 AddKeyButtonEvent(PX(0),PY(4),BW*2,BH,"SHIFT","lshift",KBD_leftshift);
1793 for (i=0;i<11;i++) AddKeyButtonEvent(PX(2+i),PY(4),BW,BH,combo_4[i].title,combo_4[i].entry,combo_4[i].key);
1794 AddKeyButtonEvent(PX(13),PY(4),BW*3,BH,"SHIFT","rshift",KBD_rightshift);
1795
1796 /* Last Row */
1797 AddKeyButtonEvent(PX(0) ,PY(5),BW*2,BH,"CTRL","lctrl",KBD_leftctrl);
1798 AddKeyButtonEvent(PX(3) ,PY(5),BW*2,BH,"ALT","lalt",KBD_leftalt);
1799 AddKeyButtonEvent(PX(5) ,PY(5),BW*6,BH,"SPACE","space",KBD_space);
1800 AddKeyButtonEvent(PX(11),PY(5),BW*2,BH,"ALT","ralt",KBD_rightalt);
1801 AddKeyButtonEvent(PX(14),PY(5),BW*2,BH,"CTRL","rctrl",KBD_rightctrl);
1802
1803 /* Arrow Keys */
1804 #define XO 17
1805 #define YO 0
1806
1807 AddKeyButtonEvent(PX(XO+0),PY(YO),BW,BH,"PRT","printscreen",KBD_printscreen);
1808 AddKeyButtonEvent(PX(XO+1),PY(YO),BW,BH,"SCL","scrolllock",KBD_scrolllock);
1809 AddKeyButtonEvent(PX(XO+2),PY(YO),BW,BH,"PAU","pause",KBD_pause);
1810 AddKeyButtonEvent(PX(XO+0),PY(YO+1),BW,BH,"INS","insert",KBD_insert);
1811 AddKeyButtonEvent(PX(XO+1),PY(YO+1),BW,BH,"HOM","home",KBD_home);
1812 AddKeyButtonEvent(PX(XO+2),PY(YO+1),BW,BH,"PUP","pageup",KBD_pageup);
1813 AddKeyButtonEvent(PX(XO+0),PY(YO+2),BW,BH,"DEL","delete",KBD_delete);
1814 AddKeyButtonEvent(PX(XO+1),PY(YO+2),BW,BH,"END","end",KBD_end);
1815 AddKeyButtonEvent(PX(XO+2),PY(YO+2),BW,BH,"PDN","pagedown",KBD_pagedown);
1816 AddKeyButtonEvent(PX(XO+1),PY(YO+4),BW,BH,"\x18","up",KBD_up);
1817 AddKeyButtonEvent(PX(XO+0),PY(YO+5),BW,BH,"\x1B","left",KBD_left);
1818 AddKeyButtonEvent(PX(XO+1),PY(YO+5),BW,BH,"\x19","down",KBD_down);
1819 AddKeyButtonEvent(PX(XO+2),PY(YO+5),BW,BH,"\x1A","right",KBD_right);
1820 #undef XO
1821 #undef YO
1822 #define XO 0
1823 #define YO 7
1824 /* Numeric KeyPad */
1825 num_lock_event=AddKeyButtonEvent(PX(XO),PY(YO),BW,BH,"NUM","numlock",KBD_numlock);
1826 AddKeyButtonEvent(PX(XO+1),PY(YO),BW,BH,"/","kp_divide",KBD_kpdivide);
1827 AddKeyButtonEvent(PX(XO+2),PY(YO),BW,BH,"*","kp_multiply",KBD_kpmultiply);
1828 AddKeyButtonEvent(PX(XO+3),PY(YO),BW,BH,"-","kp_minus",KBD_kpminus);
1829 AddKeyButtonEvent(PX(XO+0),PY(YO+1),BW,BH,"7","kp_7",KBD_kp7);
1830 AddKeyButtonEvent(PX(XO+1),PY(YO+1),BW,BH,"8","kp_8",KBD_kp8);
1831 AddKeyButtonEvent(PX(XO+2),PY(YO+1),BW,BH,"9","kp_9",KBD_kp9);
1832 AddKeyButtonEvent(PX(XO+3),PY(YO+1),BW,BH*2,"+","kp_plus",KBD_kpplus);
1833 AddKeyButtonEvent(PX(XO),PY(YO+2),BW,BH,"4","kp_4",KBD_kp4);
1834 AddKeyButtonEvent(PX(XO+1),PY(YO+2),BW,BH,"5","kp_5",KBD_kp5);
1835 AddKeyButtonEvent(PX(XO+2),PY(YO+2),BW,BH,"6","kp_6",KBD_kp6);
1836 AddKeyButtonEvent(PX(XO+0),PY(YO+3),BW,BH,"1","kp_1",KBD_kp1);
1837 AddKeyButtonEvent(PX(XO+1),PY(YO+3),BW,BH,"2","kp_2",KBD_kp2);
1838 AddKeyButtonEvent(PX(XO+2),PY(YO+3),BW,BH,"3","kp_3",KBD_kp3);
1839 AddKeyButtonEvent(PX(XO+3),PY(YO+3),BW,BH*2,"ENT","kp_enter",KBD_kpenter);
1840 AddKeyButtonEvent(PX(XO),PY(YO+4),BW*2,BH,"0","kp_0",KBD_kp0);
1841 AddKeyButtonEvent(PX(XO+2),PY(YO+4),BW,BH,".","kp_period",KBD_kpperiod);
1842 #undef XO
1843 #undef YO
1844 #define XO 10
1845 #define YO 8
1846 /* Joystick Buttons/Texts */
1847 /* Buttons 1+2 of 1st Joystick */
1848 AddJButtonButton(PX(XO),PY(YO),BW,BH,"1" ,0,0);
1849 AddJButtonButton(PX(XO+2),PY(YO),BW,BH,"2" ,0,1);
1850 /* Axes 1+2 (X+Y) of 1st Joystick */
1851 CJAxisEvent * cjaxis=AddJAxisButton(PX(XO+1),PY(YO),BW,BH,"Y-",0,1,false,NULL);
1852 AddJAxisButton (PX(XO+1),PY(YO+1),BW,BH,"Y+",0,1,true,cjaxis);
1853 cjaxis=AddJAxisButton (PX(XO),PY(YO+1),BW,BH,"X-",0,0,false,NULL);
1854 AddJAxisButton (PX(XO+2),PY(YO+1),BW,BH,"X+",0,0,true,cjaxis);
1855
1856 if (joytype==JOY_2AXIS) {
1857 /* Buttons 1+2 of 2nd Joystick */
1858 AddJButtonButton(PX(XO+4),PY(YO),BW,BH,"1" ,1,0);
1859 AddJButtonButton(PX(XO+4+2),PY(YO),BW,BH,"2" ,1,1);
1860 /* Buttons 3+4 of 1st Joystick, not accessible */
1861 AddJButtonButton_hidden(0,2);
1862 AddJButtonButton_hidden(0,3);
1863
1864 /* Axes 1+2 (X+Y) of 2nd Joystick */
1865 cjaxis= AddJAxisButton(PX(XO+4),PY(YO+1),BW,BH,"X-",1,0,false,NULL);
1866 AddJAxisButton(PX(XO+4+2),PY(YO+1),BW,BH,"X+",1,0,true,cjaxis);
1867 cjaxis= AddJAxisButton(PX(XO+4+1),PY(YO+0),BW,BH,"Y-",1,1,false,NULL);
1868 AddJAxisButton(PX(XO+4+1),PY(YO+1),BW,BH,"Y+",1,1,true,cjaxis);
1869 /* Axes 3+4 (X+Y) of 1st Joystick, not accessible */
1870 cjaxis= AddJAxisButton_hidden(0,2,false,NULL);
1871 AddJAxisButton_hidden(0,2,true,cjaxis);
1872 cjaxis= AddJAxisButton_hidden(0,3,false,NULL);
1873 AddJAxisButton_hidden(0,3,true,cjaxis);
1874 } else {
1875 /* Buttons 3+4 of 1st Joystick */
1876 AddJButtonButton(PX(XO+4),PY(YO),BW,BH,"3" ,0,2);
1877 AddJButtonButton(PX(XO+4+2),PY(YO),BW,BH,"4" ,0,3);
1878 /* Buttons 1+2 of 2nd Joystick, not accessible */
1879 AddJButtonButton_hidden(1,0);
1880 AddJButtonButton_hidden(1,1);
1881
1882 /* Axes 3+4 (X+Y) of 1st Joystick */
1883 cjaxis= AddJAxisButton(PX(XO+4),PY(YO+1),BW,BH,"X-",0,2,false,NULL);
1884 AddJAxisButton(PX(XO+4+2),PY(YO+1),BW,BH,"X+",0,2,true,cjaxis);
1885 cjaxis= AddJAxisButton(PX(XO+4+1),PY(YO+0),BW,BH,"Y-",0,3,false,NULL);
1886 AddJAxisButton(PX(XO+4+1),PY(YO+1),BW,BH,"Y+",0,3,true,cjaxis);
1887 /* Axes 1+2 (X+Y) of 2nd Joystick , not accessible*/
1888 cjaxis= AddJAxisButton_hidden(1,0,false,NULL);
1889 AddJAxisButton_hidden(1,0,true,cjaxis);
1890 cjaxis= AddJAxisButton_hidden(1,1,false,NULL);
1891 AddJAxisButton_hidden(1,1,true,cjaxis);
1892 }
1893
1894 if (joytype==JOY_CH) {
1895 /* Buttons 5+6 of 1st Joystick */
1896 AddJButtonButton(PX(XO+8),PY(YO),BW,BH,"5" ,0,4);
1897 AddJButtonButton(PX(XO+8+2),PY(YO),BW,BH,"6" ,0,5);
1898 } else {
1899 /* Buttons 5+6 of 1st Joystick, not accessible */
1900 AddJButtonButton_hidden(0,4);
1901 AddJButtonButton_hidden(0,5);
1902 }
1903
1904 /* Hat directions up, left, down, right */
1905 AddJHatButton(PX(XO+8+1),PY(YO),BW,BH,"UP",0,0,0);
1906 AddJHatButton(PX(XO+8+0),PY(YO+1),BW,BH,"LFT",0,0,3);
1907 AddJHatButton(PX(XO+8+1),PY(YO+1),BW,BH,"DWN",0,0,2);
1908 AddJHatButton(PX(XO+8+2),PY(YO+1),BW,BH,"RGT",0,0,1);
1909
1910 /* Labels for the joystick */
1911 if (joytype ==JOY_2AXIS) {
1912 new CTextButton(PX(XO+0),PY(YO-1),3*BW,20,"Joystick 1");
1913 new CTextButton(PX(XO+4),PY(YO-1),3*BW,20,"Joystick 2");
1914 new CTextButton(PX(XO+8),PY(YO-1),3*BW,20,"Disabled");
1915 } else if(joytype ==JOY_4AXIS || joytype == JOY_4AXIS_2) {
1916 new CTextButton(PX(XO+0),PY(YO-1),3*BW,20,"Axis 1/2");
1917 new CTextButton(PX(XO+4),PY(YO-1),3*BW,20,"Axis 3/4");
1918 new CTextButton(PX(XO+8),PY(YO-1),3*BW,20,"Disabled");
1919 } else if(joytype == JOY_CH) {
1920 new CTextButton(PX(XO+0),PY(YO-1),3*BW,20,"Axis 1/2");
1921 new CTextButton(PX(XO+4),PY(YO-1),3*BW,20,"Axis 3/4");
1922 new CTextButton(PX(XO+8),PY(YO-1),3*BW,20,"Hat/D-pad");
1923 } else if ( joytype==JOY_FCS) {
1924 new CTextButton(PX(XO+0),PY(YO-1),3*BW,20,"Axis 1/2");
1925 new CTextButton(PX(XO+4),PY(YO-1),3*BW,20,"Axis 3");
1926 new CTextButton(PX(XO+8),PY(YO-1),3*BW,20,"Hat/D-pad");
1927 } else if(joytype == JOY_NONE) {
1928 new CTextButton(PX(XO+0),PY(YO-1),3*BW,20,"Disabled");
1929 new CTextButton(PX(XO+4),PY(YO-1),3*BW,20,"Disabled");
1930 new CTextButton(PX(XO+8),PY(YO-1),3*BW,20,"Disabled");
1931 }
1932
1933
1934
1935 /* The modifier buttons */
1936 AddModButton(PX(0),PY(14),50,20,"Mod1",1);
1937 AddModButton(PX(2),PY(14),50,20,"Mod2",2);
1938 AddModButton(PX(4),PY(14),50,20,"Mod3",3);
1939 /* Create Handler buttons */
1940 Bitu xpos=3;Bitu ypos=11;
1941 for (CHandlerEventVector_it hit=handlergroup.begin();hit!=handlergroup.end();hit++) {
1942 new CEventButton(PX(xpos*3),PY(ypos),BW*3,BH,(*hit)->ButtonName(),(*hit));
1943 xpos++;
1944 if (xpos>6) {
1945 xpos=3;ypos++;
1946 }
1947 }
1948 /* Create some text buttons */
1949 // new CTextButton(PX(6),0,124,20,"Keyboard Layout");
1950 // new CTextButton(PX(17),0,124,20,"Joystick Layout");
1951
1952 bind_but.action=new CCaptionButton(180,330,0,0);
1953
1954 bind_but.event_title=new CCaptionButton(0,350,0,0);
1955 bind_but.bind_title=new CCaptionButton(0,365,0,0);
1956
1957 /* Create binding support buttons */
1958
1959 bind_but.mod1=new CCheckButton(20,410,60,20, "mod1",BC_Mod1);
1960 bind_but.mod2=new CCheckButton(20,432,60,20, "mod2",BC_Mod2);
1961 bind_but.mod3=new CCheckButton(20,454,60,20, "mod3",BC_Mod3);
1962 bind_but.hold=new CCheckButton(100,410,60,20,"hold",BC_Hold);
1963
1964 bind_but.next=new CBindButton(250,400,50,20,"Next",BB_Next);
1965
1966 bind_but.add=new CBindButton(250,380,50,20,"Add",BB_Add);
1967 bind_but.del=new CBindButton(300,380,50,20,"Del",BB_Del);
1968
1969 bind_but.save=new CBindButton(400,450,50,20,"Save",BB_Save);
1970 bind_but.exit=new CBindButton(450,450,50,20,"Exit",BB_Exit);
1971
1972 bind_but.bind_title->Change("Bind Title");
1973 }
1974
1975 static SDL_Color map_pal[5]={
1976 {0x00,0x00,0x00,0x00}, //0=black
1977 {0xff,0xff,0xff,0x00}, //1=white
1978 {0xff,0x00,0x00,0x00}, //2=red
1979 {0x10,0x30,0xff,0x00}, //3=blue
1980 {0x00,0xff,0x20,0x00} //4=green
1981 };
1982
1983 static void CreateStringBind(char * line) {
1984 line=trim(line);
1985 char * eventname=StripWord(line);
1986 CEvent * event;
1987 for (CEventVector_it ev_it=events.begin();ev_it!=events.end();ev_it++) {
1988 if (!strcasecmp((*ev_it)->GetName(),eventname)) {
1989 event=*ev_it;
1990 goto foundevent;
1991 }
1992 }
1993 LOG_MSG("Can't find matching event for %s",eventname);
1994 return ;
1995 foundevent:
1996 CBind * bind;
1997 for (char * bindline=StripWord(line);*bindline;bindline=StripWord(line)) {
1998 for (CBindGroup_it it=bindgroups.begin();it!=bindgroups.end();it++) {
1999 bind=(*it)->CreateConfigBind(bindline);
2000 if (bind) {
2001 event->AddBind(bind);
2002 bind->SetFlags(bindline);
2003 break;
2004 }
2005 }
2006 }
2007 }
2008
2009 static struct {
2010 const char * eventend;
2011 Bitu key;
2012 } DefaultKeys[]={
2013 {"f1",SDLK_F1}, {"f2",SDLK_F2}, {"f3",SDLK_F3}, {"f4",SDLK_F4},
2014 {"f5",SDLK_F5}, {"f6",SDLK_F6}, {"f7",SDLK_F7}, {"f8",SDLK_F8},
2015 {"f9",SDLK_F9}, {"f10",SDLK_F10}, {"f11",SDLK_F11}, {"f12",SDLK_F12},
2016
2017 {"1",SDLK_1}, {"2",SDLK_2}, {"3",SDLK_3}, {"4",SDLK_4},
2018 {"5",SDLK_5}, {"6",SDLK_6}, {"7",SDLK_7}, {"8",SDLK_8},
2019 {"9",SDLK_9}, {"0",SDLK_0},
2020
2021 {"a",SDLK_a}, {"b",SDLK_b}, {"c",SDLK_c}, {"d",SDLK_d},
2022 {"e",SDLK_e}, {"f",SDLK_f}, {"g",SDLK_g}, {"h",SDLK_h},
2023 {"i",SDLK_i}, {"j",SDLK_j}, {"k",SDLK_k}, {"l",SDLK_l},
2024 {"m",SDLK_m}, {"n",SDLK_n}, {"o",SDLK_o}, {"p",SDLK_p},
2025 {"q",SDLK_q}, {"r",SDLK_r}, {"s",SDLK_s}, {"t",SDLK_t},
2026 {"u",SDLK_u}, {"v",SDLK_v}, {"w",SDLK_w}, {"x",SDLK_x},
2027 {"y",SDLK_y}, {"z",SDLK_z}, {"space",SDLK_SPACE},
2028 {"esc",SDLK_ESCAPE}, {"equals",SDLK_EQUALS}, {"grave",SDLK_BACKQUOTE},
2029 {"tab",SDLK_TAB}, {"enter",SDLK_RETURN}, {"bspace",SDLK_BACKSPACE},
2030 {"lbracket",SDLK_LEFTBRACKET}, {"rbracket",SDLK_RIGHTBRACKET},
2031 {"minus",SDLK_MINUS}, {"capslock",SDLK_CAPSLOCK}, {"semicolon",SDLK_SEMICOLON},
2032 {"quote", SDLK_QUOTE}, {"backslash",SDLK_BACKSLASH}, {"lshift",SDLK_LSHIFT},
2033 {"rshift",SDLK_RSHIFT}, {"lalt",SDLK_LALT}, {"ralt",SDLK_RALT},
2034 {"lctrl",SDLK_LCTRL}, {"rctrl",SDLK_RCTRL}, {"comma",SDLK_COMMA},
2035 {"period",SDLK_PERIOD}, {"slash",SDLK_SLASH}, {"printscreen",SDLK_PRINT},
2036 {"scrolllock",SDLK_SCROLLOCK}, {"pause",SDLK_PAUSE}, {"pagedown",SDLK_PAGEDOWN},
2037 {"pageup",SDLK_PAGEUP}, {"insert",SDLK_INSERT}, {"home",SDLK_HOME},
2038 {"delete",SDLK_DELETE}, {"end",SDLK_END}, {"up",SDLK_UP},
2039 {"left",SDLK_LEFT}, {"down",SDLK_DOWN}, {"right",SDLK_RIGHT},
2040 {"kp_0",SDLK_KP0}, {"kp_1",SDLK_KP1}, {"kp_2",SDLK_KP2}, {"kp_3",SDLK_KP3},
2041 {"kp_4",SDLK_KP4}, {"kp_5",SDLK_KP5}, {"kp_6",SDLK_KP6}, {"kp_7",SDLK_KP7},
2042 {"kp_8",SDLK_KP8}, {"kp_9",SDLK_KP9}, {"numlock",SDLK_NUMLOCK},
2043 {"kp_divide",SDLK_KP_DIVIDE}, {"kp_multiply",SDLK_KP_MULTIPLY},
2044 {"kp_minus",SDLK_KP_MINUS}, {"kp_plus",SDLK_KP_PLUS},
2045 {"kp_period",SDLK_KP_PERIOD}, {"kp_enter",SDLK_KP_ENTER},
2046
2047 #if defined (MACOSX)
2048 /* Intl Mac keyboards in US layout actually put U+00A7 SECTION SIGN here */
2049 {"lessthan",SDLK_WORLD_0},
2050 #else
2051 {"lessthan",SDLK_LESS},
2052 #endif
2053
2054 {0,0}
2055 };
2056
2057 static void CreateDefaultBinds(void) {
2058 char buffer[512];
2059 Bitu i=0;
2060 while (DefaultKeys[i].eventend) {
2061 sprintf(buffer,"key_%s \"key %d\"",DefaultKeys[i].eventend,DefaultKeys[i].key);
2062 CreateStringBind(buffer);
2063 i++;
2064 }
2065 sprintf(buffer,"mod_1 \"key %d\"",SDLK_RCTRL);CreateStringBind(buffer);
2066 sprintf(buffer,"mod_1 \"key %d\"",SDLK_LCTRL);CreateStringBind(buffer);
2067 sprintf(buffer,"mod_2 \"key %d\"",SDLK_RALT);CreateStringBind(buffer);
2068 sprintf(buffer,"mod_2 \"key %d\"",SDLK_LALT);CreateStringBind(buffer);
2069 for (CHandlerEventVector_it hit=handlergroup.begin();hit!=handlergroup.end();hit++) {
2070 (*hit)->MakeDefaultBind(buffer);
2071 CreateStringBind(buffer);
2072 }
2073
2074 /* joystick1, buttons 1-6 */
2075 sprintf(buffer,"jbutton_0_0 \"stick_0 button 0\" ");CreateStringBind(buffer);
2076 sprintf(buffer,"jbutton_0_1 \"stick_0 button 1\" ");CreateStringBind(buffer);
2077 sprintf(buffer,"jbutton_0_2 \"stick_0 button 2\" ");CreateStringBind(buffer);
2078 sprintf(buffer,"jbutton_0_3 \"stick_0 button 3\" ");CreateStringBind(buffer);
2079 sprintf(buffer,"jbutton_0_4 \"stick_0 button 4\" ");CreateStringBind(buffer);
2080 sprintf(buffer,"jbutton_0_5 \"stick_0 button 5\" ");CreateStringBind(buffer);
2081 /* joystick2, buttons 1-2 */
2082 sprintf(buffer,"jbutton_1_0 \"stick_1 button 0\" ");CreateStringBind(buffer);
2083 sprintf(buffer,"jbutton_1_1 \"stick_1 button 1\" ");CreateStringBind(buffer);
2084
2085 /* joystick1, axes 1-4 */
2086 sprintf(buffer,"jaxis_0_0- \"stick_0 axis 0 0\" ");CreateStringBind(buffer);
2087 sprintf(buffer,"jaxis_0_0+ \"stick_0 axis 0 1\" ");CreateStringBind(buffer);
2088 sprintf(buffer,"jaxis_0_1- \"stick_0 axis 1 0\" ");CreateStringBind(buffer);
2089 sprintf(buffer,"jaxis_0_1+ \"stick_0 axis 1 1\" ");CreateStringBind(buffer);
2090 sprintf(buffer,"jaxis_0_2- \"stick_0 axis 2 0\" ");CreateStringBind(buffer);
2091 sprintf(buffer,"jaxis_0_2+ \"stick_0 axis 2 1\" ");CreateStringBind(buffer);
2092 sprintf(buffer,"jaxis_0_3- \"stick_0 axis 3 0\" ");CreateStringBind(buffer);
2093 sprintf(buffer,"jaxis_0_3+ \"stick_0 axis 3 1\" ");CreateStringBind(buffer);
2094 /* joystick2, axes 1-2 */
2095 sprintf(buffer,"jaxis_1_0- \"stick_1 axis 0 0\" ");CreateStringBind(buffer);
2096 sprintf(buffer,"jaxis_1_0+ \"stick_1 axis 0 1\" ");CreateStringBind(buffer);
2097 sprintf(buffer,"jaxis_1_1- \"stick_1 axis 1 0\" ");CreateStringBind(buffer);
2098 sprintf(buffer,"jaxis_1_1+ \"stick_1 axis 1 1\" ");CreateStringBind(buffer);
2099
2100 /* joystick1, hat */
2101 sprintf(buffer,"jhat_0_0_0 \"stick_0 hat 0 1\" ");CreateStringBind(buffer);
2102 sprintf(buffer,"jhat_0_0_1 \"stick_0 hat 0 2\" ");CreateStringBind(buffer);
2103 sprintf(buffer,"jhat_0_0_2 \"stick_0 hat 0 4\" ");CreateStringBind(buffer);
2104 sprintf(buffer,"jhat_0_0_3 \"stick_0 hat 0 8\" ");CreateStringBind(buffer);
2105 }
2106
2107 void MAPPER_AddHandler(MAPPER_Handler * handler,MapKeys key,Bitu mods,char const * const eventname,char const * const buttonname) {
2108 //Check if it allready exists=> if so return.
2109 for(CHandlerEventVector_it it=handlergroup.begin();it!=handlergroup.end();it++)
2110 if(strcmp((*it)->buttonname,buttonname) == 0) return;
2111
2112 char tempname[17];
2113 strcpy(tempname,"hand_");
2114 strcat(tempname,eventname);
2115 new CHandlerEvent(tempname,handler,key,mods,buttonname);
2116 return ;
2117 }
2118
2119 static void MAPPER_SaveBinds(void) {
2120 FILE * savefile=fopen(mapper.filename.c_str(),"wt+");
2121 if (!savefile) {
2122 LOG_MSG("Can't open %s for saving the mappings",mapper.filename.c_str());
2123 return;
2124 }
2125 char buf[128];
2126 for (CEventVector_it event_it=events.begin();event_it!=events.end();event_it++) {
2127 CEvent * event=*(event_it);
2128 fprintf(savefile,"%s ",event->GetName());
2129 for (CBindList_it bind_it=event->bindlist.begin();bind_it!=event->bindlist.end();bind_it++) {
2130 CBind * bind=*(bind_it);
2131 bind->ConfigName(buf);
2132 bind->AddFlags(buf);
2133 fprintf(savefile,"\"%s\" ",buf);
2134 }
2135 fprintf(savefile,"\n");
2136 }
2137 fclose(savefile);
2138 change_action_text("Mapper file saved.",CLR_WHITE);
2139 }
2140
2141 static bool MAPPER_LoadBinds(void) {
2142 FILE * loadfile=fopen(mapper.filename.c_str(),"rt");
2143 if (!loadfile) return false;
2144 char linein[512];
2145 while (fgets(linein,512,loadfile)) {
2146 CreateStringBind(linein);
2147 }
2148 fclose(loadfile);
2149 LOG_MSG("MAPPER: Loading mapper settings from %s", mapper.filename.c_str());
2150 return true;
2151 }
2152
2153 void MAPPER_CheckEvent(SDL_Event * event) {
2154 for (CBindGroup_it it=bindgroups.begin();it!=bindgroups.end();it++) {
2155 if ((*it)->CheckEvent(event)) return;
2156 }
2157 }
2158
2159 void BIND_MappingEvents(void) {
2160 SDL_Event event;
2161 while (SDL_PollEvent(&event)) {
2162 switch (event.type) {
2163 case SDL_MOUSEBUTTONUP:
2164 /* Check the press */
2165 for (CButton_it but_it = buttons.begin();but_it!=buttons.end();but_it++) {
2166 if ((*but_it)->OnTop(event.button.x,event.button.y)) {
2167 (*but_it)->Click();
2168 }
2169 }
2170 break;
2171 case SDL_QUIT:
2172 mapper.exit=true;
2173 break;
2174 default:
2175 if (mapper.addbind) for (CBindGroup_it it=bindgroups.begin();it!=bindgroups.end();it++) {
2176 CBind * newbind=(*it)->CreateEventBind(&event);
2177 if (!newbind) continue;
2178 mapper.aevent->AddBind(newbind);
2179 SetActiveEvent(mapper.aevent);
2180 mapper.addbind=false;
2181 break;
2182 }
2183 }
2184 }
2185 }
2186
2187 static void InitializeJoysticks(void) {
2188 mapper.sticks.num=0;
2189 mapper.sticks.num_groups=0;
2190 if (joytype != JOY_NONE) {
2191 mapper.sticks.num=SDL_NumJoysticks();
2192 if (joytype==JOY_AUTO) {
2193 // try to figure out what joystick type to select
2194 // depending on the number of physically attached joysticks
2195 if (mapper.sticks.num>1) {
2196 // more than one joystick present; if all are acceptable use 2axis
2197 // to allow emulation of two joysticks
2198 bool first_usable=false;
2199 SDL_Joystick* tmp_stick1=SDL_JoystickOpen(0);
2200 if (tmp_stick1) {
2201 if ((SDL_JoystickNumAxes(tmp_stick1)>1) || (SDL_JoystickNumButtons(tmp_stick1)>0)) {
2202 first_usable=true;
2203 }
2204 SDL_JoystickClose(tmp_stick1);
2205 }
2206 bool second_usable=false;
2207 SDL_Joystick* tmp_stick2=SDL_JoystickOpen(1);
2208 if (tmp_stick2) {
2209 if ((SDL_JoystickNumAxes(tmp_stick2)>1) || (SDL_JoystickNumButtons(tmp_stick2)>0)) {
2210 second_usable=true;
2211 }
2212 SDL_JoystickClose(tmp_stick2);
2213 }
2214 // choose joystick type now that we know which physical joysticks are usable
2215 if (first_usable) {
2216 if (second_usable) {
2217 joytype=JOY_2AXIS;
2218 LOG_MSG("Two or more joysticks reported, initializing with 2axis");
2219 } else {
2220 joytype=JOY_4AXIS;
2221 LOG_MSG("One joystick reported, initializing with 4axis");
2222 }
2223 } else if (second_usable) {
2224 joytype=JOY_4AXIS_2;
2225 LOG_MSG("One joystick reported, initializing with 4axis_2");
2226 }
2227 } else if (mapper.sticks.num) {
2228 // one joystick present; if it is acceptable use 4axis
2229 joytype=JOY_NONE;
2230 SDL_Joystick* tmp_stick1=SDL_JoystickOpen(0);
2231 if (tmp_stick1) {
2232 if ((SDL_JoystickNumAxes(tmp_stick1)>0) || (SDL_JoystickNumButtons(tmp_stick1)>0)) {
2233 joytype=JOY_4AXIS;
2234 LOG_MSG("One joystick reported, initializing with 4axis");
2235 }
2236 }
2237 } else {
2238 joytype=JOY_NONE;
2239 }
2240 }
2241 }
2242 }
2243
2244 static void CreateBindGroups(void) {
2245 bindgroups.clear();
2246 new CKeyBindGroup(SDLK_LAST);
2247 if (joytype != JOY_NONE) {
2248 #if defined (REDUCE_JOYSTICK_POLLING)
2249 // direct access to the SDL joystick, thus removed from the event handling
2250 if (mapper.sticks.num) SDL_JoystickEventState(SDL_DISABLE);
2251 #else
2252 // enable joystick event handling
2253 if (mapper.sticks.num) SDL_JoystickEventState(SDL_ENABLE);
2254 else return;
2255 #endif
2256 Bit8u joyno=0;
2257 switch (joytype) {
2258 case JOY_NONE:
2259 break;
2260 case JOY_4AXIS:
2261 mapper.sticks.stick[mapper.sticks.num_groups++]=new C4AxisBindGroup(joyno,joyno);
2262 new CStickBindGroup(joyno+1U,joyno+1U,true);
2263 break;
2264 case JOY_4AXIS_2:
2265 mapper.sticks.stick[mapper.sticks.num_groups++]=new C4AxisBindGroup(joyno+1U,joyno);
2266 new CStickBindGroup(joyno,joyno+1U,true);
2267 break;
2268 case JOY_FCS:
2269 mapper.sticks.stick[mapper.sticks.num_groups++]=new CFCSBindGroup(joyno,joyno);
2270 new CStickBindGroup(joyno+1U,joyno+1U,true);
2271 break;
2272 case JOY_CH:
2273 mapper.sticks.stick[mapper.sticks.num_groups++]=new CCHBindGroup(joyno,joyno);
2274 new CStickBindGroup(joyno+1U,joyno+1U,true);
2275 break;
2276 case JOY_2AXIS:
2277 default:
2278 mapper.sticks.stick[mapper.sticks.num_groups++]=new CStickBindGroup(joyno,joyno);
2279 if((joyno+1U) < mapper.sticks.num) {
2280 mapper.sticks.stick[mapper.sticks.num_groups++]=new CStickBindGroup(joyno+1U,joyno+1U);
2281 } else {
2282 new CStickBindGroup(joyno+1U,joyno+1U,true);
2283 }
2284 break;
2285 }
2286 }
2287 }
2288
2289 #if defined (REDUCE_JOYSTICK_POLLING)
2290 void MAPPER_UpdateJoysticks(void) {
2291 for (Bitu i=0; i<mapper.sticks.num_groups; i++) {
2292 mapper.sticks.stick[i]->UpdateJoystick();
2293 }
2294 }
2295 #endif
2296
2297 void MAPPER_LosingFocus(void) {
2298 for (CEventVector_it evit=events.begin();evit!=events.end();evit++) {
2299 if(*evit != caps_lock_event && *evit != num_lock_event)
2300 (*evit)->DeActivateAll();
2301 }
2302 }
2303
2304 void MAPPER_Run(bool pressed) {
2305 if (pressed)
2306 return;
2307 KEYBOARD_ClrBuffer(); //Clear buffer
2308 GFX_LosingFocus(); //Release any keys pressed (buffer gets filled again).
2309 MAPPER_RunInternal();
2310 }
2311
2312 SDL_Surface* SDL_SetVideoMode_Wrap(int width,int height,int bpp,Bit32u flags);
2313
2314 void MAPPER_RunInternal() {
2315 int cursor = SDL_ShowCursor(SDL_QUERY);
2316 SDL_ShowCursor(SDL_ENABLE);
2317 bool mousetoggle=false;
2318 if(mouselocked) {
2319 mousetoggle=true;
2320 GFX_CaptureMouse();
2321 }
2322
2323 /* Be sure that there is no update in progress */
2324 GFX_EndUpdate( 0 );
2325 mapper.surface=SDL_SetVideoMode_Wrap(640,480,8,0);
2326 if (mapper.surface == NULL) E_Exit("Could not initialize video mode for mapper: %s",SDL_GetError());
2327
2328 /* Set some palette entries */
2329 SDL_SetPalette(mapper.surface, SDL_LOGPAL|SDL_PHYSPAL, map_pal, 0, 5);
2330 if (last_clicked) {
2331 last_clicked->SetColor(CLR_WHITE);
2332 last_clicked=NULL;
2333 }
2334 /* Go in the event loop */
2335 mapper.exit=false;
2336 mapper.redraw=true;
2337 SetActiveEvent(0);
2338 #if defined (REDUCE_JOYSTICK_POLLING)
2339 SDL_JoystickEventState(SDL_ENABLE);
2340 #endif
2341 while (!mapper.exit) {
2342 if (mapper.redraw) {
2343 mapper.redraw=false;
2344 DrawButtons();
2345 }
2346 BIND_MappingEvents();
2347 SDL_Delay(1);
2348 }
2349 #if defined (REDUCE_JOYSTICK_POLLING)
2350 SDL_JoystickEventState(SDL_DISABLE);
2351 #endif
2352 if(mousetoggle) GFX_CaptureMouse();
2353 SDL_ShowCursor(cursor);
2354 GFX_ResetScreen();
2355 }
2356
2357 void MAPPER_Init(void) {
2358 InitializeJoysticks();
2359 CreateLayout();
2360 CreateBindGroups();
2361 if (!MAPPER_LoadBinds()) CreateDefaultBinds();
2362 if (SDL_GetModState()&KMOD_CAPS) {
2363 for (CBindList_it bit=caps_lock_event->bindlist.begin();bit!=caps_lock_event->bindlist.end();bit++) {
2364 #if SDL_VERSION_ATLEAST(1, 2, 14)
2365 (*bit)->ActivateBind(32767,true,false);
2366 (*bit)->DeActivateBind(false);
2367 #else
2368 (*bit)->ActivateBind(32767,true,true); //Skip the action itself as bios_keyboard.cpp handles the startup state.
2369 #endif
2370 }
2371 }
2372 if (SDL_GetModState()&KMOD_NUM) {
2373 for (CBindList_it bit=num_lock_event->bindlist.begin();bit!=num_lock_event->bindlist.end();bit++) {
2374 #if SDL_VERSION_ATLEAST(1, 2, 14)
2375 (*bit)->ActivateBind(32767,true,false);
2376 (*bit)->DeActivateBind(false);
2377 #else
2378 (*bit)->ActivateBind(32767,true,true);
2379 #endif
2380 }
2381 }
2382 }
2383 //Somehow including them at the top conflicts with something in setup.h
2384 #ifdef C_X11_XKB
2385 #include "SDL_syswm.h"
2386 #include <X11/XKBlib.h>
2387 #endif
2388 void MAPPER_StartUp(Section * sec) {
2389 Section_prop * section=static_cast<Section_prop *>(sec);
2390 mapper.sticks.num=0;
2391 mapper.sticks.num_groups=0;
2392
2393 memset(&virtual_joysticks,0,sizeof(virtual_joysticks));
2394
2395 usescancodes = false;
2396
2397 if (section->Get_bool("usescancodes")) {
2398 usescancodes=true;
2399
2400 /* Note: table has to be tested/updated for various OSs */
2401 #if defined (MACOSX)
2402 /* nothing */
2403 #elif defined(OS2)
2404 sdlkey_map[0x61]=SDLK_UP;
2405 sdlkey_map[0x66]=SDLK_DOWN;
2406 sdlkey_map[0x63]=SDLK_LEFT;
2407 sdlkey_map[0x64]=SDLK_RIGHT;
2408 sdlkey_map[0x60]=SDLK_HOME;
2409 sdlkey_map[0x65]=SDLK_END;
2410 sdlkey_map[0x62]=SDLK_PAGEUP;
2411 sdlkey_map[0x67]=SDLK_PAGEDOWN;
2412 sdlkey_map[0x68]=SDLK_INSERT;
2413 sdlkey_map[0x69]=SDLK_DELETE;
2414 sdlkey_map[0x5C]=SDLK_KP_DIVIDE;
2415 sdlkey_map[0x5A]=SDLK_KP_ENTER;
2416 sdlkey_map[0x5B]=SDLK_RCTRL;
2417 sdlkey_map[0x5F]=SDLK_PAUSE;
2418 // sdlkey_map[0x00]=SDLK_PRINT;
2419 sdlkey_map[0x5E]=SDLK_RALT;
2420 sdlkey_map[0x40]=SDLK_KP5;
2421 sdlkey_map[0x41]=SDLK_KP6;
2422 #elif !defined (WIN32) /* => Linux & BSDs */
2423 bool evdev_input = false;
2424 #ifdef SDL_VIDEO_DRIVER_X11
2425 //SDL needs to be compiled to use it, else the next makes no sense.
2426 #ifdef C_X11_XKB
2427 SDL_SysWMinfo info;
2428 SDL_VERSION(&info.version);
2429 if (SDL_GetWMInfo(&info)) {
2430 XkbDescPtr desc = NULL;
2431 if((desc = XkbGetMap(info.info.x11.display,XkbAllComponentsMask,XkbUseCoreKbd))) {
2432 if(XkbGetNames(info.info.x11.display,XkbAllNamesMask,desc) == 0) {
2433 const char* keycodes = XGetAtomName(info.info.x11.display, desc->names->keycodes);
2434 // const char* geom = XGetAtomName(info.info.x11.display, desc->names->geometry);
2435 if(keycodes) {
2436 LOG(LOG_MISC,LOG_NORMAL)("keyboard type %s",keycodes);
2437 if (strncmp(keycodes,"evdev",5) == 0) evdev_input = true;
2438 }
2439 XkbFreeNames(desc,XkbAllNamesMask,True);
2440 }
2441 XkbFreeClientMap(desc,0,True);
2442 }
2443 }
2444 #endif
2445 #endif
2446 if (evdev_input) {
2447 sdlkey_map[0x67]=SDLK_UP;
2448 sdlkey_map[0x6c]=SDLK_DOWN;
2449 sdlkey_map[0x69]=SDLK_LEFT;
2450 sdlkey_map[0x6a]=SDLK_RIGHT;
2451 sdlkey_map[0x66]=SDLK_HOME;
2452 sdlkey_map[0x6b]=SDLK_END;
2453 sdlkey_map[0x68]=SDLK_PAGEUP;
2454 sdlkey_map[0x6d]=SDLK_PAGEDOWN;
2455 sdlkey_map[0x6e]=SDLK_INSERT;
2456 sdlkey_map[0x6f]=SDLK_DELETE;
2457 sdlkey_map[0x62]=SDLK_KP_DIVIDE;
2458 sdlkey_map[0x60]=SDLK_KP_ENTER;
2459 sdlkey_map[0x61]=SDLK_RCTRL;
2460 sdlkey_map[0x77]=SDLK_PAUSE;
2461 sdlkey_map[0x63]=SDLK_PRINT;
2462 sdlkey_map[0x64]=SDLK_RALT;
2463 } else {
2464 sdlkey_map[0x5a]=SDLK_UP;
2465 sdlkey_map[0x60]=SDLK_DOWN;
2466 sdlkey_map[0x5c]=SDLK_LEFT;
2467 sdlkey_map[0x5e]=SDLK_RIGHT;
2468 sdlkey_map[0x59]=SDLK_HOME;
2469 sdlkey_map[0x5f]=SDLK_END;
2470 sdlkey_map[0x5b]=SDLK_PAGEUP;
2471 sdlkey_map[0x61]=SDLK_PAGEDOWN;
2472 sdlkey_map[0x62]=SDLK_INSERT;
2473 sdlkey_map[0x63]=SDLK_DELETE;
2474 sdlkey_map[0x68]=SDLK_KP_DIVIDE;
2475 sdlkey_map[0x64]=SDLK_KP_ENTER;
2476 sdlkey_map[0x65]=SDLK_RCTRL;
2477 sdlkey_map[0x66]=SDLK_PAUSE;
2478 sdlkey_map[0x67]=SDLK_PRINT;
2479 sdlkey_map[0x69]=SDLK_RALT;
2480 }
2481 #else
2482 sdlkey_map[0xc8]=SDLK_UP;
2483 sdlkey_map[0xd0]=SDLK_DOWN;
2484 sdlkey_map[0xcb]=SDLK_LEFT;
2485 sdlkey_map[0xcd]=SDLK_RIGHT;
2486 sdlkey_map[0xc7]=SDLK_HOME;
2487 sdlkey_map[0xcf]=SDLK_END;
2488 sdlkey_map[0xc9]=SDLK_PAGEUP;
2489 sdlkey_map[0xd1]=SDLK_PAGEDOWN;
2490 sdlkey_map[0xd2]=SDLK_INSERT;
2491 sdlkey_map[0xd3]=SDLK_DELETE;
2492 sdlkey_map[0xb5]=SDLK_KP_DIVIDE;
2493 sdlkey_map[0x9c]=SDLK_KP_ENTER;
2494 sdlkey_map[0x9d]=SDLK_RCTRL;
2495 sdlkey_map[0xc5]=SDLK_PAUSE;
2496 sdlkey_map[0xb7]=SDLK_PRINT;
2497 sdlkey_map[0xb8]=SDLK_RALT;
2498 #endif
2499
2500 Bitu i;
2501 for (i=0; i<MAX_SDLKEYS; i++) scancode_map[i]=0;
2502 for (i=0; i<MAX_SCANCODES; i++) {
2503 SDLKey key=sdlkey_map[i];
2504 if (key<MAX_SDLKEYS) scancode_map[key]=(Bit8u)i;
2505 }
2506 }
2507
2508 Prop_path* pp = section->Get_path("mapperfile");
2509 mapper.filename = pp->realpath;
2510 MAPPER_AddHandler(&MAPPER_Run,MK_f1,MMOD1,"mapper","Mapper");
2511 }
2512