/
main.go
141 lines (118 loc) · 3.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"fmt"
"golang.org/x/sys/windows"
"io"
"log"
"os"
"syscall"
"time"
"unsafe"
)
const LogFormat = "20060102"
//const IdleWaitingTime = 5 * 1000 // 秒
const IdleWaitingTime = 5 * 60 * 1000 // 5min
var (
mod = windows.NewLazyDLL("user32.dll")
procGetWindowText = mod.NewProc("GetWindowTextW")
procGetWindowTextLength = mod.NewProc("GetWindowTextLengthW")
user32 = syscall.MustLoadDLL("user32.dll")
kernel32 = syscall.MustLoadDLL("kernel32.dll")
getLastInputInfo = user32.MustFindProc("GetLastInputInfo")
getTickCount = kernel32.MustFindProc("GetTickCount")
lastInputInfo struct {
cbSize uint32
dwTime uint32
}
)
type (
HANDLE uintptr
HWND HANDLE
)
func GetWindowTextLength(hwnd HWND) int {
ret, _, _ := procGetWindowTextLength.Call(
uintptr(hwnd))
return int(ret)
}
func GetWindowText(hwnd HWND) string {
textLen := GetWindowTextLength(hwnd) + 1
buf := make([]uint16, textLen)
procGetWindowText.Call(
uintptr(hwnd),
uintptr(unsafe.Pointer(&buf[0])),
uintptr(textLen))
return syscall.UTF16ToString(buf)
}
func getWindow(funcName string) uintptr {
proc := mod.NewProc(funcName)
hwnd, _, _ := proc.Call()
return hwnd
}
func getLogfile(fileName string) *os.File {
logfile, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
panic("cannot open:" + err.Error())
}
log.SetOutput(io.MultiWriter(logfile, os.Stdout))
log.SetFlags(log.Ldate | log.Ltime)
return logfile
}
func getLogFileName() string {
return "./" + time.Now().Format(LogFormat) + "_gawl.log"
}
func openLogfile(fileName *string, logfile *os.File) {
tmp := getLogFileName()
if *fileName != tmp {
// 日付が変わったら新しいログに書き込む
logfile.Close()
*fileName = tmp
logfile = getLogfile(tmp)
}
}
func getIdleTime() uint32 {
lastInputInfo.cbSize = uint32(unsafe.Sizeof(lastInputInfo))
currentTickCount, _, _ := getTickCount.Call()
r1, _, err := getLastInputInfo.Call(uintptr(unsafe.Pointer(&lastInputInfo)))
if r1 == 0 {
panic("error getting last input info: " + err.Error())
}
return uint32(currentTickCount) - lastInputInfo.dwTime
}
func ticker() error {
t := time.NewTicker(1 * time.Second)
fileName := getLogFileName()
logfile := getLogfile(fileName)
defer t.Stop()
defer logfile.Close()
prevText := ""
var counter uint32
for {
select {
case <-t.C:
idleTime := getIdleTime()
if idleTime < IdleWaitingTime {
if hwnd := getWindow("GetForegroundWindow"); hwnd != 0 {
text := GetWindowText(HWND(hwnd))
if prevText != text || counter > 1 {
//fmt.Println("v:", v, "window :", text, "# hwnd:", hwnd)
openLogfile(&fileName, logfile)
log.Println(",", text)
prevText = text
}
}
counter = 1
} else {
if idleTime > (IdleWaitingTime * counter) {
openLogfile(&fileName, logfile)
log.Println(", Idle Time:", time.Duration(idleTime)*time.Millisecond)
counter = counter + 1
}
}
}
}
}
func main() {
if err := ticker(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
}
}