1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
|
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "core/launcher.h"
#include "platform/platform_launcher.h"
#include "platform/platform_specific.h"
#include "base/options.h"
#include "base/platform/base_platform_info.h"
#include "base/platform/base_platform_file_utilities.h"
#include "ui/main_queue_processor.h"
#include "core/crash_reports.h"
#include "core/update_checker.h"
#include "core/sandbox.h"
#include "base/concurrent_timer.h"
#include "base/options.h"
#include <QtCore/QLoggingCategory>
namespace Core {
namespace {
uint64 InstallationTag = 0;
base::options::toggle OptionFreeType({
.id = kOptionFreeType,
.name = "FreeType font engine",
.description = "Use the font engine from Linux instead of the system one.",
.scope = base::options::windows | base::options::macos,
.restartRequired = true,
});
class FilteredCommandLineArguments {
public:
FilteredCommandLineArguments(int argc, char **argv);
int &count();
char **values();
private:
static constexpr auto kForwardArgumentCount = 1;
int _count = 0;
std::vector<QByteArray> _owned;
std::vector<char*> _arguments;
void pushArgument(const char *text);
};
FilteredCommandLineArguments::FilteredCommandLineArguments(
int argc,
char **argv) {
// For now just pass only the first argument, the executable path.
for (auto i = 0; i != kForwardArgumentCount; ++i) {
pushArgument(argv[i]);
}
#if defined Q_OS_WIN || defined Q_OS_MAC
if (OptionFreeType.value()) {
pushArgument("-platform");
#ifdef Q_OS_WIN
pushArgument("windows:fontengine=freetype");
#else // Q_OS_WIN
pushArgument("cocoa:fontengine=freetype");
#endif // !Q_OS_WIN
}
#elif defined Q_OS_UNIX
if (QFile::exists(cWorkingDir() + u"tdata/nowayland"_q)
&& qEnvironmentVariableIsEmpty("QT_QPA_PLATFORM")) {
LOG(("Wayland: Disable on old installations"));
pushArgument("-platform");
pushArgument("xcb;wayland");
}
#endif // Q_OS_WIN || Q_OS_MAC || Q_OS_UNIX
pushArgument(nullptr);
}
int &FilteredCommandLineArguments::count() {
_count = _arguments.size() - 1;
return _count;
}
char **FilteredCommandLineArguments::values() {
return _arguments.data();
}
void FilteredCommandLineArguments::pushArgument(const char *text) {
_owned.emplace_back(text);
_arguments.push_back(_owned.back().data());
}
QString DebugModeSettingPath() {
return cWorkingDir() + u"tdata/withdebug"_q;
}
void WriteDebugModeSetting() {
auto file = QFile(DebugModeSettingPath());
if (file.open(QIODevice::WriteOnly)) {
file.write(Logs::DebugEnabled() ? "1" : "0");
}
}
void ComputeDebugMode() {
Logs::SetDebugEnabled(cAlphaVersion() != 0);
const auto debugModeSettingPath = DebugModeSettingPath();
auto file = QFile(debugModeSettingPath);
if (file.exists() && file.open(QIODevice::ReadOnly)) {
Logs::SetDebugEnabled(file.read(1) != "0");
}
if (cDebugMode()) {
Logs::SetDebugEnabled(true);
}
if (Logs::DebugEnabled()) {
QLoggingCategory::setFilterRules("qt.qpa.gl.debug=true");
}
}
void ComputeExternalUpdater() {
QFile file(u"/etc/tdesktop/externalupdater"_q);
if (file.exists() && file.open(QIODevice::ReadOnly)) {
QTextStream fileStream(&file);
while (!fileStream.atEnd()) {
const auto path = fileStream.readLine();
if (path == (cExeDir() + cExeName())) {
SetUpdaterDisabledAtStartup();
return;
}
}
}
}
QString InstallBetaVersionsSettingPath() {
return cWorkingDir() + u"tdata/devversion"_q;
}
void WriteInstallBetaVersionsSetting() {
QFile f(InstallBetaVersionsSettingPath());
if (f.open(QIODevice::WriteOnly)) {
f.write(cInstallBetaVersion() ? "1" : "0");
}
}
void ComputeInstallBetaVersions() {
const auto installBetaSettingPath = InstallBetaVersionsSettingPath();
if (cAlphaVersion()) {
cSetInstallBetaVersion(false);
} else if (QFile::exists(installBetaSettingPath)) {
QFile f(installBetaSettingPath);
if (f.open(QIODevice::ReadOnly)) {
cSetInstallBetaVersion(f.read(1) != "0");
}
} else if (AppBetaVersion) {
WriteInstallBetaVersionsSetting();
}
}
void ComputeInstallationTag() {
InstallationTag = 0;
auto file = QFile(cWorkingDir() + u"tdata/usertag"_q);
if (file.open(QIODevice::ReadOnly)) {
const auto result = file.read(
reinterpret_cast<char*>(&InstallationTag),
sizeof(uint64));
if (result != sizeof(uint64)) {
InstallationTag = 0;
}
file.close();
}
if (!InstallationTag) {
auto generator = std::mt19937(std::random_device()());
auto distribution = std::uniform_int_distribution<uint64>();
do {
InstallationTag = distribution(generator);
} while (!InstallationTag);
if (file.open(QIODevice::WriteOnly)) {
file.write(
reinterpret_cast<char*>(&InstallationTag),
sizeof(uint64));
file.close();
}
}
}
bool MoveLegacyAlphaFolder(const QString &folder, const QString &file) {
const auto was = cExeDir() + folder;
const auto now = cExeDir() + u"TelegramForcePortable"_q;
if (QDir(was).exists() && !QDir(now).exists()) {
const auto oldFile = was + "/tdata/" + file;
const auto newFile = was + "/tdata/alpha";
if (QFile::exists(oldFile) && !QFile::exists(newFile)) {
if (!QFile(oldFile).copy(newFile)) {
LOG(("FATAL: Could not copy '%1' to '%2'").arg(
oldFile,
newFile));
return false;
}
}
if (!QDir().rename(was, now)) {
LOG(("FATAL: Could not rename '%1' to '%2'").arg(was, now));
return false;
}
}
return true;
}
bool MoveLegacyAlphaFolder() {
if (!MoveLegacyAlphaFolder(u"TelegramAlpha_data"_q, u"alpha"_q)
|| !MoveLegacyAlphaFolder(u"TelegramBeta_data"_q, u"beta"_q)) {
return false;
}
return true;
}
bool CheckPortableVersionFolder() {
if (!MoveLegacyAlphaFolder()) {
return false;
}
const auto portable = cExeDir() + u"TelegramForcePortable"_q;
QFile key(portable + u"/tdata/alpha"_q);
if (cAlphaVersion()) {
Assert(*AlphaPrivateKey != 0);
cForceWorkingDir(portable + '/');
QDir().mkpath(cWorkingDir() + u"tdata"_q);
cSetAlphaPrivateKey(QByteArray(AlphaPrivateKey));
if (!key.open(QIODevice::WriteOnly)) {
LOG(("FATAL: Could not open '%1' for writing private key!"
).arg(key.fileName()));
return false;
}
QDataStream dataStream(&key);
dataStream.setVersion(QDataStream::Qt_5_3);
dataStream << quint64(cRealAlphaVersion()) << cAlphaPrivateKey();
return true;
}
if (!QDir(portable).exists()) {
return true;
}
cForceWorkingDir(portable + '/');
if (!key.exists()) {
return true;
}
if (!key.open(QIODevice::ReadOnly)) {
LOG(("FATAL: could not open '%1' for reading private key. "
"Delete it or reinstall private alpha version."
).arg(key.fileName()));
return false;
}
QDataStream dataStream(&key);
dataStream.setVersion(QDataStream::Qt_5_3);
quint64 v;
QByteArray k;
dataStream >> v >> k;
if (dataStream.status() != QDataStream::Ok || k.isEmpty()) {
LOG(("FATAL: '%1' is corrupted. "
"Delete it or reinstall private alpha version."
).arg(key.fileName()));
return false;
}
cSetAlphaVersion(AppVersion * 1000ULL);
cSetAlphaPrivateKey(k);
cSetRealAlphaVersion(v);
return true;
}
base::options::toggle OptionFractionalScalingEnabled({
.id = kOptionFractionalScalingEnabled,
.name = "Enable precise High DPI scaling",
.description = "Follow system interface scale settings exactly.",
.scope = base::options::windows | base::options::linux,
.restartRequired = true,
});
} // namespace
const char kOptionFractionalScalingEnabled[] = "fractional-scaling-enabled";
const char kOptionFreeType[] = "freetype";
std::unique_ptr<Launcher> Launcher::Create(int argc, char *argv[]) {
return std::make_unique<Platform::Launcher>(argc, argv);
}
Launcher::Launcher(int argc, char *argv[])
: _argc(argc)
, _argv(argv)
, _baseIntegration(_argc, _argv) {
crl::toggle_fp_exceptions(true);
base::Integration::Set(&_baseIntegration);
}
void Launcher::init() {
_arguments = readArguments(_argc, _argv);
prepareSettings();
initQtMessageLogging();
QApplication::setApplicationName(u"TelegramDesktop"_q);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
// fallback session management is useless for tdesktop since it doesn't have
// any "are you sure you want to close this window?" dialogs
// but it produces bugs like https://github.com/telegramdesktop/tdesktop/issues/5022
// and https://github.com/telegramdesktop/tdesktop/issues/7549
// and https://github.com/telegramdesktop/tdesktop/issues/948
// more info: https://doc.qt.io/qt-5/qguiapplication.html#isFallbackSessionManagementEnabled
QApplication::setFallbackSessionManagementEnabled(false);
#endif // Qt < 6.0.0
initHook();
}
void Launcher::initHighDpi() {
#if QT_VERSION < QT_VERSION_CHECK(6, 2, 0)
qputenv("QT_DPI_ADJUSTMENT_POLICY", "AdjustDpi");
#endif // Qt < 6.2.0
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
#endif // Qt < 6.0.0
if (OptionFractionalScalingEnabled.value()) {
QApplication::setHighDpiScaleFactorRoundingPolicy(
Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
} else {
QApplication::setHighDpiScaleFactorRoundingPolicy(
Qt::HighDpiScaleFactorRoundingPolicy::RoundPreferFloor);
}
}
int Launcher::exec() {
init();
if (cLaunchMode() == LaunchModeFixPrevious) {
return psFixPrevious();
} else if (cLaunchMode() == LaunchModeCleanup) {
return psCleanup();
}
// Must be started before Platform is started.
Logs::start(this);
base::options::init(cWorkingDir() + "tdata/experimental_options.json");
// Must be called after options are inited.
initHighDpi();
if (Logs::DebugEnabled()) {
const auto openalLogPath = QDir::toNativeSeparators(
cWorkingDir() + u"DebugLogs/last_openal_log.txt"_q);
qputenv("ALSOFT_LOGLEVEL", "3");
#ifdef Q_OS_WIN
_wputenv_s(
L"ALSOFT_LOGFILE",
openalLogPath.toStdWString().c_str());
#else // Q_OS_WIN
qputenv(
"ALSOFT_LOGFILE",
QFile::encodeName(openalLogPath));
#endif // !Q_OS_WIN
}
// Must be started before Sandbox is created.
Platform::start();
auto result = executeApplication();
DEBUG_LOG(("Telegram finished, result: %1").arg(result));
if (!UpdaterDisabled() && cRestartingUpdate()) {
DEBUG_LOG(("Sandbox Info: executing updater to install update."));
if (!launchUpdater(UpdaterLaunch::PerformUpdate)) {
base::Platform::DeleteDirectory(cWorkingDir() + u"tupdates/temp"_q);
}
} else if (cRestarting()) {
DEBUG_LOG(("Sandbox Info: executing Telegram because of restart."));
launchUpdater(UpdaterLaunch::JustRelaunch);
}
CrashReports::Finish();
Platform::finish();
Logs::finish();
return result;
}
void Launcher::workingFolderReady() {
srand((unsigned int)time(nullptr));
ComputeDebugMode();
ComputeExternalUpdater();
ComputeInstallBetaVersions();
ComputeInstallationTag();
}
void Launcher::writeDebugModeSetting() {
WriteDebugModeSetting();
}
void Launcher::writeInstallBetaVersionsSetting() {
WriteInstallBetaVersionsSetting();
}
bool Launcher::checkPortableVersionFolder() {
return CheckPortableVersionFolder();
}
QStringList Launcher::readArguments(int argc, char *argv[]) const {
Expects(argc >= 0);
if (const auto native = readArgumentsHook(argc, argv)) {
return *native;
}
auto result = QStringList();
result.reserve(argc);
for (auto i = 0; i != argc; ++i) {
result.push_back(base::FromUtf8Safe(argv[i]));
}
return result;
}
QString Launcher::argumentsString() const {
return _arguments.join(' ');
}
bool Launcher::customWorkingDir() const {
return _customWorkingDir;
}
void Launcher::prepareSettings() {
auto path = base::Platform::CurrentExecutablePath(_argc, _argv);
LOG(("Executable path before check: %1").arg(path));
if (!path.isEmpty()) {
auto info = QFileInfo(path);
if (info.isSymLink()) {
info = QFileInfo(info.symLinkTarget());
}
if (info.exists()) {
const auto dir = info.absoluteDir().absolutePath();
gExeDir = (dir.endsWith('/') ? dir : (dir + '/'));
gExeName = info.fileName();
}
}
if (cExeName().isEmpty()) {
LOG(("WARNING: Could not compute executable path, some features will be disabled."));
}
processArguments();
}
void Launcher::initQtMessageLogging() {
static QtMessageHandler OriginalMessageHandler = nullptr;
OriginalMessageHandler = qInstallMessageHandler([](
QtMsgType type,
const QMessageLogContext &context,
const QString &msg) {
const auto InvokeOriginal = [&] {
#ifndef _DEBUG
if (Logs::DebugEnabled()) {
return;
}
#endif // _DEBUG
if (OriginalMessageHandler) {
OriginalMessageHandler(type, context, msg);
}
};
InvokeOriginal();
if (Logs::DebugEnabled() || !Logs::started()) {
if (!Logs::WritingEntry()) {
// Sometimes Qt logs something inside our own logging.
LOG((msg));
}
}
});
}
uint64 Launcher::installationTag() const {
return InstallationTag;
}
void Launcher::processArguments() {
enum class KeyFormat {
NoValues,
OneValue,
AllLeftValues,
};
auto parseMap = std::map<QByteArray, KeyFormat> {
{ "-debug" , KeyFormat::NoValues },
{ "-key" , KeyFormat::OneValue },
{ "-autostart" , KeyFormat::NoValues },
{ "-fixprevious" , KeyFormat::NoValues },
{ "-cleanup" , KeyFormat::NoValues },
{ "-noupdate" , KeyFormat::NoValues },
{ "-tosettings" , KeyFormat::NoValues },
{ "-startintray" , KeyFormat::NoValues },
{ "-quit" , KeyFormat::NoValues },
{ "-sendpath" , KeyFormat::AllLeftValues },
{ "-workdir" , KeyFormat::OneValue },
{ "--" , KeyFormat::OneValue },
{ "-scale" , KeyFormat::OneValue },
};
auto parseResult = QMap<QByteArray, QStringList>();
auto parsingKey = QByteArray();
auto parsingFormat = KeyFormat::NoValues;
for (const auto &argument : std::as_const(_arguments)) {
switch (parsingFormat) {
case KeyFormat::OneValue: {
parseResult[parsingKey] = QStringList(argument.mid(0, 8192));
parsingFormat = KeyFormat::NoValues;
} break;
case KeyFormat::AllLeftValues: {
parseResult[parsingKey].push_back(argument.mid(0, 8192));
} break;
case KeyFormat::NoValues: {
parsingKey = argument.toLatin1();
auto it = parseMap.find(parsingKey);
if (it != parseMap.end()) {
parsingFormat = it->second;
parseResult[parsingKey] = QStringList();
}
} break;
}
}
gDebugMode = parseResult.contains("-debug");
gKeyFile = parseResult.value("-key", {}).join(QString()).toLower();
gKeyFile = gKeyFile.replace(QRegularExpression("[^a-z0-9\\-_]"), {});
gLaunchMode = parseResult.contains("-autostart") ? LaunchModeAutoStart
: parseResult.contains("-fixprevious") ? LaunchModeFixPrevious
: parseResult.contains("-cleanup") ? LaunchModeCleanup
: LaunchModeNormal;
gNoStartUpdate = parseResult.contains("-noupdate");
gStartToSettings = parseResult.contains("-tosettings");
gStartInTray = parseResult.contains("-startintray");
gQuit = parseResult.contains("-quit");
gSendPaths = parseResult.value("-sendpath", {});
cForceWorkingDir(parseResult.value("-workdir", {}).join(QString()));
if (!gWorkingDir.isEmpty()) {
_customWorkingDir = true;
}
gStartUrl = parseResult.value("--", {}).join(QString());
const auto scaleKey = parseResult.value("-scale", {});
if (scaleKey.size() > 0) {
using namespace style;
const auto value = scaleKey[0].toInt();
gConfigScale = ((value < kScaleMin) || (value > kScaleMax))
? kScaleAuto
: value;
}
}
int Launcher::executeApplication() {
FilteredCommandLineArguments arguments(_argc, _argv);
Sandbox sandbox(this, arguments.count(), arguments.values());
Ui::MainQueueProcessor processor;
base::ConcurrentTimerEnvironment environment;
return sandbox.start();
}
} // namespace Core
|