Skip to content

Commit

Permalink
fix for #2
Browse files Browse the repository at this point in the history
make device selection more convenient when multiple midi-devices are present

allow reassign mappings command in lowercase as well
  • Loading branch information
k5md committed Dec 2, 2021
1 parent 146fd91 commit e63ad68
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/M2KB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <locale.h>
#include <curses.h>
#include <string.h>
#include <sstream>

#pragma comment(lib, "winmm.lib")

Expand Down Expand Up @@ -133,25 +134,38 @@ struct Keymapper {

int numDevs = midiInGetNumDevs();
if (numDevs == 0) {
ui.print("No MIDI-devices detected.");
std::cin.ignore();
throw std::invalid_argument("No MIDI-devices detected.");

ui.print("No MIDI-devices detected");
throw std::invalid_argument("No MIDI-devices detected");

return;
}

if (numDevs > 1) {
ui.print("Multiple MIDI-devices detected.");
ui.print("Select a device number:");

ui.print("Multiple MIDI-devices detected");
MIDIINCAPS cur;
for (size_t i=0; i<numDevs; i++) {
midiInGetDevCaps(i, &cur, sizeof(MIDIINCAPS));
ui.print("#%d: %s", i, cur.szPname);
}

int choice = -1;
while (choice > numDevs || choice < 0)
std::cin >> choice;
int input;
int selectedNumber;
std::stringstream inputStream;
while (choice >= numDevs || choice < 0) {
ui.print("Enter valid device number: ");
choice = -1;
inputStream.clear();
inputStream.str("");
while((input = _getch()) != VK_RETURN) {
selectedNumber = input - '0';
if (selectedNumber < 0 || selectedNumber > 9) continue;
inputStream << std::to_string(selectedNumber);
ui.printchars(std::to_string(selectedNumber).c_str());
}
inputStream >> choice;
}

currentDevice = choice;
}

Expand Down Expand Up @@ -230,9 +244,9 @@ int main() {
keymapper.printConfig();
keymapper.listen();

ui.print("Reassign mappings? (Y/N)");
char reassign = std::cin.get();
if (reassign == 'Y') {
ui.print("Reassign mappings? Type \"Y\" or \"y\" to confirm");
char reassign = _getch();
if (reassign == 'Y' || reassign == 'y') {
keymapper.reassignKeymap();
}

Expand Down

0 comments on commit e63ad68

Please sign in to comment.