You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Clipboard.cxx 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. // -=- Clipboard.cxx
  19. #include <rfb_win32/Clipboard.h>
  20. #include <rfb_win32/WMShatter.h>
  21. #include <rfb/util.h>
  22. #include <rfb/LogWriter.h>
  23. using namespace rfb;
  24. using namespace rfb::win32;
  25. static LogWriter vlog("Clipboard");
  26. //
  27. // -=- CR/LF handlers
  28. //
  29. char*
  30. dos2unix(const char* text) {
  31. int len = strlen(text)+1;
  32. char* unix = new char[strlen(text)+1];
  33. int i, j=0;
  34. for (i=0; i<len; i++) {
  35. if (text[i] != '\x0d')
  36. unix[j++] = text[i];
  37. }
  38. return unix;
  39. }
  40. char*
  41. unix2dos(const char* text) {
  42. int len = strlen(text)+1;
  43. char* dos = new char[strlen(text)*2+1];
  44. int i, j=0;
  45. for (i=0; i<len; i++) {
  46. if (text[i] == '\x0a')
  47. dos[j++] = '\x0d';
  48. dos[j++] = text[i];
  49. }
  50. return dos;
  51. }
  52. //
  53. // -=- ISO-8859-1 (Latin 1) filter (in-place)
  54. //
  55. void
  56. removeNonISOLatin1Chars(char* text) {
  57. int len = strlen(text);
  58. int i=0, j=0;
  59. for (; i<len; i++) {
  60. if (((text[i] >= 1) && (text[i] <= 127)) ||
  61. ((text[i] >= 160) && (text[i] <= 255)))
  62. text[j++] = text[i];
  63. }
  64. text[j] = 0;
  65. }
  66. //
  67. // -=- Clipboard object
  68. //
  69. Clipboard::Clipboard()
  70. : MsgWindow(_T("Clipboard")), notifier(0), next_window(0) {
  71. next_window = SetClipboardViewer(getHandle());
  72. vlog.debug("registered clipboard handler");
  73. }
  74. Clipboard::~Clipboard() {
  75. vlog.debug("removing %p from chain (next is %p)", getHandle(), next_window);
  76. ChangeClipboardChain(getHandle(), next_window);
  77. }
  78. LRESULT
  79. Clipboard::processMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
  80. switch (msg) {
  81. case WM_CHANGECBCHAIN:
  82. vlog.debug("change clipboard chain (%I64x, %I64x)",
  83. (long long)wParam, (long long)lParam);
  84. if ((HWND) wParam == next_window)
  85. next_window = (HWND) lParam;
  86. else if (next_window != 0)
  87. SendMessage(next_window, msg, wParam, lParam);
  88. else
  89. vlog.error("bad clipboard chain change!");
  90. break;
  91. case WM_DRAWCLIPBOARD:
  92. {
  93. HWND owner = GetClipboardOwner();
  94. if (owner == getHandle()) {
  95. vlog.debug("local clipboard changed by me");
  96. } else {
  97. vlog.debug("local clipboard changed by %p", owner);
  98. // Open the clipboard
  99. if (OpenClipboard(getHandle())) {
  100. // Get the clipboard data
  101. HGLOBAL cliphandle = GetClipboardData(CF_TEXT);
  102. if (cliphandle) {
  103. char* clipdata = (char*) GlobalLock(cliphandle);
  104. // Notify clients
  105. if (notifier) {
  106. if (!clipdata) {
  107. notifier->notifyClipboardChanged(0, 0);
  108. } else {
  109. CharArray unix_text;
  110. unix_text.buf = dos2unix(clipdata);
  111. removeNonISOLatin1Chars(unix_text.buf);
  112. notifier->notifyClipboardChanged(unix_text.buf, strlen(unix_text.buf));
  113. }
  114. } else {
  115. vlog.debug("no clipboard notifier registered");
  116. }
  117. // Release the buffer and close the clipboard
  118. GlobalUnlock(cliphandle);
  119. }
  120. CloseClipboard();
  121. }
  122. }
  123. }
  124. if (next_window)
  125. SendMessage(next_window, msg, wParam, lParam);
  126. return 0;
  127. };
  128. return MsgWindow::processMessage(msg, wParam, lParam);
  129. };
  130. void
  131. Clipboard::setClipText(const char* text) {
  132. HANDLE clip_handle = 0;
  133. try {
  134. // - Firstly, we must open the clipboard
  135. if (!OpenClipboard(getHandle()))
  136. throw rdr::SystemException("unable to open Win32 clipboard", GetLastError());
  137. // - Pre-process the supplied clipboard text into DOS format
  138. CharArray dos_text;
  139. dos_text.buf = unix2dos(text);
  140. removeNonISOLatin1Chars(dos_text.buf);
  141. int dos_text_len = strlen(dos_text.buf);
  142. // - Allocate global memory for the data
  143. clip_handle = ::GlobalAlloc(GMEM_MOVEABLE, dos_text_len+1);
  144. char* data = (char*) GlobalLock(clip_handle);
  145. memcpy(data, dos_text.buf, dos_text_len+1);
  146. data[dos_text_len] = 0;
  147. GlobalUnlock(clip_handle);
  148. // - Next, we must clear out any existing data
  149. if (!EmptyClipboard())
  150. throw rdr::SystemException("unable to empty Win32 clipboard", GetLastError());
  151. // - Set the new clipboard data
  152. if (!SetClipboardData(CF_TEXT, clip_handle))
  153. throw rdr::SystemException("unable to set Win32 clipboard", GetLastError());
  154. clip_handle = 0;
  155. vlog.debug("set clipboard");
  156. } catch (rdr::Exception& e) {
  157. vlog.debug("%s", e.str());
  158. }
  159. // - Close the clipboard
  160. if (!CloseClipboard())
  161. vlog.debug("unable to close Win32 clipboard: %lu", GetLastError());
  162. else
  163. vlog.debug("closed clipboard");
  164. if (clip_handle) {
  165. vlog.debug("freeing clipboard handle");
  166. GlobalFree(clip_handle);
  167. }
  168. }