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.

273 lines
10KB

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