1 /*
2  * synergy -- 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 #pragma once
20 
21 #include "platform/MSWindowsClipboardFacade.h"
22 #include "synergy/IClipboard.h"
23 #include "common/stdvector.h"
24 
25 #define WIN32_LEAN_AND_MEAN
26 #include <Windows.h>
27 
28 class IMSWindowsClipboardConverter;
29 class IMSWindowsClipboardFacade;
30 
31 //! Microsoft windows clipboard implementation
32 class MSWindowsClipboard : public IClipboard {
33 public:
34     MSWindowsClipboard(HWND window);
35     MSWindowsClipboard(HWND window, IMSWindowsClipboardFacade &facade);
36     virtual ~MSWindowsClipboard();
37 
38     //! Empty clipboard without ownership
39     /*!
40     Take ownership of the clipboard and clear all data from it.
41     This must be called between a successful open() and close().
42     Return false if the clipboard ownership could not be taken;
43     the clipboard should not be emptied in this case.  Unlike
44     empty(), isOwnedBySynergy() will return false when emptied
45     this way.  This is useful when synergy wants to put data on
46     clipboard but pretend (to itself) that some other app did it.
47     When using empty(), synergy assumes the data came from the
48     server and doesn't need to be sent back.  emptyUnowned()
49     makes synergy send the data to the server.
50     */
51     bool                emptyUnowned();
52 
53     //! Test if clipboard is owned by synergy
54     static bool            isOwnedBySynergy();
55 
56     // IClipboard overrides
57     virtual bool        empty();
58     virtual void        add(EFormat, const String& data);
59     virtual bool        open(Time) const;
60     virtual void        close() const;
61     virtual Time        getTime() const;
62     virtual bool        has(EFormat) const;
63     virtual String        get(EFormat) const;
64 
65     void setFacade(IMSWindowsClipboardFacade& facade);
66 
67 private:
68     void                clearConverters();
69 
70     UINT                convertFormatToWin32(EFormat) const;
71     HANDLE                convertTextToWin32(const String& data) const;
72     String                convertTextFromWin32(HANDLE) const;
73 
74     static UINT            getOwnershipFormat();
75 
76 private:
77     typedef std::vector<IMSWindowsClipboardConverter*> ConverterList;
78 
79     HWND                m_window;
80     mutable Time        m_time;
81     ConverterList        m_converters;
82     static UINT            s_ownershipFormat;
83     IMSWindowsClipboardFacade* m_facade;
84     bool m_deleteFacade;
85 };
86 
87 //! Clipboard format converter interface
88 /*!
89 This interface defines the methods common to all win32 clipboard format
90 converters.
91 */
92 class IMSWindowsClipboardConverter : public IInterface {
93 public:
94     // accessors
95 
96     // return the clipboard format this object converts from/to
97     virtual IClipboard::EFormat
98                         getFormat() const = 0;
99 
100     // return the atom representing the win32 clipboard format that
101     // this object converts from/to
102     virtual UINT        getWin32Format() const = 0;
103 
104     // convert from the IClipboard format to the win32 clipboard format.
105     // the input data must be in the IClipboard format returned by
106     // getFormat().  the return data will be in the win32 clipboard
107     // format returned by getWin32Format(), allocated by GlobalAlloc().
108     virtual HANDLE        fromIClipboard(const String&) const = 0;
109 
110     // convert from the win32 clipboard format to the IClipboard format
111     // (i.e., the reverse of fromIClipboard()).
112     virtual String        toIClipboard(HANDLE data) const = 0;
113 };
114