Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4UIExecutive.cc
Go to the documentation of this file.
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#include "G4UIsession.hh"
28#include "G4UImanager.hh"
29
30#if defined(G4UI_BUILD_QT_SESSION)
31#include "G4UIQt.hh"
32#include "G4Qt.hh"
33#endif
34
35#if defined(G4UI_BUILD_XM_SESSION)
36#include "G4UIXm.hh"
37#endif
38
39#if defined(G4UI_BUILD_WIN32_SESSION)
40#include "G4UIWin32.hh"
41#endif
42
43#include "G4UIterminal.hh"
44#include "G4UItcsh.hh"
45#include "G4UIcsh.hh"
46#include "G4TiMemory.hh"
47
48// --------------------------------------------------------------------------
49// build flags as variables
50
51#if defined(G4UI_BUILD_QT_SESSION)
52static const G4bool qt_build = true;
53#else
54static const G4bool qt_build = false;
55#endif
56
57#if defined(G4UI_BUILD_XM_SESSION)
58static const G4bool xm_build = true;
59#else
60static const G4bool xm_build = false;
61#endif
62
63#if defined(G4UI_BUILD_WIN32_SESSION)
64static const G4bool win32_build = true;
65#else
66static const G4bool win32_build = false;
67#endif
68
69#ifndef WIN32
70static const G4bool tcsh_build = true;
71#else
72static const G4bool tcsh_build = false;
73#endif
74
75#define DISCARD_PARAMETER(p) (void)p
76
77// --------------------------------------------------------------------------
78G4UIExecutive::G4UIExecutive(G4int argc, char** argv, const G4String& type)
79 : selected(kNone), session(NULL), shell(NULL), 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 != "") 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 G4String appinput = argv[0];
101 G4String appname = "";
102 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 == NULL ) {
155 G4Exception("G4UIExecutive::G4UIExecutive()",
156 "UI0002",
158 "Specified session type is not build in your system,\n"
159 "or no session type is specified.\n"
160 "A fallback session type is used.");
161
162 selected = kCsh;
163 DISCARD_PARAMETER(argc);
164 DISCARD_PARAMETER(argv);
165 shell = new G4UIcsh;
166 session = new G4UIterminal(shell);
167 }
168
169 TIMEMORY_INIT(argc, argv);
170}
171
172// --------------------------------------------------------------------------
174{
175 delete session;
176}
177
178// --------------------------------------------------------------------------
179void G4UIExecutive::SelectSessionByArg(const G4String& stype)
180{
181 if ( qt_build && stype == "qt" ) selected = kQt;
182 else if ( xm_build && stype == "xm" ) selected = kXm;
183 else if ( win32_build && stype == "win32" ) selected = kWin32;
184 else if ( tcsh_build && stype == "tcsh" ) selected = kTcsh;
185 else if ( stype == "csh" ) selected = kCsh;
186}
187
188// --------------------------------------------------------------------------
189void G4UIExecutive::SelectSessionByEnv()
190{
191 if ( qt_build && std::getenv("G4UI_USE_QT") ) selected = kQt;
192 else if ( xm_build && std::getenv("G4UI_USE_XM") ) selected = kXm;
193 else if ( win32_build && std::getenv("G4UI_USE_WIN32") ) selected = kWin32;
194 else if ( tcsh_build && std::getenv("G4UI_USE_TCSH") ) selected = kTcsh;
195}
196
197// --------------------------------------------------------------------------
198void G4UIExecutive::SelectSessionByFile(const G4String& appname)
199{
200 const char* path = std::getenv("HOME");
201 if( path == NULL ) return;
202 G4String homedir = path;
203
204#ifndef WIN32
205 G4String fname= homedir + "/.g4session";
206#else
207 G4String fname= homedir + "\\.g4session";
208#endif
209
210 std::ifstream fsession;
211 enum { BUFSIZE= 1024 }; char linebuf[BUFSIZE];
212
213 fsession.open(fname, std::ios::in);
214
215 G4String default_session = "";
216 G4int iline = 1;
217 sessionMap.clear();
218 while( fsession.good() ) {
219 if( fsession.eof()) break;
220 fsession.getline(linebuf, BUFSIZE);
221 G4String aline = G4StrUtil::strip_copy(linebuf);
222 if ( aline[0] == '#' ) continue;
223 if ( aline == "" ) continue;
224 if ( iline == 1 )
225 default_session = aline;
226 else {
227 size_t idx = aline.find_first_of(" ");
228 if ( idx == G4String::npos ) break;
229 G4String aname = aline.substr(0, idx);
230 idx = aline.find_first_not_of(" ", idx);
231 if (idx == G4String::npos ) break;
232 G4String sname = aline.substr(idx, aline.size()-idx);
233 sessionMap[aname] = sname;
234 }
235 iline++;
236 }
237 fsession.close();
238
239 G4String stype = "";
240 std::map<G4String, G4String>::iterator it = sessionMap.find(appname);
241 if ( it != sessionMap.end() ) stype = sessionMap[appname];
242 else stype = default_session;
243 G4StrUtil::to_lower(stype);
244
245 // select session...
246 if ( qt_build && stype == "qt" ) selected = kQt;
247 else if ( xm_build && stype == "xm" ) selected = kXm;
248 else if ( win32_build && stype == "win32" ) selected = kWin32;
249 else if ( tcsh_build && stype == "tcsh" ) selected = kTcsh;
250 else if ( stype == "csh" ) selected = kCsh;
251}
252
253// --------------------------------------------------------------------------
254void G4UIExecutive::SelectSessionByBestGuess()
255{
256 if ( qt_build ) selected = kQt;
257 else if ( win32_build ) selected = kWin32;
258 else if ( tcsh_build ) selected = kTcsh;
259 else if ( xm_build ) selected = kXm;
260}
261
262// --------------------------------------------------------------------------
264{
265 if(shell) shell-> SetPrompt(prompt);
266}
267
268// --------------------------------------------------------------------------
270 TermColorIndex cmdColor)
271{
272 if(shell) shell-> SetLsColor(dirColor, cmdColor);
273}
274
275// --------------------------------------------------------------------------
277{
279}
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:59
#define TIMEMORY_INIT(...)
Definition: G4TiMemory.hh:108
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
#define DISCARD_PARAMETER(p)
TermColorIndex
Definition: G4VUIshell.hh:53
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
void SetPrompt(const G4String &prompt)
G4UIExecutive(G4int argc, char **argv, const G4String &type="")
void SetLsColor(TermColorIndex dirColor, TermColorIndex cmdColor)
Definition: G4UIXm.hh:60
G4String strip_copy(G4String str, char ch=' ')
Return copy of string with leading and trailing characters removed.
G4String to_lower_copy(G4String str)
Return lowercased copy of string.
void to_lower(G4String &str)
Convert string to lowercase.