public inbox for linux-media@vger.kernel.org
 help / color / mirror / Atom feed
From: Jan Panteltje <panteltje@yahoo.com>
To: Tony Houghton <h@realh.co.uk>
Cc: linux-media@vger.kernel.org
Subject: Re: General question about IR remote signals  from USB DVB tuner
Date: Sat, 11 Feb 2012 11:01:16 -0800 (PST)	[thread overview]
Message-ID: <1328986876.41543.YahooMailClassic@web39302.mail.mud.yahoo.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 294 bytes --]

This little demo C program looks for the IR input device,
parses the IR output and can start any application on any remote key press
or release.
Compile with gcc -o test50 test50.c
I have added a start xterm, a start firefox, and a kill firefox
as example.
add your own as needed.
Have fun:-)


[-- Attachment #2: test50.c --]
[-- Type: application/octet-stream, Size: 4159 bytes --]

/*
Copyright Jan Panteltje 2012-always
released under the GPL.
This is a small program that grabs keys from the Terratec Cinergy S2 USB HD remote and starts some applications.
This just demo code, no special purpose,
but it shows that little remote can be used to do anything your PC can do, not just TV.
Should be easy to adapt this for other DVB IR remote controllers.
You can find the key values by pressing the keys on the remote and watching the output of this program,
then add your own code for each key press and release as needed.

filename test50.c
compile with: gcc -o test50 test50.c
run with:
./test50
pres keys on IR remote,
key '1' starts an xterm
ENTER starts firefox
key 2 kills firefox
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>
#include <signal.h>
#include <pwd.h>
#include <math.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/poll.h>
#include <stdint.h>
#include <locale.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/io.h>
#include <sys/ioctl.h>
#include <ctype.h>
#include <linux/dvb/frontend.h>
#include <linux/dvb/dmx.h>

#include <linux/input.h>

/*
looking for something like:
/dev/input/by-path/pci-0000:04:00.0-event-ir
*/


int main(int argc, char **argv)
{
int a, b, c, i, j;
int fd;
FILE *fptr;
int data[1024];
char temp[1024];
char ir_device[1024];
FILE *pptr;
int found_flag = 0;
int key_code, key_pressed;

/* going to parse the result of ls in /dev/input/by-path/ */
pptr = popen("ls --color=none /dev/input/by-path/", "r");
temp[0] = 0;
j = 0;
for(i = 0; i < 1024; i++)
	{
	c = fgetc(pptr);
	if( feof(pptr) )
		{
		ir_device[i] = 0;
		break;
		}	

	if(c == 10) // LF
		{
		temp[j] = 0;
		j = 0;
		if(strstr(temp, "event-ir") != 0)
			{
//			fprintf(stderr, "found it, i=%d temp=%s\n", i, temp);
			found_flag = 1;
			break;
			}
		j = 0;				
		}
	else
		{
		temp[j] = c;
		j++;
		}
	}
pclose(pptr);

if(! found_flag)
	{
	fprintf(stderr, "could not find event-ir device, aborting.\n");
	
	exit(1);
	}

//fprintf(stderr, "temp=%s\n", temp);

sprintf(ir_device, "/dev/input/by-path/%s", temp);
fprintf(stderr, "trying ir_device=%s\n", ir_device);

fptr = fopen(ir_device, "r");
if(! fptr)
	{
	fprintf(stderr, "could not open /dev/input/event13 for read,aborting.\n");
	
	exit(1);
	}

/* grab the device for us alone, this prevents output to other applications, as that would cause havoc */
a = ioctl(fileno(fptr), EVIOCGRAB);
if(a < 0)
	{
	fprintf(stderr, "ioctl EVIOCGRAB failed because ");
	perror("");
	
	exit(1);
	}

//fprintf(stderr, "fptr=%p\n", fptr);

/*
read the correct number of bytes from the device for one key event.
**** NOTE: this number 48 is empirical and works for my TerratecCinery S2 USB remote, other remotes may perhaps require  adifferent value. ****
maybe read it with an ioctlfrom input_keymap_entry ? see /usr/include/linux/input.h 
*/

FILE *pptr1;
FILE *pptr2;

while(1)
	{
	for(i = 0; i < 48; i++)
		{
		c = fgetc(fptr);
		if(feof(fptr) )break;

		/*
		field 18 has the key value 
		field 20 has the key pressed- released flag
		*/ 
		if(i == 18) key_code = data[18];
		if(i == 20) key_pressed = data[20];

		data[i] = c;	
		} /* end for eachbyte in a field */

	do_keys(key_code, key_pressed);
		
	fprintf(stderr, "\n");
	} /*end while read fields resulting from akey press */

exit(0);
} /* end function main */
	


do_keys(int key_code, int key_pressed)
{
FILE *pptr;

/* do your things here, this can be used to do anything on a key press and key release, just some silly examples */
if(key_pressed)
	{
	fprintf(stderr, "key %02x pressed\n", key_code);

	switch(key_code)
		{
		case 0x02: // key '1'
			popen("xterm", "w");
			/* no pclose, exiting xterm will do that */  
		break;
					
		case 0x60: // ENTER
			popen("firefox &", "w");
			break;
				
		}


	} /* end if key pressed */
else
	{		
	fprintf(stderr, "key %02x released\n", key_code);		
		
	switch(key_code)
		{
		case 0x03: // key '2'			
			popen("killall -KILL firefox-bin", "r");
			break;
		}

	} /* andif key released */

} /* end function do_keys */






             reply	other threads:[~2012-02-11 19:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-11 19:01 Jan Panteltje [this message]
  -- strict thread matches above, loose matches on Subject: below --
2012-02-10 16:34 General question about IR remote signals from USB DVB tuner Jan Panteltje
2012-02-10 18:25 ` Tony Houghton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1328986876.41543.YahooMailClassic@web39302.mail.mud.yahoo.com \
    --to=panteltje@yahoo.com \
    --cc=h@realh.co.uk \
    --cc=linux-media@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox