00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
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
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
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
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
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
00115 void sendEvent(CEvent::Type type, void* = NULL) const;
00116 void sendClipboardEvent(CEvent::Type type, ClipboardID id) const;
00117
00118
00119 bool onMouseMove(SInt32 mx, SInt32 my);
00120
00121
00122
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
00136 void showCursor();
00137 void hideCursor();
00138
00139
00140 ButtonID mapMacButtonToSynergy(UInt16) const;
00141
00142
00143 SInt32 mapScrollWheelToSynergy(SInt32) const;
00144
00145
00146 SInt32 mapScrollWheelFromSynergy(SInt32) const;
00147
00148
00149 double getScrollSpeed() const;
00150
00151
00152 double getScrollSpeedFactor() const;
00153
00154
00155 void enableDragTimer(bool enable);
00156
00157
00158 void handleDrag(const CEvent&, void*);
00159
00160
00161 void handleClipboardCheck(const CEvent&, void*);
00162
00163 #if defined(MAC_OS_X_VERSION_10_5)
00164
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
00172 static pascal OSStatus
00173 userSwitchCallback(EventHandlerCallRef nextHandler,
00174 EventRef theEvent, void* inUserData);
00175
00176
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
00188 static bool isGlobalHotKeyOperatingModeAvailable();
00189 static void setGlobalHotKeysEnabled(bool enabled);
00190 static bool getGlobalHotKeysEnabled();
00191
00192
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
00244 bool m_isPrimary;
00245
00246
00247 bool m_isOnScreen;
00248
00249
00250 CGDirectDisplayID m_displayID;
00251
00252
00253 SInt32 m_x, m_y;
00254 SInt32 m_w, m_h;
00255 SInt32 m_xCenter, m_yCenter;
00256
00257
00258 mutable SInt32 m_xCursor, m_yCursor;
00259 mutable bool m_cursorPosValid;
00260
00261
00262
00263
00264
00265
00266
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
00277 COSXKeyState* m_keyState;
00278
00279
00280 COSXClipboard m_pasteboard;
00281 UInt32 m_sequenceNumber;
00282
00283
00284 COSXScreenSaver* m_screensaver;
00285 bool m_screensaverNotify;
00286
00287
00288 bool m_ownClipboard;
00289 CEventQueueTimer* m_clipboardTimer;
00290
00291
00292
00293 WindowRef m_hiddenWindow;
00294
00295
00296 WindowRef m_userInputWindow;
00297
00298 #if !defined(MAC_OS_X_VERSION_10_5)
00299
00300 DMExtendedNotificationUPP m_displayManagerNotificationUPP;
00301 ProcessSerialNumber m_PSN;
00302 #endif
00303
00304
00305 EventHandlerRef m_switchEventHandlerRef;
00306
00307
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
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
00323 static bool s_testedForGHOM;
00324 static bool s_hasGHOM;
00325
00326
00327 static CEvent::Type s_confirmSleepEvent;
00328
00329
00330 CFMachPortRef m_eventTapPort;
00331 CFRunLoopSourceRef m_eventTapRLSR;
00332
00333
00334 double m_lastSingleClick;
00335 double m_lastDoubleClick;
00336 SInt32 m_lastSingleClickXCursor;
00337 SInt32 m_lastSingleClickYCursor;
00338
00339
00340 bool m_autoShowHideCursor;
00341 };
00342
00343 #endif