Added joystick axis configuration.

This commit is contained in:
evilynux 2007-01-18 08:07:26 +00:00
parent 64f12ea810
commit ec0299ff6f
5 changed files with 256 additions and 107 deletions

View File

@ -25,20 +25,10 @@
/* Keypad key names */
const char *key_names[NB_KEYS] =
{
"A",
"B",
"Select",
"Start",
"Right",
"Left",
"Up",
"Down",
"R",
"L",
"X",
"Y",
"Debug",
"Boost"
"A", "B", "Select", "Start",
"Right", "Left", "Up", "Down",
"R", "L", "X", "Y",
"Debug", "Boost"
};
/* Default joypad configuration */
@ -47,10 +37,10 @@ const u16 default_joypad_cfg[NB_KEYS] =
0, // B
5, // select
8, // start
-1, // Right -- Start cheating abit...
-1, // Left
-1, // Up
-1, // Down -- End of cheating.
256, // Right -- Start cheating abit...
256, // Left
512, // Up
512, // Down -- End of cheating.
7, // R
6, // L
4, // X
@ -61,20 +51,20 @@ const u16 default_joypad_cfg[NB_KEYS] =
const u16 default_keyboard_cfg[NB_KEYS] =
{
97, // a
98, // b
65288, // backspace
65293, // enter
65363, // directional arrows
65361,
65362,
65364,
65454, // numeric .
65456, // numeric 0
120, // x
121, // y
112,
113
97, // a
98, // b
65288, // backspace
65293, // enter
65363, // directional arrows
65361,
65362,
65364,
65454, // numeric .
65456, // numeric 0
120, // x
121, // y
112,
113
};
/* Load default joystick and keyboard configurations */
@ -154,6 +144,34 @@ u16 inline lookup_key (u16 keyval) {
return Key;
}
/* Empty SDL Events' queue */
void clear_events()
{
SDL_Event event;
/* IMPORTANT: Reenable joystick events iif needed. */
if(SDL_JoystickEventState(SDL_QUERY) == SDL_IGNORE)
SDL_JoystickEventState(SDL_ENABLE);
#ifndef GTK_UI
sdl_quit = FALSE;
#endif
/* There's an event waiting to be processed? */
while (SDL_PollEvent(&event))
{
#ifndef GTK_UI
switch (event.type)
{
case SDL_QUIT:
sdl_quit = TRUE;
break;
}
#endif // !GTK_UI
}
return;
}
/* Get and set a new joystick key */
u16 get_set_joy_key(int index) {
BOOL done = FALSE;
@ -175,12 +193,47 @@ u16 get_set_joy_key(int index) {
break;
}
}
SDL_JoystickEventState(SDL_IGNORE);
if( SDL_JoystickEventState(SDL_QUERY) == SDL_ENABLE )
SDL_JoystickEventState(SDL_IGNORE);
joypad_cfg[index] = key;
return key;
}
/* Get and set a new joystick axis */
void get_set_joy_axis(int index, int index_o) {
BOOL done = FALSE;
SDL_Event event;
u16 key = joypad_cfg[index];
/* Clear events */
clear_events();
/* Enable joystick events if needed */
if( SDL_JoystickEventState(SDL_QUERY) == SDL_IGNORE )
SDL_JoystickEventState(SDL_ENABLE);
while(SDL_WaitEvent(&event) && !done)
{
switch(event.type)
{
case SDL_JOYAXISMOTION:
/* Discriminate small movements */
if( (event.jaxis.value >> 5) != 0 )
{
key = JOY_AXIS_(event.jaxis.axis);
done = TRUE;
}
break;
}
}
if( SDL_JoystickEventState(SDL_QUERY) == SDL_ENABLE )
SDL_JoystickEventState(SDL_IGNORE);
/* Update configuration */
joypad_cfg[index] = key;
joypad_cfg[index_o] = joypad_cfg[index];
}
#ifndef GTK_UI
/* Set mouse coordinates */
void set_mouse_coord(signed long x,signed long y)
@ -215,7 +268,6 @@ u16 get_keypad()
u16 process_ctrls_events(u16 keypad)
{
u16 key;
SDL_Event event;
/* IMPORTANT: Reenable joystick events iif needed. */
@ -234,32 +286,35 @@ u16 process_ctrls_events(u16 keypad)
/* Joystick axis motion
Note: button constants have a 1bit offset. */
case SDL_JOYAXISMOTION:
/* Horizontal */
if (event.jaxis.axis == 0)
if( event.jaxis.value == 0 )
{
key = KEYMASK_( KEY_RIGHT-1 ) | KEYMASK_( KEY_LEFT-1 );
RM_KEY( keypad, key );
}
else
{
if( event.jaxis.value > 0 ) key = KEYMASK_( KEY_RIGHT-1 );
else key = KEYMASK_( KEY_LEFT-1 );
key = lookup_joy_key( JOY_AXIS_(event.jaxis.axis) );
if( key != 0 )
{
if( event.jaxis.value == 0 )
{
switch(key)
{
case KEYMASK_( KEY_RIGHT-1 ):
key |= KEYMASK_( KEY_LEFT-1 );
break;
case KEYMASK_( KEY_UP-1 ):
key |= KEYMASK_( KEY_DOWN-1 );
break;
default:
printf("Unknown joystick state!\n");
break;
}
RM_KEY( keypad, key );
}
else if( (event.jaxis.value > 0) &&
(key == KEYMASK_( KEY_UP-1 )) )
key = KEYMASK_( KEY_DOWN-1 );
else if( (event.jaxis.value < 0) &&
(key == KEYMASK_( KEY_RIGHT-1 )) )
key = KEYMASK_( KEY_LEFT-1 );
if( (event.jaxis.value >> 5) != 0 )
ADD_KEY( keypad, key );
}
/* Vertical */
else if (event.jaxis.axis == 1)
if( event.jaxis.value == 0 )
{
key = KEYMASK_( KEY_UP-1 ) | KEYMASK_( KEY_DOWN-1 );
RM_KEY( keypad, key );
}
else
{
if( event.jaxis.value > 0 ) key = KEYMASK_( KEY_DOWN-1 );
else key = KEYMASK_( KEY_UP-1 );
ADD_KEY( keypad, key );
}
}
break;
/* Joystick button pressed */
@ -312,6 +367,7 @@ u16 process_ctrls_events(u16 keypad)
break;
}
}
return keypad;
}

View File

@ -35,6 +35,7 @@
#define ADD_KEY(keypad,key) ( (keypad) |= (key) )
#define RM_KEY(keypad,key) ( (keypad) &= ~(key) )
#define KEYMASK_(k) (1 << k)
#define JOY_AXIS_(k) ((k+1) << 8)
#define NB_KEYS 14
#define KEY_NONE 0
@ -84,6 +85,7 @@ void uninit_joy();
void set_joy_keys(u16 joyCfg[]);
void set_kb_keys(u16 kbCfg[]);
u16 get_set_joy_key(int index);
void get_set_joy_axis(int index, int index_opp);
void update_keypad(u16 keys);
u16 get_keypad();
u16 inline lookup_key (u16 keyval);

View File

@ -251,20 +251,8 @@ void init_joy_labels() {
int i;
char text[50], bname[30];
GtkButton *b;
for (i=0; i<4; i++) {
/* Key not configured */
if( joypad_cfg[i] == (u16)(-1) ) continue;
sprintf(text,"%s : %d\0\0",key_names[i],joypad_cfg[i]);
sprintf(bname,"button_joy_%s\0\0",key_names[i]);
b = (GtkButton*)glade_xml_get_widget(xml, bname);
gtk_button_set_label(b,text);
}
/* Skipping Axis */
for (i=8; i<NB_KEYS; i++) {
/* Key not configured */
if( joypad_cfg[i] == (u16)(-1) ) continue;
for (i=0; i<NB_KEYS; i++) {
if( joypad_cfg[i] == (u16)(-1) ) continue; /* Key not configured */
sprintf(text,"%s : %d\0\0",key_names[i],joypad_cfg[i]);
sprintf(bname,"button_joy_%s\0\0",key_names[i]);
b = (GtkButton*)glade_xml_get_widget(xml, bname);
@ -342,8 +330,9 @@ void ask_joy_key(GtkButton*b, int key)
char text[50];
u16 joykey;
GtkWidget * dlg = (GtkWidget*)glade_xml_get_widget(xml, "wJoyDlg");
key--; /* remove 1 to get index */
GtkDialog * dlg = (GtkDialog*)glade_xml_get_widget(xml, "wJoyDlg");
gtk_widget_show_now(dlg);
/* Need to force event processing. Otherwise, popup won't show up. */
while ( gtk_events_pending() ) gtk_main_iteration();
@ -353,23 +342,61 @@ void ask_joy_key(GtkButton*b, int key)
gtk_widget_hide((GtkWidget*)dlg);
}
void on_joy_button_A_clicked (GtkButton *b, gpointer user_data)
/* Joystick configuration / Key definition */
void ask_joy_axis(GtkButton*b, u8 key, u8 opposite_key)
{
char text[50];
char opposite_button[50];
u16 joykey;
GtkWidget * dlg;
GtkWidget * bo;
key--; /* remove 1 to get index */
opposite_key--;
sprintf(opposite_button,"button_joy_%s\0\0",key_names[opposite_key]);
dlg = (GtkWidget*)glade_xml_get_widget(xml, "wJoyDlg");
bo = (GtkWidget*)glade_xml_get_widget(xml, opposite_button);
gtk_widget_show(dlg);
/* Need to force event processing. Otherwise, popup won't show up. */
while ( gtk_events_pending() ) gtk_main_iteration();
get_set_joy_axis(key, opposite_key);
sprintf(text,"%s : %d\0\0",key_names[key],joypad_cfg[key]);
gtk_button_set_label(b,text);
sprintf(text,"%s : %d\0\0",key_names[opposite_key],joypad_cfg[opposite_key]);
gtk_button_set_label(bo,text);
gtk_widget_hide((GtkWidget*)dlg);
}
void on_button_joy_Left_clicked (GtkButton *b, gpointer user_data)
{ ask_joy_axis(b,KEY_LEFT,KEY_RIGHT); }
void on_button_joy_Up_clicked (GtkButton *b, gpointer user_data)
{ ask_joy_axis(b,KEY_UP,KEY_DOWN); }
void on_button_joy_Right_clicked (GtkButton *b, gpointer user_data)
{ ask_joy_axis(b,KEY_RIGHT,KEY_LEFT); }
void on_button_joy_Down_clicked (GtkButton *b, gpointer user_data)
{ ask_joy_axis(b,KEY_DOWN,KEY_UP); }
void on_button_joy_A_clicked (GtkButton *b, gpointer user_data)
{ ask_joy_key(b,KEY_A); }
void on_joy_button_B_clicked (GtkButton *b, gpointer user_data)
void on_button_joy_B_clicked (GtkButton *b, gpointer user_data)
{ ask_joy_key(b,KEY_B); }
void on_joy_button_X_clicked (GtkButton *b, gpointer user_data)
void on_button_joy_X_clicked (GtkButton *b, gpointer user_data)
{ ask_joy_key(b,KEY_X); }
void on_joy_button_Y_clicked (GtkButton *b, gpointer user_data)
void on_button_joy_Y_clicked (GtkButton *b, gpointer user_data)
{ ask_joy_key(b,KEY_Y); }
void on_joy_button_L_clicked (GtkButton *b, gpointer user_data)
void on_button_joy_L_clicked (GtkButton *b, gpointer user_data)
{ ask_joy_key(b,KEY_L); }
void on_joy_button_R_clicked (GtkButton *b, gpointer user_data)
void on_button_joy_R_clicked (GtkButton *b, gpointer user_data)
{ ask_joy_key(b,KEY_R); }
void on_joy_button_Select_clicked (GtkButton *b, gpointer user_data)
void on_button_joy_Select_clicked (GtkButton *b, gpointer user_data)
{ ask_joy_key(b,KEY_SELECT); }
void on_joy_button_Start_clicked (GtkButton *b, gpointer user_data)
void on_button_joy_Start_clicked (GtkButton *b, gpointer user_data)
{ ask_joy_key(b,KEY_START); }
void on_joy_button_Boost_clicked (GtkButton *b, gpointer user_data)
void on_button_joy_Boost_clicked (GtkButton *b, gpointer user_data)
{ ask_joy_key(b,KEY_BOOST); }
void on_joy_button_Debug_clicked (GtkButton *b, gpointer user_data)
void on_button_joy_Debug_clicked (GtkButton *b, gpointer user_data)
{ ask_joy_key(b,KEY_DEBUG); }

View File

@ -65,13 +65,17 @@ void on_button_Debug_clicked (GtkButton *button, gpointer user_data);
void on_button_Boost_clicked (GtkButton *button, gpointer user_data);
/* Joystick configuration / Key definition */
void on_joy_button_Y_clicked (GtkButton *button, gpointer user_data);
void on_joy_button_X_clicked (GtkButton *button, gpointer user_data);
void on_joy_button_A_clicked (GtkButton *button, gpointer user_data);
void on_joy_button_B_clicked (GtkButton *button, gpointer user_data);
void on_joy_button_Select_clicked (GtkButton *button, gpointer user_data);
void on_joy_button_L_clicked (GtkButton *button, gpointer user_data);
void on_joy_button_R_clicked (GtkButton *button, gpointer user_data);
void on_joy_button_Debug_clicked (GtkButton *button, gpointer user_data);
void on_joy_button_Start_clicked (GtkButton *button, gpointer user_data);
void on_joy_button_Boost_clicked (GtkButton *button, gpointer user_data);
void on_button_joy_Left_clicked (GtkButton *button, gpointer user_data);
void on_button_joy_Up_clicked (GtkButton *button, gpointer user_data);
void on_button_joy_Right_clicked (GtkButton *button, gpointer user_data);
void on_button_joy_Down_clicked (GtkButton *button, gpointer user_data);
void on_button_joy_Y_clicked (GtkButton *button, gpointer user_data);
void on_button_joy_X_clicked (GtkButton *button, gpointer user_data);
void on_button_joy_A_clicked (GtkButton *button, gpointer user_data);
void on_button_joy_B_clicked (GtkButton *button, gpointer user_data);
void on_button_joy_Select_clicked (GtkButton *button, gpointer user_data);
void on_button_joy_L_clicked (GtkButton *button, gpointer user_data);
void on_button_joy_R_clicked (GtkButton *button, gpointer user_data);
void on_button_joy_Debug_clicked (GtkButton *button, gpointer user_data);
void on_button_joy_Start_clicked (GtkButton *button, gpointer user_data);
void on_button_joy_Boost_clicked (GtkButton *button, gpointer user_data);

View File

@ -1523,7 +1523,7 @@
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_joy_button_Y_clicked" last_modification_time="Tue, 02 Jan 2007 10:53:47 GMT"/>
<signal name="clicked" handler="on_button_joy_Y_clicked" last_modification_time="Thu, 18 Jan 2007 06:29:16 GMT"/>
</widget>
<packing>
<property name="left_attach">4</property>
@ -1543,7 +1543,7 @@
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_joy_button_X_clicked" last_modification_time="Tue, 02 Jan 2007 10:53:39 GMT"/>
<signal name="clicked" handler="on_button_joy_X_clicked" last_modification_time="Thu, 18 Jan 2007 06:29:22 GMT"/>
</widget>
<packing>
<property name="left_attach">5</property>
@ -1563,7 +1563,7 @@
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_joy_button_A_clicked" last_modification_time="Tue, 02 Jan 2007 10:53:54 GMT"/>
<signal name="clicked" handler="on_button_joy_A_clicked" last_modification_time="Thu, 18 Jan 2007 06:29:40 GMT"/>
</widget>
<packing>
<property name="left_attach">6</property>
@ -1583,7 +1583,7 @@
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_joy_button_B_clicked" last_modification_time="Tue, 02 Jan 2007 10:54:00 GMT"/>
<signal name="clicked" handler="on_button_joy_B_clicked" last_modification_time="Thu, 18 Jan 2007 06:29:28 GMT"/>
</widget>
<packing>
<property name="left_attach">5</property>
@ -1603,7 +1603,7 @@
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_joy_button_Select_clicked" last_modification_time="Tue, 02 Jan 2007 10:53:17 GMT"/>
<signal name="clicked" handler="on_button_joy_Select_clicked" last_modification_time="Thu, 18 Jan 2007 06:29:03 GMT"/>
</widget>
<packing>
<property name="left_attach">3</property>
@ -1623,7 +1623,7 @@
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_joy_button_L_clicked" last_modification_time="Tue, 02 Jan 2007 10:52:28 GMT"/>
<signal name="clicked" handler="on_button_joy_L_clicked" last_modification_time="Thu, 18 Jan 2007 06:27:09 GMT"/>
</widget>
<packing>
<property name="left_attach">0</property>
@ -1643,7 +1643,7 @@
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_joy_button_R_clicked" last_modification_time="Tue, 02 Jan 2007 10:53:30 GMT"/>
<signal name="clicked" handler="on_button_joy_R_clicked" last_modification_time="Thu, 18 Jan 2007 06:29:34 GMT"/>
</widget>
<packing>
<property name="left_attach">5</property>
@ -1656,14 +1656,14 @@
</child>
<child>
<widget class="GtkButton" id="button_joy_DEBUG">
<widget class="GtkButton" id="button_joy_Debug">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="label" translatable="yes">Debug : </property>
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_joy_button_Debug_clicked" last_modification_time="Tue, 02 Jan 2007 11:44:01 GMT"/>
<signal name="clicked" handler="on_button_joy_Debug_clicked" last_modification_time="Thu, 18 Jan 2007 06:27:56 GMT"/>
</widget>
<packing>
<property name="left_attach">2</property>
@ -1683,7 +1683,7 @@
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_joy_button_Start_clicked" last_modification_time="Tue, 02 Jan 2007 10:53:09 GMT"/>
<signal name="clicked" handler="on_button_joy_Start_clicked" last_modification_time="Thu, 18 Jan 2007 06:28:56 GMT"/>
</widget>
<packing>
<property name="left_attach">3</property>
@ -1703,7 +1703,7 @@
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_joy_button_Boost_clicked" last_modification_time="Sat, 06 Jan 2007 13:54:50 GMT"/>
<signal name="clicked" handler="on_button_joy_Boost_clicked" last_modification_time="Thu, 18 Jan 2007 06:29:09 GMT"/>
</widget>
<packing>
<property name="left_attach">4</property>
@ -1716,18 +1716,78 @@
</child>
<child>
<widget class="GtkButton" id="button_Axis">
<widget class="GtkButton" id="button_joy_Up">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="label" translatable="yes">Axis :</property>
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_button_joy_Up_clicked" last_modification_time="Thu, 18 Jan 2007 05:57:40 GMT"/>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">2</property>
<property name="bottom_attach">3</property>
<property name="x_options">fill</property>
<property name="y_options"></property>
</packing>
</child>
<child>
<widget class="GtkButton" id="button_joy_Down">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="label" translatable="yes">Axis :</property>
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_button_joy_Down_clicked" last_modification_time="Thu, 18 Jan 2007 05:57:46 GMT"/>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">4</property>
<property name="bottom_attach">5</property>
<property name="x_options">fill</property>
<property name="y_options"></property>
</packing>
</child>
<child>
<widget class="GtkButton" id="button_joy_Left">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="label" translatable="yes">Axis :</property>
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_button_joy_Left_clicked" last_modification_time="Thu, 18 Jan 2007 05:57:55 GMT"/>
</widget>
<packing>
<property name="left_attach">0</property>
<property name="right_attach">1</property>
<property name="top_attach">3</property>
<property name="bottom_attach">4</property>
<property name="x_options">fill</property>
<property name="y_options"></property>
</packing>
</child>
<child>
<widget class="GtkButton" id="button_joy_Right">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="label" translatable="yes">Axis :</property>
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_button_joy_Right_clicked" last_modification_time="Thu, 18 Jan 2007 05:57:51 GMT"/>
</widget>
<packing>
<property name="left_attach">2</property>
<property name="right_attach">3</property>
<property name="top_attach">3</property>
<property name="bottom_attach">4</property>
<property name="x_options">fill</property>