blob: 96d1e942e26f409b02a84a2b4c51e9ecabc046a8 [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2004 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// -=- ASCII filter (in-place)
65//
66
67void
68removeNonAsciiChars(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[j++] = text[i];
74 }
75 text[j] = 0;
76}
77
78//
79// -=- Clipboard object
80//
81
82Clipboard::Clipboard()
83 : MsgWindow(_T("Clipboard")), notifier(0), next_window(0) {
84 next_window = SetClipboardViewer(getHandle());
85 vlog.debug("registered clipboard handler");
86}
87
88Clipboard::~Clipboard() {
89 vlog.debug("removing %x from chain (next is %x)", getHandle(), next_window);
90 ChangeClipboardChain(getHandle(), next_window);
91}
92
93LRESULT
94Clipboard::processMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
95 switch (msg) {
96
97 case WM_CHANGECBCHAIN:
98 vlog.debug("change clipboard chain (%x, %x)", wParam, lParam);
99 if ((HWND) wParam == next_window)
100 next_window = (HWND) lParam;
101 else if (next_window != 0)
102 SendMessage(next_window, msg, wParam, lParam);
103 else
104 vlog.error("bad clipboard chain change!");
105 break;
106
107 case WM_DRAWCLIPBOARD:
108 {
109 HWND owner = GetClipboardOwner();
110 if (owner == getHandle()) {
111 vlog.debug("local clipboard changed by me");
112 } else {
113 vlog.debug("local clipboard changed by %x", owner);
114
115 // Open the clipboard
116 if (OpenClipboard(getHandle())) {
117 // Get the clipboard data
118 HGLOBAL cliphandle = GetClipboardData(CF_TEXT);
119 if (cliphandle) {
120 char* clipdata = (char*) GlobalLock(cliphandle);
121
122 // Notify clients
123 if (notifier) {
124 if (!clipdata) {
125 notifier->notifyClipboardChanged(0, 0);
126 } else {
127 CharArray unix_text;
128 unix_text.buf = dos2unix(clipdata);
129 removeNonAsciiChars(unix_text.buf);
130 notifier->notifyClipboardChanged(unix_text.buf, strlen(unix_text.buf));
131 }
132 } else {
133 vlog.debug("no clipboard notifier registered");
134 }
135
136 // Release the buffer and close the clipboard
137 GlobalUnlock(cliphandle);
138 }
139
140 CloseClipboard();
141 }
142 }
143 }
144 if (next_window)
145 SendMessage(next_window, msg, wParam, lParam);
146 return 0;
147
148 };
149 return MsgWindow::processMessage(msg, wParam, lParam);
150};
151
152void
153Clipboard::setClipText(const char* text) {
154 HANDLE clip_handle = 0;
155
156 try {
157
158 // - Firstly, we must open the clipboard
159 if (!OpenClipboard(getHandle()))
160 throw rdr::SystemException("unable to open Win32 clipboard", GetLastError());
161
162 // - Pre-process the supplied clipboard text into DOS format
163 CharArray dos_text;
164 dos_text.buf = unix2dos(text);
165 removeNonAsciiChars(dos_text.buf);
166 int dos_text_len = strlen(dos_text.buf);
167
168 // - Allocate global memory for the data
169 clip_handle = ::GlobalAlloc(GMEM_MOVEABLE, dos_text_len+1);
170
171 char* data = (char*) GlobalLock(clip_handle);
172 memcpy(data, dos_text.buf, dos_text_len+1);
173 data[dos_text_len] = 0;
174 GlobalUnlock(clip_handle);
175
176 // - Next, we must clear out any existing data
177 if (!EmptyClipboard())
178 throw rdr::SystemException("unable to empty Win32 clipboard", GetLastError());
179
180 // - Set the new clipboard data
181 if (!SetClipboardData(CF_TEXT, clip_handle))
182 throw rdr::SystemException("unable to set Win32 clipboard", GetLastError());
183 clip_handle = 0;
184
185 vlog.debug("set clipboard");
186 } catch (rdr::Exception& e) {
187 vlog.debug(e.str());
188 }
189
190 // - Close the clipboard
191 if (!CloseClipboard())
192 vlog.debug("unable to close Win32 clipboard: %u", GetLastError());
193 else
194 vlog.debug("closed clipboard");
195 if (clip_handle) {
196 vlog.debug("freeing clipboard handle");
197 GlobalFree(clip_handle);
198 }
199}