1 | /*
|
---|
2 | * Copyright (c) 2011 Vojtech Horky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
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.
|
---|
16 | *
|
---|
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.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup drvusbmouse
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * Actual handling of USB mouse protocol.
|
---|
35 | */
|
---|
36 | #include "mouse.h"
|
---|
37 | #include <usb/debug.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <str_error.h>
|
---|
40 | #include <ipc/mouse.h>
|
---|
41 |
|
---|
42 | /** Mouse polling callback.
|
---|
43 | *
|
---|
44 | * @param dev Device that is being polled.
|
---|
45 | * @param buffer Data buffer.
|
---|
46 | * @param buffer_size Buffer size in bytes.
|
---|
47 | * @param arg Custom argument - points to usb_mouse_t.
|
---|
48 | * @return Always true.
|
---|
49 | */
|
---|
50 | bool usb_mouse_polling_callback(usb_device_t *dev,
|
---|
51 | uint8_t *buffer, size_t buffer_size, void *arg)
|
---|
52 | {
|
---|
53 | usb_mouse_t *mouse = (usb_mouse_t *) arg;
|
---|
54 |
|
---|
55 | usb_log_debug2("got buffer: %s.\n",
|
---|
56 | usb_debug_str_buffer(buffer, buffer_size, 0));
|
---|
57 |
|
---|
58 | uint8_t butt = buffer[0];
|
---|
59 | char str_buttons[4] = {
|
---|
60 | butt & 1 ? '#' : '.',
|
---|
61 | butt & 2 ? '#' : '.',
|
---|
62 | butt & 4 ? '#' : '.',
|
---|
63 | 0
|
---|
64 | };
|
---|
65 |
|
---|
66 | int shift_x = ((int) buffer[1]) - 127;
|
---|
67 | int shift_y = ((int) buffer[2]) - 127;
|
---|
68 | int wheel = ((int) buffer[3]) - 127;
|
---|
69 |
|
---|
70 | if (buffer[1] == 0) {
|
---|
71 | shift_x = 0;
|
---|
72 | }
|
---|
73 | if (buffer[2] == 0) {
|
---|
74 | shift_y = 0;
|
---|
75 | }
|
---|
76 | if (buffer[3] == 0) {
|
---|
77 | wheel = 0;
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (mouse->console_phone >= 0) {
|
---|
81 | if ((shift_x != 0) || (shift_y != 0)) {
|
---|
82 | /* FIXME: guessed for QEMU */
|
---|
83 | async_req_2_0(mouse->console_phone,
|
---|
84 | MEVENT_MOVE,
|
---|
85 | - shift_x / 10, - shift_y / 10);
|
---|
86 | }
|
---|
87 | if (butt) {
|
---|
88 | /* FIXME: proper button clicking. */
|
---|
89 | async_req_2_0(mouse->console_phone,
|
---|
90 | MEVENT_BUTTON, 1, 1);
|
---|
91 | async_req_2_0(mouse->console_phone,
|
---|
92 | MEVENT_BUTTON, 1, 0);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | usb_log_debug("buttons=%s dX=%+3d dY=%+3d wheel=%+3d\n",
|
---|
97 | str_buttons, shift_x, shift_y, wheel);
|
---|
98 |
|
---|
99 | /* Guess. */
|
---|
100 | async_usleep(1000);
|
---|
101 |
|
---|
102 | return true;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /** Callback when polling is terminated.
|
---|
106 | *
|
---|
107 | * @param dev Device where the polling terminated.
|
---|
108 | * @param recurring_errors Whether the polling was terminated due to
|
---|
109 | * recurring errors.
|
---|
110 | * @param arg Custom argument - points to usb_mouse_t.
|
---|
111 | */
|
---|
112 | void usb_mouse_polling_ended_callback(usb_device_t *dev,
|
---|
113 | bool recurring_errors, void *arg)
|
---|
114 | {
|
---|
115 | usb_mouse_t *mouse = (usb_mouse_t *) arg;
|
---|
116 |
|
---|
117 | async_hangup(mouse->console_phone);
|
---|
118 | mouse->console_phone = -1;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * @}
|
---|
123 | */
|
---|