gpt4 book ai didi

c - 替换 read(int fd, void *buf, size_t count); Linux 中的晶圆?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:41:47 25 4
gpt4 key购买 nike

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/input.h>

#define MOUSEFILE "/dev/input/mice"

int old_x=0,old_y=0;
char x,y;
int change(int X,int Y)
{
if(old_x==X&&old_y==Y)
{

printf("Mouse is Still \n ");
}
else
{

old_x=X;
old_y=Y;
printf("Mouse is Moving \n");
}

}
int main()
{
int fd;
struct input_event ie;
unsigned char button,bLeft,bMiddle,bRight;
int Mouse_status=0;
int absolute_x=0,absolute_y=0;

if((fd = open(MOUSEFILE, O_RDONLY)) == -1)
{
printf("Device open ERROR\n");
exit(EXIT_FAILURE);
}
else
{
printf("Device open OK\n");
}
printf("right-click to set absolute x,y coordinates origin (0,0)\n");
while(1)
{

read(fd, &ie, sizeof(struct input_event));
unsigned char *ptr = (unsigned char*)&ie;
button=ptr[0];
bLeft = button & 0x1;
bMiddle = ( button & 0x4 ) ;
bRight = ( button & 0x2 ) ;
x=(char) ptr[1];y=(char) ptr[2];

system("clear");
change(x,y);
if(bLeft)
{
printf("bLEFT bMIDDLE bRIGHT \n ");
printf("DOWN UP UP \n ");
}
else if(bMiddle)
{
printf("bLEFT bMIDDLE bRIGHT \n ");
printf("UP DOWN UP \n ");
}
else if(bRight)
{
printf("bLEFT bMIDDLE bRIGHT \n ");
printf("UP UP DOWN \n ");
}
else if(bLeft==0&&bMiddle==0&&bRight==0)
{
printf("bLEFT bMIDDLE bRIGHT \n ");
printf("UP UP UP \n ");
}
absolute_x+=x;
absolute_y-=y;

printf("Absolute coords from TOP_LEFT= %d %d\n",absolute_x,absolute_y);
//printf("OLD_X %d and OLD_Y %d \n",old_x,old_y);
//printf("X is %d ,Y is %d \n",x,y);
// comment to disable the display of raw event structure datas
//
//for(i=0; i<sizeof(ie); i++)
// {
// printf("%02X ", *ptr++);
// }

}
close(fd);
return 0;
}

在上面的程序中,我使用了 read wafer,这是一个阻塞调用(它会等到我给出一些输入值)。是否有任何其他函数可以执行与 read wafer 相同的操作,不应阻塞.

最佳答案

你可以简单的设置为非阻塞

int flags = fcntl(fd, F_GETFL, 0); 
fcntl(fd, F_SETFL, flags | O_NONBLOCK);

看看this例如,供引用。

关于c - 替换 read(int fd, void *buf, size_t count); Linux 中的晶圆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41952911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com