class my_mouse_class
: public QThread{
Q_OBJECT
private:
int f_mouse;
protected:
virtual void run() //logic here. poll for data, if found, emit a signal.
{
fd_set rfds;
struct timeval tv;
int retval;
while ( isRunning() )
{
/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds);
FD_SET(0, &rfds);
/* Wait up to five microseconds. */
tv.tv_sec = 0;
tv.tv_usec = 5;
retval = select( f_mouse, &rfds, NULL, NULL, &tv );
/* Don’t rely on the value of tv now! */
if ( retval == -1 )
{
perror( "select()" );
}
else
{
if ( retval )
{
printf( "Data is available now.\n" );
/* FD_ISSET(0, &rfds) will be true. */
emit data_available();
}
else
{
printf( "No data within five seconds.\n" );
}
}
}
}
public:
/**
Basic constructor, tries to open the device.
*/
{
f_mouse = open( "/dev/mouse", O_RDONLY );
if ( f_mouse < 3 ) // 1 and 2 are standart output and error output
{
throw DeviceException( "my_mouse_class::my_mouse_class(): Mouse not available." );
}
}
~my_mouse_class()
{
if ( f_mouse > 2 ) //sanity check
{
close( f_mouse ); //close the file descriptor
}
}
//read some data
ssize_t read( void * buffer, ssize_t count )
{
return ::read( f_mouse, buffer, count );
}
signals:
void data_available();
};
class my_mouse_class: public QThread
{
Q_OBJECT
private:
int f_mouse;
protected:
virtual void run() //logic here. poll for data, if found, emit a signal.
{
fd_set rfds;
struct timeval tv;
int retval;
while ( isRunning() )
{
/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds);
FD_SET(0, &rfds);
/* Wait up to five microseconds. */
tv.tv_sec = 0;
tv.tv_usec = 5;
retval = select( f_mouse, &rfds, NULL, NULL, &tv );
/* Don’t rely on the value of tv now! */
if ( retval == -1 )
{
perror( "select()" );
}
else
{
if ( retval )
{
printf( "Data is available now.\n" );
/* FD_ISSET(0, &rfds) will be true. */
emit data_available();
}
else
{
printf( "No data within five seconds.\n" );
}
}
}
}
public:
/**
Basic constructor, tries to open the device.
*/
my_mouse_class( QObject * parent ): QThread( parent )
{
f_mouse = open( "/dev/mouse", O_RDONLY );
if ( f_mouse < 3 ) // 1 and 2 are standart output and error output
{
throw DeviceException( "my_mouse_class::my_mouse_class(): Mouse not available." );
}
}
~my_mouse_class()
{
if ( f_mouse > 2 ) //sanity check
{
close( f_mouse ); //close the file descriptor
}
}
//read some data
ssize_t read( void * buffer, ssize_t count )
{
return ::read( f_mouse, buffer, count );
}
signals:
void data_available();
};
To copy to clipboard, switch view to plain text mode
Bookmarks