Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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_WT_SESSION)
36#include "G4UIWt.hh"
37#include "G4Wt.hh"
38#endif
39
40#if defined(G4UI_BUILD_XM_SESSION)
41#include "G4UIXm.hh"
42#endif
43
44#if defined(G4UI_BUILD_WIN32_SESSION)
45#include "G4UIWin32.hh"
46#endif
47
48#include "G4UIGAG.hh"
49#include "G4UIterminal.hh"
50#include "G4UItcsh.hh"
51#include "G4UIcsh.hh"
52#include "G4TiMemory.hh"
53
54// --------------------------------------------------------------------------
55// build flags as variables
56
57#if defined(G4UI_BUILD_QT_SESSION)
58static const G4bool qt_build = true;
59#else
60static const G4bool qt_build = false;
61#endif
62
63#if defined(G4UI_BUILD_XM_SESSION)
64static const G4bool xm_build = true;
65#else
66static const G4bool xm_build = false;
67#endif
68
69#if defined(G4UI_BUILD_WIN32_SESSION)
70static const G4bool win32_build = true;
71#else
72static const G4bool win32_build = false;
73#endif
74
75#if defined(G4UI_BUILD_WT_SESSION)
76static const G4bool wt_build = true;
77#else
78static const G4bool wt_build = false;
79#endif
80
81#ifndef WIN32
82static const G4bool tcsh_build = true;
83#else
84static const G4bool tcsh_build = false;
85#endif
86
87#define DISCARD_PARAMETER(p) (void)p
88
89// --------------------------------------------------------------------------
90G4UIExecutive::G4UIExecutive(G4int argc, char** argv, const G4String& type)
91 : selected(kNone), session(NULL), shell(NULL), isGUI(false)
92{
93 G4cout << "Available UI session types: [ ";
94 if ( qt_build ) G4cout << "Qt, ";
95 if ( xm_build ) G4cout << "Xm, ";
96 if ( win32_build) G4cout << "Win32, ";
97 if ( wt_build ) G4cout << "Wt, ";
98 G4cout << "GAG, ";
99 if (tcsh_build ) G4cout << "tcsh, ";
100 G4cout << "csh ]" << G4endl;
101
102 // selecting session type...
103 // 1st priority : in case argumant specified
104 G4String stype = type;
105 stype.toLower(); // session type is case-insensitive.
106 if (type != "") SelectSessionByArg(stype);
107
108 // 2nd priority : refer environment variables (as backword compatibility)
109 if ( selected == kNone ) SelectSessionByEnv();
110
111 // 3rd priority : refer $HOME/.g4session
112 if ( selected == kNone ) {
113 G4String appinput = argv[0];
114 G4String appname = "";
115 size_t islash = appinput.find_last_of("/\\");
116 if (islash == G4String::npos)
117 appname = appinput;
118 else
119 appname = appinput(islash+1, appinput.size()-islash-1);
120
121 SelectSessionByFile(appname);
122 }
123
124 // 4th, best guess of session type
125 if ( selected == kNone) SelectSessionByBestGuess();
126
127 // instantiate a session...
128 switch ( selected ) {
129 case kQt:
130#if defined(G4UI_BUILD_QT_SESSION)
131 session = new G4UIQt(argc, argv);
132 isGUI = true;
133#endif
134 break;
135 case kXm:
136#if defined(G4UI_BUILD_XM_SESSION)
137 session = new G4UIXm(argc, argv);
138 isGUI = true;
139#endif
140 break;
141 case kWin32:
142#if defined(G4UI_BUILD_WIN32_SESSION)
143 DISCARD_PARAMETER(argc);
144 DISCARD_PARAMETER(argv);
145 session = new G4UIWin32();
146 isGUI = true;
147#endif
148 break;
149 case kWt:
150#if defined(G4UI_BUILD_WT_SESSION)
151 session = new G4UIWt(argc, argv);
152 isGUI = true;
153#endif
154 break;
155 case kGag:
156 DISCARD_PARAMETER(argc);
157 DISCARD_PARAMETER(argv);
158 session = new G4UIGAG();
159 isGUI = true;
160 break;
161 case kTcsh:
162#ifndef WIN32
163 DISCARD_PARAMETER(argc);
164 DISCARD_PARAMETER(argv);
165 shell = new G4UItcsh;
166 session = new G4UIterminal(shell);
167#endif
168 break;
169 case kCsh:
170 DISCARD_PARAMETER(argc);
171 DISCARD_PARAMETER(argv);
172 shell = new G4UIcsh;
173 session = new G4UIterminal(shell);
174 default:
175 break;
176 }
177
178 // fallback (csh)
179 if ( session == NULL ) {
180 G4Exception("G4UIExecutive::G4UIExecutive()",
181 "UI0002",
183 "Specified session type is not build in your system,\n"
184 "or no session type is specified.\n"
185 "A fallback session type is used.");
186
187 selected = kCsh;
188 DISCARD_PARAMETER(argc);
189 DISCARD_PARAMETER(argv);
190 shell = new G4UIcsh;
191 session = new G4UIterminal(shell);
192 }
193
194 TIMEMORY_INIT(argc, argv);
195}
196
197// --------------------------------------------------------------------------
199{
200 if ( selected != kWt ) delete session;
201}
202
203// --------------------------------------------------------------------------
204void G4UIExecutive::SelectSessionByArg(const G4String& stype)
205{
206 if ( qt_build && stype == "qt" ) selected = kQt;
207 else if ( xm_build && stype == "xm" ) selected = kXm;
208 else if ( win32_build && stype == "win32" ) selected = kWin32;
209 else if ( wt_build && stype == "wt" ) selected = kWt;
210 else if ( stype == "gag" ) selected = kGag;
211 else if ( tcsh_build && stype == "tcsh" ) selected = kTcsh;
212 else if ( stype == "csh" ) selected = kCsh;
213}
214
215// --------------------------------------------------------------------------
216void G4UIExecutive::SelectSessionByEnv()
217{
218 if ( qt_build && std::getenv("G4UI_USE_QT") ) selected = kQt;
219 else if ( xm_build && std::getenv("G4UI_USE_XM") ) selected = kXm;
220 else if ( win32_build && std::getenv("G4UI_USE_WIN32") ) selected = kWin32;
221 else if ( wt_build && std::getenv("G4UI_USE_WT") ) selected = kWt;
222 else if ( std::getenv("G4UI_USE_GAG") ) selected = kGag;
223 else if ( tcsh_build && std::getenv("G4UI_USE_TCSH") ) selected = kTcsh;
224}
225
226// --------------------------------------------------------------------------
227void G4UIExecutive::SelectSessionByFile(const G4String& appname)
228{
229 const char* path = std::getenv("HOME");
230 if( path == NULL ) return;
231 G4String homedir = path;
232
233#ifndef WIN32
234 G4String fname= homedir + "/.g4session";
235#else
236 G4String fname= homedir + "\\.g4session";
237#endif
238
239 std::ifstream fsession;
240 enum { BUFSIZE= 1024 }; char linebuf[BUFSIZE];
241
242 fsession.open(fname, std::ios::in);
243
244 G4String default_session = "";
245 G4int iline = 1;
246 sessionMap.clear();
247 while( fsession.good() ) {
248 if( fsession.eof()) break;
249 fsession.getline(linebuf, BUFSIZE);
250 G4String aline = linebuf;
251 aline.strip(G4String::both);
252 if ( aline(0) == '#' ) continue;
253 if ( aline == "" ) continue;
254 if ( iline == 1 )
255 default_session = aline;
256 else {
257 size_t idx = aline.find_first_of(" ");
258 if ( idx == G4String::npos ) break;
259 G4String aname = aline.substr(0, idx);
260 idx = aline.find_first_not_of(" ", idx);
261 if (idx == G4String::npos ) break;
262 G4String sname = aline.substr(idx, aline.size()-idx);
263 sessionMap[aname] = sname;
264 }
265 iline++;
266 }
267 fsession.close();
268
269 G4String stype = "";
270 std::map<G4String, G4String>::iterator it = sessionMap.find(appname);
271 if ( it != sessionMap.end() ) stype = sessionMap[appname];
272 else stype = default_session;
273 stype.toLower();
274
275 // select session...
276 if ( qt_build && stype == "qt" ) selected = kQt;
277 else if ( xm_build && stype == "xm" ) selected = kXm;
278 else if ( win32_build && stype == "win32" ) selected = kWin32;
279 //else if ( wt_build && stype == "wt" ) selected = kWt;
280 else if ( stype == "gag" ) selected = kGag;
281 else if ( tcsh_build && stype == "tcsh" ) selected = kTcsh;
282 else if ( stype == "csh" ) selected = kCsh;
283}
284
285// --------------------------------------------------------------------------
286void G4UIExecutive::SelectSessionByBestGuess()
287{
288 if ( qt_build ) selected = kQt;
289 else if ( tcsh_build ) selected = kTcsh;
290 else if ( xm_build ) selected = kXm;
291}
292
293// --------------------------------------------------------------------------
295{
296 if(shell) shell-> SetPrompt(prompt);
297}
298
299// --------------------------------------------------------------------------
301 TermColorIndex cmdColor)
302{
303 if(shell) shell-> SetLsColor(dirColor, cmdColor);
304}
305
306// --------------------------------------------------------------------------
308{
309 session-> SessionStart();
310}
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
#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
G4String strip(G4int strip_Type=trailing, char c=' ')
void toLower()
void SetPrompt(const G4String &prompt)
G4UIExecutive(G4int argc, char **argv, const G4String &type="")
void SetLsColor(TermColorIndex dirColor, TermColorIndex cmdColor)