File: hyprcursor.hpp

package info (click to toggle)
libhyprcursor 0.1.13-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 284 kB
  • sloc: cpp: 1,546; ansic: 130; makefile: 16
file content (200 lines) | stat: -rw-r--r-- 6,118 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#pragma once

#include <vector>
#include <cstdlib>
#include <string>

#include "shared.h"

class CHyprcursorImplementation;

namespace Hyprcursor {

    /*!
        Simple struct for styles
    */
    struct SCursorStyleInfo {
        /*!
            Shape size.

            0 means "any" or "unspecified".
        */
        unsigned int size = 0;
    };

    /*!
        struct for cursor shape data
    */
    struct SCursorShapeData {
        std::vector<SCursorImageData> images;
    };

    /*!
        C++ structs for hyprcursor_cursor_raw_shape_image and hyprcursor_cursor_raw_shape_data
    */
    struct SCursorRawShapeImage {
        std::vector<unsigned char> data;
        int                        size  = 0;
        int                        delay = 200;
    };

    struct SCursorRawShapeData {
        std::vector<SCursorRawShapeImage> images;
        float                             hotspotX    = 0;
        float                             hotspotY    = 0;
        std::string                       overridenBy = "";
        eHyprcursorResizeAlgo             resizeAlgo  = HC_RESIZE_NONE;
        eHyprcursorDataType               type        = HC_DATA_PNG;
    };

    /*!
        struct for cursor manager options
    */
    struct SManagerOptions {
        explicit SManagerOptions();

        /*!
            The function used for logging by the cursor manager
        */
        PHYPRCURSORLOGFUNC logFn;
        /*!
            Allow fallback to env and first theme found
        */
        bool allowDefaultFallback;
    };

    /*!
        Basic Hyprcursor manager.

        Has to be created for either a specified theme, or
        nullptr if you want to use a default from the env.

        If no env is set, picks the first found.

        If none found, bool valid() will be false.

        If loading fails, bool valid() will be false.

        If theme has no valid cursor shapes, bool valid() will be false.
    */
    class CHyprcursorManager {
      public:
        CHyprcursorManager(const char* themeName);
        /*!
            \since 0.1.6
        */
        CHyprcursorManager(const char* themeName, PHYPRCURSORLOGFUNC fn);
        CHyprcursorManager(const char* themeName, SManagerOptions options);
        ~CHyprcursorManager();

        /*!
            Returns true if the theme was successfully loaded,
            i.e. everything is A-OK and nothing should fail.
        */
        bool valid();

        /*!
            Loads this theme at a given style, synchronously.

            Returns whether it succeeded.
        */
        bool loadThemeStyle(const SCursorStyleInfo& info);

        /*!
            Returns the shape data struct for a given
            style.

            Once done with a style, call cursorSurfaceDone()

            The surfaces references stay valid until cursorSurfaceStyleDone() is called on the owning style.
        */
        SCursorShapeData getShape(const char* shape, const SCursorStyleInfo& info) {
            int                size   = 0;
            SCursorImageData** images = getShapesC(size, shape, info);

            SCursorShapeData   data;

            for (int i = 0; i < size; ++i) {
                SCursorImageData image;
                image.delay    = images[i]->delay;
                image.size     = images[i]->size;
                image.surface  = images[i]->surface;
                image.hotspotX = images[i]->hotspotX;
                image.hotspotY = images[i]->hotspotY;
                data.images.push_back(image);

                free(images[i]);
            }

            free(images);

            return data;
        }

        /*!
            \since 0.1.6

            Returns the raw image data of a cursor shape, not rendered at all, alongside the metadata.
        */
        SCursorRawShapeData getRawShapeData(const char* shape_) {
            auto CDATA = getRawShapeDataC(shape_);

            if (CDATA->overridenBy) {
                SCursorRawShapeData d{.overridenBy = CDATA->overridenBy};
                free(CDATA->overridenBy);
                delete CDATA;
                return d;
            }

            SCursorRawShapeData data{.hotspotX = CDATA->hotspotX, .hotspotY = CDATA->hotspotY, .overridenBy = "", .resizeAlgo = CDATA->resizeAlgo, .type = CDATA->type};

            for (size_t i = 0; i < CDATA->len; ++i) {
                SCursorRawShapeImageC* cimage = &CDATA->images[i];
                SCursorRawShapeImage&  img    = data.images.emplace_back();
                img.size                      = cimage->size;
                img.delay                     = cimage->delay;
                img.data                      = std::vector<unsigned char>{(unsigned char*)cimage->data, (unsigned char*)cimage->data + (std::size_t)cimage->len};
            }

            delete[] CDATA->images;
            delete CDATA;

            return data;
        }

        /*!
            Prefer getShape, this is for C compat.
        */
        SCursorImageData** getShapesC(int& outSize, const char* shape_, const SCursorStyleInfo& info);

        /*!
            Prefer getShapeData, this is for C compat.
        */
        SCursorRawShapeDataC* getRawShapeDataC(const char* shape_);

        /*!
            Marks a certain style as done, allowing it to be potentially freed
        */
        void cursorSurfaceStyleDone(const SCursorStyleInfo&);

        /*!
            \since 0.1.6

            Registers a logging function to this manager.
            PHYPRCURSORLOGFUNC's msg is owned by the caller and will be freed afterwards.
            fn can be null to unregister a logger.
        */
        void registerLoggingFunction(PHYPRCURSORLOGFUNC fn);

      private:
        void                       init(const char* themeName_);

        CHyprcursorImplementation* impl                 = nullptr;
        bool                       finalizedAndValid    = false;
        bool                       allowDefaultFallback = true;
        PHYPRCURSORLOGFUNC         logFn                = nullptr;

        friend class CHyprcursorImplementation;
    };

}