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

3436 lines
120KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. // (This file gets included by juce_linux_NativeCode.cpp, rather than being
  19. // compiled on its own).
  20. #if JUCE_INCLUDED_FILE
  21. //==============================================================================
  22. // These are defined in juce_linux_Messaging.cpp
  23. extern Display* display;
  24. extern XContext windowHandleXContext;
  25. //==============================================================================
  26. namespace Atoms
  27. {
  28. enum ProtocolItems
  29. {
  30. TAKE_FOCUS = 0,
  31. DELETE_WINDOW = 1,
  32. PING = 2
  33. };
  34. static Atom Protocols, ProtocolList[3], ChangeState, State,
  35. ActiveWin, Pid, WindowType, WindowState,
  36. XdndAware, XdndEnter, XdndLeave, XdndPosition, XdndStatus,
  37. XdndDrop, XdndFinished, XdndSelection, XdndTypeList, XdndActionList,
  38. XdndActionDescription, XdndActionCopy,
  39. allowedActions[5],
  40. allowedMimeTypes[2];
  41. const unsigned long DndVersion = 3;
  42. //==============================================================================
  43. static void initialiseAtoms()
  44. {
  45. static bool atomsInitialised = false;
  46. if (! atomsInitialised)
  47. {
  48. atomsInitialised = true;
  49. Protocols = XInternAtom (display, "WM_PROTOCOLS", True);
  50. ProtocolList [TAKE_FOCUS] = XInternAtom (display, "WM_TAKE_FOCUS", True);
  51. ProtocolList [DELETE_WINDOW] = XInternAtom (display, "WM_DELETE_WINDOW", True);
  52. ProtocolList [PING] = XInternAtom (display, "_NET_WM_PING", True);
  53. ChangeState = XInternAtom (display, "WM_CHANGE_STATE", True);
  54. State = XInternAtom (display, "WM_STATE", True);
  55. ActiveWin = XInternAtom (display, "_NET_ACTIVE_WINDOW", False);
  56. Pid = XInternAtom (display, "_NET_WM_PID", False);
  57. WindowType = XInternAtom (display, "_NET_WM_WINDOW_TYPE", True);
  58. WindowState = XInternAtom (display, "_NET_WM_STATE", True);
  59. XdndAware = XInternAtom (display, "XdndAware", False);
  60. XdndEnter = XInternAtom (display, "XdndEnter", False);
  61. XdndLeave = XInternAtom (display, "XdndLeave", False);
  62. XdndPosition = XInternAtom (display, "XdndPosition", False);
  63. XdndStatus = XInternAtom (display, "XdndStatus", False);
  64. XdndDrop = XInternAtom (display, "XdndDrop", False);
  65. XdndFinished = XInternAtom (display, "XdndFinished", False);
  66. XdndSelection = XInternAtom (display, "XdndSelection", False);
  67. XdndTypeList = XInternAtom (display, "XdndTypeList", False);
  68. XdndActionList = XInternAtom (display, "XdndActionList", False);
  69. XdndActionCopy = XInternAtom (display, "XdndActionCopy", False);
  70. XdndActionDescription = XInternAtom (display, "XdndActionDescription", False);
  71. allowedMimeTypes[0] = XInternAtom (display, "text/plain", False);
  72. allowedMimeTypes[1] = XInternAtom (display, "text/uri-list", False);
  73. allowedActions[0] = XInternAtom (display, "XdndActionMove", False);
  74. allowedActions[1] = XdndActionCopy;
  75. allowedActions[2] = XInternAtom (display, "XdndActionLink", False);
  76. allowedActions[3] = XInternAtom (display, "XdndActionAsk", False);
  77. allowedActions[4] = XInternAtom (display, "XdndActionPrivate", False);
  78. }
  79. }
  80. }
  81. //==============================================================================
  82. namespace Keys
  83. {
  84. enum MouseButtons
  85. {
  86. NoButton = 0,
  87. LeftButton = 1,
  88. MiddleButton = 2,
  89. RightButton = 3,
  90. WheelUp = 4,
  91. WheelDown = 5
  92. };
  93. static int AltMask = 0;
  94. static int NumLockMask = 0;
  95. static bool numLock = false;
  96. static bool capsLock = false;
  97. static char keyStates [32];
  98. static const int extendedKeyModifier = 0x10000000;
  99. }
  100. bool KeyPress::isKeyCurrentlyDown (const int keyCode)
  101. {
  102. int keysym;
  103. if (keyCode & Keys::extendedKeyModifier)
  104. {
  105. keysym = 0xff00 | (keyCode & 0xff);
  106. }
  107. else
  108. {
  109. keysym = keyCode;
  110. if (keysym == (XK_Tab & 0xff)
  111. || keysym == (XK_Return & 0xff)
  112. || keysym == (XK_Escape & 0xff)
  113. || keysym == (XK_BackSpace & 0xff))
  114. {
  115. keysym |= 0xff00;
  116. }
  117. }
  118. ScopedXLock xlock;
  119. const int keycode = XKeysymToKeycode (display, keysym);
  120. const int keybyte = keycode >> 3;
  121. const int keybit = (1 << (keycode & 7));
  122. return (Keys::keyStates [keybyte] & keybit) != 0;
  123. }
  124. //==============================================================================
  125. #if JUCE_USE_XSHM
  126. namespace XSHMHelpers
  127. {
  128. static int trappedErrorCode = 0;
  129. extern "C" int errorTrapHandler (Display*, XErrorEvent* err)
  130. {
  131. trappedErrorCode = err->error_code;
  132. return 0;
  133. }
  134. static bool isShmAvailable() noexcept
  135. {
  136. static bool isChecked = false;
  137. static bool isAvailable = false;
  138. if (! isChecked)
  139. {
  140. isChecked = true;
  141. int major, minor;
  142. Bool pixmaps;
  143. ScopedXLock xlock;
  144. if (XShmQueryVersion (display, &major, &minor, &pixmaps))
  145. {
  146. trappedErrorCode = 0;
  147. XErrorHandler oldHandler = XSetErrorHandler (errorTrapHandler);
  148. XShmSegmentInfo segmentInfo = { 0 };
  149. XImage* xImage = XShmCreateImage (display, DefaultVisual (display, DefaultScreen (display)),
  150. 24, ZPixmap, 0, &segmentInfo, 50, 50);
  151. if ((segmentInfo.shmid = shmget (IPC_PRIVATE,
  152. xImage->bytes_per_line * xImage->height,
  153. IPC_CREAT | 0777)) >= 0)
  154. {
  155. segmentInfo.shmaddr = (char*) shmat (segmentInfo.shmid, 0, 0);
  156. if (segmentInfo.shmaddr != (void*) -1)
  157. {
  158. segmentInfo.readOnly = False;
  159. xImage->data = segmentInfo.shmaddr;
  160. XSync (display, False);
  161. if (XShmAttach (display, &segmentInfo) != 0)
  162. {
  163. XSync (display, False);
  164. XShmDetach (display, &segmentInfo);
  165. isAvailable = true;
  166. }
  167. }
  168. XFlush (display);
  169. XDestroyImage (xImage);
  170. shmdt (segmentInfo.shmaddr);
  171. }
  172. shmctl (segmentInfo.shmid, IPC_RMID, 0);
  173. XSetErrorHandler (oldHandler);
  174. if (trappedErrorCode != 0)
  175. isAvailable = false;
  176. }
  177. }
  178. return isAvailable;
  179. }
  180. }
  181. #endif
  182. //==============================================================================
  183. #if JUCE_USE_XRENDER
  184. namespace XRender
  185. {
  186. typedef Status (*tXRenderQueryVersion) (Display*, int*, int*);
  187. typedef XRenderPictFormat* (*tXrenderFindStandardFormat) (Display*, int);
  188. typedef XRenderPictFormat* (*tXRenderFindFormat) (Display*, unsigned long, XRenderPictFormat*, int);
  189. typedef XRenderPictFormat* (*tXRenderFindVisualFormat) (Display*, Visual*);
  190. static tXRenderQueryVersion xRenderQueryVersion = 0;
  191. static tXrenderFindStandardFormat xRenderFindStandardFormat = 0;
  192. static tXRenderFindFormat xRenderFindFormat = 0;
  193. static tXRenderFindVisualFormat xRenderFindVisualFormat = 0;
  194. static bool isAvailable()
  195. {
  196. static bool hasLoaded = false;
  197. if (! hasLoaded)
  198. {
  199. ScopedXLock xlock;
  200. hasLoaded = true;
  201. void* h = dlopen ("libXrender.so", RTLD_GLOBAL | RTLD_NOW);
  202. if (h != 0)
  203. {
  204. xRenderQueryVersion = (tXRenderQueryVersion) dlsym (h, "XRenderQueryVersion");
  205. xRenderFindStandardFormat = (tXrenderFindStandardFormat) dlsym (h, "XrenderFindStandardFormat");
  206. xRenderFindFormat = (tXRenderFindFormat) dlsym (h, "XRenderFindFormat");
  207. xRenderFindVisualFormat = (tXRenderFindVisualFormat) dlsym (h, "XRenderFindVisualFormat");
  208. }
  209. if (xRenderQueryVersion != 0
  210. && xRenderFindStandardFormat != 0
  211. && xRenderFindFormat != 0
  212. && xRenderFindVisualFormat != 0)
  213. {
  214. int major, minor;
  215. if (xRenderQueryVersion (display, &major, &minor))
  216. return true;
  217. }
  218. xRenderQueryVersion = 0;
  219. }
  220. return xRenderQueryVersion != 0;
  221. }
  222. static XRenderPictFormat* findPictureFormat()
  223. {
  224. ScopedXLock xlock;
  225. XRenderPictFormat* pictFormat = nullptr;
  226. if (isAvailable())
  227. {
  228. pictFormat = xRenderFindStandardFormat (display, PictStandardARGB32);
  229. if (pictFormat == 0)
  230. {
  231. XRenderPictFormat desiredFormat;
  232. desiredFormat.type = PictTypeDirect;
  233. desiredFormat.depth = 32;
  234. desiredFormat.direct.alphaMask = 0xff;
  235. desiredFormat.direct.redMask = 0xff;
  236. desiredFormat.direct.greenMask = 0xff;
  237. desiredFormat.direct.blueMask = 0xff;
  238. desiredFormat.direct.alpha = 24;
  239. desiredFormat.direct.red = 16;
  240. desiredFormat.direct.green = 8;
  241. desiredFormat.direct.blue = 0;
  242. pictFormat = xRenderFindFormat (display,
  243. PictFormatType | PictFormatDepth
  244. | PictFormatRedMask | PictFormatRed
  245. | PictFormatGreenMask | PictFormatGreen
  246. | PictFormatBlueMask | PictFormatBlue
  247. | PictFormatAlphaMask | PictFormatAlpha,
  248. &desiredFormat,
  249. 0);
  250. }
  251. }
  252. return pictFormat;
  253. }
  254. }
  255. #endif
  256. //==============================================================================
  257. namespace Visuals
  258. {
  259. static Visual* findVisualWithDepth (const int desiredDepth) noexcept
  260. {
  261. ScopedXLock xlock;
  262. Visual* visual = nullptr;
  263. int numVisuals = 0;
  264. long desiredMask = VisualNoMask;
  265. XVisualInfo desiredVisual;
  266. desiredVisual.screen = DefaultScreen (display);
  267. desiredVisual.depth = desiredDepth;
  268. desiredMask = VisualScreenMask | VisualDepthMask;
  269. if (desiredDepth == 32)
  270. {
  271. desiredVisual.c_class = TrueColor;
  272. desiredVisual.red_mask = 0x00FF0000;
  273. desiredVisual.green_mask = 0x0000FF00;
  274. desiredVisual.blue_mask = 0x000000FF;
  275. desiredVisual.bits_per_rgb = 8;
  276. desiredMask |= VisualClassMask;
  277. desiredMask |= VisualRedMaskMask;
  278. desiredMask |= VisualGreenMaskMask;
  279. desiredMask |= VisualBlueMaskMask;
  280. desiredMask |= VisualBitsPerRGBMask;
  281. }
  282. XVisualInfo* xvinfos = XGetVisualInfo (display,
  283. desiredMask,
  284. &desiredVisual,
  285. &numVisuals);
  286. if (xvinfos != 0)
  287. {
  288. for (int i = 0; i < numVisuals; i++)
  289. {
  290. if (xvinfos[i].depth == desiredDepth)
  291. {
  292. visual = xvinfos[i].visual;
  293. break;
  294. }
  295. }
  296. XFree (xvinfos);
  297. }
  298. return visual;
  299. }
  300. static Visual* findVisualFormat (const int desiredDepth, int& matchedDepth) noexcept
  301. {
  302. Visual* visual = nullptr;
  303. if (desiredDepth == 32)
  304. {
  305. #if JUCE_USE_XSHM
  306. if (XSHMHelpers::isShmAvailable())
  307. {
  308. #if JUCE_USE_XRENDER
  309. if (XRender::isAvailable())
  310. {
  311. XRenderPictFormat* pictFormat = XRender::findPictureFormat();
  312. if (pictFormat != 0)
  313. {
  314. int numVisuals = 0;
  315. XVisualInfo desiredVisual;
  316. desiredVisual.screen = DefaultScreen (display);
  317. desiredVisual.depth = 32;
  318. desiredVisual.bits_per_rgb = 8;
  319. XVisualInfo* xvinfos = XGetVisualInfo (display,
  320. VisualScreenMask | VisualDepthMask | VisualBitsPerRGBMask,
  321. &desiredVisual, &numVisuals);
  322. if (xvinfos != 0)
  323. {
  324. for (int i = 0; i < numVisuals; ++i)
  325. {
  326. XRenderPictFormat* pictVisualFormat = XRender::xRenderFindVisualFormat (display, xvinfos[i].visual);
  327. if (pictVisualFormat != 0
  328. && pictVisualFormat->type == PictTypeDirect
  329. && pictVisualFormat->direct.alphaMask)
  330. {
  331. visual = xvinfos[i].visual;
  332. matchedDepth = 32;
  333. break;
  334. }
  335. }
  336. XFree (xvinfos);
  337. }
  338. }
  339. }
  340. #endif
  341. if (visual == 0)
  342. {
  343. visual = findVisualWithDepth (32);
  344. if (visual != 0)
  345. matchedDepth = 32;
  346. }
  347. }
  348. #endif
  349. }
  350. if (visual == 0 && desiredDepth >= 24)
  351. {
  352. visual = findVisualWithDepth (24);
  353. if (visual != 0)
  354. matchedDepth = 24;
  355. }
  356. if (visual == 0 && desiredDepth >= 16)
  357. {
  358. visual = findVisualWithDepth (16);
  359. if (visual != 0)
  360. matchedDepth = 16;
  361. }
  362. return visual;
  363. }
  364. }
  365. //==============================================================================
  366. class XBitmapImage : public Image::SharedImage
  367. {
  368. public:
  369. //==============================================================================
  370. XBitmapImage (const Image::PixelFormat format_, const int w, const int h,
  371. const bool clearImage, const int imageDepth_, Visual* visual)
  372. : Image::SharedImage (format_, w, h),
  373. imageDepth (imageDepth_),
  374. gc (None)
  375. {
  376. jassert (format_ == Image::RGB || format_ == Image::ARGB);
  377. pixelStride = (format_ == Image::RGB) ? 3 : 4;
  378. lineStride = ((w * pixelStride + 3) & ~3);
  379. ScopedXLock xlock;
  380. #if JUCE_USE_XSHM
  381. usingXShm = false;
  382. if ((imageDepth > 16) && XSHMHelpers::isShmAvailable())
  383. {
  384. zerostruct (segmentInfo);
  385. segmentInfo.shmid = -1;
  386. segmentInfo.shmaddr = (char *) -1;
  387. segmentInfo.readOnly = False;
  388. xImage = XShmCreateImage (display, visual, imageDepth, ZPixmap, 0, &segmentInfo, w, h);
  389. if (xImage != 0)
  390. {
  391. if ((segmentInfo.shmid = shmget (IPC_PRIVATE,
  392. xImage->bytes_per_line * xImage->height,
  393. IPC_CREAT | 0777)) >= 0)
  394. {
  395. if (segmentInfo.shmid != -1)
  396. {
  397. segmentInfo.shmaddr = (char*) shmat (segmentInfo.shmid, 0, 0);
  398. if (segmentInfo.shmaddr != (void*) -1)
  399. {
  400. segmentInfo.readOnly = False;
  401. xImage->data = segmentInfo.shmaddr;
  402. imageData = (uint8*) segmentInfo.shmaddr;
  403. if (XShmAttach (display, &segmentInfo) != 0)
  404. usingXShm = true;
  405. else
  406. jassertfalse;
  407. }
  408. else
  409. {
  410. shmctl (segmentInfo.shmid, IPC_RMID, 0);
  411. }
  412. }
  413. }
  414. }
  415. }
  416. if (! usingXShm)
  417. #endif
  418. {
  419. imageDataAllocated.allocate (lineStride * h, format_ == Image::ARGB && clearImage);
  420. imageData = imageDataAllocated;
  421. xImage = (XImage*) ::calloc (1, sizeof (XImage));
  422. xImage->width = w;
  423. xImage->height = h;
  424. xImage->xoffset = 0;
  425. xImage->format = ZPixmap;
  426. xImage->data = (char*) imageData;
  427. xImage->byte_order = ImageByteOrder (display);
  428. xImage->bitmap_unit = BitmapUnit (display);
  429. xImage->bitmap_bit_order = BitmapBitOrder (display);
  430. xImage->bitmap_pad = 32;
  431. xImage->depth = pixelStride * 8;
  432. xImage->bytes_per_line = lineStride;
  433. xImage->bits_per_pixel = pixelStride * 8;
  434. xImage->red_mask = 0x00FF0000;
  435. xImage->green_mask = 0x0000FF00;
  436. xImage->blue_mask = 0x000000FF;
  437. if (imageDepth == 16)
  438. {
  439. const int pixelStride = 2;
  440. const int lineStride = ((w * pixelStride + 3) & ~3);
  441. imageData16Bit.malloc (lineStride * h);
  442. xImage->data = imageData16Bit;
  443. xImage->bitmap_pad = 16;
  444. xImage->depth = pixelStride * 8;
  445. xImage->bytes_per_line = lineStride;
  446. xImage->bits_per_pixel = pixelStride * 8;
  447. xImage->red_mask = visual->red_mask;
  448. xImage->green_mask = visual->green_mask;
  449. xImage->blue_mask = visual->blue_mask;
  450. }
  451. if (! XInitImage (xImage))
  452. jassertfalse;
  453. }
  454. }
  455. ~XBitmapImage()
  456. {
  457. ScopedXLock xlock;
  458. if (gc != None)
  459. XFreeGC (display, gc);
  460. #if JUCE_USE_XSHM
  461. if (usingXShm)
  462. {
  463. XShmDetach (display, &segmentInfo);
  464. XFlush (display);
  465. XDestroyImage (xImage);
  466. shmdt (segmentInfo.shmaddr);
  467. shmctl (segmentInfo.shmid, IPC_RMID, 0);
  468. }
  469. else
  470. #endif
  471. {
  472. xImage->data = nullptr;
  473. XDestroyImage (xImage);
  474. }
  475. }
  476. Image::ImageType getType() const { return Image::NativeImage; }
  477. LowLevelGraphicsContext* createLowLevelContext()
  478. {
  479. return new LowLevelGraphicsSoftwareRenderer (Image (this));
  480. }
  481. void initialiseBitmapData (Image::BitmapData& bitmap, int x, int y, Image::BitmapData::ReadWriteMode /*mode*/)
  482. {
  483. bitmap.data = imageData + x * pixelStride + y * lineStride;
  484. bitmap.pixelFormat = format;
  485. bitmap.lineStride = lineStride;
  486. bitmap.pixelStride = pixelStride;
  487. }
  488. SharedImage* clone()
  489. {
  490. jassertfalse;
  491. return nullptr;
  492. }
  493. void blitToWindow (Window window, int dx, int dy, int dw, int dh, int sx, int sy)
  494. {
  495. ScopedXLock xlock;
  496. if (gc == None)
  497. {
  498. XGCValues gcvalues;
  499. gcvalues.foreground = None;
  500. gcvalues.background = None;
  501. gcvalues.function = GXcopy;
  502. gcvalues.plane_mask = AllPlanes;
  503. gcvalues.clip_mask = None;
  504. gcvalues.graphics_exposures = False;
  505. gc = XCreateGC (display, window,
  506. GCBackground | GCForeground | GCFunction | GCPlaneMask | GCClipMask | GCGraphicsExposures,
  507. &gcvalues);
  508. }
  509. if (imageDepth == 16)
  510. {
  511. const uint32 rMask = xImage->red_mask;
  512. const uint32 rShiftL = jmax (0, getShiftNeeded (rMask));
  513. const uint32 rShiftR = jmax (0, -getShiftNeeded (rMask));
  514. const uint32 gMask = xImage->green_mask;
  515. const uint32 gShiftL = jmax (0, getShiftNeeded (gMask));
  516. const uint32 gShiftR = jmax (0, -getShiftNeeded (gMask));
  517. const uint32 bMask = xImage->blue_mask;
  518. const uint32 bShiftL = jmax (0, getShiftNeeded (bMask));
  519. const uint32 bShiftR = jmax (0, -getShiftNeeded (bMask));
  520. const Image::BitmapData srcData (Image (this), Image::BitmapData::readOnly);
  521. for (int y = sy; y < sy + dh; ++y)
  522. {
  523. const uint8* p = srcData.getPixelPointer (sx, y);
  524. for (int x = sx; x < sx + dw; ++x)
  525. {
  526. const PixelRGB* const pixel = (const PixelRGB*) p;
  527. p += srcData.pixelStride;
  528. XPutPixel (xImage, x, y,
  529. (((((uint32) pixel->getRed()) << rShiftL) >> rShiftR) & rMask)
  530. | (((((uint32) pixel->getGreen()) << gShiftL) >> gShiftR) & gMask)
  531. | (((((uint32) pixel->getBlue()) << bShiftL) >> bShiftR) & bMask));
  532. }
  533. }
  534. }
  535. // blit results to screen.
  536. #if JUCE_USE_XSHM
  537. if (usingXShm)
  538. XShmPutImage (display, (::Drawable) window, gc, xImage, sx, sy, dx, dy, dw, dh, True);
  539. else
  540. #endif
  541. XPutImage (display, (::Drawable) window, gc, xImage, sx, sy, dx, dy, dw, dh);
  542. }
  543. //==============================================================================
  544. private:
  545. XImage* xImage;
  546. const int imageDepth;
  547. HeapBlock <uint8> imageDataAllocated;
  548. HeapBlock <char> imageData16Bit;
  549. int pixelStride, lineStride;
  550. uint8* imageData;
  551. GC gc;
  552. #if JUCE_USE_XSHM
  553. XShmSegmentInfo segmentInfo;
  554. bool usingXShm;
  555. #endif
  556. static int getShiftNeeded (const uint32 mask) noexcept
  557. {
  558. for (int i = 32; --i >= 0;)
  559. if (((mask >> i) & 1) != 0)
  560. return i - 7;
  561. jassertfalse;
  562. return 0;
  563. }
  564. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (XBitmapImage);
  565. };
  566. namespace PixmapHelpers
  567. {
  568. Pixmap createColourPixmapFromImage (Display* display, const Image& image)
  569. {
  570. ScopedXLock xlock;
  571. const int width = image.getWidth();
  572. const int height = image.getHeight();
  573. HeapBlock <uint32> colour (width * height);
  574. int index = 0;
  575. for (int y = 0; y < height; ++y)
  576. for (int x = 0; x < width; ++x)
  577. colour[index++] = image.getPixelAt (x, y).getARGB();
  578. XImage* ximage = XCreateImage (display, CopyFromParent, 24, ZPixmap,
  579. 0, reinterpret_cast<char*> (colour.getData()),
  580. width, height, 32, 0);
  581. Pixmap pixmap = XCreatePixmap (display, DefaultRootWindow (display),
  582. width, height, 24);
  583. GC gc = XCreateGC (display, pixmap, 0, 0);
  584. XPutImage (display, pixmap, gc, ximage, 0, 0, 0, 0, width, height);
  585. XFreeGC (display, gc);
  586. return pixmap;
  587. }
  588. Pixmap createMaskPixmapFromImage (Display* display, const Image& image)
  589. {
  590. ScopedXLock xlock;
  591. const int width = image.getWidth();
  592. const int height = image.getHeight();
  593. const int stride = (width + 7) >> 3;
  594. HeapBlock <char> mask;
  595. mask.calloc (stride * height);
  596. const bool msbfirst = (BitmapBitOrder (display) == MSBFirst);
  597. for (int y = 0; y < height; ++y)
  598. {
  599. for (int x = 0; x < width; ++x)
  600. {
  601. const char bit = (char) (1 << (msbfirst ? (7 - (x & 7)) : (x & 7)));
  602. const int offset = y * stride + (x >> 3);
  603. if (image.getPixelAt (x, y).getAlpha() >= 128)
  604. mask[offset] |= bit;
  605. }
  606. }
  607. return XCreatePixmapFromBitmapData (display, DefaultRootWindow (display),
  608. mask.getData(), width, height, 1, 0, 1);
  609. }
  610. }
  611. //==============================================================================
  612. class LinuxComponentPeer : public ComponentPeer
  613. {
  614. public:
  615. //==============================================================================
  616. LinuxComponentPeer (Component* const component, const int windowStyleFlags, Window parentToAddTo)
  617. : ComponentPeer (component, windowStyleFlags),
  618. windowH (0), parentWindow (0),
  619. wx (0), wy (0), ww (0), wh (0),
  620. fullScreen (false), mapped (false),
  621. visual (0), depth (0)
  622. {
  623. // it's dangerous to create a window on a thread other than the message thread..
  624. jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  625. repainter = new LinuxRepaintManager (this);
  626. createWindow (parentToAddTo);
  627. setTitle (component->getName());
  628. }
  629. ~LinuxComponentPeer()
  630. {
  631. // it's dangerous to delete a window on a thread other than the message thread..
  632. jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  633. deleteIconPixmaps();
  634. destroyWindow();
  635. windowH = 0;
  636. }
  637. //==============================================================================
  638. void* getNativeHandle() const
  639. {
  640. return (void*) windowH;
  641. }
  642. static LinuxComponentPeer* getPeerFor (Window windowHandle) noexcept
  643. {
  644. XPointer peer = 0;
  645. ScopedXLock xlock;
  646. if (! XFindContext (display, (XID) windowHandle, windowHandleXContext, &peer))
  647. {
  648. if (peer != nullptr && ! ComponentPeer::isValidPeer ((LinuxComponentPeer*) peer))
  649. peer = 0;
  650. }
  651. return (LinuxComponentPeer*) peer;
  652. }
  653. void setVisible (bool shouldBeVisible)
  654. {
  655. ScopedXLock xlock;
  656. if (shouldBeVisible)
  657. XMapWindow (display, windowH);
  658. else
  659. XUnmapWindow (display, windowH);
  660. }
  661. void setTitle (const String& title)
  662. {
  663. XTextProperty nameProperty;
  664. char* strings[] = { const_cast <char*> (title.toUTF8().getAddress()) };
  665. ScopedXLock xlock;
  666. if (XStringListToTextProperty (strings, 1, &nameProperty))
  667. {
  668. XSetWMName (display, windowH, &nameProperty);
  669. XSetWMIconName (display, windowH, &nameProperty);
  670. XFree (nameProperty.value);
  671. }
  672. }
  673. void setBounds (int x, int y, int w, int h, bool isNowFullScreen)
  674. {
  675. fullScreen = isNowFullScreen;
  676. if (windowH != 0)
  677. {
  678. WeakReference<Component> deletionChecker (component);
  679. wx = x;
  680. wy = y;
  681. ww = jmax (1, w);
  682. wh = jmax (1, h);
  683. ScopedXLock xlock;
  684. // Make sure the Window manager does what we want
  685. XSizeHints* hints = XAllocSizeHints();
  686. hints->flags = USSize | USPosition;
  687. hints->width = ww;
  688. hints->height = wh;
  689. hints->x = wx;
  690. hints->y = wy;
  691. if ((getStyleFlags() & (windowHasTitleBar | windowIsResizable)) == windowHasTitleBar)
  692. {
  693. hints->min_width = hints->max_width = hints->width;
  694. hints->min_height = hints->max_height = hints->height;
  695. hints->flags |= PMinSize | PMaxSize;
  696. }
  697. XSetWMNormalHints (display, windowH, hints);
  698. XFree (hints);
  699. XMoveResizeWindow (display, windowH,
  700. wx - windowBorder.getLeft(),
  701. wy - windowBorder.getTop(), ww, wh);
  702. if (deletionChecker != 0)
  703. {
  704. updateBorderSize();
  705. handleMovedOrResized();
  706. }
  707. }
  708. }
  709. void setPosition (int x, int y) { setBounds (x, y, ww, wh, false); }
  710. void setSize (int w, int h) { setBounds (wx, wy, w, h, false); }
  711. const Rectangle<int> getBounds() const { return Rectangle<int> (wx, wy, ww, wh); }
  712. const Point<int> getScreenPosition() const { return Point<int> (wx, wy); }
  713. const Point<int> localToGlobal (const Point<int>& relativePosition)
  714. {
  715. return relativePosition + getScreenPosition();
  716. }
  717. const Point<int> globalToLocal (const Point<int>& screenPosition)
  718. {
  719. return screenPosition - getScreenPosition();
  720. }
  721. void setAlpha (float newAlpha)
  722. {
  723. //xxx todo!
  724. }
  725. void setMinimised (bool shouldBeMinimised)
  726. {
  727. if (shouldBeMinimised)
  728. {
  729. Window root = RootWindow (display, DefaultScreen (display));
  730. XClientMessageEvent clientMsg;
  731. clientMsg.display = display;
  732. clientMsg.window = windowH;
  733. clientMsg.type = ClientMessage;
  734. clientMsg.format = 32;
  735. clientMsg.message_type = Atoms::ChangeState;
  736. clientMsg.data.l[0] = IconicState;
  737. ScopedXLock xlock;
  738. XSendEvent (display, root, false, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent*) &clientMsg);
  739. }
  740. else
  741. {
  742. setVisible (true);
  743. }
  744. }
  745. bool isMinimised() const
  746. {
  747. bool minimised = false;
  748. unsigned char* stateProp;
  749. unsigned long nitems, bytesLeft;
  750. Atom actualType;
  751. int actualFormat;
  752. ScopedXLock xlock;
  753. if (XGetWindowProperty (display, windowH, Atoms::State, 0, 64, False,
  754. Atoms::State, &actualType, &actualFormat, &nitems, &bytesLeft,
  755. &stateProp) == Success
  756. && actualType == Atoms::State
  757. && actualFormat == 32
  758. && nitems > 0)
  759. {
  760. if (((unsigned long*) stateProp)[0] == IconicState)
  761. minimised = true;
  762. XFree (stateProp);
  763. }
  764. return minimised;
  765. }
  766. void setFullScreen (const bool shouldBeFullScreen)
  767. {
  768. Rectangle<int> r (lastNonFullscreenBounds); // (get a copy of this before de-minimising)
  769. setMinimised (false);
  770. if (fullScreen != shouldBeFullScreen)
  771. {
  772. if (shouldBeFullScreen)
  773. r = Desktop::getInstance().getMainMonitorArea();
  774. if (! r.isEmpty())
  775. setBounds (r.getX(), r.getY(), r.getWidth(), r.getHeight(), shouldBeFullScreen);
  776. getComponent()->repaint();
  777. }
  778. }
  779. bool isFullScreen() const
  780. {
  781. return fullScreen;
  782. }
  783. bool isChildWindowOf (Window possibleParent) const
  784. {
  785. Window* windowList = nullptr;
  786. uint32 windowListSize = 0;
  787. Window parent, root;
  788. ScopedXLock xlock;
  789. if (XQueryTree (display, windowH, &root, &parent, &windowList, &windowListSize) != 0)
  790. {
  791. if (windowList != 0)
  792. XFree (windowList);
  793. return parent == possibleParent;
  794. }
  795. return false;
  796. }
  797. bool isFrontWindow() const
  798. {
  799. Window* windowList = nullptr;
  800. uint32 windowListSize = 0;
  801. bool result = false;
  802. ScopedXLock xlock;
  803. Window parent, root = RootWindow (display, DefaultScreen (display));
  804. if (XQueryTree (display, root, &root, &parent, &windowList, &windowListSize) != 0)
  805. {
  806. for (int i = windowListSize; --i >= 0;)
  807. {
  808. LinuxComponentPeer* const peer = LinuxComponentPeer::getPeerFor (windowList[i]);
  809. if (peer != 0)
  810. {
  811. result = (peer == this);
  812. break;
  813. }
  814. }
  815. }
  816. if (windowList != 0)
  817. XFree (windowList);
  818. return result;
  819. }
  820. bool contains (const Point<int>& position, bool trueIfInAChildWindow) const
  821. {
  822. if (! (isPositiveAndBelow (position.getX(), ww) && isPositiveAndBelow (position.getY(), wh)))
  823. return false;
  824. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  825. {
  826. Component* const c = Desktop::getInstance().getComponent (i);
  827. if (c == getComponent())
  828. break;
  829. if (c->contains (position + Point<int> (wx, wy) - c->getScreenPosition()))
  830. return false;
  831. }
  832. if (trueIfInAChildWindow)
  833. return true;
  834. ::Window root, child;
  835. unsigned int bw, depth;
  836. int wx, wy, w, h;
  837. ScopedXLock xlock;
  838. if (! XGetGeometry (display, (::Drawable) windowH, &root,
  839. &wx, &wy, (unsigned int*) &w, (unsigned int*) &h,
  840. &bw, &depth))
  841. {
  842. return false;
  843. }
  844. if (! XTranslateCoordinates (display, windowH, windowH, position.getX(), position.getY(), &wx, &wy, &child))
  845. return false;
  846. return child == None;
  847. }
  848. const BorderSize<int> getFrameSize() const
  849. {
  850. return BorderSize<int>();
  851. }
  852. bool setAlwaysOnTop (bool alwaysOnTop)
  853. {
  854. return false;
  855. }
  856. void toFront (bool makeActive)
  857. {
  858. if (makeActive)
  859. {
  860. setVisible (true);
  861. grabFocus();
  862. }
  863. XEvent ev;
  864. ev.xclient.type = ClientMessage;
  865. ev.xclient.serial = 0;
  866. ev.xclient.send_event = True;
  867. ev.xclient.message_type = Atoms::ActiveWin;
  868. ev.xclient.window = windowH;
  869. ev.xclient.format = 32;
  870. ev.xclient.data.l[0] = 2;
  871. ev.xclient.data.l[1] = CurrentTime;
  872. ev.xclient.data.l[2] = 0;
  873. ev.xclient.data.l[3] = 0;
  874. ev.xclient.data.l[4] = 0;
  875. {
  876. ScopedXLock xlock;
  877. XSendEvent (display, RootWindow (display, DefaultScreen (display)),
  878. False, SubstructureRedirectMask | SubstructureNotifyMask, &ev);
  879. XWindowAttributes attr;
  880. XGetWindowAttributes (display, windowH, &attr);
  881. if (component->isAlwaysOnTop())
  882. XRaiseWindow (display, windowH);
  883. XSync (display, False);
  884. }
  885. handleBroughtToFront();
  886. }
  887. void toBehind (ComponentPeer* other)
  888. {
  889. LinuxComponentPeer* const otherPeer = dynamic_cast <LinuxComponentPeer*> (other);
  890. jassert (otherPeer != nullptr); // wrong type of window?
  891. if (otherPeer != nullptr)
  892. {
  893. setMinimised (false);
  894. Window newStack[] = { otherPeer->windowH, windowH };
  895. ScopedXLock xlock;
  896. XRestackWindows (display, newStack, 2);
  897. }
  898. }
  899. bool isFocused() const
  900. {
  901. int revert = 0;
  902. Window focusedWindow = 0;
  903. ScopedXLock xlock;
  904. XGetInputFocus (display, &focusedWindow, &revert);
  905. return focusedWindow == windowH;
  906. }
  907. void grabFocus()
  908. {
  909. XWindowAttributes atts;
  910. ScopedXLock xlock;
  911. if (windowH != 0
  912. && XGetWindowAttributes (display, windowH, &atts)
  913. && atts.map_state == IsViewable
  914. && ! isFocused())
  915. {
  916. XSetInputFocus (display, windowH, RevertToParent, CurrentTime);
  917. isActiveApplication = true;
  918. }
  919. }
  920. void textInputRequired (const Point<int>&)
  921. {
  922. }
  923. void repaint (const Rectangle<int>& area)
  924. {
  925. repainter->repaint (area.getIntersection (getComponent()->getLocalBounds()));
  926. }
  927. void performAnyPendingRepaintsNow()
  928. {
  929. repainter->performAnyPendingRepaintsNow();
  930. }
  931. void setIcon (const Image& newIcon)
  932. {
  933. const int dataSize = newIcon.getWidth() * newIcon.getHeight() + 2;
  934. HeapBlock <unsigned long> data (dataSize);
  935. int index = 0;
  936. data[index++] = newIcon.getWidth();
  937. data[index++] = newIcon.getHeight();
  938. for (int y = 0; y < newIcon.getHeight(); ++y)
  939. for (int x = 0; x < newIcon.getWidth(); ++x)
  940. data[index++] = newIcon.getPixelAt (x, y).getARGB();
  941. ScopedXLock xlock;
  942. XChangeProperty (display, windowH,
  943. XInternAtom (display, "_NET_WM_ICON", False),
  944. XA_CARDINAL, 32, PropModeReplace,
  945. reinterpret_cast<unsigned char*> (data.getData()), dataSize);
  946. deleteIconPixmaps();
  947. XWMHints* wmHints = XGetWMHints (display, windowH);
  948. if (wmHints == 0)
  949. wmHints = XAllocWMHints();
  950. wmHints->flags |= IconPixmapHint | IconMaskHint;
  951. wmHints->icon_pixmap = PixmapHelpers::createColourPixmapFromImage (display, newIcon);
  952. wmHints->icon_mask = PixmapHelpers::createMaskPixmapFromImage (display, newIcon);
  953. XSetWMHints (display, windowH, wmHints);
  954. XFree (wmHints);
  955. XSync (display, False);
  956. }
  957. void deleteIconPixmaps()
  958. {
  959. ScopedXLock xlock;
  960. XWMHints* wmHints = XGetWMHints (display, windowH);
  961. if (wmHints != 0)
  962. {
  963. if ((wmHints->flags & IconPixmapHint) != 0)
  964. {
  965. wmHints->flags &= ~IconPixmapHint;
  966. XFreePixmap (display, wmHints->icon_pixmap);
  967. }
  968. if ((wmHints->flags & IconMaskHint) != 0)
  969. {
  970. wmHints->flags &= ~IconMaskHint;
  971. XFreePixmap (display, wmHints->icon_mask);
  972. }
  973. XSetWMHints (display, windowH, wmHints);
  974. XFree (wmHints);
  975. }
  976. }
  977. //==============================================================================
  978. void handleWindowMessage (XEvent* event)
  979. {
  980. switch (event->xany.type)
  981. {
  982. case 2: /* KeyPress */ handleKeyPressEvent ((XKeyEvent*) &event->xkey); break;
  983. case KeyRelease: handleKeyReleaseEvent ((const XKeyEvent*) &event->xkey); break;
  984. case ButtonPress: handleButtonPressEvent ((const XButtonPressedEvent*) &event->xbutton); break;
  985. case ButtonRelease: handleButtonReleaseEvent ((const XButtonReleasedEvent*) &event->xbutton); break;
  986. case MotionNotify: handleMotionNotifyEvent ((const XPointerMovedEvent*) &event->xmotion); break;
  987. case EnterNotify: handleEnterNotifyEvent ((const XEnterWindowEvent*) &event->xcrossing); break;
  988. case LeaveNotify: handleLeaveNotifyEvent ((const XLeaveWindowEvent*) &event->xcrossing); break;
  989. case FocusIn: handleFocusInEvent(); break;
  990. case FocusOut: handleFocusOutEvent(); break;
  991. case Expose: handleExposeEvent ((XExposeEvent*) &event->xexpose); break;
  992. case MappingNotify: handleMappingNotify ((XMappingEvent*) &event->xmapping); break;
  993. case ClientMessage: handleClientMessageEvent ((XClientMessageEvent*) &event->xclient, event); break;
  994. case SelectionNotify: handleDragAndDropSelection (event); break;
  995. case ConfigureNotify: handleConfigureNotifyEvent ((XConfigureEvent*) &event->xconfigure); break;
  996. case ReparentNotify: handleReparentNotifyEvent(); break;
  997. case GravityNotify: handleGravityNotify(); break;
  998. case CirculateNotify:
  999. case CreateNotify:
  1000. case DestroyNotify:
  1001. // Think we can ignore these
  1002. break;
  1003. case MapNotify:
  1004. mapped = true;
  1005. handleBroughtToFront();
  1006. break;
  1007. case UnmapNotify:
  1008. mapped = false;
  1009. break;
  1010. case SelectionClear:
  1011. case SelectionRequest:
  1012. break;
  1013. default:
  1014. #if JUCE_USE_XSHM
  1015. {
  1016. ScopedXLock xlock;
  1017. if (event->xany.type == XShmGetEventBase (display))
  1018. repainter->notifyPaintCompleted();
  1019. }
  1020. #endif
  1021. break;
  1022. }
  1023. }
  1024. void handleKeyPressEvent (XKeyEvent* const keyEvent)
  1025. {
  1026. char utf8 [64] = { 0 };
  1027. juce_wchar unicodeChar = 0;
  1028. int keyCode = 0;
  1029. bool keyDownChange = false;
  1030. KeySym sym;
  1031. {
  1032. ScopedXLock xlock;
  1033. updateKeyStates (keyEvent->keycode, true);
  1034. const char* oldLocale = ::setlocale (LC_ALL, 0);
  1035. ::setlocale (LC_ALL, "");
  1036. XLookupString (keyEvent, utf8, sizeof (utf8), &sym, 0);
  1037. ::setlocale (LC_ALL, oldLocale);
  1038. unicodeChar = String::fromUTF8 (utf8, sizeof (utf8) - 1) [0];
  1039. keyCode = (int) unicodeChar;
  1040. if (keyCode < 0x20)
  1041. keyCode = XKeycodeToKeysym (display, keyEvent->keycode, currentModifiers.isShiftDown() ? 1 : 0);
  1042. keyDownChange = (sym != NoSymbol) && ! updateKeyModifiersFromSym (sym, true);
  1043. }
  1044. const ModifierKeys oldMods (currentModifiers);
  1045. bool keyPressed = false;
  1046. if ((sym & 0xff00) == 0xff00)
  1047. {
  1048. switch (sym) // Translate keypad
  1049. {
  1050. case XK_KP_Divide: keyCode = XK_slash; break;
  1051. case XK_KP_Multiply: keyCode = XK_asterisk; break;
  1052. case XK_KP_Subtract: keyCode = XK_hyphen; break;
  1053. case XK_KP_Add: keyCode = XK_plus; break;
  1054. case XK_KP_Enter: keyCode = XK_Return; break;
  1055. case XK_KP_Decimal: keyCode = Keys::numLock ? XK_period : XK_Delete; break;
  1056. case XK_KP_0: keyCode = Keys::numLock ? XK_0 : XK_Insert; break;
  1057. case XK_KP_1: keyCode = Keys::numLock ? XK_1 : XK_End; break;
  1058. case XK_KP_2: keyCode = Keys::numLock ? XK_2 : XK_Down; break;
  1059. case XK_KP_3: keyCode = Keys::numLock ? XK_3 : XK_Page_Down; break;
  1060. case XK_KP_4: keyCode = Keys::numLock ? XK_4 : XK_Left; break;
  1061. case XK_KP_5: keyCode = XK_5; break;
  1062. case XK_KP_6: keyCode = Keys::numLock ? XK_6 : XK_Right; break;
  1063. case XK_KP_7: keyCode = Keys::numLock ? XK_7 : XK_Home; break;
  1064. case XK_KP_8: keyCode = Keys::numLock ? XK_8 : XK_Up; break;
  1065. case XK_KP_9: keyCode = Keys::numLock ? XK_9 : XK_Page_Up; break;
  1066. default: break;
  1067. }
  1068. switch (sym)
  1069. {
  1070. case XK_Left:
  1071. case XK_Right:
  1072. case XK_Up:
  1073. case XK_Down:
  1074. case XK_Page_Up:
  1075. case XK_Page_Down:
  1076. case XK_End:
  1077. case XK_Home:
  1078. case XK_Delete:
  1079. case XK_Insert:
  1080. keyPressed = true;
  1081. keyCode = (sym & 0xff) | Keys::extendedKeyModifier;
  1082. break;
  1083. case XK_Tab:
  1084. case XK_Return:
  1085. case XK_Escape:
  1086. case XK_BackSpace:
  1087. keyPressed = true;
  1088. keyCode &= 0xff;
  1089. break;
  1090. default:
  1091. if (sym >= XK_F1 && sym <= XK_F16)
  1092. {
  1093. keyPressed = true;
  1094. keyCode = (sym & 0xff) | Keys::extendedKeyModifier;
  1095. }
  1096. break;
  1097. }
  1098. }
  1099. if (utf8[0] != 0 || ((sym & 0xff00) == 0 && sym >= 8))
  1100. keyPressed = true;
  1101. if (oldMods != currentModifiers)
  1102. handleModifierKeysChange();
  1103. if (keyDownChange)
  1104. handleKeyUpOrDown (true);
  1105. if (keyPressed)
  1106. handleKeyPress (keyCode, unicodeChar);
  1107. }
  1108. void handleKeyReleaseEvent (const XKeyEvent* const keyEvent)
  1109. {
  1110. updateKeyStates (keyEvent->keycode, false);
  1111. KeySym sym;
  1112. {
  1113. ScopedXLock xlock;
  1114. sym = XKeycodeToKeysym (display, keyEvent->keycode, 0);
  1115. }
  1116. const ModifierKeys oldMods (currentModifiers);
  1117. const bool keyDownChange = (sym != NoSymbol) && ! updateKeyModifiersFromSym (sym, false);
  1118. if (oldMods != currentModifiers)
  1119. handleModifierKeysChange();
  1120. if (keyDownChange)
  1121. handleKeyUpOrDown (false);
  1122. }
  1123. void handleButtonPressEvent (const XButtonPressedEvent* const buttonPressEvent)
  1124. {
  1125. updateKeyModifiers (buttonPressEvent->state);
  1126. bool buttonMsg = false;
  1127. const int map = pointerMap [buttonPressEvent->button - Button1];
  1128. if (map == Keys::WheelUp || map == Keys::WheelDown)
  1129. {
  1130. handleMouseWheel (0, Point<int> (buttonPressEvent->x, buttonPressEvent->y),
  1131. getEventTime (buttonPressEvent->time), 0, map == Keys::WheelDown ? -84.0f : 84.0f);
  1132. }
  1133. if (map == Keys::LeftButton)
  1134. {
  1135. currentModifiers = currentModifiers.withFlags (ModifierKeys::leftButtonModifier);
  1136. buttonMsg = true;
  1137. }
  1138. else if (map == Keys::RightButton)
  1139. {
  1140. currentModifiers = currentModifiers.withFlags (ModifierKeys::rightButtonModifier);
  1141. buttonMsg = true;
  1142. }
  1143. else if (map == Keys::MiddleButton)
  1144. {
  1145. currentModifiers = currentModifiers.withFlags (ModifierKeys::middleButtonModifier);
  1146. buttonMsg = true;
  1147. }
  1148. if (buttonMsg)
  1149. {
  1150. toFront (true);
  1151. handleMouseEvent (0, Point<int> (buttonPressEvent->x, buttonPressEvent->y), currentModifiers,
  1152. getEventTime (buttonPressEvent->time));
  1153. }
  1154. clearLastMousePos();
  1155. }
  1156. void handleButtonReleaseEvent (const XButtonReleasedEvent* const buttonRelEvent)
  1157. {
  1158. updateKeyModifiers (buttonRelEvent->state);
  1159. const int map = pointerMap [buttonRelEvent->button - Button1];
  1160. if (map == Keys::LeftButton) currentModifiers = currentModifiers.withoutFlags (ModifierKeys::leftButtonModifier);
  1161. else if (map == Keys::RightButton) currentModifiers = currentModifiers.withoutFlags (ModifierKeys::rightButtonModifier);
  1162. else if (map == Keys::MiddleButton) currentModifiers = currentModifiers.withoutFlags (ModifierKeys::middleButtonModifier);
  1163. handleMouseEvent (0, Point<int> (buttonRelEvent->x, buttonRelEvent->y), currentModifiers,
  1164. getEventTime (buttonRelEvent->time));
  1165. clearLastMousePos();
  1166. }
  1167. void handleMotionNotifyEvent (const XPointerMovedEvent* const movedEvent)
  1168. {
  1169. updateKeyModifiers (movedEvent->state);
  1170. const Point<int> mousePos (movedEvent->x_root, movedEvent->y_root);
  1171. if (lastMousePos != mousePos)
  1172. {
  1173. lastMousePos = mousePos;
  1174. if (parentWindow != nullptr && (styleFlags & windowHasTitleBar) == 0)
  1175. {
  1176. Window wRoot = 0, wParent = 0;
  1177. {
  1178. ScopedXLock xlock;
  1179. unsigned int numChildren;
  1180. Window* wChild = nullptr;
  1181. XQueryTree (display, windowH, &wRoot, &wParent, &wChild, &numChildren);
  1182. }
  1183. if (wParent != 0
  1184. && wParent != windowH
  1185. && wParent != wRoot)
  1186. {
  1187. parentWindow = wParent;
  1188. updateBounds();
  1189. }
  1190. else
  1191. {
  1192. parentWindow = 0;
  1193. }
  1194. }
  1195. handleMouseEvent (0, mousePos - getScreenPosition(), currentModifiers, getEventTime (movedEvent->time));
  1196. }
  1197. }
  1198. void handleEnterNotifyEvent (const XEnterWindowEvent* const enterEvent)
  1199. {
  1200. clearLastMousePos();
  1201. if (! currentModifiers.isAnyMouseButtonDown())
  1202. {
  1203. updateKeyModifiers (enterEvent->state);
  1204. handleMouseEvent (0, Point<int> (enterEvent->x, enterEvent->y), currentModifiers, getEventTime (enterEvent->time));
  1205. }
  1206. }
  1207. void handleLeaveNotifyEvent (const XLeaveWindowEvent* const leaveEvent)
  1208. {
  1209. // Suppress the normal leave if we've got a pointer grab, or if
  1210. // it's a bogus one caused by clicking a mouse button when running
  1211. // in a Window manager
  1212. if (((! currentModifiers.isAnyMouseButtonDown()) && leaveEvent->mode == NotifyNormal)
  1213. || leaveEvent->mode == NotifyUngrab)
  1214. {
  1215. updateKeyModifiers (leaveEvent->state);
  1216. handleMouseEvent (0, Point<int> (leaveEvent->x, leaveEvent->y), currentModifiers, getEventTime (leaveEvent->time));
  1217. }
  1218. }
  1219. void handleFocusInEvent()
  1220. {
  1221. isActiveApplication = true;
  1222. if (isFocused())
  1223. handleFocusGain();
  1224. }
  1225. void handleFocusOutEvent()
  1226. {
  1227. isActiveApplication = false;
  1228. if (! isFocused())
  1229. handleFocusLoss();
  1230. }
  1231. void handleExposeEvent (XExposeEvent* exposeEvent)
  1232. {
  1233. // Batch together all pending expose events
  1234. XEvent nextEvent;
  1235. ScopedXLock xlock;
  1236. if (exposeEvent->window != windowH)
  1237. {
  1238. Window child;
  1239. XTranslateCoordinates (display, exposeEvent->window, windowH,
  1240. exposeEvent->x, exposeEvent->y, &exposeEvent->x, &exposeEvent->y,
  1241. &child);
  1242. }
  1243. repaint (Rectangle<int> (exposeEvent->x, exposeEvent->y,
  1244. exposeEvent->width, exposeEvent->height));
  1245. while (XEventsQueued (display, QueuedAfterFlush) > 0)
  1246. {
  1247. XPeekEvent (display, (XEvent*) &nextEvent);
  1248. if (nextEvent.type != Expose || nextEvent.xany.window != exposeEvent->window)
  1249. break;
  1250. XNextEvent (display, (XEvent*) &nextEvent);
  1251. XExposeEvent* nextExposeEvent = (XExposeEvent*) &nextEvent.xexpose;
  1252. repaint (Rectangle<int> (nextExposeEvent->x, nextExposeEvent->y,
  1253. nextExposeEvent->width, nextExposeEvent->height));
  1254. }
  1255. }
  1256. void handleConfigureNotifyEvent (XConfigureEvent* const confEvent)
  1257. {
  1258. updateBounds();
  1259. updateBorderSize();
  1260. handleMovedOrResized();
  1261. // if the native title bar is dragged, need to tell any active menus, etc.
  1262. if ((styleFlags & windowHasTitleBar) != 0
  1263. && component->isCurrentlyBlockedByAnotherModalComponent())
  1264. {
  1265. Component* const currentModalComp = Component::getCurrentlyModalComponent();
  1266. if (currentModalComp != 0)
  1267. currentModalComp->inputAttemptWhenModal();
  1268. }
  1269. if (confEvent->window == windowH
  1270. && confEvent->above != 0
  1271. && isFrontWindow())
  1272. {
  1273. handleBroughtToFront();
  1274. }
  1275. }
  1276. void handleReparentNotifyEvent()
  1277. {
  1278. parentWindow = 0;
  1279. Window wRoot = 0;
  1280. Window* wChild = nullptr;
  1281. unsigned int numChildren;
  1282. {
  1283. ScopedXLock xlock;
  1284. XQueryTree (display, windowH, &wRoot, &parentWindow, &wChild, &numChildren);
  1285. }
  1286. if (parentWindow == windowH || parentWindow == wRoot)
  1287. parentWindow = 0;
  1288. handleGravityNotify();
  1289. }
  1290. void handleGravityNotify()
  1291. {
  1292. updateBounds();
  1293. updateBorderSize();
  1294. handleMovedOrResized();
  1295. }
  1296. void handleMappingNotify (XMappingEvent* const mappingEvent)
  1297. {
  1298. if (mappingEvent->request != MappingPointer)
  1299. {
  1300. // Deal with modifier/keyboard mapping
  1301. ScopedXLock xlock;
  1302. XRefreshKeyboardMapping (mappingEvent);
  1303. updateModifierMappings();
  1304. }
  1305. }
  1306. void handleClientMessageEvent (XClientMessageEvent* const clientMsg, XEvent* event)
  1307. {
  1308. if (clientMsg->message_type == Atoms::Protocols && clientMsg->format == 32)
  1309. {
  1310. const Atom atom = (Atom) clientMsg->data.l[0];
  1311. if (atom == Atoms::ProtocolList [Atoms::PING])
  1312. {
  1313. Window root = RootWindow (display, DefaultScreen (display));
  1314. clientMsg->window = root;
  1315. XSendEvent (display, root, False, NoEventMask, event);
  1316. XFlush (display);
  1317. }
  1318. else if (atom == Atoms::ProtocolList [Atoms::TAKE_FOCUS])
  1319. {
  1320. XWindowAttributes atts;
  1321. ScopedXLock xlock;
  1322. if (clientMsg->window != 0
  1323. && XGetWindowAttributes (display, clientMsg->window, &atts))
  1324. {
  1325. if (atts.map_state == IsViewable)
  1326. XSetInputFocus (display, clientMsg->window, RevertToParent, clientMsg->data.l[1]);
  1327. }
  1328. }
  1329. else if (atom == Atoms::ProtocolList [Atoms::DELETE_WINDOW])
  1330. {
  1331. handleUserClosingWindow();
  1332. }
  1333. }
  1334. else if (clientMsg->message_type == Atoms::XdndEnter)
  1335. {
  1336. handleDragAndDropEnter (clientMsg);
  1337. }
  1338. else if (clientMsg->message_type == Atoms::XdndLeave)
  1339. {
  1340. resetDragAndDrop();
  1341. }
  1342. else if (clientMsg->message_type == Atoms::XdndPosition)
  1343. {
  1344. handleDragAndDropPosition (clientMsg);
  1345. }
  1346. else if (clientMsg->message_type == Atoms::XdndDrop)
  1347. {
  1348. handleDragAndDropDrop (clientMsg);
  1349. }
  1350. else if (clientMsg->message_type == Atoms::XdndStatus)
  1351. {
  1352. handleDragAndDropStatus (clientMsg);
  1353. }
  1354. else if (clientMsg->message_type == Atoms::XdndFinished)
  1355. {
  1356. resetDragAndDrop();
  1357. }
  1358. }
  1359. //==============================================================================
  1360. void showMouseCursor (Cursor cursor) noexcept
  1361. {
  1362. ScopedXLock xlock;
  1363. XDefineCursor (display, windowH, cursor);
  1364. }
  1365. //==============================================================================
  1366. void setTaskBarIcon (const Image& image)
  1367. {
  1368. ScopedXLock xlock;
  1369. taskbarImage = image;
  1370. Screen* const screen = XDefaultScreenOfDisplay (display);
  1371. const int screenNumber = XScreenNumberOfScreen (screen);
  1372. String screenAtom ("_NET_SYSTEM_TRAY_S");
  1373. screenAtom << screenNumber;
  1374. Atom selectionAtom = XInternAtom (display, screenAtom.toUTF8(), false);
  1375. XGrabServer (display);
  1376. Window managerWin = XGetSelectionOwner (display, selectionAtom);
  1377. if (managerWin != None)
  1378. XSelectInput (display, managerWin, StructureNotifyMask);
  1379. XUngrabServer (display);
  1380. XFlush (display);
  1381. if (managerWin != None)
  1382. {
  1383. XEvent ev = { 0 };
  1384. ev.xclient.type = ClientMessage;
  1385. ev.xclient.window = managerWin;
  1386. ev.xclient.message_type = XInternAtom (display, "_NET_SYSTEM_TRAY_OPCODE", False);
  1387. ev.xclient.format = 32;
  1388. ev.xclient.data.l[0] = CurrentTime;
  1389. ev.xclient.data.l[1] = 0 /*SYSTEM_TRAY_REQUEST_DOCK*/;
  1390. ev.xclient.data.l[2] = windowH;
  1391. ev.xclient.data.l[3] = 0;
  1392. ev.xclient.data.l[4] = 0;
  1393. XSendEvent (display, managerWin, False, NoEventMask, &ev);
  1394. XSync (display, False);
  1395. }
  1396. // For older KDE's ...
  1397. long atomData = 1;
  1398. Atom trayAtom = XInternAtom (display, "KWM_DOCKWINDOW", false);
  1399. XChangeProperty (display, windowH, trayAtom, trayAtom, 32, PropModeReplace, (unsigned char*) &atomData, 1);
  1400. // For more recent KDE's...
  1401. trayAtom = XInternAtom (display, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", false);
  1402. XChangeProperty (display, windowH, trayAtom, XA_WINDOW, 32, PropModeReplace, (unsigned char*) &windowH, 1);
  1403. // a minimum size must be specified for GNOME and Xfce, otherwise the icon is displayed with a width of 1
  1404. XSizeHints* hints = XAllocSizeHints();
  1405. hints->flags = PMinSize;
  1406. hints->min_width = 22;
  1407. hints->min_height = 22;
  1408. XSetWMNormalHints (display, windowH, hints);
  1409. XFree (hints);
  1410. }
  1411. const Image& getTaskbarIcon() const noexcept { return taskbarImage; }
  1412. //==============================================================================
  1413. bool dontRepaint;
  1414. static ModifierKeys currentModifiers;
  1415. static bool isActiveApplication;
  1416. private:
  1417. //==============================================================================
  1418. class LinuxRepaintManager : public Timer
  1419. {
  1420. public:
  1421. LinuxRepaintManager (LinuxComponentPeer* const peer_)
  1422. : peer (peer_),
  1423. lastTimeImageUsed (0)
  1424. {
  1425. #if JUCE_USE_XSHM
  1426. shmCompletedDrawing = true;
  1427. useARGBImagesForRendering = XSHMHelpers::isShmAvailable();
  1428. if (useARGBImagesForRendering)
  1429. {
  1430. ScopedXLock xlock;
  1431. XShmSegmentInfo segmentinfo;
  1432. XImage* const testImage
  1433. = XShmCreateImage (display, DefaultVisual (display, DefaultScreen (display)),
  1434. 24, ZPixmap, 0, &segmentinfo, 64, 64);
  1435. useARGBImagesForRendering = (testImage->bits_per_pixel == 32);
  1436. XDestroyImage (testImage);
  1437. }
  1438. #endif
  1439. }
  1440. void timerCallback()
  1441. {
  1442. #if JUCE_USE_XSHM
  1443. if (! shmCompletedDrawing)
  1444. return;
  1445. #endif
  1446. if (! regionsNeedingRepaint.isEmpty())
  1447. {
  1448. stopTimer();
  1449. performAnyPendingRepaintsNow();
  1450. }
  1451. else if (Time::getApproximateMillisecondCounter() > lastTimeImageUsed + 3000)
  1452. {
  1453. stopTimer();
  1454. image = Image::null;
  1455. }
  1456. }
  1457. void repaint (const Rectangle<int>& area)
  1458. {
  1459. if (! isTimerRunning())
  1460. startTimer (repaintTimerPeriod);
  1461. regionsNeedingRepaint.add (area);
  1462. }
  1463. void performAnyPendingRepaintsNow()
  1464. {
  1465. #if JUCE_USE_XSHM
  1466. if (! shmCompletedDrawing)
  1467. {
  1468. startTimer (repaintTimerPeriod);
  1469. return;
  1470. }
  1471. #endif
  1472. peer->clearMaskedRegion();
  1473. RectangleList originalRepaintRegion (regionsNeedingRepaint);
  1474. regionsNeedingRepaint.clear();
  1475. const Rectangle<int> totalArea (originalRepaintRegion.getBounds());
  1476. if (! totalArea.isEmpty())
  1477. {
  1478. if (image.isNull() || image.getWidth() < totalArea.getWidth()
  1479. || image.getHeight() < totalArea.getHeight())
  1480. {
  1481. #if JUCE_USE_XSHM
  1482. image = Image (new XBitmapImage (useARGBImagesForRendering ? Image::ARGB
  1483. : Image::RGB,
  1484. #else
  1485. image = Image (new XBitmapImage (Image::RGB,
  1486. #endif
  1487. (totalArea.getWidth() + 31) & ~31,
  1488. (totalArea.getHeight() + 31) & ~31,
  1489. false, peer->depth, peer->visual));
  1490. }
  1491. startTimer (repaintTimerPeriod);
  1492. RectangleList adjustedList (originalRepaintRegion);
  1493. adjustedList.offsetAll (-totalArea.getX(), -totalArea.getY());
  1494. LowLevelGraphicsSoftwareRenderer context (image, -totalArea.getX(), -totalArea.getY(), adjustedList);
  1495. if (peer->depth == 32)
  1496. {
  1497. RectangleList::Iterator i (originalRepaintRegion);
  1498. while (i.next())
  1499. image.clear (*i.getRectangle() - totalArea.getPosition());
  1500. }
  1501. peer->handlePaint (context);
  1502. if (! peer->maskedRegion.isEmpty())
  1503. originalRepaintRegion.subtract (peer->maskedRegion);
  1504. for (RectangleList::Iterator i (originalRepaintRegion); i.next();)
  1505. {
  1506. #if JUCE_USE_XSHM
  1507. shmCompletedDrawing = false;
  1508. #endif
  1509. const Rectangle<int>& r = *i.getRectangle();
  1510. static_cast<XBitmapImage*> (image.getSharedImage())
  1511. ->blitToWindow (peer->windowH,
  1512. r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  1513. r.getX() - totalArea.getX(), r.getY() - totalArea.getY());
  1514. }
  1515. }
  1516. lastTimeImageUsed = Time::getApproximateMillisecondCounter();
  1517. startTimer (repaintTimerPeriod);
  1518. }
  1519. #if JUCE_USE_XSHM
  1520. void notifyPaintCompleted() { shmCompletedDrawing = true; }
  1521. #endif
  1522. private:
  1523. enum { repaintTimerPeriod = 1000 / 100 };
  1524. LinuxComponentPeer* const peer;
  1525. Image image;
  1526. uint32 lastTimeImageUsed;
  1527. RectangleList regionsNeedingRepaint;
  1528. #if JUCE_USE_XSHM
  1529. bool useARGBImagesForRendering, shmCompletedDrawing;
  1530. #endif
  1531. JUCE_DECLARE_NON_COPYABLE (LinuxRepaintManager);
  1532. };
  1533. ScopedPointer <LinuxRepaintManager> repainter;
  1534. friend class LinuxRepaintManager;
  1535. Window windowH, parentWindow;
  1536. int wx, wy, ww, wh;
  1537. Image taskbarImage;
  1538. bool fullScreen, mapped;
  1539. Visual* visual;
  1540. int depth;
  1541. BorderSize<int> windowBorder;
  1542. struct MotifWmHints
  1543. {
  1544. unsigned long flags;
  1545. unsigned long functions;
  1546. unsigned long decorations;
  1547. long input_mode;
  1548. unsigned long status;
  1549. };
  1550. static void updateKeyStates (const int keycode, const bool press) noexcept
  1551. {
  1552. const int keybyte = keycode >> 3;
  1553. const int keybit = (1 << (keycode & 7));
  1554. if (press)
  1555. Keys::keyStates [keybyte] |= keybit;
  1556. else
  1557. Keys::keyStates [keybyte] &= ~keybit;
  1558. }
  1559. static void updateKeyModifiers (const int status) noexcept
  1560. {
  1561. int keyMods = 0;
  1562. if ((status & ShiftMask) != 0) keyMods |= ModifierKeys::shiftModifier;
  1563. if ((status & ControlMask) != 0) keyMods |= ModifierKeys::ctrlModifier;
  1564. if ((status & Keys::AltMask) != 0) keyMods |= ModifierKeys::altModifier;
  1565. currentModifiers = currentModifiers.withOnlyMouseButtons().withFlags (keyMods);
  1566. Keys::numLock = ((status & Keys::NumLockMask) != 0);
  1567. Keys::capsLock = ((status & LockMask) != 0);
  1568. }
  1569. static bool updateKeyModifiersFromSym (KeySym sym, const bool press) noexcept
  1570. {
  1571. int modifier = 0;
  1572. bool isModifier = true;
  1573. switch (sym)
  1574. {
  1575. case XK_Shift_L:
  1576. case XK_Shift_R:
  1577. modifier = ModifierKeys::shiftModifier;
  1578. break;
  1579. case XK_Control_L:
  1580. case XK_Control_R:
  1581. modifier = ModifierKeys::ctrlModifier;
  1582. break;
  1583. case XK_Alt_L:
  1584. case XK_Alt_R:
  1585. modifier = ModifierKeys::altModifier;
  1586. break;
  1587. case XK_Num_Lock:
  1588. if (press)
  1589. Keys::numLock = ! Keys::numLock;
  1590. break;
  1591. case XK_Caps_Lock:
  1592. if (press)
  1593. Keys::capsLock = ! Keys::capsLock;
  1594. break;
  1595. case XK_Scroll_Lock:
  1596. break;
  1597. default:
  1598. isModifier = false;
  1599. break;
  1600. }
  1601. if (modifier != 0)
  1602. {
  1603. if (press)
  1604. currentModifiers = currentModifiers.withFlags (modifier);
  1605. else
  1606. currentModifiers = currentModifiers.withoutFlags (modifier);
  1607. }
  1608. return isModifier;
  1609. }
  1610. // Alt and Num lock are not defined by standard X
  1611. // modifier constants: check what they're mapped to
  1612. static void updateModifierMappings() noexcept
  1613. {
  1614. ScopedXLock xlock;
  1615. const int altLeftCode = XKeysymToKeycode (display, XK_Alt_L);
  1616. const int numLockCode = XKeysymToKeycode (display, XK_Num_Lock);
  1617. Keys::AltMask = 0;
  1618. Keys::NumLockMask = 0;
  1619. XModifierKeymap* mapping = XGetModifierMapping (display);
  1620. if (mapping)
  1621. {
  1622. for (int i = 0; i < 8; i++)
  1623. {
  1624. if (mapping->modifiermap [i << 1] == altLeftCode)
  1625. Keys::AltMask = 1 << i;
  1626. else if (mapping->modifiermap [i << 1] == numLockCode)
  1627. Keys::NumLockMask = 1 << i;
  1628. }
  1629. XFreeModifiermap (mapping);
  1630. }
  1631. }
  1632. //==============================================================================
  1633. void removeWindowDecorations (Window wndH)
  1634. {
  1635. Atom hints = XInternAtom (display, "_MOTIF_WM_HINTS", True);
  1636. if (hints != None)
  1637. {
  1638. MotifWmHints motifHints = { 0 };
  1639. motifHints.flags = 2; /* MWM_HINTS_DECORATIONS */
  1640. motifHints.decorations = 0;
  1641. ScopedXLock xlock;
  1642. XChangeProperty (display, wndH, hints, hints, 32, PropModeReplace,
  1643. (unsigned char*) &motifHints, 4);
  1644. }
  1645. hints = XInternAtom (display, "_WIN_HINTS", True);
  1646. if (hints != None)
  1647. {
  1648. long gnomeHints = 0;
  1649. ScopedXLock xlock;
  1650. XChangeProperty (display, wndH, hints, hints, 32, PropModeReplace,
  1651. (unsigned char*) &gnomeHints, 1);
  1652. }
  1653. hints = XInternAtom (display, "KWM_WIN_DECORATION", True);
  1654. if (hints != None)
  1655. {
  1656. long kwmHints = 2; /*KDE_tinyDecoration*/
  1657. ScopedXLock xlock;
  1658. XChangeProperty (display, wndH, hints, hints, 32, PropModeReplace,
  1659. (unsigned char*) &kwmHints, 1);
  1660. }
  1661. }
  1662. void addWindowButtons (Window wndH)
  1663. {
  1664. ScopedXLock xlock;
  1665. Atom hints = XInternAtom (display, "_MOTIF_WM_HINTS", True);
  1666. if (hints != None)
  1667. {
  1668. MotifWmHints motifHints = { 0 };
  1669. motifHints.flags = 1 | 2; /* MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS */
  1670. motifHints.decorations = 2 /* MWM_DECOR_BORDER */ | 8 /* MWM_DECOR_TITLE */ | 16; /* MWM_DECOR_MENU */
  1671. motifHints.functions = 4 /* MWM_FUNC_MOVE */;
  1672. if ((styleFlags & windowHasCloseButton) != 0)
  1673. motifHints.functions |= 32; /* MWM_FUNC_CLOSE */
  1674. if ((styleFlags & windowHasMinimiseButton) != 0)
  1675. {
  1676. motifHints.functions |= 8; /* MWM_FUNC_MINIMIZE */
  1677. motifHints.decorations |= 0x20; /* MWM_DECOR_MINIMIZE */
  1678. }
  1679. if ((styleFlags & windowHasMaximiseButton) != 0)
  1680. {
  1681. motifHints.functions |= 0x10; /* MWM_FUNC_MAXIMIZE */
  1682. motifHints.decorations |= 0x40; /* MWM_DECOR_MAXIMIZE */
  1683. }
  1684. if ((styleFlags & windowIsResizable) != 0)
  1685. {
  1686. motifHints.functions |= 2; /* MWM_FUNC_RESIZE */
  1687. motifHints.decorations |= 0x4; /* MWM_DECOR_RESIZEH */
  1688. }
  1689. XChangeProperty (display, wndH, hints, hints, 32, 0, (unsigned char*) &motifHints, 5);
  1690. }
  1691. hints = XInternAtom (display, "_NET_WM_ALLOWED_ACTIONS", True);
  1692. if (hints != None)
  1693. {
  1694. int netHints [6];
  1695. int num = 0;
  1696. if ((styleFlags & windowIsResizable) != 0)
  1697. netHints [num++] = XInternAtom (display, "_NET_WM_ACTION_RESIZE", True);
  1698. if ((styleFlags & windowHasMaximiseButton) != 0)
  1699. netHints [num++] = XInternAtom (display, "_NET_WM_ACTION_FULLSCREEN", True);
  1700. if ((styleFlags & windowHasMinimiseButton) != 0)
  1701. netHints [num++] = XInternAtom (display, "_NET_WM_ACTION_MINIMIZE", True);
  1702. if ((styleFlags & windowHasCloseButton) != 0)
  1703. netHints [num++] = XInternAtom (display, "_NET_WM_ACTION_CLOSE", True);
  1704. XChangeProperty (display, wndH, hints, XA_ATOM, 32, PropModeReplace, (unsigned char*) &netHints, num);
  1705. }
  1706. }
  1707. void setWindowType()
  1708. {
  1709. int netHints [2];
  1710. int numHints = 0;
  1711. if ((styleFlags & windowIsTemporary) != 0
  1712. || ((styleFlags & windowHasDropShadow) == 0 && Desktop::canUseSemiTransparentWindows()))
  1713. netHints [numHints++] = XInternAtom (display, "_NET_WM_WINDOW_TYPE_COMBO", True);
  1714. else
  1715. netHints [numHints++] = XInternAtom (display, "_NET_WM_WINDOW_TYPE_NORMAL", True);
  1716. netHints[numHints++] = XInternAtom (display, "_KDE_NET_WM_WINDOW_TYPE_OVERRIDE", True);
  1717. XChangeProperty (display, windowH, Atoms::WindowType, XA_ATOM, 32, PropModeReplace,
  1718. (unsigned char*) &netHints, numHints);
  1719. numHints = 0;
  1720. if ((styleFlags & windowAppearsOnTaskbar) == 0)
  1721. netHints [numHints++] = XInternAtom (display, "_NET_WM_STATE_SKIP_TASKBAR", True);
  1722. if (component->isAlwaysOnTop())
  1723. netHints [numHints++] = XInternAtom (display, "_NET_WM_STATE_ABOVE", True);
  1724. if (numHints > 0)
  1725. XChangeProperty (display, windowH, Atoms::WindowState, XA_ATOM, 32, PropModeReplace,
  1726. (unsigned char*) &netHints, numHints);
  1727. }
  1728. void createWindow (Window parentToAddTo)
  1729. {
  1730. ScopedXLock xlock;
  1731. Atoms::initialiseAtoms();
  1732. resetDragAndDrop();
  1733. // Get defaults for various properties
  1734. const int screen = DefaultScreen (display);
  1735. Window root = RootWindow (display, screen);
  1736. // Try to obtain a 32-bit visual or fallback to 24 or 16
  1737. visual = Visuals::findVisualFormat ((styleFlags & windowIsSemiTransparent) ? 32 : 24, depth);
  1738. if (visual == 0)
  1739. {
  1740. Logger::outputDebugString ("ERROR: System doesn't support 32, 24 or 16 bit RGB display.\n");
  1741. Process::terminate();
  1742. }
  1743. // Create and install a colormap suitable fr our visual
  1744. Colormap colormap = XCreateColormap (display, root, visual, AllocNone);
  1745. XInstallColormap (display, colormap);
  1746. // Set up the window attributes
  1747. XSetWindowAttributes swa;
  1748. swa.border_pixel = 0;
  1749. swa.background_pixmap = None;
  1750. swa.colormap = colormap;
  1751. swa.event_mask = getAllEventsMask();
  1752. windowH = XCreateWindow (display, parentToAddTo != 0 ? parentToAddTo : root,
  1753. 0, 0, 1, 1,
  1754. 0, depth, InputOutput, visual,
  1755. CWBorderPixel | CWColormap | CWBackPixmap | CWEventMask,
  1756. &swa);
  1757. XGrabButton (display, AnyButton, AnyModifier, windowH, False,
  1758. ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
  1759. GrabModeAsync, GrabModeAsync, None, None);
  1760. // Set the window context to identify the window handle object
  1761. if (XSaveContext (display, (XID) windowH, windowHandleXContext, (XPointer) this))
  1762. {
  1763. // Failed
  1764. jassertfalse;
  1765. Logger::outputDebugString ("Failed to create context information for window.\n");
  1766. XDestroyWindow (display, windowH);
  1767. windowH = 0;
  1768. return;
  1769. }
  1770. // Set window manager hints
  1771. XWMHints* wmHints = XAllocWMHints();
  1772. wmHints->flags = InputHint | StateHint;
  1773. wmHints->input = True; // Locally active input model
  1774. wmHints->initial_state = NormalState;
  1775. XSetWMHints (display, windowH, wmHints);
  1776. XFree (wmHints);
  1777. // Set the window type
  1778. setWindowType();
  1779. // Define decoration
  1780. if ((styleFlags & windowHasTitleBar) == 0)
  1781. removeWindowDecorations (windowH);
  1782. else
  1783. addWindowButtons (windowH);
  1784. setTitle (getComponent()->getName());
  1785. // Associate the PID, allowing to be shut down when something goes wrong
  1786. unsigned long pid = getpid();
  1787. XChangeProperty (display, windowH, Atoms::Pid, XA_CARDINAL, 32, PropModeReplace,
  1788. (unsigned char*) &pid, 1);
  1789. // Set window manager protocols
  1790. XChangeProperty (display, windowH, Atoms::Protocols, XA_ATOM, 32, PropModeReplace,
  1791. (unsigned char*) Atoms::ProtocolList, 2);
  1792. // Set drag and drop flags
  1793. XChangeProperty (display, windowH, Atoms::XdndTypeList, XA_ATOM, 32, PropModeReplace,
  1794. (const unsigned char*) Atoms::allowedMimeTypes, numElementsInArray (Atoms::allowedMimeTypes));
  1795. XChangeProperty (display, windowH, Atoms::XdndActionList, XA_ATOM, 32, PropModeReplace,
  1796. (const unsigned char*) Atoms::allowedActions, numElementsInArray (Atoms::allowedActions));
  1797. XChangeProperty (display, windowH, Atoms::XdndActionDescription, XA_STRING, 8, PropModeReplace,
  1798. (const unsigned char*) "", 0);
  1799. unsigned long dndVersion = Atoms::DndVersion;
  1800. XChangeProperty (display, windowH, Atoms::XdndAware, XA_ATOM, 32, PropModeReplace,
  1801. (const unsigned char*) &dndVersion, 1);
  1802. // Initialise the pointer and keyboard mapping
  1803. // This is not the same as the logical pointer mapping the X server uses:
  1804. // we don't mess with this.
  1805. static bool mappingInitialised = false;
  1806. if (! mappingInitialised)
  1807. {
  1808. mappingInitialised = true;
  1809. const int numButtons = XGetPointerMapping (display, 0, 0);
  1810. if (numButtons == 2)
  1811. {
  1812. pointerMap[0] = Keys::LeftButton;
  1813. pointerMap[1] = Keys::RightButton;
  1814. pointerMap[2] = pointerMap[3] = pointerMap[4] = Keys::NoButton;
  1815. }
  1816. else if (numButtons >= 3)
  1817. {
  1818. pointerMap[0] = Keys::LeftButton;
  1819. pointerMap[1] = Keys::MiddleButton;
  1820. pointerMap[2] = Keys::RightButton;
  1821. if (numButtons >= 5)
  1822. {
  1823. pointerMap[3] = Keys::WheelUp;
  1824. pointerMap[4] = Keys::WheelDown;
  1825. }
  1826. }
  1827. updateModifierMappings();
  1828. }
  1829. }
  1830. void destroyWindow()
  1831. {
  1832. ScopedXLock xlock;
  1833. XPointer handlePointer;
  1834. if (! XFindContext (display, (XID) windowH, windowHandleXContext, &handlePointer))
  1835. XDeleteContext (display, (XID) windowH, windowHandleXContext);
  1836. XDestroyWindow (display, windowH);
  1837. // Wait for it to complete and then remove any events for this
  1838. // window from the event queue.
  1839. XSync (display, false);
  1840. XEvent event;
  1841. while (XCheckWindowEvent (display, windowH, getAllEventsMask(), &event) == True)
  1842. {}
  1843. }
  1844. static int getAllEventsMask() noexcept
  1845. {
  1846. return NoEventMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask
  1847. | EnterWindowMask | LeaveWindowMask | PointerMotionMask | KeymapStateMask
  1848. | ExposureMask | StructureNotifyMask | FocusChangeMask;
  1849. }
  1850. static int64 getEventTime (::Time t)
  1851. {
  1852. static int64 eventTimeOffset = 0x12345678;
  1853. const int64 thisMessageTime = t;
  1854. if (eventTimeOffset == 0x12345678)
  1855. eventTimeOffset = Time::currentTimeMillis() - thisMessageTime;
  1856. return eventTimeOffset + thisMessageTime;
  1857. }
  1858. void updateBorderSize()
  1859. {
  1860. if ((styleFlags & windowHasTitleBar) == 0)
  1861. {
  1862. windowBorder = BorderSize<int> (0);
  1863. }
  1864. else if (windowBorder.getTopAndBottom() == 0 && windowBorder.getLeftAndRight() == 0)
  1865. {
  1866. ScopedXLock xlock;
  1867. Atom hints = XInternAtom (display, "_NET_FRAME_EXTENTS", True);
  1868. if (hints != None)
  1869. {
  1870. unsigned char* data = nullptr;
  1871. unsigned long nitems, bytesLeft;
  1872. Atom actualType;
  1873. int actualFormat;
  1874. if (XGetWindowProperty (display, windowH, hints, 0, 4, False,
  1875. XA_CARDINAL, &actualType, &actualFormat, &nitems, &bytesLeft,
  1876. &data) == Success)
  1877. {
  1878. const unsigned long* const sizes = (const unsigned long*) data;
  1879. if (actualFormat == 32)
  1880. windowBorder = BorderSize<int> ((int) sizes[2], (int) sizes[0],
  1881. (int) sizes[3], (int) sizes[1]);
  1882. XFree (data);
  1883. }
  1884. }
  1885. }
  1886. }
  1887. void updateBounds()
  1888. {
  1889. jassert (windowH != 0);
  1890. if (windowH != 0)
  1891. {
  1892. Window root, child;
  1893. unsigned int bw, depth;
  1894. ScopedXLock xlock;
  1895. if (! XGetGeometry (display, (::Drawable) windowH, &root,
  1896. &wx, &wy, (unsigned int*) &ww, (unsigned int*) &wh,
  1897. &bw, &depth))
  1898. {
  1899. wx = wy = ww = wh = 0;
  1900. }
  1901. else if (! XTranslateCoordinates (display, windowH, root, 0, 0, &wx, &wy, &child))
  1902. {
  1903. wx = wy = 0;
  1904. }
  1905. }
  1906. }
  1907. //==============================================================================
  1908. void resetDragAndDrop()
  1909. {
  1910. dragAndDropFiles.clear();
  1911. lastDropPos = Point<int> (-1, -1);
  1912. dragAndDropCurrentMimeType = 0;
  1913. dragAndDropSourceWindow = 0;
  1914. srcMimeTypeAtomList.clear();
  1915. }
  1916. void sendDragAndDropMessage (XClientMessageEvent& msg)
  1917. {
  1918. msg.type = ClientMessage;
  1919. msg.display = display;
  1920. msg.window = dragAndDropSourceWindow;
  1921. msg.format = 32;
  1922. msg.data.l[0] = windowH;
  1923. ScopedXLock xlock;
  1924. XSendEvent (display, dragAndDropSourceWindow, False, 0, (XEvent*) &msg);
  1925. }
  1926. void sendDragAndDropStatus (const bool acceptDrop, Atom dropAction)
  1927. {
  1928. XClientMessageEvent msg = { 0 };
  1929. msg.message_type = Atoms::XdndStatus;
  1930. msg.data.l[1] = (acceptDrop ? 1 : 0) | 2; // 2 indicates that we want to receive position messages
  1931. msg.data.l[4] = dropAction;
  1932. sendDragAndDropMessage (msg);
  1933. }
  1934. void sendDragAndDropLeave()
  1935. {
  1936. XClientMessageEvent msg = { 0 };
  1937. msg.message_type = Atoms::XdndLeave;
  1938. sendDragAndDropMessage (msg);
  1939. }
  1940. void sendDragAndDropFinish()
  1941. {
  1942. XClientMessageEvent msg = { 0 };
  1943. msg.message_type = Atoms::XdndFinished;
  1944. sendDragAndDropMessage (msg);
  1945. }
  1946. void handleDragAndDropStatus (const XClientMessageEvent* const clientMsg)
  1947. {
  1948. if ((clientMsg->data.l[1] & 1) == 0)
  1949. {
  1950. sendDragAndDropLeave();
  1951. if (dragAndDropFiles.size() > 0)
  1952. handleFileDragExit (dragAndDropFiles);
  1953. dragAndDropFiles.clear();
  1954. }
  1955. }
  1956. void handleDragAndDropPosition (const XClientMessageEvent* const clientMsg)
  1957. {
  1958. if (dragAndDropSourceWindow == 0)
  1959. return;
  1960. dragAndDropSourceWindow = clientMsg->data.l[0];
  1961. Point<int> dropPos ((int) clientMsg->data.l[2] >> 16,
  1962. (int) clientMsg->data.l[2] & 0xffff);
  1963. dropPos -= getScreenPosition();
  1964. if (lastDropPos != dropPos)
  1965. {
  1966. lastDropPos = dropPos;
  1967. dragAndDropTimestamp = clientMsg->data.l[3];
  1968. Atom targetAction = Atoms::XdndActionCopy;
  1969. for (int i = numElementsInArray (Atoms::allowedActions); --i >= 0;)
  1970. {
  1971. if ((Atom) clientMsg->data.l[4] == Atoms::allowedActions[i])
  1972. {
  1973. targetAction = Atoms::allowedActions[i];
  1974. break;
  1975. }
  1976. }
  1977. sendDragAndDropStatus (true, targetAction);
  1978. if (dragAndDropFiles.size() == 0)
  1979. updateDraggedFileList (clientMsg);
  1980. if (dragAndDropFiles.size() > 0)
  1981. handleFileDragMove (dragAndDropFiles, dropPos);
  1982. }
  1983. }
  1984. void handleDragAndDropDrop (const XClientMessageEvent* const clientMsg)
  1985. {
  1986. if (dragAndDropFiles.size() == 0)
  1987. updateDraggedFileList (clientMsg);
  1988. const StringArray files (dragAndDropFiles);
  1989. const Point<int> lastPos (lastDropPos);
  1990. sendDragAndDropFinish();
  1991. resetDragAndDrop();
  1992. if (files.size() > 0)
  1993. handleFileDragDrop (files, lastPos);
  1994. }
  1995. void handleDragAndDropEnter (const XClientMessageEvent* const clientMsg)
  1996. {
  1997. dragAndDropFiles.clear();
  1998. srcMimeTypeAtomList.clear();
  1999. dragAndDropCurrentMimeType = 0;
  2000. const unsigned long dndCurrentVersion = static_cast <unsigned long> (clientMsg->data.l[1] & 0xff000000) >> 24;
  2001. if (dndCurrentVersion < 3 || dndCurrentVersion > Atoms::DndVersion)
  2002. {
  2003. dragAndDropSourceWindow = 0;
  2004. return;
  2005. }
  2006. dragAndDropSourceWindow = clientMsg->data.l[0];
  2007. if ((clientMsg->data.l[1] & 1) != 0)
  2008. {
  2009. Atom actual;
  2010. int format;
  2011. unsigned long count = 0, remaining = 0;
  2012. unsigned char* data = 0;
  2013. ScopedXLock xlock;
  2014. XGetWindowProperty (display, dragAndDropSourceWindow, Atoms::XdndTypeList,
  2015. 0, 0x8000000L, False, XA_ATOM, &actual, &format,
  2016. &count, &remaining, &data);
  2017. if (data != 0)
  2018. {
  2019. if (actual == XA_ATOM && format == 32 && count != 0)
  2020. {
  2021. const unsigned long* const types = (const unsigned long*) data;
  2022. for (unsigned int i = 0; i < count; ++i)
  2023. if (types[i] != None)
  2024. srcMimeTypeAtomList.add (types[i]);
  2025. }
  2026. XFree (data);
  2027. }
  2028. }
  2029. if (srcMimeTypeAtomList.size() == 0)
  2030. {
  2031. for (int i = 2; i < 5; ++i)
  2032. if (clientMsg->data.l[i] != None)
  2033. srcMimeTypeAtomList.add (clientMsg->data.l[i]);
  2034. if (srcMimeTypeAtomList.size() == 0)
  2035. {
  2036. dragAndDropSourceWindow = 0;
  2037. return;
  2038. }
  2039. }
  2040. for (int i = 0; i < srcMimeTypeAtomList.size() && dragAndDropCurrentMimeType == 0; ++i)
  2041. for (int j = 0; j < numElementsInArray (Atoms::allowedMimeTypes); ++j)
  2042. if (srcMimeTypeAtomList[i] == Atoms::allowedMimeTypes[j])
  2043. dragAndDropCurrentMimeType = Atoms::allowedMimeTypes[j];
  2044. handleDragAndDropPosition (clientMsg);
  2045. }
  2046. void handleDragAndDropSelection (const XEvent* const evt)
  2047. {
  2048. dragAndDropFiles.clear();
  2049. if (evt->xselection.property != 0)
  2050. {
  2051. StringArray lines;
  2052. {
  2053. MemoryBlock dropData;
  2054. for (;;)
  2055. {
  2056. Atom actual;
  2057. uint8* data = 0;
  2058. unsigned long count = 0, remaining = 0;
  2059. int format = 0;
  2060. ScopedXLock xlock;
  2061. if (XGetWindowProperty (display, evt->xany.window, evt->xselection.property,
  2062. dropData.getSize() / 4, 65536, 1, AnyPropertyType, &actual,
  2063. &format, &count, &remaining, &data) == Success)
  2064. {
  2065. dropData.append (data, count * format / 8);
  2066. XFree (data);
  2067. if (remaining == 0)
  2068. break;
  2069. }
  2070. else
  2071. {
  2072. XFree (data);
  2073. break;
  2074. }
  2075. }
  2076. lines.addLines (dropData.toString());
  2077. }
  2078. for (int i = 0; i < lines.size(); ++i)
  2079. dragAndDropFiles.add (URL::removeEscapeChars (lines[i].fromFirstOccurrenceOf ("file://", false, true)));
  2080. dragAndDropFiles.trim();
  2081. dragAndDropFiles.removeEmptyStrings();
  2082. }
  2083. }
  2084. void updateDraggedFileList (const XClientMessageEvent* const clientMsg)
  2085. {
  2086. dragAndDropFiles.clear();
  2087. if (dragAndDropSourceWindow != None
  2088. && dragAndDropCurrentMimeType != 0)
  2089. {
  2090. dragAndDropTimestamp = clientMsg->data.l[2];
  2091. ScopedXLock xlock;
  2092. XConvertSelection (display,
  2093. Atoms::XdndSelection,
  2094. dragAndDropCurrentMimeType,
  2095. XInternAtom (display, "JXSelectionWindowProperty", 0),
  2096. windowH,
  2097. dragAndDropTimestamp);
  2098. }
  2099. }
  2100. StringArray dragAndDropFiles;
  2101. int dragAndDropTimestamp;
  2102. Point<int> lastDropPos;
  2103. Atom dragAndDropCurrentMimeType;
  2104. Window dragAndDropSourceWindow;
  2105. Array <Atom> srcMimeTypeAtomList;
  2106. static int pointerMap[5];
  2107. static Point<int> lastMousePos;
  2108. static void clearLastMousePos() noexcept
  2109. {
  2110. lastMousePos = Point<int> (0x100000, 0x100000);
  2111. }
  2112. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LinuxComponentPeer);
  2113. };
  2114. ModifierKeys LinuxComponentPeer::currentModifiers;
  2115. bool LinuxComponentPeer::isActiveApplication = false;
  2116. int LinuxComponentPeer::pointerMap[5];
  2117. Point<int> LinuxComponentPeer::lastMousePos;
  2118. //==============================================================================
  2119. bool Process::isForegroundProcess()
  2120. {
  2121. return LinuxComponentPeer::isActiveApplication;
  2122. }
  2123. //==============================================================================
  2124. void ModifierKeys::updateCurrentModifiers() noexcept
  2125. {
  2126. currentModifiers = LinuxComponentPeer::currentModifiers;
  2127. }
  2128. const ModifierKeys ModifierKeys::getCurrentModifiersRealtime() noexcept
  2129. {
  2130. Window root, child;
  2131. int x, y, winx, winy;
  2132. unsigned int mask;
  2133. int mouseMods = 0;
  2134. ScopedXLock xlock;
  2135. if (XQueryPointer (display, RootWindow (display, DefaultScreen (display)),
  2136. &root, &child, &x, &y, &winx, &winy, &mask) != False)
  2137. {
  2138. if ((mask & Button1Mask) != 0) mouseMods |= ModifierKeys::leftButtonModifier;
  2139. if ((mask & Button2Mask) != 0) mouseMods |= ModifierKeys::middleButtonModifier;
  2140. if ((mask & Button3Mask) != 0) mouseMods |= ModifierKeys::rightButtonModifier;
  2141. }
  2142. LinuxComponentPeer::currentModifiers = LinuxComponentPeer::currentModifiers.withoutMouseButtons().withFlags (mouseMods);
  2143. return LinuxComponentPeer::currentModifiers;
  2144. }
  2145. //==============================================================================
  2146. void Desktop::setKioskComponent (Component* kioskModeComponent, bool enableOrDisable, bool allowMenusAndBars)
  2147. {
  2148. if (enableOrDisable)
  2149. kioskModeComponent->setBounds (Desktop::getInstance().getMainMonitorArea (false));
  2150. }
  2151. //==============================================================================
  2152. ComponentPeer* Component::createNewPeer (int styleFlags, void* nativeWindowToAttachTo)
  2153. {
  2154. return new LinuxComponentPeer (this, styleFlags, (Window) nativeWindowToAttachTo);
  2155. }
  2156. //==============================================================================
  2157. // (this callback is hooked up in the messaging code)
  2158. void juce_windowMessageReceive (XEvent* event)
  2159. {
  2160. if (event->xany.window != None)
  2161. {
  2162. LinuxComponentPeer* const peer = LinuxComponentPeer::getPeerFor (event->xany.window);
  2163. if (ComponentPeer::isValidPeer (peer))
  2164. peer->handleWindowMessage (event);
  2165. }
  2166. else
  2167. {
  2168. switch (event->xany.type)
  2169. {
  2170. case KeymapNotify:
  2171. {
  2172. const XKeymapEvent* const keymapEvent = (const XKeymapEvent*) &event->xkeymap;
  2173. memcpy (Keys::keyStates, keymapEvent->key_vector, 32);
  2174. break;
  2175. }
  2176. default:
  2177. break;
  2178. }
  2179. }
  2180. }
  2181. //==============================================================================
  2182. void Desktop::getCurrentMonitorPositions (Array <Rectangle<int> >& monitorCoords, const bool /*clipToWorkArea*/)
  2183. {
  2184. if (display == 0)
  2185. return;
  2186. #if JUCE_USE_XINERAMA
  2187. int major_opcode, first_event, first_error;
  2188. ScopedXLock xlock;
  2189. if (XQueryExtension (display, "XINERAMA", &major_opcode, &first_event, &first_error))
  2190. {
  2191. typedef Bool (*tXineramaIsActive) (Display*);
  2192. typedef XineramaScreenInfo* (*tXineramaQueryScreens) (Display*, int*);
  2193. static tXineramaIsActive xXineramaIsActive = 0;
  2194. static tXineramaQueryScreens xXineramaQueryScreens = 0;
  2195. if (xXineramaIsActive == 0 || xXineramaQueryScreens == 0)
  2196. {
  2197. void* h = dlopen ("libXinerama.so", RTLD_GLOBAL | RTLD_NOW);
  2198. if (h == 0)
  2199. h = dlopen ("libXinerama.so.1", RTLD_GLOBAL | RTLD_NOW);
  2200. if (h != 0)
  2201. {
  2202. xXineramaIsActive = (tXineramaIsActive) dlsym (h, "XineramaIsActive");
  2203. xXineramaQueryScreens = (tXineramaQueryScreens) dlsym (h, "XineramaQueryScreens");
  2204. }
  2205. }
  2206. if (xXineramaIsActive != 0
  2207. && xXineramaQueryScreens != 0
  2208. && xXineramaIsActive (display))
  2209. {
  2210. int numMonitors = 0;
  2211. XineramaScreenInfo* const screens = xXineramaQueryScreens (display, &numMonitors);
  2212. if (screens != 0)
  2213. {
  2214. for (int i = numMonitors; --i >= 0;)
  2215. {
  2216. int index = screens[i].screen_number;
  2217. if (index >= 0)
  2218. {
  2219. while (monitorCoords.size() < index)
  2220. monitorCoords.add (Rectangle<int>());
  2221. monitorCoords.set (index, Rectangle<int> (screens[i].x_org,
  2222. screens[i].y_org,
  2223. screens[i].width,
  2224. screens[i].height));
  2225. }
  2226. }
  2227. XFree (screens);
  2228. }
  2229. }
  2230. }
  2231. if (monitorCoords.size() == 0)
  2232. #endif
  2233. {
  2234. Atom hints = XInternAtom (display, "_NET_WORKAREA", True);
  2235. if (hints != None)
  2236. {
  2237. const int numMonitors = ScreenCount (display);
  2238. for (int i = 0; i < numMonitors; ++i)
  2239. {
  2240. Window root = RootWindow (display, i);
  2241. unsigned long nitems, bytesLeft;
  2242. Atom actualType;
  2243. int actualFormat;
  2244. unsigned char* data = nullptr;
  2245. if (XGetWindowProperty (display, root, hints, 0, 4, False,
  2246. XA_CARDINAL, &actualType, &actualFormat, &nitems, &bytesLeft,
  2247. &data) == Success)
  2248. {
  2249. const long* const position = (const long*) data;
  2250. if (actualType == XA_CARDINAL && actualFormat == 32 && nitems == 4)
  2251. monitorCoords.add (Rectangle<int> (position[0], position[1],
  2252. position[2], position[3]));
  2253. XFree (data);
  2254. }
  2255. }
  2256. }
  2257. if (monitorCoords.size() == 0)
  2258. {
  2259. monitorCoords.add (Rectangle<int> (DisplayWidth (display, DefaultScreen (display)),
  2260. DisplayHeight (display, DefaultScreen (display))));
  2261. }
  2262. }
  2263. }
  2264. //==============================================================================
  2265. void Desktop::createMouseInputSources()
  2266. {
  2267. mouseSources.add (new MouseInputSource (0, true));
  2268. }
  2269. bool Desktop::canUseSemiTransparentWindows() noexcept
  2270. {
  2271. int matchedDepth = 0;
  2272. const int desiredDepth = 32;
  2273. return Visuals::findVisualFormat (desiredDepth, matchedDepth) != 0
  2274. && (matchedDepth == desiredDepth);
  2275. }
  2276. const Point<int> MouseInputSource::getCurrentMousePosition()
  2277. {
  2278. Window root, child;
  2279. int x, y, winx, winy;
  2280. unsigned int mask;
  2281. ScopedXLock xlock;
  2282. if (XQueryPointer (display,
  2283. RootWindow (display, DefaultScreen (display)),
  2284. &root, &child,
  2285. &x, &y, &winx, &winy, &mask) == False)
  2286. {
  2287. // Pointer not on the default screen
  2288. x = y = -1;
  2289. }
  2290. return Point<int> (x, y);
  2291. }
  2292. void Desktop::setMousePosition (const Point<int>& newPosition)
  2293. {
  2294. ScopedXLock xlock;
  2295. Window root = RootWindow (display, DefaultScreen (display));
  2296. XWarpPointer (display, None, root, 0, 0, 0, 0, newPosition.getX(), newPosition.getY());
  2297. }
  2298. Desktop::DisplayOrientation Desktop::getCurrentOrientation() const
  2299. {
  2300. return upright;
  2301. }
  2302. //==============================================================================
  2303. static bool screenSaverAllowed = true;
  2304. void Desktop::setScreenSaverEnabled (const bool isEnabled)
  2305. {
  2306. if (screenSaverAllowed != isEnabled)
  2307. {
  2308. screenSaverAllowed = isEnabled;
  2309. typedef void (*tXScreenSaverSuspend) (Display*, Bool);
  2310. static tXScreenSaverSuspend xScreenSaverSuspend = 0;
  2311. if (xScreenSaverSuspend == 0)
  2312. {
  2313. void* h = dlopen ("libXss.so", RTLD_GLOBAL | RTLD_NOW);
  2314. if (h != 0)
  2315. xScreenSaverSuspend = (tXScreenSaverSuspend) dlsym (h, "XScreenSaverSuspend");
  2316. }
  2317. ScopedXLock xlock;
  2318. if (xScreenSaverSuspend != 0)
  2319. xScreenSaverSuspend (display, ! isEnabled);
  2320. }
  2321. }
  2322. bool Desktop::isScreenSaverEnabled()
  2323. {
  2324. return screenSaverAllowed;
  2325. }
  2326. //==============================================================================
  2327. void* MouseCursor::createMouseCursorFromImage (const Image& image, int hotspotX, int hotspotY)
  2328. {
  2329. ScopedXLock xlock;
  2330. const unsigned int imageW = image.getWidth();
  2331. const unsigned int imageH = image.getHeight();
  2332. #if JUCE_USE_XCURSOR
  2333. {
  2334. typedef XcursorBool (*tXcursorSupportsARGB) (Display*);
  2335. typedef XcursorImage* (*tXcursorImageCreate) (int, int);
  2336. typedef void (*tXcursorImageDestroy) (XcursorImage*);
  2337. typedef Cursor (*tXcursorImageLoadCursor) (Display*, const XcursorImage*);
  2338. static tXcursorSupportsARGB xXcursorSupportsARGB = 0;
  2339. static tXcursorImageCreate xXcursorImageCreate = 0;
  2340. static tXcursorImageDestroy xXcursorImageDestroy = 0;
  2341. static tXcursorImageLoadCursor xXcursorImageLoadCursor = 0;
  2342. static bool hasBeenLoaded = false;
  2343. if (! hasBeenLoaded)
  2344. {
  2345. hasBeenLoaded = true;
  2346. void* h = dlopen ("libXcursor.so", RTLD_GLOBAL | RTLD_NOW);
  2347. if (h != 0)
  2348. {
  2349. xXcursorSupportsARGB = (tXcursorSupportsARGB) dlsym (h, "XcursorSupportsARGB");
  2350. xXcursorImageCreate = (tXcursorImageCreate) dlsym (h, "XcursorImageCreate");
  2351. xXcursorImageLoadCursor = (tXcursorImageLoadCursor) dlsym (h, "XcursorImageLoadCursor");
  2352. xXcursorImageDestroy = (tXcursorImageDestroy) dlsym (h, "XcursorImageDestroy");
  2353. if (xXcursorSupportsARGB == 0 || xXcursorImageCreate == 0
  2354. || xXcursorImageLoadCursor == 0 || xXcursorImageDestroy == 0
  2355. || ! xXcursorSupportsARGB (display))
  2356. xXcursorSupportsARGB = 0;
  2357. }
  2358. }
  2359. if (xXcursorSupportsARGB != 0)
  2360. {
  2361. XcursorImage* xcImage = xXcursorImageCreate (imageW, imageH);
  2362. if (xcImage != 0)
  2363. {
  2364. xcImage->xhot = hotspotX;
  2365. xcImage->yhot = hotspotY;
  2366. XcursorPixel* dest = xcImage->pixels;
  2367. for (int y = 0; y < (int) imageH; ++y)
  2368. for (int x = 0; x < (int) imageW; ++x)
  2369. *dest++ = image.getPixelAt (x, y).getARGB();
  2370. void* result = (void*) xXcursorImageLoadCursor (display, xcImage);
  2371. xXcursorImageDestroy (xcImage);
  2372. if (result != 0)
  2373. return result;
  2374. }
  2375. }
  2376. }
  2377. #endif
  2378. Window root = RootWindow (display, DefaultScreen (display));
  2379. unsigned int cursorW, cursorH;
  2380. if (! XQueryBestCursor (display, root, imageW, imageH, &cursorW, &cursorH))
  2381. return nullptr;
  2382. Image im (Image::ARGB, cursorW, cursorH, true);
  2383. {
  2384. Graphics g (im);
  2385. if (imageW > cursorW || imageH > cursorH)
  2386. {
  2387. hotspotX = (hotspotX * cursorW) / imageW;
  2388. hotspotY = (hotspotY * cursorH) / imageH;
  2389. g.drawImageWithin (image, 0, 0, imageW, imageH,
  2390. RectanglePlacement::xLeft | RectanglePlacement::yTop | RectanglePlacement::onlyReduceInSize,
  2391. false);
  2392. }
  2393. else
  2394. {
  2395. g.drawImageAt (image, 0, 0);
  2396. }
  2397. }
  2398. const int stride = (cursorW + 7) >> 3;
  2399. HeapBlock <char> maskPlane, sourcePlane;
  2400. maskPlane.calloc (stride * cursorH);
  2401. sourcePlane.calloc (stride * cursorH);
  2402. const bool msbfirst = (BitmapBitOrder (display) == MSBFirst);
  2403. for (int y = cursorH; --y >= 0;)
  2404. {
  2405. for (int x = cursorW; --x >= 0;)
  2406. {
  2407. const char mask = (char) (1 << (msbfirst ? (7 - (x & 7)) : (x & 7)));
  2408. const int offset = y * stride + (x >> 3);
  2409. const Colour c (im.getPixelAt (x, y));
  2410. if (c.getAlpha() >= 128)
  2411. maskPlane[offset] |= mask;
  2412. if (c.getBrightness() >= 0.5f)
  2413. sourcePlane[offset] |= mask;
  2414. }
  2415. }
  2416. Pixmap sourcePixmap = XCreatePixmapFromBitmapData (display, root, sourcePlane.getData(), cursorW, cursorH, 0xffff, 0, 1);
  2417. Pixmap maskPixmap = XCreatePixmapFromBitmapData (display, root, maskPlane.getData(), cursorW, cursorH, 0xffff, 0, 1);
  2418. XColor white, black;
  2419. black.red = black.green = black.blue = 0;
  2420. white.red = white.green = white.blue = 0xffff;
  2421. void* result = (void*) XCreatePixmapCursor (display, sourcePixmap, maskPixmap, &white, &black, hotspotX, hotspotY);
  2422. XFreePixmap (display, sourcePixmap);
  2423. XFreePixmap (display, maskPixmap);
  2424. return result;
  2425. }
  2426. void MouseCursor::deleteMouseCursor (void* const cursorHandle, const bool)
  2427. {
  2428. ScopedXLock xlock;
  2429. if (cursorHandle != 0)
  2430. XFreeCursor (display, (Cursor) cursorHandle);
  2431. }
  2432. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType type)
  2433. {
  2434. unsigned int shape;
  2435. switch (type)
  2436. {
  2437. case NormalCursor: return None; // Use parent cursor
  2438. case NoCursor: return createMouseCursorFromImage (Image (Image::ARGB, 16, 16, true), 0, 0);
  2439. case WaitCursor: shape = XC_watch; break;
  2440. case IBeamCursor: shape = XC_xterm; break;
  2441. case PointingHandCursor: shape = XC_hand2; break;
  2442. case LeftRightResizeCursor: shape = XC_sb_h_double_arrow; break;
  2443. case UpDownResizeCursor: shape = XC_sb_v_double_arrow; break;
  2444. case UpDownLeftRightResizeCursor: shape = XC_fleur; break;
  2445. case TopEdgeResizeCursor: shape = XC_top_side; break;
  2446. case BottomEdgeResizeCursor: shape = XC_bottom_side; break;
  2447. case LeftEdgeResizeCursor: shape = XC_left_side; break;
  2448. case RightEdgeResizeCursor: shape = XC_right_side; break;
  2449. case TopLeftCornerResizeCursor: shape = XC_top_left_corner; break;
  2450. case TopRightCornerResizeCursor: shape = XC_top_right_corner; break;
  2451. case BottomLeftCornerResizeCursor: shape = XC_bottom_left_corner; break;
  2452. case BottomRightCornerResizeCursor: shape = XC_bottom_right_corner; break;
  2453. case CrosshairCursor: shape = XC_crosshair; break;
  2454. case DraggingHandCursor:
  2455. {
  2456. static unsigned char dragHandData[] = { 71,73,70,56,57,97,16,0,16,0,145,2,0,0,0,0,255,255,255,0,
  2457. 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,
  2458. 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 };
  2459. const int dragHandDataSize = 99;
  2460. return createMouseCursorFromImage (ImageFileFormat::loadFrom (dragHandData, dragHandDataSize), 8, 7);
  2461. }
  2462. case CopyingCursor:
  2463. {
  2464. static unsigned char copyCursorData[] = { 71,73,70,56,57,97,21,0,21,0,145,0,0,0,0,0,255,255,255,0,
  2465. 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,
  2466. 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,
  2467. 252,114,147,74,83,5,50,68,147,208,217,16,71,149,252,124,5,0,59,0,0 };
  2468. const int copyCursorSize = 119;
  2469. return createMouseCursorFromImage (ImageFileFormat::loadFrom (copyCursorData, copyCursorSize), 1, 3);
  2470. }
  2471. default:
  2472. jassertfalse;
  2473. return None;
  2474. }
  2475. ScopedXLock xlock;
  2476. return (void*) XCreateFontCursor (display, shape);
  2477. }
  2478. void MouseCursor::showInWindow (ComponentPeer* peer) const
  2479. {
  2480. LinuxComponentPeer* const lp = dynamic_cast <LinuxComponentPeer*> (peer);
  2481. if (lp != 0)
  2482. lp->showMouseCursor ((Cursor) getHandle());
  2483. }
  2484. void MouseCursor::showInAllWindows() const
  2485. {
  2486. for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
  2487. showInWindow (ComponentPeer::getPeer (i));
  2488. }
  2489. //==============================================================================
  2490. const Image juce_createIconForFile (const File& file)
  2491. {
  2492. return Image::null;
  2493. }
  2494. Image::SharedImage* Image::SharedImage::createNativeImage (PixelFormat format, int width, int height, bool clearImage)
  2495. {
  2496. return createSoftwareImage (format, width, height, clearImage);
  2497. }
  2498. //==============================================================================
  2499. #if JUCE_OPENGL
  2500. //==============================================================================
  2501. class WindowedGLContext : public OpenGLContext
  2502. {
  2503. public:
  2504. WindowedGLContext (Component* const component,
  2505. const OpenGLPixelFormat& pixelFormat_,
  2506. GLXContext sharedContext)
  2507. : renderContext (0),
  2508. embeddedWindow (0),
  2509. pixelFormat (pixelFormat_),
  2510. swapInterval (0)
  2511. {
  2512. jassert (component != nullptr);
  2513. LinuxComponentPeer* const peer = dynamic_cast <LinuxComponentPeer*> (component->getTopLevelComponent()->getPeer());
  2514. if (peer == nullptr)
  2515. return;
  2516. ScopedXLock xlock;
  2517. XSync (display, False);
  2518. GLint attribs [64];
  2519. int n = 0;
  2520. attribs[n++] = GLX_RGBA;
  2521. attribs[n++] = GLX_DOUBLEBUFFER;
  2522. attribs[n++] = GLX_RED_SIZE;
  2523. attribs[n++] = pixelFormat.redBits;
  2524. attribs[n++] = GLX_GREEN_SIZE;
  2525. attribs[n++] = pixelFormat.greenBits;
  2526. attribs[n++] = GLX_BLUE_SIZE;
  2527. attribs[n++] = pixelFormat.blueBits;
  2528. attribs[n++] = GLX_ALPHA_SIZE;
  2529. attribs[n++] = pixelFormat.alphaBits;
  2530. attribs[n++] = GLX_DEPTH_SIZE;
  2531. attribs[n++] = pixelFormat.depthBufferBits;
  2532. attribs[n++] = GLX_STENCIL_SIZE;
  2533. attribs[n++] = pixelFormat.stencilBufferBits;
  2534. attribs[n++] = GLX_ACCUM_RED_SIZE;
  2535. attribs[n++] = pixelFormat.accumulationBufferRedBits;
  2536. attribs[n++] = GLX_ACCUM_GREEN_SIZE;
  2537. attribs[n++] = pixelFormat.accumulationBufferGreenBits;
  2538. attribs[n++] = GLX_ACCUM_BLUE_SIZE;
  2539. attribs[n++] = pixelFormat.accumulationBufferBlueBits;
  2540. attribs[n++] = GLX_ACCUM_ALPHA_SIZE;
  2541. attribs[n++] = pixelFormat.accumulationBufferAlphaBits;
  2542. // xxx not sure how to do fullSceneAntiAliasingNumSamples on linux..
  2543. attribs[n++] = None;
  2544. XVisualInfo* const bestVisual = glXChooseVisual (display, DefaultScreen (display), attribs);
  2545. if (bestVisual == 0)
  2546. return;
  2547. renderContext = glXCreateContext (display, bestVisual, sharedContext, GL_TRUE);
  2548. Window windowH = (Window) peer->getNativeHandle();
  2549. Colormap colourMap = XCreateColormap (display, windowH, bestVisual->visual, AllocNone);
  2550. XSetWindowAttributes swa;
  2551. swa.colormap = colourMap;
  2552. swa.border_pixel = 0;
  2553. swa.event_mask = ExposureMask | StructureNotifyMask;
  2554. embeddedWindow = XCreateWindow (display, windowH,
  2555. 0, 0, 1, 1, 0,
  2556. bestVisual->depth,
  2557. InputOutput,
  2558. bestVisual->visual,
  2559. CWBorderPixel | CWColormap | CWEventMask,
  2560. &swa);
  2561. XSaveContext (display, (XID) embeddedWindow, windowHandleXContext, (XPointer) peer);
  2562. XMapWindow (display, embeddedWindow);
  2563. XFreeColormap (display, colourMap);
  2564. XFree (bestVisual);
  2565. XSync (display, False);
  2566. }
  2567. ~WindowedGLContext()
  2568. {
  2569. ScopedXLock xlock;
  2570. deleteContext();
  2571. XUnmapWindow (display, embeddedWindow);
  2572. XDestroyWindow (display, embeddedWindow);
  2573. }
  2574. void deleteContext()
  2575. {
  2576. makeInactive();
  2577. if (renderContext != 0)
  2578. {
  2579. ScopedXLock xlock;
  2580. glXDestroyContext (display, renderContext);
  2581. renderContext = nullptr;
  2582. }
  2583. }
  2584. bool makeActive() const noexcept
  2585. {
  2586. jassert (renderContext != 0);
  2587. ScopedXLock xlock;
  2588. return glXMakeCurrent (display, embeddedWindow, renderContext)
  2589. && XSync (display, False);
  2590. }
  2591. bool makeInactive() const noexcept
  2592. {
  2593. ScopedXLock xlock;
  2594. return (! isActive()) || glXMakeCurrent (display, None, 0);
  2595. }
  2596. bool isActive() const noexcept
  2597. {
  2598. ScopedXLock xlock;
  2599. return glXGetCurrentContext() == renderContext;
  2600. }
  2601. const OpenGLPixelFormat getPixelFormat() const
  2602. {
  2603. return pixelFormat;
  2604. }
  2605. void* getRawContext() const noexcept
  2606. {
  2607. return renderContext;
  2608. }
  2609. void updateWindowPosition (int x, int y, int w, int h, int)
  2610. {
  2611. ScopedXLock xlock;
  2612. XMoveResizeWindow (display, embeddedWindow,
  2613. x, y, jmax (1, w), jmax (1, h));
  2614. }
  2615. void swapBuffers()
  2616. {
  2617. ScopedXLock xlock;
  2618. glXSwapBuffers (display, embeddedWindow);
  2619. }
  2620. bool setSwapInterval (const int numFramesPerSwap)
  2621. {
  2622. static PFNGLXSWAPINTERVALSGIPROC GLXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC) glXGetProcAddress ((const GLubyte*) "glXSwapIntervalSGI");
  2623. if (GLXSwapIntervalSGI != 0)
  2624. {
  2625. swapInterval = numFramesPerSwap;
  2626. GLXSwapIntervalSGI (numFramesPerSwap);
  2627. return true;
  2628. }
  2629. return false;
  2630. }
  2631. int getSwapInterval() const
  2632. {
  2633. return swapInterval;
  2634. }
  2635. void repaint()
  2636. {
  2637. }
  2638. //==============================================================================
  2639. GLXContext renderContext;
  2640. private:
  2641. Window embeddedWindow;
  2642. OpenGLPixelFormat pixelFormat;
  2643. int swapInterval;
  2644. //==============================================================================
  2645. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowedGLContext);
  2646. };
  2647. //==============================================================================
  2648. OpenGLContext* OpenGLComponent::createContext()
  2649. {
  2650. ScopedPointer<WindowedGLContext> c (new WindowedGLContext (this, preferredPixelFormat,
  2651. contextToShareListsWith != 0 ? (GLXContext) contextToShareListsWith->getRawContext() : 0));
  2652. return (c->renderContext != 0) ? c.release() : nullptr;
  2653. }
  2654. void juce_glViewport (const int w, const int h)
  2655. {
  2656. glViewport (0, 0, w, h);
  2657. }
  2658. void OpenGLPixelFormat::getAvailablePixelFormats (Component* component,
  2659. OwnedArray <OpenGLPixelFormat>& results)
  2660. {
  2661. results.add (new OpenGLPixelFormat()); // xxx
  2662. }
  2663. #endif
  2664. //==============================================================================
  2665. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, const bool canMoveFiles)
  2666. {
  2667. jassertfalse; // not implemented!
  2668. return false;
  2669. }
  2670. bool DragAndDropContainer::performExternalDragDropOfText (const String& text)
  2671. {
  2672. jassertfalse; // not implemented!
  2673. return false;
  2674. }
  2675. //==============================================================================
  2676. void SystemTrayIconComponent::setIconImage (const Image& newImage)
  2677. {
  2678. if (! isOnDesktop ())
  2679. addToDesktop (0);
  2680. LinuxComponentPeer* const wp = dynamic_cast <LinuxComponentPeer*> (getPeer());
  2681. if (wp != 0)
  2682. {
  2683. wp->setTaskBarIcon (newImage);
  2684. setVisible (true);
  2685. toFront (false);
  2686. repaint();
  2687. }
  2688. }
  2689. void SystemTrayIconComponent::paint (Graphics& g)
  2690. {
  2691. LinuxComponentPeer* const wp = dynamic_cast <LinuxComponentPeer*> (getPeer());
  2692. if (wp != 0)
  2693. {
  2694. g.drawImageWithin (wp->getTaskbarIcon(), 0, 0, getWidth(), getHeight(),
  2695. RectanglePlacement::xLeft | RectanglePlacement::yTop | RectanglePlacement::onlyReduceInSize,
  2696. false);
  2697. }
  2698. }
  2699. void SystemTrayIconComponent::setIconTooltip (const String& tooltip)
  2700. {
  2701. // xxx not yet implemented!
  2702. }
  2703. //==============================================================================
  2704. void PlatformUtilities::beep()
  2705. {
  2706. std::cout << "\a" << std::flush;
  2707. }
  2708. //==============================================================================
  2709. void JUCE_CALLTYPE NativeMessageBox::showMessageBox (AlertWindow::AlertIconType iconType,
  2710. const String& title, const String& message,
  2711. Component* associatedComponent)
  2712. {
  2713. AlertWindow::showMessageBox (AlertWindow::NoIcon, title, message);
  2714. }
  2715. void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (AlertWindow::AlertIconType iconType,
  2716. const String& title, const String& message,
  2717. Component* associatedComponent)
  2718. {
  2719. AlertWindow::showMessageBoxAsync (AlertWindow::NoIcon, title, message);
  2720. }
  2721. bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (AlertWindow::AlertIconType iconType,
  2722. const String& title, const String& message,
  2723. Component* associatedComponent,
  2724. ModalComponentManager::Callback* callback)
  2725. {
  2726. return AlertWindow::showOkCancelBox (iconType, title, message, String::empty, String::empty,
  2727. associatedComponent, callback);
  2728. }
  2729. int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (AlertWindow::AlertIconType iconType,
  2730. const String& title, const String& message,
  2731. Component* associatedComponent,
  2732. ModalComponentManager::Callback* callback)
  2733. {
  2734. return AlertWindow::showYesNoCancelBox (iconType, title, message,
  2735. String::empty, String::empty, String::empty,
  2736. associatedComponent, callback);
  2737. }
  2738. //==============================================================================
  2739. const int KeyPress::spaceKey = XK_space & 0xff;
  2740. const int KeyPress::returnKey = XK_Return & 0xff;
  2741. const int KeyPress::escapeKey = XK_Escape & 0xff;
  2742. const int KeyPress::backspaceKey = XK_BackSpace & 0xff;
  2743. const int KeyPress::leftKey = (XK_Left & 0xff) | Keys::extendedKeyModifier;
  2744. const int KeyPress::rightKey = (XK_Right & 0xff) | Keys::extendedKeyModifier;
  2745. const int KeyPress::upKey = (XK_Up & 0xff) | Keys::extendedKeyModifier;
  2746. const int KeyPress::downKey = (XK_Down & 0xff) | Keys::extendedKeyModifier;
  2747. const int KeyPress::pageUpKey = (XK_Page_Up & 0xff) | Keys::extendedKeyModifier;
  2748. const int KeyPress::pageDownKey = (XK_Page_Down & 0xff) | Keys::extendedKeyModifier;
  2749. const int KeyPress::endKey = (XK_End & 0xff) | Keys::extendedKeyModifier;
  2750. const int KeyPress::homeKey = (XK_Home & 0xff) | Keys::extendedKeyModifier;
  2751. const int KeyPress::insertKey = (XK_Insert & 0xff) | Keys::extendedKeyModifier;
  2752. const int KeyPress::deleteKey = (XK_Delete & 0xff) | Keys::extendedKeyModifier;
  2753. const int KeyPress::tabKey = XK_Tab & 0xff;
  2754. const int KeyPress::F1Key = (XK_F1 & 0xff) | Keys::extendedKeyModifier;
  2755. const int KeyPress::F2Key = (XK_F2 & 0xff) | Keys::extendedKeyModifier;
  2756. const int KeyPress::F3Key = (XK_F3 & 0xff) | Keys::extendedKeyModifier;
  2757. const int KeyPress::F4Key = (XK_F4 & 0xff) | Keys::extendedKeyModifier;
  2758. const int KeyPress::F5Key = (XK_F5 & 0xff) | Keys::extendedKeyModifier;
  2759. const int KeyPress::F6Key = (XK_F6 & 0xff) | Keys::extendedKeyModifier;
  2760. const int KeyPress::F7Key = (XK_F7 & 0xff) | Keys::extendedKeyModifier;
  2761. const int KeyPress::F8Key = (XK_F8 & 0xff) | Keys::extendedKeyModifier;
  2762. const int KeyPress::F9Key = (XK_F9 & 0xff) | Keys::extendedKeyModifier;
  2763. const int KeyPress::F10Key = (XK_F10 & 0xff) | Keys::extendedKeyModifier;
  2764. const int KeyPress::F11Key = (XK_F11 & 0xff) | Keys::extendedKeyModifier;
  2765. const int KeyPress::F12Key = (XK_F12 & 0xff) | Keys::extendedKeyModifier;
  2766. const int KeyPress::F13Key = (XK_F13 & 0xff) | Keys::extendedKeyModifier;
  2767. const int KeyPress::F14Key = (XK_F14 & 0xff) | Keys::extendedKeyModifier;
  2768. const int KeyPress::F15Key = (XK_F15 & 0xff) | Keys::extendedKeyModifier;
  2769. const int KeyPress::F16Key = (XK_F16 & 0xff) | Keys::extendedKeyModifier;
  2770. const int KeyPress::numberPad0 = (XK_KP_0 & 0xff) | Keys::extendedKeyModifier;
  2771. const int KeyPress::numberPad1 = (XK_KP_1 & 0xff) | Keys::extendedKeyModifier;
  2772. const int KeyPress::numberPad2 = (XK_KP_2 & 0xff) | Keys::extendedKeyModifier;
  2773. const int KeyPress::numberPad3 = (XK_KP_3 & 0xff) | Keys::extendedKeyModifier;
  2774. const int KeyPress::numberPad4 = (XK_KP_4 & 0xff) | Keys::extendedKeyModifier;
  2775. const int KeyPress::numberPad5 = (XK_KP_5 & 0xff) | Keys::extendedKeyModifier;
  2776. const int KeyPress::numberPad6 = (XK_KP_6 & 0xff) | Keys::extendedKeyModifier;
  2777. const int KeyPress::numberPad7 = (XK_KP_7 & 0xff)| Keys::extendedKeyModifier;
  2778. const int KeyPress::numberPad8 = (XK_KP_8 & 0xff)| Keys::extendedKeyModifier;
  2779. const int KeyPress::numberPad9 = (XK_KP_9 & 0xff)| Keys::extendedKeyModifier;
  2780. const int KeyPress::numberPadAdd = (XK_KP_Add & 0xff)| Keys::extendedKeyModifier;
  2781. const int KeyPress::numberPadSubtract = (XK_KP_Subtract & 0xff)| Keys::extendedKeyModifier;
  2782. const int KeyPress::numberPadMultiply = (XK_KP_Multiply & 0xff)| Keys::extendedKeyModifier;
  2783. const int KeyPress::numberPadDivide = (XK_KP_Divide & 0xff)| Keys::extendedKeyModifier;
  2784. const int KeyPress::numberPadSeparator = (XK_KP_Separator & 0xff)| Keys::extendedKeyModifier;
  2785. const int KeyPress::numberPadDecimalPoint = (XK_KP_Decimal & 0xff)| Keys::extendedKeyModifier;
  2786. const int KeyPress::numberPadEquals = (XK_KP_Equal & 0xff)| Keys::extendedKeyModifier;
  2787. const int KeyPress::numberPadDelete = (XK_KP_Delete & 0xff)| Keys::extendedKeyModifier;
  2788. const int KeyPress::playKey = (0xffeeff00) | Keys::extendedKeyModifier;
  2789. const int KeyPress::stopKey = (0xffeeff01) | Keys::extendedKeyModifier;
  2790. const int KeyPress::fastForwardKey = (0xffeeff02) | Keys::extendedKeyModifier;
  2791. const int KeyPress::rewindKey = (0xffeeff03) | Keys::extendedKeyModifier;
  2792. #endif