LinuxQuestions.org
Help answer threads with 0 replies.
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 01-21-2008, 09:20 AM   #1
danielrex15
LQ Newbie
 
Registered: Jan 2008
Posts: 4

Rep: Reputation: 1
reading mouse device


hi,
i am new to linux .as my assignment ,i have been asked to read from the usb mouse pointing device. To begin with, I am not aware of the particular device in the "/dev/" directory that I need to do a read on.
I also tried to read from "/dev/input/mice" and found that it returned only 3 bytes. I went thru some of the resources onlince and found that it needs to retur struct input_event (defined in /include/linux/). So, I am not aware of how to interpret this 3 bytes that i get from reading.
 
Old 01-21-2008, 01:27 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 75
This post should get you started on your homework.
 
Old 01-22-2008, 12:28 AM   #3
danielrex15
LQ Newbie
 
Registered: Jan 2008
Posts: 4

Original Poster
Rep: Reputation: 1
mouse device

hi,
thank you very much for your help.........can u help me with a detailed code for this.
thanks in advance
 
1 members found this post helpful.
Old 01-22-2008, 02:20 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 75
Quote:
Originally Posted by danielrex15 View Post
can u help me with a detailed code for this.
I will not do your homework. But I will tell you this: there are two ways to read from a mouse device in linux:
  1. The old mousedev way. This involves device files such as /dev/input/mouseN for reading from the Nth mouse or /dev/input/mice for reading from all mice at the same time.
  2. The new evdev way. Here you read from /dev/input/eventN where N corresponds to your input event handler for your mouse (you can view this through “cat /proc/bus/input/devices”).
If you want to use the old way, you’ll have to look at some code which uses it (such as gpm) and make sense of it.

If you want to use the new way, your code can build on this boilerplate:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

#include <linux/input.h>

#define MOUSEFILE "/dev/input/event1"

int main()
{
	int fd;
	struct input_event ie;

	if((fd = open(MOUSEFILE, O_RDONLY)) == -1) {
		perror("opening device");
		exit(EXIT_FAILURE);
	}

	while(read(fd, &ie, sizeof(struct input_event))) {
		printf("time %ld.%06ld\ttype %d\tcode %d\tvalue %d\n",
		       ie.time.tv_sec, ie.time.tv_usec, ie.type, ie.code, ie.value);
	}

	return 0;
}
Now, in the real world, you might pass the device path as an argument. Or you might pass the device description as an argument and have your program find the device path through /proc/bus/input/devices. You will obviously need to know what the types and codes and values mean. These are mostly macros in the linux/input.h header file. For example, for mice you will need to know
Code:
EV_SYN — synthetic events (e.g. 0 0 0) which separate real events
EV_KEY — key/button events
EV_REL — relative motion
EV_ABS — absolute motion
You will also need to know things like the keycodes for buttons (there are in fact ioctls which you can use to get the keyvalue mask of the device):
Code:
BTN_LEFT
BTN_RIGHT
BTN_MIDDLE
You will need to know the relative and absolute axes. You will also need to know what the value field means. E.g., for button presses, it is usually 1 and for button releases, it is 0. You will also need to use look at the timing of each event, and use it to interpret double or triple clicks, etc.

If you want some concrete code to look at, look you might take a look at the Xorg evdev input driver or library. There are a bunch more if you search the web for “evdev mouse” or similar. For example, here is a small program which finds the keys and axes supported by a device.
 
1 members found this post helpful.
Old 01-23-2008, 06:54 AM   #5
danielrex15
LQ Newbie
 
Registered: Jan 2008
Posts: 4

Original Poster
Rep: Reputation: 1
I Have Finished My Homework......only One Part Left Detecting Whether A Mouse Is Attached Or Not....how Can We Do That Any Clue.


Thanks In Advance
Daniel Rex
 
Old 01-23-2008, 12:27 PM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 75
If you just want to see all your mouse devices, you can (as I mentioned before) just use /proc/bus/input/devices. If you want something special to occur when you plug in a mouse (e.g., the running of a script) you can use udev.
 
  


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
Reading from the /dev/mouse RichardS Programming 4 08-16-2007 04:56 PM
reading mouse movement jdhar Linux - Hardware 4 06-06-2006 06:24 PM
reading and writing to a serial device (modem) Xanadu Linux - Laptop and Netbook 0 01-04-2005 11:05 AM
Reading USB mouse data spectasaurus Linux - Software 0 12-05-2004 12:00 PM
Reading USB storage device? cucolin@ Red Hat 4 02-19-2004 02:09 AM


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