]> git.saurik.com Git - apple/xnu.git/blame - iokit/Drivers/hidsystem/drvAppleADBDevices/AppleADBButtons.cpp
xnu-123.5.tar.gz
[apple/xnu.git] / iokit / Drivers / hidsystem / drvAppleADBDevices / AppleADBButtons.cpp
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22#include "AppleADBButtons.h"
23#include <IOKit/IOLib.h>
24#include <IOKit/pwr_mgt/IOPM.h>
25#include <IOKit/hidsystem/IOHIDTypes.h>
26#include <IOKit/hidsystem/IOHIDParameter.h>
27
28#define super IOHIKeyboard
29OSDefineMetaClassAndStructors(AppleADBButtons,IOHIKeyboard)
30
31bool displayWranglerFound( OSObject *, void *, IOService * );
32void button_data ( IOService * us, UInt8 adbCommand, IOByteCount length, UInt8 * data );
33void asyncFunc ( void * );
34
35// **********************************************************************************
36// start
37//
38// **********************************************************************************
39bool AppleADBButtons::start ( IOService * theNub )
40{
41 int i;
42
43 for ( i = 0; i < kMax_registrations; i++ ) {
44 keycodes[i] = kNullKey;
45 downHandlers[i] = NULL;
46 }
47
48 adbDevice = (IOADBDevice *)theNub;
49
50 if( !super::start(theNub))
51 return false;
52
53 if( !adbDevice->seizeForClient(this, button_data) ) {
54 IOLog("%s: Seize failed\n", getName());
55 return false;
56 }
57
58 addNotification( gIOPublishNotification,serviceMatching("IODisplayWrangler"), // look for the display wrangler
59 (IOServiceNotificationHandler)displayWranglerFound, this, 0 );
60 _initial_handler_id = adbDevice->handlerID();
61
62return true;
63}
64
65UInt64 AppleADBButtons::getGUID()
66{
67 return(kAppleOnboardGUID);
68}
69
70// **********************************************************************************
71// displayWranglerFound
72//
73// The Display Wrangler has appeared. We will be calling its
74// ActivityTickle method when there is user activity.
75// **********************************************************************************
76bool displayWranglerFound( OSObject * us, void * ref, IOService * yourDevice )
77{
78 if ( yourDevice != NULL ) {
79 ((AppleADBButtons *)us)->displayManager = yourDevice;
80 }
81 return true;
82}
83
84UInt32 AppleADBButtons::interfaceID()
85{
86 return NX_EVS_DEVICE_INTERFACE_ADB;
87}
88
89UInt32 AppleADBButtons::deviceType()
90{
91 return adbDevice->handlerID();
92}
93
94// **********************************************************************************
95// registerForButton
96//
97// Clients call here, specifying a button and a routine to call when that
98// button is pressed or released.
99// **********************************************************************************
100IOReturn AppleADBButtons::registerForButton ( unsigned int keycode, IOService * registrant, button_handler handler, bool down )
101{
102 int i;
103
104 for ( i = 0; i < kMax_registrations; i++ ) {
105 if ( keycodes[i] == kNullKey ) {
106 if ( down ) {
107 registrants[i] = registrant;
108 downHandlers[i] = handler;
109 keycodes[i] = keycode;
110 break;
111 }
112 }
113 }
114 return kIOReturnSuccess;
115}
116
117// **********************************************************************************
118// button_data
119//
120// **********************************************************************************
121void button_data ( IOService * us, UInt8 adbCommand, IOByteCount length, UInt8 * data )
122{
123((AppleADBButtons *)us)->packet(data,length,adbCommand);
124}
125
126
127// **********************************************************************************
128// packet
129//
130// **********************************************************************************
131IOReturn AppleADBButtons::packet (UInt8 * data, IOByteCount, UInt8 adbCommand )
132{
133 unsigned int keycode;
134 bool down;
135
136 keycode = *data;
137 down = ((keycode & 0x80) == 0);
138 keycode &= 0x7f;
139 dispatchButtonEvent(keycode,down);
140
141 keycode = *(data + 1);
142 if( keycode != 0xff ) {
143 down = ((keycode & 0x80) == 0);
144 keycode &= 0x7f;
145 dispatchButtonEvent(keycode,down);
146 }
147
148 if ( displayManager != NULL ) { // if there is a display manager, tell
149 displayManager->activityTickle(kIOPMSuperclassPolicy1); // it there is user activity
150 }
151
152 return kIOReturnSuccess;
153}
154
155
156// **********************************************************************************
157// dispatchButtonEvent
158//
159// Look for any registered handlers for this button and notify them.
160// **********************************************************************************
161void AppleADBButtons::dispatchButtonEvent (unsigned int keycode, bool down )
162{
163 int i;
164 AbsoluteTime now;
165
166 if (_initial_handler_id == 0xc0) //For Apple ADB AV and ColorSync monitors
167 {
168 switch (keycode)
169 {
170 case kVolume_up_AV:
171 keycode = kVolume_up;
172 break;
173 case kVolume_down_AV:
174 keycode = kVolume_down;
175 break;
176 case kMute_AV:
177 keycode = kMute;
178 break;
179 default:
180 //No other volume codes are available for OS X
181 break;
182 }
183 }
184
185 clock_get_uptime(&now);
186
187 for ( i = 0; i < kMax_registrations; i++ ) {
188 if ( keycodes[i] == keycode ) {
189 if ( down ) {
190 if (downHandlers[i] != NULL ) {
191 thread_call_func((thread_call_func_t)downHandlers[i],
192 (thread_call_param_t)registrants[i],
193 true);
194 }
195 }
196 }
197 }
198
199 //Only dispatch keycodes that this driver understands.
200 // See appleADBButtonsKeyMap[] for the list.
201 switch (keycode)
202 {
203 case kVolume_up:
204 case kVolume_down:
205 case kMute:
206 case kEject:
207 case kBrightness_up:
208 case kBrightness_down:
209 case kNum_lock_on_laptops:
210 dispatchKeyboardEvent(keycode, down, now);
211 break;
212 default: //Don't dispatch anything else
213 break;
214 }
215}
216
217const unsigned char *AppleADBButtons::defaultKeymapOfLength(UInt32 *length)
218{
219 static const unsigned char appleADBButtonsKeyMap[] = {
220 0x00, 0x00, // chars
221 0x00, // no modifier keys
222 0x00, // no defs
223 0x00, // no seqs
224 0x07, // 6 special keys
225 NX_KEYTYPE_SOUND_UP, kVolume_up,
226 NX_KEYTYPE_SOUND_DOWN, kVolume_down,
227 NX_KEYTYPE_MUTE, kMute,
228 NX_KEYTYPE_BRIGHTNESS_UP, kBrightness_up,
229 NX_KEYTYPE_BRIGHTNESS_DOWN, kBrightness_down,
230 NX_KEYTYPE_NUM_LOCK, kNum_lock_on_laptops,
231 NX_KEYTYPE_EJECT, kEject
232 };
233
234 *length = sizeof(appleADBButtonsKeyMap);
235
236 return appleADBButtonsKeyMap;
237}
238
239IOReturn AppleADBButtons::setParamProperties(OSDictionary *dict)
240{
241 dict->removeObject(kIOHIDKeyMappingKey);
242
243 return super::setParamProperties(dict);
244}