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