編輯:關於Android編程
1、main函數,setup_uinput_device 完成設備的注冊,然後創建一個線程 VirtualInputDev_EventThread,該線程重復發出keycode;
int main()
{
printf("Enter process !!!! \n");
stVirtualInputDevData *pKpdData = (stVirtualInputDevData*) malloc(sizeof(stVirtualInputDevData));
pKpdData->min_keycode = umin_keycode;
pKpdData->max_keycode = umax_keycode;
if (setup_uinput_device(pKpdData) < 0) {
printf("Unable to find uInput device\n");
free(pKpdData);
return -1;
}
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (0 != pthread_create(&keypad_EventThreadId, &attr, VirtualInputDev_EventThread, (void *)0)) {
printf("Create KeypadEventThread Failed!!\n");
exit(1);
}
// Coverity server need set to ignore this.
while (1) {
usleep(1000000); // sleep 1 second
}
free(pKpdData);
pKpdData = 0;
// Destroy the device
ioctl(uinp_fd, UI_DEV_DESTROY);
close(uinp_fd);
return 0;
}
int setup_uinput_device(stVirtualInputDevData* mstVirtualInputDevData)
{
struct uinput_user_dev uinp; // uInput device structure
int i;
// Open the input device
uinp_fd = open("/dev/uinput", O_WRONLY | O_NDELAY);
if (uinp_fd == 0) {
printf("Unable to open /dev/uinput\n");
return -1;
}
// Intialize the uInput device to NULL
memset(&uinp, 0x00, sizeof(uinp));
strncpy(uinp.name, "virtualinputdev", sizeof(uinp.name)-1);
uinp.id.vendor = 0x1341;
uinp.id.product = 0x0001;
uinp.id.bustype = BUS_VIRTUAL;
// Keyboard
ioctl(uinp_fd, UI_SET_EVBIT, EV_KEY);
for (i = mstVirtualInputDevData->min_keycode; i < mstVirtualInputDevData->max_keycode; i++) {
ioctl(uinp_fd, UI_SET_KEYBIT, i);
}
// Create input device into input sub-system
if (write(uinp_fd, &uinp, sizeof(uinp)) != sizeof(uinp)) {
printf("First write returned fail.\n");
return -1;
}
if (ioctl(uinp_fd, UI_DEV_CREATE)) {
printf("ioctl UI_DEV_CREATE returned fail.\n");
return -1;
}
return 1;
} static void* VirtualInputDev_EventThread(void *driver_data)
{
unsigned char u8Keycode,i=umin_keycode;
while (1) {
u8Keycode = 0xff;
/* sleep an interval time */
usleep(2000000);//sleep 5 s
/* fill event to uinput device. */
write_event_to_device(i++, 0);
if(i==4){
i = 0;
}
printf ("virtualinputdev thread ...\n");
//i %= umax_keycode;
}
printf ("virtualinputdev thread died\n");
pthread_exit(0);
return 0;
} void write_event_to_device(unsigned char u8KeyCode, unsigned char u8Repeat)
{
struct input_event event; // Input device structure
struct timespec s;
s.tv_nsec = 5000000L;
s.tv_sec = 0;
memset(&event, 0x00, sizeof(event));
gettimeofday(&event.time, 0);
event.type = EV_KEY;
event.code = u8KeyCode;
event.value = 1;
write(uinp_fd, &event, sizeof(event));
memset(&event, 0x00, sizeof(event));
gettimeofday(&event.time, 0);
event.type = EV_KEY;
event.code = u8KeyCode;
event.value = 0;
write(uinp_fd, &event, sizeof(event));
memset(&event, 0x00, sizeof(event));
gettimeofday(&event.time, 0);
event.type = EV_SYN;
event.code = SYN_REPORT;
event.value = 0;
write(uinp_fd, &event, sizeof(event));
}
Android - 五子連珠
繪制棋盤面板:MainActivity.javapackage com.xbmu.wuziqi;import android.support.v7.app.AppComp
RxJava2 淺析
前段時間閱讀了RxJava1.x的源碼,剛好RxJava2.x也發布了RC版,為了迎接10月底的正式版,趁熱打鐵,本篇將對RxJava2.x進行一個簡單的剖析。Obser
圖片會說話系列之Android事件的分發傳遞機制
在一些復雜布局中,經常會遇到事件沖突,事件失效等問題,這就需要我們深入理解Android事件的分發傳遞機制。最好的方法是自己寫一個demo,打印事件相關的日志查看其運行流
Android對話框(四)PopupWindow
主布局 Popup對話框布局 package com.example.popupwindow; import