upd to previous the commit

This commit is contained in:
2018-04-09 01:15:04 +01:00
parent b69cdd5d60
commit c4724e67bb
14 changed files with 320 additions and 62 deletions
-1
View File
@@ -16,7 +16,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
TouchBarController.shared.setupControlStripPresence()
// Insert code here to initialize your application
}
func applicationWillTerminate(_ aNotification: Notification) {
+63 -13
View File
@@ -8,23 +8,73 @@
import Foundation
func KeyPress (keyCode: CGKeyCode) {
let keyDown = CGEvent(keyboardEventSource: nil, virtualKey: keyCode, keyDown: true)
let keyUp = CGEvent(keyboardEventSource: nil, virtualKey: keyCode, keyDown: false)
keyDown?.post(tap: .cghidEventTap)
keyUp?.post(tap: .cghidEventTap)
protocol KeyPress {
var keyCode: CGKeyCode { get }
func send()
}
func ESCKeyPress() {
KeyPress(keyCode: 53)
extension KeyPress {
func send () {
let src = CGEventSource(stateID: .hidSystemState)
let keyDown = CGEvent(keyboardEventSource: src, virtualKey: keyCode, keyDown: true)
let keyUp = CGEvent(keyboardEventSource: src, virtualKey: keyCode, keyDown: false)
let loc: CGEventTapLocation = .cghidEventTap
keyDown?.post(tap: loc)
keyUp?.post(tap: loc)
}
}
func BrightnessUpPress() {
KeyPress(keyCode: 107)
struct ESCKeyPress: KeyPress {
let keyCode: CGKeyCode = 53
}
func BrightnessDownPress() {
KeyPress(keyCode: 113)
struct BrightnessUpPress: KeyPress {
let keyCode: CGKeyCode = 107
}
struct BrightnessDownPress: KeyPress {
let keyCode: CGKeyCode = 113
}
func doKey(_ key: Int, down: Bool) {
let flags = NSEvent.ModifierFlags(rawValue: down ? 0xa00 : 0xb00)
let data1 = (key << 16) | ((down ? 0xa : 0xb) << 8)
let ev = NSEvent.otherEvent(
with: NSEvent.EventType.systemDefined,
location: NSPoint(x:0.0, y:0.0),
modifierFlags: flags,
timestamp: TimeInterval(0),
windowNumber: 0,
context: nil,
// context: 0,
subtype: 8,
data1: data1,
data2: -1
)
let cev = ev!.cgEvent!
cev.post(tap: CGEventTapLocation(rawValue: 0)!)
}
func HIDPostAuxKey(_ key: Int) {
doKey(key, down: true)
doKey(key, down: false)
}
// hidsystem/ev_keymap.h
let NX_KEYTYPE_SOUND_UP = 0
let NX_KEYTYPE_SOUND_DOWN = 1
let NX_KEYTYPE_BRIGHTNESS_UP = 2
let NX_KEYTYPE_BRIGHTNESS_DOWN = 3
let NX_KEYTYPE_PLAY = 16
let NX_KEYTYPE_NEXT = 17
let NX_KEYTYPE_PREVIOUS = 18
+1 -6
View File
@@ -1,10 +1,5 @@
<?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.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
</dict>
<dict/>
</plist>
+106 -32
View File
@@ -14,19 +14,40 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
let touchBar = NSTouchBar()
var timer = Timer()
var timeButton: NSButton = NSButton()
private override init() {
super.init()
touchBar.delegate = self
touchBar.defaultItemIdentifiers = [.escButton, .volumeUp, .volumeDown, .time, .preferences]
touchBar.defaultItemIdentifiers = [
.escButton,
.brightDown,
.brightUp,
.prev,
.play,
.next,
.sleep,
.weather,
.volumeDown,
.volumeUp,
.battery,
.time,
]
self.presentTouchBar()
}
func setupControlStripPresence() {
DFRSystemModalShowsCloseBoxWhenFrontMost(true)
DFRSystemModalShowsCloseBoxWhenFrontMost(false)
let item = NSCustomTouchBarItem(identifier: .controlStripItem)
item.view = NSButton(image: #imageLiteral(resourceName: "Strip"), target: self, action: #selector(presentTouchBar))
NSTouchBarItem.addSystemTrayItem(item)
DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true)
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateTime), userInfo: nil, repeats: true)
}
func updateControlStripPresence() {
@@ -45,59 +66,112 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
switch identifier {
case .escButton:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "ESC", target: self, action: #selector(handleEsc))
item.view = NSButton(title: "esc", target: self, action: #selector(handleEsc))
return item
case .brightUp:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "🔆", target: self, action: #selector(handleBrightUp))
return item
case .brightDown:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "🔅", target: self, action: #selector(handleBrightDown))
return item
case .volumeDown:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "V-", target: self, action: #selector(handleVolumeDown))
item.view = NSButton(title: "🔉", target: self, action: #selector(handleVolumeDown))
return item
case .volumeUp:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "V+", target: self, action: #selector(handleVolumeUp))
item.view = NSButton(title: "🔊", target: self, action: #selector(handleVolumeUp))
return item
// case .prev:
// let item = NSCustomTouchBarItem(identifier: identifier)
// item.view = NSButton(title: "⏪", target: self, action: #selector(handlePrev))
// return item
case .play:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "⏯", target: self, action: #selector(handlePlay))
return item
case .next:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "⏩", target: self, action: #selector(handleNext))
return item
case .time:
let item = NSCustomTouchBarItem(identifier: identifier)
timeButton = NSButton(title: self.getCurrentTime(), target: self, action: nil)
item.view = timeButton
return item
default:
return nil
}
}
func getCurrentTime() -> String {
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.setLocalizedDateFormatFromTemplate("HH:mm")
let timestamp = dateFormatter.string(from: date)
return timestamp
}
@objc func updateTime() {
timeButton.title = getCurrentTime()
}
@objc func handleEsc() {
ESCKeyPress()
let sender = ESCKeyPress()
sender.send()
}
@objc func handleVolumeUp() {
self.volume(directon: "up")
HIDPostAuxKey(Int(NX_KEYTYPE_SOUND_UP))
}
@objc func handleVolumeDown() {
self.volume(directon: "down")
HIDPostAuxKey(Int(NX_KEYTYPE_SOUND_DOWN))
}
func volume(directon: String) {
var myAppleScript = "set volume output volume (output volume of (get volume settings) - 5)"
@objc func handleBrightDown() {
// HIDPostAuxKey(Int(NX_KEYTYPE_BRIGHTNESS_DOWN))
if (directon == "up") {
myAppleScript = "set volume output volume (output volume of (get volume settings) + 5)"
}
var error: NSDictionary?
if let scriptObject = NSAppleScript(source: myAppleScript) {
let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError(&error)
print(output.stringValue as Any)
if (error != nil) {
print("error: \(String(describing: error))")
}
}
let sender = BrightnessUpPress()
sender.send()
}
@objc func brightness(directon: String) {
if (directon == "up") {
BrightnessUpPress()
print("BR - Up")
} else {
BrightnessDownPress()
print("BR - Down")
}
@objc func handleBrightUp() {
// HIDPostAuxKey(Int(NX_KEYTYPE_BRIGHTNESS_UP))
let sender = BrightnessDownPress()
sender.send()
}
@objc func handlePrev() {
HIDPostAuxKey(Int(NX_KEYTYPE_PREVIOUS))
}
@objc func handlePlay() {
HIDPostAuxKey(Int(NX_KEYTYPE_PLAY))
}
@objc func handleNext() {
HIDPostAuxKey(Int(NX_KEYTYPE_NEXT))
}
// func getBattery() {
// var error: NSDictionary?
// if let scriptObject = NSAppleScript(source: <#T##String#>) {
// if let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError(
// &error) {
// print(output.stringValue)
// } else if (error != nil) {
// print("error: \(error)")
// }
// }
// }
}
+18 -2
View File
@@ -10,10 +10,26 @@ import Cocoa
extension NSTouchBarItem.Identifier {
static let escButton = NSTouchBarItem.Identifier("com.toxblh.mtmr.escButton")
static let time = NSTouchBarItem.Identifier("com.toxblh.mtmr.time")
// Volume
static let volumeUp = NSTouchBarItem.Identifier("com.toxblh.mtmr.volumeUp")
static let volumeDown = NSTouchBarItem.Identifier("com.toxblh.mtmr.volumeDown")
static let preferences = NSTouchBarItem.Identifier("com.toxblh.mtmr.preferences")
// Brightness
static let brightUp = NSTouchBarItem.Identifier("com.toxblh.mtmr.brightUp")
static let brightDown = NSTouchBarItem.Identifier("com.toxblh.mtmr.brightDown")
// Music
static let prev = NSTouchBarItem.Identifier("com.toxblh.mtmr.prev")
static let next = NSTouchBarItem.Identifier("com.toxblh.mtmr.next")
static let play = NSTouchBarItem.Identifier("com.toxblh.mtmr.play")
// Plugins
static let sleep = NSTouchBarItem.Identifier("com.toxblh.mtmr.sleep")
static let weather = NSTouchBarItem.Identifier("com.toxblh.mtmr.weather")
static let time = NSTouchBarItem.Identifier("com.toxblh.mtmr.time")
static let battery = NSTouchBarItem.Identifier("com.toxblh.mtmr.battery")
static let nowPlaying = NSTouchBarItem.Identifier("com.toxblh.mtmr.nowPlaying")
static let controlStripItem = NSTouchBarItem.Identifier("com.toxblh.mtmr.controlStrip")
}
+1
View File
@@ -7,3 +7,4 @@
//
#import "TouchBarPrivateApi.h"
#import "TouchBarSupport.h"
+3
View File
@@ -5,6 +5,7 @@ extern void DFRElementSetControlStripPresenceForIdentifier(NSTouchBarItemIdentif
extern void DFRSystemModalShowsCloseBoxWhenFrontMost(BOOL);
@interface NSTouchBarItem (PrivateMethods)
+ (void)addSystemTrayItem:(NSTouchBarItem *)item;
@@ -13,6 +14,7 @@ extern void DFRSystemModalShowsCloseBoxWhenFrontMost(BOOL);
@end
@interface NSTouchBar (PrivateMethods)
// presentSystemModalFunctionBar:placement:systemTrayItemIdentifier:
@@ -25,3 +27,4 @@ extern void DFRSystemModalShowsCloseBoxWhenFrontMost(BOOL);
+ (void)minimizeSystemModalFunctionBar:(NSTouchBar *)touchBar;
@end
+7 -3
View File
@@ -6,8 +6,12 @@
// Copyright © 2018 Anton Palgunov. All rights reserved.
//
#ifndef TouchBarSupport_h
#define TouchBarSupport_h
#import <Foundation/Foundation.h>
@interface MediaKeys : NSObject
#endif /* TouchBarSupport_h */
+ (void)decreaseVolume;
+ (void)increaseVolume;
+ (void)muteVolume;
@end
+58 -1
View File
@@ -6,4 +6,61 @@
// Copyright © 2018 Anton Palgunov. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "TouchBarSupport.h"
@implementation MediaKeys
static io_connect_t get_event_driver(void)
{
static mach_port_t sEventDrvrRef = 0;
mach_port_t masterPort, service, iter;
kern_return_t kr;
if (!sEventDrvrRef)
{
kr = IOMasterPort( bootstrap_port, &masterPort );
kr = IOServiceGetMatchingServices( masterPort, IOServiceMatching( kIOHIDSystemClass ), &iter );
service = IOIteratorNext( iter );
kr = IOServiceOpen( service, mach_task_self(),
kIOHIDParamConnectType, &sEventDrvrRef );
IOObjectRelease( service );
IOObjectRelease( iter );
}
return sEventDrvrRef;
}
static void HIDPostAuxKey( const UInt8 auxKeyCode )
{
NXEventData event;
kern_return_t kr;
IOGPoint loc = { 0, 0 };
// Key press event
UInt32 evtInfo = auxKeyCode << 16 | NX_KEYDOWN << 8;
bzero(&event, sizeof(NXEventData));
event.compound.subType = NX_SUBTYPE_AUX_CONTROL_BUTTONS;
event.compound.misc.L[0] = evtInfo;
kr = IOHIDPostEvent( get_event_driver(), NX_SYSDEFINED, loc, &event, kNXEventDataVersion, 0, FALSE );
// Key release event
evtInfo = auxKeyCode << 16 | NX_KEYUP << 8;
bzero(&event, sizeof(NXEventData));
event.compound.subType = NX_SUBTYPE_AUX_CONTROL_BUTTONS;
event.compound.misc.L[0] = evtInfo;
kr = IOHIDPostEvent( get_event_driver(), NX_SYSDEFINED, loc, &event, kNXEventDataVersion, 0, FALSE );
}
+ (void)decreaseVolume {
HIDPostAuxKey(NX_KEYTYPE_SOUND_DOWN);
}
+ (void)increaseVolume {
HIDPostAuxKey(NX_KEYTYPE_SOUND_UP);
}
+ (void)muteVolume {
HIDPostAuxKey(NX_KEYTYPE_MUTE);
}
@end
+29
View File
@@ -0,0 +1,29 @@
set theList to paragraphs of text of (do shell script "pmset -g batt")
set percent to word 6 of theList's item 2
set charge to word 7 of theList's item 2
if (charge = "charging") then
set iconC to "⚡️"
else
set iconC to ""
end if
set remainingRaw to my split(theList's item 2, " ")
set remainingTime to remainingRaw's item 5
if (remainingTime = "(no") then
set strTime to " (?)"
else if (remainingTime = "0:00") then
set strTime to ""
else
set strTime to " (" & remainingTime & ")"
end if
return iconC & percent & "%" & strTime
to split(someText, delimiter)
set AppleScript's text item delimiters to delimiter
set someText to someText's text items
set AppleScript's text item delimiters to {""}
return someText
end split