From 1dc84320326c844b97a7362f0bdbb34362cf3597 Mon Sep 17 00:00:00 2001 From: Nikola Tasic Date: Wed, 18 Sep 2019 06:11:13 +0200 Subject: [PATCH 1/2] added a makefile, fixed plank position not updating by restarting it, fixed screen locking when unnecessarily querying xrandr --- Makefile | 7 ++++ README.md | 2 +- main.go | 114 ++++++++++++++++++------------------------------------ 3 files changed, 46 insertions(+), 77 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..93a50d9 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +default_recipe: build + +build: + go build -o autoplank main.go + +install: build + sudo cp 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..57f563e 100644 --- a/main.go +++ b/main.go @@ -11,20 +11,40 @@ 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 = 1 ) func init() { @@ -59,7 +79,10 @@ 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 pollMouse() <-chan axis { @@ -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)) @@ -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() { @@ -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 { From 5321a4b6aa6f0e975a6124d73213f6d43d5ef647 Mon Sep 17 00:00:00 2001 From: Nikola Tasic Date: Thu, 23 Jan 2020 20:00:12 +0100 Subject: [PATCH 2/2] more concise plank process starting, gitgnore, static build makefile recipe --- .gitignore | 2 ++ LICENSE | 2 +- Makefile | 9 +++++++-- main.go | 55 +++++++++++++++++++++++++++++++++++------------------- 4 files changed, 46 insertions(+), 22 deletions(-) create mode 100644 .gitignore 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 index 93a50d9..1fb9911 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,12 @@ +out=build +flags=-ldflags "-linkmode external -extldflags -static" -a default_recipe: build +.PHONY:build build: - go build -o autoplank main.go + mkdir -p $(out) + go build $(flags) -o $(out)/autoplank main.go +.PHONY:install install: build - sudo cp autoplank /usr/local/bin/ \ No newline at end of file + sudo cp $(out)/autoplank /usr/local/bin/ \ No newline at end of file diff --git a/main.go b/main.go index 57f563e..3ffa9ac 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "os/exec" "strconv" "strings" + "syscall" "time" ) @@ -24,37 +25,26 @@ func main() { 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) + _, err := startPlank() if err != nil { - fmt.Printf(err.Error()) - } else { - err = process.Release() - if err != nil { - fmt.Printf(err.Error()) - } + log.Fatal(err.Error()) } - - exec.Command("plank") eventLoop() } var ( - versonFlag bool - interval = 1 + 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) } @@ -85,6 +75,33 @@ func (d display) IsBottom(y int) bool { 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 { aChan := make(chan axis) @@ -192,10 +209,10 @@ func movePlankTo(d display) error { 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 { + _, err := startPlank() return err } + return err }