Geant4  10.03
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 // $Id: G4UIExecutive.icc,v 1.7 2010-05-28 08:12:27 kmura Exp $
26 // GEANT4 tag $Name: geant4-09-04-patch-02 $
27 
28 #include "G4UIExecutive.hh"
29 #include "G4UIsession.hh"
30 #include "G4UImanager.hh"
31 
32 #if defined(G4UI_BUILD_QT_SESSION)
33 #include "G4UIQt.hh"
34 #include "G4Qt.hh"
35 #endif
36 
37 #if defined(G4UI_BUILD_WT_SESSION)
38 #include "G4UIWt.hh"
39 #include "G4Wt.hh"
40 #endif
41 
42 #if defined(G4UI_BUILD_XM_SESSION)
43 #include "G4UIXm.hh"
44 #endif
45 
46 #if defined(G4UI_BUILD_WIN32_SESSION)
47 #include "G4UIWin32.hh"
48 #endif
49 
50 #include "G4UIGAG.hh"
51 #include "G4UIterminal.hh"
52 #include "G4UItcsh.hh"
53 #include "G4UIcsh.hh"
54 
55 // --------------------------------------------------------------------------
56 // build flags as variables
57 
58 #if defined(G4UI_BUILD_QT_SESSION)
59 static const G4bool qt_build = true;
60 #else
61 static const G4bool qt_build = false;
62 #endif
63 
64 #if defined(G4UI_BUILD_XM_SESSION)
65 static const G4bool xm_build = true;
66 #else
67 static const G4bool xm_build = false;
68 #endif
69 
70 #if defined(G4UI_BUILD_WIN32_SESSION)
71 static const G4bool win32_build = true;
72 #else
73 static const G4bool win32_build = false;
74 #endif
75 
76 #if defined(G4UI_BUILD_WT_SESSION)
77 static const G4bool wt_build = true;
78 #else
79 static const G4bool wt_build = false;
80 #endif
81 
82 #ifndef WIN32
83 static const G4bool tcsh_build = true;
84 #else
85 static const G4bool tcsh_build = false;
86 #endif
87 
88 #define DISCARD_PARAMETER(p) (void)p
89 
90 // --------------------------------------------------------------------------
91 G4UIExecutive::G4UIExecutive(G4int argc, char** argv, const G4String& type)
92  : selected(kNone), session(NULL), shell(NULL), isGUI(false)
93 {
94  G4cout << "Available UI session types: [ ";
95  if ( qt_build ) G4cout << "Qt, ";
96  if ( xm_build ) G4cout << "Xm, ";
97  if ( win32_build) G4cout << "Win32, ";
98  if ( wt_build ) G4cout << "Wt, ";
99  G4cout << "GAG, ";
100  if (tcsh_build ) G4cout << "tcsh, ";
101  G4cout << "csh ]" << G4endl;
102 
103  // selecting session type...
104  // 1st priority : in case argumant specified
105  G4String stype = type;
106  stype.toLower(); // session type is case-insensitive.
107  if (type != "") SelectSessionByArg(stype);
108 
109  // 2nd priority : refer environment variables (as backword compatibility)
110  if ( selected == kNone ) SelectSessionByEnv();
111 
112  // 3rd priority : refer $HOME/.g4session
113  if ( selected == kNone ) {
114  G4String appinput = argv[0];
115  G4String appname = "";
116  size_t islash = appinput.find_last_of("/\\");
117  if (islash == G4String::npos)
118  appname = appinput;
119  else
120  appname = appinput(islash+1, appinput.size()-islash-1);
121 
122  SelectSessionByFile(appname);
123  }
124 
125  // 4th, best guess of session type
127 
128  // instantiate a session...
129  switch ( selected ) {
130  case kQt:
131 #if defined(G4UI_BUILD_QT_SESSION)
132  session = new G4UIQt(argc, argv);
133  isGUI = true;
134 #endif
135  break;
136  case kXm:
137 #if defined(G4UI_BUILD_XM_SESSION)
138  session = new G4UIXm(argc, argv);
139  isGUI = true;
140 #endif
141  break;
142  case kWin32:
143 #if defined(G4UI_BUILD_WIN32_SESSION)
144  DISCARD_PARAMETER(argc);
145  DISCARD_PARAMETER(argv);
146  session = new G4UIWin32();
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",
182  JustWarning,
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 
195 // --------------------------------------------------------------------------
197 {
198  if ( selected != kWt ) delete session;
199 }
200 
201 // --------------------------------------------------------------------------
203 {
204  if ( qt_build && stype == "qt" ) selected = kQt;
205  else if ( xm_build && stype == "xm" ) selected = kXm;
206  else if ( win32_build && stype == "win32" ) selected = kWin32;
207  else if ( wt_build && stype == "wt" ) selected = kWt;
208  else if ( stype == "gag" ) selected = kGag;
209  else if ( tcsh_build && stype == "tcsh" ) selected = kTcsh;
210  else if ( stype == "csh" ) selected = kCsh;
211 }
212 
213 // --------------------------------------------------------------------------
215 {
216  if ( qt_build && getenv("G4UI_USE_QT") ) selected = kQt;
217  else if ( xm_build && getenv("G4UI_USE_XM") ) selected = kXm;
218  else if ( win32_build && getenv("G4UI_USE_WIN32") ) selected = kWin32;
219  else if ( wt_build && getenv("G4UI_USE_WT") ) selected = kWt;
220  else if ( getenv("G4UI_USE_GAG") ) selected = kGag;
221  else if ( tcsh_build && getenv("G4UI_USE_TCSH") ) selected = kTcsh;
222 }
223 
224 // --------------------------------------------------------------------------
226 {
227  const char* path = getenv("HOME");
228  if( path == NULL ) return;
229  G4String homedir = path;
230 
231 #ifndef WIN32
232  G4String fname= homedir + "/.g4session";
233 #else
234  G4String fname= homedir + "\\.g4session";
235 #endif
236 
237  std::ifstream fsession;
238  enum { BUFSIZE= 1024 }; char linebuf[BUFSIZE];
239 
240  fsession.open(fname, std::ios::in);
241 
242  G4String default_session = "";
243  G4int iline = 1;
244  sessionMap.clear();
245  while( fsession.good() ) {
246  if( fsession.eof()) break;
247  fsession.getline(linebuf, BUFSIZE);
248  G4String aline = linebuf;
249  aline.strip(G4String::both);
250  if ( aline(0) == '#' ) continue;
251  if ( iline == 1 )
252  default_session = aline;
253  else {
254  size_t idx = aline.find_first_of(" ");
255  if ( idx == G4String::npos ) break;
256  G4String aname = aline.substr(0, idx);
257  idx = aline.find_first_not_of(" ", idx);
258  if (idx == G4String::npos ) break;
259  G4String sname = aline.substr(idx, aline.size()-idx);
260  sessionMap[aname] = sname;
261  }
262  iline++;
263  }
264  fsession.close();
265 
266  G4String stype = "";
267  std::map<G4String, G4String>::iterator it = sessionMap.find(appname);
268  if ( it != sessionMap.end() ) stype = sessionMap[appname];
269  else stype = default_session;
270  stype.toLower();
271 
272  // select session...
273  if ( qt_build && stype == "qt" ) selected = kQt;
274  else if ( xm_build && stype == "xm" ) selected = kXm;
275  else if ( win32_build && stype == "win32" ) selected = kWin32;
276  //else if ( wt_build && stype == "wt" ) selected = kWt;
277  else if ( stype == "gag" ) selected = kGag;
278  else if ( tcsh_build && stype == "tcsh" ) selected = kTcsh;
279  else if ( stype == "csh" ) selected = kCsh;
280 }
281 
282 // --------------------------------------------------------------------------
284 {
285  if ( qt_build ) selected = kQt;
286  else if ( tcsh_build ) selected = kTcsh;
287  else if ( xm_build ) selected = kXm;
288 }
289 
290 // --------------------------------------------------------------------------
292 {
293  if(shell) shell-> SetPrompt(prompt);
294 }
295 
296 // --------------------------------------------------------------------------
298  TermColorIndex cmdColor)
299 {
300  if(shell) shell-> SetLsColor(dirColor, cmdColor);
301 }
302 
303 // --------------------------------------------------------------------------
305 {
306  session-> SessionStart();
307 }
void SetLsColor(TermColorIndex dirColor, TermColorIndex cmdColor)
static const G4bool wt_build
static const G4bool win32_build
SessionType selected
G4String strip(G4int strip_Type=trailing, char c=' ')
static const G4bool qt_build
#define DISCARD_PARAMETER(p)
int G4int
Definition: G4Types.hh:78
void SelectSessionByBestGuess()
void SelectSessionByArg(const G4String &stype)
void SetPrompt(const G4String &prompt)
G4GLOB_DLL std::ostream G4cout
G4UIExecutive(G4int argc, char **argv, const G4String &type="")
bool G4bool
Definition: G4Types.hh:79
std::map< G4String, G4String > sessionMap
G4UIsession * session
static const G4bool xm_build
static G4UIterminal * session
void toLower()
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
void SelectSessionByEnv()
TermColorIndex
Definition: G4VUIshell.hh:54
static const G4bool tcsh_build
G4VUIshell * shell
void SelectSessionByFile(const G4String &appname)
#define G4endl
Definition: G4ios.hh:61