diff --git a/MonitorControl/AppDelegate.swift b/MonitorControl/AppDelegate.swift index d9879b9..4c93d89 100644 --- a/MonitorControl/AppDelegate.swift +++ b/MonitorControl/AppDelegate.swift @@ -207,26 +207,21 @@ class AppDelegate: NSObject, NSApplicationDelegate, MediaKeyTapDelegate { guard let currentDisplay = Utils.getCurrentDisplay(from: displays) else { return } let allDisplays = prefs.bool(forKey: Utils.PrefKeys.allScreens.rawValue) ? displays : [currentDisplay] for display in allDisplays { - var rel = 0 - if prefs.bool(forKey: "\(display.identifier)-state") { + if (prefs.object(forKey: "\(display.identifier)-state") as? Bool) ?? true { switch mediaKey { case .brightnessUp: - rel = +self.step - let value = display.calcNewValue(for: BRIGHTNESS, withRel: rel) + let value = display.calcNewValue(for: BRIGHTNESS, withRel: +step) display.setBrightness(to: value) case .brightnessDown: - rel = -self.step - let value = currentDisplay.calcNewValue(for: BRIGHTNESS, withRel: rel) + let value = currentDisplay.calcNewValue(for: BRIGHTNESS, withRel: -step) display.setBrightness(to: value) case .mute: display.mute() case .volumeUp: - rel = +self.step - let value = display.calcNewValue(for: AUDIO_SPEAKER_VOLUME, withRel: rel) + let value = display.calcNewValue(for: AUDIO_SPEAKER_VOLUME, withRel: +step) display.setVolume(to: value) case .volumeDown: - rel = -self.step - let value = display.calcNewValue(for: AUDIO_SPEAKER_VOLUME, withRel: rel) + let value = display.calcNewValue(for: AUDIO_SPEAKER_VOLUME, withRel: -step) display.setVolume(to: value) default: return diff --git a/MonitorControl/Objects/ButtonCellView.swift b/MonitorControl/Objects/ButtonCellView.swift index 292ff26..fa6aafc 100644 --- a/MonitorControl/Objects/ButtonCellView.swift +++ b/MonitorControl/Objects/ButtonCellView.swift @@ -28,7 +28,10 @@ class ButtonCellView: NSTableCellView { default: break } + + #if DEBUG print("Toggle enabled display state -> \(sender.state == .on ? "on" : "off")") + #endif } } } diff --git a/MonitorControl/Prefs/DisplayPrefsViewController.swift b/MonitorControl/Prefs/DisplayPrefsViewController.swift index 36d585e..1f1a46e 100644 --- a/MonitorControl/Prefs/DisplayPrefsViewController.swift +++ b/MonitorControl/Prefs/DisplayPrefsViewController.swift @@ -42,7 +42,10 @@ class DisplayPrefsViewController: NSViewController, MASPreferencesViewController prefs.set(false, forKey: Utils.PrefKeys.allScreens.rawValue) default: break } + + #if DEBUG print("Toggle allScreens state -> \(sender.state == .on ? "on" : "off")") + #endif } // MARK: - Table datasource @@ -65,10 +68,7 @@ class DisplayPrefsViewController: NSViewController, MASPreferencesViewController let name = Utils.getDisplayName(forEdid: edid) let serial = Utils.getDisplaySerial(forEdid: edid) - var isEnabled = true - if let enabled = prefs.object(forKey: "\(id)-state") as? Bool { - isEnabled = enabled - } + let isEnabled = (prefs.object(forKey: "\(id)-state") as? Bool) ?? true let display = Display(id, name: name, serial: serial, isEnabled: isEnabled) displays.append(display) diff --git a/MonitorControl/Prefs/KeysPrefsViewController.swift b/MonitorControl/Prefs/KeysPrefsViewController.swift index 298a922..c78b943 100644 --- a/MonitorControl/Prefs/KeysPrefsViewController.swift +++ b/MonitorControl/Prefs/KeysPrefsViewController.swift @@ -26,7 +26,11 @@ class KeysPrefsViewController: NSViewController, MASPreferencesViewController { @IBAction func listenForChanged(_ sender: NSPopUpButton) { prefs.set(sender.selectedTag(), forKey: Utils.PrefKeys.listenFor.rawValue) + + #if DEBUG print("Toggle keys listened for state state -> \(sender.selectedItem?.title ?? "")") + #endif + NotificationCenter.default.post(name: Notification.Name.init(Utils.PrefKeys.listenFor.rawValue), object: nil) } diff --git a/MonitorControl/Prefs/MainPrefsViewController.swift b/MonitorControl/Prefs/MainPrefsViewController.swift index 6f78226..322ddca 100644 --- a/MonitorControl/Prefs/MainPrefsViewController.swift +++ b/MonitorControl/Prefs/MainPrefsViewController.swift @@ -40,7 +40,10 @@ class MainPrefsViewController: NSViewController, MASPreferencesViewController { SMLoginItemSetEnabled(identifier, false) default: break } + + #if DEBUG print("Toggle start at login state -> \(sender.state == .on ? "on" : "off")") + #endif } @IBAction func showContrastSliderClicked(_ sender: NSButton) { @@ -51,7 +54,11 @@ class MainPrefsViewController: NSViewController, MASPreferencesViewController { prefs.set(false, forKey: Utils.PrefKeys.showContrast.rawValue) default: break } + + #if DEBUG print("Toggle show contrast slider state -> \(sender.state == .on ? "on" : "off")") + #endif + NotificationCenter.default.post(name: Notification.Name.init(Utils.PrefKeys.showContrast.rawValue), object: nil) } @@ -63,6 +70,9 @@ class MainPrefsViewController: NSViewController, MASPreferencesViewController { prefs.set(false, forKey: Utils.PrefKeys.lowerContrast.rawValue) default: break } + + #if DEBUG print("Toggle lower contrast after brightness state -> \(sender.state == .on ? "on" : "off")") + #endif } } diff --git a/MonitorControl/Utils.swift b/MonitorControl/Utils.swift index 54dc70c..abdc81a 100644 --- a/MonitorControl/Utils.swift +++ b/MonitorControl/Utils.swift @@ -9,6 +9,22 @@ import Cocoa class Utils: NSObject { + private static func printCommandValue(_ command: Int32, _ value: Int) { + let cmdString: (Int32) -> String? = { + switch $0 { + case BRIGHTNESS: + return "Brightness" + case CONTRAST: + return "Contrast" + case AUDIO_SPEAKER_VOLUME: + return "Volume" + default: + return nil + } + } + + print("\(cmdString(command) ?? "N/A") value: \(value)") + } // MARK: - DDCCTL @@ -21,7 +37,10 @@ class Utils: NSObject { static func sendCommand(_ command: Int32, toMonitor monitor: CGDirectDisplayID, withValue value: Int) { var wrcmd = DDCWriteCommand(control_id: UInt8(command), new_value: UInt8(value)) DDCWrite(monitor, &wrcmd) - print("\(command == BRIGHTNESS ? "Brightness" : "Volume") value : \(value)") + + #if DEBUG + printCommandValue(command, value) + #endif } /// Get current value of ddcctl command @@ -30,14 +49,18 @@ class Utils: NSObject { /// - command: The command to send /// - monitor: The id of the monitor to send the command to /// - Returns: the value of the command - static func getCommand(_ command: Int32, fromMonitor monitor: CGDirectDisplayID) -> Int { + static func getCommand(_ command: Int32, fromMonitor monitor: CGDirectDisplayID) -> Int? { var readCmd = DDCReadCommand() readCmd.control_id = UInt8(command) readCmd.max_value = 0 readCmd.current_value = 0 DDCRead(monitor, &readCmd) - print("\(command == BRIGHTNESS ? "Brightness" : "Volume") value : \(readCmd.current_value)") - return Int(readCmd.current_value) + + #if DEBUG + printCommandValue(command, Int(readCmd.current_value)) + #endif + + return readCmd.success ? Int(readCmd.current_value) : nil } // MARK: - Menu @@ -75,10 +98,8 @@ class Utils: NSObject { slider.target = handler slider.minValue = 0 slider.maxValue = 100 - slider.integerValue = getCommand(command, fromMonitor: display.identifier) slider.action = #selector(SliderHandler.valueChanged) handler.slider = slider - display.saveValue(slider.integerValue, for: command) view.addSubview(label) view.addSubview(slider) @@ -88,6 +109,26 @@ class Utils: NSObject { menu.insertItem(item, at: 0) menu.insertItem(NSMenuItem.separator(), at: 1) + DispatchQueue.global(qos: .background).async { + var val: Int? + + for _ in 0...100 { + if let res = getCommand(command, fromMonitor: display.identifier) { + val = res + break + } + usleep(40000) + } + + if let val = val { + display.saveValue(val, for: command) + + DispatchQueue.main.async { + slider.integerValue = val + } + } + } + return handler } diff --git a/Podfile b/Podfile index 4c88f13..ccf47fc 100644 --- a/Podfile +++ b/Podfile @@ -7,5 +7,4 @@ target 'MonitorControl' do pod 'MediaKeyTap', :git => 'https://github.com/the0neyouseek/MediaKeyTap.git' pod 'MASPreferences' - end diff --git a/Podfile.lock b/Podfile.lock index b53b1c9..2d13a05 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -12,13 +12,13 @@ EXTERNAL SOURCES: CHECKOUT OPTIONS: MediaKeyTap: - :commit: f369a24f6c9931f2a1485a6cd1cef94a432bb36e + :commit: 61a64c17d598de7126f24befde6bdbd5dc7fa121 :git: https://github.com/the0neyouseek/MediaKeyTap.git SPEC CHECKSUMS: MASPreferences: c08b8622dd17b47da87669e741efd7c92e970e8c MediaKeyTap: b652877e9ae2d52ca4f5310fa5152945ad3f0798 -PODFILE CHECKSUM: 3404918793c88a6c1f56a30e54cf0d9afb0cfffb +PODFILE CHECKSUM: efa14c79ecdfeaf061bbc8ed950ee540d77f987a -COCOAPODS: 1.3.1 +COCOAPODS: 1.4.0 diff --git a/ddcctl b/ddcctl index d11c3f4..3d38860 160000 --- a/ddcctl +++ b/ddcctl @@ -1 +1 @@ -Subproject commit d11c3f4a63a090923b55ec93f363b381f2729d0c +Subproject commit 3d38860d5b1de15199a42d9c5a34756738ca9b2a