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.

240 lines
9.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. extern Display* display;
  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. void initSelectionAtoms()
  28. {
  29. static bool isInitialised = false;
  30. if (! isInitialised)
  31. {
  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. String readWindowProperty (Window window, Atom prop, Atom fmt)
  41. {
  42. String returnData;
  43. char* clipData;
  44. Atom actualType;
  45. int actualFormat;
  46. unsigned long numItems, bytesLeft;
  47. if (XGetWindowProperty (display, window, prop,
  48. 0L /* offset */, 1000000 /* length (max) */, False,
  49. AnyPropertyType /* format */,
  50. &actualType, &actualFormat, &numItems, &bytesLeft,
  51. (unsigned char**) &clipData) == Success)
  52. {
  53. if (actualType == atom_UTF8_STRING && actualFormat == 8)
  54. returnData = String::fromUTF8 (clipData, numItems);
  55. else if (actualType == XA_STRING && actualFormat == 8)
  56. returnData = String (clipData, numItems);
  57. if (clipData != nullptr)
  58. XFree (clipData);
  59. jassert (bytesLeft == 0 || numItems == 1000000);
  60. }
  61. XDeleteProperty (display, window, prop);
  62. return returnData;
  63. }
  64. //==============================================================================
  65. // Send a SelectionRequest to the window owning the selection and waits for its answer (with a timeout) */
  66. bool requestSelectionContent (String& selectionContent, Atom selection, Atom requestedFormat)
  67. {
  68. Atom property_name = XInternAtom (display, "JUCE_SEL", false);
  69. // The selection owner will be asked to set the JUCE_SEL property on the
  70. // juce_messageWindowHandle with the selection content
  71. XConvertSelection (display, selection, requestedFormat, property_name,
  72. juce_messageWindowHandle, CurrentTime);
  73. int count = 50; // will wait at most for 200 ms
  74. while (--count >= 0)
  75. {
  76. XEvent event;
  77. if (XCheckTypedWindowEvent (display, juce_messageWindowHandle, SelectionNotify, &event))
  78. {
  79. if (event.xselection.property == property_name)
  80. {
  81. jassert (event.xselection.requestor == juce_messageWindowHandle);
  82. selectionContent = readWindowProperty (event.xselection.requestor,
  83. event.xselection.property,
  84. requestedFormat);
  85. return true;
  86. }
  87. else
  88. {
  89. return false; // the format we asked for was denied.. (event.xselection.property == None)
  90. }
  91. }
  92. // not very elegant.. we could do a select() or something like that...
  93. // however clipboard content requesting is inherently slow on x11, it
  94. // often takes 50ms or more so...
  95. Thread::sleep (4);
  96. }
  97. return false;
  98. }
  99. }
  100. //==============================================================================
  101. // Called from the event loop in juce_linux_Messaging in response to SelectionRequest events
  102. void juce_handleSelectionRequest (XSelectionRequestEvent &evt)
  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, numDataItems = 0;
  116. if (evt.selection == XA_PRIMARY || evt.selection == ClipboardHelpers::atom_CLIPBOARD)
  117. {
  118. if (evt.target == XA_STRING || evt.target == ClipboardHelpers::atom_UTF8_STRING)
  119. {
  120. // translate to utf8
  121. numDataItems = ClipboardHelpers::localClipboardContent.getNumBytesAsUTF8() + 1;
  122. data.calloc (numDataItems + 1);
  123. ClipboardHelpers::localClipboardContent.copyToUTF8 (data, numDataItems);
  124. propertyFormat = 8; // bits/item
  125. }
  126. else if (evt.target == ClipboardHelpers::atom_TARGETS)
  127. {
  128. // another application wants to know what we are able to send
  129. numDataItems = 2;
  130. propertyFormat = 32; // atoms are 32-bit
  131. data.calloc (numDataItems * 4);
  132. Atom* atoms = reinterpret_cast<Atom*> (data.getData());
  133. atoms[0] = ClipboardHelpers::atom_UTF8_STRING;
  134. atoms[1] = XA_STRING;
  135. }
  136. }
  137. else
  138. {
  139. DBG ("requested unsupported clipboard");
  140. }
  141. if (data != nullptr)
  142. {
  143. const int 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()), numDataItems);
  151. reply.property = evt.property; // " == success"
  152. }
  153. }
  154. XSendEvent (evt.display, evt.requestor, 0, NoEventMask, (XEvent*) &reply);
  155. }
  156. //==============================================================================
  157. void SystemClipboard::copyTextToClipboard (const String& clipText)
  158. {
  159. ClipboardHelpers::initSelectionAtoms();
  160. ClipboardHelpers::localClipboardContent = clipText;
  161. XSetSelectionOwner (display, XA_PRIMARY, juce_messageWindowHandle, CurrentTime);
  162. XSetSelectionOwner (display, ClipboardHelpers::atom_CLIPBOARD, juce_messageWindowHandle, CurrentTime);
  163. }
  164. String SystemClipboard::getTextFromClipboard()
  165. {
  166. ClipboardHelpers::initSelectionAtoms();
  167. /* 1) try to read from the "CLIPBOARD" selection first (the "high
  168. level" clipboard that is supposed to be filled by ctrl-C
  169. etc). When a clipboard manager is running, the content of this
  170. selection is preserved even when the original selection owner
  171. exits.
  172. 2) and then try to read from "PRIMARY" selection (the "legacy" selection
  173. filled by good old x11 apps such as xterm)
  174. */
  175. String content;
  176. Atom selection = XA_PRIMARY;
  177. Window selectionOwner = None;
  178. if ((selectionOwner = XGetSelectionOwner (display, selection)) == None)
  179. {
  180. selection = ClipboardHelpers::atom_CLIPBOARD;
  181. selectionOwner = XGetSelectionOwner (display, selection);
  182. }
  183. if (selectionOwner != None)
  184. {
  185. if (selectionOwner == juce_messageWindowHandle)
  186. {
  187. content = ClipboardHelpers::localClipboardContent;
  188. }
  189. else
  190. {
  191. // first try: we want an utf8 string
  192. bool ok = ClipboardHelpers::requestSelectionContent (content, selection, ClipboardHelpers::atom_UTF8_STRING);
  193. if (! ok)
  194. {
  195. // second chance, ask for a good old locale-dependent string ..
  196. ok = ClipboardHelpers::requestSelectionContent (content, selection, XA_STRING);
  197. }
  198. }
  199. }
  200. return content;
  201. }