From b3bb1a537cf224884d8010eadbdee5060865fd97 Mon Sep 17 00:00:00 2001
From: Ilya Fedin <fedin-ilja2010@ya.ru>
Date: Sat, 8 Jan 2022 10:23:16 +0400
Subject: [PATCH] Use more sources for DE detection

---
 .../linux/linux_desktop_environment.cpp       | 49 +++++++++++++------
 1 file changed, 35 insertions(+), 14 deletions(-)

diff --git a/Telegram/SourceFiles/platform/linux/linux_desktop_environment.cpp b/Telegram/SourceFiles/platform/linux/linux_desktop_environment.cpp
index 5a060fd80..a7a66e963 100644
--- a/Telegram/SourceFiles/platform/linux/linux_desktop_environment.cpp
+++ b/Telegram/SourceFiles/platform/linux/linux_desktop_environment.cpp
@@ -7,6 +7,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 */
 #include "platform/linux/linux_desktop_environment.h"
 
+#include "base/platform/base_platform_info.h"
+
 namespace Platform {
 namespace DesktopEnvironment {
 namespace {
@@ -17,12 +19,20 @@ QString GetEnv(const char *name) {
 	return value;
 }
 
+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);
 
+	const auto xdgSessionDesktop = GetEnv("XDG_SESSION_DESKTOP").toLower();
+
 	const auto desktopSession = [] {
 		const auto result = GetEnv("DESKTOP_SESSION").toLower();
 		const auto slash = result.lastIndexOf('/');
@@ -33,33 +43,37 @@ std::vector<Type> Compute() {
 		return result;
 	}();
 
-	for (const auto &current : xdgCurrentDesktop) {
-		if (current == qstr("unity")) {
+	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
-			if (desktopSession.indexOf(qstr("gnome-fallback")) >= 0) {
+			if (desktopSession.contains(qstr("gnome-fallback"))) {
 				result.push_back(Type::Gnome);
 			}
 			result.push_back(Type::Unity);
-		} else if (current == qstr("gnome")) {
+		} else if (desktop == qstr("gnome")) {
 			result.push_back(Type::Gnome);
-		} else if (current == qstr("x-cinnamon")) {
+		} else if (desktop == qstr("x-cinnamon") || desktop == qstr("cinnamon")) {
 			result.push_back(Type::Cinnamon);
-		} else if (current == qstr("kde")) {
+		} else if (desktop == qstr("kde")) {
 			result.push_back(Type::KDE);
-		} else if (current == qstr("mate")) {
+		} else if (desktop == qstr("mate")) {
 			result.push_back(Type::MATE);
 		}
+	};
+
+	for (const auto &current : xdgCurrentDesktop) {
+		desktopToType(current);
+	}
+
+	if (!xdgSessionDesktop.isEmpty()) {
+		desktopToType(xdgSessionDesktop);
 	}
 
 	if (!desktopSession.isEmpty()) {
-		if (desktopSession == qstr("gnome")) {
-			result.push_back(Type::Gnome);
-		} else if (desktopSession == qstr("cinnamon")) {
-			result.push_back(Type::Cinnamon);
-		} else if (desktopSession == qstr("mate")) {
-			result.push_back(Type::MATE);
-		}
+		desktopToType(desktopSession);
 	}
 
 	// Fall back on some older environment variables.
@@ -71,6 +85,13 @@ std::vector<Type> Compute() {
 		result.push_back(Type::KDE);
 	}
 
+	// 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;
 }