2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
22 #include "AppleADBButtons.h"
23 #include "AppleADBKeyboard.h"
24 #include <IOKit/IOLib.h>
25 #include <IOKit/pwr_mgt/IOPM.h>
26 #include <IOKit/hidsystem/IOHIDTypes.h>
27 #include <IOKit/hidsystem/IOHIDParameter.h>
29 #define super IOHIKeyboard
30 OSDefineMetaClassAndStructors(AppleADBButtons
,IOHIKeyboard
)
32 bool displayWranglerFound( OSObject
*, void *, IOService
* );
33 void button_data ( IOService
* us
, UInt8 adbCommand
, IOByteCount length
, UInt8
* data
);
34 void asyncFunc ( void * );
36 // **********************************************************************************
39 // **********************************************************************************
40 bool AppleADBButtons::start ( IOService
* theNub
)
44 for ( i
= 0; i
< kMax_registrations
; i
++ ) {
45 keycodes
[i
] = kNullKey
;
46 downHandlers
[i
] = NULL
;
49 adbDevice
= (IOADBDevice
*)theNub
;
51 if( !super::start(theNub
))
54 if( !adbDevice
->seizeForClient(this, button_data
) ) {
55 IOLog("%s: Seize failed\n", getName());
59 addNotification( gIOPublishNotification
,serviceMatching("IODisplayWrangler"), // look for the display wrangler
60 (IOServiceNotificationHandler
)displayWranglerFound
, this, 0 );
61 _initial_handler_id
= adbDevice
->handlerID();
66 UInt64
AppleADBButtons::getGUID()
68 return(kAppleOnboardGUID
);
71 // **********************************************************************************
72 // displayWranglerFound
74 // The Display Wrangler has appeared. We will be calling its
75 // ActivityTickle method when there is user activity.
76 // **********************************************************************************
77 bool displayWranglerFound( OSObject
* us
, void * ref
, IOService
* yourDevice
)
79 if ( yourDevice
!= NULL
) {
80 ((AppleADBButtons
*)us
)->displayManager
= yourDevice
;
85 UInt32
AppleADBButtons::interfaceID()
87 return NX_EVS_DEVICE_INTERFACE_ADB
;
90 UInt32
AppleADBButtons::deviceType()
92 //if initial id is 31 then this is a post-WallStreet PowerBook, so
93 //look for a AppleADBKeyboard driver and return the handler ID from it.
94 //Note though that the ADB keyboard driver may not exist.
95 if (_initial_handler_id
== 31)
98 AppleADBKeyboard
*adbkeyboard
;
102 adbkeyboard
= (AppleADBKeyboard
*)waitForService(serviceMatching("AppleADBKeyboard"), &t
);
105 return adbkeyboard
->deviceType();
110 return adbDevice
->handlerID();
113 // **********************************************************************************
116 // Clients call here, specifying a button and a routine to call when that
117 // button is pressed or released.
118 // **********************************************************************************
119 IOReturn
AppleADBButtons::registerForButton ( unsigned int keycode
, IOService
* registrant
, button_handler handler
, bool down
)
123 for ( i
= 0; i
< kMax_registrations
; i
++ ) {
124 if ( keycodes
[i
] == kNullKey
) {
126 registrants
[i
] = registrant
;
127 downHandlers
[i
] = handler
;
128 keycodes
[i
] = keycode
;
133 return kIOReturnSuccess
;
136 // **********************************************************************************
139 // **********************************************************************************
140 void button_data ( IOService
* us
, UInt8 adbCommand
, IOByteCount length
, UInt8
* data
)
142 ((AppleADBButtons
*)us
)->packet(data
,length
,adbCommand
);
146 // **********************************************************************************
149 // **********************************************************************************
150 IOReturn
AppleADBButtons::packet (UInt8
* data
, IOByteCount
, UInt8 adbCommand
)
152 unsigned int keycode
;
156 down
= ((keycode
& 0x80) == 0);
158 dispatchButtonEvent(keycode
,down
);
160 keycode
= *(data
+ 1);
161 if( keycode
!= 0xff ) {
162 down
= ((keycode
& 0x80) == 0);
164 dispatchButtonEvent(keycode
,down
);
167 if ( displayManager
!= NULL
) { // if there is a display manager, tell
168 displayManager
->activityTickle(kIOPMSuperclassPolicy1
); // it there is user activity
171 return kIOReturnSuccess
;
175 // **********************************************************************************
176 // dispatchButtonEvent
178 // Look for any registered handlers for this button and notify them.
179 // **********************************************************************************
180 void AppleADBButtons::dispatchButtonEvent (unsigned int keycode
, bool down
)
185 if (_initial_handler_id
== 0xc0) //For Apple ADB AV and ColorSync monitors
190 keycode
= kVolume_up
;
192 case kVolume_down_AV
:
193 keycode
= kVolume_down
;
199 //No other volume codes are available for OS X
204 clock_get_uptime(&now
);
206 for ( i
= 0; i
< kMax_registrations
; i
++ ) {
207 if ( keycodes
[i
] == keycode
) {
209 if (downHandlers
[i
] != NULL
) {
210 thread_call_func((thread_call_func_t
)downHandlers
[i
],
211 (thread_call_param_t
)registrants
[i
],
218 //Only dispatch keycodes that this driver understands.
219 // See appleADBButtonsKeyMap[] for the list.
227 case kBrightness_down
:
228 case kNum_lock_on_laptops
:
229 dispatchKeyboardEvent(keycode
, down
, now
);
231 default: //Don't dispatch anything else
236 const unsigned char *AppleADBButtons::defaultKeymapOfLength(UInt32
*length
)
238 static const unsigned char appleADBButtonsKeyMap
[] = {
240 0x00, // no modifier keys
243 0x07, // 6 special keys
244 NX_KEYTYPE_SOUND_UP
, kVolume_up
,
245 NX_KEYTYPE_SOUND_DOWN
, kVolume_down
,
246 NX_KEYTYPE_MUTE
, kMute
,
247 NX_KEYTYPE_BRIGHTNESS_UP
, kBrightness_up
,
248 NX_KEYTYPE_BRIGHTNESS_DOWN
, kBrightness_down
,
249 NX_KEYTYPE_NUM_LOCK
, kNum_lock_on_laptops
,
250 NX_KEYTYPE_EJECT
, kEject
253 *length
= sizeof(appleADBButtonsKeyMap
);
255 return appleADBButtonsKeyMap
;
258 IOReturn
AppleADBButtons::setParamProperties(OSDictionary
*dict
)
260 dict
->removeObject(kIOHIDKeyMappingKey
);
262 return super::setParamProperties(dict
);