blob: 0e2906236515a44b417bc44ace7d8052b20370cc [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- Clipboard.cxx
20
21#include <rfb_win32/Clipboard.h>
22#include <rfb_win32/WMShatter.h>
23#include <rfb/util.h>
24
25#include <rfb/LogWriter.h>
26
27using namespace rfb;
28using namespace rfb::win32;
29
30static LogWriter vlog("Clipboard");
31
32
33//
34// -=- CR/LF handlers
35//
36
37char*
38dos2unix(const char* text) {
39 int len = strlen(text)+1;
40 char* unix = new char[strlen(text)+1];
41 int i, j=0;
42 for (i=0; i<len; i++) {
43 if (text[i] != '\x0d')
44 unix[j++] = text[i];
45 }
46 return unix;
47}
48
49char*
50unix2dos(const char* text) {
51 int len = strlen(text)+1;
52 char* dos = new char[strlen(text)*2+1];
53 int i, j=0;
54 for (i=0; i<len; i++) {
55 if (text[i] == '\x0a')
56 dos[j++] = '\x0d';
57 dos[j++] = text[i];
58 }
59 return dos;
60}
61
62
63//
64// -=- ISO-8859-1 (Latin 1) filter (in-place)
65//
66
67void
68removeNonISOLatin1Chars(char* text) {
69 int len = strlen(text);
70 int i=0, j=0;
71 for (; i<len; i++) {
72 if (((text[i] >= 1) && (text[i] <= 127)) ||
73 ((text[i] >= 160) && (text[i] <= 255)))
74 text[j++] = text[i];
75 }
76 text[j] = 0;
77}
78
79//
80// -=- Clipboard object
81//
82
83Clipboard::Clipboard()
84 : MsgWindow(_T("Clipboard")), notifier(0), next_window(0) {
85 next_window = SetClipboardViewer(getHandle());
86 vlog.debug("registered clipboard handler");
87}
88
89Clipboard::~Clipboard() {
Pierre Ossmanfb450fb2015-03-03 16:34:56 +010090 vlog.debug("removing %p from chain (next is %p)", getHandle(), next_window);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000091 ChangeClipboardChain(getHandle(), next_window);
92}
93
94LRESULT
95Clipboard::processMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
96 switch (msg) {
97
98 case WM_CHANGECBCHAIN:
Pierre Ossman40e3ceb2015-03-03 16:46:19 +010099 vlog.debug("change clipboard chain (%I64x, %I64x)",
100 (long long)wParam, (long long)lParam);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000101 if ((HWND) wParam == next_window)
102 next_window = (HWND) lParam;
103 else if (next_window != 0)
104 SendMessage(next_window, msg, wParam, lParam);
105 else
106 vlog.error("bad clipboard chain change!");
107 break;
108
109 case WM_DRAWCLIPBOARD:
110 {
111 HWND owner = GetClipboardOwner();
112 if (owner == getHandle()) {
113 vlog.debug("local clipboard changed by me");
114 } else {
Pierre Ossmanfb450fb2015-03-03 16:34:56 +0100115 vlog.debug("local clipboard changed by %p", owner);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000116
117 // Open the clipboard
118 if (OpenClipboard(getHandle())) {
119 // Get the clipboard data
120 HGLOBAL cliphandle = GetClipboardData(CF_TEXT);
121 if (cliphandle) {
122 char* clipdata = (char*) GlobalLock(cliphandle);
123
124 // Notify clients
125 if (notifier) {
126 if (!clipdata) {
127 notifier->notifyClipboardChanged(0, 0);
128 } else {
129 CharArray unix_text;
130 unix_text.buf = dos2unix(clipdata);
131 removeNonISOLatin1Chars(unix_text.buf);
132 notifier->notifyClipboardChanged(unix_text.buf, strlen(unix_text.buf));
133 }
134 } else {
135 vlog.debug("no clipboard notifier registered");
136 }
137
138 // Release the buffer and close the clipboard
139 GlobalUnlock(cliphandle);
140 }
141
142 CloseClipboard();
143 }
144 }
145 }
146 if (next_window)
147 SendMessage(next_window, msg, wParam, lParam);
148 return 0;
149
150 };
151 return MsgWindow::processMessage(msg, wParam, lParam);
152};
153
154void
155Clipboard::setClipText(const char* text) {
156 HANDLE clip_handle = 0;
157
158 try {
159
160 // - Firstly, we must open the clipboard
161 if (!OpenClipboard(getHandle()))
162 throw rdr::SystemException("unable to open Win32 clipboard", GetLastError());
163
164 // - Pre-process the supplied clipboard text into DOS format
165 CharArray dos_text;
166 dos_text.buf = unix2dos(text);
167 removeNonISOLatin1Chars(dos_text.buf);
168 int dos_text_len = strlen(dos_text.buf);
169
170 // - Allocate global memory for the data
171 clip_handle = ::GlobalAlloc(GMEM_MOVEABLE, dos_text_len+1);
172
173 char* data = (char*) GlobalLock(clip_handle);
174 memcpy(data, dos_text.buf, dos_text_len+1);
175 data[dos_text_len] = 0;
176 GlobalUnlock(clip_handle);
177
178 // - Next, we must clear out any existing data
179 if (!EmptyClipboard())
180 throw rdr::SystemException("unable to empty Win32 clipboard", GetLastError());
181
182 // - Set the new clipboard data
183 if (!SetClipboardData(CF_TEXT, clip_handle))
184 throw rdr::SystemException("unable to set Win32 clipboard", GetLastError());
185 clip_handle = 0;
186
187 vlog.debug("set clipboard");
188 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000189 vlog.debug("%s", e.str());
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000190 }
191
192 // - Close the clipboard
193 if (!CloseClipboard())
Pierre Ossmanfb450fb2015-03-03 16:34:56 +0100194 vlog.debug("unable to close Win32 clipboard: %lu", GetLastError());
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000195 else
196 vlog.debug("closed clipboard");
197 if (clip_handle) {
198 vlog.debug("freeing clipboard handle");
199 GlobalFree(clip_handle);
200 }
201}