From de4a8882d978a7e2016a0a41eeaba032dfc82e31 Mon Sep 17 00:00:00 2001 From: Prashant Gupta Date: Wed, 20 Mar 2019 12:44:51 -0700 Subject: [PATCH] incorporating new activity tracker changes --- cmd/main.go | 17 +++--- glide.lock | 12 ++-- pkg/mousemover/misc.go | 109 ++++++++++++++++++++++++++++++++++ pkg/mousemover/mouseMover.go | 85 ++++++++++++++++++++++++++ {src => pkg}/notify/notify.go | 0 src/mousemover/misc.go | 109 ---------------------------------- src/mousemover/mouseMover.go | 44 -------------- 7 files changed, 207 insertions(+), 169 deletions(-) create mode 100644 pkg/mousemover/misc.go create mode 100644 pkg/mousemover/mouseMover.go rename {src => pkg}/notify/notify.go (100%) delete mode 100644 src/mousemover/misc.go delete mode 100644 src/mousemover/mouseMover.go diff --git a/cmd/main.go b/cmd/main.go index 7e99898..d284409 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -5,7 +5,7 @@ import ( "github.com/getlantern/systray" "github.com/getlantern/systray/example/icon" - "github.com/prashantgupta24/automatic-mouse-mover/src/mousemover" + "github.com/prashantgupta24/automatic-mouse-mover/pkg/mousemover" ) func main() { @@ -17,29 +17,26 @@ func onReady() { systray.SetIcon(icon.Data) systray.SetTitle("AMM") ammStart := systray.AddMenuItem("Start", "start the app") - ammPause := systray.AddMenuItem("Pause", "pause the app") + ammStop := systray.AddMenuItem("Stop", "stop 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{} + mouseMover := mousemover.GetInstance() for { select { case <-ammStart.ClickedCh: fmt.Println("starting the app") - quit = mousemover.Start() + mouseMover.Start() //notify.SendMessage("starting the app") - case <-ammPause.ClickedCh: + case <-ammStop.ClickedCh: fmt.Println("pausing the app") - if quit != nil { - quit <- struct{}{} - } else { - fmt.Println("app is not started") - } + mouseMover.Quit() case <-mQuit.ClickedCh: fmt.Println("Requesting quit") + mouseMover.Quit() systray.Quit() fmt.Println("Finished quitting") return diff --git a/glide.lock b/glide.lock index 6c0b9b3..6ed542d 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ hash: 0e9d554853b2c3ed23ba323715540d961c06465a1a125959a698d6162e17f91d -updated: 2019-03-13T16:48:18.629713-07:00 +updated: 2019-03-20T12:29:04.478748-07:00 imports: - name: github.com/BurntSushi/xgb version: 27f122750802c950b2c869a5b63dafcf590ced95 @@ -44,18 +44,18 @@ imports: - name: github.com/lxn/win version: 0040366d745006bbf7528ac96de2de375846aa79 - name: github.com/otiai10/gosseract - version: ca64adaa320dc563d0966d58d3811512cb3362dc + version: d442e510d0b37ec1f332a61a04e323ecee91d0cb - name: github.com/oxtoacart/bpool version: 8c4636f812cc8920c26e2002b988c878b1fd6f5e - name: github.com/prashantgupta24/activity-tracker - version: d567f73735304c333bba0a151cf4aca8602c1ae2 + version: 7330e5e8a948db11beb7aef23996d37ef26977f4 subpackages: - src/activity - src/mouse - name: github.com/robotn/gohook - version: bbbbefb5ff8541a05870596ea081ad8581b5879f + version: a7a38946710e764b00839160e13a92b09e40e96a - name: github.com/shirou/gopsutil - version: ebc97eefea9b062f9f1624c042c98f196fc90248 + version: 381f7cc0bf4ccb60157fd5c6eaabfdfc681e6c2f subpackages: - cpu - host @@ -74,7 +74,7 @@ imports: subpackages: - bmp - name: golang.org/x/sys - version: fead79001313d15903fb4605b4a1b781532cd93e + version: 6c81ef8f67ca3f42fc9cd71dfbd5f35b0c4b5771 subpackages: - unix - windows diff --git a/pkg/mousemover/misc.go b/pkg/mousemover/misc.go new file mode 100644 index 0000000..0f44004 --- /dev/null +++ b/pkg/mousemover/misc.go @@ -0,0 +1,109 @@ +package mousemover + +// import ( +// "fmt" +// "time" + +// "github.com/go-vgo/robotgo" +// ) + +// // func isPointerIdle(comm chan bool) { +// // for { +// // x1, y1 := robotgo.GetMousePos() +// // time.Sleep(time.Second * 3) +// // x2, y2 := robotgo.GetMousePos() + +// // if x1 == x2 && y1 == y2 { +// // fmt.Println("idle") +// // //comm <- true +// // } else { +// // fmt.Println("moving") +// // comm <- false +// // } +// // } +// // } + +// func checkIfMouseMoved(x1, y1, x2, y2 int, comm chan bool) { +// if x1 == x2 && y1 == y2 { +// fmt.Println("idle") +// //return false +// //comm <- true +// } else { +// fmt.Println("moving") +// comm <- false +// //return true +// } +// } + +// func isMouseClick(comm chan bool) { + +// } + +// func moveMouse(comm chan bool) { +// ticker := time.NewTicker(time.Second * 3) +// val := true +// movePixel := 10 +// x1, y1 := robotgo.GetMousePos() +// for { +// select { +// case <-ticker.C: +// fmt.Println("ticked") +// x2, y2 := robotgo.GetMousePos() +// checkIfMouseMoved(x1, y1, x2, y2, comm) +// if val { +// fmt.Println("moving mouse because idle") +// //x1, y1 := robotgo.GetMousePos() +// robotgo.Move(x2+movePixel, y2+movePixel) +// movePixel *= -1 +// } else { +// val = true +// } +// x1 = x2 +// y1 = y2 +// case val = <-comm: +// fmt.Println("val received: ", val) +// } +// } +// } +// func main() { +// // log.SetOutput(os.Stdout) +// // log.Println("starting") +// // log.Println("logging") +// // //robotgo.ScrollMouse(100, "up") +// // robotgo.Move(100, 100) +// // x, y := robotgo.GetMousePos() +// // fmt.Println("pos: ", x, y) + +// comm := make(chan bool) +// moveMouse(comm) +// //isPointerIdle(comm) + +// // for { +// // // wheelDown := robotgo.AddEvent("wheelDown") +// // // wheelRight := robotgo.AddEvent("wheelRight") + +// // // fmt.Println("wheelDown : ", wheelDown) +// // // fmt.Println("wheelRight : ", wheelRight) +// // count := 0 +// // go func(count *int) { +// // for { +// // mleft := robotgo.AddEvent("mleft") +// // if mleft == 0 { +// // *count++ +// // fmt.Println("mleft : ", *count) +// // time.Sleep(time.Millisecond * 500) +// // } +// // } + +// // }(&count) + +// // mright := robotgo.AddEvent("mright") + +// // fmt.Println("mright : ", mright) + +// // // if mleft { +// // // fmt.Println("you press... ", "mouse left button") +// // // } +// // } + +// } diff --git a/pkg/mousemover/mouseMover.go b/pkg/mousemover/mouseMover.go new file mode 100644 index 0000000..7d81172 --- /dev/null +++ b/pkg/mousemover/mouseMover.go @@ -0,0 +1,85 @@ +package mousemover + +import ( + "fmt" + "log" + "time" + + "github.com/go-vgo/robotgo" + "github.com/prashantgupta24/activity-tracker/pkg/tracker" +) + +var instance *mouseMover + +type mouseMover struct { + quit chan struct{} + isRunning bool +} + +const ( + timeout = 100 //ms +) + +func (m *mouseMover) Start() { + m.quit = make(chan struct{}) + + frequency := 5 //value always in seconds + activityTracker := &tracker.Instance{ + Frequency: frequency, + } + + heartbeatCh := activityTracker.Start() + + go func(m *mouseMover) { + m.isRunning = true + movePixel := 10 + for { + select { + case heartbeat := <-heartbeatCh: + if !heartbeat.IsActivity { + commCh := make(chan bool) + go moveMouse(movePixel, commCh) + select { + case wasMouseMoveSuccess := <-commCh: + if wasMouseMoveSuccess { + fmt.Println("moving mouse at : \n", time.Now()) + movePixel *= -1 + } + case <-time.After(timeout * time.Millisecond): + //timeout, do nothing + log.Printf("timeout happened after %vms while trying to move mouse", timeout) + } + + } + case <-m.quit: + fmt.Println("stopping mouse mover") + m.isRunning = false + activityTracker.Quit() + return + } + } + }(m) +} + +func moveMouse(movePixel int, commCh chan bool) { + currentX, currentY := robotgo.GetMousePos() + moveToX := currentX + movePixel + moveToY := currentY + movePixel + robotgo.MoveMouse(moveToX, moveToY) + commCh <- true +} + +func (m *mouseMover) Quit() { + //making it idempotent + if !m.isRunning { + m.quit <- struct{}{} + } +} + +//GetInstance gets the singleton instance for mouse mover app +func GetInstance() *mouseMover { + if instance == nil { + instance = &mouseMover{} + } + return instance +} diff --git a/src/notify/notify.go b/pkg/notify/notify.go similarity index 100% rename from src/notify/notify.go rename to pkg/notify/notify.go diff --git a/src/mousemover/misc.go b/src/mousemover/misc.go deleted file mode 100644 index 3d0cace..0000000 --- a/src/mousemover/misc.go +++ /dev/null @@ -1,109 +0,0 @@ -package mousemover - -import ( - "fmt" - "time" - - "github.com/go-vgo/robotgo" -) - -// func isPointerIdle(comm chan bool) { -// for { -// x1, y1 := robotgo.GetMousePos() -// time.Sleep(time.Second * 3) -// x2, y2 := robotgo.GetMousePos() - -// if x1 == x2 && y1 == y2 { -// fmt.Println("idle") -// //comm <- true -// } else { -// fmt.Println("moving") -// comm <- false -// } -// } -// } - -func checkIfMouseMoved(x1, y1, x2, y2 int, comm chan bool) { - if x1 == x2 && y1 == y2 { - fmt.Println("idle") - //return false - //comm <- true - } else { - fmt.Println("moving") - comm <- false - //return true - } -} - -func isMouseClick(comm chan bool) { - -} - -func moveMouse(comm chan bool) { - ticker := time.NewTicker(time.Second * 3) - val := true - movePixel := 10 - x1, y1 := robotgo.GetMousePos() - for { - select { - case <-ticker.C: - fmt.Println("ticked") - x2, y2 := robotgo.GetMousePos() - checkIfMouseMoved(x1, y1, x2, y2, comm) - if val { - fmt.Println("moving mouse because idle") - //x1, y1 := robotgo.GetMousePos() - robotgo.Move(x2+movePixel, y2+movePixel) - movePixel *= -1 - } else { - val = true - } - x1 = x2 - y1 = y2 - case val = <-comm: - fmt.Println("val received: ", val) - } - } -} -func main() { - // log.SetOutput(os.Stdout) - // log.Println("starting") - // log.Println("logging") - // //robotgo.ScrollMouse(100, "up") - // robotgo.Move(100, 100) - // x, y := robotgo.GetMousePos() - // fmt.Println("pos: ", x, y) - - comm := make(chan bool) - moveMouse(comm) - //isPointerIdle(comm) - - // for { - // // wheelDown := robotgo.AddEvent("wheelDown") - // // wheelRight := robotgo.AddEvent("wheelRight") - - // // fmt.Println("wheelDown : ", wheelDown) - // // fmt.Println("wheelRight : ", wheelRight) - // count := 0 - // go func(count *int) { - // for { - // mleft := robotgo.AddEvent("mleft") - // if mleft == 0 { - // *count++ - // fmt.Println("mleft : ", *count) - // time.Sleep(time.Millisecond * 500) - // } - // } - - // }(&count) - - // mright := robotgo.AddEvent("mright") - - // fmt.Println("mright : ", mright) - - // // if mleft { - // // fmt.Println("you press... ", "mouse left button") - // // } - // } - -} diff --git a/src/mousemover/mouseMover.go b/src/mousemover/mouseMover.go deleted file mode 100644 index 553fba5..0000000 --- a/src/mousemover/mouseMover.go +++ /dev/null @@ -1,44 +0,0 @@ -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: 15, - } - - 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.MoveMouse(nextMouseMov.MouseX, nextMouseMov.MouseY) - movePixel *= -1 - } - case <-quit: - fmt.Println("stopping mouse mover") - quitActivityTracker <- struct{}{} - return - } - } - }() - return quit -}