Rework DE detection

Variables can point to a mixed environment, make DE detection non-exclusive.
Remove unused methods.
This commit is contained in:
Ilya Fedin 2022-01-06 23:24:21 +04:00 committed by John Preston
parent ba6c3eaf73
commit 726aa3316d
3 changed files with 76 additions and 123 deletions

View file

@ -12,110 +12,94 @@ namespace DesktopEnvironment {
namespace { namespace {
QString GetEnv(const char *name) { QString GetEnv(const char *name) {
auto value = qEnvironmentVariable(name); const auto value = qEnvironmentVariable(name);
LOG(("Getting DE, %1: '%2'").arg(name, value)); LOG(("Getting DE, %1: '%2'").arg(name, value));
return value; return value;
} }
Type Compute() { std::vector<Type> Compute() {
auto xdgCurrentDesktop = GetEnv("XDG_CURRENT_DESKTOP").toLower(); auto result = std::vector<Type>();
auto list = xdgCurrentDesktop.split(':', Qt::SkipEmptyParts);
auto desktopSession = GetEnv("DESKTOP_SESSION").toLower();
auto slash = desktopSession.lastIndexOf('/');
auto kdeSession = GetEnv("KDE_SESSION_VERSION");
// DESKTOP_SESSION can contain a path const auto xdgCurrentDesktop = GetEnv(
if (slash != -1) { "XDG_CURRENT_DESKTOP").toLower().split(':', Qt::SkipEmptyParts);
desktopSession = desktopSession.mid(slash + 1);
}
if (!list.isEmpty()) { const auto desktopSession = [] {
if (list.contains("unity")) { 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;
}();
for (const auto &current : xdgCurrentDesktop) {
if (current == qstr("unity")) {
// gnome-fallback sessions set XDG_CURRENT_DESKTOP to Unity // gnome-fallback sessions set XDG_CURRENT_DESKTOP to Unity
// DESKTOP_SESSION can be gnome-fallback or gnome-fallback-compiz // DESKTOP_SESSION can be gnome-fallback or gnome-fallback-compiz
if (desktopSession.indexOf(qstr("gnome-fallback")) >= 0) { if (desktopSession.indexOf(qstr("gnome-fallback")) >= 0) {
return Type::Gnome; result.push_back(Type::Gnome);
} }
return Type::Unity; result.push_back(Type::Unity);
} else if (list.contains("xfce")) { } else if (current == qstr("gnome")) {
return Type::XFCE; result.push_back(Type::Gnome);
} else if (list.contains("gnome")) { } else if (current == qstr("x-cinnamon")) {
return Type::Gnome; result.push_back(Type::Cinnamon);
} else if (list.contains("x-cinnamon")) { } else if (current == qstr("kde")) {
return Type::Cinnamon; result.push_back(Type::KDE);
} else if (list.contains("kde")) { } else if (current == qstr("mate")) {
if (kdeSession == qstr("5")) { result.push_back(Type::MATE);
return Type::KDE5;
}
return Type::KDE4;
} else if (list.contains("mate")) {
return Type::MATE;
} else if (list.contains("lxde")) {
return Type::LXDE;
} }
} }
if (!desktopSession.isEmpty()) { if (!desktopSession.isEmpty()) {
if (desktopSession == qstr("gnome")) { if (desktopSession == qstr("gnome")) {
return Type::Gnome; result.push_back(Type::Gnome);
} else if (desktopSession == qstr("cinnamon")) { } else if (desktopSession == qstr("cinnamon")) {
return Type::Cinnamon; result.push_back(Type::Cinnamon);
} else if (desktopSession == qstr("kde4") || desktopSession == qstr("kde-plasma")) {
return Type::KDE4;
} else if (desktopSession == qstr("kde")) {
// This may mean KDE4 on newer systems, so we have to check.
if (!kdeSession.isEmpty()) {
return Type::KDE4;
}
return Type::KDE3;
} else if (desktopSession == qstr("xfce")) {
return Type::XFCE;
} else if (desktopSession == qstr("mate")) { } else if (desktopSession == qstr("mate")) {
return Type::MATE; result.push_back(Type::MATE);
} else if (desktopSession == qstr("lxde")) {
return Type::LXDE;
} }
} }
// Fall back on some older environment variables. // Fall back on some older environment variables.
// Useful particularly in the DESKTOP_SESSION=default case. // Useful particularly in the DESKTOP_SESSION=default case.
if (!GetEnv("GNOME_DESKTOP_SESSION_ID").isEmpty()) { if (!GetEnv("GNOME_DESKTOP_SESSION_ID").isEmpty()) {
return Type::Gnome; result.push_back(Type::Gnome);
} else if (!GetEnv("KDE_FULL_SESSION").isEmpty()) { }
if (!kdeSession.isEmpty()) { if (!GetEnv("KDE_FULL_SESSION").isEmpty()) {
return Type::KDE4; result.push_back(Type::KDE);
}
return Type::KDE3;
} }
return Type::Other; ranges::unique(result);
return result;
} }
Type ComputeAndLog() { std::vector<Type> ComputeAndLog() {
auto result = Compute(); const auto result = Compute();
auto name = [result]() -> QString { if (result.empty()) {
switch (result) { return {};
case Type::Other: return "Other"; }
case Type::Gnome: return "Gnome"; const auto names = ranges::accumulate(
case Type::Cinnamon: return "Cinnamon"; result | ranges::views::transform([](auto type) {
case Type::KDE3: return "KDE3"; switch (type) {
case Type::KDE4: return "KDE4"; case Type::Gnome: return qsl("Gnome, ");
case Type::KDE5: return "KDE5"; case Type::Cinnamon: return qsl("Cinnamon, ");
case Type::Unity: return "Unity"; case Type::KDE: return qsl("KDE, ");
case Type::XFCE: return "XFCE"; case Type::Unity: return qsl("Unity, ");
case Type::MATE: return "MATE"; case Type::MATE: return qsl("MATE, ");
case Type::LXDE: return "LXDE"; }
} Unexpected("Type in Platform::DesktopEnvironment::ComputeAndLog");
return QString::number(static_cast<int>(result)); }),
}; QString()).chopped(2);
LOG(("DE: %1").arg(name())); LOG(("DE: %1").arg(names));
return result; return result;
} }
} // namespace } // namespace
// Thanks Chromium. // Thanks Chromium.
Type Get() { std::vector<Type> Get() {
static const auto result = ComputeAndLog(); static const auto result = ComputeAndLog();
return result; return result;
} }

View file

@ -11,67 +11,33 @@ namespace Platform {
namespace DesktopEnvironment { namespace DesktopEnvironment {
enum class Type { enum class Type {
Other,
Gnome, Gnome,
Cinnamon, Cinnamon,
KDE3, KDE,
KDE4,
KDE5,
Unity, Unity,
XFCE,
MATE, MATE,
LXDE,
}; };
Type Get(); std::vector<Type> Get();
inline bool IsGnome() { inline bool IsGnome() {
return Get() == Type::Gnome; return ranges::contains(Get(), Type::Gnome);
} }
inline bool IsCinnamon() { inline bool IsCinnamon() {
return Get() == Type::Cinnamon; return ranges::contains(Get(), Type::Cinnamon);
}
inline bool IsKDE3() {
return Get() == Type::KDE3;
}
inline bool IsKDE4() {
return Get() == Type::KDE4;
}
inline bool IsKDE5() {
return Get() == Type::KDE5;
} }
inline bool IsKDE() { inline bool IsKDE() {
return IsKDE3() || IsKDE4() || IsKDE5(); return ranges::contains(Get(), Type::KDE);
} }
inline bool IsUnity() { inline bool IsUnity() {
return Get() == Type::Unity; return ranges::contains(Get(), Type::Unity);
}
inline bool IsXFCE() {
return Get() == Type::XFCE;
} }
inline bool IsMATE() { inline bool IsMATE() {
return Get() == Type::MATE; return ranges::contains(Get(), Type::MATE);
}
inline bool IsLXDE() {
return Get() == Type::LXDE;
}
inline bool IsGtkBased() {
return IsGnome()
|| IsCinnamon()
|| IsUnity()
|| IsMATE()
|| IsXFCE()
|| IsLXDE();
} }
} // namespace DesktopEnvironment } // namespace DesktopEnvironment

View file

@ -816,17 +816,20 @@ bool OpenSystemSettings(SystemSettingsType type) {
} }
options.push_back(std::move(command)); options.push_back(std::move(command));
}; };
if (DesktopEnvironment::IsUnity()) { for (const auto &type : DesktopEnvironment::Get()) {
add("unity-control-center", "sound"); using DesktopEnvironment::Type;
} else if (DesktopEnvironment::IsKDE()) { if (type == Type::Unity) {
add("kcmshell5", "kcm_pulseaudio"); add("unity-control-center", "sound");
add("kcmshell4", "phonon"); } else if (type == Type::KDE) {
} else if (DesktopEnvironment::IsGnome()) { add("kcmshell5", "kcm_pulseaudio");
add("gnome-control-center", "sound"); add("kcmshell4", "phonon");
} else if (DesktopEnvironment::IsCinnamon()) { } else if (type == Type::Gnome) {
add("cinnamon-settings", "sound"); add("gnome-control-center", "sound");
} else if (DesktopEnvironment::IsMATE()) { } else if (type == Type::Cinnamon) {
add("mate-volume-control"); add("cinnamon-settings", "sound");
} else if (type == Type::MATE) {
add("mate-volume-control");
}
} }
#ifdef __HAIKU__ #ifdef __HAIKU__
add("Media"); add("Media");