LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-18-2011, 06:47 AM   #1
sushiee
LQ Newbie
 
Registered: May 2011
Posts: 1

Rep: Reputation: Disabled
struct input_event + Linux Input Subsystem


Hi, I am a newbie to linux and am trying to understand the linux input subsystem.

With the online help(which is prolifically available) I understood the basics to a certain extent. Although, there is one thing that I am not able to understand, here it goes :

Lets say there's a input device that is capable of generating an event with the help of its driver. Eg:
###############################################################################
/*
Driver to setup a Fake Input Device
by Sushiee
*/

#include <linux/input.h>
#include <linux/module.h>
#include <linux/init.h>

MODULE_LICENSE("GPL");

struct input_dev *dev_ip;

void dev_ip_timeout(unsigned long unused/*UNUSED*/)
{
/* letter S or s */
input_report_key(dev_ip, KEY_S, 1);
input_sync(dev_ip);
input_report_key(dev_ip, KEY_S, 0);
input_sync(dev_ip);
/* set timer for five second */
mod_timer(&(dev_ip->timer),jiffies+5*HZ );
}

static int __init ip_init(void)
{
/* initialization */
dev_ip = input_allocate_device();
//memset(&dev_ip, 0, sizeof(struct input_dev));
//init_input_dev(&dev_ip);

/* set up descriptive labels */
dev_ip->name = "Con Device";
/* phys is unique on a running system */
dev_ip->phys = "";
dev_ip->id.bustype = BUS_HOST;
dev_ip->id.vendor = 0xDEAD;
dev_ip->id.product = 0xBEEF;
dev_ip->id.version = 0xFACE;

/* this device has one key S*/
set_bit(EV_KEY, dev_ip->evbit);
set_bit(KEY_S, dev_ip->keybit);

/* and finally register with the input core */
input_register_device(dev_ip);

/* set up a repeating timer */
init_timer(&dev_ip->timer);
dev_ip->timer.function = dev_ip_timeout;
dev_ip->timer.expires = 0;
add_timer(&dev_ip->timer);

return 0;
}

static void __exit ip_exit(void)
{
input_unregister_device(dev_ip);
}

module_init(ip_init);
module_exit(ip_exit);
###############################################################################

Its a driver for a virtual/fake device. One can complie it as a module and then install it by using : sudo insmod ./file_name.ko


Now, say this application is trying to access this device :
###############################################################################
#include <stdio.h>
#include <fcntl.h>
#include <linux/input.h>

#define EVENT_BUF_NUM 64

int main(int argc , char ** argv)
{
int fd = -1;
int i,version;
size_t read_bytes;
struct input_event event_buf[EVENT_BUF_NUM];

printf("Opening ...\n");

fd = open("/dev/input/event6", O_RDONLY);
if(fd < 0) {
printf("\nUnable to read from the device\n");
return(1);
}

printf("Reading ...\n");
while(1) {
read_bytes = read(fd, event_buf, sizeof(struct input_event)*EVENT_BUF_NUM);
if( read_bytes < sizeof(struct input_event)) {
perror("bad read");
close(fd);
return(1);
}
for(i=0; i<(read_bytes/sizeof(struct input_event)); i++) {
switch(event_buf[i].type) {
case EV_SYN:
DPRINTF("---------------------------------------\n");
break;

case EV_KEY:
switch(event_buf[i].value) {
case 0:
printf("Key Code: %d released\n", event_buf[i].code);
break;
case 1:
printf("Key Code: %d pressed\n", event_buf[i].code);
break;

case 2:
DPRINTF("Key pressed continue\n", event_buf[i].code);
break;

default:
printf("UnKnown value: type %d, code %d, value %d\n",
event_buf[i].type,
event_buf[i].code,
event_buf[i].value);
break;
}
break;
}
}
} /* end while */

close (fd);
}
###############################################################################

All, I want to know is where is the application getting the input_event structure from.
Who writes to this structure input_event?
does each device has a separate input_event structure? (I guess yes)
As,there is no input_event structure in any of these structues : struct device, struct input_dev, struct handle, struct handler. So, how is an input_event attached to the corresponding device.

Sorry for the long question.
 
  


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
[SOLVED] Question about Kernel pid_namespace - struct pid and struct upid sreeharsha.t Linux - Kernel 4 03-19-2010 05:41 AM
Typecast struct sockaddr in struct sockaddr_in sudhansu Linux - Kernel 1 02-17-2009 11:33 AM
GCC compile problem:struct A have a member variable which is just a struct type name? leon.zcom Programming 3 04-18-2008 05:40 PM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 11:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 05:30 PM


All times are GMT -5. The time now is 09:27 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