mirror of
https://github.com/robotn/gohook.git
synced 2024-11-22 02:56:54 +08:00
update godoc and code style
This commit is contained in:
parent
43543e0cff
commit
fe1bbb57e6
@ -31,13 +31,13 @@ void startev(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void pollEv(){
|
void pollEv(){
|
||||||
if(events == NULL) return;
|
if (events == NULL) return;
|
||||||
for(;eb_chan_buf_len(events)!=0;){
|
for (;eb_chan_buf_len(events)!=0;) {
|
||||||
char* tmp;
|
char* tmp;
|
||||||
if(eb_chan_try_recv(events,(const void**) &tmp) == eb_chan_res_ok){
|
if( eb_chan_try_recv(events,(const void**) &tmp) == eb_chan_res_ok ){
|
||||||
go_send(tmp);
|
go_send(tmp);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
}else{
|
} else {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -45,13 +45,13 @@ void pollEv(){
|
|||||||
|
|
||||||
void endPoll(){
|
void endPoll(){
|
||||||
sending = false;
|
sending = false;
|
||||||
pollEv();//remove last things from channel
|
pollEv(); // remove last things from channel
|
||||||
eb_chan_release(events);
|
eb_chan_release(events);
|
||||||
}
|
}
|
||||||
|
|
||||||
void dispatch_proc(iohook_event * const event) {
|
void dispatch_proc(iohook_event * const event) {
|
||||||
if(!sending) return;
|
if(!sending) return;
|
||||||
//leaking memory? hope not
|
// leaking memory? hope not
|
||||||
char* buffer = calloc(200,sizeof(char));
|
char* buffer = calloc(200,sizeof(char));
|
||||||
|
|
||||||
switch (event->type) {
|
switch (event->type) {
|
||||||
@ -59,7 +59,7 @@ void dispatch_proc(iohook_event * const event) {
|
|||||||
case EVENT_HOOK_DISABLED:
|
case EVENT_HOOK_DISABLED:
|
||||||
sprintf(buffer,"{\"id\":%i,\"time\":%" PRIu64 ",\"mask\":%hu,\"reserved\":%hu}",
|
sprintf(buffer,"{\"id\":%i,\"time\":%" PRIu64 ",\"mask\":%hu,\"reserved\":%hu}",
|
||||||
event->type, event->time, event->mask,event->reserved);
|
event->type, event->time, event->mask,event->reserved);
|
||||||
break;//send it?
|
break; // send it?
|
||||||
case EVENT_KEY_PRESSED:
|
case EVENT_KEY_PRESSED:
|
||||||
case EVENT_KEY_RELEASED:
|
case EVENT_KEY_RELEASED:
|
||||||
case EVENT_KEY_TYPED:
|
case EVENT_KEY_TYPED:
|
||||||
@ -99,14 +99,14 @@ void dispatch_proc(iohook_event * const event) {
|
|||||||
fprintf(stderr,"\nError on file: %s, unusual event->type: %i\n",__FILE__,event->type);
|
fprintf(stderr,"\nError on file: %s, unusual event->type: %i\n",__FILE__,event->type);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//to-do remove this for
|
// to-do remove this for
|
||||||
for(int i = 0; i < 5; i++){
|
for(int i = 0; i < 5; i++){
|
||||||
switch(eb_chan_try_send(events,buffer)){ //never block the hook callback
|
switch(eb_chan_try_send(events,buffer)){ // never block the hook callback
|
||||||
case eb_chan_res_ok:
|
case eb_chan_res_ok:
|
||||||
i=5;
|
i=5;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (i == 4) {//let's not leak memory
|
if (i == 4) { // let's not leak memory
|
||||||
free(buffer);
|
free(buffer);
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
|
14
extern.go
14
extern.go
@ -2,32 +2,36 @@ package hook
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
|
||||||
// #include "event/hook_async.h"
|
// #include "event/hook_async.h"
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
//export go_send
|
//export go_send
|
||||||
func go_send(s *C.char) {
|
func go_send(s *C.char) {
|
||||||
str := []byte(C.GoString(s))
|
str := []byte(C.GoString(s))
|
||||||
out := Event{}
|
out := Event{}
|
||||||
|
|
||||||
err := json.Unmarshal(str, &out)
|
err := json.Unmarshal(str, &out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if out.Keychar != CharUndefined {
|
if out.Keychar != CharUndefined {
|
||||||
raw2key[out.Rawcode] = string([]rune{out.Keychar})
|
raw2key[out.Rawcode] = string([]rune{out.Keychar})
|
||||||
}
|
}
|
||||||
//todo bury this deep into the C lib so that the time is correct
|
|
||||||
out.When = time.Now() //at least it's consistent
|
// todo bury this deep into the C lib so that the time is correct
|
||||||
|
out.When = time.Now() // at least it's consistent
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
//todo: maybe make non-bloking
|
|
||||||
|
// todo: maybe make non-bloking
|
||||||
ev <- out
|
ev <- out
|
||||||
}
|
}
|
||||||
|
76
hook.go
76
hook.go
@ -31,40 +31,49 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
HookEnabled = 1 //iota
|
HookEnabled = 1 // iota
|
||||||
HookDisabled = 2
|
HookDisabled = 2
|
||||||
KeyDown = 3
|
|
||||||
KeyHold = 4
|
KeyDown = 3
|
||||||
KeyUp = 5
|
KeyHold = 4
|
||||||
MouseUp = 6
|
KeyUp = 5
|
||||||
MouseHold = 7
|
|
||||||
MouseDown = 8
|
MouseUp = 6
|
||||||
MouseMove = 9
|
MouseHold = 7
|
||||||
MouseDrag = 10
|
MouseDown = 8
|
||||||
MouseWheel = 11
|
MouseMove = 9
|
||||||
FakeEvent = 12
|
MouseDrag = 10
|
||||||
//Keychar could be v
|
MouseWheel = 11
|
||||||
|
|
||||||
|
FakeEvent = 12
|
||||||
|
//Keychar could be v
|
||||||
CharUndefined = 0xFFFF
|
CharUndefined = 0xFFFF
|
||||||
WheelUp = -1
|
WheelUp = -1
|
||||||
WheelDown = 1
|
WheelDown = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
//Holds a system event
|
// Event Holds a system event
|
||||||
//If it's a Keyboard event the relevant fields are: Mask, Keycode, Rawcode, and Keychar,
|
// If it's a Keyboard event the relevant fields are:
|
||||||
//Keychar is probably what you want. If it's a Mouse event the relevant fields are:
|
// Mask, Keycode, Rawcode, and Keychar,
|
||||||
//Button, Clicks, X, Y, Amount, Rotation and Direction
|
// Keychar is probably what you want.
|
||||||
|
// If it's a Mouse event the relevant fields are:
|
||||||
|
// Button, Clicks, X, Y, Amount, Rotation and Direction
|
||||||
type Event struct {
|
type Event struct {
|
||||||
Kind uint8 `json:"id"`
|
Kind uint8 `json:"id"`
|
||||||
When time.Time
|
When time.Time
|
||||||
Mask uint16 `json:"mask"`
|
Mask uint16 `json:"mask"`
|
||||||
Reserved uint16 `json:"reserved"`
|
Reserved uint16 `json:"reserved"`
|
||||||
Keycode uint16 `json:"keycode"`
|
|
||||||
Rawcode uint16 `json:"rawcode"`
|
Keycode uint16 `json:"keycode"`
|
||||||
Keychar rune `json:"keychar"`
|
Rawcode uint16 `json:"rawcode"`
|
||||||
Button uint16 `json:"button"`
|
Keychar rune `json:"keychar"`
|
||||||
Clicks uint16 `json:"clicks"`
|
|
||||||
X int16 `json:"x"`
|
Button uint16 `json:"button"`
|
||||||
Y int16 `json:"y"`
|
Clicks uint16 `json:"clicks"`
|
||||||
|
|
||||||
|
X int16 `json:"x"`
|
||||||
|
Y int16 `json:"y"`
|
||||||
|
|
||||||
Amount uint16 `json:"amount"`
|
Amount uint16 `json:"amount"`
|
||||||
Rotation int16 `json:"rotation"`
|
Rotation int16 `json:"rotation"`
|
||||||
Direction uint8 `json:"direction"`
|
Direction uint8 `json:"direction"`
|
||||||
@ -75,6 +84,7 @@ var (
|
|||||||
asyncon = false
|
asyncon = false
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// String return hook kind string
|
||||||
func (e Event) String() string {
|
func (e Event) String() string {
|
||||||
switch e.Kind {
|
switch e.Kind {
|
||||||
case HookEnabled:
|
case HookEnabled:
|
||||||
@ -102,32 +112,38 @@ func (e Event) String() string {
|
|||||||
case FakeEvent:
|
case FakeEvent:
|
||||||
return fmt.Sprintf("%v - Event: {Kind: FakeEvent}", e.When)
|
return fmt.Sprintf("%v - Event: {Kind: FakeEvent}", e.When)
|
||||||
}
|
}
|
||||||
|
|
||||||
return "Unknown event, contact the mantainers"
|
return "Unknown event, contact the mantainers"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RawcodetoKeychar rawcode to keychar
|
||||||
func RawcodetoKeychar(r uint16) string {
|
func RawcodetoKeychar(r uint16) string {
|
||||||
return raw2key[r]
|
return raw2key[r]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// KeychartoiRawcode key char to rawcode
|
||||||
func KeychartoRawcode(kc string) uint16 {
|
func KeychartoRawcode(kc string) uint16 {
|
||||||
return keytoraw[kc]
|
return keytoraw[kc]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds global event hook to OS
|
// Start Adds global event hook to OS
|
||||||
// returns event channel
|
// returns event channel
|
||||||
func Start() chan Event {
|
func Start() chan Event {
|
||||||
asyncon = true
|
asyncon = true
|
||||||
go C.startev()
|
go C.startev()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
C.pollEv()
|
C.pollEv()
|
||||||
time.Sleep(time.Millisecond * 50)
|
time.Sleep(time.Millisecond * 50)
|
||||||
//todo: find smallest time that does not destroy the cpu utilization
|
|
||||||
|
// todo: find smallest time that does not destroy the cpu utilization
|
||||||
if !asyncon {
|
if !asyncon {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return ev
|
return ev
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,9 +151,11 @@ func Start() chan Event {
|
|||||||
func End() {
|
func End() {
|
||||||
C.endPoll()
|
C.endPoll()
|
||||||
C.stop_event()
|
C.stop_event()
|
||||||
|
|
||||||
for len(ev) != 0 {
|
for len(ev) != 0 {
|
||||||
<-ev
|
<-ev
|
||||||
}
|
}
|
||||||
|
|
||||||
asyncon = false
|
asyncon = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user