-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathvirtual_touchscreen.c
236 lines (197 loc) · 6.9 KB
/
virtual_touchscreen.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <linux/input.h>
#include <linux/input/mt.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/uaccess.h>
#include <asm/io.h>
#include <asm/delay.h>
#include <asm/uaccess.h>
#define MODNAME "virtual_touchscreen"
#define ABS_X_MIN 0
#define ABS_X_MAX 1024
#define ABS_Y_MIN 0
#define ABS_Y_MAX 1024
#define MAX_CONTACTS 10 // 10 fingers is it
#define DEVICE_NAME "virtual_touchscreen"
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
static int Major; /* Major number assigned to our device driver */
static int Device_Open = 0; /* Is device open? Used to prevent multiple access to the device */
struct class * cl;
struct device * dev;
struct file_operations fops __attribute__((__section__(".text"))) = {
read: device_read,
write: device_write,
open: device_open,
release: device_release
};
static struct input_dev *virt_ts_dev;
static int __init virt_ts_init(void)
{
int err;
virt_ts_dev = input_allocate_device();
if (!virt_ts_dev)
return -ENOMEM;
virt_ts_dev->evbit[0] = BIT_MASK(EV_ABS) | BIT_MASK(EV_KEY);
virt_ts_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
input_set_abs_params(virt_ts_dev, ABS_X, ABS_X_MIN, ABS_X_MAX, 0, 0);
input_set_abs_params(virt_ts_dev, ABS_Y, ABS_Y_MIN, ABS_Y_MAX, 0, 0);
virt_ts_dev->name = "Virtual touchscreen";
virt_ts_dev->phys = "virtual_ts/input0";
input_mt_init_slots(virt_ts_dev, MAX_CONTACTS, INPUT_MT_DIRECT);
input_set_abs_params(virt_ts_dev, ABS_MT_POSITION_X, ABS_X_MIN, ABS_X_MAX, 0, 0);
input_set_abs_params(virt_ts_dev, ABS_MT_POSITION_Y, ABS_Y_MIN, ABS_Y_MAX, 0, 0);
err = input_register_device(virt_ts_dev);
if (err)
goto fail1;
/* Above is evdev part. Below is character device part */
Major = register_chrdev(0, DEVICE_NAME, &fops);
if (Major < 0) {
printk ("Registering the character device failed with %d\n", Major);
goto fail1;
}
printk ("virtual_touchscreen: Major=%d\n", Major);
cl = class_create(THIS_MODULE, DEVICE_NAME);
if (!IS_ERR(cl)) {
dev = device_create(cl, NULL, MKDEV(Major,0), NULL, DEVICE_NAME);
}
return 0;
fail1: input_free_device(virt_ts_dev);
return err;
}
static int device_open(struct inode *inode, struct file *file) {
if (Device_Open) return -EBUSY;
try_module_get(THIS_MODULE);
++Device_Open;
return 0;
}
static int device_release(struct inode *inode, struct file *file) {
--Device_Open;
module_put(THIS_MODULE);
return 0;
}
static ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t *offset) {
const char* message =
"Usage: write the following commands to /dev/virtual_touchscreen:\n"
" x num - move to (x, ...)\n"
" y num - move to (..., y)\n"
" d 0 - touch down\n"
" u 0 - touch up\n"
" s slot - select multitouch slot (0 to 9)\n"
" a flag - report if the selected slot is being touched\n"
" e 0 - trigger input_mt_report_pointer_emulation\n"
" X num - report x for the given slot\n"
" Y num - report y for the given slot\n"
" S 0 - sync (should be after every block of commands)\n"
" M 0 - multitouch sync\n"
" T num - tracking ID\n"
" also 0123456789:; - arbitrary ABS_MT_ command (see linux/input.h)\n"
" each command is char and int: sscanf(\"%c%d\",...)\n"
" <s>x and y are from 0 to 1023</s> Probe yourself range of x and y\n"
" Each command is terminated with '\\n'. Short writes == dropped commands.\n"
" Read linux Documentation/input/multi-touch-protocol.txt to read about events\n";
const size_t msgsize = strlen(message);
loff_t off = *offset;
if (off >= msgsize) {
return 0;
}
if (length > msgsize - off) {
length = msgsize - off;
}
if (copy_to_user(buffer, message+off, length) != 0) {
return -EFAULT;
}
*offset+=length;
return length;
}
static void execute_command(char command, int arg1) {
switch(command) {
case 'x':
input_report_abs(virt_ts_dev, ABS_X, arg1);
break;
case 'y':
input_report_abs(virt_ts_dev, ABS_Y, arg1);
break;
case 'd':
input_report_key(virt_ts_dev, BTN_TOUCH, 1);
break;
case 'u':
input_report_key(virt_ts_dev, BTN_TOUCH, 0);
break;
case 's':
input_mt_slot(virt_ts_dev, arg1);
break;
case 'a':
input_mt_report_slot_state(virt_ts_dev, MT_TOOL_FINGER, arg1);
break;
case 'e':
input_mt_report_pointer_emulation(virt_ts_dev, true);
break;
case 'X':
input_event(virt_ts_dev, EV_ABS, ABS_MT_POSITION_X, arg1);
break;
case 'Y':
input_event(virt_ts_dev, EV_ABS, ABS_MT_POSITION_Y, arg1);
break;
case 'S':
input_sync(virt_ts_dev);
break;
case 'M':
input_mt_sync(virt_ts_dev);
break;
case 'T':
input_event(virt_ts_dev, EV_ABS, ABS_MT_TRACKING_ID, arg1);
break;
default:
if ((command>=0x30) && (command<=0x3b)) {
input_event(virt_ts_dev, EV_ABS, command, arg1);
} else {
printk("<4>virtual_touchscreen: Unknown command %c with arg %d\n", command, arg1);
}
}
}
static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t *off) {
char command;
int arg1;
char buf[64];
size_t len_to_use = len;
size_t i;
size_t p=0;
if (len_to_use > sizeof(buf)) len_to_use = sizeof(buf);
if (copy_from_user(buf, buff, len_to_use) != 0) {
return -EFAULT;
}
for(i=0; i<len_to_use; ++i) {
if (buf[i]=='\n') {
buf[i] = '\0';
if(sscanf(buf+p, "%c%d", &command, &arg1) != 2) {
printk("<4>virtual_touchscreen: sscanf failed to interpret this input\n");
}
p=i+1;
execute_command(command, arg1);
}
}
if (p == 0 && len != 0) {
printk("<4>virtual_touchscreen: Command incomplete or too long. Trailing \\n is required.\n");
// prevent endless loop
return len;
}
return p;
}
static void __exit virt_ts_exit(void)
{
input_unregister_device(virt_ts_dev);
if (!IS_ERR(cl)) {
device_destroy(cl, MKDEV(Major,0));
class_destroy(cl);
}
unregister_chrdev(Major, DEVICE_NAME);
}
module_init(virt_ts_init);
module_exit(virt_ts_exit);
MODULE_AUTHOR("Vitaly Shukela, [email protected]");
MODULE_DESCRIPTION("Virtual touchscreen driver");
MODULE_LICENSE("GPL");