Skip to content

Commit

Permalink
Revert "feat: hotkeys rework and MIDI support (#242)"
Browse files Browse the repository at this point in the history
This reverts commit ad5fa40
  • Loading branch information
D3SOX committed May 27, 2022
1 parent e78f17f commit 085abaa
Show file tree
Hide file tree
Showing 23 changed files with 347 additions and 694 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,3 @@
[submodule "lib/guardpp"]
path = lib/guardpp
url = https://github.com/Soundux/guardpp
[submodule "lib/libremidi"]
path = lib/libremidi
url = https://github.com/Soundux/libremidi
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ if (WIN32)
target_compile_options(soundux PRIVATE /W4)
else()
add_executable(soundux ${src})

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
message(STATUS "Enabling warning and error flags for debug build")
target_compile_options(soundux PRIVATE -Wall -Werror -Wextra -pedantic -Wno-unused-lambda-capture -Wno-gnu)
Expand Down Expand Up @@ -78,7 +78,6 @@ add_subdirectory(lib/nativefiledialog-extended EXCLUDE_FROM_ALL)
add_subdirectory(lib/tiny-process-library EXCLUDE_FROM_ALL)
add_subdirectory(lib/backward-cpp EXCLUDE_FROM_ALL)
add_subdirectory(lib/traypp EXCLUDE_FROM_ALL)
add_subdirectory(lib/libremidi)
add_subdirectory(lib/guardpp)
add_subdirectory(lib/lockpp)

Expand All @@ -94,7 +93,7 @@ set(HTTPLIB_REQUIRE_OPENSSL ON)
add_subdirectory(lib/cpp-httplib EXCLUDE_FROM_ALL)
target_include_directories(soundux SYSTEM PRIVATE "lib/cpp-httplib")

target_link_libraries(soundux PRIVATE webview nfd tiny-process-library tray guardpp httplib lockpp libremidi)
target_link_libraries(soundux PRIVATE webview nfd tiny-process-library tray guard httplib lockpp)

if (${EMBED_PATH} STREQUAL "OFF")
message("Web-content will not be embedded")
Expand Down
1 change: 0 additions & 1 deletion lib/libremidi
Submodule libremidi deleted from e2213b
2 changes: 1 addition & 1 deletion src/core/global/globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ namespace Soundux
inline Objects::Queue gQueue;
inline Objects::Config gConfig;
inline Objects::YoutubeDl gYtdl;
inline Objects::Hotkeys gHotKeys;
inline Objects::Settings gSettings;
inline std::unique_ptr<Objects::Window> gGui;
inline std::shared_ptr<Objects::Hotkeys> gHotKeys;

inline std::shared_ptr<guardpp::guard> gGuard;

Expand Down
226 changes: 69 additions & 157 deletions src/core/hotkeys/hotkeys.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#include "hotkeys.hpp"
#include "keys.hpp"
#include "linux/x11.hpp"
#include "windows/windows.hpp"
#include <core/global/globals.hpp>
#include <cstdint>
#include <fancy.hpp>

