2013年12月4日星期三

Between different processes on android analog buttons and touch events

I can now write a simulated keys and touch events on different android applications, but with sendPointerSync and sendKeySync can not be used in different processes , there will be a use Injecting to another application requires INJECT_EVENTS permission abnormal even if permission is added INJECT_EVENTS is not enough , so do the underlying JNI to write a simulation . Which heroes know how to do it? best to give code. greatly appreciated ! ! ! ! ! ! ! ! < br> ------ Solution ----------------------------------------- ---
take a look monkey bar code, in development / cmds / monkey / src / com / android / commands / monkey. inputmanager Laigao through the inside . I do not know whether it is something you want , right when initiate it
------ Solution -------------------------- ------------------
with sendevent command like, if you have the patience , you can also put it into the JNI
------ Solution - ------------------------------------------


cow !

Also sendevent coordinate data on the simulator normal, but the device is not the problem , take a look : http://blog.csdn.net/yiyaaixuexi/article/details/6574001
------ eference --------------------------------------- < br>

Oh I see next .
------ eference ---------------------------- -----------


sendevent command to how to use ? there in which you can find its source .
------ eference -------------------- -------------------
sendevent / dev / input / eventX X X X
http://blog.csdn.net/watson243671/article/details/6647334
------ eference ------------------- --------------------


emergence of new problems .
I'm some development board, the screen is 800 * 480
But I used the top left corner getevent, and the touch screen to the data as follows :
/ dev/input/event1: 0003 0000 0000005b
/ dev/input/event1: 0003 0001 000000f1
/ dev/input/event1: 0001 014a 00000001
/ dev/input/event1: 0000 0000 00000000
/ dev/input/event1: 0003 0000 0000005f
/ dev/input/event1: 0000 0000 00000000
/ dev/input/event1: 0003 0000 00000061
/ dev/input/event1: 0000 0000 00000000
/ dev/input/event1: 0003 0000 00000066
/ dev/input/event1: 0000 0000 00000000
/ dev/input/event1: 0003 0000 000002e9
/ dev/input/event1: 0003 0001 0000391f
/ dev/input/event1: 0001 014a 00000000
/ dev/input/event1: 0000 0000 00000000

data obtained while the lower right corner of the touch screen is :
/ dev/input/event1: 0003 0000 00000f18
/ dev/input/event1: 0003 0001 00000e89
/ dev/input/event1: 0001 014a 00000001
/ dev/input/event1: 0000 0000 00000000
/ dev/input/event1: 0003 0000 00003c5c
/ dev/input/event1: 0003 0001 00000460
/ dev/input/event1: 0001 014a 00000000
/ dev/input/event1: 0000 0000 00000000

This data is not in the top left corner coordinates (0, 0 ) near the lower right corner has reached the coordinates ( 3864 , 3721 ) and this number far beyond the ( 800 , 480 ) a ah.
I was in the simulator next test is 800 * 480 screen , the result is at ( 0, 0 ) between the ( 800 , 480 ) , which is how ah ?
------ eference --------------------------------------- < br> this is the physical code . . . . . And hardware close. . . .
------ eference --------------------------------------- < br>

physics code do not understand.
Is there any way that can be ( 0, 0 ) to ( 800 , 480 ) to a physical code that what you ?
------ eference ------ ---------------------------------
If you know the range of XY physics codes, the ratio can be mapped
personally feel that this issue should be made ​​by the system , APP to do this live, a bit difficult , because the restrictions on individual programs are safer

top, waiting for the master appears
------ eference ------------------------------ ---------
  The reply was deleted at the moderator 2012-07-16 10:59:31

------ eference ------------------------------------ ---


I found a way .
ioctl ("/ dev/input/event1", EVIOCGABS (ABS_X), & info);
ioctl ("/ dev/input/event1", EVIOCGABS (ABS_Y), & info);
can get this range.
EVIOCGABS, ABS_X, ABS_Y and input_absinfo structures are defined in .
------ eference --------- ------------------------------

There is a small problem

Like you said : "qtouch-touchscreen" is not dead event several provisions , saying 1234567 are likely to see your equipment up .
I want to determine which is the touch , which is the key before they can operate normally , I do not know how to determine it , maybe take a look getevent code can be harvested .
------ eference ---------------------------------------
issue has been resolved .
------ eference ---------------------------------------
can not your code to the next , I have encountered this problem , it is an emergency ! ! ! Thank you very much ! ! !
------ eference --------------------------------------- < br>

my using the JNI . while to ROOT privileges.



#include <stdlib.h>
#include <android/log.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/input.h>
#include <dirent.h>
#include <errno.h>
//#include "nativemethod.h"

#define LOGI(...)  __android_log_print(ANDROID_LOG_INFO, "EventEmulate", __VA_ARGS__)
#define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR, "EventEmulate", __VA_ARGS__)

