1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup GHOST
22  * Declaration of GHOST_WindowX11 class.
23  */
24 
25 #pragma once
26 
27 #include "GHOST_Window.h"
28 #include <X11/Xlib.h>
29 #include <X11/Xutil.h>
30 // For tablets
31 #ifdef WITH_X11_XINPUT
32 #  include <X11/extensions/XInput.h>
33 #endif
34 
35 #include "GHOST_TaskbarX11.h"
36 
37 #include <map>
38 
39 class GHOST_SystemX11;
40 
41 #ifdef WITH_XDND
42 class GHOST_DropTargetX11;
43 #endif
44 
45 /**
46  * X11 implementation of GHOST_IWindow.
47  * Dimensions are given in screen coordinates that are
48  * relative to the upper-left corner of the screen.
49  */
50 
51 class GHOST_WindowX11 : public GHOST_Window {
52  public:
53   /**
54    * Constructor.
55    * Creates a new window and opens it.
56    * To check if the window was created properly, use the getValid() method.
57    * \param title     The text shown in the title bar of the window.
58    * \param left      The coordinate of the left edge of the window.
59    * \param top       The coordinate of the top edge of the window.
60    * \param width     The width the window.
61    * \param height    The height the window.
62    * \param state     The state the window is initially opened with.
63    * \param parentWindow  Parent (embedder) window
64    * \param type      The type of drawing context installed in this window.
65    * \param stereoVisual  Stereo visual for quad buffered stereo.
66    * \param alphaBackground Enable alpha blending of window with display background
67    */
68   GHOST_WindowX11(GHOST_SystemX11 *system,
69                   Display *display,
70                   const char *title,
71                   GHOST_TInt32 left,
72                   GHOST_TInt32 top,
73                   GHOST_TUns32 width,
74                   GHOST_TUns32 height,
75                   GHOST_TWindowState state,
76                   GHOST_WindowX11 *parentWindow,
77                   GHOST_TDrawingContextType type = GHOST_kDrawingContextTypeNone,
78                   const bool is_dialog = false,
79                   const bool stereoVisual = false,
80                   const bool exclusive = false,
81                   const bool alphaBackground = false,
82                   const bool is_debug = false);
83 
84   bool getValid() const;
85 
86   void setTitle(const char *title);
87 
88   std::string getTitle() const;
89 
90   void getWindowBounds(GHOST_Rect &bounds) const;
91 
92   void getClientBounds(GHOST_Rect &bounds) const;
93 
94   bool isDialog() const;
95 
96   GHOST_TSuccess setClientWidth(GHOST_TUns32 width);
97 
98   GHOST_TSuccess setClientHeight(GHOST_TUns32 height);
99 
100   GHOST_TSuccess setClientSize(GHOST_TUns32 width, GHOST_TUns32 height);
101 
102   void screenToClient(GHOST_TInt32 inX,
103                       GHOST_TInt32 inY,
104                       GHOST_TInt32 &outX,
105                       GHOST_TInt32 &outY) const;
106 
107   void clientToScreen(GHOST_TInt32 inX,
108                       GHOST_TInt32 inY,
109                       GHOST_TInt32 &outX,
110                       GHOST_TInt32 &outY) const;
111 
112   GHOST_TWindowState getState() const;
113 
114   GHOST_TSuccess setState(GHOST_TWindowState state);
115 
116   GHOST_TSuccess setOrder(GHOST_TWindowOrder order);
117 
118   GHOST_TSuccess invalidate();
119 
120   GHOST_TSuccess setProgressBar(float progress);
121   GHOST_TSuccess endProgressBar();
122 
123   /**
124    * Destructor.
125    * Closes the window and disposes resources allocated.
126    */
127   ~GHOST_WindowX11();
128 
129   /**
130    * \section x11specific X11 system specific calls
131    */
132 
133   /**
134    * The reverse of invalidate! Tells this window
135    * that all events for it have been pushed into
136    * the GHOST event queue.
137    */
138 
139   void validate();
140 
141   /**
142    * Return a handle to the x11 window type.
143    */
144   Window getXWindow();
145 
GetTabletData()146   GHOST_TabletData &GetTabletData()
147   {
148     return m_tabletData;
149   }
150 
151 #if defined(WITH_X11_XINPUT) && defined(X_HAVE_UTF8_STRING)
getX11_XIC()152   XIC getX11_XIC()
153   {
154     return m_xic;
155   }
156 
157   bool createX11_XIC();
158 #endif
159 
160 #ifdef WITH_X11_XINPUT
161   void refreshXInputDevices();
162 #endif
163 
164 #ifdef WITH_XDND
getDropTarget()165   GHOST_DropTargetX11 *getDropTarget()
166   {
167     return m_dropTarget;
168   }
169 #endif
170 
171   /*
172    * Need this in case that we want start the window
173    * in FullScree or Maximized state.
174    * Check GHOST_WindowX11.cpp
175    */
176   bool m_post_init;
177   GHOST_TWindowState m_post_state;
178 
179   GHOST_TSuccess beginFullScreen() const;
180 
181   GHOST_TSuccess endFullScreen() const;
182 
183   GHOST_TSuccess setDialogHints(GHOST_WindowX11 *parentWindow);
184 
185   GHOST_TUns16 getDPIHint();
186 
187  protected:
188   /**
189    * \param type  The type of rendering context create.
190    * \return Indication of success.
191    */
192   GHOST_Context *newDrawingContext(GHOST_TDrawingContextType type);
193 
194   /**
195    * Sets the cursor visibility on the window using
196    * native window system calls.
197    */
198   GHOST_TSuccess setWindowCursorVisibility(bool visible);
199 
200   /**
201    * Sets the cursor grab on the window using
202    * native window system calls.
203    */
204   GHOST_TSuccess setWindowCursorGrab(GHOST_TGrabCursorMode mode);
205 
206   GHOST_TGrabCursorMode getWindowCursorGrab() const;
207 
208   /**
209    * Sets the cursor shape on the window using
210    * native window system calls.
211    */
212   GHOST_TSuccess setWindowCursorShape(GHOST_TStandardCursor shape);
213   GHOST_TSuccess hasCursorShape(GHOST_TStandardCursor shape);
214 
215   /**
216    * Sets the cursor shape on the window using
217    * native window system calls (Arbitrary size/color).
218    */
219   GHOST_TSuccess setWindowCustomCursorShape(GHOST_TUns8 *bitmap,
220                                             GHOST_TUns8 *mask,
221                                             int sizex,
222                                             int sizey,
223                                             int hotX,
224                                             int hotY,
225                                             bool canInvertColor);
226 
227  private:
228   /* Force use of public constructor. */
229 
230   GHOST_WindowX11();
231 
232   GHOST_WindowX11(const GHOST_WindowX11 &);
233 
234   GHOST_TSuccess getStandardCursor(GHOST_TStandardCursor g_cursor, Cursor &xcursor);
235 
236   Cursor getEmptyCursor();
237 
238   Window m_window;
239   Display *m_display;
240   XVisualInfo *m_visualInfo;
241   void *m_fbconfig;
242 
243   GHOST_TWindowState m_normal_state;
244 
245   /** A pointer to the typed system class. */
246   GHOST_SystemX11 *m_system;
247 
248   /** Used to concatenate calls to invalidate() on this window. */
249   bool m_invalid_window;
250 
251   /** XCursor structure of an empty (blank) cursor */
252   Cursor m_empty_cursor;
253 
254   /** XCursor structure of the custom cursor */
255   Cursor m_custom_cursor;
256 
257   /** XCursor to show when cursor is visible */
258   Cursor m_visible_cursor;
259 
260   /** Cache of XC_* ID's to XCursor structures */
261   std::map<unsigned int, Cursor> m_standard_cursors;
262 
263   GHOST_TaskBarX11 m_taskbar;
264 
265 #ifdef WITH_XDND
266   GHOST_DropTargetX11 *m_dropTarget;
267 #endif
268 
269   GHOST_TabletData m_tabletData;
270 
271 #if defined(WITH_X11_XINPUT) && defined(X_HAVE_UTF8_STRING)
272   XIC m_xic;
273 #endif
274 
275   bool m_valid_setup;
276   bool m_is_debug_context;
277 
278   void icccmSetState(int state);
279   int icccmGetState() const;
280 
281   void netwmMaximized(bool set);
282   bool netwmIsMaximized() const;
283 
284   void netwmFullScreen(bool set);
285   bool netwmIsFullScreen() const;
286 
287   void motifFullScreen(bool set);
288   bool motifIsFullScreen() const;
289 };
290