1/*
2 * @APPLE_LICENSE_HEADER_START@
3 *
4 * Copyright (c) 1999-2011 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
24#ifndef _DEV_EVIO_H
25#define _DEV_EVIO_H
26
27#include <sys/cdefs.h>
28
29__BEGIN_DECLS
30
31#if KERNEL
32#include <IOKit/system.h>
33#else /* !KERNEL */
34#include <mach/message.h>
35#include <IOKit/IOKitLib.h>
36#endif /* KERNEL */
37
38#include <IOKit/IOReturn.h>
39#include <IOKit/graphics/IOGraphicsTypes.h>
40#include <IOKit/hidsystem/IOHIDTypes.h>
41#include <IOKit/hidsystem/IOLLEvent.h>
42
43/*
44 * Identify this driver as one that uses the new driverkit and messaging API
45 */
46#ifndef _NeXT_MACH_EVENT_DRIVER_
47#define _NeXT_MACH_EVENT_DRIVER_	(1)
48#endif /* _NeXT_MACH_EVENT_DRIVER_ */
49
50
51/* Pressure Constants */
52#define MINPRESSURE EV_MINPRESSURE
53#define MAXPRESSURE EV_MAXPRESSURE
54
55#define	LLEQSIZE 240	/* Entries in low-level event queue */
56
57typedef struct _NXEQElStruct {
58    int	next;		/* Slot of lleq for next event */
59    OSSpinLock sema; /* Is high-level code reading this event now? */
60    NXEvent event;	/* The event itself */
61} NXEQElement;
62
63/******************************************************************************
64    SHARED MEMORY OVERVIEW
65
66    PERSPECTIVE
67    The ev driver and PostScript share at least one page of wired memory.
68    This memory contains the low-level event queue which ev deposits events
69    into and PostScript reads events from. Also, this memory contains other
70    important data such as wait cursor state and some general cursor state.
71    This memory is critical for speed.  That is, we avoid having to make
72    system calls for common operations.
73
74    SHARED MEMORY REGIONS
75    There are currently three "regions" or "zones" delineated within this
76    shared memory.  The first zone is the EvOffsets structure. This structure
77    contains two offsets from the beginning of shared memory. The first offset
78    is to the second zone, EvGlobals. The second offset is to the third
79    zone, private shmem for drivers.
80
81    INITIALIZATION OF SHARED MEMORY
82    When the WindowServer starts up, it finds all screens that will be active.
83    It then opens the ev driver and calls the EVIOSSCR ioctl repeatedly for
84    each screen in use. This lets the ev driver set up the evScreen array
85    and fill in each element. This ioctl also returns to PostScript a running
86    total shared memory size with which to allocate. PostScript then allocates
87    a region of memory this size and calls evmmap to "map in" this shared
88    region.  Evmmap initializes and fills in the EvOffsets and EvGlobals.
89    Next the WindowServer calls each screen in turn to register itself with
90    the ev driver in the same sequence as presented to EVIOSSCR.  Each screen
91    driver calls ev_register_screen() which among other things allocates a
92    part of the private shmem (of the third shared memory zone) for the driver.
93
94    DEBUGGING NOTES
95    You can easily display and set this shared memory from kgdb, but usually
96    cannot do so from within PostScript.  Gdb (or some weird interaction
97    between gdb and the os) chokes on this shmem.  So if you read or write
98    this area of memory, copy-on-write will occur and you'll get a completely
99    new page for PostScript.  This will render the shared memory scheme
100    useless and you will have to restart PostScript.  It was my understanding
101    that before, we were able to "read" this area from PS, but not write to
102    it (the idea behind copy-on-WRITE).  However, this seems to be broken
103    in 2.0.  We think this is a kernel bug.
104******************************************************************************/
105
106typedef volatile struct _evOffsets {
107    int	evGlobalsOffset;	/* Offset to EvGlobals structure */
108    int evShmemOffset;		/* Offset to private shmem regions */
109} EvOffsets;
110
111/******************************************************************************
112    EvGlobals
113    This structures defines the portion of the events driver data structure
114    that is exported to the PostScript server. It contains the event queue
115    which is in memory shared between the driver and the PostScript server.
116    All the variables necessary to read and process events from the queue are
117    contained here.
118******************************************************************************/
119
120typedef volatile struct _evGlobals {
121    OSSpinLock cursorSema; 	/* set to disable periodic code */
122    int eNum;                       /* Unique id for mouse events */
123    int buttons;                    /* State of the mouse buttons 1==down, 0==up */
124    int eventFlags;                 /* The current value of event.flags */
125    int VertRetraceClock;           /* The current value of event.time */
126    IOGPoint cursorLoc;             /* The current location of the cursor, in desktop coordinates */
127    int frame;                      /* current cursor frame */
128    IOGBounds workBounds;           /* bounding box of all screens */
129    IOGBounds mouseRect;            /* Rect for mouse-exited events */
130    int version;                    /* for run time checks */
131    int	structSize;                 /* for run time checks */
132    int lastFrame;
133                                    /* The current location of the cursor, 24.8 bit fixed point format */
134    IOFixedPoint32 screenCursorFixed; /* in Screen coordinates  */
135    IOFixedPoint32 desktopCursorFixed;/* in Desktop coordinates  */
136    unsigned int reservedA[27];
137
138    unsigned reserved:25;
139    unsigned updateCursorPositionFromFixed:1; /* if this is set, IOHIDSystem will take any cursor position updates from desktopCursorFixed instead of cursorLoc */
140    unsigned logCursorUpdates:1;    /* log cursor updates */
141    unsigned wantPressure:1;        /* pressure in current mouseRect? */
142    unsigned wantPrecision:1;       /* precise coordinates in current mouseRect? */
143    unsigned dontWantCoalesce:1;    /* coalesce within the current mouseRect? */
144    unsigned dontCoalesce:1;        /* actual flag which determines coalescing */
145    unsigned mouseRectValid:1;      /* If nonzero, post a mouse-exited whenever mouse outside mouseRect. */
146    int movedMask;                  /* This contains an event mask for the three events MOUSEMOVED,
147                                        LMOUSEDRAGGED,  and RMOUSEDRAGGED. It says whether driver should
148                                        generate those events. */
149    OSSpinLock waitCursorSema; /* protects wait cursor fields */
150    int AALastEventSent;            /* timestamp for wait cursor */
151    int AALastEventConsumed;        /* timestamp for wait cursor */
152    int waitCursorUp;               /* Is wait cursor up? */
153    char ctxtTimedOut;              /* Has wait cursor timer expired? */
154    char waitCursorEnabled;         /* Play wait cursor game (per ctxt)? */
155    char globalWaitCursorEnabled;   /* Play wait cursor game (global)? */
156    int waitThreshold;              /* time before wait cursor appears */
157
158    int LLEHead;                    /* The next event to be read */
159    int LLETail;                    /* Where the next event will go */
160    int LLELast;                    /* The last event entered */
161    NXEQElement lleq[LLEQSIZE];     /* The event queue itself */
162} EvGlobals;
163
164/* These evio structs are used in various calls supported by the ev driver. */
165
166struct evioLLEvent {
167    int setCursor;
168    int type;
169    IOGPoint location;
170    NXEventData data;
171    int setFlags;
172    int flags;
173};
174
175typedef struct evioLLEvent _NXLLEvent;
176
177#ifdef mach3xxx
178
179/*
180 * On a keypress of a VOL UP or VOL DOWN key, we send a message to the
181 * sound server to notify it of the volume change.  The message includes
182 * a flag to indicate which key was pressed, and the machine independant
183 * flag bits to indicate which modifier keys were pressed.
184 */
185
186struct evioSpecialKeyMsg
187{
188	msg_header_t Head;
189	msg_type_t keyType;
190	int key;		// special key number, from bsd/dev/ev_keymap.h
191	msg_type_t directionType;
192	int direction;		// NX_KEYDOWN, NX_KEYUP from event.h
193	msg_type_t flagsType;
194	int flags;		// device independant flags from event.h
195	msg_type_t levelType;
196	int level;		// EV_AUDIO_MIN_VOLUME to EV_AUDIO_MAX_VOLUME
197};
198#else
199struct evioSpecialKeyMsg
200{
201	mach_msg_header_t Head;
202	int key;		// special key number, from bsd/dev/ev_keymap.h
203	int direction;		// NX_KEYDOWN, NX_KEYUP from event.h
204	int flags;		// device independant flags from event.h
205	int level;		// EV_AUDIO_MIN_VOLUME to EV_AUDIO_MAX_VOLUME
206};
207#endif
208
209#define EV_SPECIAL_KEY_MSG_ID	(('S'<<24) | ('k'<<16) | ('e'<<8) | ('y'))
210typedef struct evioSpecialKeyMsg *evioSpecialKeyMsg_t;
211
212/*
213 * Volume ranges
214 */
215#define EV_AUDIO_MIN_VOLUME	0
216#define EV_AUDIO_MAX_VOLUME	64
217
218#define kIOHIDSystemClass	"IOHIDSystem"
219#define kIOHIKeyboardClass	"IOHIKeyboard"
220#define kIOHIPointingClass	"IOHIPointing"
221
222#define IOHIDSYSTEM_CONFORMSTO	kIOHIDSystemClass
223
224enum {
225    kIOHIDEventNotification     = 0,
226};
227#define kIOHIDCurrentShmemVersion           4
228#define kIOHIDLastCompatibleShmemVersion    3
229
230enum {
231    kIOHIDServerConnectType	= 0,
232    kIOHIDParamConnectType	= 1,
233    kIOHIDEventSystemConnectType = 3,
234};
235
236enum {
237    kIOHIDGlobalMemory          = 0
238};
239
240enum {
241    kIOHIDEventQueueTypeKernel  = 0,
242    kIOHIDEventQueueTypeUser    = 1
243};
244
245#ifdef KERNEL
246typedef UInt16 (*MasterVolumeUpdate)(void);
247typedef bool (*MasterMuteUpdate)(void);
248
249typedef struct {
250    MasterVolumeUpdate incrementMasterVolume;
251    MasterVolumeUpdate decrementMasterVolume;
252    MasterMuteUpdate toggleMasterMute;
253} MasterAudioFunctions;
254
255extern MasterAudioFunctions *masterAudioFunctions;
256#endif
257
258#ifndef KERNEL
259#ifndef _IOKIT_IOHIDLIB_H
260#include <IOKit/hidsystem/IOHIDLib.h>
261#endif
262#endif /* !KERNEL */
263
264__END_DECLS
265
266
267#endif /* !_DEV_EVIO_H */
268