source: mainline/uspace/drv/usbmouse/mouse.c@ d3594362

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d3594362 was 21bb58d, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

USB mouse sends events to console

The values are not properly scaled, the clicking is very simple
(doubt that the console is able to handle something more
complicated) but it is possible to switch consoles with the mouse.

Tested in QEMU only.

  • Property mode set to 100644
File size: 3.3 KB
Line 
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 <ipc/mouse.h>
40
41int usb_mouse_polling_fibril(void *arg)
42{
43 assert(arg != NULL);
44 ddf_dev_t *dev = (ddf_dev_t *) arg;
45 usb_mouse_t *mouse = (usb_mouse_t *) dev->driver_data;
46
47 assert(mouse);
48
49 size_t buffer_size = mouse->poll_pipe.max_packet_size;
50
51 if (buffer_size < 4) {
52 usb_log_error("Weird mouse, results will be skewed.\n");
53 buffer_size = 4;
54 }
55
56 uint8_t *buffer = malloc(buffer_size);
57 if (buffer == NULL) {
58 usb_log_error("Out of memory, poll fibril aborted.\n");
59 return ENOMEM;
60 }
61
62 while (true) {
63 async_usleep(mouse->poll_interval_us);
64
65 size_t actual_size;
66
67 /* FIXME: check for errors. */
68 usb_endpoint_pipe_start_session(&mouse->poll_pipe);
69
70 usb_endpoint_pipe_read(&mouse->poll_pipe,
71 buffer, buffer_size, &actual_size);
72
73 usb_endpoint_pipe_end_session(&mouse->poll_pipe);
74
75 uint8_t butt = buffer[0];
76 char str_buttons[4] = {
77 butt & 1 ? '#' : '.',
78 butt & 2 ? '#' : '.',
79 butt & 4 ? '#' : '.',
80 0
81 };
82
83 int shift_x = ((int) buffer[1]) - 127;
84 int shift_y = ((int) buffer[2]) - 127;
85 int wheel = ((int) buffer[3]) - 127;
86
87 if (buffer[1] == 0) {
88 shift_x = 0;
89 }
90 if (buffer[2] == 0) {
91 shift_y = 0;
92 }
93 if (buffer[3] == 0) {
94 wheel = 0;
95 }
96
97 if (mouse->console_phone >= 0) {
98 if ((shift_x != 0) || (shift_y != 0)) {
99 /* FIXME: guessed for QEMU */
100 async_req_2_0(mouse->console_phone,
101 MEVENT_MOVE,
102 - shift_x / 10, - shift_y / 10);
103 }
104 if (butt) {
105 /* FIXME: proper button clicking. */
106 async_req_2_0(mouse->console_phone,
107 MEVENT_BUTTON, 1, 1);
108 async_req_2_0(mouse->console_phone,
109 MEVENT_BUTTON, 1, 0);
110 }
111 }
112
113 usb_log_debug("buttons=%s dX=%+3d dY=%+3d wheel=%+3d\n",
114 str_buttons, shift_x, shift_y, wheel);
115 }
116
117 return EOK;
118}
119
120
121/**
122 * @}
123 */
Note: See TracBrowser for help on using the repository browser.