void sendkey(int fd, int key, int value) { struct input_event oev; if (value && (SHIFT & key)) { sendkey(fd, KEY_LEFTSHIFT, 1); } memset(&oev, 0, sizeof(struct input_event)); oev.type = EV_KEY; oev.code = key&KEYMASK; oev.value = value; if(write(fd, &oev, sizeof(struct input_event)) < 0) die("error: write"); if (!value && (SHIFT & key)) { sendkey(fd, KEY_LEFTSHIFT, 0); } }
void veta_handleevent(event_t *event){ switch(event->type){ case QUIT: uk_log("Quit"); ui_quit(); break; case RESET: uk_log("Reset"); clear_selection(root_cell); veta_render(); break; case SELECT_CELL: uk_log("select cell %i",event->cell); // debug_print_tree(root); select_cell(root_cell,event->cell); symbol *sym; if(NULL!=(sym=get_selected_symbol(root_cell))){ clear_selection(root_cell); if(ui2_onselect_symbol(sym)) break; sendkey(sym->data,1,0); } refresh(); break; case UNDEFINED: uk_log("got event UNDEFINED\n"); break; default: uk_log("WARNING undefined event type"); break; } }
void keyboard_state_machine(unsigned char scancode) { /* this will process the incoming scancodes, translate them to * a character stream, and write them to the masterfd of the * current pty. */ bool release = false; if(!scancode) return; /* first, take care of protocol codes */ switch(scancode) { case 0xee: case 0xf1: case 0xfa: return; case 0xfc: case 0xfd: case 0xfe: case 0xff: syslog(LOG_ERR, "internal keyboard error: %x\n", scancode); return; } /* TODO: actually handle escaped codes */ if(scancode == 0xe0) return; if(scancode & 0x80) { release = true; scancode -= 0x80; } unsigned int key = 0; switch(scancode) { case 0x1d: ctrl = !release; break; case 0x2a: shift = !release; break; case 0x38: alt = !release; break; case 0x3a: capslock = !capslock; break; default: if(shift) key = us_map_shift[scancode]; else if(ctrl) key = us_map_ctrl[scancode]; else key = us_map[scancode]; if(shift ^ capslock) key = toupper(key); if(key == 0) syslog(LOG_ERR, "unknown scancode: %x (%d %d %d)\n", scancode, shift, ctrl, alt); else if(!release && key < 256) { sendkey((unsigned char)key); } else if(!release) { special_key(key); } } }
int askuser(char *params) { Attr *attr; fmtinstall('A', _attrfmt); attr = promptforkey(params); if(attr == nil) sysfatal("no key supplied"); if(sendkey(attr) < 0) sysfatal("sending key to factotum: %r"); return 0; }
void release_keys(int fd, int input_fd) { int i, j; unsigned char keys[KEY_MAX/8 + 1]; memset(keys, 0, sizeof keys); ioctl(fd, EVIOCGKEY(sizeof keys), keys); for(i = 0; i < sizeof keys; i++) { for(j = 0; j < 8; j++) { if(keys[i] & (1 << j)) { sendkey(input_fd, i*8 + j, 0); } } } }
int main(void) { int fd; int ffd; int e1fd; int nfds; FILE *conf; struct uinput_user_dev uidev; fd_set fds; struct timeval tv1; daemon(0, 0); profiles = NULL; addprofile("org.mozilla.firefox", KEY_SPACE, SHIFT|KEY_SPACE, k_back, k_menu); addprofile("com.android.browser", KEY_SPACE, SHIFT|KEY_SPACE, k_back, k_menu); // addprofile("com.quoord.tapatalkpro.activity", KEY_DOWN, KEY_UP, k_back, k_menu); // addprofile("com.sec.android.app.camera", 212, 212, k_back, 212); // defaultprofile = addprofile("default", KEY_VOLUMEDOWN, KEY_VOLUMEUP, k_back, k_menu); defaultprofile = addprofile("default", KEY_LEFT, KEY_RIGHT, k_back, k_menu); gettimeofday(&tv1,NULL); setpids(); gettimeofday(&lastreadpids,NULL); // fprintf(stderr, "took %i microsec\n", // (lastreadpids.tv_sec-tv1.tv_sec)*1000000+lastreadpids.tv_usec-tv1.tv_usec); // fprintf(stderr, " %i %i %i\n", profiles->pid, profiles->next->pid, profiles->next->next->pid); fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK); if(fd < 0) die("error: open"); if(ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0) die("error: ioctl"); #if 0 for(i=0;i<500;i++) { if(ioctl(fd, UI_SET_KEYBIT, i) < 0) die("error: ioctl"); } #else ioctl(fd, UI_SET_KEYBIT, KEY_SPACE); ioctl(fd, UI_SET_KEYBIT, KEY_LEFTSHIFT); ioctl(fd, UI_SET_KEYBIT, KEY_VOLUMEDOWN); ioctl(fd, UI_SET_KEYBIT, KEY_VOLUMEUP); ioctl(fd, UI_SET_KEYBIT, KEY_DOWN); ioctl(fd, UI_SET_KEYBIT, KEY_UP); ioctl(fd, UI_SET_KEYBIT, KEY_RIGHT); ioctl(fd, UI_SET_KEYBIT, KEY_LEFT); ioctl(fd, UI_SET_KEYBIT, KEY_PAGEDOWN); ioctl(fd, UI_SET_KEYBIT, KEY_PAGEUP); ioctl(fd, UI_SET_KEYBIT, KEY_SEND); ioctl(fd, UI_SET_KEYBIT, KEY_CAMERA); #endif conf = fopen("/sdcard/.pgdn","r"); if(conf) { char buf[1024]; while(fgets(buf,1024,conf)) { int i,j,k; char pname[256]; int p_left, p_right, p_back, p_menu; if(sscanf(buf, "map %i", &j)==1) { if(j==k_back)MAP_back=1; if(j==k_menu)MAP_menu=1; } if(sscanf(buf, "key %i", &j)==1) { ioctl(fd, UI_SET_KEYBIT, j); } if(sscanf(buf, "keys %i %i", &j, &k)==2) { for(i=j;i<=k;i++) ioctl(fd, UI_SET_KEYBIT, i); } if(sscanf(buf, "profile %255s %i %i %i %i", pname, &p_left, &p_right, &p_back, &p_menu)==3+MAP_back+MAP_menu) { addprofile(pname, p_left, p_right, p_back, p_menu); } } fclose(conf); } mkfifo(FIFONAME, 0777); ffd=open(FIFONAME, O_RDONLY|O_NDELAY); if (ffd < 0) die("error: open fifo"); memset(&uidev, 0, sizeof(uidev)); snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-virtual-keyboard"); uidev.id.bustype = BUS_USB; uidev.id.vendor = 0x04E8; uidev.id.product = 0x7021; uidev.id.version = 1; if(write(fd, &uidev, sizeof(uidev)) < 0) die("error: write"); if(ioctl(fd, UI_DEV_CREATE) < 0) die("error: ioctl"); e1fd = open("/dev/input/event0", O_RDONLY); if(e1fd<0) die("error: open /dev/input/event0"); nfds = (ffd>e1fd ? ffd : e1fd) +1; struct profile *curprofile = defaultprofile; while(1) { struct input_event ev[64]; int r,i; FD_ZERO(&fds); FD_SET(ffd, &fds); FD_SET(e1fd, &fds); r=select(nfds, &fds, NULL, NULL, NULL); if((r>0)&& FD_ISSET(e1fd,&fds)) { int rd; rd = read(e1fd, ev, sizeof(struct input_event) * 64); for(i=0;i*sizeof(struct input_event)<rd;i++) { if((ev[i].type == EV_KEY) && (ev[i].value == 1) && ((ev[i].code == k_left) || (ev[i].code == k_right) || (MAP_back && (ev[i].code==k_back)) || (MAP_menu && (ev[i].code==k_menu))) ) { struct timeval tv; gettimeofday(&tv,NULL); if(tv.tv_sec-5> lastreadpids.tv_sec) { setpids(); gettimeofday(&lastreadpids,NULL); } curprofile=profiles; while(curprofile) { if(curprofile->pid>0) { char fname[256]; FILE *f; snprintf(fname,255,"/proc/%i/oom_adj", curprofile->pid); f=fopen(fname,"r"); if(f) { int oom_adj; fread(fname, 255, 1, f); fclose(f); if(isdigit(fname[0])||fname[0]=='-') { oom_adj = atoi(fname); if(oom_adj<=0) { break; } } } } curprofile=curprofile->next; } if(!curprofile)curprofile=defaultprofile; // fprintf(stderr, "profile: %s\n", curprofile->name); } if(ev[i].type==EV_KEY) { if(ev[i].code == k_left) { sendkey(fd, curprofile->t_left, ev[i].value); } if(ev[i].code == k_right) { sendkey(fd, curprofile->t_right, ev[i].value); } if(MAP_back && (ev[i].code == k_back)) { sendkey(fd, curprofile->t_back, ev[i].value); } if(MAP_menu && (ev[i].code == k_menu)) { sendkey(fd, curprofile->t_menu, ev[i].value); } } } } else if((r>0)&& FD_ISSET(ffd,&fds)) { char buf[1024]; char pname[256]; int p_left, p_right, p_back, p_menu; int rd; char *n; rd = read(ffd, buf, 1024); if(rd == 0) { close(ffd); ffd=open(FIFONAME, O_RDONLY|O_NDELAY); nfds = (ffd>e1fd ? ffd : e1fd) +1; } else if((rd>4) && (strncmp(buf, "map ", 4)==0)) { int i; i=strtol(buf+4,&n, 0); if(i==k_left) { int m; m=0; if(rd>n-buf) { m=strtol(n, NULL, 0); } defaultprofile->t_left = m; } if(i==k_right) { int m; m=0; if(rd>n-buf) { m=strtol(n, NULL, 0); } defaultprofile->t_right = m; } } if((rd>5) && (strncmp(buf, "send ", 5)==0)) { int i; i=strtol(buf+5,&n, 0); if(i>0) { sendkey(fd,i,1); sendkey(fd,i,0); } } if((rd>10) && (sscanf(buf, "profile %255s %i %i %i %i", pname, &p_left, &p_right, &p_back, &p_menu)==3+MAP_back+MAP_menu)) { addprofile(pname, p_left, p_right, p_back, p_menu); } } } if(ioctl(fd, UI_DEV_DESTROY) < 0) die("error: ioctl"); close(e1fd); close(fd); close(ffd); unlink(FIFONAME); return 0; }
int main(int argc, char *argv[]) { int fd; struct fs_priv *fsp = malloc(sizeof(struct fs_priv)); struct fs_state fs1 = { .click_down1 = fs1_down1, .click_down2 = fs1_down2, .click_down3 = nop, .click_up1 = fs1_up1, .click_up2 = fs1_up2, .click_up3 = nop, .priv = fsp, }; struct fs_state fs2 = { .click_down1 = fs2_down1, .click_down2 = nop, .click_down3 = nop, .click_up1 = fs2_up1, .click_up2 = nop, .click_up3 = nop, .priv = fsp, }; struct vmouse_state *ms = &fsp->ms; ms->mouse_mode = 0; fsp->fs1 = &fs1; fsp->fs2 = &fs2; if(argc == 2) { fd = serial_open(argv[1]); } else { fd = serial_open("/dev/ttyUSB0"); } input_fd = uinput_open(); kfd = open_input("CATEX TECH. 104EC-Pro"); printf("wait...\n"); sleep(1); printf("begin...\n"); ioctl(kfd, EVIOCGRAB, 1); for(;;) { int status = serial_get_status(fd); fd_set rfds; struct timeval timeout = { .tv_sec = 0, .tv_usec = 1000000 / POLL_HZ }; FD_ZERO(&rfds); FD_SET(kfd, &rfds); select(kfd + 1, &rfds, NULL, NULL, &timeout); if(FD_ISSET(kfd, &rfds)) { struct input_event ev; if(read(kfd, &ev, sizeof(ev)) < 0) { perror("read"); abort(); } if(ms->mouse_mode) { vmouse_key(ms, &ev); } else { #if 0 if(ev.type == EV_KEY && ev.code == KEY_CAPSLOCK) sendkey(input_fd, K_WIN, ev.value); else write(input_fd, &ev, sizeof(ev)); #endif write(input_fd, &ev, sizeof(ev)); } } else { vmouse_run(ms); } fs_run(&fs1, status & S1); fs_run(&fs2, status & S2); } return 0; }
static void vmouse_key(struct vmouse_state *ms, struct input_event *ev) { if(ev->type == EV_KEY) { switch(ev->code) { case KEY_LEFTCTRL: case KEY_RIGHTCTRL: case KEY_LEFTALT: case KEY_RIGHTALT: case KEY_LEFTMETA: case KEY_RIGHTMETA: case KEY_LEFTSHIFT: case KEY_RIGHTSHIFT: sendkey(input_fd, ev->code, ev->value); break; case KEY_S: if(ev->value == 1) { ms->mx = -8; } else if(ev->value == 0) { ms->mx = 0; } break; case KEY_D: if(ev->value == 1) { ms->my = 8; } else if(ev->value == 0) { ms->my = 0; } break; case KEY_F: if(ev->value == 1) { ms->mx = 8; } else if(ev->value == 0) { ms->mx = 0; } break; case KEY_E: if(ev->value == 1) { ms->my = -8; } else if(ev->value == 0) { ms->my = 0; } break; case KEY_J: if(ev->value == 1) { sendkey(input_fd, BTN_LEFT, 1); } else if(ev->value == 0) { sendkey(input_fd, BTN_LEFT, 0); } break; case KEY_K: if(ev->value == 1) { sendkey(input_fd, BTN_MIDDLE, 1); } else if(ev->value == 0) { sendkey(input_fd, BTN_MIDDLE, 0); } break; case KEY_L: if(ev->value == 1) { sendkey(input_fd, BTN_RIGHT, 1); } else if(ev->value == 0) { sendkey(input_fd, BTN_RIGHT, 0); } break; case KEY_SPACE: if(ev->value == 1) { ms->mc = 1; } else if(ev->value == 0) { ms->mc = 0; } break; } } }
int main(int argc, char* argv[]) { char keyname[128]; int pointer_button, pointer_x, pointer_y; char windowname[64]; struct lirc_config* config; char* config_file = NULL; int c; unsigned int WindowID; while ((c = getopt_long(argc, argv, "dhV", long_options, NULL)) != EOF) { switch (c) { case 'd': bDaemon = 1; continue; case 'h': printf("Usage: %s [option]... [config file]\n" " -d --daemon fork and run in background\n" " -h --help display usage summary\n" " -V --version display version\n", prog); return EXIT_SUCCESS; case 'V': printf("%s %s\n", prog, VERSION); return EXIT_SUCCESS; case '?': fprintf(stderr, "unrecognized option: -%c\n", optopt); fprintf(stderr, "Try `%s --help' for more information.\n", prog); return EXIT_FAILURE; } } if (argc == optind + 1) { config_file = argv[optind]; } else if (argc > optind + 1) { fprintf(stderr, "%s: incorrect number of arguments.\n", prog); fprintf(stderr, "Try `%s --help' for more information.\n", prog); return EXIT_FAILURE; } dpy = XOpenDisplay(NULL); if (dpy == NULL) { fprintf(stderr, "Can't open DISPLAY.\n"); exit(1); } root = RootWindow(dpy, DefaultScreen(dpy)); // windows may get closed at wrong time. Override default error handler... XSetErrorHandler(errorHandler); if (lirc_init("irxevent", 1) == -1) exit(EXIT_FAILURE); if (lirc_readconfig(config_file, &config, check) == 0) { char* ir; char* c; int ret; if (bDaemon) { if (daemon(1, 0) < 0) { perror("Failed to run as daemon"); exit(EXIT_FAILURE); } } while (lirc_nextcode(&ir) == 0) { if (ir == NULL) continue; while ((ret = lirc_code2char(config, ir, &c)) == 0 && c != NULL) { log_debug("Received code: %s Sending:\n", ir); bInError = 0; // reset error state, want to see error msg *windowname = 0; if (2 == sscanf(c, "Key %s Focus WindowID %i", keyname, &WindowID) || 4 == sscanf(c, "Button %d %d %d Focus WindowID %i", &pointer_button, &pointer_x, &pointer_y, &WindowID) || 4 == sscanf(c, "xy_Key %d %d %s Focus WindowID %i", &pointer_x, &pointer_y, keyname, &WindowID) || 2 == sscanf(c, "Key %s Focus %s", keyname, windowname) || 4 == sscanf(c, "Button %d %d %d Focus %s", &pointer_button, &pointer_x, &pointer_y, windowname) || 4 == sscanf(c, "xy_Key %d %d %s Focus %s", &pointer_x, &pointer_y, keyname, windowname)) { log_debug("Focus\n"); /* focussed ? */ if (*windowname) { WindowID = find_window_focused(root, windowname); if (!WindowID) { log_debug("target window '%s' doesn't have focus\n", windowname); continue; } log_debug("focused: %s\n", windowname); } else { Window cur; int tmp; XGetInputFocus(dpy, &cur, &tmp); if (WindowID != cur) { log_debug("target window '0x%x' doesn't have focus\n", WindowID); continue; } log_debug("focused: 0x%x\n", WindowID); } } else if (2 == sscanf(c, "Key %s WindowID %i", keyname, &WindowID) || 4 == sscanf(c, "Button %d %d %d WindowID %i", &pointer_button, &pointer_x, &pointer_y, &WindowID) || 4 == sscanf(c, "xy_Key %d %d %s WindowID %i", &pointer_x, &pointer_y, keyname, &WindowID)) { log_debug("WindowID: 0x%x\n", WindowID); /* WindowID passed */ } else if (2 == sscanf(c, "Key %s %s", keyname, windowname) || 4 == sscanf(c, "Button %d %d %d %s", &pointer_button, &pointer_x, &pointer_y, windowname) || 4 == sscanf(c, "xy_Key %d %d %s %s\n", &pointer_x, &pointer_y, keyname, windowname)) { log_debug("name: %s\n", windowname); WindowID = find_window(root, windowname); if (WindowID == 0) { log_debug("target window '%s' not found\n", windowname); continue; } } switch (c[0]) { case 'K': // Key log_debug("keyname: %s \t WindowID: 0x%x\n", keyname, WindowID); log_debug("%s\n", c); sendkey(keyname, 1, 1, (Window)WindowID, 0); break; case 'B': // Button case 'x': // xy_Key subw = find_sub_window(root, windowname, &pointer_x, &pointer_y); if (subw) { if (WindowID == subw) subw = 0; log_debug("%s\n", c); switch (c[0]) { case 'B': /* FIXME: pointer_button potentially uninititalzed. */ sendbutton(pointer_button, pointer_x, pointer_y, WindowID, subw); break; case 'x': sendkey(keyname, pointer_x, pointer_y, WindowID, subw); break; } } break; } } free(ir); if (ret == -1) break; } lirc_freeconfig(config); } lirc_deinit(); exit(0); }
int main(int argc, char *argv[]) { char keyname[128]; int pointer_button,pointer_x,pointer_y; char windowname[64]; struct lirc_config *config; progname=argv[0]; if(argc>2) { fprintf(stderr,"Usage: %s <config file>\n",argv[0]); exit(1); }; dpy=XOpenDisplay(NULL); if(dpy==NULL) { fprintf(stderr,"Can't open DISPLAY.\n"); exit(1); } root=RootWindow(dpy,DefaultScreen(dpy)); if(lirc_init("irxevent",1)==-1) exit(EXIT_FAILURE); if(lirc_readconfig(argc==2 ? argv[1]:NULL,&config,check)==0) { char *ir; char *c; int ret; while(lirc_nextcode(&ir)==0) { if(ir==NULL) continue; while((ret=lirc_code2char(config,ir,&c))==0 && c!=NULL) { debugprintf("Recieved code: %sSending event: ",ir); if(2==sscanf(c,"Key %s %s\n",keyname,windowname)) { if((w=find_window(root,windowname))) { debugprintf("keyname: %s \t windowname: %s\n",keyname,windowname); sendkey(keyname,1,1,w,0); } else { debugprintf("target window '%s' not found \n",windowname); } } else if(4==sscanf(c,"Button %d %d %d %s\n", &pointer_button,&pointer_x, &pointer_y,windowname)) { if((w=find_window(root,windowname)) && (subw=find_sub_window(root,windowname,&pointer_x,&pointer_y))) { if (w==subw) subw=0; debugprintf(" %s\n",c); sendbutton(pointer_button,pointer_x,pointer_y,w,subw); } else { debugprintf("target window '%s' not found \n",windowname); } } else if(4==sscanf(c,"xy_Key %d %d %s %s\n", &pointer_x,&pointer_y, keyname,windowname)) { if((w=find_window(root,windowname))&& (subw=find_sub_window(root,windowname,&pointer_x,&pointer_y))) { debugprintf(" %s\n",c); if (w==subw) subw=0; sendkey(keyname,pointer_x,pointer_y,w,subw); } else { debugprintf("target window '%s' not found \n",windowname); } } } free(ir); if(ret==-1) break; } lirc_freeconfig(config); } lirc_deinit(); exit(0); }