From e184e85cbf28928864fd3d935bced28295e7624f Mon Sep 17 00:00:00 2001
From: Ryan Tandy <rtandy@sd63.bc.ca>
Date: Tue, 17 Mar 2015 10:39:29 -0700
Subject: [PATCH] Launch gnome-session using settings from the session file.

Taking the settings from the session file should be more reliable and
future-proof than hard-coding them. This makes X2Go behave more like GDM
or LightDM in several cases.

Setting DESKTOP_SESSION and XDG_CURRENT_DESKTOP correctly allows Upstart
to manage the user session on Ubuntu 13.10 and later. That in turn means
GTK_MODULES no longer has to be set manually, since there is an Upstart
session job that sets it.

Passing the startup command as an argument to the session wrapper is
necessary on Ubuntu 15.04 and later.
---
 x2goserver/bin/x2goruncommand | 140 +++++++++++++-----------------------------
 1 file changed, 42 insertions(+), 98 deletions(-)

diff --git a/x2goserver/bin/x2goruncommand b/x2goserver/bin/x2goruncommand
index ac7ff7f..f35d159 100755
--- a/x2goserver/bin/x2goruncommand
+++ b/x2goserver/bin/x2goruncommand
@@ -83,108 +83,51 @@ elif [ "$sndsys" == "arts" ]; then
 	export ARTS_SERVER="localhost:$4"
 fi
 
-# detect Ubuntu version via /etc/lsb-release (not supported by Debian)
-if [ -e "/etc/lsb-release" ]; then
-	source /etc/lsb-release
-fi
-
 # let x2goruncommand choose what command to use for a given desktop shell name (GNOME, UNITY, KDE, XFCE4, LXDE, TRINITY, MATE, OPENBOX, ICEWM)
 
-# NOTES on GNOME startup behaviour in different distributions
-# ===========================================================
-
-# Debian and every other non-Ubuntu distro launchs GNOME3 (in accelerated mode) when calling gnome-session
-
-# Ubuntu launches Unity (in accelerated mode) when calling gnome-session.
-# To make the GNOME variants start up properly on Ubuntu, we have to make the following differentiations
-
-# Ubuntu 10.10 and earlier (maverick):
-#		 GNOME -> gnome-session
-#		 (would start GNOME2)
-# Ubuntu 11.04 (natty):
-#		 GNOME -> gnome-session --session=2d-gnome
-#		 UNITY -> gnome-session --session=2d-ubuntu
-#		 (GNOME3 based desktop shells)
-# Ubuntu 11.10 (oneiric) & 12.04 (precise):
-#		 GNOME -> gnome-session --session=gnome-fallback
-#		 UNITY -> gnome-session --session=ubuntu-2d
-#		 (GNOME3 based desktop shells)
-# Ubuntu 12.10 (quantal):
-#		 GNOME -> gnome-session --session=gnome-fallback
-#		 UNITY -> gnome-session --session=ubuntu
-#		 (GNOME3 based desktop shells)
-# Ubuntu 13.10 (raring) and later:
-#		 GNOME -> gnome-session --session=gnome-flashback
-#		 UNITY -> gnome-session --session=ubuntu
-#		 (GNOME3 based desktop shells)
-#		 Additionally, $GTK_MODULES must include "unity-gtk-module".
-#		 $GTK_MODULES does not need tha value for any other distro
-#                or any earlier release of Ubuntu.
-#
-#		 The logic for launching GNOME should be generic enough
-#                to work with every other distro.
-#
-#		 Also, it appears that some Linux GNOME2 distros need DESKTOP_SESSION="gnome"
-#                while others do not.
+# Use the gnome-session logic to launch a Unity 2D or Cinnamon 2D session.
+# Fall back to GNOME if the chosen session isn't available.
+if [ "$cmd" == "UNITY" ] || [ "$cmd" == "unity" ]; then
+	x2go_xsession=ubuntu-2d
+	cmd="gnome-session"
+elif [ "$cmd" == "CINNAMON" ] || [ "$cmd" == "cinnamon" ]; then
+	x2go_xsession=cinnamon2d
+	cmd="gnome-session"
+fi
 
 if [ "$cmd" == "GNOME" ] || [ "$cmd" == "gnome-session" ]; then
-	cmd="/usr/bin/gnome-session"
-	if [ "$DISTRIB_ID" == "Ubuntu" ] && [ "$(echo "$DISTRIB_RELEASE >= 13.10" | bc)" == "1" ]; then
-		export DESKTOP_SESSION="gnome-flashback"
-		if [ -z "$GTK_MODULES" ] ; then
-			export GTK_MODULES="unity-gtk-module"
-		else
-			export GTK_MODULES="$GTK_MODULES:unity-gtk-module"
+	# Try to pick an appropriate session file, and take settings from it.
+	for session in "$x2go_xsession" gnome-flashback-metacity gnome-fallback gnome-2d gnome; do
+		session_desktop=/usr/share/xsessions/"$session".desktop
+		if [ -f "$session_desktop" ]; then
+			# Run the command specified in the session file.
+			cmd="$(sed -n 's/^Exec=//p' "$session_desktop")"
+
+			# Use gnome-session's --disable-acceleration-check option if available.
+			# The Exec= line might already include it. That's OK.
+			if $cmd --help | grep -q -- --disable-acceleration-check; then
+				cmd="$cmd --disable-acceleration-check"
+			fi
+
+			# Set DESKTOP_SESSION and GDMSESSION to the base name of the session file.
+			export DESKTOP_SESSION="$session"
+			export GDMSESSION="$session"
+
+			# Try to set a reasonable XDG_CURRENT_DESKTOP.
+			# gnome-session will overwrite this, but on recent Ubuntu, some helpers are started by upstart instead of gnome-session.
+			# DesktopNames field is ;-separated. XDG_CURRENT_DESKTOP should be :-separated.
+			desktopname="$(sed -n '/^DesktopNames=/ { s/^DesktopNames=//; s/;/:/g; s/:$//; p; }' "$session_desktop")"
+			# Before DesktopNames was standardized, a few Ubuntu releases used X-LightDM-DesktopName.
+			if [ -z "$desktopname" ]; then
+				desktopname="$(sed -n 's/^X-LightDM-DesktopName=//p' "$session_desktop")"
+			fi
+			# Only export the variable if set by some session file.
+			if [ -n "$desktopname" ]; then
+				export XDG_CURRENT_DESKTOP="$desktopname"
+			fi
+			break
 		fi
