refactor: move some useful functions into misc header
This commit is contained in:
parent
c05715ed7d
commit
eb43b0c9da
@ -1,29 +1,19 @@
|
|||||||
#include "audio.hpp"
|
#include "audio.hpp"
|
||||||
#include "../../core/global/globals.hpp"
|
#include "../../core/global/globals.hpp"
|
||||||
#include "../../ui/ui.hpp"
|
#include "../../ui/ui.hpp"
|
||||||
|
#include "../misc/misc.hpp"
|
||||||
#include <fancy.hpp>
|
#include <fancy.hpp>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
#define MINIAUDIO_IMPLEMENTATION
|
#define MINIAUDIO_IMPLEMENTATION
|
||||||
#include <miniaudio.h>
|
#include <miniaudio.h>
|
||||||
|
|
||||||
#if defined(_WIN32)
|
|
||||||
#include <stringapiset.h>
|
|
||||||
std::wstring widen(const std::string &s)
|
|
||||||
{
|
|
||||||
int wsz = MultiByteToWideChar(65001, 0, s.c_str(), -1, nullptr, 0);
|
|
||||||
if (!wsz)
|
|
||||||
return std::wstring();
|
|
||||||
|
|
||||||
std::wstring out(wsz, 0);
|
|
||||||
MultiByteToWideChar(65001, 0, s.c_str(), -1, &out[0], wsz);
|
|
||||||
out.resize(wsz - 1);
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace Soundux::Objects
|
namespace Soundux::Objects
|
||||||
{
|
{
|
||||||
|
#if defined(_WIN32)
|
||||||
|
using Soundux::Helpers::widen;
|
||||||
|
#endif
|
||||||
|
|
||||||
void Audio::setup()
|
void Audio::setup()
|
||||||
{
|
{
|
||||||
refreshAudioDevices();
|
refreshAudioDevices();
|
||||||
|
@ -1,42 +1,17 @@
|
|||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
#include "pulse.hpp"
|
#include "pulse.hpp"
|
||||||
|
#include "../../misc/misc.hpp"
|
||||||
#include <fancy.hpp>
|
#include <fancy.hpp>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#include <sstream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
bool exec(const std::string &command, std::string &result)
|
|
||||||
{
|
|
||||||
result.clear();
|
|
||||||
|
|
||||||
std::array<char, 128> buffer;
|
|
||||||
auto *pipe = popen(command.c_str(), "r");
|
|
||||||
if (!pipe)
|
|
||||||
{
|
|
||||||
throw std::runtime_error("popen failed");
|
|
||||||
}
|
|
||||||
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
|
|
||||||
{
|
|
||||||
result += buffer.data();
|
|
||||||
}
|
|
||||||
|
|
||||||
return pclose(pipe) == 0;
|
|
||||||
}
|
|
||||||
auto splitByNewLine(const std::string &str)
|
|
||||||
{
|
|
||||||
std::vector<std::string> result;
|
|
||||||
std::stringstream ss(str);
|
|
||||||
for (std::string line; std::getline(ss, line, '\n');)
|
|
||||||
{
|
|
||||||
result.emplace_back(line);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace Soundux::Objects
|
namespace Soundux::Objects
|
||||||
{
|
{
|
||||||
|
using Soundux::Helpers::exec;
|
||||||
|
using Soundux::Helpers::splitByNewLine;
|
||||||
|
|
||||||
bool Pulse::setModuleId(const std::string &command, std::uint32_t &id)
|
bool Pulse::setModuleId(const std::string &command, std::uint32_t &id)
|
||||||
{
|
{
|
||||||
static const std::regex idRegex(R"((\d+))");
|
static const std::regex idRegex(R"((\d+))");
|
||||||
|
46
src/helper/misc/misc.cpp
Normal file
46
src/helper/misc/misc.cpp
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#include "misc.hpp"
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
namespace Soundux::Helpers
|
||||||
|
{
|
||||||
|
std::vector<std::string> splitByNewLine(const std::string &str)
|
||||||
|
{
|
||||||
|
std::vector<std::string> result;
|
||||||
|
std::stringstream ss(str);
|
||||||
|
for (std::string line; std::getline(ss, line, '\n');)
|
||||||
|
{
|
||||||
|
result.emplace_back(line);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#if defined(_WIN32)
|
||||||
|
std::wstring widen(const std::string &s)
|
||||||
|
{
|
||||||
|
int wsz = MultiByteToWideChar(65001, 0, s.c_str(), -1, nullptr, 0);
|
||||||
|
if (!wsz)
|
||||||
|
return std::wstring();
|
||||||
|
|
||||||
|
std::wstring out(wsz, 0);
|
||||||
|
MultiByteToWideChar(65001, 0, s.c_str(), -1, &out[0], wsz);
|
||||||
|
out.resize(wsz - 1);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
bool exec(const std::string &command, std::string &result)
|
||||||
|
{
|
||||||
|
result.clear();
|
||||||
|
|
||||||
|
std::array<char, 128> buffer;
|
||||||
|
auto *pipe = popen(command.c_str(), "r");
|
||||||
|
if (!pipe)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("popen failed");
|
||||||
|
}
|
||||||
|
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
|
||||||
|
{
|
||||||
|
result += buffer.data();
|
||||||
|
}
|
||||||
|
|
||||||
|
return pclose(pipe) == 0;
|
||||||
|
}
|
||||||
|
} // namespace Soundux::Helpers
|
22
src/helper/misc/misc.hpp
Normal file
22
src/helper/misc/misc.hpp
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <array>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
#include <stringapiset.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace Soundux
|
||||||
|
{
|
||||||
|
namespace Helpers
|
||||||
|
{
|
||||||
|
#if defined(_WIN32)
|
||||||
|
std::wstring widen(const std::string &s);
|
||||||
|
#endif
|
||||||
|
#if defined(__linux__)
|
||||||
|
bool exec(const std::string &command, std::string &result);
|
||||||
|
#endif
|
||||||
|
std::vector<std::string> splitByNewLine(const std::string &str);
|
||||||
|
} // namespace Helpers
|
||||||
|
} // namespace Soundux
|
Loading…
x
Reference in New Issue
Block a user