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