Merging master
This commit is contained in:
36
src/main.go
36
src/main.go
@@ -16,12 +16,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
SLEEP_TIME = 3 * time.Second
|
SLEEP_TIME = 3 * time.Second // time between processing xinput output
|
||||||
KEYBOARD_BUFER_SIZE = 10000
|
KEYBOARD_BUFER_SIZE = 10000
|
||||||
DATABASE_NAME = "file:gokeystat.db?cache=shared&mode=rwc"
|
DATABASE_NAME = "file:gokeystat.db?cache=shared&mode=rwc"
|
||||||
CAPTURE_TIME = 5 // time in seconds between capturing keyboard to db
|
CAPTURE_TIME = 5 // time in seconds between capturing keyboard to db
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// StatForTime stotres pressed keys and beginning time
|
||||||
type StatForTime struct {
|
type StatForTime struct {
|
||||||
time int64
|
time int64
|
||||||
keys map[uint8]int
|
keys map[uint8]int
|
||||||
@@ -32,12 +33,12 @@ func (stat *StatForTime) Init() {
|
|||||||
stat.keys = make(map[uint8]int)
|
stat.keys = make(map[uint8]int)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return map from key numbers to key names like "F1", "Tab", "d"
|
// GetKeymap returns map from key numbers to key names like "F1", "Tab", "d"
|
||||||
func GetKeymap() map[uint8]string {
|
func GetKeymap() map[uint8]string {
|
||||||
return GetKeymapFromOutput(GetKeymapOutput())
|
return GetKeymapFromOutput(GetKeymapOutput())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return output of utility that prints system keymap
|
// GetKeymapOutput returns output of utility that prints system keymap
|
||||||
func GetKeymapOutput() []byte {
|
func GetKeymapOutput() []byte {
|
||||||
cmd := exec.Command("xmodmap", "-pke")
|
cmd := exec.Command("xmodmap", "-pke")
|
||||||
out, err := cmd.Output()
|
out, err := cmd.Output()
|
||||||
@@ -47,7 +48,7 @@ func GetKeymapOutput() []byte {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return map with keymap from text
|
// GetKeymapFromOutput returns map with keymap from text
|
||||||
func GetKeymapFromOutput(buf []byte) map[uint8]string {
|
func GetKeymapFromOutput(buf []byte) map[uint8]string {
|
||||||
const KEY_NUM_STRING_RE = "\\d+[ ]*=[ ]*\\S+"
|
const KEY_NUM_STRING_RE = "\\d+[ ]*=[ ]*\\S+"
|
||||||
re := regexp.MustCompile(KEY_NUM_STRING_RE)
|
re := regexp.MustCompile(KEY_NUM_STRING_RE)
|
||||||
@@ -62,7 +63,7 @@ func GetKeymapFromOutput(buf []byte) map[uint8]string {
|
|||||||
return keyMap
|
return keyMap
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract pressed keys from bufer buf
|
// GetKeyNumsFromOutput extract pressed keys from bufer buf
|
||||||
// It returns slice with key numbers in the same order
|
// It returns slice with key numbers in the same order
|
||||||
func GetKeyNumsFromOutput(buf []byte) []uint8 {
|
func GetKeyNumsFromOutput(buf []byte) []uint8 {
|
||||||
const KEY_NUM_STRING_RE = "press[ ]+(\\d+)"
|
const KEY_NUM_STRING_RE = "press[ ]+(\\d+)"
|
||||||
@@ -90,7 +91,7 @@ func GetKeyNumsFromKeyMap(keyMap map[uint8]string) []int {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates tables, inserts keymap to db
|
// InitDb creates tables, inserts keymap to db
|
||||||
func InitDb(db *sql.DB, keyMap map[uint8]string) {
|
func InitDb(db *sql.DB, keyMap map[uint8]string) {
|
||||||
keyNums := GetKeyNumsFromKeyMap(keyMap)
|
keyNums := GetKeyNumsFromKeyMap(keyMap)
|
||||||
|
|
||||||
@@ -172,7 +173,7 @@ func AddStatTimeToDb(db *sql.DB, statTime StatForTime, keyMap map[uint8]string)
|
|||||||
tx.Commit()
|
tx.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns slice with StatForTime objects that
|
// GetStatTimesFromDb returns slice with StatForTime objects that
|
||||||
func GetStatTimesFromDb(db *sql.DB, fromTime int64, keyMap map[uint8]string) []StatForTime {
|
func GetStatTimesFromDb(db *sql.DB, fromTime int64, keyMap map[uint8]string) []StatForTime {
|
||||||
sqlStmt := "select * from keylog where time > " + strconv.FormatInt(fromTime, 10)
|
sqlStmt := "select * from keylog where time > " + strconv.FormatInt(fromTime, 10)
|
||||||
rows, err := db.Query(sqlStmt)
|
rows, err := db.Query(sqlStmt)
|
||||||
@@ -190,7 +191,7 @@ func GetStatTimesFromDb(db *sql.DB, fromTime int64, keyMap map[uint8]string) []S
|
|||||||
result := make([]int64, len(cols))
|
result := make([]int64, len(cols))
|
||||||
|
|
||||||
dest := make([]interface{}, len(cols)) // A temporary interface{} slice
|
dest := make([]interface{}, len(cols)) // A temporary interface{} slice
|
||||||
for i, _ := range rawResult {
|
for i := range rawResult {
|
||||||
dest[i] = &rawResult[i] // Put pointers to each string in the interface slice
|
dest[i] = &rawResult[i] // Put pointers to each string in the interface slice
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,10 +238,11 @@ func GetStatTimesFromDb(db *sql.DB, fromTime int64, keyMap map[uint8]string) []S
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
keyboardId := flag.Int("id", -1, "Your keyboard id")
|
keyboardID := flag.Int("id", -1, "Your keyboard id")
|
||||||
outputPath := flag.String("o", "", "Path to export file")
|
outputPath := flag.String("o", "", "Path to export file")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
log.Println("keyboardId =", *keyboardId, "outputPath =", *outputPath)
|
|
||||||
|
log.Println("keyboardID =", *keyboardID, "outputPath =", *outputPath)
|
||||||
|
|
||||||
// Opening database
|
// Opening database
|
||||||
db, err := sql.Open("sqlite3", DATABASE_NAME)
|
db, err := sql.Open("sqlite3", DATABASE_NAME)
|
||||||
@@ -253,15 +255,15 @@ func main() {
|
|||||||
|
|
||||||
keyMap := GetKeymap()
|
keyMap := GetKeymap()
|
||||||
|
|
||||||
switch {
|
|
||||||
case *keyboardId == -1 && *outputPath == "":
|
|
||||||
flag.PrintDefaults()
|
|
||||||
return
|
|
||||||
case *keyboardId != -1:
|
|
||||||
|
|
||||||
InitDb(db, keyMap)
|
InitDb(db, keyMap)
|
||||||
|
|
||||||
cmd := exec.Command("xinput", "test", strconv.Itoa(*keyboardId))
|
switch {
|
||||||
|
case *keyboardID == -1 && *outputPath == "":
|
||||||
|
flag.PrintDefaults()
|
||||||
|
return
|
||||||
|
case *keyboardID != -1:
|
||||||
|
|
||||||
|
cmd := exec.Command("xinput", "test", strconv.Itoa(*keyboardID))
|
||||||
|
|
||||||
stdout, err := cmd.StdoutPipe()
|
stdout, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user