diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4afcf19 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +build \ No newline at end of file diff --git a/LICENSE b/LICENSE index 52cb23d..b68ff5f 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1fb9911 --- /dev/null +++ b/Makefile @@ -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/ \ No newline at end of file diff --git a/README.md b/README.md index 6a4a407..02bfde7 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/main.go b/main.go index 53ff343..3ffa9ac 100644 --- a/main.go +++ b/main.go @@ -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) } @@ -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 { @@ -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)) @@ -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() { @@ -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 {