From b765332c27d6214731fb9d00fbcb9ee4cb0a2c6c Mon Sep 17 00:00:00 2001 From: kichikuou Date: Sun, 13 Dec 2020 13:27:42 +0900 Subject: [PATCH] Enable specifying joystick device Before this, value of joy_device config parameter (-devjoy in command line) was ignored. After this, this parameter will take an integer value and will be used as the device_index argument of SDL_JoystickOpen(). This also removes obsolete doc/README.joystick. --- doc/README | 2 +- doc/README.joystick | 17 --------------- src/joystick.h | 2 +- src/joystick_dmy.c | 2 +- src/joystick_sdl.c | 52 ++++++++++++++++++++------------------------- src/xsys35rc.sample | 4 ++-- src/xsystem35.c | 7 +++--- 7 files changed, 31 insertions(+), 55 deletions(-) delete mode 100644 doc/README.joystick diff --git a/doc/README b/doc/README index d6858bf..843a99e 100644 --- a/doc/README +++ b/doc/README @@ -223,7 +223,7 @@ -Ms? : Sequencer device (?:device number) -M0 : disable midi - -devjoy name : joystick のデバイス名を name にします + -devjoy n : n 番目の joystick のデバイスを使用します。 -version : バージョン情報を表示します。 diff --git a/doc/README.joystick b/doc/README.joystick deleted file mode 100644 index 20b6094..0000000 --- a/doc/README.joystick +++ /dev/null @@ -1,17 +0,0 @@ - - Joystick の使い方について - - 現在 linux の kernel 2.2.x 以降の Joystick インターフェイスに対応して -います。対応環境をおもちの方は configure --enable-joy としてコンパイル -コンパイルして下さい。 - - Joystick のデバイス名はデフォルトでは /dev/js0 で、実行時に -devjoy -オプションで指定するか、.xsys35rc ファイルで joy_device のエントリー -に加えて下さい。 - - 他環境での動作報告、パッチなどお待ちしています。宛先は以下の両方に -お願いします。 - - chikama (masaki-c@is.aist-nara.ac.jp) - linux 版の作者の村田さん(fmurata@p1.tcnet.ne.jp) - diff --git a/src/joystick.h b/src/joystick.h index a80629c..0918ebf 100644 --- a/src/joystick.h +++ b/src/joystick.h @@ -25,7 +25,7 @@ #include "portab.h" -extern void joy_set_devicename(char *); +extern void joy_set_deviceindex(int); extern int joy_open(void); extern void joy_close(void); extern int joy_getinfo(void); diff --git a/src/joystick_dmy.c b/src/joystick_dmy.c index 5782850..cce55da 100644 --- a/src/joystick_dmy.c +++ b/src/joystick_dmy.c @@ -23,7 +23,7 @@ #include "joystick.h" #include "portab.h" -void joy_set_devicename(char *name) { +void joy_set_deviceindex(int index) { return; } diff --git a/src/joystick_sdl.c b/src/joystick_sdl.c index 4c5099e..db4a8a9 100644 --- a/src/joystick_sdl.c +++ b/src/joystick_sdl.c @@ -31,30 +31,35 @@ #include "joystick.h" #include "sdl_core.h" -static int joy_axes, joy_buttons; -static char *dev_joy = NULL; -static boolean joy_enable=TRUE; +static int device_index = -1; static SDL_Joystick *js; -void joy_set_devicename(char *name) { - if( dev_joy ) free(dev_joy); - dev_joy=strdup(name); +static boolean joy_open_index(int index) { + js = SDL_JoystickOpen(index); + if (!js) + return FALSE; + + const char *name = SDL_JoystickName(js); + int axes = SDL_JoystickNumAxes(js); + int buttons = SDL_JoystickNumButtons(js); + SDL_JoystickEventState(SDL_ENABLE); + printf("SDL joystick '%s' %d axes %d buttons\n", name, axes, buttons); + return TRUE; +} + +void joy_set_deviceindex(int index) { + device_index = index; } int joy_open(void) { - int i; - const char *name; - for( i=0 ; i= 0) { + return joy_open_index(device_index) ? 1 : -1; + } else { + for (int i = 0; i < SDL_NumJoysticks(); i++) { + if (joy_open_index(i)) + return 1; } } - joy_enable=FALSE; return -1; } @@ -62,19 +67,8 @@ void joy_close(void) { SDL_JoystickClose(js); } -#if 0 -boolean joy_getinfo(int *data) { - int rt = FALSE; - if( joy_enable ) { - *data=sdl_getjoyinfo(); - rt=TRUE; - } - return rt; -} -#endif - int joy_getinfo(void) { - if (joy_enable) { + if (js) { return sdl_getjoyinfo(); } return 0; diff --git a/src/xsys35rc.sample b/src/xsys35rc.sample index 921fea0..d44beb5 100644 --- a/src/xsys35rc.sample +++ b/src/xsys35rc.sample @@ -58,10 +58,10 @@ # ------------------------------------------------------------ # ------------------------------------------------------------ -# joystick device のデバイス名 +# joystick device のデバイス番号 (0-) # -#joy_device: /dev/js0 +#joy_device: 0 # ------------------------------------------------------------ diff --git a/src/xsystem35.c b/src/xsystem35.c index d77b389..4b86059 100644 --- a/src/xsystem35.c +++ b/src/xsystem35.c @@ -128,8 +128,7 @@ static void sys35_usage(boolean verbose) { #endif puts(" -M0 : Disable MIDI output"); - puts(" -devjoy device : set joystic device name to 'device'"); - puts(" if 'device' is set to 'none', don't use the device"); + puts(" -devjoy device : joystick device index (0-)"); puts(" -ttfont_mincho: set TrueType font for mincho"); puts(" -ttfont_gothic: set TrueType font for mincho"); @@ -306,7 +305,7 @@ static void sys35_ParseOption(int *argc, char **argv) { midi_set_output_device(argv[i][2] | subdev); } else if (0 == strcmp(argv[i], "-devjoy")) { if (argv[i + 1] != NULL) { - joy_set_devicename(argv[i + 1]); + joy_set_deviceindex(atoi(argv[i + 1])); } } else if (0 == strcmp(argv[i], "-fullscreen")) { fs_on = TRUE; @@ -377,7 +376,7 @@ static void check_profile() { /* joystick device name の設定 */ param = get_profile("joy_device"); if (param) { - joy_set_devicename(param); + joy_set_deviceindex(atoi(param)); } /* Raw MIDI device name の設定 */ param = get_profile("midi_device");