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.

3089 lines
102KB

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