1 /*
2  * barrier -- mouse and keyboard sharing utility
3  * Copyright (C) 2012-2016 Symless Ltd.
4  * Copyright (C) 2002 Chris Schoeneman
5  *
6  * This package is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * found in the file LICENSE that should have accompanied this file.
9  *
10  * This package is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "platform/XWindowsClipboardTextConverter.h"
20 
21 #include "base/Unicode.h"
22 
23 //
24 // XWindowsClipboardTextConverter
25 //
26 
XWindowsClipboardTextConverter(Display * display,const char * name)27 XWindowsClipboardTextConverter::XWindowsClipboardTextConverter(
28                 Display* display, const char* name) :
29     m_atom(XInternAtom(display, name, False))
30 {
31     // do nothing
32 }
33 
~XWindowsClipboardTextConverter()34 XWindowsClipboardTextConverter::~XWindowsClipboardTextConverter()
35 {
36     // do nothing
37 }
38 
39 IClipboard::EFormat
getFormat() const40 XWindowsClipboardTextConverter::getFormat() const
41 {
42     return IClipboard::kText;
43 }
44 
45 Atom
getAtom() const46 XWindowsClipboardTextConverter::getAtom() const
47 {
48     return m_atom;
49 }
50 
51 int
getDataSize() const52 XWindowsClipboardTextConverter::getDataSize() const
53 {
54     return 8;
55 }
56 
fromIClipboard(const std::string & data) const57 std::string XWindowsClipboardTextConverter::fromIClipboard(const std::string& data) const
58 {
59     return Unicode::UTF8ToText(data);
60 }
61 
toIClipboard(const std::string & data) const62 std::string XWindowsClipboardTextConverter::toIClipboard(const std::string& data) const
63 {
64     // convert to UTF-8
65     bool errors;
66     std::string utf8 = Unicode::textToUTF8(data, &errors);
67 
68     // if there were decoding errors then, to support old applications
69     // that don't understand UTF-8 but can report the exact binary
70     // UTF-8 representation, see if the data appears to be UTF-8.  if
71     // so then use it as is.
72     if (errors && Unicode::isUTF8(data)) {
73         return data;
74     }
75 
76     return utf8;
77 }
78