namespace Soundux
{
Expand All @@ -23,117 +19,38 @@ namespace Soundux
} // namespace traits
namespace Objects
{
std::shared_ptr<Hotkeys> Hotkeys::createInstance()
void Hotkeys::init()
{
std::shared_ptr<Hotkeys> rtn;
#if defined(__linux__)
rtn = std::shared_ptr<X11>(new X11()); // NOLINT
#elif defined(_WIN32)
rtn = std::shared_ptr<WindowsHotkeys>(new WindowsHotkeys()); // NOLINT
#endif
rtn->setup();
return rtn;
listener = std::thread([this] { listen(); });
}

void Hotkeys::setup()
{
try
{
midi.open_port();
midi.set_callback([this](const libremidi::message &message) {
if (message.size() < 3)
{
Fancy::fancy.logTime().failure()
<< "Midi Message contains less than 3 bytes, can't parse information";
return;
}

auto byte0 = message[0];
auto byte1 = message[1];
auto byte2 = message[2];

MidiKey key;
key.byte0 = byte0;
key.key = byte1;
key.byte2 = byte2;
key.type = Enums::KeyType::Midi;

if (byte0 == 144)
{
onKeyDown(key);
}
else if (byte0 == 128)
{
onKeyUp(key);
}
else if (byte0 == 176)
{
if (shouldNotifyKnob)
{
Globals::gGui->onHotKeyReceived({key}); // NOLINT
}
else
{
auto newVolume = static_cast<int>((static_cast<float>(byte2) / 127.f) * 100);

if (Globals::gSettings.localVolumeKnob && key == Globals::gSettings.localVolumeKnob)
{
Globals::gSettings.localVolume = newVolume;
Globals::gGui->onVolumeChanged();

Globals::gQueue.push([=]() { Globals::gGui->onLocalVolumeChanged(newVolume); });
}
else if (Globals::gSettings.remoteVolumeKnob && key == Globals::gSettings.remoteVolumeKnob)
{
Globals::gSettings.remoteVolume = newVolume;
Globals::gGui->onVolumeChanged();

Globals::gQueue.push([=]() { Globals::gGui->onRemoteVolumeChanged(newVolume); });
}
}
}
});
midi.ignore_types(false, false, false);
}
catch (const libremidi::midi_exception &e)
{
Fancy::fancy.logTime().failure() << "Failed to initialize libremidi: " << e.what() << std::endl;
}
}
void Hotkeys::notify(bool state)
void Hotkeys::shouldNotify(bool status)
{
pressedKeys.clear();
shouldNotify = state;
notify = status;
}
void Hotkeys::requestKnob(bool state)
void Hotkeys::onKeyUp(int key)
{
shouldNotifyKnob = state;
}
void Hotkeys::onKeyUp(const Key &key)
{
if (std::find(pressedKeys.begin(), pressedKeys.end(), key) != pressedKeys.end())
if (notify && !pressedKeys.empty() &&
std::find(pressedKeys.begin(), pressedKeys.end(), key) != pressedKeys.end())
{
if (shouldNotify)
{
Globals::gGui->onHotKeyReceived(pressedKeys);
pressedKeys.clear();
}
else
{
pressedKeys.erase(std::remove_if(pressedKeys.begin(), pressedKeys.end(),
[&](const auto &keyItem) { return key == keyItem; }),
pressedKeys.end());
}
Globals::gGui->onHotKeyReceived(pressedKeys);
pressedKeys.clear();
}
else
{
pressedKeys.erase(std::remove_if(pressedKeys.begin(), pressedKeys.end(),
[key](const auto &item) { return key == item; }),
pressedKeys.end());
}
}
bool isCloseMatch(const std::vector<Key> &pressedKeys, const std::vector<Key> &keys)
bool isCloseMatch(const std::vector<int> &pressedKeys, const std::vector<int> &keys)
{
if (pressedKeys.size() >= keys.size())
{
bool allMatched = true;
for (const auto &key : keys)
{
if (std::find(pressedKeys.begin(), pressedKeys.end(), key) == pressedKeys.end()) // NOLINT
if (std::find(pressedKeys.begin(), pressedKeys.end(), key) == pressedKeys.end())
{
allMatched = false;
}
Expand All @@ -142,14 +59,13 @@ namespace Soundux
}
return false;
}
template <typename T> std::optional<Sound> getBestMatch(const T &list, const std::vector<Key> &pressedKeys)
template <typename T> std::optional<Sound> getBestMatch(const T &list, const std::vector<int> &pressedKeys)
{
std::optional<Sound> rtn;

for (const auto &_sound : list)
{
const auto &sound = [&]() constexpr
{
const auto &sound = [&] {
if constexpr (traits::is_pair<std::decay_t<decltype(_sound)>>::value)
{
return _sound.second.get();
Expand All @@ -158,13 +74,12 @@ namespace Soundux
{
return _sound;
}
}
();
}();

if (sound.hotkeys.empty())
continue;

if (pressedKeys == sound.hotkeys)
if (sound.hotkeys == pressedKeys)
{
rtn = sound;
break;
Expand All @@ -182,81 +97,78 @@ namespace Soundux
}
return rtn;
}
void Hotkeys::onKeyDown(const Key &key)
void Hotkeys::onKeyDown(int key)
{
if (std::find(pressedKeys.begin(), pressedKeys.end(), key) != pressedKeys.end())
if (std::find(keysToPress.begin(), keysToPress.end(), key) != keysToPress.end())
{
return;
}
if (std::find(pressedKeys.begin(), pressedKeys.end(), key) == pressedKeys.end())
{
pressedKeys.emplace_back(key);
}
else
{
return;
}

pressedKeys.emplace_back(key);
if (!shouldNotify)
if (notify)
{
if (!Globals::gSettings.stopHotkey.empty() &&
(Globals::gSettings.stopHotkey == pressedKeys ||
isCloseMatch(pressedKeys, Globals::gSettings.stopHotkey)))
{
Globals::gGui->stopSounds();
return;
}
return;
}

if (!Globals::gSettings.stopHotkey.empty() && (pressedKeys == Globals::gSettings.stopHotkey ||
isCloseMatch(pressedKeys, Globals::gSettings.stopHotkey)))
{
Globals::gGui->stopSounds();
return;
}

std::optional<Sound> bestMatch;
std::optional<Sound> bestMatch;

if (Globals::gSettings.tabHotkeysOnly)
if (Globals::gSettings.tabHotkeysOnly)
{
if (Globals::gData.isOnFavorites)
{
if (Globals::gData.isOnFavorites)
{
auto sounds = Globals::gData.getFavorites();
bestMatch = getBestMatch(sounds, pressedKeys);
}
else
{
auto tab = Globals::gData.getTab(Globals::gSettings.selectedTab);
if (tab)
{
bestMatch = getBestMatch(tab->sounds, pressedKeys);
}
}
auto sounds = Globals::gData.getFavorites();
bestMatch = getBestMatch(sounds, pressedKeys);
}
else
{
auto scopedSounds = Globals::gSounds.scoped();
bestMatch = getBestMatch(*scopedSounds, pressedKeys);
}

if (bestMatch)
{
auto pSound = Globals::gGui->playSound(bestMatch->id);
if (pSound)
auto tab = Globals::gData.getTab(Globals::gSettings.selectedTab);
if (tab)
{
Globals::gGui->onSoundPlayed(*pSound);
bestMatch = getBestMatch(tab->sounds, pressedKeys);
}
}
}
}
std::string Hotkeys::getKeyName(const Key &key)
{
if (key.type == Enums::KeyType::Midi)
else
{
return "MIDI_" + std::to_string(key.key);
auto scopedSounds = Globals::gSounds.scoped();
bestMatch = getBestMatch(*scopedSounds, pressedKeys);
}

return "";
if (bestMatch)
{
auto pSound = Globals::gGui->playSound(bestMatch->id);
if (pSound)
{
Globals::gGui->onSoundPlayed(*pSound);
}
}
}
std::string Hotkeys::getKeySequence(const std::vector<Key> &keys)
std::string Hotkeys::getKeySequence(const std::vector<int> &keys)
{
std::string rtn;

for (auto it = keys.begin(); it != keys.end(); ++it)
for (const auto &key : keys)
{
rtn += getKeyName(*it);
if (std::distance(it, keys.end()) > 1)
{
rtn += " + ";
}
rtn += getKeyName(key) + " + ";
}

return rtn;
if (!rtn.empty())
{
return rtn.substr(0, rtn.length() - 3);
}
return "";
}
} // namespace Objects
} // namespace Soundux

0 comments on commit 085abaa

Please sign in to comment.