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.

247 lines
9.6KB

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