1/*
2 * @APPLE_LICENSE_HEADER_START@
3 *
4 * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/* 	Copyright (c) 1992 NeXT Computer, Inc.  All rights reserved.
24 *
25 * EventSrcPCKeyboard.h - PC Keyboard EventSrc subclass definition
26 *
27 * HISTORY
28 * 28 Aug 1992    Joe Pasqua
29 *      Created.
30 */
31
32#ifndef _IOHIKEYBOARD_H
33#define _IOHIKEYBOARD_H
34
35#include <IOKit/hidsystem/IOHIDevice.h>
36#include <IOKit/hidsystem/IOHIKeyboardMapper.h>
37
38/* Start Action Definitions */
39
40/*
41 * HISTORICAL NOTE:
42 *   The following entry points were part of the IOHIKeyboardEvents
43 *   protocol.
44 */
45
46typedef void (*KeyboardEventAction)(       OSObject * target,
47                    /* eventFlags  */      unsigned   eventType,
48                    /* flags */            unsigned   flags,
49                    /* keyCode */          unsigned   key,
50                    /* charCode */         unsigned   charCode,
51                    /* charSet */          unsigned   charSet,
52                    /* originalCharCode */ unsigned   origCharCode,
53                    /* originalCharSet */  unsigned   origCharSet,
54                    /* keyboardType */     unsigned   keyboardType,
55                    /* repeat */           bool       repeat,
56                    /* atTime */           AbsoluteTime ts);
57
58typedef void (*KeyboardSpecialEventAction)(OSObject * target,
59                    /* eventType */        unsigned   eventType,
60                    /* flags */            unsigned   flags,
61                    /* keyCode */          unsigned   key,
62                    /* specialty */        unsigned   flavor,
63                    /* source id */        UInt64     guid,
64                    /* repeat */           bool       repeat,
65                    /* atTime */           AbsoluteTime ts);
66
67typedef void (*UpdateEventFlagsAction)(    OSObject * target,
68                     /* flags */           unsigned   flags);
69
70/* Event Callback Definitions */
71
72typedef void (*KeyboardEventCallback)(
73                    /* target */           OSObject * target,
74                    /* eventFlags  */      unsigned   eventType,
75                    /* flags */            unsigned   flags,
76                    /* keyCode */          unsigned   key,
77                    /* charCode */         unsigned   charCode,
78                    /* charSet */          unsigned   charSet,
79                    /* originalCharCode */ unsigned   origCharCode,
80                    /* originalCharSet */  unsigned   origCharSet,
81                    /* keyboardType */     unsigned   keyboardType,
82                    /* repeat */           bool       repeat,
83                    /* atTime */           AbsoluteTime ts,
84                    /* sender */           OSObject * sender,
85                    /* refcon */           void *     refcon);
86
87typedef void (*KeyboardSpecialEventCallback)(
88                    /* target */           OSObject * target,
89                    /* eventType */        unsigned   eventType,
90                    /* flags */            unsigned   flags,
91                    /* keyCode */          unsigned   key,
92                    /* specialty */        unsigned   flavor,
93                    /* source id */        UInt64     guid,
94                    /* repeat */           bool       repeat,
95                    /* atTime */           AbsoluteTime ts,
96                    /* sender */           OSObject * sender,
97                    /* refcon */           void *     refcon);
98
99typedef void (*UpdateEventFlagsCallback)(
100                    /* target */           OSObject * target,
101                    /* flags */            unsigned   flags,
102                    /* sender */           OSObject * sender,
103                    /* refcon */           void *     refcon);
104
105/* End Action Definitions */
106
107/* Default key repeat parameters */
108#define EV_DEFAULTINITIALREPEAT 500000000ULL    // 1/2 sec in nanoseconds
109#define EV_DEFAULTKEYREPEAT     83333333ULL     // 1/12 sec in nanoseconds
110#define EV_MINKEYREPEAT         16700000ULL     // 1/60 sec
111
112class IOHIKeyboard : public IOHIDevice
113{
114    OSDeclareDefaultStructors(IOHIKeyboard);
115
116    friend class IOHIDKeyboardDevice;
117	friend class IOHIDKeyboard;
118	friend class IOHIDConsumer;
119
120protected:
121    IOLock *	         _deviceLock;	// Lock for all device access
122    IOHIKeyboardMapper * _keyMap;	// KeyMap instance
123
124    // The following fields describe the kind of keyboard
125    UInt32		_interfaceType;
126    UInt32		_deviceType;
127
128    // The following fields describe the state of the keyboard
129    UInt32 *	_keyState;		// kbdBitVector
130    IOByteCount _keyStateSize;		// kbdBitVector allocated size
131    unsigned	_eventFlags;		// Current eventFlags
132    bool	_alphaLock;		// true means alpha lock is on
133    bool	_numLock;		// true means num lock is on
134    bool	_charKeyActive;		// true means char gen. key active
135
136    // The following fields are used in performing key repeats
137    bool 	_isRepeat;		// true means we're generating repeat
138    unsigned	_codeToRepeat;		// What we are repeating
139    bool	_calloutPending;	// true means we've sched. a callout
140    AbsoluteTime	_lastEventTime;		// Time last event was dispatched
141    AbsoluteTime	_downRepeatTime;	// Time when we should next repeat
142    AbsoluteTime	_keyRepeat;		// Delay between key repeats
143    AbsoluteTime	_initialKeyRepeat;	// Delay before initial key repeat
144    UInt64		_guid;
145
146    OSObject *                 _keyboardEventTarget;
147    KeyboardEventAction        _keyboardEventAction;
148    OSObject *                 _keyboardSpecialEventTarget;
149    KeyboardSpecialEventAction _keyboardSpecialEventAction;
150    OSObject *                 _updateEventFlagsTarget;
151    UpdateEventFlagsAction     _updateEventFlagsAction;
152
153    UInt16      _lastUsagePage;
154    UInt16      _lastUsage;
155
156protected:
157  virtual void dispatchKeyboardEvent(unsigned int keyCode,
158		     /* direction */ bool         goingDown,
159		     /* timeStamp */ AbsoluteTime time);
160    void    setLastPageAndUsage(UInt16 usagePage, UInt16 usage);
161    void    getLastPageAndUsage(UInt16 &usagePage, UInt16 &usage);
162    void    clearLastPageAndUsage();
163
164public:
165  virtual bool init(OSDictionary * properties = 0);
166  virtual bool start(IOService * provider);
167  virtual void stop(IOService * provider);
168  virtual void free();
169
170  virtual bool open(IOService *                client,
171		    IOOptionBits	       options,
172                    KeyboardEventAction        keAction,
173                    KeyboardSpecialEventAction kseAction,
174                    UpdateEventFlagsAction     uefAction);
175
176  bool open(        IOService *                  client,
177		    IOOptionBits	         options,
178                    void *,
179                    KeyboardEventCallback        keCallback,
180                    KeyboardSpecialEventCallback kseCallback,
181                    UpdateEventFlagsCallback     uefCallback);
182
183  virtual void close(IOService * client, IOOptionBits );
184
185  virtual IOReturn message( UInt32 type, IOService * provider,
186                    void * argument = 0 );
187
188  virtual IOHIDKind hidKind();
189  virtual bool 	    updateProperties( void );
190  virtual IOReturn  setParamProperties(OSDictionary * dict);
191  virtual IOReturn  setProperties( OSObject * properties );
192
193  inline  bool	    isRepeat() {return _isRepeat;}
194
195protected: // for subclasses to implement
196  virtual const unsigned char * defaultKeymapOfLength(UInt32 * length);
197  virtual void setAlphaLockFeedback(bool val);
198  virtual void setNumLockFeedback(bool val);
199  virtual UInt32 maxKeyCodes();
200
201
202private:
203  virtual bool resetKeyboard();
204  virtual void scheduleAutoRepeat();
205  static void _autoRepeat(void * arg, void *);
206  virtual void autoRepeat();
207  virtual void setRepeat(unsigned eventType, unsigned keyCode);
208  void setRepeatMode(bool repeat);
209  static void _createKeyboardNub(thread_call_param_t param0, thread_call_param_t param1);
210
211/*
212 * HISTORICAL NOTE:
213 *   The following methods were part of the KeyMapDelegate protocol;
214 *   the declarations have now been merged directly into this class.
215 */
216
217public:
218  virtual void keyboardEvent(unsigned eventType,
219      /* flags */            unsigned flags,
220      /* keyCode */          unsigned keyCode,
221      /* charCode */         unsigned charCode,
222      /* charSet */          unsigned charSet,
223      /* originalCharCode */ unsigned origCharCode,
224      /* originalCharSet */  unsigned origCharSet);
225
226  virtual void keyboardSpecialEvent(unsigned eventType,
227		    /* flags */     unsigned flags,
228		    /* keyCode */   unsigned keyCode,
229		    /* specialty */ unsigned flavor);
230
231  virtual void updateEventFlags(unsigned flags); // Does not generate events
232
233  virtual unsigned eventFlags();           // Global event flags
234  virtual unsigned deviceFlags();          // per-device event flags
235  virtual void setDeviceFlags(unsigned flags); // Set device event flags
236  virtual bool alphaLock();                // current alpha-lock state
237  virtual void setAlphaLock(bool val);     // Set current alpha-lock state
238  virtual bool numLock();
239  virtual void setNumLock(bool val);
240  virtual bool charKeyActive();            // Is a character gen. key down?
241  virtual void setCharKeyActive(bool val); // Note that a char gen key is down.
242  virtual bool doesKeyLock(unsigned key);  //does key lock physically
243  virtual unsigned getLEDStatus();  //check hardware for LED status
244
245    virtual IOReturn newUserClient( task_t          owningTask,
246                                   void *          security_id,
247                                   UInt32          type,
248                                   OSDictionary *  properties,
249                                   IOUserClient ** handler );
250    IOReturn newUserClientGated(task_t          owningTask,
251                                void *          security_id,
252                                OSDictionary *  properties,
253                                IOUserClient ** handler );
254
255    virtual bool attachToChild( IORegistryEntry * child,
256                               const IORegistryPlane * plane );
257    virtual void detachFromChild( IORegistryEntry * child,
258                                 const IORegistryPlane * plane );
259private:
260  static void _keyboardEvent( IOHIKeyboard * self,
261			     unsigned   eventType,
262      /* flags */            unsigned   flags,
263      /* keyCode */          unsigned   key,
264      /* charCode */         unsigned   charCode,
265      /* charSet */          unsigned   charSet,
266      /* originalCharCode */ unsigned   origCharCode,
267      /* originalCharSet */  unsigned   origCharSet,
268      /* keyboardType */     unsigned   keyboardType,
269      /* repeat */           bool       repeat,
270      /* atTime */           AbsoluteTime ts);
271  static void _keyboardSpecialEvent(
272                             IOHIKeyboard * self,
273                             unsigned   eventType,
274        /* flags */          unsigned   flags,
275        /* keyCode  */       unsigned   key,
276        /* specialty */      unsigned   flavor,
277        /* guid */           UInt64     guid,
278        /* repeat */         bool       repeat,
279        /* atTime */         AbsoluteTime ts);
280
281  static void _updateEventFlags( IOHIKeyboard * self,
282				unsigned flags);  /* Does not generate events */
283    bool postSecureKey(UInt8 key, bool down);
284
285};
286
287#endif /* !_IOHIKEYBOARD_H */
288