#define DEV_DIR "/dev/input"
#define sizeof_bit_array(bits)  ((bits + 7) / 8)
#define test_bit(bit, array)    (array[bit/8] & (1<<(bit%8)))
#define ABS_MT_POSITION_X 0x35
#define ABS_MT_POSITION_Y 0x36

struct EVENT_INFO
{
int fd_touch;
int fd_key;
int screen_width;
int screen_height;
int abs_x_min;
int abs_x_max;
int abs_y_min;
int abs_y_max;
};

int scan_dir(const char *dirname);
int open_dev(const char *deviceName);
int write_event(int fd, int type, int code, int value);
void calculateXY(float x, float y, int *abs_x, int *abs_y);
int containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex);

struct EVENT_INFO ei;
int initEVT = 0;

int EVT_open(struct NATIVE_INFO *info)
{
struct input_absinfo absinfo;

if(initEVT)
return 0;

if(info == NULL)
{
LOGE("info null point.");
goto fail;
}

if(info->FB_width == 0 || info->FB_height == 0)
{
LOGE("error width %d and height %d.", info->FB_width, info->FB_height);
goto fail;
}

memset(&ei, 0, sizeof(ei));
ei.screen_width = info->FB_width;
ei.screen_height = info->FB_height;

scan_dir(DEV_DIR);

if(ioctl(ei.fd_touch, EVIOCGABS(ABS_X), &absinfo)) {
LOGI("Error reading absolute controller ABS_X[%d]: %s", errno, strerror(errno));
return;
}
ei.abs_x_min = absinfo.minimum;
ei.abs_x_max = absinfo.maximum;

if(ioctl(ei.fd_touch, EVIOCGABS(ABS_Y), &absinfo)) {
LOGI("Error reading absolute controller ABS_Y[%d]: %s", errno, strerror(errno));
return;
}
ei.abs_y_min = absinfo.minimum;
ei.abs_y_max = absinfo.maximum;

initEVT = 1;
return 0;

fail:
EVT_close();
return -1;
}

int EVT_close()
{
if(ei.fd_key > 0)
close(ei.fd_key);
if(ei.fd_touch > 0)
close(ei.fd_touch);

initEVT = 0;
return 0;
}

int EVT_touch(int action, float x, float y)
{
int abs_x, abs_y;

if(initEVT == 0)
{
LOGE("event not inital");
return -1;
}

switch(action)
{
case ACTION_DOWN:
calculateXY(x, y, &abs_x, &abs_y);
write_event(ei.fd_touch, 3, 0, abs_x);
write_event(ei.fd_touch, 3, 1, abs_y);
write_event(ei.fd_touch, 1, 330, 1);
write_event(ei.fd_touch, 0, 0, 0);
break;
case ACTION_UP:
write_event(ei.fd_touch, 1, 330, 0);
write_event(ei.fd_touch, 0, 0, 0);
break;
case ACTION_MOVE:
calculateXY(x, y, &abs_x, &abs_y);
write_event(ei.fd_touch, 3, 0, abs_x);
write_event(ei.fd_touch, 3, 1, abs_y);
write_event(ei.fd_touch, 0, 0, 0);
break;
}
return 0;
}

int EVT_key(int action, int key)
{
if(initEVT == 0)
{
LOGE("event not inital");
return -1;
}

switch(action)
{
case ACTION_DOWN:
write_event(ei.fd_key, 1, key, 1);
break;
case ACTION_UP:
write_event(ei.fd_key, 1, key, 0);
break;
}
return 0;
}


int scan_dir(const char *dirname)
{
char devname[PATH_MAX];
char *filename;
DIR *dir;
struct dirent *de;
dir = opendir(dirname);
if(dir == NULL)
return -1;
strcpy(devname, dirname);
filename = devname + strlen(devname);
*filename++ = '/';
while((de = readdir(dir))) {
if(de->d_name[0] == '.' &&
   (de->d_name[1] == '\0' ||
(de->d_name[1] == '.' && de->d_name[2] == '\0')))
continue;
strcpy(filename, de->d_name);
open_dev(devname);
}
closedir(dir);
return 0;
}

int open_dev(const char *deviceName)
{
int fd;
int version;
uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
uint8_t abs_bitmask[sizeof_bit_array(ABS_MAX + 1)];

fd = open(deviceName, O_RDWR);
if(fd < 0) {
LOGI("could not open device[%d]: %s", errno, strerror(errno));
return -1;
}

if(ioctl(fd, EVIOCGVERSION, &version)) {
return -1;
}

memset(key_bitmask, 0, sizeof(key_bitmask));
if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask) >= 0) {
if (containsNonZeroByte(key_bitmask, 0, sizeof_bit_array(BTN_MISC))
|| containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_GAMEPAD),
sizeof_bit_array(BTN_DIGI))
|| containsNonZeroByte(key_bitmask, sizeof_bit_array(KEY_OK),
sizeof_bit_array(KEY_MAX + 1))) {
ei.fd_key = fd;
LOGI("get key input device: %s", deviceName);
}
}

