Upload UI
[lsnes.git] / src / platform / wxwidgets / joystick.cpp
blob15cc53f565c37dab43d7d93c884385c51f53dfad
1 #ifdef WXWIDGETS_JOYSTICK_SUPPORT
2 #include "core/command.hpp"
3 #include "core/framerate.hpp"
4 #include "core/keymapper.hpp"
5 #include "core/joystickapi.hpp"
6 #include "core/window.hpp"
7 #include "library/minmax.hpp"
8 #include "library/string.hpp"
9 #include <wx/timer.h>
10 #include <wx/joystick.h>
13 #define POLL_WAIT 20000
14 #include <cstdlib>
15 #include <string>
16 #include <sstream>
17 #include <iostream>
18 #include <stdexcept>
19 #include <sys/time.h>
20 #include <unistd.h>
22 namespace
24 volatile bool ready;
25 unsigned joysticks;
26 std::map<unsigned, wxJoystick*> objs;
28 const char* buttonnames[] = {"Button1", "Button2", "Button3", "Button4", "Button5", "Button6", "Button7",
29 "Button8", "Button9", "Button10", "Button11", "Button12", "Button13", "Button14", "Button15",
30 "Button16", "Button17", "Button18", "Button19", "Button20", "Button21", "Button22", "Button23",
31 "Button24", "Button25", "Button26", "Button27", "Button28", "Button29", "Button30", "Button31",
32 "Button32"};
34 struct joystick_timer : public wxTimer
36 joystick_timer() { start(); }
37 void start() { Start(POLL_WAIT / 1000); }
38 void stop() { Stop(); }
39 void Notify()
41 if(!ready)
42 return;
43 for(auto i : objs) {
44 wxJoystick& j = *i.second;
45 lsnes_gamepads[i.first].report_hat(0, j.GetPOVCTSPosition());
46 uint32_t bmask = j.GetButtonState();
47 for(unsigned j = 0; j < 32; j++)
48 lsnes_gamepads[i.first].report_button(j, (bmask >> j) & 1);
49 wxPoint xy = j.GetPosition();
50 lsnes_gamepads[i.first].report_axis(0, xy.x);
51 lsnes_gamepads[i.first].report_axis(1, xy.y);
52 lsnes_gamepads[i.first].report_axis(2, j.GetZPosition());
53 lsnes_gamepads[i.first].report_axis(3, j.GetRudderPosition());
54 lsnes_gamepads[i.first].report_axis(4, j.GetUPosition());
55 lsnes_gamepads[i.first].report_axis(5, j.GetVPosition());
58 }* jtimer;
60 struct _joystick_driver drv = {
61 .init = []() -> void {
62 unsigned max_joysticks = wxJoystick::GetNumberJoysticks();
63 if(!max_joysticks)
64 return; //No joystick support.
65 for(unsigned i = 0; i < max_joysticks; i++) {
66 wxJoystick* joy = new wxJoystick(i);
67 if(!joy->IsOk()) {
68 //Not usable.
69 delete joy;
70 continue;
72 unsigned jid = lsnes_gamepads.add(joy->GetProductName());
73 hw_gamepad& ngp = lsnes_gamepads[jid];
74 objs[jid] = joy;
76 if(joy->HasPOV())
77 ngp.add_hat(0, "POV");
78 for(unsigned j = 0; j < joy->GetNumberButtons() && j < 32; j++)
79 ngp.add_button(j, buttonnames[j]);
80 const char* R = "Rudder";
81 ngp.add_axis(0, joy->GetXMin(), joy->GetXMax(), false, "X");
82 ngp.add_axis(1, joy->GetYMin(), joy->GetYMax(), false, "Y");
83 if(joy->HasZ()) ngp.add_axis(2, joy->GetZMin(), joy->GetZMax(), false, "Z");
84 if(joy->HasRudder()) ngp.add_axis(3, joy->GetRudderMin(), joy->GetRudderMax(),
85 false, R);
86 if(joy->HasU()) ngp.add_axis(4, joy->GetUMin(), joy->GetUMax(), false, "U");
87 if(joy->HasV()) ngp.add_axis(5, joy->GetVMin(), joy->GetVMax(), false, "V");
88 messages << "Joystick #" << jid << " online: " << joy->GetProductName() << std::endl;
90 ready = true;
91 jtimer = new joystick_timer();
93 .quit = []() -> void {
94 if(jtimer)
95 jtimer->stop();
96 delete jtimer;
97 jtimer = NULL;
98 ready = false;
99 for(auto i : objs)
100 delete i.second;
101 usleep(50000);
103 //We don't poll in this thread, so just quit instantly.
104 .thread_fn = []() -> void {},
105 .signal = []() -> void {},
106 .name = []() -> const char* { return "Wxwidgets joystick plugin"; }
108 struct joystick_driver _drv(drv);
111 #endif