General cleanup

This commit is contained in:
htmk 2019-02-08 20:33:05 -02:00
parent 11eee3201f
commit cf7d9111a1
4 changed files with 15 additions and 39 deletions

View File

@ -1,9 +1,9 @@
# gohook # gohook
[![CircleCI Status](https://circleci.com/gh/robotn/gohook.svg?style=shield)](https://circleci.com/gh/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/robotn/gohook?branch=master&svg=true) ![Appveyor](https://ci.appveyor.com/api/projects/status/github/cauefcr/gohook?branch=master&svg=true)
[![Go Report Card](https://goreportcard.com/badge/github.com/robotn/gohook)](https://goreportcard.com/report/github.com/robotn/gohook) [![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/robotn/gohook?status.svg)](https://godoc.org/github.com/robotn/gohook) [![GoDoc](https://godoc.org/github.com/cauefcr/gohook?status.svg)](https://godoc.org/github.com/cauefcr/gohook)
<!-- This is a work in progress. --> <!-- This is a work in progress. -->
```Go ```Go
@ -11,15 +11,15 @@ package main
import ( import (
"fmt" "fmt"
//"github.com/robotn/gohook"
"github.com/robotn/gohook" "github.com/cauefcr/gohook"
) )
func main() { func main() {
// hook.AsyncHook() EvChan := hook.Start()
veve := hook.AddEvent("v") defer hook.End()
if veve == 0 { for ev := range EvChan {
fmt.Println("v...") fmt.Println(ev)
} }
} }
``` ```

View File

@ -15,9 +15,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "pub.h" #include "pub.h"
//#include "../chan/src/chan.h"
#include "../chan/eb_chan.h" #include "../chan/eb_chan.h"
//chan_t * events = NULL;
eb_chan events; eb_chan events;
void go_send(char*); void go_send(char*);
@ -26,14 +24,12 @@ void go_sleep(void);
bool sending = false; bool sending = false;
void startev(){ void startev(){
// events = chan_init(1024);
events = eb_chan_create(1024); events = eb_chan_create(1024);
eb_chan_retain(events); eb_chan_retain(events);
sending = true; sending = true;
add_event("q"); add_event("q");
} }
//bool done = false;
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;){
@ -57,17 +53,12 @@ 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));
// 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) { switch (event->type) {
case EVENT_HOOK_ENABLED: case EVENT_HOOK_ENABLED:
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);
// fprintf(stdout,"hook enabled");
break;//send it? break;//send it?
case EVENT_KEY_PRESSED: case EVENT_KEY_PRESSED:
case EVENT_KEY_RELEASED: case EVENT_KEY_RELEASED:
@ -111,19 +102,13 @@ void dispatch_proc(iohook_event * const event) {
//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)){ switch(eb_chan_try_send(events,buffer)){
// switch(chan_select(NULL,0,NULL,&events,1,(void**) &buffer)){
case eb_chan_res_ok: 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; 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);
} }
// chan_dispose(events);
// fprintf(stdout,"%i",i);
continue; continue;
} }
} }
@ -132,7 +117,6 @@ void dispatch_proc(iohook_event * const event) {
} }
int add_event(char *key_event) { int add_event(char *key_event) {
// (uint16_t *)
cevent = key_event; cevent = key_event;
// Set the logger callback for library output. // Set the logger callback for library output.
hook_set_logger(&loggerProc); hook_set_logger(&loggerProc);

View File

@ -29,9 +29,3 @@ func go_send(s *C.char) {
//todo: maybe make non-bloking //todo: maybe make non-bloking
ev <- out ev <- out
} }
////export go_sleep
//func go_sleep(){
//
// time.Sleep(time.Millisecond*50)
//}

View File

@ -66,8 +66,9 @@ var (
asyncon bool = false asyncon bool = false
) )
// Adds global event hook to OS
// returns event channel
func Start() chan Event { func Start() chan Event {
//fmt.Print("Here1")
asyncon = true asyncon = true
go C.startev() go C.startev()
go func() { go func() {
@ -75,18 +76,15 @@ func Start() chan Event {
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
//fmt.Println("_here_")
if ! asyncon { if ! asyncon {
return return
} }
} }
//fmt.Print("WOOOOOOOOOT")
}() }()
//fmt.Print("Here2")
return ev return ev
} }
// StopEvent stop event listener // End removes global event hook
func End() { func End() {
C.endPoll() C.endPoll()
C.stop_event() C.stop_event()