1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   HID Mouse Client Class                                              */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 #include "ux_host_class_hid.h"
29 #include "ux_host_class_hid_mouse.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_hid_mouse_callback                   PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function is the callback mechanism for a report registration.  */
46 /*    For the mouse, we filter the mouse coordinate changes and the       */
47 /*    state of the buttons.                                               */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    callback                              Pointer to callback           */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    None                                                                */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    None                                                                */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    HID Class                                                           */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*                                                                        */
73 /**************************************************************************/
_ux_host_class_hid_mouse_callback(UX_HOST_CLASS_HID_REPORT_CALLBACK * callback)74 VOID  _ux_host_class_hid_mouse_callback(UX_HOST_CLASS_HID_REPORT_CALLBACK *callback)
75 {
76 
77 UX_HOST_CLASS_HID_CLIENT    *hid_client;
78 UX_HOST_CLASS_HID_MOUSE     *mouse_instance;
79 
80     /* Get the HID client instance that issued the callback.  */
81     hid_client =  callback -> ux_host_class_hid_report_callback_client;
82 
83     /* Get the mouse local instance */
84     mouse_instance =  (UX_HOST_CLASS_HID_MOUSE *) hid_client -> ux_host_class_hid_client_local_instance;
85 
86     /* Analyze the usage we have received.  */
87     switch (callback -> ux_host_class_hid_report_callback_usage)
88     {
89 
90 
91         /* X/Y Axis movement.  */
92         case    UX_HOST_CLASS_HID_MOUSE_AXIS_X      :
93 
94             /* Add the deplacement to the position.  */
95             mouse_instance -> ux_host_class_hid_mouse_x_position += (SCHAR) callback -> ux_host_class_hid_report_callback_value;
96 
97             break;
98 
99         case    UX_HOST_CLASS_HID_MOUSE_AXIS_Y      :
100 
101             /* Add the deplacement to the position.  */
102             mouse_instance  -> ux_host_class_hid_mouse_y_position += (SCHAR) callback -> ux_host_class_hid_report_callback_value;
103             break;
104 
105         /* Buttons.  */
106         case    UX_HOST_CLASS_HID_MOUSE_BUTTON_1    :
107 
108             /* Check the state of button 1.  */
109             if (callback -> ux_host_class_hid_report_callback_value == UX_TRUE)
110                 mouse_instance  -> ux_host_class_hid_mouse_buttons |= UX_HOST_CLASS_HID_MOUSE_BUTTON_1_PRESSED;
111             else
112                 mouse_instance  -> ux_host_class_hid_mouse_buttons &= (ULONG)~UX_HOST_CLASS_HID_MOUSE_BUTTON_1_PRESSED;
113             break;
114 
115         case    UX_HOST_CLASS_HID_MOUSE_BUTTON_2    :
116 
117             /* Check the state of button 2.  */
118             if (callback -> ux_host_class_hid_report_callback_value == UX_TRUE)
119                 mouse_instance  -> ux_host_class_hid_mouse_buttons |= UX_HOST_CLASS_HID_MOUSE_BUTTON_2_PRESSED;
120             else
121                 mouse_instance  -> ux_host_class_hid_mouse_buttons &= (ULONG)~UX_HOST_CLASS_HID_MOUSE_BUTTON_2_PRESSED;
122             break;
123 
124         case    UX_HOST_CLASS_HID_MOUSE_BUTTON_3    :
125 
126             /* Check the state of button 3.  */
127             if (callback -> ux_host_class_hid_report_callback_value == UX_TRUE)
128                 mouse_instance  -> ux_host_class_hid_mouse_buttons |= UX_HOST_CLASS_HID_MOUSE_BUTTON_3_PRESSED;
129             else
130                 mouse_instance  -> ux_host_class_hid_mouse_buttons &= (ULONG)~UX_HOST_CLASS_HID_MOUSE_BUTTON_3_PRESSED;
131             break;
132 
133 
134         /* Wheel movement.  */
135         case    UX_HOST_CLASS_HID_MOUSE_WHEEL      :
136 
137             mouse_instance -> ux_host_class_hid_mouse_wheel += (SCHAR) callback -> ux_host_class_hid_report_callback_value;
138 
139             break;
140 
141         default :
142 
143             /* We have received a Usage we don't know about. Ignore it.  */
144             break;
145     }
146 
147     /* Return to caller.  */
148     return;
149 }
150 
151