-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse_shared.c
92 lines (77 loc) · 1.93 KB
/
mouse_shared.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <time.h>
#include <syslog.h>
#include "logfile.c"
int write_to_memory(char* str)
{
// ftok to generate unique key
key_t key = ftok("/",65);
// shmget returns an identifier in shmid
int shmid = shmget(key,1024,0666|IPC_CREAT);
// shmat to attach to shared memory
char *_str = (char*) shmat(shmid,(void*)0,0);
//printf("Write Data : ");
strcpy(_str, str);
//puts(str);
//printf("Data written in memory: %s\n",_str);
//detach from shared memory
shmdt(_str);
return 0;
}
#include "write_history.c"
int main(int argc, char** argv)
{
int fd, bytes;
unsigned char data[3];
/*int *ptr = mmap ( NULL, 1*sizeof(int),
PROT_WRITE,
MAP_SHARED,
0, 0 );*/
const char *pDevice = "/dev/input/mice";
// Open Mouse
fd = open(pDevice, O_RDWR);
if(fd == -1)
{
printf("ERROR Opening %s\n", pDevice);
return -1;
}
else {
printf("opened!");
}
int left, middle, right;
signed char x, y;
write_to_memory("NOTHING");
while(1)
{
// Read Mouse
bytes = read(fd, data, sizeof(data));
if(bytes > 0)
{
//printf("bytes = %d\n", bytes);
left = data[0] & 0x1; // not used but cable connected
right = data[0] & 0x2; // connected!!
middle = data[0] & 0x4; // not used but cable connected
x = data[1];
y = data[2];
//printf("x=%d, y=%d, left=%d, middle=%d, right=%d\n", x, y, left, middle, right);
//printf("right=%d\n", right);
if(right == 2) {
//printf("Bell pressed!!!!!!\n");
write_to_memory("BELL");
logfile("BELL_SIGNAL_FROM_USB_MOUSE");
write_to_history_file();
}
// Test ONLY:
if(x != 0 || y != 0) {
write_to_memory("X_Y_COORDINATE_CHANGE");
}
}
//usleep(50 * 1000);
}
return 0;
}