Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 4 Weeks Ago   #1 (permalink)
New to Overclock.net
 
intel nvidia

Join Date: Nov 2007
Location: Surrey, BC, Canada
Posts: 272

Rep: 14 serge2k Unknown
Unique Rep: 14
Trader Rating: 1
Default Trying to use raw keyboard input (linux, C)

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, &current);
	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.
__________________
System: The Black Box
CPU
E6750
Motherboard
Asus P5N-E SLI
Memory
2GB DDR-2 800 MHz
Graphics Card
EVGA 8800 GTS 640
Hard Drive
2x250GB Seagate in Raid 0, 2x1TB WD Caviar Blacks
Sound Card
integrated
Power Supply
Antec Earthwatts 500W
Case
Antec Sonata 3
CPU cooling
Xigmatek HDT S1283
GPU cooling
stock
OS
Windows 7 Ultimate x64
Monitor
22" LG
serge2k is offline   Reply With Quote
Old 4 Weeks Ago   #2 (permalink)
Security Sleuth
 
Pooping^fish's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: egypt
Posts: 1,267

Rep: 67 Pooping^fish is acknowledged by some
Unique Rep: 62
Trader Rating: 3
Default

Raw TTY access from userland? Why?

Give us more info. Where exactly does it fail, the IOCTL? I see some code without error checking as well.
__________________
Quote:
"O, hai! Want som pRon? Dwnlod ths kodk frst. Its teh bst pRonz ever, we prmis." -GibbyGano
Proud Member of the Linux Gaming Community
I am your friend.

System: ragequit
CPU
Q9550 4ghz @ 1.25v
Motherboard
Asus Max 2 formula
Memory
OCZ LV blade 1:1 950mhz
Graphics Card
8800gtx 610/1ghz
Hard Drive
7200.10 250gb
Sound Card
X-FI Extreme Music
Power Supply
750w Toughpower
Case
Lian li pc-65
CPU cooling
TRUE
GPU cooling
stock
OS
leetlinucks
Monitor
24" Westy
Pooping^fish is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 11:04 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.08705 seconds with 8 queries