The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3947 lines
152KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. #if JUCE_DEBUG && ! defined (JUCE_DEBUG_XERRORS)
  21. #define JUCE_DEBUG_XERRORS 1
  22. #if ! defined (JUCE_DEBUG_XERRORS_SYNCHRONOUSLY)
  23. #define JUCE_DEBUG_XERRORS_SYNCHRONOUSLY 0
  24. #endif
  25. #endif
  26. #if JUCE_MODULE_AVAILABLE_juce_gui_extra
  27. #define JUCE_X11_SUPPORTS_XEMBED 1
  28. #else
  29. #define JUCE_X11_SUPPORTS_XEMBED 0
  30. #endif
  31. namespace
  32. {
  33. struct XFreeDeleter
  34. {
  35. void operator() (void* ptr) const
  36. {
  37. if (ptr != nullptr)
  38. X11Symbols::getInstance()->xFree (ptr);
  39. }
  40. };
  41. template <typename Data>
  42. std::unique_ptr<Data, XFreeDeleter> makeXFreePtr (Data* raw) { return std::unique_ptr<Data, XFreeDeleter> (raw); }
  43. template <typename Data, typename Deleter>
  44. std::unique_ptr<Data, Deleter> makeDeletedPtr (Data* raw, const Deleter& d) { return std::unique_ptr<Data, Deleter> (raw, d); }
  45. template <typename XValueType>
  46. struct XValueHolder
  47. {
  48. XValueHolder (XValueType&& xv, const std::function<void(XValueType&)>& cleanup)
  49. : value (std::move (xv)), cleanupFunc (cleanup)
  50. {}
  51. ~XValueHolder()
  52. {
  53. cleanupFunc (value);
  54. }
  55. XValueType value;
  56. std::function<void(XValueType&)> cleanupFunc;
  57. };
  58. }
  59. //==============================================================================
  60. XWindowSystemUtilities::ScopedXLock::ScopedXLock()
  61. {
  62. if (auto* xWindow = XWindowSystem::getInstanceWithoutCreating())
  63. if (auto* d = xWindow->getDisplay())
  64. X11Symbols::getInstance()->xLockDisplay (d);
  65. }
  66. XWindowSystemUtilities::ScopedXLock::~ScopedXLock()
  67. {
  68. if (auto* xWindow = XWindowSystem::getInstanceWithoutCreating())
  69. if (auto* d = xWindow->getDisplay())
  70. X11Symbols::getInstance()->xUnlockDisplay (d);
  71. }
  72. //==============================================================================
  73. XWindowSystemUtilities::Atoms::Atoms (::Display* display)
  74. {
  75. protocols = getIfExists (display, "WM_PROTOCOLS");
  76. protocolList [TAKE_FOCUS] = getIfExists (display, "WM_TAKE_FOCUS");
  77. protocolList [DELETE_WINDOW] = getIfExists (display, "WM_DELETE_WINDOW");
  78. protocolList [PING] = getIfExists (display, "_NET_WM_PING");
  79. changeState = getIfExists (display, "WM_CHANGE_STATE");
  80. state = getIfExists (display, "WM_STATE");
  81. userTime = getCreating (display, "_NET_WM_USER_TIME");
  82. activeWin = getCreating (display, "_NET_ACTIVE_WINDOW");
  83. pid = getCreating (display, "_NET_WM_PID");
  84. windowType = getIfExists (display, "_NET_WM_WINDOW_TYPE");
  85. windowState = getIfExists (display, "_NET_WM_STATE");
  86. windowStateHidden = getIfExists (display, "_NET_WM_STATE_HIDDEN");
  87. XdndAware = getCreating (display, "XdndAware");
  88. XdndEnter = getCreating (display, "XdndEnter");
  89. XdndLeave = getCreating (display, "XdndLeave");
  90. XdndPosition = getCreating (display, "XdndPosition");
  91. XdndStatus = getCreating (display, "XdndStatus");
  92. XdndDrop = getCreating (display, "XdndDrop");
  93. XdndFinished = getCreating (display, "XdndFinished");
  94. XdndSelection = getCreating (display, "XdndSelection");
  95. XdndTypeList = getCreating (display, "XdndTypeList");
  96. XdndActionList = getCreating (display, "XdndActionList");
  97. XdndActionCopy = getCreating (display, "XdndActionCopy");
  98. XdndActionPrivate = getCreating (display, "XdndActionPrivate");
  99. XdndActionDescription = getCreating (display, "XdndActionDescription");
  100. XembedMsgType = getCreating (display, "_XEMBED");
  101. XembedInfo = getCreating (display, "_XEMBED_INFO");
  102. allowedMimeTypes[0] = getCreating (display, "UTF8_STRING");
  103. allowedMimeTypes[1] = getCreating (display, "text/plain;charset=utf-8");
  104. allowedMimeTypes[2] = getCreating (display, "text/plain");
  105. allowedMimeTypes[3] = getCreating (display, "text/uri-list");
  106. allowedActions[0] = getCreating (display, "XdndActionMove");
  107. allowedActions[1] = XdndActionCopy;
  108. allowedActions[2] = getCreating (display, "XdndActionLink");
  109. allowedActions[3] = getCreating (display, "XdndActionAsk");
  110. allowedActions[4] = XdndActionPrivate;
  111. utf8String = getCreating (display, "UTF8_STRING");
  112. clipboard = getCreating (display, "CLIPBOARD");
  113. targets = getCreating (display, "TARGETS");
  114. }
  115. Atom XWindowSystemUtilities::Atoms::getIfExists (::Display* display, const char* name)
  116. {
  117. return X11Symbols::getInstance()->xInternAtom (display, name, True);
  118. }
  119. Atom XWindowSystemUtilities::Atoms::getCreating (::Display* display, const char* name)
  120. {
  121. return X11Symbols::getInstance()->xInternAtom (display, name, False);
  122. }
  123. String XWindowSystemUtilities::Atoms::getName (::Display* display, Atom atom)
  124. {
  125. if (atom == None)
  126. return "None";
  127. return makeXFreePtr (X11Symbols::getInstance()->xGetAtomName (display, atom)).get();
  128. }
  129. bool XWindowSystemUtilities::Atoms::isMimeTypeFile (::Display* display, Atom atom)
  130. {
  131. return getName (display, atom).equalsIgnoreCase ("text/uri-list");
  132. }
  133. //==============================================================================
  134. XWindowSystemUtilities::GetXProperty::GetXProperty (::Display* display, Window window, Atom atom,
  135. long offset, long length, bool shouldDelete, Atom requestedType)
  136. {
  137. success = (X11Symbols::getInstance()->xGetWindowProperty (display, window, atom, offset, length,
  138. (Bool) shouldDelete, requestedType, &actualType,
  139. &actualFormat, &numItems, &bytesLeft, &data) == Success)
  140. && data != nullptr;
  141. }
  142. XWindowSystemUtilities::GetXProperty::~GetXProperty()
  143. {
  144. if (data != nullptr)
  145. X11Symbols::getInstance()->xFree (data);
  146. }
  147. //==============================================================================
  148. std::unique_ptr<XWindowSystemUtilities::XSettings> XWindowSystemUtilities::XSettings::createXSettings (::Display* d)
  149. {
  150. const auto settingsAtom = Atoms::getCreating (d, "_XSETTINGS_SETTINGS");
  151. const auto settingsWindow = X11Symbols::getInstance()->xGetSelectionOwner (d,
  152. Atoms::getCreating (d, "_XSETTINGS_S0"));
  153. if (settingsWindow == None)
  154. return {};
  155. return rawToUniquePtr (new XWindowSystemUtilities::XSettings (d, settingsWindow, settingsAtom));
  156. }
  157. XWindowSystemUtilities::XSettings::XSettings (::Display* d, ::Window settingsWindowIn, Atom settingsAtomIn)
  158. : display (d), settingsWindow (settingsWindowIn), settingsAtom (settingsAtomIn)
  159. {
  160. update();
  161. }
  162. XWindowSystemUtilities::XSetting XWindowSystemUtilities::XSettings::getSetting (const String& name) const
  163. {
  164. const auto iter = settings.find (name);
  165. if (iter != settings.end())
  166. return iter->second;
  167. return {};
  168. }
  169. void XWindowSystemUtilities::XSettings::update()
  170. {
  171. const GetXProperty prop { display,
  172. settingsWindow,
  173. settingsAtom,
  174. 0L,
  175. std::numeric_limits<long>::max(),
  176. false,
  177. settingsAtom };
  178. if (prop.success
  179. && prop.actualType == settingsAtom
  180. && prop.actualFormat == 8
  181. && prop.numItems > 0)
  182. {
  183. const auto bytes = (size_t) prop.numItems;
  184. auto* data = prop.data;
  185. size_t byteNum = 0;
  186. const auto increment = [&] (size_t amount)
  187. {
  188. data += amount;
  189. byteNum += amount;
  190. };
  191. struct Header
  192. {
  193. CARD8 byteOrder;
  194. CARD8 padding[3];
  195. CARD32 serial;
  196. CARD32 nSettings;
  197. };
  198. const auto* header = unalignedPointerCast<const Header*> (data);
  199. const auto headerSerial = (int) header->serial;
  200. increment (sizeof (Header));
  201. const auto readCARD16 = [&]() -> CARD16
  202. {
  203. if (byteNum + sizeof (CARD16) > bytes)
  204. return {};
  205. const auto value = header->byteOrder == MSBFirst ? ByteOrder::bigEndianShort (data)
  206. : ByteOrder::littleEndianShort (data);
  207. increment (sizeof (CARD16));
  208. return value;
  209. };
  210. const auto readCARD32 = [&]() -> CARD32
  211. {
  212. if (byteNum + sizeof (CARD32) > bytes)
  213. return {};
  214. const auto value = header->byteOrder == MSBFirst ? ByteOrder::bigEndianInt (data)
  215. : ByteOrder::littleEndianInt (data);
  216. increment (sizeof (CARD32));
  217. return value;
  218. };
  219. const auto readString = [&] (size_t nameLen) -> String
  220. {
  221. const auto padded = (nameLen + 3) & (~(size_t) 3);
  222. if (byteNum + padded > bytes)
  223. return {};
  224. auto* ptr = reinterpret_cast<const char*> (data);
  225. const String result (ptr, nameLen);
  226. increment (padded);
  227. return result;
  228. };
  229. CARD16 setting = 0;
  230. while (byteNum < bytes && setting < header->nSettings)
  231. {
  232. const auto type = *reinterpret_cast<const char*> (data);
  233. increment (2);
  234. const auto name = readString (readCARD16());
  235. const auto serial = (int) readCARD32();
  236. enum { XSettingsTypeInteger, XSettingsTypeString, XSettingsTypeColor };
  237. const auto parsedSetting = [&]() -> XSetting
  238. {
  239. switch (type)
  240. {
  241. case XSettingsTypeInteger:
  242. return { name, (int) readCARD32() };
  243. case XSettingsTypeString:
  244. return { name, readString (readCARD32()) };
  245. case XSettingsTypeColor:
  246. // Order is important, these should be kept as separate statements!
  247. const auto r = (uint8) readCARD16();
  248. const auto g = (uint8) readCARD16();
  249. const auto b = (uint8) readCARD16();
  250. const auto a = (uint8) readCARD16();
  251. return { name, Colour { r, g, b, a } };
  252. }
  253. return {};
  254. }();
  255. if (serial > lastUpdateSerial)
  256. {
  257. settings[parsedSetting.name] = parsedSetting;
  258. listeners.call ([&parsedSetting] (Listener& l) { l.settingChanged (parsedSetting); });
  259. }
  260. setting += 1;
  261. }
  262. lastUpdateSerial = headerSerial;
  263. }
  264. }
  265. //==============================================================================
  266. ::Window juce_messageWindowHandle;
  267. XContext windowHandleXContext;
  268. #if JUCE_X11_SUPPORTS_XEMBED
  269. bool juce_handleXEmbedEvent (ComponentPeer*, void*);
  270. unsigned long juce_getCurrentFocusWindow (ComponentPeer*);
  271. #endif
  272. struct MotifWmHints
  273. {
  274. unsigned long flags = 0;
  275. unsigned long functions = 0;
  276. unsigned long decorations = 0;
  277. long input_mode = 0;
  278. unsigned long status = 0;
  279. };
  280. //=============================== X11 - Error Handling =========================
  281. namespace X11ErrorHandling
  282. {
  283. static XErrorHandler oldErrorHandler = {};
  284. static XIOErrorHandler oldIOErrorHandler = {};
  285. // Usually happens when client-server connection is broken
  286. static int ioErrorHandler (::Display*)
  287. {
  288. DBG ("ERROR: connection to X server broken.. terminating.");
  289. if (JUCEApplicationBase::isStandaloneApp())
  290. MessageManager::getInstance()->stopDispatchLoop();
  291. return 0;
  292. }
  293. static int errorHandler (::Display* display, XErrorEvent* event)
  294. {
  295. ignoreUnused (display, event);
  296. #if JUCE_DEBUG_XERRORS
  297. char errorStr[64] = { 0 };
  298. char requestStr[64] = { 0 };
  299. X11Symbols::getInstance()->xGetErrorText (display, event->error_code, errorStr, 64);
  300. X11Symbols::getInstance()->xGetErrorDatabaseText (display, "XRequest", String (event->request_code).toUTF8(), "Unknown", requestStr, 64);
  301. DBG ("ERROR: X returned " << errorStr << " for operation " << requestStr);
  302. #endif
  303. return 0;
  304. }
  305. static void installXErrorHandlers()
  306. {
  307. oldIOErrorHandler = X11Symbols::getInstance()->xSetIOErrorHandler (ioErrorHandler);
  308. oldErrorHandler = X11Symbols::getInstance()->xSetErrorHandler (errorHandler);
  309. }
  310. static void removeXErrorHandlers()
  311. {
  312. X11Symbols::getInstance()->xSetIOErrorHandler (oldIOErrorHandler);
  313. oldIOErrorHandler = {};
  314. X11Symbols::getInstance()->xSetErrorHandler (oldErrorHandler);
  315. oldErrorHandler = {};
  316. }
  317. }
  318. //=============================== X11 - Keys ===================================
  319. namespace Keys
  320. {
  321. enum MouseButtons
  322. {
  323. NoButton = 0,
  324. LeftButton = 1,
  325. MiddleButton = 2,
  326. RightButton = 3,
  327. WheelUp = 4,
  328. WheelDown = 5
  329. };
  330. static int AltMask = 0;
  331. static int NumLockMask = 0;
  332. static bool numLock = false;
  333. static bool capsLock = false;
  334. static char keyStates [32];
  335. static constexpr int extendedKeyModifier = 0x10000000;
  336. static bool modifierKeysAreStale = false;
  337. static void refreshStaleModifierKeys()
  338. {
  339. if (modifierKeysAreStale)
  340. {
  341. XWindowSystem::getInstance()->getNativeRealtimeModifiers();
  342. modifierKeysAreStale = false;
  343. }
  344. }
  345. // Call this function when only the mouse keys need to be refreshed e.g. when the event
  346. // parameter already has information about the keys.
  347. static void refreshStaleMouseKeys()
  348. {
  349. if (modifierKeysAreStale)
  350. {
  351. const auto oldMods = ModifierKeys::currentModifiers;
  352. XWindowSystem::getInstance()->getNativeRealtimeModifiers();
  353. ModifierKeys::currentModifiers = oldMods.withoutMouseButtons()
  354. .withFlags (ModifierKeys::currentModifiers.withOnlyMouseButtons()
  355. .getRawFlags());
  356. modifierKeysAreStale = false;
  357. }
  358. }
  359. }
  360. const int KeyPress::spaceKey = XK_space & 0xff;
  361. const int KeyPress::returnKey = XK_Return & 0xff;
  362. const int KeyPress::escapeKey = XK_Escape & 0xff;
  363. const int KeyPress::backspaceKey = XK_BackSpace & 0xff;
  364. const int KeyPress::leftKey = (XK_Left & 0xff) | Keys::extendedKeyModifier;
  365. const int KeyPress::rightKey = (XK_Right & 0xff) | Keys::extendedKeyModifier;
  366. const int KeyPress::upKey = (XK_Up & 0xff) | Keys::extendedKeyModifier;
  367. const int KeyPress::downKey = (XK_Down & 0xff) | Keys::extendedKeyModifier;
  368. const int KeyPress::pageUpKey = (XK_Page_Up & 0xff) | Keys::extendedKeyModifier;
  369. const int KeyPress::pageDownKey = (XK_Page_Down & 0xff) | Keys::extendedKeyModifier;
  370. const int KeyPress::endKey = (XK_End & 0xff) | Keys::extendedKeyModifier;
  371. const int KeyPress::homeKey = (XK_Home & 0xff) | Keys::extendedKeyModifier;
  372. const int KeyPress::insertKey = (XK_Insert & 0xff) | Keys::extendedKeyModifier;
  373. const int KeyPress::deleteKey = (XK_Delete & 0xff) | Keys::extendedKeyModifier;
  374. const int KeyPress::tabKey = XK_Tab & 0xff;
  375. const int KeyPress::F1Key = (XK_F1 & 0xff) | Keys::extendedKeyModifier;
  376. const int KeyPress::F2Key = (XK_F2 & 0xff) | Keys::extendedKeyModifier;
  377. const int KeyPress::F3Key = (XK_F3 & 0xff) | Keys::extendedKeyModifier;
  378. const int KeyPress::F4Key = (XK_F4 & 0xff) | Keys::extendedKeyModifier;
  379. const int KeyPress::F5Key = (XK_F5 & 0xff) | Keys::extendedKeyModifier;
  380. const int KeyPress::F6Key = (XK_F6 & 0xff) | Keys::extendedKeyModifier;
  381. const int KeyPress::F7Key = (XK_F7 & 0xff) | Keys::extendedKeyModifier;
  382. const int KeyPress::F8Key = (XK_F8 & 0xff) | Keys::extendedKeyModifier;
  383. const int KeyPress::F9Key = (XK_F9 & 0xff) | Keys::extendedKeyModifier;
  384. const int KeyPress::F10Key = (XK_F10 & 0xff) | Keys::extendedKeyModifier;
  385. const int KeyPress::F11Key = (XK_F11 & 0xff) | Keys::extendedKeyModifier;
  386. const int KeyPress::F12Key = (XK_F12 & 0xff) | Keys::extendedKeyModifier;
  387. const int KeyPress::F13Key = (XK_F13 & 0xff) | Keys::extendedKeyModifier;
  388. const int KeyPress::F14Key = (XK_F14 & 0xff) | Keys::extendedKeyModifier;
  389. const int KeyPress::F15Key = (XK_F15 & 0xff) | Keys::extendedKeyModifier;
  390. const int KeyPress::F16Key = (XK_F16 & 0xff) | Keys::extendedKeyModifier;
  391. const int KeyPress::F17Key = (XK_F17 & 0xff) | Keys::extendedKeyModifier;
  392. const int KeyPress::F18Key = (XK_F18 & 0xff) | Keys::extendedKeyModifier;
  393. const int KeyPress::F19Key = (XK_F19 & 0xff) | Keys::extendedKeyModifier;
  394. const int KeyPress::F20Key = (XK_F20 & 0xff) | Keys::extendedKeyModifier;
  395. const int KeyPress::F21Key = (XK_F21 & 0xff) | Keys::extendedKeyModifier;
  396. const int KeyPress::F22Key = (XK_F22 & 0xff) | Keys::extendedKeyModifier;
  397. const int KeyPress::F23Key = (XK_F23 & 0xff) | Keys::extendedKeyModifier;
  398. const int KeyPress::F24Key = (XK_F24 & 0xff) | Keys::extendedKeyModifier;
  399. const int KeyPress::F25Key = (XK_F25 & 0xff) | Keys::extendedKeyModifier;
  400. const int KeyPress::F26Key = (XK_F26 & 0xff) | Keys::extendedKeyModifier;
  401. const int KeyPress::F27Key = (XK_F27 & 0xff) | Keys::extendedKeyModifier;
  402. const int KeyPress::F28Key = (XK_F28 & 0xff) | Keys::extendedKeyModifier;
  403. const int KeyPress::F29Key = (XK_F29 & 0xff) | Keys::extendedKeyModifier;
  404. const int KeyPress::F30Key = (XK_F30 & 0xff) | Keys::extendedKeyModifier;
  405. const int KeyPress::F31Key = (XK_F31 & 0xff) | Keys::extendedKeyModifier;
  406. const int KeyPress::F32Key = (XK_F32 & 0xff) | Keys::extendedKeyModifier;
  407. const int KeyPress::F33Key = (XK_F33 & 0xff) | Keys::extendedKeyModifier;
  408. const int KeyPress::F34Key = (XK_F34 & 0xff) | Keys::extendedKeyModifier;
  409. const int KeyPress::F35Key = (XK_F35 & 0xff) | Keys::extendedKeyModifier;
  410. const int KeyPress::numberPad0 = (XK_KP_0 & 0xff) | Keys::extendedKeyModifier;
  411. const int KeyPress::numberPad1 = (XK_KP_1 & 0xff) | Keys::extendedKeyModifier;
  412. const int KeyPress::numberPad2 = (XK_KP_2 & 0xff) | Keys::extendedKeyModifier;
  413. const int KeyPress::numberPad3 = (XK_KP_3 & 0xff) | Keys::extendedKeyModifier;
  414. const int KeyPress::numberPad4 = (XK_KP_4 & 0xff) | Keys::extendedKeyModifier;
  415. const int KeyPress::numberPad5 = (XK_KP_5 & 0xff) | Keys::extendedKeyModifier;
  416. const int KeyPress::numberPad6 = (XK_KP_6 & 0xff) | Keys::extendedKeyModifier;
  417. const int KeyPress::numberPad7 = (XK_KP_7 & 0xff) | Keys::extendedKeyModifier;
  418. const int KeyPress::numberPad8 = (XK_KP_8 & 0xff) | Keys::extendedKeyModifier;
  419. const int KeyPress::numberPad9 = (XK_KP_9 & 0xff) | Keys::extendedKeyModifier;
  420. const int KeyPress::numberPadAdd = (XK_KP_Add & 0xff) | Keys::extendedKeyModifier;
  421. const int KeyPress::numberPadSubtract = (XK_KP_Subtract & 0xff) | Keys::extendedKeyModifier;
  422. const int KeyPress::numberPadMultiply = (XK_KP_Multiply & 0xff) | Keys::extendedKeyModifier;
  423. const int KeyPress::numberPadDivide = (XK_KP_Divide & 0xff) | Keys::extendedKeyModifier;
  424. const int KeyPress::numberPadSeparator = (XK_KP_Separator & 0xff) | Keys::extendedKeyModifier;
  425. const int KeyPress::numberPadDecimalPoint = (XK_KP_Decimal & 0xff) | Keys::extendedKeyModifier;
  426. const int KeyPress::numberPadEquals = (XK_KP_Equal & 0xff) | Keys::extendedKeyModifier;
  427. const int KeyPress::numberPadDelete = (XK_KP_Delete & 0xff) | Keys::extendedKeyModifier;
  428. const int KeyPress::playKey = ((int) 0xffeeff00) | Keys::extendedKeyModifier;
  429. const int KeyPress::stopKey = ((int) 0xffeeff01) | Keys::extendedKeyModifier;
  430. const int KeyPress::fastForwardKey = ((int) 0xffeeff02) | Keys::extendedKeyModifier;
  431. const int KeyPress::rewindKey = ((int) 0xffeeff03) | Keys::extendedKeyModifier;
  432. static void updateKeyStates (int keycode, bool press) noexcept
  433. {
  434. auto keybyte = keycode >> 3;
  435. auto keybit = (1 << (keycode & 7));
  436. if (press)
  437. Keys::keyStates [keybyte] |= keybit;
  438. else
  439. Keys::keyStates [keybyte] &= ~keybit;
  440. }
  441. static void updateKeyModifiers (int status) noexcept
  442. {
  443. int keyMods = 0;
  444. if ((status & ShiftMask) != 0) keyMods |= ModifierKeys::shiftModifier;
  445. if ((status & ControlMask) != 0) keyMods |= ModifierKeys::ctrlModifier;
  446. if ((status & Keys::AltMask) != 0) keyMods |= ModifierKeys::altModifier;
  447. ModifierKeys::currentModifiers = ModifierKeys::currentModifiers.withOnlyMouseButtons().withFlags (keyMods);
  448. Keys::numLock = ((status & Keys::NumLockMask) != 0);
  449. Keys::capsLock = ((status & LockMask) != 0);
  450. }
  451. static bool updateKeyModifiersFromSym (KeySym sym, bool press) noexcept
  452. {
  453. int modifier = 0;
  454. bool isModifier = true;
  455. switch (sym)
  456. {
  457. case XK_Shift_L:
  458. case XK_Shift_R: modifier = ModifierKeys::shiftModifier; break;
  459. case XK_Control_L:
  460. case XK_Control_R: modifier = ModifierKeys::ctrlModifier; break;
  461. case XK_Alt_L:
  462. case XK_Alt_R: modifier = ModifierKeys::altModifier; break;
  463. case XK_Num_Lock:
  464. if (press)
  465. Keys::numLock = ! Keys::numLock;
  466. break;
  467. case XK_Caps_Lock:
  468. if (press)
  469. Keys::capsLock = ! Keys::capsLock;
  470. break;
  471. case XK_Scroll_Lock:
  472. break;
  473. default:
  474. isModifier = false;
  475. break;
  476. }
  477. ModifierKeys::currentModifiers = press ? ModifierKeys::currentModifiers.withFlags (modifier)
  478. : ModifierKeys::currentModifiers.withoutFlags (modifier);
  479. return isModifier;
  480. }
  481. enum
  482. {
  483. KeyPressEventType = 2
  484. };
  485. //================================== X11 - Shm =================================
  486. #if JUCE_USE_XSHM
  487. namespace XSHMHelpers
  488. {
  489. static int trappedErrorCode = 0;
  490. extern "C" int errorTrapHandler (Display*, XErrorEvent* err);
  491. extern "C" int errorTrapHandler (Display*, XErrorEvent* err)
  492. {
  493. trappedErrorCode = err->error_code;
  494. return 0;
  495. }
  496. static bool isShmAvailable (::Display* display)
  497. {
  498. static bool isChecked = false;
  499. static bool isAvailable = false;
  500. if (! isChecked)
  501. {
  502. isChecked = true;
  503. if (display != nullptr)
  504. {
  505. int major, minor;
  506. Bool pixmaps;
  507. XWindowSystemUtilities::ScopedXLock xLock;
  508. if (X11Symbols::getInstance()->xShmQueryVersion (display, &major, &minor, &pixmaps))
  509. {
  510. trappedErrorCode = 0;
  511. auto oldHandler = X11Symbols::getInstance()->xSetErrorHandler (errorTrapHandler);
  512. XShmSegmentInfo segmentInfo;
  513. zerostruct (segmentInfo);
  514. if (auto* xImage = X11Symbols::getInstance()->xShmCreateImage (display,
  515. X11Symbols::getInstance()->xDefaultVisual (display, X11Symbols::getInstance()->xDefaultScreen (display)),
  516. 24, ZPixmap, nullptr, &segmentInfo, 50, 50))
  517. {
  518. if ((segmentInfo.shmid = shmget (IPC_PRIVATE,
  519. (size_t) (xImage->bytes_per_line * xImage->height),
  520. IPC_CREAT | 0777)) >= 0)
  521. {
  522. segmentInfo.shmaddr = (char*) shmat (segmentInfo.shmid, nullptr, 0);
  523. if (segmentInfo.shmaddr != (void*) -1)
  524. {
  525. segmentInfo.readOnly = False;
  526. xImage->data = segmentInfo.shmaddr;
  527. X11Symbols::getInstance()->xSync (display, False);
  528. if (X11Symbols::getInstance()->xShmAttach (display, &segmentInfo) != 0)
  529. {
  530. X11Symbols::getInstance()->xSync (display, False);
  531. X11Symbols::getInstance()->xShmDetach (display, &segmentInfo);
  532. isAvailable = true;
  533. }
  534. }
  535. X11Symbols::getInstance()->xFlush (display);
  536. X11Symbols::getInstance()->xDestroyImage (xImage);
  537. shmdt (segmentInfo.shmaddr);
  538. }
  539. shmctl (segmentInfo.shmid, IPC_RMID, nullptr);
  540. X11Symbols::getInstance()->xSetErrorHandler (oldHandler);
  541. if (trappedErrorCode != 0)
  542. isAvailable = false;
  543. }
  544. }
  545. }
  546. }
  547. return isAvailable;
  548. }
  549. }
  550. #endif
  551. //=============================== X11 - Render =================================
  552. #if JUCE_USE_XRENDER
  553. namespace XRender
  554. {
  555. static bool isAvailable (::Display* display)
  556. {
  557. int major, minor;
  558. return X11Symbols::getInstance()->xRenderQueryVersion (display, &major, &minor);
  559. }
  560. static bool hasCompositingWindowManager (::Display* display)
  561. {
  562. return display != nullptr
  563. && X11Symbols::getInstance()->xGetSelectionOwner (display,
  564. XWindowSystemUtilities::Atoms::getCreating (display, "_NET_WM_CM_S0")) != 0;
  565. }
  566. static XRenderPictFormat* findPictureFormat (::Display* display)
  567. {
  568. XWindowSystemUtilities::ScopedXLock xLock;
  569. if (isAvailable (display))
  570. {
  571. if (auto* pictFormat = X11Symbols::getInstance()->xRenderFindStandardFormat (display, PictStandardARGB32))
  572. {
  573. XRenderPictFormat desiredFormat;
  574. desiredFormat.type = PictTypeDirect;
  575. desiredFormat.depth = 32;
  576. desiredFormat.direct.alphaMask = 0xff;
  577. desiredFormat.direct.redMask = 0xff;
  578. desiredFormat.direct.greenMask = 0xff;
  579. desiredFormat.direct.blueMask = 0xff;
  580. desiredFormat.direct.alpha = 24;
  581. desiredFormat.direct.red = 16;
  582. desiredFormat.direct.green = 8;
  583. desiredFormat.direct.blue = 0;
  584. pictFormat = X11Symbols::getInstance()->xRenderFindFormat (display,
  585. PictFormatType | PictFormatDepth
  586. | PictFormatRedMask | PictFormatRed
  587. | PictFormatGreenMask | PictFormatGreen
  588. | PictFormatBlueMask | PictFormatBlue
  589. | PictFormatAlphaMask | PictFormatAlpha,
  590. &desiredFormat,
  591. 0);
  592. return pictFormat;
  593. }
  594. }
  595. return nullptr;
  596. }
  597. }
  598. #endif
  599. //================================ X11 - Visuals ===============================
  600. namespace Visuals
  601. {
  602. static Visual* findVisualWithDepth (::Display* display, int desiredDepth)
  603. {
  604. XWindowSystemUtilities::ScopedXLock xLock;
  605. Visual* visual = nullptr;
  606. int numVisuals = 0;
  607. auto desiredMask = VisualNoMask;
  608. XVisualInfo desiredVisual;
  609. desiredVisual.screen = X11Symbols::getInstance()->xDefaultScreen (display);
  610. desiredVisual.depth = desiredDepth;
  611. desiredMask = VisualScreenMask | VisualDepthMask;
  612. if (desiredDepth == 32)
  613. {
  614. desiredVisual.c_class = TrueColor;
  615. desiredVisual.red_mask = 0x00FF0000;
  616. desiredVisual.green_mask = 0x0000FF00;
  617. desiredVisual.blue_mask = 0x000000FF;
  618. desiredVisual.bits_per_rgb = 8;
  619. desiredMask |= VisualClassMask;
  620. desiredMask |= VisualRedMaskMask;
  621. desiredMask |= VisualGreenMaskMask;
  622. desiredMask |= VisualBlueMaskMask;
  623. desiredMask |= VisualBitsPerRGBMask;
  624. }
  625. if (auto xvinfos = makeXFreePtr (X11Symbols::getInstance()->xGetVisualInfo (display, desiredMask, &desiredVisual, &numVisuals)))
  626. {
  627. for (int i = 0; i < numVisuals; i++)
  628. {
  629. if (xvinfos.get()[i].depth == desiredDepth)
  630. {
  631. visual = xvinfos.get()[i].visual;
  632. break;
  633. }
  634. }
  635. }
  636. return visual;
  637. }
  638. static Visual* findVisualFormat (::Display* display, int desiredDepth, int& matchedDepth)
  639. {
  640. Visual* visual = nullptr;
  641. if (desiredDepth == 32)
  642. {
  643. #if JUCE_USE_XSHM
  644. if (XSHMHelpers::isShmAvailable (display))
  645. {
  646. #if JUCE_USE_XRENDER
  647. if (XRender::isAvailable (display))
  648. {
  649. if (XRender::findPictureFormat (display) != nullptr)
  650. {
  651. int numVisuals = 0;
  652. XVisualInfo desiredVisual;
  653. desiredVisual.screen = X11Symbols::getInstance()->xDefaultScreen (display);
  654. desiredVisual.depth = 32;
  655. desiredVisual.bits_per_rgb = 8;
  656. if (auto xvinfos = makeXFreePtr (X11Symbols::getInstance()->xGetVisualInfo (display,
  657. VisualScreenMask | VisualDepthMask | VisualBitsPerRGBMask,
  658. &desiredVisual, &numVisuals)))
  659. {
  660. for (int i = 0; i < numVisuals; ++i)
  661. {
  662. auto pictVisualFormat = X11Symbols::getInstance()->xRenderFindVisualFormat (display, xvinfos.get()[i].visual);
  663. if (pictVisualFormat != nullptr
  664. && pictVisualFormat->type == PictTypeDirect
  665. && pictVisualFormat->direct.alphaMask)
  666. {
  667. visual = xvinfos.get()[i].visual;
  668. matchedDepth = 32;
  669. break;
  670. }
  671. }
  672. }
  673. }
  674. }
  675. #endif
  676. if (visual == nullptr)
  677. {
  678. visual = findVisualWithDepth (display, 32);
  679. if (visual != nullptr)
  680. matchedDepth = 32;
  681. }
  682. }
  683. #endif
  684. }
  685. if (visual == nullptr && desiredDepth >= 24)
  686. {
  687. visual = findVisualWithDepth (display, 24);
  688. if (visual != nullptr)
  689. matchedDepth = 24;
  690. }
  691. if (visual == nullptr && desiredDepth >= 16)
  692. {
  693. visual = findVisualWithDepth (display, 16);
  694. if (visual != nullptr)
  695. matchedDepth = 16;
  696. }
  697. return visual;
  698. }
  699. }
  700. //================================= X11 - Bitmap ===============================
  701. class XBitmapImage : public ImagePixelData
  702. {
  703. public:
  704. explicit XBitmapImage (XImage* image)
  705. : ImagePixelData (image->depth == 24 ? Image::RGB : Image::ARGB, image->width, image->height),
  706. xImage (image),
  707. imageDepth ((unsigned int) xImage->depth)
  708. {
  709. pixelStride = xImage->bits_per_pixel / 8;
  710. lineStride = xImage->bytes_per_line;
  711. imageData = reinterpret_cast<uint8*> (xImage->data);
  712. }
  713. XBitmapImage (Image::PixelFormat format, int w, int h,
  714. bool clearImage, unsigned int imageDepth_, Visual* visual)
  715. : ImagePixelData (format, w, h),
  716. imageDepth (imageDepth_)
  717. {
  718. jassert (format == Image::RGB || format == Image::ARGB);
  719. pixelStride = (format == Image::RGB) ? 3 : 4;
  720. lineStride = ((w * pixelStride + 3) & ~3);
  721. XWindowSystemUtilities::ScopedXLock xLock;
  722. #if JUCE_USE_XSHM
  723. usingXShm = false;
  724. if ((imageDepth > 16) && XSHMHelpers::isShmAvailable (display))
  725. {
  726. zerostruct (segmentInfo);
  727. segmentInfo.shmid = -1;
  728. segmentInfo.shmaddr = (char *) -1;
  729. segmentInfo.readOnly = False;
  730. xImage.reset (X11Symbols::getInstance()->xShmCreateImage (display, visual, imageDepth, ZPixmap, nullptr,
  731. &segmentInfo, (unsigned int) w, (unsigned int) h));
  732. if (xImage != nullptr)
  733. {
  734. if ((segmentInfo.shmid = shmget (IPC_PRIVATE,
  735. (size_t) (xImage->bytes_per_line * xImage->height),
  736. IPC_CREAT | 0777)) >= 0)
  737. {
  738. if (segmentInfo.shmid != -1)
  739. {
  740. segmentInfo.shmaddr = (char*) shmat (segmentInfo.shmid, nullptr, 0);
  741. if (segmentInfo.shmaddr != (void*) -1)
  742. {
  743. segmentInfo.readOnly = False;
  744. xImage->data = segmentInfo.shmaddr;
  745. imageData = (uint8*) segmentInfo.shmaddr;
  746. if (X11Symbols::getInstance()->xShmAttach (display, &segmentInfo) != 0)
  747. usingXShm = true;
  748. else
  749. jassertfalse;
  750. }
  751. else
  752. {
  753. shmctl (segmentInfo.shmid, IPC_RMID, nullptr);
  754. }
  755. }
  756. }
  757. }
  758. }
  759. if (! isUsingXShm())
  760. #endif
  761. {
  762. imageDataAllocated.allocate ((size_t) (lineStride * h), format == Image::ARGB && clearImage);
  763. imageData = imageDataAllocated;
  764. xImage.reset ((XImage*) ::calloc (1, sizeof (XImage)));
  765. xImage->width = w;
  766. xImage->height = h;
  767. xImage->xoffset = 0;
  768. xImage->format = ZPixmap;
  769. xImage->data = (char*) imageData;
  770. xImage->byte_order = X11Symbols::getInstance()->xImageByteOrder (display);
  771. xImage->bitmap_unit = X11Symbols::getInstance()->xBitmapUnit (display);
  772. xImage->bitmap_bit_order = X11Symbols::getInstance()->xBitmapBitOrder (display);
  773. xImage->bitmap_pad = 32;
  774. xImage->depth = pixelStride * 8;
  775. xImage->bytes_per_line = lineStride;
  776. xImage->bits_per_pixel = pixelStride * 8;
  777. xImage->red_mask = 0x00FF0000;
  778. xImage->green_mask = 0x0000FF00;
  779. xImage->blue_mask = 0x000000FF;
  780. if (imageDepth == 16)
  781. {
  782. int pixStride = 2;
  783. auto stride = ((w * pixStride + 3) & ~3);
  784. imageData16Bit.malloc (stride * h);
  785. xImage->data = imageData16Bit;
  786. xImage->bitmap_pad = 16;
  787. xImage->depth = pixStride * 8;
  788. xImage->bytes_per_line = stride;
  789. xImage->bits_per_pixel = pixStride * 8;
  790. xImage->red_mask = visual->red_mask;
  791. xImage->green_mask = visual->green_mask;
  792. xImage->blue_mask = visual->blue_mask;
  793. }
  794. if (! X11Symbols::getInstance()->xInitImage (xImage.get()))
  795. jassertfalse;
  796. }
  797. }
  798. ~XBitmapImage() override
  799. {
  800. XWindowSystemUtilities::ScopedXLock xLock;
  801. if (gc != None)
  802. X11Symbols::getInstance()->xFreeGC (display, gc);
  803. #if JUCE_USE_XSHM
  804. if (isUsingXShm())
  805. {
  806. X11Symbols::getInstance()->xShmDetach (display, &segmentInfo);
  807. X11Symbols::getInstance()->xFlush (display);
  808. shmdt (segmentInfo.shmaddr);
  809. shmctl (segmentInfo.shmid, IPC_RMID, nullptr);
  810. }
  811. else
  812. #endif
  813. {
  814. xImage->data = nullptr;
  815. }
  816. }
  817. std::unique_ptr<LowLevelGraphicsContext> createLowLevelContext() override
  818. {
  819. sendDataChangeMessage();
  820. return std::make_unique<LowLevelGraphicsSoftwareRenderer> (Image (this));
  821. }
  822. void initialiseBitmapData (Image::BitmapData& bitmap, int x, int y,
  823. Image::BitmapData::ReadWriteMode mode) override
  824. {
  825. const auto offset = (size_t) (x * pixelStride + y * lineStride);
  826. bitmap.data = imageData + offset;
  827. bitmap.size = (size_t) (lineStride * height) - offset;
  828. bitmap.pixelFormat = pixelFormat;
  829. bitmap.lineStride = lineStride;
  830. bitmap.pixelStride = pixelStride;
  831. if (mode != Image::BitmapData::readOnly)
  832. sendDataChangeMessage();
  833. }
  834. ImagePixelData::Ptr clone() override
  835. {
  836. jassertfalse;
  837. return nullptr;
  838. }
  839. std::unique_ptr<ImageType> createType() const override { return std::make_unique<NativeImageType>(); }
  840. void blitToWindow (::Window window, int dx, int dy, unsigned int dw, unsigned int dh, int sx, int sy)
  841. {
  842. XWindowSystemUtilities::ScopedXLock xLock;
  843. #if JUCE_USE_XSHM
  844. if (isUsingXShm())
  845. XWindowSystem::getInstance()->addPendingPaintForWindow (window);
  846. #endif
  847. if (gc == None)
  848. {
  849. XGCValues gcvalues;
  850. gcvalues.foreground = None;
  851. gcvalues.background = None;
  852. gcvalues.function = GXcopy;
  853. gcvalues.plane_mask = AllPlanes;
  854. gcvalues.clip_mask = None;
  855. gcvalues.graphics_exposures = False;
  856. gc = X11Symbols::getInstance()->xCreateGC (display, window,
  857. GCBackground | GCForeground | GCFunction | GCPlaneMask | GCClipMask | GCGraphicsExposures,
  858. &gcvalues);
  859. }
  860. if (imageDepth == 16)
  861. {
  862. auto rMask = (uint32) xImage->red_mask;
  863. auto gMask = (uint32) xImage->green_mask;
  864. auto bMask = (uint32) xImage->blue_mask;
  865. auto rShiftL = (uint32) jmax (0, getShiftNeeded (rMask));
  866. auto rShiftR = (uint32) jmax (0, -getShiftNeeded (rMask));
  867. auto gShiftL = (uint32) jmax (0, getShiftNeeded (gMask));
  868. auto gShiftR = (uint32) jmax (0, -getShiftNeeded (gMask));
  869. auto bShiftL = (uint32) jmax (0, getShiftNeeded (bMask));
  870. auto bShiftR = (uint32) jmax (0, -getShiftNeeded (bMask));
  871. Image::BitmapData srcData (Image (this), Image::BitmapData::readOnly);
  872. for (int y = sy; y < sy + (int) dh; ++y)
  873. {
  874. auto* p = srcData.getPixelPointer (sx, y);
  875. for (int x = sx; x < sx + (int) dw; ++x)
  876. {
  877. auto* pixel = (PixelRGB*) p;
  878. p += srcData.pixelStride;
  879. X11Symbols::getInstance()->xPutPixel (xImage.get(), x, y,
  880. (((((uint32) pixel->getRed()) << rShiftL) >> rShiftR) & rMask)
  881. | (((((uint32) pixel->getGreen()) << gShiftL) >> gShiftR) & gMask)
  882. | (((((uint32) pixel->getBlue()) << bShiftL) >> bShiftR) & bMask));
  883. }
  884. }
  885. }
  886. // blit results to screen.
  887. #if JUCE_USE_XSHM
  888. if (isUsingXShm())
  889. X11Symbols::getInstance()->xShmPutImage (display, (::Drawable) window, gc, xImage.get(), sx, sy, dx, dy, dw, dh, True);
  890. else
  891. #endif
  892. X11Symbols::getInstance()->xPutImage (display, (::Drawable) window, gc, xImage.get(), sx, sy, dx, dy, dw, dh);
  893. }
  894. #if JUCE_USE_XSHM
  895. bool isUsingXShm() const noexcept { return usingXShm; }
  896. #endif
  897. private:
  898. //==============================================================================
  899. struct Deleter
  900. {
  901. void operator() (XImage* img) const noexcept
  902. {
  903. X11Symbols::getInstance()->xDestroyImage (img);
  904. }
  905. };
  906. std::unique_ptr<XImage, Deleter> xImage;
  907. const unsigned int imageDepth;
  908. HeapBlock<uint8> imageDataAllocated;
  909. HeapBlock<char> imageData16Bit;
  910. int pixelStride, lineStride;
  911. uint8* imageData = nullptr;
  912. GC gc = None;
  913. ::Display* display = XWindowSystem::getInstance()->getDisplay();
  914. #if JUCE_USE_XSHM
  915. XShmSegmentInfo segmentInfo;
  916. bool usingXShm;
  917. #endif
  918. static int getShiftNeeded (const uint32 mask) noexcept
  919. {
  920. for (int i = 32; --i >= 0;)
  921. if (((mask >> i) & 1) != 0)
  922. return i - 7;
  923. jassertfalse;
  924. return 0;
  925. }
  926. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (XBitmapImage)
  927. };
  928. //=============================== X11 - Displays ===============================
  929. namespace DisplayHelpers
  930. {
  931. static double getDisplayDPI (::Display* display, int index)
  932. {
  933. auto widthMM = X11Symbols::getInstance()->xDisplayWidthMM (display, index);
  934. auto heightMM = X11Symbols::getInstance()->xDisplayHeightMM (display, index);
  935. if (widthMM > 0 && heightMM > 0)
  936. return (((X11Symbols::getInstance()->xDisplayWidth (display, index) * 25.4) / widthMM)
  937. + ((X11Symbols::getInstance()->xDisplayHeight (display, index) * 25.4) / heightMM)) / 2.0;
  938. return 96.0;
  939. }
  940. static double getDisplayScale (const String& name, double dpi)
  941. {
  942. if (auto* xSettings = XWindowSystem::getInstance()->getXSettings())
  943. {
  944. auto windowScalingFactorSetting = xSettings->getSetting (XWindowSystem::getWindowScalingFactorSettingName());
  945. if (windowScalingFactorSetting.isValid()
  946. && windowScalingFactorSetting.integerValue > 0)
  947. {
  948. return (double) windowScalingFactorSetting.integerValue;
  949. }
  950. }
  951. if (name.isNotEmpty())
  952. {
  953. // Ubuntu and derived distributions now save a per-display scale factor as a configuration
  954. // variable. This can be changed in the Monitor system settings panel.
  955. ChildProcess dconf;
  956. if (File ("/usr/bin/dconf").existsAsFile()
  957. && dconf.start ("/usr/bin/dconf read /com/ubuntu/user-interface/scale-factor", ChildProcess::wantStdOut))
  958. {
  959. if (dconf.waitForProcessToFinish (200))
  960. {
  961. auto jsonOutput = dconf.readAllProcessOutput().replaceCharacter ('\'', '"');
  962. if (dconf.getExitCode() == 0 && jsonOutput.isNotEmpty())
  963. {
  964. auto jsonVar = JSON::parse (jsonOutput);
  965. if (auto* object = jsonVar.getDynamicObject())
  966. {
  967. auto scaleFactorVar = object->getProperty (name);
  968. if (! scaleFactorVar.isVoid())
  969. {
  970. auto scaleFactor = ((double) scaleFactorVar) / 8.0;
  971. if (scaleFactor > 0.0)
  972. return scaleFactor;
  973. }
  974. }
  975. }
  976. }
  977. }
  978. }
  979. {
  980. // Other gnome based distros now use gsettings for a global scale factor
  981. ChildProcess gsettings;
  982. if (File ("/usr/bin/gsettings").existsAsFile()
  983. && gsettings.start ("/usr/bin/gsettings get org.gnome.desktop.interface scaling-factor", ChildProcess::wantStdOut))
  984. {
  985. if (gsettings.waitForProcessToFinish (200))
  986. {
  987. auto gsettingsOutput = StringArray::fromTokens (gsettings.readAllProcessOutput(), true);
  988. if (gsettingsOutput.size() >= 2 && gsettingsOutput[1].length() > 0)
  989. {
  990. auto scaleFactor = gsettingsOutput[1].getDoubleValue();
  991. if (scaleFactor > 0.0)
  992. return scaleFactor;
  993. return 1.0;
  994. }
  995. }
  996. }
  997. }
  998. // If no scale factor is set by GNOME or Ubuntu then calculate from monitor dpi
  999. // We use the same approach as chromium which simply divides the dpi by 96
  1000. // and then rounds the result
  1001. return round (dpi / 96.0);
  1002. }
  1003. #if JUCE_USE_XINERAMA
  1004. static Array<XineramaScreenInfo> xineramaQueryDisplays (::Display* display)
  1005. {
  1006. int major_opcode, first_event, first_error;
  1007. if (X11Symbols::getInstance()->xQueryExtension (display, "XINERAMA", &major_opcode, &first_event, &first_error)
  1008. && (X11Symbols::getInstance()->xineramaIsActive (display) != 0))
  1009. {
  1010. int numScreens;
  1011. if (auto xinfo = makeXFreePtr (X11Symbols::getInstance()->xineramaQueryScreens (display, &numScreens)))
  1012. return { xinfo.get(), numScreens };
  1013. }
  1014. return {};
  1015. }
  1016. #endif
  1017. }
  1018. //=============================== X11 - Pixmap =================================
  1019. namespace PixmapHelpers
  1020. {
  1021. static Pixmap createColourPixmapFromImage (::Display* display, const Image& image)
  1022. {
  1023. XWindowSystemUtilities::ScopedXLock xLock;
  1024. auto width = (unsigned int) image.getWidth();
  1025. auto height = (unsigned int) image.getHeight();
  1026. HeapBlock<uint32> colour (width * height);
  1027. int index = 0;
  1028. for (int y = 0; y < (int) height; ++y)
  1029. for (int x = 0; x < (int) width; ++x)
  1030. colour[index++] = image.getPixelAt (x, y).getARGB();
  1031. auto ximage = makeXFreePtr (X11Symbols::getInstance()->xCreateImage (display, (Visual*) CopyFromParent, 24, ZPixmap,
  1032. 0, reinterpret_cast<const char*> (colour.getData()),
  1033. width, height, 32, 0));
  1034. auto pixmap = X11Symbols::getInstance()->xCreatePixmap (display,
  1035. X11Symbols::getInstance()->xDefaultRootWindow (display),
  1036. width, height, 24);
  1037. XValueHolder<GC> gc (X11Symbols::getInstance()->xCreateGC (display, pixmap, 0, nullptr),
  1038. [&display] (GC& g) { X11Symbols::getInstance()->xFreeGC (display, g); });
  1039. X11Symbols::getInstance()->xPutImage (display, pixmap, gc.value, ximage.get(), 0, 0, 0, 0, width, height);
  1040. return pixmap;
  1041. }
  1042. static Pixmap createMaskPixmapFromImage (::Display* display, const Image& image)
  1043. {
  1044. XWindowSystemUtilities::ScopedXLock xLock;
  1045. auto width = (unsigned int) image.getWidth();
  1046. auto height = (unsigned int) image.getHeight();
  1047. auto stride = (width + 7) >> 3;
  1048. HeapBlock<char> mask;
  1049. mask.calloc (stride * height);
  1050. auto msbfirst = (X11Symbols::getInstance()->xBitmapBitOrder (display) == MSBFirst);
  1051. for (unsigned int y = 0; y < height; ++y)
  1052. {
  1053. for (unsigned int x = 0; x < width; ++x)
  1054. {
  1055. auto bit = (char) (1 << (msbfirst ? (7 - (x & 7)) : (x & 7)));
  1056. auto offset = y * stride + (x >> 3);
  1057. if (image.getPixelAt ((int) x, (int) y).getAlpha() >= 128)
  1058. mask[offset] |= bit;
  1059. }
  1060. }
  1061. return X11Symbols::getInstance()->xCreatePixmapFromBitmapData (display, X11Symbols::getInstance()->xDefaultRootWindow (display),
  1062. mask.getData(), width, height, 1, 0, 1);
  1063. }
  1064. }
  1065. //=============================== X11 - Clipboard ==============================
  1066. namespace ClipboardHelpers
  1067. {
  1068. //==============================================================================
  1069. // Read the content of a window property as either a locale-dependent string or an utf8 string
  1070. // works only for strings shorter than 1000000 bytes
  1071. static String readWindowProperty (::Display* display, Window window, Atom atom)
  1072. {
  1073. if (display != nullptr)
  1074. {
  1075. XWindowSystemUtilities::GetXProperty prop (display, window, atom, 0L, 100000, false, AnyPropertyType);
  1076. if (prop.success)
  1077. {
  1078. if (prop.actualType == XWindowSystem::getInstance()->getAtoms().utf8String && prop.actualFormat == 8)
  1079. return String::fromUTF8 ((const char*) prop.data, (int) prop.numItems);
  1080. if (prop.actualType == XA_STRING && prop.actualFormat == 8)
  1081. return String ((const char*) prop.data, prop.numItems);
  1082. }
  1083. }
  1084. return {};
  1085. }
  1086. //==============================================================================
  1087. // Send a SelectionRequest to the window owning the selection and waits for its answer (with a timeout) */
  1088. static bool requestSelectionContent (::Display* display, String& selectionContent, Atom selection, Atom requestedFormat)
  1089. {
  1090. auto property_name = X11Symbols::getInstance()->xInternAtom (display, "JUCE_SEL", false);
  1091. // The selection owner will be asked to set the JUCE_SEL property on the
  1092. // juce_messageWindowHandle with the selection content
  1093. X11Symbols::getInstance()->xConvertSelection (display, selection, requestedFormat, property_name,
  1094. juce_messageWindowHandle, CurrentTime);
  1095. int count = 50; // will wait at most for 200 ms
  1096. while (--count >= 0)
  1097. {
  1098. XEvent event;
  1099. if (X11Symbols::getInstance()->xCheckTypedWindowEvent (display, juce_messageWindowHandle, SelectionNotify, &event))
  1100. {
  1101. if (event.xselection.property == property_name)
  1102. {
  1103. jassert (event.xselection.requestor == juce_messageWindowHandle);
  1104. selectionContent = readWindowProperty (display, event.xselection.requestor, event.xselection.property);
  1105. return true;
  1106. }
  1107. return false; // the format we asked for was denied.. (event.xselection.property == None)
  1108. }
  1109. // not very elegant.. we could do a select() or something like that...
  1110. // however clipboard content requesting is inherently slow on x11, it
  1111. // often takes 50ms or more so...
  1112. Thread::sleep (4);
  1113. }
  1114. return false;
  1115. }
  1116. //==============================================================================
  1117. // Called from the event loop in juce_linux_Messaging in response to SelectionRequest events
  1118. static void handleSelection (XSelectionRequestEvent& evt)
  1119. {
  1120. // the selection content is sent to the target window as a window property
  1121. XSelectionEvent reply;
  1122. reply.type = SelectionNotify;
  1123. reply.display = evt.display;
  1124. reply.requestor = evt.requestor;
  1125. reply.selection = evt.selection;
  1126. reply.target = evt.target;
  1127. reply.property = None; // == "fail"
  1128. reply.time = evt.time;
  1129. HeapBlock<char> data;
  1130. int propertyFormat = 0;
  1131. size_t numDataItems = 0;
  1132. const auto& atoms = XWindowSystem::getInstance()->getAtoms();
  1133. if (evt.selection == XA_PRIMARY || evt.selection == atoms.clipboard)
  1134. {
  1135. if (evt.target == XA_STRING || evt.target == atoms.utf8String)
  1136. {
  1137. auto localContent = XWindowSystem::getInstance()->getLocalClipboardContent();
  1138. // Translate to utf8
  1139. numDataItems = localContent.getNumBytesAsUTF8();
  1140. auto numBytesRequiredToStore = numDataItems + 1;
  1141. data.calloc (numBytesRequiredToStore);
  1142. localContent.copyToUTF8 (data, numBytesRequiredToStore);
  1143. propertyFormat = 8; // bits per item
  1144. }
  1145. else if (evt.target == atoms.targets)
  1146. {
  1147. // Another application wants to know what we are able to send
  1148. numDataItems = 2;
  1149. data.calloc (numDataItems * sizeof (Atom));
  1150. // Atoms are flagged as 32-bit irrespective of sizeof (Atom)
  1151. propertyFormat = 32;
  1152. auto* dataAtoms = unalignedPointerCast<Atom*> (data.getData());
  1153. dataAtoms[0] = atoms.utf8String;
  1154. dataAtoms[1] = XA_STRING;
  1155. evt.target = XA_ATOM;
  1156. }
  1157. }
  1158. else
  1159. {
  1160. DBG ("requested unsupported clipboard");
  1161. }
  1162. if (data != nullptr)
  1163. {
  1164. const size_t maxReasonableSelectionSize = 1000000;
  1165. // for very big chunks of data, we should use the "INCR" protocol , which is a pain in the *ss
  1166. if (evt.property != None && numDataItems < maxReasonableSelectionSize)
  1167. {
  1168. X11Symbols::getInstance()->xChangeProperty (evt.display, evt.requestor,
  1169. evt.property, evt.target,
  1170. propertyFormat, PropModeReplace,
  1171. reinterpret_cast<const unsigned char*> (data.getData()), (int) numDataItems);
  1172. reply.property = evt.property; // " == success"
  1173. }
  1174. }
  1175. X11Symbols::getInstance()->xSendEvent (evt.display, evt.requestor, 0, NoEventMask, (XEvent*) &reply);
  1176. }
  1177. }
  1178. //==============================================================================
  1179. ComponentPeer* getPeerFor (::Window windowH)
  1180. {
  1181. if (windowH == 0)
  1182. return nullptr;
  1183. XPointer peer = nullptr;
  1184. if (auto* display = XWindowSystem::getInstance()->getDisplay())
  1185. {
  1186. XWindowSystemUtilities::ScopedXLock xLock;
  1187. X11Symbols::getInstance()->xFindContext (display, (XID) windowH, windowHandleXContext, &peer);
  1188. }
  1189. return unalignedPointerCast<ComponentPeer*> (peer);
  1190. }
  1191. //==============================================================================
  1192. static std::unordered_map<LinuxComponentPeer*, X11DragState> dragAndDropStateMap;
  1193. XWindowSystem::XWindowSystem()
  1194. {
  1195. xIsAvailable = X11Symbols::getInstance()->loadAllSymbols();
  1196. if (! xIsAvailable)
  1197. return;
  1198. if (JUCEApplicationBase::isStandaloneApp())
  1199. {
  1200. // Initialise xlib for multiple thread support
  1201. static bool initThreadCalled = false;
  1202. if (! initThreadCalled)
  1203. {
  1204. if (! X11Symbols::getInstance()->xInitThreads())
  1205. {
  1206. // This is fatal! Print error and closedown
  1207. Logger::outputDebugString ("Failed to initialise xlib thread support.");
  1208. Process::terminate();
  1209. return;
  1210. }
  1211. initThreadCalled = true;
  1212. }
  1213. X11ErrorHandling::installXErrorHandlers();
  1214. }
  1215. if (! initialiseXDisplay())
  1216. {
  1217. if (JUCEApplicationBase::isStandaloneApp())
  1218. X11ErrorHandling::removeXErrorHandlers();
  1219. X11Symbols::deleteInstance();
  1220. xIsAvailable = false;
  1221. }
  1222. }
  1223. XWindowSystem::~XWindowSystem()
  1224. {
  1225. if (xIsAvailable)
  1226. {
  1227. destroyXDisplay();
  1228. if (JUCEApplicationBase::isStandaloneApp())
  1229. X11ErrorHandling::removeXErrorHandlers();
  1230. }
  1231. X11Symbols::deleteInstance();
  1232. clearSingletonInstance();
  1233. }
  1234. //==============================================================================
  1235. static int getAllEventsMask (bool ignoresMouseClicks)
  1236. {
  1237. return NoEventMask | KeyPressMask | KeyReleaseMask
  1238. | EnterWindowMask | LeaveWindowMask | PointerMotionMask | KeymapStateMask
  1239. | ExposureMask | StructureNotifyMask | FocusChangeMask | PropertyChangeMask
  1240. | (ignoresMouseClicks ? 0 : (ButtonPressMask | ButtonReleaseMask));
  1241. }
  1242. ::Window XWindowSystem::createWindow (::Window parentToAddTo, LinuxComponentPeer* peer) const
  1243. {
  1244. if (! xIsAvailable)
  1245. {
  1246. // can't open a window on a system that doesn't have X11 installed!
  1247. jassertfalse;
  1248. return 0;
  1249. }
  1250. auto styleFlags = peer->getStyleFlags();
  1251. XWindowSystemUtilities::ScopedXLock xLock;
  1252. auto root = X11Symbols::getInstance()->xRootWindow (display, X11Symbols::getInstance()->xDefaultScreen (display));
  1253. auto visualAndDepth = displayVisuals->getBestVisualForWindow ((styleFlags & ComponentPeer::windowIsSemiTransparent) != 0);
  1254. auto colormap = X11Symbols::getInstance()->xCreateColormap (display, root, visualAndDepth.visual, AllocNone);
  1255. X11Symbols::getInstance()->xInstallColormap (display, colormap);
  1256. // Set up the window attributes
  1257. XSetWindowAttributes swa;
  1258. swa.border_pixel = 0;
  1259. swa.background_pixmap = None;
  1260. swa.colormap = colormap;
  1261. swa.override_redirect = ((styleFlags & ComponentPeer::windowIsTemporary) != 0) ? True : False;
  1262. swa.event_mask = getAllEventsMask (styleFlags & ComponentPeer::windowIgnoresMouseClicks);
  1263. auto windowH = X11Symbols::getInstance()->xCreateWindow (display, parentToAddTo != 0 ? parentToAddTo : root,
  1264. 0, 0, 1, 1,
  1265. 0, visualAndDepth.depth, InputOutput, visualAndDepth.visual,
  1266. CWBorderPixel | CWColormap | CWBackPixmap | CWEventMask | CWOverrideRedirect,
  1267. &swa);
  1268. // Set the window context to identify the window handle object
  1269. if (X11Symbols::getInstance()->xSaveContext (display, (XID) windowH, windowHandleXContext, (XPointer) peer))
  1270. {
  1271. // Failed
  1272. jassertfalse;
  1273. Logger::outputDebugString ("Failed to create context information for window.\n");
  1274. X11Symbols::getInstance()->xDestroyWindow (display, windowH);
  1275. return 0;
  1276. }
  1277. // Set window manager hints
  1278. if (auto wmHints = makeXFreePtr (X11Symbols::getInstance()->xAllocWMHints()))
  1279. {
  1280. wmHints->flags = InputHint | StateHint;
  1281. wmHints->input = True;
  1282. wmHints->initial_state = NormalState;
  1283. X11Symbols::getInstance()->xSetWMHints (display, windowH, wmHints.get());
  1284. }
  1285. // Set class hint
  1286. if (auto* app = JUCEApplicationBase::getInstance())
  1287. {
  1288. if (auto classHint = makeXFreePtr (X11Symbols::getInstance()->xAllocClassHint()))
  1289. {
  1290. auto appName = app->getApplicationName();
  1291. classHint->res_name = (char*) appName.getCharPointer().getAddress();
  1292. classHint->res_class = (char*) appName.getCharPointer().getAddress();
  1293. X11Symbols::getInstance()->xSetClassHint (display, windowH, classHint.get());
  1294. }
  1295. }
  1296. // Set the window type
  1297. setWindowType (windowH, styleFlags);
  1298. // Define decoration
  1299. if ((styleFlags & ComponentPeer::windowHasTitleBar) == 0)
  1300. removeWindowDecorations (windowH);
  1301. else
  1302. addWindowButtons (windowH, styleFlags);
  1303. // Associate the PID, allowing to be shut down when something goes wrong
  1304. auto pid = (unsigned long) getpid();
  1305. xchangeProperty (windowH, atoms.pid, XA_CARDINAL, 32, &pid, 1);
  1306. // Set window manager protocols
  1307. xchangeProperty (windowH, atoms.protocols, XA_ATOM, 32, atoms.protocolList, 2);
  1308. // Set drag and drop flags
  1309. xchangeProperty (windowH, atoms.XdndTypeList, XA_ATOM, 32, atoms.allowedMimeTypes, numElementsInArray (atoms.allowedMimeTypes));
  1310. xchangeProperty (windowH, atoms.XdndActionList, XA_ATOM, 32, atoms.allowedActions, numElementsInArray (atoms.allowedActions));
  1311. xchangeProperty (windowH, atoms.XdndActionDescription, XA_STRING, 8, "", 0);
  1312. auto dndVersion = XWindowSystemUtilities::Atoms::DndVersion;
  1313. xchangeProperty (windowH, atoms.XdndAware, XA_ATOM, 32, &dndVersion, 1);
  1314. unsigned long info[2] = { 0, 1 };
  1315. xchangeProperty (windowH, atoms.XembedInfo, atoms.XembedInfo, 32, (unsigned char*) info, 2);
  1316. return windowH;
  1317. }
  1318. void XWindowSystem::destroyWindow (::Window windowH)
  1319. {
  1320. auto* peer = dynamic_cast<LinuxComponentPeer*> (getPeerFor (windowH));
  1321. if (peer == nullptr)
  1322. {
  1323. jassertfalse;
  1324. return;
  1325. }
  1326. #if JUCE_X11_SUPPORTS_XEMBED
  1327. juce_handleXEmbedEvent (peer, nullptr);
  1328. #endif
  1329. deleteIconPixmaps (windowH);
  1330. dragAndDropStateMap.erase (peer);
  1331. XWindowSystemUtilities::ScopedXLock xLock;
  1332. XPointer handlePointer;
  1333. if (! X11Symbols::getInstance()->xFindContext (display, (XID) windowH, windowHandleXContext, &handlePointer))
  1334. X11Symbols::getInstance()->xDeleteContext (display, (XID) windowH, windowHandleXContext);
  1335. X11Symbols::getInstance()->xDestroyWindow (display, windowH);
  1336. // Wait for it to complete and then remove any events for this
  1337. // window from the event queue.
  1338. X11Symbols::getInstance()->xSync (display, false);
  1339. XEvent event;
  1340. while (X11Symbols::getInstance()->xCheckWindowEvent (display, windowH,
  1341. getAllEventsMask (peer->getStyleFlags() & ComponentPeer::windowIgnoresMouseClicks),
  1342. &event) == True)
  1343. {}
  1344. #if JUCE_USE_XSHM
  1345. if (XSHMHelpers::isShmAvailable (display))
  1346. shmPaintsPendingMap.erase (windowH);
  1347. #endif
  1348. }
  1349. //==============================================================================
  1350. void XWindowSystem::setTitle (::Window windowH, const String& title) const
  1351. {
  1352. jassert (windowH != 0);
  1353. XTextProperty nameProperty{};
  1354. char* strings[] = { const_cast<char*> (title.toRawUTF8()) };
  1355. XWindowSystemUtilities::ScopedXLock xLock;
  1356. if (X11Symbols::getInstance()->xutf8TextListToTextProperty (display,
  1357. strings,
  1358. numElementsInArray (strings),
  1359. XUTF8StringStyle,
  1360. &nameProperty) >= 0)
  1361. {
  1362. X11Symbols::getInstance()->xSetWMName (display, windowH, &nameProperty);
  1363. X11Symbols::getInstance()->xSetWMIconName (display, windowH, &nameProperty);
  1364. X11Symbols::getInstance()->xFree (nameProperty.value);
  1365. }
  1366. }
  1367. void XWindowSystem::setIcon (::Window windowH, const Image& newIcon) const
  1368. {
  1369. jassert (windowH != 0);
  1370. auto dataSize = newIcon.getWidth() * newIcon.getHeight() + 2;
  1371. HeapBlock<unsigned long> data (dataSize);
  1372. int index = 0;
  1373. data[index++] = (unsigned long) newIcon.getWidth();
  1374. data[index++] = (unsigned long) newIcon.getHeight();
  1375. for (int y = 0; y < newIcon.getHeight(); ++y)
  1376. for (int x = 0; x < newIcon.getWidth(); ++x)
  1377. data[index++] = (unsigned long) newIcon.getPixelAt (x, y).getARGB();
  1378. XWindowSystemUtilities::ScopedXLock xLock;
  1379. xchangeProperty (windowH, XWindowSystemUtilities::Atoms::getCreating (display, "_NET_WM_ICON"),
  1380. XA_CARDINAL, 32, data.getData(), dataSize);
  1381. deleteIconPixmaps (windowH);
  1382. auto wmHints = makeXFreePtr (X11Symbols::getInstance()->xGetWMHints (display, windowH));
  1383. if (wmHints == nullptr)
  1384. wmHints = makeXFreePtr (X11Symbols::getInstance()->xAllocWMHints());
  1385. if (wmHints != nullptr)
  1386. {
  1387. wmHints->flags |= IconPixmapHint | IconMaskHint;
  1388. wmHints->icon_pixmap = PixmapHelpers::createColourPixmapFromImage (display, newIcon);
  1389. wmHints->icon_mask = PixmapHelpers::createMaskPixmapFromImage (display, newIcon);
  1390. X11Symbols::getInstance()->xSetWMHints (display, windowH, wmHints.get());
  1391. }
  1392. X11Symbols::getInstance()->xSync (display, False);
  1393. }
  1394. void XWindowSystem::setVisible (::Window windowH, bool shouldBeVisible) const
  1395. {
  1396. jassert (windowH != 0);
  1397. XWindowSystemUtilities::ScopedXLock xLock;
  1398. if (shouldBeVisible)
  1399. X11Symbols::getInstance()->xMapWindow (display, windowH);
  1400. else
  1401. X11Symbols::getInstance()->xUnmapWindow (display, windowH);
  1402. }
  1403. void XWindowSystem::setBounds (::Window windowH, Rectangle<int> newBounds, bool isFullScreen) const
  1404. {
  1405. jassert (windowH != 0);
  1406. if (auto* peer = getPeerFor (windowH))
  1407. {
  1408. if (peer->isFullScreen() && ! isFullScreen)
  1409. {
  1410. // When transitioning back from fullscreen, we might need to remove
  1411. // the FULLSCREEN window property
  1412. Atom fs = XWindowSystemUtilities::Atoms::getIfExists (display, "_NET_WM_STATE_FULLSCREEN");
  1413. if (fs != None)
  1414. {
  1415. auto root = X11Symbols::getInstance()->xRootWindow (display, X11Symbols::getInstance()->xDefaultScreen (display));
  1416. XClientMessageEvent clientMsg;
  1417. clientMsg.display = display;
  1418. clientMsg.window = windowH;
  1419. clientMsg.type = ClientMessage;
  1420. clientMsg.format = 32;
  1421. clientMsg.message_type = atoms.windowState;
  1422. clientMsg.data.l[0] = 0; // Remove
  1423. clientMsg.data.l[1] = (long) fs;
  1424. clientMsg.data.l[2] = 0;
  1425. clientMsg.data.l[3] = 1; // Normal Source
  1426. XWindowSystemUtilities::ScopedXLock xLock;
  1427. X11Symbols::getInstance()->xSendEvent (display, root, false,
  1428. SubstructureRedirectMask | SubstructureNotifyMask,
  1429. (XEvent*) &clientMsg);
  1430. }
  1431. }
  1432. updateConstraints (windowH, *peer);
  1433. XWindowSystemUtilities::ScopedXLock xLock;
  1434. if (auto hints = makeXFreePtr (X11Symbols::getInstance()->xAllocSizeHints()))
  1435. {
  1436. hints->flags = USSize | USPosition;
  1437. hints->x = newBounds.getX();
  1438. hints->y = newBounds.getY();
  1439. hints->width = newBounds.getWidth();
  1440. hints->height = newBounds.getHeight();
  1441. X11Symbols::getInstance()->xSetWMNormalHints (display, windowH, hints.get());
  1442. }
  1443. const auto nativeWindowBorder = [&]() -> BorderSize<int>
  1444. {
  1445. if (const auto& frameSize = peer->getFrameSizeIfPresent())
  1446. return frameSize->multipliedBy (peer->getPlatformScaleFactor());
  1447. return {};
  1448. }();
  1449. X11Symbols::getInstance()->xMoveResizeWindow (display, windowH,
  1450. newBounds.getX() - nativeWindowBorder.getLeft(),
  1451. newBounds.getY() - nativeWindowBorder.getTop(),
  1452. (unsigned int) newBounds.getWidth(),
  1453. (unsigned int) newBounds.getHeight());
  1454. }
  1455. }
  1456. void XWindowSystem::updateConstraints (::Window windowH) const
  1457. {
  1458. if (auto* peer = getPeerFor (windowH))
  1459. updateConstraints (windowH, *peer);
  1460. }
  1461. void XWindowSystem::updateConstraints (::Window windowH, ComponentPeer& peer) const
  1462. {
  1463. XWindowSystemUtilities::ScopedXLock xLock;
  1464. if (auto hints = makeXFreePtr (X11Symbols::getInstance()->xAllocSizeHints()))
  1465. {
  1466. if ((peer.getStyleFlags() & ComponentPeer::windowIsResizable) == 0)
  1467. {
  1468. hints->min_width = hints->max_width = peer.getBounds().getWidth();
  1469. hints->min_height = hints->max_height = peer.getBounds().getHeight();
  1470. hints->flags = PMinSize | PMaxSize;
  1471. }
  1472. else if (auto* c = peer.getConstrainer())
  1473. {
  1474. const auto windowBorder = [&]() -> BorderSize<int>
  1475. {
  1476. if (const auto& frameSize = peer.getFrameSizeIfPresent())
  1477. return *frameSize;
  1478. return {};
  1479. }();
  1480. const auto factor = peer.getPlatformScaleFactor();
  1481. const auto leftAndRight = windowBorder.getLeftAndRight();
  1482. const auto topAndBottom = windowBorder.getTopAndBottom();
  1483. hints->min_width = jmax (1, (int) (factor * c->getMinimumWidth()) - leftAndRight);
  1484. hints->max_width = jmax (1, (int) (factor * c->getMaximumWidth()) - leftAndRight);
  1485. hints->min_height = jmax (1, (int) (factor * c->getMinimumHeight()) - topAndBottom);
  1486. hints->max_height = jmax (1, (int) (factor * c->getMaximumHeight()) - topAndBottom);
  1487. hints->flags = PMinSize | PMaxSize;
  1488. }
  1489. X11Symbols::getInstance()->xSetWMNormalHints (display, windowH, hints.get());
  1490. }
  1491. }
  1492. bool XWindowSystem::contains (::Window windowH, Point<int> localPos) const
  1493. {
  1494. ::Window root, child;
  1495. int wx, wy;
  1496. unsigned int ww, wh, bw, bitDepth;
  1497. XWindowSystemUtilities::ScopedXLock xLock;
  1498. return X11Symbols::getInstance()->xGetGeometry (display, (::Drawable) windowH, &root, &wx, &wy, &ww, &wh, &bw, &bitDepth)
  1499. && X11Symbols::getInstance()->xTranslateCoordinates (display, windowH, windowH, localPos.getX(), localPos.getY(), &wx, &wy, &child)
  1500. && child == None;
  1501. }
  1502. ComponentPeer::OptionalBorderSize XWindowSystem::getBorderSize (::Window windowH) const
  1503. {
  1504. jassert (windowH != 0);
  1505. XWindowSystemUtilities::ScopedXLock xLock;
  1506. auto hints = XWindowSystemUtilities::Atoms::getIfExists (display, "_NET_FRAME_EXTENTS");
  1507. if (hints != None)
  1508. {
  1509. XWindowSystemUtilities::GetXProperty prop (display, windowH, hints, 0, 4, false, XA_CARDINAL);
  1510. if (prop.success && prop.actualFormat == 32)
  1511. {
  1512. auto data = prop.data;
  1513. std::array<unsigned long, 4> sizes;
  1514. for (auto& size : sizes)
  1515. {
  1516. memcpy (&size, data, sizeof (unsigned long));
  1517. data += sizeof (unsigned long);
  1518. }
  1519. return ComponentPeer::OptionalBorderSize ({ (int) sizes[2], (int) sizes[0], (int) sizes[3], (int) sizes[1] });
  1520. }
  1521. }
  1522. return {};
  1523. }
  1524. Rectangle<int> XWindowSystem::getWindowBounds (::Window windowH, ::Window parentWindow)
  1525. {
  1526. jassert (windowH != 0);
  1527. Window root, child;
  1528. int wx = 0, wy = 0;
  1529. unsigned int ww = 0, wh = 0, bw, bitDepth;
  1530. XWindowSystemUtilities::ScopedXLock xLock;
  1531. if (X11Symbols::getInstance()->xGetGeometry (display, (::Drawable) windowH, &root, &wx, &wy, &ww, &wh, &bw, &bitDepth))
  1532. {
  1533. int rootX = 0, rootY = 0;
  1534. if (! X11Symbols::getInstance()->xTranslateCoordinates (display, windowH, root, 0, 0, &rootX, &rootY, &child))
  1535. rootX = rootY = 0;
  1536. if (parentWindow == 0)
  1537. {
  1538. wx = rootX;
  1539. wy = rootY;
  1540. }
  1541. else
  1542. {
  1543. // XGetGeometry returns wx and wy relative to the parent window's origin.
  1544. // XTranslateCoordinates returns rootX and rootY relative to the root window.
  1545. parentScreenPosition = Point<int> (rootX - wx, rootY - wy);
  1546. }
  1547. }
  1548. return { wx, wy, (int) ww, (int) wh };
  1549. }
  1550. Point<int> XWindowSystem::getPhysicalParentScreenPosition() const
  1551. {
  1552. return parentScreenPosition;
  1553. }
  1554. void XWindowSystem::setMinimised (::Window windowH, bool shouldBeMinimised) const
  1555. {
  1556. jassert (windowH != 0);
  1557. if (shouldBeMinimised)
  1558. {
  1559. auto root = X11Symbols::getInstance()->xRootWindow (display, X11Symbols::getInstance()->xDefaultScreen (display));
  1560. XClientMessageEvent clientMsg;
  1561. clientMsg.display = display;
  1562. clientMsg.window = windowH;
  1563. clientMsg.type = ClientMessage;
  1564. clientMsg.format = 32;
  1565. clientMsg.message_type = atoms.changeState;
  1566. clientMsg.data.l[0] = IconicState;
  1567. XWindowSystemUtilities::ScopedXLock xLock;
  1568. X11Symbols::getInstance()->xSendEvent (display, root, false, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent*) &clientMsg);
  1569. }
  1570. }
  1571. bool XWindowSystem::isMinimised (::Window windowH) const
  1572. {
  1573. jassert (windowH != 0);
  1574. XWindowSystemUtilities::ScopedXLock xLock;
  1575. XWindowSystemUtilities::GetXProperty prop (display, windowH, atoms.state, 0, 64, false, atoms.state);
  1576. if (prop.success && prop.actualType == atoms.state
  1577. && prop.actualFormat == 32 && prop.numItems > 0)
  1578. {
  1579. unsigned long state;
  1580. memcpy (&state, prop.data, sizeof (unsigned long));
  1581. return state == IconicState;
  1582. }
  1583. return false;
  1584. }
  1585. void XWindowSystem::setMaximised (::Window windowH, bool shouldBeMaximised) const
  1586. {
  1587. const auto root = X11Symbols::getInstance()->xRootWindow (display, X11Symbols::getInstance()->xDefaultScreen (display));
  1588. XEvent ev;
  1589. ev.xclient.window = windowH;
  1590. ev.xclient.type = ClientMessage;
  1591. ev.xclient.format = 32;
  1592. ev.xclient.message_type = XWindowSystemUtilities::Atoms::getCreating (display, "_NET_WM_STATE");
  1593. ev.xclient.data.l[0] = shouldBeMaximised;
  1594. ev.xclient.data.l[1] = (long) XWindowSystemUtilities::Atoms::getCreating (display, "_NET_WM_STATE_MAXIMIZED_HORZ");
  1595. ev.xclient.data.l[2] = (long) XWindowSystemUtilities::Atoms::getCreating (display, "_NET_WM_STATE_MAXIMIZED_VERT");
  1596. ev.xclient.data.l[3] = 1;
  1597. ev.xclient.data.l[4] = 0;
  1598. XWindowSystemUtilities::ScopedXLock xLock;
  1599. X11Symbols::getInstance()->xSendEvent (display, root, false, SubstructureRedirectMask | SubstructureNotifyMask, &ev);
  1600. }
  1601. void XWindowSystem::toFront (::Window windowH, bool) const
  1602. {
  1603. jassert (windowH != 0);
  1604. XWindowSystemUtilities::ScopedXLock xLock;
  1605. XEvent ev;
  1606. ev.xclient.type = ClientMessage;
  1607. ev.xclient.serial = 0;
  1608. ev.xclient.send_event = True;
  1609. ev.xclient.message_type = atoms.activeWin;
  1610. ev.xclient.window = windowH;
  1611. ev.xclient.format = 32;
  1612. ev.xclient.data.l[0] = 2;
  1613. ev.xclient.data.l[1] = getUserTime (windowH);
  1614. ev.xclient.data.l[2] = 0;
  1615. ev.xclient.data.l[3] = 0;
  1616. ev.xclient.data.l[4] = 0;
  1617. X11Symbols::getInstance()->xSendEvent (display, X11Symbols::getInstance()->xRootWindow (display, X11Symbols::getInstance()->xDefaultScreen (display)),
  1618. False, SubstructureRedirectMask | SubstructureNotifyMask, &ev);
  1619. X11Symbols::getInstance()->xSync (display, False);
  1620. }
  1621. void XWindowSystem::toBehind (::Window windowH, ::Window otherWindow) const
  1622. {
  1623. jassert (windowH != 0 && otherWindow != 0);
  1624. const auto topLevelA = findTopLevelWindowOf (windowH);
  1625. const auto topLevelB = findTopLevelWindowOf (otherWindow);
  1626. Window newStack[] = { topLevelA, topLevelB };
  1627. XWindowSystemUtilities::ScopedXLock xLock;
  1628. X11Symbols::getInstance()->xRestackWindows (display, newStack, numElementsInArray (newStack));
  1629. }
  1630. bool XWindowSystem::isFocused (::Window windowH) const
  1631. {
  1632. jassert (windowH != 0);
  1633. int revert = 0;
  1634. Window focusedWindow = 0;
  1635. XWindowSystemUtilities::ScopedXLock xLock;
  1636. X11Symbols::getInstance()->xGetInputFocus (display, &focusedWindow, &revert);
  1637. if (focusedWindow == PointerRoot)
  1638. return false;
  1639. return isParentWindowOf (windowH, focusedWindow);
  1640. }
  1641. ::Window XWindowSystem::getFocusWindow (::Window windowH) const
  1642. {
  1643. jassert (windowH != 0);
  1644. #if JUCE_X11_SUPPORTS_XEMBED
  1645. if (auto w = (::Window) juce_getCurrentFocusWindow (dynamic_cast<LinuxComponentPeer*> (getPeerFor (windowH))))
  1646. return w;
  1647. #endif
  1648. return windowH;
  1649. }
  1650. bool XWindowSystem::grabFocus (::Window windowH) const
  1651. {
  1652. jassert (windowH != 0);
  1653. XWindowAttributes atts;
  1654. XWindowSystemUtilities::ScopedXLock xLock;
  1655. if (windowH != 0
  1656. && X11Symbols::getInstance()->xGetWindowAttributes (display, windowH, &atts)
  1657. && atts.map_state == IsViewable
  1658. && ! isFocused (windowH))
  1659. {
  1660. X11Symbols::getInstance()->xSetInputFocus (display, getFocusWindow (windowH), RevertToParent, (::Time) getUserTime (windowH));
  1661. return true;
  1662. }
  1663. return false;
  1664. }
  1665. bool XWindowSystem::canUseSemiTransparentWindows() const
  1666. {
  1667. #if JUCE_USE_XRENDER
  1668. if (XRender::hasCompositingWindowManager (display))
  1669. {
  1670. int matchedDepth = 0, desiredDepth = 32;
  1671. return Visuals::findVisualFormat (display, desiredDepth, matchedDepth) != nullptr
  1672. && matchedDepth == desiredDepth;
  1673. }
  1674. #endif
  1675. return false;
  1676. }
  1677. bool XWindowSystem::canUseARGBImages() const
  1678. {
  1679. static bool canUseARGB = false;
  1680. #if JUCE_USE_XSHM
  1681. static bool checked = false;
  1682. if (! checked)
  1683. {
  1684. if (XSHMHelpers::isShmAvailable (display))
  1685. {
  1686. XWindowSystemUtilities::ScopedXLock xLock;
  1687. XShmSegmentInfo segmentinfo;
  1688. auto testImage = X11Symbols::getInstance()->xShmCreateImage (display,
  1689. X11Symbols::getInstance()->xDefaultVisual (display, X11Symbols::getInstance()->xDefaultScreen (display)),
  1690. 24, ZPixmap, nullptr, &segmentinfo, 64, 64);
  1691. canUseARGB = testImage != nullptr && testImage->bits_per_pixel == 32;
  1692. X11Symbols::getInstance()->xDestroyImage (testImage);
  1693. }
  1694. else
  1695. {
  1696. canUseARGB = false;
  1697. }
  1698. checked = true;
  1699. }
  1700. #endif
  1701. return canUseARGB;
  1702. }
  1703. bool XWindowSystem::isDarkModeActive() const
  1704. {
  1705. const auto themeName = [this]() -> String
  1706. {
  1707. if (xSettings != nullptr)
  1708. {
  1709. const auto themeNameSetting = xSettings->getSetting (getThemeNameSettingName());
  1710. if (themeNameSetting.isValid()
  1711. && themeNameSetting.stringValue.isNotEmpty())
  1712. {
  1713. return themeNameSetting.stringValue;
  1714. }
  1715. }
  1716. ChildProcess gsettings;
  1717. if (File ("/usr/bin/gsettings").existsAsFile()
  1718. && gsettings.start ("/usr/bin/gsettings get org.gnome.desktop.interface gtk-theme", ChildProcess::wantStdOut))
  1719. {
  1720. if (gsettings.waitForProcessToFinish (200))
  1721. return gsettings.readAllProcessOutput();
  1722. }
  1723. return {};
  1724. }();
  1725. return (themeName.isNotEmpty()
  1726. && (themeName.containsIgnoreCase ("dark") || themeName.containsIgnoreCase ("black")));
  1727. }
  1728. Image XWindowSystem::createImage (bool isSemiTransparent, int width, int height, bool argb) const
  1729. {
  1730. auto visualAndDepth = displayVisuals->getBestVisualForWindow (isSemiTransparent);
  1731. #if JUCE_USE_XSHM
  1732. return Image (new XBitmapImage (argb ? Image::ARGB : Image::RGB,
  1733. #else
  1734. return Image (new XBitmapImage (Image::RGB,
  1735. #endif
  1736. (width + 31) & ~31,
  1737. (height + 31) & ~31,
  1738. false, (unsigned int) visualAndDepth.depth, visualAndDepth.visual));
  1739. }
  1740. void XWindowSystem::blitToWindow (::Window windowH, Image image, Rectangle<int> destinationRect, Rectangle<int> totalRect) const
  1741. {
  1742. jassert (windowH != 0);
  1743. auto* xbitmap = static_cast<XBitmapImage*> (image.getPixelData());
  1744. xbitmap->blitToWindow (windowH,
  1745. destinationRect.getX(), destinationRect.getY(),
  1746. (unsigned int) destinationRect.getWidth(),
  1747. (unsigned int) destinationRect.getHeight(),
  1748. destinationRect.getX() - totalRect.getX(), destinationRect.getY() - totalRect.getY());
  1749. }
  1750. void XWindowSystem::processPendingPaintsForWindow (::Window windowH)
  1751. {
  1752. #if JUCE_USE_XSHM
  1753. if (! XSHMHelpers::isShmAvailable (display))
  1754. return;
  1755. if (getNumPaintsPendingForWindow (windowH) > 0)
  1756. {
  1757. XWindowSystemUtilities::ScopedXLock xLock;
  1758. XEvent evt;
  1759. while (X11Symbols::getInstance()->xCheckTypedWindowEvent (display, windowH, shmCompletionEvent, &evt))
  1760. removePendingPaintForWindow (windowH);
  1761. }
  1762. #endif
  1763. }
  1764. int XWindowSystem::getNumPaintsPendingForWindow (::Window windowH)
  1765. {
  1766. #if JUCE_USE_XSHM
  1767. if (XSHMHelpers::isShmAvailable (display))
  1768. return shmPaintsPendingMap[windowH];
  1769. #endif
  1770. return 0;
  1771. }
  1772. void XWindowSystem::addPendingPaintForWindow (::Window windowH)
  1773. {
  1774. #if JUCE_USE_XSHM
  1775. if (XSHMHelpers::isShmAvailable (display))
  1776. ++shmPaintsPendingMap[windowH];
  1777. #endif
  1778. }
  1779. void XWindowSystem::removePendingPaintForWindow (::Window windowH)
  1780. {
  1781. #if JUCE_USE_XSHM
  1782. if (XSHMHelpers::isShmAvailable (display))
  1783. --shmPaintsPendingMap[windowH];
  1784. #endif
  1785. }
  1786. void XWindowSystem::setScreenSaverEnabled (bool enabled) const
  1787. {
  1788. using tXScreenSaverSuspend = void (*) (Display*, Bool);
  1789. static tXScreenSaverSuspend xScreenSaverSuspend = nullptr;
  1790. if (xScreenSaverSuspend == nullptr)
  1791. if (void* h = dlopen ("libXss.so.1", RTLD_GLOBAL | RTLD_NOW))
  1792. xScreenSaverSuspend = (tXScreenSaverSuspend) dlsym (h, "XScreenSaverSuspend");
  1793. XWindowSystemUtilities::ScopedXLock xLock;
  1794. if (xScreenSaverSuspend != nullptr)
  1795. xScreenSaverSuspend (display, ! enabled);
  1796. }
  1797. Point<float> XWindowSystem::getCurrentMousePosition() const
  1798. {
  1799. Window root, child;
  1800. int x, y, winx, winy;
  1801. unsigned int mask;
  1802. XWindowSystemUtilities::ScopedXLock xLock;
  1803. if (X11Symbols::getInstance()->xQueryPointer (display,
  1804. X11Symbols::getInstance()->xRootWindow (display,
  1805. X11Symbols::getInstance()->xDefaultScreen (display)),
  1806. &root, &child,
  1807. &x, &y, &winx, &winy, &mask) == False)
  1808. {
  1809. x = y = -1;
  1810. }
  1811. return { (float) x, (float) y };
  1812. }
  1813. void XWindowSystem::setMousePosition (Point<float> pos) const
  1814. {
  1815. XWindowSystemUtilities::ScopedXLock xLock;
  1816. auto root = X11Symbols::getInstance()->xRootWindow (display,
  1817. X11Symbols::getInstance()->xDefaultScreen (display));
  1818. X11Symbols::getInstance()->xWarpPointer (display, None, root, 0, 0, 0, 0,
  1819. roundToInt (pos.getX()), roundToInt (pos.getY()));
  1820. }
  1821. Cursor XWindowSystem::createCustomMouseCursorInfo (const Image& image, Point<int> hotspot) const
  1822. {
  1823. if (display == nullptr)
  1824. return {};
  1825. XWindowSystemUtilities::ScopedXLock xLock;
  1826. auto imageW = (unsigned int) image.getWidth();
  1827. auto imageH = (unsigned int) image.getHeight();
  1828. auto hotspotX = hotspot.x;
  1829. auto hotspotY = hotspot.y;
  1830. #if JUCE_USE_XCURSOR
  1831. if (auto xcImage = makeDeletedPtr (X11Symbols::getInstance()->xcursorImageCreate ((int) imageW, (int) imageH),
  1832. [] (XcursorImage* i) { X11Symbols::getInstance()->xcursorImageDestroy (i); }))
  1833. {
  1834. xcImage->xhot = (XcursorDim) hotspotX;
  1835. xcImage->yhot = (XcursorDim) hotspotY;
  1836. auto* dest = xcImage->pixels;
  1837. for (int y = 0; y < (int) imageH; ++y)
  1838. for (int x = 0; x < (int) imageW; ++x)
  1839. *dest++ = image.getPixelAt (x, y).getARGB();
  1840. auto result = X11Symbols::getInstance()->xcursorImageLoadCursor (display, xcImage.get());
  1841. if (result != Cursor{})
  1842. return result;
  1843. }
  1844. #endif
  1845. auto root = X11Symbols::getInstance()->xRootWindow (display,
  1846. X11Symbols::getInstance()->xDefaultScreen (display));
  1847. unsigned int cursorW, cursorH;
  1848. if (! X11Symbols::getInstance()->xQueryBestCursor (display, root, imageW, imageH, &cursorW, &cursorH))
  1849. return {};
  1850. Image im (Image::ARGB, (int) cursorW, (int) cursorH, true);
  1851. {
  1852. Graphics g (im);
  1853. if (imageW > cursorW || imageH > cursorH)
  1854. {
  1855. hotspotX = (hotspotX * (int) cursorW) / (int) imageW;
  1856. hotspotY = (hotspotY * (int) cursorH) / (int) imageH;
  1857. g.drawImage (image, Rectangle<float> ((float) imageW, (float) imageH),
  1858. RectanglePlacement::xLeft | RectanglePlacement::yTop | RectanglePlacement::onlyReduceInSize);
  1859. }
  1860. else
  1861. {
  1862. g.drawImageAt (image, 0, 0);
  1863. }
  1864. }
  1865. auto stride = (cursorW + 7) >> 3;
  1866. HeapBlock<char> maskPlane, sourcePlane;
  1867. maskPlane.calloc (stride * cursorH);
  1868. sourcePlane.calloc (stride * cursorH);
  1869. auto msbfirst = (X11Symbols::getInstance()->xBitmapBitOrder (display) == MSBFirst);
  1870. for (auto y = (int) cursorH; --y >= 0;)
  1871. {
  1872. for (auto x = (int) cursorW; --x >= 0;)
  1873. {
  1874. auto mask = (char) (1 << (msbfirst ? (7 - (x & 7)) : (x & 7)));
  1875. auto offset = (unsigned int) y * stride + ((unsigned int) x >> 3);
  1876. auto c = im.getPixelAt (x, y);
  1877. if (c.getAlpha() >= 128) maskPlane[offset] |= mask;
  1878. if (c.getBrightness() >= 0.5f) sourcePlane[offset] |= mask;
  1879. }
  1880. }
  1881. auto xFreePixmap = [this] (Pixmap& p) { X11Symbols::getInstance()->xFreePixmap (display, p); };
  1882. XValueHolder<Pixmap> sourcePixmap (X11Symbols::getInstance()->xCreatePixmapFromBitmapData (display, root, sourcePlane.getData(), cursorW, cursorH, 0xffff, 0, 1), xFreePixmap);
  1883. XValueHolder<Pixmap> maskPixmap (X11Symbols::getInstance()->xCreatePixmapFromBitmapData (display, root, maskPlane.getData(), cursorW, cursorH, 0xffff, 0, 1), xFreePixmap);
  1884. XColor white, black;
  1885. black.red = black.green = black.blue = 0;
  1886. white.red = white.green = white.blue = 0xffff;
  1887. return X11Symbols::getInstance()->xCreatePixmapCursor (display, sourcePixmap.value, maskPixmap.value, &white, &black,
  1888. (unsigned int) hotspotX, (unsigned int) hotspotY);
  1889. }
  1890. void XWindowSystem::deleteMouseCursor (Cursor cursorHandle) const
  1891. {
  1892. if (cursorHandle != Cursor{} && display != nullptr)
  1893. {
  1894. XWindowSystemUtilities::ScopedXLock xLock;
  1895. X11Symbols::getInstance()->xFreeCursor (display, (Cursor) cursorHandle);
  1896. }
  1897. }
  1898. static Cursor createDraggingHandCursor()
  1899. {
  1900. constexpr unsigned char dragHandData[] = {
  1901. 71,73,70,56,57,97,16,0,16,0,145,2,0,0,0,0,255,255,255,0,0,0,0,0,0,33,249,4,1,0,0,2,0,44,0,0,0,0,16,0,16,0,
  1902. 0,2,52,148,47,0,200,185,16,130,90,12,74,139,107,84,123,39,132,117,151,116,132,146,248,60,209,138,98,22,203,
  1903. 114,34,236,37,52,77,217, 247,154,191,119,110,240,193,128,193,95,163,56,60,234,98,135,2,0,59
  1904. };
  1905. auto image = ImageFileFormat::loadFrom (dragHandData, (size_t) numElementsInArray (dragHandData));
  1906. return XWindowSystem::getInstance()->createCustomMouseCursorInfo (std::move (image), { 8, 7 });
  1907. }
  1908. Cursor XWindowSystem::createStandardMouseCursor (MouseCursor::StandardCursorType type) const
  1909. {
  1910. if (display == nullptr)
  1911. return None;
  1912. unsigned int shape;
  1913. switch (type)
  1914. {
  1915. case MouseCursor::NormalCursor:
  1916. case MouseCursor::ParentCursor: return None; // Use parent cursor
  1917. case MouseCursor::NoCursor: return XWindowSystem::createCustomMouseCursorInfo (Image (Image::ARGB, 16, 16, true), {});
  1918. case MouseCursor::WaitCursor: shape = XC_watch; break;
  1919. case MouseCursor::IBeamCursor: shape = XC_xterm; break;
  1920. case MouseCursor::PointingHandCursor: shape = XC_hand2; break;
  1921. case MouseCursor::LeftRightResizeCursor: shape = XC_sb_h_double_arrow; break;
  1922. case MouseCursor::UpDownResizeCursor: shape = XC_sb_v_double_arrow; break;
  1923. case MouseCursor::UpDownLeftRightResizeCursor: shape = XC_fleur; break;
  1924. case MouseCursor::TopEdgeResizeCursor: shape = XC_top_side; break;
  1925. case MouseCursor::BottomEdgeResizeCursor: shape = XC_bottom_side; break;
  1926. case MouseCursor::LeftEdgeResizeCursor: shape = XC_left_side; break;
  1927. case MouseCursor::RightEdgeResizeCursor: shape = XC_right_side; break;
  1928. case MouseCursor::TopLeftCornerResizeCursor: shape = XC_top_left_corner; break;
  1929. case MouseCursor::TopRightCornerResizeCursor: shape = XC_top_right_corner; break;
  1930. case MouseCursor::BottomLeftCornerResizeCursor: shape = XC_bottom_left_corner; break;
  1931. case MouseCursor::BottomRightCornerResizeCursor: shape = XC_bottom_right_corner; break;
  1932. case MouseCursor::CrosshairCursor: shape = XC_crosshair; break;
  1933. case MouseCursor::DraggingHandCursor: return createDraggingHandCursor();
  1934. case MouseCursor::CopyingCursor:
  1935. {
  1936. constexpr unsigned char copyCursorData[] = {
  1937. 71,73,70,56,57,97,21,0,21,0,145,0,0,0,0,0,255,255,255,0,128,128,255,255,255,33,249,4,1,0,0,3,0,44,0,0,0,0,
  1938. 21,0,21,0,0,2,72,4,134,169,171,16,199,98,11,79,90,71,161,93,56,111,78,133,218,215,137,31,82,154,100,200,
  1939. 86,91,202,142,12,108,212,87,235,174,15,54,214,126,237,226,37,96,59,141,16,37,18,201,142,157,230,204,51,112,
  1940. 252,114,147,74,83,5,50,68,147,208,217,16,71,149,252,124,5,0,59,0,0
  1941. };
  1942. auto image = ImageFileFormat::loadFrom (copyCursorData, (size_t) numElementsInArray (copyCursorData));
  1943. return createCustomMouseCursorInfo (std::move (image), { 1, 3 });
  1944. }
  1945. case MouseCursor::NumStandardCursorTypes:
  1946. default:
  1947. {
  1948. jassertfalse;
  1949. return None;
  1950. }
  1951. }
  1952. XWindowSystemUtilities::ScopedXLock xLock;
  1953. return X11Symbols::getInstance()->xCreateFontCursor (display, shape);
  1954. }
  1955. void XWindowSystem::showCursor (::Window windowH, Cursor cursorHandle) const
  1956. {
  1957. jassert (windowH != 0);
  1958. XWindowSystemUtilities::ScopedXLock xLock;
  1959. X11Symbols::getInstance()->xDefineCursor (display, windowH, (Cursor) cursorHandle);
  1960. }
  1961. bool XWindowSystem::isKeyCurrentlyDown (int keyCode) const
  1962. {
  1963. int keysym;
  1964. if (keyCode & Keys::extendedKeyModifier)
  1965. {
  1966. keysym = 0xff00 | (keyCode & 0xff);
  1967. }
  1968. else
  1969. {
  1970. keysym = keyCode;
  1971. if (keysym == (XK_Tab & 0xff)
  1972. || keysym == (XK_Return & 0xff)
  1973. || keysym == (XK_Escape & 0xff)
  1974. || keysym == (XK_BackSpace & 0xff))
  1975. {
  1976. keysym |= 0xff00;
  1977. }
  1978. }
  1979. XWindowSystemUtilities::ScopedXLock xLock;
  1980. auto keycode = X11Symbols::getInstance()->xKeysymToKeycode (display, (KeySym) keysym);
  1981. auto keybyte = keycode >> 3;
  1982. auto keybit = (1 << (keycode & 7));
  1983. return (Keys::keyStates [keybyte] & keybit) != 0;
  1984. }
  1985. ModifierKeys XWindowSystem::getNativeRealtimeModifiers() const
  1986. {
  1987. ::Window root, child;
  1988. int x, y, winx, winy;
  1989. unsigned int mask;
  1990. int mouseMods = 0;
  1991. XWindowSystemUtilities::ScopedXLock xLock;
  1992. if (X11Symbols::getInstance()->xQueryPointer (display,
  1993. X11Symbols::getInstance()->xRootWindow (display,
  1994. X11Symbols::getInstance()->xDefaultScreen (display)),
  1995. &root, &child, &x, &y, &winx, &winy, &mask) != False)
  1996. {
  1997. if ((mask & Button1Mask) != 0) mouseMods |= ModifierKeys::leftButtonModifier;
  1998. if ((mask & Button2Mask) != 0) mouseMods |= ModifierKeys::middleButtonModifier;
  1999. if ((mask & Button3Mask) != 0) mouseMods |= ModifierKeys::rightButtonModifier;
  2000. }
  2001. ModifierKeys::currentModifiers = ModifierKeys::currentModifiers.withoutMouseButtons().withFlags (mouseMods);
  2002. // We are keeping track of the state of modifier keys and mouse buttons with the assumption that
  2003. // for every mouse down we are going to receive a mouse up etc.
  2004. //
  2005. // This assumption is broken when getNativeRealtimeModifiers() is called. If for example we call
  2006. // this function when the mouse cursor is in another application and the mouse button happens to
  2007. // be down, then its represented state in currentModifiers may remain down indefinitely, since
  2008. // we aren't going to receive an event when it's released.
  2009. //
  2010. // We mark this state in this variable, and we can restore synchronization when our window
  2011. // receives an event again.
  2012. Keys::modifierKeysAreStale = true;
  2013. return ModifierKeys::currentModifiers;
  2014. }
  2015. static bool hasWorkAreaData (const XWindowSystemUtilities::GetXProperty& prop)
  2016. {
  2017. return prop.success
  2018. && prop.actualType == XA_CARDINAL
  2019. && prop.actualFormat == 32
  2020. && prop.numItems == 4
  2021. && prop.data != nullptr;
  2022. }
  2023. static Rectangle<int> getWorkArea (const XWindowSystemUtilities::GetXProperty& prop)
  2024. {
  2025. if (hasWorkAreaData (prop))
  2026. {
  2027. auto* positionData = prop.data;
  2028. std::array<long, 4> position;
  2029. for (auto& p : position)
  2030. {
  2031. memcpy (&p, positionData, sizeof (long));
  2032. positionData += sizeof (long);
  2033. }
  2034. return { (int) position[0], (int) position[1],
  2035. (int) position[2], (int) position[3] };
  2036. }
  2037. return {};
  2038. }
  2039. Array<Displays::Display> XWindowSystem::findDisplays (float masterScale) const
  2040. {
  2041. Array<Displays::Display> displays;
  2042. auto workAreaHints = XWindowSystemUtilities::Atoms::getIfExists (display, "_NET_WORKAREA");
  2043. #if JUCE_USE_XRANDR
  2044. if (workAreaHints != None)
  2045. {
  2046. int major_opcode, first_event, first_error;
  2047. if (X11Symbols::getInstance()->xQueryExtension (display, "RANDR", &major_opcode, &first_event, &first_error))
  2048. {
  2049. auto numMonitors = X11Symbols::getInstance()->xScreenCount (display);
  2050. auto mainDisplay = X11Symbols::getInstance()->xRRGetOutputPrimary (display, X11Symbols::getInstance()->xRootWindow (display, 0));
  2051. for (int i = 0; i < numMonitors; ++i)
  2052. {
  2053. auto rootWindow = X11Symbols::getInstance()->xRootWindow (display, i);
  2054. XWindowSystemUtilities::GetXProperty prop (display, rootWindow, workAreaHints, 0, 4, false, XA_CARDINAL);
  2055. if (! hasWorkAreaData (prop))
  2056. continue;
  2057. if (auto screens = makeDeletedPtr (X11Symbols::getInstance()->xRRGetScreenResources (display, rootWindow),
  2058. [] (XRRScreenResources* srs) { X11Symbols::getInstance()->xRRFreeScreenResources (srs); }))
  2059. {
  2060. for (int j = 0; j < screens->noutput; ++j)
  2061. {
  2062. if (screens->outputs[j])
  2063. {
  2064. // Xrandr on the raspberry pi fails to determine the main display (mainDisplay == 0)!
  2065. // Detect this edge case and make the first found display the main display
  2066. if (! mainDisplay)
  2067. mainDisplay = screens->outputs[j];
  2068. if (auto output = makeDeletedPtr (X11Symbols::getInstance()->xRRGetOutputInfo (display, screens.get(), screens->outputs[j]),
  2069. [] (XRROutputInfo* oi) { X11Symbols::getInstance()->xRRFreeOutputInfo (oi); }))
  2070. {
  2071. if (output->crtc)
  2072. {
  2073. if (auto crtc = makeDeletedPtr (X11Symbols::getInstance()->xRRGetCrtcInfo (display, screens.get(), output->crtc),
  2074. [] (XRRCrtcInfo* ci) { X11Symbols::getInstance()->xRRFreeCrtcInfo (ci); }))
  2075. {
  2076. Displays::Display d;
  2077. d.totalArea = { crtc->x, crtc->y, (int) crtc->width, (int) crtc->height };
  2078. d.isMain = (mainDisplay == screens->outputs[j]) && (i == 0);
  2079. d.dpi = DisplayHelpers::getDisplayDPI (display, 0);
  2080. // The raspberry pi returns a zero sized display, so we need to guard for divide-by-zero
  2081. if (output->mm_width > 0 && output->mm_height > 0)
  2082. d.dpi = ((static_cast<double> (crtc->width) * 25.4 * 0.5) / static_cast<double> (output->mm_width))
  2083. + ((static_cast<double> (crtc->height) * 25.4 * 0.5) / static_cast<double> (output->mm_height));
  2084. auto scale = DisplayHelpers::getDisplayScale (output->name, d.dpi);
  2085. scale = (scale <= 0.1 || ! JUCEApplicationBase::isStandaloneApp()) ? 1.0 : scale;
  2086. d.scale = masterScale * scale;
  2087. if (d.isMain)
  2088. displays.insert (0, d);
  2089. else
  2090. displays.add (d);
  2091. }
  2092. }
  2093. }
  2094. }
  2095. }
  2096. }
  2097. }
  2098. if (! displays.isEmpty() && ! displays.getReference (0).isMain)
  2099. displays.getReference (0).isMain = true;
  2100. }
  2101. }
  2102. if (displays.isEmpty())
  2103. #endif
  2104. #if JUCE_USE_XINERAMA
  2105. {
  2106. auto screens = DisplayHelpers::xineramaQueryDisplays (display);
  2107. auto numMonitors = screens.size();
  2108. for (int index = 0; index < numMonitors; ++index)
  2109. {
  2110. for (auto j = numMonitors; --j >= 0;)
  2111. {
  2112. if (screens[j].screen_number == index)
  2113. {
  2114. Displays::Display d;
  2115. d.totalArea = { screens[j].x_org, screens[j].y_org,
  2116. screens[j].width, screens[j].height };
  2117. d.isMain = (index == 0);
  2118. d.scale = masterScale;
  2119. d.dpi = DisplayHelpers::getDisplayDPI (display, 0); // (all screens share the same DPI)
  2120. displays.add (d);
  2121. }
  2122. }
  2123. }
  2124. }
  2125. if (displays.isEmpty())
  2126. #endif
  2127. {
  2128. if (workAreaHints != None)
  2129. {
  2130. auto numMonitors = X11Symbols::getInstance()->xScreenCount (display);
  2131. for (int i = 0; i < numMonitors; ++i)
  2132. {
  2133. XWindowSystemUtilities::GetXProperty prop (display,
  2134. X11Symbols::getInstance()->xRootWindow (display, i),
  2135. workAreaHints, 0, 4, false, XA_CARDINAL);
  2136. auto workArea = getWorkArea (prop);
  2137. if (! workArea.isEmpty())
  2138. {
  2139. Displays::Display d;
  2140. d.totalArea = workArea;
  2141. d.isMain = displays.isEmpty();
  2142. d.scale = masterScale;
  2143. d.dpi = DisplayHelpers::getDisplayDPI (display, i);
  2144. displays.add (d);
  2145. }
  2146. }
  2147. }
  2148. if (displays.isEmpty())
  2149. {
  2150. Displays::Display d;
  2151. d.totalArea = { X11Symbols::getInstance()->xDisplayWidth (display, X11Symbols::getInstance()->xDefaultScreen (display)),
  2152. X11Symbols::getInstance()->xDisplayHeight (display, X11Symbols::getInstance()->xDefaultScreen (display)) };
  2153. d.isMain = true;
  2154. d.scale = masterScale;
  2155. d.dpi = DisplayHelpers::getDisplayDPI (display, 0);
  2156. displays.add (d);
  2157. }
  2158. }
  2159. for (auto& d : displays)
  2160. d.userArea = d.totalArea; // JUCE currently does not support requesting the user area on Linux
  2161. return displays;
  2162. }
  2163. ::Window XWindowSystem::createKeyProxy (::Window windowH) const
  2164. {
  2165. jassert (windowH != 0);
  2166. XSetWindowAttributes swa;
  2167. swa.event_mask = KeyPressMask | KeyReleaseMask | FocusChangeMask;
  2168. auto keyProxy = X11Symbols::getInstance()->xCreateWindow (display, windowH,
  2169. -1, -1, 1, 1, 0, 0,
  2170. InputOnly, CopyFromParent,
  2171. CWEventMask,
  2172. &swa);
  2173. X11Symbols::getInstance()->xMapWindow (display, keyProxy);
  2174. X11Symbols::getInstance()->xSaveContext (display, (XID) keyProxy, windowHandleXContext, (XPointer) this);
  2175. return keyProxy;
  2176. }
  2177. void XWindowSystem::deleteKeyProxy (::Window keyProxy) const
  2178. {
  2179. jassert (keyProxy != 0);
  2180. XPointer handlePointer;
  2181. if (! X11Symbols::getInstance()->xFindContext (display, (XID) keyProxy, windowHandleXContext, &handlePointer))
  2182. X11Symbols::getInstance()->xDeleteContext (display, (XID) keyProxy, windowHandleXContext);
  2183. X11Symbols::getInstance()->xDestroyWindow (display, keyProxy);
  2184. X11Symbols::getInstance()->xSync (display, false);
  2185. XEvent event;
  2186. while (X11Symbols::getInstance()->xCheckWindowEvent (display, keyProxy, getAllEventsMask (false), &event) == True)
  2187. {}
  2188. }
  2189. bool XWindowSystem::externalDragFileInit (LinuxComponentPeer* peer, const StringArray& files, bool, std::function<void()>&& callback) const
  2190. {
  2191. auto& dragState = dragAndDropStateMap[peer];
  2192. if (dragState.isDragging())
  2193. return false;
  2194. StringArray uriList;
  2195. for (auto& f : files)
  2196. {
  2197. if (f.matchesWildcard ("?*://*", false))
  2198. uriList.add (f);
  2199. else
  2200. uriList.add ("file://" + f);
  2201. }
  2202. return dragState.externalDragInit ((::Window) peer->getNativeHandle(), false, uriList.joinIntoString ("\r\n"), std::move (callback));
  2203. }
  2204. bool XWindowSystem::externalDragTextInit (LinuxComponentPeer* peer, const String& text, std::function<void()>&& callback) const
  2205. {
  2206. auto& dragState = dragAndDropStateMap[peer];
  2207. if (dragState.isDragging())
  2208. return false;
  2209. return dragState.externalDragInit ((::Window) peer->getNativeHandle(), true, text, std::move (callback));
  2210. }
  2211. void XWindowSystem::copyTextToClipboard (const String& clipText)
  2212. {
  2213. localClipboardContent = clipText;
  2214. X11Symbols::getInstance()->xSetSelectionOwner (display, XA_PRIMARY, juce_messageWindowHandle, CurrentTime);
  2215. X11Symbols::getInstance()->xSetSelectionOwner (display, atoms.clipboard, juce_messageWindowHandle, CurrentTime);
  2216. }
  2217. String XWindowSystem::getTextFromClipboard() const
  2218. {
  2219. /* 1) try to read from the "CLIPBOARD" selection first (the "high
  2220. level" clipboard that is supposed to be filled by ctrl-C
  2221. etc). When a clipboard manager is running, the content of this
  2222. selection is preserved even when the original selection owner
  2223. exits.
  2224. 2) and then try to read from "PRIMARY" selection (the "legacy" selection
  2225. filled by good old x11 apps such as xterm)
  2226. */
  2227. auto getContentForSelection = [this] (Atom selectionAtom) -> String
  2228. {
  2229. auto selectionOwner = X11Symbols::getInstance()->xGetSelectionOwner (display, selectionAtom);
  2230. if (selectionOwner == None)
  2231. return {};
  2232. if (selectionOwner == juce_messageWindowHandle)
  2233. return localClipboardContent;
  2234. String content;
  2235. if (! ClipboardHelpers::requestSelectionContent (display, content, selectionAtom, atoms.utf8String))
  2236. ClipboardHelpers::requestSelectionContent (display, content, selectionAtom, XA_STRING);
  2237. return content;
  2238. };
  2239. auto content = getContentForSelection (atoms.clipboard);
  2240. if (content.isEmpty())
  2241. content = getContentForSelection (XA_PRIMARY);
  2242. return content;
  2243. }
  2244. //==============================================================================
  2245. ::Window XWindowSystem::findTopLevelWindowOf (::Window w) const
  2246. {
  2247. if (w == 0)
  2248. return 0;
  2249. Window* windowList = nullptr;
  2250. uint32 windowListSize = 0;
  2251. Window parent, root;
  2252. XWindowSystemUtilities::ScopedXLock xLock;
  2253. const auto result = X11Symbols::getInstance()->xQueryTree (display, w, &root, &parent, &windowList, &windowListSize);
  2254. const auto deleter = makeXFreePtr (windowList);
  2255. if (result == 0)
  2256. return 0;
  2257. if (parent == root)
  2258. return w;
  2259. return findTopLevelWindowOf (parent);
  2260. }
  2261. bool XWindowSystem::isParentWindowOf (::Window windowH, ::Window possibleChild) const
  2262. {
  2263. if (windowH == 0 || possibleChild == 0)
  2264. return false;
  2265. if (possibleChild == windowH)
  2266. return true;
  2267. Window* windowList = nullptr;
  2268. uint32 windowListSize = 0;
  2269. Window parent, root;
  2270. XWindowSystemUtilities::ScopedXLock xLock;
  2271. const auto result = X11Symbols::getInstance()->xQueryTree (display, possibleChild, &root, &parent, &windowList, &windowListSize);
  2272. const auto deleter = makeXFreePtr (windowList);
  2273. if (result == 0 || parent == root)
  2274. return false;
  2275. return isParentWindowOf (windowH, parent);
  2276. }
  2277. bool XWindowSystem::isFrontWindow (::Window windowH) const
  2278. {
  2279. jassert (windowH != 0);
  2280. Window* windowList = nullptr;
  2281. uint32 windowListSize = 0;
  2282. XWindowSystemUtilities::ScopedXLock xLock;
  2283. Window parent;
  2284. auto root = X11Symbols::getInstance()->xRootWindow (display, X11Symbols::getInstance()->xDefaultScreen (display));
  2285. const auto queryResult = X11Symbols::getInstance()->xQueryTree (display, root, &root, &parent, &windowList, &windowListSize);
  2286. const auto deleter = makeXFreePtr (windowList);
  2287. if (queryResult == 0)
  2288. return false;
  2289. for (int i = (int) windowListSize; --i >= 0;)
  2290. {
  2291. if (auto* peer = dynamic_cast<LinuxComponentPeer*> (getPeerFor (windowList[i])))
  2292. return peer == dynamic_cast<LinuxComponentPeer*> (getPeerFor (windowH));
  2293. }
  2294. return false;
  2295. }
  2296. void XWindowSystem::xchangeProperty (::Window windowH, Atom property, Atom type, int format, const void* data, int numElements) const
  2297. {
  2298. jassert (windowH != 0);
  2299. X11Symbols::getInstance()->xChangeProperty (display, windowH, property, type, format, PropModeReplace, (const unsigned char*) data, numElements);
  2300. }
  2301. void XWindowSystem::removeWindowDecorations (::Window windowH) const
  2302. {
  2303. jassert (windowH != 0);
  2304. auto hints = XWindowSystemUtilities::Atoms::getIfExists (display, "_MOTIF_WM_HINTS");
  2305. if (hints != None)
  2306. {
  2307. MotifWmHints motifHints;
  2308. zerostruct (motifHints);
  2309. motifHints.flags = 2; /* MWM_HINTS_DECORATIONS */
  2310. motifHints.decorations = 0;
  2311. XWindowSystemUtilities::ScopedXLock xLock;
  2312. xchangeProperty (windowH, hints, hints, 32, &motifHints, 4);
  2313. }
  2314. hints = XWindowSystemUtilities::Atoms::getIfExists (display, "_WIN_HINTS");
  2315. if (hints != None)
  2316. {
  2317. long gnomeHints = 0;
  2318. XWindowSystemUtilities::ScopedXLock xLock;
  2319. xchangeProperty (windowH, hints, hints, 32, &gnomeHints, 1);
  2320. }
  2321. hints = XWindowSystemUtilities::Atoms::getIfExists (display, "KWM_WIN_DECORATION");
  2322. if (hints != None)
  2323. {
  2324. long kwmHints = 2; /*KDE_tinyDecoration*/
  2325. XWindowSystemUtilities::ScopedXLock xLock;
  2326. xchangeProperty (windowH, hints, hints, 32, &kwmHints, 1);
  2327. }
  2328. hints = XWindowSystemUtilities::Atoms::getIfExists (display, "_KDE_NET_WM_WINDOW_TYPE_OVERRIDE");
  2329. if (hints != None)
  2330. {
  2331. XWindowSystemUtilities::ScopedXLock xLock;
  2332. xchangeProperty (windowH, atoms.windowType, XA_ATOM, 32, &hints, 1);
  2333. }
  2334. }
  2335. static void addAtomIfExists (bool condition, const char* key, ::Display* display, std::vector<Atom>& atoms)
  2336. {
  2337. if (condition)
  2338. {
  2339. auto atom = XWindowSystemUtilities::Atoms::getIfExists (display, key);
  2340. if (atom != None)
  2341. atoms.push_back (atom);
  2342. }
  2343. }
  2344. void XWindowSystem::addWindowButtons (::Window windowH, int styleFlags) const
  2345. {
  2346. jassert (windowH != 0);
  2347. XWindowSystemUtilities::ScopedXLock xLock;
  2348. auto motifAtom = XWindowSystemUtilities::Atoms::getIfExists (display, "_MOTIF_WM_HINTS");
  2349. if (motifAtom != None)
  2350. {
  2351. MotifWmHints motifHints;
  2352. zerostruct (motifHints);
  2353. motifHints.flags = 1 | 2; /* MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS */
  2354. motifHints.decorations = 2 /* MWM_DECOR_BORDER */ | 8 /* MWM_DECOR_TITLE */ | 16; /* MWM_DECOR_MENU */
  2355. motifHints.functions = 4 /* MWM_FUNC_MOVE */;
  2356. if ((styleFlags & ComponentPeer::windowHasCloseButton) != 0)
  2357. motifHints.functions |= 32; /* MWM_FUNC_CLOSE */
  2358. if ((styleFlags & ComponentPeer::windowHasMinimiseButton) != 0)
  2359. {
  2360. motifHints.functions |= 8; /* MWM_FUNC_MINIMIZE */
  2361. motifHints.decorations |= 0x20; /* MWM_DECOR_MINIMIZE */
  2362. }
  2363. if ((styleFlags & ComponentPeer::windowHasMaximiseButton) != 0)
  2364. {
  2365. motifHints.functions |= 0x10; /* MWM_FUNC_MAXIMIZE */
  2366. motifHints.decorations |= 0x40; /* MWM_DECOR_MAXIMIZE */
  2367. }
  2368. if ((styleFlags & ComponentPeer::windowIsResizable) != 0)
  2369. {
  2370. motifHints.functions |= 2; /* MWM_FUNC_RESIZE */
  2371. motifHints.decorations |= 0x4; /* MWM_DECOR_RESIZEH */
  2372. }
  2373. xchangeProperty (windowH, motifAtom, motifAtom, 32, &motifHints, 5);
  2374. }
  2375. auto actionsAtom = XWindowSystemUtilities::Atoms::getIfExists (display, "_NET_WM_ALLOWED_ACTIONS");
  2376. if (actionsAtom != None)
  2377. {
  2378. std::vector<Atom> netHints;
  2379. addAtomIfExists ((styleFlags & ComponentPeer::windowIsResizable) != 0, "_NET_WM_ACTION_RESIZE", display, netHints);
  2380. addAtomIfExists ((styleFlags & ComponentPeer::windowHasMaximiseButton) != 0, "_NET_WM_ACTION_FULLSCREEN", display, netHints);
  2381. addAtomIfExists ((styleFlags & ComponentPeer::windowHasMinimiseButton) != 0, "_NET_WM_ACTION_MINIMIZE", display, netHints);
  2382. addAtomIfExists ((styleFlags & ComponentPeer::windowHasCloseButton) != 0, "_NET_WM_ACTION_CLOSE", display, netHints);
  2383. auto numHints = (int) netHints.size();
  2384. if (numHints > 0)
  2385. xchangeProperty (windowH, actionsAtom, XA_ATOM, 32, netHints.data(), numHints);
  2386. }
  2387. }
  2388. void XWindowSystem::setWindowType (::Window windowH, int styleFlags) const
  2389. {
  2390. jassert (windowH != 0);
  2391. if (atoms.windowType != None)
  2392. {
  2393. auto hint = (styleFlags & ComponentPeer::windowIsTemporary) != 0
  2394. || ((styleFlags & ComponentPeer::windowHasDropShadow) == 0 && Desktop::canUseSemiTransparentWindows())
  2395. ? XWindowSystemUtilities::Atoms::getIfExists (display, "_NET_WM_WINDOW_TYPE_COMBO")
  2396. : XWindowSystemUtilities::Atoms::getIfExists (display, "_NET_WM_WINDOW_TYPE_NORMAL");
  2397. if (hint != None)
  2398. xchangeProperty (windowH, atoms.windowType, XA_ATOM, 32, &hint, 1);
  2399. }
  2400. if (atoms.windowState != None)
  2401. {
  2402. std::vector<Atom> netStateHints;
  2403. addAtomIfExists ((styleFlags & ComponentPeer::windowAppearsOnTaskbar) == 0, "_NET_WM_STATE_SKIP_TASKBAR", display, netStateHints);
  2404. addAtomIfExists (getPeerFor (windowH)->getComponent().isAlwaysOnTop(), "_NET_WM_STATE_ABOVE", display, netStateHints);
  2405. auto numHints = (int) netStateHints.size();
  2406. if (numHints > 0)
  2407. xchangeProperty (windowH, atoms.windowState, XA_ATOM, 32, netStateHints.data(), numHints);
  2408. }
  2409. }
  2410. void XWindowSystem::initialisePointerMap()
  2411. {
  2412. auto numButtons = X11Symbols::getInstance()->xGetPointerMapping (display, nullptr, 0);
  2413. pointerMap[2] = pointerMap[3] = pointerMap[4] = Keys::NoButton;
  2414. if (numButtons == 2)
  2415. {
  2416. pointerMap[0] = Keys::LeftButton;
  2417. pointerMap[1] = Keys::RightButton;
  2418. }
  2419. else if (numButtons >= 3)
  2420. {
  2421. pointerMap[0] = Keys::LeftButton;
  2422. pointerMap[1] = Keys::MiddleButton;
  2423. pointerMap[2] = Keys::RightButton;
  2424. if (numButtons >= 5)
  2425. {
  2426. pointerMap[3] = Keys::WheelUp;
  2427. pointerMap[4] = Keys::WheelDown;
  2428. }
  2429. }
  2430. }
  2431. void XWindowSystem::deleteIconPixmaps (::Window windowH) const
  2432. {
  2433. jassert (windowH != 0);
  2434. XWindowSystemUtilities::ScopedXLock xLock;
  2435. if (auto wmHints = makeXFreePtr (X11Symbols::getInstance()->xGetWMHints (display, windowH)))
  2436. {
  2437. if ((wmHints->flags & IconPixmapHint) != 0)
  2438. {
  2439. wmHints->flags &= ~IconPixmapHint;
  2440. X11Symbols::getInstance()->xFreePixmap (display, wmHints->icon_pixmap);
  2441. }
  2442. if ((wmHints->flags & IconMaskHint) != 0)
  2443. {
  2444. wmHints->flags &= ~IconMaskHint;
  2445. X11Symbols::getInstance()->xFreePixmap (display, wmHints->icon_mask);
  2446. }
  2447. X11Symbols::getInstance()->xSetWMHints (display, windowH, wmHints.get());
  2448. }
  2449. }
  2450. // Alt and Num lock are not defined by standard X modifier constants: check what they're mapped to
  2451. void XWindowSystem::updateModifierMappings() const
  2452. {
  2453. XWindowSystemUtilities::ScopedXLock xLock;
  2454. auto altLeftCode = X11Symbols::getInstance()->xKeysymToKeycode (display, XK_Alt_L);
  2455. auto numLockCode = X11Symbols::getInstance()->xKeysymToKeycode (display, XK_Num_Lock);
  2456. Keys::AltMask = 0;
  2457. Keys::NumLockMask = 0;
  2458. if (auto mapping = makeDeletedPtr (X11Symbols::getInstance()->xGetModifierMapping (display),
  2459. [] (XModifierKeymap* mk) { X11Symbols::getInstance()->xFreeModifiermap (mk); }))
  2460. {
  2461. for (int modifierIdx = 0; modifierIdx < 8; ++modifierIdx)
  2462. {
  2463. for (int keyIndex = 0; keyIndex < mapping->max_keypermod; ++keyIndex)
  2464. {
  2465. auto key = mapping->modifiermap[(modifierIdx * mapping->max_keypermod) + keyIndex];
  2466. if (key == altLeftCode)
  2467. Keys::AltMask = 1 << modifierIdx;
  2468. else if (key == numLockCode)
  2469. Keys::NumLockMask = 1 << modifierIdx;
  2470. }
  2471. }
  2472. }
  2473. }
  2474. long XWindowSystem::getUserTime (::Window windowH) const
  2475. {
  2476. jassert (windowH != 0);
  2477. XWindowSystemUtilities::GetXProperty prop (display, windowH, atoms.userTime, 0, 65536, false, XA_CARDINAL);
  2478. if (! prop.success)
  2479. return 0;
  2480. long result = 0;
  2481. std::memcpy (&result, prop.data, sizeof (long));
  2482. return result;
  2483. }
  2484. void XWindowSystem::initialiseXSettings()
  2485. {
  2486. xSettings = XWindowSystemUtilities::XSettings::createXSettings (display);
  2487. if (xSettings != nullptr)
  2488. X11Symbols::getInstance()->xSelectInput (display,
  2489. xSettings->getSettingsWindow(),
  2490. StructureNotifyMask | PropertyChangeMask);
  2491. }
  2492. XWindowSystem::DisplayVisuals::DisplayVisuals (::Display* xDisplay)
  2493. {
  2494. auto findVisualWithDepthOrNull = [&] (int desiredDepth) -> Visual*
  2495. {
  2496. int matchedDepth = 0;
  2497. auto* visual = Visuals::findVisualFormat (xDisplay, desiredDepth, matchedDepth);
  2498. if (desiredDepth == matchedDepth)
  2499. return visual;
  2500. return nullptr;
  2501. };
  2502. visual16Bit = findVisualWithDepthOrNull (16);
  2503. visual24Bit = findVisualWithDepthOrNull (24);
  2504. visual32Bit = findVisualWithDepthOrNull (32);
  2505. }
  2506. XWindowSystem::VisualAndDepth XWindowSystem::DisplayVisuals::getBestVisualForWindow (bool isSemiTransparent) const
  2507. {
  2508. if (isSemiTransparent && visual32Bit != nullptr)
  2509. return { visual32Bit, 32 };
  2510. if (visual24Bit != nullptr)
  2511. return { visual24Bit, 24 };
  2512. return { visual16Bit, 16 };
  2513. }
  2514. bool XWindowSystem::DisplayVisuals::isValid() const noexcept
  2515. {
  2516. return (visual32Bit != nullptr || visual24Bit != nullptr || visual16Bit != nullptr);
  2517. }
  2518. //==============================================================================
  2519. bool XWindowSystem::initialiseXDisplay()
  2520. {
  2521. jassert (display == nullptr);
  2522. String displayName (getenv ("DISPLAY"));
  2523. if (displayName.isEmpty())
  2524. displayName = ":0.0";
  2525. // it seems that on some systems XOpenDisplay will occasionally
  2526. // fail the first time, but succeed on a second attempt..
  2527. for (int retries = 2; --retries >= 0;)
  2528. {
  2529. display = X11Symbols::getInstance()->xOpenDisplay (displayName.toUTF8());
  2530. if (display != nullptr)
  2531. break;
  2532. }
  2533. // No X Server running
  2534. if (display == nullptr)
  2535. return false;
  2536. #if JUCE_DEBUG_XERRORS_SYNCHRONOUSLY
  2537. X11Symbols::getInstance()->xSynchronize (display, True);
  2538. #endif
  2539. // Create a context to store user data associated with Windows we create
  2540. windowHandleXContext = (XContext) X11Symbols::getInstance()->xrmUniqueQuark();
  2541. // Create our message window (this will never be mapped)
  2542. auto screen = X11Symbols::getInstance()->xDefaultScreen (display);
  2543. auto root = X11Symbols::getInstance()->xRootWindow (display, screen);
  2544. X11Symbols::getInstance()->xSelectInput (display, root, SubstructureNotifyMask);
  2545. // We're only interested in client messages for this window, which are always sent
  2546. XSetWindowAttributes swa;
  2547. swa.event_mask = NoEventMask;
  2548. juce_messageWindowHandle = X11Symbols::getInstance()->xCreateWindow (display, root,
  2549. 0, 0, 1, 1, 0, 0, InputOnly,
  2550. X11Symbols::getInstance()->xDefaultVisual (display, screen),
  2551. CWEventMask, &swa);
  2552. X11Symbols::getInstance()->xSync (display, False);
  2553. atoms = XWindowSystemUtilities::Atoms (display);
  2554. initialisePointerMap();
  2555. updateModifierMappings();
  2556. initialiseXSettings();
  2557. #if JUCE_USE_XSHM
  2558. if (XSHMHelpers::isShmAvailable (display))
  2559. shmCompletionEvent = X11Symbols::getInstance()->xShmGetEventBase (display) + ShmCompletion;
  2560. #endif
  2561. displayVisuals = std::make_unique<DisplayVisuals> (display);
  2562. if (! displayVisuals->isValid())
  2563. {
  2564. Logger::outputDebugString ("ERROR: System doesn't support 32, 24 or 16 bit RGB display.\n");
  2565. return false;
  2566. }
  2567. // Setup input event handler
  2568. LinuxEventLoop::registerFdCallback (X11Symbols::getInstance()->xConnectionNumber (display),
  2569. [this] (int)
  2570. {
  2571. do
  2572. {
  2573. XEvent evt;
  2574. {
  2575. XWindowSystemUtilities::ScopedXLock xLock;
  2576. if (! X11Symbols::getInstance()->xPending (display))
  2577. return;
  2578. X11Symbols::getInstance()->xNextEvent (display, &evt);
  2579. }
  2580. if (evt.type == SelectionRequest && evt.xany.window == juce_messageWindowHandle)
  2581. {
  2582. ClipboardHelpers::handleSelection (evt.xselectionrequest);
  2583. }
  2584. else if (evt.xany.window != juce_messageWindowHandle)
  2585. {
  2586. windowMessageReceive (evt);
  2587. }
  2588. } while (display != nullptr);
  2589. });
  2590. return true;
  2591. }
  2592. void XWindowSystem::destroyXDisplay()
  2593. {
  2594. if (xIsAvailable)
  2595. {
  2596. jassert (display != nullptr);
  2597. {
  2598. XWindowSystemUtilities::ScopedXLock xLock;
  2599. X11Symbols::getInstance()->xDestroyWindow (display, juce_messageWindowHandle);
  2600. juce_messageWindowHandle = 0;
  2601. X11Symbols::getInstance()->xSync (display, True);
  2602. }
  2603. LinuxEventLoop::unregisterFdCallback (X11Symbols::getInstance()->xConnectionNumber (display));
  2604. {
  2605. XWindowSystemUtilities::ScopedXLock xLock;
  2606. X11Symbols::getInstance()->xCloseDisplay (display);
  2607. display = nullptr;
  2608. displayVisuals = nullptr;
  2609. }
  2610. }
  2611. }
  2612. //==============================================================================
  2613. ::Window juce_createKeyProxyWindow (ComponentPeer* peer);
  2614. ::Window juce_createKeyProxyWindow (ComponentPeer* peer)
  2615. {
  2616. return XWindowSystem::getInstance()->createKeyProxy ((::Window) peer->getNativeHandle());
  2617. }
  2618. void juce_deleteKeyProxyWindow (::Window keyProxy);
  2619. void juce_deleteKeyProxyWindow (::Window keyProxy)
  2620. {
  2621. XWindowSystem::getInstance()->deleteKeyProxy (keyProxy);
  2622. }
  2623. //==============================================================================
  2624. template <typename EventType>
  2625. static Point<float> getLogicalMousePos (const EventType& e, double scaleFactor) noexcept
  2626. {
  2627. return Point<float> ((float) e.x, (float) e.y) / scaleFactor;
  2628. }
  2629. static int64 getEventTime (::Time t)
  2630. {
  2631. static int64 eventTimeOffset = 0x12345678;
  2632. auto thisMessageTime = (int64) t;
  2633. if (eventTimeOffset == 0x12345678)
  2634. eventTimeOffset = Time::currentTimeMillis() - thisMessageTime;
  2635. return eventTimeOffset + thisMessageTime;
  2636. }
  2637. template <typename EventType>
  2638. static int64 getEventTime (const EventType& t)
  2639. {
  2640. return getEventTime (t.time);
  2641. }
  2642. void XWindowSystem::handleWindowMessage (LinuxComponentPeer* peer, XEvent& event) const
  2643. {
  2644. switch (event.xany.type)
  2645. {
  2646. case KeyPressEventType: handleKeyPressEvent (peer, event.xkey); break;
  2647. case KeyRelease: handleKeyReleaseEvent (peer, event.xkey); break;
  2648. case ButtonPress: handleButtonPressEvent (peer, event.xbutton); break;
  2649. case ButtonRelease: handleButtonReleaseEvent (peer, event.xbutton); break;
  2650. case MotionNotify: handleMotionNotifyEvent (peer, event.xmotion); break;
  2651. case EnterNotify: handleEnterNotifyEvent (peer, event.xcrossing); break;
  2652. case LeaveNotify: handleLeaveNotifyEvent (peer, event.xcrossing); break;
  2653. case FocusIn: handleFocusInEvent (peer); break;
  2654. case FocusOut: handleFocusOutEvent (peer); break;
  2655. case Expose: handleExposeEvent (peer, event.xexpose); break;
  2656. case MappingNotify: handleMappingNotify (event.xmapping); break;
  2657. case ClientMessage: handleClientMessageEvent (peer, event.xclient, event); break;
  2658. case SelectionNotify: dragAndDropStateMap[peer].handleDragAndDropSelection (event); break;
  2659. case ConfigureNotify: handleConfigureNotifyEvent (peer, event.xconfigure); break;
  2660. case ReparentNotify:
  2661. case GravityNotify: handleGravityNotify (peer); break;
  2662. case SelectionClear: dragAndDropStateMap[peer].handleExternalSelectionClear(); break;
  2663. case SelectionRequest: dragAndDropStateMap[peer].handleExternalSelectionRequest (event); break;
  2664. case PropertyNotify: propertyNotifyEvent (peer, event.xproperty); break;
  2665. case CirculateNotify:
  2666. case CreateNotify:
  2667. case DestroyNotify:
  2668. case UnmapNotify:
  2669. break;
  2670. case MapNotify:
  2671. peer->handleBroughtToFront();
  2672. break;
  2673. default:
  2674. #if JUCE_USE_XSHM
  2675. if (XSHMHelpers::isShmAvailable (display))
  2676. {
  2677. XWindowSystemUtilities::ScopedXLock xLock;
  2678. if (event.xany.type == shmCompletionEvent)
  2679. XWindowSystem::getInstance()->removePendingPaintForWindow ((::Window) peer->getNativeHandle());
  2680. }
  2681. #endif
  2682. break;
  2683. }
  2684. }
  2685. void XWindowSystem::handleKeyPressEvent (LinuxComponentPeer* peer, XKeyEvent& keyEvent) const
  2686. {
  2687. auto oldMods = ModifierKeys::currentModifiers;
  2688. Keys::refreshStaleModifierKeys();
  2689. char utf8 [64] = { 0 };
  2690. juce_wchar unicodeChar = 0;
  2691. int keyCode = 0;
  2692. bool keyDownChange = false;
  2693. KeySym sym;
  2694. {
  2695. XWindowSystemUtilities::ScopedXLock xLock;
  2696. updateKeyStates ((int) keyEvent.keycode, true);
  2697. String oldLocale (::setlocale (LC_ALL, nullptr));
  2698. ::setlocale (LC_ALL, "");
  2699. X11Symbols::getInstance()->xLookupString (&keyEvent, utf8, sizeof (utf8), &sym, nullptr);
  2700. if (oldLocale.isNotEmpty())
  2701. ::setlocale (LC_ALL, oldLocale.toRawUTF8());
  2702. unicodeChar = *CharPointer_UTF8 (utf8);
  2703. keyCode = (int) unicodeChar;
  2704. if (keyCode < 0x20)
  2705. keyCode = (int) X11Symbols::getInstance()->xkbKeycodeToKeysym (display, (::KeyCode) keyEvent.keycode, 0,
  2706. ModifierKeys::currentModifiers.isShiftDown() ? 1 : 0);
  2707. keyDownChange = (sym != NoSymbol) && ! updateKeyModifiersFromSym (sym, true);
  2708. }
  2709. bool keyPressed = false;
  2710. if ((sym & 0xff00) == 0xff00 || keyCode == XK_ISO_Left_Tab)
  2711. {
  2712. switch (sym) // Translate keypad
  2713. {
  2714. case XK_KP_Add: keyCode = XK_plus; break;
  2715. case XK_KP_Subtract: keyCode = XK_hyphen; break;
  2716. case XK_KP_Divide: keyCode = XK_slash; break;
  2717. case XK_KP_Multiply: keyCode = XK_asterisk; break;
  2718. case XK_KP_Enter: keyCode = XK_Return; break;
  2719. case XK_KP_Insert: keyCode = XK_Insert; break;
  2720. case XK_Delete:
  2721. case XK_KP_Delete: keyCode = XK_Delete; break;
  2722. case XK_KP_Left: keyCode = XK_Left; break;
  2723. case XK_KP_Right: keyCode = XK_Right; break;
  2724. case XK_KP_Up: keyCode = XK_Up; break;
  2725. case XK_KP_Down: keyCode = XK_Down; break;
  2726. case XK_KP_Home: keyCode = XK_Home; break;
  2727. case XK_KP_End: keyCode = XK_End; break;
  2728. case XK_KP_Page_Down: keyCode = XK_Page_Down; break;
  2729. case XK_KP_Page_Up: keyCode = XK_Page_Up; break;
  2730. case XK_KP_0: keyCode = XK_0; break;
  2731. case XK_KP_1: keyCode = XK_1; break;
  2732. case XK_KP_2: keyCode = XK_2; break;
  2733. case XK_KP_3: keyCode = XK_3; break;
  2734. case XK_KP_4: keyCode = XK_4; break;
  2735. case XK_KP_5: keyCode = XK_5; break;
  2736. case XK_KP_6: keyCode = XK_6; break;
  2737. case XK_KP_7: keyCode = XK_7; break;
  2738. case XK_KP_8: keyCode = XK_8; break;
  2739. case XK_KP_9: keyCode = XK_9; break;
  2740. default: break;
  2741. }
  2742. switch (keyCode)
  2743. {
  2744. case XK_Left:
  2745. case XK_Right:
  2746. case XK_Up:
  2747. case XK_Down:
  2748. case XK_Page_Up:
  2749. case XK_Page_Down:
  2750. case XK_End:
  2751. case XK_Home:
  2752. case XK_Delete:
  2753. case XK_Insert:
  2754. keyPressed = true;
  2755. keyCode = (keyCode & 0xff) | Keys::extendedKeyModifier;
  2756. break;
  2757. case XK_Tab:
  2758. case XK_Return:
  2759. case XK_Escape:
  2760. case XK_BackSpace:
  2761. keyPressed = true;
  2762. keyCode &= 0xff;
  2763. break;
  2764. case XK_ISO_Left_Tab:
  2765. keyPressed = true;
  2766. keyCode = XK_Tab & 0xff;
  2767. break;
  2768. default:
  2769. if (sym >= XK_F1 && sym <= XK_F35)
  2770. {
  2771. keyPressed = true;
  2772. keyCode = static_cast<int> ((sym & 0xff) | Keys::extendedKeyModifier);
  2773. }
  2774. break;
  2775. }
  2776. }
  2777. if (utf8[0] != 0 || ((sym & 0xff00) == 0 && sym >= 8))
  2778. keyPressed = true;
  2779. if (oldMods != ModifierKeys::currentModifiers)
  2780. peer->handleModifierKeysChange();
  2781. if (keyDownChange)
  2782. peer->handleKeyUpOrDown (true);
  2783. if (keyPressed)
  2784. peer->handleKeyPress (keyCode, unicodeChar);
  2785. }
  2786. void XWindowSystem::handleKeyReleaseEvent (LinuxComponentPeer* peer, const XKeyEvent& keyEvent) const
  2787. {
  2788. auto isKeyReleasePartOfAutoRepeat = [&]() -> bool
  2789. {
  2790. if (X11Symbols::getInstance()->xPending (display))
  2791. {
  2792. XEvent e;
  2793. X11Symbols::getInstance()->xPeekEvent (display, &e);
  2794. // Look for a subsequent key-down event with the same timestamp and keycode
  2795. return e.type == KeyPressEventType
  2796. && e.xkey.keycode == keyEvent.keycode
  2797. && e.xkey.time == keyEvent.time;
  2798. }
  2799. return false;
  2800. }();
  2801. if (! isKeyReleasePartOfAutoRepeat)
  2802. {
  2803. updateKeyStates ((int) keyEvent.keycode, false);
  2804. KeySym sym;
  2805. {
  2806. XWindowSystemUtilities::ScopedXLock xLock;
  2807. sym = X11Symbols::getInstance()->xkbKeycodeToKeysym (display, (::KeyCode) keyEvent.keycode, 0, 0);
  2808. }
  2809. auto oldMods = ModifierKeys::currentModifiers;
  2810. auto keyDownChange = (sym != NoSymbol) && ! updateKeyModifiersFromSym (sym, false);
  2811. if (oldMods != ModifierKeys::currentModifiers)
  2812. peer->handleModifierKeysChange();
  2813. if (keyDownChange)
  2814. peer->handleKeyUpOrDown (false);
  2815. }
  2816. }
  2817. void XWindowSystem::handleWheelEvent (LinuxComponentPeer* peer, const XButtonPressedEvent& buttonPressEvent, float amount) const
  2818. {
  2819. MouseWheelDetails wheel;
  2820. wheel.deltaX = 0.0f;
  2821. wheel.deltaY = amount;
  2822. wheel.isReversed = false;
  2823. wheel.isSmooth = false;
  2824. wheel.isInertial = false;
  2825. peer->handleMouseWheel (MouseInputSource::InputSourceType::mouse, getLogicalMousePos (buttonPressEvent, peer->getPlatformScaleFactor()),
  2826. getEventTime (buttonPressEvent), wheel);
  2827. }
  2828. void XWindowSystem::handleButtonPressEvent (LinuxComponentPeer* peer, const XButtonPressedEvent& buttonPressEvent, int buttonModifierFlag) const
  2829. {
  2830. ModifierKeys::currentModifiers = ModifierKeys::currentModifiers.withFlags (buttonModifierFlag);
  2831. peer->toFront (true);
  2832. peer->handleMouseEvent (MouseInputSource::InputSourceType::mouse, getLogicalMousePos (buttonPressEvent, peer->getPlatformScaleFactor()),
  2833. ModifierKeys::currentModifiers, MouseInputSource::defaultPressure,
  2834. MouseInputSource::defaultOrientation, getEventTime (buttonPressEvent), {});
  2835. }
  2836. void XWindowSystem::handleButtonPressEvent (LinuxComponentPeer* peer, const XButtonPressedEvent& buttonPressEvent) const
  2837. {
  2838. updateKeyModifiers ((int) buttonPressEvent.state);
  2839. auto mapIndex = (uint32) (buttonPressEvent.button - Button1);
  2840. if (mapIndex < (uint32) numElementsInArray (pointerMap))
  2841. {
  2842. switch (pointerMap[mapIndex])
  2843. {
  2844. case Keys::WheelUp: handleWheelEvent (peer, buttonPressEvent, 50.0f / 256.0f); break;
  2845. case Keys::WheelDown: handleWheelEvent (peer, buttonPressEvent, -50.0f / 256.0f); break;
  2846. case Keys::LeftButton: handleButtonPressEvent (peer, buttonPressEvent, ModifierKeys::leftButtonModifier); break;
  2847. case Keys::RightButton: handleButtonPressEvent (peer, buttonPressEvent, ModifierKeys::rightButtonModifier); break;
  2848. case Keys::MiddleButton: handleButtonPressEvent (peer, buttonPressEvent, ModifierKeys::middleButtonModifier); break;
  2849. default: break;
  2850. }
  2851. }
  2852. }
  2853. void XWindowSystem::handleButtonReleaseEvent (LinuxComponentPeer* peer, const XButtonReleasedEvent& buttonRelEvent) const
  2854. {
  2855. updateKeyModifiers ((int) buttonRelEvent.state);
  2856. if (peer->getParentWindow() != 0)
  2857. peer->updateWindowBounds();
  2858. auto mapIndex = (uint32) (buttonRelEvent.button - Button1);
  2859. if (mapIndex < (uint32) numElementsInArray (pointerMap))
  2860. {
  2861. switch (pointerMap[mapIndex])
  2862. {
  2863. case Keys::LeftButton: ModifierKeys::currentModifiers = ModifierKeys::currentModifiers.withoutFlags (ModifierKeys::leftButtonModifier); break;
  2864. case Keys::RightButton: ModifierKeys::currentModifiers = ModifierKeys::currentModifiers.withoutFlags (ModifierKeys::rightButtonModifier); break;
  2865. case Keys::MiddleButton: ModifierKeys::currentModifiers = ModifierKeys::currentModifiers.withoutFlags (ModifierKeys::middleButtonModifier); break;
  2866. default: break;
  2867. }
  2868. }
  2869. auto& dragState = dragAndDropStateMap[peer];
  2870. if (dragState.isDragging())
  2871. dragState.handleExternalDragButtonReleaseEvent();
  2872. peer->handleMouseEvent (MouseInputSource::InputSourceType::mouse, getLogicalMousePos (buttonRelEvent, peer->getPlatformScaleFactor()),
  2873. ModifierKeys::currentModifiers, MouseInputSource::defaultPressure, MouseInputSource::defaultOrientation, getEventTime (buttonRelEvent));
  2874. }
  2875. void XWindowSystem::handleMotionNotifyEvent (LinuxComponentPeer* peer, const XPointerMovedEvent& movedEvent) const
  2876. {
  2877. updateKeyModifiers ((int) movedEvent.state);
  2878. Keys::refreshStaleMouseKeys();
  2879. auto& dragState = dragAndDropStateMap[peer];
  2880. if (dragState.isDragging())
  2881. dragState.handleExternalDragMotionNotify();
  2882. peer->handleMouseEvent (MouseInputSource::InputSourceType::mouse, getLogicalMousePos (movedEvent, peer->getPlatformScaleFactor()),
  2883. ModifierKeys::currentModifiers, MouseInputSource::defaultPressure,
  2884. MouseInputSource::defaultOrientation, getEventTime (movedEvent));
  2885. }
  2886. void XWindowSystem::handleEnterNotifyEvent (LinuxComponentPeer* peer, const XEnterWindowEvent& enterEvent) const
  2887. {
  2888. if (peer->getParentWindow() != 0)
  2889. peer->updateWindowBounds();
  2890. if (! ModifierKeys::currentModifiers.isAnyMouseButtonDown())
  2891. {
  2892. updateKeyModifiers ((int) enterEvent.state);
  2893. peer->handleMouseEvent (MouseInputSource::InputSourceType::mouse, getLogicalMousePos (enterEvent, peer->getPlatformScaleFactor()),
  2894. ModifierKeys::currentModifiers, MouseInputSource::defaultPressure,
  2895. MouseInputSource::defaultOrientation, getEventTime (enterEvent));
  2896. }
  2897. }
  2898. void XWindowSystem::handleLeaveNotifyEvent (LinuxComponentPeer* peer, const XLeaveWindowEvent& leaveEvent) const
  2899. {
  2900. // Suppress the normal leave if we've got a pointer grab, or if
  2901. // it's a bogus one caused by clicking a mouse button when running
  2902. // in a Window manager
  2903. if (((! ModifierKeys::currentModifiers.isAnyMouseButtonDown()) && leaveEvent.mode == NotifyNormal)
  2904. || leaveEvent.mode == NotifyUngrab)
  2905. {
  2906. updateKeyModifiers ((int) leaveEvent.state);
  2907. peer->handleMouseEvent (MouseInputSource::InputSourceType::mouse, getLogicalMousePos (leaveEvent, peer->getPlatformScaleFactor()),
  2908. ModifierKeys::currentModifiers, MouseInputSource::defaultPressure,
  2909. MouseInputSource::defaultOrientation, getEventTime (leaveEvent));
  2910. }
  2911. }
  2912. void XWindowSystem::handleFocusInEvent (LinuxComponentPeer* peer) const
  2913. {
  2914. peer->isActiveApplication = true;
  2915. if (isFocused ((::Window) peer->getNativeHandle()) && ! peer->focused)
  2916. {
  2917. peer->focused = true;
  2918. peer->handleFocusGain();
  2919. }
  2920. }
  2921. void XWindowSystem::handleFocusOutEvent (LinuxComponentPeer* peer) const
  2922. {
  2923. if (! isFocused ((::Window) peer->getNativeHandle()) && peer->focused)
  2924. {
  2925. peer->focused = false;
  2926. peer->isActiveApplication = false;
  2927. peer->handleFocusLoss();
  2928. }
  2929. }
  2930. void XWindowSystem::handleExposeEvent (LinuxComponentPeer* peer, XExposeEvent& exposeEvent) const
  2931. {
  2932. // Batch together all pending expose events
  2933. XEvent nextEvent;
  2934. XWindowSystemUtilities::ScopedXLock xLock;
  2935. // if we have opengl contexts then just repaint them all
  2936. // regardless if this is really necessary
  2937. peer->repaintOpenGLContexts();
  2938. auto windowH = (::Window) peer->getNativeHandle();
  2939. if (exposeEvent.window != windowH)
  2940. {
  2941. Window child;
  2942. X11Symbols::getInstance()->xTranslateCoordinates (display, exposeEvent.window, windowH,
  2943. exposeEvent.x, exposeEvent.y, &exposeEvent.x, &exposeEvent.y,
  2944. &child);
  2945. }
  2946. // exposeEvent is in local window local coordinates so do not convert with
  2947. // physicalToScaled, but rather use currentScaleFactor
  2948. auto currentScaleFactor = peer->getPlatformScaleFactor();
  2949. peer->repaint (Rectangle<int> (exposeEvent.x, exposeEvent.y,
  2950. exposeEvent.width, exposeEvent.height) / currentScaleFactor);
  2951. while (X11Symbols::getInstance()->xEventsQueued (display, QueuedAfterFlush) > 0)
  2952. {
  2953. X11Symbols::getInstance()->xPeekEvent (display, &nextEvent);
  2954. if (nextEvent.type != Expose || nextEvent.xany.window != exposeEvent.window)
  2955. break;
  2956. X11Symbols::getInstance()->xNextEvent (display, &nextEvent);
  2957. auto& nextExposeEvent = (XExposeEvent&) nextEvent.xexpose;
  2958. peer->repaint (Rectangle<int> (nextExposeEvent.x, nextExposeEvent.y,
  2959. nextExposeEvent.width, nextExposeEvent.height) / currentScaleFactor);
  2960. }
  2961. }
  2962. void XWindowSystem::dismissBlockingModals (LinuxComponentPeer* peer) const
  2963. {
  2964. if (peer->getComponent().isCurrentlyBlockedByAnotherModalComponent())
  2965. if (auto* currentModalComp = Component::getCurrentlyModalComponent())
  2966. if (auto* otherPeer = currentModalComp->getPeer())
  2967. if ((otherPeer->getStyleFlags() & ComponentPeer::windowIsTemporary) != 0)
  2968. currentModalComp->inputAttemptWhenModal();
  2969. }
  2970. void XWindowSystem::handleConfigureNotifyEvent (LinuxComponentPeer* peer, XConfigureEvent& confEvent) const
  2971. {
  2972. peer->updateWindowBounds();
  2973. peer->updateBorderSize();
  2974. peer->handleMovedOrResized();
  2975. // if the native title bar is dragged, need to tell any active menus, etc.
  2976. if ((peer->getStyleFlags() & ComponentPeer::windowHasTitleBar) != 0)
  2977. dismissBlockingModals (peer);
  2978. auto windowH = (::Window) peer->getNativeHandle();
  2979. if (confEvent.window == windowH && confEvent.above != 0 && isFrontWindow (windowH))
  2980. peer->handleBroughtToFront();
  2981. }
  2982. void XWindowSystem::handleGravityNotify (LinuxComponentPeer* peer) const
  2983. {
  2984. peer->updateWindowBounds();
  2985. peer->updateBorderSize();
  2986. peer->handleMovedOrResized();
  2987. }
  2988. void XWindowSystem::propertyNotifyEvent (LinuxComponentPeer* peer, const XPropertyEvent& event) const
  2989. {
  2990. const auto isStateChangeEvent = [&]
  2991. {
  2992. if (event.atom != atoms.state)
  2993. return false;
  2994. return isMinimised (event.window);
  2995. };
  2996. const auto isHidden = [&]
  2997. {
  2998. if (event.atom != atoms.windowState)
  2999. return false;
  3000. XWindowSystemUtilities::ScopedXLock xLock;
  3001. XWindowSystemUtilities::GetXProperty prop (display, event.window, atoms.windowState, 0, 128, false, XA_ATOM);
  3002. if (! (prop.success && prop.actualFormat == 32 && prop.actualType == XA_ATOM))
  3003. return false;
  3004. const auto* data = unalignedPointerCast<const long*> (prop.data);
  3005. const auto end = data + prop.numItems;
  3006. return std::find (data, end, atoms.windowStateHidden) != end;
  3007. };
  3008. if (isStateChangeEvent() || isHidden())
  3009. dismissBlockingModals (peer);
  3010. if (event.atom == XWindowSystemUtilities::Atoms::getIfExists (display, "_NET_FRAME_EXTENTS"))
  3011. peer->updateBorderSize();
  3012. }
  3013. void XWindowSystem::handleMappingNotify (XMappingEvent& mappingEvent) const
  3014. {
  3015. if (mappingEvent.request != MappingPointer)
  3016. {
  3017. // Deal with modifier/keyboard mapping
  3018. XWindowSystemUtilities::ScopedXLock xLock;
  3019. X11Symbols::getInstance()->xRefreshKeyboardMapping (&mappingEvent);
  3020. updateModifierMappings();
  3021. }
  3022. }
  3023. void XWindowSystem::handleClientMessageEvent (LinuxComponentPeer* peer, XClientMessageEvent& clientMsg, XEvent& event) const
  3024. {
  3025. if (clientMsg.message_type == atoms.protocols && clientMsg.format == 32)
  3026. {
  3027. auto atom = (Atom) clientMsg.data.l[0];
  3028. if (atom == atoms.protocolList [XWindowSystemUtilities::Atoms::PING])
  3029. {
  3030. auto root = X11Symbols::getInstance()->xRootWindow (display, X11Symbols::getInstance()->xDefaultScreen (display));
  3031. clientMsg.window = root;
  3032. X11Symbols::getInstance()->xSendEvent (display, root, False, NoEventMask, &event);
  3033. X11Symbols::getInstance()->xFlush (display);
  3034. }
  3035. else if (atom == atoms.protocolList [XWindowSystemUtilities::Atoms::TAKE_FOCUS])
  3036. {
  3037. if ((peer->getStyleFlags() & ComponentPeer::windowIgnoresKeyPresses) == 0)
  3038. {
  3039. XWindowAttributes atts;
  3040. XWindowSystemUtilities::ScopedXLock xLock;
  3041. if (clientMsg.window != 0
  3042. && X11Symbols::getInstance()->xGetWindowAttributes (display, clientMsg.window, &atts))
  3043. {
  3044. if (atts.map_state == IsViewable)
  3045. {
  3046. auto windowH = (::Window) peer->getNativeHandle();
  3047. X11Symbols::getInstance()->xSetInputFocus (display, (clientMsg.window == windowH ? getFocusWindow (windowH)
  3048. : clientMsg.window),
  3049. RevertToParent, (::Time) clientMsg.data.l[1]);
  3050. }
  3051. }
  3052. }
  3053. }
  3054. else if (atom == atoms.protocolList [XWindowSystemUtilities::Atoms::DELETE_WINDOW])
  3055. {
  3056. peer->handleUserClosingWindow();
  3057. }
  3058. }
  3059. else if (clientMsg.message_type == atoms.XdndEnter)
  3060. {
  3061. dragAndDropStateMap[peer].handleDragAndDropEnter (clientMsg, peer);
  3062. }
  3063. else if (clientMsg.message_type == atoms.XdndLeave)
  3064. {
  3065. dragAndDropStateMap[peer].handleDragAndDropExit();
  3066. }
  3067. else if (clientMsg.message_type == atoms.XdndPosition)
  3068. {
  3069. dragAndDropStateMap[peer].handleDragAndDropPosition (clientMsg, peer);
  3070. }
  3071. else if (clientMsg.message_type == atoms.XdndDrop)
  3072. {
  3073. dragAndDropStateMap[peer].handleDragAndDropDrop (clientMsg, peer);
  3074. }
  3075. else if (clientMsg.message_type == atoms.XdndStatus)
  3076. {
  3077. dragAndDropStateMap[peer].handleExternalDragAndDropStatus (clientMsg);
  3078. }
  3079. else if (clientMsg.message_type == atoms.XdndFinished)
  3080. {
  3081. dragAndDropStateMap[peer].externalResetDragAndDrop();
  3082. }
  3083. else if (clientMsg.message_type == atoms.XembedMsgType && clientMsg.format == 32)
  3084. {
  3085. handleXEmbedMessage (peer, clientMsg);
  3086. }
  3087. }
  3088. void XWindowSystem::handleXEmbedMessage (LinuxComponentPeer* peer, XClientMessageEvent& clientMsg) const
  3089. {
  3090. switch (clientMsg.data.l[1])
  3091. {
  3092. case 0: // XEMBED_EMBEDDED_NOTIFY
  3093. peer->setParentWindow ((::Window) clientMsg.data.l[3]);
  3094. peer->updateWindowBounds();
  3095. peer->getComponent().setBounds (peer->getBounds());
  3096. break;
  3097. case 4: // XEMBED_FOCUS_IN
  3098. handleFocusInEvent (peer);
  3099. break;
  3100. case 5: // XEMBED_FOCUS_OUT
  3101. handleFocusOutEvent (peer);
  3102. break;
  3103. default:
  3104. break;
  3105. }
  3106. }
  3107. //==============================================================================
  3108. void XWindowSystem::dismissBlockingModals (LinuxComponentPeer* peer, const XConfigureEvent& configure) const
  3109. {
  3110. if (peer == nullptr)
  3111. return;
  3112. const auto peerHandle = peer->getWindowHandle();
  3113. if (configure.window != peerHandle && isParentWindowOf (configure.window, peerHandle))
  3114. dismissBlockingModals (peer);
  3115. }
  3116. void XWindowSystem::windowMessageReceive (XEvent& event)
  3117. {
  3118. if (event.xany.window != None)
  3119. {
  3120. #if JUCE_X11_SUPPORTS_XEMBED
  3121. if (! juce_handleXEmbedEvent (nullptr, &event))
  3122. #endif
  3123. {
  3124. auto* instance = XWindowSystem::getInstance();
  3125. if (auto* xSettings = instance->getXSettings())
  3126. {
  3127. if (event.xany.window == xSettings->getSettingsWindow())
  3128. {
  3129. if (event.xany.type == PropertyNotify)
  3130. xSettings->update();
  3131. else if (event.xany.type == DestroyNotify)
  3132. instance->initialiseXSettings();
  3133. return;
  3134. }
  3135. }
  3136. if (auto* peer = dynamic_cast<LinuxComponentPeer*> (getPeerFor (event.xany.window)))
  3137. {
  3138. XWindowSystem::getInstance()->handleWindowMessage (peer, event);
  3139. return;
  3140. }
  3141. if (event.type != ConfigureNotify)
  3142. return;
  3143. for (auto i = ComponentPeer::getNumPeers(); --i >= 0;)
  3144. instance->dismissBlockingModals (dynamic_cast<LinuxComponentPeer*> (ComponentPeer::getPeer (i)),
  3145. event.xconfigure);
  3146. }
  3147. }
  3148. else if (event.xany.type == KeymapNotify)
  3149. {
  3150. auto& keymapEvent = (const XKeymapEvent&) event.xkeymap;
  3151. memcpy (Keys::keyStates, keymapEvent.key_vector, 32);
  3152. }
  3153. }
  3154. //==============================================================================
  3155. JUCE_IMPLEMENT_SINGLETON (XWindowSystem)
  3156. Image createSnapshotOfNativeWindow (void* window)
  3157. {
  3158. ::Window root;
  3159. int wx, wy;
  3160. unsigned int ww, wh, bw, bitDepth;
  3161. XWindowSystemUtilities::ScopedXLock xLock;
  3162. const auto display = XWindowSystem::getInstance()->getDisplay();
  3163. if (! X11Symbols::getInstance()->xGetGeometry (display, (::Drawable) window, &root, &wx, &wy, &ww, &wh, &bw, &bitDepth))
  3164. return {};
  3165. const auto scale = []
  3166. {
  3167. if (auto* d = Desktop::getInstance().getDisplays().getPrimaryDisplay())
  3168. return d->scale;
  3169. return 1.0;
  3170. }();
  3171. auto image = Image { new XBitmapImage { X11Symbols::getInstance()->xGetImage (display,
  3172. (::Drawable) window,
  3173. 0,
  3174. 0,
  3175. ww,
  3176. wh,
  3177. AllPlanes,
  3178. ZPixmap) } };
  3179. return image.rescaled ((int) ((double) ww / scale), (int) ((double) wh / scale));
  3180. }
  3181. } // namespace juce