Skip to content

Commit

Permalink
Add Apple Silicon support & major revamp (3.0.0) (MonitorControl#520)
Browse files Browse the repository at this point in the history
Co-authored-by: Joni Van Roost <[email protected]>
Co-authored-by: Rayan Khan <[email protected]>
Co-authored-by: Guillaume B <[email protected]>
  • Loading branch information
4 people authored Aug 21, 2021
1 parent fd142cb commit d46c429
Show file tree
Hide file tree
Showing 91 changed files with 3,967 additions and 2,626 deletions.
2 changes: 1 addition & 1 deletion .bartycrouch.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[update]
tasks = ["interfaces", "code", "transform", "normalize"]
tasks = ["interfaces", "code", "normalize"]

[update.interfaces]
paths = ["."]
Expand Down
Binary file added .github/Icon-1024.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/MonitorControl Icon.fig
Binary file not shown.
Binary file removed .github/menuadvanced.png
Binary file not shown.
Binary file removed .github/menudisplay.png
Binary file not shown.
Binary file removed .github/menugeneral.png
Binary file not shown.
Binary file removed .github/menukeys.png
Binary file not shown.
Binary file removed .github/menulet.png
Binary file not shown.
Binary file removed .github/osd.jpg
Binary file not shown.
Binary file added .github/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
155 changes: 69 additions & 86 deletions MonitorControl.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

385 changes: 266 additions & 119 deletions MonitorControl/AppDelegate.swift

Large diffs are not rendered by default.

66 changes: 33 additions & 33 deletions MonitorControl/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
{
"images" : [
{
"size" : "16x16",
"idiom" : "mac",
"filename" : "Icon-16.png",
"scale" : "1x"
"idiom" : "mac",
"scale" : "1x",
"size" : "16x16"
},
{
"size" : "16x16",
"idiom" : "mac",
"filename" : "Icon-33.png",
"scale" : "2x"
"idiom" : "mac",
"scale" : "2x",
"size" : "16x16"
},
{
"size" : "32x32",
"idiom" : "mac",
"filename" : "Icon-32.png",
"scale" : "1x"
"idiom" : "mac",
"scale" : "1x",
"size" : "32x32"
},
{
"size" : "32x32",
"idiom" : "mac",
"filename" : "Icon-64.png",
"scale" : "2x"
"idiom" : "mac",
"scale" : "2x",
"size" : "32x32"
},
{
"size" : "128x128",
"idiom" : "mac",
"filename" : "Icon-128.png",
"scale" : "1x"
"idiom" : "mac",
"scale" : "1x",
"size" : "128x128"
},
{
"size" : "128x128",
"idiom" : "mac",
"filename" : "Icon-257.png",
"scale" : "2x"
"idiom" : "mac",
"scale" : "2x",
"size" : "128x128"
},
{
"size" : "256x256",
"idiom" : "mac",
"filename" : "Icon-256.png",
"scale" : "1x"
"idiom" : "mac",
"scale" : "1x",
"size" : "256x256"
},
{
"size" : "256x256",
"idiom" : "mac",
"filename" : "Icon-513.png",
"scale" : "2x"
"idiom" : "mac",
"scale" : "2x",
"size" : "256x256"
},
{
"size" : "512x512",
"idiom" : "mac",
"filename" : "Icon-512.png",
"scale" : "1x"
"idiom" : "mac",
"scale" : "1x",
"size" : "512x512"
},
{
"size" : "512x512",
"idiom" : "mac",
"filename" : "Icon-1024.png",
"scale" : "2x"
"idiom" : "mac",
"scale" : "2x",
"size" : "512x512"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "xcode",
"version" : 1
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified MonitorControl/Assets.xcassets/AppIcon.appiconset/Icon-128.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 modified MonitorControl/Assets.xcassets/AppIcon.appiconset/Icon-16.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 modified MonitorControl/Assets.xcassets/AppIcon.appiconset/Icon-256.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 modified MonitorControl/Assets.xcassets/AppIcon.appiconset/Icon-257.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 modified MonitorControl/Assets.xcassets/AppIcon.appiconset/Icon-32.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 modified MonitorControl/Assets.xcassets/AppIcon.appiconset/Icon-33.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 modified MonitorControl/Assets.xcassets/AppIcon.appiconset/Icon-512.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 modified MonitorControl/Assets.xcassets/AppIcon.appiconset/Icon-513.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 modified MonitorControl/Assets.xcassets/AppIcon.appiconset/Icon-64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions MonitorControl/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "xcode",
"version" : 1
}
}
}
23 changes: 0 additions & 23 deletions MonitorControl/Assets.xcassets/KeyboardPref.imageset/Contents.json

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 0 additions & 1 deletion MonitorControl/Extensions/NSNotification+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Cocoa
extension NSNotification.Name {
static let accessibilityApi = NSNotification.Name(rawValue: "com.apple.accessibility.api")
static let listenFor = NSNotification.Name(rawValue: Utils.PrefKeys.listenFor.rawValue)
static let showContrast = NSNotification.Name(rawValue: Utils.PrefKeys.showContrast.rawValue)
static let friendlyName = NSNotification.Name(rawValue: Utils.PrefKeys.friendlyName.rawValue)
static let preferenceReset = NSNotification.Name(rawValue: Utils.PrefKeys.preferenceReset.rawValue)
static let displayListUpdate = NSNotification.Name(rawValue: Utils.PrefKeys.displayListUpdate.rawValue)
Expand Down
9 changes: 2 additions & 7 deletions MonitorControl/Extensions/NSScreen+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,8 @@ public extension NSScreen {
while case let object = IOIteratorNext(servicePortIterator), object != 0 {
let dict = (IODisplayCreateInfoDictionary(object, UInt32(kIODisplayOnlyPreferredName)).takeRetainedValue() as NSDictionary as? [String: AnyObject])!

if dict[kDisplayVendorID] as? UInt32 == self.vendorNumber,
dict[kDisplayProductID] as? UInt32 == self.modelNumber,
dict[kDisplaySerialNumber] as? UInt32 == self.serialNumber
{
if let productName = dict["DisplayProductName"] as? [String: String],
let firstKey = Array(productName.keys).first
{
if dict[kDisplayVendorID] as? UInt32 == self.vendorNumber, dict[kDisplayProductID] as? UInt32 == self.modelNumber, dict[kDisplaySerialNumber] as? UInt32 == self.serialNumber {
if let productName = dict["DisplayProductName"] as? [String: String], let firstKey = Array(productName.keys).first {
return productName[firstKey]!
}
}
Expand Down
5 changes: 2 additions & 3 deletions MonitorControl/Extensions/Preferences+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Preferences

extension Preferences.PaneIdentifier {
static let main = Self("Main")
static let keys = Self("Keys")
static let advanced = Self("Advanced")
static let display = Self("Display")
static let displays = Self("Displays")
static let about = Self("About")
}
2 changes: 1 addition & 1 deletion MonitorControl/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>947</string>
<string>2866</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
116 changes: 107 additions & 9 deletions MonitorControl/Manager/DisplayManager.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import Cocoa
import DDC

class DisplayManager {
public static let shared = DisplayManager()

private var displays: [Display] {
didSet {
NotificationCenter.default.post(name: Notification.Name(Utils.PrefKeys.displayListUpdate.rawValue), object: nil)
}
}
private var displays: [Display]

init() {
self.displays = []
Expand All @@ -17,13 +14,33 @@ class DisplayManager {
self.displays = displays
}

func getExternalDisplays() -> [ExternalDisplay] {
return self.displays.compactMap { $0 as? ExternalDisplay }
}

func getAllDisplays() -> [Display] {
return self.displays
}

func getAllNonVirtualDisplays() -> [Display] {
return self.displays.compactMap { display -> Display? in
if !display.isVirtual {
return display
} else { return nil }
}
}

func getDdcCapableDisplays() -> [ExternalDisplay] {
return self.displays.compactMap { display -> ExternalDisplay? in
if let externalDisplay = display as? ExternalDisplay, externalDisplay.ddc != nil {
if let externalDisplay = display as? ExternalDisplay, !externalDisplay.isSw(), !externalDisplay.isVirtual {
return externalDisplay
} else { return nil }
}
}

func getNonVirtualExternalDisplays() -> [ExternalDisplay] {
return self.displays.compactMap { display -> ExternalDisplay? in
if let externalDisplay = display as? ExternalDisplay, !externalDisplay.isVirtual {
return externalDisplay
} else { return nil }
}
Expand All @@ -34,10 +51,12 @@ class DisplayManager {
}

func getCurrentDisplay() -> Display? {
guard let mainDisplayID = NSScreen.main?.displayID else {
return nil
let mouseLocation = NSEvent.mouseLocation
let screens = NSScreen.screens
if let screenWithMouse = (screens.first { NSMouseInRect(mouseLocation, $0.frame, false) }) {
return self.displays.first { $0.identifier == screenWithMouse.displayID }
}
return self.displays.first { $0.identifier == mainDisplayID }
return nil
}

func addDisplay(display: Display) {
Expand All @@ -53,4 +72,83 @@ class DisplayManager {
func clearDisplays() {
self.displays = []
}

func resetSwBrightnessForAllDisplays(settingsOnly: Bool = false, async: Bool = false) {
for externalDisplay in self.getNonVirtualExternalDisplays() {
if !settingsOnly {
_ = externalDisplay.setSwBrightness(value: externalDisplay.getSwMaxBrightness(), smooth: async)
} else {
externalDisplay.saveSwBirghtnessPrefValue(Int(externalDisplay.getSwMaxBrightness()))
}
if externalDisplay.isSw() {
externalDisplay.saveValue(Int(externalDisplay.getSwMaxBrightness()), for: .brightness)
}
}
}

func setBrightnessSliderValue(externalDisplay: ExternalDisplay, value: Int32) {
if let slider = externalDisplay.brightnessSliderHandler?.slider {
slider.intValue = value
}
}

func getBrightnessSliderMaxValue(externalDisplay: ExternalDisplay) -> Double {
if let slider = externalDisplay.brightnessSliderHandler?.slider {
return slider.maxValue
}
return 0
}

func restoreSwBrightnessForAllDisplays(async: Bool = false) {
for externalDisplay in self.getExternalDisplays() {
let sliderMax = self.getBrightnessSliderMaxValue(externalDisplay: externalDisplay)
if externalDisplay.getValue(for: .brightness) == 0 || externalDisplay.isSw() {
let savedPrefValue = externalDisplay.getSwBrightnessPrefValue()
if externalDisplay.getSwBrightness() != savedPrefValue {
if let manager = OSDManager.sharedManager() as? OSDManager { // This will give the user a hint why is the brightness suddenly changes and also give screen activity to counter the 'no gamma change when there is no screen activity' issue on some macs
manager.showImage(OSDImage.brightness.rawValue, onDisplayID: externalDisplay.identifier, priority: 0x1F4, msecUntilFade: 0)
}
}
externalDisplay.saveSwBirghtnessPrefValue(Int(externalDisplay.getSwBrightness()))
_ = externalDisplay.setSwBrightness(value: UInt8(savedPrefValue), smooth: async)
if !externalDisplay.isSw(), prefs.bool(forKey: Utils.PrefKeys.lowerSwAfterBrightness.rawValue) {
if savedPrefValue < externalDisplay.getSwMaxBrightness() {
self.setBrightnessSliderValue(externalDisplay: externalDisplay, value: Int32(Float(sliderMax / 2) * (Float(savedPrefValue) / Float(externalDisplay.getSwMaxBrightness()))))
} else {
self.setBrightnessSliderValue(externalDisplay: externalDisplay, value: Int32(sliderMax / 2) + Int32(externalDisplay.getValue(for: DDC.Command.brightness)))
}
} else if externalDisplay.isSw() {
self.setBrightnessSliderValue(externalDisplay: externalDisplay, value: Int32(Float(sliderMax) * (Float(savedPrefValue) / Float(externalDisplay.getSwMaxBrightness()))))
}
} else {
_ = externalDisplay.setSwBrightness(value: externalDisplay.getSwMaxBrightness())
if externalDisplay.isSw() {
self.setBrightnessSliderValue(externalDisplay: externalDisplay, value: Int32(sliderMax))
}
}
}
}

func getDisplayNameByID(displayID: CGDirectDisplayID) -> String {
let defaultName: String = NSLocalizedString("Unknown", comment: "Unknown display name")
if #available(macOS 11.0, *) {
if let dictionary = ((CoreDisplay_DisplayCreateInfoDictionary(displayID))?.takeRetainedValue() as NSDictionary?), let nameList = dictionary["DisplayProductName"] as? [String: String], var name = nameList[Locale.current.identifier] ?? nameList["en_US"] ?? nameList.first?.value {
if CGDisplayIsInHWMirrorSet(displayID) != 0 || CGDisplayIsInMirrorSet(displayID) != 0 {
let mirroredDisplayID = CGDisplayMirrorsDisplay(displayID)
if mirroredDisplayID != 0, let dictionary = ((CoreDisplay_DisplayCreateInfoDictionary(mirroredDisplayID))?.takeRetainedValue() as NSDictionary?), let nameList = dictionary["DisplayProductName"] as? [String: String], let mirroredName = nameList[Locale.current.identifier] ?? nameList["en_US"] ?? nameList.first?.value {
name.append("~" + mirroredName)
}
}
return name
}
}
if let screen = NSScreen.getByDisplayID(displayID: displayID) {
if #available(OSX 10.15, *) {
return screen.localizedName
} else {
return screen.displayName ?? defaultName
}
}
return defaultName
}
}
Loading

0 comments on commit d46c429

Please sign in to comment.