Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Problems while writing to device file
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 05-14-2008   #1 (permalink)
montylee
Just Joined!
 
Join Date: May 2008
Location: India
Posts: 9
Question Problems while writing to device file

I want to record and play keyboard events on my Linux machine.
Basically i store all the keyboard events entered by the user into a text file. For this, i am simply reading the device file: /dev/input/event0 using read() and storing the events into a text file. While playback i read() the text file and write() the events to the device file.

The code also stores and plays the time difference between the keys pressed.
The code works fine for mouse, but for keyboard it is giving some problems. While playback it shows the proper output including the delay but once the program terminates it dumps the entire entered string on the terminal.
Here's an example usage:

Recording
# ./record /dev/input/event0 ./record.txt
Now whatever the user enters thru the keyboard is recorded until the user presses the ctrl+c key.

Playback

# ./play /dev/input/event0 ./record.txt
Suppose i have entered "monty" while recording, and i have given 2 seconds delay after entering "mo" and then i entered "nty", here's the output:
mo(2seconds delay)nty
After this immediately the prompt returns and dumps the entire string:
#montyc
This "c" is shown as i had pressed Ctrl+C for stopping the recording...

Now, anybody knows while the entire string is getting dumped to the screen when the program terminates, and is there any way to resolve this.

Here's my code:

Recording
Code:
FILE *deviceConnection,*record_file;
struct input_event myEvent;

deviceConnection = fopen(argv[1], "rw");
record_file = fopen(argv[2], "rw+");

while (1) {
	printf(".");
        // read from the device file into the input_event structure */
	fread(&myEvent, sizeof(struct input_event), 1, deviceConnection);
	// Write the events to the text file */
       fwrite(&myEvent, sizeof(struct input_event), 1, record_file);
       fflush(record_file);		
}
fclose(deviceConnection);
fclose(record_file);
Playback
Code:
FILE *record_file;
struct input_event myEvent;
char filename[128] ;
int devHandle = -1;

devHandle = open(argv[1], O_RDWR);
record_file = fopen(argv[2], "r+");

while (!feof(record_file))
{
	fread(&myEvent, sizeof(struct input_event), 1, record_file);
	if (myEvent.value != 0) {
		printf ("We are in Key Press \n");
		write(devHandle, &(myEvent), sizeof(struct input_event);
	}
}

close (devHandle);
fclose(record_file);
__________________
My GNU/Linux Blog: http://funwithlinux.blogspot.com
montylee is offline   Reply With Quote
Old 05-14-2008   #2 (permalink)
wildpossum
Just Joined!
 
wildpossum's Avatar
 
Join Date: Apr 2008
Location: Sydney/Australia
Posts: 74
Send a message via Skype™ to wildpossum
A quick solution to your interest is simply to run "script" - which make typescript of terminal session to the file of your choice. Have a geek at "man script" and it will tell you all about it. If you wish to further extend "script" functionality, get the source code and do it. This is the essence of Linux.

G.
__________________
Grahame
AMD Phenom(QuadCore), 8GB, 3ware RAID6 1.8TB, HD3850(512MB) ..etc.
wildpossum is offline   Reply With Quote
Old 05-14-2008   #3 (permalink)
montylee
Just Joined!
 
Join Date: May 2008
Location: India
Posts: 9
Wink

Hey, thanks for your reply!

Using the script command doesn't look good in my code. I would really like to use some other option.
__________________
My GNU/Linux Blog: http://funwithlinux.blogspot.com
montylee is offline   Reply With Quote
Reply


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


All times are GMT. The time now is 12:19 AM.

Powered by vBulletin 3.6.8 ©2000 - 2007, content relevant URLs by vBSEO, Property of Core Root.

Content Relevant URLs by vBSEO 3.0.0