bsnes/ruby/input/joypad/udev.cpp
Tim Allen e1223366a7 Update to v103r22 release.
byuu says:

Changelog:

  - ruby: ported all remaining drivers to new API¹
  - ruby/wasapi: fix for dropping one sample per period [SuperMikeMan]
  - gb: emulated most of the TAMA RTC; but RTC state is still volatile²

¹: the new ports are:

  - audio/{directsound, alsa, pulseaudio, pulseaudiosimple, ao}
  - input/{udev, quartz, carbon}

It's pretty much guaranteed many of them will have compilation errors.
Please paste the error logs and I'll try to fix them up. It may take a
WIP or two to get there.

It's also possible things broke from the updates. If so, I could use
help comparing the old file to the new file, looking for mistakes, since
I can't test on these platforms apart from audio/directsound.

Please report working drivers in this list, so we can mark them off the
list. I'll need both macOS and Linux testers.

audio/directsound.cpp:112:

    if(DirectSoundCreate(0, &_interface, 0) != DS_OK) return terminate(), false;

²: once I get this working, I'll add load/save support for the RTC
values. For now, the RTC data will be lost when you close the emulator.

Right now, you can set the date/time in real-time mode, and when you
start the game, the time will be correct, and the time will tick
forward. Note that it runs off emulated time instead of actual real
time, so if you fast-forward to 300%, one minute will be 20 seconds.

The really big limitation right now is that when you exit the game, and
restart it, and resume a new game, the hour spot gets corrupted, and
this seems to instantly kill your pet. Fun. This is crazy because the
commands the game sends to the TAMA interface are identical between
starting a new game and getting in-game versus loading a game.

It's likely going to require disassembling the game's code and seeing
what in the hell it's doing, but I am extremely bad at LR35092 assembly.
Hopefully endrift can help here :|
2017-07-28 21:42:24 +10:00

280 lines
8.9 KiB
C++

