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