Skip to content

Commit a784f19

Browse files
reitermarkusthe0neyouseek
authored andcommitted
🔧 Fix function keys not working and retry more often when getting values. (MonitorControl#21)
* Update `ddcctl` submodule. * Update dependencies. * Enable displays by default. * Retry more often to get values from display. * Only print when debugging. Fix MonitorControl#13
1 parent 7510c13 commit a784f19

File tree

9 files changed

+77
-25
lines changed

9 files changed

+77
-25
lines changed

MonitorControl/AppDelegate.swift

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,26 +207,21 @@ class AppDelegate: NSObject, NSApplicationDelegate, MediaKeyTapDelegate {
207207
guard let currentDisplay = Utils.getCurrentDisplay(from: displays) else { return }
208208
let allDisplays = prefs.bool(forKey: Utils.PrefKeys.allScreens.rawValue) ? displays : [currentDisplay]
209209
for display in allDisplays {
210-
var rel = 0
211-
if prefs.bool(forKey: "\(display.identifier)-state") {
210+
if (prefs.object(forKey: "\(display.identifier)-state") as? Bool) ?? true {
212211
switch mediaKey {
213212
case .brightnessUp:
214-
rel = +self.step
215-
let value = display.calcNewValue(for: BRIGHTNESS, withRel: rel)
213+
let value = display.calcNewValue(for: BRIGHTNESS, withRel: +step)
216214
display.setBrightness(to: value)
217215
case .brightnessDown:
218-
rel = -self.step
219-
let value = currentDisplay.calcNewValue(for: BRIGHTNESS, withRel: rel)
216+
let value = currentDisplay.calcNewValue(for: BRIGHTNESS, withRel: -step)
220217
display.setBrightness(to: value)
221218
case .mute:
222219
display.mute()
223220
case .volumeUp:
224-
rel = +self.step
225-
let value = display.calcNewValue(for: AUDIO_SPEAKER_VOLUME, withRel: rel)
221+
let value = display.calcNewValue(for: AUDIO_SPEAKER_VOLUME, withRel: +step)
226222
display.setVolume(to: value)
227223
case .volumeDown:
228-
rel = -self.step
229-
let value = display.calcNewValue(for: AUDIO_SPEAKER_VOLUME, withRel: rel)
224+
let value = display.calcNewValue(for: AUDIO_SPEAKER_VOLUME, withRel: -step)
230225
display.setVolume(to: value)
231226
default:
232227
return

MonitorControl/Objects/ButtonCellView.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ class ButtonCellView: NSTableCellView {
2828
default:
2929
break
3030
}
31+
32+
#if DEBUG
3133
print("Toggle enabled display state -> \(sender.state == .on ? "on" : "off")")
34+
#endif
3235
}
3336
}
3437
}

MonitorControl/Prefs/DisplayPrefsViewController.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ class DisplayPrefsViewController: NSViewController, MASPreferencesViewController
4242
prefs.set(false, forKey: Utils.PrefKeys.allScreens.rawValue)
4343
default: break
4444
}
45+
46+
#if DEBUG
4547
print("Toggle allScreens state -> \(sender.state == .on ? "on" : "off")")
48+
#endif
4649
}
4750

4851
// MARK: - Table datasource
@@ -65,10 +68,7 @@ class DisplayPrefsViewController: NSViewController, MASPreferencesViewController
6568

6669
let name = Utils.getDisplayName(forEdid: edid)
6770
let serial = Utils.getDisplaySerial(forEdid: edid)
68-
var isEnabled = true
69-
if let enabled = prefs.object(forKey: "\(id)-state") as? Bool {
70-
isEnabled = enabled
71-
}
71+
let isEnabled = (prefs.object(forKey: "\(id)-state") as? Bool) ?? true
7272

7373
let display = Display(id, name: name, serial: serial, isEnabled: isEnabled)
7474
displays.append(display)

MonitorControl/Prefs/KeysPrefsViewController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ class KeysPrefsViewController: NSViewController, MASPreferencesViewController {
2626

2727
@IBAction func listenForChanged(_ sender: NSPopUpButton) {
2828
prefs.set(sender.selectedTag(), forKey: Utils.PrefKeys.listenFor.rawValue)
29+
30+
#if DEBUG
2931
print("Toggle keys listened for state state -> \(sender.selectedItem?.title ?? "")")
32+
#endif
33+
3034
NotificationCenter.default.post(name: Notification.Name.init(Utils.PrefKeys.listenFor.rawValue), object: nil)
3135
}
3236

MonitorControl/Prefs/MainPrefsViewController.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ class MainPrefsViewController: NSViewController, MASPreferencesViewController {
4040
SMLoginItemSetEnabled(identifier, false)
4141
default: break
4242
}
43+
44+
#if DEBUG
4345
print("Toggle start at login state -> \(sender.state == .on ? "on" : "off")")
46+
#endif
4447
}
4548

4649
@IBAction func showContrastSliderClicked(_ sender: NSButton) {
@@ -51,7 +54,11 @@ class MainPrefsViewController: NSViewController, MASPreferencesViewController {
5154
prefs.set(false, forKey: Utils.PrefKeys.showContrast.rawValue)
5255
default: break
5356
}
57+
58+
#if DEBUG
5459
print("Toggle show contrast slider state -> \(sender.state == .on ? "on" : "off")")
60+
#endif
61+
5562
NotificationCenter.default.post(name: Notification.Name.init(Utils.PrefKeys.showContrast.rawValue), object: nil)
5663
}
5764

@@ -63,6 +70,9 @@ class MainPrefsViewController: NSViewController, MASPreferencesViewController {
6370
prefs.set(false, forKey: Utils.PrefKeys.lowerContrast.rawValue)
6471
default: break
6572
}
73+
74+
#if DEBUG
6675
print("Toggle lower contrast after brightness state -> \(sender.state == .on ? "on" : "off")")
76+
#endif
6777
}
6878
}

