CMSWindowsScreen.h

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

Generated on Fri Nov 6 00:18:45 2009 for synergy-plus by  doxygen 1.4.7