source: trunk/synergy/lib/platform/CPMClipboard.cpp@ 2752

Last change on this file since 2752 was 2752, checked in by bird, 19 years ago

Two classes (CPMScreen and CPMKeyState) + the hook dll left (and debugging of course).

File size: 5.1 KB
RevLine 
[2749]1/*
2 * synergy -- mouse and keyboard sharing utility
3 * Copyright (C) 2002 Chris Schoeneman
[2752]4 * Copyright (C) 2006 Knut St. Osmundsen
[2749]5 *
6 * This package is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * found in the file COPYING that should have accompanied this file.
9 *
10 * This package is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
[2752]16#include "CPMClipboard.h"
17#include "CPMClipboardTextConverter.h"
18#include "CPMClipboardBitmapConverter.h"
19#include "CPMClipboardHTMLConverter.h"
20#include "CPMUtil.h"
[2749]21#include "CLog.h"
22
23//
[2752]24// CPMClipboard
[2749]25//
26
[2752]27ATOM CPMClipboard::s_ownershipFormat = 0;
[2749]28
[2752]29CPMClipboard::CPMClipboard(HWND window) :
[2749]30 m_window(window),
31 m_time(0)
32{
33 // add converters, most desired first
[2752]34 m_converters.push_back(new CPMClipboardTextConverter);
35 m_converters.push_back(new CPMClipboardBitmapConverter);
36 m_converters.push_back(new CPMClipboardHTMLConverter);
[2749]37}
38
[2752]39CPMClipboard::~CPMClipboard()
[2749]40{
41 clearConverters();
42}
43
44bool
[2752]45CPMClipboard::emptyUnowned()
[2749]46{
47 LOG((CLOG_DEBUG "empty clipboard"));
48
49 // empty the clipboard (and take ownership)
[2752]50 if (!WinEmptyClipbrd(CPMUtil::getHAB())) {
[2749]51 LOG((CLOG_DEBUG "failed to grab clipboard"));
52 return false;
53 }
54
55 return true;
56}
57
58bool
[2752]59CPMClipboard::empty()
[2749]60{
61 if (!emptyUnowned()) {
62 return false;
63 }
64
65 // mark clipboard as being owned by synergy
[2752]66 PVOID pv = NULL;
67 APIRET rc = DosAllocSharedMem(&pv, NULL, 1, PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_GETTABLE | OBJ_GIVEABLE);
68 if (rc) {
69 assert(rc);
70 return false;
71 }
72 return !!WinSetClipbrdData(CPMUtil::getHAB(), (ULONG)pv, getOwnershipFormat(), CFI_POINTER);
[2749]73}
74
75void
[2752]76CPMClipboard::add(EFormat format, const CString& data)
[2749]77{
78 LOG((CLOG_DEBUG "add %d bytes to clipboard format: %d", data.size(), format));
79
80 // convert data to win32 form
81 for (ConverterList::const_iterator index = m_converters.begin();
[2752]82 index != m_converters.end();
83 ++index) {
84 IPMClipboardConverter* converter = *index;
[2749]85
86 // skip converters for other formats
87 if (converter->getFormat() == format) {
[2752]88 ULONG ulPMData = converter->fromIClipboard(data);
89 if (ulPMData != NULL) {
90 ULONG ulPMFormat = converter->getPMFormat();
91 ULONG ulPMFormatInfo = converter->getPMFormatInfo();
92 if (!WinSetClipbrdData(CPMUtil::getHAB(), ulPMData, ulPMFormat, ulPMFormatInfo)) {
93 // free converted data if we couldn't put it on the clipboard
94 converter->freePMData(ulPMData);
[2749]95 }
96 }
97 }
98 }
99}
100
101bool
[2752]102CPMClipboard::open(Time time) const
[2749]103{
104 LOG((CLOG_DEBUG "open clipboard"));
105
[2752]106 if (!WinOpenClipbrd(m_window)) {
[2749]107 LOG((CLOG_WARN "failed to open clipboard"));
108 return false;
109 }
110
111 m_time = time;
112
113 return true;
114}
115
116void
[2752]117CPMClipboard::close() const
[2749]118{
119 LOG((CLOG_DEBUG "close clipboard"));
[2752]120 WinCloseClipbrd(CPMUtil::getHAB());
[2749]121}
122
123IClipboard::Time
[2752]124CPMClipboard::getTime() const
[2749]125{
126 return m_time;
127}
128
129bool
[2752]130CPMClipboard::has(EFormat format) const
[2749]131{
132 for (ConverterList::const_iterator index = m_converters.begin();
[2752]133 index != m_converters.end();
134 ++index) {
135 IPMClipboardConverter* converter = *index;
[2749]136 if (converter->getFormat() == format) {
[2752]137 ULONG fFmtInfo;
138 if (WinQueryClipbrdFmtInfo(CPMUtil::getHAB(), getOwnershipFormat(), &fFmtInfo)) {
[2749]139 return true;
140 }
141 }
142 }
143 return false;
144}
145
146CString
[2752]147CPMClipboard::get(EFormat format) const
[2749]148{
149 // find the converter for the first clipboard format we can handle
[2752]150 IPMClipboardConverter* converter = NULL;
151 ULONG pmFormat = WinEnumClipbrdFmts(CPMUtil::getHAB(), 0);
152 while (converter == NULL && pmFormat != 0) {
[2749]153 for (ConverterList::const_iterator index = m_converters.begin();
[2752]154 index != m_converters.end();
155 ++index) {
[2749]156 converter = *index;
[2752]157 if (converter->getPMFormat() == pmFormat &&
158 converter->getFormat() == format) {
[2749]159 break;
160 }
161 converter = NULL;
162 }
[2752]163 pmFormat = WinEnumClipbrdFmts(CPMUtil::getHAB(), pmFormat);
[2749]164 }
165
166 // if no converter then we don't recognize any formats
167 if (converter == NULL) {
168 return CString();
169 }
170
171 // get a handle to the clipboard data
[2752]172 ULONG ulData = WinQueryClipbrdData(CPMUtil::getHAB(), converter->getPMFormat());
173 if (ulData == 0) {
[2749]174 return CString();
175 }
176
177 // convert
[2752]178 return converter->toIClipboard(ulData);
[2749]179}
180
181void
[2752]182CPMClipboard::clearConverters()
[2749]183{
184 for (ConverterList::iterator index = m_converters.begin();
[2752]185 index != m_converters.end();
186 ++index) {
[2749]187 delete *index;
188 }
189 m_converters.clear();
190}
191
192bool
[2752]193CPMClipboard::isOwnedBySynergy()
[2749]194{
[2752]195 ULONG fFmtInfo;
196 return (!!WinQueryClipbrdFmtInfo(CPMUtil::getHAB(), getOwnershipFormat(), &fFmtInfo));
[2749]197}
198
[2752]199ATOM
200CPMClipboard::getOwnershipFormat()
[2749]201{
202 // create ownership format if we haven't yet
203 if (s_ownershipFormat == 0) {
[2752]204 ATOM hAtom = WinAddAtom(WinQuerySystemAtomTable(), (PCSZ)"SynergyOwnership");
205 if (hAtom == 0)
206 hAtom = WinFindAtom(WinQuerySystemAtomTable(), (PCSZ)"SynergyOwnership");
207 assert(hAtom != 0);
208 s_ownershipFormat = hAtom;
[2749]209 }
210
211 // return the format
212 return s_ownershipFormat;
213}
Note: See TracBrowser for help on using the repository browser.