Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 // $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_XM_SESSION)
38 #include "G4UIXm.hh"
39 #endif
40 
41 #if defined(G4UI_BUILD_WIN32_SESSION)
42 #include "G4UIWin32.hh"
43 #endif
44 
45 #if defined(G4UI_BUILD_WT_SESSION)
46 //#include "G4UIWt.hh"
47 #endif
48 
49 #include "G4UIGAG.hh"
50 #include "G4UIterminal.hh"
51 #include "G4UItcsh.hh"
52 #include "G4UIcsh.hh"
53 
54 // --------------------------------------------------------------------------
55 // build flags as variables
56 
57 #if defined(G4UI_BUILD_QT_SESSION)
58 static const G4bool qt_build = true;
59 #else
60 static const G4bool qt_build = false;
61 #endif
62 
63 #if defined(G4UI_BUILD_XM_SESSION)
64 static const G4bool xm_build = true;
65 #else
66 static const G4bool xm_build = false;
67 #endif
68 
69 #if defined(G4UI_BUILD_WIN32_SESSION)
70 static const G4bool win32_build = true;
71 #else
72 static const G4bool win32_build = false;
73 #endif
74 
75 #if defined(G4UI_BUILD_WT_SESSION)
76 static const G4bool wt_build = true;
77 #else
78 static const G4bool wt_build = false;
79 #endif
80 
81 #ifndef WIN32
82 static const G4bool tcsh_build = true;
83 #else
84 static const G4bool tcsh_build = false;
85 #endif
86 
87 #define DISCARD_PARAMETER(p) (void)p
88 
89 // --------------------------------------------------------------------------
90 G4UIExecutive::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 #endif
147  break;
148  case kWt:
149 #if defined(G4UI_BUILD_WT_SESSION)
150  //session = new G4UIWt(argc, argv);
151  //isGUI = true;
152 #endif
153  break;
154  case kGag:
155  DISCARD_PARAMETER(argc);
156  DISCARD_PARAMETER(argv);
157  session = new G4UIGAG();
158  isGUI = true;
159  break;
160  case kTcsh:
161 #ifndef WIN32
162  DISCARD_PARAMETER(argc);
163  DISCARD_PARAMETER(argv);
164  shell = new G4UItcsh;
165  session = new G4UIterminal(shell);
166 #endif
167  break;
168  case kCsh:
169  DISCARD_PARAMETER(argc);
170  DISCARD_PARAMETER(argv);
171  shell = new G4UIcsh;
172  session = new G4UIterminal(shell);
173  default:
174  break;
175  }
176 
177  // fallback (csh)
178  if ( session == NULL ) {
179  G4Exception("G4UIExecutive::G4UIExecutive()",
180  "UI0002",
181  JustWarning,
182  "Specified session type is not build in your system,\n"
183  "or no session type is specified.\n"
184  "A fallback session type is used.");
185 
186  selected = kCsh;
187  DISCARD_PARAMETER(argc);
188  DISCARD_PARAMETER(argv);
189  shell = new G4UIcsh;
190  session = new G4UIterminal(shell);
191  }
192 }
193 
194 // --------------------------------------------------------------------------
196 {
197  if ( selected != kWt ) delete session;
198 }
199 
200 // --------------------------------------------------------------------------
201 void G4UIExecutive::SelectSessionByArg(const G4String& stype)
202 {
203  if ( qt_build && stype == "qt" ) selected = kQt;
204  else if ( xm_build && stype == "xm" ) selected = kXm;
205  else if ( win32_build && stype == "win32" ) selected = kWin32;
206  //else if ( wt_build && stype == "wt" ) selected = kWt;
207  else if ( stype == "gag" ) selected = kGag;
208  else if ( tcsh_build && stype == "tcsh" ) selected = kTcsh;
209  else if ( stype == "csh" ) selected = kCsh;
210 }
211 
212 // --------------------------------------------------------------------------
213 void G4UIExecutive::SelectSessionByEnv()
214 {
215  if ( qt_build && getenv("G4UI_USE_QT") ) selected = kQt;
216  else if ( xm_build && getenv("G4UI_USE_XM") ) selected = kXm;
217  else if ( win32_build && getenv("G4UI_USE_WIN32") ) selected = kWin32;
218  //else if ( wt_build && getenv("G4UI_USE_WT") ) selected = kWt;
219  else if ( getenv("G4UI_USE_GAG") ) selected = kGag;
220  else if ( tcsh_build && getenv("G4UI_USE_TCSH") ) selected = kTcsh;
221 }
222 
223 // --------------------------------------------------------------------------
224 void G4UIExecutive::SelectSessionByFile(const G4String& appname)
225 {
226  const char* path = getenv("HOME");
227  if( path == NULL ) return;
228  G4String homedir = path;
229 
230 #ifndef WIN32
231  G4String fname= homedir + "/.g4session";
232 #else
233  G4String fname= homedir + "\\.g4session";
234 #endif
235 
236  std::ifstream fsession;
237  enum { BUFSIZE= 1024 }; char linebuf[BUFSIZE];
238 
239  fsession.open(fname, std::ios::in);
240 
241  G4String default_session = "";
242  G4int iline = 1;
243  sessionMap.clear();
244  while( fsession.good() ) {
245  if( fsession.eof()) break;
246  fsession.getline(linebuf, BUFSIZE);
247  G4String aline = linebuf;
248  aline.strip(G4String::both);
249  if ( aline(0) == '#' ) continue;
250  if ( iline == 1 )
251  default_session = aline;
252  else {
253  size_t idx = aline.find_first_of(" ");
254  if ( idx == G4String::npos ) break;
255  G4String aname = aline.substr(0, idx);
256  idx = aline.find_first_not_of(" ", idx);
257  if (idx == G4String::npos ) break;
258  G4String sname = aline.substr(idx, aline.size()-idx);
259  sessionMap[aname] = sname;
260  }
261  iline++;
262  }
263  fsession.close();
264 
265  G4String stype = "";
266  std::map<G4String, G4String>::iterator it = sessionMap.find(appname);
267  if ( it != sessionMap.end() ) stype = sessionMap[appname];
268  else stype = default_session;
269  stype.toLower();
270 
271  // select session...
272  if ( qt_build && stype == "qt" ) selected = kQt;
273  else if ( xm_build && stype == "xm" ) selected = kXm;
274  else if ( win32_build && stype == "win32" ) selected = kWin32;
275  //else if ( wt_build && stype == "wt" ) selected = kWt;
276  else if ( stype == "gag" ) selected = kGag;
277  else if ( tcsh_build && stype == "tcsh" ) selected = kTcsh;
278  else if ( stype == "csh" ) selected = kCsh;
279 }
280 
281 // --------------------------------------------------------------------------
282 void G4UIExecutive::SelectSessionByBestGuess()
283 {
284  if ( qt_build ) selected = kQt;
285  else if ( tcsh_build ) selected = kTcsh;
286  else if ( xm_build ) selected = kXm;
287 }
288 
289 // --------------------------------------------------------------------------
291 {
292  if(shell) shell-> SetPrompt(prompt);
293 }
294 
295 // --------------------------------------------------------------------------
297  TermColorIndex cmdColor)
298 {
299  if(shell) shell-> SetLsColor(dirColor, cmdColor);
300 }
301 
302 // --------------------------------------------------------------------------
304 {
305  session-> SessionStart();
306 }