Subversion Repositories Mobile Apps.GyroMouse

Rev

Rev 5 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5 Rev 7
Line 43... Line 43...
43
    deinit {
43
    deinit {
44
        NotificationCenter.default.removeObserver(self)
44
        NotificationCenter.default.removeObserver(self)
45
    }
45
    }
46
    
46
    
47
    //MARK: - Mouse movements
47
    //MARK: - Mouse movements
48
    
48
 
49
    private func computePointerMovementWithPacket(_ packet: GyroPacket) {
-
 
50
         let roll = packet.roll
-
 
51
        let sgn: Double = roll ?? 0 < 0 ? -1 : 1
-
 
52
        let gate = 0.75
-
 
53
        let changeMovCond = roll == nil || (roll! > -gate && roll! < gate)
-
 
54
        
-
 
55
        let rotatZ = changeMovCond ? packet.rotatZ! : packet.rotatX! * -sgn
49
    var averagedPoints: [CGPoint] = [ CGPoint(x: -1, y: -1), CGPoint(x: -1, y: -1), CGPoint(x: -1, y: -1), CGPoint(x: -1, y: -1), CGPoint(x: -1, y: -1), CGPoint(x: -1, y: -1) ]
56
        let accZ = changeMovCond ? packet.accZ! : packet.accX!
-
 
57
        let rotatX = changeMovCond ? packet.rotatX! : packet.rotatZ! * sgn
-
 
58
        let accX = changeMovCond ? packet.accX! : packet.accZ!
-
 
59
        let gravZ = changeMovCond ? packet.gravZ! : packet.gravX! * sgn
-
 
60
        
50
    
61
        var dx = ((rotatZ * -1) * (1 + abs(accZ))) * packet.moveVelocity! * abs(gravZ)
-
 
62
        var dy = ((rotatX * -1) * (1 + abs(accX))) * packet.moveVelocity! * abs(gravZ)
51
    func computePointerMovement(grav: (X: Double, Y: Double, Z: Double), rotat: (X: Double, Z: Double), acc: (X: Double, Y: Double, Z: Double), moveVelocity: Double) {
63
        
-
 
64
        if packet.type == .scroll {
-
 
65
            dx = min(dx,1)
-
 
66
            dy = min(dy,1)
-
 
67
        }
-
 
68
        
-
 
69
        var x = mLoc.x + CGFloat(dx)
-
 
70
        var y = mLoc.y + CGFloat(dy)
-
 
71
        
-
 
72
        let frame = NSScreen.screens[activeScreen].frame
52
        let frame = NSScreen.screens[activeScreen].frame
73
        let s = (o: frame.origin, w: frame.maxX - 1, h: frame.maxY - 1)
53
        let bounds = (origin: frame.origin, width: frame.maxX - 1, height: frame.maxY - 1)
74
 
54
 
-
 
55
        // compute new mouse location
75
        mLoc = (x: x, y: y)
56
        mLoc = (
-
 
57
            x: max(bounds.origin.x, min(mLoc.x + CGFloat((rotat.Z * -1) * (1 + abs(acc.Z)) * moveVelocity * abs(grav.Z)), bounds.width)),
-
 
58
            y: max(0,               min(mLoc.y + CGFloat((rotat.X * -1) * (1 + abs(acc.X)) * moveVelocity * abs(grav.Z)), bounds.height))
-
 
59
        )
-
 
60
 
76
        x = max(s.o.x,min(x, s.w))
61
        // initialize history array
-
 
62
        for i in 0...(averagedPoints.count - 1) {
-
 
63
            if ((averagedPoints[i].x == -1) && (averagedPoints[i].y == -1)) {
-
 
64
                averagedPoints[0].x = mLoc.x
77
        y = max(0,min(y, s.h))
65
                averagedPoints[0].y = mLoc.y
-
 
66
            }
78
        
67
        }
-
 
68
 
-
 
69
        // shift history array and insert new position at the end
79
        let point = CGPoint(x: x, y: y)
70
        for i in 0...(averagedPoints.count - 2) {
-
 
71
            averagedPoints[i].x = averagedPoints[i + 1].x
-
 
72
            averagedPoints[i].y = averagedPoints[i + 1].y
-
 
73
        }
-
 
74
        averagedPoints[averagedPoints.count - 1].x = mLoc.x
-
 
75
        averagedPoints[averagedPoints.count - 1].y = mLoc.y
80
 
76
 
-
 
77
        // compute averaged coordinates
-
 
78
        var averagedPoint = CGPoint(x: 0, y: 0)
-
 
79
        for i in 0...(averagedPoints.count - 1) {
-
 
80
            averagedPoint.x += averagedPoints[i].x
-
 
81
            averagedPoint.y += averagedPoints[i].y
-
 
82
        }
-
 
83
        averagedPoint.x /= CGFloat(averagedPoints.count)
-
 
84
        averagedPoint.y /= CGFloat(averagedPoints.count)
-
 
85
 
-
 
86
        //print ("Updated mouse pos to \(averagedPoint.x),\(averagedPoint.y)")
-
 
87
 
81
        let mouseEvent = CGEvent(mouseEventSource: nil, mouseType: .mouseMoved, mouseCursorPosition: point, mouseButton: .left)
88
        let mouseEvent = CGEvent(mouseEventSource: nil, mouseType: .mouseMoved, mouseCursorPosition: averagedPoint, mouseButton: .left)
82
        mouseEvent?.post(tap: CGEventTapLocation.cghidEventTap)
89
        mouseEvent?.post(tap: CGEventTapLocation.cghidEventTap)
83
 
90
 
84
        if isDragging {
91
        if (isDragging) {
85
             let mouseEvent = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDragged, mouseCursorPosition: point, mouseButton: .left)
92
             let mouseEvent = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDragged, mouseCursorPosition: averagedPoint, mouseButton: .left)
86
            mouseEvent?.post(tap: CGEventTapLocation.cghidEventTap)
93
            mouseEvent?.post(tap: CGEventTapLocation.cghidEventTap)
87
        }
94
        }
88
    }
95
    }
89
    
96
    
90
    func resetPointerPosition(moveMouse: Bool) {
97
    func resetPointerPosition(moveMouse: Bool) {
Line 103... Line 110...
103
    
110
    
104
    //MARK: - Mouse actions
111
    //MARK: - Mouse actions
105
    
112
    
106
    var rollGate = 0
113
    var rollGate = 0
107
 
114
 
108
    private func scrollWithRoll(_ roll: Double, velocity: Double) {
115
    func scrollWithRoll(roll: Double, velocity: Double) {
109
        rollGate += 1
116
        rollGate += 1
110
        if rollGate == 10 {
117
        if rollGate == 10 {
111
            let amount = (Int32) (roll * velocity * -50)
118
            let amount = (Int32) (roll * velocity * -50)
112
            let mouseEvent = CGEvent(scrollWheelEvent2Source: nil, units: .pixel, wheelCount: 1, wheel1: amount, wheel2: 0, wheel3: 0)
119
            let mouseEvent = CGEvent(scrollWheelEvent2Source: nil, units: .pixel, wheelCount: 1, wheel1: amount, wheel2: 0, wheel3: 0)
113
            mouseEvent?.post(tap: CGEventTapLocation.cghidEventTap)
120
            mouseEvent?.post(tap: CGEventTapLocation.cghidEventTap)
114
 
121
 
115
            rollGate = 0
122
            rollGate = 0
116
        }
123
        }
117
    }
124
    }
118
 
125
 
119
    private func clickButton(_ button: ButtonType, click: ClickType) {
126
    func clickButton(isRightButton: Bool, isButtonDown: Bool) {
120
        let mousePos = CGPoint(x: mLoc.x, y: mLoc.y)
127
        let mousePos = CGPoint(x: mLoc.x, y: mLoc.y)
121
        let mouseEventType: CGEventType
128
        let mouseEventType: CGEventType
122
        let mouseButton: CGMouseButton
129
        let mouseButton: CGMouseButton
123
 
130
 
124
        if button == .left {
131
        if (isRightButton) {
125
            mouseButton = .left
-
 
126
            mouseEventType = (click == .down ? .leftMouseDown : .leftMouseUp)
-
 
127
            isDragging = (click == .down)
-
 
128
        } else {
-
 
129
            mouseButton = .right
132
            mouseButton = .right
130
            mouseEventType = (click == .down ? .rightMouseDown : .rightMouseUp)
133
            mouseEventType = (isButtonDown ? .rightMouseDown : .rightMouseUp)
131
            isDragging = false
134
            isDragging = false
-
 
135
        } else {
-
 
136
            mouseButton = .left
-
 
137
            mouseEventType = (isButtonDown ? .leftMouseDown : .leftMouseUp)
-
 
138
            isDragging = isButtonDown
132
        }
139
        }
133
        
140
        
134
        let mouseEvent = CGEvent(mouseEventSource: nil, mouseType: mouseEventType, mouseCursorPosition: mousePos, mouseButton: mouseButton)
141
        let mouseEvent = CGEvent(mouseEventSource: nil, mouseType: mouseEventType, mouseCursorPosition: mousePos, mouseButton: mouseButton)
135
        mouseEvent?.post(tap: CGEventTapLocation.cghidEventTap)
142
        mouseEvent?.post(tap: CGEventTapLocation.cghidEventTap)
136
    }
143
    }
137
    
144
    
138
    //MARK: - AppleScript launcher
145
    //MARK: - AppleScript launcher
139
    
146
    
140
    private func runAppleScript(_ scriptText: String) {
147
    func runAppleScript(_ scriptText: String) {
141
        let script = NSAppleScript(source: scriptText)!
148
        let script = NSAppleScript(source: scriptText)!
142
        var error: NSDictionary? = nil
149
        var error: NSDictionary? = nil
143
        let resultMaybe = script.executeAndReturnError(&error) as NSAppleEventDescriptor?
150
        let resultMaybe = script.executeAndReturnError(&error) as NSAppleEventDescriptor?
144
        if (resultMaybe == nil)
151
        if (resultMaybe == nil)
145
        {
152
        {
Line 152... Line 159...
152
                alert.alertStyle = .warning
159
                alert.alertStyle = .warning
153
                alert.runModal()
160
                alert.runModal()
154
            }
161
            }
155
            print (error as Any)
162
            print (error as Any)
156
            return
163
            return
157
        }
-
 
158
    }
-
 
159
    
-
 
160
    //MARK: - ServerHandlerDelegate
-
 
161
    
-
 
162
    func serverDidReceivePacket(_ packet: GyroPacket) {
-
 
163
        switch packet.type {
-
 
164
        case .scroll:
-
 
165
            scrollWithRoll(packet.roll!, velocity: packet.scrollVelocity!)
-
 
166
            computePointerMovementWithPacket(packet)
-
 
167
        case .movement:
-
 
168
            computePointerMovementWithPacket(packet)
-
 
169
        case .click:
-
 
170
            clickButton(packet.button!, click: packet.click!)
-
 
171
        case .keyTapped:
-
 
172
            runAppleScript("tell app \"System Events\" to keystroke \"\(packet.key!)\"")
-
 
173
        case .deleteBackward:
-
 
174
            runAppleScript ("tell app \"System Events\" to key code 51")
-
 
175
        case .returnTapped:
-
 
176
            runAppleScript("tell app \"System Events\" to keystroke return")
-
 
177
        case .resetPointerPosition:
-
 
178
            resetPointerPosition(moveMouse: true)
-
 
179
        }
164
        }
180
    }
165
    }
181
}
166
}