Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 1 | /* 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 | |
| 27 | using namespace rfb; |
| 28 | using namespace rfb::win32; |
| 29 | |
| 30 | static LogWriter vlog("Clipboard"); |
| 31 | |
| 32 | |
| 33 | // |
| 34 | // -=- CR/LF handlers |
| 35 | // |
| 36 | |
| 37 | char* |
| 38 | dos2unix(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 | |
| 49 | char* |
| 50 | unix2dos(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 | |
| 67 | void |
| 68 | removeNonISOLatin1Chars(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 | |
| 83 | Clipboard::Clipboard() |
| 84 | : MsgWindow(_T("Clipboard")), notifier(0), next_window(0) { |
| 85 | next_window = SetClipboardViewer(getHandle()); |
| 86 | vlog.debug("registered clipboard handler"); |
| 87 | } |
| 88 | |
| 89 | Clipboard::~Clipboard() { |
| 90 | vlog.debug("removing %x from chain (next is %x)", getHandle(), next_window); |
| 91 | ChangeClipboardChain(getHandle(), next_window); |
| 92 | } |
| 93 | |
| 94 | LRESULT |
| 95 | Clipboard::processMessage(UINT msg, WPARAM wParam, LPARAM lParam) { |
| 96 | switch (msg) { |
| 97 | |
| 98 | case WM_CHANGECBCHAIN: |
| 99 | vlog.debug("change clipboard chain (%x, %x)", wParam, lParam); |
| 100 | if ((HWND) wParam == next_window) |
| 101 | next_window = (HWND) lParam; |
| 102 | else if (next_window != 0) |
| 103 | SendMessage(next_window, msg, wParam, lParam); |
| 104 | else |
| 105 | vlog.error("bad clipboard chain change!"); |
| 106 | break; |
| 107 | |
| 108 | case WM_DRAWCLIPBOARD: |
| 109 | { |
| 110 | HWND owner = GetClipboardOwner(); |
| 111 | if (owner == getHandle()) { |
| 112 | vlog.debug("local clipboard changed by me"); |
| 113 | } else { |
| 114 | vlog.debug("local clipboard changed by %x", owner); |
| 115 | |
| 116 | // Open the clipboard |
| 117 | if (OpenClipboard(getHandle())) { |
| 118 | // Get the clipboard data |
| 119 | HGLOBAL cliphandle = GetClipboardData(CF_TEXT); |
| 120 | if (cliphandle) { |
| 121 | char* clipdata = (char*) GlobalLock(cliphandle); |
| 122 | |
| 123 | // Notify clients |
| 124 | if (notifier) { |
| 125 | if (!clipdata) { |
| 126 | notifier->notifyClipboardChanged(0, 0); |
| 127 | } else { |
| 128 | CharArray unix_text; |
| 129 | unix_text.buf = dos2unix(clipdata); |
| 130 | removeNonISOLatin1Chars(unix_text.buf); |
| 131 | notifier->notifyClipboardChanged(unix_text.buf, strlen(unix_text.buf)); |
| 132 | } |
| 133 | } else { |
| 134 | vlog.debug("no clipboard notifier registered"); |
| 135 | } |
| 136 | |
| 137 | // Release the buffer and close the clipboard |
| 138 | GlobalUnlock(cliphandle); |
| 139 | } |
| 140 | |
| 141 | CloseClipboard(); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | if (next_window) |
| 146 | SendMessage(next_window, msg, wParam, lParam); |
| 147 | return 0; |
| 148 | |
| 149 | }; |
| 150 | return MsgWindow::processMessage(msg, wParam, lParam); |
| 151 | }; |
| 152 | |
| 153 | void |
| 154 | Clipboard::setClipText(const char* text) { |
| 155 | HANDLE clip_handle = 0; |
| 156 | |
| 157 | try { |
| 158 | |
| 159 | // - Firstly, we must open the clipboard |
| 160 | if (!OpenClipboard(getHandle())) |
| 161 | throw rdr::SystemException("unable to open Win32 clipboard", GetLastError()); |
| 162 | |
| 163 | // - Pre-process the supplied clipboard text into DOS format |
| 164 | CharArray dos_text; |
| 165 | dos_text.buf = unix2dos(text); |
| 166 | removeNonISOLatin1Chars(dos_text.buf); |
| 167 | int dos_text_len = strlen(dos_text.buf); |
| 168 | |
| 169 | // - Allocate global memory for the data |
| 170 | clip_handle = ::GlobalAlloc(GMEM_MOVEABLE, dos_text_len+1); |
| 171 | |
| 172 | char* data = (char*) GlobalLock(clip_handle); |
| 173 | memcpy(data, dos_text.buf, dos_text_len+1); |
| 174 | data[dos_text_len] = 0; |
| 175 | GlobalUnlock(clip_handle); |
| 176 | |
| 177 | // - Next, we must clear out any existing data |
| 178 | if (!EmptyClipboard()) |
| 179 | throw rdr::SystemException("unable to empty Win32 clipboard", GetLastError()); |
| 180 | |
| 181 | // - Set the new clipboard data |
| 182 | if (!SetClipboardData(CF_TEXT, clip_handle)) |
| 183 | throw rdr::SystemException("unable to set Win32 clipboard", GetLastError()); |
| 184 | clip_handle = 0; |
| 185 | |
| 186 | vlog.debug("set clipboard"); |
| 187 | } catch (rdr::Exception& e) { |
Pierre Ossman | ad8609a | 2012-04-26 09:04:14 +0000 | [diff] [blame] | 188 | vlog.debug("%s", e.str()); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // - Close the clipboard |
| 192 | if (!CloseClipboard()) |
| 193 | vlog.debug("unable to close Win32 clipboard: %u", GetLastError()); |
| 194 | else |
| 195 | vlog.debug("closed clipboard"); |
| 196 | if (clip_handle) { |
| 197 | vlog.debug("freeing clipboard handle"); |
| 198 | GlobalFree(clip_handle); |
| 199 | } |
| 200 | } |