0032433: Visualization, TKService - introduce Wasm_Window implementing Aspect_Window...
[occt.git] / src / Aspect / Aspect_DisplayConnection.cxx
1 // Copyright (c) 2013-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <Aspect_DisplayConnection.hxx>
15
16 #include <Aspect_DisplayConnectionDefinitionError.hxx>
17 #include <OSD_Environment.hxx>
18
19 #if defined(HAVE_XLIB)
20   #include <X11/Xlib.h>
21   #include <X11/Xutil.h>
22 #endif
23
24 IMPLEMENT_STANDARD_RTTIEXT(Aspect_DisplayConnection,Standard_Transient)
25
26 // =======================================================================
27 // function : Aspect_DisplayConnection
28 // purpose  :
29 // =======================================================================
30 Aspect_DisplayConnection::Aspect_DisplayConnection()
31 {
32 #if defined(HAVE_XLIB)
33   myDisplay = NULL;
34   myDefVisualInfo = NULL;
35   myDefFBConfig = NULL;
36   myIsOwnDisplay = false;
37   OSD_Environment anEnv ("DISPLAY");
38   myDisplayName = anEnv.Value();
39   Init (NULL);
40 #endif
41 }
42
43 // =======================================================================
44 // function : ~Aspect_DisplayConnection
45 // purpose  :
46 // =======================================================================
47 Aspect_DisplayConnection::~Aspect_DisplayConnection()
48 {
49 #if defined(HAVE_XLIB)
50   if (myDefVisualInfo != NULL)
51   {
52     XFree (myDefVisualInfo);
53   }
54   if (myDisplay != NULL
55    && myIsOwnDisplay)
56   {
57     XCloseDisplay ((Display* )myDisplay);
58   }
59 #endif
60 }
61
62 // =======================================================================
63 // function : Aspect_DisplayConnection
64 // purpose  :
65 // =======================================================================
66 Aspect_DisplayConnection::Aspect_DisplayConnection (const TCollection_AsciiString& theDisplayName)
67 : myDisplay (NULL),
68   myDefVisualInfo (NULL),
69   myDefFBConfig (NULL),
70   myIsOwnDisplay (false)
71 {
72   myDisplayName = theDisplayName;
73   Init (NULL);
74 }
75
76 // =======================================================================
77 // function : Aspect_DisplayConnection
78 // purpose  :
79 // =======================================================================
80 Aspect_DisplayConnection::Aspect_DisplayConnection (Aspect_XDisplay* theDisplay)
81 : myDisplay (NULL),
82   myDefVisualInfo (NULL),
83   myDefFBConfig (NULL),
84   myIsOwnDisplay (false)
85 {
86   Init (theDisplay);
87 }
88
89 // =======================================================================
90 // function : SetDefaultVisualInfo
91 // purpose  :
92 // =======================================================================
93 void Aspect_DisplayConnection::SetDefaultVisualInfo (Aspect_XVisualInfo* theVisual,
94                                                      Aspect_FBConfig theFBConfig)
95 {
96   if (myDefVisualInfo != NULL)
97   {
98   #if defined(HAVE_XLIB)
99     XFree (myDefVisualInfo);
100   #endif
101   }
102   myDefVisualInfo = theVisual;
103   myDefFBConfig = theFBConfig;
104 }
105
106 // =======================================================================
107 // function : Init
108 // purpose  :
109 // =======================================================================
110 void Aspect_DisplayConnection::Init (Aspect_XDisplay* theDisplay)
111 {
112 #if defined(HAVE_XLIB)
113   if (myDisplay != NULL
114    && myIsOwnDisplay)
115   {
116     XCloseDisplay ((Display* )myDisplay);
117   }
118   myIsOwnDisplay = false;
119   myAtoms.Clear();
120
121   myDisplay = theDisplay != NULL ? theDisplay : (Aspect_XDisplay* )XOpenDisplay (myDisplayName.ToCString());
122   if (myDisplay == NULL)
123   {
124     TCollection_AsciiString aMessage;
125     aMessage += "Can not connect to the server \"";
126     aMessage += myDisplayName + "\"";
127     throw Aspect_DisplayConnectionDefinitionError(aMessage.ToCString());
128   }
129   else
130   {
131     myIsOwnDisplay = theDisplay == NULL;
132     myAtoms.Bind (Aspect_XA_DELETE_WINDOW, (uint64_t )XInternAtom((Display* )myDisplay, "WM_DELETE_WINDOW", False));
133   }
134 #else
135   myDisplay = theDisplay;
136   myIsOwnDisplay = theDisplay == NULL;
137 #endif
138 }