From b50e1ee84d3b03d20b7c54585548558efef9d912 Mon Sep 17 00:00:00 2001 From: Oleg Pykhalov Date: Sun, 4 Feb 2024 08:30:09 +0300 Subject: Allow to switch monitor inputs. HDMI-1, HDMI-2, DP-1. --- README.md | 19 +++++++++++++++++++ main.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/README.md b/README.md index 6d97db7..4509509 100644 --- a/README.md +++ b/README.md @@ -48,3 +48,22 @@ Toggle brightness between current and minimal values: ```shell curl --request POST 127.0.0.1:49281/toggle ``` + +Switch to HDMI-1: +```shell +curl -d '{"value": "x0090"}' -XPOST 127.0.0.1:49281/set-input +``` + +Switch to HDMI-2: +```shell +curl -d '{"value": "x0091"}' -XPOST 127.0.0.1:49281/set-input +``` + +Switch to DP-1: +```shell +curl -d '{"value": "x00d0"}' -XPOST 127.0.0.1:49281/set-input +``` + +# Thanks + +- [LG 29UM69G fails switching input · Issue #100 · rockowitz/ddcutil](https://github.com/rockowitz/ddcutil/issues/100) diff --git a/main.go b/main.go index 60de258..69e2f58 100644 --- a/main.go +++ b/main.go @@ -27,7 +27,13 @@ type Brightness struct { ToggledValue int } +type Input struct { + Lock bool + Value string +} + var currentBrightness Brightness +var currentInput Input func update() { if currentBrightness.Lock == false { @@ -79,6 +85,49 @@ func ddcutilSetBrightness(brightness Brightness) { } } +func ddcutilSetInput(input Input) { + if currentInput.Lock == false { + currentInput.Lock = true + cmd := exec.Command("ddcutil", "setvcp", "xF4", + input.Value, + "--i2c-source-addr=x50", + "--noverify") + log.Println(cmd.String()) + _, err := cmd.CombinedOutput() + if err != nil { + log.Println(err) + return + } + currentInput.Value = input.Value + currentInput.Lock = false + } else { + log.Println("Active lock, no operation performed") + return + } +} + +func setInput(w http.ResponseWriter, r *http.Request) { + if r.Method == "POST" { + body, err := io.ReadAll(r.Body) + if err != nil { + log.Println(err) + return + } + var input Input + json.Unmarshal([]byte(body), &input) + newValue := Input{Value: input.Value} + if newValue.Value == currentInput.Value { + log.Println("Input is the same") + } else { + ddcutilSetInput(newValue) + log.Println("Current input is", currentInput.Value) + } + } else { + log.Println("Active lock, no operation performed") + return + } +} + func set(w http.ResponseWriter, r *http.Request) { if r.Method == "POST" { body, err := io.ReadAll(r.Body) @@ -172,5 +221,6 @@ func main() { http.HandleFunc("/decrease", decrease) http.HandleFunc("/increase", increase) http.HandleFunc("/toggle", toggle) + http.HandleFunc("/set-input", setInput) log.Fatal(http.ListenAndServe(configuration.Listen, nil)) } -- cgit v1.2.3