WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Raw mouse function in console  (Read 10052 times)

Offline dull

  • Newbie
  • *
  • Posts: 9
Raw mouse function in console
« on: March 03, 2013, 02:03:49 PM »
 I am trying to make a simple function to run in the bash-shell of tc minimal(core+vmlinuz;
Things like GPM are totally useless for this; I noticed that in the tc shell, I can:
sudo mount /dev/input/ /mnt/mows(after mkdir mows in mnt); with ls mows, I find "mouse0";

with sudo sh ./mouse0, the system is actualy responding to the mouse, but in unpredictable ways
making gibberish on the screen. So I mounted a an ext4 belonging to linux mint and copied
mouse0 to it, hoping to study it.

 To my dismay, it's properties were "0 bytes". So, I imagine this "mouse0" script gets written up
by something in the rootfs,or the kernel, or both; I also figure that the rootfs uses the same old
xterm-ncuses approach, which I want to avoid, as I want it old style console mode,very small footprint(in dos, I can write a mouse events function using int33-int86-union regs and such
using only a few kilobytes.)
  I tried to study the "getmouseevents.c" file in GPM to glean insight, but am quickly stumped by references to structs that are evidently buried deep inside some shared libraries which I can't seem to trace.

  Anybody out there with the requisite insights. TNX.

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11044
Re: Raw mouse function in console
« Reply #1 on: March 03, 2013, 02:15:49 PM »
mouse0 is a device node, not a file or a script. It relays mouse events from the kernel as you found.

I don't think you can use dos-style bios calls under a linux console, but I'm not 100% sure on that.


If you'd like to manually read those events, see Documentation/input in the kernel source tree.
« Last Edit: March 03, 2013, 02:19:16 PM by curaga »
The only barriers that can stop you are the ones you create yourself.

Offline tinypoodle

  • Hero Member
  • *****
  • Posts: 3857
Re: Raw mouse function in console
« Reply #2 on: March 03, 2013, 03:04:24 PM »
I don't think you need to mount /dev/input/, you could just directly access the dir.

bash and ncurses are not included in base but available as extensions.
"Software gets slower faster than hardware gets faster." Niklaus Wirth - A Plea for Lean Software (1995)

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11624
Re: Raw mouse function in console
« Reply #3 on: March 03, 2013, 06:00:35 PM »
Hi dull
Install  compiletc.tcz.  Download the attached file. Execute:
Code: [Select]
gcc mousemon.c -o mousemonRun the program:
Code: [Select]
sudo ./mousemonWhen you move the mouse, the characters it sends will be displayed on the screen in hex. If your screen is 80
characters wide, the data will line up in columns. When you've had enough, hit Ctrll-C.

Offline dull

  • Newbie
  • *
  • Posts: 9
Re: Raw mouse function in console
« Reply #4 on: March 03, 2013, 09:10:42 PM »
Rich,
  Now I'm all excited; just downloaded the file ;does it work in console mode? I'll get back to you. TNX.

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11624
Re: Raw mouse function in console
« Reply #5 on: March 04, 2013, 02:12:04 AM »
Hi dull
Here is a version that decodes the output of  /dev/input/mouse0.
Code: [Select]
DeltaX=  +8 DeltaY=  +0 Left=U Middle=U Right=U 0X08    8    0
DeltaX=  +4 DeltaY=  +0 Left=U Middle=U Right=U 0X08    4    0
DeltaX=  +5 DeltaY=  +0 Left=U Middle=U Right=U 0X08    5    0
DeltaX=  +3 DeltaY=  +0 Left=U Middle=U Right=U 0X08    3    0
DeltaX=  +1 DeltaY=  +0 Left=U Middle=U Right=U 0X08    1    0
DeltaX=  +1 DeltaY=  +0 Left=U Middle=U Right=U 0X08    1    0
If you run the program with the  -s  switch, it will omit the linefeed when printing.
The last three fields are the raw data in hex, decimal, and decimal. For more information on the mouse protocol:
http://www.computer-engineering.org/ps2mouse/

Offline dull

  • Newbie
  • *
  • Posts: 9
Re: Raw mouse function in console
« Reply #6 on: March 04, 2013, 09:32:55 AM »
First, tnx all;Rich,
I modified your code sample as follows:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include<gconio.h>

/******************************************************************/
int main (int argc, char *argv[])
{
   FILE *mousefp;
int a[10];
   int mousedata;
int knt;
int ev;
int aknt;


   if((mousefp=fopen("/dev/input/mouse0", "rb")) == NULL)
   {
      printf("Error line:%d, could open directory\n", __LINE__);
      exit(-1);
   }
ev=0;
while(1){//outer
knt=0;
aknt=0;
   while(1)//inner
   {
        printf("top-while\n");
      mousedata=fgetc(mousefp);
       
        //printf("%d;%X\n",knt,mousedata);knt=knt+1;
a[aknt]=mousedata;aknt=aknt+1;knt=knt+1;
      fflush(stdout);
        printf("bottom-while\n");
if(knt>5){knt=0;break;}
   }//inner
printf("event:%X\n",ev);ev=ev+1;
printf("a[0]=%d;a[1]=%d;a[2]=%d;a[3]=%d;a[4]=%d;a[5]=%d\n",a[0],a[1],a[2],a[3],a[4],a[5]);
        }//outer
   return(0);
}
/******************************************************************/


The output of this clearly demonstrates that the inner while(your while) is executing six times
bfore falling through to the outer loop! What is even more amazing to me is that the following
code exhibits the same behaviour with a different executable than the printf:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include<gconio.h>

/******************************************************************/
int main (int argc, char *argv[])
{
   FILE *mousefp;
int a[10];
   int mousedata;
int knt;
int ev;
int aknt;


   if((mousefp=fopen("/dev/input/mouse0", "rb")) == NULL)
   {
      printf("Error line:%d, could open directory\n", __LINE__);
      exit(-1);
   }
ev=0;
while(1){//outer
knt=0;
aknt=0;
   while(1)//inner
   {
        printf("top-while\n");
      mousedata=fgetc(mousefp);
       
        //printf("%d;%X\n",knt,mousedata);knt=knt+1;
a[aknt]=mousedata;aknt=aknt+1;knt=knt+1;
      fflush(stdout);
        printf("bottom-while\n");
if(knt>5){knt=0;break;}
   }//inner
printf("event:%X\n",ev);ev=ev+1;
printf("a[0]=%d;a[1]=%d;a[2]=%d;a[3]=%d;a[4]=%d;a[5]=%d\n",a[0],a[1],a[2],a[3],a[4],a[5]);
        }//outer
   return(0);
}
/******************************************************************/

  it looks like *any?* expression with 'mousedata' as the r-value will do this; which further implies
that the variable 'mousedata gets "commandeered" by the process engendered by 'mouse0';
further that any expression with 'mousedata' as the r-value is likewise "commandeered",
possibly by wrapping it in a temporary function-wrapper?
  I can see how the variable could be directly written to by pointer from somwhere, but the miltiple execution involving it is, to me, *astounding!*!
  I could not have *bought* what I have learned in the last 24 hours.
 
  As for the other suggestions I will try them later, in about six hours. Again TNX all.

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11624
Re: Raw mouse function in console
« Reply #7 on: March 04, 2013, 03:00:15 PM »
Hi dull
Quote
The output of this clearly demonstrates that the inner while(your while) is executing six times
bfore falling through to the outer loop!
Well yes, that's because you told it to do that by adding a counter and conditional break statement. What I wrote
simply loops forever writing 16 entries per line before wrapping (if console has 80 character lines) to the next line.
Quote
What is even more amazing to me is that the following code exhibits the same behaviour with a different executable than the printf:
Not that amazing considering you post exactly the same code.
Quote
it looks like *any?* expression with 'mousedata' as the r-value will do this; which further implies
that the variable 'mousedata gets "commandeered" by the process engendered by 'mouse0';
further that any expression with 'mousedata' as the r-value is likewise "commandeered",
possibly by wrapping it in a temporary function-wrapper?
I don't understand what you are trying to say. You do realize  mousedata  is just a variable name I made up. It
has nothing to do with the operating system and is only written to by me.

Offline tinypoodle

  • Hero Member
  • *****
  • Posts: 3857
Re: Raw mouse function in console
« Reply #8 on: March 04, 2013, 04:29:37 PM »
Perhaps
Code: [Select]
hexdump [OPTIONS] /dev/input/mouse0which provides display in various user specified formats (check with "--help") might be of any use?
"Software gets slower faster than hardware gets faster." Niklaus Wirth - A Plea for Lean Software (1995)

Offline dull

  • Newbie
  • *
  • Posts: 9
Re: Raw mouse function in console
« Reply #9 on: March 05, 2013, 09:08:37 PM »
RICH,
   I have modified your code slightly, inserting two printf's; the first, berfore the forever while, is "before forever".
The second one is just inside the while: 'inside forever".
   Now, start the program, but after <enter> do nothing, don't even breath on the mouse. The cursor, or output will sit there forever, doing nothing. When the mouse is moved, then you see the intended output, including "inside forever".
  This means that the program is waiting between those two statements untill the mouse is moved.

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11624
Re: Raw mouse function in console
« Reply #10 on: March 05, 2013, 09:38:00 PM »
Hi dull
Quote
This means that the program is waiting between those two statements untill the mouse is moved.
That's what is supposed to do. When the mouse moves there is data available in /dev/input/mouse0. When the
mouse is stationary, there is no data available in /dev/input/mouse0, and the read blocks.

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11624
Re: Raw mouse function in console
« Reply #11 on: March 06, 2013, 02:55:42 AM »
Hi dull
Here is a program that should give you a good starting framework for what you are trying to do. It puts a block
cursor on the screen that moves with the mouse. As you move the mouse, it prints the X and Y positions as
well as the state of the mouse buttons. If you modify the program to write to a file instead of the screen, and
run it as a background task, your application can poll the file for mouse coordinates and button status.

To anyone else who tries this program, it should be run in the console (Ctrl-Alt-F1).

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11624
Re: Raw mouse function in console
« Reply #12 on: March 06, 2013, 03:06:27 AM »
The programming and scripting section is probably a more appropriate area for this. Moving.

Offline dull

  • Newbie
  • *
  • Posts: 9
Re: Raw mouse function in console
« Reply #13 on: March 06, 2013, 04:56:55 PM »
RICH,
thats what I was trying to figure; the read waits. I think I knew that once, but forgot it. Will play with the above. Get back to you.

Offline dull

  • Newbie
  • *
  • Posts: 9
Re: Raw mouse function in console
« Reply #14 on: March 06, 2013, 07:15:21 PM »
Rich,
figured out a neat way to clear the screen:
system("sh ./ cs");
where cs is the script:
echo -en "\033[2J";

My experience with the system function from the dos side is that it can directly execute some commands all the time;some commands some of the time, but one thing it never fails at is the execution of a script. I expect it to be that way in linux.
Tnx for the great code.