can't seem to figure out why this code won't work. It gets into raw mode fine but exiting I keep getting Error 22
Code:
#include <unistd.h>
#include <linux/kd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
unsigned long old = -1;
void handler(int sig);
void exitRawMode();
void enterRawMode();
struct termios oldt, newt;
int fd;
int main()
{
signal(SIGKILL, handler);
printf("EBADF-%i\nENOTTY-%i\nEINVAL-%i\nEPERM-%i", EBADF, ENOTTY, EINVAL, EPERM);
enterRawMode();
int i = 0;
//for(i; i < 10; i++)
//{
// printf("%c",getchar());
//}
exitRawMode();
return 0;
}
void handler(int sig)
{
switch(sig)
{
case SIGKILL: ioctl(STDIN_FILENO, KDSKBMODE, &old);
exit(0);
break;
}
}
void enterRawMode()
{
fd = open("/dev/tty3", O_NONBLOCK | O_RDWR);
if(fd == -1)
{
printf("FAIL CONSOLE!");
printf("\n%d\n", errno);
}
else printf("Success console.\n%d", fd);
tcgetattr(fd, &oldt);
memcpy(&newt, &oldt, sizeof(struct termios));
oldt.c_lflag &= ~( ECHO| ICANON | IEXTEN | ISIG | ICRNL);
tcsetattr(fd, TCSANOW, &newt);
int ret = ioctl(fd, KDGKBMODE, &old);
printf("\n%d\n", old);
printf("%i\n", K_RAW);
printf("%i\n", K_UNICODE);
if(ret != -1)
{
printf("\noriginal %d\n", old);
printf("raw %i\n", K_RAW);
printf("unicode %i\n", K_UNICODE);
}
else
{
printf("fail");
printf("%d\n", errno);
printf("%d\n", EINVAL);
}
ret = ioctl(fd, KDSKBMODE, K_RAW);
printf("ret = %i", ret);
if(ret == -1)
{
printf("failed to enter raw mode\n");
printf("%i", errno);
}
else
{
printf("Now in raw mode\n");
unsigned long temp;
ioctl(fd, KDGKBMODE, &temp);
printf("current %i", temp);
}
close(fd);
}
void exitRawMode()
{
fd = open("/dev/tty3", O_NONBLOCK | O_RDWR);
if(fd == -1)
{
printf("\nFailed to reopen file descriptor");
}
else printf("\nFile Descriptor is now %i", fd);
unsigned long current;
int ret = ioctl(fd, KDGKBMODE, ¤t);
printf("\nold %d\n", old);
printf("\ncurrent %d", current);
printf("raw %i\n", K_RAW);
printf("unicode %i\n", K_UNICODE);
if(ret != -1)
{
printf("\nFailed to read here");
printf("\nError %i", errno);
printf("\noriginal %d\n", old);
printf("raw %i\n", K_RAW);
printf("unicode %i\n", K_UNICODE);
}
else
{
printf("\nfail");
printf("%d\n", errno);
printf("%d\n", EINVAL);
}
ret = ioctl(fd, KDSKBMODE, K_UNICODE);
if(ret != -1)
{
printf("failed to exit raw mode");
printf("\n%i", errno);
}
else printf("Now in cooked mode");
close(fd);
}
Additionally I actaully want to be able to run this within a gnome-terminal (or just be able to execute it as an X application) is that going to be more difficult?
My friend and I worked on this for a few hours tonight but couldn't get it working.
__________________