|
| 1 | +#pragma once |
| 2 | + |
| 3 | +struct InputMouseQuartz { |
| 4 | + Input& input; |
| 5 | + InputMouseQuartz(Input& input) : input(input) {} |
| 6 | + |
| 7 | + shared_pointer<HID::Mouse> hid{new HID::Mouse}; |
| 8 | + |
| 9 | + bool mouseAcquired = false; |
| 10 | + |
| 11 | + auto acquire() -> bool { |
| 12 | + if(!mouseAcquired) { |
| 13 | + CGError error = CGAssociateMouseAndMouseCursorPosition(NO); |
| 14 | + if(error != kCGErrorSuccess) return false; |
| 15 | + [NSCursor hide]; |
| 16 | + |
| 17 | + mouseAcquired = true; |
| 18 | + } |
| 19 | + return true; |
| 20 | + } |
| 21 | + |
| 22 | + auto acquired() -> bool { |
| 23 | + return mouseAcquired; |
| 24 | + } |
| 25 | + |
| 26 | + auto release() -> bool { |
| 27 | + if(mouseAcquired) { |
| 28 | + CGError error = CGAssociateMouseAndMouseCursorPosition(YES); |
| 29 | + if(error != kCGErrorSuccess) return false; |
| 30 | + [NSCursor unhide]; |
| 31 | + |
| 32 | + mouseAcquired = false; |
| 33 | + } |
| 34 | + return true; |
| 35 | + } |
| 36 | + |
| 37 | + auto assign(uint groupID, uint inputID, int16_t value) -> void { |
| 38 | + auto& group = hid->group(groupID); |
| 39 | + if(group.input(inputID).value() == value) return; |
| 40 | + input.doChange(hid, groupID, inputID, group.input(inputID).value(), value); |
| 41 | + group.input(inputID).setValue(value); |
| 42 | + } |
| 43 | + |
| 44 | + auto poll(vector<shared_pointer<HID::Device>>& devices) -> void { |
| 45 | + int deltaX, deltaY; |
| 46 | + CGGetLastMouseDelta(&deltaX, &deltaY); |
| 47 | + |
| 48 | + assign(HID::Mouse::GroupID::Axis, 0, deltaX); |
| 49 | + assign(HID::Mouse::GroupID::Axis, 1, deltaY); |
| 50 | + |
| 51 | + NSUInteger buttons = [NSEvent pressedMouseButtons]; |
| 52 | + |
| 53 | + assign(HID::Mouse::GroupID::Button, 0, !!(buttons & (1 << 0))); |
| 54 | + assign(HID::Mouse::GroupID::Button, 2, !!(buttons & (1 << 1))); |
| 55 | + assign(HID::Mouse::GroupID::Button, 1, !!(buttons & (1 << 2))); |
| 56 | + assign(HID::Mouse::GroupID::Button, 4, !!(buttons & (1 << 3))); |
| 57 | + assign(HID::Mouse::GroupID::Button, 3, !!(buttons & (1 << 4))); |
| 58 | + |
| 59 | + devices.append(hid); |
| 60 | + } |
| 61 | + |
| 62 | + auto initialize() -> bool { |
| 63 | + hid->setVendorID(HID::Mouse::GenericVendorID); |
| 64 | + hid->setProductID(HID::Mouse::GenericProductID); |
| 65 | + hid->setPathID(0); |
| 66 | + |
| 67 | + hid->axes().append("X"); |
| 68 | + hid->axes().append("Y"); |
| 69 | + |
| 70 | + hid->buttons().append("Left"); |
| 71 | + hid->buttons().append("Middle"); |
| 72 | + hid->buttons().append("Right"); |
| 73 | + hid->buttons().append("Up"); |
| 74 | + hid->buttons().append("Down"); |
| 75 | + |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + auto terminate() -> void { |
| 80 | + release(); |
| 81 | + } |
| 82 | +}; |
0 commit comments