| 1 |
/* |
| 2 |
* Copyright (C) 2011~2017 by CSSlayer |
| 3 |
* [email protected] |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* |
| 9 |
* 1. Redistributions of source code must retain the above Copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* 2. Redistributions in binary form must reproduce the above Copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* |
| 16 |
* 3. Neither the name of the authors nor the names of its contributors |
| 17 |
* may be used to endorse or promote products derived from this |
| 18 |
* software without specific prior written permission. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "fcitxwatcher.h" |
| 22 |
#include <QDBusConnection> |
| 23 |
#include <QDBusConnectionInterface> |
| 24 |
#include <QDBusServiceWatcher> |
| 25 |
#include <QDir> |
| 26 |
#include <QFileSystemWatcher> |
| 27 |
#include <errno.h> |
| 28 |
#include <signal.h> |
| 29 |
#include <unistd.h> |
| 30 |
|
| 31 |
// utils function in fcitx-utils and fcitx-config |
| 32 |
bool _pid_exists(pid_t pid) { |
| 33 |
if (pid <= 0) |
| 34 |
return 0; |
| 35 |
return !(kill(pid, 0) && (errno == ESRCH)); |
| 36 |
} |
| 37 |
|
| 38 |
int displayNumber() { |
| 39 |
QByteArray display(qgetenv("DISPLAY")); |
| 40 |
QByteArray displayNumber("0"); |
| 41 |
int pos = display.indexOf(':'); |
| 42 |
|
| 43 |
if (pos >= 0) { |
| 44 |
++pos; |
| 45 |
int pos2 = display.indexOf('.', pos); |
| 46 |
if (pos2 > 0) { |
| 47 |
displayNumber = display.mid(pos, pos2 - pos); |
| 48 |
} else { |
| 49 |
displayNumber = display.mid(pos); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
bool ok; |
| 54 |
int d = displayNumber.toInt(&ok); |
| 55 |
if (ok) { |
| 56 |
return d; |
| 57 |
} |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
QString socketFile() { |
| 62 |
QString filename = |
| 63 |
QString("%1-%2") |
| 64 |
.arg(QString::fromLatin1(QDBusConnection::localMachineId())) |
| 65 |
.arg(displayNumber()); |
| 66 |
|
| 67 |
QString home = QString::fromLocal8Bit(qgetenv("XDG_CONFIG_HOME")); |
| 68 |
if (home.isEmpty()) { |
| 69 |
home = QDir::homePath().append(QLatin1String("/.config")); |
| 70 |
} |
| 71 |
return QString("%1/fcitx/dbus/%2").arg(home).arg(filename); |
| 72 |
} |
| 73 |
|
| 74 |
FcitxWatcher::FcitxWatcher(QDBusConnection sessionBus, QObject *parent) |
| 75 |
: QObject(parent), m_fsWatcher(new QFileSystemWatcher(this)), |
| 76 |
m_serviceWatcher(new QDBusServiceWatcher(this)), m_connection(nullptr), |
| 77 |
m_sessionBus(sessionBus), m_socketFile(socketFile()), |
| 78 |
m_serviceName(QString("org.fcitx.Fcitx-%1").arg(displayNumber())), |
| 79 |
m_availability(false) {} |
| 80 |
|
| 81 |
FcitxWatcher::~FcitxWatcher() { |
| 82 |
cleanUpConnection(); |
| 83 |
delete m_fsWatcher; |
| 84 |
m_fsWatcher = nullptr; |
| 85 |
} |
| 86 |
|
| 87 |
bool FcitxWatcher::availability() const { return m_availability; } |
| 88 |
|
| 89 |
QDBusConnection FcitxWatcher::connection() const { |
| 90 |
if (m_connection) { |
| 91 |
return *m_connection; |
| 92 |
} |
| 93 |
return m_sessionBus; |
| 94 |
} |
| 95 |
|
| 96 |
QString FcitxWatcher::service() const { |
| 97 |
if (m_connection) { |
| 98 |
return m_serviceName; |
| 99 |
} |
| 100 |
if (m_mainPresent) { |
| 101 |
return m_serviceName; |
| 102 |
} |
| 103 |
if (m_portalPresent) { |
| 104 |
return "org.freedesktop.portal.Fcitx"; |
| 105 |
} |
| 106 |
return QString(); |
| 107 |
} |
| 108 |
|
| 109 |
void FcitxWatcher::setAvailability(bool availability) { |
| 110 |
if (m_availability != availability) { |
| 111 |
m_availability = availability; |
| 112 |
Q_EMIT availabilityChanged(m_availability); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
void FcitxWatcher::watch() { |
| 117 |
if (m_watched) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
connect(m_serviceWatcher, |
| 122 |
SIGNAL(serviceOwnerChanged(QString, QString, QString)), this, |
| 123 |
SLOT(imChanged(QString, QString, QString))); |
| 124 |
m_serviceWatcher->setConnection(m_sessionBus); |
| 125 |
m_serviceWatcher->addWatchedService(m_serviceName); |
| 126 |
m_serviceWatcher->addWatchedService("org.freedesktop.portal.Fcitx"); |
| 127 |
|
| 128 |
if (m_sessionBus.interface()->isServiceRegistered(m_serviceName)) { |
| 129 |
m_mainPresent = true; |
| 130 |
} |
| 131 |
if (m_sessionBus.interface()->isServiceRegistered( |
| 132 |
"org.freedesktop.portal.Fcitx")) { |
| 133 |
m_portalPresent = true; |
| 134 |
} |
| 135 |
|
| 136 |
watchSocketFile(); |
| 137 |
createConnection(); |
| 138 |
m_watched = true; |
| 139 |
} |
| 140 |
|
| 141 |
void FcitxWatcher::unwatch() { |
| 142 |
if (!m_watched) { |
| 143 |
return; |
| 144 |
} |
| 145 |
disconnect(m_serviceWatcher, |
| 146 |
SIGNAL(serviceOwnerChanged(QString, QString, QString)), this, |
| 147 |
SLOT(imChanged(QString, QString, QString))); |
| 148 |
unwatchSocketFile(); |
| 149 |
cleanUpConnection(); |
| 150 |
m_mainPresent = false; |
| 151 |
m_portalPresent = false; |
| 152 |
m_watched = false; |
| 153 |
updateAvailability(); |
| 154 |
} |
| 155 |
|
| 156 |
QString FcitxWatcher::address() { |
| 157 |
QString addr; |
| 158 |
QByteArray addrVar = qgetenv("FCITX_DBUS_ADDRESS"); |
| 159 |
if (!addrVar.isNull()) |
| 160 |
return QString::fromLocal8Bit(addrVar); |
| 161 |
|
| 162 |
QFile file(m_socketFile); |
| 163 |
if (!file.open(QIODevice::ReadOnly)) |
| 164 |
return QString(); |
| 165 |
|
| 166 |
const int BUFSIZE = 1024; |
| 167 |
|
| 168 |
char buffer[BUFSIZE]; |
| 169 |
size_t sz = file.read(buffer, BUFSIZE); |
| 170 |
file.close(); |
| 171 |
if (sz == 0) |
| 172 |
return QString(); |
| 173 |
char *p = buffer; |
| 174 |
while (*p) |
| 175 |
p++; |
| 176 |
size_t addrlen = p - buffer; |
| 177 |
if (sz != addrlen + 2 * sizeof(pid_t) + 1) |
| 178 |
return QString(); |
| 179 |
|
| 180 |
/* skip '\0' */ |
| 181 |
p++; |
| 182 |
pid_t *ppid = (pid_t *)p; |
| 183 |
pid_t daemonpid = ppid[0]; |
| 184 |
pid_t fcitxpid = ppid[1]; |
| 185 |
|
| 186 |
if (!_pid_exists(daemonpid) || !_pid_exists(fcitxpid)) |
| 187 |
return QString(); |
| 188 |
|
| 189 |
addr = QLatin1String(buffer); |
| 190 |
|
| 191 |
return addr; |
| 192 |
} |
| 193 |
|
| 194 |
void FcitxWatcher::cleanUpConnection() { |
| 195 |
QDBusConnection::disconnectFromBus("fcitx"); |
| 196 |
delete m_connection; |
| 197 |
m_connection = nullptr; |
| 198 |
} |
| 199 |
|
| 200 |
void FcitxWatcher::socketFileChanged() { |
| 201 |
cleanUpConnection(); |
| 202 |
createConnection(); |
| 203 |
} |
| 204 |
|
| 205 |
void FcitxWatcher::createConnection() { |
| 206 |
QString addr = address(); |
| 207 |
// qDebug() << addr; |
| 208 |
if (!addr.isNull()) { |
| 209 |
QDBusConnection connection( |
| 210 |
QDBusConnection::connectToBus(addr, "fcitx")); |
| 211 |
if (connection.isConnected()) { |
| 212 |
// qDebug() << "create private"; |
| 213 |
m_connection = new QDBusConnection(connection); |
| 214 |
} else { |
| 215 |
QDBusConnection::disconnectFromBus("fcitx"); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
if (m_connection) { |
| 220 |
m_connection->connect("org.freedesktop.DBus.Local", |
| 221 |
"/org/freedesktop/DBus/Local", |
| 222 |
"org.freedesktop.DBus.Local", "Disconnected", |
| 223 |
this, SLOT(dbusDisconnected())); |
| 224 |
unwatchSocketFile(); |
| 225 |
} |
| 226 |
updateAvailability(); |
| 227 |
} |
| 228 |
|
| 229 |
void FcitxWatcher::dbusDisconnected() { |
| 230 |
cleanUpConnection(); |
| 231 |
watchSocketFile(); |
| 232 |
// Try recreation immediately to avoid race. |
| 233 |
createConnection(); |
| 234 |
} |
| 235 |
|
| 236 |
void FcitxWatcher::watchSocketFile() { |
| 237 |
if (m_socketFile.isEmpty()) { |
| 238 |
return; |
| 239 |
} |
| 240 |
QFileInfo info(m_socketFile); |
| 241 |
QDir dir(info.path()); |
| 242 |
if (!dir.exists()) { |
| 243 |
QDir rt(QDir::root()); |
| 244 |
rt.mkpath(info.path()); |
| 245 |
} |
| 246 |
m_fsWatcher->addPath(info.path()); |
| 247 |
if (info.exists()) { |
| 248 |
m_fsWatcher->addPath(info.filePath()); |
| 249 |
} |
| 250 |
|
| 251 |
connect(m_fsWatcher, SIGNAL(fileChanged(QString)), this, |
| 252 |
SLOT(socketFileChanged())); |
| 253 |
connect(m_fsWatcher, SIGNAL(directoryChanged(QString)), this, |
| 254 |
SLOT(socketFileChanged())); |
| 255 |
} |
| 256 |
|
| 257 |
void FcitxWatcher::unwatchSocketFile() { |
| 258 |
if (!m_fsWatcher->files().isEmpty()) { |
| 259 |
m_fsWatcher->removePaths(m_fsWatcher->files()); |
| 260 |
} |
| 261 |
if (!m_fsWatcher->directories().isEmpty()) { |
| 262 |
m_fsWatcher->removePaths(m_fsWatcher->directories()); |
| 263 |
} |
| 264 |
m_fsWatcher->disconnect(SIGNAL(fileChanged(QString))); |
| 265 |
m_fsWatcher->disconnect(SIGNAL(directoryChanged(QString))); |
| 266 |
} |
| 267 |
|
| 268 |
void FcitxWatcher::imChanged(const QString &service, const QString &, |
| 269 |
const QString &newOwner) { |
| 270 |
if (service == m_serviceName) { |
| 271 |
if (!newOwner.isEmpty()) { |
| 272 |
m_mainPresent = true; |
| 273 |
} else { |
| 274 |
m_mainPresent = false; |
| 275 |
} |
| 276 |
} else if (service == "org.freedesktop.portal.Fcitx") { |
| 277 |
if (!newOwner.isEmpty()) { |
| 278 |
m_portalPresent = true; |
| 279 |
} else { |
| 280 |
m_portalPresent = false; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
updateAvailability(); |
| 285 |
} |
| 286 |
|
| 287 |
void FcitxWatcher::updateAvailability() { |
| 288 |
setAvailability(m_mainPresent || m_portalPresent || m_connection); |
| 289 |
} |