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.

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