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
RevLine 
[854eddd6]1/*
[8edec53]2 * Copyright (c) 2021 Jiri Svoboda
[1875a0c]3 * Copyright (c) 2011 Martin Decky
[854eddd6]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
[1875a0c]30/** @addtogroup mouse_proto
[854eddd6]31 * @ingroup input
32 * @{
33 */
[1875a0c]34/**
35 * @file
36 * @brief Mouse device connector controller driver.
[854eddd6]37 */
38
[1875a0c]39#include <stdio.h>
40#include <vfs/vfs_sess.h>
[854eddd6]41#include <async.h>
42#include <errno.h>
[1875a0c]43#include <ipc/mouseev.h>
[cc574511]44#include <loc.h>
[38d150e]45#include <stdlib.h>
[8edec53]46#include <time.h>
[b6a088f]47#include "../mouse.h"
48#include "../mouse_port.h"
49#include "../mouse_proto.h"
50#include "../input.h"
[1875a0c]51
[8edec53]52enum {
53 /** Default double-click speed in milliseconds */
54 dclick_delay_ms = 500
55};
56
[1875a0c]57/** Mousedev softstate */
58typedef struct {
59 /** Link to generic mouse device */
60 mouse_dev_t *mouse_dev;
[8edec53]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;
[1875a0c]65} mousedev_t;
[854eddd6]66
[1875a0c]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;
[a35b458]72
[1875a0c]73 mousedev->mouse_dev = mdev;
[8edec53]74 mousedev->press_bnum = -1;
[a35b458]75
[1875a0c]76 return mousedev;
[854eddd6]77}
78
[1875a0c]79static void mousedev_destroy(mousedev_t *mousedev)
[854eddd6]80{
[1875a0c]81 free(mousedev);
[854eddd6]82}
83
[8edec53]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
[984a9ba]108static void mousedev_callback_conn(ipc_call_t *icall, void *arg)
[854eddd6]109{
[1875a0c]110 /* Mousedev device structure */
111 mousedev_t *mousedev = (mousedev_t *) arg;
[a35b458]112
[854eddd6]113 while (true) {
114 ipc_call_t call;
[984a9ba]115 async_get_call(&call);
[a35b458]116
[fafb8e5]117 if (!ipc_get_imethod(&call)) {
[889cdb1]118 async_answer_0(&call, EOK);
[5288463]119 mousedev_destroy(mousedev);
[854eddd6]120 return;
121 }
[a35b458]122
[b7fd2a0]123 errno_t retval;
[a35b458]124
[fafb8e5]125 switch (ipc_get_imethod(&call)) {
[1875a0c]126 case MOUSEEV_MOVE_EVENT:
[edb3cf2]127 mouse_push_event_move(mousedev->mouse_dev,
[fafb8e5]128 ipc_get_arg1(&call), ipc_get_arg2(&call),
129 ipc_get_arg3(&call));
[1875a0c]130 retval = EOK;
[854eddd6]131 break;
[8a99c7e]132 case MOUSEEV_ABS_MOVE_EVENT:
133 mouse_push_event_abs_move(mousedev->mouse_dev,
[fafb8e5]134 ipc_get_arg1(&call), ipc_get_arg2(&call),
135 ipc_get_arg3(&call), ipc_get_arg4(&call));
[8a99c7e]136 retval = EOK;
137 break;
[1875a0c]138 case MOUSEEV_BUTTON_EVENT:
[edb3cf2]139 mouse_push_event_button(mousedev->mouse_dev,
[fafb8e5]140 ipc_get_arg1(&call), ipc_get_arg2(&call));
[8edec53]141 if (ipc_get_arg2(&call) != 0)
142 mousedev_press(mousedev, ipc_get_arg1(&call));
[1875a0c]143 retval = EOK;
[854eddd6]144 break;
145 default:
146 retval = ENOTSUP;
147 break;
148 }
[a35b458]149
[984a9ba]150 async_answer_0(&call, retval);
[854eddd6]151 }
152}
153
[b7fd2a0]154static errno_t mousedev_proto_init(mouse_dev_t *mdev)
[1875a0c]155{
[f9b2cb4c]156 async_sess_t *sess = loc_service_connect(mdev->svc_id, INTERFACE_DDF, 0);
[1875a0c]157 if (sess == NULL) {
[cce8a83]158 printf("%s: Failed starting session with '%s'\n", NAME,
159 mdev->svc_name);
[3123d2a]160 return ENOENT;
[1875a0c]161 }
[a35b458]162
[1875a0c]163 mousedev_t *mousedev = mousedev_new(mdev);
164 if (mousedev == NULL) {
165 printf("%s: Failed allocating device structure for '%s'.\n",
[cce8a83]166 NAME, mdev->svc_name);
[5288463]167 async_hangup(sess);
[3123d2a]168 return ENOMEM;
[1875a0c]169 }
[a35b458]170
[1875a0c]171 async_exch_t *exch = async_exchange_begin(sess);
172 if (exch == NULL) {
[cc574511]173 printf("%s: Failed starting exchange with '%s'.\n", NAME,
[cce8a83]174 mdev->svc_name);
[1875a0c]175 mousedev_destroy(mousedev);
[5288463]176 async_hangup(sess);
[3123d2a]177 return ENOENT;
[1875a0c]178 }
[a35b458]179
[f9b2cb4c]180 port_id_t port;
[b7fd2a0]181 errno_t rc = async_create_callback_port(exch, INTERFACE_MOUSE_CB, 0, 0,
[f9b2cb4c]182 mousedev_callback_conn, mousedev, &port);
[a35b458]183
[1875a0c]184 async_exchange_end(exch);
[5288463]185 async_hangup(sess);
[a35b458]186
[1875a0c]187 if (rc != EOK) {
188 printf("%s: Failed creating callback connection from '%s'.\n",
[cce8a83]189 NAME, mdev->svc_name);
[1875a0c]190 mousedev_destroy(mousedev);
[3123d2a]191 return rc;
[1875a0c]192 }
[a35b458]193
[3123d2a]194 return EOK;
[1875a0c]195}
196
197mouse_proto_ops_t mousedev_proto = {
198 .init = mousedev_proto_init
199};
200
[854eddd6]201/**
202 * @}
203 */
Note: See TracBrowser for help on using the repository browser.