tdesktop/Telegram/SourceFiles/platform/linux/linux_desktop_environment.cpp

130 lines
3.4 KiB
C++
Raw Normal View History

/*
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 "platform/linux/linux_desktop_environment.h"
2022-01-08 06:23:16 +00:00
#include "base/platform/base_platform_info.h"
namespace Platform {
namespace DesktopEnvironment {
namespace {
QString GetEnv(const char *name) {
const auto value = qEnvironmentVariable(name);
LOG(("Getting DE, %1: '%2'").arg(name, value));
return value;
}
2022-01-08 06:23:16 +00:00
QString GetWM() {
const auto value = base::Platform::GetWindowManager();
LOG(("Getting WM: '%1'").arg(value));
return value;
}
std::vector<Type> Compute() {
auto result = std::vector<Type>();
const auto xdgCurrentDesktop = GetEnv(
"XDG_CURRENT_DESKTOP").toLower().split(':', Qt::SkipEmptyParts);
2022-01-08 06:23:16 +00:00
const auto xdgSessionDesktop = GetEnv("XDG_SESSION_DESKTOP").toLower();
const auto desktopSession = [] {
const auto result = GetEnv("DESKTOP_SESSION").toLower();
const auto slash = result.lastIndexOf('/');
// DESKTOP_SESSION can contain a path
if (slash != -1) {
return result.mid(slash + 1);
}
return result;
}();
2022-01-08 06:23:16 +00:00
const auto windowManager = GetWM().toLower();
const auto desktopToType = [&](const QString &desktop) {
if (desktop == qstr("unity")) {
// gnome-fallback sessions set XDG_CURRENT_DESKTOP to Unity
// DESKTOP_SESSION can be gnome-fallback or gnome-fallback-compiz
2022-01-08 06:23:16 +00:00
if (desktopSession.contains(qstr("gnome-fallback"))) {
result.push_back(Type::Gnome);
}
result.push_back(Type::Unity);
2022-01-08 06:23:16 +00:00
} else if (desktop == qstr("gnome")) {
result.push_back(Type::Gnome);
2022-01-08 06:23:16 +00:00
} else if (desktop == qstr("x-cinnamon") || desktop == qstr("cinnamon")) {
result.push_back(Type::Cinnamon);
2022-01-08 06:23:16 +00:00
} else if (desktop == qstr("kde")) {
result.push_back(Type::KDE);
2022-01-08 06:23:16 +00:00
} else if (desktop == qstr("mate")) {
result.push_back(Type::MATE);
}
2022-01-08 06:23:16 +00:00
};
for (const auto &current : xdgCurrentDesktop) {
desktopToType(current);
}
if (!xdgSessionDesktop.isEmpty()) {
desktopToType(xdgSessionDesktop);
}
if (!desktopSession.isEmpty()) {
2022-01-08 06:23:16 +00:00
desktopToType(desktopSession);
}
// Fall back on some older environment variables.
// Useful particularly in the DESKTOP_SESSION=default case.
if (!GetEnv("GNOME_DESKTOP_SESSION_ID").isEmpty()) {
result.push_back(Type::Gnome);
}
if (!GetEnv("KDE_FULL_SESSION").isEmpty()) {
result.push_back(Type::KDE);
}
2022-01-08 06:23:16 +00:00
// Some DEs could be detected via X11
if (!windowManager.isEmpty()) {
if (windowManager == qstr("gnome shell")) {
result.push_back(Type::Gnome);
}
}
ranges::unique(result);
return result;
}
std::vector<Type> ComputeAndLog() {
const auto result = Compute();
if (result.empty()) {
return {};
}
const auto names = ranges::accumulate(
result | ranges::views::transform([](auto type) {
switch (type) {
case Type::Gnome: return qsl("Gnome, ");
case Type::Cinnamon: return qsl("Cinnamon, ");
case Type::KDE: return qsl("KDE, ");
case Type::Unity: return qsl("Unity, ");
case Type::MATE: return qsl("MATE, ");
}
Unexpected("Type in Platform::DesktopEnvironment::ComputeAndLog");
}),
QString()).chopped(2);
LOG(("DE: %1").arg(names));
return result;
}
} // namespace
// Thanks Chromium.
std::vector<Type> Get() {
static const auto result = ComputeAndLog();
return result;
}
} // namespace DesktopEnvironment
} // namespace Platform