-
-
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
134 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
64 changes: 64 additions & 0 deletions
64
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,64 @@ | ||
#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} | ||
{ | ||
// X11 quirk | ||
if(qApp->platformName() == "xcb") | ||
scanCodeOffset = -8; | ||
} | ||
|
||
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; | ||
} | ||
|
||
static inline uint32_t key(QKeyEvent& e) | ||
{ | ||
#if defined(__linux__) | ||
return e.nativeScanCode() - scanCodeOffset; | ||
#elif defined(__APPLE__) | ||
return e.nativeVirtualKey(); | ||
#else | ||
return e.nativeScanCode(); | ||
#endif | ||
} | ||
QObject* target{}; | ||
int scanCodeOffset{0}; | ||
}; | ||
} | ||
#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