From 1d9b17bd18edd7c461b2f191e9b272cca027fbcd Mon Sep 17 00:00:00 2001 From: evilynux Date: Wed, 10 Jan 2007 04:34:56 +0000 Subject: [PATCH] Unified joystick controls. Both gtk and gtk-glade UIs use src/ctrlssdl.{h,x} for joystick support. --- desmume/src/gtk-glade/Makefile.am | 2 +- desmume/src/gtk-glade/desmume.c | 16 +++++++++++++--- desmume/src/gtk-glade/desmume.h | 3 +-- desmume/src/gtk-glade/globals.h | 4 +++- desmume/src/gtk-glade/main.c | 24 +++++------------------- desmume/src/gtk/Makefile.am | 10 +++++++++- desmume/src/gtk/desmume.c | 21 +++++++++++++++------ desmume/src/gtk/desmume.h | 2 +- desmume/src/gtk/globals.h | 5 +++-- desmume/src/gtk/main.c | 27 +++++++-------------------- 10 files changed, 58 insertions(+), 56 deletions(-) diff --git a/desmume/src/gtk-glade/Makefile.am b/desmume/src/gtk-glade/Makefile.am index 5dcbdff19..5220d27bf 100644 --- a/desmume/src/gtk-glade/Makefile.am +++ b/desmume/src/gtk-glade/Makefile.am @@ -3,7 +3,7 @@ desmume_glade_SOURCES = \ callbacks.c callbacks.h callbacks_IO.c callbacks_IO.h desmume.c desmume.h \ globals.h keyval_names.c main.c printscreen.c \ dTools/callbacks_1_ioregs.c dTools/callbacks_dtools.h \ - ../sndsdl.h ../sndsdl.c joysdl.h joysdl.c + ../sndsdl.h ../sndsdl.c ../ctrlssdl.h ../ctrlssdl.c desmume_glade_LDADD = ../libdesmume.a $(SDL_LIBS) $(LIBGLADE_LIBS) desmume_glade_CFLAGS = $(SDL_CFLAGS) $(LIBGLADE_CFLAGS) desmume_glade_LDFLAGS = -rdynamic diff --git a/desmume/src/gtk-glade/desmume.c b/desmume/src/gtk-glade/desmume.c index 253fc6eda..3411fc66e 100755 --- a/desmume/src/gtk-glade/desmume.c +++ b/desmume/src/gtk-glade/desmume.c @@ -86,9 +86,19 @@ BOOL desmume_running() void desmume_cycle() { - process_joy_events(); - desmume_last_cycle = NDS_exec((560190 << 1) - desmume_last_cycle, FALSE); - SPU_Emulate(); + u16 keypad; + /* Joystick events */ + /* Retrieve old value: can use joysticks w/ another device (from our side) */ + keypad = ~((unsigned short *)MMU.ARM7_REG)[0x130>>1]; + keypad = (keypad & 0x3) << 10; + keypad |= ~((unsigned short *)ARM9Mem.ARM9_REG)[0x130>>1] & 0x3FF; + /* Look for queued events */ + keypad = process_ctrls_events(keypad); + /* Update keypad value */ + desmume_keypad(keypad); + + desmume_last_cycle = NDS_exec((560190 << 1) - desmume_last_cycle, FALSE); + SPU_Emulate(); } void desmume_keypad(u16 k) diff --git a/desmume/src/gtk-glade/desmume.h b/desmume/src/gtk-glade/desmume.h index 3829948f0..dc9f6ff94 100755 --- a/desmume/src/gtk-glade/desmume.h +++ b/desmume/src/gtk-glade/desmume.h @@ -20,7 +20,6 @@ #ifndef __DESMUME_H__ #define __DESMUME_H__ - #define DESMUME_NB_KEYS 14 #define DESMUME_KEYMASK_(k) (1 << k) @@ -42,7 +41,7 @@ #define DESMUME_KEY_DEBUG 13 #define DESMUME_KEY_BOOST 14 -#include "joysdl.h" +#include "globals.h" extern void desmume_init(); extern void desmume_free(); diff --git a/desmume/src/gtk-glade/globals.h b/desmume/src/gtk-glade/globals.h index 6f71dd275..b6f081c10 100755 --- a/desmume/src/gtk-glade/globals.h +++ b/desmume/src/gtk-glade/globals.h @@ -22,6 +22,8 @@ #ifndef __GLOBALS_H__ #define __GLOBALS_H__ +#define GTK + #include #include #include @@ -39,13 +41,13 @@ #include "../NDSSystem.h" #include "../cflash.h" #include "../sndsdl.h" +#include "../ctrlssdl.h" #include "../types.h" #include "desmume.h" uint Frameskip; gint Keypad_Config[DESMUME_NB_KEYS]; gint Keypad_Temp[DESMUME_NB_KEYS]; -u16 Joypad_Config[DESMUME_NB_KEYS]; /* main.c */ GtkWidget * pWindow; diff --git a/desmume/src/gtk-glade/main.c b/desmume/src/gtk-glade/main.c index 183893098..d48847be7 100755 --- a/desmume/src/gtk-glade/main.c +++ b/desmume/src/gtk-glade/main.c @@ -164,8 +164,6 @@ int WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgumen int main(int argc, char *argv[]) { const char *commandLine_File = NULL; - int nbJoysticks; - int i; if(argc == 2) commandLine_File = argv[1]; @@ -174,34 +172,20 @@ int main(int argc, char *argv[]) { #endif init_keyvals(); - memcpy(Joypad_Config, Default_Joypad_Config, sizeof(Joypad_Config)); - gtk_init(&argc, &argv); - if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) == -1) + if(SDL_Init(SDL_INIT_VIDEO) == -1) { fprintf(stderr, "Error trying to initialize SDL: %s\n", SDL_GetError()); return 1; } desmume_init(); + /* Initialize joysticks */ + if(!init_joy(Default_Joypad_Config)) return 1; CONFIG_FILE = g_build_filename(g_get_home_dir(), ".desmume.ini", NULL); Read_ConfigFile(); - /* Initialize joysticks */ - nbJoysticks = SDL_NumJoysticks(); - printf("Nbr of joysticks: %d\n\n", nbJoysticks); - - for (i = 0; i < nbJoysticks; i++) - { - SDL_Joystick * joy = SDL_JoystickOpen(i); - printf("Joystick %s\n", i, SDL_JoystickName(i)); - printf("Axes: %d\n", SDL_JoystickNumAxes(joy)); - printf("Buttons: %d\n", SDL_JoystickNumButtons(joy)); - printf("Trackballs: %d\n", SDL_JoystickNumBalls(joy)); - printf("Hats: %d\n\n", SDL_JoystickNumHats(joy)); - } - /* load the interface */ xml = glade_xml_new("glade/DeSmuMe.glade", NULL, NULL); xml_tools = glade_xml_new("glade/DeSmuMe_Dtools.glade", NULL, NULL); @@ -236,6 +220,8 @@ int main(int argc, char *argv[]) { #ifdef DEBUG LogStop(); #endif + /* Unload joystick */ + uninit_joy(); SDL_Quit(); Write_ConfigFile(); diff --git a/desmume/src/gtk/Makefile.am b/desmume/src/gtk/Makefile.am index c8f27d621..beadc4674 100644 --- a/desmume/src/gtk/Makefile.am +++ b/desmume/src/gtk/Makefile.am @@ -1,5 +1,13 @@ bin_PROGRAMS = desmume -desmume_SOURCES = gtk-compat.c gtk-compat.h desmume.h desmume.c dToolsList.c tools/ioregsView.c globals.h ../sndsdl.c joysdl.h joysdl.c main.c +desmume_SOURCES = \ + gtk-compat.c gtk-compat.h \ + desmume.h desmume.c \ + dToolsList.c \ + tools/ioregsView.c \ + globals.h \ + ../sndsdl.c \ + ../ctrlssdl.h ../ctrlssdl.c \ + main.c desmume_LDADD = ../libdesmume.a $(SDL_LIBS) $(GTK_LIBS) desmume_CFLAGS = $(SDL_CFLAGS) $(GTK_CFLAGS) #desmume_LDFLAGS = -rdynamic diff --git a/desmume/src/gtk/desmume.c b/desmume/src/gtk/desmume.c index 1a8019620..1440ed036 100644 --- a/desmume/src/gtk/desmume.c +++ b/desmume/src/gtk/desmume.c @@ -62,16 +62,25 @@ BOOL desmume_running() void desmume_cycle() { - process_joy_events(); - desmume_last_cycle = NDS_exec((560190 << 1) - desmume_last_cycle, FALSE); - SPU_Emulate(); + u16 keypad; + /* Joystick events */ + /* Retrieve old value: can use joysticks w/ another device (from our side) */ + keypad = ~((unsigned short *)MMU.ARM7_REG)[0x130>>1]; + keypad = (keypad & 0x3) << 10; + keypad |= ~((unsigned short *)ARM9Mem.ARM9_REG)[0x130>>1] & 0x3FF; + /* Look for queued events */ + keypad = process_ctrls_events(keypad); + /* Update keypad value */ + desmume_keypad(keypad); + + desmume_last_cycle = NDS_exec((560190 << 1) - desmume_last_cycle, FALSE); + SPU_Emulate(); } void desmume_keypad(u16 k) { - printf("Update keypad...%x\n", k); - unsigned short k_ext = (k >> 10) & 0x3; - unsigned short k_pad = k & 0x3FF; + unsigned short k_ext = (~k >> 10) & 0x3; + unsigned short k_pad = ~k & 0x3FF; ((unsigned short *)ARM9Mem.ARM9_REG)[0x130>>1] = k_pad; ((unsigned short *)MMU.ARM7_REG)[0x130>>1] = k_pad; MMU.ARM7_REG[0x136] = (k_ext & 0x3) | (MMU.ARM7_REG[0x136] & ~0x3); diff --git a/desmume/src/gtk/desmume.h b/desmume/src/gtk/desmume.h index 45036a78c..fb9b90ba0 100644 --- a/desmume/src/gtk/desmume.h +++ b/desmume/src/gtk/desmume.h @@ -20,7 +20,7 @@ #define DESMUME_KEY_Y 12 #define DESMUME_KEY_DEBUG 13 -#include "joysdl.h" +#include "globals.h" extern void desmume_init(); extern void desmume_free(); diff --git a/desmume/src/gtk/globals.h b/desmume/src/gtk/globals.h index b629430c3..266966fd5 100644 --- a/desmume/src/gtk/globals.h +++ b/desmume/src/gtk/globals.h @@ -23,6 +23,8 @@ #ifndef GLOBALS_H #define GLOBALS_H +#define GTK + #include #include #include @@ -39,9 +41,8 @@ #include "../NDSSystem.h" #include "../cflash.h" #include "../sndsdl.h" +#include "../ctrlssdl.h" #include "../types.h" #include "desmume.h" -u16 Joypad_Config[DESMUME_NB_KEYS]; - #endif /* GLOBALS_H */ diff --git a/desmume/src/gtk/main.c b/desmume/src/gtk/main.c index e1180c70a..be59c2ebb 100644 --- a/desmume/src/gtk/main.c +++ b/desmume/src/gtk/main.c @@ -421,7 +421,7 @@ static gint Key_Press(GtkWidget *w, GdkEventKey *e) { Key = DESMUME_KEYMASK_(i); Cur_Keypad &= ~Key; - if(desmume_running()) desmume_keypad(Cur_Keypad); + if(desmume_running()) desmume_keypad(~Cur_Keypad); } //fprintf(stderr,"P:%d(%d)->%X => [%X]\n", e->keyval, e->hardware_keycode, Key, Cur_Keypad); @@ -439,7 +439,7 @@ static gint Key_Release(GtkWidget *w, GdkEventKey *e) { Key = DESMUME_KEYMASK_(i); Cur_Keypad |= Key; - if(desmume_running()) desmume_keypad(Cur_Keypad); + if(desmume_running()) desmume_keypad(~Cur_Keypad); } //fprintf(stderr,"R:%d(%d)->%X => [%X]\n", e->keyval, e->hardware_keycode, Key, Cur_Keypad); @@ -993,7 +993,6 @@ static void dui_set_accel_group(gpointer action, gpointer group) { int main (int argc, char *argv[]) { - int nbJoysticks; int i; const char *commandLine_File = NULL; @@ -1008,34 +1007,20 @@ int main (int argc, char *argv[]) #ifdef DEBUG LogStart(); #endif - - memcpy(Joypad_Config, Default_Joypad_Config, sizeof(Joypad_Config)); gtk_init(&argc, &argv); - if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) == -1) + if(SDL_Init(SDL_INIT_VIDEO) == -1) { fprintf(stderr, "Error trying to initialize SDL: %s\n", SDL_GetError()); return 1; } desmume_init(); + /* Initialize joysticks */ + if(!init_joy(Default_Joypad_Config)) return 1; dTools_running = (BOOL*)malloc(sizeof(BOOL) * dTools_list_size); for(i=0; i