nux-1.14.0
|
00001 /* 00002 * Copyright 2010 Inalogic® Inc. 00003 * 00004 * This program is free software: you can redistribute it and/or modify it 00005 * under the terms of the GNU Lesser General Public License, as 00006 * published by the Free Software Foundation; either version 2.1 or 3.0 00007 * of the License. 00008 * 00009 * This program is distributed in the hope that it will be useful, but 00010 * WITHOUT ANY WARRANTY; without even the implied warranties of 00011 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR 00012 * PURPOSE. See the applicable version of the GNU Lesser General Public 00013 * License for more details. 00014 * 00015 * You should have received a copy of both the GNU Lesser General Public 00016 * License along with this program. If not, see <http://www.gnu.org/licenses/> 00017 * 00018 * Authored by: Jay Taoko <jaytaoko@inalogic.com> 00019 * 00020 */ 00021 00022 00023 #ifndef GRAPHICSDISPLAYX11_H 00024 #define GRAPHICSDISPLAYX11_H 00025 00026 #include "Gfx_Interface.h" 00027 #include "GLTimer.h" 00028 #include "GLDeviceObjects.h" 00029 #include "GLRenderStates.h" 00030 00031 /* Xlib.h is the default header that is included and has the core functionallity */ 00032 #include <X11/Xlib.h> 00033 /* Xatom.h includes functionallity for creating new protocol messages */ 00034 #include <X11/Xatom.h> 00035 /* keysym.h contains keysymbols which we use to resolv what keys that are being pressed */ 00036 #include <X11/keysym.h> 00037 00038 #include <X11/extensions/xf86vmode.h> 00039 #include <X11/extensions/Xinerama.h> 00040 00041 namespace nux 00042 { 00043 00044 class Event; 00045 class MainFBO; 00046 class GpuDevice; 00047 class GraphicsEngine; 00048 class IOpenGLFrameBufferObject; 00049 00050 enum WindowStyle 00051 { 00052 WINDOWSTYLE_NORMAL, 00053 WINDOWSTYLE_PANEL, 00054 WINDOWSTYLE_DIALOG, 00055 WINDOWSTYLE_TOOL, 00056 WINDOWSTYLE_NOBORDER, 00057 }; 00058 00059 enum DndAction 00060 { 00061 DNDACTION_COPY, 00062 DNDACTION_MOVE, 00063 DNDACTION_LINK, 00064 DNDACTION_ASK, 00065 DNDACTION_PRIVATE, 00066 00067 DNDACTION_NONE, 00068 }; 00069 00070 #define NUX_THREADMSG (WM_APP+0) 00071 #define NUX_THREADMSG_START_RENDERING (WM_APP+1) // Connection established // start at WM_APP 00072 #define NUX_THREADMSG_CHILD_WINDOW_TERMINATED (WM_APP+2) // General failure - Wait Connection failed 00073 #define NUX_THREADMSG_THREAD_TERMINATED (WM_APP+3) // Set wParam = Thread ID, lParam = 0 00074 00075 #define _xdnd_max_type 100 00076 #define xdnd_version 5 00077 00078 // This will become GLWindow 00079 class GraphicsDisplay : public GraphicSystem 00080 { 00081 friend class GraphicsEngine; 00082 00083 private: 00084 Display *m_X11Display; 00085 int m_X11Screen; 00086 Window m_X11Window; 00087 XVisualInfo *m_X11VisualInfo; 00088 00089 int m_ParentWindow; 00090 GLXContext m_GLCtx; 00091 GLXFBConfig _fb_config; 00092 XSetWindowAttributes m_X11Attr; 00093 00094 int m_NumVideoModes; 00095 XF86VidModeModeInfo **m_X11VideoModes; 00096 /* original desktop mode which we save so we can restore it later */ 00097 XF86VidModeModeInfo m_X11OriginalVideoMode; 00098 00099 Atom m_WMDeleteWindow; 00100 Colormap m_X11Colormap; 00101 int m_BorderPixel; 00102 00103 int _x11_major; 00104 int _x11_minor; 00105 int _glx_major; 00106 int _glx_minor; 00107 bool _has_glx_13; 00108 00109 XEvent m_X11LastEvent; 00110 Bool m_X11RepeatKey; 00111 00112 TCHAR m_WindowClassName[256]; 00113 GLuint m_PixelFormat; // Holds The Results After Searching For A Match 00114 NString m_WindowTitle; 00115 00116 // size, position 00117 Size m_ViewportSize; 00118 Size m_WindowSize; 00119 Point m_WindowPosition; 00120 00121 // surface attibute; 00122 bool m_Fullscreen; 00123 unsigned int m_ScreenBitDepth; 00124 00125 // verifiy that the interface is properly created 00126 bool m_GfxInterfaceCreated; 00127 00128 // Device information 00129 void GetDisplayInfo(); 00130 int m_BestMode; 00131 00132 bool m_CreatedFromForeignWindow; 00133 00134 public: 00135 typedef void (*GrabReleaseCallback) (bool replaced, void *user_data); 00136 00137 typedef struct _DndSourceFuncs 00138 { 00139 nux::NBitmapData * (*get_drag_image) (void * data); 00140 std::list<const char *> (*get_drag_types) (void * data); 00141 const char * (*get_data_for_type) (const char * type, int *size, int *format, void * data); 00142 void (*drag_finished) (DndAction result, void * data); 00143 } DndSourceFuncs; 00144 00145 bool HasXPendingEvent() const; 00146 Display *GetX11Display() 00147 { 00148 return m_X11Display; 00149 } 00150 // Device 00151 int m_num_device_modes; 00152 00153 // Event object 00154 IEvent *m_pEvent; 00155 00156 bool IsGfxInterfaceCreated(); 00157 00159 00167 bool CreateOpenGLWindow ( 00168 const TCHAR *WindowTitle, 00169 unsigned int WindowWidth, 00170 unsigned int WindowHeight, 00171 WindowStyle Style, 00172 const GraphicsDisplay *Parent, 00173 bool FullscreenFlag = false, 00174 bool create_rendering_data = true); 00175 00177 00181 bool CreateFromOpenGLWindow (Display *X11Display, Window X11Window, GLXContext OpenGLContext); 00182 00183 void DestroyOpenGLWindow(); 00184 00185 void SetWindowTitle (const TCHAR *Title); 00186 00188 00192 void SetWindowSize (int width, int height); 00193 00195 void SetWindowPosition (int width, int height); 00196 00198 void SetViewPort (int x, int y, int width, int height); 00199 00200 Point GetMouseScreenCoord(); 00201 Point GetMouseWindowCoord(); 00202 Point GetWindowCoord(); 00203 Rect GetWindowGeometry(); 00204 Rect GetNCWindowGeometry(); 00205 void MakeGLContextCurrent(); 00206 void SwapBuffer (bool glswap = true); 00207 00208 // Event methods 00209 void GetSystemEvent (IEvent *evt); 00210 00211 // Os specific 00212 int GetGlXMajor () const; 00213 int GetGlXMinor () const; 00214 00215 #if defined (NUX_OS_LINUX) 00216 void InjectXEvent (IEvent *evt, XEvent xevent); 00217 #endif 00218 00219 IEvent &GetCurrentEvent(); 00220 00221 // That method is deprecated, it always returns false and still here in 00222 // order to maintain API compatibility. 00223 bool isWindowMinimized() const 00224 { 00225 return false; 00226 } 00227 00228 void ShowWindow(); 00229 void HideWindow(); 00230 bool IsWindowVisible (); 00231 00232 void EnterMaximizeWindow(); 00233 void ExitMaximizeWindow(); 00234 00235 Window GetWindowHandle() const 00236 { 00237 return m_X11Window; 00238 } 00239 bool IsChildWindow() const 00240 { 00241 return m_ParentWindow != 0; 00242 } 00243 00244 // Return true if VSync swap control is available 00245 bool HasVSyncSwapControl() const; 00246 void EnableVSyncSwapControl(); 00247 void DisableVSyncSwapControl(); 00248 00249 // m_FrameRate 00250 float GetFrameTime() const; 00251 void ResetFrameTime(); 00252 00253 GraphicsEngine* GetGraphicsEngine() const; 00254 00255 GpuDevice* GetGpuDevice () const; 00256 00257 // Dialog 00258 /*bool StartOpenFileDialog(FileDialogOption& fdo); 00259 bool StartSaveFileDialog(FileDialogOption& fdo); 00260 bool StartColorDialog(ColorDialogOption& cdo);*/ 00261 00262 00263 void GetDesktopSize (int &w, int &h); 00264 void GetWindowSize (int &w, int &h); 00265 int GetWindowWidth (); 00266 int GetWindowHeight (); 00267 00269 00274 void ResetWindowSize (); 00275 00276 bool HasFrameBufferSupport(); 00277 /*void SetWindowCursor(HCURSOR cursor); 00278 HCURSOR GetWindowCursor() const;*/ 00279 00280 00282 00291 void PauseThreadGraphicsRendering(); 00292 bool IsPauseThreadGraphicsRendering() const; 00293 00294 void ProcessForeignX11Event (XEvent *xevent, IEvent *nux_event); 00295 void ProcessXEvent (XEvent xevent, bool foreign); 00296 void RecalcXYPosition (Window TheMainWindow, XEvent xevent, int &x, int &y); 00297 void RecalcXYPosition (int x_root, int y_root, int &x_recalc, int &y_recalc); 00298 00299 void SendDndStatus (bool accept, DndAction action, Rect region); 00300 void SendDndFinished (bool accepted, DndAction performed_action); 00301 std::list<char *> GetDndMimeTypes (); 00302 char * GetDndData (char *property); 00303 00304 void StartDndDrag (const DndSourceFuncs &funcs, void *user_data); 00305 00306 bool GrabPointer (GrabReleaseCallback callback, void *data, bool replace_existing); 00307 bool UngrabPointer (void *data); 00308 bool PointerIsGrabbed (); 00309 00310 bool GrabKeyboard (GrabReleaseCallback callback, void *data, bool replace_existing); 00311 bool UngrabKeyboard (void *data); 00312 bool KeyboardIsGrabbed (); 00313 00314 void * KeyboardGrabData () { return _global_keyboard_grab_data; } 00315 void * PointerGrabData () { return _global_pointer_grab_data; } 00316 00317 private: 00318 void InitGlobalGrabWindow (); 00319 00320 void HandleXDndPosition (XEvent event, Event* nux_event); 00321 void HandleXDndEnter (XEvent event); 00322 void HandleXDndStatus (XEvent event); 00323 void HandleXDndLeave (XEvent event); 00324 void HandleXDndDrop (XEvent event, Event* nux_event); 00325 void HandleXDndFinished (XEvent event); 00326 00327 void SendXDndStatus (Display *display, Window source, Window target, bool accept, Atom action, Rect box); 00328 bool GetXDndSelectionEvent (Display *display, Window target, Atom property, long time, XEvent *result, int attempts); 00329 void SendXDndFinished (Display *display, Window source, Window target, bool result, Atom action); 00330 char * GetXDndData (Display *display, Window requestor, Atom property, long time); 00331 00332 void EndDndDrag (DndAction action); 00333 bool GrabDndSelection (Display *display, Window window, Time time); 00334 void HandleDndDragSourceEvent (XEvent event); 00335 void HandleDndSelectionRequest (XEvent event); 00336 Window GetDndTargetWindowForPos (int x, int y); 00337 00338 void DrawDndSourceWindow (); 00339 00340 void SendDndSourcePosition (Window target, int x, int y, Time time); 00341 void SendDndSourceEnter (Window target); 00342 void SendDndSourceLeave (Window target); 00343 void SendDndSourceDrop (Window target, Time time); 00344 void SetDndSourceTargetWindow (Window target); 00345 00346 static gboolean OnDragEndTimeout (gpointer data); 00347 00348 Point _last_dnd_position; 00349 00350 bool m_PauseGraphicsRendering; 00351 GLTimer m_Timer; 00352 float m_FrameTime; 00353 GpuDevice *m_DeviceFactory; 00354 GraphicsEngine *m_GraphicsContext; 00355 WindowStyle m_Style; 00356 00357 Atom _xdnd_types[_xdnd_max_type + 1]; 00358 Display *_drag_display; 00359 Window _drag_window; 00360 Window _drag_source; 00361 long _drag_drop_timestamp; 00362 00363 void * _dnd_source_data; 00364 DndSourceFuncs _dnd_source_funcs; 00365 00366 Window _dnd_source_window; 00367 Window _dnd_source_target_window; 00368 00369 Window _global_grab_window; 00370 00371 void *_global_pointer_grab_data; 00372 bool _global_pointer_grab_active; 00373 GrabReleaseCallback _global_pointer_grab_callback; 00374 00375 void *_global_keyboard_grab_data; 00376 bool _global_keyboard_grab_active; 00377 GrabReleaseCallback _global_keyboard_grab_callback; 00378 00379 bool _dnd_is_drag_source; 00380 bool _dnd_source_target_accepts_drop; 00381 bool _dnd_source_grab_active; 00382 bool _dnd_source_drop_sent; 00383 public: 00384 ~GraphicsDisplay(); 00385 GLEWContext *GetGLEWContext() 00386 { 00387 return &m_GLEWContext; 00388 } 00389 GLXEWContext *GetGLXEWContext() 00390 { 00391 return &m_GLXEWContext; 00392 } 00393 00394 NString FindResourceLocation (const TCHAR *ResourceFileName, bool ErrorOnFail = false); 00395 NString FindUITextureLocation (const TCHAR *ResourceFileName, bool ErrorOnFail = false); 00396 NString FindShaderLocation (const TCHAR *ResourceFileName, bool ErrorOnFail = false); 00397 NString FindFontLocation (const TCHAR *ResourceFileName, bool ErrorOnFail = false); 00398 00399 const std::vector<NString>& GetFontSearchPath() const 00400 { 00401 return m_FontSearchPath; 00402 } 00403 const std::vector<NString>& GetShaderSearchPath() const 00404 { 00405 return m_ShaderSearchPath; 00406 } 00407 const std::vector<NString>& GetUITextureSearchPath() const 00408 { 00409 return m_UITextureSearchPath; 00410 } 00411 00412 private: 00413 std::vector<NString> m_FontSearchPath; 00414 std::vector<NString> m_ShaderSearchPath; 00415 std::vector<NString> m_UITextureSearchPath; 00416 FilePath m_ResourcePathLocation; 00417 00418 GraphicsDisplay(); 00419 GraphicsDisplay (const GraphicsDisplay &); 00420 // Does not make sense for a singleton. This is a self assignment. 00421 GraphicsDisplay &operator= (const GraphicsDisplay &); 00422 00423 GLEWContext m_GLEWContext; 00424 GLXEWContext m_GLXEWContext; 00425 friend class DisplayAccessController; 00426 }; 00427 00428 } 00429 00430 #endif //GRAPHICSDISPLAYX11_H