aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/barrier/IClipboard.cpp
blob: d484121db8ebdd92fe385512bfe40397fcd201a7 (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
/*
 * barrier -- mouse and keyboard sharing utility
 * Copyright (C) 2012-2016 Symless Ltd.
 * Copyright (C) 2004 Chris Schoeneman
 *
 * This package is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * found in the file LICENSE that should have accompanied this file.
 *
 * This package 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/>.
 */

#include "barrier/IClipboard.h"
#include "common/stdvector.h"

//
// IClipboard
//

void
IClipboard::unmarshall(IClipboard* clipboard, const String& data, Time time)
{
    assert(clipboard != NULL);

    const char* index = data.data();

    if (clipboard->open(time)) {
        // clear existing data
        clipboard->empty();

        // read the number of formats
        const UInt32 numFormats = readUInt32(index);
        index += 4;

        // read each format
        for (UInt32 i = 0; i < numFormats; ++i) {
            // get the format id
            IClipboard::EFormat format =
                static_cast<IClipboard::EFormat>(readUInt32(index));
            index += 4;

            // get the size of the format data
            UInt32 size = readUInt32(index);
            index += 4;

            // save the data if it's a known format.  if either the client
            // or server supports more clipboard formats than the other
            // then one of them will get a format >= kNumFormats here.
            if (format <IClipboard::kNumFormats) {
                clipboard->add(format, String(index, size));
            }
            index += size;
        }

        // done
        clipboard->close();
    }
}

String
IClipboard::marshall(const IClipboard* clipboard)
{
    // return data format:
    // 4 bytes => number of formats included
    // 4 bytes => format enum
    // 4 bytes => clipboard data size n
    // n bytes => clipboard data
    // back to the second 4 bytes if there is another format

    assert(clipboard != NULL);

    String data;

    std::vector<String> formatData;
    formatData.resize(IClipboard::kNumFormats);
    // FIXME -- use current time
    if (clipboard->open(0)) {

        // compute size of marshalled data
        UInt32 size = 4;
        UInt32 numFormats = 0;
        for (UInt32 format = 0; format != IClipboard::kNumFormats; ++format) {
            if (clipboard->has(static_cast<IClipboard::EFormat>(format))) {
                ++numFormats;
                formatData[format] =
                    clipboard->get(static_cast<IClipboard::EFormat>(format));
                size += 4 + 4 + (UInt32)formatData[format].size();
            }
        }

        // allocate space
        data.reserve(size);

        // marshall the data
        writeUInt32(&data, numFormats);
        for (UInt32 format = 0; format != IClipboard::kNumFormats; ++format) {
            if (clipboard->has(static_cast<IClipboard::EFormat>(format))) {
                writeUInt32(&data, format);
                writeUInt32(&data, (UInt32)formatData[format].size());
                data += formatData[format];
            }
        }
        clipboard->close();
    }

    return data;
}

bool
IClipboard::copy(IClipboard* dst, const IClipboard* src)
{
    assert(dst != NULL);
    assert(src != NULL);

    return copy(dst, src, src->getTime());
}

bool
IClipboard::copy(IClipboard* dst, const IClipboard* src, Time time)
{
    assert(dst != NULL);
    assert(src != NULL);

    bool success = false;
    if (src->open(time)) {
        if (dst->open(time)) {
            if (dst->empty()) {
                for (SInt32 format = 0;
                                format != IClipboard::kNumFormats; ++format) {
                    IClipboard::EFormat eFormat = (IClipboard::EFormat)format;
                    if (src->has(eFormat)) {
                        dst->add(eFormat, src->get(eFormat));
                    }
                }
                success = true;
            }
            dst->close();
        }
        src->close();
    }

    return success;
}

UInt32
IClipboard::readUInt32(const char* buf)
{
    const unsigned char* ubuf = reinterpret_cast<const unsigned char*>(buf);
    return    (static_cast<UInt32>(ubuf[0]) << 24) |
            (static_cast<UInt32>(ubuf[1]) << 16) |
            (static_cast<UInt32>(ubuf[2]) <<  8) |
             static_cast<UInt32>(ubuf[3]);
}

void
IClipboard::writeUInt32(String* buf, UInt32 v)
{
    *buf += static_cast<UInt8>((v >> 24) & 0xff);
    *buf += static_cast<UInt8>((v >> 16) & 0xff);
    *buf += static_cast<UInt8>((v >>  8) & 0xff);
    *buf += static_cast<UInt8>( v        & 0xff);
}