BsLinuxWindow.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsLinuxWindow.h"
  4. #include "BsLinuxPlatform.h"
  5. #include "BsLinuxDragAndDrop.h"
  6. #include <X11/Xlib.h>
  7. #include <X11/Xutil.h>
  8. #include <X11/Xatom.h>
  9. #include <X11/extensions/Xrandr.h>
  10. #define _NET_WM_STATE_REMOVE 0
  11. #define _NET_WM_STATE_ADD 1
  12. #define _NET_WM_STATE_TOGGLE 2
  13. #define WM_NormalState 1
  14. #define WM_IconicState 3
  15. namespace bs
  16. {
  17. enum class WindowState
  18. {
  19. Minimized,
  20. Maximized,
  21. Normal
  22. };
  23. struct LinuxWindow::Pimpl
  24. {
  25. ::Window xWindow = 0;
  26. INT32 x, y;
  27. UINT32 width, height;
  28. bool hasTitleBar = true;
  29. bool dragInProgress = false;
  30. bool resizeDisabled = false;
  31. WindowState state = WindowState::Normal;
  32. Rect2I dragZone;
  33. INT32 dragStartX, dragStartY;
  34. };
  35. LinuxWindow::LinuxWindow(const WINDOW_DESC &desc)
  36. {
  37. ::Display* display = LinuxPlatform::getXDisplay();
  38. INT32 screen;
  39. if(desc.screen == (UINT32)-1)
  40. screen = XDefaultScreen(display);
  41. else
  42. screen = std::min((INT32)desc.screen, XScreenCount(display));
  43. XSetWindowAttributes attributes;
  44. attributes.background_pixel = XWhitePixel(display, screen);
  45. attributes.border_pixel = XBlackPixel(display, screen);
  46. attributes.colormap = XCreateColormap(display,
  47. XRootWindow(display, screen),
  48. desc.visualInfo.visual,
  49. AllocNone);
  50. UINT32 borderWidth = 0;
  51. m->x = desc.x;
  52. m->y = desc.y;
  53. m->width = desc.width;
  54. m->height = desc.height;
  55. m->xWindow = XCreateWindow(display,
  56. XRootWindow(display, screen),
  57. desc.x, desc.y,
  58. desc.width, desc.height,
  59. borderWidth, desc.visualInfo.depth,
  60. InputOutput, desc.visualInfo.visual,
  61. CWBackPixel | CWBorderPixel | CWColormap, &attributes);
  62. XStoreName(display, m->xWindow, desc.title.c_str());
  63. XSizeHints hints;
  64. hints.flags = PPosition | PSize;
  65. hints.x = desc.x;
  66. hints.y = desc.y;
  67. hints.width = desc.width;
  68. hints.height = desc.height;
  69. if(!desc.allowResize)
  70. {
  71. hints.flags |= PMinSize | PMaxSize;
  72. hints.min_height = desc.height;
  73. hints.max_height = desc.height;
  74. hints.min_width = desc.width;
  75. hints.max_width = desc.width;
  76. }
  77. XSetNormalHints(display, m->xWindow, &hints);
  78. setShowDecorations(desc.showDecorations);
  79. setIsModal(desc.modal);
  80. // Ensures the child window is always on top of the parent window
  81. if(desc.parent)
  82. XSetTransientForHint(display, m->xWindow, desc.parent);
  83. XSelectInput(display, m->xWindow,
  84. ExposureMask | FocusChangeMask |
  85. KeyPressMask | KeyReleaseMask |
  86. ButtonPressMask | ButtonReleaseMask |
  87. EnterWindowMask | LeaveWindowMask |
  88. PointerMotionMask | ButtonMotionMask |
  89. StructureNotifyMask
  90. );
  91. XMapWindow(display, m->xWindow);
  92. // Make sure we get the window delete message from WM, so we can clean up ourselves
  93. Atom atomDeleteWindow = XInternAtom(display, "WM_DELETE_WINDOW", False);
  94. XSetWMProtocols(display, m->xWindow, &atomDeleteWindow, 1);
  95. // Enable drag and drop
  96. LinuxDragAndDrop::makeDNDAware(m->xWindow);
  97. // Set background image if assigned
  98. if(desc.background)
  99. {
  100. Pixmap pixmap = LinuxPlatform::createPixmap(*desc.background);
  101. XSetWindowBackgroundPixmap(display, m->xWindow, pixmap);
  102. XFreePixmap(display, pixmap);
  103. }
  104. m->hasTitleBar = desc.showDecorations;
  105. m->resizeDisabled = !desc.allowResize;
  106. LinuxPlatform::_registerWindow(m->xWindow, this);
  107. }
  108. LinuxWindow::~LinuxWindow()
  109. {
  110. if(m->xWindow != 0)
  111. _cleanUp();
  112. }
  113. void LinuxWindow::close()
  114. {
  115. XDestroyWindow(LinuxPlatform::getXDisplay(), m->xWindow);
  116. XFlush(LinuxPlatform::getXDisplay());
  117. _cleanUp();
  118. }
  119. void LinuxWindow::move(INT32 x, INT32 y)
  120. {
  121. m->x = x;
  122. m->y = y;
  123. XMoveWindow(LinuxPlatform::getXDisplay(), m->xWindow, x, y);
  124. }
  125. void LinuxWindow::resize(UINT32 width, UINT32 height)
  126. {
  127. // If resize is disabled on WM level, we need to force it
  128. if(m->resizeDisabled)
  129. {
  130. XSizeHints hints;
  131. hints.flags = PMinSize | PMaxSize;
  132. hints.min_height = height;
  133. hints.max_height = height;
  134. hints.min_width = width;
  135. hints.max_width = width;
  136. XSetNormalHints(LinuxPlatform::getXDisplay(), m->xWindow, &hints);
  137. }
  138. m->width = width;
  139. m->height = height;
  140. XResizeWindow(LinuxPlatform::getXDisplay(), m->xWindow, width, height);
  141. }
  142. void LinuxWindow::hide()
  143. {
  144. // TODOPORT - Need to track all states so I can restore them on show()
  145. XUnmapWindow(LinuxPlatform::getXDisplay(), m->xWindow);
  146. }
  147. void LinuxWindow::show()
  148. {
  149. XMapWindow(LinuxPlatform::getXDisplay(), m->xWindow);
  150. XMoveResizeWindow(LinuxPlatform::getXDisplay(), m->xWindow, m->x, m->y, m->width, m->height);
  151. // TODOPORT - Restore all states (pos, size and style)
  152. }
  153. void LinuxWindow::maximize()
  154. {
  155. maximize(true);
  156. }
  157. void LinuxWindow::minimize()
  158. {
  159. minimize(true);
  160. }
  161. void LinuxWindow::restore()
  162. {
  163. if(isMaximized())
  164. maximize(false);
  165. else if(isMinimized())
  166. minimize(false);
  167. }
  168. INT32 LinuxWindow::getLeft() const
  169. {
  170. INT32 x, y;
  171. ::Window child;
  172. XTranslateCoordinates(LinuxPlatform::getXDisplay(), m->xWindow, DefaultRootWindow(LinuxPlatform::getXDisplay()),
  173. 0, 0, &x, &y, &child);
  174. return x;
  175. }
  176. INT32 LinuxWindow::getTop() const
  177. {
  178. INT32 x, y;
  179. ::Window child;
  180. XTranslateCoordinates(LinuxPlatform::getXDisplay(), m->xWindow, DefaultRootWindow(LinuxPlatform::getXDisplay()),
  181. 0, 0, &x, &y, &child);
  182. return y;
  183. }
  184. UINT32 LinuxWindow::getWidth() const
  185. {
  186. XWindowAttributes xwa;
  187. XGetWindowAttributes(LinuxPlatform::getXDisplay(), m->xWindow, &xwa);
  188. return (UINT32)xwa.width;
  189. }
  190. UINT32 LinuxWindow::getHeight() const
  191. {
  192. XWindowAttributes xwa;
  193. XGetWindowAttributes(LinuxPlatform::getXDisplay(), m->xWindow, &xwa);
  194. return (UINT32)xwa.height;
  195. }
  196. Vector2I LinuxWindow::windowToScreenPos(const Vector2I& windowPos) const
  197. {
  198. Vector2I screenPos;
  199. ::Window child;
  200. XTranslateCoordinates(LinuxPlatform::getXDisplay(), m->xWindow, DefaultRootWindow(LinuxPlatform::getXDisplay()),
  201. windowPos.x, windowPos.y, &screenPos.x, &screenPos.y, &child);
  202. return screenPos;
  203. }
  204. Vector2I LinuxWindow::screenToWindowPos(const Vector2I& screenPos) const
  205. {
  206. Vector2I windowPos;
  207. ::Window child;
  208. XTranslateCoordinates(LinuxPlatform::getXDisplay(), DefaultRootWindow(LinuxPlatform::getXDisplay()), m->xWindow,
  209. screenPos.x, screenPos.y, &windowPos.x, &windowPos.y, &child);
  210. return windowPos;
  211. }
  212. void LinuxWindow::setIcon(const PixelData& data)
  213. {
  214. Pixmap iconPixmap = LinuxPlatform::createPixmap(data);
  215. XWMHints* hints = XAllocWMHints();
  216. hints->flags = IconPixmapHint;
  217. hints->icon_pixmap = iconPixmap;
  218. XSetWMHints(LinuxPlatform::getXDisplay(), m->xWindow, hints);
  219. XFlush(LinuxPlatform::getXDisplay());
  220. XFree(hints);
  221. XFreePixmap(LinuxPlatform::getXDisplay(), iconPixmap);
  222. }
  223. void LinuxWindow::_cleanUp()
  224. {
  225. LinuxPlatform::_unregisterWindow(m->xWindow);
  226. m->xWindow = 0;
  227. }
  228. void LinuxWindow::_setDragZone(const Rect2I& rect)
  229. {
  230. m->dragZone = rect;
  231. }
  232. bool LinuxWindow::_dragStart(int32_t x, int32_t y)
  233. {
  234. if(m->hasTitleBar)
  235. return false;
  236. if(m->dragZone.width == 0 || m->dragZone.height == 0)
  237. return false;
  238. if(x >= m->dragZone.x && x < (m->dragZone.x + m->dragZone.width) &&
  239. y >= m->dragZone.y && y < (m->dragZone.y + m->dragZone.height))
  240. {
  241. m->dragStartX = x;
  242. m->dragStartY = y;
  243. m->dragInProgress = true;
  244. return true;
  245. }
  246. return false;
  247. }
  248. void LinuxWindow::_dragUpdate(int32_t x, int32_t y)
  249. {
  250. if(!m->dragInProgress)
  251. return;
  252. int32_t offsetX = x - m->dragStartX;
  253. int32_t offsetY = y - m->dragStartY;
  254. move(getLeft() + offsetX, getTop() + offsetY);
  255. }
  256. void LinuxWindow::_dragEnd()
  257. {
  258. m->dragInProgress = false;
  259. }
  260. ::Window LinuxWindow::_getXWindow() const
  261. {
  262. return m->xWindow;
  263. }
  264. bool LinuxWindow::isMaximized() const
  265. {
  266. Atom wmState = XInternAtom(LinuxPlatform::getXDisplay(), "_NET_WM_STATE", False);
  267. Atom type;
  268. int32_t format;
  269. uint64_t length;
  270. uint64_t remaining;
  271. uint8_t* data = nullptr;
  272. int32_t result = XGetWindowProperty(LinuxPlatform::getXDisplay(), m->xWindow, wmState,
  273. 0, 1024, False, XA_ATOM, &type, &format,
  274. &length, &remaining, &data);
  275. if (result == Success)
  276. {
  277. Atom* atoms = (Atom*)data;
  278. Atom wmMaxHorz = XInternAtom(LinuxPlatform::getXDisplay(), "_NET_WM_STATE_MAXIMIZED_HORZ", False);
  279. Atom wmMaxVert = XInternAtom(LinuxPlatform::getXDisplay(), "_NET_WM_STATE_MAXIMIZED_VERT", False);
  280. bool foundHorz = false;
  281. bool foundVert = false;
  282. for (uint64_t i = 0; i < length; i++)
  283. {
  284. if (atoms[i] == wmMaxHorz)
  285. foundHorz = true;
  286. if (atoms[i] == wmMaxVert)
  287. foundVert = true;
  288. if (foundVert && foundHorz)
  289. return true;
  290. }
  291. XFree(atoms);
  292. }
  293. return false;
  294. }
  295. bool LinuxWindow::isMinimized()
  296. {
  297. Atom wmState = XInternAtom(LinuxPlatform::getXDisplay(), "WM_STATE", True);
  298. Atom type;
  299. int32_t format;
  300. uint64_t length;
  301. uint64_t remaining;
  302. uint8_t* data = nullptr;
  303. int32_t result = XGetWindowProperty(LinuxPlatform::getXDisplay(), m->xWindow, wmState,
  304. 0, 1024, False, AnyPropertyType, &type, &format,
  305. &length, &remaining, &data);
  306. if(result == Success)
  307. {
  308. long* state = (long*) data;
  309. if(state[0] == WM_IconicState)
  310. return true;
  311. }
  312. return false;
  313. }
  314. void LinuxWindow::maximize(bool enable)
  315. {
  316. Atom wmState = XInternAtom(LinuxPlatform::getXDisplay(), "_NET_WM_STATE", False);
  317. Atom wmMaxHorz = XInternAtom(LinuxPlatform::getXDisplay(), "_NET_WM_STATE_MAXIMIZED_HORZ", False);
  318. Atom wmMaxVert = XInternAtom(LinuxPlatform::getXDisplay(), "_NET_WM_STATE_MAXIMIZED_VERT", False);
  319. XEvent xev;
  320. memset(&xev, 0, sizeof(xev));
  321. xev.type = ClientMessage;
  322. xev.xclient.window = m->xWindow;
  323. xev.xclient.message_type = wmState;
  324. xev.xclient.format = 32;
  325. xev.xclient.data.l[0] = enable ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
  326. xev.xclient.data.l[1] = wmMaxHorz;
  327. xev.xclient.data.l[2] = wmMaxVert;
  328. XSendEvent(LinuxPlatform::getXDisplay(), DefaultRootWindow(LinuxPlatform::getXDisplay()), False,
  329. SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  330. }
  331. void LinuxWindow::minimize(bool enable)
  332. {
  333. XEvent xev;
  334. Atom wmChange = XInternAtom(LinuxPlatform::getXDisplay(), "WM_CHANGE_STATE", False);
  335. memset(&xev, 0, sizeof(xev));
  336. xev.type = ClientMessage;
  337. xev.xclient.window = m->xWindow;
  338. xev.xclient.message_type = wmChange;
  339. xev.xclient.format = 32;
  340. xev.xclient.data.l[0] = enable ? WM_IconicState : WM_NormalState;
  341. XSendEvent(LinuxPlatform::getXDisplay(), DefaultRootWindow(LinuxPlatform::getXDisplay()), False,
  342. SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  343. }
  344. void LinuxWindow::_setFullscreen(bool fullscreen)
  345. {
  346. // Attempt to bypass compositor if switching to fullscreen
  347. if(fullscreen)
  348. {
  349. Atom wmBypassCompositor = XInternAtom(LinuxPlatform::getXDisplay(), "_NET_WM_BYPASS_COMPOSITOR", False);
  350. if (wmBypassCompositor)
  351. {
  352. static constexpr uint32_t enabled = 1;
  353. XChangeProperty(LinuxPlatform::getXDisplay(), m->xWindow, wmBypassCompositor,
  354. XA_CARDINAL, 32, PropModeReplace, (unsigned char*) &enabled, 1);
  355. }
  356. }
  357. // Make the switch to fullscreen
  358. XEvent xev;
  359. Atom wmState = XInternAtom(LinuxPlatform::getXDisplay(), "_NET_WM_STATE", False);
  360. Atom wmFullscreen = XInternAtom(LinuxPlatform::getXDisplay(), "_NET_WM_STATE_FULLSCREEN", False);
  361. memset(&xev, 0, sizeof(xev));
  362. xev.type = ClientMessage;
  363. xev.xclient.window = m->xWindow;
  364. xev.xclient.message_type = wmState;
  365. xev.xclient.format = 32;
  366. xev.xclient.data.l[0] = fullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
  367. xev.xclient.data.l[1] = wmFullscreen;
  368. xev.xclient.data.l[2] = 0;
  369. XSendEvent(LinuxPlatform::getXDisplay(), DefaultRootWindow(LinuxPlatform::getXDisplay()), False,
  370. SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  371. }
  372. void LinuxWindow::setShowDecorations(bool show)
  373. {
  374. static constexpr uint32_t MWM_HINTS_FUNCTIONS = (1 << 0);
  375. static constexpr uint32_t MWM_HINTS_DECORATIONS = (1 << 1);
  376. static constexpr uint32_t MWM_DECOR_BORDER = (1 << 1);
  377. static constexpr uint32_t MWM_DECOR_RESIZEH = (1 << 2);
  378. static constexpr uint32_t MWM_DECOR_TITLE = (1 << 3);
  379. static constexpr uint32_t MWM_DECOR_MENU = (1 << 4);
  380. static constexpr uint32_t MWM_DECOR_MINIMIZE = (1 << 5);
  381. static constexpr uint32_t MWM_DECOR_MAXIMIZE = (1 << 6);
  382. static constexpr uint32_t MWM_FUNC_RESIZE = (1 << 1);
  383. static constexpr uint32_t MWM_FUNC_MOVE = (1 << 2);
  384. static constexpr uint32_t MWM_FUNC_MINIMIZE = (1 << 3);
  385. static constexpr uint32_t MWM_FUNC_MAXIMIZE = (1 << 4);
  386. static constexpr uint32_t MWM_FUNC_CLOSE = (1 << 5);
  387. struct MotifHints
  388. {
  389. uint32_t flags;
  390. uint32_t functions;
  391. uint32_t decorations;
  392. int32_t inputMode;
  393. uint32_t status;
  394. };
  395. if(show)
  396. return;
  397. MotifHints motifHints;
  398. motifHints.flags = MWM_HINTS_DECORATIONS;
  399. motifHints.decorations = 0;
  400. motifHints.functions = 0;
  401. motifHints.inputMode = 0;
  402. motifHints.status = 0;
  403. Atom wmHintsAtom = XInternAtom(LinuxPlatform::getXDisplay(), "_MOTIF_WM_HINTS", False);
  404. XChangeProperty(LinuxPlatform::getXDisplay(), m->xWindow,
  405. wmHintsAtom, wmHintsAtom,
  406. 32,
  407. PropModeReplace,
  408. (unsigned char *)&motifHints,
  409. 5);
  410. }
  411. void LinuxWindow::setIsModal(bool modal)
  412. {
  413. if(modal)
  414. {
  415. Atom wmState = XInternAtom(LinuxPlatform::getXDisplay(), "_NET_WM_STATE", False);
  416. Atom wmValue = XInternAtom(LinuxPlatform::getXDisplay(), "_NET_WM_STATE_MODAL", False);
  417. XEvent xev;
  418. memset(&xev, 0, sizeof(xev));
  419. xev.type = ClientMessage;
  420. xev.xclient.window = m->xWindow;
  421. xev.xclient.message_type = wmState;
  422. xev.xclient.format = 32;
  423. xev.xclient.data.l[0] = _NET_WM_STATE_ADD;
  424. xev.xclient.data.l[1] = wmValue;
  425. xev.xclient.data.l[2] = 0;
  426. xev.xclient.data.l[3] = 1;
  427. XSendEvent(LinuxPlatform::getXDisplay(), DefaultRootWindow(LinuxPlatform::getXDisplay()), False,
  428. SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  429. }
  430. }
  431. }