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.0KB

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