- 论坛徽章:
- 0
|
当我把管道和鼠标设备的描述符用select控制的时候,select只能检测到鼠标的数据,此时
我无论如何向管道写数据都收不到。难道select不能监视硬件设备文件描述符?
很急啊,我都调试了一天了,就是不知道原因在哪里?望大家帮忙!俺先谢谢了。
代码如下:
#include "/debug/gcc4_debug.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <termios.h>
#include <linux/kd.h>
#include <errno.h>
#include <pthread.h>
/* Mouse button bits*/
#define MSG_MS_ON 0x01
#define MSG_KB_ON 0x02
#define KEY_DEV "/dev/tty"
#define MOUSE_DEV "/dev/psaux"
#define TRAN_DEV "/dev/ttyS0"
static int m_pipe_fd[2];
static int m_ms_fd = -1;
static int mouse_init ();
static void mouse_uninit ();
static int pipe_init ();
static void pipe_uninit ();
static void set_status (char status);
static void thread_send (void *param);
static int mouse_init ()
{
unsigned char IMPS2_Param[] = { 243, 200, 243, 100, 243, 80 };
if ((m_ms_fd = open (MOUSE_DEV, O_RDWR)) < 0)
{
perror ("open");
exit (1);
}
//write (m_ms_fd, IMPS2_Param, sizeof (IMPS2_Param));
return 0;
}
static void mouse_uninit ()
{
if (m_ms_fd != -1)
close (m_ms_fd);
}
static int pipe_init ()
{
if (pipe (m_pipe_fd) < 0)
{
perror ("pipe");
exit (1);
}
return 0;
}
static void pipe_uninit ()
{
close (m_pipe_fd[0]);
close (m_pipe_fd[1]);
}
void set_status (char status)
{
if (write (m_pipe_fd[1], &status, 1) != 1)
{
printf ("write to pipe error!\n");
perror ("write");
}
}
static void thread_send (void *param)
{
char cmd;
int ret;
int maxfd;
fd_set rset;
unsigned int status;
status = 0;
FD_ZERO (&rset);
maxfd = m_pipe_fd[0];
maxfd = MAX (maxfd, m_ms_fd);
maxfd = maxfd + 1;
FD_SET (m_pipe_fd[0], &rset);
FD_SET (m_ms_fd, &rset);
while (1)
{
cmd = 0;
printf ("Select now...\n");
if (select (maxfd, &rset, NULL, NULL, NULL) < 0)
{
printf ("select ...\n");
if (errno == EINTR)
continue;
else
perror ("select");
}
if (FD_ISSET (m_pipe_fd[0], &rset))
{
printf ("Read a cmd\n");
ret = read (m_pipe_fd[0], &cmd, 1);
if (ret != 1)
{
perror ("read");
exit (0);
}
}
char buffer[1024];
int n;
if (FD_ISSET (m_ms_fd, &rset))
{
n = read (m_ms_fd, buffer, 1024);
printf ("Read form ms -%d \n",n);
}
switch (cmd)
{
case 'm':
status |= MSG_MS_ON;
break;
case 's':
status &= (~MSG_MS_ON);
break;
case 'k':
status |= MSG_KB_ON;
break;
case 'b':
status &= (~MSG_KB_ON);
break;
default:
break;
}
}
}
int main (int argc, char **argv)
{
pthread_t thread;
int ret;
char c;
pipe_init ();
mouse_init ();
ret = pthread_create (&thread, NULL, (void *) &thread_send, NULL);
if (ret != 0)
{
printf ("Create msg server error!\n");
perror ("thread create");
return ret;
}
c = '?';
while (c != 27)
{
switch (c)
{
case '?':
printf ("Usage: Please input a char\n"
"\t?---print help\n"
"\tm---mouse send on\n"
"\ts---mouse send off\n"
"\tk---keyboard send on\n"
"\tb---keyboard send off\n");
break;
case 'm':
set_status ('m');
break;
case 's':
set_status ('s');
break;
case 'k':
set_status ('k');
break;
case 'b':
set_status ('b');
break;
default:
break;
}
c = getchar ();
}
pipe_uninit ();
mouse_uninit ();
return 0;
}
|
|
|