-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement support for keyboard input on macos
- Loading branch information
Showing
7 changed files
with
132 additions
and
46 deletions.
There are no files selected for viewing
Submodule libossia
updated
3 files
+1 −1 | 3rdparty/libremidi | |
+2 −2 | src/ossia/protocols/midi/midi_protocol.cpp | |
+3 −3 | src/ossia/protocols/midi/midi_protocol.hpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/plugins/score-plugin-protocols/Protocols/MIDI/MIDIKeyboardEventFilter.linux.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#pragma once | ||
#if !defined(__APPLE__) | ||
#include <score/application/GUIApplicationContext.hpp> | ||
|
||
#include <QGuiApplication> | ||
#include <QKeyEvent> | ||
#include <QMainWindow> | ||
#include <QObject> | ||
|
||
#include <libremidi/backends/keyboard/config.hpp> | ||
|
||
namespace Protocols | ||
{ | ||
class MidiKeyboardEventFilter : public QObject | ||
{ | ||
public: | ||
libremidi::kbd_input_configuration::scancode_callback press, release; | ||
MidiKeyboardEventFilter( | ||
libremidi::kbd_input_configuration::scancode_callback press, | ||
libremidi::kbd_input_configuration::scancode_callback release) | ||
: press{std::move(press)} | ||
, release{std::move(release)} | ||
, target{score::GUIAppContext().mainWindow} | ||
{ | ||
} | ||
|
||
bool eventFilter(QObject* object, QEvent* event) | ||
{ | ||
if(object == target) | ||
{ | ||
if(event->type() == QEvent::KeyPress) | ||
{ | ||
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event); | ||
if(!keyEvent->isAutoRepeat()) | ||
press(key(*keyEvent)); | ||
} | ||
else if(event->type() == QEvent::KeyRelease) | ||
{ | ||
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event); | ||
if(!keyEvent->isAutoRepeat()) | ||
release(key(*keyEvent)); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
inline uint32_t key(QKeyEvent& e) | ||
{ | ||
#if defined(__linux__) | ||
// X11 quirk | ||
static const int scanCodeOffset = (qApp->platformName() == "xcb") ? -8 : 0; | ||
return e.nativeScanCode() - scanCodeOffset; | ||
#elif defined(__APPLE__) | ||
return e.nativeVirtualKey(); | ||
#else | ||
return e.nativeScanCode(); | ||
#endif | ||
} | ||
QObject* target{}; | ||
}; | ||
} | ||
#endif |
53 changes: 53 additions & 0 deletions
53
src/plugins/score-plugin-protocols/Protocols/MIDI/MIDIKeyboardEventFilter.macos.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#pragma once | ||
#if defined(__APPLE__) | ||
#include <QDebug> | ||
#include <QObject> | ||
|
||
#include <CoreGraphics/CoreGraphics.h> | ||
#include <libremidi/backends/keyboard/config.hpp> | ||
|
||
namespace Protocols | ||
{ | ||
class MidiKeyboardEventFilter : public QObject | ||
{ | ||
public: | ||
libremidi::kbd_input_configuration::scancode_callback press, release; | ||
MidiKeyboardEventFilter( | ||
libremidi::kbd_input_configuration::scancode_callback press, | ||
libremidi::kbd_input_configuration::scancode_callback release) | ||
: press{std::move(press)} | ||
, release{std::move(release)} | ||
{ | ||
CGEventMask mask = CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventKeyUp) | ||
| CGEventMaskBit(kCGEventScrollWheel) | ||
| CGEventMaskBit(kCGEventLeftMouseDown) | ||
| CGEventMaskBit(kCGEventRightMouseDown); | ||
auto eventTap = CGEventTapCreate( | ||
kCGSessionEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, mask, | ||
[](CGEventTapProxy, CGEventType type, CGEventRef event, void* ctx) { | ||
auto& self = *decltype(this)(ctx); | ||
switch(type) | ||
{ | ||
case kCGEventKeyDown: | ||
self.press(CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode)); | ||
break; | ||
case kCGEventKeyUp: | ||
self.release(CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode)); | ||
break; | ||
} | ||
return event; | ||
}, this); | ||
if(eventTap == nullptr) | ||
return; | ||
|
||
CFRunLoopAddSource( | ||
CFRunLoopGetCurrent(), | ||
CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0), | ||
kCFRunLoopCommonModes); | ||
// Enable the event tap. | ||
CGEventTapEnable(eventTap, true); | ||
} | ||
}; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters