xcursor stuff

This commit is contained in:
Vaxry 2024-07-05 23:22:20 +02:00
parent 6c1d88dc73
commit 5008147870
3 changed files with 42 additions and 48 deletions

View file

@ -4,6 +4,7 @@
#include "PointerManager.hpp" #include "PointerManager.hpp"
#include "../xwayland/XWayland.hpp" #include "../xwayland/XWayland.hpp"
#include <cstring> #include <cstring>
#include "../helpers/CursorShapes.hpp"
extern "C" { extern "C" {
#include <X11/Xcursor/Xcursor.h> #include <X11/Xcursor/Xcursor.h>
@ -139,9 +140,14 @@ void CCursorManager::setXCursor(const std::string& name) {
return; return;
} }
m_vCursorBuffers.emplace_back(makeShared<CCursorBuffer>(xcursor.defaultCursor->pixels.data(), xcursor.defaultCursor->size, xcursor.defaultCursor->hotspot)); auto& icon = xcursor.defaultCursor;
// try to get an icon we know if we have one
if (xcursor.cursors.contains(name))
icon = xcursor.cursors.at(name);
g_pPointerManager->setCursorBuffer(getCursorBuffer(), xcursor.defaultCursor->hotspot / scale, scale); m_vCursorBuffers.emplace_back(makeShared<CCursorBuffer>(icon->pixels.data(), icon->size, icon->hotspot));
g_pPointerManager->setCursorBuffer(getCursorBuffer(), icon->hotspot / scale, scale);
if (m_vCursorBuffers.size() > 1) if (m_vCursorBuffers.size() > 1)
dropBufferRef(m_vCursorBuffers.at(0).get()); dropBufferRef(m_vCursorBuffers.at(0).get());
@ -300,6 +306,10 @@ bool CCursorManager::changeTheme(const std::string& name, const int size) {
} }
void CCursorManager::SXCursorManager::loadTheme(const std::string& name, int size) { void CCursorManager::SXCursorManager::loadTheme(const std::string& name, int size) {
if (lastLoadSize == size && themeName == name)
return;
lastLoadSize = size;
themeLoaded = false; themeLoaded = false;
themeName = name.empty() ? "default" : name; themeName = name.empty() ? "default" : name;
@ -323,4 +333,25 @@ void CCursorManager::SXCursorManager::loadTheme(const std::string& name, int siz
std::memcpy(defaultCursor->pixels.data(), img->pixels, img->width * img->height); std::memcpy(defaultCursor->pixels.data(), img->pixels, img->width * img->height);
themeLoaded = true; themeLoaded = true;
// gather as many shapes as we can find.
cursors.clear();
for (auto& shape : CURSOR_SHAPE_NAMES) {
auto xImage = XcursorShapeLoadImage(1, shape, size);
if (!xImage) {
Debug::log(LOG, "XCursor failed to find a shape with name {}, skipping", shape);
continue;
}
auto xcursor = makeShared<SXCursor>();
xcursor->size = {(int)xImage->width, (int)xImage->height};
xcursor->hotspot = {(int)xImage->xhot, (int)xImage->yhot};
xcursor->pixels.resize(xImage->width * xImage->height);
std::memcpy(xcursor->pixels.data(), xImage->pixels, xImage->width * xImage->height);
cursors.emplace(std::string{shape}, xcursor);
}
} }

View file

@ -83,9 +83,12 @@ class CCursorManager {
struct SXCursorManager { struct SXCursorManager {
void loadTheme(const std::string& name, int size); void loadTheme(const std::string& name, int size);
int lastLoadSize = 0;
bool themeLoaded = false; bool themeLoaded = false;
std::string themeName = ""; std::string themeName = "";
SP<SXCursor> defaultCursor; SP<SXCursor> defaultCursor;
std::unordered_map<std::string, SP<SXCursor>> cursors;
} xcursor; } xcursor;
}; };

View file

@ -3,46 +3,6 @@
#define LOGM PROTO::cursorShape->protoLog #define LOGM PROTO::cursorShape->protoLog
// clang-format off
constexpr const char* SHAPE_NAMES[] = {
"invalid",
"default",
"context-menu",
"help",
"pointer",
"progress",
"wait",
"cell",
"crosshair",
"text",
"vertical-text",
"alias",
"copy",
"move",
"no-drop",
"not-allowed",
"grab",
"grabbing",
"e-resize",
"n-resize",
"ne-resize",
"nw-resize",
"s-resize",
"se-resize",
"sw-resize",
"w-resize",
"ew-resize",
"ns-resize",
"nesw-resize",
"nwse-resize",
"col-resize",
"row-resize",
"all-scroll",
"zoom-in",
"zoom-out",
};
// clang-format on
CCursorShapeProtocol::CCursorShapeProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) { CCursorShapeProtocol::CCursorShapeProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) {
; ;
} }