Skip to content

Commit

Permalink
Readme file and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lpicanco committed Jun 11, 2019
1 parent 4c4cab3 commit f9c8df6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,50 @@
# i3-autodisplay
i3wm display auto-configuration
i3-autodisplay is a i3wm display auto-configuration

## Installation

### Pre-requisites
[xrandr](https://www.x.org/archive/current/doc/man/man1/xrandr.1.xhtml) program

### Pre built binary
Fetch the [latest release](https://github.com/lpicanco/i3-autodisplay/releases).

### From sources

```bash
go get github.com/lpicanco/i3-autodisplay
cd $GOPATH/src/github.com/lpicanco/i3-autodisplay
go build
```

## Usage
`i3-autodisplay` requires a configuration file to work. The configuration file can be loaded from these locations:

* `$XDG_HOME/i3-autodisplay/config.yml`
* `$HOME/.config/i3-autodisplay/config.yml`
* Specified via `-config` parameter

In your i3wm configuration add the following line:

```
exec --no-startup-id <path to i3-autodisplay>
```

Usage via command line:
```bash
./i3-autodisplay -config sample_config.yml
```

Sample configuration file:
```yaml
displays:
- name: eDP1
workspaces: [1,2,3,4,5,6,7,8,9,0]
- name: HDMI1
workspaces: [2,4,6,8]
randr_extra_options: "--left-of eDP1"
- name: DP1
workspaces: [1,3,5,7,9]
randr_extra_options: "--left-of HDMI1"
```
23 changes: 12 additions & 11 deletions display/randr.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package display

import (
"github.com/lpicanco/i3-autodisplay/i3"
"log"
"os/exec"
"reflect"
"strings"

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

"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/randr"
"github.com/BurntSushi/xgb/xproto"
Expand Down Expand Up @@ -38,12 +39,22 @@ func Refresh() {
return
}

currentWorkspace, err := i3.GetCurrentWorkspaceNumber()
if err != nil {
log.Fatal(err)
}

for _, display := range config.Config.Displays {
if currentOutputConfiguration[display.Name] {
refreshDisplay(display)
}
}

err = i3.SetCurrentWorkspace(currentWorkspace)
if err != nil {
log.Fatal(err)
}

lastOutputConfiguration = currentOutputConfiguration
}

Expand Down Expand Up @@ -72,11 +83,6 @@ func ListenEvents() {
}

func refreshDisplay(display config.Display) {
currentWorkspace, err := i3.GetCurrentWorkspaceNumber()
if err != nil {
log.Fatal(err)
}

args := []string{"--output", display.Name, "--auto"}
if display.RandrExtraOptions != "" {
args = append(args, strings.Split(display.RandrExtraOptions, " ")...)
Expand All @@ -93,11 +99,6 @@ func refreshDisplay(display config.Display) {
if err != nil {
log.Fatalf("Error updating i3 workspaces: %s\n", err)
}

err = i3.SetCurrentWorkspace(currentWorkspace)
if err != nil {
log.Fatal(err)
}
}

func getOutputConfiguration() map[string]bool {
Expand Down
3 changes: 1 addition & 2 deletions i3/i3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package i3
import (
"errors"
"fmt"

"github.com/lpicanco/i3-autodisplay/config"
"go.i3wm.org/i3"
"log"
)

func GetCurrentWorkspaceNumber() (int64, error) {
Expand Down Expand Up @@ -33,7 +33,6 @@ func UpdateWorkspaces(display config.Display) error {
for _, workspace := range display.Workspaces {

command := fmt.Sprintf("workspace %d; move workspace to %s", workspace, display.Name)
log.Println(command)
_, err := i3.RunCommand(command)

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions sample_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ displays:
- name: eDP1
workspaces: [1,2,3,4,5,6,7,8,9,0]
- name: HDMI1
workspaces: [1,3,5,7,9]
workspaces: [2,4,6,8]
randr_extra_options: "--left-of eDP1"
- name: DP1
workspaces: [2,4,6,8]
workspaces: [1,3,5,7,9]
randr_extra_options: "--left-of HDMI1"

0 comments on commit f9c8df6

Please sign in to comment.