1 /****************************************************************************
2 Copyright © 2020 Adrien Faveraux <ad1rie3@hotmail.fr>
3 
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) version 3, or any
8 later version accepted by the membership of KDE e.V. (or its
9 successor approved by the membership of KDE e.V.), which shall
10 act as a proxy defined in Section 6 of version 3 of the license.
11 
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library.  If not, see <http://www.gnu.org/licenses/>.
19 ****************************************************************************/
20 #pragma once
21 #include "text_input_v2.h"
22 
23 #include <QPointer>
24 #include <QRect>
25 #include <QVector>
26 #include <vector>
27 
28 #include <wayland-text-input-unstable-v2-server-protocol.h>
29 
30 namespace Wrapland::Server
31 {
32 
33 constexpr uint32_t TextInputManagerV2Version = 1;
34 using TextInputManagerV2Global = Wayland::Global<TextInputManagerV2, TextInputManagerV2Version>;
35 using TextInputManagerV2Bind = Wayland::Bind<TextInputManagerV2Global>;
36 
37 class TextInputManagerV2::Private : public TextInputManagerV2Global
38 {
39 public:
40     Private(Display* display, TextInputManagerV2* q);
41 
42 private:
43     static void
44     getTextInputCallback(TextInputManagerV2Bind* bind, uint32_t id, wl_resource* wlSeat);
45 
46     static const struct zwp_text_input_manager_v2_interface s_interface;
47 };
48 
49 class TextInputV2::Private : public Wayland::Resource<TextInputV2>
50 {
51 public:
52     Private(Client* client, uint32_t version, uint32_t id, TextInputV2* q);
53 
54     void sendEnter(Surface* surface, quint32 serial);
55     void sendLeave(quint32 serial, Surface* surface);
56     void preEdit(const QByteArray& text, const QByteArray& commit);
57     void commit(const QByteArray& text);
58     void deleteSurroundingText(quint32 beforeLength, quint32 afterLength);
59     void setTextDirection(Qt::LayoutDirection direction);
60     void setPreEditCursor(qint32 index);
61     void setCursorPosition(qint32 index, qint32 anchor);
62     void keysymPressed(quint32 keysym, Qt::KeyboardModifiers modifiers);
63     void keysymReleased(quint32 keysym, Qt::KeyboardModifiers modifiers);
64 
65     void sendInputPanelState();
66     void sendLanguage();
67 
68     QByteArray preferredLanguage;
69     QRect cursorRectangle;
70     TextInputV2::ContentHints contentHints = TextInputV2::ContentHint::None;
71     TextInputV2::ContentPurpose contentPurpose = TextInputV2::ContentPurpose::Normal;
72     Seat* seat = nullptr;
73     QPointer<Surface> surface;
74     bool enabled = false;
75     QByteArray surroundingText;
76     qint32 surroundingTextCursorPosition = 0;
77     qint32 surroundingTextSelectionAnchor = 0;
78     bool inputPanelVisible = false;
79     QRect overlappedSurfaceArea;
80     QByteArray language;
81 
82 private:
83     static const struct zwp_text_input_v2_interface s_interface;
84 
85     static void enableCallback(wl_client* wlClient, wl_resource* wlResource, wl_resource* surface);
86     static void disableCallback(wl_client* wlClient, wl_resource* wlResource, wl_resource* surface);
87     static void updateStateCallback(wl_client* wlClient,
88                                     wl_resource* wlResource,
89                                     uint32_t serial,
90                                     uint32_t reason);
91     static void showInputPanelCallback(wl_client* wlClient, wl_resource* wlResource);
92     static void hideInputPanelCallback(wl_client* wlClient, wl_resource* wlResource);
93     static void setSurroundingTextCallback(wl_client* wlClient,
94                                            wl_resource* wlResource,
95                                            const char* text,
96                                            int32_t cursor,
97                                            int32_t anchor);
98     static void setContentTypeCallback(wl_client* wlClient,
99                                        wl_resource* wlResource,
100                                        uint32_t hint,
101                                        uint32_t purpose);
102     static void setCursorRectangleCallback(wl_client* wlClient,
103                                            wl_resource* wlResource,
104                                            int32_t x,
105                                            int32_t y,
106                                            int32_t width,
107                                            int32_t height);
108     static void setPreferredLanguageCallback(wl_client* wlClient,
109                                              wl_resource* wlResource,
110                                              const char* language);
111 
112     void enable(Surface* s);
113     void disable();
114 };
115 
116 }
117