From cf7d9111a10e70284f6191400aec006710cb9d8b Mon Sep 17 00:00:00 2001 From: htmk Date: Fri, 8 Feb 2019 20:33:05 -0200 Subject: [PATCH] General cleanup --- README.md | 20 ++++++++++---------- event/goEvent.h | 18 +----------------- extern.go | 8 +------- hook.go | 8 +++----- 4 files changed, 15 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index bd46bc6..0fa0dc7 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # gohook -[![CircleCI Status](https://circleci.com/gh/robotn/gohook.svg?style=shield)](https://circleci.com/gh/robotn/gohook) -![Appveyor](https://ci.appveyor.com/api/projects/status/github/robotn/gohook?branch=master&svg=true) -[![Go Report Card](https://goreportcard.com/badge/github.com/robotn/gohook)](https://goreportcard.com/report/github.com/robotn/gohook) -[![GoDoc](https://godoc.org/github.com/robotn/gohook?status.svg)](https://godoc.org/github.com/robotn/gohook) +[![CircleCI Status](https://circleci.com/gh/cauefcr/gohook.svg?style=shield)](https://circleci.com/gh/cauefcr/gohook) +![Appveyor](https://ci.appveyor.com/api/projects/status/github/cauefcr/gohook?branch=master&svg=true) +[![Go Report Card](https://goreportcard.com/badge/github.com/cauefcr/gohook)](https://goreportcard.com/report/github.com/cauefcr/gohook) +[![GoDoc](https://godoc.org/github.com/cauefcr/gohook?status.svg)](https://godoc.org/github.com/cauefcr/gohook) ```Go @@ -11,15 +11,15 @@ package main import ( "fmt" - - "github.com/robotn/gohook" + //"github.com/robotn/gohook" + "github.com/cauefcr/gohook" ) func main() { - // hook.AsyncHook() - veve := hook.AddEvent("v") - if veve == 0 { - fmt.Println("v...") + EvChan := hook.Start() + defer hook.End() + for ev := range EvChan { + fmt.Println(ev) } } ``` \ No newline at end of file diff --git a/event/goEvent.h b/event/goEvent.h index 1c8a7c3..0bdbd23 100644 --- a/event/goEvent.h +++ b/event/goEvent.h @@ -15,9 +15,7 @@ #include #include "pub.h" -//#include "../chan/src/chan.h" #include "../chan/eb_chan.h" -//chan_t * events = NULL; eb_chan events; void go_send(char*); @@ -26,14 +24,12 @@ void go_sleep(void); bool sending = false; void startev(){ -// events = chan_init(1024); events = eb_chan_create(1024); eb_chan_retain(events); sending = true; add_event("q"); } -//bool done = false; void pollEv(){ if(events == NULL) return; for(;eb_chan_buf_len(events)!=0;){ @@ -57,17 +53,12 @@ void dispatch_proc(iohook_event * const event) { if(!sending) return; //leaking memory? hope not char* buffer = calloc(200,sizeof(char)); -// char buffer[256] = { 0 }; -// size_t length = snprintf(buffer, sizeof(buffer), -// "{id:%i,when:%" PRIu64 ",mask=0x%X", -// event->type, event->time, event->mask); switch (event->type) { case EVENT_HOOK_ENABLED: case EVENT_HOOK_DISABLED: sprintf(buffer,"{\"id\":%i,\"time\":%" PRIu64 ",\"mask\":%hu,\"reserved\":%hu}", event->type, event->time, event->mask,event->reserved); -// fprintf(stdout,"hook enabled"); break;//send it? case EVENT_KEY_PRESSED: case EVENT_KEY_RELEASED: @@ -111,19 +102,13 @@ void dispatch_proc(iohook_event * const event) { //to-do remove this for for(int i = 0; i < 5; i++){ switch(eb_chan_try_send(events,buffer)){ -// switch(chan_select(NULL,0,NULL,&events,1,(void**) &buffer)){ case eb_chan_res_ok: -// case 0: -// fprintf(stdout,"\nlen:%i,item sent: %s",chan_size(events),buffer); -// fprintf(stdout,"\nlen:%i,item sent: %s",eb_chan_buf_len(events),buffer); i=5; break; default: if (i == 4) {//let's not leak memory free(buffer); } -// chan_dispose(events); -// fprintf(stdout,"%i",i); continue; } } @@ -132,7 +117,6 @@ void dispatch_proc(iohook_event * const event) { } int add_event(char *key_event) { - // (uint16_t *) cevent = key_event; // Set the logger callback for library output. hook_set_logger(&loggerProc); @@ -228,7 +212,7 @@ int stop_event(){ loggerProc(LOG_LEVEL_ERROR, "Failed to get XRecord context. (%#X)", status); break; - // Default error. + // Default error. case IOHOOK_FAILURE: default: // loggerProc(LOG_LEVEL_ERROR, "An unknown hook error occurred. (%#X)", status); diff --git a/extern.go b/extern.go index ffe1c70..c170152 100644 --- a/extern.go +++ b/extern.go @@ -28,10 +28,4 @@ func go_send(s *C.char) { } //todo: maybe make non-bloking ev <- out -} - -////export go_sleep -//func go_sleep(){ -// -// time.Sleep(time.Millisecond*50) -//} +} \ No newline at end of file diff --git a/hook.go b/hook.go index 157c476..f0cfd3c 100644 --- a/hook.go +++ b/hook.go @@ -66,8 +66,9 @@ var ( asyncon bool = false ) +// Adds global event hook to OS +// returns event channel func Start() chan Event { - //fmt.Print("Here1") asyncon = true go C.startev() go func() { @@ -75,18 +76,15 @@ func Start() chan Event { C.pollEv() time.Sleep(time.Millisecond * 50) //todo: find smallest time that does not destroy the cpu utilization - //fmt.Println("_here_") if ! asyncon { return } } - //fmt.Print("WOOOOOOOOOT") }() - //fmt.Print("Here2") return ev } -// StopEvent stop event listener +// End removes global event hook func End() { C.endPoll() C.stop_event()