update godoc and code style

This commit is contained in:
vcaesar 2019-02-19 10:47:03 -04:00
parent 43543e0cff
commit fe1bbb57e6
3 changed files with 66 additions and 44 deletions

View File

@ -31,13 +31,13 @@ void startev(){
}
void pollEv(){
if(events == NULL) return;
for(;eb_chan_buf_len(events)!=0;){
if (events == NULL) return;
for (;eb_chan_buf_len(events)!=0;) {
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);
free(tmp);
}else{
} else {
//
}
}
@ -45,13 +45,13 @@ void pollEv(){
void endPoll(){
sending = false;
pollEv();//remove last things from channel
pollEv(); // remove last things from channel
eb_chan_release(events);
}
void dispatch_proc(iohook_event * const event) {
if(!sending) return;
//leaking memory? hope not
// leaking memory? hope not
char* buffer = calloc(200,sizeof(char));
switch (event->type) {
@ -59,7 +59,7 @@ void dispatch_proc(iohook_event * const event) {
case EVENT_HOOK_DISABLED:
sprintf(buffer,"{\"id\":%i,\"time\":%" PRIu64 ",\"mask\":%hu,\"reserved\":%hu}",
event->type, event->time, event->mask,event->reserved);
break;//send it?
break; // send it?
case EVENT_KEY_PRESSED:
case EVENT_KEY_RELEASED:
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);
return;
}
//to-do remove this for
// to-do remove this for
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:
i=5;
break;
default:
if (i == 4) {//let's not leak memory
if (i == 4) { // let's not leak memory
free(buffer);
}
continue;

View File

@ -2,32 +2,36 @@ package hook
/*
// #include "event/hook_async.h"
*/
import "C"
import (
"encoding/json"
"log"
"time"
"encoding/json"
)
//export go_send
func go_send(s *C.char) {
str := []byte(C.GoString(s))
out := Event{}
err := json.Unmarshal(str, &out)
if err != nil {
log.Fatal(err)
}
if out.Keychar != CharUndefined {
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 {
log.Fatal(err)
}
//todo: maybe make non-bloking
// todo: maybe make non-bloking
ev <- out
}

76
hook.go
View File

@ -31,40 +31,49 @@ import (
)
const (
HookEnabled = 1 //iota
HookEnabled = 1 // iota
HookDisabled = 2
KeyDown = 3
KeyHold = 4
KeyUp = 5
MouseUp = 6
MouseHold = 7
MouseDown = 8
MouseMove = 9
MouseDrag = 10
MouseWheel = 11
FakeEvent = 12
//Keychar could be v
KeyDown = 3
KeyHold = 4
KeyUp = 5
MouseUp = 6
MouseHold = 7
MouseDown = 8
MouseMove = 9
MouseDrag = 10
MouseWheel = 11
FakeEvent = 12
//Keychar could be v
CharUndefined = 0xFFFF
WheelUp = -1
WheelDown = 1
)
//Holds a system event
//If it's a Keyboard event the relevant fields are: Mask, Keycode, Rawcode, and Keychar,
//Keychar is probably what you want. If it's a Mouse event the relevant fields are:
//Button, Clicks, X, Y, Amount, Rotation and Direction
// Event Holds a system event
// If it's a Keyboard event the relevant fields are:
// Mask, Keycode, Rawcode, and Keychar,
// 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 {
Kind uint8 `json:"id"`
When time.Time
Mask uint16 `json:"mask"`
Reserved uint16 `json:"reserved"`
Keycode uint16 `json:"keycode"`
Rawcode uint16 `json:"rawcode"`
Keychar rune `json:"keychar"`
Button uint16 `json:"button"`
Clicks uint16 `json:"clicks"`
X int16 `json:"x"`
Y int16 `json:"y"`
Kind uint8 `json:"id"`
When time.Time
Mask uint16 `json:"mask"`
Reserved uint16 `json:"reserved"`
Keycode uint16 `json:"keycode"`
Rawcode uint16 `json:"rawcode"`
Keychar rune `json:"keychar"`
Button uint16 `json:"button"`
Clicks uint16 `json:"clicks"`
X int16 `json:"x"`
Y int16 `json:"y"`
Amount uint16 `json:"amount"`
Rotation int16 `json:"rotation"`
Direction uint8 `json:"direction"`
@ -75,6 +84,7 @@ var (
asyncon = false
)
// String return hook kind string
func (e Event) String() string {
switch e.Kind {
case HookEnabled:
@ -102,32 +112,38 @@ func (e Event) String() string {
case FakeEvent:
return fmt.Sprintf("%v - Event: {Kind: FakeEvent}", e.When)
}
return "Unknown event, contact the mantainers"
}
// RawcodetoKeychar rawcode to keychar
func RawcodetoKeychar(r uint16) string {
return raw2key[r]
}
// KeychartoiRawcode key char to rawcode
func KeychartoRawcode(kc string) uint16 {
return keytoraw[kc]
}
// Adds global event hook to OS
// Start Adds global event hook to OS
// returns event channel
func Start() chan Event {
asyncon = true
go C.startev()
go func() {
for {
C.pollEv()
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 {
return
}
}
}()
return ev
}
@ -135,9 +151,11 @@ func Start() chan Event {
func End() {
C.endPoll()
C.stop_event()
for len(ev) != 0 {
<-ev
}
asyncon = false
}