b85b0aba2596dbbfeea661a58dfe6d69f98f1e36
[occt.git] / src / Graphic3d / Graphic3d_GraphicDevice.cxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 //
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
8 //
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 //
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
18
19 /***********************************************************************
20
21      FONCTION :
22      ----------
23         Classe Graphic3d_GraphicDevice.cxx :
24
25
26      HISTORIQUE DES MODIFICATIONS   :
27      --------------------------------
28       Janvier 1994 : CAL ; Creation.
29       04-02-97  : FMN ; Suppression appel call_tox_set_colormap_mapping()
30       10-09-00  : GG  ; NEW OpenGl driver loading specification
31                         when nothing is defined the driver libTKOpenGl[.so|.sl]
32                         is loading from the current PATH
33
34 ************************************************************************/
35 #if (!defined(_WIN32) && !defined(__WIN32__))
36
37 #include <stdio.h>
38 #include <sys/utsname.h>
39
40 /*----------------------------------------------------------------------*/
41 /*
42  * Includes
43  */
44
45 #include <Graphic3d_GraphicDevice.ixx>
46 #include <Graphic3d_GraphicDriver.hxx>
47 #include <OSD_Function.hxx>
48 #include <OSD_Environment.hxx>
49 #include <TCollection_AsciiString.hxx>
50
51 #include <Xw_Cextern.hxx>
52
53 //-Static data definitions
54
55 static char LocalMessag[80] ;
56
57 //-Aliases
58
59 //-Global data definitions
60
61 //-Constructor
62
63 Graphic3d_GraphicDevice::Graphic3d_GraphicDevice (const Standard_CString Connexion, const Xw_TypeOfMapping /*Mapping*/, const Standard_Integer Ncolors, const Standard_Boolean UseDefault ):
64 Xw_GraphicDevice () {
65 Standard_CString connexion = (Connexion) ? Connexion : Standard_CString("") ;
66 Standard_Boolean status ;
67
68         MyExtendedDisplay = Xw_open_display((Standard_PCharacter)connexion) ;
69
70         if( !MyExtendedDisplay ) {
71            sprintf(LocalMessag,"Cannot connect to server '%s'",connexion) ;
72            Aspect_GraphicDeviceDefinitionError::Raise (LocalMessag);
73         }
74
75         SetGraphicDriver ();
76
77         status = MyGraphicDriver->Begin (connexion);
78
79         if( !status ) {
80            sprintf(LocalMessag,"Cannot connect to graphic library from '%s'",
81                 connexion) ;
82            Aspect_GraphicDeviceDefinitionError::Raise (LocalMessag);
83         }
84
85         this->InitMaps (connexion,Xw_TOM_READONLY,Ncolors,UseDefault);
86
87
88 }
89
90 Graphic3d_GraphicDevice::Graphic3d_GraphicDevice (const Aspect_Display pdisplay)
91   : Xw_GraphicDevice ()
92  {
93   Standard_Boolean status ;
94
95   if( !pdisplay )
96     Aspect_GraphicDeviceDefinitionError::Raise ("Bad display pointer");
97   MyExtendedDisplay = Xw_set_display(pdisplay) ;
98   Standard_CString connexion = Xw_get_display_name(MyExtendedDisplay);
99
100   if( !MyExtendedDisplay ) {
101     if( connexion )
102       sprintf(LocalMessag,"Cannot connect to server '%s'",connexion) ;
103     else
104       sprintf(LocalMessag,"Cannot connect to an UNKNOWN server") ;
105     Aspect_GraphicDeviceDefinitionError::Raise (LocalMessag);
106   }
107
108   SetGraphicDriver ();
109
110   status = MyGraphicDriver->Begin (pdisplay);
111
112   if( !status ) {
113     sprintf(LocalMessag,"Cannot connect to graphic library from '%s'",
114             connexion) ;
115     Aspect_GraphicDeviceDefinitionError::Raise (LocalMessag);
116   }
117
118   this->InitMaps (connexion,Xw_TOM_READONLY,0,Standard_True);
119 }
120
121 // Destructor
122 void Graphic3d_GraphicDevice::Destroy()
123 {
124   MyGraphicDriver->End();
125 }
126
127 // Methods in order
128
129 Handle(Aspect_GraphicDriver) Graphic3d_GraphicDevice::GraphicDriver () const {
130
131         return MyGraphicDriver;
132
133 }
134
135 void Graphic3d_GraphicDevice::SetGraphicDriver () {
136
137   TCollection_AsciiString aShr = ShrEnvString ();
138
139   OSD_SharedLibrary TheSharedLibrary (aShr.ToCString());
140
141   Standard_Boolean Result = TheSharedLibrary.DlOpen (OSD_RTLD_LAZY);
142
143         if (! Result) {
144             Aspect_GraphicDeviceDefinitionError::Raise
145                 (TheSharedLibrary.DlError ());
146         }
147         else {
148             // Management of traces
149 char *tracevalue = NULL;
150             tracevalue = (char *)(getenv ("CSF_GraphicTrace"));
151             if (tracevalue)
152                 cout << "Information : " << aShr << " loaded\n" << flush;
153
154 OSD_Function new_GLGraphicDriver =
155             TheSharedLibrary.DlSymb ("MetaGraphicDriverFactory");
156             if (tracevalue)
157                 cout << "Information : MetaGraphicDriverFactory "
158                 << (new_GLGraphicDriver ? "found\n" : "not found\n") << flush;
159             if (! new_GLGraphicDriver) {
160                 Aspect_GraphicDeviceDefinitionError::Raise
161                         (TheSharedLibrary.DlError ());
162             }
163             else {
164                 // Sequence :
165                 // new_GLGraphicDriver is OSD_Function :
166                 // typedef int (* OSD_Function)(...);
167                 // that is why good cast in GraphicDriver.
168                 Handle(Graphic3d_GraphicDriver)
169                         (*fp) (Standard_CString) = NULL;
170                 fp = (Handle(Graphic3d_GraphicDriver)
171                         (*) (Standard_CString)) new_GLGraphicDriver;
172                 if (tracevalue) cout << "FP == "<<(void *) fp<<endl;
173                 Standard_CString aSharedName = TheSharedLibrary.Name ();
174                 MyGraphicDriver = (*fp) (aSharedName);
175
176 //              MyGraphicDriver = (*fp) (TheSharedLibrary.Name ());
177
178                 // Management of traces
179                 if (tracevalue)
180                     MyGraphicDriver->SetTrace
181                         ((Standard_Integer) atoi (tracevalue));
182             }
183         }
184
185 }
186
187 TCollection_AsciiString Graphic3d_GraphicDevice::ShrEnvString() const
188 {
189   OSD_Environment aEnvShr ("CSF_GraphicShr");
190   if (!aEnvShr.Value().IsEmpty())
191   {
192     return aEnvShr.Value();
193   }
194
195   // load TKOpenGl using default searching mechanisms in system
196 #if defined(__hpux) || defined(HPUX)
197   return TCollection_AsciiString ("libTKOpenGl.sl");
198 #elif defined(__APPLE__)
199   return TCollection_AsciiString ("libTKOpenGl.dylib");
200 #else
201   return TCollection_AsciiString ("libTKOpenGl.so");
202 #endif
203 }
204
205 #endif // !WNT