Subject: Re: [X2Go-Dev] Bug#774: please set DESKTOP_SESSION and XDG_CURRENT_DESKTOP like local sessions

From: Ryan Tandy <[email-masked]>
Date: Tue, 17 Mar 2015 12:11:51 -0700

Control: tags -1 + patch

Hi! Sorry it's taken me so long to get back to you about this.

On Thu, Feb 05, 2015 at 11:31:05AM +0000, Mike Gabriel wrote:
>On  Do 29 Jan 2015 19:17:40 CET, Ryan Tandy wrote:
>>I can write a patch that adds all this information to x2goruncommand 
>>in the existing format. However it occurs to me that it might make 
>>more sense to parse the files in /usr/share/xsessions and use the 
>>information directly instead of duplicating it. Please let me know 
>>which you'd prefer to see.
>
>
>x2goruncommand actually requires a complete rewrite (and has to be 
>based on freedesktop.org specs).
>
>Until that happens, let's hard-code this stuff. Please provide your patch.

Well, I started doing that, but it ended up being a lot less work (and a 
lot shorter code) to just take the settings from the session files. :)

Please consider the attached patch. It makes x2goruncommand behave more 
like GDM/LightDM for GNOME-ish sessions.

Testing GNOME Flashback on recent Ubuntu requires a couple of patches to 
Ubuntu packages, which I'm working on getting accepted. Details on 
Launchpad:

https://bugs.launchpad.net/ubuntu/+source/gnome-session/+bug/1251281/comments/46
https://launchpad.net/~rtandy/+archive/ubuntu/flashback/+packages

The gnome-session patch is applicable to Jessie too, but I think it's 
too late in the freeze to get it accepted now. :(

Many thanks for your work on X2Go!

-- 
Ryan Tandy - Programmer/Analyst                        [email-masked]
School District 63 (Saanich)                                250-652-7385
>From 9d84e88f7a7f4b0d47bfcf933796b2ca7cfd179f Mon Sep 17 00:00:00 2001
From: Ryan Tandy <[email-masked]>
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 | 137 ++++++++++++------------------------------
 1 file changed, 39 insertions(+), 98 deletions(-)

diff --git a/x2goserver/bin/x2goruncommand b/x2goserver/bin/x2goruncommand
index ac7ff7f..c28ada1 100755
--- a/x2goserver/bin/x2goruncommand
+++ b/x2goserver/bin/x2goruncommand
@@ -83,108 +83,48 @@ 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.
+			export XDG_CURRENT_DESKTOP="$(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 "$XDG_CURRENT_DESKTOP" ]; then
+				export XDG_CURRENT_DESKTOP="$(sed -n 's/^X-LightDM-DesktopName=//p' "$session_desktop")"
+			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 +233,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