Various improvements considering the modern C++ practices

This commit is contained in:
Enes Hecan 2022-09-30 12:18:26 +02:00
parent 034a663dbb
commit 1df6080535
18 changed files with 164 additions and 52 deletions

114
.clang-format Normal file
View File

@ -0,0 +1,114 @@
---
Language: Cpp
Standard: Auto
AccessModifierOffset: -4
AlignAfterOpenBracket: DontAlign
AlignArrayOfStructures: None
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: Consecutive
AlignConsecutiveDeclarations: None
AlignConsecutiveMacros: None
AlignEscapedNewlines: Right
AlignOperands: DontAlign
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: true
BinPackParameters: true
BitFieldColonSpacing: Both
BreakAfterJavaFieldAnnotations: true
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeConceptDeclarations: true
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
BreakConstructorInitializersBeforeComma: false
BreakInheritanceList: BeforeColon
BreakStringLiterals: true
ColumnLimit: 0
CompactNamespaces: true
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 8
ContinuationIndentWidth: 4
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
FixNamespaceComments: false
IncludeBlocks: Preserve
IncludeIsMainRegex: (Test)?$
IncludeIsMainSourceRegex: ''
IndentAccessModifiers: false
IndentCaseBlocks: false
IndentCaseLabels: true
IndentExternBlock: AfterExternBlock
IndentGotoLabels: true
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
LambdaBodyIndentation: Signature
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
PPIndentWidth: -1
PackConstructorInitializers: BinPack
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyIndentedWhitespace: 0
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
QualifierAlignment: Leave
ReferenceAlignment: Pointer
ReflowComments: true
RemoveBracesLLVM: false
SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 1
SortIncludes: CaseSensitive
SortJavaStaticImport: Before
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: Never
SpacesInCStyleCastParentheses: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInParentheses: false
SpacesInSquareBrackets: false
TabWidth: 4
UseCRLF: false
UseTab: Never
...

6
.clang-tidy Normal file
View File

@ -0,0 +1,6 @@
---
Checks: '-*,clang-diagnostic-*,bugprone-*,cert-*,misc-*,modernize-*,performance-*,readability-*,hicpp-exception-baseclass,hicpp-avoid-goto,-clang-diagnostic-address-of-packed-member,-cert-dcl16-c,-cert-dcl21-cpp,-cert-err58-cpp,-misc-unused-parameters,-misc-non-private-member-variables-in-classes,-modernize-avoid-c-arrays,-modernize-concat-nested-namespaces,-modernize-raw-string-literal,-modernize-use-default-member-init,-modernize-use-nodiscard,-modernize-use-override,-modernize-use-trailing-return-type,-readability-else-after-return,-readability-identifier-naming,-readability-implicit-bool-cast,-readability-implicit-bool-conversion,-readability-magic-numbers,-readability-named-parameter,-readability-qualified-auto,-readability-redundant-access-specifiers,-readability-redundant-member-init,-readability-uppercase-literal-suffix'
WarningsAsErrors: '*'
HeaderFilterRegex: ''
FormatStyle: none
...

3
.gitignore vendored
View File

@ -12,6 +12,7 @@ _deps
build/
result
/.vscode/
/.idea
*.o
*-protocol.c
@ -22,4 +23,4 @@ hyprctl/hyprctl
gmon.out
*.out
*.tar.gz
*.tar.gz

View File

@ -1,6 +1,6 @@
#include "Hyprpaper.hpp"
CHyprpaper::CHyprpaper() { }
CHyprpaper::CHyprpaper() = default;
void CHyprpaper::init() {
@ -9,12 +9,11 @@ void CHyprpaper::init() {
g_pConfigManager = std::make_unique<CConfigManager>();
g_pIPCSocket = std::make_unique<CIPCSocket>();
m_sDisplay = (wl_display *)wl_display_connect(NULL);
m_sDisplay = (wl_display *)wl_display_connect(nullptr);
if (!m_sDisplay) {
Debug::log(CRIT, "No wayland compositor running!");
exit(1);
return;
}
preloadAllWallpapersFromConfig();
@ -35,7 +34,7 @@ void CHyprpaper::init() {
m_bShouldExit = true;
}).detach();
while (1) { // we also tick every 1ms for socket and other shit's updates
while (true) { // we also tick every 1ms for socket and other shit's updates
tick(false);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
@ -352,7 +351,7 @@ void CHyprpaper::createBuffer(SPoolBuffer* pBuffer, int32_t w, int32_t h, uint32
exit(1);
}
const auto DATA = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, FD, 0);
const auto DATA = mmap(nullptr, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, FD, 0);
const auto POOL = wl_shm_create_pool(g_pHyprpaper->m_sSHM, FD, SIZE);
pBuffer->buffer = wl_shm_pool_create_buffer(POOL, 0, w, h, STRIDE, format);
wl_shm_pool_destroy(POOL);
@ -470,4 +469,4 @@ void CHyprpaper::renderWallpaperForMonitor(SMonitor* pMonitor) {
}
}
}
}
}

