| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2023~2023 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "hybridinputcontext.h" |
| 9 |
#include "fcitx4inputcontextproxy.h" |
| 10 |
#include "fcitxqtinputcontextproxy.h" |
| 11 |
#include "fcitxqtwatcher.h" |
| 12 |
#include <cstddef> |
| 13 |
|
| 14 |
namespace fcitx { |
| 15 |
|
| 16 |
HybridInputContext::HybridInputContext(FcitxQtWatcher *watcher, |
| 17 |
Fcitx4Watcher *fcitx4Watcher, |
| 18 |
QObject *parent) |
| 19 |
: QObject(parent), watcher_(watcher), fcitx4Watcher_(fcitx4Watcher) { |
| 20 |
// We use a timer here to avoid recheck being triggered multiple times. |
| 21 |
// For fcitx5, the fcitx4frontend and dbusfrontend will be initialized |
| 22 |
// around the same time. 50ms is specifically selected to be smaller than |
| 23 |
// the 100ms timer within input context. |
| 24 |
timer_.setInterval(50); |
| 25 |
timer_.setSingleShot(true); |
| 26 |
connect(&timer_, &QTimer::timeout, this, |
| 27 |
[this]() { doRecheck(/*skipFcitx5=*/false); }); |
| 28 |
connect(watcher_, &FcitxQtWatcher::availabilityChanged, this, |
| 29 |
&HybridInputContext::recheck); |
| 30 |
connect(fcitx4Watcher_, &Fcitx4Watcher::availabilityChanged, this, |
| 31 |
&HybridInputContext::recheck); |
| 32 |
|
| 33 |
recheck(); |
| 34 |
} |
| 35 |
|
| 36 |
void HybridInputContext::recheck() { timer_.start(); } |
| 37 |
|
| 38 |
void HybridInputContext::doRecheck(bool skipFcitx5) { |
| 39 |
const bool fcitx5Available = !skipFcitx5 && watcher_->availability(); |
| 40 |
if (!fcitx5Available) { |
| 41 |
delete proxy_; |
| 42 |
proxy_ = nullptr; |
| 43 |
} |
| 44 |
|
| 45 |
if (!fcitx4Watcher_->availability()) { |
| 46 |
delete proxy4_; |
| 47 |
proxy4_ = nullptr; |
| 48 |
} |
| 49 |
|
| 50 |
if (fcitx5Available) { |
| 51 |
delete proxy4_; |
| 52 |
proxy4_ = nullptr; |
| 53 |
if (!proxy_) { |
| 54 |
proxy_ = new FcitxQtInputContextProxy(watcher_, this); |
| 55 |
proxy_->setDisplay(display_); |
| 56 |
connect(proxy_, &FcitxQtInputContextProxy::commitString, this, |
| 57 |
&HybridInputContext::commitString); |
| 58 |
connect(proxy_, &FcitxQtInputContextProxy::currentIM, this, |
| 59 |
&HybridInputContext::currentIM); |
| 60 |
connect(proxy_, &FcitxQtInputContextProxy::deleteSurroundingText, |
| 61 |
this, &HybridInputContext::deleteSurroundingText); |
| 62 |
connect(proxy_, &FcitxQtInputContextProxy::forwardKey, this, |
| 63 |
&HybridInputContext::forwardKey); |
| 64 |
connect(proxy_, &FcitxQtInputContextProxy::updateFormattedPreedit, |
| 65 |
this, &HybridInputContext::updateFormattedPreedit); |
| 66 |
connect(proxy_, &FcitxQtInputContextProxy::updateClientSideUI, this, |
| 67 |
&HybridInputContext::updateClientSideUI); |
| 68 |
connect(proxy_, &FcitxQtInputContextProxy::inputContextCreated, |
| 69 |
this, &HybridInputContext::inputContextCreated); |
| 70 |
connect(proxy_, |
| 71 |
&FcitxQtInputContextProxy::inputContextCreationFailed, this, |
| 72 |
&HybridInputContext::inputContextCreationFailed); |
| 73 |
connect(proxy_, &FcitxQtInputContextProxy::notifyFocusOut, this, |
| 74 |
&HybridInputContext::notifyFocusOut); |
| 75 |
connect(proxy_, |
| 76 |
&FcitxQtInputContextProxy::virtualKeyboardVisibilityChanged, |
| 77 |
this, |
| 78 |
&HybridInputContext::virtualKeyboardVisibilityChanged); |
| 79 |
} |
| 80 |
} else if (fcitx4Watcher_->availability()) { |
| 81 |
if (!proxy4_) { |
| 82 |
proxy4_ = new Fcitx4InputContextProxy(fcitx4Watcher_, this); |
| 83 |
connect(proxy4_, &Fcitx4InputContextProxy::commitString, this, |
| 84 |
&HybridInputContext::commitString); |
| 85 |
connect(proxy4_, &Fcitx4InputContextProxy::currentIM, this, |
| 86 |
&HybridInputContext::currentIM); |
| 87 |
connect(proxy4_, &Fcitx4InputContextProxy::deleteSurroundingText, |
| 88 |
this, &HybridInputContext::deleteSurroundingText); |
| 89 |
connect(proxy4_, &Fcitx4InputContextProxy::forwardKey, this, |
| 90 |
&HybridInputContext::forwardKey); |
| 91 |
connect( |
| 92 |
proxy4_, &Fcitx4InputContextProxy::updateFormattedPreedit, this, |
| 93 |
[this](const FcitxQtFormattedPreeditList &list, int cursorpos) { |
| 94 |
auto newList = list; |
| 95 |
for (auto item : newList) { |
| 96 |
const qint32 underlineBit = (1 << 3); |
| 97 |
// revert non underline and "underline" |
| 98 |
item.setFormat(item.format() ^ underlineBit); |
| 99 |
} |
| 100 |
|
| 101 |
Q_EMIT updateFormattedPreedit(newList, cursorpos); |
| 102 |
}); |
| 103 |
connect(proxy4_, &Fcitx4InputContextProxy::inputContextCreated, |
| 104 |
this, |
| 105 |
[this]() { Q_EMIT inputContextCreated(QByteArray()); }); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
void HybridInputContext::inputContextCreationFailed() { |
| 111 |
doRecheck(/*skipFcitx5=*/true); |
| 112 |
} |
| 113 |
|
| 114 |
bool HybridInputContext::isValid() const { |
| 115 |
if (proxy_) { |
| 116 |
return proxy_->isValid(); |
| 117 |
} else if (proxy4_) { |
| 118 |
return proxy4_->isValid(); |
| 119 |
} |
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
void HybridInputContext::reset() { |
| 124 |
if (proxy_) { |
| 125 |
proxy_->reset(); |
| 126 |
} else if (proxy4_) { |
| 127 |
proxy4_->reset(); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
void HybridInputContext::selectCandidate(int i) { |
| 132 |
if (proxy_) { |
| 133 |
proxy_->selectCandidate(i); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
void HybridInputContext::prevPage() { |
| 138 |
if (proxy_) { |
| 139 |
proxy_->prevPage(); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
void HybridInputContext::nextPage() { |
| 144 |
if (proxy_) { |
| 145 |
proxy_->nextPage(); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
bool HybridInputContext::supportInvokeAction() const { |
| 150 |
if (proxy_) { |
| 151 |
return proxy_->supportInvokeAction(); |
| 152 |
} |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
void HybridInputContext::invokeAction(unsigned int action, int cursor) { |
| 157 |
if (proxy_) { |
| 158 |
proxy_->invokeAction(action, cursor); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
void HybridInputContext::setSurroundingText(const QString &text, |
| 163 |
unsigned int cursor, |
| 164 |
unsigned int anchor) { |
| 165 |
if (proxy_) { |
| 166 |
proxy_->setSurroundingText(text, cursor, anchor); |
| 167 |
} else if (proxy4_) { |
| 168 |
proxy4_->setSurroundingText(text, cursor, anchor); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
void HybridInputContext::setSurroundingTextPosition(unsigned int cursor, |
| 173 |
unsigned int anchor) { |
| 174 |
if (proxy_) { |
| 175 |
proxy_->setSurroundingTextPosition(cursor, anchor); |
| 176 |
} else if (proxy4_) { |
| 177 |
proxy4_->setSurroundingTextPosition(cursor, anchor); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
void HybridInputContext::focusIn() { |
| 182 |
if (proxy_) { |
| 183 |
proxy_->focusIn(); |
| 184 |
} else if (proxy4_) { |
| 185 |
proxy4_->focusIn(); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
void HybridInputContext::focusOut() { |
| 190 |
if (proxy_) { |
| 191 |
proxy_->focusOut(); |
| 192 |
} else if (proxy4_) { |
| 193 |
proxy4_->focusOut(); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
void HybridInputContext::setCursorRectV2(int x, int y, int w, int h, |
| 198 |
double scale) { |
| 199 |
if (proxy_) { |
| 200 |
proxy_->setCursorRectV2(x, y, w, h, scale); |
| 201 |
} else if (proxy4_) { |
| 202 |
proxy4_->setCursorRect(x, y, w, h); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
void HybridInputContext::setCursorRect(int x, int y, int w, int h) { |
| 207 |
if (proxy_) { |
| 208 |
proxy_->setCursorRect(x, y, w, h); |
| 209 |
} else if (proxy4_) { |
| 210 |
proxy4_->setCursorRect(x, y, w, h); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
void HybridInputContext::setSupportedCapability(quint64 supported) { |
| 215 |
if (proxy_) { |
| 216 |
proxy_->setSupportedCapability(supported); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
void HybridInputContext::setCapability(quint64 capability) { |
| 221 |
if (proxy_) { |
| 222 |
proxy_->setCapability(capability); |
| 223 |
} else if (proxy4_) { |
| 224 |
proxy4_->setCapability(capability & 0xfffffffffULL); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
void HybridInputContext::showVirtualKeyboard() { |
| 229 |
if (proxy_) { |
| 230 |
proxy_->showVirtualKeyboard(); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
void HybridInputContext::hideVirtualKeyboard() { |
| 235 |
if (proxy_) { |
| 236 |
proxy_->hideVirtualKeyboard(); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
bool HybridInputContext::isVirtualKeyboardVisible() const { |
| 241 |
if (proxy_) { |
| 242 |
return proxy_->isVirtualKeyboardVisible(); |
| 243 |
} |
| 244 |
return false; |
| 245 |
} |
| 246 |
|
| 247 |
void HybridInputContext::setDisplay(const QString &display) { |
| 248 |
if (proxy_) { |
| 249 |
proxy_->setDisplay(display); |
| 250 |
} |
| 251 |
display_ = display; |
| 252 |
} |
| 253 |
|
| 254 |
QDBusPendingCall HybridInputContext::processKeyEvent(unsigned int keyval, |
| 255 |
unsigned int keycode, |
| 256 |
unsigned int state, |
| 257 |
bool type, |
| 258 |
unsigned int time) { |
| 259 |
if (proxy_) { |
| 260 |
return proxy_->processKeyEvent(keyval, keycode, state, type, time); |
| 261 |
} |
| 262 |
return proxy4_->processKeyEvent(keyval, keycode, state, type ? 1 : 0, time); |
| 263 |
} |
| 264 |
|
| 265 |
bool HybridInputContext::processKeyEventResult(const QDBusPendingCall &call) { |
| 266 |
if (call.isError()) { |
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
if (call.reply().signature() == "b") { |
| 271 |
QDBusPendingReply<bool> reply = call; |
| 272 |
return reply.value(); |
| 273 |
} else if (call.reply().signature() == "i") { |
| 274 |
QDBusPendingReply<int> reply = call; |
| 275 |
return reply.value() > 0; |
| 276 |
} |
| 277 |
return false; |
| 278 |
} |
| 279 |
|
| 280 |
} // namespace fcitx |