Audio plugin host https://kx.studio/carla
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.

4237 lines
152KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. extern ::Display* display;
  18. extern XContext windowHandleXContext;
  19. typedef void (*WindowMessageReceiveCallback) (XEvent&);
  20. extern WindowMessageReceiveCallback dispatchWindowMessage;
  21. //==============================================================================
  22. struct Atoms
  23. {
  24. Atoms()
  25. {
  26. protocols = getIfExists ("WM_PROTOCOLS");
  27. protocolList [TAKE_FOCUS] = getIfExists ("WM_TAKE_FOCUS");
  28. protocolList [DELETE_WINDOW] = getIfExists ("WM_DELETE_WINDOW");
  29. protocolList [PING] = getIfExists ("_NET_WM_PING");
  30. changeState = getIfExists ("WM_CHANGE_STATE");
  31. state = getIfExists ("WM_STATE");
  32. userTime = getCreating ("_NET_WM_USER_TIME");
  33. activeWin = getCreating ("_NET_ACTIVE_WINDOW");
  34. pid = getCreating ("_NET_WM_PID");
  35. windowType = getIfExists ("_NET_WM_WINDOW_TYPE");
  36. windowState = getIfExists ("_NET_WM_STATE");
  37. XdndAware = getCreating ("XdndAware");
  38. XdndEnter = getCreating ("XdndEnter");
  39. XdndLeave = getCreating ("XdndLeave");
  40. XdndPosition = getCreating ("XdndPosition");
  41. XdndStatus = getCreating ("XdndStatus");
  42. XdndDrop = getCreating ("XdndDrop");
  43. XdndFinished = getCreating ("XdndFinished");
  44. XdndSelection = getCreating ("XdndSelection");
  45. XdndTypeList = getCreating ("XdndTypeList");
  46. XdndActionList = getCreating ("XdndActionList");
  47. XdndActionCopy = getCreating ("XdndActionCopy");
  48. XdndActionPrivate = getCreating ("XdndActionPrivate");
  49. XdndActionDescription = getCreating ("XdndActionDescription");
  50. allowedMimeTypes[0] = getCreating ("UTF8_STRING");
  51. allowedMimeTypes[1] = getCreating ("text/plain;charset=utf-8");
  52. allowedMimeTypes[2] = getCreating ("text/plain");
  53. allowedMimeTypes[3] = getCreating ("text/uri-list");
  54. allowedActions[0] = getCreating ("XdndActionMove");
  55. allowedActions[1] = XdndActionCopy;
  56. allowedActions[2] = getCreating ("XdndActionLink");
  57. allowedActions[3] = getCreating ("XdndActionAsk");
  58. allowedActions[4] = XdndActionPrivate;
  59. }
  60. enum ProtocolItems
  61. {
  62. TAKE_FOCUS = 0,
  63. DELETE_WINDOW = 1,
  64. PING = 2
  65. };
  66. Atom protocols, protocolList[3], changeState, state, userTime,
  67. activeWin, pid, windowType, windowState,
  68. XdndAware, XdndEnter, XdndLeave, XdndPosition, XdndStatus,
  69. XdndDrop, XdndFinished, XdndSelection, XdndTypeList, XdndActionList,
  70. XdndActionDescription, XdndActionCopy, XdndActionPrivate,
  71. allowedActions[5],
  72. allowedMimeTypes[4];
  73. static const unsigned long DndVersion;
  74. static Atom getIfExists (const char* name) { return XInternAtom (display, name, True); }
  75. static Atom getCreating (const char* name) { return XInternAtom (display, name, False); }
  76. static String getName (const Atom& atom)
  77. {
  78. if (atom == None)
  79. return "None";
  80. return String (XGetAtomName (display, atom));
  81. }
  82. static bool isMimeTypeFile (const Atom& atom) { return getName (atom).equalsIgnoreCase ("text/uri-list"); }
  83. };
  84. const unsigned long Atoms::DndVersion = 3;
  85. //==============================================================================
  86. struct GetXProperty
  87. {
  88. GetXProperty (Window window, Atom atom, long offset, long length, bool shouldDelete, Atom requestedType)
  89. : data (nullptr)
  90. {
  91. success = (XGetWindowProperty (display, window, atom, offset, length,
  92. (Bool) shouldDelete, requestedType, &actualType,
  93. &actualFormat, &numItems, &bytesLeft, &data) == Success)
  94. && data != nullptr;
  95. }
  96. ~GetXProperty()
  97. {
  98. if (data != nullptr)
  99. XFree (data);
  100. }
  101. bool success;
  102. unsigned char* data;
  103. unsigned long numItems, bytesLeft;
  104. Atom actualType;
  105. int actualFormat;
  106. };
  107. //==============================================================================
  108. namespace Keys
  109. {
  110. enum MouseButtons
  111. {
  112. NoButton = 0,
  113. LeftButton = 1,
  114. MiddleButton = 2,
  115. RightButton = 3,
  116. WheelUp = 4,
  117. WheelDown = 5
  118. };
  119. static int AltMask = 0;
  120. static int NumLockMask = 0;
  121. static bool numLock = false;
  122. static bool capsLock = false;
  123. static char keyStates [32];
  124. static const int extendedKeyModifier = 0x10000000;
  125. }
  126. bool KeyPress::isKeyCurrentlyDown (const int keyCode)
  127. {
  128. if (display == nullptr)
  129. return false;
  130. int keysym;
  131. if (keyCode & Keys::extendedKeyModifier)
  132. {
  133. keysym = 0xff00 | (keyCode & 0xff);
  134. }
  135. else
  136. {
  137. keysym = keyCode;
  138. if (keysym == (XK_Tab & 0xff)
  139. || keysym == (XK_Return & 0xff)
  140. || keysym == (XK_Escape & 0xff)
  141. || keysym == (XK_BackSpace & 0xff))
  142. {
  143. keysym |= 0xff00;
  144. }
  145. }
  146. ScopedXLock xlock;
  147. const int keycode = XKeysymToKeycode (display, (KeySym) keysym);
  148. const int keybyte = keycode >> 3;
  149. const int keybit = (1 << (keycode & 7));
  150. return (Keys::keyStates [keybyte] & keybit) != 0;
  151. }
  152. //==============================================================================
  153. #if JUCE_USE_XSHM
  154. namespace XSHMHelpers
  155. {
  156. static int trappedErrorCode = 0;
  157. extern "C" int errorTrapHandler (Display*, XErrorEvent* err)
  158. {
  159. trappedErrorCode = err->error_code;
  160. return 0;
  161. }
  162. static bool isShmAvailable() noexcept
  163. {
  164. static bool isChecked = false;
  165. static bool isAvailable = false;
  166. if (! isChecked)
  167. {
  168. isChecked = true;
  169. if (display != nullptr)
  170. {
  171. int major, minor;
  172. Bool pixmaps;
  173. ScopedXLock xlock;
  174. if (XShmQueryVersion (display, &major, &minor, &pixmaps))
  175. {
  176. trappedErrorCode = 0;
  177. XErrorHandler oldHandler = XSetErrorHandler (errorTrapHandler);
  178. XShmSegmentInfo segmentInfo;
  179. zerostruct (segmentInfo);
  180. if (XImage* xImage = XShmCreateImage (display, DefaultVisual (display, DefaultScreen (display)),
  181. 24, ZPixmap, 0, &segmentInfo, 50, 50))
  182. {
  183. if ((segmentInfo.shmid = shmget (IPC_PRIVATE,
  184. (size_t) (xImage->bytes_per_line * xImage->height),
  185. IPC_CREAT | 0777)) >= 0)
  186. {
  187. segmentInfo.shmaddr = (char*) shmat (segmentInfo.shmid, 0, 0);
  188. if (segmentInfo.shmaddr != (void*) -1)
  189. {
  190. segmentInfo.readOnly = False;
  191. xImage->data = segmentInfo.shmaddr;
  192. XSync (display, False);
  193. if (XShmAttach (display, &segmentInfo) != 0)
  194. {
  195. XSync (display, False);
  196. XShmDetach (display, &segmentInfo);
  197. isAvailable = true;
  198. }
  199. }
  200. XFlush (display);
  201. XDestroyImage (xImage);
  202. shmdt (segmentInfo.shmaddr);
  203. }
  204. shmctl (segmentInfo.shmid, IPC_RMID, 0);
  205. XSetErrorHandler (oldHandler);
  206. if (trappedErrorCode != 0)
  207. isAvailable = false;
  208. }
  209. }
  210. }
  211. }
  212. return isAvailable;
  213. }
  214. }
  215. #endif
  216. //==============================================================================
  217. #if JUCE_USE_XRENDER
  218. namespace XRender
  219. {
  220. typedef Status (*tXRenderQueryVersion) (Display*, int*, int*);
  221. typedef XRenderPictFormat* (*tXRenderFindStandardFormat) (Display*, int);
  222. typedef XRenderPictFormat* (*tXRenderFindFormat) (Display*, unsigned long, XRenderPictFormat*, int);
  223. typedef XRenderPictFormat* (*tXRenderFindVisualFormat) (Display*, Visual*);
  224. static tXRenderQueryVersion xRenderQueryVersion = nullptr;
  225. static tXRenderFindStandardFormat xRenderFindStandardFormat = nullptr;
  226. static tXRenderFindFormat xRenderFindFormat = nullptr;
  227. static tXRenderFindVisualFormat xRenderFindVisualFormat = nullptr;
  228. static bool isAvailable()
  229. {
  230. static bool hasLoaded = false;
  231. if (! hasLoaded)
  232. {
  233. if (display != nullptr)
  234. {
  235. hasLoaded = true;
  236. ScopedXLock xlock;
  237. if (void* h = dlopen ("libXrender.so", RTLD_GLOBAL | RTLD_NOW))
  238. {
  239. xRenderQueryVersion = (tXRenderQueryVersion) dlsym (h, "XRenderQueryVersion");
  240. xRenderFindStandardFormat = (tXRenderFindStandardFormat) dlsym (h, "XRenderFindStandardFormat");
  241. xRenderFindFormat = (tXRenderFindFormat) dlsym (h, "XRenderFindFormat");
  242. xRenderFindVisualFormat = (tXRenderFindVisualFormat) dlsym (h, "XRenderFindVisualFormat");
  243. }
  244. if (xRenderQueryVersion != nullptr
  245. && xRenderFindStandardFormat != nullptr
  246. && xRenderFindFormat != nullptr
  247. && xRenderFindVisualFormat != nullptr)
  248. {
  249. int major, minor;
  250. if (xRenderQueryVersion (display, &major, &minor))
  251. return true;
  252. }
  253. }
  254. xRenderQueryVersion = nullptr;
  255. }
  256. return xRenderQueryVersion != nullptr;
  257. }
  258. static bool hasCompositingWindowManager() noexcept
  259. {
  260. return display != nullptr
  261. && XGetSelectionOwner (display, Atoms::getCreating ("_NET_WM_CM_S0")) != 0;
  262. }
  263. static XRenderPictFormat* findPictureFormat()
  264. {
  265. ScopedXLock xlock;
  266. XRenderPictFormat* pictFormat = nullptr;
  267. if (isAvailable())
  268. {
  269. pictFormat = xRenderFindStandardFormat (display, PictStandardARGB32);
  270. if (pictFormat == nullptr)
  271. {
  272. XRenderPictFormat desiredFormat;
  273. desiredFormat.type = PictTypeDirect;
  274. desiredFormat.depth = 32;
  275. desiredFormat.direct.alphaMask = 0xff;
  276. desiredFormat.direct.redMask = 0xff;
  277. desiredFormat.direct.greenMask = 0xff;
  278. desiredFormat.direct.blueMask = 0xff;
  279. desiredFormat.direct.alpha = 24;
  280. desiredFormat.direct.red = 16;
  281. desiredFormat.direct.green = 8;
  282. desiredFormat.direct.blue = 0;
  283. pictFormat = xRenderFindFormat (display,
  284. PictFormatType | PictFormatDepth
  285. | PictFormatRedMask | PictFormatRed
  286. | PictFormatGreenMask | PictFormatGreen
  287. | PictFormatBlueMask | PictFormatBlue
  288. | PictFormatAlphaMask | PictFormatAlpha,
  289. &desiredFormat,
  290. 0);
  291. }
  292. }
  293. return pictFormat;
  294. }
  295. }
  296. #endif
  297. //==============================================================================
  298. namespace Visuals
  299. {
  300. static Visual* findVisualWithDepth (const int desiredDepth) noexcept
  301. {
  302. ScopedXLock xlock;
  303. Visual* visual = nullptr;
  304. int numVisuals = 0;
  305. long desiredMask = VisualNoMask;
  306. XVisualInfo desiredVisual;
  307. desiredVisual.screen = DefaultScreen (display);
  308. desiredVisual.depth = desiredDepth;
  309. desiredMask = VisualScreenMask | VisualDepthMask;
  310. if (desiredDepth == 32)
  311. {
  312. desiredVisual.c_class = TrueColor;
  313. desiredVisual.red_mask = 0x00FF0000;
  314. desiredVisual.green_mask = 0x0000FF00;
  315. desiredVisual.blue_mask = 0x000000FF;
  316. desiredVisual.bits_per_rgb = 8;
  317. desiredMask |= VisualClassMask;
  318. desiredMask |= VisualRedMaskMask;
  319. desiredMask |= VisualGreenMaskMask;
  320. desiredMask |= VisualBlueMaskMask;
  321. desiredMask |= VisualBitsPerRGBMask;
  322. }
  323. if (XVisualInfo* xvinfos = XGetVisualInfo (display,
  324. desiredMask,
  325. &desiredVisual,
  326. &numVisuals))
  327. {
  328. for (int i = 0; i < numVisuals; i++)
  329. {
  330. if (xvinfos[i].depth == desiredDepth)
  331. {
  332. visual = xvinfos[i].visual;
  333. break;
  334. }
  335. }
  336. XFree (xvinfos);
  337. }
  338. return visual;
  339. }
  340. static Visual* findVisualFormat (const int desiredDepth, int& matchedDepth) noexcept
  341. {
  342. Visual* visual = nullptr;
  343. if (desiredDepth == 32)
  344. {
  345. #if JUCE_USE_XSHM
  346. if (XSHMHelpers::isShmAvailable())
  347. {
  348. #if JUCE_USE_XRENDER
  349. if (XRender::isAvailable())
  350. {
  351. if (XRenderPictFormat* pictFormat = XRender::findPictureFormat())
  352. {
  353. int numVisuals = 0;
  354. XVisualInfo desiredVisual;
  355. desiredVisual.screen = DefaultScreen (display);
  356. desiredVisual.depth = 32;
  357. desiredVisual.bits_per_rgb = 8;
  358. if (XVisualInfo* xvinfos = XGetVisualInfo (display,
  359. VisualScreenMask | VisualDepthMask | VisualBitsPerRGBMask,
  360. &desiredVisual, &numVisuals))
  361. {
  362. for (int i = 0; i < numVisuals; ++i)
  363. {
  364. XRenderPictFormat* pictVisualFormat = XRender::xRenderFindVisualFormat (display, xvinfos[i].visual);
  365. if (pictVisualFormat != nullptr
  366. && pictVisualFormat->type == PictTypeDirect
  367. && pictVisualFormat->direct.alphaMask)
  368. {
  369. visual = xvinfos[i].visual;
  370. matchedDepth = 32;
  371. break;
  372. }
  373. }
  374. XFree (xvinfos);
  375. }
  376. }
  377. }
  378. #endif
  379. if (visual == nullptr)
  380. {
  381. visual = findVisualWithDepth (32);
  382. if (visual != nullptr)
  383. matchedDepth = 32;
  384. }
  385. }
  386. #endif
  387. }
  388. if (visual == nullptr && desiredDepth >= 24)
  389. {
  390. visual = findVisualWithDepth (24);
  391. if (visual != nullptr)
  392. matchedDepth = 24;
  393. }
  394. if (visual == nullptr && desiredDepth >= 16)
  395. {
  396. visual = findVisualWithDepth (16);
  397. if (visual != nullptr)
  398. matchedDepth = 16;
  399. }
  400. return visual;
  401. }
  402. }
  403. //==============================================================================
  404. class XBitmapImage : public ImagePixelData
  405. {
  406. public:
  407. XBitmapImage (const Image::PixelFormat format, const int w, const int h,
  408. const bool clearImage, const unsigned int imageDepth_, Visual* visual)
  409. : ImagePixelData (format, w, h),
  410. imageDepth (imageDepth_),
  411. gc (None)
  412. {
  413. jassert (format == Image::RGB || format == Image::ARGB);
  414. pixelStride = (format == Image::RGB) ? 3 : 4;
  415. lineStride = ((w * pixelStride + 3) & ~3);
  416. ScopedXLock xlock;
  417. #if JUCE_USE_XSHM
  418. usingXShm = false;
  419. if ((imageDepth > 16) && XSHMHelpers::isShmAvailable())
  420. {
  421. zerostruct (segmentInfo);
  422. segmentInfo.shmid = -1;
  423. segmentInfo.shmaddr = (char *) -1;
  424. segmentInfo.readOnly = False;
  425. xImage = XShmCreateImage (display, visual, imageDepth, ZPixmap, 0,
  426. &segmentInfo, (unsigned int) w, (unsigned int) h);
  427. if (xImage != nullptr)
  428. {
  429. if ((segmentInfo.shmid = shmget (IPC_PRIVATE,
  430. (size_t) (xImage->bytes_per_line * xImage->height),
  431. IPC_CREAT | 0777)) >= 0)
  432. {
  433. if (segmentInfo.shmid != -1)
  434. {
  435. segmentInfo.shmaddr = (char*) shmat (segmentInfo.shmid, 0, 0);
  436. if (segmentInfo.shmaddr != (void*) -1)
  437. {
  438. segmentInfo.readOnly = False;
  439. xImage->data = segmentInfo.shmaddr;
  440. imageData = (uint8*) segmentInfo.shmaddr;
  441. if (XShmAttach (display, &segmentInfo) != 0)
  442. usingXShm = true;
  443. else
  444. jassertfalse;
  445. }
  446. else
  447. {
  448. shmctl (segmentInfo.shmid, IPC_RMID, 0);
  449. }
  450. }
  451. }
  452. }
  453. }
  454. if (! isUsingXShm())
  455. #endif
  456. {
  457. imageDataAllocated.allocate ((size_t) (lineStride * h), format == Image::ARGB && clearImage);
  458. imageData = imageDataAllocated;
  459. xImage = (XImage*) ::calloc (1, sizeof (XImage));
  460. xImage->width = w;
  461. xImage->height = h;
  462. xImage->xoffset = 0;
  463. xImage->format = ZPixmap;
  464. xImage->data = (char*) imageData;
  465. xImage->byte_order = ImageByteOrder (display);
  466. xImage->bitmap_unit = BitmapUnit (display);
  467. xImage->bitmap_bit_order = BitmapBitOrder (display);
  468. xImage->bitmap_pad = 32;
  469. xImage->depth = pixelStride * 8;
  470. xImage->bytes_per_line = lineStride;
  471. xImage->bits_per_pixel = pixelStride * 8;
  472. xImage->red_mask = 0x00FF0000;
  473. xImage->green_mask = 0x0000FF00;
  474. xImage->blue_mask = 0x000000FF;
  475. if (imageDepth == 16)
  476. {
  477. const int pixStride = 2;
  478. const int stride = ((w * pixStride + 3) & ~3);
  479. imageData16Bit.malloc ((size_t) (stride * h));
  480. xImage->data = imageData16Bit;
  481. xImage->bitmap_pad = 16;
  482. xImage->depth = pixStride * 8;
  483. xImage->bytes_per_line = stride;
  484. xImage->bits_per_pixel = pixStride * 8;
  485. xImage->red_mask = visual->red_mask;
  486. xImage->green_mask = visual->green_mask;
  487. xImage->blue_mask = visual->blue_mask;
  488. }
  489. if (! XInitImage (xImage))
  490. jassertfalse;
  491. }
  492. }
  493. ~XBitmapImage()
  494. {
  495. ScopedXLock xlock;
  496. if (gc != None)
  497. XFreeGC (display, gc);
  498. #if JUCE_USE_XSHM
  499. if (isUsingXShm())
  500. {
  501. XShmDetach (display, &segmentInfo);
  502. XFlush (display);
  503. XDestroyImage (xImage);
  504. shmdt (segmentInfo.shmaddr);
  505. shmctl (segmentInfo.shmid, IPC_RMID, 0);
  506. }
  507. else
  508. #endif
  509. {
  510. xImage->data = nullptr;
  511. XDestroyImage (xImage);
  512. }
  513. }
  514. LowLevelGraphicsContext* createLowLevelContext() override
  515. {
  516. sendDataChangeMessage();
  517. return new LowLevelGraphicsSoftwareRenderer (Image (this));
  518. }
  519. void initialiseBitmapData (Image::BitmapData& bitmap, int x, int y, Image::BitmapData::ReadWriteMode mode) override
  520. {
  521. bitmap.data = imageData + x * pixelStride + y * lineStride;
  522. bitmap.pixelFormat = pixelFormat;
  523. bitmap.lineStride = lineStride;
  524. bitmap.pixelStride = pixelStride;
  525. if (mode != Image::BitmapData::readOnly)
  526. sendDataChangeMessage();
  527. }
  528. ImagePixelData* clone() override
  529. {
  530. jassertfalse;
  531. return nullptr;
  532. }
  533. ImageType* createType() const override { return new NativeImageType(); }
  534. void blitToWindow (Window window, int dx, int dy, unsigned int dw, unsigned int dh, int sx, int sy)
  535. {
  536. ScopedXLock xlock;
  537. if (gc == None)
  538. {
  539. XGCValues gcvalues;
  540. gcvalues.foreground = None;
  541. gcvalues.background = None;
  542. gcvalues.function = GXcopy;
  543. gcvalues.plane_mask = AllPlanes;
  544. gcvalues.clip_mask = None;
  545. gcvalues.graphics_exposures = False;
  546. gc = XCreateGC (display, window,
  547. GCBackground | GCForeground | GCFunction | GCPlaneMask | GCClipMask | GCGraphicsExposures,
  548. &gcvalues);
  549. }
  550. if (imageDepth == 16)
  551. {
  552. const uint32 rMask = (uint32) xImage->red_mask;
  553. const uint32 gMask = (uint32) xImage->green_mask;
  554. const uint32 bMask = (uint32) xImage->blue_mask;
  555. const uint32 rShiftL = (uint32) jmax (0, getShiftNeeded (rMask));
  556. const uint32 rShiftR = (uint32) jmax (0, -getShiftNeeded (rMask));
  557. const uint32 gShiftL = (uint32) jmax (0, getShiftNeeded (gMask));
  558. const uint32 gShiftR = (uint32) jmax (0, -getShiftNeeded (gMask));
  559. const uint32 bShiftL = (uint32) jmax (0, getShiftNeeded (bMask));
  560. const uint32 bShiftR = (uint32) jmax (0, -getShiftNeeded (bMask));
  561. const Image::BitmapData srcData (Image (this), Image::BitmapData::readOnly);
  562. for (int y = sy; y < sy + (int)dh; ++y)
  563. {
  564. const uint8* p = srcData.getPixelPointer (sx, y);
  565. for (int x = sx; x < sx + (int)dw; ++x)
  566. {
  567. const PixelRGB* const pixel = (const PixelRGB*) p;
  568. p += srcData.pixelStride;
  569. XPutPixel (xImage, x, y,
  570. (((((uint32) pixel->getRed()) << rShiftL) >> rShiftR) & rMask)
  571. | (((((uint32) pixel->getGreen()) << gShiftL) >> gShiftR) & gMask)
  572. | (((((uint32) pixel->getBlue()) << bShiftL) >> bShiftR) & bMask));
  573. }
  574. }
  575. }
  576. // blit results to screen.
  577. #if JUCE_USE_XSHM
  578. if (isUsingXShm())
  579. XShmPutImage (display, (::Drawable) window, gc, xImage, sx, sy, dx, dy, dw, dh, True);
  580. else
  581. #endif
  582. XPutImage (display, (::Drawable) window, gc, xImage, sx, sy, dx, dy, dw, dh);
  583. }
  584. #if JUCE_USE_XSHM
  585. bool isUsingXShm() const noexcept { return usingXShm; }
  586. #endif
  587. private:
  588. //==============================================================================
  589. XImage* xImage;
  590. const unsigned int imageDepth;
  591. HeapBlock<uint8> imageDataAllocated;
  592. HeapBlock<char> imageData16Bit;
  593. int pixelStride, lineStride;
  594. uint8* imageData;
  595. GC gc;
  596. #if JUCE_USE_XSHM
  597. XShmSegmentInfo segmentInfo;
  598. bool usingXShm;
  599. #endif
  600. static int getShiftNeeded (const uint32 mask) noexcept
  601. {
  602. for (int i = 32; --i >= 0;)
  603. if (((mask >> i) & 1) != 0)
  604. return i - 7;
  605. jassertfalse;
  606. return 0;
  607. }
  608. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (XBitmapImage)
  609. };
  610. //==============================================================================
  611. #if JUCE_USE_XRANDR
  612. template <>
  613. struct ContainerDeletePolicy<XRRScreenResources>
  614. {
  615. static void destroy (XRRScreenResources* object);
  616. };
  617. template <>
  618. struct ContainerDeletePolicy<XRROutputInfo>
  619. {
  620. static void destroy (XRROutputInfo* object);
  621. };
  622. template <>
  623. struct ContainerDeletePolicy<XRRCrtcInfo>
  624. {
  625. static void destroy (XRRCrtcInfo* object);
  626. };
  627. #endif
  628. //==============================================================================
  629. class DisplayGeometry
  630. {
  631. private:
  632. //==============================================================================
  633. DisplayGeometry (::Display* dpy, double masterScale)
  634. {
  635. jassert (instance == nullptr);
  636. instance = this;
  637. queryDisplayInfos (dpy, masterScale);
  638. updatePositions();
  639. }
  640. public:
  641. //==============================================================================
  642. struct ExtendedInfo
  643. {
  644. // Unlike Desktop::Displays::Display, the following is in
  645. // physical pixels, i.e. the area is not scaled
  646. Rectangle<int> totalBounds;
  647. // Usable bounds is the usable area in local coordinates
  648. // with respect to the above totalBounds
  649. Rectangle<int> usableBounds;
  650. // top-left point of display in scaled coordinates. This
  651. // is different from totalBounds.getTopLeft() / scale,
  652. // because the neighbouring display may have a different
  653. // scale factor
  654. Point<int> topLeftScaled;
  655. double dpi, scale;
  656. bool isMain;
  657. };
  658. Array<ExtendedInfo> infos;
  659. //==============================================================================
  660. ExtendedInfo& findDisplayForRect (const Rectangle<int>& bounds, bool isScaledBounds)
  661. {
  662. int maxArea = -1;
  663. ExtendedInfo* retval = nullptr;
  664. for (int i = 0; i < infos.size(); ++i)
  665. {
  666. ExtendedInfo& dpy = infos.getReference (i);
  667. Rectangle<int> displayBounds = dpy.totalBounds;
  668. if (isScaledBounds)
  669. displayBounds = (displayBounds.withZeroOrigin() / dpy.scale) + dpy.topLeftScaled;
  670. displayBounds = displayBounds.getIntersection (bounds);
  671. int area = displayBounds.getWidth() * displayBounds.getHeight();
  672. if (area >= maxArea)
  673. {
  674. maxArea = area;
  675. retval = &dpy;
  676. }
  677. }
  678. return *retval;
  679. }
  680. ExtendedInfo& findDisplayForPoint (Point<int> pt, bool isScaledPoint)
  681. {
  682. int minDistance = (int) ((((unsigned int)(-1)) >> 1) - 1);
  683. ExtendedInfo* retval = nullptr;
  684. for (int i = 0; i < infos.size(); ++i)
  685. {
  686. ExtendedInfo& dpy = infos.getReference (i);
  687. Rectangle<int> displayBounds = dpy.totalBounds;
  688. if (isScaledPoint)
  689. displayBounds = (displayBounds.withZeroOrigin() / dpy.scale) + dpy.topLeftScaled;
  690. if (displayBounds.contains (pt))
  691. return dpy;
  692. int distance = displayBounds.getCentre().getDistanceFrom (pt);
  693. if (distance <= minDistance)
  694. {
  695. minDistance = distance;
  696. retval = &dpy;
  697. }
  698. }
  699. return *retval;
  700. }
  701. //==============================================================================
  702. static Rectangle<int> physicalToScaled (const Rectangle<int>& physicalBounds)
  703. {
  704. // first find with which display physicalBounds has the most overlap
  705. ExtendedInfo& dpy = getInstance().findDisplayForRect (physicalBounds, false);
  706. // convert to local screen bounds
  707. Rectangle<int> retval = physicalBounds - dpy.totalBounds.getTopLeft();
  708. // now we can safely scale the coordinates and convert to global again
  709. return (retval / dpy.scale) + dpy.topLeftScaled;
  710. }
  711. static Rectangle<int> scaledToPhysical (const Rectangle<int>& scaledBounds)
  712. {
  713. // first find with which display physicalBounds has the most overlap
  714. ExtendedInfo& dpy = getInstance().findDisplayForRect (scaledBounds, true);
  715. // convert to local screen bounds
  716. Rectangle<int> retval = scaledBounds - dpy.topLeftScaled;
  717. // now we can safely scale the coordinates and convert to global again
  718. return (retval * dpy.scale) + dpy.totalBounds.getTopLeft();
  719. }
  720. //==============================================================================
  721. template <typename ValueType>
  722. static Point<ValueType> physicalToScaled (const Point<ValueType>& physicalPoint)
  723. {
  724. ExtendedInfo& dpy = getInstance().findDisplayForPoint (physicalPoint.roundToInt(), false);
  725. Point<ValueType> scaledTopLeft =
  726. Point<ValueType> (dpy.topLeftScaled.getX(), dpy.topLeftScaled.getY());
  727. Point<ValueType> physicalTopLeft =
  728. Point<ValueType> (dpy.totalBounds.getX(), dpy.totalBounds.getY());
  729. return ((physicalPoint - physicalTopLeft) / dpy.scale) + scaledTopLeft;
  730. }
  731. template <typename ValueType>
  732. static Point<ValueType> scaledToPhysical (const Point<ValueType>& scaledPoint)
  733. {
  734. ExtendedInfo& dpy = getInstance().findDisplayForPoint (scaledPoint.roundToInt(), true);
  735. Point<ValueType> scaledTopLeft =
  736. Point<ValueType> (dpy.topLeftScaled.getX(), dpy.topLeftScaled.getY());
  737. Point<ValueType> physicalTopLeft =
  738. Point<ValueType> (dpy.totalBounds.getX(), dpy.totalBounds.getY());
  739. return ((scaledPoint - scaledTopLeft) * dpy.scale) + physicalTopLeft;
  740. }
  741. //==============================================================================
  742. static DisplayGeometry& getInstance()
  743. {
  744. jassert (instance != nullptr);
  745. return *instance;
  746. }
  747. static DisplayGeometry& getOrCreateInstance (::Display* dpy, double masterScale)
  748. {
  749. if (instance == nullptr)
  750. new DisplayGeometry (dpy, masterScale);
  751. return getInstance();
  752. }
  753. private:
  754. //==============================================================================
  755. static DisplayGeometry* instance;
  756. //==============================================================================
  757. #if JUCE_USE_XINERAMA
  758. static Array<XineramaScreenInfo> XineramaQueryDisplays (::Display* dpy)
  759. {
  760. typedef Bool (*tXineramaIsActive) (::Display*);
  761. typedef XineramaScreenInfo* (*tXineramaQueryScreens) (::Display*, int*);
  762. int major_opcode, first_event, first_error;
  763. if (XQueryExtension (dpy, "XINERAMA", &major_opcode, &first_event, &first_error))
  764. {
  765. static void* libXinerama = nullptr;
  766. static tXineramaIsActive isActiveFuncPtr = nullptr;
  767. static tXineramaQueryScreens xineramaQueryScreens = nullptr;
  768. if (libXinerama == nullptr)
  769. {
  770. libXinerama = dlopen ("libXinerama.so", RTLD_GLOBAL | RTLD_NOW);
  771. if (libXinerama == nullptr)
  772. libXinerama = dlopen ("libXinerama.so.1", RTLD_GLOBAL | RTLD_NOW);
  773. if (libXinerama != nullptr)
  774. {
  775. isActiveFuncPtr = (tXineramaIsActive) dlsym (libXinerama, "XineramaIsActive");
  776. xineramaQueryScreens = (tXineramaQueryScreens) dlsym (libXinerama, "XineramaQueryScreens");
  777. }
  778. }
  779. if (isActiveFuncPtr != nullptr && xineramaQueryScreens != nullptr && isActiveFuncPtr (dpy) != 0)
  780. {
  781. int numScreens;
  782. if (XineramaScreenInfo* xinfo = xineramaQueryScreens (dpy, &numScreens))
  783. {
  784. Array<XineramaScreenInfo> infos (xinfo, numScreens);
  785. XFree (xinfo);
  786. return infos;
  787. }
  788. }
  789. }
  790. return Array<XineramaScreenInfo>();
  791. }
  792. #endif
  793. //==============================================================================
  794. #if JUCE_USE_XRANDR
  795. friend struct ContainerDeletePolicy<XRRScreenResources>;
  796. friend struct ContainerDeletePolicy<XRROutputInfo>;
  797. friend struct ContainerDeletePolicy<XRRCrtcInfo>;
  798. class XRandrWrapper
  799. {
  800. private:
  801. XRandrWrapper()
  802. : libXrandr (nullptr),
  803. getScreenResourcesPtr (nullptr),
  804. freeScreenResourcesPtr (nullptr),
  805. getOutputInfoPtr (nullptr),
  806. freeOutputInfoPtr (nullptr),
  807. getCrtcInfoPtr (nullptr),
  808. freeCrtcInfoPtr (nullptr),
  809. getOutputPrimaryPtr (nullptr)
  810. {
  811. if (libXrandr == nullptr)
  812. {
  813. libXrandr = dlopen ("libXrandr.so", RTLD_GLOBAL | RTLD_NOW);
  814. if (libXrandr == nullptr)
  815. libXrandr = dlopen ("libXinerama.so.2", RTLD_GLOBAL | RTLD_NOW);
  816. if (libXrandr != nullptr)
  817. {
  818. getScreenResourcesPtr = (tXRRGetScreenResources) dlsym (libXrandr, "XRRGetScreenResources");
  819. freeScreenResourcesPtr = (tXRRFreeScreenResources) dlsym (libXrandr, "XRRFreeScreenResources");
  820. getOutputInfoPtr = (tXRRGetOutputInfo) dlsym (libXrandr, "XRRGetOutputInfo");
  821. freeOutputInfoPtr = (tXRRFreeOutputInfo) dlsym (libXrandr, "XRRFreeOutputInfo");
  822. getCrtcInfoPtr = (tXRRGetCrtcInfo) dlsym (libXrandr, "XRRGetCrtcInfo");
  823. freeCrtcInfoPtr = (tXRRFreeCrtcInfo) dlsym (libXrandr, "XRRFreeCrtcInfo");
  824. getOutputPrimaryPtr = (tXRRGetOutputPrimary) dlsym (libXrandr, "XRRGetOutputPrimary");
  825. }
  826. }
  827. instance = this;
  828. }
  829. public:
  830. //==============================================================================
  831. static XRandrWrapper& getInstance()
  832. {
  833. if (instance == nullptr)
  834. instance = new XRandrWrapper();
  835. return *instance;
  836. }
  837. //==============================================================================
  838. XRRScreenResources* getScreenResources (::Display* dpy, ::Window window)
  839. {
  840. if (getScreenResourcesPtr != nullptr)
  841. return getScreenResourcesPtr (dpy, window);
  842. return nullptr;
  843. }
  844. XRROutputInfo* getOutputInfo (::Display* dpy, XRRScreenResources* resources, RROutput output)
  845. {
  846. if (getOutputInfoPtr != nullptr)
  847. return getOutputInfoPtr (dpy, resources, output);
  848. return nullptr;
  849. }
  850. XRRCrtcInfo* getCrtcInfo (::Display* dpy, XRRScreenResources* resources, RRCrtc crtc)
  851. {
  852. if (getCrtcInfoPtr != nullptr)
  853. return getCrtcInfoPtr (dpy, resources, crtc);
  854. return nullptr;
  855. }
  856. RROutput getOutputPrimary (::Display* dpy, ::Window window)
  857. {
  858. if (getOutputPrimaryPtr != nullptr)
  859. return getOutputPrimaryPtr (dpy, window);
  860. return 0;
  861. }
  862. private:
  863. //==============================================================================
  864. friend struct ContainerDeletePolicy<XRRScreenResources>;
  865. friend struct ContainerDeletePolicy<XRROutputInfo>;
  866. friend struct ContainerDeletePolicy<XRRCrtcInfo>;
  867. void freeScreenResources (XRRScreenResources* ptr)
  868. {
  869. if (freeScreenResourcesPtr != nullptr)
  870. freeScreenResourcesPtr (ptr);
  871. }
  872. void freeOutputInfo (XRROutputInfo* ptr)
  873. {
  874. if (freeOutputInfoPtr != nullptr)
  875. freeOutputInfoPtr (ptr);
  876. }
  877. void freeCrtcInfo (XRRCrtcInfo* ptr)
  878. {
  879. if (freeCrtcInfoPtr != nullptr)
  880. freeCrtcInfoPtr (ptr);
  881. }
  882. private:
  883. static XRandrWrapper* instance;
  884. typedef XRRScreenResources* (*tXRRGetScreenResources) (::Display*, ::Window);
  885. typedef void (*tXRRFreeScreenResources) (XRRScreenResources*);
  886. typedef XRROutputInfo* (*tXRRGetOutputInfo) (::Display*, XRRScreenResources*, RROutput);
  887. typedef void (*tXRRFreeOutputInfo) (XRROutputInfo*);
  888. typedef XRRCrtcInfo* (*tXRRGetCrtcInfo) (::Display*, XRRScreenResources*, RRCrtc);
  889. typedef void (*tXRRFreeCrtcInfo) (XRRCrtcInfo*);
  890. typedef RROutput (*tXRRGetOutputPrimary) (::Display*, ::Window);
  891. void* libXrandr;
  892. tXRRGetScreenResources getScreenResourcesPtr;
  893. tXRRFreeScreenResources freeScreenResourcesPtr;
  894. tXRRGetOutputInfo getOutputInfoPtr;
  895. tXRRFreeOutputInfo freeOutputInfoPtr;
  896. tXRRGetCrtcInfo getCrtcInfoPtr;
  897. tXRRFreeCrtcInfo freeCrtcInfoPtr;
  898. tXRRGetOutputPrimary getOutputPrimaryPtr;
  899. };
  900. #endif
  901. static double getDisplayDPI (int index)
  902. {
  903. double dpiX = (DisplayWidth (display, index) * 25.4) / DisplayWidthMM (display, index);
  904. double dpiY = (DisplayHeight (display, index) * 25.4) / DisplayHeightMM (display, index);
  905. return (dpiX + dpiY) / 2.0;
  906. }
  907. static double getScaleForDisplay (const String& name, const ExtendedInfo& info)
  908. {
  909. if (! name.isEmpty())
  910. {
  911. // Ubuntu and derived distributions now save a per-display scale factor as a configuration
  912. // variable. This can be changed in the Monitor system settings panel.
  913. ChildProcess dconf;
  914. if (File ("/usr/bin/dconf").existsAsFile() &&
  915. dconf.start ("/usr/bin/dconf read /com/ubuntu/user-interface/scale-factor", ChildProcess::wantStdOut))
  916. {
  917. if (dconf.waitForProcessToFinish (200))
  918. {
  919. String jsonOutput = dconf.readAllProcessOutput().replaceCharacter ('\'', '"');
  920. if (dconf.getExitCode() == 0 && jsonOutput.isNotEmpty())
  921. {
  922. var jsonVar = JSON::parse (jsonOutput);
  923. if (DynamicObject* object = jsonVar.getDynamicObject())
  924. {
  925. var scaleFactorVar = object->getProperty (name);
  926. if (! scaleFactorVar.isVoid())
  927. {
  928. double scaleFactor = ((double) scaleFactorVar) / 8.0;
  929. if (scaleFactor > 0.0)
  930. return scaleFactor;
  931. }
  932. }
  933. }
  934. }
  935. }
  936. }
  937. {
  938. // Other gnome based distros now use gsettings for a global scale factor
  939. ChildProcess gsettings;
  940. if (File ("/usr/bin/gsettings").existsAsFile() &&
  941. gsettings.start ("/usr/bin/gsettings get org.gnome.desktop.interface scaling-factor", ChildProcess::wantStdOut))
  942. {
  943. if (gsettings.waitForProcessToFinish (200))
  944. {
  945. StringArray gsettingsOutput = StringArray::fromTokens (gsettings.readAllProcessOutput(), true);
  946. if (gsettingsOutput.size() >= 2 && gsettingsOutput[1].length() > 0)
  947. {
  948. double scaleFactor = gsettingsOutput[1].getDoubleValue();
  949. if (scaleFactor > 0.0)
  950. return scaleFactor;
  951. }
  952. }
  953. }
  954. }
  955. // If no scale factor is set by GNOME or Ubuntu then calculate from monitor dpi
  956. // We use the same approach as chromium which simply divides the dpi by 96
  957. // and then rounds the result
  958. return round (info.dpi / 150.0);
  959. }
  960. //==============================================================================
  961. void queryDisplayInfos (::Display* dpy, double masterScale) noexcept
  962. {
  963. ScopedXLock xlock;
  964. #if JUCE_USE_XRANDR
  965. {
  966. int major_opcode, first_event, first_error;
  967. if (XQueryExtension (dpy, "RANDR", &major_opcode, &first_event, &first_error))
  968. {
  969. XRandrWrapper& xrandr = XRandrWrapper::getInstance();
  970. ScopedPointer<XRRScreenResources> screens;
  971. const int numMonitors = ScreenCount (dpy);
  972. RROutput mainDisplay = xrandr.getOutputPrimary (dpy, RootWindow (dpy, 0));
  973. for (int i = 0; i < numMonitors; ++i)
  974. {
  975. if ((screens = xrandr.getScreenResources (dpy, RootWindow (dpy, i))).get())
  976. {
  977. for (int j = 0; j < screens->noutput; ++j)
  978. {
  979. if (! screens->outputs[j])
  980. continue;
  981. // Xrandr on the raspberry pi fails to determine the main display (mainDisplay == 0)!
  982. // Detect this edge case and make the first found display the main display
  983. if (! mainDisplay)
  984. mainDisplay = screens->outputs[j];
  985. ScopedPointer<XRROutputInfo> output;
  986. if ((output = xrandr.getOutputInfo (dpy, screens.get(), screens->outputs[j])).get())
  987. {
  988. if (! output->crtc)
  989. continue;
  990. ScopedPointer<XRRCrtcInfo> crtc;
  991. if ((crtc = xrandr.getCrtcInfo (dpy, screens.get(), output->crtc)).get())
  992. {
  993. ExtendedInfo e;
  994. e.totalBounds = Rectangle<int> (crtc->x, crtc->y,
  995. (int) crtc->width, (int) crtc->height);
  996. e.usableBounds = e.totalBounds.withZeroOrigin(); // Support for usable area is not implemented in JUCE yet
  997. e.topLeftScaled = e.totalBounds.getTopLeft();
  998. e.isMain = (mainDisplay == screens->outputs[j]) && (i == 0);
  999. e.dpi = getDisplayDPI (0);
  1000. // The raspberry pi returns a zero sized display, so we need to guard for divide-by-zero
  1001. if (output->mm_width > 0 && output->mm_height > 0)
  1002. e.dpi = ((static_cast<double> (crtc->width) * 25.4 * 0.5) / static_cast<double> (output->mm_width))
  1003. + ((static_cast<double> (crtc->height) * 25.4 * 0.5) / static_cast<double> (output->mm_height));
  1004. e.scale = masterScale * getScaleForDisplay (output->name, e);
  1005. infos.add (e);
  1006. }
  1007. }
  1008. }
  1009. }
  1010. }
  1011. }
  1012. }
  1013. if (infos.size() == 0)
  1014. #endif
  1015. #if JUCE_USE_XINERAMA
  1016. {
  1017. Array<XineramaScreenInfo> screens = XineramaQueryDisplays (dpy);
  1018. int numMonitors = screens.size();
  1019. for (int index = 0; index < numMonitors; ++index)
  1020. {
  1021. for (int j = numMonitors; --j >= 0;)
  1022. {
  1023. if (screens[j].screen_number == index)
  1024. {
  1025. ExtendedInfo e;
  1026. e.totalBounds = Rectangle<int> (screens[j].x_org,
  1027. screens[j].y_org,
  1028. screens[j].width,
  1029. screens[j].height);
  1030. e.usableBounds = e.totalBounds.withZeroOrigin(); // Support for usable area is not implemented in JUCE yet
  1031. e.topLeftScaled = e.totalBounds.getTopLeft(); // this will be overwritten by updatePositions later
  1032. e.isMain = (index == 0);
  1033. e.scale = masterScale;
  1034. e.dpi = getDisplayDPI (0); // (all screens share the same DPI)
  1035. infos.add (e);
  1036. }
  1037. }
  1038. }
  1039. }
  1040. if (infos.size() == 0)
  1041. #endif
  1042. {
  1043. Atom hints = Atoms::getIfExists ("_NET_WORKAREA");
  1044. if (hints != None)
  1045. {
  1046. const int numMonitors = ScreenCount (dpy);
  1047. for (int i = 0; i < numMonitors; ++i)
  1048. {
  1049. GetXProperty prop (RootWindow (dpy, i), hints, 0, 4, false, XA_CARDINAL);
  1050. if (prop.success && prop.actualType == XA_CARDINAL && prop.actualFormat == 32 && prop.numItems == 4)
  1051. {
  1052. const long* const position = (const long*) prop.data;
  1053. ExtendedInfo e;
  1054. e.totalBounds = Rectangle<int> ((int) position[0], (int) position[1],
  1055. (int) position[2], (int) position[3]);
  1056. e.usableBounds = e.totalBounds.withZeroOrigin(); // Support for usable area is not implemented in JUCE yet
  1057. e.topLeftScaled = e.totalBounds.getTopLeft(); // this will be overwritten by updatePositions later
  1058. e.isMain = (infos.size() == 0);
  1059. e.scale = masterScale;
  1060. e.dpi = getDisplayDPI (i);
  1061. infos.add (e);
  1062. }
  1063. }
  1064. }
  1065. if (infos.size() == 0)
  1066. {
  1067. ExtendedInfo e;
  1068. e.totalBounds = Rectangle<int> (DisplayWidth (dpy, DefaultScreen (dpy)),
  1069. DisplayHeight (dpy, DefaultScreen (dpy)));
  1070. e.usableBounds = e.totalBounds; // Support for usable area is not implemented in JUCE yet
  1071. e.topLeftScaled = e.totalBounds.getTopLeft(); // this will be overwritten by updatePositions later
  1072. e.isMain = true;
  1073. e.scale = masterScale;
  1074. e.dpi = getDisplayDPI (0);
  1075. infos.add (e);
  1076. }
  1077. }
  1078. }
  1079. //==============================================================================
  1080. struct SortByCoordinate
  1081. {
  1082. bool sortByYCoordinate;
  1083. SortByCoordinate (bool byYCoordinate)
  1084. : sortByYCoordinate (byYCoordinate)
  1085. {
  1086. }
  1087. int compareElements (const ExtendedInfo* a, const ExtendedInfo* b)
  1088. {
  1089. int coordinateA, coordinateB;
  1090. if (sortByYCoordinate)
  1091. {
  1092. coordinateA = a->totalBounds.getY();
  1093. coordinateB = b->totalBounds.getY();
  1094. }
  1095. else
  1096. {
  1097. coordinateA = a->totalBounds.getX();
  1098. coordinateB = b->totalBounds.getX();
  1099. }
  1100. return coordinateA - coordinateB;
  1101. }
  1102. };
  1103. //==============================================================================
  1104. void updateScaledDisplayCoordinate(bool updateYCoordinates)
  1105. {
  1106. if (infos.size() < 2)
  1107. return;
  1108. Array<ExtendedInfo*> copy;
  1109. {
  1110. SortByCoordinate sorter (updateYCoordinates);
  1111. for (int i = 0; i < infos.size(); ++i)
  1112. copy.addSorted (sorter, &infos.getReference (i));
  1113. }
  1114. for (int i = 1; i < copy.size(); ++i)
  1115. {
  1116. ExtendedInfo& current = *copy[i];
  1117. // Is this screen's position aligned to any other previous display?
  1118. for (int j = i - 1; j >= 0; --j)
  1119. {
  1120. ExtendedInfo& other = *copy[j];
  1121. int prevCoordinate = updateYCoordinates ? other.totalBounds.getBottom() : other.totalBounds.getRight();
  1122. int curCoordinate = updateYCoordinates ? current.totalBounds.getY() : current.totalBounds.getX();
  1123. if (prevCoordinate == curCoordinate)
  1124. {
  1125. // both displays are aligned! As "other" comes before "current" in the array, it must already
  1126. // have a valid topLeftScaled which we can use
  1127. Point<int> topLeftScaled = other.topLeftScaled;
  1128. topLeftScaled += Point<int> (other.totalBounds.getWidth(), other.totalBounds.getHeight()) / other.scale;
  1129. if (updateYCoordinates)
  1130. current.topLeftScaled.setY (topLeftScaled.getY());
  1131. else
  1132. current.topLeftScaled.setX (topLeftScaled.getX());
  1133. break;
  1134. }
  1135. }
  1136. }
  1137. }
  1138. void updatePositions()
  1139. {
  1140. updateScaledDisplayCoordinate (false);
  1141. updateScaledDisplayCoordinate (true);
  1142. }
  1143. };
  1144. DisplayGeometry* DisplayGeometry::instance = nullptr;
  1145. #if JUCE_USE_XRANDR
  1146. DisplayGeometry::XRandrWrapper* DisplayGeometry::XRandrWrapper::instance = nullptr;
  1147. void ContainerDeletePolicy<XRRScreenResources>::destroy (XRRScreenResources* ptr)
  1148. {
  1149. if (ptr != nullptr)
  1150. DisplayGeometry::XRandrWrapper::getInstance().freeScreenResources (ptr);
  1151. }
  1152. void ContainerDeletePolicy<XRROutputInfo>::destroy (XRROutputInfo* ptr)
  1153. {
  1154. if (ptr != nullptr)
  1155. DisplayGeometry::XRandrWrapper::getInstance().freeOutputInfo (ptr);
  1156. }
  1157. void ContainerDeletePolicy<XRRCrtcInfo>::destroy (XRRCrtcInfo* ptr)
  1158. {
  1159. if (ptr != nullptr)
  1160. DisplayGeometry::XRandrWrapper::getInstance().freeCrtcInfo (ptr);
  1161. }
  1162. #endif
  1163. //==============================================================================
  1164. namespace PixmapHelpers
  1165. {
  1166. Pixmap createColourPixmapFromImage (Display* display, const Image& image)
  1167. {
  1168. ScopedXLock xlock;
  1169. const unsigned int width = (unsigned int) image.getWidth();
  1170. const unsigned int height = (unsigned int) image.getHeight();
  1171. HeapBlock<uint32> colour (width * height);
  1172. int index = 0;
  1173. for (int y = 0; y < (int) height; ++y)
  1174. for (int x = 0; x < (int) width; ++x)
  1175. colour[index++] = image.getPixelAt (x, y).getARGB();
  1176. XImage* ximage = XCreateImage (display, CopyFromParent, 24, ZPixmap,
  1177. 0, reinterpret_cast<char*> (colour.getData()),
  1178. width, height, 32, 0);
  1179. Pixmap pixmap = XCreatePixmap (display, DefaultRootWindow (display),
  1180. width, height, 24);
  1181. GC gc = XCreateGC (display, pixmap, 0, 0);
  1182. XPutImage (display, pixmap, gc, ximage, 0, 0, 0, 0, width, height);
  1183. XFreeGC (display, gc);
  1184. return pixmap;
  1185. }
  1186. Pixmap createMaskPixmapFromImage (Display* display, const Image& image)
  1187. {
  1188. ScopedXLock xlock;
  1189. const unsigned int width = (unsigned int) image.getWidth();
  1190. const unsigned int height = (unsigned int) image.getHeight();
  1191. const unsigned int stride = (width + 7) >> 3;
  1192. HeapBlock<char> mask;
  1193. mask.calloc (stride * height);
  1194. const bool msbfirst = (BitmapBitOrder (display) == MSBFirst);
  1195. for (unsigned int y = 0; y < height; ++y)
  1196. {
  1197. for (unsigned int x = 0; x < width; ++x)
  1198. {
  1199. const char bit = (char) (1 << (msbfirst ? (7 - (x & 7)) : (x & 7)));
  1200. const unsigned int offset = y * stride + (x >> 3);
  1201. if (image.getPixelAt ((int) x, (int) y).getAlpha() >= 128)
  1202. mask[offset] |= bit;
  1203. }
  1204. }
  1205. return XCreatePixmapFromBitmapData (display, DefaultRootWindow (display),
  1206. mask.getData(), width, height, 1, 0, 1);
  1207. }
  1208. }
  1209. static void* createDraggingHandCursor()
  1210. {
  1211. static unsigned char dragHandData[] = { 71,73,70,56,57,97,16,0,16,0,145,2,0,0,0,0,255,255,255,0,
  1212. 0,0,0,0,0,33,249,4,1,0,0,2,0,44,0,0,0,0,16,0, 16,0,0,2,52,148,47,0,200,185,16,130,90,12,74,139,107,84,123,39,
  1213. 132,117,151,116,132,146,248,60,209,138,98,22,203,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 };
  1214. const int dragHandDataSize = 99;
  1215. return CustomMouseCursorInfo (ImageFileFormat::loadFrom (dragHandData, dragHandDataSize), 8, 7).create();
  1216. }
  1217. //==============================================================================
  1218. static int numAlwaysOnTopPeers = 0;
  1219. bool juce_areThereAnyAlwaysOnTopWindows()
  1220. {
  1221. return numAlwaysOnTopPeers > 0;
  1222. }
  1223. //==============================================================================
  1224. class LinuxComponentPeer : public ComponentPeer
  1225. {
  1226. public:
  1227. LinuxComponentPeer (Component& comp, const int windowStyleFlags, Window parentToAddTo)
  1228. : ComponentPeer (comp, windowStyleFlags),
  1229. windowH (0), parentWindow (0),
  1230. fullScreen (false), mapped (false),
  1231. visual (nullptr), depth (0),
  1232. isAlwaysOnTop (comp.isAlwaysOnTop()),
  1233. currentScaleFactor (1.0)
  1234. {
  1235. // it's dangerous to create a window on a thread other than the message thread..
  1236. jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  1237. dispatchWindowMessage = windowMessageReceive;
  1238. repainter = new LinuxRepaintManager (*this);
  1239. if (isAlwaysOnTop)
  1240. ++numAlwaysOnTopPeers;
  1241. createWindow (parentToAddTo);
  1242. setTitle (component.getName());
  1243. }
  1244. ~LinuxComponentPeer()
  1245. {
  1246. // it's dangerous to delete a window on a thread other than the message thread..
  1247. jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  1248. deleteIconPixmaps();
  1249. destroyWindow();
  1250. windowH = 0;
  1251. if (isAlwaysOnTop)
  1252. --numAlwaysOnTopPeers;
  1253. }
  1254. // (this callback is hooked up in the messaging code)
  1255. static void windowMessageReceive (XEvent& event)
  1256. {
  1257. if (event.xany.window != None)
  1258. {
  1259. if (LinuxComponentPeer* const peer = getPeerFor (event.xany.window))
  1260. peer->handleWindowMessage (event);
  1261. }
  1262. else if (event.xany.type == KeymapNotify)
  1263. {
  1264. const XKeymapEvent& keymapEvent = (const XKeymapEvent&) event.xkeymap;
  1265. memcpy (Keys::keyStates, keymapEvent.key_vector, 32);
  1266. }
  1267. }
  1268. //==============================================================================
  1269. void* getNativeHandle() const override
  1270. {
  1271. return (void*) windowH;
  1272. }
  1273. static LinuxComponentPeer* getPeerFor (Window windowHandle) noexcept
  1274. {
  1275. XPointer peer = nullptr;
  1276. if (display != nullptr)
  1277. {
  1278. ScopedXLock xlock;
  1279. if (! XFindContext (display, (XID) windowHandle, windowHandleXContext, &peer))
  1280. if (peer != nullptr && ! ComponentPeer::isValidPeer (reinterpret_cast<LinuxComponentPeer*> (peer)))
  1281. peer = nullptr;
  1282. }
  1283. return reinterpret_cast<LinuxComponentPeer*> (peer);
  1284. }
  1285. void setVisible (bool shouldBeVisible) override
  1286. {
  1287. ScopedXLock xlock;
  1288. if (shouldBeVisible)
  1289. XMapWindow (display, windowH);
  1290. else
  1291. XUnmapWindow (display, windowH);
  1292. }
  1293. void setTitle (const String& title) override
  1294. {
  1295. XTextProperty nameProperty;
  1296. char* strings[] = { const_cast<char*> (title.toRawUTF8()) };
  1297. ScopedXLock xlock;
  1298. if (XStringListToTextProperty (strings, 1, &nameProperty))
  1299. {
  1300. XSetWMName (display, windowH, &nameProperty);
  1301. XSetWMIconName (display, windowH, &nameProperty);
  1302. XFree (nameProperty.value);
  1303. }
  1304. }
  1305. void setBounds (const Rectangle<int>& newBounds, bool isNowFullScreen) override
  1306. {
  1307. if (fullScreen && ! isNowFullScreen)
  1308. {
  1309. // When transitioning back from fullscreen, we might need to remove
  1310. // the FULLSCREEN window property
  1311. Atom fs = Atoms::getIfExists ("_NET_WM_STATE_FULLSCREEN");
  1312. if (fs != None)
  1313. {
  1314. Window root = RootWindow (display, DefaultScreen (display));
  1315. XClientMessageEvent clientMsg;
  1316. clientMsg.display = display;
  1317. clientMsg.window = windowH;
  1318. clientMsg.type = ClientMessage;
  1319. clientMsg.format = 32;
  1320. clientMsg.message_type = atoms.windowState;
  1321. clientMsg.data.l[0] = 0; // Remove
  1322. clientMsg.data.l[1] = (long) fs;
  1323. clientMsg.data.l[2] = 0;
  1324. clientMsg.data.l[3] = 1; // Normal Source
  1325. ScopedXLock xlock;
  1326. XSendEvent (display, root, false,
  1327. SubstructureRedirectMask | SubstructureNotifyMask,
  1328. (XEvent*) &clientMsg);
  1329. }
  1330. }
  1331. fullScreen = isNowFullScreen;
  1332. if (windowH != 0)
  1333. {
  1334. bounds = newBounds.withSize (jmax (1, newBounds.getWidth()),
  1335. jmax (1, newBounds.getHeight()));
  1336. currentScaleFactor = DisplayGeometry::getInstance().findDisplayForRect (bounds, true).scale;
  1337. Rectangle<int> physicalBounds =
  1338. DisplayGeometry::scaledToPhysical (bounds);
  1339. WeakReference<Component> deletionChecker (&component);
  1340. ScopedXLock xlock;
  1341. XSizeHints* const hints = XAllocSizeHints();
  1342. hints->flags = USSize | USPosition;
  1343. hints->x = physicalBounds.getX();
  1344. hints->y = physicalBounds.getY();
  1345. hints->width = physicalBounds.getWidth();
  1346. hints->height = physicalBounds.getHeight();
  1347. if ((getStyleFlags() & windowIsResizable) == 0)
  1348. {
  1349. hints->min_width = hints->max_width = hints->width;
  1350. hints->min_height = hints->max_height = hints->height;
  1351. hints->flags |= PMinSize | PMaxSize;
  1352. }
  1353. XSetWMNormalHints (display, windowH, hints);
  1354. XFree (hints);
  1355. XMoveResizeWindow (display, windowH,
  1356. physicalBounds.getX() - windowBorder.getLeft(),
  1357. physicalBounds.getY() - windowBorder.getTop(),
  1358. (unsigned int) physicalBounds.getWidth(),
  1359. (unsigned int) physicalBounds.getHeight());
  1360. if (deletionChecker != nullptr)
  1361. {
  1362. updateBorderSize();
  1363. handleMovedOrResized();
  1364. }
  1365. }
  1366. }
  1367. Rectangle<int> getBounds() const override { return bounds; }
  1368. Point<float> localToGlobal (Point<float> relativePosition) override
  1369. {
  1370. return relativePosition + bounds.getPosition().toFloat();
  1371. }
  1372. Point<float> globalToLocal (Point<float> screenPosition) override
  1373. {
  1374. return screenPosition - bounds.getPosition().toFloat();
  1375. }
  1376. void setAlpha (float /* newAlpha */) override
  1377. {
  1378. //xxx todo!
  1379. }
  1380. StringArray getAvailableRenderingEngines() override
  1381. {
  1382. return StringArray ("Software Renderer");
  1383. }
  1384. void setMinimised (bool shouldBeMinimised) override
  1385. {
  1386. if (shouldBeMinimised)
  1387. {
  1388. Window root = RootWindow (display, DefaultScreen (display));
  1389. XClientMessageEvent clientMsg;
  1390. clientMsg.display = display;
  1391. clientMsg.window = windowH;
  1392. clientMsg.type = ClientMessage;
  1393. clientMsg.format = 32;
  1394. clientMsg.message_type = atoms.changeState;
  1395. clientMsg.data.l[0] = IconicState;
  1396. ScopedXLock xlock;
  1397. XSendEvent (display, root, false, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent*) &clientMsg);
  1398. }
  1399. else
  1400. {
  1401. setVisible (true);
  1402. }
  1403. }
  1404. bool isMinimised() const override
  1405. {
  1406. ScopedXLock xlock;
  1407. GetXProperty prop (windowH, atoms.state, 0, 64, false, atoms.state);
  1408. return prop.success
  1409. && prop.actualType == atoms.state
  1410. && prop.actualFormat == 32
  1411. && prop.numItems > 0
  1412. && ((unsigned long*) prop.data)[0] == IconicState;
  1413. }
  1414. void setFullScreen (const bool shouldBeFullScreen) override
  1415. {
  1416. Rectangle<int> r (lastNonFullscreenBounds); // (get a copy of this before de-minimising)
  1417. setMinimised (false);
  1418. if (fullScreen != shouldBeFullScreen)
  1419. {
  1420. if (shouldBeFullScreen)
  1421. r = Desktop::getInstance().getDisplays().getMainDisplay().userArea;
  1422. if (! r.isEmpty())
  1423. setBounds (ScalingHelpers::scaledScreenPosToUnscaled (component, r), shouldBeFullScreen);
  1424. component.repaint();
  1425. }
  1426. }
  1427. bool isFullScreen() const override
  1428. {
  1429. return fullScreen;
  1430. }
  1431. bool isChildWindowOf (Window possibleParent) const
  1432. {
  1433. Window* windowList = nullptr;
  1434. uint32 windowListSize = 0;
  1435. Window parent, root;
  1436. ScopedXLock xlock;
  1437. if (XQueryTree (display, windowH, &root, &parent, &windowList, &windowListSize) != 0)
  1438. {
  1439. if (windowList != nullptr)
  1440. XFree (windowList);
  1441. return parent == possibleParent;
  1442. }
  1443. return false;
  1444. }
  1445. bool isFrontWindow() const
  1446. {
  1447. Window* windowList = nullptr;
  1448. uint32 windowListSize = 0;
  1449. bool result = false;
  1450. ScopedXLock xlock;
  1451. Window parent, root = RootWindow (display, DefaultScreen (display));
  1452. if (XQueryTree (display, root, &root, &parent, &windowList, &windowListSize) != 0)
  1453. {
  1454. for (int i = (int) windowListSize; --i >= 0;)
  1455. {
  1456. if (LinuxComponentPeer* const peer = LinuxComponentPeer::getPeerFor (windowList[i]))
  1457. {
  1458. result = (peer == this);
  1459. break;
  1460. }
  1461. }
  1462. }
  1463. if (windowList != nullptr)
  1464. XFree (windowList);
  1465. return result;
  1466. }
  1467. bool contains (Point<int> localPos, bool trueIfInAChildWindow) const override
  1468. {
  1469. if (! bounds.withZeroOrigin().contains (localPos))
  1470. return false;
  1471. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  1472. {
  1473. Component* const c = Desktop::getInstance().getComponent (i);
  1474. if (c == &component)
  1475. break;
  1476. if (ComponentPeer* peer = c->getPeer())
  1477. if (peer->contains (localPos + bounds.getPosition() - peer->getBounds().getPosition(), true))
  1478. return false;
  1479. }
  1480. if (trueIfInAChildWindow)
  1481. return true;
  1482. ::Window root, child;
  1483. int wx, wy;
  1484. unsigned int ww, wh, bw, bitDepth;
  1485. ScopedXLock xlock;
  1486. localPos *= currentScaleFactor;
  1487. return XGetGeometry (display, (::Drawable) windowH, &root, &wx, &wy, &ww, &wh, &bw, &bitDepth)
  1488. && XTranslateCoordinates (display, windowH, windowH, localPos.getX(), localPos.getY(), &wx, &wy, &child)
  1489. && child == None;
  1490. }
  1491. BorderSize<int> getFrameSize() const override
  1492. {
  1493. return BorderSize<int>();
  1494. }
  1495. bool setAlwaysOnTop (bool /* alwaysOnTop */) override
  1496. {
  1497. return false;
  1498. }
  1499. void toFront (bool makeActive) override
  1500. {
  1501. if (makeActive)
  1502. {
  1503. setVisible (true);
  1504. grabFocus();
  1505. }
  1506. {
  1507. ScopedXLock xlock;
  1508. XEvent ev;
  1509. ev.xclient.type = ClientMessage;
  1510. ev.xclient.serial = 0;
  1511. ev.xclient.send_event = True;
  1512. ev.xclient.message_type = atoms.activeWin;
  1513. ev.xclient.window = windowH;
  1514. ev.xclient.format = 32;
  1515. ev.xclient.data.l[0] = 2;
  1516. ev.xclient.data.l[1] = getUserTime();
  1517. ev.xclient.data.l[2] = 0;
  1518. ev.xclient.data.l[3] = 0;
  1519. ev.xclient.data.l[4] = 0;
  1520. XSendEvent (display, RootWindow (display, DefaultScreen (display)),
  1521. False, SubstructureRedirectMask | SubstructureNotifyMask, &ev);
  1522. XSync (display, False);
  1523. }
  1524. handleBroughtToFront();
  1525. }
  1526. void toBehind (ComponentPeer* other) override
  1527. {
  1528. if (LinuxComponentPeer* const otherPeer = dynamic_cast<LinuxComponentPeer*> (other))
  1529. {
  1530. if (otherPeer->styleFlags & windowIsTemporary)
  1531. return;
  1532. setMinimised (false);
  1533. Window newStack[] = { otherPeer->windowH, windowH };
  1534. ScopedXLock xlock;
  1535. XRestackWindows (display, newStack, 2);
  1536. }
  1537. else
  1538. jassertfalse; // wrong type of window?
  1539. }
  1540. bool isFocused() const override
  1541. {
  1542. int revert = 0;
  1543. Window focusedWindow = 0;
  1544. ScopedXLock xlock;
  1545. XGetInputFocus (display, &focusedWindow, &revert);
  1546. return focusedWindow == windowH;
  1547. }
  1548. void grabFocus() override
  1549. {
  1550. XWindowAttributes atts;
  1551. ScopedXLock xlock;
  1552. if (windowH != 0
  1553. && XGetWindowAttributes (display, windowH, &atts)
  1554. && atts.map_state == IsViewable
  1555. && ! isFocused())
  1556. {
  1557. XSetInputFocus (display, windowH, RevertToParent, (::Time) getUserTime());
  1558. isActiveApplication = true;
  1559. }
  1560. }
  1561. void textInputRequired (Point<int>, TextInputTarget&) override {}
  1562. void repaint (const Rectangle<int>& area) override
  1563. {
  1564. repainter->repaint (area.getIntersection (bounds.withZeroOrigin()));
  1565. }
  1566. void performAnyPendingRepaintsNow() override
  1567. {
  1568. repainter->performAnyPendingRepaintsNow();
  1569. }
  1570. void setIcon (const Image& newIcon) override
  1571. {
  1572. const int dataSize = newIcon.getWidth() * newIcon.getHeight() + 2;
  1573. HeapBlock<unsigned long> data ((size_t) dataSize);
  1574. int index = 0;
  1575. data[index++] = (unsigned long) newIcon.getWidth();
  1576. data[index++] = (unsigned long) newIcon.getHeight();
  1577. for (int y = 0; y < newIcon.getHeight(); ++y)
  1578. for (int x = 0; x < newIcon.getWidth(); ++x)
  1579. data[index++] = (unsigned long) newIcon.getPixelAt (x, y).getARGB();
  1580. ScopedXLock xlock;
  1581. xchangeProperty (windowH, Atoms::getCreating ("_NET_WM_ICON"), XA_CARDINAL, 32, data.getData(), dataSize);
  1582. deleteIconPixmaps();
  1583. XWMHints* wmHints = XGetWMHints (display, windowH);
  1584. if (wmHints == nullptr)
  1585. wmHints = XAllocWMHints();
  1586. wmHints->flags |= IconPixmapHint | IconMaskHint;
  1587. wmHints->icon_pixmap = PixmapHelpers::createColourPixmapFromImage (display, newIcon);
  1588. wmHints->icon_mask = PixmapHelpers::createMaskPixmapFromImage (display, newIcon);
  1589. XSetWMHints (display, windowH, wmHints);
  1590. XFree (wmHints);
  1591. XSync (display, False);
  1592. }
  1593. void deleteIconPixmaps()
  1594. {
  1595. ScopedXLock xlock;
  1596. XWMHints* wmHints = XGetWMHints (display, windowH);
  1597. if (wmHints != nullptr)
  1598. {
  1599. if ((wmHints->flags & IconPixmapHint) != 0)
  1600. {
  1601. wmHints->flags &= ~IconPixmapHint;
  1602. XFreePixmap (display, wmHints->icon_pixmap);
  1603. }
  1604. if ((wmHints->flags & IconMaskHint) != 0)
  1605. {
  1606. wmHints->flags &= ~IconMaskHint;
  1607. XFreePixmap (display, wmHints->icon_mask);
  1608. }
  1609. XSetWMHints (display, windowH, wmHints);
  1610. XFree (wmHints);
  1611. }
  1612. }
  1613. //==============================================================================
  1614. void handleWindowMessage (XEvent& event)
  1615. {
  1616. switch (event.xany.type)
  1617. {
  1618. case KeyPressEventType: handleKeyPressEvent (event.xkey); break;
  1619. case KeyRelease: handleKeyReleaseEvent (event.xkey); break;
  1620. case ButtonPress: handleButtonPressEvent (event.xbutton); break;
  1621. case ButtonRelease: handleButtonReleaseEvent (event.xbutton); break;
  1622. case MotionNotify: handleMotionNotifyEvent (event.xmotion); break;
  1623. case EnterNotify: handleEnterNotifyEvent (event.xcrossing); break;
  1624. case LeaveNotify: handleLeaveNotifyEvent (event.xcrossing); break;
  1625. case FocusIn: handleFocusInEvent(); break;
  1626. case FocusOut: handleFocusOutEvent(); break;
  1627. case Expose: handleExposeEvent (event.xexpose); break;
  1628. case MappingNotify: handleMappingNotify (event.xmapping); break;
  1629. case ClientMessage: handleClientMessageEvent (event.xclient, event); break;
  1630. case SelectionNotify: handleDragAndDropSelection (event); break;
  1631. case ConfigureNotify: handleConfigureNotifyEvent (event.xconfigure); break;
  1632. case ReparentNotify: handleReparentNotifyEvent(); break;
  1633. case GravityNotify: handleGravityNotify(); break;
  1634. case SelectionClear: handleExternalSelectionClear(); break;
  1635. case SelectionRequest: handleExternalSelectionRequest (event); break;
  1636. case CirculateNotify:
  1637. case CreateNotify:
  1638. case DestroyNotify:
  1639. // Think we can ignore these
  1640. break;
  1641. case MapNotify:
  1642. mapped = true;
  1643. handleBroughtToFront();
  1644. break;
  1645. case UnmapNotify:
  1646. mapped = false;
  1647. break;
  1648. default:
  1649. #if JUCE_USE_XSHM
  1650. if (XSHMHelpers::isShmAvailable())
  1651. {
  1652. ScopedXLock xlock;
  1653. if (event.xany.type == XShmGetEventBase (display))
  1654. repainter->notifyPaintCompleted();
  1655. }
  1656. #endif
  1657. break;
  1658. }
  1659. }
  1660. void handleKeyPressEvent (XKeyEvent& keyEvent)
  1661. {
  1662. const ModifierKeys oldMods (currentModifiers);
  1663. char utf8 [64] = { 0 };
  1664. juce_wchar unicodeChar = 0;
  1665. int keyCode = 0;
  1666. bool keyDownChange = false;
  1667. KeySym sym;
  1668. {
  1669. ScopedXLock xlock;
  1670. updateKeyStates ((int) keyEvent.keycode, true);
  1671. String oldLocale (::setlocale (LC_ALL, 0));
  1672. ::setlocale (LC_ALL, "");
  1673. XLookupString (&keyEvent, utf8, sizeof (utf8), &sym, 0);
  1674. if (oldLocale.isNotEmpty())
  1675. ::setlocale (LC_ALL, oldLocale.toRawUTF8());
  1676. unicodeChar = *CharPointer_UTF8 (utf8);
  1677. keyCode = (int) unicodeChar;
  1678. if (keyCode < 0x20)
  1679. keyCode = (int) XkbKeycodeToKeysym (display, (::KeyCode) keyEvent.keycode, 0, currentModifiers.isShiftDown() ? 1 : 0);
  1680. keyDownChange = (sym != NoSymbol) && ! updateKeyModifiersFromSym (sym, true);
  1681. }
  1682. bool keyPressed = false;
  1683. if ((sym & 0xff00) == 0xff00 || keyCode == XK_ISO_Left_Tab)
  1684. {
  1685. switch (sym) // Translate keypad
  1686. {
  1687. case XK_KP_Add: keyCode = XK_plus; break;
  1688. case XK_KP_Subtract: keyCode = XK_hyphen; break;
  1689. case XK_KP_Divide: keyCode = XK_slash; break;
  1690. case XK_KP_Multiply: keyCode = XK_asterisk; break;
  1691. case XK_KP_Enter: keyCode = XK_Return; break;
  1692. case XK_KP_Insert: keyCode = XK_Insert; break;
  1693. case XK_Delete:
  1694. case XK_KP_Delete: keyCode = XK_Delete; break;
  1695. case XK_KP_Left: keyCode = XK_Left; break;
  1696. case XK_KP_Right: keyCode = XK_Right; break;
  1697. case XK_KP_Up: keyCode = XK_Up; break;
  1698. case XK_KP_Down: keyCode = XK_Down; break;
  1699. case XK_KP_Home: keyCode = XK_Home; break;
  1700. case XK_KP_End: keyCode = XK_End; break;
  1701. case XK_KP_Page_Down: keyCode = XK_Page_Down; break;
  1702. case XK_KP_Page_Up: keyCode = XK_Page_Up; break;
  1703. case XK_KP_0: keyCode = XK_0; break;
  1704. case XK_KP_1: keyCode = XK_1; break;
  1705. case XK_KP_2: keyCode = XK_2; break;
  1706. case XK_KP_3: keyCode = XK_3; break;
  1707. case XK_KP_4: keyCode = XK_4; break;
  1708. case XK_KP_5: keyCode = XK_5; break;
  1709. case XK_KP_6: keyCode = XK_6; break;
  1710. case XK_KP_7: keyCode = XK_7; break;
  1711. case XK_KP_8: keyCode = XK_8; break;
  1712. case XK_KP_9: keyCode = XK_9; break;
  1713. default: break;
  1714. }
  1715. switch (keyCode)
  1716. {
  1717. case XK_Left:
  1718. case XK_Right:
  1719. case XK_Up:
  1720. case XK_Down:
  1721. case XK_Page_Up:
  1722. case XK_Page_Down:
  1723. case XK_End:
  1724. case XK_Home:
  1725. case XK_Delete:
  1726. case XK_Insert:
  1727. keyPressed = true;
  1728. keyCode = (keyCode & 0xff) | Keys::extendedKeyModifier;
  1729. break;
  1730. case XK_Tab:
  1731. case XK_Return:
  1732. case XK_Escape:
  1733. case XK_BackSpace:
  1734. keyPressed = true;
  1735. keyCode &= 0xff;
  1736. break;
  1737. case XK_ISO_Left_Tab:
  1738. keyPressed = true;
  1739. keyCode = XK_Tab & 0xff;
  1740. break;
  1741. default:
  1742. if (sym >= XK_F1 && sym <= XK_F16)
  1743. {
  1744. keyPressed = true;
  1745. keyCode = (sym & 0xff) | Keys::extendedKeyModifier;
  1746. }
  1747. break;
  1748. }
  1749. }
  1750. if (utf8[0] != 0 || ((sym & 0xff00) == 0 && sym >= 8))
  1751. keyPressed = true;
  1752. if (oldMods != currentModifiers)
  1753. handleModifierKeysChange();
  1754. if (keyDownChange)
  1755. handleKeyUpOrDown (true);
  1756. if (keyPressed)
  1757. handleKeyPress (keyCode, unicodeChar);
  1758. }
  1759. static bool isKeyReleasePartOfAutoRepeat (const XKeyEvent& keyReleaseEvent)
  1760. {
  1761. if (XPending (display))
  1762. {
  1763. XEvent e;
  1764. XPeekEvent (display, &e);
  1765. // Look for a subsequent key-down event with the same timestamp and keycode
  1766. return e.type == KeyPressEventType
  1767. && e.xkey.keycode == keyReleaseEvent.keycode
  1768. && e.xkey.time == keyReleaseEvent.time;
  1769. }
  1770. return false;
  1771. }
  1772. void handleKeyReleaseEvent (const XKeyEvent& keyEvent)
  1773. {
  1774. if (! isKeyReleasePartOfAutoRepeat (keyEvent))
  1775. {
  1776. updateKeyStates ((int) keyEvent.keycode, false);
  1777. KeySym sym;
  1778. {
  1779. ScopedXLock xlock;
  1780. sym = XkbKeycodeToKeysym (display, (::KeyCode) keyEvent.keycode, 0, 0);
  1781. }
  1782. const ModifierKeys oldMods (currentModifiers);
  1783. const bool keyDownChange = (sym != NoSymbol) && ! updateKeyModifiersFromSym (sym, false);
  1784. if (oldMods != currentModifiers)
  1785. handleModifierKeysChange();
  1786. if (keyDownChange)
  1787. handleKeyUpOrDown (false);
  1788. }
  1789. }
  1790. template <typename EventType>
  1791. Point<float> getMousePos (const EventType& e) noexcept
  1792. {
  1793. return Point<float> ((float) e.x, (float) e.y) / currentScaleFactor;
  1794. }
  1795. void handleWheelEvent (const XButtonPressedEvent& buttonPressEvent, const float amount)
  1796. {
  1797. MouseWheelDetails wheel;
  1798. wheel.deltaX = 0.0f;
  1799. wheel.deltaY = amount;
  1800. wheel.isReversed = false;
  1801. wheel.isSmooth = false;
  1802. wheel.isInertial = false;
  1803. handleMouseWheel (0, getMousePos (buttonPressEvent), getEventTime (buttonPressEvent), wheel);
  1804. }
  1805. void handleButtonPressEvent (const XButtonPressedEvent& buttonPressEvent, int buttonModifierFlag)
  1806. {
  1807. currentModifiers = currentModifiers.withFlags (buttonModifierFlag);
  1808. toFront (true);
  1809. handleMouseEvent (0, getMousePos (buttonPressEvent), currentModifiers,
  1810. MouseInputSource::invalidPressure, getEventTime (buttonPressEvent));
  1811. }
  1812. void handleButtonPressEvent (const XButtonPressedEvent& buttonPressEvent)
  1813. {
  1814. updateKeyModifiers ((int) buttonPressEvent.state);
  1815. switch (pointerMap [buttonPressEvent.button - Button1])
  1816. {
  1817. case Keys::WheelUp: handleWheelEvent (buttonPressEvent, 50.0f / 256.0f); break;
  1818. case Keys::WheelDown: handleWheelEvent (buttonPressEvent, -50.0f / 256.0f); break;
  1819. case Keys::LeftButton: handleButtonPressEvent (buttonPressEvent, ModifierKeys::leftButtonModifier); break;
  1820. case Keys::RightButton: handleButtonPressEvent (buttonPressEvent, ModifierKeys::rightButtonModifier); break;
  1821. case Keys::MiddleButton: handleButtonPressEvent (buttonPressEvent, ModifierKeys::middleButtonModifier); break;
  1822. default: break;
  1823. }
  1824. clearLastMousePos();
  1825. }
  1826. void handleButtonReleaseEvent (const XButtonReleasedEvent& buttonRelEvent)
  1827. {
  1828. updateKeyModifiers ((int) buttonRelEvent.state);
  1829. if (parentWindow != 0)
  1830. updateWindowBounds();
  1831. switch (pointerMap [buttonRelEvent.button - Button1])
  1832. {
  1833. case Keys::LeftButton: currentModifiers = currentModifiers.withoutFlags (ModifierKeys::leftButtonModifier); break;
  1834. case Keys::RightButton: currentModifiers = currentModifiers.withoutFlags (ModifierKeys::rightButtonModifier); break;
  1835. case Keys::MiddleButton: currentModifiers = currentModifiers.withoutFlags (ModifierKeys::middleButtonModifier); break;
  1836. default: break;
  1837. }
  1838. if (dragState.dragging)
  1839. handleExternalDragButtonReleaseEvent();
  1840. handleMouseEvent (0, getMousePos (buttonRelEvent), currentModifiers,
  1841. MouseInputSource::invalidPressure, getEventTime (buttonRelEvent));
  1842. clearLastMousePos();
  1843. }
  1844. void handleMotionNotifyEvent (const XPointerMovedEvent& movedEvent)
  1845. {
  1846. updateKeyModifiers ((int) movedEvent.state);
  1847. lastMousePos = Point<int> (movedEvent.x_root, movedEvent.y_root);
  1848. if (dragState.dragging)
  1849. handleExternalDragMotionNotify();
  1850. handleMouseEvent (0, getMousePos (movedEvent), currentModifiers,
  1851. MouseInputSource::invalidPressure, getEventTime (movedEvent));
  1852. }
  1853. void handleEnterNotifyEvent (const XEnterWindowEvent& enterEvent)
  1854. {
  1855. if (parentWindow != 0)
  1856. updateWindowBounds();
  1857. clearLastMousePos();
  1858. if (! currentModifiers.isAnyMouseButtonDown())
  1859. {
  1860. updateKeyModifiers ((int) enterEvent.state);
  1861. handleMouseEvent (0, getMousePos (enterEvent), currentModifiers,
  1862. MouseInputSource::invalidPressure, getEventTime (enterEvent));
  1863. }
  1864. }
  1865. void handleLeaveNotifyEvent (const XLeaveWindowEvent& leaveEvent)
  1866. {
  1867. // Suppress the normal leave if we've got a pointer grab, or if
  1868. // it's a bogus one caused by clicking a mouse button when running
  1869. // in a Window manager
  1870. if (((! currentModifiers.isAnyMouseButtonDown()) && leaveEvent.mode == NotifyNormal)
  1871. || leaveEvent.mode == NotifyUngrab)
  1872. {
  1873. updateKeyModifiers ((int) leaveEvent.state);
  1874. handleMouseEvent (0, getMousePos (leaveEvent), currentModifiers,
  1875. MouseInputSource::invalidPressure, getEventTime (leaveEvent));
  1876. }
  1877. }
  1878. void handleFocusInEvent()
  1879. {
  1880. isActiveApplication = true;
  1881. if (isFocused())
  1882. handleFocusGain();
  1883. }
  1884. void handleFocusOutEvent()
  1885. {
  1886. isActiveApplication = false;
  1887. if (! isFocused())
  1888. handleFocusLoss();
  1889. }
  1890. void handleExposeEvent (XExposeEvent& exposeEvent)
  1891. {
  1892. // Batch together all pending expose events
  1893. XEvent nextEvent;
  1894. ScopedXLock xlock;
  1895. // if we have opengl contexts then just repaint them all
  1896. // regardless if this is really necessary
  1897. repaintOpenGLContexts ();
  1898. if (exposeEvent.window != windowH)
  1899. {
  1900. Window child;
  1901. XTranslateCoordinates (display, exposeEvent.window, windowH,
  1902. exposeEvent.x, exposeEvent.y, &exposeEvent.x, &exposeEvent.y,
  1903. &child);
  1904. }
  1905. // exposeEvent is in local window local coordinates so do not convert with
  1906. // physicalToScaled, but rather use currentScaleFactor
  1907. repaint (Rectangle<int> (exposeEvent.x, exposeEvent.y,
  1908. exposeEvent.width, exposeEvent.height) / currentScaleFactor);
  1909. while (XEventsQueued (display, QueuedAfterFlush) > 0)
  1910. {
  1911. XPeekEvent (display, &nextEvent);
  1912. if (nextEvent.type != Expose || nextEvent.xany.window != exposeEvent.window)
  1913. break;
  1914. XNextEvent (display, &nextEvent);
  1915. const XExposeEvent& nextExposeEvent = (const XExposeEvent&) nextEvent.xexpose;
  1916. repaint (Rectangle<int> (nextExposeEvent.x, nextExposeEvent.y,
  1917. nextExposeEvent.width, nextExposeEvent.height) / currentScaleFactor);
  1918. }
  1919. }
  1920. void handleConfigureNotifyEvent (XConfigureEvent& confEvent)
  1921. {
  1922. updateWindowBounds();
  1923. updateBorderSize();
  1924. handleMovedOrResized();
  1925. // if the native title bar is dragged, need to tell any active menus, etc.
  1926. if ((styleFlags & windowHasTitleBar) != 0
  1927. && component.isCurrentlyBlockedByAnotherModalComponent())
  1928. {
  1929. if (Component* const currentModalComp = Component::getCurrentlyModalComponent())
  1930. currentModalComp->inputAttemptWhenModal();
  1931. }
  1932. if (confEvent.window == windowH
  1933. && confEvent.above != 0
  1934. && isFrontWindow())
  1935. {
  1936. handleBroughtToFront();
  1937. }
  1938. }
  1939. void handleReparentNotifyEvent()
  1940. {
  1941. parentWindow = 0;
  1942. Window wRoot = 0;
  1943. Window* wChild = nullptr;
  1944. unsigned int numChildren;
  1945. {
  1946. ScopedXLock xlock;
  1947. XQueryTree (display, windowH, &wRoot, &parentWindow, &wChild, &numChildren);
  1948. }
  1949. if (parentWindow == windowH || parentWindow == wRoot)
  1950. parentWindow = 0;
  1951. handleGravityNotify();
  1952. }
  1953. void handleGravityNotify()
  1954. {
  1955. updateWindowBounds();
  1956. updateBorderSize();
  1957. handleMovedOrResized();
  1958. }
  1959. void handleMappingNotify (XMappingEvent& mappingEvent)
  1960. {
  1961. if (mappingEvent.request != MappingPointer)
  1962. {
  1963. // Deal with modifier/keyboard mapping
  1964. ScopedXLock xlock;
  1965. XRefreshKeyboardMapping (&mappingEvent);
  1966. updateModifierMappings();
  1967. }
  1968. }
  1969. void handleClientMessageEvent (XClientMessageEvent& clientMsg, XEvent& event)
  1970. {
  1971. if (clientMsg.message_type == atoms.protocols && clientMsg.format == 32)
  1972. {
  1973. const Atom atom = (Atom) clientMsg.data.l[0];
  1974. if (atom == atoms.protocolList [Atoms::PING])
  1975. {
  1976. Window root = RootWindow (display, DefaultScreen (display));
  1977. clientMsg.window = root;
  1978. XSendEvent (display, root, False, NoEventMask, &event);
  1979. XFlush (display);
  1980. }
  1981. else if (atom == atoms.protocolList [Atoms::TAKE_FOCUS])
  1982. {
  1983. if ((getStyleFlags() & juce::ComponentPeer::windowIgnoresKeyPresses) == 0)
  1984. {
  1985. XWindowAttributes atts;
  1986. ScopedXLock xlock;
  1987. if (clientMsg.window != 0
  1988. && XGetWindowAttributes (display, clientMsg.window, &atts))
  1989. {
  1990. if (atts.map_state == IsViewable)
  1991. XSetInputFocus (display, clientMsg.window, RevertToParent, (::Time) clientMsg.data.l[1]);
  1992. }
  1993. }
  1994. }
  1995. else if (atom == atoms.protocolList [Atoms::DELETE_WINDOW])
  1996. {
  1997. handleUserClosingWindow();
  1998. }
  1999. }
  2000. else if (clientMsg.message_type == atoms.XdndEnter)
  2001. {
  2002. handleDragAndDropEnter (clientMsg);
  2003. }
  2004. else if (clientMsg.message_type == atoms.XdndLeave)
  2005. {
  2006. handleDragExit (dragInfo);
  2007. resetDragAndDrop();
  2008. }
  2009. else if (clientMsg.message_type == atoms.XdndPosition)
  2010. {
  2011. handleDragAndDropPosition (clientMsg);
  2012. }
  2013. else if (clientMsg.message_type == atoms.XdndDrop)
  2014. {
  2015. handleDragAndDropDrop (clientMsg);
  2016. }
  2017. else if (clientMsg.message_type == atoms.XdndStatus)
  2018. {
  2019. handleExternalDragAndDropStatus (clientMsg);
  2020. }
  2021. else if (clientMsg.message_type == atoms.XdndFinished)
  2022. {
  2023. externalResetDragAndDrop();
  2024. }
  2025. }
  2026. bool externalDragTextInit (const String& text)
  2027. {
  2028. if (dragState.dragging)
  2029. return false;
  2030. return externalDragInit (true, text);
  2031. }
  2032. bool externalDragFileInit (const StringArray& files, bool /*canMoveFiles*/)
  2033. {
  2034. if (dragState.dragging)
  2035. return false;
  2036. StringArray uriList;
  2037. for (int i = 0; i < files.size(); ++i)
  2038. {
  2039. const String& f = files[i];
  2040. if (f.matchesWildcard ("?*://*", false))
  2041. uriList.add (f);
  2042. else
  2043. uriList.add ("file://" + f);
  2044. }
  2045. return externalDragInit (false, uriList.joinIntoString ("\r\n"));
  2046. }
  2047. //==============================================================================
  2048. void showMouseCursor (Cursor cursor) noexcept
  2049. {
  2050. ScopedXLock xlock;
  2051. XDefineCursor (display, windowH, cursor);
  2052. }
  2053. //==============================================================================
  2054. double getCurrentScale() noexcept
  2055. {
  2056. return currentScaleFactor;
  2057. }
  2058. //==============================================================================
  2059. void addOpenGLRepaintListener (Component* dummy)
  2060. {
  2061. if (dummy != nullptr)
  2062. glRepaintListeners.addIfNotAlreadyThere (dummy);
  2063. }
  2064. void removeOpenGLRepaintListener (Component* dummy)
  2065. {
  2066. if (dummy != nullptr)
  2067. glRepaintListeners.removeAllInstancesOf (dummy);
  2068. }
  2069. void repaintOpenGLContexts()
  2070. {
  2071. for (int i = 0; i < glRepaintListeners.size(); ++i)
  2072. {
  2073. if (Component* c = glRepaintListeners [i])
  2074. c->handleCommandMessage (0);
  2075. }
  2076. }
  2077. //==============================================================================
  2078. bool dontRepaint;
  2079. static ModifierKeys currentModifiers;
  2080. static bool isActiveApplication;
  2081. private:
  2082. //==============================================================================
  2083. class LinuxRepaintManager : public Timer
  2084. {
  2085. public:
  2086. LinuxRepaintManager (LinuxComponentPeer& p)
  2087. : peer (p), lastTimeImageUsed (0)
  2088. {
  2089. #if JUCE_USE_XSHM
  2090. shmPaintsPending = 0;
  2091. useARGBImagesForRendering = XSHMHelpers::isShmAvailable();
  2092. if (useARGBImagesForRendering)
  2093. {
  2094. ScopedXLock xlock;
  2095. XShmSegmentInfo segmentinfo;
  2096. XImage* const testImage
  2097. = XShmCreateImage (display, DefaultVisual (display, DefaultScreen (display)),
  2098. 24, ZPixmap, 0, &segmentinfo, 64, 64);
  2099. useARGBImagesForRendering = (testImage->bits_per_pixel == 32);
  2100. XDestroyImage (testImage);
  2101. }
  2102. #endif
  2103. }
  2104. void timerCallback() override
  2105. {
  2106. #if JUCE_USE_XSHM
  2107. if (shmPaintsPending != 0)
  2108. return;
  2109. #endif
  2110. if (! regionsNeedingRepaint.isEmpty())
  2111. {
  2112. stopTimer();
  2113. performAnyPendingRepaintsNow();
  2114. }
  2115. else if (Time::getApproximateMillisecondCounter() > lastTimeImageUsed + 3000)
  2116. {
  2117. stopTimer();
  2118. image = Image::null;
  2119. }
  2120. }
  2121. void repaint (const Rectangle<int>& area)
  2122. {
  2123. if (! isTimerRunning())
  2124. startTimer (repaintTimerPeriod);
  2125. regionsNeedingRepaint.add (area * peer.currentScaleFactor);
  2126. }
  2127. void performAnyPendingRepaintsNow()
  2128. {
  2129. #if JUCE_USE_XSHM
  2130. if (shmPaintsPending != 0)
  2131. {
  2132. startTimer (repaintTimerPeriod);
  2133. return;
  2134. }
  2135. #endif
  2136. RectangleList<int> originalRepaintRegion (regionsNeedingRepaint);
  2137. regionsNeedingRepaint.clear();
  2138. const Rectangle<int> totalArea (originalRepaintRegion.getBounds());
  2139. if (! totalArea.isEmpty())
  2140. {
  2141. if (image.isNull() || image.getWidth() < totalArea.getWidth()
  2142. || image.getHeight() < totalArea.getHeight())
  2143. {
  2144. #if JUCE_USE_XSHM
  2145. image = Image (new XBitmapImage (useARGBImagesForRendering ? Image::ARGB
  2146. : Image::RGB,
  2147. #else
  2148. image = Image (new XBitmapImage (Image::RGB,
  2149. #endif
  2150. (totalArea.getWidth() + 31) & ~31,
  2151. (totalArea.getHeight() + 31) & ~31,
  2152. false, (unsigned int) peer.depth, peer.visual));
  2153. }
  2154. startTimer (repaintTimerPeriod);
  2155. RectangleList<int> adjustedList (originalRepaintRegion);
  2156. adjustedList.offsetAll (-totalArea.getX(), -totalArea.getY());
  2157. if (peer.depth == 32)
  2158. for (const Rectangle<int>* i = originalRepaintRegion.begin(), * const e = originalRepaintRegion.end(); i != e; ++i)
  2159. image.clear (*i - totalArea.getPosition());
  2160. {
  2161. ScopedPointer<LowLevelGraphicsContext> context (peer.getComponent().getLookAndFeel()
  2162. .createGraphicsContext (image, -totalArea.getPosition(), adjustedList));
  2163. context->addTransform (AffineTransform::scale ((float) peer.currentScaleFactor));
  2164. peer.handlePaint (*context);
  2165. }
  2166. for (const Rectangle<int>* i = originalRepaintRegion.begin(), * const e = originalRepaintRegion.end(); i != e; ++i)
  2167. {
  2168. XBitmapImage* xbitmap = static_cast<XBitmapImage*> (image.getPixelData());
  2169. #if JUCE_USE_XSHM
  2170. if (xbitmap->isUsingXShm())
  2171. ++shmPaintsPending;
  2172. #endif
  2173. xbitmap->blitToWindow (peer.windowH,
  2174. i->getX(), i->getY(),
  2175. (unsigned int) i->getWidth(),
  2176. (unsigned int) i->getHeight(),
  2177. i->getX() - totalArea.getX(), i->getY() - totalArea.getY());
  2178. }
  2179. }
  2180. lastTimeImageUsed = Time::getApproximateMillisecondCounter();
  2181. startTimer (repaintTimerPeriod);
  2182. }
  2183. #if JUCE_USE_XSHM
  2184. void notifyPaintCompleted() noexcept { --shmPaintsPending; }
  2185. #endif
  2186. private:
  2187. enum { repaintTimerPeriod = 1000 / 100 };
  2188. LinuxComponentPeer& peer;
  2189. Image image;
  2190. uint32 lastTimeImageUsed;
  2191. RectangleList<int> regionsNeedingRepaint;
  2192. #if JUCE_USE_XSHM
  2193. bool useARGBImagesForRendering;
  2194. int shmPaintsPending;
  2195. #endif
  2196. JUCE_DECLARE_NON_COPYABLE (LinuxRepaintManager)
  2197. };
  2198. const Atoms atoms;
  2199. ScopedPointer<LinuxRepaintManager> repainter;
  2200. friend class LinuxRepaintManager;
  2201. Window windowH, parentWindow;
  2202. Rectangle<int> bounds;
  2203. Image taskbarImage;
  2204. bool fullScreen, mapped;
  2205. Visual* visual;
  2206. int depth;
  2207. BorderSize<int> windowBorder;
  2208. bool isAlwaysOnTop;
  2209. double currentScaleFactor;
  2210. Array<Component*> glRepaintListeners;
  2211. enum { KeyPressEventType = 2 };
  2212. struct MotifWmHints
  2213. {
  2214. unsigned long flags;
  2215. unsigned long functions;
  2216. unsigned long decorations;
  2217. long input_mode;
  2218. unsigned long status;
  2219. };
  2220. static void updateKeyStates (const int keycode, const bool press) noexcept
  2221. {
  2222. const int keybyte = keycode >> 3;
  2223. const int keybit = (1 << (keycode & 7));
  2224. if (press)
  2225. Keys::keyStates [keybyte] |= keybit;
  2226. else
  2227. Keys::keyStates [keybyte] &= ~keybit;
  2228. }
  2229. static void updateKeyModifiers (const int status) noexcept
  2230. {
  2231. int keyMods = 0;
  2232. if ((status & ShiftMask) != 0) keyMods |= ModifierKeys::shiftModifier;
  2233. if ((status & ControlMask) != 0) keyMods |= ModifierKeys::ctrlModifier;
  2234. if ((status & Keys::AltMask) != 0) keyMods |= ModifierKeys::altModifier;
  2235. currentModifiers = currentModifiers.withOnlyMouseButtons().withFlags (keyMods);
  2236. Keys::numLock = ((status & Keys::NumLockMask) != 0);
  2237. Keys::capsLock = ((status & LockMask) != 0);
  2238. }
  2239. static bool updateKeyModifiersFromSym (KeySym sym, const bool press) noexcept
  2240. {
  2241. int modifier = 0;
  2242. bool isModifier = true;
  2243. switch (sym)
  2244. {
  2245. case XK_Shift_L:
  2246. case XK_Shift_R: modifier = ModifierKeys::shiftModifier; break;
  2247. case XK_Control_L:
  2248. case XK_Control_R: modifier = ModifierKeys::ctrlModifier; break;
  2249. case XK_Alt_L:
  2250. case XK_Alt_R: modifier = ModifierKeys::altModifier; break;
  2251. case XK_Num_Lock:
  2252. if (press)
  2253. Keys::numLock = ! Keys::numLock;
  2254. break;
  2255. case XK_Caps_Lock:
  2256. if (press)
  2257. Keys::capsLock = ! Keys::capsLock;
  2258. break;
  2259. case XK_Scroll_Lock:
  2260. break;
  2261. default:
  2262. isModifier = false;
  2263. break;
  2264. }
  2265. currentModifiers = press ? currentModifiers.withFlags (modifier)
  2266. : currentModifiers.withoutFlags (modifier);
  2267. return isModifier;
  2268. }
  2269. // Alt and Num lock are not defined by standard X
  2270. // modifier constants: check what they're mapped to
  2271. static void updateModifierMappings() noexcept
  2272. {
  2273. ScopedXLock xlock;
  2274. const int altLeftCode = XKeysymToKeycode (display, XK_Alt_L);
  2275. const int numLockCode = XKeysymToKeycode (display, XK_Num_Lock);
  2276. Keys::AltMask = 0;
  2277. Keys::NumLockMask = 0;
  2278. if (XModifierKeymap* const mapping = XGetModifierMapping (display))
  2279. {
  2280. for (int i = 0; i < 8; i++)
  2281. {
  2282. if (mapping->modifiermap [i << 1] == altLeftCode)
  2283. Keys::AltMask = 1 << i;
  2284. else if (mapping->modifiermap [i << 1] == numLockCode)
  2285. Keys::NumLockMask = 1 << i;
  2286. }
  2287. XFreeModifiermap (mapping);
  2288. }
  2289. }
  2290. //==============================================================================
  2291. static void xchangeProperty (Window wndH, Atom property, Atom type, int format, const void* data, int numElements)
  2292. {
  2293. XChangeProperty (display, wndH, property, type, format, PropModeReplace, (const unsigned char*) data, numElements);
  2294. }
  2295. void removeWindowDecorations (Window wndH)
  2296. {
  2297. Atom hints = Atoms::getIfExists ("_MOTIF_WM_HINTS");
  2298. if (hints != None)
  2299. {
  2300. MotifWmHints motifHints;
  2301. zerostruct (motifHints);
  2302. motifHints.flags = 2; /* MWM_HINTS_DECORATIONS */
  2303. motifHints.decorations = 0;
  2304. ScopedXLock xlock;
  2305. xchangeProperty (wndH, hints, hints, 32, &motifHints, 4);
  2306. }
  2307. hints = Atoms::getIfExists ("_WIN_HINTS");
  2308. if (hints != None)
  2309. {
  2310. long gnomeHints = 0;
  2311. ScopedXLock xlock;
  2312. xchangeProperty (wndH, hints, hints, 32, &gnomeHints, 1);
  2313. }
  2314. hints = Atoms::getIfExists ("KWM_WIN_DECORATION");
  2315. if (hints != None)
  2316. {
  2317. long kwmHints = 2; /*KDE_tinyDecoration*/
  2318. ScopedXLock xlock;
  2319. xchangeProperty (wndH, hints, hints, 32, &kwmHints, 1);
  2320. }
  2321. hints = Atoms::getIfExists ("_KDE_NET_WM_WINDOW_TYPE_OVERRIDE");
  2322. if (hints != None)
  2323. {
  2324. ScopedXLock xlock;
  2325. xchangeProperty (wndH, atoms.windowType, XA_ATOM, 32, &hints, 1);
  2326. }
  2327. }
  2328. void addWindowButtons (Window wndH)
  2329. {
  2330. ScopedXLock xlock;
  2331. Atom hints = Atoms::getIfExists ("_MOTIF_WM_HINTS");
  2332. if (hints != None)
  2333. {
  2334. MotifWmHints motifHints;
  2335. zerostruct (motifHints);
  2336. motifHints.flags = 1 | 2; /* MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS */
  2337. motifHints.decorations = 2 /* MWM_DECOR_BORDER */ | 8 /* MWM_DECOR_TITLE */ | 16; /* MWM_DECOR_MENU */
  2338. motifHints.functions = 4 /* MWM_FUNC_MOVE */;
  2339. if ((styleFlags & windowHasCloseButton) != 0)
  2340. motifHints.functions |= 32; /* MWM_FUNC_CLOSE */
  2341. if ((styleFlags & windowHasMinimiseButton) != 0)
  2342. {
  2343. motifHints.functions |= 8; /* MWM_FUNC_MINIMIZE */
  2344. motifHints.decorations |= 0x20; /* MWM_DECOR_MINIMIZE */
  2345. }
  2346. if ((styleFlags & windowHasMaximiseButton) != 0)
  2347. {
  2348. motifHints.functions |= 0x10; /* MWM_FUNC_MAXIMIZE */
  2349. motifHints.decorations |= 0x40; /* MWM_DECOR_MAXIMIZE */
  2350. }
  2351. if ((styleFlags & windowIsResizable) != 0)
  2352. {
  2353. motifHints.functions |= 2; /* MWM_FUNC_RESIZE */
  2354. motifHints.decorations |= 0x4; /* MWM_DECOR_RESIZEH */
  2355. }
  2356. xchangeProperty (wndH, hints, hints, 32, &motifHints, 5);
  2357. }
  2358. hints = Atoms::getIfExists ("_NET_WM_ALLOWED_ACTIONS");
  2359. if (hints != None)
  2360. {
  2361. Atom netHints [6];
  2362. int num = 0;
  2363. if ((styleFlags & windowIsResizable) != 0)
  2364. netHints [num++] = Atoms::getIfExists ("_NET_WM_ACTION_RESIZE");
  2365. if ((styleFlags & windowHasMaximiseButton) != 0)
  2366. netHints [num++] = Atoms::getIfExists ("_NET_WM_ACTION_FULLSCREEN");
  2367. if ((styleFlags & windowHasMinimiseButton) != 0)
  2368. netHints [num++] = Atoms::getIfExists ("_NET_WM_ACTION_MINIMIZE");
  2369. if ((styleFlags & windowHasCloseButton) != 0)
  2370. netHints [num++] = Atoms::getIfExists ("_NET_WM_ACTION_CLOSE");
  2371. xchangeProperty (wndH, hints, XA_ATOM, 32, &netHints, num);
  2372. }
  2373. }
  2374. void setWindowType()
  2375. {
  2376. Atom netHints [2];
  2377. if (styleFlags & windowIsTemporary)
  2378. netHints [0] = Atoms::getIfExists ("_NET_WM_WINDOW_TYPE_TOOLTIP");
  2379. else if ((styleFlags & windowHasDropShadow) == 0 && Desktop::canUseSemiTransparentWindows())
  2380. netHints [0] = Atoms::getIfExists ("_NET_WM_WINDOW_TYPE_COMBO");
  2381. else
  2382. netHints [0] = Atoms::getIfExists ("_NET_WM_WINDOW_TYPE_NORMAL");
  2383. xchangeProperty (windowH, atoms.windowType, XA_ATOM, 32, &netHints, 1);
  2384. int numHints = 0;
  2385. if ((styleFlags & windowAppearsOnTaskbar) == 0)
  2386. netHints [numHints++] = Atoms::getIfExists ("_NET_WM_STATE_SKIP_TASKBAR");
  2387. if (component.isAlwaysOnTop())
  2388. netHints [numHints++] = Atoms::getIfExists ("_NET_WM_STATE_ABOVE");
  2389. if (numHints > 0)
  2390. xchangeProperty (windowH, atoms.windowState, XA_ATOM, 32, &netHints, numHints);
  2391. }
  2392. void createWindow (Window parentToAddTo)
  2393. {
  2394. ScopedXLock xlock;
  2395. resetDragAndDrop();
  2396. // Get defaults for various properties
  2397. const int screen = DefaultScreen (display);
  2398. Window root = RootWindow (display, screen);
  2399. parentWindow = parentToAddTo;
  2400. // Try to obtain a 32-bit visual or fallback to 24 or 16
  2401. visual = Visuals::findVisualFormat ((styleFlags & windowIsSemiTransparent) ? 32 : 24, depth);
  2402. if (visual == nullptr)
  2403. {
  2404. Logger::outputDebugString ("ERROR: System doesn't support 32, 24 or 16 bit RGB display.\n");
  2405. Process::terminate();
  2406. }
  2407. // Create and install a colormap suitable fr our visual
  2408. Colormap colormap = XCreateColormap (display, root, visual, AllocNone);
  2409. XInstallColormap (display, colormap);
  2410. // Set up the window attributes
  2411. XSetWindowAttributes swa;
  2412. swa.border_pixel = 0;
  2413. swa.background_pixmap = None;
  2414. swa.colormap = colormap;
  2415. swa.override_redirect = (styleFlags & windowIsTemporary) ? True : False;
  2416. swa.event_mask = getAllEventsMask();
  2417. windowH = XCreateWindow (display, parentToAddTo != 0 ? parentToAddTo : root,
  2418. 0, 0, 1, 1,
  2419. 0, depth, InputOutput, visual,
  2420. CWBorderPixel | CWColormap | CWBackPixmap | CWEventMask | CWOverrideRedirect,
  2421. &swa);
  2422. unsigned int buttonMask = EnterWindowMask | LeaveWindowMask | PointerMotionMask;
  2423. if ((styleFlags & windowIgnoresMouseClicks) == 0)
  2424. buttonMask |= ButtonPressMask | ButtonReleaseMask;
  2425. XGrabButton (display, AnyButton, AnyModifier, windowH, False,
  2426. buttonMask, GrabModeAsync, GrabModeAsync, None, None);
  2427. // Set the window context to identify the window handle object
  2428. if (XSaveContext (display, (XID) windowH, windowHandleXContext, (XPointer) this))
  2429. {
  2430. // Failed
  2431. jassertfalse;
  2432. Logger::outputDebugString ("Failed to create context information for window.\n");
  2433. XDestroyWindow (display, windowH);
  2434. windowH = 0;
  2435. return;
  2436. }
  2437. // Set window manager hints
  2438. XWMHints* wmHints = XAllocWMHints();
  2439. wmHints->flags = InputHint | StateHint;
  2440. wmHints->input = True; // Locally active input model
  2441. wmHints->initial_state = NormalState;
  2442. XSetWMHints (display, windowH, wmHints);
  2443. XFree (wmHints);
  2444. // Set the window type
  2445. setWindowType();
  2446. // Define decoration
  2447. if ((styleFlags & windowHasTitleBar) == 0)
  2448. removeWindowDecorations (windowH);
  2449. else
  2450. addWindowButtons (windowH);
  2451. setTitle (component.getName());
  2452. // Associate the PID, allowing to be shut down when something goes wrong
  2453. unsigned long pid = (unsigned long) getpid();
  2454. xchangeProperty (windowH, atoms.pid, XA_CARDINAL, 32, &pid, 1);
  2455. // Set window manager protocols
  2456. xchangeProperty (windowH, atoms.protocols, XA_ATOM, 32, atoms.protocolList, 2);
  2457. // Set drag and drop flags
  2458. xchangeProperty (windowH, atoms.XdndTypeList, XA_ATOM, 32, atoms.allowedMimeTypes, numElementsInArray (atoms.allowedMimeTypes));
  2459. xchangeProperty (windowH, atoms.XdndActionList, XA_ATOM, 32, atoms.allowedActions, numElementsInArray (atoms.allowedActions));
  2460. xchangeProperty (windowH, atoms.XdndActionDescription, XA_STRING, 8, "", 0);
  2461. xchangeProperty (windowH, atoms.XdndAware, XA_ATOM, 32, &Atoms::DndVersion, 1);
  2462. initialisePointerMap();
  2463. updateModifierMappings();
  2464. }
  2465. void destroyWindow()
  2466. {
  2467. ScopedXLock xlock;
  2468. XPointer handlePointer;
  2469. if (! XFindContext (display, (XID) windowH, windowHandleXContext, &handlePointer))
  2470. XDeleteContext (display, (XID) windowH, windowHandleXContext);
  2471. XDestroyWindow (display, windowH);
  2472. // Wait for it to complete and then remove any events for this
  2473. // window from the event queue.
  2474. XSync (display, false);
  2475. XEvent event;
  2476. while (XCheckWindowEvent (display, windowH, getAllEventsMask(), &event) == True)
  2477. {}
  2478. }
  2479. int getAllEventsMask() const noexcept
  2480. {
  2481. return NoEventMask | KeyPressMask | KeyReleaseMask
  2482. | EnterWindowMask | LeaveWindowMask | PointerMotionMask | KeymapStateMask
  2483. | ExposureMask | StructureNotifyMask | FocusChangeMask
  2484. | ((styleFlags & windowIgnoresMouseClicks) != 0 ? (ButtonPressMask | ButtonReleaseMask) : 0);
  2485. }
  2486. template <typename EventType>
  2487. static int64 getEventTime (const EventType& t)
  2488. {
  2489. return getEventTime (t.time);
  2490. }
  2491. static int64 getEventTime (::Time t)
  2492. {
  2493. static int64 eventTimeOffset = 0x12345678;
  2494. const int64 thisMessageTime = (int64) t;
  2495. if (eventTimeOffset == 0x12345678)
  2496. eventTimeOffset = Time::currentTimeMillis() - thisMessageTime;
  2497. return eventTimeOffset + thisMessageTime;
  2498. }
  2499. long getUserTime() const
  2500. {
  2501. GetXProperty prop (windowH, atoms.userTime, 0, 65536, false, XA_CARDINAL);
  2502. return prop.success ? *(long*) prop.data : 0;
  2503. }
  2504. void updateBorderSize()
  2505. {
  2506. if ((styleFlags & windowHasTitleBar) == 0)
  2507. {
  2508. windowBorder = BorderSize<int> (0);
  2509. }
  2510. else if (windowBorder.getTopAndBottom() == 0 && windowBorder.getLeftAndRight() == 0)
  2511. {
  2512. ScopedXLock xlock;
  2513. Atom hints = Atoms::getIfExists ("_NET_FRAME_EXTENTS");
  2514. if (hints != None)
  2515. {
  2516. GetXProperty prop (windowH, hints, 0, 4, false, XA_CARDINAL);
  2517. if (prop.success && prop.actualFormat == 32)
  2518. {
  2519. const unsigned long* const sizes = (const unsigned long*) prop.data;
  2520. windowBorder = BorderSize<int> ((int) sizes[2], (int) sizes[0],
  2521. (int) sizes[3], (int) sizes[1]);
  2522. }
  2523. }
  2524. }
  2525. }
  2526. void updateWindowBounds()
  2527. {
  2528. jassert (windowH != 0);
  2529. if (windowH != 0)
  2530. {
  2531. Window root, child;
  2532. int wx = 0, wy = 0;
  2533. unsigned int ww = 0, wh = 0, bw, bitDepth;
  2534. ScopedXLock xlock;
  2535. if (XGetGeometry (display, (::Drawable) windowH, &root, &wx, &wy, &ww, &wh, &bw, &bitDepth))
  2536. if (! XTranslateCoordinates (display, windowH, root, 0, 0, &wx, &wy, &child))
  2537. wx = wy = 0;
  2538. Rectangle<int> physicalBounds (wx, wy, (int) ww, (int) wh);
  2539. currentScaleFactor =
  2540. DisplayGeometry::getInstance().findDisplayForRect (physicalBounds, false).scale;
  2541. bounds = DisplayGeometry::physicalToScaled (physicalBounds);
  2542. }
  2543. }
  2544. //==============================================================================
  2545. struct DragState
  2546. {
  2547. DragState()
  2548. : isText (false), dragging (false), expectingStatus (false),
  2549. canDrop (false), targetWindow (None), xdndVersion (-1)
  2550. {
  2551. if (isText)
  2552. allowedTypes.add (Atoms::getCreating ("text/plain"));
  2553. else
  2554. allowedTypes.add (Atoms::getCreating ("text/uri-list"));
  2555. }
  2556. bool isText;
  2557. bool dragging; // currently performing outgoing external dnd as Xdnd source, have grabbed mouse
  2558. bool expectingStatus; // XdndPosition sent, waiting for XdndStatus
  2559. bool canDrop; // target window signals it will accept the drop
  2560. Window targetWindow; // potential drop target
  2561. int xdndVersion; // negotiated version with target
  2562. Rectangle<int> silentRect;
  2563. String textOrFiles;
  2564. Array<Atom> allowedTypes;
  2565. };
  2566. //==============================================================================
  2567. void resetDragAndDrop()
  2568. {
  2569. dragInfo.clear();
  2570. dragInfo.position = Point<int> (-1, -1);
  2571. dragAndDropCurrentMimeType = 0;
  2572. dragAndDropSourceWindow = 0;
  2573. srcMimeTypeAtomList.clear();
  2574. finishAfterDropDataReceived = false;
  2575. }
  2576. void resetExternalDragState()
  2577. {
  2578. dragState = DragState();
  2579. }
  2580. void sendDragAndDropMessage (XClientMessageEvent& msg)
  2581. {
  2582. msg.type = ClientMessage;
  2583. msg.display = display;
  2584. msg.window = dragAndDropSourceWindow;
  2585. msg.format = 32;
  2586. msg.data.l[0] = (long) windowH;
  2587. ScopedXLock xlock;
  2588. XSendEvent (display, dragAndDropSourceWindow, False, 0, (XEvent*) &msg);
  2589. }
  2590. bool sendExternalDragAndDropMessage (XClientMessageEvent& msg, const Window targetWindow)
  2591. {
  2592. msg.type = ClientMessage;
  2593. msg.display = display;
  2594. msg.window = targetWindow;
  2595. msg.format = 32;
  2596. msg.data.l[0] = (long) windowH;
  2597. ScopedXLock xlock;
  2598. return XSendEvent (display, targetWindow, False, 0, (XEvent*) &msg) != 0;
  2599. }
  2600. void sendExternalDragAndDropDrop (const Window targetWindow)
  2601. {
  2602. XClientMessageEvent msg;
  2603. zerostruct (msg);
  2604. msg.message_type = atoms.XdndDrop;
  2605. msg.data.l[2] = CurrentTime;
  2606. sendExternalDragAndDropMessage (msg, targetWindow);
  2607. }
  2608. void sendExternalDragAndDropEnter (const Window targetWindow)
  2609. {
  2610. XClientMessageEvent msg;
  2611. zerostruct (msg);
  2612. msg.message_type = atoms.XdndEnter;
  2613. msg.data.l[1] = (dragState.xdndVersion << 24);
  2614. msg.data.l[2] = (long) dragState.allowedTypes[0];
  2615. sendExternalDragAndDropMessage (msg, targetWindow);
  2616. }
  2617. void sendExternalDragAndDropPosition (const Window targetWindow)
  2618. {
  2619. XClientMessageEvent msg;
  2620. zerostruct (msg);
  2621. msg.message_type = atoms.XdndPosition;
  2622. Point<int> mousePos (Desktop::getInstance().getMousePosition());
  2623. if (dragState.silentRect.contains (mousePos)) // we've been asked to keep silent
  2624. return;
  2625. mousePos = DisplayGeometry::scaledToPhysical (mousePos);
  2626. msg.data.l[1] = 0;
  2627. msg.data.l[2] = (mousePos.x << 16) | mousePos.y;
  2628. msg.data.l[3] = CurrentTime;
  2629. msg.data.l[4] = (long) atoms.XdndActionCopy; // this is all JUCE currently supports
  2630. dragState.expectingStatus = sendExternalDragAndDropMessage (msg, targetWindow);
  2631. }
  2632. void sendDragAndDropStatus (const bool acceptDrop, Atom dropAction)
  2633. {
  2634. XClientMessageEvent msg;
  2635. zerostruct (msg);
  2636. msg.message_type = atoms.XdndStatus;
  2637. msg.data.l[1] = (acceptDrop ? 1 : 0) | 2; // 2 indicates that we want to receive position messages
  2638. msg.data.l[4] = (long) dropAction;
  2639. sendDragAndDropMessage (msg);
  2640. }
  2641. void sendExternalDragAndDropLeave (const Window targetWindow)
  2642. {
  2643. XClientMessageEvent msg;
  2644. zerostruct (msg);
  2645. msg.message_type = atoms.XdndLeave;
  2646. sendExternalDragAndDropMessage (msg, targetWindow);
  2647. }
  2648. void sendDragAndDropFinish()
  2649. {
  2650. XClientMessageEvent msg;
  2651. zerostruct (msg);
  2652. msg.message_type = atoms.XdndFinished;
  2653. sendDragAndDropMessage (msg);
  2654. }
  2655. void handleExternalSelectionClear()
  2656. {
  2657. if (dragState.dragging)
  2658. externalResetDragAndDrop();
  2659. }
  2660. void handleExternalSelectionRequest (const XEvent& evt)
  2661. {
  2662. Atom targetType = evt.xselectionrequest.target;
  2663. XEvent s;
  2664. s.xselection.type = SelectionNotify;
  2665. s.xselection.requestor = evt.xselectionrequest.requestor;
  2666. s.xselection.selection = evt.xselectionrequest.selection;
  2667. s.xselection.target = targetType;
  2668. s.xselection.property = None;
  2669. s.xselection.time = evt.xselectionrequest.time;
  2670. if (dragState.allowedTypes.contains (targetType))
  2671. {
  2672. s.xselection.property = evt.xselectionrequest.property;
  2673. xchangeProperty (evt.xselectionrequest.requestor,
  2674. evt.xselectionrequest.property,
  2675. targetType, 8,
  2676. dragState.textOrFiles.toRawUTF8(),
  2677. (int) dragState.textOrFiles.getNumBytesAsUTF8());
  2678. }
  2679. XSendEvent (display, evt.xselectionrequest.requestor, True, 0, &s);
  2680. }
  2681. void handleExternalDragAndDropStatus (const XClientMessageEvent& clientMsg)
  2682. {
  2683. if (dragState.expectingStatus)
  2684. {
  2685. dragState.expectingStatus = false;
  2686. dragState.canDrop = false;
  2687. dragState.silentRect = Rectangle<int>();
  2688. if ((clientMsg.data.l[1] & 1) != 0
  2689. && ((Atom) clientMsg.data.l[4] == atoms.XdndActionCopy
  2690. || (Atom) clientMsg.data.l[4] == atoms.XdndActionPrivate))
  2691. {
  2692. if ((clientMsg.data.l[1] & 2) == 0) // target requests silent rectangle
  2693. dragState.silentRect.setBounds ((int) clientMsg.data.l[2] >> 16,
  2694. (int) clientMsg.data.l[2] & 0xffff,
  2695. (int) clientMsg.data.l[3] >> 16,
  2696. (int) clientMsg.data.l[3] & 0xffff);
  2697. dragState.canDrop = true;
  2698. }
  2699. }
  2700. }
  2701. void handleExternalDragButtonReleaseEvent()
  2702. {
  2703. if (dragState.dragging)
  2704. XUngrabPointer (display, CurrentTime);
  2705. if (dragState.canDrop)
  2706. {
  2707. sendExternalDragAndDropDrop (dragState.targetWindow);
  2708. }
  2709. else
  2710. {
  2711. sendExternalDragAndDropLeave (dragState.targetWindow);
  2712. externalResetDragAndDrop();
  2713. }
  2714. }
  2715. void handleExternalDragMotionNotify()
  2716. {
  2717. Window targetWindow = externalFindDragTargetWindow (RootWindow (display, DefaultScreen (display)));
  2718. if (dragState.targetWindow != targetWindow)
  2719. {
  2720. if (dragState.targetWindow != None)
  2721. sendExternalDragAndDropLeave (dragState.targetWindow);
  2722. dragState.canDrop = false;
  2723. dragState.silentRect = Rectangle<int>();
  2724. if (targetWindow == None)
  2725. return;
  2726. GetXProperty prop (targetWindow, atoms.XdndAware,
  2727. 0, 2, false, AnyPropertyType);
  2728. if (prop.success
  2729. && prop.data != None
  2730. && prop.actualFormat == 32
  2731. && prop.numItems == 1)
  2732. {
  2733. dragState.xdndVersion = jmin ((int) prop.data[0], (int) Atoms::DndVersion);
  2734. }
  2735. else
  2736. {
  2737. dragState.xdndVersion = -1;
  2738. return;
  2739. }
  2740. sendExternalDragAndDropEnter (targetWindow);
  2741. dragState.targetWindow = targetWindow;
  2742. }
  2743. if (! dragState.expectingStatus)
  2744. sendExternalDragAndDropPosition (targetWindow);
  2745. }
  2746. void handleDragAndDropPosition (const XClientMessageEvent& clientMsg)
  2747. {
  2748. if (dragAndDropSourceWindow == 0)
  2749. return;
  2750. dragAndDropSourceWindow = (::Window) clientMsg.data.l[0];
  2751. Point<int> dropPos ((int) clientMsg.data.l[2] >> 16,
  2752. (int) clientMsg.data.l[2] & 0xffff);
  2753. dropPos -= bounds.getPosition();
  2754. Atom targetAction = atoms.XdndActionCopy;
  2755. for (int i = numElementsInArray (atoms.allowedActions); --i >= 0;)
  2756. {
  2757. if ((Atom) clientMsg.data.l[4] == atoms.allowedActions[i])
  2758. {
  2759. targetAction = atoms.allowedActions[i];
  2760. break;
  2761. }
  2762. }
  2763. sendDragAndDropStatus (true, targetAction);
  2764. if (dragInfo.position != dropPos)
  2765. {
  2766. dragInfo.position = dropPos;
  2767. if (dragInfo.isEmpty())
  2768. updateDraggedFileList (clientMsg);
  2769. if (! dragInfo.isEmpty())
  2770. handleDragMove (dragInfo);
  2771. }
  2772. }
  2773. void handleDragAndDropDrop (const XClientMessageEvent& clientMsg)
  2774. {
  2775. if (dragInfo.isEmpty())
  2776. {
  2777. // no data, transaction finished in handleDragAndDropSelection()
  2778. finishAfterDropDataReceived = true;
  2779. updateDraggedFileList (clientMsg);
  2780. }
  2781. else
  2782. {
  2783. handleDragAndDropDataReceived(); // data was already received
  2784. }
  2785. }
  2786. void handleDragAndDropDataReceived()
  2787. {
  2788. DragInfo dragInfoCopy (dragInfo);
  2789. sendDragAndDropFinish();
  2790. resetDragAndDrop();
  2791. if (! dragInfoCopy.isEmpty())
  2792. handleDragDrop (dragInfoCopy);
  2793. }
  2794. void handleDragAndDropEnter (const XClientMessageEvent& clientMsg)
  2795. {
  2796. dragInfo.clear();
  2797. srcMimeTypeAtomList.clear();
  2798. dragAndDropCurrentMimeType = 0;
  2799. const unsigned long dndCurrentVersion = static_cast<unsigned long> (clientMsg.data.l[1] & 0xff000000) >> 24;
  2800. if (dndCurrentVersion < 3 || dndCurrentVersion > Atoms::DndVersion)
  2801. {
  2802. dragAndDropSourceWindow = 0;
  2803. return;
  2804. }
  2805. dragAndDropSourceWindow = (::Window) clientMsg.data.l[0];
  2806. if ((clientMsg.data.l[1] & 1) != 0)
  2807. {
  2808. ScopedXLock xlock;
  2809. GetXProperty prop (dragAndDropSourceWindow, atoms.XdndTypeList, 0, 0x8000000L, false, XA_ATOM);
  2810. if (prop.success
  2811. && prop.actualType == XA_ATOM
  2812. && prop.actualFormat == 32
  2813. && prop.numItems != 0)
  2814. {
  2815. const unsigned long* const types = (const unsigned long*) prop.data;
  2816. for (unsigned long i = 0; i < prop.numItems; ++i)
  2817. if (types[i] != None)
  2818. srcMimeTypeAtomList.add (types[i]);
  2819. }
  2820. }
  2821. if (srcMimeTypeAtomList.size() == 0)
  2822. {
  2823. for (int i = 2; i < 5; ++i)
  2824. if (clientMsg.data.l[i] != None)
  2825. srcMimeTypeAtomList.add ((unsigned long) clientMsg.data.l[i]);
  2826. if (srcMimeTypeAtomList.size() == 0)
  2827. {
  2828. dragAndDropSourceWindow = 0;
  2829. return;
  2830. }
  2831. }
  2832. for (int i = 0; i < srcMimeTypeAtomList.size() && dragAndDropCurrentMimeType == 0; ++i)
  2833. for (int j = 0; j < numElementsInArray (atoms.allowedMimeTypes); ++j)
  2834. if (srcMimeTypeAtomList[i] == atoms.allowedMimeTypes[j])
  2835. dragAndDropCurrentMimeType = atoms.allowedMimeTypes[j];
  2836. handleDragAndDropPosition (clientMsg);
  2837. }
  2838. void handleDragAndDropSelection (const XEvent& evt)
  2839. {
  2840. dragInfo.clear();
  2841. if (evt.xselection.property != None)
  2842. {
  2843. StringArray lines;
  2844. {
  2845. MemoryBlock dropData;
  2846. for (;;)
  2847. {
  2848. GetXProperty prop (evt.xany.window, evt.xselection.property,
  2849. dropData.getSize() / 4, 65536, false, AnyPropertyType);
  2850. if (! prop.success)
  2851. break;
  2852. dropData.append (prop.data, prop.numItems * (size_t) prop.actualFormat / 8);
  2853. if (prop.bytesLeft <= 0)
  2854. break;
  2855. }
  2856. lines.addLines (dropData.toString());
  2857. }
  2858. if (Atoms::isMimeTypeFile (dragAndDropCurrentMimeType))
  2859. {
  2860. for (int i = 0; i < lines.size(); ++i)
  2861. dragInfo.files.add (URL::removeEscapeChars (lines[i].replace ("file://", String::empty, true)));
  2862. dragInfo.files.trim();
  2863. dragInfo.files.removeEmptyStrings();
  2864. }
  2865. else
  2866. {
  2867. dragInfo.text = lines.joinIntoString ("\n");
  2868. }
  2869. if (finishAfterDropDataReceived)
  2870. handleDragAndDropDataReceived();
  2871. }
  2872. }
  2873. void updateDraggedFileList (const XClientMessageEvent& clientMsg)
  2874. {
  2875. jassert (dragInfo.isEmpty());
  2876. if (dragAndDropSourceWindow != None
  2877. && dragAndDropCurrentMimeType != None)
  2878. {
  2879. ScopedXLock xlock;
  2880. XConvertSelection (display,
  2881. atoms.XdndSelection,
  2882. dragAndDropCurrentMimeType,
  2883. Atoms::getCreating ("JXSelectionWindowProperty"),
  2884. windowH,
  2885. (::Time) clientMsg.data.l[2]);
  2886. }
  2887. }
  2888. bool isWindowDnDAware (Window w) const
  2889. {
  2890. int numProperties = 0;
  2891. Atom* const properties = XListProperties (display, w, &numProperties);
  2892. bool dndAwarePropFound = false;
  2893. for (int i = 0; i < numProperties; ++i)
  2894. if (properties[i] == atoms.XdndAware)
  2895. dndAwarePropFound = true;
  2896. if (properties != nullptr)
  2897. XFree (properties);
  2898. return dndAwarePropFound;
  2899. }
  2900. Window externalFindDragTargetWindow (Window targetWindow)
  2901. {
  2902. if (targetWindow == None)
  2903. return None;
  2904. if (isWindowDnDAware (targetWindow))
  2905. return targetWindow;
  2906. Window child, phonyWin;
  2907. int phony;
  2908. unsigned int uphony;
  2909. XQueryPointer (display, targetWindow, &phonyWin, &child,
  2910. &phony, &phony, &phony, &phony, &uphony);
  2911. return externalFindDragTargetWindow (child);
  2912. }
  2913. bool externalDragInit (bool isText, const String& textOrFiles)
  2914. {
  2915. ScopedXLock xlock;
  2916. resetExternalDragState();
  2917. dragState.isText = isText;
  2918. dragState.textOrFiles = textOrFiles;
  2919. dragState.targetWindow = windowH;
  2920. const int pointerGrabMask = Button1MotionMask | ButtonReleaseMask;
  2921. if (XGrabPointer (display, windowH, True, pointerGrabMask,
  2922. GrabModeAsync, GrabModeAsync, None, None, CurrentTime) == GrabSuccess)
  2923. {
  2924. // No other method of changing the pointer seems to work, this call is needed from this very context
  2925. XChangeActivePointerGrab (display, pointerGrabMask, (Cursor) createDraggingHandCursor(), CurrentTime);
  2926. XSetSelectionOwner (display, atoms.XdndSelection, windowH, CurrentTime);
  2927. // save the available types to XdndTypeList
  2928. xchangeProperty (windowH, atoms.XdndTypeList, XA_ATOM, 32,
  2929. dragState.allowedTypes.getRawDataPointer(),
  2930. dragState.allowedTypes.size());
  2931. dragState.dragging = true;
  2932. handleExternalDragMotionNotify();
  2933. return true;
  2934. }
  2935. return false;
  2936. }
  2937. void externalResetDragAndDrop()
  2938. {
  2939. if (dragState.dragging)
  2940. {
  2941. ScopedXLock xlock;
  2942. XUngrabPointer (display, CurrentTime);
  2943. }
  2944. resetExternalDragState();
  2945. }
  2946. DragState dragState;
  2947. DragInfo dragInfo;
  2948. Atom dragAndDropCurrentMimeType;
  2949. Window dragAndDropSourceWindow;
  2950. bool finishAfterDropDataReceived;
  2951. Array<Atom> srcMimeTypeAtomList;
  2952. int pointerMap[5];
  2953. void initialisePointerMap()
  2954. {
  2955. const int numButtons = XGetPointerMapping (display, 0, 0);
  2956. pointerMap[2] = pointerMap[3] = pointerMap[4] = Keys::NoButton;
  2957. if (numButtons == 2)
  2958. {
  2959. pointerMap[0] = Keys::LeftButton;
  2960. pointerMap[1] = Keys::RightButton;
  2961. }
  2962. else if (numButtons >= 3)
  2963. {
  2964. pointerMap[0] = Keys::LeftButton;
  2965. pointerMap[1] = Keys::MiddleButton;
  2966. pointerMap[2] = Keys::RightButton;
  2967. if (numButtons >= 5)
  2968. {
  2969. pointerMap[3] = Keys::WheelUp;
  2970. pointerMap[4] = Keys::WheelDown;
  2971. }
  2972. }
  2973. }
  2974. static Point<int> lastMousePos;
  2975. static void clearLastMousePos() noexcept
  2976. {
  2977. lastMousePos = Point<int> (0x100000, 0x100000);
  2978. }
  2979. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LinuxComponentPeer)
  2980. };
  2981. ModifierKeys LinuxComponentPeer::currentModifiers;
  2982. bool LinuxComponentPeer::isActiveApplication = false;
  2983. Point<int> LinuxComponentPeer::lastMousePos;
  2984. //==============================================================================
  2985. JUCE_API bool JUCE_CALLTYPE Process::isForegroundProcess()
  2986. {
  2987. return LinuxComponentPeer::isActiveApplication;
  2988. }
  2989. // N/A on Linux as far as I know.
  2990. JUCE_API void JUCE_CALLTYPE Process::makeForegroundProcess() {}
  2991. JUCE_API void JUCE_CALLTYPE Process::hide() {}
  2992. //==============================================================================
  2993. void ModifierKeys::updateCurrentModifiers() noexcept
  2994. {
  2995. currentModifiers = LinuxComponentPeer::currentModifiers;
  2996. }
  2997. ModifierKeys ModifierKeys::getCurrentModifiersRealtime() noexcept
  2998. {
  2999. if (display != nullptr)
  3000. {
  3001. Window root, child;
  3002. int x, y, winx, winy;
  3003. unsigned int mask;
  3004. int mouseMods = 0;
  3005. ScopedXLock xlock;
  3006. if (XQueryPointer (display, RootWindow (display, DefaultScreen (display)),
  3007. &root, &child, &x, &y, &winx, &winy, &mask) != False)
  3008. {
  3009. if ((mask & Button1Mask) != 0) mouseMods |= ModifierKeys::leftButtonModifier;
  3010. if ((mask & Button2Mask) != 0) mouseMods |= ModifierKeys::middleButtonModifier;
  3011. if ((mask & Button3Mask) != 0) mouseMods |= ModifierKeys::rightButtonModifier;
  3012. }
  3013. LinuxComponentPeer::currentModifiers = LinuxComponentPeer::currentModifiers.withoutMouseButtons().withFlags (mouseMods);
  3014. }
  3015. return LinuxComponentPeer::currentModifiers;
  3016. }
  3017. //==============================================================================
  3018. void Desktop::setKioskComponent (Component* comp, bool enableOrDisable, bool /* allowMenusAndBars */)
  3019. {
  3020. if (enableOrDisable)
  3021. comp->setBounds (getDisplays().getMainDisplay().totalArea);
  3022. }
  3023. void Desktop::allowedOrientationsChanged() {}
  3024. //==============================================================================
  3025. ComponentPeer* Component::createNewPeer (int styleFlags, void* nativeWindowToAttachTo)
  3026. {
  3027. return new LinuxComponentPeer (*this, styleFlags, (Window) nativeWindowToAttachTo);
  3028. }
  3029. //==============================================================================
  3030. void Desktop::Displays::findDisplays (float masterScale)
  3031. {
  3032. DisplayGeometry& geometry = DisplayGeometry::getOrCreateInstance (display, masterScale);
  3033. // add the main display first
  3034. int mainDisplayIdx;
  3035. for (mainDisplayIdx = 0; mainDisplayIdx < geometry.infos.size(); ++mainDisplayIdx)
  3036. {
  3037. const DisplayGeometry::ExtendedInfo& info = geometry.infos.getReference (mainDisplayIdx);
  3038. if (info.isMain)
  3039. break;
  3040. }
  3041. // no main display found then use the first
  3042. if (mainDisplayIdx >= geometry.infos.size())
  3043. mainDisplayIdx = 0;
  3044. // add the main display
  3045. {
  3046. const DisplayGeometry::ExtendedInfo& info =
  3047. geometry.infos.getReference (mainDisplayIdx);
  3048. Desktop::Displays::Display d;
  3049. d.isMain = true;
  3050. d.scale = masterScale * info.scale;
  3051. d.dpi = info.dpi;
  3052. d.totalArea = DisplayGeometry::physicalToScaled (info.totalBounds);
  3053. d.userArea = (info.usableBounds / d.scale) + info.topLeftScaled;
  3054. displays.add (d);
  3055. }
  3056. for (int i = 0; i < geometry.infos.size(); ++i)
  3057. {
  3058. // don't add the main display a second time
  3059. if (i == mainDisplayIdx)
  3060. continue;
  3061. const DisplayGeometry::ExtendedInfo& info = geometry.infos.getReference (i);
  3062. Desktop::Displays::Display d;
  3063. d.isMain = false;
  3064. d.scale = masterScale * info.scale;
  3065. d.dpi = info.dpi;
  3066. d.totalArea = DisplayGeometry::physicalToScaled (info.totalBounds);
  3067. d.userArea = (info.usableBounds / d.scale) + info.topLeftScaled;
  3068. displays.add (d);
  3069. }
  3070. }
  3071. //==============================================================================
  3072. bool MouseInputSource::SourceList::addSource()
  3073. {
  3074. if (sources.size() == 0)
  3075. {
  3076. addSource (0, true);
  3077. return true;
  3078. }
  3079. return false;
  3080. }
  3081. bool Desktop::canUseSemiTransparentWindows() noexcept
  3082. {
  3083. #if JUCE_USE_XRENDER
  3084. if (XRender::hasCompositingWindowManager())
  3085. {
  3086. int matchedDepth = 0, desiredDepth = 32;
  3087. return Visuals::findVisualFormat (desiredDepth, matchedDepth) != 0
  3088. && matchedDepth == desiredDepth;
  3089. }
  3090. #endif
  3091. return false;
  3092. }
  3093. Point<float> MouseInputSource::getCurrentRawMousePosition()
  3094. {
  3095. if (display == nullptr)
  3096. return Point<float>();
  3097. Window root, child;
  3098. int x, y, winx, winy;
  3099. unsigned int mask;
  3100. ScopedXLock xlock;
  3101. if (XQueryPointer (display,
  3102. RootWindow (display, DefaultScreen (display)),
  3103. &root, &child,
  3104. &x, &y, &winx, &winy, &mask) == False)
  3105. {
  3106. // Pointer not on the default screen
  3107. x = y = -1;
  3108. }
  3109. return DisplayGeometry::physicalToScaled (Point<float> ((float) x, (float) y));
  3110. }
  3111. void MouseInputSource::setRawMousePosition (Point<float> newPosition)
  3112. {
  3113. if (display != nullptr)
  3114. {
  3115. ScopedXLock xlock;
  3116. Window root = RootWindow (display, DefaultScreen (display));
  3117. newPosition = DisplayGeometry::scaledToPhysical (newPosition);
  3118. XWarpPointer (display, None, root, 0, 0, 0, 0, roundToInt (newPosition.getX()), roundToInt (newPosition.getY()));
  3119. }
  3120. }
  3121. double Desktop::getDefaultMasterScale()
  3122. {
  3123. return 1.0;
  3124. }
  3125. Desktop::DisplayOrientation Desktop::getCurrentOrientation() const
  3126. {
  3127. return upright;
  3128. }
  3129. //==============================================================================
  3130. static bool screenSaverAllowed = true;
  3131. void Desktop::setScreenSaverEnabled (const bool isEnabled)
  3132. {
  3133. if (screenSaverAllowed != isEnabled)
  3134. {
  3135. screenSaverAllowed = isEnabled;
  3136. if (display != nullptr)
  3137. {
  3138. typedef void (*tXScreenSaverSuspend) (Display*, Bool);
  3139. static tXScreenSaverSuspend xScreenSaverSuspend = nullptr;
  3140. if (xScreenSaverSuspend == nullptr)
  3141. if (void* h = dlopen ("libXss.so", RTLD_GLOBAL | RTLD_NOW))
  3142. xScreenSaverSuspend = (tXScreenSaverSuspend) dlsym (h, "XScreenSaverSuspend");
  3143. ScopedXLock xlock;
  3144. if (xScreenSaverSuspend != nullptr)
  3145. xScreenSaverSuspend (display, ! isEnabled);
  3146. }
  3147. }
  3148. }
  3149. bool Desktop::isScreenSaverEnabled()
  3150. {
  3151. return screenSaverAllowed;
  3152. }
  3153. //==============================================================================
  3154. void* CustomMouseCursorInfo::create() const
  3155. {
  3156. if (display == nullptr)
  3157. return nullptr;
  3158. ScopedXLock xlock;
  3159. const unsigned int imageW = (unsigned int) image.getWidth();
  3160. const unsigned int imageH = (unsigned int) image.getHeight();
  3161. int hotspotX = hotspot.x;
  3162. int hotspotY = hotspot.y;
  3163. #if JUCE_USE_XCURSOR
  3164. {
  3165. typedef XcursorBool (*tXcursorSupportsARGB) (Display*);
  3166. typedef XcursorImage* (*tXcursorImageCreate) (int, int);
  3167. typedef void (*tXcursorImageDestroy) (XcursorImage*);
  3168. typedef Cursor (*tXcursorImageLoadCursor) (Display*, const XcursorImage*);
  3169. static tXcursorSupportsARGB xcursorSupportsARGB = nullptr;
  3170. static tXcursorImageCreate xcursorImageCreate = nullptr;
  3171. static tXcursorImageDestroy xcursorImageDestroy = nullptr;
  3172. static tXcursorImageLoadCursor xcursorImageLoadCursor = nullptr;
  3173. static bool hasBeenLoaded = false;
  3174. if (! hasBeenLoaded)
  3175. {
  3176. hasBeenLoaded = true;
  3177. if (void* h = dlopen ("libXcursor.so", RTLD_GLOBAL | RTLD_NOW))
  3178. {
  3179. xcursorSupportsARGB = (tXcursorSupportsARGB) dlsym (h, "XcursorSupportsARGB");
  3180. xcursorImageCreate = (tXcursorImageCreate) dlsym (h, "XcursorImageCreate");
  3181. xcursorImageLoadCursor = (tXcursorImageLoadCursor) dlsym (h, "XcursorImageLoadCursor");
  3182. xcursorImageDestroy = (tXcursorImageDestroy) dlsym (h, "XcursorImageDestroy");
  3183. if (xcursorSupportsARGB == nullptr || xcursorImageCreate == nullptr
  3184. || xcursorImageLoadCursor == nullptr || xcursorImageDestroy == nullptr
  3185. || ! xcursorSupportsARGB (display))
  3186. xcursorSupportsARGB = nullptr;
  3187. }
  3188. }
  3189. if (xcursorSupportsARGB != nullptr)
  3190. {
  3191. if (XcursorImage* xcImage = xcursorImageCreate ((int) imageW, (int) imageH))
  3192. {
  3193. xcImage->xhot = (XcursorDim) hotspotX;
  3194. xcImage->yhot = (XcursorDim) hotspotY;
  3195. XcursorPixel* dest = xcImage->pixels;
  3196. for (int y = 0; y < (int) imageH; ++y)
  3197. for (int x = 0; x < (int) imageW; ++x)
  3198. *dest++ = image.getPixelAt (x, y).getARGB();
  3199. void* result = (void*) xcursorImageLoadCursor (display, xcImage);
  3200. xcursorImageDestroy (xcImage);
  3201. if (result != nullptr)
  3202. return result;
  3203. }
  3204. }
  3205. }
  3206. #endif
  3207. Window root = RootWindow (display, DefaultScreen (display));
  3208. unsigned int cursorW, cursorH;
  3209. if (! XQueryBestCursor (display, root, imageW, imageH, &cursorW, &cursorH))
  3210. return nullptr;
  3211. Image im (Image::ARGB, (int) cursorW, (int) cursorH, true);
  3212. {
  3213. Graphics g (im);
  3214. if (imageW > cursorW || imageH > cursorH)
  3215. {
  3216. hotspotX = (hotspotX * (int) cursorW) / (int) imageW;
  3217. hotspotY = (hotspotY * (int) cursorH) / (int) imageH;
  3218. g.drawImageWithin (image, 0, 0, (int) imageW, (int) imageH,
  3219. RectanglePlacement::xLeft | RectanglePlacement::yTop | RectanglePlacement::onlyReduceInSize,
  3220. false);
  3221. }
  3222. else
  3223. {
  3224. g.drawImageAt (image, 0, 0);
  3225. }
  3226. }
  3227. const unsigned int stride = (cursorW + 7) >> 3;
  3228. HeapBlock<char> maskPlane, sourcePlane;
  3229. maskPlane.calloc (stride * cursorH);
  3230. sourcePlane.calloc (stride * cursorH);
  3231. const bool msbfirst = (BitmapBitOrder (display) == MSBFirst);
  3232. for (int y = (int) cursorH; --y >= 0;)
  3233. {
  3234. for (int x = (int) cursorW; --x >= 0;)
  3235. {
  3236. const char mask = (char) (1 << (msbfirst ? (7 - (x & 7)) : (x & 7)));
  3237. const unsigned int offset = (unsigned int) y * stride + ((unsigned int) x >> 3);
  3238. const Colour c (im.getPixelAt (x, y));
  3239. if (c.getAlpha() >= 128) maskPlane[offset] |= mask;
  3240. if (c.getBrightness() >= 0.5f) sourcePlane[offset] |= mask;
  3241. }
  3242. }
  3243. Pixmap sourcePixmap = XCreatePixmapFromBitmapData (display, root, sourcePlane.getData(), cursorW, cursorH, 0xffff, 0, 1);
  3244. Pixmap maskPixmap = XCreatePixmapFromBitmapData (display, root, maskPlane.getData(), cursorW, cursorH, 0xffff, 0, 1);
  3245. XColor white, black;
  3246. black.red = black.green = black.blue = 0;
  3247. white.red = white.green = white.blue = 0xffff;
  3248. void* result = (void*) XCreatePixmapCursor (display, sourcePixmap, maskPixmap, &white, &black,
  3249. (unsigned int) hotspotX, (unsigned int) hotspotY);
  3250. XFreePixmap (display, sourcePixmap);
  3251. XFreePixmap (display, maskPixmap);
  3252. return result;
  3253. }
  3254. void MouseCursor::deleteMouseCursor (void* const cursorHandle, const bool)
  3255. {
  3256. if (cursorHandle != nullptr && display != nullptr)
  3257. {
  3258. ScopedXLock xlock;
  3259. XFreeCursor (display, (Cursor) cursorHandle);
  3260. }
  3261. }
  3262. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType type)
  3263. {
  3264. if (display == nullptr)
  3265. return None;
  3266. unsigned int shape;
  3267. switch (type)
  3268. {
  3269. case NormalCursor:
  3270. case ParentCursor: return None; // Use parent cursor
  3271. case NoCursor: return CustomMouseCursorInfo (Image (Image::ARGB, 16, 16, true), 0, 0).create();
  3272. case WaitCursor: shape = XC_watch; break;
  3273. case IBeamCursor: shape = XC_xterm; break;
  3274. case PointingHandCursor: shape = XC_hand2; break;
  3275. case LeftRightResizeCursor: shape = XC_sb_h_double_arrow; break;
  3276. case UpDownResizeCursor: shape = XC_sb_v_double_arrow; break;
  3277. case UpDownLeftRightResizeCursor: shape = XC_fleur; break;
  3278. case TopEdgeResizeCursor: shape = XC_top_side; break;
  3279. case BottomEdgeResizeCursor: shape = XC_bottom_side; break;
  3280. case LeftEdgeResizeCursor: shape = XC_left_side; break;
  3281. case RightEdgeResizeCursor: shape = XC_right_side; break;
  3282. case TopLeftCornerResizeCursor: shape = XC_top_left_corner; break;
  3283. case TopRightCornerResizeCursor: shape = XC_top_right_corner; break;
  3284. case BottomLeftCornerResizeCursor: shape = XC_bottom_left_corner; break;
  3285. case BottomRightCornerResizeCursor: shape = XC_bottom_right_corner; break;
  3286. case CrosshairCursor: shape = XC_crosshair; break;
  3287. case DraggingHandCursor: return createDraggingHandCursor();
  3288. case CopyingCursor:
  3289. {
  3290. static unsigned char copyCursorData[] = { 71,73,70,56,57,97,21,0,21,0,145,0,0,0,0,0,255,255,255,0,
  3291. 128,128,255,255,255,33,249,4,1,0,0,3,0,44,0,0,0,0,21,0, 21,0,0,2,72,4,134,169,171,16,199,98,11,79,90,71,161,93,56,111,
  3292. 78,133,218,215,137,31,82,154,100,200,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,
  3293. 252,114,147,74,83,5,50,68,147,208,217,16,71,149,252,124,5,0,59,0,0 };
  3294. const int copyCursorSize = 119;
  3295. return CustomMouseCursorInfo (ImageFileFormat::loadFrom (copyCursorData, copyCursorSize), 1, 3).create();
  3296. }
  3297. default:
  3298. jassertfalse;
  3299. return None;
  3300. }
  3301. ScopedXLock xlock;
  3302. return (void*) XCreateFontCursor (display, shape);
  3303. }
  3304. void MouseCursor::showInWindow (ComponentPeer* peer) const
  3305. {
  3306. if (LinuxComponentPeer* const lp = dynamic_cast<LinuxComponentPeer*> (peer))
  3307. lp->showMouseCursor ((Cursor) getHandle());
  3308. }
  3309. void MouseCursor::showInAllWindows() const
  3310. {
  3311. for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
  3312. showInWindow (ComponentPeer::getPeer (i));
  3313. }
  3314. //==============================================================================
  3315. Image juce_createIconForFile (const File& /* file */)
  3316. {
  3317. return Image::null;
  3318. }
  3319. //==============================================================================
  3320. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, const bool canMoveFiles)
  3321. {
  3322. if (files.size() == 0)
  3323. return false;
  3324. if (MouseInputSource* draggingSource = Desktop::getInstance().getDraggingMouseSource(0))
  3325. if (Component* sourceComp = draggingSource->getComponentUnderMouse())
  3326. if (LinuxComponentPeer* const lp = dynamic_cast<LinuxComponentPeer*> (sourceComp->getPeer()))
  3327. return lp->externalDragFileInit (files, canMoveFiles);
  3328. // This method must be called in response to a component's mouseDown or mouseDrag event!
  3329. jassertfalse;
  3330. return false;
  3331. }
  3332. bool DragAndDropContainer::performExternalDragDropOfText (const String& text)
  3333. {
  3334. if (text.isEmpty())
  3335. return false;
  3336. if (MouseInputSource* draggingSource = Desktop::getInstance().getDraggingMouseSource(0))
  3337. if (Component* sourceComp = draggingSource->getComponentUnderMouse())
  3338. if (LinuxComponentPeer* const lp = dynamic_cast<LinuxComponentPeer*> (sourceComp->getPeer()))
  3339. return lp->externalDragTextInit (text);
  3340. // This method must be called in response to a component's mouseDown or mouseDrag event!
  3341. jassertfalse;
  3342. return false;
  3343. }
  3344. //==============================================================================
  3345. void LookAndFeel::playAlertSound()
  3346. {
  3347. std::cout << "\a" << std::flush;
  3348. }
  3349. //==============================================================================
  3350. Rectangle<int> juce_LinuxScaledToPhysicalBounds(ComponentPeer* peer, const Rectangle<int>& bounds)
  3351. {
  3352. Rectangle<int> retval = bounds;
  3353. if (LinuxComponentPeer* linuxPeer = dynamic_cast<LinuxComponentPeer*> (peer))
  3354. retval *= linuxPeer->getCurrentScale();
  3355. return retval;
  3356. }
  3357. void juce_LinuxAddRepaintListener (ComponentPeer* peer, Component* dummy)
  3358. {
  3359. if (LinuxComponentPeer* linuxPeer = dynamic_cast<LinuxComponentPeer*> (peer))
  3360. linuxPeer->addOpenGLRepaintListener (dummy);
  3361. }
  3362. void juce_LinuxRemoveRepaintListener (ComponentPeer* peer, Component* dummy)
  3363. {
  3364. if (LinuxComponentPeer* linuxPeer = dynamic_cast<LinuxComponentPeer*> (peer))
  3365. linuxPeer->removeOpenGLRepaintListener (dummy);
  3366. }
  3367. //==============================================================================
  3368. #if JUCE_MODAL_LOOPS_PERMITTED
  3369. void JUCE_CALLTYPE NativeMessageBox::showMessageBox (AlertWindow::AlertIconType iconType,
  3370. const String& title, const String& message,
  3371. Component* /* associatedComponent */)
  3372. {
  3373. AlertWindow::showMessageBox (iconType, title, message);
  3374. }
  3375. #endif
  3376. void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (AlertWindow::AlertIconType iconType,
  3377. const String& title, const String& message,
  3378. Component* associatedComponent,
  3379. ModalComponentManager::Callback* callback)
  3380. {
  3381. AlertWindow::showMessageBoxAsync (iconType, title, message, String::empty, associatedComponent, callback);
  3382. }
  3383. bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (AlertWindow::AlertIconType iconType,
  3384. const String& title, const String& message,
  3385. Component* associatedComponent,
  3386. ModalComponentManager::Callback* callback)
  3387. {
  3388. return AlertWindow::showOkCancelBox (iconType, title, message, String::empty, String::empty,
  3389. associatedComponent, callback);
  3390. }
  3391. int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (AlertWindow::AlertIconType iconType,
  3392. const String& title, const String& message,
  3393. Component* associatedComponent,
  3394. ModalComponentManager::Callback* callback)
  3395. {
  3396. return AlertWindow::showYesNoCancelBox (iconType, title, message,
  3397. String::empty, String::empty, String::empty,
  3398. associatedComponent, callback);
  3399. }
  3400. //==============================================================================
  3401. const int KeyPress::spaceKey = XK_space & 0xff;
  3402. const int KeyPress::returnKey = XK_Return & 0xff;
  3403. const int KeyPress::escapeKey = XK_Escape & 0xff;
  3404. const int KeyPress::backspaceKey = XK_BackSpace & 0xff;
  3405. const int KeyPress::leftKey = (XK_Left & 0xff) | Keys::extendedKeyModifier;
  3406. const int KeyPress::rightKey = (XK_Right & 0xff) | Keys::extendedKeyModifier;
  3407. const int KeyPress::upKey = (XK_Up & 0xff) | Keys::extendedKeyModifier;
  3408. const int KeyPress::downKey = (XK_Down & 0xff) | Keys::extendedKeyModifier;
  3409. const int KeyPress::pageUpKey = (XK_Page_Up & 0xff) | Keys::extendedKeyModifier;
  3410. const int KeyPress::pageDownKey = (XK_Page_Down & 0xff) | Keys::extendedKeyModifier;
  3411. const int KeyPress::endKey = (XK_End & 0xff) | Keys::extendedKeyModifier;
  3412. const int KeyPress::homeKey = (XK_Home & 0xff) | Keys::extendedKeyModifier;
  3413. const int KeyPress::insertKey = (XK_Insert & 0xff) | Keys::extendedKeyModifier;
  3414. const int KeyPress::deleteKey = (XK_Delete & 0xff) | Keys::extendedKeyModifier;
  3415. const int KeyPress::tabKey = XK_Tab & 0xff;
  3416. const int KeyPress::F1Key = (XK_F1 & 0xff) | Keys::extendedKeyModifier;
  3417. const int KeyPress::F2Key = (XK_F2 & 0xff) | Keys::extendedKeyModifier;
  3418. const int KeyPress::F3Key = (XK_F3 & 0xff) | Keys::extendedKeyModifier;
  3419. const int KeyPress::F4Key = (XK_F4 & 0xff) | Keys::extendedKeyModifier;
  3420. const int KeyPress::F5Key = (XK_F5 & 0xff) | Keys::extendedKeyModifier;
  3421. const int KeyPress::F6Key = (XK_F6 & 0xff) | Keys::extendedKeyModifier;
  3422. const int KeyPress::F7Key = (XK_F7 & 0xff) | Keys::extendedKeyModifier;
  3423. const int KeyPress::F8Key = (XK_F8 & 0xff) | Keys::extendedKeyModifier;
  3424. const int KeyPress::F9Key = (XK_F9 & 0xff) | Keys::extendedKeyModifier;
  3425. const int KeyPress::F10Key = (XK_F10 & 0xff) | Keys::extendedKeyModifier;
  3426. const int KeyPress::F11Key = (XK_F11 & 0xff) | Keys::extendedKeyModifier;
  3427. const int KeyPress::F12Key = (XK_F12 & 0xff) | Keys::extendedKeyModifier;
  3428. const int KeyPress::F13Key = (XK_F13 & 0xff) | Keys::extendedKeyModifier;
  3429. const int KeyPress::F14Key = (XK_F14 & 0xff) | Keys::extendedKeyModifier;
  3430. const int KeyPress::F15Key = (XK_F15 & 0xff) | Keys::extendedKeyModifier;
  3431. const int KeyPress::F16Key = (XK_F16 & 0xff) | Keys::extendedKeyModifier;
  3432. const int KeyPress::numberPad0 = (XK_KP_0 & 0xff) | Keys::extendedKeyModifier;
  3433. const int KeyPress::numberPad1 = (XK_KP_1 & 0xff) | Keys::extendedKeyModifier;
  3434. const int KeyPress::numberPad2 = (XK_KP_2 & 0xff) | Keys::extendedKeyModifier;
  3435. const int KeyPress::numberPad3 = (XK_KP_3 & 0xff) | Keys::extendedKeyModifier;
  3436. const int KeyPress::numberPad4 = (XK_KP_4 & 0xff) | Keys::extendedKeyModifier;
  3437. const int KeyPress::numberPad5 = (XK_KP_5 & 0xff) | Keys::extendedKeyModifier;
  3438. const int KeyPress::numberPad6 = (XK_KP_6 & 0xff) | Keys::extendedKeyModifier;
  3439. const int KeyPress::numberPad7 = (XK_KP_7 & 0xff)| Keys::extendedKeyModifier;
  3440. const int KeyPress::numberPad8 = (XK_KP_8 & 0xff)| Keys::extendedKeyModifier;
  3441. const int KeyPress::numberPad9 = (XK_KP_9 & 0xff)| Keys::extendedKeyModifier;
  3442. const int KeyPress::numberPadAdd = (XK_KP_Add & 0xff)| Keys::extendedKeyModifier;
  3443. const int KeyPress::numberPadSubtract = (XK_KP_Subtract & 0xff)| Keys::extendedKeyModifier;
  3444. const int KeyPress::numberPadMultiply = (XK_KP_Multiply & 0xff)| Keys::extendedKeyModifier;
  3445. const int KeyPress::numberPadDivide = (XK_KP_Divide & 0xff)| Keys::extendedKeyModifier;
  3446. const int KeyPress::numberPadSeparator = (XK_KP_Separator & 0xff)| Keys::extendedKeyModifier;
  3447. const int KeyPress::numberPadDecimalPoint = (XK_KP_Decimal & 0xff)| Keys::extendedKeyModifier;
  3448. const int KeyPress::numberPadEquals = (XK_KP_Equal & 0xff)| Keys::extendedKeyModifier;
  3449. const int KeyPress::numberPadDelete = (XK_KP_Delete & 0xff)| Keys::extendedKeyModifier;
  3450. const int KeyPress::playKey = ((int) 0xffeeff00) | Keys::extendedKeyModifier;
  3451. const int KeyPress::stopKey = ((int) 0xffeeff01) | Keys::extendedKeyModifier;
  3452. const int KeyPress::fastForwardKey = ((int) 0xffeeff02) | Keys::extendedKeyModifier;
  3453. const int KeyPress::rewindKey = ((int) 0xffeeff03) | Keys::extendedKeyModifier;