File indexing completed on 2024-06-02 05:36:02

0001 /*
0002     SPDX-FileCopyrightText: 2016 Martin Gräßlin <mgraesslin@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 #ifndef KWAYLAND_CLIENT_TEXTINPUT_H
0007 #define KWAYLAND_CLIENT_TEXTINPUT_H
0008 
0009 #include <QObject>
0010 
0011 #include "KWayland/Client/kwaylandclient_export.h"
0012 
0013 struct wl_text_input;
0014 struct wl_text_input_manager;
0015 struct zwp_text_input_manager_v2;
0016 
0017 namespace KWayland
0018 {
0019 namespace Client
0020 {
0021 class EventQueue;
0022 class Surface;
0023 class Seat;
0024 
0025 /**
0026  * @brief TextInput represents a Wayland interface for text input.
0027  *
0028  * The TextInput allows to have text composed by the Compositor and be sent to
0029  * the client.
0030  *
0031  * Depending on the interface the TextInputManager got created for this class
0032  * encapsulates one of the following interfaces:
0033  * @li wl_text_input
0034  * @li zwp_text_input_v2
0035  *
0036  * @since 5.23
0037  **/
0038 class KWAYLANDCLIENT_EXPORT TextInput : public QObject
0039 {
0040     Q_OBJECT
0041 public:
0042     ~TextInput() override;
0043     /**
0044      * @returns @c true if managing a resource.
0045      **/
0046     bool isValid() const;
0047 
0048     /**
0049      * @returns The Surface which has the text input focus on this TextInput.
0050      * @see entered
0051      * @see left
0052      **/
0053     Surface *enteredSurface() const;
0054 
0055     void setEventQueue(EventQueue *queue);
0056     EventQueue *eventQueue() const;
0057 
0058     /**
0059      * @returns whether the input panel (virtual keyboard) is currently visible on the screen
0060      * @see inputPanelStateChanged
0061      **/
0062     bool isInputPanelVisible() const;
0063 
0064     /**
0065      * Enable text input in a @p surface (usually when a text entry inside of it has focus).
0066      *
0067      * This can be called before or after a surface gets text (or keyboard) focus via the
0068      * enter event. Text input to a surface is only active when it has the current
0069      * text (or keyboard) focus and is enabled.
0070      * @see deactivate
0071      **/
0072     void enable(Surface *surface);
0073 
0074     /**
0075      * Disable text input in a @p surface (typically when there is no focus on any
0076      * text entry inside the surface).
0077      * @see enable
0078      **/
0079     void disable(Surface *surface);
0080 
0081     /**
0082      * Requests input panels (virtual keyboard) to show.
0083      * @see hideInputPanel
0084      **/
0085     void showInputPanel();
0086 
0087     /**
0088      * Requests input panels (virtual keyboard) to hide.
0089      * @see showInputPanel
0090      **/
0091     void hideInputPanel();
0092 
0093     /**
0094      * Should be called by an editor widget when the input state should be
0095      * reset, for example after the text was changed outside of the normal
0096      * input method flow.
0097      **/
0098     void reset();
0099 
0100     /**
0101      * Sets the plain surrounding text around the input position.
0102      *
0103      * @param text The text surrounding the cursor position
0104      * @param cursor Index in the text describing the cursor position
0105      * @param anchor Index of the selection anchor, if no selection same as cursor
0106      **/
0107     void setSurroundingText(const QString &text, quint32 cursor, quint32 anchor);
0108 
0109     /**
0110      * The possible states for a keyEvent.
0111      * @see keyEvent
0112      **/
0113     enum class KeyState {
0114         Pressed,
0115         Released,
0116     };
0117 
0118     /**
0119      * ContentHint allows to modify the behavior of the text input.
0120      **/
0121     enum class ContentHint : uint32_t {
0122         /**
0123          * no special behaviour
0124          */
0125         None = 0,
0126         /**
0127          * suggest word completions
0128          */
0129         AutoCompletion = 1 << 0,
0130         /**
0131          * suggest word corrections
0132          */
0133         AutoCorrection = 1 << 1,
0134         /**
0135          * switch to uppercase letters at the start of a sentence
0136          */
0137         AutoCapitalization = 1 << 2,
0138         /**
0139          * prefer lowercase letters
0140          */
0141         LowerCase = 1 << 3,
0142         /**
0143          * prefer uppercase letters
0144          */
0145         UpperCase = 1 << 4,
0146         /**
0147          * prefer casing for titles and headings (can be language dependent)
0148          */
0149         TitleCase = 1 << 5,
0150         /**
0151          * characters should be hidden
0152          */
0153         HiddenText = 1 << 6,
0154         /**
0155          * typed text should not be stored
0156          */
0157         SensitiveData = 1 << 7,
0158         /**
0159          * just latin characters should be entered
0160          */
0161         Latin = 1 << 8,
0162         /**
0163          * the text input is multi line
0164          */
0165         MultiLine = 1 << 9,
0166     };
0167     Q_DECLARE_FLAGS(ContentHints, ContentHint)
0168 
0169     /**
0170      * The ContentPurpose allows to specify the primary purpose of a text input.
0171      *
0172      * This allows an input method to show special purpose input panels with
0173      * extra characters or to disallow some characters.
0174      */
0175     enum class ContentPurpose : uint32_t {
0176         /**
0177          * default input, allowing all characters
0178          */
0179         Normal,
0180         /**
0181          * allow only alphabetic characters
0182          **/
0183         Alpha,
0184         /**
0185          * allow only digits
0186          */
0187         Digits,
0188         /**
0189          * input a number (including decimal separator and sign)
0190          */
0191         Number,
0192         /**
0193          * input a phone number
0194          */
0195         Phone,
0196         /**
0197          * input an URL
0198          */
0199         Url,
0200         /**
0201          * input an email address
0202          **/
0203         Email,
0204         /**
0205          * input a name of a person
0206          */
0207         Name,
0208         /**
0209          * input a password
0210          */
0211         Password,
0212         /**
0213          * input a date
0214          */
0215         Date,
0216         /**
0217          * input a time
0218          */
0219         Time,
0220         /**
0221          * input a date and time
0222          */
0223         DateTime,
0224         /**
0225          * input for a terminal
0226          */
0227         Terminal,
0228     };
0229     /**
0230      * Sets the content @p purpose and content @p hints.
0231      * While the @p purpose is the basic purpose of an input field, the @p hints flags allow
0232      * to modify some of the behavior.
0233      **/
0234     void setContentType(ContentHints hints, ContentPurpose purpose);
0235 
0236     /**
0237      * Sets the cursor outline @p rect in surface local coordinates.
0238      *
0239      * Allows the compositor to e.g. put a window with word suggestions
0240      * near the cursor.
0241      **/
0242     void setCursorRectangle(const QRect &rect);
0243 
0244     /**
0245      * Sets a specific @p language.
0246      *
0247      * This allows for example a virtual keyboard to show a language specific layout.
0248      * The @p language argument is a RFC-3066 format language tag.
0249      **/
0250     void setPreferredLanguage(const QString &language);
0251 
0252     /**
0253      * The text direction of input text.
0254      *
0255      * It is mainly needed for showing input cursor on correct side of the
0256      * editor when there is no input yet done and making sure neutral
0257      * direction text is laid out properly.
0258      * @see textDirectionChnaged
0259      **/
0260     Qt::LayoutDirection textDirection() const;
0261 
0262     /**
0263      * The language of the input text.
0264      *
0265      * As long as the server has not emitted the language, the code will be empty.
0266      *
0267      * @returns a RFC-3066 format language tag in utf-8.
0268      * @see languageChanged
0269      **/
0270     QByteArray language() const;
0271 
0272     /**
0273      * The cursor position inside the {@link composingText} (as byte offset) relative
0274      * to the start of the {@link composingText}.
0275      * If index is a negative number no cursor is shown.
0276      * @see composingText
0277      * @see composingTextChanged
0278      **/
0279     qint32 composingTextCursorPosition() const;
0280 
0281     /**
0282      * The currently being composed text around the {@link composingTextCursorPosition}.
0283      * @see composingTextCursorPosition
0284      * @see composingTextChanged
0285      **/
0286     QByteArray composingText() const;
0287 
0288     /**
0289      * The fallback text can be used to replace the {@link composingText} in some cases
0290      * (for example when losing focus).
0291      *
0292      * @see composingText
0293      * @see composingTextChanged
0294      **/
0295     QByteArray composingFallbackText() const;
0296 
0297     /**
0298      * The commit text to be inserted.
0299      *
0300      * The commit text might be empty if only text should be deleted or the cursor be moved.
0301      * @see cursorPosition
0302      * @see anchorPosition
0303      * @see deleteSurroundingText
0304      * @see committed
0305      **/
0306     QByteArray commitText() const;
0307 
0308     /**
0309      * The cursor position in bytes at which the {@link commitText} should be inserted.
0310      * @see committed
0311      **/
0312     qint32 cursorPosition() const;
0313 
0314     /**
0315      * The text between anchorPosition and {@link cursorPosition} should be selected.
0316      * @see cursorPosition
0317      * @see committed
0318      **/
0319     qint32 anchorPosition() const;
0320 
0321     /**
0322      * Holds the length before and after the cursor position to be deleted.
0323      **/
0324     struct DeleteSurroundingText {
0325         quint32 beforeLength;
0326         quint32 afterLength;
0327     };
0328     /**
0329      * @returns The length in bytes which should be deleted around the cursor position
0330      * @see committed
0331      **/
0332     DeleteSurroundingText deleteSurroundingText() const;
0333 
0334 Q_SIGNALS:
0335     /**
0336      * Emitted whenever a Surface is focused on this TextInput.
0337      * @see enteredSurface
0338      * @see left
0339      **/
0340     void entered();
0341     /**
0342      * Emitted whenever a Surface loses the focus on this TextInput.
0343      * @see enteredSurface
0344      * @see entered
0345      **/
0346     void left();
0347     /**
0348      * Emitted whenever the state of the input panel (virtual keyboard changes).
0349      * @see isInputPanelVisible
0350      **/
0351     void inputPanelStateChanged();
0352     /**
0353      * Emitted whenever the text direction changes.
0354      * @see textDirection
0355      **/
0356     void textDirectionChanged();
0357     /**
0358      * Emitted whenever the language changes.
0359      * @see language
0360      **/
0361     void languageChanged();
0362 
0363     /**
0364      * Emitted when a key event was sent.
0365      * Key events are not used for normal text input operations, but for specific key symbols
0366      * which are not composable through text.
0367      *
0368      * @param xkbKeySym The XKB key symbol, not a key code
0369      * @param state Whether the event represents a press or release event
0370      * @param modifiers The hold modifiers on this event
0371      * @param time Timestamp of this event
0372      **/
0373     void keyEvent(quint32 xkbKeySym, KWayland::Client::TextInput::KeyState state, Qt::KeyboardModifiers modifiers, quint32 time);
0374 
0375     /**
0376      * Emitted whenever the composing text and related states changed.
0377      * @see composingText
0378      * @see composingTextCursorPosition
0379      * @see composingFallbackText
0380      **/
0381     void composingTextChanged();
0382 
0383     /**
0384      * Emitted when the currently composing text got committed.
0385      * The {@link commitText} should get inserted at the {@link cursorPosition} and
0386      * the text around {@link deleteSurroundingText} should be deleted.
0387      *
0388      * @see commitText
0389      * @see cursorPosition
0390      * @see anchorPosition
0391      * @see deleteSurroundingText
0392      **/
0393     void committed();
0394 
0395 protected:
0396     class Private;
0397     QScopedPointer<Private> d;
0398     explicit TextInput(Private *p, QObject *parent = nullptr);
0399 };
0400 
0401 /**
0402  * @brief Manager class for the TextInputManager interfaces.
0403  *
0404  * The TextInputManager supports multiple interfaces:
0405  * @li wl_text_input_manager
0406  * @li zwp_text_input_manager_v2
0407  *
0408  * Due to that it is different to other manager classes. It can only be created through
0409  * the corresponding factory method in Registry. A manual setup is not directly possible.
0410  *
0411  * The only task of a TextInputManager is to create TextInput for a given Seat.
0412  *
0413  * @since 5.23
0414  **/
0415 class KWAYLANDCLIENT_EXPORT TextInputManager : public QObject
0416 {
0417     Q_OBJECT
0418 public:
0419     ~TextInputManager() override;
0420 
0421     /**
0422      * Setup this TextInputManager to manage the @p textinputmanagerunstablev0.
0423      * When using Registry::createTextInputManager there is no need to call this
0424      * method.
0425      **/
0426     void setup(wl_text_input_manager *textinputmanagerunstablev0);
0427     /**
0428      * Setup this TextInputManager to manage the @p textinputmanagerunstablev0.
0429      * When using Registry::createTextInputManager there is no need to call this
0430      * method.
0431      **/
0432     void setup(zwp_text_input_manager_v2 *textinputmanagerunstablev2);
0433     /**
0434      * @returns @c true if managing a resource.
0435      **/
0436     bool isValid() const;
0437     /**
0438      * Releases the interface.
0439      * After the interface has been released the TextInputManager instance is no
0440      * longer valid and can be setup with another interface.
0441      **/
0442     void release();
0443     /**
0444      * Destroys the data held by this TextInputManager.
0445      * This method is supposed to be used when the connection to the Wayland
0446      * server goes away. If the connection is not valid anymore, it's not
0447      * possible to call release anymore as that calls into the Wayland
0448      * connection and the call would fail. This method cleans up the data, so
0449      * that the instance can be deleted or set up to a new  interface
0450      * once there is a new connection available.
0451      *
0452      * This method is automatically invoked when the Registry which created this
0453      * TextInput gets destroyed.
0454      *
0455      * @see release
0456      **/
0457     void destroy();
0458 
0459     /**
0460      * Sets the @p queue to use for creating objects with this TextInputManager.
0461      **/
0462     void setEventQueue(EventQueue *queue);
0463     /**
0464      * @returns The event queue to use for creating objects with this TextInputManager.
0465      **/
0466     EventQueue *eventQueue();
0467 
0468     /**
0469      * Creates a TextInput for the @p seat.
0470      *
0471      * @param seat The Seat to create the TextInput for
0472      * @param parent The parent to use for the TextInput
0473      **/
0474     TextInput *createTextInput(Seat *seat, QObject *parent = nullptr);
0475 
0476     /**
0477      * @returns @c null if not for a wl_text_input_manager
0478      **/
0479     operator wl_text_input_manager *();
0480     /**
0481      * @returns @c null if not for a wl_text_input_manager
0482      **/
0483     operator wl_text_input_manager *() const;
0484     /**
0485      * @returns @c null if not for a zwp_text_input_manager_v2
0486      **/
0487     operator zwp_text_input_manager_v2 *();
0488     /**
0489      * @returns @c null if not for a zwp_text_input_manager_v2
0490      **/
0491     operator zwp_text_input_manager_v2 *() const;
0492 
0493 Q_SIGNALS:
0494     /**
0495      * The corresponding global for this interface on the Registry got removed.
0496      *
0497      * This signal gets only emitted if the TextInputManager got created by
0498      * Registry::createTextInputManager
0499      **/
0500     void removed();
0501 
0502 protected:
0503     class Private;
0504     explicit TextInputManager(Private *p, QObject *parent = nullptr);
0505 
0506     QScopedPointer<Private> d;
0507 };
0508 
0509 Q_DECLARE_OPERATORS_FOR_FLAGS(TextInput::ContentHints)
0510 
0511 }
0512 }
0513 
0514 Q_DECLARE_METATYPE(KWayland::Client::TextInput::KeyState)
0515 Q_DECLARE_METATYPE(KWayland::Client::TextInput::ContentHint)
0516 Q_DECLARE_METATYPE(KWayland::Client::TextInput::ContentPurpose)
0517 Q_DECLARE_METATYPE(KWayland::Client::TextInput::ContentHints)
0518 
0519 #endif