Back to index

nux  3.0.0
GraphicsDisplayX11.h
Go to the documentation of this file.
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 
00039 #include <X11/extensions/xf86vmode.h>
00040 #include <X11/extensions/Xinerama.h>
00041 
00042 namespace nux
00043 {
00044 
00045   class Event;
00046   class MainFBO;
00047   class GpuDevice;
00048   class GraphicsEngine;
00049   class IOpenGLFrameBufferObject;
00050 
00051   enum WindowStyle
00052   {
00053     WINDOWSTYLE_NORMAL,
00054     WINDOWSTYLE_PANEL,
00055     WINDOWSTYLE_DIALOG,
00056     WINDOWSTYLE_TOOL,
00057     WINDOWSTYLE_NOBORDER,
00058   };
00059   
00060   enum DndAction
00061   {
00062     DNDACTION_COPY,
00063     DNDACTION_MOVE,
00064     DNDACTION_LINK,
00065     DNDACTION_ASK,
00066     DNDACTION_PRIVATE,
00067     
00068     DNDACTION_NONE,
00069   };
00070   
00071 #define NUX_THREADMSG                           (WM_APP+0)
00072 #define NUX_THREADMSG_START_RENDERING           (WM_APP+1)  // Connection established // start at WM_APP
00073 #define NUX_THREADMSG_CHILD_WINDOW_TERMINATED   (WM_APP+2)  // General failure - Wait Connection failed
00074 #define NUX_THREADMSG_THREAD_TERMINATED         (WM_APP+3)  // Set wParam = Thread ID, lParam = 0
00075 
00076 #define _xdnd_max_type 100
00077 #define xdnd_version 5
00078 
00079 // This will become GLWindow
00080   class GraphicsDisplay : public GraphicSystem
00081   {
00082   private:
00083     Display     *m_X11Display;
00084     int         m_X11Screen;
00085     Window      m_X11Window;
00086     XVisualInfo *m_X11VisualInfo;
00087 
00088     int         m_ParentWindow;
00089 #ifndef NUX_OPENGLES_20
00090     GLXContext  m_GLCtx;
00091     GLXFBConfig _fb_config;
00092     GLXWindow   glx_window_;
00093 #else
00094     EGLContext  m_GLCtx;
00095     EGLSurface  m_GLSurface;
00096     EGLConfig   _fb_config;
00097 #endif
00098 
00099     XSetWindowAttributes m_X11Attr;
00100 
00101     int m_NumVideoModes;
00102     XF86VidModeModeInfo **m_X11VideoModes;
00103     /* original desktop mode which we save so we can restore it later */
00104     XF86VidModeModeInfo m_X11OriginalVideoMode;
00105 
00106     Atom            m_WMDeleteWindow;
00107     Colormap        m_X11Colormap;
00108     int             m_BorderPixel;
00109 
00110     int _x11_major;
00111     int _x11_minor;
00112     int _glx_major;
00113     int _glx_minor;
00114     bool _has_glx_13;
00115 
00116     XEvent m_X11LastEvent;
00117     Bool m_X11RepeatKey;
00118 
00119     char m_WindowClassName[256];
00120     GLuint      m_PixelFormat;      // Holds The Results After Searching For A Match
00121     NString     m_WindowTitle;
00122 
00123     // size, position
00124     Size m_ViewportSize;
00125     Size m_WindowSize;
00126     Point m_WindowPosition;
00127 
00128     // surface attibute;
00129     bool m_Fullscreen;
00130     unsigned int m_ScreenBitDepth;
00131 
00132     // verifiy that the interface is properly created
00133     bool m_GfxInterfaceCreated;
00134 
00135     // Device information
00136     void GetDisplayInfo();
00137     int m_BestMode;
00138 
00139     bool m_CreatedFromForeignWindow;
00140     Time last_click_time_;
00145     static int double_click_time_delay; 
00146     int double_click_counter_;
00147 
00148   public:
00149     typedef void(*GrabReleaseCallback) (bool replaced, void *user_data);
00150   
00151     typedef struct _DndSourceFuncs
00152     {
00153       nux::NBitmapData *       (*get_drag_image)    (void * data);
00154       std::list<const char *>  (*get_drag_types)    (void * data);
00155       const char *             (*get_data_for_type) (const char * type, int *size, int *format, void * data);
00156       void                     (*drag_finished)     (DndAction result, void * data);
00157     } DndSourceFuncs;
00158     
00159     bool HasXPendingEvent() const;
00160     Display *GetX11Display()
00161     {
00162       return m_X11Display;
00163     }
00164     // Device
00165     int m_num_device_modes;
00166 
00167     // Event object
00168     Event *m_pEvent;
00169 
00170     bool IsGfxInterfaceCreated();
00171 
00173 
00181     bool CreateOpenGLWindow(
00182       const char *WindowTitle,
00183       unsigned int WindowWidth,
00184       unsigned int WindowHeight,
00185       WindowStyle Style,
00186       const GraphicsDisplay *Parent,
00187       bool FullscreenFlag = false,
00188       bool create_rendering_data = true);
00189 
00191 
00195 #ifdef NUX_OPENGLES_20
00196     bool CreateFromOpenGLWindow(Display *X11Display, Window X11Window, EGLContext OpenGLContext);
00197 #else
00198     bool CreateFromOpenGLWindow(Display *X11Display, Window X11Window, GLXContext OpenGLContext);
00199 #endif
00200 
00201     void DestroyOpenGLWindow();
00202 
00203     void SetWindowTitle(const char *Title);
00204 
00206 
00210     void SetWindowSize(int width, int height);
00211 
00213     void SetWindowPosition(int width, int height);
00214     
00216     void SetViewPort(int x, int y, int width, int height);
00217 
00218     Point GetMouseScreenCoord();
00219     Point GetMouseWindowCoord();
00220     Point GetWindowCoord();
00221     Rect GetWindowGeometry();
00222     Rect GetNCWindowGeometry();
00223     void MakeGLContextCurrent();
00224     void SwapBuffer(bool glswap = true);
00225 
00226     // Event methods
00227     void GetSystemEvent(Event *evt);
00228 
00229     // Os specific
00230     int GetGlXMajor() const;
00231     int GetGlXMinor() const;
00232 
00233 #if defined(NUX_OS_LINUX)
00234     void InjectXEvent(Event *evt, XEvent xevent);
00235 
00236     typedef struct _EventFilterArg
00237     {
00238       bool   (*filter)    (XEvent event, void * data);
00239       void * data;
00240     } EventFilterArg;
00241 
00242     void AddEventFilter (EventFilterArg arg);
00243     void RemoveEventFilter (void *owner);
00244 
00245     std::list<EventFilterArg> _event_filters;
00246 #endif
00247     
00248     Event &GetCurrentEvent();
00249 
00250     // That method is deprecated, it always returns false and still here in
00251     // order to maintain API compatibility.
00252     bool isWindowMinimized() const
00253     {
00254       return false;
00255     }
00256 
00257     void ShowWindow();
00258     void HideWindow();
00259     bool IsWindowVisible();
00260 
00261     void EnterMaximizeWindow();
00262     void ExitMaximizeWindow();
00263 
00264     Window GetWindowHandle() const
00265     {
00266       return m_X11Window;
00267     }
00268     bool IsChildWindow() const
00269     {
00270       return m_ParentWindow != 0;
00271     }
00272 
00273     // Return true if VSync swap control is available
00274     bool HasVSyncSwapControl() const;
00275     void EnableVSyncSwapControl();
00276     void DisableVSyncSwapControl();
00277 
00278     // m_FrameRate
00279     float GetFrameTime() const;
00280     void ResetFrameTime();
00281 
00282     GraphicsEngine* GetGraphicsEngine() const;
00283 
00284     GpuDevice* GetGpuDevice() const;
00285 
00286     // Dialog
00287     /*bool StartOpenFileDialog(FileDialogOption& fdo);
00288     bool StartSaveFileDialog(FileDialogOption& fdo);
00289     bool StartColorDialog(ColorDialogOption& cdo);*/
00290 
00291 
00292     void GetDesktopSize(int &w, int &h);
00293     void GetWindowSize(int &w, int &h);
00294     int GetWindowWidth();
00295     int GetWindowHeight();
00296 
00298 
00303     void ResetWindowSize();
00304 
00305     bool HasFrameBufferSupport();
00306     /*void SetWindowCursor(HCURSOR cursor);
00307     HCURSOR GetWindowCursor() const;*/
00308 
00309 
00311 
00320     void PauseThreadGraphicsRendering();
00321     bool IsPauseThreadGraphicsRendering() const;
00322 
00323     void ProcessForeignX11Event(XEvent *xevent, Event *nux_event);
00324     void ProcessXEvent(XEvent xevent, bool foreign);
00325     void RecalcXYPosition(Window TheMainWindow, XEvent xevent, int &x, int &y);
00326     void RecalcXYPosition(int x_root, int y_root, int &x_recalc, int &y_recalc);
00327     
00328     void              SendDndStatus   (bool accept, DndAction action, Rect region);
00329     void              SendDndFinished(bool accepted, DndAction performed_action);
00330     std::list<char *> GetDndMimeTypes();
00331     char *            GetDndData      (char *property);
00332 
00333     void StartDndDrag(const DndSourceFuncs &funcs, void *user_data);
00334     
00335     bool GrabPointer   (GrabReleaseCallback callback, void *data, bool replace_existing);
00336     bool UngrabPointer(void *data);
00337     bool PointerIsGrabbed();
00338     
00339     bool GrabKeyboard   (GrabReleaseCallback callback, void *data, bool replace_existing);
00340     bool UngrabKeyboard(void *data);
00341     bool KeyboardIsGrabbed();
00342     
00343     void * KeyboardGrabData() { return _global_keyboard_grab_data; }
00344     void * PointerGrabData() { return _global_pointer_grab_data; }
00345 
00346   private:
00347     void InitGlobalGrabWindow();
00348   
00349     void HandleXDndPosition(XEvent event, Event* nux_event);
00350     void HandleXDndEnter    (XEvent event);
00351     void HandleXDndStatus   (XEvent event);
00352     void HandleXDndLeave    (XEvent event);
00353     void HandleXDndDrop     (XEvent event, Event* nux_event);
00354     void HandleXDndFinished(XEvent event);
00355     
00356     void SendXDndStatus(Display *display, Window source, Window target, bool accept, Atom action, Rect box);
00357     bool GetXDndSelectionEvent(Display *display, Window target, Atom property, long time, XEvent *result, int attempts);
00358     void SendXDndFinished(Display *display, Window source, Window target, bool result, Atom action);
00359     char * GetXDndData(Display *display, Window requestor, Atom property, long time);
00360 
00361     void EndDndDrag(DndAction action);
00362     bool GrabDndSelection(Display *display, Window window, Time time);
00363     void HandleDndDragSourceEvent(XEvent event);
00364     void HandleDndSelectionRequest(XEvent event);
00365     Window GetDndTargetWindowForPos(int x, int y);
00366     
00367     void DrawDndSourceWindow();
00368     
00369     void SendDndSourcePosition(Window target, int x, int y, Time time);
00370     void SendDndSourceEnter(Window target);
00371     void SendDndSourceLeave(Window target);
00372     void SendDndSourceDrop(Window target, Time time);
00373     void SetDndSourceTargetWindow(Window target);
00374     
00375     static gboolean OnDragEndTimeout(gpointer data);
00376   
00377     Point _last_dnd_position;
00378 
00379     bool m_PauseGraphicsRendering;
00380     GLTimer m_Timer;
00381     float m_FrameTime;
00382     GpuDevice *m_DeviceFactory;
00383     GraphicsEngine *m_GraphicsContext;
00384     WindowStyle m_Style;
00385 
00386     Atom _xdnd_types[_xdnd_max_type + 1];
00387     Display *_drag_display;
00388     Window _drag_window;
00389     Window _drag_source;
00390     long _drag_drop_timestamp;
00391 
00392     void * _dnd_source_data;
00393     DndSourceFuncs _dnd_source_funcs;
00394 
00395     Window _dnd_source_window;
00396     Window _dnd_source_target_window;
00397     
00398     Window              _global_grab_window;
00399     
00400     void               *_global_pointer_grab_data;
00401     bool                _global_pointer_grab_active;
00402     GrabReleaseCallback _global_pointer_grab_callback;
00403 
00404     void               *_global_keyboard_grab_data;
00405     bool                _global_keyboard_grab_active;
00406     GrabReleaseCallback _global_keyboard_grab_callback;
00407 
00408     bool _dnd_is_drag_source;
00409     bool _dnd_source_target_accepts_drop;
00410     bool _dnd_source_grab_active;
00411     bool _dnd_source_drop_sent;
00412   public:
00413     ~GraphicsDisplay();
00414 #ifndef NUX_OPENGLES_20
00415     GLEWContext *GetGLEWContext()
00416     {
00417       return &m_GLEWContext;
00418     }
00419     GLXEWContext *GetGLXEWContext()
00420     {
00421       return &m_GLXEWContext;
00422     }
00423 #endif
00424 
00425     NString FindResourceLocation(const char *ResourceFileName, bool ErrorOnFail = false);
00426     NString FindUITextureLocation(const char *ResourceFileName, bool ErrorOnFail = false);
00427     NString FindShaderLocation(const char *ResourceFileName, bool ErrorOnFail = false);
00428     NString FindFontLocation(const char *ResourceFileName, bool ErrorOnFail = false);
00429 
00430     const std::vector<NString>& GetFontSearchPath() const
00431     {
00432       return m_FontSearchPath;
00433     }
00434     const std::vector<NString>& GetShaderSearchPath() const
00435     {
00436       return m_ShaderSearchPath;
00437     }
00438     const std::vector<NString>& GetUITextureSearchPath() const
00439     {
00440       return m_UITextureSearchPath;
00441     }
00442 
00443   private:
00444     std::vector<NString> m_FontSearchPath;
00445     std::vector<NString> m_ShaderSearchPath;
00446     std::vector<NString> m_UITextureSearchPath;
00447     FilePath m_ResourcePathLocation;
00448 
00449     GraphicsDisplay();
00450     GraphicsDisplay(const GraphicsDisplay &);
00451     // Does not make sense for a singleton. This is a self assignment.
00452     GraphicsDisplay &operator= (const GraphicsDisplay &);
00453 
00454 #ifndef NUX_OPENGLES_20
00455     GLEWContext m_GLEWContext;
00456     GLXEWContext m_GLXEWContext;
00457 #endif
00458 
00459     int MouseMove(XEvent xevent, Event *event);
00460     int MousePress(XEvent xevent, Event *event);
00461     int MouseRelease(XEvent xevent, Event *event);
00462 
00463     friend class DisplayAccessController;
00464     friend class GraphicsEngine;
00465   };
00466 
00467 }
00468 
00469 #endif //GRAPHICSDISPLAYX11_H