diff --git a/README.md b/README.md index b96cafc..bda76f4 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,16 @@ Semulate touch,drap,pinch for Android phone. ### The lib of go-airinput [![GoDoc](https://godoc.org/github.com/NetEase/airinput/go-airinput?status.svg)](https://godoc.org/github.com/NetEase/airinput/go-airinput) +### Distribution +I put a pre compiled file in **dist** folder + +About the usage: + +1. use `run.bat` to push file to your phone. +2. open browser `http://:21000` + +![IMG](images/browser-airinput.png) + ### About Still in develop, but the code is healthy. diff --git a/go-airinput/input.c b/go-airinput/input.c index e0bd9be..cf35aa8 100644 --- a/go-airinput/input.c +++ b/go-airinput/input.c @@ -246,17 +246,26 @@ void execute_drag(int fd, uint32_t device_flags, int start_x, // drag desired_interval_msec = duration_msec / num_steps; - for (i=0; i 0){ + execute_sleep(wait_nsecs); + } + + /* if (desired_interval_msec > 0 && avg_event_dispatch_time_msec < desired_interval_msec) { execute_sleep(desired_interval_msec - avg_event_dispatch_time_msec); } memcpy(&time_before_last_move, ¤t_time, sizeof(struct timespec)); + */ execute_move(fd, device_flags, start_x+delta[0]*i, start_y+delta[1]*i); } @@ -280,6 +289,9 @@ void execute_pinch(int fd, uint32_t device_flags, int touch1_x1, int sleeptime = duration_msec / num_steps; int i; + struct timespec start_time, current_time; + clock_gettime(CLOCK_MONOTONIC, &start_time); + print_action(ACTION_START, "pinch", "\"touch1_x1\": %d, \"touch1_y1\": %d, \"touch1_x2\": %d, " "\"touch1_y2\": %d, \"touch2_x1\": %d, \"touch2_y1\": %d, " @@ -297,8 +309,13 @@ void execute_pinch(int fd, uint32_t device_flags, int touch1_x1, execute_press(fd, device_flags, touch2_x1, touch2_y1); // drag - for (i=0; i 0){ + execute_sleep(wait_nsecs); + } + //execute_sleep(sleeptime); change_mt_slot(fd, device_flags, 0); execute_move(fd, device_flags, touch1_x1+delta1[0]*i, touch1_y1+delta1[1]*i); diff --git a/go-airinput/screen.go b/go-airinput/screen.go index 9bb0291..92f05ac 100644 --- a/go-airinput/screen.go +++ b/go-airinput/screen.go @@ -23,11 +23,13 @@ func ScreenSize() (w, h int) { return } +const DEV_FB0 = "/dev/graphics/fb0" + // TakeSnapshot of android phone (by read /dev/fb0) // Only ok with few phones, a lot of phone will got blank image. func TakeSnapshot2() *image.RGBA { var pict C.struct_picture - C.TakeScreenshot(C.CString("/dev/graphics/fb0"), &pict) + C.TakeScreenshot(C.CString(DEV_FB0), &pict) w, h := int(pict.xres), int(pict.yres) img := image.NewRGBA(image.Rectangle{image.Point{0, 0}, image.Point{w, h}}) size := w * h * 4 // Assume bytes per pixel is 4 bytes diff --git a/images/browser-airinput.png b/images/browser-airinput.png new file mode 100755 index 0000000..64bbfda Binary files /dev/null and b/images/browser-airinput.png differ diff --git a/jsrunner.go b/jsrunner.go new file mode 100644 index 0000000..ffbf248 --- /dev/null +++ b/jsrunner.go @@ -0,0 +1,57 @@ +// Package main provides ... +package main + +import ( + "time" + + "github.com/netease/airinput/go-airinput" + "github.com/robertkrimen/otto" +) + +var vm = otto.New() + +func init() { + totime := func(msec int64) time.Duration { + return time.Millisecond * time.Duration(msec) + } + vm.Set("tap", func(call otto.FunctionCall) otto.Value { + x, _ := call.Argument(0).ToInteger() + y, _ := call.Argument(1).ToInteger() + msec, _ := call.Argument(2).ToInteger() + airinput.Tap(int(x), int(y), totime(msec)) + return otto.UndefinedValue() + }) + vm.Set("drag", func(call otto.FunctionCall) otto.Value { + x0, _ := call.Argument(0).ToInteger() + y0, _ := call.Argument(1).ToInteger() + x1, _ := call.Argument(2).ToInteger() + y1, _ := call.Argument(3).ToInteger() + steps, _ := call.Argument(4).ToInteger() + msec, _ := call.Argument(5).ToInteger() + airinput.Drag(int(x0), int(y0), int(x1), int(y1), int(steps), totime(msec)) + return otto.UndefinedValue() + }) + vm.Set("pinch", func(call otto.FunctionCall) otto.Value { + ax0, _ := call.Argument(0).ToInteger() + ay0, _ := call.Argument(1).ToInteger() + ax1, _ := call.Argument(2).ToInteger() + ay1, _ := call.Argument(3).ToInteger() + bx0, _ := call.Argument(4).ToInteger() + by0, _ := call.Argument(5).ToInteger() + bx1, _ := call.Argument(6).ToInteger() + by1, _ := call.Argument(7).ToInteger() + steps, _ := call.Argument(8).ToInteger() + msec, _ := call.Argument(9).ToInteger() + airinput.Pinch( + int(ax0), int(ay0), int(ax1), int(ay1), + int(bx0), int(by0), int(bx1), int(by1), + int(steps), totime(msec)) + return otto.UndefinedValue() + }) +} + +// //abc = 1 + 2 +// console.log("The value of abc is " + abc); // 4 +func RunJS(code string) (otto.Value, error) { + return vm.Run(code) +} diff --git a/run.bat b/run.bat index 941cc90..c665fb2 100644 --- a/run.bat +++ b/run.bat @@ -11,6 +11,7 @@ adb.exe shell chmod 755 "/data/local/tmp/air-native" echo Starting... rem adb.exe shell kill -9 "/data/local/tmp/air-native" -adb.exe shell "/data/local/tmp/air-native -daemon" +rem adb.exe shell "/data/local/tmp/air-native -daemon" +adb.exe shell "/data/local/tmp/air-native" echo Service started successfully. diff --git a/server.go b/server.go index fa07213..8e28e11 100644 --- a/server.go +++ b/server.go @@ -5,6 +5,7 @@ import ( "fmt" "image/png" "io" + "io/ioutil" "net/http" "os" "time" @@ -19,9 +20,23 @@ func ServeWeb(addr string) { Native-Airinput

Native Airinput

- pinch test + + + + `) }) + http.HandleFunc("/runjs", func(w http.ResponseWriter, r *http.Request) { + code, _ := ioutil.ReadAll(r.Body) + ret, _ := RunJS(string(code)) + io.WriteString(w, ret.String()) + }) http.HandleFunc("/test", func(rw http.ResponseWriter, r *http.Request) { w, h := airinput.ScreenSize() fmt.Printf("width: %d, height: %d\n", w, h)