Geant4 Cross Reference |
1 // 2 // ******************************************************************** 3 // * License and Disclaimer * 4 // * * 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. * 10 // * * 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitation of liability. * 17 // * * 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************************************** 25 26 #include "G4UIExecutive.hh" 27 28 #include "G4UImanager.hh" 29 #include "G4UIsession.hh" 30 31 #if defined(G4UI_BUILD_QT_SESSION) 32 # include "G4Qt.hh" 33 # include "G4UIQt.hh" 34 #endif 35 36 #if defined(G4UI_BUILD_XM_SESSION) 37 # include "G4UIXm.hh" 38 #endif 39 40 #if defined(G4UI_BUILD_WIN32_SESSION) 41 # include "G4UIWin32.hh" 42 #endif 43 44 #include "G4UIcsh.hh" 45 #include "G4UItcsh.hh" 46 #include "G4UIterminal.hh" 47 48 // -------------------------------------------------------------------------- 49 // build flags as variables 50 51 #if defined(G4UI_BUILD_QT_SESSION) 52 static const G4bool qt_build = true; 53 #else 54 static const G4bool qt_build = false; 55 #endif 56 57 #if defined(G4UI_BUILD_XM_SESSION) 58 static const G4bool xm_build = true; 59 #else 60 static const G4bool xm_build = false; 61 #endif 62 63 #if defined(G4UI_BUILD_WIN32_SESSION) 64 static const G4bool win32_build = true; 65 #else 66 static const G4bool win32_build = false; 67 #endif 68 69 #ifndef WIN32 70 static const G4bool tcsh_build = true; 71 #else 72 static const G4bool tcsh_build = false; 73 #endif 74 75 #define DISCARD_PARAMETER(p) (void)p 76 77 // -------------------------------------------------------------------------- 78 G4UIExecutive::G4UIExecutive(G4int argc, char** argv, const G4String& type) 79 : selected(kNone), session(nullptr), shell(nullptr), isGUI(false), verbose(true) 80 { 81 if (verbose) { 82 G4cout << "Available UI session types: [ "; 83 if (qt_build) G4cout << "Qt, "; 84 if (xm_build) G4cout << "Xm, "; 85 if (win32_build) G4cout << "Win32, "; 86 if (tcsh_build) G4cout << "tcsh, "; 87 G4cout << "csh ]" << G4endl; 88 } 89 90 // selecting session type... 91 // 1st priority : in case argumant specified 92 G4String stype = G4StrUtil::to_lower_copy(type); // session type is case-insensitive. 93 if (! type.empty()) SelectSessionByArg(stype); 94 95 // 2nd priority : refer environment variables (as backword compatibility) 96 if (selected == kNone) SelectSessionByEnv(); 97 98 // 3rd priority : refer $HOME/.g4session 99 if (selected == kNone) { 100 const G4String& appinput = argv[0]; 101 G4String appname = ""; 102 std::size_t islash = appinput.find_last_of("/\\"); 103 if (islash == G4String::npos) 104 appname = appinput; 105 else 106 appname = appinput.substr(islash + 1, appinput.size() - islash - 1); 107 108 SelectSessionByFile(appname); 109 } 110 111 // 4th, best guess of session type 112 if (selected == kNone) SelectSessionByBestGuess(); 113 114 // instantiate a session... 115 switch (selected) { 116 case kQt: 117 #if defined(G4UI_BUILD_QT_SESSION) 118 session = new G4UIQt(argc, argv); 119 isGUI = true; 120 #endif 121 break; 122 case kXm: 123 #if defined(G4UI_BUILD_XM_SESSION) 124 session = new G4UIXm(argc, argv); 125 isGUI = true; 126 #endif 127 break; 128 case kWin32: 129 #if defined(G4UI_BUILD_WIN32_SESSION) 130 DISCARD_PARAMETER(argc); 131 DISCARD_PARAMETER(argv); 132 session = new G4UIWin32(); 133 isGUI = true; 134 #endif 135 break; 136 case kTcsh: 137 #if ! (defined(WIN32) || defined(__MINGW32__)) 138 DISCARD_PARAMETER(argc); 139 DISCARD_PARAMETER(argv); 140 shell = new G4UItcsh; 141 session = new G4UIterminal(shell); 142 #endif 143 break; 144 case kCsh: 145 DISCARD_PARAMETER(argc); 146 DISCARD_PARAMETER(argv); 147 shell = new G4UIcsh; 148 session = new G4UIterminal(shell); 149 default: 150 break; 151 } 152 153 // fallback (csh) 154 if (session == nullptr) { 155 G4Exception("G4UIExecutive::G4UIExecutive()", "UI0002", JustWarning, 156 "Specified session type is not build in your system,\n" 157 "or no session type is specified.\n" 158 "A fallback session type is used."); 159 160 selected = kCsh; 161 DISCARD_PARAMETER(argc); 162 DISCARD_PARAMETER(argv); 163 shell = new G4UIcsh; 164 session = new G4UIterminal(shell); 165 } 166 } 167 168 // -------------------------------------------------------------------------- 169 G4UIExecutive::~G4UIExecutive() { delete session; } 170 171 // -------------------------------------------------------------------------- 172 void G4UIExecutive::SelectSessionByArg(const G4String& stype) 173 { 174 if (qt_build && stype == "qt") 175 selected = kQt; 176 else if (xm_build && stype == "xm") 177 selected = kXm; 178 else if (win32_build && stype == "win32") 179 selected = kWin32; 180 else if (tcsh_build && stype == "tcsh") 181 selected = kTcsh; 182 else if (stype == "csh") 183 selected = kCsh; 184 } 185 186 // -------------------------------------------------------------------------- 187 void G4UIExecutive::SelectSessionByEnv() 188 { 189 if (qt_build && (std::getenv("G4UI_USE_QT") != nullptr)) 190 selected = kQt; 191 else if (xm_build && (std::getenv("G4UI_USE_XM") != nullptr)) 192 selected = kXm; 193 else if (win32_build && (std::getenv("G4UI_USE_WIN32") != nullptr)) 194 selected = kWin32; 195 else if (tcsh_build && (std::getenv("G4UI_USE_TCSH") != nullptr)) 196 selected = kTcsh; 197 } 198 199 // -------------------------------------------------------------------------- 200 void G4UIExecutive::SelectSessionByFile(const G4String& appname) 201 { 202 const char* path = std::getenv("HOME"); 203 if (path == nullptr) return; 204 G4String homedir = path; 205 206 #ifndef WIN32 207 G4String fname = homedir + "/.g4session"; 208 #else 209 G4String fname = homedir + "\\.g4session"; 210 #endif 211 212 std::ifstream fsession; 213 enum 214 { 215 BUFSIZE = 1024 216 }; 217 char linebuf[BUFSIZE]; 218 219 fsession.open(fname, std::ios::in); 220 221 G4String default_session = ""; 222 G4int iline = 1; 223 sessionMap.clear(); 224 while (fsession.good()) { 225 if (fsession.eof()) break; 226 fsession.getline(linebuf, BUFSIZE); 227 const G4String& aline = G4StrUtil::strip_copy(linebuf); 228 if (aline[0] == '#') continue; 229 if (aline.empty()) continue; 230 if (iline == 1) 231 default_session = aline; 232 else { 233 size_t idx = aline.find_first_of(' '); 234 if (idx == G4String::npos) break; 235 G4String aname = aline.substr(0, idx); 236 idx = aline.find_first_not_of(' ', idx); 237 if (idx == G4String::npos) break; 238 const G4String& sname = aline.substr(idx, aline.size() - idx); 239 sessionMap[aname] = sname; 240 } 241 iline++; 242 } 243 fsession.close(); 244 245 G4String stype = ""; 246 auto it = sessionMap.find(appname); 247 if (it != sessionMap.end()) 248 stype = sessionMap[appname]; 249 else 250 stype = std::move(default_session); 251 G4StrUtil::to_lower(stype); 252 253 // select session... 254 if (qt_build && stype == "qt") 255 selected = kQt; 256 else if (xm_build && stype == "xm") 257 selected = kXm; 258 else if (win32_build && stype == "win32") 259 selected = kWin32; 260 else if (tcsh_build && stype == "tcsh") 261 selected = kTcsh; 262 else if (stype == "csh") 263 selected = kCsh; 264 } 265 266 // -------------------------------------------------------------------------- 267 void G4UIExecutive::SelectSessionByBestGuess() 268 { 269 if (qt_build) 270 selected = kQt; 271 else if (win32_build) 272 selected = kWin32; 273 else if (tcsh_build) 274 selected = kTcsh; 275 else if (xm_build) 276 selected = kXm; 277 } 278 279 // -------------------------------------------------------------------------- 280 void G4UIExecutive::SetPrompt(const G4String& prompt) 281 { 282 if (shell != nullptr) shell->SetPrompt(prompt); 283 } 284 285 // -------------------------------------------------------------------------- 286 void G4UIExecutive::SetLsColor(TermColorIndex dirColor, TermColorIndex cmdColor) 287 { 288 if (shell != nullptr) shell->SetLsColor(dirColor, cmdColor); 289 } 290 291 // -------------------------------------------------------------------------- 292 void G4UIExecutive::SessionStart() { session->SessionStart(); } 293