aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/platform/MSWindowsDesks.h
blob: da93c34adbf0ff8b9f31ebc355f309aba98f62a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * barrier -- mouse and keyboard sharing utility
 * Copyright (C) 2018 Debauchee Open Source Group
 * Copyright (C) 2012-2016 Symless Ltd.
 * Copyright (C) 2004 Chris Schoeneman
 * 
 * This package is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * found in the file LICENSE that should have accompanied this file.
 * 
 * This package is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "platform/synwinhk.h"
#include "barrier/key_types.h"
#include "barrier/mouse_types.h"
#include "barrier/option_types.h"
#include "mt/CondVar.h"
#include "mt/Mutex.h"
#include "base/String.h"
#include "common/stdmap.h"

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

class Event;
class EventQueueTimer;
class Thread;
class IJob;
class IScreenSaver;
class IEventQueue;

//! Microsoft Windows desk handling
/*!
Desks in Microsoft Windows are only remotely like desktops on X11
systems.  A desk is another virtual surface for windows but desks
impose serious restrictions:  a thread can interact with only one
desk at a time, you can't switch desks if the thread has any hooks
installed or owns any windows, windows cannot exist on multiple
desks at once, etc.  Basically, they're useless except for running
the login window or the screensaver, which is what they're used
for.  Barrier must deal with them mainly because of the login
window and screensaver but users can create their own desks and
barrier should work on those too.

This class encapsulates all the desk nastiness.  Clients of this
object don't have to know anything about desks.
*/
class MSWindowsDesks {
public:
    //! Constructor
    /*!
    \p isPrimary is true iff the desk is for a primary screen.
    \p screensaver points to a screensaver object and it's used
    only to check if the screensaver is active.  The \p updateKeys
    job is adopted and is called when the key state should be
    updated in a thread attached to the current desk.
    \p hookLibrary must be a handle to the hook library.
    */
    MSWindowsDesks(
        bool isPrimary, bool noHooks,
        const IScreenSaver* screensaver, IEventQueue* events,
        IJob* updateKeys, bool stopOnDeskSwitch);
    ~MSWindowsDesks();

    //! @name manipulators
    //@{

    //! Enable desk tracking
    /*!
    Enables desk tracking.  While enabled, this object checks to see
    if the desk has changed and ensures that the hooks are installed
    on the new desk.  \c setShape should be called at least once
    before calling \c enable.
    */
    void                enable();

    //! Disable desk tracking
    /*!
    Disables desk tracking.  \sa enable.
    */
    void                disable();

    //! Notify of entering a desk
    /*!
    Prepares a desk for when the cursor enters it.
    */
    void                enter();

    //! Notify of leaving a desk
    /*!
    Prepares a desk for when the cursor leaves it.
    */
    void                leave(HKL keyLayout);

    //! Notify of options changes
    /*!
    Resets all options to their default values.
    */
    void                resetOptions();

    //! Notify of options changes
    /*!
    Set options to given values.  Ignores unknown options and doesn't
    modify options that aren't given in \c options.
    */
    void                setOptions(const OptionsList& options);

    //! Update the key state
    /*!
    Causes the key state to get updated to reflect the physical keyboard
    state and current keyboard mapping.
    */
    void                updateKeys();

    //! Tell desk about new size
    /*!
    This tells the desks that the display size has changed.
    */
    void                setShape(SInt32 x, SInt32 y,
                            SInt32 width, SInt32 height,
                            SInt32 xCenter, SInt32 yCenter, bool isMultimon);

    //! Install/uninstall screensaver hooks
    /*!
    If \p install is true then the screensaver hooks are installed and,
    if desk tracking is enabled, updated whenever the desk changes.  If
    \p install is false then the screensaver hooks are uninstalled.
    */
    void                installScreensaverHooks(bool install);

    //! Start ignoring user input
    /*!
    Starts ignoring user input so we don't pick up our own synthesized events.
    */
    void                fakeInputBegin();

    //! Stop ignoring user input
    /*!
    Undoes whatever \c fakeInputBegin() did.
    */
    void                fakeInputEnd();

    //@}
    //! @name accessors
    //@{

    //! Get cursor position
    /*!
    Return the current position of the cursor in \c x and \c y.
    */
    void                getCursorPos(SInt32& x, SInt32& y) const;

