summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go176
1 files changed, 176 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..60de258
--- /dev/null
+++ b/main.go
@@ -0,0 +1,176 @@
+package main
+
+import (
+ "encoding/json"
+ "io"
+ "io/ioutil"
+ "log"
+ "net/http"
+ "os"
+ "os/exec"
+ "strconv"
+ "strings"
+)
+
+type Configuration struct {
+ Listen string
+ Vcp string
+}
+
+var configuration = Configuration{
+ Listen: "127.0.0.1:49281",
+ Vcp: "10"}
+
+type Brightness struct {
+ Lock bool
+ Value int
+ ToggledValue int
+}
+
+var currentBrightness Brightness
+
+func update() {
+ if currentBrightness.Lock == false {
+ currentBrightness.Lock = true
+ cmd := exec.Command("ddcutil", "getvcp", "--brief", configuration.Vcp)
+ log.Println(cmd.String())
+ out, err := cmd.CombinedOutput()
+ if err != nil {
+ log.Println(err)
+ return
+ }
+ outString := strings.Split(strings.Trim(string(out), "\n"), " ")
+ currentBrightness.Value, err = strconv.Atoi(outString[len(outString)-2])
+ if err != nil {
+ log.Println(err)
+ return
+ }
+ log.Println("Current brightness is", currentBrightness.Value)
+ currentBrightness.Lock = false
+ } else {
+ log.Println("Active lock, no operation performed")
+ return
+ }
+}
+
+func get(w http.ResponseWriter, r *http.Request) {
+ if r.Method == "GET" {
+ log.Println("Current brightness cached value is",
+ currentBrightness.Value)
+ }
+}
+
+func ddcutilSetBrightness(brightness Brightness) {
+ if currentBrightness.Lock == false {
+ currentBrightness.Lock = true
+ cmd := exec.Command("ddcutil", "setvcp", configuration.Vcp,
+ strconv.Itoa(brightness.Value))
+ log.Println(cmd.String())
+ _, err := cmd.CombinedOutput()
+ if err != nil {
+ log.Println(err)
+ return
+ }
+ currentBrightness.Value = brightness.Value
+ currentBrightness.Lock = false
+ } 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)
+ if err != nil {
+ log.Println(err)
+ return
+ }
+ var brightness Brightness
+ json.Unmarshal([]byte(body), &brightness)
+ ddcutilSetBrightness(brightness)
+ }
+}
+
+func decrease(w http.ResponseWriter, r *http.Request) {
+ if r.Method == "POST" {
+ body, err := io.ReadAll(r.Body)
+ if err != nil {
+ log.Println(err)
+ return
+ }
+ var brightness Brightness
+ json.Unmarshal([]byte(body), &brightness)
+ newValue := Brightness{Value: currentBrightness.Value - brightness.Value}
+ if newValue.Value < 0 {
+ log.Println("Brightness is minimum")
+ } else {
+ ddcutilSetBrightness(newValue)
+ log.Println("Current brightness is", currentBrightness.Value)
+ }
+ } else {
+ log.Println("Active lock, no operation performed")
+ return
+ }
+}
+
+func increase(w http.ResponseWriter, r *http.Request) {
+ if r.Method == "POST" {
+ body, err := io.ReadAll(r.Body)
+ if err != nil {
+ log.Println(err)
+ return
+ }
+ var brightness Brightness
+ json.Unmarshal([]byte(body), &brightness)
+ newValue := Brightness{Value: currentBrightness.Value + brightness.Value}
+ if newValue.Value > 100 {
+ log.Println("Brightness is maximum")
+ } else {
+ ddcutilSetBrightness(newValue)
+ log.Println("Current brightness is", currentBrightness.Value)
+ }
+ } else {
+ log.Println("Active lock, no operation performed")
+ return
+ }
+}
+
+func toggle(w http.ResponseWriter, r *http.Request) {
+ if r.Method == "POST" {
+ if currentBrightness.Value == 0 {
+ ddcutilSetBrightness(Brightness{Value: currentBrightness.ToggledValue})
+ log.Println("Current brightness is", currentBrightness.Value)
+ } else {
+ currentBrightness.ToggledValue = currentBrightness.Value
+ ddcutilSetBrightness(Brightness{Value: 0})
+ log.Println("Current brightness is", currentBrightness.Value)
+ }
+ }
+}
+
+func main() {
+ jsonFile, err := os.Open("ddcutil-daemon.json")
+ if err != nil {
+ log.Fatalln(err)
+ }
+ byteValue, err := ioutil.ReadAll(jsonFile)
+ if err != nil {
+ log.Fatalln(err)
+ }
+ var configuration Configuration
+ json.Unmarshal(byteValue, &configuration)
+ jsonFile.Close()
+
+ currentBrightness.Lock = false
+ update()
+ http.HandleFunc("/update", func(w http.ResponseWriter, r *http.Request) {
+ update()
+ })
+ http.HandleFunc("/get", get)
+ http.HandleFunc("/set", set)
+ http.HandleFunc("/decrease", decrease)
+ http.HandleFunc("/increase", increase)
+ http.HandleFunc("/toggle", toggle)
+ log.Fatal(http.ListenAndServe(configuration.Listen, nil))
+}