File: clipboard.cpp

package info (click to toggle)
seriousproton 2020.01.15%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 720 kB
  • sloc: cpp: 7,666; ansic: 376; php: 59; makefile: 15
file content (92 lines) | stat: -rw-r--r-- 2,293 bytes parent folder | download | duplicates (2)
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
#include "engine.h"

#ifdef __WIN32__
#include <windows.h>
#endif
#ifdef __linux__
#include <stdio.h>
#endif

string Clipboard::readClipboard()
{
#ifdef __WIN32__
    P<WindowManager> windowManager = engine->getObject("windowManager");
    if (!OpenClipboard(windowManager->window.getSystemHandle()))
    {
        LOG(WARNING) << "Failed to open the clipboard for reading";
        return "";
    }
    HANDLE handle = GetClipboardData(CF_TEXT);
    if (!handle)
    {
        LOG(WARNING) << "Failed to open the clipboard for reading";
        CloseClipboard();
        return "";
    }
    string ret;
    ret = static_cast<char*>(GlobalLock(handle));
    GlobalUnlock(handle);
    CloseClipboard();
    return ret;
#endif//__WIN32__
#ifdef __linux__
    FILE* pipe = popen("/usr/bin/xclip -o -selection clipboard", "r");
    if (!pipe)
    {
        LOG(WARNING) << "Failed to execute /usr/bin/xclip for clipboard access";
        return "";
    }
    char buffer[1024];
    std::string result = "";
    while (!feof(pipe))
    {
        if (fgets(buffer, 1024, pipe) != NULL)
            result += buffer;
    }
    pclose(pipe);
    return result;
#endif

    return "";
}

void Clipboard::setClipboard(string value)
{
#ifdef __WIN32__
    P<WindowManager> windowManager = engine->getObject("windowManager");
    if (!OpenClipboard(windowManager->window.getSystemHandle()))
    {
        LOG(WARNING) << "Failed to open the clipboard for writing";
        return;
    }

    EmptyClipboard();

    HANDLE string_handle;
    size_t string_size = (value.length()+1) * sizeof(char);
    string_handle = GlobalAlloc(GMEM_MOVEABLE, string_size);

    if (!string_handle)
    {
        LOG(WARNING) << "Failed to allocate a string for the clipboard writing";
        CloseClipboard();
        return;
    }

    memcpy(GlobalLock(string_handle), value.c_str(), string_size);
    GlobalUnlock(string_handle);
    SetClipboardData(CF_TEXT, string_handle);

    CloseClipboard();
#endif//__WIN32__
#ifdef __linux__
    FILE* pipe = popen("/usr/bin/xclip -i -selection clipboard -silent", "we");
    if (!pipe)
    {
        LOG(WARNING) << "Failed to execute /usr/bin/xclip for clipboard access";
        return;
    }
    fwrite(value.c_str(), value.size(), 1, pipe);
    pclose(pipe);
#endif
}