LibreOffice Module vcl (master)  1
saldisp.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_VCL_INC_UNX_SALDISP_HXX
21 #define INCLUDED_VCL_INC_UNX_SALDISP_HXX
22 
23 class SalDisplay;
24 class SalColormap;
25 class SalVisual;
26 class SalXLib;
27 
28 #include <X11/Xlib.h>
29 #include <X11/Xutil.h>
30 #include <X11/extensions/render.h>
31 #include <epoxy/glx.h>
32 
33 #include <rtl/string.hxx>
34 #include <unx/saltype.h>
36 #include <vcl/ptrstyle.hxx>
37 #include <sal/types.h>
38 #include <cassert>
39 #include <list>
40 #include <unordered_map>
41 #include <vector>
42 #include <tools/gen.hxx>
43 #include <salwtype.hxx>
44 #include <unx/gendata.hxx>
45 #include <unx/gendisp.hxx>
46 #include <o3tl/enumarray.hxx>
47 
48 #include <vclpluginapi.h>
49 
50 /* From <X11/Intrinsic.h> */
51 typedef unsigned long Pixel;
52 
53 class BitmapPalette;
54 class SalFrame;
55 class ColorMask;
56 
57 namespace vcl_sal { class WMAdaptor; }
58 
59 // server vendor
60 
61 typedef enum {
65 } srv_vendor_t;
66 
67 extern "C" srv_vendor_t sal_GetServerVendor( Display *p_display );
68 
69 // MSB/Bigendian view (Color == RGB, r=0xFF0000, g=0xFF00, b=0xFF)
70 
71 enum class SalRGB { RGB, RBG,
72  GBR, GRB,
73  BGR, BRG,
74  otherSalRGB };
75 
76 class SalVisual : public XVisualInfo
77 {
82  int nRedBits_;
85 public:
86  SalVisual();
87  SalVisual( const XVisualInfo* pXVI );
88 
89  VisualID GetVisualId() const { return visualid; }
90  Visual *GetVisual() const { return visual; }
91  int GetClass() const { return c_class; }
92  int GetDepth() const { return depth; }
93 
94  Pixel GetTCPixel( Color nColor ) const;
95  Color GetTCColor( Pixel nPixel ) const;
96 };
97 
98 // A move-only flag, used by SalColormap to track ownership of its m_aVisual.visual:
99 struct OwnershipFlag {
100  bool owner = false;
101 
102  OwnershipFlag() = default;
103 
104  OwnershipFlag(OwnershipFlag && other) noexcept: owner(other.owner) { other.owner = false; }
105 
106  OwnershipFlag & operator =(OwnershipFlag && other) noexcept {
107  assert(&other != this);
108  owner = other.owner;
109  other.owner = false;
110  return *this;
111  }
112 };
113 
115 {
117  Colormap m_hColormap;
118  std::vector<Color> m_aPalette; // Pseudocolor
121  std::vector<sal_uInt16> m_aLookupTable; // Pseudocolor: 12bit reduction
124  Pixel m_nUsed; // Pseudocolor
126 
127  void GetPalette();
128  void GetLookupTable();
129 public:
130  SalColormap( const SalDisplay* pSalDisplay,
131  Colormap hColormap,
132  SalX11Screen nXScreen );
133  SalColormap( sal_uInt16 nDepth );
134  SalColormap();
135 
136  ~SalColormap();
137 
138  SalColormap(SalColormap &&) = default;
139  SalColormap & operator =(SalColormap &&) = default;
140 
141  Colormap GetXColormap() const { return m_hColormap; }
142  const SalDisplay* GetDisplay() const { return m_pDisplay; }
143  inline Display* GetXDisplay() const;
144  const SalVisual& GetVisual() const { return m_aVisual; }
145  Visual* GetXVisual() const { return m_aVisual.GetVisual(); }
146  Pixel GetWhitePixel() const { return m_nWhitePixel; }
147  Pixel GetBlackPixel() const { return m_nBlackPixel; }
148  Pixel GetUsed() const { return m_nUsed; }
149 
150  bool GetXPixels( XColor &rColor,
151  int r,
152  int g,
153  int b ) const;
154  inline bool GetXPixel( XColor &rColor,
155  int r,
156  int g,
157  int b ) const;
158  Pixel GetPixel( Color nColor ) const;
159  Color GetColor( Pixel nPixel ) const;
160 };
161 
162 class SalI18N_InputMethod;
163 
164 typedef int(*YieldFunc)(int fd, void* data);
165 
166 class SalXLib
167 {
168 protected:
169  timeval m_aTimeout;
172 
173  int nFDs_;
174  fd_set aReadFDS_;
176 
178  std::unique_ptr<SalI18N_InputMethod> m_pInputMethod;
179 
180 public:
181  SalXLib();
182  virtual ~SalXLib();
183  virtual void Init();
184 
185  virtual bool Yield( bool bWait, bool bHandleAllCurrentEvents );
186  virtual void Wakeup();
188 
189  virtual void Insert( int fd, void* data,
190  YieldFunc pending,
191  YieldFunc queued,
192  YieldFunc handle );
193  virtual void Remove( int fd );
194 
195  virtual void StartTimer( sal_uInt64 nMS );
196  virtual void StopTimer();
197 
198  virtual bool CheckTimeout( bool bExecuteTimers = true );
199 
200  SalI18N_InputMethod* GetInputMethod() const { return m_pInputMethod.get(); }
201  Display* GetDisplay() const { return m_pDisplay; }
202 };
203 
205 class AttributeProvider;
206 
207 extern "C" {
208  typedef Bool(*X_if_predicate)(Display*,XEvent*,XPointer);
209 }
210 
211 class GLX11Window : public GLWindow
212 {
213 public:
215  int screen;
217  XVisualInfo* vi;
218  GLXContext ctx;
219  OString GLXExtensions;
220 
221  bool HasGLXExtension(const char* name) const;
222 
223  GLX11Window();
224  virtual bool Synchronize(bool bOnoff) const override;
225  virtual ~GLX11Window() override;
226 };
227 
229 {
230 public:
231  struct RenderEntry
232  {
233  Pixmap m_aPixmap;
235 
236  RenderEntry() : m_aPixmap( 0 ), m_aPicture( 0 ) {}
237  };
238 
239  typedef std::unordered_map<int,RenderEntry> RenderEntryMap;
240 
241  struct ScreenData
242  {
243  bool m_bInit;
244 
245  ::Window m_aRoot;
246  ::Window m_aRefWindow;
256  Pixmap m_hInvert50;
257  mutable RenderEntryMap m_aRenderData;
258 
260  m_bInit( false ),
261  m_aRoot( None ),
262  m_aRefWindow( None ),
263  m_aMonoGC( None ),
264  m_aCopyGC( None ),
265  m_aAndInvertedGC( None ),
266  m_aAndGC( None ),
267  m_aOrGC( None ),
268  m_aStippleGC( None ),
269  m_hInvert50( None ),
270  m_aRenderData( 1 )
271  {}
272  };
273 
274 protected:
277 
278  Display *pDisp_; // X Display
279 
281  std::vector< ScreenData > m_aScreens;
283  Pair aResolution_; // [dpi]
285 
287 
288  // until x bytes
289 
291 
292  // Keyboard
293  bool bNumLockFromXS_; // Num Lock handled by X Server
294  int nNumLockIndex_; // modifier index in modmap
295  KeySym nShiftKeySym_; // first shift modifier
296  KeySym nCtrlKeySym_; // first control modifier
297  KeySym nMod1KeySym_; // first mod1 modifier
298 
299  std::unique_ptr<vcl_sal::WMAdaptor> m_pWMAdaptor;
300 
302  std::vector< tools::Rectangle > m_aXineramaScreens;
303  std::vector< int > m_aXineramaScreenIndexMap;
304  std::list<SalObject*> m_aSalObjects;
305 
306  mutable Time m_nLastUserEventTime; // mutable because changed on first access
307 
308  virtual bool Dispatch( XEvent *pEvent ) = 0;
309  void InitXinerama();
310  void InitRandR( ::Window aRoot ) const;
311  static void DeInitRandR();
312  void processRandREvent( XEvent* );
313 
314  void doDestruct();
315  void addXineramaScreenUnique( int i, long i_nX, long i_nY, long i_nWidth, long i_nHeight );
316  Time GetEventTimeImpl( bool bAlwaysReget = false ) const;
317 public:
318  static bool BestOpenGLVisual(Display* pDisplay, int nScreen, XVisualInfo& rVI);
319  static bool BestVisual(Display *pDisp, int nScreen, XVisualInfo &rVI);
320 
321  SalDisplay( Display* pDisp );
322 
323  virtual ~SalDisplay() override;
324 
325  void Init();
326 
327 #ifdef DBG_UTIL
328  void PrintInfo() const;
329  void DbgPrintDisplayEvent(const char *pComment, XEvent *pEvent) const;
330 #endif
331 
332  void Beep() const;
333 
334  void ModifierMapping();
335  void SimulateKeyPress( sal_uInt16 nKeyCode );
336  KeyIndicatorState GetIndicatorState() const;
337  OUString GetKeyNameFromKeySym( KeySym keysym ) const;
338  OUString GetKeyName( sal_uInt16 nKeyCode ) const;
339  sal_uInt16 GetKeyCode( KeySym keysym, char*pcPrintable ) const;
340  KeySym GetKeySym( XKeyEvent *pEvent,
341  char *pPrintable,
342  int *pLen,
343  KeySym *pUnmodifiedKeySym,
344  Status *pStatus,
345  XIC = nullptr ) const;
346 
347  Cursor GetPointer( PointerStyle ePointerStyle );
348  int CaptureMouse( SalFrame *pCapture );
349 
350  ScreenData* initScreen( SalX11Screen nXScreen ) const;
351  const ScreenData& getDataForScreen( SalX11Screen nXScreen ) const
352  {
353  if( nXScreen.getXScreen() >= m_aScreens.size() )
354  return m_aInvalidScreenData;
355  if( ! m_aScreens[nXScreen.getXScreen()].m_bInit )
356  initScreen( nXScreen );
357  return m_aScreens[nXScreen.getXScreen()];
358  }
359 
360  ::Window GetDrawable( SalX11Screen nXScreen ) const { return getDataForScreen( nXScreen ).m_aRefWindow; }
361  Display *GetDisplay() const { return pDisp_; }
362  const SalX11Screen& GetDefaultXScreen() const { return m_nXDefaultScreen; }
363  const Size& GetScreenSize( SalX11Screen nXScreen ) const { return getDataForScreen( nXScreen ).m_aSize; }
364  srv_vendor_t GetServerVendor() const { return meServerVendor; }
365  bool IsDisplay() const { return !!pXLib_; }
366  GC GetCopyGC( SalX11Screen nXScreen ) const { return getDataForScreen(nXScreen).m_aCopyGC; }
367  Pixmap GetInvert50( SalX11Screen nXScreen ) const { return getDataForScreen(nXScreen).m_hInvert50; }
368  const SalColormap& GetColormap( SalX11Screen nXScreen ) const { return getDataForScreen(nXScreen).m_aColormap; }
369  const SalVisual& GetVisual( SalX11Screen nXScreen ) const { return getDataForScreen(nXScreen).m_aVisual; }
370  RenderEntryMap& GetRenderEntries( SalX11Screen nXScreen ) const { return getDataForScreen(nXScreen).m_aRenderData; }
371  const Pair &GetResolution() const { return aResolution_; }
372  sal_uLong GetMaxRequestSize() const { return nMaxRequestSize_; }
373  Time GetLastUserEventTime() const { return GetEventTimeImpl(); }
374  // this is an equivalent of gdk_x11_get_server_time()
375  Time GetX11ServerTime() const { return GetEventTimeImpl( true ); }
376 
377  bool XIfEventWithTimeout( XEvent*, XPointer, X_if_predicate ) const;
378  SalXLib* GetXLib() const { return pXLib_; }
379 
380  SalI18N_InputMethod* GetInputMethod() const { return pXLib_->GetInputMethod(); }
381  SalI18N_KeyboardExtension* GetKbdExtension() const { return mpKbdExtension; }
383  { mpKbdExtension = pKbdExtension; }
384  ::vcl_sal::WMAdaptor* getWMAdaptor() const { return m_pWMAdaptor.get(); }
385  bool IsXinerama() const { return m_bXinerama; }
386  const std::vector< tools::Rectangle >& GetXineramaScreens() const { return m_aXineramaScreens; }
387  ::Window GetRootWindow( SalX11Screen nXScreen ) const
388  { return getDataForScreen( nXScreen ).m_aRoot; }
389  unsigned int GetXScreenCount() const { return m_aScreens.size(); }
390 
391  const SalFrameSet& getFrames() const { return m_aFrames; }
392 
393  std::list< SalObject* >& getSalObjects() { return m_aSalObjects; }
394 };
395 
397 { return m_pDisplay->GetDisplay(); }
398 
399 class SalX11Display : public SalDisplay
400 {
401 public:
402  SalX11Display( Display* pDisp );
403  virtual ~SalX11Display() override;
404 
405  virtual bool Dispatch( XEvent *pEvent ) override;
406  virtual void Yield();
407  virtual void TriggerUserEventProcessing() override;
408 
409  bool IsEvent();
410  void SetupInput();
411 };
412 
413 namespace vcl_sal {
414  // get foreign key names
415  OUString getKeysymReplacementName(
416  const OUString& pLang,
417  KeySym nSymbol );
418 
420  {
421  assert(data != nullptr);
422  assert(data->GetType() != SAL_DATA_GTK3);
423  return static_cast<SalDisplay *>(data->GetDisplay());
424  }
425 }
426 
427 #endif // INCLUDED_VCL_INC_UNX_SALDISP_HXX
428 
429 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Bool(* X_if_predicate)(Display *, XEvent *, XPointer)
Definition: saldisp.hxx:208
bool IsXinerama() const
Definition: saldisp.hxx:385
GenericUnixSalDataType GetType() const
Definition: gendata.hxx:72
std::list< SalObject * > m_aSalObjects
Definition: saldisp.hxx:304
virtual bool Dispatch(XEvent *pEvent) override
SalX11Screen m_nXDefaultScreen
Definition: saldisp.hxx:280
std::unique_ptr< SalI18N_InputMethod > m_pInputMethod
Definition: saldisp.hxx:178
virtual void Init()
OString GLXExtensions
Definition: saldisp.hxx:219
bool IsDisplay() const
Definition: saldisp.hxx:365
virtual ~SalXLib()
Time GetLastUserEventTime() const
Definition: saldisp.hxx:373
unsigned int GetXScreenCount() const
Definition: saldisp.hxx:389
::vcl_sal::WMAdaptor * getWMAdaptor() const
Definition: saldisp.hxx:384
void Init()
virtual void Wakeup()
int m_pTimeoutFDS[2]
Definition: saldisp.hxx:171
o3tl::enumarray< PointerStyle, Cursor > aPointerCache_
Definition: saldisp.hxx:290
SalXLib * GetXLib() const
Definition: saldisp.hxx:378
Time m_nLastUserEventTime
Definition: saldisp.hxx:306
virtual void Remove(int fd)
sal_uIntPtr sal_uLong
void GetPalette()
const wchar_t *typedef int(__stdcall *DllNativeUnregProc)(int
KeySym nCtrlKeySym_
Definition: saldisp.hxx:296
std::vector< int > m_aXineramaScreenIndexMap
Definition: saldisp.hxx:303
const ScreenData & getDataForScreen(SalX11Screen nXScreen) const
Definition: saldisp.hxx:351
Pair aResolution_
Definition: saldisp.hxx:283
int GetDepth() const
Definition: saldisp.hxx:92
const SalDisplay * m_pDisplay
Definition: saldisp.hxx:116
void SetupInput()
Pixel m_nBlackPixel
Definition: saldisp.hxx:123
bool bNumLockFromXS_
Definition: saldisp.hxx:293
None
SalGenericDisplay * GetDisplay() const
Definition: gendata.hxx:60
SalColormap m_aColormap
Definition: saldisp.hxx:249
SalVisual m_aVisual
Definition: saldisp.hxx:119
Color GetTCColor(Pixel nPixel) const
GC GetCopyGC(SalX11Screen nXScreen) const
Definition: saldisp.hxx:366
RenderEntryMap m_aRenderData
Definition: saldisp.hxx:257
bool HasGLXExtension(const char *name) const
Status
SalColormap & operator=(SalColormap &&)=default
virtual void TriggerUserEventProcessing() override
Display * GetDisplay() const
Definition: saldisp.hxx:201
virtual bool Synchronize(bool bOnoff) const override
SalRGB eRGBMode_
Definition: saldisp.hxx:78
Display * m_pDisplay
Definition: saldisp.hxx:177
Pixel GetUsed() const
Definition: saldisp.hxx:148
srv_vendor_t meServerVendor
Definition: saldisp.hxx:286
fd_set aReadFDS_
Definition: saldisp.hxx:174
Pixel m_nUsed
Definition: saldisp.hxx:124
SalI18N_InputMethod * GetInputMethod() const
Definition: saldisp.hxx:200
virtual bool CheckTimeout(bool bExecuteTimers=true)
const Size & GetScreenSize(SalX11Screen nXScreen) const
Definition: saldisp.hxx:363
VisualID GetVisualId() const
Definition: saldisp.hxx:89
Pixel GetWhitePixel() const
Definition: saldisp.hxx:146
void TriggerUserEventProcessing()
::Window GetDrawable(SalX11Screen nXScreen) const
Definition: saldisp.hxx:360
::Window GetRootWindow(SalX11Screen nXScreen) const
Definition: saldisp.hxx:387
int nGreenShift_
Definition: saldisp.hxx:80
int nBlueBits_
Definition: saldisp.hxx:84
SalI18N_KeyboardExtension * GetKbdExtension() const
Definition: saldisp.hxx:381
#define VCLPLUG_GEN_PUBLIC
Definition: vclpluginapi.h:29
Display * GetDisplay() const
Definition: saldisp.hxx:361
std::list< SalObject * > & getSalObjects()
Definition: saldisp.hxx:393
ScreenData const m_aInvalidScreenData
Definition: saldisp.hxx:282
Holds the information of our new child window.
unsigned int getXScreen() const
Definition: saltype.h:20
sal_uLong m_nTimeoutMS
Definition: saldisp.hxx:170
const SalVisual & GetVisual(SalX11Screen nXScreen) const
Definition: saldisp.hxx:369
Display * GetXDisplay() const
Definition: saldisp.hxx:396
RenderEntryMap & GetRenderEntries(SalX11Screen nXScreen) const
Definition: saldisp.hxx:370
std::vector< sal_uInt16 > m_aLookupTable
Definition: saldisp.hxx:121
KeyIndicatorState
Definition: keycodes.hxx:180
Time GetX11ServerTime() const
Definition: saldisp.hxx:375
bool GetXPixels(XColor &rColor, int r, int g, int b) const
int GetClass() const
Definition: saldisp.hxx:91
SalXLib * pXLib_
Definition: saldisp.hxx:275
Display
SalI18N_InputMethod * GetInputMethod() const
Definition: saldisp.hxx:380
srv_vendor_t
Definition: saldisp.hxx:61
SalRGB
Definition: saldisp.hxx:71
virtual void StopTimer()
SalI18N_KeyboardExtension * mpKbdExtension
Definition: saldisp.hxx:276
Pixmap GetInvert50(SalX11Screen nXScreen) const
Definition: saldisp.hxx:367
virtual bool Yield(bool bWait, bool bHandleAllCurrentEvents)
std::unordered_map< int, RenderEntry > RenderEntryMap
Definition: saldisp.hxx:239
sal_uLong GetMaxRequestSize() const
Definition: saldisp.hxx:372
OwnershipFlag(OwnershipFlag &&other) noexcept
Definition: saldisp.hxx:104
int(* YieldFunc)(int fd, void *data)
Definition: saldisp.hxx:164
int nNumLockIndex_
Definition: saldisp.hxx:294
std::vector< tools::Rectangle > m_aXineramaScreens
Definition: saldisp.hxx:302
GLXContext ctx
Definition: saldisp.hxx:218
Colormap m_hColormap
Definition: saldisp.hxx:117
A SalFrame is a system window (e.g. an X11 window).
Definition: salframe.hxx:112
int nFDs_
Definition: saldisp.hxx:173
virtual void Yield()
SalX11Display(Display *pDisp)
fd_set aExceptionFDS_
Definition: saldisp.hxx:175
unsigned long Pixel
Definition: saldisp.hxx:26
const SalDisplay * GetDisplay() const
Definition: saldisp.hxx:142
int nRedBits_
Definition: saldisp.hxx:82
Colormap GetXColormap() const
Definition: saldisp.hxx:141
SalX11Screen m_nXScreen
Definition: saldisp.hxx:125
const std::vector< tools::Rectangle > & GetXineramaScreens() const
Definition: saldisp.hxx:386
OwnershipFlag()=default
std::vector< ScreenData > m_aScreens
Definition: saldisp.hxx:281
void GetLookupTable()
int nBlueShift_
Definition: saldisp.hxx:81
OwnershipFlag m_aVisualOwnership
Definition: saldisp.hxx:120
srv_vendor_t sal_GetServerVendor(Display *p_display)
const SalColormap & GetColormap(SalX11Screen nXScreen) const
Definition: saldisp.hxx:368
virtual ~GLX11Window() override
OwnershipFlag & operator=(OwnershipFlag &&other) noexcept
Definition: saldisp.hxx:106
Pixel GetBlackPixel() const
Definition: saldisp.hxx:147
KeySym nShiftKeySym_
Definition: saldisp.hxx:295
PointerStyle
Definition: ptrstyle.hxx:25
Visual * GetVisual() const
Definition: saldisp.hxx:90
Pixel GetPixel(Color nColor) const
std::vector< Color > m_aPalette
Definition: saldisp.hxx:118
timeval m_aTimeout
Definition: saldisp.hxx:169
virtual void Insert(int fd, void *data, YieldFunc pending, YieldFunc queued, YieldFunc handle)
Visual * GetXVisual() const
Definition: saldisp.hxx:145
Pixel m_nWhitePixel
Definition: saldisp.hxx:122
SalDisplay * getSalDisplay(GenericUnixSalData const *data)
Definition: saldisp.hxx:419
std::unique_ptr< vcl_sal::WMAdaptor > m_pWMAdaptor
Definition: saldisp.hxx:299
Color GetColor(Pixel nPixel) const
srv_vendor_t GetServerVendor() const
Definition: saldisp.hxx:364
Pixel GetTCPixel(Color nColor) const
bool m_bXinerama
Definition: saldisp.hxx:301
bool GetXPixel(XColor &rColor, int r, int g, int b) const
const SalVisual & GetVisual() const
Definition: saldisp.hxx:144
sal_uLong nMaxRequestSize_
Definition: saldisp.hxx:284
const SalFrameSet & getFrames() const
Definition: saldisp.hxx:391
void SetKbdExtension(SalI18N_KeyboardExtension *pKbdExtension)
Definition: saldisp.hxx:382
const Pair & GetResolution() const
Definition: saldisp.hxx:371
std::unordered_set< SalFrame *, SalFrameHash > SalFrameSet
virtual void StartTimer(sal_uInt64 nMS)
Display * dpy
Definition: saldisp.hxx:214
int nRedShift_
Definition: saldisp.hxx:79
OUString getKeysymReplacementName(const OUString &pLang, KeySym nSymbol)
virtual ~SalX11Display() override
const SalX11Screen & GetDefaultXScreen() const
Definition: saldisp.hxx:362
XVisualInfo * vi
Definition: saldisp.hxx:217
KeySym nMod1KeySym_
Definition: saldisp.hxx:297
int nGreenBits_
Definition: saldisp.hxx:83
Window win
Definition: saldisp.hxx:216
Display * pDisp_
Definition: saldisp.hxx:278