| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2012~2017 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
* |
| 6 |
*/ |
| 7 |
#ifndef _DBUSADDONS_FCITXQTINPUTCONTEXTPROXY_P_H_ |
| 8 |
#define _DBUSADDONS_FCITXQTINPUTCONTEXTPROXY_P_H_ |
| 9 |
|
| 10 |
#include "fcitxqtinputcontextproxy.h" |
| 11 |
#include "fcitxqtinputcontextproxyimpl.h" |
| 12 |
#include "fcitxqtinputmethodproxy.h" |
| 13 |
#include "fcitxqtwatcher.h" |
| 14 |
#include <QDBusServiceWatcher> |
| 15 |
#include <cstddef> |
| 16 |
|
| 17 |
namespace fcitx { |
| 18 |
|
| 19 |
class FcitxQtInputContextProxyPrivate { |
| 20 |
public: |
| 21 |
FcitxQtInputContextProxyPrivate(FcitxQtWatcher *watcher, |
| 22 |
FcitxQtInputContextProxy *q) |
| 23 |
: q_ptr(q), fcitxWatcher_(watcher), watcher_(q) { |
| 24 |
registerFcitxQtDBusTypes(); |
| 25 |
QObject::connect(fcitxWatcher_, &FcitxQtWatcher::availabilityChanged, q, |
| 26 |
[this]() { availabilityChanged(); }); |
| 27 |
watcher_.setWatchMode(QDBusServiceWatcher::WatchForUnregistration); |
| 28 |
QObject::connect(&watcher_, &QDBusServiceWatcher::serviceUnregistered, |
| 29 |
q, [this]() { |
| 30 |
cleanUp(); |
| 31 |
availabilityChanged(); |
| 32 |
}); |
| 33 |
availabilityChanged(); |
| 34 |
} |
| 35 |
|
| 36 |
~FcitxQtInputContextProxyPrivate() { |
| 37 |
Q_Q(FcitxQtInputContextProxy); |
| 38 |
if (isValid()) { |
| 39 |
icproxy_->DestroyIC(); |
| 40 |
} |
| 41 |
QObject::disconnect( |
| 42 |
q, &FcitxQtInputContextProxy::virtualKeyboardVisibilityChanged, |
| 43 |
nullptr, nullptr); |
| 44 |
cleanUp(); |
| 45 |
} |
| 46 |
|
| 47 |
bool isValid() const { return (icproxy_ && icproxy_->isValid()); } |
| 48 |
|
| 49 |
void availabilityChanged() { |
| 50 |
QTimer::singleShot(100, q_ptr, [this]() { recheck(); }); |
| 51 |
} |
| 52 |
|
| 53 |
void recheck() { |
| 54 |
if (!isValid() && fcitxWatcher_->availability()) { |
| 55 |
createInputContext(); |
| 56 |
} |
| 57 |
if (!fcitxWatcher_->availability()) { |
| 58 |
cleanUp(); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
void cleanUp() { |
| 63 |
auto services = watcher_.watchedServices(); |
| 64 |
for (const auto &service : services) { |
| 65 |
watcher_.removeWatchedService(service); |
| 66 |
} |
| 67 |
|
| 68 |
delete improxy_; |
| 69 |
improxy_ = nullptr; |
| 70 |
delete icproxy_; |
| 71 |
icproxy_ = nullptr; |
| 72 |
delete createInputContextWatcher_; |
| 73 |
createInputContextWatcher_ = nullptr; |
| 74 |
delete introspectWatcher_; |
| 75 |
introspectWatcher_ = nullptr; |
| 76 |
delete queryWatcher_; |
| 77 |
queryWatcher_ = nullptr; |
| 78 |
setVirtualKeyboardVisible(false); |
| 79 |
supportInvokeAction_ = false; |
| 80 |
} |
| 81 |
|
| 82 |
void createInputContext() { |
| 83 |
Q_Q(FcitxQtInputContextProxy); |
| 84 |
if (!fcitxWatcher_->availability()) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
cleanUp(); |
| 89 |
|
| 90 |
auto service = fcitxWatcher_->serviceName(); |
| 91 |
auto connection = fcitxWatcher_->connection(); |
| 92 |
|
| 93 |
auto owner = connection.interface()->serviceOwner(service); |
| 94 |
if (!owner.isValid()) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
watcher_.setConnection(connection); |
| 99 |
watcher_.setWatchedServices(QStringList() << owner); |
| 100 |
// Avoid race, query again. |
| 101 |
if (!connection.interface()->isServiceRegistered(owner)) { |
| 102 |
cleanUp(); |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
QFileInfo info(QCoreApplication::applicationFilePath()); |
| 107 |
improxy_ = new FcitxQtInputMethodProxy( |
| 108 |
owner, "/org/freedesktop/portal/inputmethod", connection, q); |
| 109 |
FcitxQtStringKeyValueList list; |
| 110 |
FcitxQtStringKeyValue arg; |
| 111 |
arg.setKey("program"); |
| 112 |
arg.setValue(info.fileName()); |
| 113 |
list << arg; |
| 114 |
if (!display_.isEmpty()) { |
| 115 |
FcitxQtStringKeyValue arg2; |
| 116 |
arg2.setKey("display"); |
| 117 |
arg2.setValue(display_); |
| 118 |
list << arg2; |
| 119 |
} |
| 120 |
|
| 121 |
// Qt has good support for showing virtual keyboard, so we should |
| 122 |
// disable the default behavior supported by fcitx5 |
| 123 |
FcitxQtStringKeyValue clientControlVirtualkeyboardShow; |
| 124 |
clientControlVirtualkeyboardShow.setKey( |
| 125 |
"clientControlVirtualkeyboardShow"); |
| 126 |
clientControlVirtualkeyboardShow.setValue("true"); |
| 127 |
list << clientControlVirtualkeyboardShow; |
| 128 |
// Qt has poor support for hiding virtual keyboard, so we should enable |
| 129 |
// the default behavior supported by fcitx5 |
| 130 |
FcitxQtStringKeyValue clientControlVirtualkeyboardHide; |
| 131 |
clientControlVirtualkeyboardHide.setKey( |
| 132 |
"clientControlVirtualkeyboardHide"); |
| 133 |
clientControlVirtualkeyboardHide.setValue("false"); |
| 134 |
list << clientControlVirtualkeyboardHide; |
| 135 |
|
| 136 |
auto result = improxy_->CreateInputContext(list); |
| 137 |
createInputContextWatcher_ = new QDBusPendingCallWatcher(result); |
| 138 |
QObject::connect(createInputContextWatcher_, |
| 139 |
&QDBusPendingCallWatcher::finished, q, |
| 140 |
[this]() { createInputContextFinished(); }); |
| 141 |
} |
| 142 |
|
| 143 |
void createInputContextFinished() { |
| 144 |
Q_Q(FcitxQtInputContextProxy); |
| 145 |
if (createInputContextWatcher_->isError()) { |
| 146 |
cleanUp(); |
| 147 |
Q_EMIT q->inputContextCreationFailed(); |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
QDBusPendingReply<QDBusObjectPath, QByteArray> reply( |
| 152 |
*createInputContextWatcher_); |
| 153 |
icproxy_ = new FcitxQtInputContextProxyImpl(improxy_->service(), |
| 154 |
reply.value().path(), |
| 155 |
improxy_->connection(), q); |
| 156 |
QObject::connect(icproxy_, &FcitxQtInputContextProxyImpl::CommitString, |
| 157 |
q, &FcitxQtInputContextProxy::commitString); |
| 158 |
QObject::connect(icproxy_, &FcitxQtInputContextProxyImpl::CurrentIM, q, |
| 159 |
&FcitxQtInputContextProxy::currentIM); |
| 160 |
QObject::connect(icproxy_, |
| 161 |
&FcitxQtInputContextProxyImpl::DeleteSurroundingText, |
| 162 |
q, &FcitxQtInputContextProxy::deleteSurroundingText); |
| 163 |
QObject::connect(icproxy_, &FcitxQtInputContextProxyImpl::ForwardKey, q, |
| 164 |
&FcitxQtInputContextProxy::forwardKey); |
| 165 |
QObject::connect(icproxy_, |
| 166 |
&FcitxQtInputContextProxyImpl::UpdateFormattedPreedit, |
| 167 |
q, &FcitxQtInputContextProxy::updateFormattedPreedit); |
| 168 |
QObject::connect(icproxy_, |
| 169 |
&FcitxQtInputContextProxyImpl::UpdateClientSideUI, q, |
| 170 |
&FcitxQtInputContextProxy::updateClientSideUI); |
| 171 |
QObject::connect(icproxy_, |
| 172 |
&FcitxQtInputContextProxyImpl::NotifyFocusOut, q, |
| 173 |
&FcitxQtInputContextProxy::notifyFocusOut); |
| 174 |
QObject::connect( |
| 175 |
icproxy_, |
| 176 |
&FcitxQtInputContextProxyImpl::VirtualKeyboardVisibilityChanged, q, |
| 177 |
[this](bool visible) { |
| 178 |
if (queryWatcher_) { |
| 179 |
queryWatcher_->deleteLater(); |
| 180 |
queryWatcher_ = nullptr; |
| 181 |
} |
| 182 |
setVirtualKeyboardVisible(visible); |
| 183 |
}); |
| 184 |
|
| 185 |
delete createInputContextWatcher_; |
| 186 |
createInputContextWatcher_ = nullptr; |
| 187 |
Q_EMIT q->inputContextCreated(reply.argumentAt<1>()); |
| 188 |
|
| 189 |
introspect(); |
| 190 |
virtualKeyboardVisibilityQuery(); |
| 191 |
} |
| 192 |
|
| 193 |
void introspect() { |
| 194 |
Q_Q(FcitxQtInputContextProxy); |
| 195 |
if (introspectWatcher_) { |
| 196 |
delete introspectWatcher_; |
| 197 |
introspectWatcher_ = nullptr; |
| 198 |
} |
| 199 |
QDBusMessage call = QDBusMessage::createMethodCall( |
| 200 |
icproxy_->service(), icproxy_->path(), |
| 201 |
"org.freedesktop.DBus.Introspectable", "Introspect"); |
| 202 |
|
| 203 |
introspectWatcher_ = new QDBusPendingCallWatcher( |
| 204 |
fcitxWatcher_->connection().asyncCall(call)); |
| 205 |
QObject::connect(introspectWatcher_, &QDBusPendingCallWatcher::finished, |
| 206 |
q, [this]() { introspectFinished(); }); |
| 207 |
} |
| 208 |
|
| 209 |
void introspectFinished() { |
| 210 |
if (introspectWatcher_->isFinished() && |
| 211 |
!introspectWatcher_->isError()) { |
| 212 |
QDBusPendingReply<QString> reply = *introspectWatcher_; |
| 213 |
|
| 214 |
if (reply.value().contains("InvokeAction")) { |
| 215 |
supportInvokeAction_ = true; |
| 216 |
} |
| 217 |
} |
| 218 |
delete introspectWatcher_; |
| 219 |
introspectWatcher_ = nullptr; |
| 220 |
} |
| 221 |
|
| 222 |
void virtualKeyboardVisibilityQuery() { |
| 223 |
Q_Q(FcitxQtInputContextProxy); |
| 224 |
if (queryWatcher_) { |
| 225 |
delete queryWatcher_; |
| 226 |
queryWatcher_ = nullptr; |
| 227 |
} |
| 228 |
|
| 229 |
queryWatcher_ = |
| 230 |
new QDBusPendingCallWatcher(icproxy_->IsVirtualKeyboardVisible()); |
| 231 |
QObject::connect( |
| 232 |
queryWatcher_, &QDBusPendingCallWatcher::finished, q, |
| 233 |
[this]() { virtualKeyboardVisibilityQueryFinished(); }); |
| 234 |
} |
| 235 |
|
| 236 |
void virtualKeyboardVisibilityQueryFinished() { |
| 237 |
if (queryWatcher_ && queryWatcher_->isFinished() && |
| 238 |
!queryWatcher_->isError()) { |
| 239 |
QDBusPendingReply<bool> reply = *queryWatcher_; |
| 240 |
setVirtualKeyboardVisible(reply.value()); |
| 241 |
} |
| 242 |
delete queryWatcher_; |
| 243 |
queryWatcher_ = nullptr; |
| 244 |
} |
| 245 |
|
| 246 |
void setVirtualKeyboardVisible(bool visible) { |
| 247 |
Q_Q(FcitxQtInputContextProxy); |
| 248 |
if (isVirtualKeyboardVisible_ != visible) { |
| 249 |
isVirtualKeyboardVisible_ = visible; |
| 250 |
Q_EMIT q->virtualKeyboardVisibilityChanged( |
| 251 |
isVirtualKeyboardVisible_); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
FcitxQtInputContextProxy *q_ptr; |
| 256 |
Q_DECLARE_PUBLIC(FcitxQtInputContextProxy); |
| 257 |
|
| 258 |
FcitxQtWatcher *fcitxWatcher_; |
| 259 |
QDBusServiceWatcher watcher_; |
| 260 |
FcitxQtInputMethodProxy *improxy_ = nullptr; |
| 261 |
FcitxQtInputContextProxyImpl *icproxy_ = nullptr; |
| 262 |
bool isVirtualKeyboardVisible_ = false; |
| 263 |
bool supportInvokeAction_ = false; |
| 264 |
QDBusPendingCallWatcher *createInputContextWatcher_ = nullptr; |
| 265 |
QDBusPendingCallWatcher *introspectWatcher_ = nullptr; |
| 266 |
QDBusPendingCallWatcher *queryWatcher_ = nullptr; |
| 267 |
QString display_; |
| 268 |
}; |
| 269 |
} // namespace fcitx |
| 270 |
|
| 271 |
#endif // _DBUSADDONS_FCITXQTINPUTCONTEXTPROXY_P_H_ |