summaryrefslogtreecommitdiff
path: root/xc/programs/Xserver/hw/darwin/xfIOKit.c
blob: 121c40ba3f0b96871554ad18943cdbd3bbcfb523 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/**************************************************************
 *
 * IOKit support for the Darwin X Server
 *
 * HISTORY:
 * Original port to Mac OS X Server by John Carmack
 * Port to Darwin 1.0 by Dave Zarzycki
 * Significantly rewritten for XFree86 4.0.1 by Torrey Lyons
 *
 **************************************************************/
/* $XFree86: xc/programs/Xserver/hw/darwin/xfIOKit.c,v 1.7 2001/04/16 06:51:48 torrey Exp $ */

#define NDEBUG 1

#include "X.h"
#include "Xproto.h"
#include "os.h"
#include "servermd.h"
#include "inputstr.h"
#include "scrnintstr.h"
#include "mi.h"
#include "mibstore.h"
#include "mipointer.h"
#include "micmap.h"

#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>

#include <mach/mach_interface.h>

#define NO_CFPLUGIN
#include <IOKit/IOKitLib.h>
#include <IOKit/hidsystem/IOHIDShared.h>
#include <IOKit/graphics/IOGraphicsLib.h>
#include <drivers/event_status_driver.h>

// Define this to work around bugs in the display drivers for
// older PowerBook G3's. If the X server starts without this
// #define, you don't need it.
#undef OLD_POWERBOOK_G3

#include "darwin.h"
#include "xfIOKit.h"

static EvGlobals *              evg;
static mach_port_t              masterPort;
static mach_port_t              notificationPort;
static IONotificationPortRef    NotificationPortRef;
static mach_port_t              pmNotificationPort;


/*
 * XFIOKitStoreColors
 * This is a callback from X to change the hardware colormap
 * when using PsuedoColor.
 */
static void XFIOKitStoreColors(
    ColormapPtr     pmap,
    int             numEntries,
    xColorItem      *pdefs)
{
    kern_return_t   kr;
    int             i;
    IOColorEntry    *newColors;

    assert( newColors = (IOColorEntry *)
                xalloc( numEntries*sizeof(IOColorEntry) ));

    // Convert xColorItem values to IOColorEntry
    // assume the colormap is PsuedoColor
    // as we do not support DirectColor
    for (i = 0; i < numEntries; i++) {
        newColors[i].index = pdefs[i].pixel;
        newColors[i].red =   pdefs[i].red;
        newColors[i].green = pdefs[i].green;
        newColors[i].blue =  pdefs[i].blue;
    }

    kr = IOFBSetCLUT( dfb.fbService, 0, numEntries,
                      kSetCLUTByValue, newColors );
    kern_assert( kr );

    xfree( newColors );
}

/*
 * XFIOKitBell
 * FIXME
 */
void XFIOKitBell(
    int             loud,
    DeviceIntPtr    pDevice,
    pointer         ctrl,
    int             fbclass)
{
}

/*
 * XFIOKitGiveUp
 * FIXME: Crashes kernel if used
 */
void XFIOKitGiveUp( void )
{
#if 1
    // we must close the HID System first
    // because it is a client of the framebuffer
    NXCloseEventStatus( dfb.hidParam );
    IOServiceClose( dfb.hidService );
    IOServiceClose( dfb.fbService );
#endif
}

/*
 * ClearEvent
 * Clear an event from the HID System event queue
 */
static void ClearEvent(NXEvent * ep)
{
    static NXEvent nullEvent = {NX_NULLEVENT, {0, 0 }, 0, -1, 0 };

    *ep = nullEvent;
    ep->data.compound.subType = ep->data.compound.misc.L[0] =
                                ep->data.compound.misc.L[1] = 0;
}

/*
 * XFIOKitHIDThread
 * Read the HID System event queue and pass to pipe
 */