-		args=" --session=$DESKTOP_SESSION"
-        elif [ -e /usr/share/gnome-session/sessions/gnome-flashback.session ]; then
-                export DESKTOP_SESSION="gnome-flashback"
-                args=" --session=$DESKTOP_SESSION"
-        elif [ -e /usr/share/gnome-session/sessions/gnome-fallback.session ]; then
-                export DESKTOP_SESSION="gnome-fallback"
-                args=" --session=$DESKTOP_SESSION"
-        elif [ -e /usr/share/gnome-session/sessions/2d-gnome.session ]; then
-                export DESKTOP_SESSION="2d-gnome"
-                args=" --session=$DESKTOP_SESSION"
-        elif [ "$DISTRIB_ID" == "Ubuntu" ] && [ "$(echo "$DISTRIB_RELEASE <= 10.10" | bc)" == "1" ]; then
-                export DESKTOP_SESSION="gnome"
-	elif cat /etc/debian_version | egrep "^(squeeze|6\.).*" >/dev/null; then
-                export DESKTOP_SESSION="gnome"
-        fi
-
-elif ([ "$cmd" == "UNITY" ] || [ "$cmd" == "unity" ]); then
-	cmd="/usr/bin/gnome-session"
-	if [ "$DISTRIB_ID" == "Ubuntu" ] && [ "$(echo "$DISTRIB_RELEASE >= 12.10" | bc)" == "1" ]; then
-		export DESKTOP_SESSION="ubuntu"
-		args=" --session=$DESKTOP_SESSION"
-	elif [ "$DISTRIB_ID" == "Ubuntu" ] && [ "$(echo "$DISTRIB_RELEASE == 11.10" | bc)" == "1" -o "$(echo "$DISTRIB_RELEASE == 12.04" | bc)" == "1" ]; then
-		export DESKTOP_SESSION="ubuntu-2d"
-		args=" --session=$DESKTOP_SESSION"
-	elif [ "$DISTRIB_ID" == "Ubuntu" ] && [ "$(echo "$DISTRIB_RELEASE == 11.04" | bc)" == "1" ]; then
-		export DESKTOP_SESSION="2d-ubuntu"
-		args=" --session=$DESKTOP_SESSION"
-	fi
-	# on earlier Ubuntu versions or with non-Ubuntu Distros the ,,UNITY'' command in X2Go will launch the GNOME2 desktop shell
-
-elif ([ "$cmd" == "CINNAMON" ] || [ "$cmd" == "cinnamon" ]); then
-	# Cinnamon 2.0 and newer
-	# The cmd is a script that calls the "cinnamon-session" binary.
-	if [ -e /usr/bin/cinnamon-session-cinnamon2d ]; then
-		cmd="/usr/bin/cinnamon-session-cinnamon2d"
-	# Cinnamon 1.6 & 1.8
-	# The cmd is a script that calls the "gnome-session" binary.
-	elif [ -e /usr/bin/gnome-session-cinnamon2d ]; then
-		cmd="/usr/bin/gnome-session-cinnamon2d"
-	# Cinnamon 1.4
-	# The cmd is a script that calls the "gnome-session" binary.
-	elif [ -e /usr/bin/gnome-session-cinnamon ]; then
-		cmd="/usr/bin/gnome-session-cinnamon"
-	# Fallback to the old behavior.
-	else
-		cmd="/usr/bin/gnome-session"
-		export DESKTOP_SESSION="cinnamon2d"
-		args=" --session=$DESKTOP_SESSION"
-	fi
+	done
 
 elif [ "$cmd" == "MATE" ]; then
 	cmd="/usr/bin/mate-session"
@@ -293,7 +236,8 @@ if [ "$EXEC" != "" ] && [ -x "$EXEC" ]; then
 	if x2gofeature X2GO_XSESSION &>/dev/null && [ "x$X2GO_SESS_TYPE" = "xD" ]; then
 		STARTUP="$cmd$args"
 		"$X2GO_LIB_PATH/x2gosyslog" "$0" "notice" "launching session with Xsession-x2go mechanism, using STARTUP=\"$STARTUP\""
-		XSESSION_EXEC="$cmd" STARTUP="/usr/bin/env LD_LIBRARY_PATH=${LD_LIBRARY_PATH} ${STARTUP}" /etc/x2go/Xsession
+		# LightDM passes the session command as an argument to the session wrapper. Some Ubuntu Xsession.d scripts depend on that.
+		XSESSION_EXEC="$cmd" STARTUP="/usr/bin/env LD_LIBRARY_PATH=${LD_LIBRARY_PATH} ${STARTUP}" /etc/x2go/Xsession "$cmd"
 	else
 		"$X2GO_LIB_PATH/x2gosyslog" "$0" "debug" "executing command \"$cmd$args\"..."
 
-- 
1.9.1

