Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for turning off monitor #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,12 @@ displays:
randr_extra_options: "--left-of HDMI1"
```

If you'd like to have your laptop monitor turned off when an external one is connected, here's another example ('eDP-1' being the laptop screen):
```yaml
displays:
- name: eDP-1
workspaces: [1,2,3,4,5,6,7,8,9,0]
turn_off_when: ["DP-1"]
- name: DP-1
workspaces: [1,2,3,4,5,6,7,8,9,0]
```
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (

type Display struct {
Name string
RandrExtraOptions string `yaml:"randr_extra_options"`
RandrExtraOptions string `yaml:"randr_extra_options"`
TurnOffWhen []string `yaml:"turn_off_when"`
Workspaces []int
}

Expand Down
39 changes: 32 additions & 7 deletions display/randr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os/exec"
"reflect"
"strings"
"time"

"github.com/lpicanco/i3-autodisplay/i3"

Expand Down Expand Up @@ -46,20 +47,32 @@ func Refresh() {

args := []string{}
for _, display := range config.Config.Displays {
active := currentOutputConfiguration[display.Name]
active := isDisplayActive(display, currentOutputConfiguration)
args = append(args, getDisplayOptions(display, active)...)
}

log.Println("xrandr", args)
cmd := exec.Command("xrandr", args...)
out, err := cmd.CombinedOutput()
// sometimes during suspend/wake up routines xrandr is unable to configure monitors.
// 5 secs of retries should be enough to work it around
retries := 5
retriesLeft := retries

if err != nil {
log.Fatalf("Error executing xrandr: %s\n%s", err, out)
for {
log.Println("xrandr", args)
cmd := exec.Command("xrandr", args...)
out, err := cmd.CombinedOutput()
if err == nil {
break
}
log.Printf("Error executing xrandr: %s\n%s", err, out)
retriesLeft--
if retriesLeft == 0 {
log.Fatalf("unable to execute the xrandr command after %d attempts", retries)
}
time.Sleep(time.Second)
}

for _, display := range config.Config.Displays {
if currentOutputConfiguration[display.Name] {
if isDisplayActive(display, currentOutputConfiguration) {
refreshDisplay(display)
}
}
Expand Down Expand Up @@ -96,6 +109,18 @@ func ListenEvents() {
}
}

func isDisplayActive(display config.Display, currentOutputConfiguration map[string]bool) bool {
if !currentOutputConfiguration[display.Name] {
return false
}
for _, d := range display.TurnOffWhen {
if currentOutputConfiguration[d] {
return false
}
}
return true
}

func getDisplayOptions(display config.Display, active bool) []string {
if active {
args := []string{"--output", display.Name, "--auto"}
Expand Down