Fix Win32 joystick driver to compile
[lsnes.git] / src / platform / win32mm / joystick.cpp
blob6d971b154f5c4d6fa64bf1f70cd747c180f15feb
1 #include "core/command.hpp"
2 #include "core/framerate.hpp"
3 #include "core/keymapper.hpp"
4 #include "core/joystickapi.hpp"
5 #include "core/window.hpp"
6 #include "library/minmax.hpp"
7 #include "library/string.hpp"
9 #include <windows.h>
10 #include <mmsystem.h>
11 #include <regstr.h>
12 #include <cstdlib>
13 #include <string>
14 #include <sstream>
15 #include <iostream>
16 #include <stdexcept>
17 #include <sys/time.h>
18 #include <unistd.h>
20 namespace
22 volatile bool quit_signaled;
23 volatile bool quit_ack;
24 std::map<unsigned, unsigned> idx_to_jid;
25 unsigned joysticks;
27 const char* buttonnames[] = {"Button1", "Button2", "Button3", "Button4", "Button5", "Button6", "Button7",
28 "Button8", "Button9", "Button10", "Button11", "Button12", "Button13", "Button14", "Button15",
29 "Button16", "Button17", "Button18", "Button19", "Button20", "Button21", "Button22", "Button23",
30 "Button24", "Button25", "Button26", "Button27", "Button28", "Button29", "Button30", "Button31",
31 "Button32"};
32 #define POLL_WAIT 20000
34 struct _joystick_driver drv = {
35 .init = []() -> void {
36 unsigned max_joysticks = joyGetNumDevs();
37 if(!max_joysticks)
38 return; //No joystick support.
39 joysticks = max_joysticks;
40 for(unsigned i = 0; i < max_joysticks; i++) {
41 JOYINFOEX info;
42 JOYCAPS caps;
43 info.dwSize = sizeof(info);
44 info.dwFlags = JOY_RETURNALL;
45 if(joyGetPosEx(i, &info) != JOYERR_NOERROR)
46 continue; //Not usable.
47 if(joyGetDevCaps(i, &caps, sizeof(caps)) != JOYERR_NOERROR)
48 continue; //Not usable.
49 idx_to_jid[i] = lsnes_gamepads.add(caps.szPname);
50 hw_gamepad& ngp = lsnes_gamepads[idx_to_jid[i]];
51 if(caps.wCaps & JOYCAPS_HASPOV)
52 ngp.add_hat(0, "POV");
53 for(unsigned j = 0; j < caps.wNumButtons && j < 32; j++)
54 ngp.add_button(j, buttonnames[j]);
55 ngp.add_axis(0, caps.wXmin, caps.wXmax, false, "X");
56 ngp.add_axis(1, caps.wYmin, caps.wYmax, false, "Y");
57 if(caps.wCaps & JOYCAPS_HASZ) ngp.add_axis(2, caps.wZmin, caps.wZmax, false, "Z");
58 if(caps.wCaps & JOYCAPS_HASR) ngp.add_axis(3, caps.wRmin, caps.wRmax, false,
59 "Rudder");
60 if(caps.wCaps & JOYCAPS_HASU) ngp.add_axis(4, caps.wUmin, caps.wUmax, false, "U");
61 if(caps.wCaps & JOYCAPS_HASV) ngp.add_axis(5, caps.wVmin, caps.wVmax, false, "V");
62 messages << "Joystick #" << idx_to_jid[i] << " online: " << caps.szPname << std::endl;
64 quit_ack = quit_signaled = false;
66 .quit = []() -> void {
67 quit_signaled = true;
68 while(!quit_ack);
70 .thread_fn = []() -> void {
71 while(!quit_signaled) {
72 for(unsigned i = 0; i < joysticks; i++) {
73 JOYINFOEX info;
74 info.dwSize = sizeof(info);
75 info.dwFlags = JOY_RETURNALL;
76 if(joyGetPosEx(i, &info) != JOYERR_NOERROR)
77 continue; //Not usable.
78 lsnes_gamepads[idx_to_jid[i]].report_hat(0, info.dwPOV);
79 for(unsigned j = 0; j < 32; j++)
80 lsnes_gamepads[idx_to_jid[i]].report_button(j,
81 (info.dwButtons >> j) & 1);
82 lsnes_gamepads[idx_to_jid[i]].report_axis(0, info.dwXpos);
83 lsnes_gamepads[idx_to_jid[i]].report_axis(1, info.dwYpos);
84 lsnes_gamepads[idx_to_jid[i]].report_axis(2, info.dwZpos);
85 lsnes_gamepads[idx_to_jid[i]].report_axis(3, info.dwRpos);
86 lsnes_gamepads[idx_to_jid[i]].report_axis(4, info.dwUpos);
87 lsnes_gamepads[idx_to_jid[i]].report_axis(5, info.dwVpos);
89 usleep(POLL_WAIT);
91 quit_ack = true;
93 .signal = []() -> void {
94 quit_signaled = true;
95 while(!quit_ack);
97 .name = []() -> const char* { return "Win32mm joystick plugin"; }
99 struct joystick_driver _drv(drv);