BsLinuxPlatform.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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 "BsLinuxDropTarget.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. void Platform::copyToClipboard(const WString& string)
  305. {
  306. Lock lock(mData->lock);
  307. mData->clipboardData = string;
  308. Atom clipboardAtom = XInternAtom(mData->xDisplay, "CLIPBOARD", 0);
  309. XSetSelectionOwner(mData->xDisplay, clipboardAtom, mData->mainXWindow, CurrentTime);
  310. }
  311. WString Platform::copyFromClipboard()
  312. {
  313. Lock lock(mData->lock);
  314. Atom clipboardAtom = XInternAtom(mData->xDisplay, "CLIPBOARD", 0);
  315. ::Window selOwner = XGetSelectionOwner(mData->xDisplay, clipboardAtom);
  316. if(selOwner == None)
  317. return L"";
  318. if(selOwner == mData->mainXWindow)
  319. return mData->clipboardData;
  320. XConvertSelection(mData->xDisplay, clipboardAtom, XA_STRING, clipboardAtom, mData->mainXWindow,
  321. CurrentTime);
  322. XFlush(mData->xDisplay);
  323. // Note: This might discard events if there are any in between the one we need. Ideally we let the
  324. // processEvents() handle them
  325. while(true)
  326. {
  327. XEvent event;
  328. XNextEvent(mData->xDisplay, &event);
  329. if(event.type == SelectionNotify && event.xselection.requestor == mData->mainXWindow)
  330. break;
  331. }
  332. Atom actualType;
  333. INT32 actualFormat;
  334. unsigned long length;
  335. unsigned long bytesRemaining;
  336. UINT8* data;
  337. XGetWindowProperty(mData->xDisplay, mData->mainXWindow, clipboardAtom,
  338. 0, 0, False, AnyPropertyType, &actualType, &actualFormat, &length, &bytesRemaining, &data);
  339. if(bytesRemaining > 0)
  340. {
  341. unsigned long unused;
  342. INT32 result = XGetWindowProperty(mData->xDisplay, mData->mainXWindow, clipboardAtom,
  343. 0, bytesRemaining, False, AnyPropertyType, &actualType, &actualFormat, &length,
  344. &unused, &data);
  345. if(result == Success)
  346. return UTF8::toWide(String((const char*)data));
  347. XFree(data);
  348. }
  349. return L"";
  350. }
  351. WString Platform::keyCodeToUnicode(UINT32 keyCode)
  352. {
  353. Lock lock(mData->lock);
  354. // Note: Assuming the keyCode is equal to X11 KeySym. Which it is because that's how our input manager reports them.
  355. KeySym keySym = (KeySym)keyCode;
  356. XKeyPressedEvent event;
  357. bs_zero_out(event);
  358. event.keycode = XKeysymToKeycode(mData->xDisplay, keySym);
  359. event.display = mData->xDisplay;
  360. Status status;
  361. char buffer[16];
  362. INT32 length = Xutf8LookupString(mData->IC, &event, buffer, sizeof(buffer), nullptr, &status);
  363. if(length > 0)
  364. {
  365. buffer[length] = '\0';
  366. return UTF8::toWide(String(buffer));
  367. }
  368. return L"";
  369. }
  370. void Platform::openFolder(const Path& path)
  371. {
  372. String pathString = path.toString();
  373. const char* commandPattern = "xdg-open '%s'";
  374. char* commandStr = (char*)bs_stack_alloc((UINT32)pathString.size() + (UINT32)strlen(commandPattern) + 1);
  375. sprintf(commandStr, commandPattern, pathString.c_str());
  376. system(commandStr);
  377. bs_stack_free(commandStr);
  378. }
  379. /**
  380. * Converts an X11 KeySym code into an input command, if possible. Returns true if conversion was done.
  381. *
  382. * @param[in] keySym KeySym to try to translate to a command.
  383. * @param[in] shift True if the shift key was held down when the key was pressed.
  384. * @param[out] command Input command. Only valid if function returns true.
  385. * @return True if the KeySym is an input command.
  386. */
  387. bool parseInputCommand(KeySym keySym, bool shift, InputCommandType& command)
  388. {
  389. switch (keySym)
  390. {
  391. case XK_Left:
  392. command = shift ? InputCommandType::SelectLeft : InputCommandType::CursorMoveLeft;
  393. return true;
  394. case XK_Right:
  395. command = shift ? InputCommandType::SelectRight : InputCommandType::CursorMoveRight;
  396. return true;
  397. case XK_Up:
  398. command = shift ? InputCommandType::SelectUp : InputCommandType::CursorMoveUp;
  399. return true;
  400. case XK_Down:
  401. command = shift ? InputCommandType::SelectDown : InputCommandType::CursorMoveDown;
  402. return true;
  403. case XK_Escape:
  404. command = InputCommandType::Escape;
  405. return true;
  406. case XK_ISO_Enter:
  407. command = shift ? InputCommandType::Return : InputCommandType::Confirm;
  408. return true;
  409. case XK_BackSpace:
  410. command = InputCommandType::Backspace;
  411. return true;
  412. case XK_Delete:
  413. command = InputCommandType::Delete;
  414. return true;
  415. }
  416. return false;
  417. }
  418. /** Returns a LinuxWindow from a native X11 window handle. */
  419. LinuxWindow* getLinuxWindow(LinuxPlatform::Pimpl* data, ::Window xWindow)
  420. {
  421. auto iterFind = data->windowMap.find(xWindow);
  422. if (iterFind != data->windowMap.end())
  423. {
  424. LinuxWindow* window = iterFind->second;
  425. return window;
  426. }
  427. return nullptr;
  428. }
  429. /** Returns a RenderWindow from a native X11 window handle. Returns null if the window isn't a RenderWindow */
  430. ct::RenderWindow* getRenderWindow(LinuxPlatform::Pimpl* data, ::Window xWindow)
  431. {
  432. LinuxWindow* linuxWindow = getLinuxWindow(data, xWindow);
  433. if(linuxWindow != nullptr)
  434. return (ct::RenderWindow*)linuxWindow->_getUserData();
  435. return nullptr;
  436. }
  437. void Platform::_messagePump()
  438. {
  439. while(true)
  440. {
  441. Lock lock(mData->lock);
  442. if(XPending(mData->xDisplay) <= 0)
  443. break;
  444. XEvent event;
  445. XNextEvent(mData->xDisplay, &event);
  446. switch (event.type)
  447. {
  448. case ClientMessage:
  449. {
  450. if(LinuxDragAndDrop::handleClientMessage(event.xclient))
  451. break;
  452. if((Atom)event.xclient.data.l[0] == mData->atomDeleteWindow)
  453. XDestroyWindow(mData->xDisplay, event.xclient.window);
  454. }
  455. break;
  456. case DestroyNotify:
  457. {
  458. LinuxWindow* window = getLinuxWindow(mData, event.xdestroywindow.window);
  459. if(window != nullptr)
  460. {
  461. CoreApplication::instance().quitRequested();
  462. window->_cleanUp();
  463. if (mData->mainXWindow == 0)
  464. return;
  465. }
  466. }
  467. break;
  468. case KeyPress:
  469. {
  470. // Process text input
  471. //// Check if input manager wants this event. If not, we process it.
  472. if(!XFilterEvent(&event, None))
  473. {
  474. Status status;
  475. char buffer[16];
  476. INT32 length = Xutf8LookupString(mData->IC, &event.xkey, buffer, sizeof(buffer), nullptr,
  477. &status);
  478. if(length > 0)
  479. {
  480. buffer[length] = '\0';
  481. U32String utfStr = UTF8::toUTF32(String(buffer));
  482. if(utfStr.length() > 0)
  483. onCharInput((UINT32)utfStr[0]);
  484. }
  485. }
  486. // Handle input commands
  487. InputCommandType command = InputCommandType::Backspace;
  488. KeySym keySym = XkbKeycodeToKeysym(mData->xDisplay, (KeyCode)event.xkey.keycode, 0, 0);
  489. bool shift = (event.xkey.state & ShiftMask) != 0;
  490. if(parseInputCommand(keySym, shift, command))
  491. {
  492. if(!onInputCommand.empty())
  493. onInputCommand(command);
  494. }
  495. }
  496. break;
  497. case KeyRelease:
  498. // Do nothing
  499. break;
  500. case ButtonPress:
  501. {
  502. UINT32 button = event.xbutton.button;
  503. OSMouseButton mouseButton;
  504. bool validPress = false;
  505. switch(button)
  506. {
  507. case Button1:
  508. mouseButton = OSMouseButton::Left;
  509. validPress = true;
  510. break;
  511. case Button2:
  512. mouseButton = OSMouseButton::Middle;
  513. validPress = true;
  514. break;
  515. case Button3:
  516. mouseButton = OSMouseButton::Right;
  517. validPress = true;
  518. break;
  519. default:
  520. break;
  521. }
  522. if(validPress)
  523. {
  524. // Send event
  525. Vector2I pos;
  526. pos.x = event.xbutton.x_root;
  527. pos.y = event.xbutton.y_root;
  528. OSPointerButtonStates btnStates;
  529. btnStates.ctrl = (event.xbutton.state & ControlMask) != 0;
  530. btnStates.shift = (event.xbutton.state & ShiftMask) != 0;
  531. btnStates.mouseButtons[0] = (event.xbutton.state & Button1Mask) != 0;
  532. btnStates.mouseButtons[1] = (event.xbutton.state & Button2Mask) != 0;
  533. btnStates.mouseButtons[2] = (event.xbutton.state & Button3Mask) != 0;
  534. onCursorButtonPressed(pos, mouseButton, btnStates);
  535. // Handle double-click
  536. if(button == Button1)
  537. {
  538. if (event.xbutton.time < (mData->lastButtonPressTime + DOUBLE_CLICK_MS))
  539. {
  540. onCursorDoubleClick(pos, btnStates);
  541. mData->lastButtonPressTime = 0;
  542. }
  543. else
  544. mData->lastButtonPressTime = event.xbutton.time;
  545. }
  546. }
  547. // Handle window dragging for windows without a title bar
  548. if(button == Button1)
  549. {
  550. LinuxWindow* window = getLinuxWindow(mData, event.xbutton.window);
  551. if(window != nullptr)
  552. window->_dragStart(event.xbutton.x, event.xbutton.y);
  553. }
  554. break;
  555. }
  556. case ButtonRelease:
  557. {
  558. UINT32 button = event.xbutton.button;
  559. Vector2I pos;
  560. pos.x = event.xbutton.x_root;
  561. pos.y = event.xbutton.y_root;
  562. OSPointerButtonStates btnStates;
  563. btnStates.ctrl = (event.xbutton.state & ControlMask) != 0;
  564. btnStates.shift = (event.xbutton.state & ShiftMask) != 0;
  565. btnStates.mouseButtons[0] = (event.xbutton.state & Button1Mask) != 0;
  566. btnStates.mouseButtons[1] = (event.xbutton.state & Button2Mask) != 0;
  567. btnStates.mouseButtons[2] = (event.xbutton.state & Button3Mask) != 0;
  568. switch(button)
  569. {
  570. case Button1:
  571. onCursorButtonReleased(pos, OSMouseButton::Left, btnStates);
  572. break;
  573. case Button2:
  574. onCursorButtonReleased(pos, OSMouseButton::Middle, btnStates);
  575. break;
  576. case Button3:
  577. onCursorButtonReleased(pos, OSMouseButton::Right, btnStates);
  578. break;
  579. case Button4: // Vertical mouse wheel
  580. case Button5:
  581. {
  582. INT32 delta = button == Button4 ? 1 : -1;
  583. onMouseWheelScrolled((float)delta);
  584. }
  585. break;
  586. default:
  587. break;
  588. }
  589. // Handle window dragging for windows without a title bar
  590. if(button == Button1)
  591. {
  592. LinuxWindow* window = getLinuxWindow(mData, event.xbutton.window);
  593. if(window != nullptr)
  594. window->_dragEnd();
  595. }
  596. break;
  597. }
  598. case MotionNotify:
  599. {
  600. Vector2I pos;
  601. pos.x = event.xmotion.x_root;
  602. pos.y = event.xmotion.y_root;
  603. // Handle clipping if enabled
  604. if(clipCursor(mData, pos))
  605. _setCursorPosition(mData, pos);
  606. // Send event
  607. OSPointerButtonStates btnStates;
  608. btnStates.ctrl = (event.xmotion.state & ControlMask) != 0;
  609. btnStates.shift = (event.xmotion.state & ShiftMask) != 0;
  610. btnStates.mouseButtons[0] = (event.xmotion.state & Button1Mask) != 0;
  611. btnStates.mouseButtons[1] = (event.xmotion.state & Button2Mask) != 0;
  612. btnStates.mouseButtons[2] = (event.xmotion.state & Button3Mask) != 0;
  613. onCursorMoved(pos, btnStates);
  614. // Handle window dragging for windows without a title bar
  615. LinuxWindow* window = getLinuxWindow(mData, event.xmotion.window);
  616. if(window != nullptr)
  617. window->_dragUpdate(event.xmotion.x, event.xmotion.y);
  618. }
  619. break;
  620. case EnterNotify:
  621. // Do nothing
  622. break;
  623. case LeaveNotify:
  624. {
  625. if (event.xcrossing.mode == NotifyNormal)
  626. {
  627. Vector2I pos;
  628. pos.x = event.xcrossing.x_root;
  629. pos.y = event.xcrossing.y_root;
  630. if (clipCursor(mData, pos))
  631. _setCursorPosition(mData, pos);
  632. }
  633. ct::RenderWindow* renderWindow = getRenderWindow(mData, event.xcrossing.window);
  634. if(renderWindow != nullptr)
  635. renderWindow->_notifyMouseLeft();
  636. }
  637. break;
  638. case ConfigureNotify:
  639. {
  640. LinuxWindow* window = getLinuxWindow(mData, event.xconfigure.window);
  641. if(window != nullptr)
  642. {
  643. updateClipBounds(mData, window);
  644. ct::RenderWindow* renderWindow = (ct::RenderWindow*)window->_getUserData();
  645. if(renderWindow != nullptr)
  646. renderWindow->_windowMovedOrResized();
  647. }
  648. }
  649. break;
  650. case FocusIn:
  651. {
  652. // Update input context focus
  653. XSetICFocus(mData->IC);
  654. // Send event to render window
  655. ct::RenderWindow* renderWindow = getRenderWindow(mData, event.xfocus.window);
  656. // Not a render window, so it doesn't care about these events
  657. if (renderWindow != nullptr)
  658. {
  659. if (!renderWindow->getProperties().hasFocus)
  660. renderWindow->_windowFocusReceived();
  661. }
  662. }
  663. break;
  664. case FocusOut:
  665. {
  666. // Update input context focus
  667. XUnsetICFocus(mData->IC);
  668. // Send event to render window
  669. ct::RenderWindow* renderWindow = getRenderWindow(mData, event.xfocus.window);
  670. // Not a render window, so it doesn't care about these events
  671. if (renderWindow != nullptr)
  672. {
  673. if (renderWindow->getProperties().hasFocus)
  674. renderWindow->_windowFocusLost();
  675. }
  676. }
  677. break;
  678. case SelectionNotify:
  679. LinuxDragAndDrop::handleSelectionNotify(event.xselection);
  680. break;
  681. case SelectionRequest:
  682. {
  683. // Send the data saved by the last clipboard copy operation
  684. Atom compoundTextAtom = XInternAtom(mData->xDisplay, "COMPOUND_TEXT", 0);
  685. Atom utf8StringAtom = XInternAtom(mData->xDisplay, "UTF8_STRING", 0);
  686. Atom targetsAtom = XInternAtom(mData->xDisplay, "TARGETS", 0);
  687. XSelectionRequestEvent& selReq = event.xselectionrequest;
  688. XEvent response;
  689. if(selReq.target == XA_STRING || selReq.target == compoundTextAtom || selReq.target == utf8StringAtom)
  690. {
  691. String utf8data = UTF8::fromWide(mData->clipboardData);
  692. const UINT8* data = (const UINT8*)utf8data.c_str();
  693. INT32 dataLength = (INT32)utf8data.length();
  694. XChangeProperty(mData->xDisplay, selReq.requestor, selReq.property,
  695. selReq.target, 8, PropModeReplace, data, dataLength);
  696. response.xselection.property = selReq.property;
  697. }
  698. else if(selReq.target == targetsAtom)
  699. {
  700. Atom data[2];
  701. data[0] = utf8StringAtom;
  702. data[1] = XA_STRING;
  703. XChangeProperty (mData->xDisplay, selReq.requestor, selReq.property, selReq.target,
  704. 8, PropModeReplace, (unsigned char*)&data, sizeof (data));
  705. response.xselection.property = selReq.property;
  706. }
  707. else
  708. {
  709. response.xselection.property = None;
  710. }
  711. response.xselection.type = SelectionNotify;
  712. response.xselection.display = selReq.display;
  713. response.xselection.requestor = selReq.requestor;
  714. response.xselection.selection = selReq.selection;
  715. response.xselection.target = selReq.target;
  716. response.xselection.time = selReq.time;
  717. XSendEvent (mData->xDisplay, selReq.requestor, 0, 0, &response);
  718. XFlush (mData->xDisplay);
  719. }
  720. break;
  721. case PropertyNotify:
  722. // Report minimize, maximize and restore events
  723. if(event.xproperty.atom == mData->atomWmState)
  724. {
  725. Atom type;
  726. INT32 format;
  727. unsigned long count, bytesRemaining;
  728. UINT8* data = nullptr;
  729. INT32 result = XGetWindowProperty(mData->xDisplay, event.xproperty.window, mData->atomWmState,
  730. 0, 1024, False, AnyPropertyType, &type, &format,
  731. &count, &bytesRemaining, &data);
  732. if (result == Success)
  733. {
  734. ct::RenderWindow* renderWindow = getRenderWindow(mData, event.xproperty.window);
  735. // Not a render window, so it doesn't care about these events
  736. if(renderWindow == nullptr)
  737. continue;
  738. Atom* atoms = (Atom*)data;
  739. bool foundHorz = false;
  740. bool foundVert = false;
  741. for (unsigned long i = 0; i < count; i++)
  742. {
  743. if (atoms[i] == mData->atomWmStateMaxHorz) foundHorz = true;
  744. if (atoms[i] == mData->atomWmStateMaxVert) foundVert = true;
  745. if (foundVert && foundHorz)
  746. {
  747. if(event.xproperty.state == PropertyNewValue)
  748. renderWindow->_notifyMaximized();
  749. else
  750. renderWindow->_notifyRestored();
  751. }
  752. if(atoms[i] == mData->atomWmStateHidden)
  753. {
  754. if(event.xproperty.state == PropertyNewValue)
  755. renderWindow->_notifyMinimized();
  756. else
  757. renderWindow->_notifyRestored();
  758. }
  759. }
  760. XFree(atoms);
  761. }
  762. }
  763. break;
  764. default:
  765. break;
  766. }
  767. }
  768. }
  769. void Platform::_startUp()
  770. {
  771. Lock lock(mData->lock);
  772. mData->xDisplay = XOpenDisplay(nullptr);
  773. if(XSupportsLocale())
  774. {
  775. XSetLocaleModifiers("");
  776. mData->IM = XOpenIM(mData->xDisplay, nullptr, nullptr, nullptr);
  777. // Note: Currently our windows don't support pre-edit and status areas, which are used for more complex types
  778. // of character input. Later on it might be beneficial to support them.
  779. mData->IC = XCreateIC(mData->IM, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, nullptr);
  780. }
  781. mData->atomDeleteWindow = XInternAtom(mData->xDisplay, "WM_DELETE_WINDOW", False);
  782. mData->atomWmState = XInternAtom(mData->xDisplay, "_NET_WM_STATE", False);
  783. mData->atomWmStateHidden = XInternAtom(mData->xDisplay, "_NET_WM_STATE_HIDDEN", False);
  784. mData->atomWmStateMaxHorz = XInternAtom(mData->xDisplay, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
  785. mData->atomWmStateMaxVert = XInternAtom(mData->xDisplay, "_NET_WM_STATE_MAXIMIZED_VERT", False);
  786. // Drag and drop
  787. LinuxDragAndDrop::startUp(mData->xDisplay);
  788. // Create empty cursor
  789. char data[1];
  790. memset(data, 0, sizeof(data));
  791. Pixmap pixmap = XCreateBitmapFromData(mData->xDisplay, DefaultRootWindow(mData->xDisplay), data, 1, 1);
  792. XColor color;
  793. color.red = color.green = color.blue = 0;
  794. mData->emptyCursor = XCreatePixmapCursor(mData->xDisplay, pixmap, pixmap, &color, &color, 0, 0);
  795. XFreePixmap(mData->xDisplay, pixmap);
  796. }
  797. void Platform::_update()
  798. {
  799. LinuxDragAndDrop::update();
  800. }
  801. void Platform::_coreUpdate()
  802. {
  803. _messagePump();
  804. }
  805. void Platform::_shutDown()
  806. {
  807. Lock lock(mData->lock);
  808. // Free empty cursor
  809. XFreeCursor(mData->xDisplay, mData->emptyCursor);
  810. mData->emptyCursor = None;
  811. // Shutdown drag and drop
  812. LinuxDragAndDrop::shutDown();
  813. if(mData->IC)
  814. {
  815. XDestroyIC(mData->IC);
  816. mData->IC = 0;
  817. }
  818. if(mData->IM)
  819. {
  820. XCloseIM(mData->IM);
  821. mData->IM = 0;
  822. }
  823. XCloseDisplay(mData->xDisplay);
  824. mData->xDisplay = nullptr;
  825. bs_delete(mData);
  826. mData = nullptr;
  827. }
  828. ::Display* LinuxPlatform::getXDisplay()
  829. {
  830. return mData->xDisplay;
  831. }
  832. ::Window LinuxPlatform::getMainXWindow()
  833. {
  834. return mData->mainXWindow;
  835. }
  836. void LinuxPlatform::lockX()
  837. {
  838. mData->lock.lock();
  839. }
  840. void LinuxPlatform::unlockX()
  841. {
  842. mData->lock.unlock();
  843. }
  844. void LinuxPlatform::_registerWindow(::Window xWindow, LinuxWindow* window)
  845. {
  846. // First window is assumed to be the main
  847. if(mData->mainXWindow == 0)
  848. {
  849. mData->mainXWindow = xWindow;
  850. // Input context client window must be set before use
  851. XSetICValues(mData->IC,
  852. XNClientWindow, xWindow,
  853. XNFocusWindow, xWindow,
  854. nullptr);
  855. }
  856. mData->windowMap[xWindow] = window;
  857. applyCurrentCursor(mData, xWindow);
  858. }
  859. void LinuxPlatform::_unregisterWindow(::Window xWindow)
  860. {
  861. auto iterFind = mData->windowMap.find(xWindow);
  862. if(iterFind != mData->windowMap.end())
  863. {
  864. if(mData->cursorClipEnabled && mData->cursorClipWindow == iterFind->second)
  865. clipCursorDisable();
  866. mData->windowMap.erase(iterFind);
  867. }
  868. if(mData->mainXWindow == xWindow)
  869. mData->mainXWindow = 0;
  870. }
  871. Pixmap LinuxPlatform::createPixmap(const PixelData& data)
  872. {
  873. SPtr<PixelData> bgraData = PixelData::create(data.getWidth(), data.getHeight(), 1, PF_BGRA8);
  874. PixelUtil::bulkPixelConversion(data, *bgraData);
  875. UINT32 depth = (UINT32)XDefaultDepth(mData->xDisplay, 0);
  876. XImage* image = XCreateImage(mData->xDisplay, XDefaultVisual(mData->xDisplay, 0), depth, ZPixmap, 0,
  877. (char*)bgraData->getData(), data.getWidth(), data.getHeight(), 32, 0);
  878. Pixmap pixmap = XCreatePixmap(mData->xDisplay, XDefaultRootWindow(mData->xDisplay),
  879. data.getWidth(), data.getHeight(), depth);
  880. XGCValues gcValues;
  881. GC gc = XCreateGC(mData->xDisplay, pixmap, 0, &gcValues);
  882. XPutImage(mData->xDisplay, pixmap, gc, image, 0, 0, 0, 0, data.getWidth(), data.getHeight());
  883. XFreeGC(mData->xDisplay, gc);
  884. // Make sure XDestroyImage doesn't free the data pointed to by 'data.bytes'
  885. image->data = nullptr;
  886. XDestroyImage(image);
  887. return pixmap;
  888. }
  889. }