BsLinuxWindow.cpp 13 KB

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