refactor: adjust code to our clang-tidy

This commit is contained in:
Curve 2020-12-22 19:40:48 +01:00
parent f471ba7f06
commit 5e0861d397
12 changed files with 130 additions and 82 deletions

View File

@ -44,4 +44,8 @@ Checks: "*,\
-modernize-avoid-c-arrays,\
-cppcoreguidelines-avoid-c-arrays,\
-cert-env33-c,\
-abseil-*"
-abseil-*,\
-fuchsia-default-arguments-declarations,\
-readability-redundant-access-specifiers,\
-readability-convert-member-functions-to-static,\
-readability-implicit-bool-conversion"

View File

@ -1,5 +1,6 @@
#pragma once
#include <qlist.h>
#include <utility>
#include <vector>
#include <QString>
#include <QObject>
@ -15,7 +16,7 @@ struct QPulseAudioRecordingStream
public:
void setInstance(Soundux::Playback::internal::PulseAudioRecordingStream instance)
{
this->instance = instance;
this->instance = std::move(instance);
}
Q_INVOKABLE QString getName() const
{
@ -41,7 +42,7 @@ struct QSound
public:
void setInstance(Soundux::Config::Sound instance)
{
this->instance = instance;
this->instance = std::move(instance);
}
Soundux::Config::Sound getInstance() const
{
@ -80,7 +81,7 @@ struct QTab
public:
void setInstance(Soundux::Config::Tab instance)
{
this->instance = instance;
this->instance = std::move(instance);
}
Soundux::Config::Tab getInstance() const
{
@ -96,7 +97,7 @@ struct QTab
}
Q_INVOKABLE std::vector<QSound> getSounds() const
{
auto &sounds = instance.sounds;
const auto &sounds = instance.sounds;
std::vector<QSound> qSounds;
for (const auto &sound : sounds)

View File

@ -13,7 +13,7 @@ namespace Soundux
{
namespace Config
{
using namespace nlohmann;
using nlohmann::json;
struct Sound
{
@ -22,11 +22,11 @@ namespace Soundux
std::vector<int> hotKeys;
std::uint64_t lastWriteTime;
bool operator==(const std::string &path)
bool operator==(const std::string &path) const
{
return path == this->path;
}
bool operator==(const Sound &other)
bool operator==(const Sound &other) const
{
return other.name == name && other.path == path;
}
@ -37,7 +37,7 @@ namespace Soundux
std::string folder;
std::vector<Sound> sounds;
bool operator==(const Tab &other)
bool operator==(const Tab &other) const
{
return other.folder == folder && other.title == title;
}
@ -84,7 +84,9 @@ namespace Soundux
j.at("hotKeys").get_to(sound.hotKeys);
if (j.contains("lastWriteTime"))
{
j.at("lastWriteTime").get_to(sound.lastWriteTime);
}
}
inline void to_json(json &j, const Tab &tab)
@ -121,13 +123,21 @@ namespace Soundux
j.at("tabHotkeysOnly").get_to(config.tabHotkeysOnly);
if (j.contains("volumes"))
{
j.at("volumes").get_to(config.volumes);
}
if (j.contains("width"))
{
j.at("width").get_to(config.width);
}
if (j.contains("height"))
{
j.at("height").get_to(config.height);
}
if (j.contains("allowOverlapping"))
{
j.at("allowOverlapping").get_to(config.allowOverlapping);
}
}
inline void loadConfig()

View File

@ -4,10 +4,10 @@
#include "hotkeys/global.h"
#include "playback/global.h"
#include "playback/linux.h"
#include <filesystem>
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <filesystem>
#include <string>
#include <vector>
@ -168,7 +168,7 @@ void Core::addFolderTab(QUrl folder)
void Core::updateFolderSounds(QTab qTab)
{
// TODO: fix that the previously selected item is selected afterwards when it's still there
// TODO(d3s0x): fix that the previously selected item is selected afterwards when it's still there
auto instance = qTab.getInstance();
for (auto &tab : Soundux::Config::gConfig.tabs)
{
@ -310,7 +310,7 @@ void Core::playSound(std::string path)
}
#ifdef __linux__
static std::string moveBackCmd;
auto sinkMonitorId = "TODO";
const auto *sinkMonitorId = "TODO";
auto outputApp = Soundux::Playback::getCurrentOutputApplication();
@ -350,12 +350,12 @@ void Core::playSound(std::string path)
void Core::changeLocalVolume(int volume)
{
Soundux::Playback::setVolume(Soundux::Playback::defaultPlayback.name, volume / 100.f);
Soundux::Playback::setVolume(Soundux::Playback::defaultPlayback.name, volume / 100.F);
}
void Core::changeRemoteVolume(int volume)
{
Soundux::Playback::setVolume(sink.name, volume / 100.f);
Soundux::Playback::setVolume(sink.name, volume / 100.F);
}
void Core::stopPlayback()

View File

@ -1,8 +1,8 @@
#pragma once
#include <QObject>
#include <QQmlApplicationEngine>
#include <QSharedPointer>
#include <qglobal.h>
#include <QObject>
#include <vector>
#include "config/config.h"
@ -25,7 +25,7 @@ class Core : public QObject
{
Q_OBJECT
public:
explicit Core(QObject * = 0);
explicit Core(QObject * = nullptr);
public slots:
void setEngine(QQmlApplicationEngine *);

View File

@ -12,8 +12,10 @@ void Soundux::Hooks::internal::onKeyEvent(int key, bool down)
for (auto keyState : capturedKeyStates)
{
if (keyState.second.first)
{
pressedStates.push_back(
std::make_tuple(keyState.first, keyState.second.first, keyState.second.second));
}
}
pressedStates.push_back(std::make_tuple(key, down, std::chrono::system_clock::now()));
@ -85,7 +87,7 @@ void Soundux::Hooks::internal::onKeyEvent(int key, bool down)
for (const auto &sound : tab.sounds)
{
bool allPressed = !sound.hotKeys.empty();
for (auto &hotKey : sound.hotKeys)
for (const auto &hotKey : sound.hotKeys)
{
if (!pressedKeys[hotKey])
{

View File

@ -1,14 +1,14 @@
#pragma once
#include "../config/config.h"
#include <chrono>
#include <map>
#include "../config/config.h"
namespace Soundux
{
namespace Hooks
{
// Defined by linux/windows.h
std::string getKeyName(const int key);
std::string getKeyName(int key);
namespace internal
{

View File

@ -1,14 +1,14 @@
#include <X11/Xlib.h>
#ifdef __linux__
#pragma once
#include <atomic>
#include <chrono>
#include <thread>
#include "global.h"
#include <iostream>
#include <X11/XKBlib.h>
#include <X11/extensions/XI2.h>
#include <X11/extensions/XInput2.h>
#include <atomic>
#include <chrono>
#include <iostream>
#include <thread>
namespace Soundux
{
@ -22,7 +22,7 @@ namespace Soundux
const char *displayenv = std::getenv("DISPLAY");
Display *x11display = XOpenDisplay(displayenv);
if (x11display == NULL)
if (x11display == nullptr)
{
std::cerr << "Failed to get X11-Display with value provided by environment variable(" << displayenv
<< "), falling back "
@ -31,7 +31,7 @@ namespace Soundux
x11display = XOpenDisplay(":0");
}
if (x11display == NULL)
if (x11display == nullptr)
{
std::cerr << "Failed to open X11 Display" << std::endl;
return nullptr;
@ -41,8 +41,11 @@ namespace Soundux
inline void hook()
{
int xiOpCode, queryEvent, queryError;
if (!XQueryExtension(display, "XInputExtension", &xiOpCode, &queryEvent, &queryError))
int xiOpCode = 0;
int queryEvent = 0;
int queryError = 0;
if (XQueryExtension(display, "XInputExtension", &xiOpCode, &queryEvent, &queryError) == 0)
{
std::cerr << "XInput extension is not aviable" << std::endl;
return;
@ -50,15 +53,17 @@ namespace Soundux
// Custom context
{
int major = 2, minor = 0;
int major = 2;
int minor = 0;
int queryResult = XIQueryVersion(display, &major, &minor);
if (queryResult == BadRequest)
{
std::cerr << "XI 2.0 support is required - Current Version: " << major << "." << minor
<< std::endl;
return;
}
else if (queryResult != Success)
if (queryResult != Success)
{
std::cerr << "Unknown error" << std::endl;
return;
@ -69,29 +74,29 @@ namespace Soundux
XIEventMask mask;
mask.deviceid = XIAllMasterDevices;
mask.mask_len = XIMaskLen(XI_LASTEVENT);
mask.mask = (unsigned char *)calloc(mask.mask_len, sizeof(char));
mask.mask = static_cast<unsigned char *>(calloc(mask.mask_len, sizeof(char)));
XISetMask(mask.mask, XI_RawKeyPress);
XISetMask(mask.mask, XI_RawKeyRelease);
XISelectEvents(display, root, &mask, 1);
XSync(display, false);
XSync(display, 0);
free(mask.mask);
while (!killThread.load())
{
while (!killThread.load())
{
if (XPending(display))
if (XPending(display) != 0)
{
XEvent event;
XNextEvent(display, &event);
XGenericEventCookie *cookie = reinterpret_cast<XGenericEventCookie *>(&event.xcookie);
auto *cookie = reinterpret_cast<XGenericEventCookie *>(&event.xcookie);
if (XGetEventData(display, cookie) && cookie->type == GenericEvent &&
if ((XGetEventData(display, cookie) != 0) && cookie->type == GenericEvent &&
cookie->extension == xiOpCode &&
(cookie->evtype == XI_RawKeyRelease || cookie->evtype == XI_RawKeyPress))
{
XIRawEvent *ev = reinterpret_cast<XIRawEvent *>(cookie->data);
auto *ev = reinterpret_cast<XIRawEvent *>(cookie->data);
auto key = ev->detail;
internal::onKeyEvent(key, cookie->evtype == XI_RawKeyPress);
@ -120,11 +125,15 @@ namespace Soundux
{
KeySym s = XkbKeycodeToKeysym(internal::display, key, 0, 0);
if (NoSymbol == s)
{
return "Unknown";
}
char *str = XKeysymToString(s);
if (str == nullptr)
{
return "Unknown";
}
return str;
}

View File

@ -1,7 +1,7 @@
#include <QQmlApplicationEngine>
#include <QApplication>
#include <QQuickStyle>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickStyle>
#include <filesystem>
#include <fstream>
#include <qqml.h>
@ -9,9 +9,9 @@
#include <string>
#include "core.h"
#include "bindings/bindings.h"
#include "config/config.h"
#include "playback/global.h"
#include "bindings/bindings.h"
#ifdef _WIN32
#include "hotkeys/windows.h"

View File

@ -1,14 +1,14 @@
#pragma once
#include <miniaudio.h>
#include <functional>
#include <exception>
#include <iostream>
#include <vector>
#include <thread>
#include <atomic>
#include <exception>
#include <functional>
#include <iostream>
#include <map>
#include <map>
#include <miniaudio.h>
#include <mutex>
#include <map>
#include <map>
#include <thread>
#include <vector>
namespace Soundux
{
@ -51,7 +51,7 @@ namespace Soundux
std::uint64_t playAudio(const std::string &file, const ma_device_info &deviceInfo);
void stop(const std::uint64_t &deviceId);
inline std::function<void(const internal::PlayingDevice &)> stopCallback = [](const auto &) {};
inline std::function<void(const internal::PlayingDevice &)> stopCallback = [](const auto &device) {};
void pause(const std::uint64_t &deviceId);
@ -78,7 +78,9 @@ namespace Soundux
{
auto &device = currentlyPlayingDevices.at(i);
if (device.device != sound->first)
{
continue;
}
stop(device.id);
}
playingDeviceMutex.unlock();

View File

@ -1,7 +1,7 @@
#include "global.h"
#include <cstdint>
#define MINIAUDIO_IMPLEMENTATION
#include <miniaudio.h>
#include "global.h"
namespace Soundux
{
@ -9,7 +9,7 @@ namespace Soundux
{
ma_device device;
ma_device_config deviceConfig = ma_device_config_init(ma_device_type_playback);
ma_device_init(0, &deviceConfig, &device);
ma_device_init(nullptr, &deviceConfig, &device);
Playback::internal::DefaultDevice playbackInfo;
playbackInfo.name = device.playback.name;
@ -22,7 +22,7 @@ namespace Soundux
{
ma_device device;
ma_device_config deviceConfig = ma_device_config_init(ma_device_type_capture);
ma_device_init(0, &deviceConfig, &device);
ma_device_init(nullptr, &deviceConfig, &device);
Playback::internal::DefaultDevice captureInfo;
captureInfo.name = device.capture.name;
@ -35,16 +35,16 @@ namespace Soundux
{
ma_context context;
if (ma_context_init(0, 0, 0, &context) != MA_SUCCESS)
if (ma_context_init(nullptr, 0, nullptr, &context) != MA_SUCCESS)
{
std::cerr << "Failed to initialize context" << std::endl;
return {};
}
ma_device_info *pPlayBackDeviceInfos;
ma_uint32 deviceCount;
ma_device_info *pPlayBackDeviceInfos{};
ma_uint32 deviceCount{};
ma_result result = ma_context_get_devices(&context, &pPlayBackDeviceInfos, &deviceCount, 0, 0);
ma_result result = ma_context_get_devices(&context, &pPlayBackDeviceInfos, &deviceCount, nullptr, nullptr);
if (result != MA_SUCCESS)
{
std::cerr << "Failed to get playback devices!" << std::endl;
@ -65,16 +65,16 @@ namespace Soundux
{
ma_context context;
if (ma_context_init(0, 0, 0, &context) != MA_SUCCESS)
if (ma_context_init(nullptr, 0, nullptr, &context) != MA_SUCCESS)
{
std::cerr << "Failed to initialize context" << std::endl;
return {};
}
ma_device_info *pCaptureDeviceInfos;
ma_uint32 deviceCount;
ma_device_info *pCaptureDeviceInfos{};
ma_uint32 deviceCount{};
ma_result result = ma_context_get_devices(&context, &pCaptureDeviceInfos, &deviceCount, 0, 0);
ma_result result = ma_context_get_devices(&context, &pCaptureDeviceInfos, &deviceCount, nullptr, nullptr);
if (result != MA_SUCCESS)
{
std::cerr << "Failed to get playback devices!" << std::endl;
@ -102,10 +102,12 @@ namespace Soundux
//? Theoretically we could remove this, but this will result in the defaultPlayBackVolume being 0. This will
//? only change when the user manually changes this value in the ui where the default value will not match.
if (usedDevices.find(defaultPlayback.name) == usedDevices.end())
usedDevices.insert(std::make_pair(defaultPlayback.name, 1.f));
{
usedDevices.insert(std::make_pair(defaultPlayback.name, 1.F));
}
ma_decoder *decoder = new ma_decoder;
ma_result result = ma_decoder_init_file(file.c_str(), 0, decoder);
auto *decoder = new ma_decoder;
ma_result result = ma_decoder_init_file(file.c_str(), nullptr, decoder);
if (result != MA_SUCCESS)
{
@ -113,7 +115,7 @@ namespace Soundux
return -1;
}
ma_device *device = new ma_device;
auto *device = new ma_device;
ma_device_config config = ma_device_config_init(ma_device_type_playback);
config.playback.format = decoder->outputFormat;
config.playback.channels = decoder->outputChannels;
@ -121,7 +123,7 @@ namespace Soundux
config.dataCallback = internal::data_callback;
config.pUserData = decoder;
if (ma_device_init(0, &config, device) != MA_SUCCESS)
if (ma_device_init(nullptr, &config, device) != MA_SUCCESS)
{
std::cerr << "Failed to open playback device" << std::endl;
return -1;
@ -145,10 +147,12 @@ namespace Soundux
static std::uint64_t counter = 0;
if (usedDevices.find(deviceInfo.name) == usedDevices.end())
usedDevices.insert(std::make_pair(deviceInfo.name, 1.f));
{
usedDevices.insert(std::make_pair(deviceInfo.name, 1.F));
}
ma_decoder *decoder = new ma_decoder;
ma_result result = ma_decoder_init_file(file.c_str(), 0, decoder);
auto *decoder = new ma_decoder;
ma_result result = ma_decoder_init_file(file.c_str(), nullptr, decoder);
if (result != MA_SUCCESS)
{
@ -156,7 +160,7 @@ namespace Soundux
return -1;
}
ma_device *device = new ma_device;
auto *device = new ma_device;
ma_device_config config = ma_device_config_init(ma_device_type_playback);
config.playback.format = decoder->outputFormat;
config.playback.channels = decoder->outputChannels;
@ -165,7 +169,7 @@ namespace Soundux
config.playback.pDeviceID = &deviceInfo.id;
config.pUserData = decoder;
if (ma_device_init(0, &config, device) != MA_SUCCESS)
if (ma_device_init(nullptr, &config, device) != MA_SUCCESS)
{
std::cerr << "Failed to open playback device" << std::endl;
return -1;
@ -267,12 +271,16 @@ namespace Soundux
void Playback::internal::data_callback(ma_device *device, void *output, [[maybe_unused]] const void *input,
std::uint32_t frameCount)
{
ma_decoder *decoder = reinterpret_cast<ma_decoder *>(device->pUserData);
if (decoder == 0)
auto *decoder = reinterpret_cast<ma_decoder *>(device->pUserData);
if (decoder == nullptr)
{
return;
}
if (usedDevices.find(device->playback.name) != usedDevices.end())
{
device->masterVolumeFactor = usedDevices[device->playback.name];
}
auto readFrames = ma_decoder_read_pcm_frames(decoder, output, frameCount);

View File

@ -1,15 +1,15 @@
#pragma once
#ifdef __linux__
#include <stdexcept>
#include <exception>
#include <optional>
#include <iostream>
#include <cstdio>
#include <string>
#include <memory>
#include <vector>
#include <array>
#include <cstdio>
#include <exception>
#include <iostream>
#include <memory>
#include <optional>
#include <regex>
#include <stdexcept>
#include <string>
#include <vector>
#include "global.h"
#include "../config/config.h"
@ -61,7 +61,7 @@ namespace Soundux
std::string resampleMethod;
std::string processBinary;
operator bool()
operator bool() const
{
return index >= 0;
}
@ -76,7 +76,8 @@ namespace Soundux
inline std::string createSink()
{
system(("pactl load-module module-null-sink sink_name=" + internal::sinkName +
" sink_properties=device.description=" + internal::sinkName + " > nul").c_str());
" sink_properties=device.description=" + internal::sinkName + " > nul")
.c_str());
auto defaultInput = internal::getDefaultCaptureDevice();
// Create loopback for input
@ -91,13 +92,14 @@ namespace Soundux
};
inline void deleteSink()
{
// TODO: only unload soundboard sink
// TODO(d3s0x): only unload soundboard sink
system("pactl unload-module module-null-sink 2> nul");
system("pactl unload-module module-loopback 2> nul");
};
inline auto getSources()
{
using namespace internal;
using internal::getOutput;
using internal::PulseAudioRecordingStream;
auto input = getOutput("pactl list source-outputs");
@ -106,7 +108,9 @@ namespace Soundux
auto ss = std::stringstream{str};
for (std::string line; std::getline(ss, line, '\n');)
{
result.push_back(line);
}
return result;
};
@ -136,13 +140,21 @@ namespace Soundux
else if (stream)
{
if (match[4].matched)
{
stream.driver = match[4];
}
else if (match[6].matched)
{
stream.source = match[6];
}
else if (match[8].matched)
{
stream.processBinary = match[8];
}
else if (match[10].matched)
{
stream.resampleMethod = match[10];
}
}
}
}