aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/protocols/TextInputV3.hpp
blob: ba8b75e6e26f72291f98da1077f4a3cbc4dacb9f (plain)
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
#pragma once

#include <memory>
#include <vector>
#include <cstdint>
#include <string>
#include "WaylandProtocol.hpp"
#include "text-input-unstable-v3.hpp"
#include "../helpers/signal/Signal.hpp"
#include "../helpers/math/Math.hpp"

class CWLSurfaceResource;

class CTextInputV3 {
  public:
    CTextInputV3(SP<CZwpTextInputV3> resource_);
    ~CTextInputV3();

    void       enter(SP<CWLSurfaceResource> surf);
    void       leave(SP<CWLSurfaceResource> surf);
    void       preeditString(const std::string& text, int32_t cursorBegin, int32_t cursorEnd);
    void       commitString(const std::string& text);
    void       deleteSurroundingText(uint32_t beforeLength, uint32_t afterLength);
    void       sendDone();

    bool       good();

    wl_client* client();

    struct {
        CSignal onCommit;
        CSignal enable;
        CSignal disable;
        CSignal reset;
        CSignal destroy;
    } events;

    struct SState {
        struct {
            bool        updated = false;
            std::string text    = "";
            uint32_t    cursor  = 0;
            uint32_t    anchor  = 0;
        } surrounding;

        struct {
            bool                         updated = false;
            zwpTextInputV3ContentHint    hint    = ZWP_TEXT_INPUT_V3_CONTENT_HINT_NONE;
            zwpTextInputV3ContentPurpose purpose = ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL;
        } contentType;

        struct {
            bool updated = false;
            CBox cursorBox;
        } box;

        struct {
            bool isEnablePending  = false;
            bool isDisablePending = false;
            bool value            = false;
        } enabled;

        zwpTextInputV3ChangeCause cause = ZWP_TEXT_INPUT_V3_CHANGE_CAUSE_INPUT_METHOD;

        void                      reset();
    };
    SState pending, current;

  private:
    SP<CZwpTextInputV3> resource;

    int                 serial = 0;
};

class CTextInputV3Protocol : public IWaylandProtocol {
  public:
    CTextInputV3Protocol(const wl_interface* iface, const int& ver, const std::string& name);

    virtual void bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id);

    struct {
        CSignal newTextInput; // WP<CTextInputV3>
    } events;

  private:
    void onManagerResourceDestroy(wl_resource* res);
    void destroyTextInput(CTextInputV3* input);
    void onGetTextInput(CZwpTextInputManagerV3* pMgr, uint32_t id, wl_resource* seat);

    //
    std::vector<UP<CZwpTextInputManagerV3>> m_vManagers;
    std::vector<SP<CTextInputV3>>           m_vTextInputs;

    friend class CTextInputV3;
};

namespace PROTO {
    inline UP<CTextInputV3Protocol> textInputV3;
};