8000 Fix function keys not working and retry more often when getting values. by reitermarkus · Pull Request #21 · MonitorControl/MonitorControl · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions MonitorControl/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions MonitorControl/Objects/ButtonCellView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ class ButtonCellView: NSTableCellView {
default:
break
}

#if DEBUG
print("Toggle enabled display state -> \(sender.state == .on ? "on" : "off")")
#endif
}
}
}
8 changes: 4 additions & 4 deletions MonitorControl/Prefs/DisplayPrefsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions MonitorControl/Prefs/KeysPrefsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
10 changes: 10 additions & 0 deletions MonitorControl/Prefs/MainPrefsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}

Expand All @@ -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
}
}
53 changes: 47 additions & 6 deletions MonitorControl/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
1 change: 0 additions & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ target 'MonitorControl' do

pod 'MediaKeyTap', :git => 'https://github.com/the0neyouseek/MediaKeyTap.git'
pod 'MASPreferences'

end
6 changes: 3 additions & 3 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion ddcctl
Submodule ddcctl updated 2 files
+0 −22 README.md
+50 −0 issue_template.md
0