Skip to content

Commit

Permalink
add runjs support
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Dec 17, 2014
1 parent 7d53dd2 commit b963c8e
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 6 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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://<phone ip addr>:21000`

![IMG](images/browser-airinput.png)

### About
Still in develop, but the code is healthy.

Expand Down
23 changes: 20 additions & 3 deletions go-airinput/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -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<num_steps; i++) {
for (i=0; i<=num_steps; i++) {
clock_gettime(CLOCK_MONOTONIC, &current_time);
/*
avg_event_dispatch_time_msec = ((avg_event_dispatch_time_msec * i) +
timediff_msec(&time_before_last_move,
&current_time)) / i;
*/
double wait_nsecs = desired_interval_msec * i - timediff_msec(&start_time, &current_time);
if (wait_nsecs > 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, &current_time, sizeof(struct timespec));
*/
execute_move(fd, device_flags, start_x+delta[0]*i, start_y+delta[1]*i);
}

Expand All @@ -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, "
Expand All @@ -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<num_steps; i++) {
execute_sleep(sleeptime);
for (i=0; i<=num_steps; i++) {
clock_gettime(CLOCK_MONOTONIC, &current_time);
double wait_nsecs = sleeptime * i - timediff_msec(&start_time, &current_time);
if (wait_nsecs > 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);
Expand Down
4 changes: 3 additions & 1 deletion go-airinput/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added images/browser-airinput.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions jsrunner.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 2 additions & 1 deletion run.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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.
17 changes: 16 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"image/png"
"io"
"io/ioutil"
"net/http"
"os"
"time"
Expand All @@ -19,9 +20,23 @@ func ServeWeb(addr string) {
<head><title>Native-Airinput</title></head>
<body><h2>Native Airinput</h2>
<div><img src="/screen.png" height="500px"/></div>
<a href="/test">pinch test</test>
<textarea id="jscode" style="height:100px; width:500px"></textarea>
<button id="btn-run">RUN</button>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.js"></script>
<script>
$(function(){
$("#btn-run").click(function(){
$.ajax('/runjs', {type:'POST', processData: false, data: $("#jscode").val()});
});
});
</script>
</body></html>`)
})
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)
Expand Down

0 comments on commit b963c8e

Please sign in to comment.