Browse Source

Add Mouse device to Computer keyboard/mouse MIDI driver.

tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
2bd9d590dc
4 changed files with 91 additions and 11 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +3
    -0
      include/keyboard.hpp
  3. +80
    -10
      src/keyboard.cpp
  4. +7
    -1
      src/window.cpp

+ 1
- 0
CHANGELOG.md View File

@@ -33,6 +33,7 @@ In this document, Mod is Ctrl on Windows/Linux and Cmd on Mac.
- Allow RtAudio device block size to be as low as 16. - Allow RtAudio device block size to be as low as 16.
- Copy cable color when cloning cables with Ctrl+click. - Copy cable color when cloning cables with Ctrl+click.
- Fix key commands on AZERTY, Dvorak, and all other keyboard layouts. - Fix key commands on AZERTY, Dvorak, and all other keyboard layouts.
- Add Mouse device to Computer keyboard/mouse MIDI driver.


- Core - Core
- Add Audio-2 module with stereo input/output, a level knob, and VU meters. - Add Audio-2 module with stereo input/output, a level knob, and VU meters.


+ 3
- 0
include/keyboard.hpp View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <common.hpp> #include <common.hpp>
#include <math.hpp>




namespace rack { namespace rack {
@@ -13,6 +14,8 @@ namespace keyboard {
void init(); void init();
void press(int key); void press(int key);
void release(int key); void release(int key);
/** pos is in the unit box. */
void mouseMove(math::Vec pos);




} // namespace keyboard } // namespace keyboard


+ 80
- 10
src/keyboard.cpp View File

@@ -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

+ 7
- 1
src/window.cpp View File

@@ -155,6 +155,12 @@ static void cursorPosCallback(GLFWwindow* win, double xpos, double ypos) {
window->internal->lastMousePos = mousePos; window->internal->lastMousePos = mousePos;


APP->event->handleHover(mousePos, mouseDelta); APP->event->handleHover(mousePos, mouseDelta);

// Keyboard/mouse MIDI driver
int width, height;
glfwGetWindowSize(win, &width, &height);
math::Vec scaledPos(xpos / width, ypos / height);
keyboard::mouseMove(scaledPos);
} }


static void cursorEnterCallback(GLFWwindow* win, int entered) { static void cursorEnterCallback(GLFWwindow* win, int entered) {
@@ -186,7 +192,7 @@ static void keyCallback(GLFWwindow* win, int key, int scancode, int action, int
if (APP->event->handleKey(window->internal->lastMousePos, key, scancode, action, mods)) if (APP->event->handleKey(window->internal->lastMousePos, key, scancode, action, mods))
return; return;


// Keyboard MIDI driver
// Keyboard/mouse MIDI driver
if (action == GLFW_PRESS && (mods & RACK_MOD_MASK) == 0) { if (action == GLFW_PRESS && (mods & RACK_MOD_MASK) == 0) {
keyboard::press(key); keyboard::press(key);
} }


Loading…
Cancel
Save