Description: look for an X11 fallback session corresponding to the saved
 wayland session, in case the system cannot run wayland. I.e. "ubuntu" should
 fall back to "ubuntu-xorg" if it exists, not "gnome-xorg" just because it
 appeared first in the list of valid sessions.
Author: Olivier Tilloy <olivier.tilloy@canonical.com>
Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/gdm3/+bug/1718446

--- a/daemon/gdm-session.c
+++ b/daemon/gdm-session.c
@@ -87,6 +87,7 @@ struct _GdmSessionPrivate
         /* per open scope */
         char                *selected_program;
         char                *selected_session;
+        char                *discarded_session;
         char                *saved_session;
         char                *saved_language;
         char                *selected_user;
@@ -544,6 +545,16 @@ get_fallback_session_name (GdmSession *s
                 }
         }
 
+        if (self->priv->discarded_session != NULL) {
+                name = g_strconcat (self->priv->discarded_session, "-xorg", NULL);
+                if (get_session_command_for_name (self, name, NULL)) {
+                        g_free (self->priv->fallback_session_name);
+                        self->priv->fallback_session_name = name;
+                        goto out;
+                }
+        }
+        g_free (name);
+
         name = g_strdup ("gnome");
         if (get_session_command_for_name (self, name, NULL)) {
                 g_free (self->priv->fallback_session_name);
@@ -934,6 +945,11 @@ worker_on_saved_session_name_read (GdmDB
                 g_debug ("GdmSession: not using invalid .dmrc session: %s", session_name);
                 g_free (self->priv->saved_session);
                 self->priv->saved_session = NULL;
+                g_free (self->priv->fallback_session_name);
+                self->priv->fallback_session_name = NULL;
+                g_free (self->priv->discarded_session);
+                self->priv->discarded_session = g_strdup (session_name);
+                gdm_session_defaults_changed (self);
         } else if (strcmp (session_name,
                    get_default_session_name (self)) != 0) {
                 g_free (self->priv->saved_session);
@@ -2820,6 +2836,9 @@ do_reset (GdmSession *self)
         g_free (self->priv->saved_session);
         self->priv->saved_session = NULL;
 
+        g_free (self->priv->discarded_session);
+        self->priv->discarded_session = NULL;
+
         g_free (self->priv->saved_language);
         self->priv->saved_language = NULL;
 
@@ -3457,6 +3476,7 @@ gdm_session_finalize (GObject *object)
         g_free (self->priv->selected_user);
         g_free (self->priv->selected_session);
         g_free (self->priv->saved_session);
+        g_free (self->priv->discarded_session);
         g_free (self->priv->saved_language);
 
         g_free (self->priv->fallback_session_name);
