BsLinuxPlatform.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Image/BsPixelData.h"
  4. #include "Image/BsPixelUtil.h"
  5. #include "String/BsUnicode.h"
  6. #include "Linux/BsLinuxPlatform.h"
  7. #include "Linux/BsLinuxWindow.h"
  8. #include "RenderAPI/BsRenderWindow.h"
  9. #include "BsLinuxDragAndDrop.h"
  10. #include "BsCoreApplication.h"
  11. #include <X11/X.h>
  12. #include <X11/Xatom.h>
  13. #include <X11/Xcursor/Xcursor.h>
  14. #include <X11/Xlib.h>
  15. #include <X11/XKBlib.h>
  16. namespace bs
  17. {
  18. Event<void(const Vector2I&, const OSPointerButtonStates&)> Platform::onCursorMoved;
  19. Event<void(const Vector2I&, OSMouseButton button, const OSPointerButtonStates&)> Platform::onCursorButtonPressed;
  20. Event<void(const Vector2I&, OSMouseButton button, const OSPointerButtonStates&)> Platform::onCursorButtonReleased;
  21. Event<void(const Vector2I&, const OSPointerButtonStates&)> Platform::onCursorDoubleClick;
  22. Event<void(InputCommandType)> Platform::onInputCommand;
  23. Event<void(float)> Platform::onMouseWheelScrolled;
  24. Event<void(UINT32)> Platform::onCharInput;
  25. Event<void()> Platform::onMouseCaptureChanged;
  26. enum class X11CursorType
  27. {
  28. Arrow,
  29. ArrowDrag,
  30. ArrowLeftRight,
  31. Wait,
  32. IBeam,
  33. SizeTopLeft,
  34. SizeTopRight,
  35. SizeBotLeft,
  36. SizeBotRight,
  37. SizeLeft,
  38. SizeRight,
  39. SizeTop,
  40. SizeBottom,
  41. Deny,
  42. Count
  43. };;
  44. struct Platform::Pimpl
  45. {
  46. ::Display* xDisplay = nullptr;
  47. ::Window mainXWindow = 0;
  48. ::Window fullscreenXWindow = 0;
  49. std::unordered_map<::Window, LinuxWindow*> windowMap;
  50. Mutex lock;
  51. XIM IM;
  52. XIC IC;
  53. Time lastButtonPressTime;
  54. Atom atomDeleteWindow;
  55. Atom atomWmState;
  56. Atom atomWmStateHidden;
  57. Atom atomWmStateMaxVert;
  58. Atom atomWmStateMaxHorz;
  59. // Clipboard
  60. WString clipboardData;
  61. // Cursor
  62. ::Cursor currentCursor = None;
  63. ::Cursor emptyCursor = None;
  64. bool isCursorHidden = false;
  65. Rect2I cursorClipRect;
  66. LinuxWindow* cursorClipWindow = nullptr;
  67. bool cursorClipEnabled = false;
  68. };
  69. static const UINT32 DOUBLE_CLICK_MS = 500;
  70. Vector2I _getCursorPosition(Platform::Pimpl* data)
  71. {
  72. Vector2I pos;
  73. UINT32 screenCount = (UINT32)XScreenCount(data->xDisplay);
  74. for (UINT32 i = 0; i < screenCount; ++i)
  75. {
  76. ::Window outRoot, outChild;
  77. INT32 childX, childY;
  78. UINT32 mask;
  79. if(XQueryPointer(data->xDisplay, XRootWindow(data->xDisplay, i), &outRoot, &outChild, &pos.x,
  80. &pos.y, &childX, &childY, &mask))
  81. break;
  82. }
  83. return pos;
  84. }
  85. void _setCursorPosition(Platform::Pimpl* data, const Vector2I& screenPos)
  86. {
  87. UINT32 screenCount = (UINT32)XScreenCount(data->xDisplay);
  88. // Note assuming screens are laid out horizontally left to right
  89. INT32 screenX = 0;
  90. for(UINT32 i = 0; i < screenCount; ++i)
  91. {
  92. ::Window root = XRootWindow(data->xDisplay, i);
  93. INT32 screenXEnd = screenX + XDisplayWidth(data->xDisplay, i);
  94. if(screenPos.x >= screenX && screenPos.x < screenXEnd)
  95. {
  96. XWarpPointer(data->xDisplay, None, root, 0, 0, 0, 0, screenPos.x, screenPos.y);
  97. XFlush(data->xDisplay);
  98. return;
  99. }
  100. screenX = screenXEnd;
  101. }
  102. }
  103. void applyCurrentCursor(Platform::Pimpl* data, ::Window window)
  104. {
  105. if(data->isCursorHidden)
  106. XDefineCursor(data->xDisplay, window, data->emptyCursor);
  107. else
  108. {
  109. if (data->currentCursor != None)
  110. XDefineCursor(data->xDisplay, window, data->currentCursor);
  111. else
  112. XUndefineCursor(data->xDisplay, window);
  113. }
  114. }
  115. void updateClipBounds(Platform::Pimpl* data, LinuxWindow* window)
  116. {
  117. if(!data->cursorClipEnabled || data->cursorClipWindow != window)
  118. return;
  119. data->cursorClipRect.x = window->getLeft();
  120. data->cursorClipRect.y = window->getTop();
  121. data->cursorClipRect.width = window->getWidth();
  122. data->cursorClipRect.height = window->getHeight();
  123. }
  124. bool clipCursor(Platform::Pimpl* data, Vector2I& pos)
  125. {
  126. if(!data->cursorClipEnabled)
  127. return false;
  128. INT32 clippedX = pos.x - data->cursorClipRect.x;
  129. INT32 clippedY = pos.y - data->cursorClipRect.y;
  130. if(clippedX < 0)
  131. clippedX = data->cursorClipRect.x + data->cursorClipRect.width + clippedX;
  132. else if(clippedX >= (INT32)data->cursorClipRect.width)
  133. clippedX = data->cursorClipRect.x + (clippedX - data->cursorClipRect.width);
  134. else
  135. clippedX = data->cursorClipRect.x + clippedX;
  136. if(clippedY < 0)
  137. clippedY = data->cursorClipRect.y + data->cursorClipRect.height + clippedY;
  138. else if(clippedY >= (INT32)data->cursorClipRect.height)
  139. clippedY = data->cursorClipRect.y + (clippedY - data->cursorClipRect.height);
  140. else
  141. clippedY = data->cursorClipRect.y + clippedY;
  142. if(clippedX != pos.x || clippedY != pos.y)
  143. {
  144. pos.x = clippedX;
  145. pos.y = clippedY;
  146. return true;
  147. }
  148. return false;
  149. }
  150. void setCurrentCursor(Platform::Pimpl* data, ::Cursor cursor)
  151. {
  152. if(data->currentCursor)
  153. XFreeCursor(data->xDisplay, data->currentCursor);
  154. data->currentCursor = cursor;
  155. for(auto& entry : data->windowMap)
  156. applyCurrentCursor(data, entry.first);
  157. }
  158. Platform::Pimpl* Platform::mData = bs_new<Platform::Pimpl>();
  159. Platform::~Platform()
  160. { }
  161. Vector2I Platform::getCursorPosition()
  162. {
  163. Lock lock(mData->lock);
  164. return _getCursorPosition(mData);
  165. }
  166. void Platform::setCursorPosition(const Vector2I& screenPos)
  167. {
  168. Lock lock(mData->lock);
  169. _setCursorPosition(mData, screenPos);
  170. }
  171. void Platform::captureMouse(const RenderWindow& window)
  172. {
  173. Lock lock(mData->lock);
  174. LinuxWindow* linuxWindow;
  175. window.getCustomAttribute("WINDOW", &linuxWindow);
  176. UINT32 mask = ButtonPressMask | ButtonReleaseMask | PointerMotionMask | FocusChangeMask;
  177. XGrabPointer(mData->xDisplay, linuxWindow->_getXWindow(), False, mask, GrabModeAsync,
  178. GrabModeAsync, None, None, CurrentTime);
  179. XSync(mData->xDisplay, False);
  180. }
  181. void Platform::releaseMouseCapture()
  182. {
  183. Lock lock(mData->lock);
  184. XUngrabPointer(mData->xDisplay, CurrentTime);
  185. XSync(mData->xDisplay, False);
  186. }
  187. bool Platform::isPointOverWindow(const RenderWindow& window, const Vector2I& screenPos)
  188. {
  189. Lock lock(mData->lock);
  190. LinuxWindow* linuxWindow;
  191. window.getCustomAttribute("WINDOW", &linuxWindow);
  192. ::Window xWindow = linuxWindow->_getXWindow();
  193. Vector2I pos;
  194. UINT32 screenCount = (UINT32)XScreenCount(mData->xDisplay);
  195. for (UINT32 i = 0; i < screenCount; ++i)
  196. {
  197. ::Window outRoot, outChild;
  198. INT32 childX, childY;
  199. UINT32 mask;
  200. if(XQueryPointer(mData->xDisplay, XRootWindow(mData->xDisplay, i), &outRoot, &outChild, &pos.x,
  201. &pos.y, &childX, &childY, &mask))
  202. {
  203. return outChild == xWindow;
  204. }
  205. }
  206. return false;
  207. }
  208. void Platform::hideCursor()
  209. {
  210. Lock lock(mData->lock);
  211. mData->isCursorHidden = true;
  212. for(auto& entry : mData->windowMap)
  213. applyCurrentCursor(mData, entry.first);
  214. }
  215. void Platform::showCursor()
  216. {
  217. Lock lock(mData->lock);
  218. mData->isCursorHidden = false;
  219. for(auto& entry : mData->windowMap)
  220. applyCurrentCursor(mData, entry.first);
  221. }
  222. bool Platform::isCursorHidden()
  223. {
  224. Lock lock(mData->lock);
  225. return mData->isCursorHidden;
  226. }
  227. void Platform::clipCursorToWindow(const RenderWindow& window)
  228. {
  229. Lock lock(mData->lock);
  230. LinuxWindow* linuxWindow;
  231. window.getCustomAttribute("WINDOW", &linuxWindow);
  232. mData->cursorClipEnabled = true;
  233. mData->cursorClipWindow = linuxWindow;
  234. updateClipBounds(mData, linuxWindow);
  235. Vector2I pos = _getCursorPosition(mData);
  236. if(clipCursor(mData, pos))
  237. _setCursorPosition(mData, pos);
  238. }
  239. void Platform::clipCursorToRect(const Rect2I& screenRect)
  240. {
  241. Lock lock(mData->lock);
  242. mData->cursorClipEnabled = true;
  243. mData->cursorClipRect = screenRect;
  244. mData->cursorClipWindow = nullptr;
  245. Vector2I pos = _getCursorPosition(mData);
  246. if(clipCursor(mData, pos))
  247. _setCursorPosition(mData, pos);
  248. }
  249. void Platform::clipCursorDisable()
  250. {
  251. Lock lock(mData->lock);
  252. mData->cursorClipEnabled = false;
  253. mData->cursorClipWindow = None;
  254. }
  255. void Platform::setCursor(PixelData& pixelData, const Vector2I& hotSpot)
  256. {
  257. SPtr<PixelData> bgraData = PixelData::create(pixelData.getWidth(), pixelData.getHeight(), 1, PF_BGRA8);
  258. PixelUtil::bulkPixelConversion(pixelData, *bgraData);
  259. Lock lock(mData->lock);
  260. XcursorImage* image = XcursorImageCreate((int)bgraData->getWidth(), (int)bgraData->getHeight());
  261. image->xhot = (XcursorDim)hotSpot.x;
  262. image->yhot = (XcursorDim)hotSpot.y;
  263. image->delay = 0;
  264. memcpy(image->pixels, bgraData->getData(), bgraData->getSize());
  265. ::Cursor cursor = XcursorImageLoadCursor(mData->xDisplay, image);
  266. XcursorImageDestroy(image);
  267. setCurrentCursor(mData, cursor);
  268. }
  269. void Platform::setIcon(const PixelData& pixelData)
  270. {
  271. SPtr<PixelData> resizedData = PixelData::create(32, 32, 1, PF_RGBA8);
  272. PixelUtil::scale(pixelData, *resizedData);
  273. if(!mData->mainXWindow)
  274. return;
  275. auto iterFind = mData->windowMap.find(mData->mainXWindow);
  276. if(iterFind == mData->windowMap.end())
  277. return;
  278. LinuxWindow* mainLinuxWindow = iterFind->second;
  279. Lock lock(mData->lock);
  280. mainLinuxWindow->setIcon(pixelData);
  281. }
  282. void Platform::setCaptionNonClientAreas(const ct::RenderWindow& window, const Vector<Rect2I>& nonClientAreas)
  283. {
  284. if(nonClientAreas.size() == 0)
  285. return;
  286. Lock lock(mData->lock);
  287. LinuxWindow* linuxWindow;
  288. window.getCustomAttribute("WINDOW", &linuxWindow);
  289. // Note: Only supporting a single area
  290. linuxWindow->_setDragZone(nonClientAreas[0]);
  291. }
  292. void Platform::setResizeNonClientAreas(const ct::RenderWindow& window, const Vector<NonClientResizeArea>& nonClientAreas)
  293. {
  294. // Do nothing, resize areas not supported on Linux (but they are provided even on undecorated windows by the WM)
  295. }
  296. void Platform::resetNonClientAreas(const ct::RenderWindow& window)
  297. {
  298. // Do nothing, resize areas not supported on Linux (but they are provided even on undecorated windows by the WM)
  299. }
  300. void Platform::sleep(UINT32 duration)
  301. {
  302. usleep(duration * 1000);
  303. }
  304. OSDropTarget& Platform::createDropTarget(const RenderWindow* window, INT32 x, INT32 y, UINT32 width, UINT32 height)
  305. {
  306. return LinuxDragAndDrop::createDropTarget(window, x, y, width, height);
  307. }
  308. void Platform::destroyDropTarget(OSDropTarget& target)
  309. {
  310. LinuxDragAndDrop::destroyDropTarget(target);
  311. }
  312. void Platform::copyToClipboard(const WString& string)
  313. {
  314. Lock lock(mData->lock);
  315. mData->clipboardData = string;
  316. Atom clipboardAtom = XInternAtom(mData->xDisplay, "CLIPBOARD", 0);
  317. XSetSelectionOwner(mData->xDisplay, clipboardAtom, mData->mainXWindow, CurrentTime);
  318. }
  319. WString Platform::copyFromClipboard()
  320. {
  321. Lock lock(mData->lock);
  322. Atom clipboardAtom = XInternAtom(mData->xDisplay, "CLIPBOARD", 0);
  323. ::Window selOwner = XGetSelectionOwner(mData->xDisplay, clipboardAtom);
  324. if(selOwner == None)
  325. return L"";
  326. if(selOwner == mData->mainXWindow)
  327. return mData->clipboardData;
  328. XConvertSelection(mData->xDisplay, clipboardAtom, XA_STRING, clipboardAtom, mData->mainXWindow,
  329. CurrentTime);
  330. XFlush(mData->xDisplay);
  331. // Note: This might discard events if there are any in between the one we need. Ideally we let the
  332. // processEvents() handle them
  333. while(true)
  334. {
  335. XEvent event;
  336. XNextEvent(mData->xDisplay, &event);
  337. if(event.type == SelectionNotify && event.xselection.requestor == mData->mainXWindow)
  338. break;
  339. }
  340. Atom actualType;
  341. INT32 actualFormat;
  342. unsigned long length;
  343. unsigned long bytesRemaining;
  344. UINT8* data;
  345. XGetWindowProperty(mData->xDisplay, mData->mainXWindow, clipboardAtom,
  346. 0, 0, False, AnyPropertyType, &actualType, &actualFormat, &length, &bytesRemaining, &data);
  347. if(bytesRemaining > 0)
  348. {
  349. unsigned long unused;
  350. INT32 result = XGetWindowProperty(mData->xDisplay, mData->mainXWindow, clipboardAtom,
  351. 0, bytesRemaining, False, AnyPropertyType, &actualType, &actualFormat, &length,
  352. &unused, &data);
  353. if(result == Success)
  354. return UTF8::toWide(String((const char*)data));
  355. XFree(data);
  356. }
  357. return L"";
  358. }
  359. WString Platform::keyCodeToUnicode(UINT32 keyCode)
  360. {
  361. Lock lock(mData->lock);
  362. // Note: Assuming the keyCode is equal to X11 KeySym. Which it is because that's how our input manager reports them.
  363. KeySym keySym = (KeySym)keyCode;
  364. XKeyPressedEvent event;
  365. bs_zero_out(event);
  366. event.keycode = XKeysymToKeycode(mData->xDisplay, keySym);
  367. event.display = mData->xDisplay;
  368. Status status;
  369. char buffer[16];
  370. INT32 length = Xutf8LookupString(mData->IC, &event, buffer, sizeof(buffer), nullptr, &status);
  371. if(length > 0)
  372. {
  373. buffer[length] = '\0';
  374. return UTF8::toWide(String(buffer));
  375. }
  376. return L"";
  377. }
  378. /**
  379. * Converts an X11 KeySym code into an input command, if possible. Returns true if conversion was done.
  380. *
  381. * @param[in] keySym KeySym to try to translate to a command.
  382. * @param[in] shift True if the shift key was held down when the key was pressed.
  383. * @param[out] command Input command. Only valid if function returns true.
  384. * @return True if the KeySym is an input command.
  385. */
  386. bool parseInputCommand(KeySym keySym, bool shift, InputCommandType& command)
  387. {
  388. switch (keySym)
  389. {
  390. case XK_Left:
  391. command = shift ? InputCommandType::SelectLeft : InputCommandType::CursorMoveLeft;
  392. return true;
  393. case XK_Right:
  394. command = shift ? InputCommandType::SelectRight : InputCommandType::CursorMoveRight;
  395. return true;
  396. case XK_Up:
  397. command = shift ? InputCommandType::SelectUp : InputCommandType::CursorMoveUp;
  398. return true;
  399. case XK_Down:
  400. command = shift ? InputCommandType::SelectDown : InputCommandType::CursorMoveDown;
  401. return true;
  402. case XK_Escape:
  403. command = InputCommandType::Escape;
  404. return true;
  405. case XK_ISO_Enter:
  406. command = shift ? InputCommandType::Return : InputCommandType::Confirm;
  407. return true;
  408. case XK_BackSpace:
  409. command = InputCommandType::Backspace;
  410. return true;
  411. case XK_Delete:
  412. command = InputCommandType::Delete;
  413. return true;
  414. }
  415. return false;
  416. }
  417. /** Returns a LinuxWindow from a native X11 window handle. */
  418. LinuxWindow* getLinuxWindow(LinuxPlatform::Pimpl* data, ::Window xWindow)
  419. {
  420. auto iterFind = data->windowMap.find(xWindow);
  421. if (iterFind != data->windowMap.end())
  422. {
  423. LinuxWindow* window = iterFind->second;
  424. return window;
  425. }
  426. return nullptr;
  427. }
  428. /** Returns a RenderWindow from a native X11 window handle. Returns null if the window isn't a RenderWindow */
  429. ct::RenderWindow* getRenderWindow(LinuxPlatform::Pimpl* data, ::Window xWindow)
  430. {
  431. LinuxWindow* linuxWindow = getLinuxWindow(data, xWindow);
  432. if(linuxWindow != nullptr)
  433. return (ct::RenderWindow*)linuxWindow->_getUserData();
  434. return nullptr;
  435. }
  436. void Platform::_messagePump()
  437. {
  438. while(true)
  439. {
  440. Lock lock(mData->lock);
  441. if(XPending(mData->xDisplay) <= 0)
  442. break;
  443. XEvent event;
  444. XNextEvent(mData->xDisplay, &event);
  445. switch (event.type)
  446. {
  447. case ClientMessage:
  448. {
  449. if(LinuxDragAndDrop::handleClientMessage(event.xclient))
  450. break;
  451. if((Atom)event.xclient.data.l[0] == mData->atomDeleteWindow)
  452. XDestroyWindow(mData->xDisplay, event.xclient.window);
  453. }
  454. break;
  455. case DestroyNotify:
  456. {
  457. LinuxWindow* window = getLinuxWindow(mData, event.xdestroywindow.window);
  458. if(window != nullptr)
  459. {
  460. CoreApplication::instance().quitRequested();
  461. window->_cleanUp();
  462. if (mData->mainXWindow == 0)
  463. return;
  464. }
  465. }
  466. break;
  467. case KeyPress:
  468. {
  469. // Process text input
  470. //// Check if input manager wants this event. If not, we process it.
  471. if(!XFilterEvent(&event, None))
  472. {
  473. Status status;
  474. char buffer[16];
  475. INT32 length = Xutf8LookupString(mData->IC, &event.xkey, buffer, sizeof(buffer), nullptr,
  476. &status);
  477. if(length > 0)
  478. {
  479. buffer[length] = '\0';
  480. U32String utfStr = UTF8::toUTF32(String(buffer));
  481. if(utfStr.length() > 0)
  482. onCharInput((UINT32)utfStr[0]);
  483. }
  484. }
  485. // Handle input commands
  486. InputCommandType command = InputCommandType::Backspace;
  487. KeySym keySym = XkbKeycodeToKeysym(mData->xDisplay, (KeyCode)event.xkey.keycode, 0, 0);
  488. bool shift = (event.xkey.state & ShiftMask) != 0;
  489. if(parseInputCommand(keySym, shift, command))
  490. {
  491. if(!onInputCommand.empty())
  492. onInputCommand(command);
  493. }
  494. }
  495. break;
  496. case KeyRelease:
  497. // Do nothing
  498. break;
  499. case ButtonPress:
  500. {
  501. UINT32 button = event.xbutton.button;
  502. OSMouseButton mouseButton;
  503. bool validPress = false;
  504. switch(button)
  505. {
  506. case Button1:
  507. mouseButton = OSMouseButton::Left;
  508. validPress = true;
  509. break;
  510. case Button2:
  511. mouseButton = OSMouseButton::Middle;
  512. validPress = true;
  513. break;
  514. case Button3:
  515. mouseButton = OSMouseButton::Right;
  516. validPress = true;
  517. break;
  518. default:
  519. break;
  520. }
  521. if(validPress)
  522. {
  523. // Send event
  524. Vector2I pos;
  525. pos.x = event.xbutton.x_root;
  526. pos.y = event.xbutton.y_root;
  527. OSPointerButtonStates btnStates;
  528. btnStates.ctrl = (event.xbutton.state & ControlMask) != 0;
  529. btnStates.shift = (event.xbutton.state & ShiftMask) != 0;
  530. btnStates.mouseButtons[0] = (event.xbutton.state & Button1Mask) != 0;
  531. btnStates.mouseButtons[1] = (event.xbutton.state & Button2Mask) != 0;
  532. btnStates.mouseButtons[2] = (event.xbutton.state & Button3Mask) != 0;
  533. onCursorButtonPressed(pos, mouseButton, btnStates);
  534. // Handle double-click
  535. if(button == Button1)
  536. {
  537. if (event.xbutton.time < (mData->lastButtonPressTime + DOUBLE_CLICK_MS))
  538. {
  539. onCursorDoubleClick(pos, btnStates);
  540. mData->lastButtonPressTime = 0;
  541. }
  542. else
  543. mData->lastButtonPressTime = event.xbutton.time;
  544. }
  545. }
  546. // Handle window dragging for windows without a title bar
  547. if(button == Button1)
  548. {
  549. LinuxWindow* window = getLinuxWindow(mData, event.xbutton.window);
  550. if(window != nullptr)
  551. window->_dragStart(event.xbutton.x, event.xbutton.y);
  552. }
  553. break;
  554. }
  555. case ButtonRelease:
  556. {
  557. UINT32 button = event.xbutton.button;
  558. Vector2I pos;
  559. pos.x = event.xbutton.x_root;
  560. pos.y = event.xbutton.y_root;
  561. OSPointerButtonStates btnStates;
  562. btnStates.ctrl = (event.xbutton.state & ControlMask) != 0;
  563. btnStates.shift = (event.xbutton.state & ShiftMask) != 0;
  564. btnStates.mouseButtons[0] = (event.xbutton.state & Button1Mask) != 0;
  565. btnStates.mouseButtons[1] = (event.xbutton.state & Button2Mask) != 0;
  566. btnStates.mouseButtons[2] = (event.xbutton.state & Button3Mask) != 0;
  567. switch(button)
  568. {
  569. case Button1:
  570. onCursorButtonReleased(pos, OSMouseButton::Left, btnStates);
  571. break;
  572. case Button2:
  573. onCursorButtonReleased(pos, OSMouseButton::Middle, btnStates);
  574. break;
  575. case Button3:
  576. onCursorButtonReleased(pos, OSMouseButton::Right, btnStates);
  577. break;
  578. case Button4: // Vertical mouse wheel
  579. case Button5:
  580. {
  581. INT32 delta = button == Button4 ? 1 : -1;
  582. onMouseWheelScrolled((float)delta);
  583. }
  584. break;
  585. default:
  586. break;
  587. }
  588. // Handle window dragging for windows without a title bar
  589. if(button == Button1)
  590. {
  591. LinuxWindow* window = getLinuxWindow(mData, event.xbutton.window);
  592. if(window != nullptr)
  593. window->_dragEnd();
  594. }
  595. break;
  596. }
  597. case MotionNotify:
  598. {
  599. Vector2I pos;
  600. pos.x = event.xmotion.x_root;
  601. pos.y = event.xmotion.y_root;
  602. // Handle clipping if enabled
  603. if(clipCursor(mData, pos))
  604. _setCursorPosition(mData, pos);
  605. // Send event
  606. OSPointerButtonStates btnStates;
  607. btnStates.ctrl = (event.xmotion.state & ControlMask) != 0;
  608. btnStates.shift = (event.xmotion.state & ShiftMask) != 0;
  609. btnStates.mouseButtons[0] = (event.xmotion.state & Button1Mask) != 0;
  610. btnStates.mouseButtons[1] = (event.xmotion.state & Button2Mask) != 0;
  611. btnStates.mouseButtons[2] = (event.xmotion.state & Button3Mask) != 0;
  612. onCursorMoved(pos, btnStates);
  613. // Handle window dragging for windows without a title bar
  614. LinuxWindow* window = getLinuxWindow(mData, event.xmotion.window);
  615. if(window != nullptr)
  616. window->_dragUpdate(event.xmotion.x, event.xmotion.y);
  617. }
  618. break;
  619. case EnterNotify:
  620. // Do nothing
  621. break;
  622. case LeaveNotify:
  623. {
  624. if (event.xcrossing.mode == NotifyNormal)
  625. {
  626. Vector2I pos;
  627. pos.x = event.xcrossing.x_root;
  628. pos.y = event.xcrossing.y_root;
  629. if (clipCursor(mData, pos))
  630. _setCursorPosition(mData, pos);
  631. }
  632. ct::RenderWindow* renderWindow = getRenderWindow(mData, event.xcrossing.window);
  633. if(renderWindow != nullptr)
  634. renderWindow->_notifyMouseLeft();
  635. }
  636. break;
  637. case ConfigureNotify:
  638. {
  639. LinuxWindow* window = getLinuxWindow(mData, event.xconfigure.window);
  640. if(window != nullptr)
  641. {
  642. updateClipBounds(mData, window);
  643. ct::RenderWindow* renderWindow = (ct::RenderWindow*)window->_getUserData();
  644. if(renderWindow != nullptr)
  645. renderWindow->_windowMovedOrResized();
  646. }
  647. }
  648. break;
  649. case FocusIn:
  650. {
  651. // Update input context focus
  652. XSetICFocus(mData->IC);
  653. // Send event to render window
  654. ct::RenderWindow* renderWindow = getRenderWindow(mData, event.xfocus.window);
  655. // Not a render window, so it doesn't care about these events
  656. if (renderWindow != nullptr)
  657. {
  658. if (!renderWindow->getProperties().hasFocus)
  659. renderWindow->_windowFocusReceived();
  660. }
  661. }
  662. break;
  663. case FocusOut:
  664. {
  665. // Update input context focus
  666. XUnsetICFocus(mData->IC);
  667. // Send event to render window
  668. ct::RenderWindow* renderWindow = getRenderWindow(mData, event.xfocus.window);
  669. // Not a render window, so it doesn't care about these events
  670. if (renderWindow != nullptr)
  671. {
  672. if (renderWindow->getProperties().hasFocus)
  673. renderWindow->_windowFocusLost();
  674. }
  675. }
  676. break;
  677. case SelectionNotify:
  678. LinuxDragAndDrop::handleSelectionNotify(event.xselection);
  679. break;
  680. case SelectionRequest:
  681. {
  682. // Send the data saved by the last clipboard copy operation
  683. Atom compoundTextAtom = XInternAtom(mData->xDisplay, "COMPOUND_TEXT", 0);
  684. Atom utf8StringAtom = XInternAtom(mData->xDisplay, "UTF8_STRING", 0);
  685. Atom targetsAtom = XInternAtom(mData->xDisplay, "TARGETS", 0);
  686. XSelectionRequestEvent& selReq = event.xselectionrequest;
  687. XEvent response;
  688. if(selReq.target == XA_STRING || selReq.target == compoundTextAtom || selReq.target == utf8StringAtom)
  689. {
  690. String utf8data = UTF8::fromWide(mData->clipboardData);
  691. const UINT8* data = (const UINT8*)utf8data.c_str();
  692. INT32 dataLength = (INT32)utf8data.length();
  693. XChangeProperty(mData->xDisplay, selReq.requestor, selReq.property,
  694. selReq.target, 8, PropModeReplace, data, dataLength);
  695. response.xselection.property = selReq.property;
  696. }
  697. else if(selReq.target == targetsAtom)
  698. {
  699. Atom data[2];
  700. data[0] = utf8StringAtom;
  701. data[1] = XA_STRING;
  702. XChangeProperty (mData->xDisplay, selReq.requestor, selReq.property, selReq.target,
  703. 8, PropModeReplace, (unsigned char*)&data, sizeof (data));
  704. response.xselection.property = selReq.property;
  705. }
  706. else
  707. {
  708. response.xselection.property = None;
  709. }
  710. response.xselection.type = SelectionNotify;
  711. response.xselection.display = selReq.display;
  712. response.xselection.requestor = selReq.requestor;
  713. response.xselection.selection = selReq.selection;
  714. response.xselection.target = selReq.target;
  715. response.xselection.time = selReq.time;
  716. XSendEvent (mData->xDisplay, selReq.requestor, 0, 0, &response);
  717. XFlush (mData->xDisplay);
  718. }
  719. break;
  720. case PropertyNotify:
  721. // Report minimize, maximize and restore events
  722. if(event.xproperty.atom == mData->atomWmState)
  723. {
  724. Atom type;
  725. INT32 format;
  726. unsigned long count, bytesRemaining;
  727. UINT8* data = nullptr;
  728. INT32 result = XGetWindowProperty(mData->xDisplay, event.xproperty.window, mData->atomWmState,
  729. 0, 1024, False, AnyPropertyType, &type, &format,
  730. &count, &bytesRemaining, &data);
  731. if (result == Success)
  732. {
  733. ct::RenderWindow* renderWindow = getRenderWindow(mData, event.xproperty.window);
  734. // Not a render window, so it doesn't care about these events
  735. if(renderWindow == nullptr)
  736. continue;
  737. Atom* atoms = (Atom*)data;
  738. bool foundHorz = false;
  739. bool foundVert = false;
  740. for (unsigned long i = 0; i < count; i++)
  741. {
  742. if (atoms[i] == mData->atomWmStateMaxHorz) foundHorz = true;
  743. if (atoms[i] == mData->atomWmStateMaxVert) foundVert = true;
  744. if (foundVert && foundHorz)
  745. {
  746. if(event.xproperty.state == PropertyNewValue)
  747. renderWindow->_notifyMaximized();
  748. else
  749. renderWindow->_notifyRestored();
  750. }
  751. if(atoms[i] == mData->atomWmStateHidden)
  752. {
  753. if(event.xproperty.state == PropertyNewValue)
  754. renderWindow->_notifyMinimized();
  755. else
  756. renderWindow->_notifyRestored();
  757. }
  758. }
  759. XFree(atoms);
  760. }
  761. }
  762. break;
  763. default:
  764. break;
  765. }
  766. }
  767. }
  768. void Platform::_startUp()
  769. {
  770. Lock lock(mData->lock);
  771. mData->xDisplay = XOpenDisplay(nullptr);
  772. if(XSupportsLocale())
  773. {
  774. XSetLocaleModifiers("");
  775. mData->IM = XOpenIM(mData->xDisplay, nullptr, nullptr, nullptr);
  776. // Note: Currently our windows don't support pre-edit and status areas, which are used for more complex types
  777. // of character input. Later on it might be beneficial to support them.
  778. mData->IC = XCreateIC(mData->IM, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, nullptr);
  779. }
  780. mData->atomDeleteWindow = XInternAtom(mData->xDisplay, "WM_DELETE_WINDOW", False);
  781. mData->atomWmState = XInternAtom(mData->xDisplay, "_NET_WM_STATE", False);
  782. mData->atomWmStateHidden = XInternAtom(mData->xDisplay, "_NET_WM_STATE_HIDDEN", False);
  783. mData->atomWmStateMaxHorz = XInternAtom(mData->xDisplay, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
  784. mData->atomWmStateMaxVert = XInternAtom(mData->xDisplay, "_NET_WM_STATE_MAXIMIZED_VERT", False);
  785. // Drag and drop
  786. LinuxDragAndDrop::startUp(mData->xDisplay);
  787. // Create empty cursor
  788. char data[1];
  789. memset(data, 0, sizeof(data));
  790. Pixmap pixmap = XCreateBitmapFromData(mData->xDisplay, DefaultRootWindow(mData->xDisplay), data, 1, 1);
  791. XColor color;
  792. color.red = color.green = color.blue = 0;
  793. mData->emptyCursor = XCreatePixmapCursor(mData->xDisplay, pixmap, pixmap, &color, &color, 0, 0);
  794. XFreePixmap(mData->xDisplay, pixmap);
  795. }
  796. void Platform::_update()
  797. {
  798. LinuxDragAndDrop::update();
  799. }
  800. void Platform::_coreUpdate()
  801. {
  802. _messagePump();
  803. }
  804. void Platform::_shutDown()
  805. {
  806. Lock lock(mData->lock);
  807. // Free empty cursor
  808. XFreeCursor(mData->xDisplay, mData->emptyCursor);
  809. mData->emptyCursor = None;
  810. // Shutdown drag and drop
  811. LinuxDragAndDrop::shutDown();
  812. if(mData->IC)
  813. {
  814. XDestroyIC(mData->IC);
  815. mData->IC = 0;
  816. }
  817. if(mData->IM)
  818. {
  819. XCloseIM(mData->IM);
  820. mData->IM = 0;
  821. }
  822. XCloseDisplay(mData->xDisplay);
  823. mData->xDisplay = nullptr;
  824. bs_delete(mData);
  825. mData = nullptr;
  826. }
  827. ::Display* LinuxPlatform::getXDisplay()
  828. {
  829. return mData->xDisplay;
  830. }
  831. ::Window LinuxPlatform::getMainXWindow()
  832. {
  833. return mData->mainXWindow;
  834. }
  835. void LinuxPlatform::lockX()
  836. {
  837. mData->lock.lock();
  838. }
  839. void LinuxPlatform::unlockX()
  840. {
  841. mData->lock.unlock();
  842. }
  843. void LinuxPlatform::_registerWindow(::Window xWindow, LinuxWindow* window)
  844. {
  845. // First window is assumed to be the main
  846. if(mData->mainXWindow == 0)
  847. {
  848. mData->mainXWindow = xWindow;
  849. // Input context client window must be set before use
  850. XSetICValues(mData->IC,
  851. XNClientWindow, xWindow,
  852. XNFocusWindow, xWindow,
  853. nullptr);
  854. }
  855. mData->windowMap[xWindow] = window;
  856. applyCurrentCursor(mData, xWindow);
  857. }
  858. void LinuxPlatform::_unregisterWindow(::Window xWindow)
  859. {
  860. auto iterFind = mData->windowMap.find(xWindow);
  861. if(iterFind != mData->windowMap.end())
  862. {
  863. if(mData->cursorClipEnabled && mData->cursorClipWindow == iterFind->second)
  864. clipCursorDisable();
  865. mData->windowMap.erase(iterFind);
  866. }
  867. if(mData->mainXWindow == xWindow)
  868. mData->mainXWindow = 0;
  869. }
  870. Pixmap LinuxPlatform::createPixmap(const PixelData& data)
  871. {
  872. SPtr<PixelData> bgraData = PixelData::create(data.getWidth(), data.getHeight(), 1, PF_BGRA8);
  873. PixelUtil::bulkPixelConversion(data, *bgraData);
  874. UINT32 depth = (UINT32)XDefaultDepth(mData->xDisplay, 0);
  875. XImage* image = XCreateImage(mData->xDisplay, XDefaultVisual(mData->xDisplay, 0), depth, ZPixmap, 0,
  876. (char*)bgraData->getData(), data.getWidth(), data.getHeight(), 32, 0);
  877. Pixmap pixmap = XCreatePixmap(mData->xDisplay, XDefaultRootWindow(mData->xDisplay),
  878. data.getWidth(), data.getHeight(), depth);
  879. XGCValues gcValues;
  880. GC gc = XCreateGC(mData->xDisplay, pixmap, 0, &gcValues);
  881. XPutImage(mData->xDisplay, pixmap, gc, image, 0, 0, 0, 0, data.getWidth(), data.getHeight());
  882. XFreeGC(mData->xDisplay, gc);
  883. // Make sure XDestroyImage doesn't free the data pointed to by 'data.bytes'
  884. image->data = nullptr;
  885. XDestroyImage(image);
  886. return pixmap;
  887. }
  888. }