u5 release
[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_FPS,                // A
368   BUTTON_ID_A,                  // B
369   BUTTON_ID_B,                  // X
370   BUTTON_ID_MENU,               // 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   if(new_buttons & GP2X_L)
416     new_button = CURSOR_L;
417
418   if(new_buttons & GP2X_R)
419     new_button = CURSOR_R;
420
421
422   if(new_button != CURSOR_NONE)
423   {
424     get_ticks_us(&button_repeat_timestamp);
425     button_repeat_state = BUTTON_HELD_INITIAL;
426     button_repeat = new_buttons;
427     cursor_repeat = new_button;
428   }
429   else
430   {
431     if(buttons & button_repeat)
432     {
433       u64 new_ticks;
434       get_ticks_us(&new_ticks);
435
436       if(button_repeat_state == BUTTON_HELD_INITIAL)
437       {
438         if((new_ticks - button_repeat_timestamp) >
439          BUTTON_REPEAT_START)
440         {
441           new_button = cursor_repeat;
442           button_repeat_timestamp = new_ticks;
443           button_repeat_state = BUTTON_HELD_REPEAT;
444         }
445       }
446
447       if(button_repeat_state == BUTTON_HELD_REPEAT)
448       {
449         if((new_ticks - button_repeat_timestamp) >
450          BUTTON_REPEAT_CONTINUE)
451         {
452           new_button = cursor_repeat;
453           button_repeat_timestamp = new_ticks;
454         }
455       }
456     }
457   }
458
459   return new_button;
460 }
461
462 #define GP2X_VOL_MIDDLE (1 << 24)
463
464 u32 button_gp2x_mask_to_config[] =
465 {
466   GP2X_UP,
467   GP2X_LEFT,
468   GP2X_DOWN,
469   GP2X_RIGHT,
470   GP2X_START,
471   GP2X_SELECT,
472   GP2X_L,
473   GP2X_R,
474   GP2X_A,
475   GP2X_B,
476   GP2X_X,
477   GP2X_Y,
478   GP2X_VOL_DOWN,
479   GP2X_VOL_UP,
480   GP2X_PUSH,
481   GP2X_VOL_MIDDLE
482 };
483
484 u32 button_id_to_gba_mask[] =
485 {
486   BUTTON_UP,
487   BUTTON_DOWN,
488   BUTTON_LEFT,
489   BUTTON_RIGHT,
490   BUTTON_A,
491   BUTTON_B,
492   BUTTON_L,
493   BUTTON_R,
494   BUTTON_START,
495   BUTTON_SELECT,
496   BUTTON_NONE,
497   BUTTON_NONE,
498   BUTTON_NONE,
499   BUTTON_NONE
500 };
501
502 u32 update_input()
503 {
504   static u32 rapidfire_flag = 1;
505   static u32 last_buttons;
506   u32 handled_buttons;
507   u32 button_id;
508   u32 new_key = 0;
509   u32 buttons = gpsp_gp2x_joystick_read();
510   u32 i;
511
512   if((buttons & GP2X_VOL_DOWN) && (buttons & GP2X_VOL_UP))
513   {
514     buttons &= ~(GP2X_VOL_DOWN | GP2X_VOL_UP);
515     buttons |= GP2X_VOL_MIDDLE;
516   }
517
518   /* for Wiz */
519   if((buttons & GP2X_VOL_DOWN) && (buttons & GP2X_SELECT))
520   {
521     buttons &= ~(GP2X_VOL_DOWN | GP2X_SELECT);
522     buttons |= GP2X_VOL_MIDDLE;
523   }
524
525   handled_buttons = ((last_buttons ^ buttons) | GP2X_VOL_DOWN | GP2X_VOL_UP) & buttons;
526   last_buttons = buttons;
527
528   for(i = 0; i < 16; i++)
529   {
530     if(handled_buttons & button_gp2x_mask_to_config[i])
531       button_id = gamepad_config_map[i];
532     else
533       button_id = BUTTON_ID_NONE;
534
535     switch(button_id)
536     {
537       case BUTTON_ID_MENU:
538       {
539         u16 *screen_copy = copy_screen();
540         u32 ret_val = menu(screen_copy);
541         free(screen_copy);
542
543         return ret_val;
544       }
545
546       case BUTTON_ID_LOADSTATE:
547       {
548         u8 current_savestate_filename[512];
549         get_savestate_filename_noshot(savestate_slot,
550          current_savestate_filename);
551         load_state(current_savestate_filename);
552         return 1;
553       }
554
555       case BUTTON_ID_SAVESTATE:
556       {
557         u8 current_savestate_filename[512];
558         u16 *current_screen = copy_screen();
559         get_savestate_filename_noshot(savestate_slot,
560          current_savestate_filename);
561         save_state(current_savestate_filename, current_screen);
562         free(current_screen);
563         return 0;
564       }
565
566       case BUTTON_ID_FASTFORWARD:
567         print_string("FASTFORWARD", 0xFFFF, 0x0000, 0, 50);
568         synchronize_flag ^= 1;
569         return 0;
570
571       case BUTTON_ID_VOLUP:
572         gp2x_sound_volume(1);
573         break;
574
575       case BUTTON_ID_VOLDOWN:
576         gp2x_sound_volume(0);
577         break;
578
579       case BUTTON_ID_FPS:
580         gp2x_fps_debug ^= 1;
581         break;
582     }
583
584     if(buttons & button_gp2x_mask_to_config[i])
585     {
586       button_id = gamepad_config_map[i];
587       if(button_id < BUTTON_ID_MENU)
588       {
589         new_key |= button_id_to_gba_mask[button_id];
590       }
591       else
592
593       if((button_id >= BUTTON_ID_RAPIDFIRE_A) &&
594        (button_id <= BUTTON_ID_RAPIDFIRE_L))
595       {
596         rapidfire_flag ^= 1;
597         if(rapidfire_flag)
598         {
599           new_key |= button_id_to_gba_mask[button_id -
600            BUTTON_ID_RAPIDFIRE_A + BUTTON_ID_A];
601         }
602         else
603         {
604           new_key &= ~button_id_to_gba_mask[button_id -
605            BUTTON_ID_RAPIDFIRE_A + BUTTON_ID_A];
606         }
607       }
608     }
609   }
610
611   if((new_key | key) != key)
612     trigger_key(new_key);
613
614   key = new_key;
615
616   io_registers[REG_P1] = (~key) & 0x3FF;
617
618   return 0;
619 }
620
621 void init_input()
622 {
623
624 }
625
626 #endif
627
628
629
630 #ifdef PC_BUILD
631
632 u32 key_map(SDLKey key_sym)
633 {
634   switch(key_sym)
635   {
636     case SDLK_LSHIFT:
637       return BUTTON_L;
638
639     case SDLK_x:
640       return BUTTON_R;
641
642     case SDLK_DOWN:
643       return BUTTON_DOWN;
644
645     case SDLK_UP:
646       return BUTTON_UP;
647
648     case SDLK_LEFT:
649       return BUTTON_LEFT;
650
651     case SDLK_RIGHT:
652       return BUTTON_RIGHT;
653
654     case SDLK_RETURN:
655       return BUTTON_START;
656
657     case SDLK_RSHIFT:
658       return BUTTON_SELECT;
659
660     case SDLK_LCTRL:
661       return BUTTON_B;
662
663     case SDLK_LALT:
664       return BUTTON_A;
665
666     default:
667       return BUTTON_NONE;
668   }
669 }
670
671 u32 joy_map(u32 button)
672 {
673   switch(button)
674   {
675     case 4:
676       return BUTTON_L;
677
678     case 5:
679       return BUTTON_R;
680
681     case 9:
682       return BUTTON_START;
683
684     case 8:
685       return BUTTON_SELECT;
686
687     case 0:
688       return BUTTON_B;
689
690     case 1:
691       return BUTTON_A;
692
693     default:
694       return BUTTON_NONE;
695   }
696 }
697
698 gui_action_type get_gui_input()
699 {
700   SDL_Event event;
701   gui_action_type gui_action = CURSOR_NONE;
702
703   delay_us(30000);
704
705   while(SDL_PollEvent(&event))
706   {
707     switch(event.type)
708     {
709       case SDL_QUIT:
710         quit();
711
712       case SDL_KEYDOWN:
713       {
714         switch(event.key.keysym.sym)
715         {
716           case SDLK_ESCAPE:
717             gui_action = CURSOR_EXIT;
718             break;
719
720           case SDLK_DOWN:
721             gui_action = CURSOR_DOWN;
722             break;
723
724           case SDLK_UP:
725             gui_action = CURSOR_UP;
726             break;
727
728           case SDLK_LEFT:
729             gui_action = CURSOR_LEFT;
730             break;
731
732           case SDLK_RIGHT:
733             gui_action = CURSOR_RIGHT;
734             break;
735
736           case SDLK_RETURN:
737             gui_action = CURSOR_SELECT;
738             break;
739
740           case SDLK_BACKSPACE:
741             gui_action = CURSOR_BACK;
742             break;
743         }
744         break;
745       }
746     }
747   }
748
749   return gui_action;
750 }
751
752 u32 update_input()
753 {
754   SDL_Event event;
755
756   while(SDL_PollEvent(&event))
757   {
758     switch(event.type)
759     {
760       case SDL_QUIT:
761         quit();
762
763       case SDL_KEYDOWN:
764       {
765         if(event.key.keysym.sym == SDLK_ESCAPE)
766         {
767           quit();
768         }
769
770         if(event.key.keysym.sym == SDLK_BACKSPACE)
771         {
772           u16 *screen_copy = copy_screen();
773           u32 ret_val = menu(screen_copy);
774           free(screen_copy);
775
776           return ret_val;
777         }
778         else
779
780         if(event.key.keysym.sym == SDLK_F1)
781         {
782           debug_on();
783         }
784         else
785
786         if(event.key.keysym.sym == SDLK_F2)
787         {
788           FILE *fp = fopen("palette_ram.bin", "wb");
789           printf("writing palette RAM\n");
790           fwrite(palette_ram, 1024, 1, fp);
791           fclose(fp);
792           printf("writing palette VRAM\n");
793           fp = fopen("vram.bin", "wb");
794           fwrite(vram, 1024 * 96, 1, fp);
795           fclose(fp);
796           printf("writing palette OAM RAM\n");
797           fp = fopen("oam_ram.bin", "wb");
798           fwrite(oam_ram, 1024, 1, fp);
799           fclose(fp);
800           printf("writing palette I/O registers\n");
801           fp = fopen("io_registers.bin", "wb");
802           fwrite(io_registers, 1024, 1, fp);
803           fclose(fp);
804         }
805         else
806
807         if(event.key.keysym.sym == SDLK_F3)
808         {
809           dump_translation_cache();
810         }
811         else
812
813         if(event.key.keysym.sym == SDLK_F5)
814         {
815           u8 current_savestate_filename[512];
816           u16 *current_screen = copy_screen();
817           get_savestate_filename_noshot(savestate_slot,
818            current_savestate_filename);
819           save_state(current_savestate_filename, current_screen);
820           free(current_screen);
821         }
822         else
823
824         if(event.key.keysym.sym == SDLK_F7)
825         {
826           u8 current_savestate_filename[512];
827           get_savestate_filename_noshot(savestate_slot,
828            current_savestate_filename);
829           load_state(current_savestate_filename);
830           debug_on();
831           return 1;
832         }
833         else
834
835         if(event.key.keysym.sym == SDLK_BACKQUOTE)
836         {
837           synchronize_flag ^= 1;
838         }
839         else
840         {
841           key |= key_map(event.key.keysym.sym);
842           trigger_key(key);
843         }
844
845         break;
846       }
847
848       case SDL_KEYUP:
849       {
850         key &= ~(key_map(event.key.keysym.sym));
851         break;
852       }
853
854       case SDL_JOYBUTTONDOWN:
855       {
856         key |= joy_map(event.jbutton.button);
857         trigger_key(key);
858         break;
859       }
860
861       case SDL_JOYBUTTONUP:
862       {
863         key &= ~(joy_map(event.jbutton.button));
864         break;
865       }
866     }
867   }
868
869   io_registers[REG_P1] = (~key) & 0x3FF;
870
871   return 0;
872 }
873
874 void init_input()
875 {
876   u32 joystick_count = SDL_NumJoysticks();
877
878   if(joystick_count > 0)
879   {
880     SDL_JoystickOpen(0);
881     SDL_JoystickEventState(SDL_ENABLE);
882   }
883 }
884
885 #endif
886
887
888 #define input_savestate_builder(type)                                         \
889 void input_##type##_savestate(file_tag_type savestate_file)                   \
890 {                                                                             \
891   file_##type##_variable(savestate_file, key);                                \
892 }                                                                             \
893
894 input_savestate_builder(read);
895 input_savestate_builder(write_mem);
896