1 | /*
|
---|
2 | * synergy -- mouse and keyboard sharing utility
|
---|
3 | * Copyright (C) 2004 Chris Schoeneman
|
---|
4 | *
|
---|
5 | * This package is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU General Public License
|
---|
7 | * found in the file COPYING that should have accompanied this file.
|
---|
8 | *
|
---|
9 | * This package is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | * GNU General Public License for more details.
|
---|
13 | */
|
---|
14 |
|
---|
15 | #include "CXWindowsClipboardBMPConverter.h"
|
---|
16 |
|
---|
17 | // BMP file header structure
|
---|
18 | struct CBMPHeader {
|
---|
19 | public:
|
---|
20 | UInt16 type;
|
---|
21 | UInt32 size;
|
---|
22 | UInt16 reserved1;
|
---|
23 | UInt16 reserved2;
|
---|
24 | UInt32 offset;
|
---|
25 | };
|
---|
26 |
|
---|
27 | // BMP is little-endian
|
---|
28 | static inline
|
---|
29 | UInt32
|
---|
30 | fromLEU32(const UInt8* data)
|
---|
31 | {
|
---|
32 | return static_cast<UInt32>(data[0]) |
|
---|
33 | (static_cast<UInt32>(data[1]) << 8) |
|
---|
34 | (static_cast<UInt32>(data[2]) << 16) |
|
---|
35 | (static_cast<UInt32>(data[3]) << 24);
|
---|
36 | }
|
---|
37 |
|
---|
38 | static
|
---|
39 | void
|
---|
40 | toLE(UInt8*& dst, char src)
|
---|
41 | {
|
---|
42 | dst[0] = static_cast<UInt8>(src);
|
---|
43 | dst += 1;
|
---|
44 | }
|
---|
45 |
|
---|
46 | static
|
---|
47 | void
|
---|
48 | toLE(UInt8*& dst, UInt16 src)
|
---|
49 | {
|
---|
50 | dst[0] = static_cast<UInt8>(src & 0xffu);
|
---|
51 | dst[1] = static_cast<UInt8>((src >> 8) & 0xffu);
|
---|
52 | dst += 2;
|
---|
53 | }
|
---|
54 |
|
---|
55 | static
|
---|
56 | void
|
---|
57 | toLE(UInt8*& dst, UInt32 src)
|
---|
58 | {
|
---|
59 | dst[0] = static_cast<UInt8>(src & 0xffu);
|
---|
60 | dst[1] = static_cast<UInt8>((src >> 8) & 0xffu);
|
---|
61 | dst[2] = static_cast<UInt8>((src >> 16) & 0xffu);
|
---|
62 | dst[3] = static_cast<UInt8>((src >> 24) & 0xffu);
|
---|
63 | dst += 4;
|
---|
64 | }
|
---|
65 |
|
---|
66 | //
|
---|
67 | // CXWindowsClipboardBMPConverter
|
---|
68 | //
|
---|
69 |
|
---|
70 | CXWindowsClipboardBMPConverter::CXWindowsClipboardBMPConverter(
|
---|
71 | Display* display) :
|
---|
72 | m_atom(XInternAtom(display, "image/bmp", False))
|
---|
73 | {
|
---|
74 | // do nothing
|
---|
75 | }
|
---|
76 |
|
---|
77 | CXWindowsClipboardBMPConverter::~CXWindowsClipboardBMPConverter()
|
---|
78 | {
|
---|
79 | // do nothing
|
---|
80 | }
|
---|
81 |
|
---|
82 | IClipboard::EFormat
|
---|
83 | CXWindowsClipboardBMPConverter::getFormat() const
|
---|
84 | {
|
---|
85 | return IClipboard::kBitmap;
|
---|
86 | }
|
---|
87 |
|
---|
88 | Atom
|
---|
89 | CXWindowsClipboardBMPConverter::getAtom() const
|
---|
90 | {
|
---|
91 | return m_atom;
|
---|
92 | }
|
---|
93 |
|
---|
94 | int
|
---|
95 | CXWindowsClipboardBMPConverter::getDataSize() const
|
---|
96 | {
|
---|
97 | return 8;
|
---|
98 | }
|
---|
99 |
|
---|
100 | CString
|
---|
101 | CXWindowsClipboardBMPConverter::fromIClipboard(const CString& bmp) const
|
---|
102 | {
|
---|
103 | // create BMP image
|
---|
104 | UInt8 header[14];
|
---|
105 | UInt8* dst = header;
|
---|
106 | toLE(dst, 'B');
|
---|
107 | toLE(dst, 'M');
|
---|
108 | toLE(dst, static_cast<UInt32>(14 + bmp.size()));
|
---|
109 | toLE(dst, static_cast<UInt16>(0));
|
---|
110 | toLE(dst, static_cast<UInt16>(0));
|
---|
111 | toLE(dst, static_cast<UInt32>(14 + 40));
|
---|
112 | return CString(reinterpret_cast<const char*>(header), 14) + bmp;
|
---|
113 | }
|
---|
114 |
|
---|
115 | CString
|
---|
116 | CXWindowsClipboardBMPConverter::toIClipboard(const CString& bmp) const
|
---|
117 | {
|
---|
118 | // make sure data is big enough for a BMP file
|
---|
119 | if (bmp.size() <= 14 + 40) {
|
---|
120 | return CString();
|
---|
121 | }
|
---|
122 |
|
---|
123 | // check BMP file header
|
---|
124 | const UInt8* rawBMPHeader = reinterpret_cast<const UInt8*>(bmp.data());
|
---|
125 | if (rawBMPHeader[0] != 'B' || rawBMPHeader[1] != 'M') {
|
---|
126 | return CString();
|
---|
127 | }
|
---|
128 |
|
---|
129 | // get offset to image data
|
---|
130 | UInt32 offset = fromLEU32(rawBMPHeader + 10);
|
---|
131 |
|
---|
132 | // construct BMP
|
---|
133 | if (offset == 14 + 40) {
|
---|
134 | return bmp.substr(14);
|
---|
135 | }
|
---|
136 | else {
|
---|
137 | return bmp.substr(14, 40) + bmp.substr(offset, bmp.size() - offset);
|
---|
138 | }
|
---|
139 | }
|
---|