1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
#pragma once
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 {
s32 code = 0;
u32 id = 0;
s16 value = 0;
input_absinfo info;
JoypadInput() {}
JoypadInput(s32 code) : code(code) {}
JoypadInput(s32 code, u32 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};
s32 fd = -1;
dev_t device = 0;
string deviceName;
string deviceNode;
u8 evbit[(EV_MAX + 7) / 8] = {0};
u8 keybit[(KEY_MAX + 7) / 8] = {0};
u8 absbit[(ABS_MAX + 7) / 8] = {0};
u8 ffbit[(FF_MAX + 7) / 8] = {0};
u32 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;
s32 effectID = -1;
};
vector<Joypad> joypads;
auto assign(shared_pointer<HID::Joypad> hid, u32 groupID, u32 inputID, s16 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];
s32 length = 0;
while((length = read(jp.fd, events, sizeof(events))) > 0) {
length /= sizeof(input_event);
for(u32 i : range(length)) {
s32 code = events[i].code;
s32 type = events[i].type;
s32 value = events[i].value;
if(type == EV_ABS) {
if(auto input = jp.axes.find({code})) {
s32 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})) {
s32 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(u64 id, bool enable) -> bool {
for(auto& jp : joypads) {
if(jp.hid->id() != id) continue;
if(!jp.hid->rumble()) continue;
if(!enable) {
if(jp.effectID == -1) return true; //already stopped?
ioctl(jp.fd, EVIOCRMFF, jp.effectID);
jp.effectID = -1;
} else {
if(jp.effectID != -1) return true; //already started?
ff_effect effect;
memory::fill(&effect, 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;
input_event play;
memory::fill(&play, sizeof(input_event));
play.type = EV_FF;
play.code = jp.effectID;
play.value = enable;
(void)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;
u8 evbit[(EV_MAX + 7) / 8] = {0};
u8 keybit[(KEY_MAX + 7) / 8] = {0};
u8 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");
}
}
}
u32 axes = 0;
u32 hats = 0;
u32 buttons = 0;
for(s32 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(s32 i = BTN_JOYSTICK; i < KEY_MAX; i++) {
if(testBit(jp.keybit, i)) {
jp.buttons.insert({i, buttons++});
}
}
for(s32 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);
createJoypadHID(jp);
joypads.append(jp);
}
#undef testBit
}
auto createJoypadHID(Joypad& jp) -> void {
jp.hid->setVendorID(jp.vendorID.hex());
jp.hid->setProductID(jp.productID.hex());
jp.hid->setPathID(Hash::CRC32(jp.deviceName).value());
for(u32 n : range(jp.axes.size())) jp.hid->axes().append(n);
for(u32 n : range(jp.hats.size())) jp.hid->hats().append(n);
for(u32 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(u32 n : range(joypads.size())) {
if(joypads[n].deviceNode == deviceNode) {
close(joypads[n].fd);
joypads.remove(n);
return;
}
}
}
};
|