wiz port wip
[gpsp.git] / input.c
1 /* gameplaySP
2  *
3  * Copyright (C) 2006 Exophase <exophase@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the 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 GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19
20 #include "common.h"
21
22 // Special thanks to psp298 for the analog->dpad code!
23
24 void trigger_key(u32 key)
25 {
26   u32 p1_cnt = io_registers[REG_P1CNT];
27   u32 p1;
28
29   if((p1_cnt >> 14) & 0x01)
30   {
31     u32 key_intersection = (p1_cnt & key) & 0x3FF;
32
33     if(p1_cnt >> 15)
34     {
35       if(key_intersection == (p1_cnt & 0x3FF))
36         raise_interrupt(IRQ_KEYPAD);
37     }
38     else
39     {
40       if(key_intersection)
41         raise_interrupt(IRQ_KEYPAD);
42     }
43   }
44 }
45
46 u32 key = 0;
47
48 u32 global_enable_analog = 1;
49 u32 analog_sensitivity_level = 4;
50
51 typedef enum
52 {
53   BUTTON_NOT_HELD,
54   BUTTON_HELD_INITIAL,
55   BUTTON_HELD_REPEAT
56 } button_repeat_state_type;
57
58
59 // These define autorepeat values (in microseconds), tweak as necessary.
60
61 #define BUTTON_REPEAT_START    200000
62 #define BUTTON_REPEAT_CONTINUE 50000
63
64 button_repeat_state_type button_repeat_state = BUTTON_NOT_HELD;
65 u32 button_repeat = 0;
66 gui_action_type cursor_repeat = CURSOR_NONE;
67
68
69 #ifdef PSP_BUILD
70
71 u32 gamepad_config_map[16] =
72 {
73   BUTTON_ID_MENU,               // Triangle
74   BUTTON_ID_A,                  // Circle
75   BUTTON_ID_B,                  // Cross
76   BUTTON_ID_START,              // Square
77   BUTTON_ID_L,                  // Ltrigger
78   BUTTON_ID_R,                  // Rtrigger
79   BUTTON_ID_DOWN,               // Down
80   BUTTON_ID_LEFT,               // Left
81   BUTTON_ID_UP,                 // Up
82   BUTTON_ID_RIGHT,              // Right
83   BUTTON_ID_SELECT,             // Select
84   BUTTON_ID_START,              // Start
85   BUTTON_ID_UP,                 // Analog up
86   BUTTON_ID_DOWN,               // Analog down
87   BUTTON_ID_LEFT,               // Analog left
88   BUTTON_ID_RIGHT               // Analog right
89 };
90
91 #define PSP_ALL_BUTTON_MASK 0xFFFF
92
93 gui_action_type get_gui_input()
94 {
95   SceCtrlData ctrl_data;
96   gui_action_type new_button = CURSOR_NONE;
97   u32 new_buttons;
98
99   static u32 last_buttons = 0;
100   static u64 button_repeat_timestamp;
101
102   delay_us(25000);
103
104   sceCtrlPeekBufferPositive(&ctrl_data, 1);
105   ctrl_data.Buttons &= PSP_ALL_BUTTON_MASK;
106   new_buttons = (last_buttons ^ ctrl_data.Buttons) & ctrl_data.Buttons;
107   last_buttons = ctrl_data.Buttons;
108
109   if(new_buttons & PSP_CTRL_LEFT)
110     new_button = CURSOR_LEFT;
111
112   if(new_buttons & PSP_CTRL_RIGHT)
113     new_button = CURSOR_RIGHT;
114
115   if(new_buttons & PSP_CTRL_UP)
116     new_button = CURSOR_UP;
117
118   if(new_buttons & PSP_CTRL_DOWN)
119     new_button = CURSOR_DOWN;
120
121   if(new_buttons & PSP_CTRL_START)
122     new_button = CURSOR_SELECT;
123
124   if(new_buttons & PSP_CTRL_CIRCLE)
125     new_button = CURSOR_SELECT;
126
127   if(new_buttons & PSP_CTRL_CROSS)
128     new_button = CURSOR_EXIT;
129
130   if(new_buttons & PSP_CTRL_SQUARE)
131     new_button = CURSOR_BACK;
132
133   if(new_button != CURSOR_NONE)
134   {
135     get_ticks_us(&button_repeat_timestamp);
136     button_repeat_state = BUTTON_HELD_INITIAL;
137     button_repeat = new_buttons;
138     cursor_repeat = new_button;
139   }
140   else
141   {
142     if(ctrl_data.Buttons & button_repeat)
143     {
144       u64 new_ticks;
145       get_ticks_us(&new_ticks);
146
147       if(button_repeat_state == BUTTON_HELD_INITIAL)
148       {
149         if((new_ticks - button_repeat_timestamp) >
150          BUTTON_REPEAT_START)
151         {
152           new_button = cursor_repeat;
153           button_repeat_timestamp = new_ticks;
154           button_repeat_state = BUTTON_HELD_REPEAT;
155         }
156       }
157
158       if(button_repeat_state == BUTTON_HELD_REPEAT)
159       {
160         if((new_ticks - button_repeat_timestamp) >
161          BUTTON_REPEAT_CONTINUE)
162         {
163           new_button = cursor_repeat;
164           button_repeat_timestamp = new_ticks;
165         }
166       }
167     }
168   }
169
170   return new_button;
171 }
172
173 #define PSP_CTRL_ANALOG_UP    (1 << 28)
174 #define PSP_CTRL_ANALOG_DOWN  (1 << 29)
175 #define PSP_CTRL_ANALOG_LEFT  (1 << 30)
176 #define PSP_CTRL_ANALOG_RIGHT (1 << 31)
177
178 u32 button_psp_mask_to_config[] =
179 {
180   PSP_CTRL_TRIANGLE,
181   PSP_CTRL_CIRCLE,
182   PSP_CTRL_CROSS,
183   PSP_CTRL_SQUARE,
184   PSP_CTRL_LTRIGGER,
185   PSP_CTRL_RTRIGGER,
186   PSP_CTRL_DOWN,
187   PSP_CTRL_LEFT,
188   PSP_CTRL_UP,
189   PSP_CTRL_RIGHT,
190   PSP_CTRL_SELECT,
191   PSP_CTRL_START,
192   PSP_CTRL_ANALOG_UP,
193   PSP_CTRL_ANALOG_DOWN,
194   PSP_CTRL_ANALOG_LEFT,
195   PSP_CTRL_ANALOG_RIGHT
196 };
197
198 u32 button_id_to_gba_mask[] =
199 {
200   BUTTON_UP,
201   BUTTON_DOWN,
202   BUTTON_LEFT,
203   BUTTON_RIGHT,
204   BUTTON_A,
205   BUTTON_B,
206   BUTTON_L,
207   BUTTON_R,
208   BUTTON_START,
209   BUTTON_SELECT,
210   BUTTON_NONE,
211   BUTTON_NONE,
212   BUTTON_NONE,
213   BUTTON_NONE
214 };
215
216 gui_action_type get_gui_input_fs_hold(u32 button_id)
217 {
218   gui_action_type new_button = get_gui_input();
219   if((last_buttons & button_psp_mask_to_config[button_id]) == 0)
220     return CURSOR_BACK;
221
222   return new_button;
223 }
224
225 u32 rapidfire_flag = 1;
226
227 u32 update_input()
228 {
229   SceCtrlData ctrl_data;
230   u32 buttons;
231   u32 non_repeat_buttons;
232   u32 button_id;
233   u32 i;
234   u32 new_key = 0;
235   u32 analog_sensitivity = 92 - (analog_sensitivity_level * 4);
236   u32 inv_analog_sensitivity = 256 - analog_sensitivity;
237
238   sceCtrlPeekBufferPositive(&ctrl_data, 1);
239
240   buttons = ctrl_data.Buttons;
241
242   if(global_enable_analog)
243   {
244     if(ctrl_data.Lx < analog_sensitivity)
245       buttons |= PSP_CTRL_ANALOG_LEFT;
246
247     if(ctrl_data.Lx > inv_analog_sensitivity)
248       buttons |= PSP_CTRL_ANALOG_RIGHT;
249
250     if(ctrl_data.Ly < analog_sensitivity)
251       buttons |= PSP_CTRL_ANALOG_UP;
252
253     if(ctrl_data.Ly > inv_analog_sensitivity)
254       buttons |= PSP_CTRL_ANALOG_DOWN;
255   }
256
257   non_repeat_buttons = (last_buttons ^ buttons) & buttons;
258   last_buttons = buttons;
259
260   for(i = 0; i < 16; i++)
261   {
262     if(non_repeat_buttons & button_psp_mask_to_config[i])
263       button_id = gamepad_config_map[i];
264     else
265       button_id = BUTTON_ID_NONE;
266
267     switch(button_id)
268     {
269       case BUTTON_ID_MENU:
270       {
271         u16 *screen_copy = copy_screen();
272         u32 ret_val = menu(screen_copy);
273         free(screen_copy);
274
275         return ret_val;
276       }
277
278       case BUTTON_ID_LOADSTATE:
279       {
280         u8 current_savestate_filename[512];
281         get_savestate_filename_noshot(savestate_slot,
282          current_savestate_filename);
283         load_state(current_savestate_filename);
284         return 1;
285       }
286
287       case BUTTON_ID_SAVESTATE:
288       {
289         u8 current_savestate_filename[512];
290         u16 *current_screen = copy_screen();
291         get_savestate_filename_noshot(savestate_slot,
292          current_savestate_filename);
293         save_state(current_savestate_filename, current_screen);
294         free(current_screen);
295         return 0;
296       }
297
298       case BUTTON_ID_FASTFORWARD:
299         print_string("FASTFORWARD", 0xFFFF, 0x0000, 0, 50);
300         synchronize_flag ^= 1;
301         return 0;
302     }
303
304     if(buttons & button_psp_mask_to_config[i])
305     {
306       button_id = gamepad_config_map[i];
307       if(button_id < BUTTON_ID_MENU)
308       {
309         new_key |= button_id_to_gba_mask[button_id];
310       }
311       else
312
313       if((button_id >= BUTTON_ID_RAPIDFIRE_A) &&
314        (button_id <= BUTTON_ID_RAPIDFIRE_L))
315       {
316         rapidfire_flag ^= 1;
317         if(rapidfire_flag)
318         {
319           new_key |= button_id_to_gba_mask[button_id -
320            BUTTON_ID_RAPIDFIRE_A + BUTTON_ID_A];
321         }
322         else
323         {
324           new_key &= ~button_id_to_gba_mask[button_id -
325            BUTTON_ID_RAPIDFIRE_A + BUTTON_ID_A];
326         }
327       }
328     }
329   }
330
331   if((new_key | key) != key)
332     trigger_key(new_key);
333
334   key = new_key;
335
336   io_registers[REG_P1] = (~key) & 0x3FF;
337
338   return 0;
339 }
340
341 void init_input()
342 {
343   sceCtrlSetSamplingCycle(0);
344   sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
345 }
346
347 #endif
348
349
350 #ifdef GP2X_BUILD
351
352 // GP2X SDL requires a user made input method
353 #include <sys/mman.h>
354 #include <sys/ioctl.h>
355 #include "gp2x/gp2x.h"
356
357 u32 gamepad_config_map[16] =
358 {
359   BUTTON_ID_UP,                 // Up
360   BUTTON_ID_LEFT,               // Left
361   BUTTON_ID_DOWN,               // Down
362   BUTTON_ID_RIGHT,              // Right
363   BUTTON_ID_START,              // Start
364   BUTTON_ID_SELECT,             // Select
365   BUTTON_ID_L,                  // Ltrigger
366   BUTTON_ID_R,                  // Rtrigger
367   BUTTON_ID_NONE,               // A
368   BUTTON_ID_A,                  // B
369   BUTTON_ID_B,                  // X
370   BUTTON_ID_NONE,               // Y
371   BUTTON_ID_VOLDOWN,            // Vol down
372   BUTTON_ID_VOLUP,              // Vol up
373   BUTTON_ID_FPS,                // Push
374   BUTTON_ID_MENU                // Vol middle
375 };
376
377 extern u32 gp2x_fps_debug;
378 extern u32 gpsp_gp2x_joystick_read(void);
379
380 gui_action_type get_gui_input()
381 {
382   gui_action_type new_button = CURSOR_NONE;
383   u32 buttons = gpsp_gp2x_joystick_read();
384   u32 new_buttons;
385
386   static u32 last_buttons = 0;
387   static u64 button_repeat_timestamp;
388
389   delay_us(25000);
390
391   new_buttons = (last_buttons ^ buttons) & buttons;
392   last_buttons = buttons;
393
394   if(new_buttons & GP2X_A)
395     new_button = CURSOR_BACK;
396
397   if(new_buttons & GP2X_X)
398     new_button = CURSOR_EXIT;
399
400   if(new_buttons & GP2X_B)
401     new_button = CURSOR_SELECT;
402
403   if(new_buttons & GP2X_UP)
404     new_button = CURSOR_UP;
405
406   if(new_buttons & GP2X_DOWN)
407     new_button = CURSOR_DOWN;
408
409   if(new_buttons & GP2X_LEFT)
410     new_button = CURSOR_LEFT;
411
412   if(new_buttons & GP2X_RIGHT)
413     new_button = CURSOR_RIGHT;
414
415
416   if(new_button != CURSOR_NONE)
417   {
418     get_ticks_us(&button_repeat_timestamp);
419     button_repeat_state = BUTTON_HELD_INITIAL;
420     button_repeat = new_buttons;
421     cursor_repeat = new_button;
422   }
423   else
424   {
425     if(buttons & button_repeat)
426     {
427       u64 new_ticks;
428       get_ticks_us(&new_ticks);
429
430       if(button_repeat_state == BUTTON_HELD_INITIAL)
431       {
432         if((new_ticks - button_repeat_timestamp) >
433          BUTTON_REPEAT_START)
434         {
435           new_button = cursor_repeat;
436           button_repeat_timestamp = new_ticks;
437           button_repeat_state = BUTTON_HELD_REPEAT;
438         }
439       }
440
441       if(button_repeat_state == BUTTON_HELD_REPEAT)
442       {
443         if((new_ticks - button_repeat_timestamp) >
444          BUTTON_REPEAT_CONTINUE)
445         {
446           new_button = cursor_repeat;
447           button_repeat_timestamp = new_ticks;
448         }
449       }
450     }
451   }
452
453   return new_button;
454 }
455
456 #define GP2X_VOL_MIDDLE (1 << 24)
457
458 u32 button_gp2x_mask_to_config[] =
459 {
460   GP2X_UP,
461   GP2X_LEFT,
462   GP2X_DOWN,
463   GP2X_RIGHT,
464   GP2X_START,
465   GP2X_SELECT,
466   GP2X_L,
467   GP2X_R,
468   GP2X_A,
469   GP2X_B,
470   GP2X_X,
471   GP2X_Y,
472   GP2X_VOL_DOWN,
473   GP2X_VOL_UP,
474   GP2X_PUSH,
475   GP2X_VOL_MIDDLE
476 };
477
478 u32 button_id_to_gba_mask[] =
479 {
480   BUTTON_UP,
481   BUTTON_DOWN,
482   BUTTON_LEFT,
483   BUTTON_RIGHT,
484   BUTTON_A,
485   BUTTON_B,
486   BUTTON_L,
487   BUTTON_R,
488   BUTTON_START,
489   BUTTON_SELECT,
490   BUTTON_NONE,
491   BUTTON_NONE,
492   BUTTON_NONE,
493   BUTTON_NONE
494 };
495
496 u32 update_input()
497 {
498   static u32 rapidfire_flag = 1;
499   static u32 last_buttons;
500   u32 handled_buttons;
501   u32 button_id;
502   u32 new_key = 0;
503   u32 buttons = gpsp_gp2x_joystick_read();
504   u32 i;
505
506   if((buttons & GP2X_VOL_DOWN) && (buttons & GP2X_VOL_UP))
507   {
508     buttons &= ~(GP2X_VOL_DOWN | GP2X_VOL_UP);
509     buttons |= GP2X_VOL_MIDDLE;
510   }
511
512   handled_buttons = ((last_buttons ^ buttons) | GP2X_VOL_DOWN | GP2X_VOL_UP) & buttons;
513   last_buttons = buttons;
514
515   for(i = 0; i < 16; i++)
516   {
517     if(handled_buttons & button_gp2x_mask_to_config[i])
518       button_id = gamepad_config_map[i];
519     else
520       button_id = BUTTON_ID_NONE;
521
522     switch(button_id)
523     {
524       case BUTTON_ID_MENU:
525       {
526         u16 *screen_copy = copy_screen();
527         u32 ret_val = menu(screen_copy);
528         free(screen_copy);
529
530         return ret_val;
531       }
532
533       case BUTTON_ID_LOADSTATE:
534       {
535         u8 current_savestate_filename[512];
536         get_savestate_filename_noshot(savestate_slot,
537          current_savestate_filename);
538         load_state(current_savestate_filename);
539         return 1;
540       }
541
542       case BUTTON_ID_SAVESTATE:
543       {
544         u8 current_savestate_filename[512];
545         u16 *current_screen = copy_screen();
546         get_savestate_filename_noshot(savestate_slot,
547          current_savestate_filename);
548         save_state(current_savestate_filename, current_screen);
549         free(current_screen);
550         return 0;
551       }
552
553       case BUTTON_ID_FASTFORWARD:
554         print_string("FASTFORWARD", 0xFFFF, 0x0000, 0, 50);
555         synchronize_flag ^= 1;
556         return 0;
557
558       case BUTTON_ID_VOLUP:
559         gp2x_sound_volume(1);
560         break;
561
562       case BUTTON_ID_VOLDOWN:
563         gp2x_sound_volume(0);
564         break;
565
566       case BUTTON_ID_FPS:
567         gp2x_fps_debug ^= 1;
568         break;
569     }
570
571     if(buttons & button_gp2x_mask_to_config[i])
572     {
573       button_id = gamepad_config_map[i];
574       if(button_id < BUTTON_ID_MENU)
575       {
576         new_key |= button_id_to_gba_mask[button_id];
577       }
578       else
579
580       if((button_id >= BUTTON_ID_RAPIDFIRE_A) &&
581        (button_id <= BUTTON_ID_RAPIDFIRE_L))
582       {
583         rapidfire_flag ^= 1;
584         if(rapidfire_flag)
585         {
586           new_key |= button_id_to_gba_mask[button_id -
587            BUTTON_ID_RAPIDFIRE_A + BUTTON_ID_A];
588         }
589         else
590         {
591           new_key &= ~button_id_to_gba_mask[button_id -
592            BUTTON_ID_RAPIDFIRE_A + BUTTON_ID_A];
593         }
594       }
595     }
596   }
597
598   if((new_key | key) != key)
599     trigger_key(new_key);
600
601   key = new_key;
602
603   io_registers[REG_P1] = (~key) & 0x3FF;
604
605   return 0;
606 }
607
608 void init_input()
609 {
610
611 }
612
613 #endif
614
615
616
617 #ifdef PC_BUILD
618
619 u32 key_map(SDLKey key_sym)
620 {
621   switch(key_sym)
622   {
623     case SDLK_LSHIFT:
624       return BUTTON_L;
625
626     case SDLK_x:
627       return BUTTON_R;
628
629     case SDLK_DOWN:
630       return BUTTON_DOWN;
631
632     case SDLK_UP:
633       return BUTTON_UP;
634
635     case SDLK_LEFT:
636       return BUTTON_LEFT;
637
638     case SDLK_RIGHT:
639       return BUTTON_RIGHT;
640
641     case SDLK_RETURN:
642       return BUTTON_START;
643
644     case SDLK_RSHIFT:
645       return BUTTON_SELECT;
646
647     case SDLK_LCTRL:
648       return BUTTON_B;
649
650     case SDLK_LALT:
651       return BUTTON_A;
652
653     default:
654       return BUTTON_NONE;
655   }
656 }
657
658 u32 joy_map(u32 button)
659 {
660   switch(button)
661   {
662     case 4:
663       return BUTTON_L;
664
665     case 5:
666       return BUTTON_R;
667
668     case 9:
669       return BUTTON_START;
670
671     case 8:
672       return BUTTON_SELECT;
673
674     case 0:
675       return BUTTON_B;
676
677     case 1:
678       return BUTTON_A;
679
680     default:
681       return BUTTON_NONE;
682   }
683 }
684
685 gui_action_type get_gui_input()
686 {
687   SDL_Event event;
688   gui_action_type gui_action = CURSOR_NONE;
689
690   delay_us(30000);
691
692   while(SDL_PollEvent(&event))
693   {
694     switch(event.type)
695     {
696       case SDL_QUIT:
697         quit();
698
699       case SDL_KEYDOWN:
700       {
701         switch(event.key.keysym.sym)
702         {
703           case SDLK_ESCAPE:
704             gui_action = CURSOR_EXIT;
705             break;
706
707           case SDLK_DOWN:
708             gui_action = CURSOR_DOWN;
709             break;
710
711           case SDLK_UP:
712             gui_action = CURSOR_UP;
713             break;
714
715           case SDLK_LEFT:
716             gui_action = CURSOR_LEFT;
717             break;
718
719           case SDLK_RIGHT:
720             gui_action = CURSOR_RIGHT;
721             break;
722
723           case SDLK_RETURN:
724             gui_action = CURSOR_SELECT;
725             break;
726
727           case SDLK_BACKSPACE:
728             gui_action = CURSOR_BACK;
729             break;
730         }
731         break;
732       }
733     }
734   }
735
736   return gui_action;
737 }
738
739 u32 update_input()
740 {
741   SDL_Event event;
742
743   while(SDL_PollEvent(&event))
744   {
745     switch(event.type)
746     {
747       case SDL_QUIT:
748         quit();
749
750       case SDL_KEYDOWN:
751       {
752         if(event.key.keysym.sym == SDLK_ESCAPE)
753         {
754           quit();
755         }
756
757         if(event.key.keysym.sym == SDLK_BACKSPACE)
758         {
759           u16 *screen_copy = copy_screen();
760           u32 ret_val = menu(screen_copy);
761           free(screen_copy);
762
763           return ret_val;
764         }
765         else
766
767         if(event.key.keysym.sym == SDLK_F1)
768         {
769           debug_on();
770         }
771         else
772
773         if(event.key.keysym.sym == SDLK_F2)
774         {
775           FILE *fp = fopen("palette_ram.bin", "wb");
776           printf("writing palette RAM\n");
777           fwrite(palette_ram, 1024, 1, fp);
778           fclose(fp);
779           printf("writing palette VRAM\n");
780           fp = fopen("vram.bin", "wb");
781           fwrite(vram, 1024 * 96, 1, fp);
782           fclose(fp);
783           printf("writing palette OAM RAM\n");
784           fp = fopen("oam_ram.bin", "wb");
785           fwrite(oam_ram, 1024, 1, fp);
786           fclose(fp);
787           printf("writing palette I/O registers\n");
788           fp = fopen("io_registers.bin", "wb");
789           fwrite(io_registers, 1024, 1, fp);
790           fclose(fp);
791         }
792         else
793
794         if(event.key.keysym.sym == SDLK_F3)
795         {
796           dump_translation_cache();
797         }
798         else
799
800         if(event.key.keysym.sym == SDLK_F5)
801         {
802           u8 current_savestate_filename[512];
803           u16 *current_screen = copy_screen();
804           get_savestate_filename_noshot(savestate_slot,
805            current_savestate_filename);
806           save_state(current_savestate_filename, current_screen);
807           free(current_screen);
808         }
809         else
810
811         if(event.key.keysym.sym == SDLK_F7)
812         {
813           u8 current_savestate_filename[512];
814           get_savestate_filename_noshot(savestate_slot,
815            current_savestate_filename);
816           load_state(current_savestate_filename);
817           debug_on();
818           return 1;
819         }
820         else
821
822         if(event.key.keysym.sym == SDLK_BACKQUOTE)
823         {
824           synchronize_flag ^= 1;
825         }
826         else
827         {
828           key |= key_map(event.key.keysym.sym);
829           trigger_key(key);
830         }
831
832         break;
833       }
834
835       case SDL_KEYUP:
836       {
837         key &= ~(key_map(event.key.keysym.sym));
838         break;
839       }
840
841       case SDL_JOYBUTTONDOWN:
842       {
843         key |= joy_map(event.jbutton.button);
844         trigger_key(key);
845         break;
846       }
847
848       case SDL_JOYBUTTONUP:
849       {
850         key &= ~(joy_map(event.jbutton.button));
851         break;
852       }
853     }
854   }
855
856   io_registers[REG_P1] = (~key) & 0x3FF;
857
858   return 0;
859 }
860
861 void init_input()
862 {
863   u32 joystick_count = SDL_NumJoysticks();
864
865   if(joystick_count > 0)
866   {
867     SDL_JoystickOpen(0);
868     SDL_JoystickEventState(SDL_ENABLE);
869   }
870 }
871
872 #endif
873
874
875 #define input_savestate_builder(type)                                         \
876 void input_##type##_savestate(file_tag_type savestate_file)                   \
877 {                                                                             \
878   file_##type##_variable(savestate_file, key);                                \
879 }                                                                             \
880
881 input_savestate_builder(read);
882 input_savestate_builder(write_mem);
883