• Main Page
  • Classes
  • Files
  • File List

CMSWindowsScreen.h

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2012 Bolton Software Ltd.
00004  * Copyright (C) 2002 Chris Schoeneman
00005  * 
00006  * This package is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * found in the file COPYING that should have accompanied this file.
00009  * 
00010  * This package is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #ifndef CMSWINDOWSSCREEN_H
00020 #define CMSWINDOWSSCREEN_H
00021 
00022 #include "CPlatformScreen.h"
00023 #include "CSynergyHook.h"
00024 #include "CCondVar.h"
00025 #include "CMutex.h"
00026 #include "CString.h"
00027 #include "CMSWindowsHookLibraryLoader.h"
00028 #include "CGameDevice.h"
00029 #include "CMSWindowsXInput.h"
00030 #include "CEventGameDevice.h"
00031 
00032 #define WIN32_LEAN_AND_MEAN
00033 #include <windows.h>
00034 
00035 class CEventQueueTimer;
00036 class CMSWindowsDesks;
00037 class CMSWindowsKeyState;
00038 class CMSWindowsScreenSaver;
00039 class CThread;
00040 
00042 class CMSWindowsScreen : public CPlatformScreen {
00043 public:
00044     CMSWindowsScreen(
00045         bool isPrimary,
00046         bool noHooks,
00047         const CGameDeviceInfo &gameDevice,
00048         bool stopOnDeskSwitch);
00049     virtual ~CMSWindowsScreen();
00050 
00052 
00053 
00055 
00059     static void         init(HINSTANCE);
00060 
00062 
00063 
00064 
00066 
00069     static HINSTANCE    getWindowInstance();
00070 
00072 
00073     // IScreen overrides
00074     virtual void*       getEventTarget() const;
00075     virtual bool        getClipboard(ClipboardID id, IClipboard*) const;
00076     virtual void        getShape(SInt32& x, SInt32& y,
00077                             SInt32& width, SInt32& height) const;
00078     virtual void        getCursorPos(SInt32& x, SInt32& y) const;
00079 
00080     // IPrimaryScreen overrides
00081     virtual void        reconfigure(UInt32 activeSides);
00082     virtual void        warpCursor(SInt32 x, SInt32 y);
00083     virtual UInt32      registerHotKey(KeyID key,
00084                             KeyModifierMask mask);
00085     virtual void        unregisterHotKey(UInt32 id);
00086     virtual void        fakeInputBegin();
00087     virtual void        fakeInputEnd();
00088     virtual SInt32      getJumpZoneSize() const;
00089     virtual bool        isAnyMouseButtonDown() const;
00090     virtual void        getCursorCenter(SInt32& x, SInt32& y) const;
00091     virtual void        gameDeviceTimingResp(UInt16 freq);
00092     virtual void        gameDeviceFeedback(GameDeviceID id, UInt16 m1, UInt16 m2);
00093 
00094     // ISecondaryScreen overrides
00095     virtual void        fakeMouseButton(ButtonID id, bool press);
00096     virtual void        fakeMouseMove(SInt32 x, SInt32 y) const;
00097     virtual void        fakeMouseRelativeMove(SInt32 dx, SInt32 dy) const;
00098     virtual void        fakeMouseWheel(SInt32 xDelta, SInt32 yDelta) const;
00099     virtual void        fakeGameDeviceButtons(GameDeviceID id, GameDeviceButton buttons) const;
00100     virtual void        fakeGameDeviceSticks(GameDeviceID id, SInt16 x1, SInt16 y1, SInt16 x2, SInt16 y2) const;
00101     virtual void        fakeGameDeviceTriggers(GameDeviceID id, UInt8 t1, UInt8 t2) const;
00102     virtual void        queueGameDeviceTimingReq() const;
00103 
00104     // IKeyState overrides
00105     virtual void        updateKeys();
00106     virtual void        fakeKeyDown(KeyID id, KeyModifierMask mask,
00107                             KeyButton button);
00108     virtual bool        fakeKeyRepeat(KeyID id, KeyModifierMask mask,
00109                             SInt32 count, KeyButton button);
00110     virtual bool        fakeKeyUp(KeyButton button);
00111     virtual void        fakeAllKeysUp();
00112 
00113     // IPlatformScreen overrides
00114     virtual void        enable();
00115     virtual void        disable();
00116     virtual void        enter();
00117     virtual bool        leave();
00118     virtual bool        setClipboard(ClipboardID, const IClipboard*);
00119     virtual void        checkClipboards();
00120     virtual void        openScreensaver(bool notify);
00121     virtual void        closeScreensaver();
00122     virtual void        screensaver(bool activate);
00123     virtual void        resetOptions();
00124     virtual void        setOptions(const COptionsList& options);
00125     virtual void        setSequenceNumber(UInt32);
00126     virtual bool        isPrimary() const;
00127 
00128 protected:
00129     // IPlatformScreen overrides
00130     virtual void        handleSystemEvent(const CEvent&, void*);
00131     virtual void        updateButtons();
00132     virtual IKeyState*  getKeyState() const;
00133 
00134 private:
00135     // initialization and shutdown operations
00136     HINSTANCE           openHookLibrary(const char* name);
00137     void                closeHookLibrary(HINSTANCE hookLibrary) const;
00138     HCURSOR             createBlankCursor() const;
00139     void                destroyCursor(HCURSOR cursor) const;
00140     ATOM                createWindowClass() const;
00141     ATOM                createDeskWindowClass(bool isPrimary) const;
00142     void                destroyClass(ATOM windowClass) const;
00143     HWND                createWindow(ATOM windowClass, const char* name) const;
00144     void                destroyWindow(HWND) const;
00145 
00146     // convenience function to send events
00147 public: // HACK
00148     void                sendEvent(CEvent::Type type, void* = NULL);
00149 private: // HACK
00150     void                sendClipboardEvent(CEvent::Type type, ClipboardID id);
00151 
00152     // handle message before it gets dispatched.  returns true iff
00153     // the message should not be dispatched.
00154     bool                onPreDispatch(HWND, UINT, WPARAM, LPARAM);
00155 
00156     // handle message before it gets dispatched.  returns true iff
00157     // the message should not be dispatched.
00158     bool                onPreDispatchPrimary(HWND, UINT, WPARAM, LPARAM);
00159 
00160     // handle message.  returns true iff handled and optionally sets
00161     // \c *result (which defaults to 0).
00162     bool                onEvent(HWND, UINT, WPARAM, LPARAM, LRESULT* result);
00163 
00164     // message handlers
00165     bool                onMark(UInt32 mark);
00166     bool                onKey(WPARAM, LPARAM);
00167     bool                onHotKey(WPARAM, LPARAM);
00168     bool                onMouseButton(WPARAM, LPARAM);
00169     bool                onMouseMove(SInt32 x, SInt32 y);
00170     bool                onMouseWheel(SInt32 xDelta, SInt32 yDelta);
00171     bool                onScreensaver(bool activated);
00172     bool                onDisplayChange();
00173     bool                onClipboardChange();
00174 
00175     // warp cursor without discarding queued events
00176     void                warpCursorNoFlush(SInt32 x, SInt32 y);
00177 
00178     // discard posted messages
00179     void                nextMark();
00180 
00181     // test if event should be ignored
00182     bool                ignore() const;
00183 
00184     // update screen size cache
00185     void                updateScreenShape();
00186 
00187     // fix timer callback
00188     void                handleFixes(const CEvent&, void*);
00189 
00190     // fix the clipboard viewer chain
00191     void                fixClipboardViewer();
00192 
00193     // enable/disable special key combinations so we can catch/pass them
00194     void                enableSpecialKeys(bool) const;
00195 
00196     // map a button event to a button ID
00197     ButtonID            mapButtonFromEvent(WPARAM msg, LPARAM button) const;
00198 
00199     // map a button event to a press (true) or release (false)
00200     bool                mapPressFromEvent(WPARAM msg, LPARAM button) const;
00201 
00202     // job to update the key state
00203     void                updateKeysCB(void*);
00204 
00205     // determine whether the mouse is hidden by the system and force
00206     // it to be displayed if user has entered this secondary screen.
00207     void                forceShowCursor();
00208 
00209     // forceShowCursor uses MouseKeys to show the cursor.  since we
00210     // don't actually want MouseKeys behavior we have to make sure
00211     // it applies when NumLock is in whatever state it's not in now.
00212     // this method does that.
00213     void                updateForceShowCursor();
00214 
00215     // our window proc
00216     static LRESULT CALLBACK wndProc(HWND, UINT, WPARAM, LPARAM);
00217 
00218 private:
00219     struct CHotKeyItem {
00220     public:
00221         CHotKeyItem(UINT vk, UINT modifiers);
00222 
00223         UINT            getVirtualKey() const;
00224 
00225         bool            operator<(const CHotKeyItem&) const;
00226 
00227     private:
00228         UINT            m_keycode;
00229         UINT            m_mask;
00230     };
00231     typedef std::map<UInt32, CHotKeyItem> HotKeyMap;
00232     typedef std::vector<UInt32> HotKeyIDList;
00233     typedef std::map<CHotKeyItem, UInt32> HotKeyToIDMap;
00234 
00235     static HINSTANCE    s_windowInstance;
00236 
00237     // true if screen is being used as a primary screen, false otherwise
00238     bool                m_isPrimary;
00239 
00240     // true if hooks are not to be installed (useful for debugging)
00241     bool                m_noHooks;
00242 
00243     // true if windows 95/98/me
00244     bool                m_is95Family;
00245 
00246     // true if mouse has entered the screen
00247     bool                m_isOnScreen;
00248 
00249     // our resources
00250     ATOM                m_class;
00251 
00252     // screen shape stuff
00253     SInt32              m_x, m_y;
00254     SInt32              m_w, m_h;
00255     SInt32              m_xCenter, m_yCenter;
00256 
00257     // true if system appears to have multiple monitors
00258     bool                m_multimon;
00259 
00260     // last mouse position
00261     SInt32              m_xCursor, m_yCursor;
00262 
00263     // last clipboard
00264     UInt32              m_sequenceNumber;
00265 
00266     // used to discard queued messages that are no longer needed
00267     UInt32              m_mark;
00268     UInt32              m_markReceived;
00269 
00270     // the main loop's thread id
00271     DWORD               m_threadID;
00272 
00273     // timer for periodically checking stuff that requires polling
00274     CEventQueueTimer*   m_fixTimer;
00275 
00276     // the keyboard layout to use when off primary screen
00277     HKL                 m_keyLayout;
00278 
00279     // screen saver stuff
00280     CMSWindowsScreenSaver*  m_screensaver;
00281     bool                    m_screensaverNotify;
00282     bool                    m_screensaverActive;
00283 
00284     // clipboard stuff.  our window is used mainly as a clipboard
00285     // owner and as a link in the clipboard viewer chain.
00286     HWND                m_window;
00287     HWND                m_nextClipboardWindow;
00288     bool                m_ownClipboard;
00289 
00290     // one desk per desktop and a cond var to communicate with it
00291     CMSWindowsDesks*    m_desks;
00292 
00293     // hook library stuff
00294     HINSTANCE           m_hookLibrary;
00295 
00296     // keyboard stuff
00297     CMSWindowsKeyState* m_keyState;
00298 
00299     // hot key stuff
00300     HotKeyMap           m_hotKeys;
00301     HotKeyIDList        m_oldHotKeyIDs;
00302     HotKeyToIDMap       m_hotKeyToIDMap;
00303 
00304     // map of button state
00305     bool                m_buttons[1 + kButtonExtra0 + 1];
00306 
00307     // the system shows the mouse cursor when an internal display count
00308     // is >= 0.  this count is maintained per application but there's
00309     // apparently a system wide count added to the application's count.
00310     // this system count is 0 if there's a mouse attached to the system
00311     // and -1 otherwise.  the MouseKeys accessibility feature can modify
00312     // this system count by making the system appear to have a mouse.
00313     //
00314     // m_hasMouse is true iff there's a mouse attached to the system or
00315     // MouseKeys is simulating one.  we track this so we can force the
00316     // cursor to be displayed when the user has entered this screen.
00317     // m_showingMouse is true when we're doing that.
00318     bool                m_hasMouse;
00319     bool                m_showingMouse;
00320     bool                m_gotOldMouseKeys;
00321     MOUSEKEYS           m_mouseKeys;
00322     MOUSEKEYS           m_oldMouseKeys;
00323 
00324     // loads synrgyhk.dll
00325     CMSWindowsHookLibraryLoader
00326                         m_hookLibraryLoader;
00327 
00328     const CGameDeviceInfo&  m_gameDeviceInfo;
00329     CGameDevice*        m_gameDevice;
00330 
00331     static CMSWindowsScreen*    s_screen;
00332 
00333     // save last position of mouse to compute next delta movement
00334     void saveMousePosition(SInt32 x, SInt32 y);
00335 };
00336 
00337 #endif

Generated on Sun May 19 2013 00:00:05 for Synergy by  doxygen 1.7.1