Skip to content
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
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
default_recipe: build

build:
go build -o autoplank main.go

install: build
sudo cp autoplank /usr/local/bin/
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ autoplank

Requires Go 1.8 or newer.

Install Go using:

```
sudo apt install git golang-go xdotool
```

Then build:

```
go build -o autoplank && sudo mv autoplank /usr/local/bin
make install
```

### [Optional] Create a service
Expand Down
118 changes: 40 additions & 78 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,46 @@ import (
"os/exec"
"strconv"
"strings"
"sync"
"time"
)

var version = "0.1-untracked-dev"
var version = "0.1.1-untracked-dev"
var displaysFound []display

func main() {
if ds, err := fetchDisplays(); err == nil {
displaysFound = ds
} else {
log.Fatal("unable to gather screen information")
}
validateDeps()
// we set up the process we want to start
// no error handling needed here because validate deps checks for plank command
plank, _ := exec.LookPath("plank")
var attr = os.ProcAttr{Dir: ".", Env: os.Environ(), Files: []*os.File{nil, nil, nil, nil}}
process, err := os.StartProcess(plank, []string{}, &attr)
if err != nil {
fmt.Printf(err.Error())
} else {
err = process.Release()
if err != nil {
fmt.Printf(err.Error())
}
}

exec.Command("plank")
eventLoop()
}

var (
versonFlag bool
interval = 2
interval = 200
)

func init() {

flag.BoolVar(&versonFlag, "v", versonFlag, "show version")
flag.IntVar(&interval, "interval", interval, "mouse poll interval in secs")
flag.IntVar(&interval, "interval", interval, "mouse poll interval in millisecs")

flag.Parse()

Expand Down Expand Up @@ -59,14 +79,17 @@ func (d display) Within(x, y int) bool {
}

func (d display) IsBottom(y int) bool {
return y < d.offset.y+d.axis.y && y > d.offset.y+d.axis.y-20
// if the cursor is this low on the screen user is going to use plank
// we start the moving procedure
yOffset := 1080
return y < d.offset.y+d.axis.y && y > d.offset.y+d.axis.y-yOffset
}

func pollMouse() <-chan axis {
aChan := make(chan axis)

go func() {
for range time.Tick(time.Second * time.Duration(interval)) {
for range time.Tick(time.Millisecond * time.Duration(interval)) {
pos, err := getMouseLocation()
if err != nil {
log.Println(err)
Expand Down Expand Up @@ -101,68 +124,12 @@ func getMouseLocation() (a axis, err error) {
return a, nil
}

var dLock sync.RWMutex
var displaysFound []display

func watchDisplays() {
var err error
displaysFound, err = fetchDisplays()
if err != nil {
log.Println(err)
}

for range time.Tick(time.Second * 5) {
dLock.Lock()
displaysFound, err = fetchDisplays()
if err != nil {
log.Println(err)
}
dLock.Unlock()
}
}

func getDisplays(lastUpdate time.Time) ([]display, bool) {
dLock.RLock()
defer dLock.RUnlock()

if !displaysUpdateTime.After(lastUpdate) {
return nil, false
}

if len(displaysFound) == 0 {
// this is rare and should never happen
// may be a one off and can be fixed at the next
// poll.
// let's simply log
log.Println("Error: no displays are found")
}

// create a copy to not worry about
// race conditions outside this
displaysCopy := make([]display, len(displaysFound))
copy(displaysCopy, displaysFound)

return displaysCopy, true
}

// keep track of previous displays state
var (
displaysConf string
displaysUpdateTime time.Time
)

func fetchDisplays() ([]display, error) {
cmd := exec.Command("xrandr")
out, err := cmd.Output()
if err != nil {
return nil, err
}
if string(out) == displaysConf {
// ignore
return displaysFound, nil
}
displaysConf = string(out)
displaysUpdateTime = time.Now()

var displays []display
scanner := bufio.NewScanner(bytes.NewReader(out))
Expand Down Expand Up @@ -219,22 +186,24 @@ func movePlankTo(d display) error {
return nil
}

var buf bytes.Buffer
fmt.Fprint(&buf, "attempting to move plank to "+d.name)
if d.primary {
fmt.Fprintf(&buf, " - primary")
}
err = exec.Command("dconf", "write", dconfPlank, value).
Run()

log.Println(buf.String())
if err == nil {
fmt.Printf("attempting to move plank to %s\n", d.name)
_ = exec.Command("killall", "plank").Run()
return exec.Command("plank").Start()
} else {
return err
}

return exec.Command("dconf", "write", dconfPlank, value).
Run()
}

var requiredCommands = []string{
"xrandr",
"xdotool",
"dconf",
"plank",
}

func validateDeps() {
Expand All @@ -252,16 +221,9 @@ func validateDeps() {
}

func eventLoop() {
var displays []display
var lastRequestTime time.Time
go watchDisplays()
var pos axis
for pos = range pollMouse() {
if ds, ok := getDisplays(lastRequestTime); ok {
lastRequestTime = time.Now()
displays = ds
}
for _, d := range displays {
for _, d := range displaysFound {
if d.Within(pos.x, pos.y) && d.IsBottom(pos.y) {
err := movePlankTo(d)
if err != nil {
Expand Down