]> SALOME platform Git repositories - modules/gui.git/blob - src/Session/SALOME_Session_Server.cxx
Salome HOME
ptv, do not send mouse release event just after mouse double click
[modules/gui.git] / src / Session / SALOME_Session_Server.cxx
1 // SALOME Session : implementation of Session.idl
2 //
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 // File : SALOME_Session_Server.cxx
25 // Author : Paul RASCLE, EDF
26 // Module : SALOME
27
28 #include <Container_init_python.hxx>
29 #include "Utils_ORB_INIT.hxx"
30 #include "Utils_SINGLETON.hxx"
31 #include "SALOME_NamingService.hxx"
32 #include "SALOMETraceCollector.hxx"
33
34 #include "InquireServersQThread.h" // splash
35
36 #include <iostream>
37 #include <unistd.h>
38
39 #include <qdir.h>
40 #include <qfile.h>
41 #include <qapplication.h>
42 #include <qwaitcondition.h>
43
44 #include "Utils_SALOME_Exception.hxx"
45 #include "Utils_CorbaException.hxx"
46 #include "SALOME_Event.hxx"
47
48 #include <SALOMEconfig.h>
49 #include CORBA_SERVER_HEADER(SALOME_Session)
50 #include CORBA_SERVER_HEADER(SALOMEDS)
51
52 #include <utilities.h>
53 #include "Session_Session_i.hxx"
54 #include "Session_ServerLauncher.hxx"
55
56 #include "SUIT_Tools.h"
57 #include "SUIT_Session.h"
58 #include "SUIT_Application.h"
59 #include "SUIT_MessageBox.h"
60
61 #include "SUIT_ExceptionHandler.h"
62
63 extern "C" int HandleSignals( QApplication *theQApplication );
64
65 /*! - read arguments, define list of server to launch with their arguments.
66  * - wait for naming service
67  * - create and run a thread for launch of all servers
68  *
69 */
70
71 //! CORBA server for SALOME Session
72 /*!
73  * SALOME_Session Server launches a SALOME session servant.
74  * The servant registers to the Naming Service.
75  * See SALOME_Session.idl for interface specification.
76  *
77  * Main services offered by the servant are:
78  * - launch GUI
79  * - stop Session ( must be idle )
80  * - get session state
81  */
82
83 QString salomeVersion()
84 {
85   QString path( ::getenv( "GUI_ROOT_DIR" ) );
86   if ( !path.isEmpty() )
87     path += QDir::separator();
88   path += QString( "bin/salome/VERSION" );
89
90   QFile vf( path );
91   if ( !vf.open( IO_ReadOnly ) )
92     return QString::null;
93
94   QString line;
95   vf.readLine( line, 1024 );
96   vf.close();
97
98   if ( line.isEmpty() )
99     return QString::null;
100
101   while ( !line.isEmpty() && line.at( line.length() - 1 ) == QChar( '\n' ) )
102     line.remove( line.length() - 1, 1 );
103
104   QString ver;
105   int idx = line.findRev( ":" );
106   if ( idx != -1 )
107     ver = line.mid( idx + 1 ).stripWhiteSpace();
108
109   return ver;
110 }
111
112 PyObject* salome_shared_modules_module = 0;
113
114 void MessageOutput( QtMsgType type, const char* msg )
115 {
116   switch ( type )
117   {
118   case QtDebugMsg:
119     MESSAGE( "Debug: " << msg );
120     break;
121   case QtWarningMsg:
122     MESSAGE( "Warning: " << msg );
123     break;
124   case QtFatalMsg:
125     MESSAGE( "Fatal: " << msg );
126     break;
127   }
128 }
129
130 /* XPM */
131 static const char* pixmap_not_found_xpm[] = {
132 "16 16 3 1",
133 "       c None",
134 ".      c #000000",
135 "+      c #A80000",
136 "                ",
137 "                ",
138 "    .     .     ",
139 "   .+.   .+.    ",
140 "  .+++. .+++.   ",
141 "   .+++.+++.    ",
142 "    .+++++.     ",
143 "     .+++.      ",
144 "    .+++++.     ",
145 "   .+++.+++.    ",
146 "  .+++. .+++.   ",
147 "   .+.   .+.    ",
148 "    .     .     ",
149 "                ",
150 "                ",
151 "                "};
152
153 class SALOME_Session : public SUIT_Session
154 {
155 public:
156   SALOME_Session() : SUIT_Session() {}
157   virtual ~SALOME_Session() {}
158
159 protected:
160   virtual SUIT_ResourceMgr* createResourceMgr( const QString& appName ) const
161   {
162     SUIT_ResourceMgr* resMgr = new SUIT_ResourceMgr( appName, QString( "%1Config" ) );
163     resMgr->setVersion( salomeVersion() );
164     resMgr->setCurrentFormat( "xml" );
165     resMgr->setOption( "translators", QString( "%P_msg_%L.qm|%P_icons.qm|%P_images.qm" ) );
166     static QPixmap defaultPixmap( pixmap_not_found_xpm );
167     resMgr->setDefaultPixmap( defaultPixmap );
168     return resMgr;
169   }
170 };
171
172 class SALOME_QApplication : public QApplication
173 {
174 public:
175   SALOME_QApplication( int& argc, char** argv ) : QApplication( argc, argv ), myHandler ( 0 ) {}
176
177   virtual bool notify( QObject* receiver, QEvent* e )
178   {
179     return myHandler ? myHandler->handle( receiver, e ) :
180       QApplication::notify( receiver, e );
181   }
182   SUIT_ExceptionHandler* handler() const { return myHandler; }
183   void setHandler( SUIT_ExceptionHandler* h ) { myHandler = h; }
184
185 private:
186   SUIT_ExceptionHandler* myHandler;
187 };
188
189 // class which calls SALOME::Session::GetInterface() from another thread
190 // to avoid mutual lock ( if called from the same thread as main()
191 class GetInterfaceThread : public QThread
192 {
193 public:
194   GetInterfaceThread( SALOME::Session_var s ) : session ( s ) {}
195 protected:
196   virtual void run()
197   {
198     if ( !CORBA::is_nil( session ) )
199       session->GetInterface();
200     else
201       printf( "\nFATAL ERROR: SALOME::Session object is nil! Can not display GUI\n\n" );
202   }
203 private:
204   SALOME::Session_var session;
205 };
206
207 // returns true if 'str' is found in argv
208 bool isFound( const char* str, int argc, char** argv )
209 {
210   for ( int i = 1; i <= ( argc-1 ); i++ )
211     if ( !strcmp( argv[i], str ) )
212       return true;
213   return false;
214 }
215
216 // ---------------------------- MAIN -----------------------
217 int main( int argc, char **argv )
218 {
219   qInstallMsgHandler( MessageOutput );
220
221   // QApplication should be create before all other operations
222   // When uses QApplication::libraryPaths() ( example, QFile::encodeName() )
223   // qApp used for detection of the executable dir path.
224   SALOME_QApplication _qappl( argc, argv );
225   ASSERT( QObject::connect( &_qappl, SIGNAL( lastWindowClosed() ), &_qappl, SLOT( quit() ) ) );
226
227   QString path = QDir::convertSeparators( SUIT_Tools::addSlash( QString( ::getenv( "GUI_ROOT_DIR" ) ) ) + QString( "bin/salome" ) );
228   _qappl.addLibraryPath( path );
229   
230   _qappl.setStyle( "salome" );
231
232   int result = -1;
233
234   CORBA::ORB_var orb;
235   PortableServer::POA_var poa;
236
237   SUIT_Session* aGUISession = 0;
238   SALOME_NamingService* _NS = 0;
239   GetInterfaceThread* guiThread = 0;
240   SALOMETraceCollector* myThreadTrace = 0;
241   Session_ServerLauncher* myServerLauncher = 0;
242
243   try {
244     
245     // Python initialisation : only once
246
247     int _argc = 1;
248     char* _argv[] = {""};
249     KERNEL_PYTHON::init_python( _argc,_argv );
250     PyEval_RestoreThread( KERNEL_PYTHON::_gtstate );
251     if ( !KERNEL_PYTHON::salome_shared_modules_module ) // import only once
252       KERNEL_PYTHON::salome_shared_modules_module = PyImport_ImportModule( "salome_shared_modules" );
253     if ( !KERNEL_PYTHON::salome_shared_modules_module )
254     {
255       INFOS( "salome_shared_modules_module == NULL" );
256       PyErr_Print();
257       PyErr_Clear();
258     }
259     PyEval_ReleaseThread( KERNEL_PYTHON::_gtstate );
260
261     // Create ORB, get RootPOA object, NamingService, etc.
262     ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
263     ASSERT( SINGLETON_<ORB_INIT>::IsAlreadyExisting() );
264     int orbArgc = 1;
265     orb = init( orbArgc, argv );
266     myThreadTrace = SALOMETraceCollector::instance( orb );
267
268     // Install SALOME thread event handler
269     SALOME_Event::GetSessionThread();
270
271     CORBA::Object_var obj = orb->resolve_initial_references( "RootPOA" );
272     poa = PortableServer::POA::_narrow( obj );
273
274     PortableServer::POAManager_var pman = poa->the_POAManager();
275     pman->activate() ;
276     INFOS( "pman->activate()" );
277
278     _NS = new SALOME_NamingService( orb );
279
280     result = 0;
281   }
282   catch ( SALOME_Exception& e ) {
283     INFOS( "run(): SALOME::SALOME_Exception is caught: "<<e.what() );
284   }
285   catch ( CORBA::SystemException& e ) {
286     INFOS( "Caught CORBA::SystemException." );
287   }
288   catch ( CORBA::Exception& e ) {
289     INFOS( "Caught CORBA::Exception." );
290     CORBA::Any tmp;
291     tmp<<= e;
292     CORBA::TypeCode_var tc = tmp.type();
293     const char *p = tc->name();
294     INFOS ( "run(): CORBA exception of the kind : "<<p<< " is caught" );
295   }
296   catch ( exception& e ) {
297     INFOS( "run(): An exception has been caught: " <<e.what() );
298   }
299   catch (...) {
300     INFOS( "Caught unknown exception." );
301   }
302
303   // CORBA Servant Launcher
304   QMutex _GUIMutex;
305   QWaitCondition _ServerLaunch, _SessionStarted;
306
307   if ( !result )
308   {
309     _GUIMutex.lock();  // to block Launch server thread until wait( mutex )
310
311     // Activate embedded CORBA servers: Registry, SALOMEDS, etc.
312     myServerLauncher = new Session_ServerLauncher( argc, argv, orb, poa, &_GUIMutex, &_ServerLaunch, &_SessionStarted );
313     myServerLauncher->start();
314
315     _ServerLaunch.wait( &_GUIMutex ); // to be reseased by Launch server thread when ready:
316     
317     // show splash screen if "SPLASH" parameter was passed ( default )
318     if ( isFound( "SPLASH", argc, argv ) )
319     {
320       // create temporary resource manager just to load splash icon
321       SUIT_ResourceMgr resMgr( "SalomeApp", QString( "%1Config" ) );
322       resMgr.setVersion( salomeVersion() );
323       resMgr.setCurrentFormat( "xml" );
324       resMgr.loadLanguage( "SalomeApp", "en" );
325
326       // create splash object: widget ( splash with progress bar ) and "pinging" thread
327       InquireServersGUI splash;
328       splash.setPixmap( resMgr.loadPixmap( "SalomeApp", QObject::tr( "ABOUT" ) ) );
329       SUIT_Tools::centerWidget( &splash, _qappl.desktop() );
330       
331       _qappl.setMainWidget( &splash );
332       QObject::connect( &_qappl, SIGNAL( lastWindowClosed() ), &_qappl, SLOT( quit() ) );
333       splash.show(); // display splash with running progress bar
334       _qappl.exec(); // wait untill splash closes ( progress runs till end or Cancel is pressed )
335       
336       result = splash.getExitStatus(); // 1 is error
337     }
338     else
339       _SessionStarted.wait();
340   }
341
342   // call Session::GetInterface() if "GUI" parameter was passed ( default )
343   if ( !result && isFound( "GUI", argc, argv ) )
344   {
345     CORBA::Object_var obj = _NS->Resolve( "/Kernel/Session" );
346     SALOME::Session_var session = SALOME::Session::_narrow( obj ) ;
347     ASSERT ( ! CORBA::is_nil( session ) );
348
349     INFOS( "Session activated, Launch IAPP..." );
350     guiThread = new GetInterfaceThread( session );
351     guiThread->start();
352   }
353
354   if ( !result )
355   {
356
357     // GUI activation
358     // Allow multiple activation/deactivation of GUI
359     while ( true )
360     {
361       MESSAGE( "waiting wakeAll()" );
362       _ServerLaunch.wait( &_GUIMutex ); // to be reseased by Launch server thread when ready:
363       // atomic operation lock - unlock on mutex
364       // unlock mutex: serverThread runs, calls _ServerLaunch->wakeAll()
365       // this thread wakes up, and lock mutex
366
367       _GUIMutex.unlock();
368
369       // SUIT_Session creation
370       aGUISession = new SALOME_Session();
371
372       // Load SalomeApp dynamic library
373       INFOS( "creation SUIT_Application" );
374       SUIT_Application* aGUIApp = aGUISession->startApplication( "SalomeApp", 0, 0 );
375       if ( aGUIApp )
376       {
377         _qappl.setHandler( aGUISession->handler() ); // after loading SalomeApp application
378                                                      // aGUISession contains SalomeApp_ExceptionHandler
379         // Run GUI loop
380         MESSAGE( "run(): starting the main event loop" );
381         result = _qappl.exec();
382
383         if ( result == SUIT_Session::FROM_GUI ) // desktop is closed by user from GUI
384           break;
385       }
386
387       delete aGUISession;
388       aGUISession = 0;
389
390       // Prepare _GUIMutex for a new GUI activation
391       _GUIMutex.lock();
392     }
393   }
394
395   if ( myServerLauncher )
396     myServerLauncher->KillAll(); // kill embedded servers
397
398   delete aGUISession;
399   delete guiThread;
400   delete myServerLauncher;
401   delete _NS;
402   delete myThreadTrace;
403
404   return result;
405 }