Audio plugin host https://kx.studio/carla
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.

juce_linux_Clipboard.cpp 10KB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 ::Display* display;
  18. extern ::Window juce_messageWindowHandle;
  19. namespace ClipboardHelpers
  20. {
  21. static String localClipboardContent;
  22. static Atom atom_UTF8_STRING;
  23. static Atom atom_CLIPBOARD;
  24. static Atom atom_TARGETS;
  25. //==============================================================================
  26. static void initSelectionAtoms()
  27. {
  28. static bool isInitialised = false;
  29. if (! isInitialised)
  30. {
  31. isInitialised = true;
  32. atom_UTF8_STRING = XInternAtom (display, "UTF8_STRING", False);
  33. atom_CLIPBOARD = XInternAtom (display, "CLIPBOARD", False);
  34. atom_TARGETS = XInternAtom (display, "TARGETS", False);
  35. }
  36. }
  37. //==============================================================================
  38. // Read the content of a window property as either a locale-dependent string or an utf8 string
  39. // works only for strings shorter than 1000000 bytes
  40. static String readWindowProperty (Window window, Atom prop)
  41. {
  42. String returnData;
  43. if (display != nullptr)
  44. {
  45. char* clipData;
  46. Atom actualType;
  47. int actualFormat;
  48. unsigned long numItems, bytesLeft;
  49. if (XGetWindowProperty (display, window, prop,
  50. 0L /* offset */, 1000000 /* length (max) */, False,
  51. AnyPropertyType /* format */,
  52. &actualType, &actualFormat, &numItems, &bytesLeft,
  53. (unsigned char**) &clipData) == Success)
  54. {
  55. if (actualType == atom_UTF8_STRING && actualFormat == 8)
  56. returnData = String::fromUTF8 (clipData, (int) numItems);
  57. else if (actualType == XA_STRING && actualFormat == 8)
  58. returnData = String (clipData, numItems);
  59. if (clipData != nullptr)
  60. XFree (clipData);
  61. jassert (bytesLeft == 0 || numItems == 1000000);
  62. }
  63. XDeleteProperty (display, window, prop);
  64. }
  65. return returnData;
  66. }
  67. //==============================================================================
  68. // Send a SelectionRequest to the window owning the selection and waits for its answer (with a timeout) */
  69. static bool requestSelectionContent (String& selectionContent, 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 (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. if (display != nullptr)
  103. {
  104. ClipboardHelpers::initSelectionAtoms();
  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. //==============================================================================
  161. typedef void (*SelectionRequestCallback) (XSelectionRequestEvent&);
  162. extern SelectionRequestCallback handleSelectionRequest;
  163. struct ClipboardCallbackInitialiser
  164. {
  165. ClipboardCallbackInitialiser()
  166. {
  167. handleSelectionRequest = ClipboardHelpers::handleSelection;
  168. }
  169. };
  170. static ClipboardCallbackInitialiser clipboardInitialiser;
  171. //==============================================================================
  172. void SystemClipboard::copyTextToClipboard (const String& clipText)
  173. {
  174. if (display != nullptr)
  175. {
  176. ClipboardHelpers::initSelectionAtoms();
  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. if (display != nullptr)
  186. {
  187. ClipboardHelpers::initSelectionAtoms();
  188. /* 1) try to read from the "CLIPBOARD" selection first (the "high
  189. level" clipboard that is supposed to be filled by ctrl-C
  190. etc). When a clipboard manager is running, the content of this
  191. selection is preserved even when the original selection owner
  192. exits.
  193. 2) and then try to read from "PRIMARY" selection (the "legacy" selection
  194. filled by good old x11 apps such as xterm)
  195. */
  196. Atom selection = XA_PRIMARY;
  197. Window selectionOwner = None;
  198. if ((selectionOwner = XGetSelectionOwner (display, selection)) == None)
  199. {
  200. selection = ClipboardHelpers::atom_CLIPBOARD;
  201. selectionOwner = XGetSelectionOwner (display, selection);
  202. }
  203. if (selectionOwner != None)
  204. {
  205. if (selectionOwner == juce_messageWindowHandle)
  206. {
  207. content = ClipboardHelpers::localClipboardContent;
  208. }
  209. else
  210. {
  211. // first try: we want an utf8 string
  212. bool ok = ClipboardHelpers::requestSelectionContent (content, selection, ClipboardHelpers::atom_UTF8_STRING);
  213. if (! ok)
  214. {
  215. // second chance, ask for a good old locale-dependent string ..
  216. ok = ClipboardHelpers::requestSelectionContent (content, selection, XA_STRING);
  217. }
  218. }
  219. }
  220. }
  221. return content;
  222. }