summaryrefslogtreecommitdiff
path: root/src/utils/copynpaste.cpp
blob: 8e35377345df4449407f1c6c6b87cab2c512642b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
 *  The ManaPlus Client
 *  Copyright (C) 2001-2010  Wormux Team
 *  Copyright (C) 2011-2017  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 *  IMPORTANT!
 *
 *  This code was taken from Wormux svn trunk at Feb 25 2010. Please don't
 *  make any unnecessary modifications, and try to sync up modifications
 *  when possible.
 */

#ifdef _MSC_VER
# include "msvc/config.h"
#elif defined(HAVE_CONFIG_H)
#include "config.h"
#endif  // _MSC_VER

#include "utils/copynpaste.h"

#include "debug.h"

#ifdef USE_SDL2
#include <SDL_clipboard.h>

#else  // USE_SDL2

#if defined(__APPLE__)
#ifdef Status
#undef Status
#endif  // Status
#include <Carbon/Carbon.h>
#elif defined USE_X11
#include "render/graphics.h"

#include "utils/sdlhelper.h"

#include <SDL_syswm.h>
#include <unistd.h>
#elif defined __native_client__
#include "utils/naclmessages.h"
#elif defined WIN32
#include <SDL_syswm.h>
#endif  // defined(__APPLE__)

#endif  // USE_SDL2

#ifdef USE_SDL2
bool retrieveBuffer(std::string& text, size_t& pos)
{
    char *buf = SDL_GetClipboardText();
    if (buf)
    {
        text.insert(pos, buf);
        pos += strlen(buf);
        SDL_free(buf);
        return true;
    }
    return false;
}

bool sendBuffer(const std::string &restrict text)
{
    return !SDL_SetClipboardText(text.c_str());
}

#else  //  USE_SDL2

#ifdef WIN32
bool retrieveBuffer(std::string& text, size_t& pos)
{
    bool ret = false;

    if (!OpenClipboard(nullptr))
        return false;

    HANDLE h = GetClipboardData(CF_UNICODETEXT);
    if (h)
    {
        LPCWSTR data = static_cast<LPCWSTR>(GlobalLock(h));

        if (data)
        {
            const size_t len = WideCharToMultiByte(CP_UTF8, 0, data, -1,
                nullptr, 0, nullptr, nullptr);
            if (len > 0)
            {
                // Convert from UTF-16 to UTF-8
                void *temp = calloc(len, 1);
                if (WideCharToMultiByte(CP_UTF8, 0, data, -1,
                    static_cast<LPSTR>(temp), len, nullptr, nullptr))
                {
                    text.insert(pos, static_cast<char*>(temp));
                    pos += len - 1;
                }
                free(temp);
                ret = true;
            }
        }
        GlobalUnlock(h);
    }
    else
    {
        h = GetClipboardData(CF_TEXT);

        if (h)
        {
            const char *const data = static_cast<char*>(GlobalLock(h));
            if (data)
            {
                text.insert(pos, data);
                pos += strlen(data);
                ret = true;
            }
            GlobalUnlock(h);
        }
    }

    CloseClipboard();
    return ret;
}

bool sendBuffer(const std::string &restrict text)
{
    const int wCharsLen = MultiByteToWideChar(CP_UTF8,
        0, text.c_str(), -1, nullptr, 0);
    if (!wCharsLen)
        return false;

    HANDLE h = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
        CAST_SIZE(wCharsLen) * sizeof(WCHAR));
    WCHAR *const out = static_cast<WCHAR*>(GlobalLock(h));

    MultiByteToWideChar(CP_UTF8, 0, text.c_str(), -1, out, wCharsLen);

    if (!OpenClipboard(nullptr))
    {
        GlobalUnlock(h);
        GlobalFree(h);
        return false;
    }
    GlobalUnlock(h);
    EmptyClipboard();
    if (!SetClipboardData(CF_UNICODETEXT, out))
    {
        GlobalFree(h);
        CloseClipboard();
        return false;
    }
    GlobalFree(h);
    CloseClipboard();

    return true;
}

#elif defined(__APPLE__)

