Blob Blame History Raw
From 23c95cf92b69ea69ff15ec8eac88859669ad666a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Esser?= <besser82@fedoraproject.org>
Date: Thu, 13 Apr 2017 21:11:26 +0200
Subject: [PATCH] Add $(ENV:YUI_PREFERED_BACKEND) to set prefered UI-backend

---
 src/YUILoader.cc | 93 ++++++++++++++++++++++++++++++++++----------------------
 src/YUILoader.h  | 27 ++++++++++++++--
 2 files changed, 81 insertions(+), 39 deletions(-)

diff --git a/src/YUILoader.cc b/src/YUILoader.cc
index 7933ae4..2be22c2 100644
--- a/src/YUILoader.cc
+++ b/src/YUILoader.cc
@@ -42,11 +42,29 @@
 void YUILoader::loadUI( bool withThreads )
 {
     bool isGtk = false;
-    const char * envDisplay = getenv( "DISPLAY" );
-    const char * envDesktop = getenv( "XDG_CURRENT_DESKTOP" );
+    const char * envDesktop;
+    const char * envDisplay;
+    const char * envPreset;
     std::string wantedGUI;
 
-    yuiMilestone () << "XDG_CURRENT_DESKTOP: \"" << envDesktop << "\"" << std::endl;
+    if( getenv( "DISPLAY" ) )
+	envDisplay = getenv( "DISPLAY" );
+    else
+	envDisplay = "";
+
+    if( getenv( "XDG_CURRENT_DESKTOP" ) )
+	envDesktop = getenv( "XDG_CURRENT_DESKTOP" );
+    else
+	envDesktop = "";
+
+    if( getenv( "YUI_PREFERED_BACKEND" ) )
+	envPreset = getenv( "YUI_PREFERED_BACKEND" );
+    else
+	envPreset = "";
+
+    yuiMilestone () << "DISPLAY: \""              << envDisplay << "\"" << std::endl;
+    yuiMilestone () << "XDG_CURRENT_DESKTOP: \""  << envDesktop << "\"" << std::endl;
+    yuiMilestone () << "YUI_PREFERED_BACKEND: \"" << envPreset  << "\"" << std::endl;
 
     // Taken from: https://specifications.freedesktop.org/menu-spec/menu-spec-1.1.html#onlyshowin-registry
     isGtk = ( ( strstr( envDesktop, "Cinnamon" ) != NULL ) || isGtk );
@@ -58,61 +76,62 @@ void YUILoader::loadUI( bool withThreads )
     isGtk = ( ( strstr( envDesktop, "Unity"    ) != NULL ) || isGtk );
     isGtk = ( ( strstr( envDesktop, "XFCE"     ) != NULL ) || isGtk );
 
-    if( isGtk )
-    {
-	yuiMilestone () << "Detected a Gtk-based desktop environment." << std::endl;
-	yuiMilestone () << "Prefering Gtk-UI if available." << std::endl;
-    }
+    if( isGtk ) yuiMilestone () << "Detected a Gtk-based desktop environment." << std::endl
+                                << "Prefering Gtk-UI if available and no" << std::endl
+                                << "user-selected override is present." << std::endl;
 
     YCommandLine cmdline;
 
-    bool wantNcurses = cmdline.find("--ncurses") != -1;
-    if( wantNcurses )
-	yuiMilestone () << "Using UI-backend: \"" << YUIPlugin_NCurses << "\". Forced on command-line." << std::endl;
-    bool wantQt = cmdline.find("--qt") != -1;
-    if( wantQt )
-	yuiMilestone () << "Using UI-backend: \"" << YUIPlugin_Qt << "\". Forced on command-line." << std::endl;
-    bool wantGtk = cmdline.find("--gtk") != -1;
-    if( wantGtk )
-	yuiMilestone () << "Using UI-backend: \"" << YUIPlugin_Gtk << "\". Forced on command-line." << std::endl;
+    bool wantGtk     = ( cmdline.find( "--gtk" )     != -1 );
+    bool wantNcurses = ( cmdline.find( "--ncurses" ) != -1 );
+    bool wantQt      = ( cmdline.find( "--qt" )      != -1 );
+    wantGtk     = ( wantGtk     || ( strcmp( envPreset, YUIPlugin_Gtk )     == 0 ) ) && !( wantNcurses || wantQt );
+    wantNcurses = ( wantNcurses || ( strcmp( envPreset, YUIPlugin_NCurses ) == 0 ) ) && !( wantGtk     || wantQt );
+    wantQt      = ( wantQt      || ( strcmp( envPreset, YUIPlugin_Qt )      == 0 ) ) && !( wantGtk     || wantNcurses );
+
+    if( wantGtk )     wantedGUI = YUIPlugin_Gtk;
+    if( wantNcurses ) wantedGUI = YUIPlugin_NCurses;
+    if( wantQt )      wantedGUI = YUIPlugin_Qt;
+
+    yuiMilestone () << "User-selected UI-plugin: \"" << wantedGUI << "\"" << std::endl;
 
-    bool haveQt = pluginExists( YUIPlugin_Qt );
-    bool haveGtk = pluginExists( YUIPlugin_Gtk );
+    bool haveGtk     = pluginExists( YUIPlugin_Gtk );
+    bool haveNcurses = pluginExists( YUIPlugin_NCurses );
+    bool haveQt      = pluginExists( YUIPlugin_Qt );
 
-    if ( envDisplay && !wantNcurses )
+    if ( ( haveGtk || haveQt ) && strcmp ( envDisplay, "" ) &&
+	 ( !wantNcurses || !isatty( STDOUT_FILENO ) ) )
     {
 	// Qt is default if available.
 	if ( haveQt )
-	   wantedGUI = YUIPlugin_Qt;
+	    wantedGUI = YUIPlugin_Qt;
 
 	// Do we want to use Gtk instead?
-	if ( haveGtk && ( isGtk || wantGtk ) && !wantQt )
-	   wantedGUI = YUIPlugin_Gtk;
+	if ( haveGtk && ( ( ( isGtk || wantGtk ) && !wantQt ) || !haveQt ) )
+	    wantedGUI = YUIPlugin_Gtk;
 
-	if ( strcmp( wantedGUI.c_str(), "" ) )
+	yuiMilestone () << "Using UI-plugin: \"" << wantedGUI << "\""<< std::endl;
+
+	try
 	{
-	   yuiMilestone () << "Using UI-backend: \"" << wantedGUI << "\""<< std::endl;
-	   try
-	   {
-	      YSettings::loadedUI( wantedGUI, true );
-	      loadPlugin( wantedGUI, withThreads );
-	      return;
-	   }
-	   catch ( YUIException & ex )
-	   {
-	      YUI_CAUGHT( ex );
-	   }
+	    YSettings::loadedUI( wantedGUI, true );
+	    loadPlugin( wantedGUI, withThreads );
+	    return;
+	}
+	catch ( YUIException & ex )
+	{
+	    YUI_CAUGHT( ex );
 	}
     }
 
-    if ( isatty( STDOUT_FILENO ) )
+    if ( haveNcurses && isatty( STDOUT_FILENO ) )
     {
 	//
 	// NCurses UI
 	//
 
 	wantedGUI = YUIPlugin_NCurses;
-	yuiMilestone () << "Using UI-backend: \"" << wantedGUI << "\""<< std::endl;
+	yuiMilestone () << "Using UI-plugin: \"" << wantedGUI << "\""<< std::endl;
 
 	try
 	{
diff --git a/src/YUILoader.h b/src/YUILoader.h
index 1086b65..dfbb50c 100644
--- a/src/YUILoader.h
+++ b/src/YUILoader.h
@@ -46,9 +46,32 @@ class YUILoader
 {
 public:
     /**
-     * Load any of the available UI plug-ins in this order:
+     * Load any of the available UI-plugins in this order:
      * - Qt      if $DISPLAY is set
-     * - NCurses if stdout is a tty
+     * - Gtk     if $DISPLAY is set and Qt is not
+     *           available or a GTK-based desktop
+     *           environment is detected from the
+     *           environment variable XDG_CURRENT_DESKTOP
+     * - NCurses if stdout is a TTY or Qt and Gtk are not
+     *           available
+     *
+     * This behaviour can be overridden by either
+     * - specifing one of the switches '--gtk', '--ncurses'
+     *   or '--qt' on the command-line of the program, or
+     * - setting the environment variable YUI_PREFERED_BACKEND
+     *   to one of 'gtk', 'ncurses' or 'qt'.
+     *
+     * If a command-line switch is given to the program, the
+     * setting from the environment variable will be overridden
+     * by the UI-plugin chosen with the switch.
+     *
+     * If the user-selected UI-plugin is not installed on the
+     * system, an installed UI-plugin will be chosen in the
+     * order as stated above.
+     *
+     * The same applies if a needed pre-condition for a specific
+     * UI-plugin (no $DISPLAY for Gtk and Qt or no TTY for NCurses)
+     * is not met.
      **/
     static void loadUI( bool withThreads = false );