Gorgon Game Engine
X11.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <set>
5 #include <string>
6 #include <limits>
7 #include <memory>
8 
9 #include <X11/X.h>
10 #include <X11/Xlib.h>
11 
12 #undef None
13 #undef Success
14 
15 #include <GL/glx.h>
16 
17 #include "../../Geometry/Point.h"
18 #include "../../Event.h"
19 #include "../../Input.h"
20 #include "../../Input/DnD.h"
21 #include "../../Any.h"
22 
23 #include "../../Window.h"
24 
25 
26 
27 static constexpr int Success = 0, None = 0;
28 
29 namespace Gorgon {
30 
31 
32 namespace internal {
33 
34  struct windowdata {
36  bool min = false;
37  bool pointerdisplayed=true;
38  bool move=false;
40  bool ismapped=false;
41  GLXContext context=0;
42  bool focused = false;
43  Geometry::Point ppoint = {std::numeric_limits<int>::min(), std::numeric_limits<int>::min()};
44 
45  std::map<Input::Key, ConsumableEvent<Window, Input::Key, bool>::Token> handlers;
46  std::set<KeySym> pressed;
47 
48  struct xdnddata {
49  bool filelist = false;
50  bool utf8 = false;
51  bool string = false;
52 
53  bool requested = false;
54  int drop = 0;
55 
56  bool localpointer = false;
57  } xdnd;
58  };
59 }
60 
61 namespace WindowManager {
62 
63 
65  extern Atom
112 
113 
115  struct clipboardentry {
116  Atom type;
117  std::shared_ptr<CopyFreeAny> data;
118 
119  bool operator ==(const clipboardentry &other) const {
120  return type == other.type;
121  }
122  };
123 
124  namespace internal {
126 
127  struct icondata {
128  ~icondata() {
129  delete[] data;
130  }
131 
132  long w = 0, h = 0;
133  Byte *data = nullptr;
134  };
135 
136  }
137 
139 
140  void XdndInit(Gorgon::internal::windowdata *w);
141 
142  std::string xeventname(XEvent &event);
143 
144  std::string GetAtomName(Atom atom);
145 
146  Input::Mouse::Button buttonfromx11(unsigned btn);
147 
148  Input::Keyboard::Key mapx11key(KeySym key, unsigned int keycode);
149 
150  void assertkeys(Window &wind, Gorgon::internal::windowdata *data);
151 
152  std::string xeventname(XEvent &event);
153 
154 
155 
157  extern Display *display;
158 
160  extern Visual *visual;
161 
163  extern Cursor blank_cursor;
164 
166  extern bool xrandr;
167 
169  extern bool xinerama;
170 
171 
173  int waitfor_mapnotify(Display *d, XEvent *e, char *arg);
174 
175  int waitfor_propertynotify(Display *d, XEvent *e, char *arg);
176 
177  int waitfor_cppropertynotify(Display *d, XEvent *e, char *arg);
178 
179  int waitfor_selectionnotify(Display *d, XEvent *e, char *arg);
180 
181  ::Window getanywindow();
182 
183  extern std::vector<clipboardentry> clipboard_entries;
184 
185  void handleclipboardevent(XEvent event);
186 
187  void handledndevent(XEvent event, Window &wind);
188 
189  void handleinputevent(XEvent event, Window &wind);
190 
191  template<class T_>
192  T_ GetX4Prop(Atom atom, ::Window w, const T_ &def) {
193  Atom actual_type;
194  int actual_format;
195  unsigned long item_count;
196  unsigned long bytes_left;
197 
198  Byte *data;
199 
200  int status = XGetWindowProperty(
201  display,
202  w,
203  atom,
204  0, 4, 0, AnyPropertyType,
205  &actual_type, &actual_format,
206  &item_count, &bytes_left,
207  &data
208  );
209 
210  if(status!=Success) return def;
211 
212  if(item_count<4 || actual_format!=32) return def;
213 
214  long *cardinals=reinterpret_cast<long*>(data);
215 
216  T_ ret={
217  (int)cardinals[0],
218  (int)cardinals[1],
219  (int)cardinals[2],
220  (int)cardinals[3]
221  };
222 
223  XFree(data);
224 
225  return ret;
226  }
227 
229 }}
230 
Gorgon::internal::windowdata::move
bool move
Definition: X11.h:38
Gorgon::Input::Mouse::None
@ None
Definition: Mouse.h:32
Gorgon::WindowManager::XA_WM_NAME
Atom XA_WM_NAME
Definition: X11.h:85
Gorgon::WindowManager::XA_ATOM
Atom XA_ATOM
Definition: X11.h:79
Gorgon::Window::GetMinimumRequiredSize
static Geometry::Size GetMinimumRequiredSize()
Returns the minimum size required to fit any window inside.
Definition: WindowManager.cpp:14
Gorgon::swap
void swap(Event< Source_, Args_... > &l, Event< Source_, Args_... > &r)
Swaps two events.
Definition: Event.h:351
Gorgon::WindowManager::GetMousePosition
Geometry::Point GetMousePosition(Gorgon::internal::windowdata *wind)
Definition: X11.cpp:223
Gorgon::WindowManager::Initialize
void Initialize()
Initializes window manager system.
Definition: WindowManager.cpp:40
Gorgon::Window
This class represents a window.
Definition: Window.h:31
Gorgon::WindowManager::XA_CP_PROP
Atom XA_CP_PROP
Definition: X11.h:98
Gorgon::Input::Keyboard::Key
Gorgon::Input::Key Key
A key on the keyboard.
Definition: Keyboard.h:12
Gorgon::WindowManager::handleinputevent
void handleinputevent(XEvent event, Window &wind)
Definition: Input.cpp:463
Gorgon::WindowManager::XdndActionCopy
Atom XdndActionCopy
Definition: X11.h:107
Gorgon::Containers::basic_Image::GetMode
Graphics::ColorMode GetMode() const
Returns the color mode of the image.
Definition: Image.h:1311
Gorgon::internal::windowdata::handlers
std::map< Input::Key, ConsumableEvent< Window, Input::Key, bool >::Token > handlers
Definition: X11.h:45
Gorgon::Containers::Image
basic_Image< Byte > Image
Definition: Image.h:1364
Gorgon::internal::windowdata::focused
bool focused
Definition: X11.h:42
Gorgon::Containers::basic_Image::ImportBMP
bool ImportBMP(const std::string &filename, bool dib=false)
Imports a given bitmap file. BMP RLE compression and colorspaces are not supported.
Definition: Image.h:591
Gorgon::WindowManager::Monitor::FromLocation
static const Monitor * FromLocation(Geometry::Point location)
Returns the monitor from the given location. If none found, will return nullptr.
Definition: WindowManager.h:137
Gorgon::WindowManager::GetClipboardFormats
std::vector< Resource::GID::Type > GetClipboardFormats()
Returns the list of formats that is in the clipboard supported by the Gorgon Library.
Definition: Clipboard.cpp:223
Gorgon::WindowManager::XA_PROTOCOLS
Atom XA_PROTOCOLS
Definition: X11.h:69
X11Keysym.h
Gorgon::Graphics::ColorMode
ColorMode
Color modes for images.
Definition: Color.h:16
Gorgon::Resource::GID::Image_Data
constexpr Type Image_Data
Uncompressed image data.
Definition: GID.h:155
Gorgon::internal::windowdata::xdnddata::requested
bool requested
Definition: X11.h:53
Gorgon::Event
This class provides event mechanism.
Definition: Event.h:134
Gorgon::Encoding::Png
PNG Png
A ready to use PNG class.
Definition: PNG.cpp:304
Gorgon::WindowManager::XdndFinished
Atom XdndFinished
Definition: X11.h:102
Gorgon::WindowManager::Icon::Destroy
void Destroy()
Definition: X11.cpp:274
Gorgon::WindowManager::XA_NET_FRAME_EXTENTS
Atom XA_NET_FRAME_EXTENTS
Definition: X11.h:82
Gorgon::internal::windowdata::xdnddata::drop
int drop
Definition: X11.h:54
Gorgon::WindowManager::XA_Filelist
Atom XA_Filelist
Definition: X11.h:110
Gorgon::WindowManager::XA_TEXT
Atom XA_TEXT
Definition: X11.h:71
Gorgon::Geometry::basic_Point::X
T_ X
X coordinate.
Definition: Point.h:368
Gorgon::Containers::basic_Image::CopyToBGRABufferLong
void CopyToBGRABufferLong(unsigned long *buffer) const
Copies this image to a RGBA buffer, buffer should be resize before calling this function.
Definition: Image.h:536
Gorgon::WindowManager::XA_INCR
Atom XA_INCR
Definition: X11.h:81
Gorgon::Geometry::basic_Rectangle::X
T_ X
X coordinate of the top left corner of this rectangle.
Definition: Rectangle.h:354
Gorgon::WindowManager::XA_CARDINAL
Atom XA_CARDINAL
Definition: X11.h:80
Gorgon::Geometry::basic_Size::Height
T_ Height
Height of this size object.
Definition: Size.h:261
Gorgon::Window::Fullscreen
static const struct Gorgon::Window::FullscreenTag Fullscreen
Definition: WindowManager.cpp:12
Gorgon::WindowManager::XA_NET_REQUEST_FRAME_EXTENTS
Atom XA_NET_REQUEST_FRAME_EXTENTS
Definition: X11.h:84
Gorgon::WindowManager::XdndLeave
Atom XdndLeave
Definition: X11.h:105
Gorgon::Resource::GID::Text
constexpr Type Text
Stores text data, no longer a resource.
Definition: GID.h:134
Gorgon::WindowManager::internal::getdata
Gorgon::internal::windowdata * getdata(const Window &w)
Definition: Window.cpp:15
Gorgon::WindowManager::XA_TARGETS
Atom XA_TARGETS
Definition: X11.h:68
Gorgon::WindowManager::init
void init()
Definition: X11.cpp:143
Gorgon::Resource::GID::URIList
constexpr Type URIList
List of URIs, not a resource.
Definition: GID.h:146
Gorgon::internal::windowdata::min
bool min
Definition: X11.h:36
Gorgon::Encoding::PNG::Encode
void Encode(const Containers::Image &input, std::ostream &output, bool replace_colormode=false)
Encodes a given input.
Definition: PNG.h:163
Gorgon::internal::windowdata::pressed
std::set< KeySym > pressed
Definition: X11.h:46
Gorgon::WindowManager::XdndSelection
Atom XdndSelection
Definition: X11.h:100
Gorgon::WindowManager::XA_BMP
Atom XA_BMP
Definition: X11.h:78
Gorgon::WindowManager::getanywindow
::Window getanywindow()
Definition: Window.cpp:21
Gorgon::WindowManager::Monitor::Refresh
static void Refresh(bool force=false)
Asks WindowManager to refresh the list of monitors.
Definition: Monitor.cpp:86
Gorgon::Geometry::basic_Rectangle::Width
T_ Width
Width of the rectangle.
Definition: Rectangle.h:360
Gorgon::Containers::basic_Image::GetWidth
int GetWidth() const
Returns the width of the image.
Definition: Image.h:1296
Gorgon::WindowManager::Icon::FromImage
void FromImage(const Containers::Image &image)
Definition: X11.cpp:260
Gorgon::Resource::GID::URL
constexpr Type URL
URL data, not a resource.
Definition: GID.h:140
Gorgon::WindowManager::XdndActionMove
Atom XdndActionMove
Definition: X11.h:108
Gorgon::internal::windowdata::pointerdisplayed
bool pointerdisplayed
Definition: X11.h:37
Gorgon::Geometry::basic_Bounds::Left
T_ Left
Left-most boundary.
Definition: Bounds.h:399
Gorgon::internal::windowdata::moveto
Geometry::Point moveto
Definition: X11.h:39
Gorgon::WindowManager::XA_STRING
Atom XA_STRING
Definition: X11.h:72
Gorgon::internal::windowdata::context
GLXContext context
Definition: X11.h:41
Gorgon::WindowManager::XA_UTF8_STRING
Atom XA_UTF8_STRING
Definition: X11.h:73
Gorgon::WindowManager::XA_WM_CHANGE_STATE
Atom XA_WM_CHANGE_STATE
Definition: X11.h:94
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Geometry::basic_Size
This class represents a 2D geometric size.
Definition: Size.h:23
Gorgon::WindowManager::XA_JPG
Atom XA_JPG
Definition: X11.h:77
Gorgon::IO::MemoryInputStream
Definition: MemoryStream.h:75
Gorgon::WindowManager::Monitor::~Monitor
~Monitor()
Definition: Monitor.cpp:23
Gorgon::WindowManager::assertkeys
void assertkeys(Window &wind, Gorgon::internal::windowdata *data)
Definition: Input.cpp:267
Gorgon::Encoding::URIDecode
std::string URIDecode(const std::string &str)
Decodes a given URI string according to RFC 3986. May throw URIError.
Definition: URI.cpp:74
Gorgon::WindowManager::handledndevent
void handledndevent(XEvent event, Window &wind)
Definition: DnD.cpp:312
Gorgon::WindowManager::XA_NET_WM_STATE_ADD
Atom XA_NET_WM_STATE_ADD
Definition: X11.h:88
Gorgon::WindowManager::SetClipboardList
void SetClipboardList(std::vector< std::string > list, Resource::GID::Type type=Resource::GID::FileList, bool append=false)
Sets a list of strings to the clipboard.
Definition: Clipboard.cpp:493
Gorgon::WindowManager::GetClipboardText
std::string GetClipboardText(Resource::GID::Type type=Resource::GID::Text)
Returns the clipboard text.
Definition: Clipboard.cpp:249
Gorgon::Containers::Collection
Collection is a container for reference typed objects.
Definition: Collection.h:21
Gorgon::Geometry::Point
basic_Point< int > Point
Definition: Point.h:598
Gorgon::WindowManager::XA_NET_WM_ICON
Atom XA_NET_WM_ICON
Definition: X11.h:96
Gorgon::WindowManager::Icon::operator=
Icon & operator=(const Icon &)=delete
Gorgon::WindowManager::XdndPosition
Atom XdndPosition
Definition: X11.h:104
Gorgon::WindowManager::Icon::Icon
Icon()
Definition: X11.cpp:239
Gorgon::WindowManager::XdndStatus
Atom XdndStatus
Definition: X11.h:103
Gorgon::internal::windowdata::handle
::Window handle
Definition: X11.h:35
Gorgon::WindowManager::XA_STRUT
Atom XA_STRUT
Definition: X11.h:95
Gorgon::Resource::GID::FileList
constexpr Type FileList
List of local files, not a resource.
Definition: GID.h:143
Gorgon::Graphics::HasAlpha
bool HasAlpha(ColorMode mode)
Returns if the given color mode has alpha channel.
Definition: Color.h:69
Gorgon::WindowManager::XA_CLIPBOARD
Atom XA_CLIPBOARD
X11 atoms for various data identifiers.
Gorgon::Containers::basic_Image::Duplicate
basic_Image Duplicate() const
Duplicates this image, essentially performing the work of copy constructor.
Definition: Image.h:62
Gorgon::WindowManager::XA_PNG
Atom XA_PNG
Definition: X11.h:76
Gorgon::WindowManager::GetClipboardBitmap
Containers::Image GetClipboardBitmap()
Returns a bitmap from the clipboard.
Definition: Clipboard.cpp:541
Gorgon::WindowManager::WM_DELETE_WINDOW
Atom WM_DELETE_WINDOW
Definition: X11.h:70
Gorgon::internal::windowdata::ppoint
Geometry::Point ppoint
Definition: X11.h:43
Gorgon::Geometry::operator==
bool operator==(PointList< P_ > &left, const PointList< P_ > &right)
Comparison: this operation is expensive: O(n).
Definition: PointList.h:385
Gorgon::Geometry::basic_Point
This class represents a 2D point.
Definition: Point.h:32
Gorgon::WindowManager::XA_NET_WM_STATE_FULLSCREEN
Atom XA_NET_WM_STATE_FULLSCREEN
Definition: X11.h:89
Gorgon::WindowManager::XA_NET_WM_STATE_MAXIMIZED_HORZ
Atom XA_NET_WM_STATE_MAXIMIZED_HORZ
Definition: X11.h:90
Gorgon::Encoding::Jpg
JPEG Jpg
Definition: JPEG.cpp:327
Gorgon::WindowManager::XA_NET_WM_STATE_MAXIMIZED_VERT
Atom XA_NET_WM_STATE_MAXIMIZED_VERT
Definition: X11.h:91
Gorgon::WindowManager::XA_NET_ACTIVE_WINDOW
Atom XA_NET_ACTIVE_WINDOW
Definition: X11.h:93
Gorgon::WindowManager::XA_NET_WM_NAME
Atom XA_NET_WM_NAME
Definition: X11.h:86
Gorgon::Byte
unsigned char Byte
Represents smallest cell in memory.
Definition: Types.h:9
Gorgon::Window::Windows
static const Containers::Collection< Window > & Windows
List of currently created windows.
Definition: Window.h:401
Gorgon::Geometry::basic_Rectangle::Y
T_ Y
Y coordinate of the top left corner of this rectangle.
Definition: Rectangle.h:357
Gorgon::WindowManager::addpadding
void addpadding(const Monitor *monitor, int l, int t, int r, int b)
Definition: Monitor.cpp:27
Gorgon::Geometry::basic_Bounds::Right
T_ Right
Right-most boundary.
Definition: Bounds.h:405
Gorgon::WindowManager::XA_NET_WORKAREA
Atom XA_NET_WORKAREA
Definition: X11.h:83
Gorgon::Geometry::basic_Rectangle::Height
T_ Height
Height of the rectangle.
Definition: Rectangle.h:363
Gorgon::WindowManager::XA_TEXT_HTML
Atom XA_TEXT_HTML
Definition: X11.h:74
Gorgon::WindowManager::XA_TIMESTAMP
Atom XA_TIMESTAMP
Definition: X11.h:67
Gorgon::WindowManager::XA_NET_WM_STATE_HIDDEN
Atom XA_NET_WM_STATE_HIDDEN
Definition: X11.h:92
Gorgon::Containers::PushBackUnique
void PushBackUnique(V_ &vector, T_ &&val)
This function push_backs an item to the given vector if the item does not exists in the vector.
Definition: Vector.h:10
Gorgon::Geometry::basic_Bounds::Bottom
T_ Bottom
Bottom-most boundary.
Definition: Bounds.h:408
Gorgon::internal::windowdata::xdnddata::localpointer
bool localpointer
Definition: X11.h:56
Gorgon::internal::windowdata::xdnddata::utf8
bool utf8
Definition: X11.h:50
Gorgon::Containers::basic_Image
This class is a container for image data.
Definition: Image.h:19
Gorgon::WindowManager::Monitor::IsChangeEventSupported
static bool IsChangeEventSupported()
In some cases, Changed event is not supported.
Definition: Monitor.cpp:179
Gorgon::Geometry::basic_Point::Y
T_ Y
Y coordinate.
Definition: Point.h:371
Gorgon::WindowManager::Icon
Represents an icon object that can be used as window icon.
Definition: WindowManager.h:43
Gorgon::Encoding::PNG::Decode
void Decode(std::istream &input, Containers::Image &output)
Decodes the given PNG data.
Definition: PNG.h:186
Gorgon::Geometry::basic_Size::Width
T_ Width
Width of this size object.
Definition: Size.h:258
Gorgon::WindowManager::GetClipboardList
std::vector< std::string > GetClipboardList(Resource::GID::Type type=Resource::GID::FileList)
Returns a list of strings from the clipboard.
Definition: Clipboard.cpp:383
Gorgon::Resource::GID::HTML
constexpr Type HTML
HTML data, not a resource.
Definition: GID.h:137
Gorgon::WindowManager::XdndAware
Atom XdndAware
Definition: X11.h:99
Gorgon::WindowManager::XdndTypeList
Atom XdndTypeList
Definition: X11.h:109
Gorgon::WindowManager::Monitor::ChangedEvent
static Event ChangedEvent
Fires when window manager raises an event about a change in the monitor or screen layout.
Definition: WindowManager.h:161
Gorgon::Input::Mouse::Button
Button
Lists the mouse button constants.
Definition: Mouse.h:31
Gorgon::Resource::GID::Type
Type to store GID information.
Definition: GID.h:23
Gorgon::internal::windowdata::ismapped
bool ismapped
Definition: X11.h:40
Gorgon::Containers::PushBackOrUpdate
void PushBackOrUpdate(V_ &vector, T_ &&val)
This function push_backs an item to the given vector if the item does not exists in the vector,...
Definition: Vector.h:34
Gorgon::WindowManager::Icon::~Icon
~Icon()
Definition: X11.cpp:282
Gorgon::internal::windowdata::xdnddata::filelist
bool filelist
Definition: X11.h:49
Gorgon::Containers::basic_Image::GetHeight
int GetHeight() const
Returns the height of the image.
Definition: Image.h:1301
Gorgon::Encoding::JPEG::Decode
void Decode(std::istream &input, Containers::Image &output)
Decodes given JPG data from the given input and creates the image.
Definition: JPEG.h:130
Gorgon::WindowManager::SetClipboardText
void SetClipboardText(const std::string &text, Resource::GID::Type type=Resource::GID::Text, bool unicode=true, bool append=false)
Sets the clipboard text to given string.
Definition: Clipboard.cpp:350
Gorgon::WindowManager::XA_URL
Atom XA_URL
Definition: X11.h:75
Gorgon::Geometry::basic_Rectangle
Represents a rectangle in a 2D space.
Definition: Rectangle.h:19
Gorgon::WindowManager::XdndDrop
Atom XdndDrop
Definition: X11.h:106
Gorgon::WindowManager::XA_PRIMARY
Atom XA_PRIMARY
Definition: X11.h:97
X11.h
Gorgon::WindowManager::buttonfromx11
Input::Mouse::Button buttonfromx11(unsigned btn)
Definition: Input.cpp:74
Gorgon::WindowManager::mapx11key
Input::Keyboard::Key mapx11key(KeySym key, unsigned int keycode)
Definition: Input.cpp:100
Gorgon::Encoding::JPEG::Encode
void Encode(Containers::Image &input, std::ostream &output, int quality=90)
Encode given image to JPG compressed data.
Definition: JPEG.h:162
Gorgon::WindowManager::xeventname
std::string xeventname(XEvent &event)
Definition: X11.cpp:289
Gorgon::WindowManager::XA_NET_WM_STATE
Atom XA_NET_WM_STATE
Definition: X11.h:87
Gorgon::WindowManager::CurrentContext
intptr_t CurrentContext()
Returns an identifier for the current context.
Definition: WindowManager.cpp:34
Gorgon::internal::windowdata::xdnd
struct Gorgon::internal::windowdata::xdnddata xdnd
Gorgon::internal::windowdata::xdnddata
Definition: X11.h:48
Gorgon::WindowManager::GetAtomName
std::string GetAtomName(Atom atom)
Definition: X11.cpp:215
Gorgon::internal::windowdata
Definition: X11.h:34
Gorgon::WindowManager::Monitor
Definition: WindowManager.h:84
Gorgon::WindowManager::XdndEnter
Atom XdndEnter
Definition: X11.h:101
Gorgon::Geometry::basic_Bounds::Top
T_ Top
Top-most boundary.
Definition: Bounds.h:402
Gorgon::WindowManager::SetClipboardBitmap
void SetClipboardBitmap(Containers::Image img, bool append=false)
Changes the clipboard to the given image.
Definition: Clipboard.cpp:668