G4UIExecutive.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 // $Id: G4UIExecutive.icc,v 1.7 2010-05-28 08:12:27 kmura Exp $
00026 // GEANT4 tag $Name: geant4-09-04-patch-02 $
00027 
00028 #include "G4UIExecutive.hh"
00029 #include "G4UIsession.hh"
00030 #include "G4UImanager.hh"
00031 
00032 #if defined(G4UI_BUILD_QT_SESSION)
00033 #include "G4UIQt.hh"
00034 #include "G4Qt.hh"
00035 #endif
00036 
00037 #if defined(G4UI_BUILD_XM_SESSION)
00038 #include "G4UIXm.hh"
00039 #endif
00040 
00041 #if defined(G4UI_BUILD_WIN32_SESSION)
00042 #include "G4UIWin32.hh"
00043 #endif
00044 
00045 #if defined(G4UI_BUILD_WT_SESSION)
00046 //#include "G4UIWt.hh"
00047 #endif
00048 
00049 #include "G4UIGAG.hh"
00050 #include "G4UIterminal.hh"
00051 #include "G4UItcsh.hh"
00052 #include "G4UIcsh.hh"
00053 
00054 // --------------------------------------------------------------------------
00055 // build flags as variables
00056 
00057 #if defined(G4UI_BUILD_QT_SESSION)
00058 static const G4bool qt_build = true;
00059 #else
00060 static const G4bool qt_build = false;
00061 #endif
00062 
00063 #if defined(G4UI_BUILD_XM_SESSION)
00064 static const G4bool xm_build = true;
00065 #else
00066 static const G4bool xm_build = false;
00067 #endif
00068 
00069 #if defined(G4UI_BUILD_WIN32_SESSION)
00070 static const G4bool win32_build = true;
00071 #else
00072 static const G4bool win32_build = false;
00073 #endif
00074 
00075 #if defined(G4UI_BUILD_WT_SESSION)
00076 static const G4bool wt_build = true;
00077 #else
00078 static const G4bool wt_build = false;
00079 #endif
00080 
00081 #ifndef WIN32
00082 static const G4bool tcsh_build = true;
00083 #else
00084 static const G4bool tcsh_build = false;
00085 #endif
00086 
00087 #define DISCARD_PARAMETER(p) (void)p
00088 
00089 // --------------------------------------------------------------------------
00090 G4UIExecutive::G4UIExecutive(G4int argc, char** argv, const G4String& type)
00091   : selected(kNone), session(NULL), shell(NULL), isGUI(false)
00092 {
00093   G4cout << "Available UI session types: [ ";
00094   if ( qt_build ) G4cout << "Qt, ";
00095   if ( xm_build ) G4cout << "Xm, ";
00096   if ( win32_build) G4cout << "Win32, ";
00097   if ( wt_build ) G4cout << "Wt, ";
00098   G4cout << "GAG, ";
00099   if (tcsh_build ) G4cout << "tcsh, ";
00100   G4cout << "csh ]" << G4endl;
00101 
00102   // selecting session type...
00103   // 1st priority : in case argumant specified
00104   G4String stype = type;
00105   stype.toLower();   // session type is case-insensitive.
00106   if (type != "") SelectSessionByArg(stype);
00107 
00108   // 2nd priority : refer environment variables (as backword compatibility)
00109   if ( selected == kNone ) SelectSessionByEnv();
00110 
00111   // 3rd priority : refer $HOME/.g4session
00112   if ( selected == kNone ) {
00113     G4String appinput = argv[0];
00114     G4String appname = "";
00115     size_t islash = appinput.find_last_of("/\\");
00116     if (islash == G4String::npos) 
00117       appname = appinput;
00118     else 
00119       appname = appinput(islash+1, appinput.size()-islash-1);
00120 
00121     SelectSessionByFile(appname);
00122   }
00123 
00124   // 4th, best guess of session type  
00125   if ( selected == kNone) SelectSessionByBestGuess();
00126     
00127   // instantiate a session...
00128   switch ( selected ) {
00129   case kQt:
00130 #if defined(G4UI_BUILD_QT_SESSION)
00131     session = new G4UIQt(argc, argv);
00132     isGUI = true;
00133 #endif
00134     break;
00135   case kXm:
00136 #if defined(G4UI_BUILD_XM_SESSION)
00137     session = new G4UIXm(argc, argv);
00138     isGUI = true;
00139 #endif
00140     break;
00141   case kWin32:
00142 #if defined(G4UI_BUILD_WIN32_SESSION)
00143     DISCARD_PARAMETER(argc);
00144     DISCARD_PARAMETER(argv);
00145     session = new G4UIWin32();
00146 #endif
00147     break;
00148   case kWt:
00149 #if defined(G4UI_BUILD_WT_SESSION)
00150     //session = new G4UIWt(argc, argv);
00151     //isGUI = true;
00152 #endif
00153     break;
00154   case kGag:
00155     DISCARD_PARAMETER(argc);
00156     DISCARD_PARAMETER(argv);
00157     session = new G4UIGAG();
00158     isGUI = true;
00159     break;
00160  case kTcsh:
00161 #ifndef WIN32
00162     DISCARD_PARAMETER(argc);
00163     DISCARD_PARAMETER(argv);
00164     shell = new G4UItcsh;
00165     session = new G4UIterminal(shell);
00166 #endif
00167     break;
00168   case kCsh:
00169     DISCARD_PARAMETER(argc);
00170     DISCARD_PARAMETER(argv);
00171     shell = new G4UIcsh;
00172     session = new G4UIterminal(shell);    
00173   default:
00174     break;
00175   }
00176 
00177   // fallback (csh)
00178   if ( session == NULL ) {
00179     G4Exception("G4UIExecutive::G4UIExecutive()", 
00180                 "UI0002",
00181                 JustWarning,
00182                 "Specified session type is not build in your system,\n"
00183                 "or no session type is specified.\n"
00184                 "A fallback session type is used.");
00185 
00186     selected = kCsh;
00187     DISCARD_PARAMETER(argc);
00188     DISCARD_PARAMETER(argv);
00189     shell = new G4UIcsh;
00190     session = new G4UIterminal(shell);
00191   }
00192 }
00193 
00194 // --------------------------------------------------------------------------
00195 G4UIExecutive::~G4UIExecutive()
00196 {
00197   if ( selected != kWt ) delete session;
00198 }
00199 
00200 // --------------------------------------------------------------------------
00201 void G4UIExecutive::SelectSessionByArg(const G4String& stype)
00202 {
00203   if ( qt_build && stype == "qt" ) selected = kQt;
00204   else if ( xm_build && stype == "xm" ) selected = kXm;
00205   else if ( win32_build && stype == "win32" ) selected = kWin32;
00206   //else if ( wt_build && stype == "wt" ) selected = kWt;
00207   else if ( stype == "gag" ) selected = kGag;
00208   else if ( tcsh_build && stype == "tcsh" ) selected = kTcsh;
00209   else if ( stype == "csh" ) selected = kCsh;
00210 }
00211 
00212 // --------------------------------------------------------------------------
00213 void G4UIExecutive::SelectSessionByEnv()
00214 {
00215   if ( qt_build && getenv("G4UI_USE_QT") ) selected = kQt;
00216   else if ( xm_build && getenv("G4UI_USE_XM") ) selected = kXm;
00217   else if ( win32_build && getenv("G4UI_USE_WIN32") ) selected = kWin32;
00218   //else if ( wt_build && getenv("G4UI_USE_WT") ) selected = kWt;
00219   else if ( getenv("G4UI_USE_GAG") ) selected = kGag;
00220   else if ( tcsh_build && getenv("G4UI_USE_TCSH") ) selected = kTcsh;
00221 }
00222 
00223 // --------------------------------------------------------------------------
00224 void G4UIExecutive::SelectSessionByFile(const G4String& appname)
00225 {
00226   const char* path = getenv("HOME");
00227   if( path == NULL ) return;
00228   G4String homedir = path;
00229 
00230 #ifndef WIN32
00231   G4String fname= homedir + "/.g4session";
00232 #else
00233   G4String fname= homedir + "\\.g4session";
00234 #endif
00235     
00236   std::ifstream fsession;
00237   enum { BUFSIZE= 1024 }; char linebuf[BUFSIZE];
00238   
00239   fsession.open(fname, std::ios::in);
00240 
00241   G4String default_session = "";
00242   G4int iline = 1;
00243   sessionMap.clear();
00244   while( fsession.good() ) {
00245     if( fsession.eof()) break;
00246     fsession.getline(linebuf, BUFSIZE);
00247     G4String aline = linebuf;
00248     aline.strip(G4String::both);
00249     if ( aline(0) == '#' ) continue;
00250     if ( iline == 1 )
00251       default_session = aline;
00252     else {
00253       size_t idx = aline.find_first_of(" ");
00254       if ( idx == G4String::npos ) break;
00255       G4String aname = aline.substr(0, idx);
00256       idx = aline.find_first_not_of(" ", idx);
00257       if (idx == G4String::npos ) break;
00258       G4String sname = aline.substr(idx, aline.size()-idx);
00259       sessionMap[aname] = sname;
00260     }
00261     iline++;
00262   }
00263   fsession.close();
00264 
00265   G4String stype = "";
00266   std::map<G4String, G4String>::iterator it = sessionMap.find(appname);
00267   if ( it != sessionMap.end() ) stype = sessionMap[appname];
00268   else stype = default_session;
00269   stype.toLower();
00270 
00271   // select session...
00272   if ( qt_build && stype == "qt" ) selected = kQt;
00273   else if ( xm_build && stype == "xm" ) selected = kXm;
00274   else if ( win32_build && stype == "win32" ) selected = kWin32;
00275   //else if ( wt_build && stype == "wt" ) selected = kWt;
00276   else if ( stype == "gag" ) selected = kGag;
00277   else if ( tcsh_build && stype == "tcsh" ) selected = kTcsh;
00278   else if ( stype == "csh" ) selected = kCsh;
00279 }
00280 
00281 // --------------------------------------------------------------------------
00282 void G4UIExecutive::SelectSessionByBestGuess()
00283 {
00284   if ( qt_build ) selected = kQt;
00285   else if ( tcsh_build ) selected = kTcsh;
00286   else if ( xm_build ) selected = kXm;
00287 }
00288 
00289 // --------------------------------------------------------------------------
00290 void G4UIExecutive::SetPrompt(const G4String& prompt)
00291 {
00292   if(shell) shell-> SetPrompt(prompt);
00293 }
00294 
00295 // --------------------------------------------------------------------------
00296 void G4UIExecutive::SetLsColor(TermColorIndex dirColor,
00297                                TermColorIndex cmdColor)
00298 {
00299   if(shell) shell-> SetLsColor(dirColor, cmdColor);
00300 }
00301 
00302 // --------------------------------------------------------------------------
00303 void G4UIExecutive::SessionStart()
00304 {
00305   session-> SessionStart();
00306 }

Generated on Mon May 27 17:50:06 2013 for Geant4 by  doxygen 1.4.7