memset(abs_bitmask, 0, sizeof(abs_bitmask));
if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask) >= 0) {
// Is this a new modern multi-touch driver?
if (test_bit(ABS_MT_POSITION_X, abs_bitmask)
&& test_bit(ABS_MT_POSITION_Y, abs_bitmask)) {
ei.fd_touch = fd;
LOGI("get multi-touch input device: %s", deviceName);

// Is this an old style single-touch driver?
} else if (test_bit(BTN_TOUCH, key_bitmask)
&& test_bit(ABS_X, abs_bitmask) && test_bit(ABS_Y, abs_bitmask)) {
ei.fd_touch = fd;
LOGI("get single-touch input device: %s", deviceName);
}
}
}

int write_event(int fd, int type, int code, int value)
{
struct input_event event;

memset(&event, 0, sizeof(event));
event.type = type;
event.code = code;
event.value = value;
if(write(fd, &event, sizeof(event)) < sizeof(event)) {
LOGI("write event failed[%d]: %s", errno, strerror(errno));
return -1;
}
return 0;
}

void calculateXY(float x, float y, int *abs_x, int *abs_y)
{
*abs_x = ei.abs_x_min +
(int)((x * (float)(ei.abs_x_max - ei.abs_x_min)) / ei.screen_width + 0.5);
*abs_y = ei.abs_y_min +
(int)((y * (float)(ei.abs_y_max - ei.abs_y_min)) / ei.screen_height + 0.5);
}

int containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex)
{
    const uint8_t* end = array + endIndex;
    array += startIndex;
    while (array != end) {
        if (*(array++) != 0) {
            return 1;
        }
    }
    return 0;
}



struct NATIVE_INFO structure which is my own definition. which contains the width and height of the screen .
------ eference --------------- ------------------------
Liu ~
------ eference ---------- -----------------------------
landlord did not give header files. . . Compile various errors
------ eference ------------------------------------ ---


This is just to give you as a reference .
header file is only nativemethod.h I wrote it myself , the other should all be there .
------ eference ------------- --------------------------
landlord, can not give a definition NATIVE_INFO for ?
------ eference --------------------------------------- < br> recently, I also studied the landlord .... this is a white ... can you say a little more points the amount of ... grateful ah ~ ~ ~ ~
------ eference ---------------------------- -----------
brothers and I also need to solve the same problem , the landlord can give you copies of the source code
------ eference ---------- -----------------------------
Why can simulate the value of 122 keys
------ eference ---------------------------------------
I refer to try it, found command ACTION_DOWN ACTION_UP ACTION_MOVE need to send different formats on different devices
would like to ask how this place plus judge can write a generic. . .

I hope you have time gets advice

Here is what I wrote for two different devices function , do not know if you can add some written as a judge . . . .
Samsung follows

case AMOTION_EVENT_ACTION_DOWN:
calculateXY (x, y, & abs_x, & abs_y);
write_event (ei.fd_touch, 3, 57, 0);
write_event (ei.fd_touch, 3, 53, abs_x);
write_event (ei.fd_touch, 3, 54, abs_y);
write_event (ei.fd_touch, 3, 48, 20);
write_event (ei.fd_touch, 0, 0, 0);
break;
case AMOTION_EVENT_ACTION_UP:
write_event (ei.fd_touch, 3, 57, -1);
write_event (ei.fd_touch, 0, 0, 0);
break;
case AMOTION_EVENT_ACTION_MOVE:
calculateXY (x, y, & abs_x, & abs_y);
write_event (ei.fd_touch, 3, 53, abs_x);
write_event (ei.fd_touch, 3, 54, abs_y);
write_event (ei.fd_touch, 3, 48, 20);
write_event (ei.fd_touch, 0, 0, 0);
break;



another device
case AMOTION_EVENT_ACTION_DOWN:
calculateXY (x, y, & abs_x, & abs_y);
write_event (ei.fd_touch, 3, 53, abs_x);
write_event (ei.fd_touch, 3, 54, abs_y);
write_event (ei.fd_touch, 3, 48, 1);
write_event (ei.fd_touch, 0, 2, 0);
write_event (ei.fd_touch, 0, 0, 0);
break;
case AMOTION_EVENT_ACTION_UP:
write_event (ei.fd_touch, 0, 2, 0);
write_event (ei.fd_touch, 0, 0, 0);
break;
case AMOTION_EVENT_ACTION_MOVE:
calculateXY (x, y, & abs_x, & abs_y);
write_event (ei.fd_touch, 3, 53, abs_x);
write_event (ei.fd_touch, 3, 54, abs_y);
write_event (ei.fd_touch, 3, 48, 1);
write_event (ei.fd_touch, 0, 2, 0);
write_event (ei.fd_touch, 0, 0, 0);
break;

没有评论:

发表评论