View File

@ -60,4 +60,4 @@ private:
bool m_bShouldExit = false;
};
inline std::unique_ptr<CHyprpaper> g_pHyprpaper;
inline std::unique_ptr<CHyprpaper> g_pHyprpaper;

View File

@ -5,7 +5,7 @@ CConfigManager::CConfigManager() {
// init the entire thing
std::string configPath;
if (g_pHyprpaper->m_szExplicitConfigPath == "") {
if (g_pHyprpaper->m_szExplicitConfigPath.empty()) {
const char *const ENVHOME = getenv("HOME");
configPath = ENVHOME + (std::string) "/.config/hypr/hyprpaper.conf";
}
@ -19,7 +19,6 @@ CConfigManager::CConfigManager() {
if (!ifs.good()) {
Debug::log(CRIT, "Hyprpaper was not provided a config!");
exit(1);
return; //jic
}
std::string line = "";
@ -36,7 +35,7 @@ CConfigManager::CConfigManager() {
parseError += "Config error at line " + std::to_string(linenum) + ": Line parsing error.";
}
if (parseError != "" && parseError.find("Config error at line") != 0) {
if (!parseError.empty() && parseError.find("Config error at line") != 0) {
parseError = "Config error at line " + std::to_string(linenum) + ": " + parseError;
}
@ -46,7 +45,7 @@ CConfigManager::CConfigManager() {
ifs.close();
}
if (parseError != "") {
if (!parseError.empty()) {
Debug::log(CRIT, "Exiting because of config parse errors!\n%s", parseError.c_str());
exit(1);
return;

View File

@ -11,7 +11,7 @@ public:
std::deque<std::string> m_dRequestedPreloads;
private:
std::string parseError = "";
std::string parseError;
void parseLine(std::string&);
std::string removeBeginEndSpacesTabs(std::string in);
@ -24,4 +24,4 @@ private:
friend class CIPCSocket;
};
inline std::unique_ptr<CConfigManager> g_pConfigManager;
inline std::unique_ptr<CConfigManager> g_pConfigManager;

View File

@ -14,4 +14,4 @@ enum LogLevel {
namespace Debug {
void log(LogLevel level, const char* fmt, ...);
};
}

View File

@ -26,4 +26,4 @@ namespace Events {
inline const zwlr_layer_surface_v1_listener layersurfaceListener = { .configure = ls_configure };
inline const struct wl_registry_listener registryListener = { .global = handleGlobal, .global_remove = handleGlobalRemove };
};
}

View File

@ -22,7 +22,7 @@ cairo_surface_t* JPEG::createSurfaceFromJPEG(const std::string& path) {
void* imageRawData;
struct stat fileInfo;
struct stat fileInfo = {};
const auto FD = open(path.c_str(), O_RDONLY);
@ -36,8 +36,8 @@ cairo_surface_t* JPEG::createSurfaceFromJPEG(const std::string& path) {
// now the JPEG is in the memory
jpeg_decompress_struct decompressStruct;
jpeg_error_mgr errorManager;
jpeg_decompress_struct decompressStruct = {};
jpeg_error_mgr errorManager = {};
decompressStruct.err = jpeg_std_error(&errorManager);
jpeg_create_decompress(&decompressStruct);
@ -72,4 +72,4 @@ cairo_surface_t* JPEG::createSurfaceFromJPEG(const std::string& path) {
jpeg_destroy_decompress(&decompressStruct);
return cairoSurface;
}
}

View File

@ -6,7 +6,7 @@ Vector2D::Vector2D(double xx, double yy) {
}
Vector2D::Vector2D() { x = 0; y = 0; }
Vector2D::~Vector2D() {}
Vector2D::~Vector2D() = default;
double Vector2D::normalize() {
// get max abs
@ -18,6 +18,6 @@ double Vector2D::normalize() {
return max;
}
Vector2D Vector2D::floor() {
return Vector2D((int)x, (int)y);
}
Vector2D Vector2D::floor() const {
return {static_cast<int>(x), static_cast<int>(y)};
}

View File

@ -1,6 +1,6 @@
#pragma once
#include <math.h>
#include <cmath>
class Vector2D {
public:
@ -35,5 +35,5 @@ class Vector2D {
return a.x != x || a.y != y;
}
Vector2D floor();
};
Vector2D floor() const;
};

View File

@ -4,12 +4,12 @@
#include <deque>
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstring>
#include <string>
#include <pthread.h>
#include <cmath>
#include <math.h>
#include <cmath>
#define class _class
#define namespace _namespace
@ -27,13 +27,13 @@ extern "C" {
#include <GLES3/gl32.h>
#include <GLES3/gl3ext.h>
#include <assert.h>
#include <cassert>
#include <cairo.h>
#include <cairo/cairo.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/mman.h>
#include <unistd.h>
#include <wayland-client.h>

View File

@ -2,15 +2,15 @@
#include "../Hyprpaper.hpp"
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include <errno.h>
#include <cerrno>
void CIPCSocket::initialize() {
std::thread([&]() {
@ -40,7 +40,7 @@ void CIPCSocket::initialize() {
// 10 max queued.
listen(SOCKET, 10);
sockaddr_in clientAddress;
sockaddr_in clientAddress = {};
socklen_t clientSize = sizeof(clientAddress);
char readBuffer[1024] = {0};
@ -104,7 +104,7 @@ bool CIPCSocket::mainThreadParseRequest() {
g_pConfigManager->parseKeyword(copy.substr(0, copy.find_first_of(' ')), copy.substr(copy.find_first_of(' ') + 1));
if (g_pConfigManager->parseError != "") {
if (!g_pConfigManager->parseError.empty()) {
m_szReply = g_pConfigManager->parseError;
m_bReplyReady = true;
m_bRequestReady = false;
@ -123,4 +123,4 @@ bool CIPCSocket::mainThreadParseRequest() {
m_bRequestReady = false;
return true;
}
}

View File

@ -10,7 +10,6 @@ CLayerSurface::CLayerSurface(SMonitor* pMonitor) {
if (!pSurface) {
Debug::log(CRIT, "The compositor did not allow hyprpaper a surface!");
exit(1);
return;
}
const auto PINPUTREGION = wl_compositor_create_region(g_pHyprpaper->m_sCompositor);
@ -18,7 +17,6 @@ CLayerSurface::CLayerSurface(SMonitor* pMonitor) {
if (!PINPUTREGION) {
Debug::log(CRIT, "The compositor did not allow hyprpaper a region!");
exit(1);
return;
}
wl_surface_set_input_region(pSurface, PINPUTREGION);
@ -28,7 +26,6 @@ CLayerSurface::CLayerSurface(SMonitor* pMonitor) {
if (!pLayerSurface) {
Debug::log(CRIT, "The compositor did not allow hyprpaper a layersurface!");
exit(1);
return;
}
zwlr_layer_surface_v1_set_size(pLayerSurface, 0, 0);
@ -47,4 +44,4 @@ CLayerSurface::~CLayerSurface() {
zwlr_layer_surface_v1_destroy(pLayerSurface);
wl_display_flush(g_pHyprpaper->m_sDisplay);
}
}

View File

@ -6,13 +6,11 @@ struct SMonitor;
class CLayerSurface {
public:
CLayerSurface(SMonitor*);
explicit CLayerSurface(SMonitor*);
~CLayerSurface();
SMonitor* m_pMonitor = nullptr;
zwlr_layer_surface_v1* pLayerSurface = nullptr;
wl_surface* pSurface = nullptr;
bool m_bCurrent = false;
};
};

View File

@ -18,7 +18,6 @@ void CWallpaperTarget::create(const std::string& path) {
} else {
Debug::log(CRIT, "unrecognized image %s", path.c_str());
exit(1);
return;
}
if (cairo_surface_status(CAIROSURFACE) != CAIRO_STATUS_SUCCESS) {
@ -33,4 +32,4 @@ void CWallpaperTarget::create(const std::string& path) {
Debug::log(LOG, "Preloaded target %s in %.2fms -> Pixel size: [%i, %i]", path.c_str(), MS, (int)m_vSize.x, (int)m_vSize.y);
m_pCairoSurface = CAIROSURFACE;
}
}

View File

@ -9,7 +9,6 @@ public:
~CWallpaperTarget();
void create(const std::string& path);
void render();
std::string m_szPath;
@ -18,4 +17,4 @@ public:
bool m_bHasAlpha = true;
cairo_surface_t* m_pCairoSurface;
};
};