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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23. #include <rfb_win32/Clipboard.h>
  24. #include <rfb_win32/WMShatter.h>
  25. #include <rfb/util.h>
  26. #include <rfb/LogWriter.h>
  27. using namespace rfb;
  28. using namespace rfb::win32;
  29. static LogWriter vlog("Clipboard");
  30. //
  31. // -=- Clipboard object
  32. //
  33. Clipboard::Clipboard()
  34. : MsgWindow("Clipboard"), notifier(0), next_window(0) {
  35. next_window = SetClipboardViewer(getHandle());
  36. vlog.debug("registered clipboard handler");
  37. }
  38. Clipboard::~Clipboard() {
  39. vlog.debug("removing %p from chain (next is %p)", getHandle(), next_window);
  40. ChangeClipboardChain(getHandle(), next_window);
  41. }
  42. LRESULT
  43. Clipboard::processMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
  44. switch (msg) {
  45. case WM_CHANGECBCHAIN:
  46. vlog.debug("change clipboard chain (%I64x, %I64x)",
  47. (long long)wParam, (long long)lParam);
  48. if ((HWND) wParam == next_window)
  49. next_window = (HWND) lParam;
  50. else if (next_window != 0)
  51. SendMessage(next_window, msg, wParam, lParam);
  52. else
  53. vlog.error("bad clipboard chain change!");
  54. break;
  55. case WM_DRAWCLIPBOARD:
  56. {
  57. HWND owner = GetClipboardOwner();
  58. if (owner == getHandle()) {
  59. vlog.debug("local clipboard changed by me");
  60. } else {
  61. vlog.debug("local clipboard changed by %p", owner);
  62. if (notifier == NULL)
  63. vlog.debug("no clipboard notifier registered");
  64. else
  65. notifier->notifyClipboardChanged(IsClipboardFormatAvailable(CF_UNICODETEXT));
  66. }
  67. }
  68. if (next_window)
  69. SendMessage(next_window, msg, wParam, lParam);
  70. return 0;
  71. };
  72. return MsgWindow::processMessage(msg, wParam, lParam);
  73. };
  74. std::string
  75. Clipboard::getClipText() {
  76. HGLOBAL cliphandle;
  77. wchar_t* clipdata;
  78. std::string utf8;
  79. // Open the clipboard
  80. if (!OpenClipboard(getHandle()))
  81. return NULL;
  82. // Get the clipboard data
  83. cliphandle = GetClipboardData(CF_UNICODETEXT);
  84. if (!cliphandle) {
  85. CloseClipboard();
  86. return NULL;
  87. }
  88. clipdata = (wchar_t*) GlobalLock(cliphandle);
  89. if (!clipdata) {
  90. CloseClipboard();
  91. return NULL;
  92. }
  93. // Convert it to UTF-8
  94. utf8 = utf16ToUTF8(clipdata);
  95. // Release the buffer and close the clipboard
  96. GlobalUnlock(cliphandle);
  97. CloseClipboard();
  98. return convertLF(utf8.c_str());
  99. }
  100. void
  101. Clipboard::setClipText(const char* text) {
  102. HANDLE clip_handle = 0;
  103. try {
  104. // - Firstly, we must open the clipboard
  105. if (!OpenClipboard(getHandle()))
  106. throw rdr::SystemException("unable to open Win32 clipboard", GetLastError());
  107. // - Convert the supplied clipboard text into UTF-16 format with CRLF
  108. std::string filtered(convertCRLF(text));
  109. std::wstring utf16(utf8ToUTF16(filtered.c_str()));
  110. // - Allocate global memory for the data
  111. clip_handle = ::GlobalAlloc(GMEM_MOVEABLE, (utf16.size() + 1) * 2);
  112. wchar_t* data = (wchar_t*) GlobalLock(clip_handle);
  113. wcscpy(data, utf16.c_str());
  114. GlobalUnlock(clip_handle);
  115. // - Next, we must clear out any existing data
  116. if (!EmptyClipboard())
  117. throw rdr::SystemException("unable to empty Win32 clipboard", GetLastError());
  118. // - Set the new clipboard data
  119. if (!SetClipboardData(CF_UNICODETEXT, clip_handle))
  120. throw rdr::SystemException("unable to set Win32 clipboard", GetLastError());
  121. clip_handle = 0;
  122. vlog.debug("set clipboard");
  123. } catch (rdr::Exception& e) {
  124. vlog.debug("%s", e.str());
  125. }
  126. // - Close the clipboard
  127. if (!CloseClipboard())
  128. vlog.debug("unable to close Win32 clipboard: %lu", GetLastError());
  129. else
  130. vlog.debug("closed clipboard");
  131. if (clip_handle) {
  132. vlog.debug("freeing clipboard handle");
  133. GlobalFree(clip_handle);
  134. }
  135. }