// Sorry for the very long code, all nicer OS X APIs are coded in
// Objective C and not C!
// Also it does very thorough error handling
bool getDataFromPasteboard(PasteboardRef inPasteboard,
                           char* flavorText /* out */,
                           const int bufSize)
{
    ItemCount itemCount;
    PasteboardSyncFlags syncFlags = PasteboardSynchronize(inPasteboard);
    OSStatus err = PasteboardGetItemCount(inPasteboard, &itemCount);
    require_noerr(err, CantGetPasteboardItemCount);

    for (UInt32 itemIndex = 1; itemIndex <= itemCount; itemIndex ++)
    {
        PasteboardItemID    itemID;
        CFArrayRef          flavorTypeArray;
        CFIndex             flavorCount;

        err = PasteboardGetItemIdentifier(inPasteboard, itemIndex, &itemID);
        require_noerr(err, CantGetPasteboardItemIdentifier);

        err = PasteboardCopyItemFlavors(inPasteboard,
            itemID, &flavorTypeArray);
        require_noerr(err, CantCopyPasteboardItemFlavors);

        flavorCount = CFArrayGetCount(flavorTypeArray);

        for (CFIndex flavorIndex = 0; flavorIndex < flavorCount;
             flavorIndex ++)
        {
            CFStringRef flavorType = (CFStringRef)CFArrayGetValueAtIndex(
                flavorTypeArray, flavorIndex);

            // we're only interested by text...
            if (UTTypeConformsTo(flavorType, CFSTR("public.utf8-plain-text")))
            {
                CFDataRef flavorData;
                err = PasteboardCopyItemFlavorData(inPasteboard, itemID,
                                                   flavorType, &flavorData);
                require_noerr(err, CantCopyFlavorData);
                CFIndex flavorDataSize = CFDataGetLength(flavorData);
                flavorDataSize = (flavorDataSize<254) ? flavorDataSize : 254;

                if (flavorDataSize + 2 > bufSize)
                {
                    fprintf(stderr,
                        "Cannot copy clipboard, contents is too big!\n");
                    return false;
                }

                for (short dataIndex = 0; dataIndex <= flavorDataSize;
                     dataIndex ++)
                {
                    signed char byte = *(CFDataGetBytePtr(
                        flavorData) + dataIndex);
                    flavorText[dataIndex] = byte;
                }

                flavorText[flavorDataSize] = '\0';
                flavorText[flavorDataSize + 1] = '\n';

                CFRelease(flavorData);
                return true;
            }

            continue;
CantCopyFlavorData:
            fprintf(stderr, "Cannot copy clipboard, CantCopyFlavorData!\n");
        }

        CFRelease(flavorTypeArray);
        continue;

CantCopyPasteboardItemFlavors:
        fprintf(stderr,
            "Cannot copy clipboard, CantCopyPasteboardItemFlavors!\n");
        continue;
CantGetPasteboardItemIdentifier:
        fprintf(stderr,
            "Cannot copy clipboard, CantGetPasteboardItemIdentifier!\n");
        continue;
    }
    fprintf(stderr,
        "Cannot copy clipboard, found no acceptable flavour!\n");
    return false;

CantGetPasteboardItemCount:
    fprintf(stderr, "Cannot copy clipboard, CantGetPasteboardItemCount!\n");
    return false;
}

bool getClipBoard(char* text  /* out */, const int bufSize)
{
    PasteboardRef theClipboard;
    OSStatus err = PasteboardCreate(kPasteboardClipboard, &theClipboard);
    require_noerr(err, PasteboardCreateFailed);

    if (!getDataFromPasteboard(theClipboard, text, bufSize))
    {
        fprintf(stderr,
            "Cannot copy clipboard, getDataFromPasteboardFailed!\n");
        return false;
    }

    CFRelease(theClipboard);

    return true;

    // ---- error handling
PasteboardCreateFailed:
    fprintf(stderr, "Cannot copy clipboard, PasteboardCreateFailed!\n");
    CFRelease(theClipboard);
    return false;
}

bool retrieveBuffer(std::string& text, size_t& pos)
{
    const int bufSize = 512;
    char buffer[bufSize + 1];

    if (getClipBoard(buffer, bufSize))
    {
        text = buffer;
        pos += strlen(buffer);
        return true;
    }
    else
    {
        return false;
    }
}

bool sendBuffer(const std::string &restrict text)
{
    return false;
}

#elif defined USE_X11

