source: mainline/uspace/srv/hid/input/proto/mousedev.c

Last change on this file was 8edec53, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Support double-click

Needed to open Navigator entries using the mouse.

  • Property mode set to 100644
File size: 5.2 KB
Line 
1/*
2 * Copyright (c) 2021 Jiri Svoboda
3 * Copyright (c) 2011 Martin Decky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup mouse_proto
31 * @ingroup input
32 * @{
33 */
34/**
35 * @file
36 * @brief Mouse device connector controller driver.
37 */
38
39#include <stdio.h>
40#include <vfs/vfs_sess.h>
41#include <async.h>
42#include <errno.h>
43#include <ipc/mouseev.h>
44#include <loc.h>
45#include <stdlib.h>
46#include <time.h>
47#include "../mouse.h"
48#include "../mouse_port.h"
49#include "../mouse_proto.h"
50#include "../input.h"
51
52enum {
53 /** Default double-click speed in milliseconds */
54 dclick_delay_ms = 500
55};
56
57/** Mousedev softstate */
58typedef struct {
59 /** Link to generic mouse device */
60 mouse_dev_t *mouse_dev;
61 /** Button number of last button pressed (or -1 if none) */
62 int press_bnum;
63 /** Time at which button was last pressed */
64 struct timespec press_time;
65} mousedev_t;
66
67static mousedev_t *mousedev_new(mouse_dev_t *mdev)
68{
69 mousedev_t *mousedev = calloc(1, sizeof(mousedev_t));
70 if (mousedev == NULL)
71 return NULL;
72
73 mousedev->mouse_dev = mdev;
74 mousedev->press_bnum = -1;
75
76 return mousedev;
77}
78
79static void mousedev_destroy(mousedev_t *mousedev)
80{
81 free(mousedev);
82}
83
84static void mousedev_press(mousedev_t *mousedev, int bnum)
85{
86 struct timespec now;
87 nsec_t ms_delay;
88
89 getuptime(&now);
90
91 /* Same button was pressed previously */
92 if (mousedev->press_bnum == bnum) {
93 /* Compute milliseconds since previous press */
94 ms_delay = ts_sub_diff(&now, &mousedev->press_time) / 1000000;
95
96 if (ms_delay <= dclick_delay_ms) {
97 mouse_push_event_dclick(mousedev->mouse_dev, bnum);
98 mousedev->press_bnum = -1;
99 return;
100 }
101 }
102
103 /* Record which button was last pressed and at what time */
104 mousedev->press_bnum = bnum;
105 mousedev->press_time = now;
106}
107
108static void mousedev_callback_conn(ipc_call_t *icall, void *arg)
109{
110 /* Mousedev device structure */
111 mousedev_t *mousedev = (mousedev_t *) arg;
112
113 while (true) {
114 ipc_call_t call;
115 async_get_call(&call);
116
117 if (!ipc_get_imethod(&call)) {
118 async_answer_0(&call, EOK);
119 mousedev_destroy(mousedev);
120 return;
121 }
122
123 errno_t retval;
124
125 switch (ipc_get_imethod(&call)) {
126 case MOUSEEV_MOVE_EVENT:
127 mouse_push_event_move(mousedev->mouse_dev,
128 ipc_get_arg1(&call), ipc_get_arg2(&call),
129 ipc_get_arg3(&call));
130 retval = EOK;
131 break;
132 case MOUSEEV_ABS_MOVE_EVENT:
133 mouse_push_event_abs_move(mousedev->mouse_dev,
134 ipc_get_arg1(&call), ipc_get_arg2(&call),
135 ipc_get_arg3(&call), ipc_get_arg4(&call));
136 retval = EOK;
137 break;
138 case MOUSEEV_BUTTON_EVENT:
139 mouse_push_event_button(mousedev->mouse_dev,
140 ipc_get_arg1(&call), ipc_get_arg2(&call));
141 if (ipc_get_arg2(&call) != 0)
142 mousedev_press(mousedev, ipc_get_arg1(&call));
143 retval = EOK;
144 break;
145 default:
146 retval = ENOTSUP;
147 break;
148 }
149
150 async_answer_0(&call, retval);
151 }
152}
153
154static errno_t mousedev_proto_init(mouse_dev_t *mdev)
155{
156 async_sess_t *sess = loc_service_connect(mdev->svc_id, INTERFACE_DDF, 0);
157 if (sess == NULL) {
158 printf("%s: Failed starting session with '%s'\n", NAME,
159 mdev->svc_name);
160 return ENOENT;
161 }
162
163 mousedev_t *mousedev = mousedev_new(mdev);
164 if (mousedev == NULL) {
165 printf("%s: Failed allocating device structure for '%s'.\n",
166 NAME, mdev->svc_name);
167 async_hangup(sess);
168 return ENOMEM;
169 }
170
171 async_exch_t *exch = async_exchange_begin(sess);
172 if (exch == NULL) {
173 printf("%s: Failed starting exchange with '%s'.\n", NAME,
174 mdev->svc_name);
175 mousedev_destroy(mousedev);
176 async_hangup(sess);
177 return ENOENT;
178 }
179
180 port_id_t port;
181 errno_t rc = async_create_callback_port(exch, INTERFACE_MOUSE_CB, 0, 0,
182 mousedev_callback_conn, mousedev, &port);
183
184 async_exchange_end(exch);
185 async_hangup(sess);
186
187 if (rc != EOK) {
188 printf("%s: Failed creating callback connection from '%s'.\n",
189 NAME, mdev->svc_name);
190 mousedev_destroy(mousedev);
191 return rc;
192 }
193
194 return EOK;
195}
196
197mouse_proto_ops_t mousedev_proto = {
198 .init = mousedev_proto_init
199};
200
201/**
202 * @}
203 */
Note: See TracBrowser for help on using the repository browser.