Skip to content

Commit

Permalink
🎉 1.0
Browse files Browse the repository at this point in the history
- Code rework
- Native OSD
- Change default shortcuts for problems with apps
- Codesign app

Signed-off-by: Guillaume Broder <[email protected]>
  • Loading branch information
the0neyouseek committed Sep 24, 2017
1 parent 38137e8 commit 036a29d
Show file tree
Hide file tree
Showing 33 changed files with 691 additions and 160 deletions.
Binary file added .github/menulet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/osd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions License.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright © 2017

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions MonitorControl.OSX/Bridging-Header.h

This file was deleted.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//
// AppDelegate.swift
// MonitorControl.OSX
// MonitorControl
//
// Created by Mathew Kurian on 9/26/16.
// Copyright © 2016 Mathew Kurian. All rights reserved.
// Last edited by Guillaume Broder on 9/17/2017
// MIT Licensed. 2017.
//

import Cocoa
Expand All @@ -18,47 +19,13 @@ struct Display {
var app: AppDelegate! = nil
let prefs = UserDefaults.standard

func ddcctl(monitor: CGDirectDisplayID, command: Int32, value: Int) {
var wrcmd = DDCWriteCommand(control_id: UInt8(command), new_value: UInt8(value))
DDCWrite(monitor, &wrcmd)
print(value)
}

class SliderHandler : NSObject {
var display : Display
var command : Int32 = 0

public init(display: Display, command: Int32) {
self.display = display
self.command = command
}

func valueChanged(slider: NSSlider) {
let snapInterval = 25
let snapThreshold = 3

var value = slider.integerValue

let closest = (value + snapInterval / 2) / snapInterval * snapInterval
if abs(closest - value) <= snapThreshold {
value = closest
slider.integerValue = value
}

ddcctl(monitor: display.id, command: command, value: value)

prefs.setValue(value, forKey: "\(command)-\(display.serial)")
prefs.synchronize()
}
}

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet weak var statusMenu: NSMenu!
@IBOutlet weak var window: NSWindow!

let statusItem = NSStatusBar.system().statusItem(withLength: NSVariableStatusItemLength)
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)

