32 # include "msvc/config.h"
33 #elif defined(HAVE_CONFIG_H)
43 PRAGMA48(GCC diagnostic ignored
"-Wshadow")
44 #include <SDL_clipboard.h>
49 #if defined(__APPLE__)
53 #include <Carbon/Carbon.h>
55 #include "render/graphics.h"
60 PRAGMA48(GCC diagnostic ignored
"-Wshadow")
61 #include <SDL_syswm.h>
64 #elif defined __native_client__
69 PRAGMA48(GCC diagnostic ignored
"-Wshadow")
70 #include <SDL_syswm.h>
79 char *buf = SDL_GetClipboardText();
82 text.insert(pos, buf);
92 return !SDL_SetClipboardText(text.c_str());
102 if (!OpenClipboard(
nullptr))
105 HANDLE h = GetClipboardData(CF_UNICODETEXT);
108 LPCWSTR
data =
static_cast<LPCWSTR
>(GlobalLock(h));
112 const size_t len = WideCharToMultiByte(CP_UTF8, 0,
data, -1,
113 nullptr, 0,
nullptr,
nullptr);
117 void *temp = calloc(len, 1);
118 if (WideCharToMultiByte(CP_UTF8, 0,
data, -1,
119 static_cast<LPSTR
>(temp), len,
nullptr,
nullptr))
121 text.insert(pos,
static_cast<char*
>(temp));
132 h = GetClipboardData(CF_TEXT);
136 const char *
const data =
static_cast<char*
>(GlobalLock(h));
139 text.insert(pos,
data);
153 const int wCharsLen = MultiByteToWideChar(CP_UTF8,
154 0, text.c_str(), -1,
nullptr, 0);
158 HANDLE h = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
160 WCHAR *
const out =
static_cast<WCHAR*
>(GlobalLock(h));
162 MultiByteToWideChar(CP_UTF8, 0, text.c_str(), -1, out, wCharsLen);
164 if (!OpenClipboard(
nullptr))
172 if (!SetClipboardData(CF_UNICODETEXT, out))
184 #elif defined(__APPLE__)
189 bool getDataFromPasteboard(PasteboardRef inPasteboard,
194 PasteboardSyncFlags syncFlags = PasteboardSynchronize(inPasteboard);
195 OSStatus err = PasteboardGetItemCount(inPasteboard, &itemCount);
196 require_noerr(err, CantGetPasteboardItemCount);
198 for (UInt32 itemIndex = 1; itemIndex <= itemCount; itemIndex ++)
200 PasteboardItemID itemID;
201 CFArrayRef flavorTypeArray;
204 err = PasteboardGetItemIdentifier(inPasteboard, itemIndex, &itemID);
205 require_noerr(err, CantGetPasteboardItemIdentifier);
207 err = PasteboardCopyItemFlavors(inPasteboard,
208 itemID, &flavorTypeArray);
209 require_noerr(err, CantCopyPasteboardItemFlavors);
211 flavorCount = CFArrayGetCount(flavorTypeArray);
213 for (CFIndex flavorIndex = 0; flavorIndex < flavorCount;
216 CFStringRef flavorType = (CFStringRef)CFArrayGetValueAtIndex(
217 flavorTypeArray, flavorIndex);
220 if (UTTypeConformsTo(flavorType, CFSTR(
"public.utf8-plain-text")))
222 CFDataRef flavorData;
223 err = PasteboardCopyItemFlavorData(inPasteboard, itemID,
224 flavorType, &flavorData);
225 require_noerr(err, CantCopyFlavorData);
226 CFIndex flavorDataSize = CFDataGetLength(flavorData);
227 flavorDataSize = (flavorDataSize<254) ? flavorDataSize : 254;
229 if (flavorDataSize + 2 > bufSize)
232 "Cannot copy clipboard, contents is too big!\n");
236 for (
short dataIndex = 0; dataIndex <= flavorDataSize;
239 signed char byte = *(CFDataGetBytePtr(
240 flavorData) + dataIndex);
241 flavorText[dataIndex] =
byte;
244 flavorText[flavorDataSize] =
'\0';
245 flavorText[flavorDataSize + 1] =
'\n';
247 CFRelease(flavorData);
253 fprintf(stderr,
"Cannot copy clipboard, CantCopyFlavorData!\n");
256 CFRelease(flavorTypeArray);
259 CantCopyPasteboardItemFlavors:
261 "Cannot copy clipboard, CantCopyPasteboardItemFlavors!\n");
263 CantGetPasteboardItemIdentifier:
265 "Cannot copy clipboard, CantGetPasteboardItemIdentifier!\n");
269 "Cannot copy clipboard, found no acceptable flavour!\n");
272 CantGetPasteboardItemCount:
273 fprintf(stderr,
"Cannot copy clipboard, CantGetPasteboardItemCount!\n");
277 bool getClipBoard(
char* text ,
const int bufSize)
279 PasteboardRef theClipboard;
280 OSStatus err = PasteboardCreate(kPasteboardClipboard, &theClipboard);
281 require_noerr(err, PasteboardCreateFailed);
283 if (!getDataFromPasteboard(theClipboard, text, bufSize))
286 "Cannot copy clipboard, getDataFromPasteboardFailed!\n");
290 CFRelease(theClipboard);
295 PasteboardCreateFailed:
296 fprintf(stderr,
"Cannot copy clipboard, PasteboardCreateFailed!\n");
297 CFRelease(theClipboard);
303 const int bufSize = 512;
304 char buffer[bufSize + 1];
306 if (getClipBoard(buffer, bufSize))
309 pos += strlen(buffer);
323 #elif defined USE_X11
325 static char* getSelection2(Display *
const dpy,
Window us, Atom selection,
329 Window owner = XGetSelectionOwner(dpy, selection);
334 XConvertSelection(dpy, selection, request_target,
335 XA_PRIMARY, us, CurrentTime);
338 while (max_events --)
343 if (e.type == SelectionNotify)
345 if (e.xselection.property ==
None)
348 long unsigned len,
left, dummy;
351 unsigned char *
data =
nullptr;
353 int ret = XGetWindowProperty(dpy, us, e.xselection.property, 0, 0,
354 False, AnyPropertyType, &type, &format, &len, &
left, &
data);
362 ret = XGetWindowProperty(dpy, us, e.xselection.property, 0,
363 left, False, AnyPropertyType, &type, &format, &len,
369 return reinterpret_cast<char*
>(
data);
375 static Atom requestAtom;
377 static char* getSelection(Display *
const dpy,
Window us, Atom selection)
379 char *
data =
nullptr;
380 if (requestAtom !=
None)
381 data = getSelection2(dpy, us, selection, requestAtom);
383 data = getSelection2(dpy, us, selection, XA_STRING);
391 SDL_VERSION(&
info.version)
394 Display *
const dpy =
info.info.x11.display;
397 requestAtom = XInternAtom(dpy,
"UTF8_STRING",
true);
398 char *
data = getSelection(dpy, us, XA_PRIMARY);
400 data = getSelection(dpy, us, XA_SECONDARY);
403 Atom XA_CLIPBOARD = XInternAtom(dpy,
"CLIPBOARD", 0);
404 if (XA_CLIPBOARD !=
None)
405 data = getSelection(dpy, us, XA_CLIPBOARD);
411 const size_t sz = text.size();
415 text.insert(pos,
data);
425 static bool runxsel(
const std::string &text,
const char *p1,
426 const char *p2 =
nullptr);
431 runxsel(text,
"-b",
"-i");
435 static bool runxsel(
const std::string &text,
const char *p1,
const char *p2)
443 if ((pid = fork()) == -1)
451 if (fd[0] != STDIN_FILENO)
453 if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO)
461 const char *
const xselPath =
462 #if defined __OpenBSD__ || defined __FreeBSD__ || defined __DragonFly__
463 "/usr/local/bin/xsel";
470 execl(xselPath,
"xsel", p1, p2,
471 static_cast<char *
>(
nullptr));
475 execl(xselPath,
"xsel", p1,
476 static_cast<char *
>(
nullptr));
484 const size_t len = text.length();
485 if (
write(fd[1], text.c_str(), len) !=
static_cast<ssize_t
>(len))
494 #elif defined __native_client__
498 NaclMessageHandle *handle = naclRegisterMessageHandler(
"clipboard-paste");
499 naclPostMessage(
"clipboard-paste",
"");
500 std::string response = naclWaitForMessage(handle);
501 text.insert(pos, response);
502 pos += response.size();
508 naclPostMessage(
"clipboard-copy", text);
bool sendBuffer(const std::string &text)
bool retrieveBuffer(std::string &text, size_t &pos)
const Image *restrict const left
bool info(InputEvent &event)
bool getWindowWMInfo(const SDL_Surface *const window, SDL_SysWMinfo *const info)
int64_t write(File *const file, const void *const buffer, const uint32_t objSize, const uint32_t objCount)
int close(File *const file)