File indexing completed on 2024-05-19 16:35:30

0001 /*
0002     SPDX-FileCopyrightText: 2022 Xuetian Weng <wengxt@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 #pragma once
0007 
0008 #include <QObject>
0009 #include <memory>
0010 
0011 #include "textinput.h"
0012 
0013 struct wl_resource;
0014 namespace KWaylandServer
0015 {
0016 class ClientConnection;
0017 class Display;
0018 class SeatInterface;
0019 class SurfaceInterface;
0020 class TextInputV1Interface;
0021 class TextInputV1InterfacePrivate;
0022 class TextInputManagerV1InterfacePrivate;
0023 
0024 /**
0025  * @brief Represent the Global for the interface.
0026  *
0027  * The class can represent different interfaces. Which concrete interface is represented
0028  * can be determined through {@link interfaceVersion}.
0029  *
0030  * To create a TextInputManagerV1Interface use {@link Display::createTextInputManager}
0031  */
0032 class KWIN_EXPORT TextInputManagerV1Interface : public QObject
0033 {
0034     Q_OBJECT
0035 public:
0036     explicit TextInputManagerV1Interface(Display *display, QObject *parent = nullptr);
0037     ~TextInputManagerV1Interface() override;
0038 
0039 private:
0040     std::unique_ptr<TextInputManagerV1InterfacePrivate> d;
0041 };
0042 
0043 /**
0044  * @brief Represents a generic Resource for a text input object.
0045  *
0046  * This class does not directly correspond to a Wayland resource, but is a generic contract
0047  * for any interface which implements a text input, e.g. the unstable wl_text_input interface.
0048  *
0049  * It does not expose the actual interface to cover up the fact that the interface is unstable
0050  * and might change. If one needs to know the actual used protocol, use the method {@link interfaceVersion}.
0051  *
0052  * A TextInputV1Interface gets created by the {@link TextInputManagerV1Interface}. The individual
0053  * instances are not exposed directly. The Display provides access to the currently active
0054  * TextInputV1Interface. This is evaluated automatically based on which SurfaceInterface has
0055  * keyboard focus.
0056  *
0057  * @see TextInputManagerV1Interface
0058  * @see SeatInterface
0059  */
0060 class KWIN_EXPORT TextInputV1Interface : public QObject
0061 {
0062     Q_OBJECT
0063 public:
0064     explicit TextInputV1Interface(SeatInterface *seat);
0065     ~TextInputV1Interface() override;
0066 
0067     /**
0068      * The preferred language as a RFC-3066 format language tag.
0069      *
0070      * This can be used by the server to show a language specific virtual keyboard layout.
0071      * @see preferredLanguageChanged
0072      */
0073     QString preferredLanguage() const;
0074 
0075     /**
0076      * @see cursorRectangleChanged
0077      */
0078     QRect cursorRectangle() const;
0079 
0080     /**
0081      * @see contentTypeChanged
0082      */
0083     TextInputContentPurpose contentPurpose() const;
0084 
0085     /**
0086      *@see contentTypeChanged
0087      */
0088     TextInputContentHints contentHints() const;
0089 
0090     /**
0091      * @returns The plain surrounding text around the input position.
0092      * @see surroundingTextChanged
0093      * @see surroundingTextCursorPosition
0094      * @see surroundingTextSelectionAnchor
0095      */
0096     QString surroundingText() const;
0097     /**
0098      * @returns The byte offset of current cursor position within the {@link surroundingText}
0099      * @see surroundingText
0100      * @see surroundingTextChanged
0101      */
0102     qint32 surroundingTextCursorPosition() const;
0103     /**
0104      * The byte offset of the selection anchor within the {@link surroundingText}.
0105      *
0106      * If there is no selected text this is the same as cursor.
0107      * @return The byte offset of the selection anchor
0108      * @see surroundingText
0109      * @see surroundingTextChanged
0110      */
0111     qint32 surroundingTextSelectionAnchor() const;
0112 
0113     /**
0114      * @return The surface the TextInputV1Interface is enabled on
0115      * @see isEnabled
0116      * @see enabledChanged
0117      */
0118     QPointer<SurfaceInterface> surface() const;
0119 
0120     /**
0121      * @return whether @p client supports text-input-v1
0122      */
0123     bool clientSupportsTextInput(ClientConnection *client) const;
0124 
0125     /**
0126      * @return Whether the TextInputV1Interface is currently enabled for a SurfaceInterface.
0127      * @see surface
0128      * @see enabledChanged
0129      */
0130     bool isEnabled() const;
0131 
0132     /**
0133      * Notify when a new composing @p text (pre-edit) should be set around the
0134      * current cursor position. Any previously set composing text should
0135      * be removed.
0136      *
0137      * The @p commitText can be used to replace the preedit text on reset
0138      * (for example on unfocus).
0139      *
0140      * @param text The new utf8-encoded pre-edit text
0141      * @param commitText Utf8-encoded text to replace preedit text on reset
0142      * @see commit
0143      * @see preEditCursor
0144      */
0145     void preEdit(const QString &text, const QString &commitText);
0146 
0147     /**
0148      * Notify when @p text should be inserted into the editor widget.
0149      * The text to commit could be either just a single character after a key press or the
0150      * result of some composing ({@link preEdit}). It could be also an empty text
0151      * when some text should be removed (see {@link deleteSurroundingText}) or when
0152      * the input cursor should be moved (see {@link cursorPosition}).
0153      *
0154      * Any previously set composing text should be removed.
0155      * @param text The utf8-encoded text to be inserted into the editor widget
0156      * @see preEdit
0157      * @see deleteSurroundingText
0158      */
0159     void commitString(const QString &text);
0160 
0161     /**
0162      * Sets the cursor position inside the composing text (as byte offset) relative to the
0163      * start of the composing text. When @p index is a negative number no cursor is shown.
0164      *
0165      * The Client applies the @p index together with {@link preEdit}.
0166      * @param index The cursor position relative to the start of the composing text
0167      * @see preEdit
0168      */
0169     void setPreEditCursor(qint32 index);
0170 
0171     /**
0172      * Sets the style for a range of text for the composing text (as byte offset).
0173      *
0174      * The Client applies the @p index together with {@link preEdit}.
0175      * @param index The position relative to the start of the composing text
0176      * @param length The length of the style to apply on
0177      * @param style style flag
0178      * @see preEdit
0179      */
0180     void preEditStyling(uint32_t index, uint32_t length, uint32_t style);
0181 
0182     /**
0183      * Notify when the text around the current cursor position should be deleted.
0184      *
0185      * The Client processes this event together with the commit string
0186      *
0187      * @param beforeLength length of text before current cursor position.
0188      * @param afterLength length of text after current cursor position.
0189      * @see commit
0190      */
0191     void deleteSurroundingText(quint32 beforeLength, quint32 afterLength);
0192 
0193     /**
0194      * Notify when the cursor @p index or @p anchor position should be modified.
0195      *
0196      * The Client applies this together with the commit string.
0197      */
0198     void setCursorPosition(qint32 index, qint32 anchor);
0199 
0200     /**
0201      * Sets the text @p direction of input text.
0202      */
0203     void setTextDirection(Qt::LayoutDirection direction);
0204 
0205     void keysymPressed(quint32 time, quint32 keysym, quint32 modifiers = 0);
0206     void keysymReleased(quint32 time, quint32 keysym, quint32 modifiers = 0);
0207 
0208     /**
0209      * Informs the client about changes in the visibility of the input panel (virtual keyboard).
0210      *
0211      * The @p overlappedSurfaceArea defines the area overlapped by the input panel (virtual keyboard)
0212      * on the SurfaceInterface having the text focus in surface local coordinates.
0213      *
0214      * @param visible Whether the input panel is currently visible
0215      * @param overlappedSurfaceArea The overlapping area in surface local coordinates
0216      */
0217     void setInputPanelState(bool visible);
0218 
0219     /**
0220      * Sets the language of the input text. The @p languageTag is a RFC-3066 format language tag.
0221      */
0222     void setLanguage(const QString &languageTag);
0223 
0224     /**
0225      * Sets the modifiers map to use when modifiers are included in a key event.
0226      */
0227     void setModifiersMap(const QByteArray &modifiersMap);
0228 
0229 Q_SIGNALS:
0230     /**
0231      * Requests input panels (virtual keyboard) to show.
0232      * @see requestHideInputPanel
0233      */
0234     void requestShowInputPanel();
0235     /**
0236      * Requests input panels (virtual keyboard) to hide.
0237      * @see requestShowInputPanel
0238      */
0239     void requestHideInputPanel();
0240     /**
0241      * Emitted whenever the preferred @p language changes.
0242      * @see preferredLanguage
0243      */
0244     void preferredLanguageChanged(const QString &language);
0245     /**
0246      * @see cursorRectangle
0247      */
0248     void cursorRectangleChanged(const QRect &rect);
0249     /**
0250      * Emitted when the {@link contentPurpose} and/or {@link contentHints} changes.
0251      * @see contentPurpose
0252      * @see contentHints
0253      */
0254     void contentTypeChanged();
0255     /**
0256      * Emitted when the {@link surroundingText}, {@link surroundingTextCursorPosition}
0257      * and/or {@link surroundingTextSelectionAnchor} changed.
0258      * @see surroundingText
0259      * @see surroundingTextCursorPosition
0260      * @see surroundingTextSelectionAnchor
0261      */
0262     void surroundingTextChanged();
0263     /**
0264      * Emitted whenever this TextInputV1Interface gets enabled or disabled for a SurfaceInterface.
0265      * @see isEnabled
0266      * @see surface
0267      */
0268     void enabledChanged();
0269     /**
0270      * Emitted whenever TextInputV1Interface should update the current state.
0271      */
0272     void stateUpdated(quint32 serial);
0273     /**
0274      * Emitted whenever TextInputV1Interface gets reset.
0275      */
0276     void reset();
0277     /**
0278      * Emitted whenever TextInputV1Interface request invoke action on preedit
0279      */
0280     void invokeAction(quint32 button, quint32 index);
0281 
0282 private:
0283     friend class TextInputManagerV1InterfacePrivate;
0284     friend class SeatInterface;
0285     friend class SeatInterfacePrivate;
0286     friend class TextInputV1InterfacePrivate;
0287 
0288     std::unique_ptr<TextInputV1InterfacePrivate> d;
0289 };
0290 
0291 }
0292 
0293 Q_DECLARE_METATYPE(KWaylandServer::TextInputV1Interface *)