added unload

This commit is contained in:
vaxerski 2022-07-17 14:17:54 +02:00
parent 59956ce32a
commit 8a7df4675a
8 changed files with 64 additions and 1 deletions

View file

@ -65,6 +65,46 @@ bool CHyprpaper::isPreloaded(const std::string& path) {
return false;
}
void CHyprpaper::unloadWallpaper(const std::string& path) {
bool found = false;
for (auto& [ewp, cls] : m_mWallpaperTargets) {
if (ewp == path) {
// found
found = true;
break;
}
}
if (!found) {
Debug::log(LOG, "Cannot unload a target that was not loaded!");
return;
}
// clean buffers
for (auto it = m_vBuffers.begin(); it != m_vBuffers.end(); it++) {
if (it->get()->pTarget->m_szPath != path)
continue;
const auto PRELOADPATH = it->get()->name;
Debug::log(LOG, "Unloading target %s, preload path %s", path.c_str(), PRELOADPATH.c_str());
std::filesystem::remove(PRELOADPATH);
destroyBuffer(it->get());
it = m_vBuffers.erase(it);
if (it == m_vBuffers.end())
break;
}
m_mWallpaperTargets.erase(path); // will free the cairo surface
}
void CHyprpaper::preloadAllWallpapersFromConfig() {
if (g_pConfigManager->m_dRequestedPreloads.empty())
return;
@ -320,6 +360,7 @@ void CHyprpaper::createBuffer(SPoolBuffer* pBuffer, int32_t w, int32_t h, uint32
pBuffer->surface = cairo_image_surface_create_for_data((unsigned char*)DATA, CAIRO_FORMAT_ARGB32, w, h, STRIDE);
pBuffer->cairo = cairo_create(pBuffer->surface);
pBuffer->pixelSize = Vector2D(w, h);
pBuffer->name = name;
}
void CHyprpaper::destroyBuffer(SPoolBuffer* pBuffer) {

View file

@ -44,6 +44,7 @@ public:
void recheckMonitor(SMonitor*);
void ensurePoolBuffersPresent();
SPoolBuffer* getPoolBuffer(SMonitor*, CWallpaperTarget*);
void unloadWallpaper(const std::string&);
std::mutex m_mtTickMutex;
private:

View file

@ -93,6 +93,8 @@ void CConfigManager::parseKeyword(const std::string& COMMAND, const std::string&
handleWallpaper(COMMAND, VALUE);
else if (COMMAND == "preload")
handlePreload(COMMAND, VALUE);
else if (COMMAND == "unload")
handleUnload(COMMAND, VALUE);
else
parseError = "unknown keyword " + COMMAND;
}
@ -140,3 +142,14 @@ void CConfigManager::handlePreload(const std::string& COMMAND, const std::string
m_dRequestedPreloads.emplace_back(WALLPAPER);
}
void CConfigManager::handleUnload(const std::string& COMMAND, const std::string& VALUE) {
auto WALLPAPER = VALUE;
if (WALLPAPER[0] == '~') {
static const char* const ENVHOME = getenv("HOME");
WALLPAPER = std::string(ENVHOME) + WALLPAPER.substr(1);
}
g_pHyprpaper->unloadWallpaper(WALLPAPER);
}

View file

@ -19,6 +19,7 @@ private:
void handleWallpaper(const std::string&, const std::string&);
void handlePreload(const std::string&, const std::string&);
void handleUnload(const std::string&, const std::string&);
friend class CIPCSocket;
};

View file

@ -10,6 +10,7 @@ struct SPoolBuffer {
cairo_t* cairo = nullptr;
void* data = nullptr;
size_t size = 0;
std::string name = "";
CWallpaperTarget* pTarget = nullptr;
Vector2D pixelSize;

View file

@ -99,7 +99,7 @@ void CIPCSocket::mainThreadParseRequest() {
Debug::log(LOG, "Received a request: %s", copy.c_str());
// parse
if (copy.find("wallpaper") == 0 || copy.find("preload") == 0) {
if (copy.find("wallpaper") == 0 || copy.find("preload") == 0 || copy.find("unload") == 0) {
g_pConfigManager->parseError = ""; // reset parse error
g_pConfigManager->parseKeyword(copy.substr(0, copy.find_first_of(' ')), copy.substr(copy.find_first_of(' ') + 1));

View file

@ -1,5 +1,9 @@
#include "WallpaperTarget.hpp"
CWallpaperTarget::~CWallpaperTarget() {
cairo_surface_destroy(m_pCairoSurface);
}
void CWallpaperTarget::create(const std::string& path) {
m_szPath = path;

View file

@ -5,6 +5,8 @@
class CWallpaperTarget {
public:
~CWallpaperTarget();
void create(const std::string& path);
void render();