File indexing completed on 2024-04-28 05:30:21

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2016 Martin Gräßlin <mgraesslin@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #pragma once
0010 
0011 #include "wayland/textinput_v2.h"
0012 
0013 #include <utility>
0014 #include <vector>
0015 
0016 #include <QObject>
0017 
0018 #include "effect/globals.h"
0019 #include <kwin_export.h>
0020 
0021 #include <QPointer>
0022 #include <QTimer>
0023 
0024 class QProcess;
0025 
0026 namespace KWin
0027 {
0028 
0029 class Window;
0030 class InputPanelV1Window;
0031 class InputMethodGrabV1;
0032 
0033 /**
0034  * This class implements the zwp_input_method_unstable_v1, which is currently used to provide
0035  * the Virtual Keyboard using supported input method client (maliit-keyboard e.g.)
0036  **/
0037 class KWIN_EXPORT InputMethod : public QObject
0038 {
0039     Q_OBJECT
0040 public:
0041     enum ForwardModifiersForce {
0042         NoForce = 0,
0043         Force = 1,
0044     };
0045 
0046     InputMethod();
0047     ~InputMethod() override;
0048 
0049     void init();
0050     void setEnabled(bool enable);
0051     bool isEnabled() const
0052     {
0053         return m_enabled;
0054     }
0055     bool isActive() const;
0056     void setActive(bool active);
0057     void hide();
0058     void show();
0059     bool isVisible() const;
0060     bool isAvailable() const;
0061 
0062     InputPanelV1Window *panel() const;
0063     void setPanel(InputPanelV1Window *panel);
0064     void setInputMethodCommand(const QString &path);
0065 
0066     InputMethodGrabV1 *keyboardGrab();
0067     bool shouldShowOnActive() const;
0068 
0069     void forwardModifiers(ForwardModifiersForce force);
0070     bool activeClientSupportsTextInput() const;
0071     void forceActivate();
0072 
0073 Q_SIGNALS:
0074     void panelChanged();
0075     void activeChanged(bool active);
0076     void enabledChanged(bool enabled);
0077     void visibleChanged();
0078     void availableChanged();
0079     void activeClientSupportsTextInputChanged();
0080 
0081 private Q_SLOTS:
0082     // textinput interface slots
0083     void handleFocusedSurfaceChanged();
0084     void surroundingTextChanged();
0085     void contentTypeChanged();
0086     void textInputInterfaceV1EnabledChanged();
0087     void textInputInterfaceV2EnabledChanged();
0088     void textInputInterfaceV3EnabledChanged();
0089     void stateCommitted(uint32_t serial);
0090     void textInputInterfaceV1StateUpdated(quint32 serial);
0091     void textInputInterfaceV1Reset();
0092     void invokeAction(quint32 button, quint32 index);
0093     void textInputInterfaceV2StateUpdated(quint32 serial, KWin::TextInputV2Interface::UpdateReason reason);
0094     void textInputInterfaceV3EnableRequested();
0095 
0096     // inputcontext slots
0097     void setPreeditString(uint32_t serial, const QString &text, const QString &commit);
0098     void setPreeditStyling(quint32 index, quint32 length, quint32 style);
0099     void setPreeditCursor(qint32 index);
0100     void key(quint32 serial, quint32 time, quint32 key, bool pressed);
0101     void modifiers(quint32 serial, quint32 mods_depressed, quint32 mods_latched, quint32 mods_locked, quint32 group);
0102 
0103 private:
0104     void updateInputPanelState();
0105     void adoptInputMethodContext();
0106     void commitString(qint32 serial, const QString &text);
0107     void keysymReceived(quint32 serial, quint32 time, quint32 sym, bool pressed, quint32 modifiers);
0108     void deleteSurroundingText(int32_t index, uint32_t length);
0109     void setCursorPosition(qint32 index, qint32 anchor);
0110     void setLanguage(uint32_t serial, const QString &language);
0111     void setTextDirection(uint32_t serial, Qt::LayoutDirection direction);
0112     void startInputMethod();
0113     void stopInputMethod();
0114     void setTrackedWindow(Window *trackedWindow);
0115     void installKeyboardGrab(InputMethodGrabV1 *keyboardGrab);
0116     void updateModifiersMap(const QByteArray &modifiers);
0117 
0118     bool touchEventTriggered() const;
0119     void resetPendingPreedit();
0120     void refreshActive();
0121 
0122     struct
0123     {
0124         QString text = QString();
0125         qint32 cursor = 0;
0126         std::vector<std::pair<quint32, quint32>> highlightRanges;
0127     } preedit;
0128 
0129     bool m_enabled = true;
0130     quint32 m_serial = 0;
0131     QPointer<InputPanelV1Window> m_panel;
0132     QPointer<Window> m_trackedWindow;
0133     QPointer<InputMethodGrabV1> m_keyboardGrab;
0134 
0135     QProcess *m_inputMethodProcess = nullptr;
0136     QTimer m_inputMethodCrashTimer;
0137     uint m_inputMethodCrashes = 0;
0138     QString m_inputMethodCommand;
0139 
0140     bool m_hasPendingModifiers = false;
0141     bool m_activeClientSupportsTextInput = false;
0142     bool m_shouldShowPanel = false;
0143 };
0144 
0145 }