29
#include "qemu/osdep.h"
30
#include "qemu/module.h"
31
#include "chardev/char-serial.h"
32
#include "ui/console.h"
35
#include "qom/object.h"
38
#define WC_OUTPUT_BUF_MAX_LEN 512
39
#define WC_COMMAND_MAX_LEN 60
41
#define WC_L7(n) ((n) & 127)
42
#define WC_M7(n) (((n) >> 7) & 127)
43
#define WC_H2(n) ((n) >> 14)
45
#define WC_L4(n) ((n) & 15)
46
#define WC_H4(n) (((n) >> 4) & 15)
49
#define WC_MODEL_STRING_LENGTH 18
50
uint8_t WC_MODEL_STRING[WC_MODEL_STRING_LENGTH + 1] = "~#CT-0045R,V1.3-5,";
52
#define WC_CONFIG_STRING_LENGTH 8
53
uint8_t WC_CONFIG_STRING[WC_CONFIG_STRING_LENGTH + 1] = "96,N,8,0";
55
#define WC_FULL_CONFIG_STRING_LENGTH 61
56
uint8_t WC_FULL_CONFIG_STRING[WC_FULL_CONFIG_STRING_LENGTH + 1] = {
57
0x5c, 0x39, 0x36, 0x2c, 0x4e, 0x2c, 0x38, 0x2c,
58
0x31, 0x28, 0x01, 0x24, 0x57, 0x41, 0x43, 0x30,
59
0x30, 0x34, 0x35, 0x5c, 0x5c, 0x50, 0x45, 0x4e, 0x5c,
60
0x57, 0x41, 0x43, 0x30, 0x30, 0x30, 0x30, 0x5c,
61
0x54, 0x61, 0x62, 0x6c, 0x65, 0x74, 0x0d, 0x0a,
62
0x43, 0x54, 0x2d, 0x30, 0x30, 0x34, 0x35, 0x52,
63
0x2c, 0x56, 0x31, 0x2e, 0x33, 0x2d, 0x35, 0x0d,
64
0x0a, 0x45, 0x37, 0x29
70
QemuInputHandlerState *hs;
77
uint8_t outbuf[WC_OUTPUT_BUF_MAX_LEN];
82
int axis[INPUT_AXIS__MAX];
83
bool btns[INPUT_BUTTON__MAX];
86
typedef struct TabletChardev TabletChardev;
88
#define TYPE_CHARDEV_WCTABLET "chardev-wctablet"
89
DECLARE_INSTANCE_CHECKER(TabletChardev, WCTABLET_CHARDEV,
90
TYPE_CHARDEV_WCTABLET)
93
static void wctablet_chr_accept_input(Chardev *chr);
95
static void wctablet_shift_input(TabletChardev *tablet, int count)
97
tablet->query_index -= count;
98
memmove(tablet->query, tablet->query + count, tablet->query_index);
99
tablet->query[tablet->query_index] = 0;
102
static void wctablet_queue_output(TabletChardev *tablet, uint8_t *buf, int count)
104
if (tablet->outlen + count > sizeof(tablet->outbuf)) {
108
memcpy(tablet->outbuf + tablet->outlen, buf, count);
109
tablet->outlen += count;
110
wctablet_chr_accept_input(CHARDEV(tablet));
113
static void wctablet_reset(TabletChardev *tablet)
116
tablet->query_index = 0;
119
tablet->send_events = false;
122
static void wctablet_queue_event(TabletChardev *tablet)
124
uint8_t codes[8] = { 0xe0, 0, 0, 0, 0, 0, 0 };
126
if (tablet->line_speed != 9600) {
130
int newX = tablet->axis[INPUT_AXIS_X] * 0.1537;
131
int nexY = tablet->axis[INPUT_AXIS_Y] * 0.1152;
133
codes[0] = codes[0] | WC_H2(newX);
134
codes[1] = codes[1] | WC_M7(newX);
135
codes[2] = codes[2] | WC_L7(newX);
137
codes[3] = codes[3] | WC_H2(nexY);
138
codes[4] = codes[4] | WC_M7(nexY);
139
codes[5] = codes[5] | WC_L7(nexY);
141
if (tablet->btns[INPUT_BUTTON_LEFT]) {
145
wctablet_queue_output(tablet, codes, 7);
148
static void wctablet_input_event(DeviceState *dev, QemuConsole *src,
151
TabletChardev *tablet = (TabletChardev *)dev;
152
InputMoveEvent *move;
156
case INPUT_EVENT_KIND_ABS:
157
move = evt->u.abs.data;
158
tablet->axis[move->axis] = move->value;
161
case INPUT_EVENT_KIND_BTN:
162
btn = evt->u.btn.data;
163
tablet->btns[btn->button] = btn->down;
172
static void wctablet_input_sync(DeviceState *dev)
174
TabletChardev *tablet = (TabletChardev *)dev;
176
if (tablet->send_events) {
177
wctablet_queue_event(tablet);
181
static const QemuInputHandler wctablet_handler = {
182
.name = "QEMU Wacom Pen Tablet",
183
.mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_ABS,
184
.event = wctablet_input_event,
185
.sync = wctablet_input_sync,
188
static void wctablet_chr_accept_input(Chardev *chr)
190
TabletChardev *tablet = WCTABLET_CHARDEV(chr);
193
canWrite = qemu_chr_be_can_write(chr);
195
if (len > tablet->outlen) {
196
len = tablet->outlen;
200
qemu_chr_be_write(chr, tablet->outbuf, len);
201
tablet->outlen -= len;
202
if (tablet->outlen) {
203
memmove(tablet->outbuf, tablet->outbuf + len, tablet->outlen);
208
static int wctablet_chr_write(struct Chardev *chr,
209
const uint8_t *buf, int len)
211
TabletChardev *tablet = WCTABLET_CHARDEV(chr);
212
unsigned int i, clen;
215
if (tablet->line_speed != 9600) {
218
for (i = 0; i < len && tablet->query_index < sizeof(tablet->query) - 1; i++) {
219
tablet->query[tablet->query_index++] = buf[i];
221
tablet->query[tablet->query_index] = 0;
223
while (tablet->query_index > 0 && (tablet->query[0] == '@' ||
224
tablet->query[0] == '\r' ||
225
tablet->query[0] == '\n')) {
226
wctablet_shift_input(tablet, 1);
228
if (!tablet->query_index) {
232
if (strncmp((char *)tablet->query, "~#", 2) == 0) {
235
wctablet_shift_input(tablet, 2);
236
wctablet_queue_output(tablet, WC_MODEL_STRING,
237
WC_MODEL_STRING_LENGTH);
242
pos = strchr((char *)tablet->query, '\r');
244
pos = strchr((char *)tablet->query, '\n');
249
clen = pos - (char *)tablet->query;
252
if (strncmp((char *)tablet->query, "RE", 2) == 0 &&
255
wctablet_shift_input(tablet, 3);
256
wctablet_queue_output(tablet, WC_CONFIG_STRING,
257
WC_CONFIG_STRING_LENGTH);
259
} else if (strncmp((char *)tablet->query, "ST", 2) == 0 &&
262
wctablet_shift_input(tablet, 3);
263
tablet->send_events = true;
264
wctablet_queue_event(tablet);
266
} else if (strncmp((char *)tablet->query, "SP", 2) == 0 &&
269
wctablet_shift_input(tablet, 3);
270
tablet->send_events = false;
272
} else if (strncmp((char *)tablet->query, "TS", 2) == 0 &&
274
unsigned int input = tablet->query[2];
277
((input & 0x80) == 0) ? 0x7e : 0x7f,
278
(((WC_H4(input) & 0x7) ^ 0x5) << 4) | (WC_L4(input) ^ 0x7),
284
trace_wct_cmd_ts(input);
285
wctablet_shift_input(tablet, 4);
286
wctablet_queue_output(tablet, codes, 7);
289
tablet->query[clen] = 0;
290
trace_wct_cmd_other((char *)tablet->query);
291
wctablet_shift_input(tablet, clen + 1);
298
static int wctablet_chr_ioctl(Chardev *chr, int cmd, void *arg)
300
TabletChardev *tablet = WCTABLET_CHARDEV(chr);
301
QEMUSerialSetParams *ssp;
304
case CHR_IOCTL_SERIAL_SET_PARAMS:
306
if (tablet->line_speed != ssp->speed) {
307
trace_wct_speed(ssp->speed);
308
wctablet_reset(tablet);
309
tablet->line_speed = ssp->speed;
318
static void wctablet_chr_finalize(Object *obj)
320
TabletChardev *tablet = WCTABLET_CHARDEV(obj);
323
qemu_input_handler_unregister(tablet->hs);
327
static void wctablet_chr_open(Chardev *chr,
328
ChardevBackend *backend,
332
TabletChardev *tablet = WCTABLET_CHARDEV(chr);
337
memcpy(tablet->outbuf, WC_FULL_CONFIG_STRING, WC_FULL_CONFIG_STRING_LENGTH);
338
tablet->outlen = WC_FULL_CONFIG_STRING_LENGTH;
339
tablet->query_index = 0;
341
tablet->hs = qemu_input_handler_register((DeviceState *)tablet,
345
static void wctablet_chr_class_init(ObjectClass *oc, void *data)
347
ChardevClass *cc = CHARDEV_CLASS(oc);
349
cc->open = wctablet_chr_open;
350
cc->chr_write = wctablet_chr_write;
351
cc->chr_ioctl = wctablet_chr_ioctl;
352
cc->chr_accept_input = wctablet_chr_accept_input;
355
static const TypeInfo wctablet_type_info = {
356
.name = TYPE_CHARDEV_WCTABLET,
357
.parent = TYPE_CHARDEV,
358
.instance_size = sizeof(TabletChardev),
359
.instance_finalize = wctablet_chr_finalize,
360
.class_init = wctablet_chr_class_init,
363
static void register_types(void)
365
type_register_static(&wctablet_type_info);
368
type_init(register_types);