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.

3118 lines
103KB

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