#ifndef RUBY_INPUT_JOYPAD_UDEV
#define RUBY_INPUT_JOYPAD_UDEV
struct InputJoypadUdev {
Input& input;
InputJoypadUdev(Input& input) : input(input) {}
udev* context = nullptr;
udev_monitor* monitor = nullptr;
udev_enumerate* enumerator = nullptr;
udev_list_entry* devices = nullptr;
udev_list_entry* item = nullptr;
struct JoypadInput {
int code = 0;
uint id = 0;
int16_t value = 0;
input_absinfo info;
JoypadInput() {}
JoypadInput(int code) : code(code) {}
JoypadInput(int code, uint id) : code(code), id(id) {}
bool operator< (const JoypadInput& source) const { return code < source.code; }
bool operator==(const JoypadInput& source) const { return code == source.code; }
};
struct Joypad {
shared_pointer<HID::Joypad> hid{new HID::Joypad};
int fd = -1;
dev_t device = 0;
string deviceName;
string deviceNode;
uint8_t evbit[(EV_MAX + 7) / 8] = {0};
uint8_t keybit[(KEY_MAX + 7) / 8] = {0};
uint8_t absbit[(ABS_MAX + 7) / 8] = {0};
uint8_t ffbit[(FF_MAX + 7) / 8] = {0};
uint effects = 0;
string name;
string manufacturer;
string product;
string serial;
string vendorID;
string productID;
set<JoypadInput> axes;
set<JoypadInput> hats;
set<JoypadInput> buttons;
bool rumble = false;
uint effectID = 0;
};
vector<Joypad> joypads;
auto assign(shared_pointer<HID::Joypad> hid, uint groupID, uint inputID, int16_t value) -> void {
auto& group = hid->group(groupID);
if(group.input(inputID).value() == value) return;
input.doChange(hid, groupID, inputID, group.input(inputID).value(), value);
group.input(inputID).setValue(value);
}
auto poll(vector<shared_pointer<HID::Device>>& devices) -> void {
while(hotplugDevicesAvailable()) hotplugDevice();
for(auto& jp : joypads) {
input_event events[32];
int length = 0;
while((length = read(jp.fd, events, sizeof(events))) > 0) {
length /= sizeof(input_event);
for(uint i : range(length)) {
int code = events[i].code;
int type = events[i].type;
int value = events[i].value;
if(type == EV_ABS) {
if(auto input = jp.axes.find({code})) {
int range = input().info.maximum - input().info.minimum;
value = (value - input().info.minimum) * 65535ll / range - 32767;
assign(jp.hid, HID::Joypad::GroupID::Axis, input().id, sclamp<16>(value));
} else if(auto input = jp.hats.find({code})) {
int range = input().info.maximum - input().info.minimum;
value = (value - input().info.minimum) * 65535ll / range - 32767;
assign(jp.hid, HID::Joypad::GroupID::Hat, input().id, sclamp<16>(value));
}
} else if(type == EV_KEY) {
if(code >= BTN_MISC) {
if(auto input = jp.buttons.find({code})) {
assign(jp.hid, HID::Joypad::GroupID::Button, input().id, (bool)value);
}
}
}
}
}
devices.append(jp.hid);
}
}
auto rumble(uint64_t id, bool enable) -> bool {
for(auto& jp : joypads) {
if(jp.hid->id() != id) continue;
if(!jp.hid->rumble()) continue;
input_event play;
memset(&play, 0, sizeof(input_event));
play.type = EV_FF;
play.code = jp.effectID;
play.value = enable;
auto unused = write(jp.fd, &play, sizeof(input_event));
return true;
}
return false;
}
auto initialize() -> bool {
context = udev_new();
if(context == nullptr) return false;
monitor = udev_monitor_new_from_netlink(context, "udev");
if(monitor) {
udev_monitor_filter_add_match_subsystem_devtype(monitor, "input", nullptr);
udev_monitor_enable_receiving(monitor);
}
enumerator = udev_enumerate_new(context);
if(enumerator) {
udev_enumerate_add_match_property(enumerator, "ID_INPUT_JOYSTICK", "1");
udev_enumerate_scan_devices(enumerator);
devices = udev_enumerate_get_list_entry(enumerator);
for(udev_list_entry* item = devices; item != nullptr; item = udev_list_entry_get_next(item)) {
string name = udev_list_entry_get_name(item);
udev_device* device = udev_device_new_from_syspath(context, name);
string deviceNode = udev_device_get_devnode(device);
if(deviceNode) createJoypad(device, deviceNode);
udev_device_unref(device);
}
}
return true;
}
auto terminate() -> void {
if(enumerator) { udev_enumerate_unref(enumerator); enumerator = nullptr; }
}
private:
auto hotplugDevicesAvailable() -> bool {
pollfd fd = {0};
fd.fd = udev_monitor_get_fd(monitor);
fd.events = POLLIN;
return (::poll(&fd, 1, 0) == 1) && (fd.revents & POLLIN);
}
auto hotplugDevice() -> void {
udev_device* device = udev_monitor_receive_device(monitor);
if(device == nullptr) return;
string value = udev_device_get_property_value(device, "ID_INPUT_JOYSTICK");
string action = udev_device_get_action(device);
string deviceNode = udev_device_get_devnode(device);
if(value == "1") {
if(action == "add") {
createJoypad(device, deviceNode);
}
if(action == "remove") {
removeJoypad(device, deviceNode);
}
}
}
auto createJoypad(udev_device* device, const string& deviceNode) -> void {
Joypad jp;
jp.deviceNode = deviceNode;
struct stat st;
if(stat(deviceNode, &st) < 0) return;
jp.device = st.st_rdev;
jp.fd = open(deviceNode, O_RDWR | O_NONBLOCK);
if(jp.fd < 0) return;
uint8_t evbit[(EV_MAX + 7) / 8] = {0};
uint8_t keybit[(KEY_MAX + 7) / 8] = {0};
uint8_t absbit[(ABS_MAX + 7) / 8] = {0};
ioctl(jp.fd, EVIOCGBIT(0, sizeof(jp.evbit)), jp.evbit);
ioctl(jp.fd, EVIOCGBIT(EV_KEY, sizeof(jp.keybit)), jp.keybit);
ioctl(jp.fd, EVIOCGBIT(EV_ABS, sizeof(jp.absbit)), jp.absbit);
ioctl(jp.fd, EVIOCGBIT(EV_FF, sizeof(jp.ffbit)), jp.ffbit);
ioctl(jp.fd, EVIOCGEFFECTS, &jp.effects);
#define testBit(buffer, bit) (buffer[(bit) >> 3] & 1 << ((bit) & 7))
if(testBit(jp.evbit, EV_KEY)) {
if(udev_device* parent = udev_device_get_parent_with_subsystem_devtype(device, "input", nullptr)) {
jp.name = udev_device_get_sysattr_value(parent, "name");
jp.vendorID = udev_device_get_sysattr_value(parent, "id/vendor");
jp.productID = udev_device_get_sysattr_value(parent, "id/product");
if(udev_device* root = udev_device_get_parent_with_subsystem_devtype(parent, "usb", "usb_device")) {
if(jp.vendorID == udev_device_get_sysattr_value(root, "idVendor")
&& jp.productID == udev_device_get_sysattr_value(root, "idProduct")
) {
jp.deviceName = udev_device_get_devpath(root);
jp.manufacturer = udev_device_get_sysattr_value(root, "manufacturer");
jp.product = udev_device_get_sysattr_value(root, "product");
jp.serial = udev_device_get_sysattr_value(root, "serial");
}
}
}
uint axes = 0;
uint hats = 0;
uint buttons = 0;
for(int i = 0; i < ABS_MISC; i++) {
if(testBit(jp.absbit, i)) {
if(i >= ABS_HAT0X && i <= ABS_HAT3Y) {
if(auto hat = jp.hats.insert({i, hats++})) {
ioctl(jp.fd, EVIOCGABS(i), &hat().info);
}
} else {
if(auto axis = jp.axes.insert({i, axes++})) {
ioctl(jp.fd, EVIOCGABS(i), &axis().info);
}
}
}
}
for(int i = BTN_JOYSTICK; i < KEY_MAX; i++) {
if(testBit(jp.keybit, i)) {
jp.buttons.insert({i, buttons++});
}
}
for(int i = BTN_MISC; i < BTN_JOYSTICK; i++) {
if(testBit(jp.keybit, i)) {
jp.buttons.insert({i, buttons++});
}
}
jp.rumble = jp.effects >= 2 && testBit(jp.ffbit, FF_RUMBLE);
if(jp.rumble) {
ff_effect effect;
memset(&effect, 0, sizeof(ff_effect));
effect.type = FF_RUMBLE;
effect.id = -1;
effect.u.rumble.strong_magnitude = 65535;
effect.u.rumble.weak_magnitude = 65535;
ioctl(jp.fd, EVIOCSFF, &effect);
jp.effectID = effect.id;
}
createJoypadHID(jp);
joypads.append(jp);
}
#undef testBit
}
auto createJoypadHID(Joypad& jp) -> void {
uint64_t pathID = Hash::CRC32(jp.deviceName.data(), jp.deviceName.size()).value();
jp.hid->setID(pathID << 32 | jp.vendorID.hex() << 16 | jp.productID.hex() << 0);
for(uint n : range(jp.axes.size())) jp.hid->axes().append(n);
for(uint n : range(jp.hats.size())) jp.hid->hats().append(n);
for(uint n : range(jp.buttons.size())) jp.hid->buttons().append(n);
jp.hid->setRumble(jp.rumble);
}
auto removeJoypad(udev_device* device, const string& deviceNode) -> void {
for(uint n : range(joypads.size())) {
if(joypads[n].deviceNode == deviceNode) {
close(joypads[n].fd);
joypads.remove(n);
return;
}
}
}
};
#endif