2
* Copyright (c) 2012 Martin Sucha
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* - The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
#include <device/char_dev.h>
31
#include <ipc/serial_ctl.h>
34
#include <fibril_synch.h>
35
#include <abi/ipc/methods.h>
36
#include <ipc/mouseev.h>
41
#define NAME "isdv4_tablet"
43
static async_sess_t *client_sess = NULL;
44
static fibril_mutex_t client_mutex;
45
static isdv4_state_t state;
47
static void syntax_print(void)
49
fprintf(stderr, "Usage: %s [--baud=<baud>] [--print-events] [device_service]\n", NAME);
52
static int read_fibril(void *unused)
54
int rc = isdv4_read_events(&state);
56
fprintf(stderr, "Failed reading events");
64
static void mouse_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
66
async_answer_0(iid, EOK);
69
async_callback_receive(EXCHANGE_SERIALIZE);
70
fibril_mutex_lock(&client_mutex);
71
if (client_sess == NULL) {
74
fibril_mutex_unlock(&client_mutex);
78
ipc_callid_t callid = async_get_call(&call);
80
if (!IPC_GET_IMETHOD(call))
83
async_answer_0(callid, ENOTSUP);
87
static void emit_event(const isdv4_event_t *event)
89
fibril_mutex_lock(&client_mutex);
90
async_sess_t *sess = client_sess;
91
fibril_mutex_unlock(&client_mutex);
95
async_exch_t *exch = async_exchange_begin(sess);
97
unsigned int max_x = state.stylus_max_x;
98
unsigned int max_y = state.stylus_max_y;
99
if (event->source == TOUCH) {
100
max_x = state.touch_max_x;
101
max_y = state.touch_max_y;
103
async_msg_4(exch, MOUSEEV_ABS_MOVE_EVENT, event->x, event->y,
105
if (event->type == PRESS || event->type == RELEASE) {
106
async_msg_2(exch, MOUSEEV_BUTTON_EVENT, event->button,
107
event->type == PRESS);
110
async_exchange_end(exch);
113
static void print_and_emit_event(const isdv4_event_t *event)
115
const char *type = NULL;
116
switch (event->type) {
124
type = "PROXIMITY IN";
127
type = "PROXIMITY OUT";
137
const char *source = NULL;
138
switch (event->source) {
140
source = "stylus tip";
143
source = "stylus eraser";
150
printf("%s %s %u %u %u %u\n", type, source, event->x, event->y,
151
event->pressure, event->button);
156
static const char *touch_type(unsigned int data_id)
160
return "resistive+stylus";
162
return "capacitive+stylus";
174
int main(int argc, char **argv)
176
sysarg_t baud = 38400;
178
char *serial_port_name = NULL;
183
isdv4_event_fn event_fn = emit_event;
185
if (argc > arg && str_test_prefix(argv[arg], "--baud=")) {
186
size_t arg_offset = str_lsize(argv[arg], 7);
187
char* arg_str = argv[arg] + arg_offset;
188
if (str_length(arg_str) == 0) {
189
fprintf(stderr, "--baud requires an argument\n");
194
baud = strtol(arg_str, &endptr, 10);
195
if (*endptr != '\0') {
196
fprintf(stderr, "Invalid value for baud\n");
203
if (argc > arg && str_cmp(argv[arg], "--print-events") == 0) {
204
event_fn = print_and_emit_event;
209
serial_port_name = argv[arg];
210
rc = loc_service_get_id(serial_port_name, &svc_id, 0);
212
fprintf(stderr, "Cannot find device service %s\n",
219
category_id_t serial_cat_id;
221
rc = loc_category_get_id("serial", &serial_cat_id, 0);
223
fprintf(stderr, "Failed getting id of category "
228
service_id_t *svc_ids;
231
rc = loc_category_get_svcs(serial_cat_id, &svc_ids, &svc_count); if (rc != EOK) {
232
fprintf(stderr, "Failed getting list of services\n");
236
if (svc_count == 0) {
237
fprintf(stderr, "No service in category 'serial'\n");
244
rc = loc_service_get_name(svc_id, &serial_port_name);
246
fprintf(stderr, "Failed getting name of serial service\n");
254
fprintf(stderr, "Too many arguments\n");
259
fibril_mutex_initialize(&client_mutex);
261
printf(NAME ": Using serial port %s\n", serial_port_name);
263
async_sess_t *sess = loc_service_connect(EXCHANGE_SERIALIZE, svc_id,
266
fprintf(stderr, "Failed connecting to service\n");
269
async_exch_t *exch = async_exchange_begin(sess);
270
rc = async_req_4_0(exch, SERIAL_SET_COM_PROPS, baud,
271
SERIAL_NO_PARITY, 8, 1);
272
async_exchange_end(exch);
275
fprintf(stderr, "Failed setting serial properties\n");
279
rc = isdv4_init(&state, sess, event_fn);
281
fprintf(stderr, "Failed initializing isdv4 state");
285
rc = isdv4_init_tablet(&state);
287
fprintf(stderr, "Failed initializing tablet");
291
printf("Tablet information:\n");
292
printf(" Stylus: %ux%u pressure: %u tilt: ", state.stylus_max_x,
293
state.stylus_max_y, state.stylus_max_pressure);
294
if (state.stylus_tilt_supported) {
295
printf("%ux%u\n", state.stylus_max_xtilt, state.stylus_max_ytilt);
298
printf("not supported\n");
300
printf(" Touch: %ux%u type: %s\n", state.touch_max_x, state.touch_max_y,
301
touch_type(state.touch_type));
303
fid_t fibril = fibril_create(read_fibril, NULL);
304
/* From this on, state is to be used only by read_fibril */
305
fibril_add_ready(fibril);
307
async_set_client_connection(mouse_connection);
308
rc = loc_server_register(NAME);
310
printf("%s: Unable to register driver.\n", NAME);
314
service_id_t service_id;
316
rc = asprintf(&service_name, "mouse/isdv4-%" PRIun, svc_id);
318
printf(NAME ": Unable to create service name\n");
322
rc = loc_service_register(service_name, &service_id);
324
printf(NAME ": Unable to register service %s.\n", service_name);
328
category_id_t mouse_category;
329
rc = loc_category_get_id("mouse", &mouse_category, IPC_FLAG_BLOCKING);
331
printf(NAME ": Unable to get mouse category id.\n");
334
rc = loc_service_add_to_cat(service_id, mouse_category);
336
printf(NAME ": Unable to add device to mouse category.\n");
340
printf("%s: Accepting connections\n", NAME);