static void *XFIOKitHIDThread(void *arg)
{
    int iokitEventWriteFD = (int)arg;

    for (;;) {
        NXEvent                 ev;
        NXEQElement             *oldHead;
        mach_msg_return_t       kr;
        mach_msg_empty_rcv_t    msg;

        kr = mach_msg((mach_msg_header_t*) &msg, MACH_RCV_MSG, 0,
                      sizeof(msg), notificationPort, 0, MACH_PORT_NULL);
        kern_assert(kr);

        while (evg->LLEHead != evg->LLETail) {
            oldHead = (NXEQElement*)&evg->lleq[evg->LLEHead];
            ev_lock(&oldHead->sema);
            ev = oldHead->event;
            ClearEvent(&oldHead->event);
            evg->LLEHead = oldHead->next;
            ev_unlock(&oldHead->sema);

            write(iokitEventWriteFD, &ev, sizeof(ev));
        }
    }
    return NULL;
}

/*
 * XFIOKitPMThread
 * Handle power state notifications
 */
static void *XFIOKitPMThread(void *arg)
{
    ScreenPtr pScreen = (ScreenPtr)arg;

    for (;;) {
        mach_msg_return_t       kr;
        mach_msg_empty_rcv_t    msg;

        kr = mach_msg((mach_msg_header_t*) &msg, MACH_RCV_MSG, 0,
                      sizeof(msg), pmNotificationPort, 0, MACH_PORT_NULL);
        kern_assert(kr);

        // display is powering down
        if (msg.header.msgh_id == 0) {
            IOFBAcknowledgePM( dfb.fbService );
            xf86SetRootClip(pScreen, FALSE);
        }
        // display just woke up
        else if (msg.header.msgh_id == 1) {
            xf86SetRootClip(pScreen, TRUE);
        }
    }
    return NULL;
}

