LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-09-2004, 10:27 PM   #1
Dr Twox
LQ Newbie
 
Registered: Jul 2004
Location: Sunshine Coast, Australia
Distribution: LFS, Debian Woody, Mandrake
Posts: 6

Rep: Reputation: 0
How do I add a timeout feature to this code?


I have this small app (code below) that prints whatever button/axis a user pressed on a gamepad. How can I add a timeout feature so after waiting 5 seconds the program will print "IDLE" and exit?

alarm(5); works, but I can't figure out how to change the alarm message from "Alarm clock" to IDLE.

I've looked at the man pages for setitimer and select, but it's a bit too complicated for my rudimentary C skills!

Code:
#include <linux/joystick.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>

int main()
{
	int jfd;
	struct js_event jse;
	
	if ((jfd = PrivoxyWindowOpen("/dev/js0", O_RDONLY)) < 0)
        {
                perror("jstest");
                return 1;
        }
	
	while(read(jfd, &jse, sizeof(struct js_event)) == sizeof(struct js_event)) {
		if (jse.type == JS_EVENT_BUTTON)
		{
			if(jse.value == 0) //1=return immediately, 0=wait for release
			{ 
				switch(jse.number)
				{
					case 0: printf("B1"); break;
					case 1: printf("B2"); break;
					case 2: printf("B3"); break;
					case 3: printf("B4"); break;
					case 4: printf("B5"); break;
					case 5: printf("B6"); break;
					case 6: printf("B7"); break;
					case 7: printf("B8"); break;
					case 8: printf("B9"); break;
					case 9: printf("B10"); break;
					case 10: printf("B11"); break;
					case 11: printf("B12"); break;
					default: printf("UNKNOWN"); break;
				}
				return 0;
			}				
		} else	
		if(jse.value != 0)
		{
				if(jse.number == 0 && jse.value > 0) { 
					printf("RIGHT");
					return 0;
				} else
				if(jse.number == 0 && jse.value < 0) {
					printf("LEFT");
					return 0;
				} else
				if(jse.number == 1 && jse.value > 0) {
					printf("DOWN");
					return 0;
				} else
				if(jse.number == 1 && jse.value < 0) {
					printf("UP");
					return 0;
				}
		}
	}
	close(jfd);
	exit(0);
}
 
Old 07-10-2004, 03:08 AM   #2
frelihm
Member
 
Registered: Jan 2003
Location: Ljubljana, Slovenia, Europe
Distribution: debian
Posts: 72

Rep: Reputation: 15
1. save the system time of starting program (orig_time)
2. insert the most outer loop which will rad current system time (curr_time)
3. insert if statement for calulcating the difference between time and if the diferrence is biger or equal 5 seconds (curr_time-orig_time>=TIMEUOT_INTERVAL), exit
4. else if there was no activity on jostick, print "IDLE"
 
Old 07-12-2004, 05:15 AM   #3
Dr Twox
LQ Newbie
 
Registered: Jul 2004
Location: Sunshine Coast, Australia
Distribution: LFS, Debian Woody, Mandrake
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks!

Thanks!
I'll try it whenever I can find some time (probably some time between next week and 2034!)
:P
 
Old 07-12-2004, 05:19 AM   #4
frelihm
Member
 
Registered: Jan 2003
Location: Ljubljana, Slovenia, Europe
Distribution: debian
Posts: 72

Rep: Reputation: 15
no problem
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Add GUI feature, how to? sateeshgalla Programming 1 09-15-2005 01:09 PM
how to Add GUI feature to Target board sateeshgalla Linux - General 1 09-14-2005 04:34 PM
Editing buttons (quote, code etc.) add the code to the end vharishankar LQ Suggestions & Feedback 2 09-13-2004 10:32 AM
How do i adjust the timeout feature on Eth 0 DANNY0617 Linux - Networking 1 02-06-2004 11:59 AM
is there a timeout/keep alive feature on Linux? RandomName Linux - Networking 0 09-29-2003 10:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration