Rev 1 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
//
// MouseHandler.swift
// GyroServer
//
// Created by Matteo Riva on 07/08/15.
// Copyright © 2015 Matteo Riva. All rights reserved.
//
import CoreGraphics
import Cocoa
import Carbon
import CoreServices
class MouseHandler: NSObject, ServerHandlerDelegate {
private let server = ServerHandler()
private var activeScreen: Int {
get {
return UserDefaults.standard.integer(forKey: "activeScreen")
}
}
private var mLoc: (x: CGFloat, y: CGFloat) = (x: 0, y: 0)
private var isDragging = false
override init() {
super.init()
resetPointerPosition(moveMouse: false)
server.startBroadcast()
NotificationCenter.default.addObserver(forName: ActiveScreenDidChangeNotification, object: nil, queue: OperationQueue.main) {[unowned self] (_) -> Void in
self.resetPointerPosition(moveMouse: false)
}
NotificationCenter.default.addObserver(forName: ServerDidConnectNotification, object: nil, queue: OperationQueue.main) {[unowned self] (_) -> Void in
self.resetPointerPosition(moveMouse: true)
self.server.delegate = self
}
NotificationCenter.default.addObserver(forName: ServerDidDisconnectNotification, object: nil, queue: OperationQueue.main) {[unowned self] (_) -> Void in
self.server.delegate = nil
self.resetPointerPosition(moveMouse: false)
}
}
deinit {
NotificationCenter.default.removeObserver(self)
}
//MARK: - Mouse movements
private func computePointerMovementWithPacket(_ packet: GyroPacket) {
let roll = packet.roll
let sgn: Double = roll ?? 0 < 0 ? -1 : 1
let gate = 0.75
let changeMovCond = roll == nil || (roll! > -gate && roll! < gate)
let rotatZ = changeMovCond ? packet.rotatZ! : packet.rotatX! * -sgn
let accZ = changeMovCond ? packet.accZ! : packet.accX!
let rotatX = changeMovCond ? packet.rotatX! : packet.rotatZ! * sgn
let accX = changeMovCond ? packet.accX! : packet.accZ!
let gravZ = changeMovCond ? packet.gravZ! : packet.gravX! * sgn
var dx = ((rotatZ * -1) * (1 + abs(accZ))) * packet.moveVelocity! * abs(gravZ)
var dy = ((rotatX * -1) * (1 + abs(accX))) * packet.moveVelocity! * abs(gravZ)
if packet.type == .scroll {
dx = min(dx,1)
dy = min(dy,1)
}
var x = mLoc.x + CGFloat(dx)
var y = mLoc.y + CGFloat(dy)
let frame = NSScreen.screens[activeScreen].frame
let s = (o: frame.origin, w: frame.maxX - 1, h: frame.maxY - 1)
mLoc = (x: x, y: y)
x = max(s.o.x,min(x, s.w))
y = max(0,min(y, s.h))
let point = CGPoint(x: x, y: y)
let mouseEvent = CGEvent(mouseEventSource: nil, mouseType: .mouseMoved, mouseCursorPosition: point, mouseButton: .left)
mouseEvent?.post(tap: CGEventTapLocation.cghidEventTap)
if isDragging {
let mouseEvent = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDragged, mouseCursorPosition: point, mouseButton: .left)
mouseEvent?.post(tap: CGEventTapLocation.cghidEventTap)
}
}
func resetPointerPosition(moveMouse: Bool) {
let aIndex = UserDefaults.standard.integer(forKey: "activeScreen")
let frame = NSScreen.screens[aIndex].frame
let x = frame.midX
let y = frame.midY - frame.origin.y
NSLog("x: %f, y: %f, origin x: %f, y: %f, mid x: %f, y: %f", x,y,frame.origin.x, frame.origin.y, frame.midX, frame.midY)
mLoc = (x: x, y: y)
if moveMouse {
let point = CGPoint(x: x, y: y)
let mouseEvent = CGEvent(mouseEventSource: nil, mouseType: .mouseMoved, mouseCursorPosition: point, mouseButton: .left)
mouseEvent?.post(tap: CGEventTapLocation.cghidEventTap)
}
}
//MARK: - Mouse actions
var rollGate = 0
private func scrollWithRoll(_ roll: Double, velocity: Double) {
rollGate += 1
if rollGate == 10 {
let amount = (Int32) (roll * velocity * -50)
let mouseEvent = CGEvent(scrollWheelEvent2Source: nil, units: .pixel, wheelCount: 1, wheel1: amount, wheel2: 0, wheel3: 0)
mouseEvent?.post(tap: CGEventTapLocation.cghidEventTap)
rollGate = 0
}
}
private func clickButton(_ button: ButtonType, click: ClickType) {
let mousePos = CGPoint(x: mLoc.x, y: mLoc.y)
let mouseEventType: CGEventType
let mouseButton: CGMouseButton
if button == .left {
mouseButton = .left
mouseEventType = (click == .down ? .leftMouseDown : .leftMouseUp)
isDragging = (click == .down)
} else {
mouseButton = .right
mouseEventType = (click == .down ? .rightMouseDown : .rightMouseUp)
isDragging = false
}
let mouseEvent = CGEvent(mouseEventSource: nil, mouseType: mouseEventType, mouseCursorPosition: mousePos, mouseButton: mouseButton)
mouseEvent?.post(tap: CGEventTapLocation.cghidEventTap)
}
//MARK: - AppleScript launcher
private func runAppleScript(_ scriptText: String) {
let script = NSAppleScript(source: scriptText)!
var error: NSDictionary? = nil
let resultMaybe = script.executeAndReturnError(&error) as NSAppleEventDescriptor?
if (resultMaybe == nil)
{
let myStringDict = error as? [String:AnyObject]
if (myStringDict?["NSAppleScriptErrorNumber"] as! Int16 == 1002) {
let alert = NSAlert()
alert.addButton(withTitle: "OK".localized)
alert.messageText = "system_message".localized + "\n" + (myStringDict?["NSAppleScriptErrorBriefMessage"] as! String)
alert.informativeText = "allow_perms".localized
alert.alertStyle = .warning
alert.runModal()
}
print (error as Any)
return
}
}
//MARK: - ServerHandlerDelegate
func serverDidReceivePacket(_ packet: GyroPacket) {
switch packet.type {
case .scroll:
scrollWithRoll(packet.roll!, velocity: packet.scrollVelocity!)
computePointerMovementWithPacket(packet)
case .movement:
computePointerMovementWithPacket(packet)
case .click:
clickButton(packet.button!, click: packet.click!)
case .keyTapped:
runAppleScript("tell app \"System Events\" to keystroke \"\(packet.key!)\"")
case .deleteBackward:
runAppleScript ("tell app \"System Events\" to key code 51")
case .returnTapped:
runAppleScript("tell app \"System Events\" to keystroke return")
case .resetPointerPosition:
resetPointerPosition(moveMouse: true)
}
}
}