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
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. extern ::Window juce_messageWindowHandle;
  18. namespace ClipboardHelpers
  19. {
  20. static String localClipboardContent;
  21. static Atom atom_UTF8_STRING;
  22. static Atom atom_CLIPBOARD;
  23. static Atom atom_TARGETS;
  24. //==============================================================================
  25. static void initSelectionAtoms (::Display* display)
  26. {
  27. static bool isInitialised = false;
  28. if (! isInitialised)
  29. {
  30. isInitialised = true;
  31. atom_UTF8_STRING = Atoms::getCreating (display, "UTF8_STRING");
  32. atom_CLIPBOARD = Atoms::getCreating (display, "CLIPBOARD");
  33. atom_TARGETS = Atoms::getCreating (display, "TARGETS");
  34. }
  35. }
  36. //==============================================================================
  37. // Read the content of a window property as either a locale-dependent string or an utf8 string
  38. // works only for strings shorter than 1000000 bytes
  39. static String readWindowProperty (::Display* display, Window window, Atom prop)
  40. {
  41. String returnData;
  42. if (display != nullptr)
  43. {
  44. char* clipData;
  45. Atom actualType;
  46. int actualFormat;
  47. unsigned long numItems, bytesLeft;
  48. if (XGetWindowProperty (display, window, prop,
  49. 0L /* offset */, 1000000 /* length (max) */, False,
  50. AnyPropertyType /* format */,
  51. &actualType, &actualFormat, &numItems, &bytesLeft,
  52. (unsigned char**) &clipData) == Success)
  53. {
  54. if (actualType == atom_UTF8_STRING && actualFormat == 8)
  55. returnData = String::fromUTF8 (clipData, (int) numItems);
  56. else if (actualType == XA_STRING && actualFormat == 8)
  57. returnData = String (clipData, numItems);
  58. if (clipData != nullptr)
  59. XFree (clipData);
  60. jassert (bytesLeft == 0 || numItems == 1000000);
  61. }
  62. XDeleteProperty (display, window, prop);
  63. }
  64. return returnData;
  65. }
  66. //==============================================================================
  67. // Send a SelectionRequest to the window owning the selection and waits for its answer (with a timeout) */
  68. static bool requestSelectionContent (::Display* display, String& selectionContent,
  69. Atom selection, Atom requestedFormat)
  70. {
  71. Atom property_name = XInternAtom (display, "JUCE_SEL", false);
  72. // The selection owner will be asked to set the JUCE_SEL property on the
  73. // juce_messageWindowHandle with the selection content
  74. XConvertSelection (display, selection, requestedFormat, property_name,
  75. juce_messageWindowHandle, CurrentTime);
  76. int count = 50; // will wait at most for 200 ms
  77. while (--count >= 0)
  78. {
  79. XEvent event;
  80. if (XCheckTypedWindowEvent (display, juce_messageWindowHandle, SelectionNotify, &event))
  81. {
  82. if (event.xselection.property == property_name)
  83. {
  84. jassert (event.xselection.requestor == juce_messageWindowHandle);
  85. selectionContent = readWindowProperty (display, event.xselection.requestor,
  86. event.xselection.property);
  87. return true;
  88. }
  89. return false; // the format we asked for was denied.. (event.xselection.property == None)
  90. }
  91. // not very elegant.. we could do a select() or something like that...
  92. // however clipboard content requesting is inherently slow on x11, it
  93. // often takes 50ms or more so...
  94. Thread::sleep (4);
  95. }
  96. return false;
  97. }
  98. //==============================================================================
  99. // Called from the event loop in juce_linux_Messaging in response to SelectionRequest events
  100. static void handleSelection (XSelectionRequestEvent& evt)
  101. {
  102. ClipboardHelpers::initSelectionAtoms (evt.display);
  103. // the selection content is sent to the target window as a window property
  104. XSelectionEvent reply;
  105. reply.type = SelectionNotify;
  106. reply.display = evt.display;
  107. reply.requestor = evt.requestor;
  108. reply.selection = evt.selection;
  109. reply.target = evt.target;
  110. reply.property = None; // == "fail"
  111. reply.time = evt.time;
  112. HeapBlock<char> data;
  113. int propertyFormat = 0;
  114. size_t numDataItems = 0;
  115. if (evt.selection == XA_PRIMARY || evt.selection == ClipboardHelpers::atom_CLIPBOARD)
  116. {
  117. if (evt.target == XA_STRING || evt.target == ClipboardHelpers::atom_UTF8_STRING)
  118. {
  119. // translate to utf8
  120. numDataItems = ClipboardHelpers::localClipboardContent.getNumBytesAsUTF8() + 1;
  121. data.calloc (numDataItems + 1);
  122. ClipboardHelpers::localClipboardContent.copyToUTF8 (data, numDataItems);
  123. propertyFormat = 8; // bits/item
  124. }
  125. else if (evt.target == ClipboardHelpers::atom_TARGETS)
  126. {
  127. // another application wants to know what we are able to send
  128. numDataItems = 2;
  129. propertyFormat = 32; // atoms are 32-bit
  130. data.calloc (numDataItems * 4);
  131. Atom* atoms = reinterpret_cast<Atom*> (data.getData());
  132. atoms[0] = ClipboardHelpers::atom_UTF8_STRING;
  133. atoms[1] = XA_STRING;
  134. evt.target = XA_ATOM;
  135. }
  136. }
  137. else
  138. {
  139. DBG ("requested unsupported clipboard");
  140. }
  141. if (data != nullptr)
  142. {
  143. const size_t maxReasonableSelectionSize = 1000000;
  144. // for very big chunks of data, we should use the "INCR" protocol , which is a pain in the *ss
  145. if (evt.property != None && numDataItems < maxReasonableSelectionSize)
  146. {
  147. XChangeProperty (evt.display, evt.requestor,
  148. evt.property, evt.target,
  149. propertyFormat /* 8 or 32 */, PropModeReplace,
  150. reinterpret_cast<const unsigned char*> (data.getData()), (int) numDataItems);
  151. reply.property = evt.property; // " == success"
  152. }
  153. }
  154. XSendEvent (evt.display, evt.requestor, 0, NoEventMask, (XEvent*) &reply);
  155. }
  156. }
  157. //==============================================================================
  158. typedef void (*SelectionRequestCallback) (XSelectionRequestEvent&);
  159. extern SelectionRequestCallback handleSelectionRequest;
  160. struct ClipboardCallbackInitialiser
  161. {
  162. ClipboardCallbackInitialiser()
  163. {
  164. handleSelectionRequest = ClipboardHelpers::handleSelection;
  165. }
  166. };
  167. static ClipboardCallbackInitialiser clipboardInitialiser;
  168. //==============================================================================
  169. void SystemClipboard::copyTextToClipboard (const String& clipText)
  170. {
  171. ScopedXDisplay xDisplay;
  172. ::Display* display = xDisplay.get();
  173. if (display != nullptr)
  174. {
  175. ClipboardHelpers::initSelectionAtoms (display);
  176. ClipboardHelpers::localClipboardContent = clipText;
  177. XSetSelectionOwner (display, XA_PRIMARY, juce_messageWindowHandle, CurrentTime);
  178. XSetSelectionOwner (display, ClipboardHelpers::atom_CLIPBOARD, juce_messageWindowHandle, CurrentTime);
  179. }
  180. }
  181. String SystemClipboard::getTextFromClipboard()
  182. {
  183. String content;
  184. ScopedXDisplay xDisplay;
  185. ::Display* display = xDisplay.get();
  186. if (display != nullptr)
  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. }