sunnuntai 6. elokuuta 2017

Using Wiimote as a lightgun in MAME

Last time I wrote about how to get Wiimote to work as a mouse pointer in X. After that, I started to wonder if I could also use the Wiimote as a light gun in emulators, especially in MAME. MAME has some nice arcade shooters, like Time crisis, which would be much more fun with a proper pointer.

It should be quite simple, since MAME supports mouse as a light gun out of the box. Or so I thought. It turns out, it isn't so simple after all. I found a blog post which explained how to do it, but I couldn't get it to work.

First, I use autodetection in X.org, so I don't have xorg.conf. Anyways, since I already got X to detect the wiimote, I didn't think it would be a problem.
Second, even though e.g. jstest-gtk displayed the joystick, I couldn't get MAME to see it. No joysticks no matter what I tried. Also as a mouse the Wiimote acted weird. In desktop all was good, but in MAME cursor just jiggled in bottom right corner.

Anyways, after 3 evenings of trying I got it to work. Here's how it went:

First I connected the wiimote:
$ sudo wminput -c ir_ptr A4:C1:E2:C4:DB:4F
Put Wiimote in discoverable mode now (press 1+2)...
Ready.

Even though the jstest-gtk and sdl-jstest both displayed the wiimote as joystick and operated properly, the sdl2-jstest did not work. I assume MAME uses sdl2, so that explais why MAME did not see the Wiimote either.
$ sdl2-jstest --list
No joysticks were found


Then I decided to try different approach. I added my user to the "input" group, so that it can read /dev/input/eventXX which is where the Wiimote data comes from:
$ sudo usermod -G input [user]

After that I used evtest to check that the Wiimote really works, and what the event device number is:
$ evtest
No device specified, trying to scan all of /dev/input/event*
Not running as root, no devices may be available.
Available devices:

...
/dev/input/event14:     Nintendo Wiimote
Select the device event number [0-14]: 14
Input driver version is 1.0.1
Input device ID: bus 0x5 vendor 0x1 product 0x1 version 0x4
Input device name: "Nintendo Wiimote"

...

I entered 14 to the prompt (the number after the Wiimote event), and checked that events are reported when pointing the wiimote to a candle and moving it around. The events look like random blocks in the terminal.

After verifying what the event device is (in this case /dev/input/event14) , I forced SDL to use that device as the joystick device with an environmental variable, and behold, the Wiimote was detected as a joystick:
$ SDL_JOYSTICK_DEVICE=/dev/input/event14 sdl2-jstest --list
Found 1 joystick(s)

Joystick Name:     'Nintendo Wiimote'

...

And also testing it with the sdl2-jstest now worked properly. Great!
$ SDL_JOYSTICK_DEVICE=/dev/input/event14 sdl2-jstest --test 0

Now it was time to test with MAME. But first I wanted to disable the mouse operation of Wiimote so that it would not interfere with the joystick operation. I did this by finding the Wiimote device id with xinput, and then disabling that device:
$ xinput list
| Virtual core pointer             id=2    [master pointer  (3)]
|   ↳ Virtual core XTEST pointer   id=4    [slave  pointer  (2)]
|   ↳ Nintendo Wiimote             id=13   [slave  pointer  (2)]
| Virtual core keyboard            id=3    [master keyboard (2)]
    ↳ Virtual core XTEST keyboard  id=5    [slave  keyboard (3)]


$ xinput disable 13


After that the Wiimote did not move the mouse cursor anymore. Then I tried running MAME with these settings. I used -verbose mode to get debug info and piped it to less to be able to inspect the log. Now the Wiimote was dispalyed in the "Initializing Joysticks" portion.
$ SDL_JOYSTICK_DEVICE=/dev/input/event14 mame -verbose -sr 11025 timecrs2v2b | less

From the MAME menu (with TAB key) I could go to input and lightgun settings and set the lightgun 1 analog X and Y properly by moving the wiimote. The setting indicated something like JS1 axis 0 and JS 1 axis 1 if I remember correctly. The Wiimote worked properly as a lightgun!

I assume the problems come to the fact that the Wiimote operates as mouse and joystick simultaneously, why SDL does not really know which it is. That's why the device name has to be told explicitly. I also wonder if pointing it to /dev/input/js0 also, but I didn't test that yet.

For reference, here is the relevant parts from my ~/.mame/mame.ini to set the joystick to emulate lightgun:
# CORE INPUT OPTIONS
coin_lockout              1
ctrlr
mouse                     1
joystick                  1
lightgun                  1
multikeyboard             0
multimouse                0
steadykey                 0
offscreen_reload          1
joystick_map              auto
joystick_deadzone         0
joystick_saturation       0.85
natural                   0
joystick_contradictory    0
coin_impulse              0

# CORE INPUT AUTOMATIC ENABLE OPTIONS
paddle_device             joystick
adstick_device            keyboard
pedal_device              keyboard
dial_device               keyboard
trackball_device          keyboard
lightgun_device           joystick
positional_device         keyboard
mouse_device              mouse


And after storing the settings in MAME ingame menu, the following config in ~/.mame/cfg/timecris.cfg:
  <input>
    <port tag=":LIGHTX" type="P1_LIGHTGUN_X" mask="4095" defvalue="381">
      <newseq type="standard">
        JOYCODE_1_XAXIS
      </newseq>
    </port>
    <port tag=":LIGHTY" type="P1_LIGHTGUN_Y" mask="4095" defvalue="163">
      <newseq type="standard">
        JOYCODE_1_YAXIS
      </newseq>
    </port>
    <port tag=":MCUP5A" type="P1_BUTTON1" mask="16" defvalue="16">
      <newseq type="standard">
        JOYCODE_1_BUTTON2
      </newseq>
    </port>
    <port tag=":MCUP5A" type="P1_BUTTON2" mask="32" defvalue="32">
      <newseq type="standard">
        JOYCODE_1_BUTTON1
       </newseq>
    </port>
  </input>


Hopefully this will help someone to get the Wiimote to work in MAME. I also got the wiimote to work as a NES zapper in Mednafen, but I will write about it later.

Ei kommentteja:

Lähetä kommentti