static char* getSelection2(Display *const dpy, Window us, Atom selection,
                           Atom request_target)
{
    int max_events = 50;
    Window owner = XGetSelectionOwner(dpy, selection);

    if (owner == None)
        return nullptr;

    XConvertSelection(dpy, selection, request_target,
        XA_PRIMARY, us, CurrentTime);
    XFlush(dpy);

    while (max_events --)
    {
        XEvent e;

        XNextEvent(dpy, &e);
        if (e.type == SelectionNotify)
        {
            if (e.xselection.property == None)
                return nullptr;

            long unsigned len, left, dummy;
            int format;
            Atom type;
            unsigned char *data = nullptr;

            int ret = XGetWindowProperty(dpy, us, e.xselection.property, 0, 0,
                False, AnyPropertyType, &type, &format, &len, &left, &data);
            if (left < 1)
            {
                if (ret == Success)
                    XFree(data);
                return nullptr;
            }

            ret = XGetWindowProperty(dpy, us, e.xselection.property, 0,
                left, False, AnyPropertyType, &type, &format, &len,
                &dummy, &data);

            if (ret != Success)
                return nullptr;

            return reinterpret_cast<char*>(data);
        }
    }
    return nullptr;
}

static Atom requestAtom;

static char* getSelection(Display *const dpy, Window us, Atom selection)
{
    char *data = nullptr;
    if (requestAtom != None)
        data = getSelection2(dpy, us, selection, requestAtom);
    if (!data)
        data = getSelection2(dpy, us, selection, XA_STRING);
    return data;
}

bool retrieveBuffer(std::string& text, size_t& pos)
{
    SDL_SysWMinfo info;

    SDL_VERSION(&info.version);
    if (SDL::getWindowWMInfo(mainGraphics->getWindow(), &info))
    {
        Display *const dpy = info.info.x11.display;
        Window us = info.info.x11.window;

        requestAtom = XInternAtom(dpy, "UTF8_STRING", true);
        char *data = getSelection(dpy, us, XA_PRIMARY);
        if (!data)
            data = getSelection(dpy, us, XA_SECONDARY);
        if (!data)
        {
            Atom XA_CLIPBOARD = XInternAtom(dpy, "CLIPBOARD", 0);
            if (XA_CLIPBOARD != None)
                data = getSelection(dpy, us, XA_CLIPBOARD);
        }

        if (data)
        {
            // check cursor position
            const size_t sz = text.size();
            if (pos > sz)
                pos = sz;

            text.insert(pos, data);
            pos += strlen(data);
            XFree(data);

            return true;
        }
    }
    return false;
}

static bool runxsel(const std::string &text, const char *p1,
                    const char *p2 = nullptr);

bool sendBuffer(const std::string &restrict text)
{
    runxsel(text, "-i");
    runxsel(text, "-b", "-i");
    return true;
}

static bool runxsel(const std::string &text, const char *p1, const char *p2)
{
    pid_t pid;
    int fd[2];

    if (pipe(fd))
        return false;

    if ((pid = fork()) == -1)
    {   // fork error
        return false;
    }
    else if (!pid)
    {   // child
        close(fd[1]);

        if (fd[0] != STDIN_FILENO)
        {
            if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO)
            {
                close(fd[0]);
                _exit(1);
            }
            close(fd[0]);
        }

        const char *const xselPath =
#if defined __OpenBSD__ || defined __FreeBSD__ || defined __DragonFly__
            "/usr/local/bin/xsel";
#else  // defined __OpenBSD__ || defined __FreeBSD__ || defined __DragonFly__
            "/usr/bin/xsel";
#endif  // defined __OpenBSD__ || defined __FreeBSD__ || defined __DragonFly__

        if (p2)
        {
            execl(xselPath, "xsel", p1, p2,
                static_cast<char *>(nullptr));
        }
        else
        {
            execl(xselPath, "xsel", p1,
                static_cast<char *>(nullptr));
        }

        _exit(1);
    }

    // parent
    close(fd[0]);
    const size_t len = text.length();
    if (write(fd[1], text.c_str(), len) != static_cast<ssize_t>(len))
    {
        close(fd[1]);
        return false;
    }
    close(fd[1]);
    return true;
}

#elif defined __native_client__

bool retrieveBuffer(std::string& text, size_t& pos)
{
    NaclMessageHandle *handle = naclRegisterMessageHandler("clipboard-paste");
    naclPostMessage("clipboard-paste", "");
    std::string response = naclWaitForMessage(handle);
    text.insert(pos, response);
    pos += response.size();
    return true;
}

bool sendBuffer(const std::string &restrict text)
{
    naclPostMessage("clipboard-copy", text);
    return true;
}
#else  // WIN32

bool retrieveBuffer(std::string &text A_UNUSED, size_t &pos A_UNUSED)
{
    return false;
}

bool sendBuffer(const std::string &restrict text A_UNUSED)
{
    return false;
}
#endif  // WIN32
#endif  // USE_SDL2