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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
build
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Expand Down
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
out=build
flags=-ldflags "-linkmode external -extldflags -static" -a
default_recipe: build

.PHONY:build
build:
mkdir -p $(out)
go build $(flags) -o $(out)/autoplank main.go

.PHONY:install
install: build
sudo cp $(out)/autoplank /usr/local/bin/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ autoplank
Requires Go 1.8 or newer.

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

### [Optional] Create a service
Expand Down
137 changes: 58 additions & 79 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,40 @@ import (
"os/exec"
"strconv"
"strings"
"sync"
"syscall"
"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()
_, err := startPlank()
if err != nil {
log.Fatal(err.Error())
}
eventLoop()
}

var (
versonFlag bool
interval = 2
versionFlag bool
interval = 1
)

func init() {

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

flag.Parse()

if versonFlag {
if versionFlag {
fmt.Println("autoplank v" + version)
os.Exit(0)
}
Expand All @@ -59,7 +69,37 @@ 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 := 100
return y < d.offset.y+d.axis.y && y > d.offset.y+d.axis.y-yOffset
}

func startPlank() (*os.Process, error) {
// 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 cred = &syscall.Credential{Gid: uint32(os.Getuid()), Uid: uint32(os.Getgid()), Groups: []uint32{}, NoSetGroups: true}
var sysproc = &syscall.SysProcAttr{Credential: cred, Noctty: true}
var attr = os.ProcAttr{
Dir: ".",
Env: os.Environ(),
Files: []*os.File{
os.Stdin,
os.Stdout,
os.Stderr,
},
Sys: sysproc,
}
proc, err := os.StartProcess(plank, []string{}, &attr)
if err != nil {
return nil, err
}
err = proc.Release()
if err != nil {
return nil, err
}
return proc, nil
}

func pollMouse() <-chan axis {
Expand Down Expand Up @@ -101,68 +141,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 +203,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()
_, err := startPlank()
return err
}
return err

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

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

func validateDeps() {
Expand All @@ -252,16 +238,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