Skip to content

Commit

Permalink
package airinput as a go library
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Dec 11, 2014
1 parent f719495 commit 5db8c7d
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 217 deletions.
8 changes: 5 additions & 3 deletions input.c → go-airinput/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ enum {
static int global_tracking_id = 1;
static uint32_t device_flags;

static int print_actions = 1;
static int print_actions = 0;
static int action_level = 0;

int fd; // touchscreen fd
Expand All @@ -63,16 +63,18 @@ void execute_pinch(int fd, uint32_t device_flags, int touch1_x1,
int touch2_y1, int touch2_x2, int touch2_y2, int num_steps,
int duration_msec);

void set_debug(int on){
print_actions = on;
}

// must be execute before everythong
int input_init(char * device){
fd = open(device, O_RDWR);
if(fd < 0) {
fprintf(stderr, "could not open %s, %s\n", device, strerror(errno));
return 1;
}
printf("%d\n", device_flags);
device_flags = figure_out_events_device_reports(fd);
printf("%d\n", device_flags);
}

void tap(int x, int y, int msec){
Expand Down
88 changes: 88 additions & 0 deletions go-airinput/input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package airinput

import (
// #include "input.h"
"C"
"fmt"
"io/ioutil"
"log"
"regexp"
"strings"
"time"

"github.com/codeskyblue/comtool"
)

var goDebug = false

func dprintf(format string, args ...interface{}) {
if strings.HasSuffix(format, "\n") {
format += "\n"
}
if goDebug {
fmt.Printf(format, args...)
}
}

func init() {
var id int = -1
for i := 0; i < 10; i++ {
namePath := fmt.Sprintf("/sys/class/input/event%d/device/name", i)
if !comtool.Exists(namePath) {
break
}
data, err := ioutil.ReadFile(namePath)
if err != nil {
continue
}
name := strings.TrimSpace(string(data))
dprintf("event%d: name: %s", i, name)
// $name may have Touchscreen and touchscreen
for _, possibleName := range []string{"ouchscreen$", "-tpd$"} {
re := regexp.MustCompile(possibleName)
if re.MatchString(name) {
id = i
break
}
}
if id != -1 {
break
}
}
dprintf("eventid: %d", id)
if id == -1 {
log.Fatal("can autodetect touchpad event")
}
C.input_init(C.CString(fmt.Sprintf("/dev/input/event%d", id)))
}

// Where show debug info
func Debug(state bool) {
flag := C.int(0)
if state {
flag = C.int(1)
}
goDebug = state
C.set_debug(flag)
}

// void tap(int x, int y, int duration_msec);
func Tap(x, y int, duration time.Duration) {
msec := int(duration.Nanoseconds() / 1e6)
C.tap(C.int(x), C.int(y), C.int(msec))
}

// void drag(int start_x, int start_y, int end_x, int end_y, int num_steps, int msec);
func Drag(startX, startY int, endX, endY int, steps int, duration time.Duration) {
msec := int(duration.Nanoseconds() / 1e6)
C.drag(C.int(startX), C.int(startY), C.int(endX), C.int(endY), C.int(steps), C.int(msec))
}

//void pinch(int Ax0, int Ay0, int Ax1, int Ay1,
// int Bx0, int By0, int Bx1, int By1, int num_steps, int msec);
func Pinch(Ax0, Ay0, Ax1, Ay1 int,
Bx0, By0, Bx1, By1 int, steps int, duration time.Duration) {
msec := int(duration.Nanoseconds() / 1e6)
C.pinch(C.int(Ax0), C.int(Ay0), C.int(Ax1), C.int(Ay1),
C.int(Bx0), C.int(By0), C.int(Bx1), C.int(By1), C.int(steps), C.int(msec))
}
2 changes: 2 additions & 0 deletions input.h → go-airinput/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#ifndef INPUT_H
#define INPUT_H

void set_debug(int on);

int input_init(char * event);

void tap(int x, int y, int duration_msec);
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion screen.go → go-airinput/screen.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package airinput

// #include "screen.h"
import "C"
Expand Down
File renamed without changes.
163 changes: 0 additions & 163 deletions input.go

This file was deleted.

64 changes: 17 additions & 47 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,27 @@
// Currently this is a sample
package main

// #include "input.h"
import "C"
import (
"fmt"
"io/ioutil"
"log"
"regexp"
"strings"
"time"
)
import "github.com/codeskyblue/comtool"

func init() {
var id int = -1
for i := 0; i < 10; i++ {
namePath := fmt.Sprintf("/sys/class/input/event%d/device/name", i)
if !comtool.Exists(namePath) {
break
}
data, err := ioutil.ReadFile(namePath)
if err != nil {
continue
}
name := strings.TrimSpace(string(data))
fmt.Printf("event%d: name: %s\n", i, name)
for _, possibleName := range []string{"touchscreen$", "-tpd$"} {
re := regexp.MustCompile(possibleName)
if re.MatchString(name) {
id = i
break
}
}
if id != -1 {
break
}
}
fmt.Println("eventid:", id)
if id == -1 {
log.Fatal("can autodetect touchpad event")
}
C.input_init(C.CString(fmt.Sprintf("/dev/input/event%d", id)))
}
"github.com/netease/airinput/go-airinput"
)

func main() {
//C.tap(C.int(154), C.int(1082), 5000) // 1000 = 1s
fmt.Println(ScreenSize())
w, _ := ScreenSize()
fmt.Println(w / 6 * 5)
x, y := C.int(w/6), C.int(300)
//C.drag(x, y, C.int(945), y, 10, 5000)
mx := C.int(w / 2)
C.pinch(x, y, mx, y, C.int(w/6*5), y, mx, y, 5, 1000)
//airinput.Debug(false)
w, h := airinput.ScreenSize()
fmt.Printf("width: %d, height: %d\n", w, h)

lx, ly := w/6, 300
mx, my := w/2, ly
rx, ry := w/6*5, ly

airinput.Pinch(lx, ly, mx, my,
rx, ry, mx, my, 10, time.Second)

time.Sleep(time.Second * 1)
C.pinch(mx, y, x, y, mx, y, C.int(w/6*5), y, 5, 1000)

airinput.Pinch(mx, my, lx, ly,
mx, my, rx, ry, 10, time.Second)
}
6 changes: 3 additions & 3 deletions run.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@echo off
adb push airtoolbox /data/local/tmp/
adb shell chmod 755 /data/local/tmp/airtoolbox
adb shell /data/local/tmp/airtoolbox
adb push airinput /data/local/tmp/
adb shell chmod 755 /data/local/tmp/airinput
adb shell /data/local/tmp/airinput

0 comments on commit 5db8c7d

Please sign in to comment.