The Pedigree Project  0.1
HidUtils.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "modules/drivers/common/hid/HidUtils.h"
21 #include "modules/drivers/common/hid/HidUsages.h"
22 #include "pedigree/kernel/machine/HidInputManager.h"
23 #include "pedigree/kernel/machine/InputManager.h"
24 
25 uint64_t
26 HidUtils::getBufferField(uint8_t *pBuffer, size_t nStart, size_t nLength)
27 {
28  uint64_t nValue = 0;
29  size_t i = 0;
30  while (i < nLength)
31  {
32  uint8_t nBits =
33  ((nStart % 8) + nLength - i) < 8 ? nLength % 8 : 8 - (nStart % 8);
34  nValue |= ((pBuffer[nStart / 8] >> (nStart % 8)) & ((1 << nBits) - 1))
35  << i;
36  i += nBits;
37  nStart += nBits;
38  }
39  return nValue;
40 }
41 
42 void HidUtils::fixNegativeMinimum(int64_t &nMin, int64_t nMax)
43 {
44  // Ignore the call if nMax is larger than nMin (no sign bit set in nMin)
45  if (nMax > nMin)
46  return;
47 
48  // Check for all possible sign bits
49  // NOTE this was "nMin = -((1 << bitNum) - nMin)" originally, now may seem
50  // confusing
51  if (nMin < (1LL << 8) && nMin & (1LL << 7)) // Signed 8-bit
52  nMin -= 1LL << 8; // Turn nMin negative
53  else if (nMin < (1LL << 16) && nMin & (1LL << 15)) // Signed 16-bit
54  nMin -= 1LL << 16; // Turn nMin negative
55  else if (nMin < (1LL << 32) && nMin & (1LL << 31)) // Signed 32-bit
56  nMin -= 1LL << 32; // Turn nMin negative
57 }
58 
59 void HidUtils::fixNegativeValue(int64_t nMin, int64_t nMax, int64_t &nValue)
60 {
61  // Ignore the call if nMin is not negative at all
62  if (nMin >= 0)
63  return;
64 
65  // Ignore the call if nMax is larger than nValue (no sign bit set in nValue)
66  if (nMax > nValue)
67  return;
68 
69  // Check for all possible sign bits
70  if (nMin > -(1LL << 8) && nValue < (1LL << 8) &&
71  nValue & (1LL << 7)) // Signed 8-bit
72  nValue -= 1LL << 8; // Turn nValue negative
73  else if (
74  nMin > -(1LL << 16) && nValue < (1LL << 16) &&
75  nValue & (1LL << 15)) // Signed 16-bit
76  nValue -= 1LL << 16; // Turn nValue negative
77  else if (
78  nMin > -(1LL << 32) && nValue < (1LL << 32) &&
79  nValue & (1LL << 31)) // Signed 32-bit
80  nValue -= 1LL << 32; // Turn nValue negative
81 }
82 
84  HidDeviceType deviceType, uint16_t nUsagePage, uint16_t nUsage,
85  int64_t nRelativeValue)
86 {
87  // Button bitmaps
89  static uint32_t mouseButtons = 0;
90  static uint32_t joystickButtons = 0;
91 
92  // Is this a key on a keyboard/keypad?
93  if ((deviceType == Keyboard) && (nUsagePage == HidUsagePages::Keyboard))
94  {
95  if (nRelativeValue > 0)
97  else
99  }
100 
101  // Is this an axis on a mouse?
102  if ((deviceType == Mouse) && (nUsagePage == HidUsagePages::GenericDesktop))
103  {
104  if (nUsage == HidUsages::X)
106  nRelativeValue, 0, 0, mouseButtons);
107  if (nUsage == HidUsages::Y)
109  0, nRelativeValue, 0, mouseButtons);
110  if (nUsage == HidUsages::Wheel)
112  0, 0, nRelativeValue, mouseButtons);
113  }
114 
115  // Is this an axis on a joystick?
116  if ((deviceType == Joystick) &&
117  (nUsagePage == HidUsagePages::GenericDesktop))
118  {
119  if (nUsage == HidUsages::X)
121  nRelativeValue, 0, 0, joystickButtons);
122  if (nUsage == HidUsages::Y)
124  0, nRelativeValue, 0, joystickButtons);
125  if (nUsage == HidUsages::Z)
127  0, 0, nRelativeValue, joystickButtons);
128  }
129 
130  // Is this a button on a mouse?
131  if ((deviceType == Mouse) && (nUsagePage == HidUsagePages::Button))
132  {
133  // Set/unset the bit in the bitmap
134  if (nRelativeValue > 0)
135  mouseButtons |= 1 << (nUsage - 1);
136  else
137  mouseButtons &= ~(1 << (nUsage - 1));
138 
139  // Send the new bitmap to the input manager
140  InputManager::instance().mouseUpdate(0, 0, 0, mouseButtons);
141  }
142 
143  // Is this a button on a joystick?
144  if ((deviceType == Joystick) && (nUsagePage == HidUsagePages::Button))
145  {
146  // Set/unset the bit in the bitmap
147  if (nRelativeValue > 0)
148  joystickButtons |= 1 << (nUsage - 1);
149  else
150  joystickButtons &= ~(1 << (nUsage - 1));
151 
152  // Send the new bitmap to the input manager
153  InputManager::instance().joystickUpdate(0, 0, 0, joystickButtons);
154  }
155 }
void keyUp(uint8_t keyCode)
Called when a key transitions to the "up" state.
void mouseUpdate(ssize_t relX, ssize_t relY, ssize_t relZ, uint32_t buttonBitmap)
Called whenever mouse input comes in.
static HidInputManager & instance()
Singleton design.
void keyDown(uint8_t keyCode)
Called when a key transitions to the "down" state.
void sendInputToManager(HidDeviceType deviceType, uint16_t nUsagePage, uint16_t nUsage, int64_t nRelativeValue)
Sends the input to the right handler in HidInputManager or InputManager.
Definition: HidUtils.cc:83
void joystickUpdate(ssize_t relX, ssize_t relY, ssize_t relZ, uint32_t buttonBitmap)
Called whenever joystick input comes in.
static InputManager & instance()
Singleton design.
Definition: InputManager.h:107
void fixNegativeMinimum(int64_t &nMin, int64_t nMax)
Converts.
Definition: HidUtils.cc:42
uint64_t getBufferField(uint8_t *pBuffer, size_t nStart, size_t nLength)
Retrieves a field in a buffer.
Definition: HidUtils.cc:26
void fixNegativeValue(int64_t nMin, int64_t nMax, int64_t &nValue)
Converts.
Definition: HidUtils.cc:59