modernize dep internals

This commit is contained in:
Vaxry 2024-12-13 21:03:21 +00:00
parent a3ceb20095
commit e527a6667c
10 changed files with 91 additions and 105 deletions

View file

@ -63,8 +63,8 @@ pkg_check_modules(
libjxl libjxl
libjxl_cms libjxl_cms
libjxl_threads libjxl_threads
hyprlang>=0.2.0 hyprlang>=0.6.0
hyprutils>=0.2.0 hyprutils>=0.2.4
hyprgraphics) hyprgraphics)
file(GLOB_RECURSE SRCFILES "src/*.cpp") file(GLOB_RECURSE SRCFILES "src/*.cpp")

View file

@ -39,7 +39,7 @@ static void handleGlobal(CCWlRegistry* registry, uint32_t name, const char* inte
static void handleGlobalRemove(CCWlRegistry* registry, uint32_t name) { static void handleGlobalRemove(CCWlRegistry* registry, uint32_t name) {
for (auto& m : g_pHyprpaper->m_vMonitors) { for (auto& m : g_pHyprpaper->m_vMonitors) {
if (m->wayland_name == name) { if (m->wayland_name == name) {
Debug::log(LOG, "Destroying output %s", m->name.c_str()); Debug::log(LOG, "Destroying output {}", m->name);
g_pHyprpaper->clearWallpaperFromMonitor(m->name); g_pHyprpaper->clearWallpaperFromMonitor(m->name);
std::erase_if(g_pHyprpaper->m_vMonitors, [&](const auto& other) { return other->wayland_name == name; }); std::erase_if(g_pHyprpaper->m_vMonitors, [&](const auto& other) { return other->wayland_name == name; });
return; return;
@ -139,7 +139,7 @@ void CHyprpaper::unloadWallpaper(const std::string& path) {
const auto PRELOADPATH = it->get()->name; const auto PRELOADPATH = it->get()->name;
Debug::log(LOG, "Unloading target %s, preload path %s", path.c_str(), PRELOADPATH.c_str()); Debug::log(LOG, "Unloading target {}, preload path {}", path, PRELOADPATH);
std::filesystem::remove(PRELOADPATH); std::filesystem::remove(PRELOADPATH);
@ -161,7 +161,7 @@ void CHyprpaper::preloadAllWallpapersFromConfig() {
bool exists = false; bool exists = false;
for (auto& [ewp, cls] : m_mWallpaperTargets) { for (auto& [ewp, cls] : m_mWallpaperTargets) {
if (ewp == wp) { if (ewp == wp) {
Debug::log(LOG, "Ignoring request to preload %s as it already is preloaded!", ewp.c_str()); Debug::log(LOG, "Ignoring request to preload {} as it already is preloaded!", ewp);
exists = true; exists = true;
break; break;
} }
@ -243,14 +243,13 @@ void CHyprpaper::removeOldHyprpaperImages() {
memoryFreed += entry.file_size(); memoryFreed += entry.file_size();
if (!std::filesystem::remove(entry.path())) if (!std::filesystem::remove(entry.path()))
Debug::log(LOG, "Couldn't remove %s", entry.path().string().c_str()); Debug::log(LOG, "Couldn't remove {}", entry.path().string());
cleaned++; cleaned++;
} }
} }
if (cleaned != 0) { if (cleaned != 0)
Debug::log(LOG, "Cleaned old hyprpaper preloads (%i), removing %.1fMB", cleaned, ((float)memoryFreed) / 1000000.f); Debug::log(LOG, "Cleaned old hyprpaper preloads ({}), removing {:.1f}MB", cleaned, ((float)memoryFreed) / 1000000.f);
}
} }
SMonitor* CHyprpaper::getMonitorFromName(const std::string& monname) { SMonitor* CHyprpaper::getMonitorFromName(const std::string& monname) {
@ -294,7 +293,7 @@ void CHyprpaper::ensurePoolBuffersPresent() {
PBUFFER->target = wt.m_szPath; PBUFFER->target = wt.m_szPath;
Debug::log(LOG, "Buffer created for target %s, Shared Memory usage: %.1fMB", wt.m_szPath.c_str(), PBUFFER->size / 1000000.f); Debug::log(LOG, "Buffer created for target {}, Shared Memory usage: {:.1f}MB", wt.m_szPath, PBUFFER->size / 1000000.f);
anyNewBuffers = true; anyNewBuffers = true;
} }
@ -308,7 +307,7 @@ void CHyprpaper::ensurePoolBuffersPresent() {
bytesUsed += bf->size; bytesUsed += bf->size;
} }
Debug::log(LOG, "Total SM usage for all buffers: %.1fMB", bytesUsed / 1000000.f); Debug::log(LOG, "Total SM usage for all buffers: {:.1f}MB", bytesUsed / 1000000.f);
} }
} }
@ -398,7 +397,7 @@ void CHyprpaper::ensureMonitorHasActiveWallpaper(SMonitor* pMonitor) {
if (!it->second) { if (!it->second) {
pMonitor->hasATarget = false; pMonitor->hasATarget = false;
Debug::log(WARN, "Monitor %s does not have a target! A wallpaper will not be created.", pMonitor->name.c_str()); Debug::log(WARN, "Monitor {} does not have a target! A wallpaper will not be created.", pMonitor->name);
return; return;
} }
@ -507,8 +506,8 @@ SPoolBuffer* CHyprpaper::getPoolBuffer(SMonitor* pMonitor, CWallpaperTarget* pWa
} }
void CHyprpaper::renderWallpaperForMonitor(SMonitor* pMonitor) { void CHyprpaper::renderWallpaperForMonitor(SMonitor* pMonitor) {
static auto* const PRENDERSPLASH = reinterpret_cast<Hyprlang::INT* const*>(g_pConfigManager->config->getConfigValuePtr("splash")->getDataStaticPtr()); static auto PRENDERSPLASH = Hyprlang::CSimpleConfigValue<Hyprlang::INT>(g_pConfigManager->config.get(), "splash");
static auto* const PSPLASHOFFSET = reinterpret_cast<Hyprlang::FLOAT* const*>(g_pConfigManager->config->getConfigValuePtr("splash_offset")->getDataStaticPtr()); static auto PSPLASHOFFSET = Hyprlang::CSimpleConfigValue<Hyprlang::FLOAT>(g_pConfigManager->config.get(), "splash_offset");
if (!m_mMonitorActiveWallpaperTargets[pMonitor]) if (!m_mMonitorActiveWallpaperTargets[pMonitor])
recheckMonitor(pMonitor); recheckMonitor(pMonitor);
@ -565,8 +564,8 @@ void CHyprpaper::renderWallpaperForMonitor(SMonitor* pMonitor) {
origin.x = -(PWALLPAPERTARGET->m_vSize.x * scale - DIMENSIONS.x) / 2.0 / scale; origin.x = -(PWALLPAPERTARGET->m_vSize.x * scale - DIMENSIONS.x) / 2.0 / scale;
} }
Debug::log(LOG, "Image data for %s: %s at [%.2f, %.2f], scale: %.2f (original image size: [%i, %i])", pMonitor->name.c_str(), PWALLPAPERTARGET->m_szPath.c_str(), origin.x, Debug::log(LOG, "Image data for {}: {} at [{:.2f}, {:.2f}], scale: {:.2f} (original image size: [{}, {}])", pMonitor->name, PWALLPAPERTARGET->m_szPath, origin.x, origin.y,
origin.y, scale, (int)PWALLPAPERTARGET->m_vSize.x, (int)PWALLPAPERTARGET->m_vSize.y); scale, (int)PWALLPAPERTARGET->m_vSize.x, (int)PWALLPAPERTARGET->m_vSize.y);
if (TILE) { if (TILE) {
cairo_pattern_t* pattern = cairo_pattern_create_for_surface(PWALLPAPERTARGET->m_pCairoSurface->cairo()); cairo_pattern_t* pattern = cairo_pattern_create_for_surface(PWALLPAPERTARGET->m_pCairoSurface->cairo());
@ -579,7 +578,7 @@ void CHyprpaper::renderWallpaperForMonitor(SMonitor* pMonitor) {
cairo_paint(PCAIRO); cairo_paint(PCAIRO);
if (**PRENDERSPLASH && getenv("HYPRLAND_INSTANCE_SIGNATURE")) { if (*PRENDERSPLASH && getenv("HYPRLAND_INSTANCE_SIGNATURE")) {
auto SPLASH = execAndGet("hyprctl splash"); auto SPLASH = execAndGet("hyprctl splash");
SPLASH.pop_back(); SPLASH.pop_back();
@ -590,20 +589,20 @@ void CHyprpaper::renderWallpaperForMonitor(SMonitor* pMonitor) {
const auto FONTSIZE = (int)(DIMENSIONS.y / 76.0 / scale); const auto FONTSIZE = (int)(DIMENSIONS.y / 76.0 / scale);
cairo_set_font_size(PCAIRO, FONTSIZE); cairo_set_font_size(PCAIRO, FONTSIZE);
static auto* const PSPLASHCOLOR = reinterpret_cast<Hyprlang::INT* const*>(g_pConfigManager->config->getConfigValuePtr("splash_color")->getDataStaticPtr()); static auto PSPLASHCOLOR = Hyprlang::CSimpleConfigValue<Hyprlang::INT>(g_pConfigManager->config.get(), "splash_color");
Debug::log(LOG, "Splash color: %x", **PSPLASHCOLOR); Debug::log(LOG, "Splash color: {:x}", *PSPLASHCOLOR);
cairo_set_source_rgba(PCAIRO, ((**PSPLASHCOLOR >> 16) & 0xFF) / 255.0, ((**PSPLASHCOLOR >> 8) & 0xFF) / 255.0, (**PSPLASHCOLOR & 0xFF) / 255.0, cairo_set_source_rgba(PCAIRO, ((*PSPLASHCOLOR >> 16) & 0xFF) / 255.0, ((**PSPLASHCOLOR >> 8) & 0xFF) / 255.0, (*PSPLASHCOLOR & 0xFF) / 255.0,
((**PSPLASHCOLOR >> 24) & 0xFF) / 255.0); ((*PSPLASHCOLOR >> 24) & 0xFF) / 255.0);
cairo_text_extents_t textExtents; cairo_text_extents_t textExtents;
cairo_text_extents(PCAIRO, SPLASH.c_str(), &textExtents); cairo_text_extents(PCAIRO, SPLASH.c_str(), &textExtents);
cairo_move_to(PCAIRO, ((DIMENSIONS.x - textExtents.width * scale) / 2.0) / scale, ((DIMENSIONS.y * (100 - **PSPLASHOFFSET)) / 100 - textExtents.height * scale) / scale); cairo_move_to(PCAIRO, ((DIMENSIONS.x - textExtents.width * scale) / 2.0) / scale, ((DIMENSIONS.y * (100 - *PSPLASHOFFSET)) / 100 - textExtents.height * scale) / scale);
Debug::log(LOG, "Splash font size: %d, pos: %.2f, %.2f", FONTSIZE, (DIMENSIONS.x - textExtents.width) / 2.0 / scale, Debug::log(LOG, "Splash font size: {}, pos: {:.2f}, {:.2f}", FONTSIZE, (DIMENSIONS.x - textExtents.width) / 2.0 / scale,
((DIMENSIONS.y * (100 - **PSPLASHOFFSET)) / 100 - textExtents.height * scale) / scale); ((DIMENSIONS.y * (100 - *PSPLASHOFFSET)) / 100 - textExtents.height * scale) / scale);
cairo_show_text(PCAIRO, SPLASH.c_str()); cairo_show_text(PCAIRO, SPLASH.c_str());
@ -623,7 +622,7 @@ void CHyprpaper::renderWallpaperForMonitor(SMonitor* pMonitor) {
pMonitor->pCurrentLayerSurface->pSurface->sendSetOpaqueRegion(opaqueRegion.get()); pMonitor->pCurrentLayerSurface->pSurface->sendSetOpaqueRegion(opaqueRegion.get());
if (pMonitor->pCurrentLayerSurface->pFractionalScaleInfo) { if (pMonitor->pCurrentLayerSurface->pFractionalScaleInfo) {
Debug::log(LOG, "Submitting viewport dest size %ix%i for %x", static_cast<int>(std::round(pMonitor->size.x)), static_cast<int>(std::round(pMonitor->size.y)), Debug::log(LOG, "Submitting viewport dest size {}x{} for {:x}", static_cast<int>(std::round(pMonitor->size.x)), static_cast<int>(std::round(pMonitor->size.y)),
pMonitor->pCurrentLayerSurface); pMonitor->pCurrentLayerSurface);
pMonitor->pCurrentLayerSurface->pViewport->sendSetDestination(static_cast<int>(std::round(pMonitor->size.x)), static_cast<int>(std::round(pMonitor->size.y))); pMonitor->pCurrentLayerSurface->pViewport->sendSetDestination(static_cast<int>(std::round(pMonitor->size.x)), static_cast<int>(std::round(pMonitor->size.y)));
} }

View file

@ -1,49 +0,0 @@
#include "Log.hpp"
#include "../includes.hpp"
#include <fstream>
#include <iostream>
void Debug::log(LogLevel level, const char* fmt, ...) {
std::string levelstr = "";
switch (level) {
case LOG: levelstr = "[LOG] "; break;
case WARN: levelstr = "[WARN] "; break;
case ERR: levelstr = "[ERR] "; break;
case CRIT: levelstr = "[CRITICAL] "; break;
case INFO: levelstr = "[INFO] "; break;
default: break;
}
char buf[LOGMESSAGESIZE] = "";
char* outputStr;
int logLen;
va_list args;
va_start(args, fmt);
logLen = vsnprintf(buf, sizeof buf, fmt, args);
va_end(args);
if ((long unsigned int)logLen < sizeof buf) {
outputStr = strdup(buf);
} else {
outputStr = (char*)malloc(logLen + 1);
if (!outputStr) {
printf("CRITICAL: Cannot alloc size %d for log! (Out of memory?)", logLen + 1);
return;
}
va_start(args, fmt);
vsnprintf(outputStr, logLen + 1U, fmt, args);
va_end(args);
}
// hyprpaper only logs to stdout
std::cout << levelstr << outputStr << "\n";
// free the log
free(outputStr);
}

View file

@ -1,17 +1,57 @@
#pragma once #pragma once
#include <format>
#include <iostream>
#include <string> #include <string>
#define LOGMESSAGESIZE 1024 enum eLogLevel {
TRACE = 0,
enum LogLevel { INFO,
NONE = -1, LOG,
LOG = 0,
WARN, WARN,
ERR, ERR,
CRIT, CRIT,
INFO NONE
}; };
#define RASSERT(expr, reason, ...) \
if (!(expr)) { \
Debug::log(CRIT, "\n==========================================================================================\nASSERTION FAILED! \n\n{}\n\nat: line {} in {}", \
std::format(reason, ##__VA_ARGS__), __LINE__, \
([]() constexpr -> std::string { return std::string(__FILE__).substr(std::string(__FILE__).find_last_of('/') + 1); })().c_str()); \
std::abort(); \
}
#define ASSERT(expr) RASSERT(expr, "?")
namespace Debug { namespace Debug {
void log(LogLevel level, const char* fmt, ...); inline bool quiet = false;
} inline bool verbose = false;
template <typename... Args>
void log(eLogLevel level, const std::string& fmt, Args&&... args) {
if (!verbose && level == TRACE)
return;
if (quiet)
return;
if (level != NONE) {
std::cout << '[';
switch (level) {
case TRACE: std::cout << "TRACE"; break;
case INFO: std::cout << "INFO"; break;
case LOG: std::cout << "LOG"; break;
case WARN: std::cout << "WARN"; break;
case ERR: std::cout << "ERR"; break;
case CRIT: std::cout << "CRITICAL"; break;
default: break;
}
std::cout << "] ";
}
std::cout << std::vformat(fmt, std::make_format_args(args...)) << std::endl;
}
};

View file

@ -3,6 +3,9 @@
#include "../debug/Log.hpp" #include "../debug/Log.hpp"
#include <memory> #include <memory>
#include <hyprutils/os/Process.hpp>
using namespace Hyprutils::OS;
bool vectorDeltaLessThan(const Vector2D& a, const Vector2D& b, const float& delta) { bool vectorDeltaLessThan(const Vector2D& a, const Vector2D& b, const float& delta) {
return std::abs(a.x - b.x) < delta && std::abs(a.y - b.y) < delta; return std::abs(a.x - b.x) < delta && std::abs(a.y - b.y) < delta;
} }
@ -12,15 +15,8 @@ bool vectorDeltaLessThan(const Vector2D& a, const Vector2D& b, const Vector2D& d
} }
std::string execAndGet(const char* cmd) { std::string execAndGet(const char* cmd) {
std::array<char, 128> buffer; CProcess proc("/bin/bash", {cmd});
std::string result; if (!proc.runSync())
const std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
Debug::log(ERR, "execAndGet: failed in pipe");
return ""; return "";
} return proc.stdOut();
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
} }

View file

@ -50,7 +50,7 @@ void CIPCSocket::initialize() {
char readBuffer[1024] = {0}; char readBuffer[1024] = {0};
Debug::log(LOG, "hyprpaper socket started at %s (fd: %i)", socketPath.c_str(), SOCKET); Debug::log(LOG, "hyprpaper socket started at {} (fd: {})", socketPath, SOCKET);
while (1) { while (1) {
const auto ACCEPTEDCONNECTION = accept(SOCKET, (sockaddr*)&clientAddress, &clientSize); const auto ACCEPTEDCONNECTION = accept(SOCKET, (sockaddr*)&clientAddress, &clientSize);
if (ACCEPTEDCONNECTION < 0) { if (ACCEPTEDCONNECTION < 0) {
@ -58,7 +58,7 @@ void CIPCSocket::initialize() {
break; break;
} else { } else {
do { do {
Debug::log(LOG, "Accepted incoming socket connection request on fd %i", ACCEPTEDCONNECTION); Debug::log(LOG, "Accepted incoming socket connection request on fd {}", ACCEPTEDCONNECTION);
std::lock_guard<std::mutex> lg(g_pHyprpaper->m_mtTickMutex); std::lock_guard<std::mutex> lg(g_pHyprpaper->m_mtTickMutex);
auto messageSize = read(ACCEPTEDCONNECTION, readBuffer, 1024); auto messageSize = read(ACCEPTEDCONNECTION, readBuffer, 1024);
@ -100,7 +100,7 @@ bool CIPCSocket::mainThreadParseRequest() {
// now we can work on the copy // now we can work on the copy
Debug::log(LOG, "Received a request: %s", copy.c_str()); Debug::log(LOG, "Received a request: {}", copy);
// set default reply // set default reply
m_szReply = "ok"; m_szReply = "ok";
@ -123,7 +123,7 @@ bool CIPCSocket::mainThreadParseRequest() {
if (copy.find("listloaded") == 0) { if (copy.find("listloaded") == 0) {
const auto numWallpapersLoaded = g_pHyprpaper->m_mWallpaperTargets.size(); const auto numWallpapersLoaded = g_pHyprpaper->m_mWallpaperTargets.size();
Debug::log(LOG, "numWallpapersLoaded: %d", numWallpapersLoaded); Debug::log(LOG, "numWallpapersLoaded: {}", numWallpapersLoaded);
if (numWallpapersLoaded == 0) { if (numWallpapersLoaded == 0) {
m_szReply = "no wallpapers loaded"; m_szReply = "no wallpapers loaded";
@ -145,7 +145,7 @@ bool CIPCSocket::mainThreadParseRequest() {
if (copy.find("listactive") == 0) { if (copy.find("listactive") == 0) {
const auto numWallpapersActive = g_pHyprpaper->m_mMonitorActiveWallpapers.size(); const auto numWallpapersActive = g_pHyprpaper->m_mMonitorActiveWallpapers.size();
Debug::log(LOG, "numWallpapersActive: %d", numWallpapersActive); Debug::log(LOG, "numWallpapersActive: {}", numWallpapersActive);
if (numWallpapersActive == 0) { if (numWallpapersActive == 0) {
m_szReply = "no wallpapers active"; m_szReply = "no wallpapers active";

View file

@ -3,7 +3,7 @@
#include "Hyprpaper.hpp" #include "Hyprpaper.hpp"
int main(int argc, char** argv, char** envp) { int main(int argc, char** argv, char** envp) {
Debug::log(LOG, "Welcome to hyprpaper!\nbuilt from commit %s (%s)", GIT_COMMIT_HASH, GIT_COMMIT_MESSAGE); Debug::log(LOG, "Welcome to hyprpaper!\nbuilt from commit {} ({})", GIT_COMMIT_HASH, GIT_COMMIT_MESSAGE);
// parse some args // parse some args
std::string configPath; std::string configPath;
@ -11,7 +11,7 @@ int main(int argc, char** argv, char** envp) {
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
if ((!strcmp(argv[i], "-c") || !strcmp(argv[i], "--config")) && argc >= i + 2) { if ((!strcmp(argv[i], "-c") || !strcmp(argv[i], "--config")) && argc >= i + 2) {
configPath = std::string(argv[++i]); configPath = std::string(argv[++i]);
Debug::log(LOG, "Using config location %s.", configPath.c_str()); Debug::log(LOG, "Using config location {}.", configPath);
} else if (!strcmp(argv[i], "--no-fractional") || !strcmp(argv[i], "-n")) { } else if (!strcmp(argv[i], "--no-fractional") || !strcmp(argv[i], "-n")) {
noFractional = true; noFractional = true;
Debug::log(LOG, "Disabling fractional scaling support!"); Debug::log(LOG, "Disabling fractional scaling support!");

View file

@ -66,7 +66,7 @@ CLayerSurface::CLayerSurface(SMonitor* pMonitor) {
pFractionalScaleInfo->setPreferredScale([this](CCWpFractionalScaleV1* r, uint32_t sc120) { pFractionalScaleInfo->setPreferredScale([this](CCWpFractionalScaleV1* r, uint32_t sc120) {
const double SCALE = sc120 / 120.0; const double SCALE = sc120 / 120.0;
Debug::log(LOG, "handlePreferredScale: %.2lf for %lx", SCALE, this); Debug::log(LOG, "handlePreferredScale: {:.2f} for {:x}", SCALE, (uintptr_t)this);
if (fScale != SCALE) { if (fScale != SCALE) {
fScale = SCALE; fScale = SCALE;

View file

@ -15,7 +15,7 @@ void CWallpaperTarget::create(const std::string& path) {
auto loadedImage = CImage(path); auto loadedImage = CImage(path);
if (!loadedImage.success()) { if (!loadedImage.success()) {
Debug::log(CRIT, "Cannot load image %s: %s", path.c_str(), loadedImage.getError().c_str()); Debug::log(CRIT, "Cannot load image {}: {}", path, loadedImage.getError());
exit(1); exit(1);
} }
@ -23,7 +23,7 @@ void CWallpaperTarget::create(const std::string& path) {
const auto MS = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now() - BEGINLOAD).count() / 1000.f; const auto MS = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now() - BEGINLOAD).count() / 1000.f;
Debug::log(LOG, "Preloaded target %s in %.2fms -> Pixel size: [%i, %i]", path.c_str(), MS, (int)m_vSize.x, (int)m_vSize.y); Debug::log(LOG, "Preloaded target {} in {:.2f}ms -> Pixel size: [{}, {}]", path, MS, (int)m_vSize.x, (int)m_vSize.y);
m_pCairoSurface = loadedImage.cairoSurface(); m_pCairoSurface = loadedImage.cairoSurface();
} }