📄 lircmd.c
字号:
int x,y,z,up,down,toggle; x=config_table[i].x; y=config_table[i].y; z=config_table[i].z; down=config_table[i].down; up=config_table[i].up; toggle=config_table[i].toggle; if(x || y || z) { mouse_move(x,y,z,rep); } if(toggle) { /* assert(down==up); assert(up==BUTTON1 || up==BUTTON2 || up==BUTTON3); */ if(ms.buttons[map_buttons(up)]==button_up) mouse_button(down,0,rep); else mouse_button(0,up,rep); } else { if(down && up) /* click */ { mouse_button(down,0,rep);#ifdef CLICK_DELAY usleep(CLICK_DELAY);#endif mouse_button(0,up,rep); } else if(down || up); { mouse_button(down,up,rep); } } break; } } } found=1; tm=tm->tm_next; } if(found==0) { if(ms.active==1 && ms.always_active==0 && ms.toggle_active==0) { deactivate(); } }}struct trans_mouse *read_config(FILE *fd){ char buffer[PACKET_SIZE]; char *directives,*remote,*button; enum directive d; int len; int line; struct trans_mouse *tm_new,*tm_list,*tm_last=NULL; tm_list=NULL; new_ms=ms; new_ms.always_active=1; new_ms.toggle_active=0; line=0; while(fgets(buffer,PACKET_SIZE,fd)!=NULL) { line++; len=strlen(buffer); if(len==PACKET_SIZE-1 && buffer[len-1]!='\n') { syslog(LOG_ERR,"line %d too long in config file", line); freetm(tm_list); return((void *) -1); } len--; if(buffer[len]=='\n') buffer[len]=0; /* ignore comments */ if(buffer[0]=='#') continue; directives=strtok(buffer,WHITE_SPACE); /* ignore empty lines */ if(directives==NULL) continue; if(strcasecmp("PROTOCOL",directives)==0) { char *name; name=strtok(NULL,WHITE_SPACE); if(name!=NULL) { if(strcasecmp("MouseSystems",name)==0) { new_ms.protocol=mouse_systems; } else if(strcasecmp("IMPS/2",name)==0) { new_ms.protocol=imps_2; } else if(strcasecmp("IntelliMouse",name)==0) { new_ms.protocol=im_serial; } else { syslog(LOG_WARNING, "unknown protocol %s",name); } } if(name==NULL || strtok(NULL,WHITE_SPACE)!=NULL) { syslog(LOG_WARNING, "invalid line %d in config file " "ignored",line); continue; } continue; } if(strcasecmp("ACCELERATOR",directives)==0) { char *number; number=strtok(NULL,WHITE_SPACE); if(number!=NULL) new_ms.acc_start=atoi(number); number=strtok(NULL,WHITE_SPACE); if(number!=NULL) new_ms.acc_max=atoi(number); number=strtok(NULL,WHITE_SPACE); if(number!=NULL) new_ms.acc_fak=atoi(number); if(strtok(NULL,WHITE_SPACE)!=NULL) { syslog(LOG_WARNING, "invalid line %d in config file " "ignored",line); new_ms.acc_start=ms.acc_start; new_ms.acc_max=ms.acc_max; new_ms.acc_fak=ms.acc_fak; continue; } continue; } remote=strtok(NULL,WHITE_SPACE); button=strtok(NULL,WHITE_SPACE); if(remote==NULL || button==NULL || strtok(NULL,WHITE_SPACE)!=NULL) { syslog(LOG_WARNING, "invalid line %d in config file ignored", line); continue; } if(strcasecmp("ACTIVATE",directives)==0) { d=mouse_activate; new_ms.always_active=0; } else if(strcasecmp("TOGGLE_ACTIVATE",directives)==0) { d=mouse_toggle_activate; new_ms.always_active=0; } else { int i; d=mouse_activate; /* make compiler happy */ for(i=0;config_table[i].string!=NULL;i++) { if(strcasecmp(config_table[i].string, directives)==0) { d=config_table[i].d; break; } } if(config_table[i].string==NULL) { syslog(LOG_WARNING, "unknown directive \"%s\" ignored", directives); continue; } } if(strcmp("*",remote)==0) remote=ALL; else remote=strdup(remote); if(strcmp("*",button)==0) button=ALL; else button=strdup(button); tm_new=malloc(sizeof(struct trans_mouse)); if(remote==NULL || button==NULL || tm_new==NULL) { syslog(LOG_ERR,"out of memory"); if(remote!=NULL) free(remote); if(button!=NULL) free(button); if(tm_new!=NULL) free(tm_new); free(tm_list); return((void *) -1); } tm_new->tm_next=NULL; tm_new->tm_remote=remote; tm_new->tm_button=button; tm_new->tm_directive=d; if(tm_list==NULL) { tm_list=tm_new; tm_last=tm_new; } else { tm_last->tm_next=tm_new; tm_last=tm_new; } } return(tm_list);}void loop(){ ssize_t len=0; char buffer[PACKET_SIZE+1]; int rep,ret; char button[PACKET_SIZE+1]; char remote[PACKET_SIZE+1]; char *end; int end_len=0; sigset_t block; sigemptyset(&block); sigaddset(&block,SIGHUP); buffer[0]=0; while(1) { if(hup) { dohup(); hup=0; } if(strchr(buffer,'\n')==NULL) { sigprocmask(SIG_UNBLOCK,&block,NULL); len=read(lircd,buffer+end_len,PACKET_SIZE-end_len); sigprocmask(SIG_BLOCK,&block,NULL); if(len<=0) { if(len==-1 && errno==EINTR) continue; raise(SIGTERM); } } buffer[len+end_len]=0; ret=sscanf(buffer,"%*llx %x %s %s\n",&rep,button,remote); end=strchr(buffer,'\n'); if(end==NULL) { end_len=0; continue; } end++; end_len=strlen(end); memmove(buffer,end,end_len+1); if(ret==3) { mouse_conv(rep,button,remote); } } }int main(int argc,char **argv){ FILE *fd; struct sigaction act; struct sockaddr_un addr; sigset_t block; int nodaemon=0; while(1) { int c; static struct option long_options[] = { {"help",no_argument,NULL,'h'}, {"version",no_argument,NULL,'v'}, {"nodaemon",no_argument,NULL,'n'}, {0, 0, 0, 0} }; c = getopt_long(argc,argv,"hvn",long_options,NULL); if(c==-1) break; switch (c) { case 'h': printf("Usage: %s [options] [config-file]\n",progname); printf("\t -h --help\t\tdisplay this message\n"); printf("\t -v --version\t\tdisplay version\n"); printf("\t -n --nodaemon\t\tdon't fork to background\n"); return(EXIT_SUCCESS); case 'v': printf("%s\n",progname); return(EXIT_SUCCESS); case 'n': nodaemon=1; break; default: printf("Usage: %s [options] [config-file]\n",progname); return(EXIT_FAILURE); } } if(optind==argc-1) { configfile=argv[optind]; } else if(optind!=argc) { fprintf(stderr,"%s: invalid argument count\n",progname); return(EXIT_FAILURE); } /* connect to lircd */ addr.sun_family=AF_UNIX; strcpy(addr.sun_path,LIRCD); lircd=socket(AF_UNIX,SOCK_STREAM,0); if(lircd==-1) { fprintf(stderr,"%s: could not open socket\n",progname); perror(progname); exit(EXIT_FAILURE); }; if(connect(lircd,(struct sockaddr *)&addr,sizeof(addr))==-1) { fprintf(stderr,"%s: could not connect to socket\n",progname); perror(progname); exit(EXIT_FAILURE); }; /* open fifo */ if(mkfifo(LIRCM,0644)==-1) { if(errno!=EEXIST) { fprintf(stderr,"%s: could not create fifo\n",progname); perror(progname); exit(EXIT_FAILURE); } } lircm=open(LIRCM,O_RDWR|O_NONBLOCK); if(lircm==-1) { fprintf(stderr,"%s: could not open fifo\n",progname); perror(progname); exit(EXIT_FAILURE); } /* read config file */ fd=fopen(configfile,"r"); if(fd==NULL) { fprintf(stderr,"%s: could not open config file\n",progname); perror(progname); exit(EXIT_FAILURE); } tm_first=read_config(fd); fclose(fd); if(tm_first==(void *) -1) { fprintf(stderr,"%s: reading of config file failed\n",progname); exit(EXIT_FAILURE); } else { ms=new_ms; }#ifdef DAEMONIZE if(!nodaemon) daemonize();#endif openlog(progname,LOG_CONS,LOG_DAEMON); signal(SIGPIPE,SIG_IGN); act.sa_handler=sigterm; sigfillset(&act.sa_mask); act.sa_flags=SA_RESTART; /* don't fiddle with EINTR */ sigaction(SIGTERM,&act,NULL); sigaction(SIGINT,&act,NULL); /* block SIGHUP first */ sigemptyset(&block); sigaddset(&block,SIGHUP); sigprocmask(SIG_BLOCK,&block,NULL); act.sa_handler=sighup; sigemptyset(&act.sa_mask); act.sa_flags=0; /* need EINTR in loop() */ sigaction(SIGHUP,&act,NULL); loop(); /* never reached */ return(EXIT_SUCCESS);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -