Page 1 of 1

Mouse logger

PostPosted: Thu Feb 05, 2015 7:51 am
by pretentious
This is a program i wrote, inspired by http://iographica.com/
the c executable reads from the mouse device on my computer and prints a timestamp and the relative movement into another file. The python script reads the file in as an argument and redraws the mouses movement on a canvas. I set up a cronjob for this and set the image as my background. It was cool for a little while, and I wanted to add some more features the iographica has, like circles and shit (and I have knowledge of different mouse actions too) but I probably have undiagnosed ADD and moved onto other things
mouse_log.c
Code: Select all
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main(){
    int x = 683, y = 384;
FILE *input = fopen("/dev/input/mouse0","r");
FILE *log = fopen("/var/log/mouse_log", "a");
signed char char_num;
signed char protocol[3];
    while(input){// tests to see if file exists
        for(char_num = 0; char_num <3; char_num++){
            protocol[char_num] = fgetc(input);
        }
        x+=protocol[1];
        if(x >1366)x = 1366;
        if(x < 0)x = 0;
        y-=protocol[2];
        if(y > 768)y = 768;
        if(y < 0)y = 0;
        // I'm still not entirely sure how to decode the mouse buttons but in the future it will likely be easier to user numbers than words anyway
        // assuming protocol -1 when device is unplugged
        while(protocol[0] == -1){
            sleep(1);
            input = fopen("/dev/input/mouse0","r");
            if(input)
                break;
        }
    if(protocol[0] != -1)
        fprintf(log, "%d %d %d %d\n", (int)time(NULL), protocol[0], x, y);       
    }
    fclose(input);
    fclose(log);
}


line.py
Code: Select all
from PIL import Image, ImageDraw
import sys
#import time
filecount = open(sys.argv[1],'r')
f = open(sys.argv[1],'r')
img = Image.new("RGB",(1367*2, 768*2) ,"black")
pixdata = img.load()
formatted = f.readline().split()
x1 = int(formatted[2])
y1 = int(formatted[3])
draw = ImageDraw.Draw(img)
for line in f:
    formatted = line.split()
    if len(formatted) != 4:
        continue
    #print formatted
    x2 = int(formatted[2])
    y2 = int(formatted[3])
    colour = pixdata[x1,y1]
    shade = colour[0]
    if shade < 200:
        shade = shade + 25
    draw.line((x1*2,y1*2,x2*2,y2*2),(shade,shade,shade))
    x1 = x2
    y1 = y2
new = img.resize((1367,769), Image.ANTIALIAS)
#new.show()
new.save("wall.png","PNG")

Re: Mouse logger

PostPosted: Thu Feb 05, 2015 5:36 pm
by ghost107
It's kinda heavy on the CPU(Try to add a sleep):

Checking the mousse Polling rate on Ubuntu:
Code: Select all

cat /sys/module/usbhid/parameters/mousepoll
Result(should be one of this values):
1 ms = 1000Hz
2 ms = 500Hz
4 ms = 250Hz
8 ms = 125Hz
10 ms = 100Hz


You should look into Xlib(Xlib is like Win32 of Linux system):
http://tronche.com/gui/x/xlib/input/
Code: Select all
#include "X11/Xlib.h"

Display *display;
Window window;
XEvent event;
unsigned long event_mask = PointerMotion;

display = XOpenDisplay(NULL);                    //Connect  to the display server
window = XRootWindow(display, 0);                //0 = primary display, 1 = seconday display
XSelectInput(display, window,  event_mask);  // select a input type

while(true) {
   XNextEvent( display, &event );
   switch( event.type ) {
      case MotionNotify:
         printf("x=%d, y=%d\n", event.xmotion.x, event.xmotion.y );
         break;
   }
}

Re: Mouse logger

PostPosted: Thu Feb 05, 2015 5:46 pm
by cyberdrain
Bookmarked! :3 I have to read these back another time.

Re: Mouse logger

PostPosted: Sat Feb 07, 2015 1:45 am
by pretentious
ghost107 wrote:It's kinda heavy on the CPU(Try to add a sleep):

It's interesting that you mention this because I made a point to avoid system bloat and even though the code looks dodgy, I saw negligible resource use. It gave me the impression that fgetc pauses execution while waiting to read something. But I'm probably wrong.

I also tried using the x11 library previously to this but couldn't get it working as a background process. Oh well lol