MonitorControl/Utils.swift

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@
99
import Cocoa
1010

1111
class Utils: NSObject {
12+
private static func printCommandValue(_ command: Int32, _ value: Int) {
13+
let cmdString: (Int32) -> String? = {
14+
switch $0 {
15+
case BRIGHTNESS:
16+
return "Brightness"
17+
case CONTRAST:
18+
return "Contrast"
19+
case AUDIO_SPEAKER_VOLUME:
20+
return "Volume"
21+
default:
22+
return nil
23+
}
24+
}
25+
26+
print("\(cmdString(command) ?? "N/A") value: \(value)")
27+
}
1228

1329
// MARK: - DDCCTL
1430

@@ -21,7 +37,10 @@ class Utils: NSObject {
2137
static func sendCommand(_ command: Int32, toMonitor monitor: CGDirectDisplayID, withValue value: Int) {
2238
var wrcmd = DDCWriteCommand(control_id: UInt8(command), new_value: UInt8(value))
2339
DDCWrite(monitor, &wrcmd)
24-
print("\(command == BRIGHTNESS ? "Brightness" : "Volume") value : \(value)")
40+
41+
#if DEBUG
42+
printCommandValue(command, value)
43+
#endif
2544
}
2645

2746
/// Get current value of ddcctl command
@@ -30,14 +49,18 @@ class Utils: NSObject {
3049
/// - command: The command to send
3150
/// - monitor: The id of the monitor to send the command to
3251
/// - Returns: the value of the command
33-
static func getCommand(_ command: Int32, fromMonitor monitor: CGDirectDisplayID) -> Int {
52+
static func getCommand(_ command: Int32, fromMonitor monitor: CGDirectDisplayID) -> Int? {
3453
var readCmd = DDCReadCommand()
3554
readCmd.control_id = UInt8(command)
3655
readCmd.max_value = 0
3756
readCmd.current_value = 0
3857
DDCRead(monitor, &readCmd)
39-
print("\(command == BRIGHTNESS ? "Brightness" : "Volume") value : \(readCmd.current_value)")
40-
return Int(readCmd.current_value)
58+
59+
#if DEBUG
60+
printCommandValue(command, Int(readCmd.current_value))
61+
#endif
62+
63+
return readCmd.success ? Int(readCmd.current_value) : nil
4164
}
4265

4366
// MARK: - Menu
@@ -75,10 +98,8 @@ class Utils: NSObject {
7598
slider.target = handler
7699
slider.minValue = 0
77100
slider.maxValue = 100
78-
slider.integerValue = getCommand(command, fromMonitor: display.identifier)
79101
slider.action = #selector(SliderHandler.valueChanged)
80102
handler.slider = slider
81-
display.saveValue(slider.integerValue, for: command)
82103

83104
view.addSubview(label)
84105
view.addSubview(slider)
@@ -88,6 +109,26 @@ class Utils: NSObject {
88109
menu.insertItem(item, at: 0)
89110
menu.insertItem(NSMenuItem.separator(), at: 1)
90111

112+
DispatchQueue.global(qos: .background).async {
113+
var val: Int?
114+
115+
for _ in 0...100 {
116+
if let res = getCommand(command, fromMonitor: display.identifier) {
117+
val = res
118+
break
119+
}
120+
usleep(40000)
121+
}
122+
123+
if let val = val {
124+
display.saveValue(val, for: command)
125+
126+
DispatchQueue.main.async {
127+
slider.integerValue = val
128+
}
129+
}
130+
}
131+
91132
return handler
92133
}
93134

Podfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ target 'MonitorControl' do
77

88
pod 'MediaKeyTap', :git => 'https://github.com/the0neyouseek/MediaKeyTap.git'
99
pod 'MASPreferences'
10-
1110
end

Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ EXTERNAL SOURCES:
1212

1313
CHECKOUT OPTIONS:
1414
MediaKeyTap:
15-
:commit: f369a24f6c9931f2a1485a6cd1cef94a432bb36e
15+
:commit: 61a64c17d598de7126f24befde6bdbd5dc7fa121
1616
:git: https://github.com/the0neyouseek/MediaKeyTap.git
1717

1818
SPEC CHECKSUMS:
1919
MASPreferences: c08b8622dd17b47da87669e741efd7c92e970e8c
2020
MediaKeyTap: b652877e9ae2d52ca4f5310fa5152945ad3f0798
2121

22-
PODFILE CHECKSUM: 3404918793c88a6c1f56a30e54cf0d9afb0cfffb
22+
PODFILE CHECKSUM: efa14c79ecdfeaf061bbc8ed950ee540d77f987a
2323

24-
COCOAPODS: 1.3.1
24+
COCOAPODS: 1.4.0

ddcctl

0 commit comments

Comments
 (0)