From 0d1828505b2356b370ddab301f24602eb5c9ea87 Mon Sep 17 00:00:00 2001 From: Prashant Gupta Date: Wed, 13 Mar 2019 16:57:03 -0700 Subject: [PATCH] dev work * adding system tray feature * updated glide lock * working prototype * added notify package * refactoring packages * moved stuff out * adding lock file * changing activity function * moving stuff around * refactoring --- cmd/main.go | 56 +++++++++++++++++ glide.lock | 39 ++++++++++-- glide.yaml | 13 +++- src/mouseMover.go | 99 ------------------------------- misc.go => src/mousemover/misc.go | 2 +- src/mousemover/mouseMover.go | 44 ++++++++++++++ src/notify/notify.go | 16 +++++ 7 files changed, 161 insertions(+), 108 deletions(-) create mode 100644 cmd/main.go delete mode 100644 src/mouseMover.go rename misc.go => src/mousemover/misc.go (99%) create mode 100644 src/mousemover/mouseMover.go create mode 100644 src/notify/notify.go diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..7e99898 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,56 @@ +package main + +import ( + "fmt" + + "github.com/getlantern/systray" + "github.com/getlantern/systray/example/icon" + "github.com/prashantgupta24/automatic-mouse-mover/src/mousemover" +) + +func main() { + systray.Run(onReady, onExit) +} + +func onReady() { + go func() { + systray.SetIcon(icon.Data) + systray.SetTitle("AMM") + ammStart := systray.AddMenuItem("Start", "start the app") + ammPause := systray.AddMenuItem("Pause", "pause the app") + systray.AddSeparator() + mQuit := systray.AddMenuItem("Quit", "Quit the whole app") + // Sets the icon of a menu item. Only available on Mac. + //mQuit.SetIcon(icon.Data) + var quit chan struct{} + for { + select { + case <-ammStart.ClickedCh: + fmt.Println("starting the app") + quit = mousemover.Start() + //notify.SendMessage("starting the app") + + case <-ammPause.ClickedCh: + fmt.Println("pausing the app") + if quit != nil { + quit <- struct{}{} + } else { + fmt.Println("app is not started") + } + + case <-mQuit.ClickedCh: + fmt.Println("Requesting quit") + systray.Quit() + fmt.Println("Finished quitting") + return + } + } + + }() +} + +func onExit() { + // clean up here + fmt.Println("exiting") + +} diff --git a/glide.lock b/glide.lock index 559b9bd..6c0b9b3 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: 2f203324a3ca8f6d3a3e571dc0393f79835b0ea6194850ddc6b03e9355dfddcb -updated: 2019-03-07T18:33:39.501851-08:00 +hash: 0e9d554853b2c3ed23ba323715540d961c06465a1a125959a698d6162e17f91d +updated: 2019-03-13T16:48:18.629713-07:00 imports: - name: github.com/BurntSushi/xgb version: 27f122750802c950b2c869a5b63dafcf590ced95 @@ -13,22 +13,49 @@ imports: - ewmh - xevent - xprop +- name: github.com/deckarep/gosx-notifier + version: e127226297fb751aa3b582db5e92361fcbfc5a6c +- name: github.com/getlantern/context + version: c447772a6520c1a3ae6294a7df725ee08230f14e +- name: github.com/getlantern/errors + version: e24b7f4ff7c70be59bbefca6b7695d68cda8b399 +- name: github.com/getlantern/golog + version: cca714f7feb5df8e455f409b549d384441ac4578 +- name: github.com/getlantern/hex + version: 083fba3033ad473db3dd31c9bb368473d37581a7 +- name: github.com/getlantern/hidden + version: d52a649ab33af200943bb599898dbdcfdbc94cb7 +- name: github.com/getlantern/ops + version: 37353306c90844c8e0591956f56611f46299d202 +- name: github.com/getlantern/systray + version: 26d5b920200dbc1869c4bfde4571497082f83caa + subpackages: + - example/icon - name: github.com/go-ole/go-ole version: 97b6244175ae18ea6eef668034fd6565847501c9 subpackages: - oleutil +- name: github.com/go-stack/stack + version: 2fee6af1a9795aafbe0253a0cfbdf668e1fb8a9a - name: github.com/go-vgo/robotgo - version: 7e017b4cb4d39e6af9cad664c075739fe9cc76fd + version: 193b4935d4b146c729d763c2c59a0d3e7f3cee8a subpackages: - clipboard - name: github.com/lxn/win - version: e90c084872a829e2193ded9dbdb9a0180ddcfac0 + version: 0040366d745006bbf7528ac96de2de375846aa79 - name: github.com/otiai10/gosseract version: ca64adaa320dc563d0966d58d3811512cb3362dc +- name: github.com/oxtoacart/bpool + version: 8c4636f812cc8920c26e2002b988c878b1fd6f5e +- name: github.com/prashantgupta24/activity-tracker + version: d567f73735304c333bba0a151cf4aca8602c1ae2 + subpackages: + - src/activity + - src/mouse - name: github.com/robotn/gohook version: bbbbefb5ff8541a05870596ea081ad8581b5879f - name: github.com/shirou/gopsutil - version: 27ec6a0789cefd42d1aa3ea7455b48fce7a401db + version: ebc97eefea9b062f9f1624c042c98f196fc90248 subpackages: - cpu - host @@ -47,7 +74,7 @@ imports: subpackages: - bmp - name: golang.org/x/sys - version: 1c9583448a9c3aa0f9a6a5241bf73c0bd8aafded + version: fead79001313d15903fb4605b4a1b781532cd93e subpackages: - unix - windows diff --git a/glide.yaml b/glide.yaml index 06e0d96..6a9a345 100644 --- a/glide.yaml +++ b/glide.yaml @@ -1,4 +1,13 @@ -package: . +package: github.com/prashantgupta24/automatic-mouse-mover import: +- package: github.com/deckarep/gosx-notifier +- package: github.com/getlantern/systray + subpackages: + - example/icon - package: github.com/go-vgo/robotgo - version: ~0.70.0 + version: ^0.80.0 +- package: github.com/prashantgupta24/activity-tracker + version: ^0.0.1 + subpackages: + - src/activity + - src/mouse diff --git a/src/mouseMover.go b/src/mouseMover.go deleted file mode 100644 index 103253e..0000000 --- a/src/mouseMover.go +++ /dev/null @@ -1,99 +0,0 @@ -package main - -import ( - "fmt" - "time" - - "github.com/go-vgo/robotgo" -) - -const ( - timeToCheck = 3 -) - -type mousePos struct { - mouseX int - mouseY int -} - -func moveMouse(comm chan struct{}, quit chan struct{}) { - ticker := time.NewTicker(time.Second * timeToCheck) - isIdle := true - movePixel := 10 - lastMousePos := getMousePos() - for { - select { - case <-ticker.C: - fmt.Println("ticked : ", isIdle) - currentMousePos := getMousePos() - if isIdle && isPointerIdle(currentMousePos, lastMousePos) { - fmt.Println("moving mouse") - nextMouseMov := &mousePos{ - mouseX: currentMousePos.mouseX + movePixel, - mouseY: currentMousePos.mouseY + movePixel, - } - robotgo.Move(nextMouseMov.mouseX, nextMouseMov.mouseY) - lastMousePos = nextMouseMov - } else { - lastMousePos = currentMousePos - } - isIdle = true - movePixel *= -1 - case <-comm: - isIdle = false - //fmt.Println("val received: ", isIdle) - case <-quit: - return - } - } -} - -func isPointerIdle(currentMousePos, lastMousePos *mousePos) bool { - fmt.Println("current : ", currentMousePos) - fmt.Println("last : ", lastMousePos) - - if currentMousePos.mouseX == lastMousePos.mouseX && - currentMousePos.mouseY == lastMousePos.mouseY { - return true - } - return false -} - -func getMousePos() *mousePos { - x, y := robotgo.GetMousePos() - return &mousePos{ - mouseX: x, - mouseY: y, - } -} - -func isMouseClicked(comm chan struct{}) { - go func() { - for { - mleft := robotgo.AddEvent("mleft") - if mleft == 0 { - fmt.Println("mleft clicked") - time.Sleep(100 * time.Millisecond) - comm <- struct{}{} - } - } - }() - - go func() { - for { - mright := robotgo.AddEvent("mright") - if mright == 0 { - fmt.Println("mright clicked") - time.Sleep(100 * time.Millisecond) - comm <- struct{}{} - } - } - }() -} - -func main() { - comm := make(chan struct{}) - quit := make(chan struct{}) - go isMouseClicked(comm) - moveMouse(comm, quit) -} diff --git a/misc.go b/src/mousemover/misc.go similarity index 99% rename from misc.go rename to src/mousemover/misc.go index 8f6f9de..3d0cace 100644 --- a/misc.go +++ b/src/mousemover/misc.go @@ -1,4 +1,4 @@ -package main +package mousemover import ( "fmt" diff --git a/src/mousemover/mouseMover.go b/src/mousemover/mouseMover.go new file mode 100644 index 0000000..78e7c69 --- /dev/null +++ b/src/mousemover/mouseMover.go @@ -0,0 +1,44 @@ +package mousemover + +import ( + "fmt" + "time" + + "github.com/go-vgo/robotgo" + "github.com/prashantgupta24/activity-tracker/src/activity" + "github.com/prashantgupta24/activity-tracker/src/mouse" +) + +func Start() (quit chan struct{}) { + quit = make(chan struct{}) + + activityTracker := &activity.ActivityTracker{ + TimeToCheck: 5, + } + + heartbeatCh, quitActivityTracker := activityTracker.Start() + + go func() { + movePixel := 10 + for { + select { + case heartbeat := <-heartbeatCh: + if !heartbeat.IsActivity { + currentMousePos := mouse.GetPosition() + fmt.Println("moving mouse at : ", time.Now()) + nextMouseMov := &mouse.Position{ + MouseX: currentMousePos.MouseX + movePixel, + MouseY: currentMousePos.MouseY + movePixel, + } + robotgo.Move(nextMouseMov.MouseX, nextMouseMov.MouseY) + movePixel *= -1 + } + case <-quit: + fmt.Println("stopping mouse mover") + quitActivityTracker <- struct{}{} + return + } + } + }() + return quit +} diff --git a/src/notify/notify.go b/src/notify/notify.go new file mode 100644 index 0000000..ebe1b28 --- /dev/null +++ b/src/notify/notify.go @@ -0,0 +1,16 @@ +package notify + +import ( + "fmt" + + gosxnotifier "github.com/deckarep/gosx-notifier" +) + +//SendMessage to OSX notification centre +func SendMessage(msg string) { + note := gosxnotifier.NewNotification(msg) + err := note.Push() + if err != nil { + fmt.Println("could not send notification") + } +}