static void SetupFBandHID(void)
{
    kern_return_t           kr;
    io_service_t            service;
    io_iterator_t           iter;
    io_name_t               name;
    vm_address_t            shmem, vram;
    vm_size_t               shmemSize;
    int                     i;
    UInt32                  numModes;
    IODisplayModeInformation modeInfo;
    IODisplayModeID         displayMode, *allModes;
    IOIndex                 displayDepth;
    IOFramebufferInformation fbInfo;
    StdFBShmem_t            *cshmem;

    dfb.fbService = 0;
    dfb.hidService = 0;

    // find and open the IOFrameBuffer service
    kr = IOServiceGetMatchingServices( masterPort,
                        IOServiceMatching( IOFRAMEBUFFER_CONFORMSTO ),
                        &iter );
    kern_assert( kr );

    // find the requested screen
    assert(service = IOIteratorNext(iter));
    for (i = 0; i < darwinScreenNumber; i++) {
        service = IOIteratorNext(iter);
        if (service == 0)
            FatalError("Could not find the requested screen number %i.\n",
                       darwinScreenNumber);
    }

    kr = IOServiceOpen( service, mach_task_self(),
                        kIOFBServerConnectType, &dfb.fbService );
    if (kr != KERN_SUCCESS)
#ifdef DARWIN_WITH_QUARTZ
        FatalError("Failed to connect as window server!\nQuit the Mac OS X window server or use the -quartz option.\n");
#else
        FatalError("Failed to connect as window server!\nMake sure you have quit the Mac OS X window server.\n");
#endif

    IOObjectRelease( service );
    IOObjectRelease( iter );

    // create the slice of shared memory containing cursor state data
    kr = IOFBCreateSharedCursor( dfb.fbService, kIOFBCurrentShmemVersion,
                               	 32, 32 );
    kern_assert( kr );

    // Register for power management events for the framebuffer's device
    kr = IOCreateReceivePort(kOSNotificationMessageID, &pmNotificationPort);
    kern_assert(kr);
    kr = IOConnectSetNotificationPort( dfb.fbService, 0,
                                       pmNotificationPort, 0 );
    if (kr != KERN_SUCCESS) {
        ErrorF("Power management registration failed.\n");
    }

    // SET THE SCREEN PARAMETERS
    // get the current screen resolution, refresh rate and depth
    kr = IOFBGetCurrentDisplayModeAndDepth( dfb.fbService, &displayMode,
                                            &displayDepth );
    kern_assert( kr );

    // use the current screen resolution if the user
    // only wants to change the refresh rate
    if (darwinDesiredRefresh != -1 && darwinDesiredWidth == 0) {
        kr = IOFBGetDisplayModeInformation( dfb.fbService, displayMode,
                                            &modeInfo );
        kern_assert( kr );
        darwinDesiredWidth = modeInfo.nominalWidth;
        darwinDesiredHeight = modeInfo.nominalHeight;
    }

    // use the current resolution and refresh rate
    // if the user doesn't have a preference
    if (darwinDesiredWidth == 0) {

        // change the pixel depth if desired
        if (darwinDesiredDepth != -1) {
            kr = IOFBGetDisplayModeInformation( dfb.fbService, displayMode,
                                                &modeInfo );
            kern_assert( kr );
            if (modeInfo.maxDepthIndex < darwinDesiredDepth)
                FatalError("Current screen resolution does not support desired pixel depth!\n");

            displayDepth = darwinDesiredDepth;
            kr = IOFBSetDisplayModeAndDepth( dfb.fbService, displayMode,
                                             displayDepth );
            kern_assert( kr );
        }
 
    // look for display mode with correct resolution and refresh rate
    } else {

        // get an array of all supported display modes
        kr = IOFBGetDisplayModeCount( dfb.fbService, &numModes );
        kern_assert( kr );
        assert(allModes = (IODisplayModeID *)
                xalloc( numModes * sizeof(IODisplayModeID) ));
        kr = IOFBGetDisplayModes( dfb.fbService, numModes, allModes );
        kern_assert( kr );

        for (i = 0; i < numModes; i++) {
            kr = IOFBGetDisplayModeInformation( dfb.fbService, allModes[i],
                                                &modeInfo );
            kern_assert( kr );

            if (modeInfo.flags & kDisplayModeValidFlag &&
                modeInfo.nominalWidth == darwinDesiredWidth &&
                modeInfo.nominalHeight == darwinDesiredHeight) {

                if (darwinDesiredDepth == -1)
                    darwinDesiredDepth = modeInfo.maxDepthIndex;
                if (modeInfo.maxDepthIndex < darwinDesiredDepth)
                    FatalError("Desired screen resolution does not support desired pixel depth!\n");
                if ((darwinDesiredRefresh == -1 ||
                    (darwinDesiredRefresh << 16) == modeInfo.refreshRate)) {
                    displayMode = allModes[i];
                    displayDepth = darwinDesiredDepth;
                    kr = IOFBSetDisplayModeAndDepth( dfb.fbService, displayMode,
                                                     displayDepth );
                    kern_assert( kr );
                    break;
                }
            }
        }

        xfree( allModes );
        if (i >= numModes)
            FatalError("Desired screen resolution or refresh rate is not supported!\n");
    }

    kr = IOFBGetPixelInformation( dfb.fbService, displayMode, displayDepth,
                                  kIOFBSystemAperture, &dfb.pixelInfo );
    kern_assert( kr );

#ifdef __i386__
    /* x86 in 8bit mode currently needs fixed color map... */
    if( dfb.pixelInfo.bitsPerComponent == 8 ) {
        dfb.pixelInfo.pixelType = kIOFixedCLUTPixels;
    }
#endif

#ifdef OLD_POWERBOOK_G3
    if (dfb.pixelInfo.pixelType == kIOCLUTPixels)
        dfb.pixelInfo.pixelType = kIOFixedCLUTPixels;
#endif

    kr = IOFBGetFramebufferInformationForAperture( dfb.fbService, kIOFBSystemAperture,
                                                   &fbInfo );
    kern_assert( kr );

    kr = IOConnectMapMemory( dfb.fbService, kIOFBCursorMemory,
                             mach_task_self(), (vm_address_t *) &cshmem,
                             &shmemSize, kIOMapAnywhere );
    kern_assert( kr );
    dfb.cursorShmem = cshmem;

    kr = IOConnectMapMemory( dfb.fbService, kIOFBSystemAperture, mach_task_self(),
                             &vram, &shmemSize, kIOMapAnywhere );
    kern_assert( kr );

    dfb.framebuffer = (void*)vram;
    dfb.width = fbInfo.activeWidth;
    dfb.height = fbInfo.activeHeight;
    dfb.pitch = fbInfo.bytesPerRow;
    dfb.bitsPerPixel = fbInfo.bitsPerPixel;
    dfb.colorBitsPerPixel = dfb.pixelInfo.componentCount *
                            dfb.pixelInfo.bitsPerComponent;

    // find and open the HID System Service
    kr = IOServiceGetMatchingServices( masterPort,
                                       IOServiceMatching( kIOHIDSystemClass ),
                                       &iter );
    kern_assert( kr );

    assert( service = IOIteratorNext( iter ) );

    kr = IORegistryEntryGetName( service, name );
    kern_assert( kr );

    kr = IOServiceOpen( service, mach_task_self(), kIOHIDServerConnectType,
                        &dfb.hidService );
    kern_assert( kr );

    IOObjectRelease( service );
    IOObjectRelease( iter );

    kr = IOHIDCreateSharedMemory( dfb.hidService, kIOHIDCurrentShmemVersion );
    kern_assert( kr );

    kr = IOHIDSetEventsEnable(dfb.hidService, TRUE);
    kern_assert( kr );

    // Inform the HID system that the framebuffer is also connected to it
    kr = IOConnectAddClient( dfb.hidService, dfb.fbService );
    kern_assert( kr );

    kr = IOHIDSetCursorEnable(dfb.hidService, TRUE);
    kern_assert( kr );

    kr = IOConnectMapMemory( dfb.hidService, kIOHIDGlobalMemory, mach_task_self(),
                             &shmem, &shmemSize, kIOMapAnywhere );
    kern_assert( kr );

    evg = (EvGlobals *)(shmem + ((EvOffsets *)shmem)->evGlobalsOffset);

    assert(sizeof(EvGlobals) == evg->structSize);

    NotificationPortRef = IONotificationPortCreate( masterPort );

    notificationPort = IONotificationPortGetMachPort(NotificationPortRef);

    kr = IOConnectSetNotificationPort( dfb.hidService, kIOHIDEventNotification,
                                       notificationPort, 0 );
    kern_assert( kr );

    evg->movedMask |= NX_MOUSEMOVEDMASK;
}