var monitorItems: [NSMenuItem] = []
var displays: [Display] = []
Expand All @@ -68,14 +35,16 @@ class AppDelegate: NSObject, NSApplicationDelegate {
var defaultBrightnessSlider: NSSlider! = nil
var defaultVolumeSlider: NSSlider! = nil

let step = 100/16;

@IBAction func quitClicked(_ sender: AnyObject) {
NSApplication.shared().terminate(self)
NSApplication.shared.terminate(self)
}

func applicationDidFinishLaunching(_ aNotification: Notification) {
app = self

statusItem.title = ""
statusItem.image = NSImage.init(named: NSImage.Name(rawValue: "status"))
statusItem.menu = statusMenu

acquirePrivileges()
Expand All @@ -84,39 +53,63 @@ class AppDelegate: NSObject, NSApplicationDelegate {
updateDisplays()

NSEvent.addGlobalMonitorForEvents(
matching: NSEventMask.keyDown, handler: {(event: NSEvent) in
matching: NSEvent.EventTypeMask.keyDown, handler: {(event: NSEvent) in
if self.defaultDisplay == nil {
return
}

let modifiers = NSEventModifierFlags.init(rawValue: NSEventModifierFlags.command.rawValue |
NSEventModifierFlags.control.rawValue |
NSEventModifierFlags.option.rawValue |
NSEventModifierFlags.shift.rawValue)
var flags = event.modifierFlags.intersection(modifiers)

if !flags.contains(NSEventModifierFlags.command) {
return
}
flags.subtract(NSEventModifierFlags.command)

var rel = 0
if event.keyCode == 27 {
rel = -5
} else if event.keyCode == 24 {
rel = +5
} else {
// Keyboard shortcut only for main screen
let currentDisplayId = NSScreen.main?.deviceDescription[NSDeviceDescriptionKey.init("NSScreenNumber")] as! CGDirectDisplayID
if (self.defaultDisplay.id != currentDisplayId) {
return
}

// Brightness -> Shift + Control + Alt + Command + (Up/Down)
// Volume -> Shift + Control + Alt + Command + (Left/Right)
// Mute -> Minus

// Capture keys
let modifiers = NSEvent.ModifierFlags.init(rawValue: NSEvent.ModifierFlags.shift.rawValue | NSEvent.ModifierFlags.command.rawValue | NSEvent.ModifierFlags.control.rawValue | NSEvent.ModifierFlags.option.rawValue)
let flags = event.modifierFlags.intersection(modifiers)

// Only do something if all modifiers are active
if !flags.contains(NSEvent.ModifierFlags.shift) || !flags.contains(NSEvent.ModifierFlags.command) || !flags.contains(NSEvent.ModifierFlags.control) || !flags.contains(NSEvent.ModifierFlags.option) {
return
}

var brightnessRel = 0
var volumeRel = 0
var rel = 0

// Down key
if event.keyCode == Utils.key.keyDownArrow.rawValue {
brightnessRel = -self.step
// Up key
} else if event.keyCode == Utils.key.keyUpArrow.rawValue {
brightnessRel = +self.step
// Left key
} else if event.keyCode == Utils.key.keyLeftArrow.rawValue {
volumeRel = -self.step
// Right key
} else if event.keyCode == Utils.key.keyRightArrow.rawValue {
volumeRel = +self.step
// M key
} else if event.keyCode == Utils.key.keyMute.rawValue {
volumeRel = -100
} else {
return
}

var command = Int32()
var slider: NSSlider! = nil
if flags == NSEventModifierFlags.option {
if brightnessRel == 0 {
command = AUDIO_SPEAKER_VOLUME
slider = self.defaultVolumeSlider
} else if flags == NSEventModifierFlags.shift {
rel = volumeRel
} else if volumeRel == 0 {
command = BRIGHTNESS
slider = self.defaultBrightnessSlider
rel = brightnessRel
} else {
return
}
Expand All @@ -128,30 +121,27 @@ class AppDelegate: NSObject, NSApplicationDelegate {
prefs.synchronize()
slider.intValue = Int32(value)

ddcctl(monitor: self.defaultDisplay.id, command: command, value: value)
Utils.ddcctl(monitor: self.defaultDisplay.id, command: command, value: value)

// OSD
let manager : OSDManager = OSDManager.sharedManager() as! OSDManager
var osdImage : Int = 1 // Brightness Image
if brightnessRel == 0 {
osdImage = 3 // Speaker image
if value == 0 {
osdImage = 4 // Mute speaker
}
}
manager.showImage(Int64(osdImage), onDisplayID: self.defaultDisplay.id, priority: 0x1f4, msecUntilFade: 2000, filledChiclets: UInt32(value/self.step), totalChiclets: UInt32(100/self.step), locked: false)
})
}

func makeLabel(text: String, frame: NSRect) -> NSTextField {
let label = NSTextField(frame: frame)
label.stringValue = text
label.isBordered = false
label.isBezeled = false
label.isEditable = false
label.drawsBackground = false
return label
}

func addSliderItem(menu: NSMenu, isDefaultDisplay: Bool, display: Display, command: Int32, title: String, shortcut: String) -> NSSlider {
func addSliderItem(menu: NSMenu, isDefaultDisplay: Bool, display: Display, command: Int32, title: String) -> NSSlider {
let item = NSMenuItem()

let view = NSView(frame: NSRect(x: 0, y: 5, width: 250, height: 40))

let label = makeLabel(text: title, frame: NSRect(x: 20, y: 19, width: 130, height: 20))

let labelKeyCode = makeLabel(text: shortcut, frame: NSRect(x: 120, y: 19, width: 100, height: 20))
labelKeyCode.isHidden = !isDefaultDisplay
labelKeyCode.alignment = NSTextAlignment.right
let label = Utils.makeLabel(text: title, frame: NSRect(x: 20, y: 19, width: 130, height: 20))

let handler = SliderHandler(display: display, command: command)
sliderHandlers.append(handler)
Expand All @@ -161,14 +151,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
slider.minValue = 0
slider.maxValue = 100
slider.integerValue = prefs.integer(forKey: "\(command)-\(display.serial)")
slider.action = #selector(SliderHandler.valueChanged)
slider.action = #selector(SliderHandler.valueChanged)

view.addSubview(label)
view.addSubview(labelKeyCode)
view.addSubview(slider)

item.view = view

menu.addItem(item)
menu.addItem(NSMenuItem.separator())

Expand All @@ -183,14 +172,15 @@ class AppDelegate: NSObject, NSApplicationDelegate {
for m in monitorItems {
statusMenu.removeItem(m)
}

monitorItems = []
displays = []
sliderHandlers = []

sleep(1)

for s in NSScreen.screens()! {
let id = s.deviceDescription["NSScreenNumber"] as! CGDirectDisplayID
for s in NSScreen.screens {
let id = s.deviceDescription[NSDeviceDescriptionKey.init("NSScreenNumber")] as! CGDirectDisplayID
if CGDisplayIsBuiltin(id) != 0 {
continue
}
Expand All @@ -211,17 +201,16 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let monitorMenuItem = NSMenuItem()
let monitorSubMenu = NSMenu()

let brightnessSlider = addSliderItem(menu: monitorSubMenu, isDefaultDisplay: isDefaultDisplay, display: d, command: BRIGHTNESS, title: "Brightness", shortcut: "⇧⌘- / ⇧⌘+")
let _ = addSliderItem(menu: monitorSubMenu, isDefaultDisplay: isDefaultDisplay, display: d, command: CONTRAST, title: "Contrast", shortcut: "")
let volumeSlider = addSliderItem(menu: monitorSubMenu, isDefaultDisplay: isDefaultDisplay, display: d, command: AUDIO_SPEAKER_VOLUME, title: "Volume", shortcut: "⌥⌘- / ⌥⌘+")

let brightnessSlider = addSliderItem(menu: monitorSubMenu, isDefaultDisplay: isDefaultDisplay, display: d, command: BRIGHTNESS, title: NSLocalizedString("Brightness", comment: "Sown in menu"))
let _ = addSliderItem(menu: monitorSubMenu, isDefaultDisplay: isDefaultDisplay, display: d, command: CONTRAST, title: NSLocalizedString("Contrast", comment: "Shown in menu"))
let volumeSlider = addSliderItem(menu: monitorSubMenu, isDefaultDisplay: isDefaultDisplay, display: d, command: AUDIO_SPEAKER_VOLUME, title: NSLocalizedString("Volume", comment: "Shown in menu"))

let defaultMonitorItem = NSMenuItem()
let defaultMonitorView = NSView(frame: NSRect(x: 0, y: 5, width: 250, height: 25))

let defaultMonitorSelectButtom = NSButton(frame: NSRect(x: 25, y: 0, width: 200, height: 25))
defaultMonitorSelectButtom.title = isDefaultDisplay ? "Default" : "Set as default"
defaultMonitorSelectButtom.bezelStyle = NSRoundRectBezelStyle
defaultMonitorSelectButtom.title = isDefaultDisplay ? NSLocalizedString("Default", comment: "Shown in menu") : NSLocalizedString("Set as default", comment: "Shown in menu")
defaultMonitorSelectButtom.bezelStyle = NSButton.BezelStyle.rounded
defaultMonitorSelectButtom.isEnabled = !isDefaultDisplay

defaultMonitorView.addSubview(defaultMonitorSelectButtom)
Expand All @@ -246,19 +235,19 @@ class AppDelegate: NSObject, NSApplicationDelegate {
if defaultDisplay == nil {
// If no DDC capable display was detected
let item = NSMenuItem()
item.title = "No supported display found"
item.title = NSLocalizedString("No supported display found", comment: "Shown in menu")
item.isEnabled = false
monitorItems.append(item)
statusMenu.insertItem(item, at: 0)
}
}
func acquirePrivileges() {
let options: NSDictionary = [kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString: true]
let accessibilityEnabled = AXIsProcessTrustedWithOptions(options)

if !accessibilityEnabled {
print("You need to enable the keylogger in the System Prefrences")
print(NSLocalizedString("You need to enable the keylogger in the System Prefrences for the keyboard shortcuts to work", comment: ""))
}

return
Expand Down Expand Up @@ -293,10 +282,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

func getDisplayName(_ edid: EDID) -> String {
return getDescriptorString(edid, 0xFC) ?? "Display"
return getDescriptorString(edid, 0xFC) ?? NSLocalizedString("Display", comment: "")
}

func getDisplaySerial(_ edid: EDID) -> String {
return getDescriptorString(edid, 0xFF) ?? "Unknown"
return getDescriptorString(edid, 0xFF) ?? NSLocalizedString("Unknown", comment: "")
}
}
6 changes: 6 additions & 0 deletions MonitorControl/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
25 changes: 25 additions & 0 deletions MonitorControl/Assets.xcassets/status.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "status.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
},
"properties" : {
"template-rendering-intent" : "template"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 036a29d

Please sign in to comment.