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.

281 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. extern ::Window juce_messageWindowHandle;
  22. namespace ClipboardHelpers
  23. {
  24. static String localClipboardContent;
  25. static Atom atom_UTF8_STRING;
  26. static Atom atom_CLIPBOARD;
  27. static Atom atom_TARGETS;
  28. //==============================================================================
  29. static void initSelectionAtoms (::Display* display)
  30. {
  31. static bool isInitialised = false;
  32. if (! isInitialised)
  33. {
  34. isInitialised = true;
  35. atom_UTF8_STRING = Atoms::getCreating (display, "UTF8_STRING");
  36. atom_CLIPBOARD = Atoms::getCreating (display, "CLIPBOARD");
  37. atom_TARGETS = Atoms::getCreating (display, "TARGETS");
  38. }
  39. }
  40. //==============================================================================
  41. // Read the content of a window property as either a locale-dependent string or an utf8 string
  42. // works only for strings shorter than 1000000 bytes
  43. static String readWindowProperty (::Display* display, Window window, Atom prop)
  44. {
  45. String returnData;
  46. if (display != nullptr)
  47. {
  48. char* clipData;
  49. Atom actualType;
  50. int actualFormat;
  51. unsigned long numItems, bytesLeft;
  52. if (XGetWindowProperty (display, window, prop,
  53. 0L /* offset */, 1000000 /* length (max) */, False,
  54. AnyPropertyType /* format */,
  55. &actualType, &actualFormat, &numItems, &bytesLeft,
  56. (unsigned char**) &clipData) == Success)
  57. {
  58. if (actualType == atom_UTF8_STRING && actualFormat == 8)
  59. returnData = String::fromUTF8 (clipData, (int) numItems);
  60. else if (actualType == XA_STRING && actualFormat == 8)
  61. returnData = String (clipData, numItems);
  62. if (clipData != nullptr)
  63. XFree (clipData);
  64. jassert (bytesLeft == 0 || numItems == 1000000);
  65. }
  66. XDeleteProperty (display, window, prop);
  67. }
  68. return returnData;
  69. }
  70. //==============================================================================
  71. // Send a SelectionRequest to the window owning the selection and waits for its answer (with a timeout) */
  72. static bool requestSelectionContent (::Display* display, String& selectionContent,
  73. Atom selection, Atom requestedFormat)
  74. {
  75. Atom property_name = XInternAtom (display, "JUCE_SEL", false);
  76. // The selection owner will be asked to set the JUCE_SEL property on the
  77. // juce_messageWindowHandle with the selection content
  78. XConvertSelection (display, selection, requestedFormat, property_name,
  79. juce_messageWindowHandle, CurrentTime);
  80. int count = 50; // will wait at most for 200 ms
  81. while (--count >= 0)
  82. {
  83. XEvent event;
  84. if (XCheckTypedWindowEvent (display, juce_messageWindowHandle, SelectionNotify, &event))
  85. {
  86. if (event.xselection.property == property_name)
  87. {
  88. jassert (event.xselection.requestor == juce_messageWindowHandle);
  89. selectionContent = readWindowProperty (display, event.xselection.requestor,
  90. event.xselection.property);
  91. return true;
  92. }
  93. return false; // the format we asked for was denied.. (event.xselection.property == None)
  94. }
  95. // not very elegant.. we could do a select() or something like that...
  96. // however clipboard content requesting is inherently slow on x11, it
  97. // often takes 50ms or more so...
  98. Thread::sleep (4);
  99. }
  100. return false;
  101. }
  102. //==============================================================================
  103. // Called from the event loop in juce_linux_Messaging in response to SelectionRequest events
  104. static void handleSelection (XSelectionRequestEvent& evt)
  105. {
  106. ClipboardHelpers::initSelectionAtoms (evt.display);
  107. // the selection content is sent to the target window as a window property
  108. XSelectionEvent reply;
  109. reply.type = SelectionNotify;
  110. reply.display = evt.display;
  111. reply.requestor = evt.requestor;
  112. reply.selection = evt.selection;
  113. reply.target = evt.target;
  114. reply.property = None; // == "fail"
  115. reply.time = evt.time;
  116. HeapBlock<char> data;
  117. int propertyFormat = 0;
  118. size_t numDataItems = 0;
  119. if (evt.selection == XA_PRIMARY || evt.selection == ClipboardHelpers::atom_CLIPBOARD)
  120. {
  121. if (evt.target == XA_STRING || evt.target == ClipboardHelpers::atom_UTF8_STRING)
  122. {
  123. // translate to utf8
  124. numDataItems = ClipboardHelpers::localClipboardContent.getNumBytesAsUTF8() + 1;
  125. data.calloc (numDataItems + 1);
  126. ClipboardHelpers::localClipboardContent.copyToUTF8 (data, numDataItems);
  127. propertyFormat = 8; // bits/item
  128. }
  129. else if (evt.target == ClipboardHelpers::atom_TARGETS)
  130. {
  131. // another application wants to know what we are able to send
  132. numDataItems = 2;
  133. propertyFormat = 32; // atoms are 32-bit
  134. data.calloc (numDataItems * 4);
  135. Atom* atoms = reinterpret_cast<Atom*> (data.getData());
  136. atoms[0] = ClipboardHelpers::atom_UTF8_STRING;
  137. atoms[1] = XA_STRING;
  138. evt.target = XA_ATOM;
  139. }
  140. }
  141. else
  142. {
  143. DBG ("requested unsupported clipboard");
  144. }
  145. if (data != nullptr)
  146. {
  147. const size_t maxReasonableSelectionSize = 1000000;
  148. // for very big chunks of data, we should use the "INCR" protocol , which is a pain in the *ss
  149. if (evt.property != None && numDataItems < maxReasonableSelectionSize)
  150. {
  151. XChangeProperty (evt.display, evt.requestor,
  152. evt.property, evt.target,
  153. propertyFormat /* 8 or 32 */, PropModeReplace,
  154. reinterpret_cast<const unsigned char*> (data.getData()), (int) numDataItems);
  155. reply.property = evt.property; // " == success"
  156. }
  157. }
  158. XSendEvent (evt.display, evt.requestor, 0, NoEventMask, (XEvent*) &reply);
  159. }
  160. }
  161. //==============================================================================
  162. typedef void (*SelectionRequestCallback) (XSelectionRequestEvent&);
  163. extern SelectionRequestCallback handleSelectionRequest;
  164. struct ClipboardCallbackInitialiser
  165. {
  166. ClipboardCallbackInitialiser()
  167. {
  168. handleSelectionRequest = ClipboardHelpers::handleSelection;
  169. }
  170. };
  171. static ClipboardCallbackInitialiser clipboardInitialiser;
  172. //==============================================================================
  173. void SystemClipboard::copyTextToClipboard (const String& clipText)
  174. {
  175. ScopedXDisplay xDisplay;
  176. if (auto display = xDisplay.display)
  177. {
  178. ClipboardHelpers::initSelectionAtoms (display);
  179. ClipboardHelpers::localClipboardContent = clipText;
  180. XSetSelectionOwner (display, XA_PRIMARY, juce_messageWindowHandle, CurrentTime);
  181. XSetSelectionOwner (display, ClipboardHelpers::atom_CLIPBOARD, juce_messageWindowHandle, CurrentTime);
  182. }
  183. }
  184. String SystemClipboard::getTextFromClipboard()
  185. {
  186. String content;
  187. ScopedXDisplay xDisplay;
  188. if (auto display = xDisplay.display)
  189. {
  190. ClipboardHelpers::initSelectionAtoms (display);
  191. /* 1) try to read from the "CLIPBOARD" selection first (the "high
  192. level" clipboard that is supposed to be filled by ctrl-C
  193. etc). When a clipboard manager is running, the content of this
  194. selection is preserved even when the original selection owner
  195. exits.
  196. 2) and then try to read from "PRIMARY" selection (the "legacy" selection
  197. filled by good old x11 apps such as xterm)
  198. */
  199. Atom selection = XA_PRIMARY;
  200. Window selectionOwner = None;
  201. if ((selectionOwner = XGetSelectionOwner (display, selection)) == None)
  202. {
  203. selection = ClipboardHelpers::atom_CLIPBOARD;
  204. selectionOwner = XGetSelectionOwner (display, selection);
  205. }
  206. if (selectionOwner != None)
  207. {
  208. if (selectionOwner == juce_messageWindowHandle)
  209. {
  210. content = ClipboardHelpers::localClipboardContent;
  211. }
  212. else
  213. {
  214. // first try: we want an utf8 string
  215. bool ok = ClipboardHelpers::requestSelectionContent (display, content,
  216. selection, ClipboardHelpers::atom_UTF8_STRING);
  217. if (! ok)
  218. {
  219. // second chance, ask for a good old locale-dependent string ..
  220. ok = ClipboardHelpers::requestSelectionContent (display, content,
  221. selection, XA_STRING);
  222. }
  223. }
  224. }
  225. }
  226. return content;
  227. }
  228. } // namespace juce