/* 
 * XFIOKitAddScreen
 *  IOKit specific initialization for each screen.
 */
Bool XFIOKitAddScreen(ScreenPtr pScreen) 
{
    pthread_t pmThread;

    // setup cursor support, use hardware cursor if possible
    if (! XFIOKitInitCursor(pScreen)) {
        return FALSE;
    }

    // initialize colormap handling as needed
    if (dfb.pixelInfo.pixelType == kIOCLUTPixels) {
        pScreen->StoreColors = XFIOKitStoreColors;
    }

    // initialize power manager handling
    pthread_create( &pmThread, NULL, XFIOKitPMThread,
                    (void *) pScreen );

    return TRUE;
}

/*
 * XFIOKitOsVendorInit
 *  One-time initialization of IOKit support.
 *  Connect to framebuffer and HID system.
 */
void XFIOKitOsVendorInit(void)
{
    kern_return_t           kr;
    int                     fd[2];

    ErrorF("Display mode: IOKit\n");

    kr = IOMasterPort(bootstrap_port, &masterPort);
    kern_assert( kr );

    SetupFBandHID();

    assert( pipe(fd) == 0 );
    darwinEventFD = fd[0];
    fcntl(darwinEventFD, F_SETFL, O_NONBLOCK);
    pthread_create(&dfb.hidThread, NULL, XFIOKitHIDThread, (void *) fd[1]);
}