|
@@ -14,6 +14,7 @@ struct Driver; |
|
|
|
|
|
|
|
|
static const int DRIVER = -11; |
|
|
static const int DRIVER = -11; |
|
|
static Driver* driver = NULL; |
|
|
static Driver* driver = NULL; |
|
|
|
|
|
static const int MOUSE_DEVICE_ID = 1000; |
|
|
|
|
|
|
|
|
enum { |
|
|
enum { |
|
|
CMD_OCTAVE_DOWN = -1, |
|
|
CMD_OCTAVE_DOWN = -1, |
|
@@ -106,6 +107,14 @@ struct InputDevice : midi::InputDevice { |
|
|
int octave = 5; |
|
|
int octave = 5; |
|
|
std::map<int, int> pressedNotes; |
|
|
std::map<int, int> pressedNotes; |
|
|
|
|
|
|
|
|
|
|
|
void setDeviceId(int deviceId) { |
|
|
|
|
|
this->deviceId = deviceId; |
|
|
|
|
|
// Default lowest key of numpad is C1. |
|
|
|
|
|
if (deviceId == 1) { |
|
|
|
|
|
octave = 3; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
std::string getName() override { |
|
|
std::string getName() override { |
|
|
return deviceInfos[deviceId].name; |
|
|
return deviceInfos[deviceId].name; |
|
|
} |
|
|
} |
|
@@ -114,7 +123,7 @@ struct InputDevice : midi::InputDevice { |
|
|
// Do nothing if no ports are subscribed |
|
|
// Do nothing if no ports are subscribed |
|
|
if (subscribed.empty()) |
|
|
if (subscribed.empty()) |
|
|
return; |
|
|
return; |
|
|
auto keyMap = deviceInfos[deviceId].keyMap; |
|
|
|
|
|
|
|
|
const auto& keyMap = deviceInfos[deviceId].keyMap; |
|
|
auto it = keyMap.find(key); |
|
|
auto it = keyMap.find(key); |
|
|
if (it == keyMap.end()) |
|
|
if (it == keyMap.end()) |
|
|
return; |
|
|
return; |
|
@@ -164,18 +173,52 @@ struct InputDevice : midi::InputDevice { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct MouseInputDevice : midi::InputDevice { |
|
|
|
|
|
int16_t lastValues[2] = {}; |
|
|
|
|
|
|
|
|
|
|
|
std::string getName() override { |
|
|
|
|
|
return "Mouse"; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void onMouseMove(math::Vec pos) { |
|
|
|
|
|
int16_t values[2]; |
|
|
|
|
|
values[0] = math::clamp((int) std::round(pos.x * 0x3f80), 0, 0x3f80); |
|
|
|
|
|
// Flip Y values |
|
|
|
|
|
values[1] = math::clamp((int) std::round((1.f - pos.y) * 0x3f80), 0, 0x3f80); |
|
|
|
|
|
|
|
|
|
|
|
for (int id = 0; id < 2; id++) { |
|
|
|
|
|
if (values[id] != lastValues[id]) { |
|
|
|
|
|
// Continuous controller MSB |
|
|
|
|
|
midi::Message m; |
|
|
|
|
|
m.setStatus(0xb); |
|
|
|
|
|
m.setNote(id); |
|
|
|
|
|
m.setValue(values[id] >> 7); |
|
|
|
|
|
onMessage(m); |
|
|
|
|
|
// Continuous controller LSB |
|
|
|
|
|
midi::Message m2; |
|
|
|
|
|
m2.setStatus(0xb); |
|
|
|
|
|
m2.setNote(id + 32); |
|
|
|
|
|
m2.setValue(values[id] & 0x7f); |
|
|
|
|
|
onMessage(m2); |
|
|
|
|
|
lastValues[id] = values[id]; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Driver : midi::Driver { |
|
|
struct Driver : midi::Driver { |
|
|
InputDevice devices[deviceCount]; |
|
|
InputDevice devices[deviceCount]; |
|
|
|
|
|
MouseInputDevice mouseDevice; |
|
|
|
|
|
|
|
|
Driver() { |
|
|
Driver() { |
|
|
for (int deviceId = 0; deviceId < deviceCount; deviceId++) { |
|
|
for (int deviceId = 0; deviceId < deviceCount; deviceId++) { |
|
|
devices[deviceId].deviceId = deviceId; |
|
|
|
|
|
|
|
|
devices[deviceId].setDeviceId(deviceId); |
|
|
} |
|
|
} |
|
|
devices[1].octave = 3; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
std::string getName() override { |
|
|
std::string getName() override { |
|
|
return "Computer keyboard"; |
|
|
|
|
|
|
|
|
return "Computer keyboard/mouse"; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
std::vector<int> getInputDeviceIds() override { |
|
|
std::vector<int> getInputDeviceIds() override { |
|
@@ -183,26 +226,42 @@ struct Driver : midi::Driver { |
|
|
for (int deviceId = 0; deviceId < deviceCount; deviceId++) { |
|
|
for (int deviceId = 0; deviceId < deviceCount; deviceId++) { |
|
|
deviceIds.push_back(deviceId); |
|
|
deviceIds.push_back(deviceId); |
|
|
} |
|
|
} |
|
|
|
|
|
deviceIds.push_back(MOUSE_DEVICE_ID); |
|
|
return deviceIds; |
|
|
return deviceIds; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
midi::InputDevice* getInputDevice(int deviceId) { |
|
|
|
|
|
if (deviceId == MOUSE_DEVICE_ID) |
|
|
|
|
|
return &mouseDevice; |
|
|
|
|
|
if (0 <= deviceId && deviceId < deviceCount) |
|
|
|
|
|
return &devices[deviceId]; |
|
|
|
|
|
return NULL; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
std::string getInputDeviceName(int deviceId) override { |
|
|
std::string getInputDeviceName(int deviceId) override { |
|
|
return deviceInfos[deviceId].name; |
|
|
|
|
|
|
|
|
midi::InputDevice* inputDevice = getInputDevice(deviceId); |
|
|
|
|
|
if (!inputDevice) |
|
|
|
|
|
return ""; |
|
|
|
|
|
return inputDevice->getName(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
midi::InputDevice* subscribeInput(int deviceId, midi::Input* input) override { |
|
|
midi::InputDevice* subscribeInput(int deviceId, midi::Input* input) override { |
|
|
if (!(0 <= deviceId && deviceId < deviceCount)) |
|
|
|
|
|
|
|
|
midi::InputDevice* inputDevice = getInputDevice(deviceId); |
|
|
|
|
|
if (!inputDevice) |
|
|
return NULL; |
|
|
return NULL; |
|
|
devices[deviceId].subscribe(input); |
|
|
|
|
|
return &devices[deviceId]; |
|
|
|
|
|
|
|
|
inputDevice->subscribe(input); |
|
|
|
|
|
return inputDevice; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void unsubscribeInput(int deviceId, midi::Input* input) override { |
|
|
void unsubscribeInput(int deviceId, midi::Input* input) override { |
|
|
if (!(0 <= deviceId && deviceId < deviceCount)) |
|
|
|
|
|
|
|
|
midi::InputDevice* inputDevice = getInputDevice(deviceId); |
|
|
|
|
|
if (!inputDevice) |
|
|
return; |
|
|
return; |
|
|
devices[deviceId].unsubscribe(input); |
|
|
|
|
|
|
|
|
inputDevice->unsubscribe(input); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Events that forward to InputDevices |
|
|
|
|
|
|
|
|
void onKeyPress(int key) { |
|
|
void onKeyPress(int key) { |
|
|
for (int deviceId = 0; deviceId < deviceCount; deviceId++) { |
|
|
for (int deviceId = 0; deviceId < deviceCount; deviceId++) { |
|
|
devices[deviceId].onKeyPress(key); |
|
|
devices[deviceId].onKeyPress(key); |
|
@@ -214,6 +273,10 @@ struct Driver : midi::Driver { |
|
|
devices[deviceId].onKeyRelease(key); |
|
|
devices[deviceId].onKeyRelease(key); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void onMouseMove(math::Vec pos) { |
|
|
|
|
|
mouseDevice.onMouseMove(pos); |
|
|
|
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -235,5 +298,12 @@ void release(int key) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void mouseMove(math::Vec pos) { |
|
|
|
|
|
if (!driver) |
|
|
|
|
|
return; |
|
|
|
|
|
driver->onMouseMove(pos); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace keyboard |
|
|
} // namespace keyboard |
|
|
} // namespace rack |
|
|
} // namespace rack |