cgi - 停止阅读输入

时间:2014-09-15 08:50:20

标签: linux timer cgi

我试图通过cgi脚本表示鼠标移动。 试过这个:

file=fopen(MOUSEFILE, "r"); 
while((fread( &ie, sizeof(struct input_event),1,file)==0) && (i<1000)){i++;} 
fclose(file);

if(i==1000){
  printf("<IMG SRC=\"../images/mouse.bmp\">"); 
}else{
  if(ie.type==2 && ie.code==0 && ie.value==-1){
    printf("<IMG SRC=\"../images/left.bmp\"");
  }else if(ie.type==2 && ie.code==0 && ie.value==1){
    printf("<IMG SRC=\"../images/right.bmp\"");
  }else if(ie.type==1 && ie.code==272 && ie.value==1){
    printf("<IMG SRC=\"../images/pressed.bmp\">");
  }
}
如果移动鼠标或正在进行某些操作,

工作正常,但如果鼠标处于非活动状态,则页面会因等待fread而死亡。如何在500ms后停止执行fread - 1s ??? 或者我怎样才能获得解决方法?

1 个答案:

答案 0 :(得分:0)

找到了解决方法:

file = open(MOUSEFILE, O_RDONLY | O_NDELAY);
while(  (1>read(file, &ie, sizeof(struct input_event))) && (i<50000)  ){i++;}
close(file);
if(i==50000){
  printf("<IMG SRC=\"../images/mouse.bmp\">"); 
}else{ ...

O_NDELAY 并不关心文件是否准备就绪,并在第一次尝试后中止读取。 所以代码尝试 50000 次并在此之后退出