M
Martin Holm Pedersen
Hey All..
Im having a bit of a problem with my program that i wrote for linux in c. I
use select() to monitor if the user has pressed a key and reads the key
with read(). It works fine om my IBM laptop but once i move the program to
my dell laptop it seems like it doesn't even recognize the select-function.
That is, it doesn't use the timeout assigned at all. I don't get any errors
when i compile on either computer. I run debian/testing on both laptops and
i use CVS for version-control.. I have tried to compile it on the IBM and
run it on the dell.. But nothing seems to work..
- Martin
The code is something along the lines of:
#include <sys/types.h>
#include <sys/stat.h>
#include <asm/io.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <time.h>
#include <termio.h>
#include <sys/ioctl.h>
struct termio OldTerm;
struct termio NewTerm;
struct timeval tv1;
void SetRaw(int);
void TermRestore(int);
int main(void) {
int fd=0, res=0, i=0;
char keystroke = 0;
fd_set rfds1;
SetRaw(fd);
FD_ZERO(&rfds1);
FD_SET(fileno(stdin), &rfds1);
while(1){
tv1.tv_sec=0;
tv1.tv_usec=1;
/* PRINT THE MENU */
res=select(fd+1, &rfds1,NULL,NULL,&tv1);
if(res){
read(fd, &keystroke, 1);
if(keystroke=='1'){
}
if(keystroke=='2'){
}
if(keystroke=='3'){
}
if(keystroke=='0'){
printf("så slutter vi!\n");
TermRestore(fd);
exit(1);
}
}
system("clear");
}
TermRestore(fd);
return 0;
}
void SetRaw(int fd)
{
/*
* Get terminal modes, and saves them in the OldTerm struct.
*/
(void)ioctl(fd, TCGETA, &OldTerm);
/*
* Set the modes to the way we want them.
*/
NewTerm.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
NewTerm.c_oflag |= (OPOST|ONLCR|TAB3);
NewTerm.c_oflag &= ~(OCRNL|ONOCR|ONLRET);
NewTerm.c_cc[VMIN] = 1;
NewTerm.c_cc[VTIME] = 0;
(void)ioctl(fd, TCSETAW, &NewTerm);
printf("fd is %d\n", fd);
}
void TermRestore(int fd)
{
/*
* Restore saved modes.
*/
(void)ioctl(fd, TCSETAW, &OldTerm);
}
Im having a bit of a problem with my program that i wrote for linux in c. I
use select() to monitor if the user has pressed a key and reads the key
with read(). It works fine om my IBM laptop but once i move the program to
my dell laptop it seems like it doesn't even recognize the select-function.
That is, it doesn't use the timeout assigned at all. I don't get any errors
when i compile on either computer. I run debian/testing on both laptops and
i use CVS for version-control.. I have tried to compile it on the IBM and
run it on the dell.. But nothing seems to work..
- Martin
The code is something along the lines of:
#include <sys/types.h>
#include <sys/stat.h>
#include <asm/io.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <time.h>
#include <termio.h>
#include <sys/ioctl.h>
struct termio OldTerm;
struct termio NewTerm;
struct timeval tv1;
void SetRaw(int);
void TermRestore(int);
int main(void) {
int fd=0, res=0, i=0;
char keystroke = 0;
fd_set rfds1;
SetRaw(fd);
FD_ZERO(&rfds1);
FD_SET(fileno(stdin), &rfds1);
while(1){
tv1.tv_sec=0;
tv1.tv_usec=1;
/* PRINT THE MENU */
res=select(fd+1, &rfds1,NULL,NULL,&tv1);
if(res){
read(fd, &keystroke, 1);
if(keystroke=='1'){
}
if(keystroke=='2'){
}
if(keystroke=='3'){
}
if(keystroke=='0'){
printf("så slutter vi!\n");
TermRestore(fd);
exit(1);
}
}
system("clear");
}
TermRestore(fd);
return 0;
}
void SetRaw(int fd)
{
/*
* Get terminal modes, and saves them in the OldTerm struct.
*/
(void)ioctl(fd, TCGETA, &OldTerm);
/*
* Set the modes to the way we want them.
*/
NewTerm.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
NewTerm.c_oflag |= (OPOST|ONLCR|TAB3);
NewTerm.c_oflag &= ~(OCRNL|ONOCR|ONLRET);
NewTerm.c_cc[VMIN] = 1;
NewTerm.c_cc[VTIME] = 0;
(void)ioctl(fd, TCSETAW, &NewTerm);
printf("fd is %d\n", fd);
}
void TermRestore(int fd)
{
/*
* Restore saved modes.
*/
(void)ioctl(fd, TCSETAW, &OldTerm);
}