• Main Page
  • Classes
  • Files
  • File List

COSXScreen.h

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2012 Bolton Software Ltd.
00004  * Copyright (C) 2004 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 COSXSCREEN_H
00020 #define COSXSCREEN_H
00021 
00022 #include <bitset>
00023 
00024 #include "stdmap.h"
00025 #include "stdvector.h"
00026 
00027 #include <Carbon/Carbon.h>
00028 #include "COSXClipboard.h"
00029 #include "CPlatformScreen.h"
00030 
00031 #include <mach/mach_port.h>
00032 #include <mach/mach_interface.h>
00033 #include <mach/mach_init.h>
00034 #include <IOKit/pwr_mgt/IOPMLib.h>
00035 #include <IOKit/IOMessage.h>
00036 
00037 extern "C" {
00038     typedef int CGSConnectionID;
00039     CGError CGSSetConnectionProperty(CGSConnectionID cid, CGSConnectionID targetCID, CFStringRef key, CFTypeRef value);
00040     int _CGSDefaultConnection();
00041 }    
00042 
00043 
00044 template <class T>
00045 class CCondVar;
00046 class CEventQueueTimer;
00047 class CMutex;
00048 class CThread;
00049 class COSXKeyState;
00050 class COSXScreenSaver;
00051 
00053 class COSXScreen : public CPlatformScreen {
00054 public:
00055     COSXScreen(bool isPrimary, bool autoShowHideCursor=true);
00056     virtual ~COSXScreen();
00057 
00058     // IScreen overrides
00059     virtual void*       getEventTarget() const;
00060     virtual bool        getClipboard(ClipboardID id, IClipboard*) const;
00061     virtual void        getShape(SInt32& x, SInt32& y,
00062                             SInt32& width, SInt32& height) const;
00063     virtual void        getCursorPos(SInt32& x, SInt32& y) const;
00064 
00065     // IPrimaryScreen overrides
00066     virtual void        reconfigure(UInt32 activeSides);
00067     virtual void        warpCursor(SInt32 x, SInt32 y);
00068     virtual UInt32      registerHotKey(KeyID key, KeyModifierMask mask);
00069     virtual void        unregisterHotKey(UInt32 id);
00070     virtual void        fakeInputBegin();
00071     virtual void        fakeInputEnd();
00072     virtual SInt32      getJumpZoneSize() const;
00073     virtual bool        isAnyMouseButtonDown() const;
00074     virtual void        getCursorCenter(SInt32& x, SInt32& y) const;
00075     virtual void        gameDeviceTimingResp(UInt16 freq) { }
00076 
00077     // ISecondaryScreen overrides
00078     virtual void        fakeMouseButton(ButtonID id, bool press);
00079     virtual void        fakeMouseMove(SInt32 x, SInt32 y) const;
00080     virtual void        fakeMouseRelativeMove(SInt32 dx, SInt32 dy) const;
00081     virtual void        fakeMouseWheel(SInt32 xDelta, SInt32 yDelta) const;
00082     virtual void        fakeGameDeviceButtons(GameDeviceID id, GameDeviceButton buttons) const { }
00083     virtual void        fakeGameDeviceSticks(GameDeviceID id, SInt16 x1, SInt16 y1, SInt16 x2, SInt16 y2) const { }
00084     virtual void        fakeGameDeviceTriggers(GameDeviceID id, UInt8 t1, UInt8 t2) const { }
00085     virtual void        queueGameDeviceTimingReq() const { }
00086 
00087     // IPlatformScreen overrides
00088     virtual void        enable();
00089     virtual void        disable();
00090     virtual void        enter();
00091     virtual bool        leave();
00092     virtual bool        setClipboard(ClipboardID, const IClipboard*);
00093     virtual void        checkClipboards();
00094     virtual void        openScreensaver(bool notify);
00095     virtual void        closeScreensaver();
00096     virtual void        screensaver(bool activate);
00097     virtual void        resetOptions();
00098     virtual void        setOptions(const COptionsList& options);
00099     virtual void        setSequenceNumber(UInt32);
00100     virtual bool        isPrimary() const;
00101     virtual void        gameDeviceFeedback(GameDeviceID id, UInt16 m1, UInt16 m2) { }
00102 
00103 protected:
00104     // IPlatformScreen overrides
00105     virtual void        handleSystemEvent(const CEvent&, void*);
00106     virtual void        updateButtons();
00107     virtual IKeyState*  getKeyState() const;
00108 
00109 private:
00110     void                updateScreenShape();
00111     void                updateScreenShape(const CGDirectDisplayID, const CGDisplayChangeSummaryFlags);
00112     void                postMouseEvent(CGPoint&) const;
00113     
00114     // convenience function to send events
00115     void                sendEvent(CEvent::Type type, void* = NULL) const;
00116     void                sendClipboardEvent(CEvent::Type type, ClipboardID id) const;
00117 
00118     // message handlers
00119     bool                onMouseMove(SInt32 mx, SInt32 my);
00120     // mouse button handler.  pressed is true if this is a mousedown
00121     // event, false if it is a mouseup event.  macButton is the index
00122     // of the button pressed using the mac button mapping.
00123     bool                onMouseButton(bool pressed, UInt16 macButton);
00124     bool                onMouseWheel(SInt32 xDelta, SInt32 yDelta) const;
00125     
00126     #if !defined(MAC_OS_X_VERSION_10_5)
00127     bool                onDisplayChange();
00128     #endif
00129     void                constructMouseButtonEventMap();
00130 
00131     bool                onKey(CGEventRef event);
00132     
00133     bool                onHotKey(EventRef event) const;
00134     
00135     // Added here to allow the carbon cursor hack to be called. 
00136     void                showCursor();
00137     void                hideCursor();
00138 
00139     // map mac mouse button to synergy buttons
00140     ButtonID            mapMacButtonToSynergy(UInt16) const;
00141 
00142     // map mac scroll wheel value to a synergy scroll wheel value
00143     SInt32              mapScrollWheelToSynergy(SInt32) const;
00144 
00145     // map synergy scroll wheel value to a mac scroll wheel value
00146     SInt32              mapScrollWheelFromSynergy(SInt32) const;
00147 
00148     // get the current scroll wheel speed
00149     double              getScrollSpeed() const;
00150 
00151     // get the current scroll wheel speed
00152     double              getScrollSpeedFactor() const;
00153 
00154     // enable/disable drag handling for buttons 3 and up
00155     void                enableDragTimer(bool enable);
00156 
00157     // drag timer handler
00158     void                handleDrag(const CEvent&, void*);
00159 
00160     // clipboard check timer handler
00161     void                handleClipboardCheck(const CEvent&, void*);
00162 
00163 #if defined(MAC_OS_X_VERSION_10_5)
00164     // Resolution switch callback
00165     static void displayReconfigurationCallback(CGDirectDisplayID,
00166                             CGDisplayChangeSummaryFlags, void*);
00167 #else
00168     static pascal void  displayManagerCallback(void* inUserData,
00169                             SInt16 inMessage, void* inNotifyData);
00170 #endif
00171     // fast user switch callback
00172     static pascal OSStatus
00173                         userSwitchCallback(EventHandlerCallRef nextHandler,
00174                            EventRef theEvent, void* inUserData);
00175     
00176     // sleep / wakeup support
00177     void                watchSystemPowerThread(void*);
00178     static void         testCanceled(CFRunLoopTimerRef timer, void*info);
00179     static void         powerChangeCallback(void* refcon, io_service_t service,
00180                             natural_t messageType, void* messageArgument);
00181     void                handlePowerChangeRequest(natural_t messageType,
00182                              void* messageArgument);
00183 
00184     static CEvent::Type getConfirmSleepEvent();
00185     void                handleConfirmSleep(const CEvent& event, void*);
00186     
00187     // global hotkey operating mode
00188     static bool         isGlobalHotKeyOperatingModeAvailable();
00189     static void         setGlobalHotKeysEnabled(bool enabled);
00190     static bool         getGlobalHotKeysEnabled();
00191     
00192     // Quartz event tap support
00193     static CGEventRef   handleCGInputEvent(CGEventTapProxy proxy,
00194                                            CGEventType type,
00195                                            CGEventRef event,
00196                                            void* refcon);
00197         static CGEventRef   handleCGInputEventSecondary(CGEventTapProxy proxy,
00198                                                                            CGEventType type,
00199                                                                            CGEventRef event,
00200                                                                            void* refcon);
00201 private:
00202     struct CHotKeyItem {
00203     public:
00204         CHotKeyItem(UInt32, UInt32);
00205         CHotKeyItem(EventHotKeyRef, UInt32, UInt32);
00206 
00207         EventHotKeyRef  getRef() const;
00208 
00209         bool            operator<(const CHotKeyItem&) const;
00210 
00211     private:
00212         EventHotKeyRef  m_ref;
00213         UInt32          m_keycode;
00214         UInt32          m_mask;
00215     };
00216 
00217     enum MouseButtonState {
00218         kMouseButtonUp = 0,
00219         kMouseButtonDragged,
00220         kMouseButtonDown,
00221         kMouseButtonStateMax
00222     };
00223     
00224 
00225     class CMouseButtonState {
00226     public:
00227         void set(UInt32 button, MouseButtonState state);
00228         bool any();
00229         void reset(); 
00230         void overwrite(UInt32 buttons);
00231 
00232         bool test(UInt32 button) const;
00233         SInt8 getFirstButtonDown() const;
00234     private:
00235         std::bitset<NumButtonIDs>     m_buttons;
00236     };
00237 
00238     typedef std::map<UInt32, CHotKeyItem> HotKeyMap;
00239     typedef std::vector<UInt32> HotKeyIDList;
00240     typedef std::map<KeyModifierMask, UInt32> ModifierHotKeyMap;
00241     typedef std::map<CHotKeyItem, UInt32> HotKeyToIDMap;
00242 
00243     // true if screen is being used as a primary screen, false otherwise
00244     bool                m_isPrimary;
00245 
00246     // true if mouse has entered the screen
00247     bool                m_isOnScreen;
00248 
00249     // the display
00250     CGDirectDisplayID   m_displayID;
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     // mouse state
00258     mutable SInt32      m_xCursor, m_yCursor;
00259     mutable bool        m_cursorPosValid;
00260     
00261     /* FIXME: this data structure is explicitly marked mutable due
00262        to a need to track the state of buttons since the remote
00263        side only lets us know of change events, and because the
00264        fakeMouseButton button method is marked 'const'. This is
00265        Evil, and this should be moved to a place where it need not
00266        be mutable as soon as possible. */
00267     mutable CMouseButtonState m_buttonState;
00268     typedef std::map<UInt16, CGEventType> MouseButtonEventMapType;
00269     std::vector<MouseButtonEventMapType> MouseButtonEventMap;
00270 
00271     bool                m_cursorHidden;
00272     SInt32              m_dragNumButtonsDown;
00273     Point               m_dragLastPoint;
00274     CEventQueueTimer*   m_dragTimer;
00275 
00276     // keyboard stuff
00277     COSXKeyState*       m_keyState;
00278 
00279     // clipboards
00280     COSXClipboard       m_pasteboard;
00281     UInt32              m_sequenceNumber;
00282 
00283     // screen saver stuff
00284     COSXScreenSaver*    m_screensaver;
00285     bool                m_screensaverNotify;
00286 
00287     // clipboard stuff
00288     bool                m_ownClipboard;
00289     CEventQueueTimer*   m_clipboardTimer;
00290 
00291     // window object that gets user input events when the server
00292     // has focus.
00293     WindowRef           m_hiddenWindow;
00294     // window object that gets user input events when the server
00295     // does not have focus.
00296     WindowRef           m_userInputWindow;
00297 
00298 #if !defined(MAC_OS_X_VERSION_10_5)
00299     // display manager stuff (to get screen resolution switches).
00300     DMExtendedNotificationUPP   m_displayManagerNotificationUPP;
00301     ProcessSerialNumber         m_PSN;
00302 #endif
00303 
00304     // fast user switching
00305     EventHandlerRef         m_switchEventHandlerRef;
00306 
00307     // sleep / wakeup
00308     CMutex*                 m_pmMutex;
00309     CThread*                m_pmWatchThread;
00310     CCondVar<bool>*         m_pmThreadReady;
00311     CFRunLoopRef            m_pmRunloop;
00312     io_connect_t            m_pmRootPort;
00313 
00314     // hot key stuff
00315     HotKeyMap               m_hotKeys;
00316     HotKeyIDList            m_oldHotKeyIDs;
00317     ModifierHotKeyMap       m_modifierHotKeys;
00318     UInt32                  m_activeModifierHotKey;
00319     KeyModifierMask         m_activeModifierHotKeyMask;
00320     HotKeyToIDMap           m_hotKeyToIDMap;
00321 
00322     // global hotkey operating mode
00323     static bool             s_testedForGHOM;
00324     static bool             s_hasGHOM;
00325 
00326     // events
00327     static CEvent::Type     s_confirmSleepEvent;
00328     
00329     // Quartz input event support
00330     CFMachPortRef           m_eventTapPort;
00331     CFRunLoopSourceRef      m_eventTapRLSR;
00332 
00333     // for double click coalescing.
00334     double                  m_lastSingleClick;
00335     double                  m_lastDoubleClick;
00336     SInt32                  m_lastSingleClickXCursor;
00337     SInt32                  m_lastSingleClickYCursor;
00338 
00339     // cursor will hide and show on enable and disable if true.
00340     bool                    m_autoShowHideCursor;
00341 };
00342 
00343 #endif

Generated on Wed Jun 19 2013 00:00:05 for Synergy by  doxygen 1.7.1