Skip to content

Commit

Permalink
Finish everything else
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusSkucas committed Aug 15, 2021
1 parent 5c5f1d0 commit 4d3b2b4
Show file tree
Hide file tree
Showing 37 changed files with 3,200 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
KeyPopper.xcodeproj/project.xcworkspace/xcuserdata
KeyPopper.xcodeproj/project.xcworkspace/xcuserdata
/KeyPopper Installer/build
KeyPopper.xcodeproj/xcuserdata/
15 changes: 15 additions & 0 deletions KettleCornService/KettleCornSerivce.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>sh.linus.keypopper.KettleCornSerivce</string>
<key>Program</key>
<string>/Library/PreferencePanes/KeyPopper\ PrefPane.prefPane/Contents/XPCServices/KettleCornService</string>
<key>MachServices</key>
<dict>
<key>sh.linus.keypopper.KettleCornService.mach</key>
<true/>
</dict>
</dict>
</plist>
8 changes: 8 additions & 0 deletions KettleCornService/KettleCornService.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.automation.apple-events</key>
<true/>
</dict>
</plist>
81 changes: 81 additions & 0 deletions KettleCornService/KettleCornService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// KettleCornService.swift
// KettleCornService
//
// Created by Linus Skucas on 8/3/21.
//

import Foundation

struct SoundState {
private let userDefaults = UserDefaults.init(suiteName: "sh.linus.keypopper")!

var sound: PoppingSounds {
get {
let soundInt = userDefaults.integer(forKey: "sound")
let sound = PoppingSounds(rawValue: soundInt)!
return sound
}
set {
userDefaults.set(newValue.rawValue, forKey: "sound")
}
}
var enabled: Bool {
get {
let enabled = userDefaults.bool(forKey: "enabled")
return enabled
}
set {
userDefaults.set(newValue, forKey: "enabled")
}
}
static var shared: SoundState = SoundState()
}

@objc class KettleCornService: NSObject, KettleCornServiceProtocol {
// func hello(_ text: String, withReply reply: @escaping (String) -> Void) {
// reply("Hello, \(text)!")
// }

var connection: NSXPCConnection!
var service: PoppingServiceProtocol!

override init() {
super.init()
connection = NSXPCConnection(machServiceName: "sh.linus.keypopper.PoppingService.mach")
connection.remoteObjectInterface = NSXPCInterface(with: PoppingServiceProtocol.self)
connection.resume()

service = connection.remoteObjectProxyWithErrorHandler { error in
fatalError("Error: \(error.localizedDescription)")
} as! PoppingServiceProtocol
}

func changeSoundTo(_ soundInt: Int) {
let sound = PoppingSounds(rawValue: soundInt)!
// Store sound
NSLog("Change sound")
SoundState.shared.sound = sound
}

func changeSoundState(shouldPlaySound: Bool) {
NSLog("Change state")
SoundState.shared.enabled = shouldPlaySound
}

func boop() {
guard SoundState.shared.enabled else { return }

switch SoundState.shared.sound {
case .pop:
service.pop()
NSLog("POP")
case .frog:
service.frog()
NSLog("Frog")
case .moo:
service.moo()
NSLog("Moo")
}
}
}
19 changes: 19 additions & 0 deletions KettleCornService/KettleCornServiceDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// KettleCornServiceDelegate.swift
// KettleCornService
//
// Created by Linus Skucas on 8/3/21.
//

import Foundation

class KettleCornServiceDelegate: NSObject, NSXPCListenerDelegate {
func listener(_ listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool {
let exportedObject = KettleCornService()
newConnection.exportedInterface = NSXPCInterface(with: KettleCornServiceProtocol.self)
newConnection.exportedObject = exportedObject

newConnection.resume()
return true
}
}
16 changes: 16 additions & 0 deletions KettleCornService/KettleCornServiceProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// KettleCornServiceProtocol.swift
// KettleCornService
//
// Created by Linus Skucas on 8/3/21.
//

import Foundation

@objc(KettleCornServiceProtocol) protocol KettleCornServiceProtocol {
// func hello(_ text: String, withReply reply: @escaping (String) -> Void)
func changeSoundTo(_ soundInt: Int)
func changeSoundState(shouldPlaySound: Bool)
func boop()
}

14 changes: 14 additions & 0 deletions KettleCornService/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// main.swift
// KettleCornService
//
// Created by Linus Skucas on 8/3/21.
//

import Foundation

let delegate = KettleCornServiceDelegate()
let listener = NSXPCListener(machServiceName: "sh.linus.keypopper.KettleCornService.mach")
listener.delegate = delegate
listener.resume()
RunLoop.main.run()
61 changes: 61 additions & 0 deletions KeyPopper Catcher/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// AppDelegate.swift
// KeyPopper Catcher
//
// Created by Linus Skucas on 8/13/21.
//

import Cocoa

@main
class AppDelegate: NSObject, NSApplicationDelegate {

// var connection: NSXPCConnection!
// var service: KettleCornServiceProtocol!

func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application

// connection = NSXPCConnection(machServiceName: "sh.linus.keypopper.KettleCornService.mach")
// connection.remoteObjectInterface = NSXPCInterface(with: KettleCornServiceProtocol.self)
// connection.resume()
//
// service = connection.remoteObjectProxyWithErrorHandler { error in
// fatalError("Error: \(error.localizedDescription)")
// } as! KettleCornServiceProtocol

let eventMask = (1 << CGEventType.keyDown.rawValue) | (1 << CGEventType.keyUp.rawValue)
NSLog("gggp")
func cgEventCallback(proxy: CGEventTapProxy, type: CGEventType, event: CGEvent, refcon: UnsafeMutableRawPointer?) -> Unmanaged<CGEvent>? {
let connection = NSXPCConnection(machServiceName: "sh.linus.keypopper.KettleCornService.mach")
connection.remoteObjectInterface = NSXPCInterface(with: KettleCornServiceProtocol.self)
connection.resume()

let service = connection.remoteObjectProxyWithErrorHandler { error in
fatalError("Error: \(error.localizedDescription)")
} as! KettleCornServiceProtocol
service.boop()
return nil
}

guard let eventTap = CGEvent.tapCreate(tap: .cgAnnotatedSessionEventTap, place: .headInsertEventTap, options: .listenOnly, eventsOfInterest: CGEventMask(eventMask), callback: cgEventCallback, userInfo: nil) else {
DispatchQueue.main.async {
NSLog("faileddddddddd")
}
return
}
let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0)
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, .commonModes)
CGEvent.tapEnable(tap: eventTap, enable: true)
CFRunLoopRun()
}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
// connection.invalidate()
}

func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
return true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
58 changes: 58 additions & 0 deletions KeyPopper Catcher/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"images" : [
{
"idiom" : "mac",
"scale" : "1x",
"size" : "16x16"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "16x16"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "32x32"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "32x32"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "128x128"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "128x128"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "256x256"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "256x256"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "512x512"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "512x512"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
6 changes: 6 additions & 0 deletions KeyPopper Catcher/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading

0 comments on commit 4d3b2b4

Please sign in to comment.