-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientConnectionClipboard.cpp
130 lines (113 loc) · 4.41 KB
/
ClientConnectionClipboard.cpp
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
// The VNC system 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
// (at your option) 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, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
//
// If the source code for the VNC system is not available from the place
// whence you received this file, check http://www.orl.co.uk/vnc or contact
// the authors on [email protected] for information on obtaining it.
#include "stdhdrs.h"
#include "vncviewer.h"
#include "ClientConnection.h"
#include "Exception.h"
// This file contains the code for getting text from, and putting text into
// the Windows clipboard.
//
// ProcessClipboardChange
// Called by ClientConnection::WndProc.
// We've been informed that the local clipboard has been updated.
// If it's text we want to send it to the server.
//
void ClientConnection::ProcessLocalClipboardChange()
{
log.Print(2, _T("Clipboard changed\n"));
HWND hOwner = GetClipboardOwner();
if (hOwner == m_hwnd) {
log.Print(2, _T("We changed it - ignore!\n"));
} else if (!m_initialClipboardSeen) {
log.Print(2, _T("Don't send initial clipboard!\n"));
m_initialClipboardSeen = true;
} else {
// The clipboard should not be modified by more than one thread at once
omni_mutex_lock l(m_clipMutex);
if (OpenClipboard(m_hwnd)) {
HGLOBAL hglb = GetClipboardData(CF_TEXT);
if (hglb == NULL) {
CloseClipboard();
} else {
LPSTR lpstr = (LPSTR) GlobalLock(hglb);
char *contents = new char[strlen(lpstr) + 1];
char *unixcontents = new char[strlen(lpstr) + 1];
strcpy(contents,lpstr);
GlobalUnlock(hglb);
CloseClipboard();
// Translate to Unix-format lines before sending
for (int i = 0, j = 0; contents[i] != '\0'; i++) {
if (contents[i] != '\x0d') {
unixcontents[j++] = contents[i];
}
}
unixcontents[j] = '\0';
try {
SendClientCutText(unixcontents, strlen(unixcontents));
} catch (WarningException &e) {
log.Print(0, _T("Exception while sending clipboard text : %s\n"), e.m_info);
DestroyWindow(m_hwnd);
}
delete [] contents;
delete [] unixcontents;
}
}
}
// Pass the message to the next window in clipboard viewer chain
::SendMessage(m_hwndNextViewer, WM_DRAWCLIPBOARD , 0,0);
}
// We've read some text from the remote server, and
// we need to copy it into the local clipboard.
// Called by ClientConnection::ReadServerCutText()
void ClientConnection::UpdateLocalClipboard(char *buf, int len) {
// Copy to wincontents replacing LF with CR-LF
char *wincontents = new char[len * 2 + 1];
for (int i = 0, j = 0; m_netbuf[i] != 0; i++, j++) {
if (buf[i] == '\x0a') {
wincontents[j++] = '\x0d';
len++;
}
wincontents[j] = buf[i];
}
wincontents[j] = '\0';
// The clipboard should not be modified by more than one thread at once
{
omni_mutex_lock l(m_clipMutex);
if (!OpenClipboard(m_hwnd)) {
throw WarningException("Failed to open clipboard\n");
}
if (! ::EmptyClipboard()) {
throw WarningException("Failed to empty clipboard\n");
}
// Allocate a global memory object for the text.
HGLOBAL hglbCopy = GlobalAlloc(GMEM_DDESHARE, (len +1) * sizeof(TCHAR));
if (hglbCopy != NULL) {
// Lock the handle and copy the text to the buffer.
LPTSTR lptstrCopy = (LPTSTR) GlobalLock(hglbCopy);
memcpy(lptstrCopy, wincontents, len * sizeof(TCHAR));
lptstrCopy[len] = (TCHAR) 0; // null character
GlobalUnlock(hglbCopy); // Place the handle on the clipboard.
SetClipboardData(CF_TEXT, hglbCopy);
}
delete [] wincontents;
if (! ::CloseClipboard()) {
throw WarningException("Failed to close clipboard\n");
}
}
}