Add few more members.

This commit is contained in:
2025-03-17 22:53:38 -04:00
parent 6b10887599
commit 5f308e7b23
3 changed files with 51 additions and 32 deletions

View File

@@ -21,7 +21,7 @@ namespace jstick {
namespace Buttons {}
class Joystick {
Joystick();
Joystick(int hwid = 0);
public:
protected:
@@ -45,6 +45,8 @@ namespace jstick {
Unknown,
};
enum class XBoxButton : uint8_t
{
ButtonA = 0,
@@ -92,8 +94,15 @@ namespace jstick {
/// @note jstick supports only one device at a time, currently.
bool Connected(int hwid = 0);
/// Attempts to connect and initialize a joystick device with the given Hardware ID.
bool Connect(int hwid = 0);
int Connect(int hwid = 0);
bool Disconnect(int hwid = 0);
ControllerType GetDeviceTypeFromName(const std::string& name)
{
if (name == "Microsoft X-Box 360 pad")
return ControllerType::XBox;
}
std::string GetDeviceName(int hwid = 0);
int NumJoysticksDetected();
void JoystickServiceUpdate();
void ReadEventLoop();

View File

@@ -231,42 +231,45 @@ int main(int argc, char *argv[])
{
std::cout << jstick::NumJoysticksDetected() << std::endl;
bool success = jstick::Connect(0);
int hwid = jstick::Connect(0);
jstick::ButtonPressed += [](jstick::XBoxButton btn){
std::cout << std::format("Button Pressed: {}", (int)btn) << std::endl;
};
if (hwid != -1) {
jstick::ButtonReleased += [](jstick::XBoxButton btn) {
std::cout << std::format("Button Released: {}", (int)btn) << std::endl;
};
std::cout << jstick::GetDeviceName(hwid) << std::endl;
jstick::DPadMoved += [](Vector2 v) {
std::cout << std::format("DPad Moved {},{}", v.x, v.y) << std::endl;
};
jstick::LeftThumbstickMoved += [](Vector2 v)
{
std::cout << std::format("LThumb Moved {},{}", v.x, v.y) << std::endl;
};
jstick::ButtonPressed += [](jstick::XBoxButton btn){
std::cout << std::format("Button Pressed: {}", (int)btn) << std::endl;
};
jstick::RightThumbstickMoved += [](Vector2 v)
{
std::cout << std::format("RThumb Moved {},{}", v.x, v.y) << std::endl;
};
jstick::ButtonReleased += [](jstick::XBoxButton btn) {
std::cout << std::format("Button Released: {}", (int)btn) << std::endl;
};
jstick::LeftTriggerMoved += [] (short val)
{
std::cout << std::format("LTrigger Moved {}", val) << std::endl;
};
jstick::DPadMoved += [](Vector2 v) {
std::cout << std::format("DPad Moved {},{}", v.x, v.y) << std::endl;
};
jstick::RightTriggerMoved += [](short val)
{
std::cout << std::format("RTrigger Moved {}", val) << std::endl;
};
jstick::LeftThumbstickMoved += [](Vector2 v){
std::cout << std::format("LThumb Moved {},{}", v.x, v.y) << std::endl;
};
while(success)
{
jstick::RightThumbstickMoved += [](Vector2 v){
std::cout << std::format("RThumb Moved {},{}", v.x, v.y) << std::endl;
};
jstick::LeftTriggerMoved += [] (short val) {
std::cout << std::format("LTrigger Moved {}", val) << std::endl;
};
jstick::RightTriggerMoved += [](short val) {
std::cout << std::format("RTrigger Moved {}", val) << std::endl;
};
}
while(true) {
jstick::ReadEventLoop();
}

View File

@@ -63,7 +63,14 @@ bool jstick::Connected(int hwid) {
return js_connected && js_handle == hwid;
}
bool jstick::Connect(int hwid) {
std::string jstick::GetDeviceName(int hwid) {
char name_buf[128];
if (ioctl(hwid, JSIOCGNAME(sizeof(name_buf)), name_buf) < 0)
strncpy(name_buf, "Unknown", sizeof(name_buf));
return std::string(name_buf);
}
int jstick::Connect(int hwid) {
if (js_connected) {
std::cerr << "Joystick already connected! Support for multiple joysticks is in-progress!" << std::endl;
return false;
@@ -77,7 +84,7 @@ bool jstick::Connect(int hwid) {
js_connected = true;
JoystickConnected.Invoke(js_handle);
return true;
return js_handle;
}
bool jstick::Disconnect(int hwid) {