1 /* input.c: generalised input events layer for Fuse
2    Copyright (c) 2004 Philip Kendall
3 
4    $Id: input.c 4915 2013-04-07 05:32:09Z fredm $
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License along
17    with this program; if not, write to the Free Software Foundation, Inc.,
18    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 
20    Author contact information:
21 
22    E-mail: philip-fuse@shadowmagic.org.uk
23 
24 */
25 
26 #include <config.h>
27 
28 #include "fuse.h"
29 #include "input.h"
30 #include "keyboard.h"
31 #include "peripherals/joystick.h"
32 #include "settings.h"
33 #include "snapshot.h"
34 #include "tape.h"
35 #include "ui/ui.h"
36 #include "utils.h"
37 
38 static int keypress( const input_event_key_t *event );
39 static int keyrelease( const input_event_key_t *event );
40 static int do_joystick( const input_event_joystick_t *joystick_event,
41 			int press );
42 
43 int
input_event(const input_event_t * event)44 input_event( const input_event_t *event )
45 {
46 
47   switch( event->type ) {
48 
49   case INPUT_EVENT_KEYPRESS: return keypress( &( event->types.key ) );
50   case INPUT_EVENT_KEYRELEASE: return keyrelease( &( event->types.key ) );
51 
52   case INPUT_EVENT_JOYSTICK_PRESS:
53     return do_joystick( &( event->types.joystick ), 1 );
54   case INPUT_EVENT_JOYSTICK_RELEASE:
55     return do_joystick( &( event->types.joystick ), 0 );
56 
57   }
58 
59   ui_error( UI_ERROR_ERROR, "unknown input event type %d", event->type );
60   return 1;
61 
62 }
63 
64 static int
keypress(const input_event_key_t * event)65 keypress( const input_event_key_t *event )
66 {
67   int swallow;
68   const keyboard_spectrum_keys_t *ptr;
69 
70   if( ui_widget_level >= 0 ) {
71     ui_widget_keyhandler( event->native_key );
72     return 0;
73   }
74 
75   /* Escape => ask UI to end mouse grab, return if grab ended */
76   if( event->native_key == INPUT_KEY_Escape && ui_mouse_grabbed ) {
77     ui_mouse_grabbed = ui_mouse_release( 0 );
78     if( !ui_mouse_grabbed ) return 0;
79   }
80 
81   swallow = 0;
82   /* Joystick emulation via keyboard keys */
83   if ( event->spectrum_key == settings_current.joystick_keyboard_up ) {
84     swallow = joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_UP   , 1 );
85   }
86   else if( event->spectrum_key == settings_current.joystick_keyboard_down ) {
87     swallow = joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_DOWN , 1 );
88   }
89   else if( event->spectrum_key == settings_current.joystick_keyboard_left ) {
90     swallow = joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_LEFT , 1 );
91   }
92   else if( event->spectrum_key == settings_current.joystick_keyboard_right ) {
93     swallow = joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_RIGHT, 1 );
94   }
95   else if( event->spectrum_key == settings_current.joystick_keyboard_fire ) {
96     swallow = joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_FIRE , 1 );
97   }
98 
99   if( swallow ) return 0;
100 
101   ptr = keyboard_get_spectrum_keys( event->spectrum_key );
102 
103   if( ptr ) {
104     keyboard_press( ptr->key1 );
105     keyboard_press( ptr->key2 );
106   }
107 
108   ui_popup_menu( event->native_key );
109 
110   return 0;
111 }
112 
113 static int
keyrelease(const input_event_key_t * event)114 keyrelease( const input_event_key_t *event )
115 {
116   const keyboard_spectrum_keys_t *ptr;
117 
118   ptr = keyboard_get_spectrum_keys( event->spectrum_key );
119 
120   if( ptr ) {
121     keyboard_release( ptr->key1 );
122     keyboard_release( ptr->key2 );
123   }
124 
125   /* Joystick emulation via keyboard keys */
126   if( event->spectrum_key == settings_current.joystick_keyboard_up ) {
127     joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_UP   , 0 );
128   }
129   else if( event->spectrum_key == settings_current.joystick_keyboard_down ) {
130     joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_DOWN , 0 );
131   }
132   else if( event->spectrum_key == settings_current.joystick_keyboard_left ) {
133     joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_LEFT , 0 );
134   }
135   else if( event->spectrum_key == settings_current.joystick_keyboard_right ) {
136     joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_RIGHT, 0 );
137   }
138   else if( event->spectrum_key == settings_current.joystick_keyboard_fire ) {
139     joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_FIRE , 0 );
140   }
141 
142   return 0;
143 }
144 
145 static keyboard_key_name
get_fire_button_key(int which,input_key button)146 get_fire_button_key( int which, input_key button )
147 {
148   switch( which ) {
149 
150   case 0:
151     switch( button ) {
152     case INPUT_JOYSTICK_FIRE_1 : return settings_current.joystick_1_fire_1;
153     case INPUT_JOYSTICK_FIRE_2 : return settings_current.joystick_1_fire_2;
154     case INPUT_JOYSTICK_FIRE_3 : return settings_current.joystick_1_fire_3;
155     case INPUT_JOYSTICK_FIRE_4 : return settings_current.joystick_1_fire_4;
156     case INPUT_JOYSTICK_FIRE_5 : return settings_current.joystick_1_fire_5;
157     case INPUT_JOYSTICK_FIRE_6 : return settings_current.joystick_1_fire_6;
158     case INPUT_JOYSTICK_FIRE_7 : return settings_current.joystick_1_fire_7;
159     case INPUT_JOYSTICK_FIRE_8 : return settings_current.joystick_1_fire_8;
160     case INPUT_JOYSTICK_FIRE_9 : return settings_current.joystick_1_fire_9;
161     case INPUT_JOYSTICK_FIRE_10: return settings_current.joystick_1_fire_10;
162     case INPUT_JOYSTICK_FIRE_11: return settings_current.joystick_1_fire_11;
163     case INPUT_JOYSTICK_FIRE_12: return settings_current.joystick_1_fire_12;
164     case INPUT_JOYSTICK_FIRE_13: return settings_current.joystick_1_fire_13;
165     case INPUT_JOYSTICK_FIRE_14: return settings_current.joystick_1_fire_14;
166     case INPUT_JOYSTICK_FIRE_15: return settings_current.joystick_1_fire_15;
167     default: break;
168     }
169     break;
170 
171   case 1:
172     switch( button ) {
173     case INPUT_JOYSTICK_FIRE_1 : return settings_current.joystick_2_fire_1;
174     case INPUT_JOYSTICK_FIRE_2 : return settings_current.joystick_2_fire_2;
175     case INPUT_JOYSTICK_FIRE_3 : return settings_current.joystick_2_fire_3;
176     case INPUT_JOYSTICK_FIRE_4 : return settings_current.joystick_2_fire_4;
177     case INPUT_JOYSTICK_FIRE_5 : return settings_current.joystick_2_fire_5;
178     case INPUT_JOYSTICK_FIRE_6 : return settings_current.joystick_2_fire_6;
179     case INPUT_JOYSTICK_FIRE_7 : return settings_current.joystick_2_fire_7;
180     case INPUT_JOYSTICK_FIRE_8 : return settings_current.joystick_2_fire_8;
181     case INPUT_JOYSTICK_FIRE_9 : return settings_current.joystick_2_fire_9;
182     case INPUT_JOYSTICK_FIRE_10: return settings_current.joystick_2_fire_10;
183     case INPUT_JOYSTICK_FIRE_11: return settings_current.joystick_2_fire_11;
184     case INPUT_JOYSTICK_FIRE_12: return settings_current.joystick_2_fire_12;
185     case INPUT_JOYSTICK_FIRE_13: return settings_current.joystick_2_fire_13;
186     case INPUT_JOYSTICK_FIRE_14: return settings_current.joystick_2_fire_14;
187     case INPUT_JOYSTICK_FIRE_15: return settings_current.joystick_2_fire_15;
188     default: break;
189     }
190     break;
191 
192   }
193 
194   ui_error( UI_ERROR_ERROR, "get_fire_button_key: which = %d, button = %d",
195 	    which, button );
196   fuse_abort();
197 }
198 
199 static int
do_joystick(const input_event_joystick_t * joystick_event,int press)200 do_joystick( const input_event_joystick_t *joystick_event, int press )
201 {
202   int which;
203 
204 #ifdef USE_WIDGET
205   if( ui_widget_level >= 0 ) {
206     if( press ) ui_widget_keyhandler( joystick_event->button );
207     return 0;
208   }
209 
210 #ifndef GEKKO /* Home button opens the menu on Wii */
211   switch( joystick_event->button ) {
212   case INPUT_JOYSTICK_FIRE_2:
213     if( press ) ui_popup_menu( INPUT_KEY_F1 );
214     break;
215 
216   default: break;		/* Remove gcc warning */
217 
218   }
219 #endif  /* #ifndef GEKKO */
220 
221 #endif				/* #ifdef USE_WIDGET */
222 
223   which = joystick_event->which;
224 
225   if( joystick_event->button < INPUT_JOYSTICK_FIRE_1 ) {
226 
227     joystick_button button;
228 
229     switch( joystick_event->button ) {
230     case INPUT_JOYSTICK_UP   : button = JOYSTICK_BUTTON_UP;    break;
231     case INPUT_JOYSTICK_DOWN : button = JOYSTICK_BUTTON_DOWN;  break;
232     case INPUT_JOYSTICK_LEFT : button = JOYSTICK_BUTTON_LEFT;  break;
233     case INPUT_JOYSTICK_RIGHT: button = JOYSTICK_BUTTON_RIGHT; break;
234 
235     default:
236       ui_error( UI_ERROR_ERROR, "do_joystick: unknown button %d",
237 		joystick_event->button );
238       fuse_abort();
239     }
240 
241     joystick_press( which, button, press );
242 
243   } else {
244 
245     keyboard_key_name key;
246 
247     key = get_fire_button_key( which, joystick_event->button );
248 
249     if( key == KEYBOARD_JOYSTICK_FIRE ) {
250       joystick_press( which, JOYSTICK_BUTTON_FIRE, press );
251     } else {
252       if( press ) {
253 	keyboard_press( key );
254       } else {
255 	keyboard_release( key );
256       }
257     }
258 
259   }
260 
261   return 0;
262 }
263