    //! Fake key press/release
    /*!
    Synthesize a press or release of key \c button.
    */
    void                fakeKeyEvent(KeyButton button, UINT virtualKey,
                            bool press, bool isAutoRepeat) const;

    //! Fake mouse press/release
    /*!
    Synthesize a press or release of mouse button \c id.
    */
    void                fakeMouseButton(ButtonID id, bool press);

    //! Fake mouse move
    /*!
    Synthesize a mouse move to the absolute coordinates \c x,y.
    */
    void                fakeMouseMove(SInt32 x, SInt32 y) const;

    //! Fake mouse move
    /*!
    Synthesize a mouse move to the relative coordinates \c dx,dy.
    */
    void                fakeMouseRelativeMove(SInt32 dx, SInt32 dy) const;

    //! Fake mouse wheel
    /*!
    Synthesize a mouse wheel event of amount \c delta in direction \c axis.
    */
    void                fakeMouseWheel(SInt32 xDelta, SInt32 yDelta) const;

    //@}

private:
    class Desk {
    public:
        String            m_name;
        Thread*        m_thread;
        DWORD            m_threadID;
        DWORD            m_targetID;
        HDESK            m_desk;
        HWND            m_window;
        HWND            m_foregroundWindow;
        bool            m_lowLevel;
    };
    typedef std::map<String, Desk*> Desks;

    // initialization and shutdown operations
    HCURSOR                createBlankCursor() const;
    void                destroyCursor(HCURSOR cursor) const;
    ATOM                createDeskWindowClass(bool isPrimary) const;
    void                destroyClass(ATOM windowClass) const;
    HWND                createWindow(ATOM windowClass, const char* name) const;
    void                destroyWindow(HWND) const;

    // message handlers
    void                deskMouseMove(SInt32 x, SInt32 y) const;
    void                deskMouseRelativeMove(SInt32 dx, SInt32 dy) const;
    void                deskEnter(Desk* desk);
    void                deskLeave(Desk* desk, HKL keyLayout);
    void                deskThread(void* vdesk);

    // desk switch checking and handling
    Desk*                addDesk(const String& name, HDESK hdesk);
    void                removeDesks();
    void                checkDesk();
    bool                isDeskAccessible(const Desk* desk) const;
    void                handleCheckDesk(const Event& event, void*);

    // communication with desk threads
    void                waitForDesk() const;
    void                sendMessage(UINT, WPARAM, LPARAM) const;

    // work around for messed up keyboard events from low-level hooks
    HWND                getForegroundWindow() const;

    // desk API wrappers
    HDESK                openInputDesktop();
    void                closeDesktop(HDESK);
    String                getDesktopName(HDESK);

    // our desk window procs
    static LRESULT CALLBACK primaryDeskProc(HWND, UINT, WPARAM, LPARAM);
    static LRESULT CALLBACK secondaryDeskProc(HWND, UINT, WPARAM, LPARAM);

private:
    // true if screen is being used as a primary screen, false otherwise
    bool                m_isPrimary;

    // true if hooks are not to be installed (useful for debugging)
    bool                m_noHooks;

    // true if mouse has entered the screen
    bool                m_isOnScreen;

    // our resources
    ATOM                m_deskClass;
    HCURSOR                m_cursor;

    // screen shape stuff
    SInt32                m_x, m_y;
    SInt32                m_w, m_h;
    SInt32                m_xCenter, m_yCenter;

    // true if system appears to have multiple monitors
    bool                m_multimon;

    // the timer used to check for desktop switching
    EventQueueTimer*    m_timer;

    // screen saver stuff
    DWORD                m_threadID;
    const IScreenSaver*    m_screensaver;
    bool                m_screensaverNotify;

    // the current desk and it's name
    Desk*                m_activeDesk;
    String                m_activeDeskName;

    // one desk per desktop and a cond var to communicate with it
    Mutex                m_mutex;
    CondVar<bool>        m_deskReady;
    Desks                m_desks;

    // keyboard stuff
    IJob*                m_updateKeys;
    HKL                    m_keyLayout;

    // options
    bool                m_leaveForegroundOption;

    IEventQueue*        m_events;

    // true if program should stop on desk switch.
    bool                m_stopOnDeskSwitch;
};