Skip to content

Commit

Permalink
Core: modernize internals (#219)
Browse files Browse the repository at this point in the history
* flake.nix: use gcc14Stdenv, update

---------

Co-authored-by: Mihai Fufezan <[email protected]>
  • Loading branch information
vaxerski and fufexan authored Dec 14, 2024
1 parent a3ceb20 commit 85e850b
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 122 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ pkg_check_modules(
libjxl
libjxl_cms
libjxl_threads
hyprlang>=0.2.0
hyprutils>=0.2.0
hyprlang>=0.6.0
hyprutils>=0.2.4
hyprgraphics)

file(GLOB_RECURSE SRCFILES "src/*.cpp")
Expand Down
30 changes: 15 additions & 15 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
inputs.hyprwayland-scanner.overlays.default
(final: prev: rec {
hyprpaper = final.callPackage ./nix/default.nix {
stdenv = final.gcc13Stdenv;
stdenv = final.gcc14Stdenv;
version = version + "+date=" + (mkDate (self.lastModifiedDate or "19700101")) + "_" + (self.shortRev or "dirty");
commit = self.rev or "";
};
Expand Down
47 changes: 23 additions & 24 deletions src/Hyprpaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static void handleGlobal(CCWlRegistry* registry, uint32_t name, const char* inte
static void handleGlobalRemove(CCWlRegistry* registry, uint32_t name) {
for (auto& m : g_pHyprpaper->m_vMonitors) {
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);
std::erase_if(g_pHyprpaper->m_vMonitors, [&](const auto& other) { return other->wayland_name == name; });
return;
Expand Down Expand Up @@ -139,7 +139,7 @@ void CHyprpaper::unloadWallpaper(const std::string& path) {

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);

Expand All @@ -161,7 +161,7 @@ void CHyprpaper::preloadAllWallpapersFromConfig() {
bool exists = false;
for (auto& [ewp, cls] : m_mWallpaperTargets) {
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;
break;
}
Expand Down Expand Up @@ -243,14 +243,13 @@ void CHyprpaper::removeOldHyprpaperImages() {

memoryFreed += entry.file_size();
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++;
}
}

if (cleaned != 0) {
Debug::log(LOG, "Cleaned old hyprpaper preloads (%i), removing %.1fMB", cleaned, ((float)memoryFreed) / 1000000.f);
}
if (cleaned != 0)
Debug::log(LOG, "Cleaned old hyprpaper preloads ({}), removing {:.1f}MB", cleaned, ((float)memoryFreed) / 1000000.f);
}

SMonitor* CHyprpaper::getMonitorFromName(const std::string& monname) {
Expand Down Expand Up @@ -294,7 +293,7 @@ void CHyprpaper::ensurePoolBuffersPresent() {

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;
}
Expand All @@ -308,7 +307,7 @@ void CHyprpaper::ensurePoolBuffersPresent() {
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);
}
}

Expand Down Expand Up @@ -398,7 +397,7 @@ void CHyprpaper::ensureMonitorHasActiveWallpaper(SMonitor* pMonitor) {

if (!it->second) {
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;
}

Expand Down Expand Up @@ -507,8 +506,8 @@ SPoolBuffer* CHyprpaper::getPoolBuffer(SMonitor* pMonitor, CWallpaperTarget* pWa
}

void CHyprpaper::renderWallpaperForMonitor(SMonitor* pMonitor) {
static auto* const PRENDERSPLASH = reinterpret_cast<Hyprlang::INT* const*>(g_pConfigManager->config->getConfigValuePtr("splash")->getDataStaticPtr());
static auto* const PSPLASHOFFSET = reinterpret_cast<Hyprlang::FLOAT* const*>(g_pConfigManager->config->getConfigValuePtr("splash_offset")->getDataStaticPtr());
static auto PRENDERSPLASH = Hyprlang::CSimpleConfigValue<Hyprlang::INT>(g_pConfigManager->config.get(), "splash");
static auto PSPLASHOFFSET = Hyprlang::CSimpleConfigValue<Hyprlang::FLOAT>(g_pConfigManager->config.get(), "splash_offset");

if (!m_mMonitorActiveWallpaperTargets[pMonitor])
recheckMonitor(pMonitor);
Expand Down Expand Up @@ -565,8 +564,8 @@ void CHyprpaper::renderWallpaperForMonitor(SMonitor* pMonitor) {
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,
origin.y, scale, (int)PWALLPAPERTARGET->m_vSize.x, (int)PWALLPAPERTARGET->m_vSize.y);
Debug::log(LOG, "Image data for {}: {} at [{:.2f}, {:.2f}], scale: {:.2f} (original image size: [{}, {}])", pMonitor->name, PWALLPAPERTARGET->m_szPath, origin.x, origin.y,
scale, (int)PWALLPAPERTARGET->m_vSize.x, (int)PWALLPAPERTARGET->m_vSize.y);

if (TILE) {
cairo_pattern_t* pattern = cairo_pattern_create_for_surface(PWALLPAPERTARGET->m_pCairoSurface->cairo());
Expand All @@ -579,7 +578,7 @@ void CHyprpaper::renderWallpaperForMonitor(SMonitor* pMonitor) {

cairo_paint(PCAIRO);

if (**PRENDERSPLASH && getenv("HYPRLAND_INSTANCE_SIGNATURE")) {
if (*PRENDERSPLASH && getenv("HYPRLAND_INSTANCE_SIGNATURE")) {
auto SPLASH = execAndGet("hyprctl splash");
SPLASH.pop_back();

Expand All @@ -590,20 +589,20 @@ void CHyprpaper::renderWallpaperForMonitor(SMonitor* pMonitor) {
const auto FONTSIZE = (int)(DIMENSIONS.y / 76.0 / scale);
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,
((**PSPLASHCOLOR >> 24) & 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);

cairo_text_extents_t 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,
((DIMENSIONS.y * (100 - **PSPLASHOFFSET)) / 100 - textExtents.height * scale) / 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);

cairo_show_text(PCAIRO, SPLASH.c_str());

Expand All @@ -623,8 +622,8 @@ void CHyprpaper::renderWallpaperForMonitor(SMonitor* pMonitor) {
pMonitor->pCurrentLayerSurface->pSurface->sendSetOpaqueRegion(opaqueRegion.get());

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)),
pMonitor->pCurrentLayerSurface);
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)),
(uintptr_t)pMonitor->pCurrentLayerSurface);
pMonitor->pCurrentLayerSurface->pViewport->sendSetDestination(static_cast<int>(std::round(pMonitor->size.x)), static_cast<int>(std::round(pMonitor->size.y)));
}
pMonitor->pCurrentLayerSurface->pSurface->sendCommit();
Expand Down
2 changes: 1 addition & 1 deletion src/Hyprpaper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

struct SWallpaperRenderData {
bool contain = false;
bool tile = false;
bool tile = false;
};

class CHyprpaper {
Expand Down
49 changes: 0 additions & 49 deletions src/debug/Log.cpp

This file was deleted.

56 changes: 48 additions & 8 deletions src/debug/Log.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,57 @@
#pragma once
#include <format>
#include <iostream>
#include <string>

#define LOGMESSAGESIZE 1024

enum LogLevel {
NONE = -1,
LOG = 0,
enum eLogLevel {
TRACE = 0,
INFO,
LOG,
WARN,
ERR,
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 {
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;
}
};
16 changes: 6 additions & 10 deletions src/helpers/MiscFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "../debug/Log.hpp"
#include <memory>

#include <hyprutils/os/Process.hpp>
using namespace Hyprutils::OS;

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;
}
Expand All @@ -12,15 +15,8 @@ bool vectorDeltaLessThan(const Vector2D& a, const Vector2D& b, const Vector2D& d
}

std::string execAndGet(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
const std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
Debug::log(ERR, "execAndGet: failed in pipe");
CProcess proc("/bin/bash", {cmd});
if (!proc.runSync())
return "";
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
return proc.stdOut();
}
Loading

0 comments on commit 85e850b

Please sign in to comment.