Skip to content

Commit

Permalink
fix cursor gsettings on session change
Browse files Browse the repository at this point in the history
  • Loading branch information
ikalco committed Aug 12, 2024
1 parent 118d4e1 commit 6c30922
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 60 deletions.
1 change: 1 addition & 0 deletions src/Compositor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ void CCompositor::initAllSignals() {
}

g_pConfigManager->m_bWantsMonitorReload = true;
g_pCursorManager->syncGsettings();
} else {
Debug::log(LOG, "Session got deactivated!");

Expand Down
66 changes: 65 additions & 1 deletion src/managers/CursorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "../config/ConfigValue.hpp"
#include "PointerManager.hpp"
#include "../xwayland/XWayland.hpp"
#include <gio/gio.h>
#include <gio/gsettingsschema.h>

static int cursorAnimTimer(SP<CEventLoopTimer> self, void* data) {
const auto cursorMgr = reinterpret_cast<CCursorManager*>(data);
Expand Down Expand Up @@ -337,4 +339,66 @@ bool CCursorManager::changeTheme(const std::string& name, const int size) {
updateTheme();

return true;
}
}

void CCursorManager::syncGsettings() {
syncGsettings(m_szTheme.empty() ? m_pXcursor->themeName : m_szTheme, m_iSize);
}

void CCursorManager::syncGsettings(const std::string& name, int size) {
static auto SYNCGSETTINGS = CConfigValue<Hyprlang::INT>("cursor:sync_gsettings_theme");
if (!*SYNCGSETTINGS)
return;

auto checkParamExists = [](std::string const& paramName, std::string const& category) {
auto* gSettingsSchemaSource = g_settings_schema_source_get_default();

if (!gSettingsSchemaSource) {
Debug::log(WARN, "GSettings default schema source does not exist, cant sync GSettings");
return false;
}

auto* gSettingsSchema = g_settings_schema_source_lookup(gSettingsSchemaSource, category.c_str(), true);
bool hasParam = false;

if (gSettingsSchema != NULL) {
hasParam = gSettingsSchema && g_settings_schema_has_key(gSettingsSchema, paramName.c_str());
g_settings_schema_unref(gSettingsSchema);
}

return hasParam;
};

using SettingValue = std::variant<std::string, int>;
auto setValue = [&checkParamExists](std::string const& paramName, const SettingValue& paramValue, std::string const& category) {
if (!checkParamExists(paramName, category)) {
Debug::log(WARN, "GSettings parameter doesnt exist {} in {}", paramName, category);
return;
}

auto* gsettings = g_settings_new(category.c_str());

if (!gsettings) {
Debug::log(WARN, "GSettings failed to allocate new settings with category {}", category);
return;
}

std::visit(
[&](auto&& value) {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, std::string>)
g_settings_set_string(gsettings, paramName.c_str(), value.c_str());
else if constexpr (std::is_same_v<T, int>)
g_settings_set_int(gsettings, paramName.c_str(), value);
},
paramValue);

g_settings_sync();
g_object_unref(gsettings);
};

Debug::log(LOG, "syncing gsettings {} {}", name, size);

setValue("cursor-theme", name, "org.gnome.desktop.interface");
setValue("cursor-size", size, "org.gnome.desktop.interface");
}
4 changes: 3 additions & 1 deletion src/managers/CursorManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class CCursorManager {
void updateTheme();
SCursorImageData dataFor(const std::string& name); // for xwayland
void setXWaylandCursor();
void syncGsettings();
static void syncGsettings(const std::string& name, int size);

void tickAnimatedCursor();

Expand All @@ -75,4 +77,4 @@ class CCursorManager {
Hyprcursor::SCursorShapeData m_sCurrentCursorShapeData;
};

inline std::unique_ptr<CCursorManager> g_pCursorManager;
inline std::unique_ptr<CCursorManager> g_pCursorManager;
57 changes: 2 additions & 55 deletions src/managers/XCursorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <gio/gsettingsschema.h>
#include "config/ConfigValue.hpp"
#include "helpers/CursorShapes.hpp"
#include "../managers/CursorManager.hpp"
#include "debug/Log.hpp"
#include "XCursorManager.hpp"

Expand Down Expand Up @@ -150,9 +151,7 @@ void CXCursorManager::loadTheme(std::string const& name, int size) {
cursors.emplace_back(cursor);
}

static auto SYNCGSETTINGS = CConfigValue<Hyprlang::INT>("cursor:sync_gsettings_theme");
if (*SYNCGSETTINGS)
syncGsettings();
CCursorManager::syncGsettings(themeName, lastLoadSize);
}

SP<SXCursors> CXCursorManager::getShape(std::string const& shape, int size) {
Expand Down Expand Up @@ -549,55 +548,3 @@ std::vector<SP<SXCursors>> CXCursorManager::loadAllFromDir(std::string const& pa

return newCursors;
}

void CXCursorManager::syncGsettings() {
auto checkParamExists = [](std::string const& paramName, std::string const& category) {
auto* gSettingsSchemaSource = g_settings_schema_source_get_default();

if (!gSettingsSchemaSource) {
Debug::log(WARN, "GSettings default schema source does not exist, cant sync GSettings");
return false;
}

auto* gSettingsSchema = g_settings_schema_source_lookup(gSettingsSchemaSource, category.c_str(), true);
bool hasParam = false;

if (gSettingsSchema != NULL) {
hasParam = gSettingsSchema && g_settings_schema_has_key(gSettingsSchema, paramName.c_str());
g_settings_schema_unref(gSettingsSchema);
}

return hasParam;
};

using SettingValue = std::variant<std::string, int>;
auto setValue = [&checkParamExists](std::string const& paramName, const SettingValue& paramValue, std::string const& category) {
if (!checkParamExists(paramName, category)) {
Debug::log(WARN, "GSettings parameter doesnt exist {} in {}", paramName, category);
return;
}

auto* gsettings = g_settings_new(category.c_str());

if (!gsettings) {
Debug::log(WARN, "GSettings failed to allocate new settings with category {}", category);
return;
}

std::visit(
[&](auto&& value) {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, std::string>)
g_settings_set_string(gsettings, paramName.c_str(), value.c_str());
else if constexpr (std::is_same_v<T, int>)
g_settings_set_int(gsettings, paramName.c_str(), value);
},
paramValue);

g_settings_sync();
g_object_unref(gsettings);
};

setValue("cursor-theme", themeName, "org.gnome.desktop.interface");
setValue("cursor-size", lastLoadSize, "org.gnome.desktop.interface");
}
6 changes: 3 additions & 3 deletions src/managers/XCursorManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ class CXCursorManager {
void loadTheme(const std::string& name, int size);
SP<SXCursors> getShape(std::string const& shape, int size);

std::string themeName = "";

private:
SP<SXCursors> createCursor(std::string const& shape, XcursorImages* xImages);
std::unordered_set<std::string> themePaths(std::string const& theme);
std::string getLegacyShapeName(std::string const& shape);
std::vector<SP<SXCursors>> loadStandardCursors(std::string const& name, int size);
std::vector<SP<SXCursors>> loadAllFromDir(std::string const& path, int size);
void syncGsettings();

int lastLoadSize = 0;
std::string themeName = "";
SP<SXCursors> defaultCursor;
SP<SXCursors> hyprCursor;
std::vector<SP<SXCursors>> cursors;
};
};

0 comments on commit 6c30922

Please sign in to comment.