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 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25 #include <X11/extensions/render.h>
26 #include <epoxy/glx.h>
27 
28 #include <rtl/string.hxx>
29 #include <unx/saltype.h>
31 #include <vcl/ptrstyle.hxx>
32 #include <sal/types.h>
33 #include <cassert>
34 #include <list>
35 #include <unordered_map>
36 #include <vector>
37 #include <tools/gen.hxx>
38 #include <salwtype.hxx>
39 #include <unx/gendata.hxx>
40 #include <unx/gendisp.hxx>
41 #include <o3tl/enumarray.hxx>
42 
43 #include <vclpluginapi.h>
44 
45 class SalDisplay;
46 class SalColormap;
47 class SalVisual;
48 class SalXLib;
49 
50 
51 /* From <X11/Intrinsic.h> */
52 typedef unsigned long Pixel;
53 
54 class BitmapPalette;
55 class SalFrame;
56 class ColorMask;
57 
58 namespace vcl_sal { class WMAdaptor; }
59 
60 // server vendor
61 
62 typedef enum {
66 } srv_vendor_t;
67 
68 extern "C" srv_vendor_t sal_GetServerVendor( Display *p_display );
69 
70 // MSB/Bigendian view (Color == RGB, r=0xFF0000, g=0xFF00, b=0xFF)
71 
72 enum class SalRGB { RGB, RBG,
73  GBR, GRB,
74  BGR, BRG,
75  otherSalRGB };
76 
77 class SalVisual : public XVisualInfo
78 {
83  int nRedBits_;
86 public:
87  SalVisual();
88  SalVisual( const XVisualInfo* pXVI );
89 
90  VisualID GetVisualId() const { return visualid; }
91  Visual *GetVisual() const { return visual; }
92  int GetClass() const { return c_class; }
93  int GetDepth() const { return depth; }
94 
95  Pixel GetTCPixel( Color nColor ) const;
96  Color GetTCColor( Pixel nPixel ) const;
97 };
98 
99 // A move-only flag, used by SalColormap to track ownership of its m_aVisual.visual:
101  bool owner = false;
102 
103  OwnershipFlag() = default;
104 
105  OwnershipFlag(OwnershipFlag && other) noexcept: owner(other.owner) { other.owner = false; }
106 
107  OwnershipFlag & operator =(OwnershipFlag && other) noexcept {
108  assert(&other != this);
109  owner = other.owner;
110  other.owner = false;
111  return *this;
112  }
113 };
114 
116 {
118  Colormap m_hColormap;
119  std::vector<Color> m_aPalette; // Pseudocolor
122  std::vector<sal_uInt16> m_aLookupTable; // Pseudocolor: 12bit reduction
125  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  ~SalXLib();
183  void Init();
184 
185  bool Yield( bool bWait, bool bHandleAllCurrentEvents );
186  void Wakeup();
188 
189  void Insert( int fd, void* data,
190  YieldFunc pending,
191  YieldFunc queued,
192  YieldFunc handle );
193  void Remove( int fd );
194 
195  void StartTimer( sal_uInt64 nMS );
196  void StopTimer();
197 
198  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 final : 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 void 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, tools::Long i_nX, tools::Long i_nY, tools::Long i_nWidth, tools::Long i_nHeight );
316  Time GetEventTimeImpl( bool bAlwaysReget = false ) const;
317 public:
318  static bool BestVisual(Display *pDisp, int nScreen, XVisualInfo &rVI);
319 
320  SalDisplay( Display* pDisp );
321 
322  virtual ~SalDisplay() override;
323 
324  void Init();
325 
326 #ifdef DBG_UTIL
327  void PrintInfo() const;
328  void DbgPrintDisplayEvent(const char *pComment, const XEvent *pEvent) const;
329 #endif
330 
331  void Beep() const;
332 
333  void ModifierMapping();
334  void SimulateKeyPress( sal_uInt16 nKeyCode );
335  KeyIndicatorState GetIndicatorState() const;
336  OUString GetKeyNameFromKeySym( KeySym keysym ) const;
337  OUString GetKeyName( sal_uInt16 nKeyCode ) const;
338  sal_uInt16 GetKeyCode( KeySym keysym, char*pcPrintable ) const;
339  KeySym GetKeySym( XKeyEvent *pEvent,
340  char *pPrintable,
341  int *pLen,
342  KeySym *pUnmodifiedKeySym,
343  Status *pStatus,
344  XIC = nullptr ) const;
345 
346  Cursor GetPointer( PointerStyle ePointerStyle );
347  int CaptureMouse( SalFrame *pCapture );
348 
349  ScreenData* initScreen( SalX11Screen nXScreen ) const;
350  const ScreenData& getDataForScreen( SalX11Screen nXScreen ) const
351  {
352  if( nXScreen.getXScreen() >= m_aScreens.size() )
353  return m_aInvalidScreenData;
354  if( ! m_aScreens[nXScreen.getXScreen()].m_bInit )
355  initScreen( nXScreen );
356  return m_aScreens[nXScreen.getXScreen()];
357  }
358 
359  ::Window GetDrawable( SalX11Screen nXScreen ) const { return getDataForScreen( nXScreen ).m_aRefWindow; }
360  Display *GetDisplay() const { return pDisp_; }
361  const SalX11Screen& GetDefaultXScreen() const { return m_nXDefaultScreen; }
362  const Size& GetScreenSize( SalX11Screen nXScreen ) const { return getDataForScreen( nXScreen ).m_aSize; }
363  srv_vendor_t GetServerVendor() const { return meServerVendor; }
364  bool IsDisplay() const { return !!pXLib_; }
365  GC GetCopyGC( SalX11Screen nXScreen ) const { return getDataForScreen(nXScreen).m_aCopyGC; }
366  Pixmap GetInvert50( SalX11Screen nXScreen ) const { return getDataForScreen(nXScreen).m_hInvert50; }
367  const SalColormap& GetColormap( SalX11Screen nXScreen ) const { return getDataForScreen(nXScreen).m_aColormap; }
368  const SalVisual& GetVisual( SalX11Screen nXScreen ) const { return getDataForScreen(nXScreen).m_aVisual; }
369  RenderEntryMap& GetRenderEntries( SalX11Screen nXScreen ) const { return getDataForScreen(nXScreen).m_aRenderData; }
370  const Pair &GetResolution() const { return aResolution_; }
371  sal_uLong GetMaxRequestSize() const { return nMaxRequestSize_; }
372  Time GetLastUserEventTime() const { return GetEventTimeImpl(); }
373  // this is an equivalent of gdk_x11_get_server_time()
374  Time GetX11ServerTime() const { return GetEventTimeImpl( true ); }
375 
376  bool XIfEventWithTimeout( XEvent*, XPointer, X_if_predicate ) const;
377  SalXLib* GetXLib() const { return pXLib_; }
378 
379  SalI18N_InputMethod* GetInputMethod() const { return pXLib_->GetInputMethod(); }
380  SalI18N_KeyboardExtension* GetKbdExtension() const { return mpKbdExtension; }
382  { mpKbdExtension = pKbdExtension; }
383  ::vcl_sal::WMAdaptor* getWMAdaptor() const { return m_pWMAdaptor.get(); }
384  bool IsXinerama() const { return m_bXinerama; }
385  const std::vector< tools::Rectangle >& GetXineramaScreens() const { return m_aXineramaScreens; }
386  ::Window GetRootWindow( SalX11Screen nXScreen ) const
387  { return getDataForScreen( nXScreen ).m_aRoot; }
388  unsigned int GetXScreenCount() const { return m_aScreens.size(); }
389 
390  const SalFrameSet& getFrames() const { return m_aFrames; }
391 
392  std::list< SalObject* >& getSalObjects() { return m_aSalObjects; }
393 };
394 
396 { return m_pDisplay->GetDisplay(); }
397 
398 class SalX11Display final : public SalDisplay
399 {
400 public:
401  SalX11Display( Display* pDisp );
402  virtual ~SalX11Display() override;
403 
404  virtual void Dispatch( XEvent *pEvent ) override;
405  virtual void Yield();
406  virtual void TriggerUserEventProcessing() override;
407 
408  bool IsEvent();
409  void SetupInput();
410 };
411 
412 namespace vcl_sal {
413  // get foreign key names
414  OUString getKeysymReplacementName(
415  const OUString& pLang,
416  KeySym nSymbol );
417 
419  {
420  assert(data != nullptr);
421  return static_cast<SalDisplay *>(data->GetDisplay());
422  }
423 }
424 
425 #endif // INCLUDED_VCL_INC_UNX_SALDISP_HXX
426 
427 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Bool(* X_if_predicate)(Display *, XEvent *, XPointer)
Definition: saldisp.hxx:208
bool IsXinerama() const
Definition: saldisp.hxx:384
std::list< SalObject * > m_aSalObjects
Definition: saldisp.hxx:304
SalX11Screen m_nXDefaultScreen
Definition: saldisp.hxx:280
std::unique_ptr< SalI18N_InputMethod > m_pInputMethod
Definition: saldisp.hxx:178
OString GLXExtensions
Definition: saldisp.hxx:219
bool IsDisplay() const
Definition: saldisp.hxx:364
Time GetLastUserEventTime() const
Definition: saldisp.hxx:372
unsigned int GetXScreenCount() const
Definition: saldisp.hxx:388
::vcl_sal::WMAdaptor * getWMAdaptor() const
Definition: saldisp.hxx:383
void Init()
virtual ~GLX11Window() override
int m_pTimeoutFDS[2]
Definition: saldisp.hxx:171
o3tl::enumarray< PointerStyle, Cursor > aPointerCache_
Definition: saldisp.hxx:290
SalXLib * GetXLib() const
Definition: saldisp.hxx:377
Time m_nLastUserEventTime
Definition: saldisp.hxx:306
sal_uIntPtr sal_uLong
long Long
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:350
Pair aResolution_
Definition: saldisp.hxx:283
int GetDepth() const
Definition: saldisp.hxx:93
const SalDisplay * m_pDisplay
Definition: saldisp.hxx:117
void SetupInput()
Pixel m_nBlackPixel
Definition: saldisp.hxx:124
bool bNumLockFromXS_
Definition: saldisp.hxx:293
None
SalGenericDisplay * GetDisplay() const
Definition: gendata.hxx:66
SalColormap m_aColormap
Definition: saldisp.hxx:249
SalVisual m_aVisual
Definition: saldisp.hxx:120
Color GetTCColor(Pixel nPixel) const
GC GetCopyGC(SalX11Screen nXScreen) const
Definition: saldisp.hxx:365
RenderEntryMap m_aRenderData
Definition: saldisp.hxx:257
bool HasGLXExtension(const char *name) const
Status
void Remove(int fd)
SalColormap & operator=(SalColormap &&)=default
virtual void TriggerUserEventProcessing() override
Display * GetDisplay() const
Definition: saldisp.hxx:201
SalRGB eRGBMode_
Definition: saldisp.hxx:79
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:125
virtual void Dispatch(XEvent *pEvent) override
SalI18N_InputMethod * GetInputMethod() const
Definition: saldisp.hxx:200
const Size & GetScreenSize(SalX11Screen nXScreen) const
Definition: saldisp.hxx:362
bool CheckTimeout(bool bExecuteTimers=true)
VisualID GetVisualId() const
Definition: saldisp.hxx:90
Pixel GetWhitePixel() const
Definition: saldisp.hxx:146
void TriggerUserEventProcessing()
::Window GetDrawable(SalX11Screen nXScreen) const
Definition: saldisp.hxx:359
::Window GetRootWindow(SalX11Screen nXScreen) const
Definition: saldisp.hxx:386
int nGreenShift_
Definition: saldisp.hxx:81
int nBlueBits_
Definition: saldisp.hxx:85
SalI18N_KeyboardExtension * GetKbdExtension() const
Definition: saldisp.hxx:380
virtual bool Synchronize(bool bOnoff) const override
#define VCLPLUG_GEN_PUBLIC
Definition: vclpluginapi.h:29
Display * GetDisplay() const
Definition: saldisp.hxx:360
std::list< SalObject * > & getSalObjects()
Definition: saldisp.hxx:392
Holds the information of our new child window.
unsigned int getXScreen() const
Definition: saltype.h:20
void Insert(int fd, void *data, YieldFunc pending, YieldFunc queued, YieldFunc handle)
sal_uLong m_nTimeoutMS
Definition: saldisp.hxx:170
const SalVisual & GetVisual(SalX11Screen nXScreen) const
Definition: saldisp.hxx:368
Display * GetXDisplay() const
Definition: saldisp.hxx:395
RenderEntryMap & GetRenderEntries(SalX11Screen nXScreen) const
Definition: saldisp.hxx:369
std::vector< sal_uInt16 > m_aLookupTable
Definition: saldisp.hxx:122
void Wakeup()
KeyIndicatorState
Definition: keycodes.hxx:184
Time GetX11ServerTime() const
Definition: saldisp.hxx:374
bool GetXPixels(XColor &rColor, int r, int g, int b) const
int GetClass() const
Definition: saldisp.hxx:92
bool Yield(bool bWait, bool bHandleAllCurrentEvents)
SalXLib * pXLib_
Definition: saldisp.hxx:275
Display
ScreenData m_aInvalidScreenData
Definition: saldisp.hxx:282
SalI18N_InputMethod * GetInputMethod() const
Definition: saldisp.hxx:379
srv_vendor_t
Definition: saldisp.hxx:62
SalRGB
Definition: saldisp.hxx:72
SalI18N_KeyboardExtension * mpKbdExtension
Definition: saldisp.hxx:276
Pixmap GetInvert50(SalX11Screen nXScreen) const
Definition: saldisp.hxx:366
exports com.sun.star.chart2. data
std::unordered_map< int, RenderEntry > RenderEntryMap
Definition: saldisp.hxx:239
sal_uLong GetMaxRequestSize() const
Definition: saldisp.hxx:371
OwnershipFlag(OwnershipFlag &&other) noexcept
Definition: saldisp.hxx:105
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
void Init()
GLXContext ctx
Definition: saldisp.hxx:218
void StartTimer(sal_uInt64 nMS)
Colormap m_hColormap
Definition: saldisp.hxx:118
A SalFrame is a system window (e.g. an X11 window).
Definition: salframe.hxx:114
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:48
const SalDisplay * GetDisplay() const
Definition: saldisp.hxx:142
int nRedBits_
Definition: saldisp.hxx:83
void StopTimer()
Colormap GetXColormap() const
Definition: saldisp.hxx:141
const std::vector< tools::Rectangle > & GetXineramaScreens() const
Definition: saldisp.hxx:385
OwnershipFlag()=default
std::vector< ScreenData > m_aScreens
Definition: saldisp.hxx:281
void GetLookupTable()
int nBlueShift_
Definition: saldisp.hxx:82
OwnershipFlag m_aVisualOwnership
Definition: saldisp.hxx:121
srv_vendor_t sal_GetServerVendor(Display *p_display)
const SalColormap & GetColormap(SalX11Screen nXScreen) const
Definition: saldisp.hxx:367
OwnershipFlag & operator=(OwnershipFlag &&other) noexcept
Definition: saldisp.hxx:107
Pixel GetBlackPixel() const
Definition: saldisp.hxx:147
KeySym nShiftKeySym_
Definition: saldisp.hxx:295
PointerStyle
Definition: ptrstyle.hxx:25
Visual * GetVisual() const
Definition: saldisp.hxx:91
Pixel GetPixel(Color nColor) const
std::vector< Color > m_aPalette
Definition: saldisp.hxx:119
timeval m_aTimeout
Definition: saldisp.hxx:169
Visual * GetXVisual() const
Definition: saldisp.hxx:145
Pixel m_nWhitePixel
Definition: saldisp.hxx:123
SalDisplay * getSalDisplay(GenericUnixSalData const *data)
Definition: saldisp.hxx:418
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:363
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:390
void SetKbdExtension(SalI18N_KeyboardExtension *pKbdExtension)
Definition: saldisp.hxx:381
const Pair & GetResolution() const
Definition: saldisp.hxx:370
Display * dpy
Definition: saldisp.hxx:214
int nRedShift_
Definition: saldisp.hxx:80
OUString getKeysymReplacementName(const OUString &pLang, KeySym nSymbol)
virtual ~SalX11Display() override
const SalX11Screen & GetDefaultXScreen() const
Definition: saldisp.hxx:361
XVisualInfo * vi
Definition: saldisp.hxx:217
KeySym nMod1KeySym_
Definition: saldisp.hxx:297
int nGreenBits_
Definition: saldisp.hxx:84
Window win
Definition: saldisp.hxx:216
Display * pDisp_
Definition: saldisp.hxx:278