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.

3311 lines
110KB

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