initial pandora port, with hardware scaling and stuff
[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 #if defined(GP2X_BUILD) || defined(PND_BUILD)
351
352 extern u32 fps_debug;
353
354 gui_action_type get_gui_input()
355 {
356   gui_action_type new_button = CURSOR_NONE;
357   u32 buttons = gpsp_plat_joystick_read();
358   u32 new_buttons;
359
360   static u32 last_buttons = 0;
361   static u64 button_repeat_timestamp;
362
363   delay_us(25000);
364
365   new_buttons = (last_buttons ^ buttons) & buttons;
366   last_buttons = buttons;
367
368   new_button = gpsp_plat_buttons_to_cursor(new_buttons);
369   if(new_button != CURSOR_NONE)
370   {
371     get_ticks_us(&button_repeat_timestamp);
372     button_repeat_state = BUTTON_HELD_INITIAL;
373     button_repeat = new_buttons;
374     cursor_repeat = new_button;
375   }
376   else
377   {
378     if(buttons & button_repeat)
379     {
380       u64 new_ticks;
381       get_ticks_us(&new_ticks);
382
383       if(button_repeat_state == BUTTON_HELD_INITIAL)
384       {
385         if((new_ticks - button_repeat_timestamp) >
386          BUTTON_REPEAT_START)
387         {
388           new_button = cursor_repeat;
389           button_repeat_timestamp = new_ticks;
390           button_repeat_state = BUTTON_HELD_REPEAT;
391         }
392       }
393
394       if(button_repeat_state == BUTTON_HELD_REPEAT)
395       {
396         if((new_ticks - button_repeat_timestamp) >
397          BUTTON_REPEAT_CONTINUE)
398         {
399           new_button = cursor_repeat;
400           button_repeat_timestamp = new_ticks;
401         }
402       }
403     }
404   }
405
406   return new_button;
407 }
408
409 u32 button_id_to_gba_mask[] =
410 {
411   BUTTON_UP,
412   BUTTON_DOWN,
413   BUTTON_LEFT,
414   BUTTON_RIGHT,
415   BUTTON_A,
416   BUTTON_B,
417   BUTTON_L,
418   BUTTON_R,
419   BUTTON_START,
420   BUTTON_SELECT,
421   BUTTON_NONE,
422   BUTTON_NONE,
423   BUTTON_NONE,
424   BUTTON_NONE
425 };
426
427 u32 update_input()
428 {
429   static u32 rapidfire_flag = 1;
430   static u32 last_buttons;
431   u32 handled_buttons;
432   u32 button_id;
433   u32 new_key = 0;
434   u32 buttons = gpsp_plat_joystick_read();
435   u32 i;
436
437 #ifdef GP2X_BUILD
438   if((buttons & GP2X_VOL_DOWN) && (buttons & GP2X_VOL_UP))
439   {
440     buttons &= ~(GP2X_VOL_DOWN | GP2X_VOL_UP);
441     buttons |= GP2X_VOL_MIDDLE;
442   }
443
444   /* for Wiz */
445   if((buttons & GP2X_VOL_DOWN) && (buttons & GP2X_SELECT))
446   {
447     buttons &= ~(GP2X_VOL_DOWN | GP2X_SELECT);
448     buttons |= GP2X_VOL_MIDDLE;
449   }
450
451   last_buttons &= ~(GP2X_VOL_DOWN | GP2X_VOL_UP);
452 #endif
453
454   handled_buttons = (last_buttons ^ buttons) & buttons;
455   last_buttons = buttons;
456
457   for(i = 0; i < PLAT_BUTTON_COUNT; i++)
458   {
459     if(handled_buttons & button_plat_mask_to_config[i])
460       button_id = gamepad_config_map[i];
461     else
462       button_id = BUTTON_ID_NONE;
463
464     switch(button_id)
465     {
466       case BUTTON_ID_MENU:
467       {
468         u16 *screen_copy = copy_screen();
469         u32 ret_val = menu(screen_copy);
470         free(screen_copy);
471
472         return ret_val;
473       }
474
475       case BUTTON_ID_LOADSTATE:
476       {
477         u8 current_savestate_filename[512];
478         get_savestate_filename_noshot(savestate_slot,
479          current_savestate_filename);
480         load_state(current_savestate_filename);
481         return 1;
482       }
483
484       case BUTTON_ID_SAVESTATE:
485       {
486         u8 current_savestate_filename[512];
487         u16 *current_screen = copy_screen();
488         get_savestate_filename_noshot(savestate_slot,
489          current_savestate_filename);
490         save_state(current_savestate_filename, current_screen);
491         free(current_screen);
492         return 0;
493       }
494
495       case BUTTON_ID_FASTFORWARD:
496         synchronize_flag ^= 1;
497         return 0;
498
499 #ifdef GP2X_BUILD
500       case BUTTON_ID_VOLUP:
501         gp2x_sound_volume(1);
502         break;
503
504       case BUTTON_ID_VOLDOWN:
505         gp2x_sound_volume(0);
506         break;
507 #endif
508
509       case BUTTON_ID_FPS:
510         fps_debug ^= 1;
511         break;
512     }
513
514     if(buttons & button_plat_mask_to_config[i])
515     {
516       button_id = gamepad_config_map[i];
517       if(button_id < BUTTON_ID_MENU)
518       {
519         new_key |= button_id_to_gba_mask[button_id];
520       }
521       else
522
523       if((button_id >= BUTTON_ID_RAPIDFIRE_A) &&
524        (button_id <= BUTTON_ID_RAPIDFIRE_L))
525       {
526         rapidfire_flag ^= 1;
527         if(rapidfire_flag)
528         {
529           new_key |= button_id_to_gba_mask[button_id -
530            BUTTON_ID_RAPIDFIRE_A + BUTTON_ID_A];
531         }
532         else
533         {
534           new_key &= ~button_id_to_gba_mask[button_id -
535            BUTTON_ID_RAPIDFIRE_A + BUTTON_ID_A];
536         }
537       }
538     }
539   }
540
541   if((new_key | key) != key)
542     trigger_key(new_key);
543
544   key = new_key;
545
546   io_registers[REG_P1] = (~key) & 0x3FF;
547
548   return 0;
549 }
550
551 void init_input()
552 {
553
554 }
555
556 #endif
557
558
559
560 #ifdef PC_BUILD
561
562 u32 key_map(SDLKey key_sym)
563 {
564   switch(key_sym)
565   {
566     case SDLK_LSHIFT:
567       return BUTTON_L;
568
569     case SDLK_x:
570       return BUTTON_R;
571
572     case SDLK_DOWN:
573       return BUTTON_DOWN;
574
575     case SDLK_UP:
576       return BUTTON_UP;
577
578     case SDLK_LEFT:
579       return BUTTON_LEFT;
580
581     case SDLK_RIGHT:
582       return BUTTON_RIGHT;
583
584     case SDLK_RETURN:
585       return BUTTON_START;
586
587     case SDLK_RSHIFT:
588       return BUTTON_SELECT;
589
590     case SDLK_LCTRL:
591       return BUTTON_B;
592
593     case SDLK_LALT:
594       return BUTTON_A;
595
596     default:
597       return BUTTON_NONE;
598   }
599 }
600
601 u32 joy_map(u32 button)
602 {
603   switch(button)
604   {
605     case 4:
606       return BUTTON_L;
607
608     case 5:
609       return BUTTON_R;
610
611     case 9:
612       return BUTTON_START;
613
614     case 8:
615       return BUTTON_SELECT;
616
617     case 0:
618       return BUTTON_B;
619
620     case 1:
621       return BUTTON_A;
622
623     default:
624       return BUTTON_NONE;
625   }
626 }
627
628 gui_action_type get_gui_input()
629 {
630   SDL_Event event;
631   gui_action_type gui_action = CURSOR_NONE;
632
633   delay_us(30000);
634
635   while(SDL_PollEvent(&event))
636   {
637     switch(event.type)
638     {
639       case SDL_QUIT:
640         quit();
641
642       case SDL_KEYDOWN:
643       {
644         switch(event.key.keysym.sym)
645         {
646           case SDLK_ESCAPE:
647             gui_action = CURSOR_EXIT;
648             break;
649
650           case SDLK_DOWN:
651             gui_action = CURSOR_DOWN;
652             break;
653
654           case SDLK_UP:
655             gui_action = CURSOR_UP;
656             break;
657
658           case SDLK_LEFT:
659             gui_action = CURSOR_LEFT;
660             break;
661
662           case SDLK_RIGHT:
663             gui_action = CURSOR_RIGHT;
664             break;
665
666           case SDLK_RETURN:
667             gui_action = CURSOR_SELECT;
668             break;
669
670           case SDLK_BACKSPACE:
671             gui_action = CURSOR_BACK;
672             break;
673         }
674         break;
675       }
676     }
677   }
678
679   return gui_action;
680 }
681
682 u32 update_input()
683 {
684   SDL_Event event;
685
686   while(SDL_PollEvent(&event))
687   {
688     switch(event.type)
689     {
690       case SDL_QUIT:
691         quit();
692
693       case SDL_KEYDOWN:
694       {
695         if(event.key.keysym.sym == SDLK_ESCAPE)
696         {
697           quit();
698         }
699
700         if(event.key.keysym.sym == SDLK_BACKSPACE)
701         {
702           u16 *screen_copy = copy_screen();
703           u32 ret_val = menu(screen_copy);
704           free(screen_copy);
705
706           return ret_val;
707         }
708         else
709
710         if(event.key.keysym.sym == SDLK_F1)
711         {
712           debug_on();
713         }
714         else
715
716         if(event.key.keysym.sym == SDLK_F2)
717         {
718           FILE *fp = fopen("palette_ram.bin", "wb");
719           printf("writing palette RAM\n");
720           fwrite(palette_ram, 1024, 1, fp);
721           fclose(fp);
722           printf("writing palette VRAM\n");
723           fp = fopen("vram.bin", "wb");
724           fwrite(vram, 1024 * 96, 1, fp);
725           fclose(fp);
726           printf("writing palette OAM RAM\n");
727           fp = fopen("oam_ram.bin", "wb");
728           fwrite(oam_ram, 1024, 1, fp);
729           fclose(fp);
730           printf("writing palette I/O registers\n");
731           fp = fopen("io_registers.bin", "wb");
732           fwrite(io_registers, 1024, 1, fp);
733           fclose(fp);
734         }
735         else
736
737         if(event.key.keysym.sym == SDLK_F3)
738         {
739           dump_translation_cache();
740         }
741         else
742
743         if(event.key.keysym.sym == SDLK_F5)
744         {
745           u8 current_savestate_filename[512];
746           u16 *current_screen = copy_screen();
747           get_savestate_filename_noshot(savestate_slot,
748            current_savestate_filename);
749           save_state(current_savestate_filename, current_screen);
750           free(current_screen);
751         }
752         else
753
754         if(event.key.keysym.sym == SDLK_F7)
755         {
756           u8 current_savestate_filename[512];
757           get_savestate_filename_noshot(savestate_slot,
758            current_savestate_filename);
759           load_state(current_savestate_filename);
760           debug_on();
761           return 1;
762         }
763         else
764
765         if(event.key.keysym.sym == SDLK_BACKQUOTE)
766         {
767           synchronize_flag ^= 1;
768         }
769         else
770         {
771           key |= key_map(event.key.keysym.sym);
772           trigger_key(key);
773         }
774
775         break;
776       }
777
778       case SDL_KEYUP:
779       {
780         key &= ~(key_map(event.key.keysym.sym));
781         break;
782       }
783
784       case SDL_JOYBUTTONDOWN:
785       {
786         key |= joy_map(event.jbutton.button);
787         trigger_key(key);
788         break;
789       }
790
791       case SDL_JOYBUTTONUP:
792       {
793         key &= ~(joy_map(event.jbutton.button));
794         break;
795       }
796     }
797   }
798
799   io_registers[REG_P1] = (~key) & 0x3FF;
800
801   return 0;
802 }
803
804 void init_input()
805 {
806   u32 joystick_count = SDL_NumJoysticks();
807
808   if(joystick_count > 0)
809   {
810     SDL_JoystickOpen(0);
811     SDL_JoystickEventState(SDL_ENABLE);
812   }
813 }
814
815 #endif
816
817
818 #define input_savestate_builder(type)                                         \
819 void input_##type##_savestate(file_tag_type savestate_file)                   \
820 {                                                                             \
821   file_##type##_variable(savestate_file, key);                                \
822 }                                                                             \
823
824 input_savestate_builder(read);
825 input_savestate_builder(write_mem);
826