Search
lxdream.org :: lxdream/src/drivers/osx_iokit.m
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/osx_iokit.m
changeset 1297:7e98a164b2d9
prev1023:264e2fd90be8
author nkeynes
date Sun May 24 19:46:06 2015 +1000 (8 years ago)
permissions -rw-r--r--
last change Remove static from gl_load_frame_buffer() - also needed by video_egl.c
Fix error location in gl_frame_buffer_to_tex
file annotate diff log raw
nkeynes@964
     1
/**
nkeynes@964
     2
 * $Id$
nkeynes@964
     3
 *
nkeynes@964
     4
 * OSX support functions for handling the IOKit registry. 
nkeynes@964
     5
 * Currently this manages access to CD/DVD drives + media, plus HID devices.
nkeynes@964
     6
 * 
nkeynes@964
     7
 * The HID part is much simpler...
nkeynes@964
     8
 *
nkeynes@964
     9
 * Copyright (c) 2008 Nathan Keynes.
nkeynes@964
    10
 *
nkeynes@964
    11
 * This program is free software; you can redistribute it and/or modify
nkeynes@964
    12
 * it under the terms of the GNU General Public License as published by
nkeynes@964
    13
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@964
    14
 * (at your option) any later version.
nkeynes@964
    15
 *
nkeynes@964
    16
 * This program is distributed in the hope that it will be useful,
nkeynes@964
    17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@964
    18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@964
    19
 * GNU General Public License for more details.
nkeynes@964
    20
 */
nkeynes@964
    21
nkeynes@1297
    22
#include <glib.h>
nkeynes@964
    23
#include <sys/param.h>
nkeynes@964
    24
#include <paths.h>
nkeynes@964
    25
#include <string.h>
nkeynes@964
    26
#include <unistd.h>
nkeynes@964
    27
#include <stdio.h>
nkeynes@964
    28
#include <fcntl.h>
nkeynes@964
    29
#include <CoreFoundation/CFRunLoop.h>
nkeynes@964
    30
#include <IOKit/IOMessage.h>
nkeynes@964
    31
#include <IOKit/hid/IOHIDLib.h>
nkeynes@964
    32
#include "osx_iokit.h"
nkeynes@964
    33
nkeynes@964
    34
nkeynes@964
    35
nkeynes@964
    36
static IONotificationPortRef notify_port = 0;
nkeynes@964
    37
static io_iterator_t iokit_iterators[4] = {0,0,0,0};
nkeynes@964
    38
nkeynes@964
    39
struct osx_cdrom_drive {
nkeynes@964
    40
    io_string_t ioservice_path;
nkeynes@964
    41
    io_string_t vendor_name;
nkeynes@964
    42
    io_string_t product_name;
nkeynes@964
    43
    char media_path[MAXPATHLEN]; // BSD device path if media present, otherwise the empty string.
nkeynes@964
    44
    io_iterator_t media_load_iterator;
nkeynes@964
    45
    io_iterator_t media_unload_iterator;
nkeynes@964
    46
    int media_fh; // BSD device handle if open, otherwise -1
nkeynes@964
    47
    media_changed_callback_t media_changed;
nkeynes@964
    48
    void *media_changed_user_data;
nkeynes@964
    49
};
nkeynes@964
    50
nkeynes@964
    51
static gboolean get_bsdname_for_iomedia( io_object_t iomedia, char *buf, int buflen );
nkeynes@1023
    52
static gboolean get_boolean_property( io_object_t io, CFStringRef key, gboolean def );
nkeynes@964
    53
nkeynes@964
    54
/***************** IOKit Callbacks ******************/
nkeynes@964
    55
nkeynes@964
    56
/**
nkeynes@964
    57
 * Called from IOKit for any IOMessages on an IOMedia. Currently the only message
nkeynes@964
    58
 * we're interested in is service termination.
nkeynes@964
    59
 */
nkeynes@964
    60
static void osx_cdrom_media_notify( void *ref, io_service_t service, uint32_t msgType,
nkeynes@964
    61
                                    void *msgArgument )
nkeynes@964
    62
{
nkeynes@964
    63
    if( msgType == kIOMessageServiceIsTerminated ) {
nkeynes@964
    64
        osx_cdrom_drive_t drive = (osx_cdrom_drive_t)ref;
nkeynes@964
    65
        if( drive->media_changed != NULL ) {
nkeynes@964
    66
            drive->media_changed( drive, FALSE, drive->media_changed_user_data );
nkeynes@964
    67
        }
nkeynes@964
    68
        if( drive->media_fh != -1 ) {
nkeynes@964
    69
            close(drive->media_fh);
nkeynes@964
    70
            drive->media_fh = -1;
nkeynes@964
    71
        }
nkeynes@964
    72
        drive->media_path[0] = '\0';
nkeynes@964
    73
        IOObjectRelease( drive->media_unload_iterator );
nkeynes@964
    74
    }
nkeynes@964
    75
}
nkeynes@964
    76
nkeynes@964
    77
/**
nkeynes@964
    78
 * Called from IOKit when an IOMedia is inserted that we have be interested in.
nkeynes@964
    79
 * FIXME: Can the matcher be restricted to descendents of the drive node? currently
nkeynes@964
    80
 * we watch for all IOMedia events and compare the device path to see if it's one we
nkeynes@964
    81
 * care about.
nkeynes@964
    82
 * FIXME: We assume for now that a drive has at most one piece of media at a time. 
nkeynes@964
    83
 * If this isn't the case, the system may get a little confused.
nkeynes@964
    84
 */
nkeynes@964
    85
static void osx_cdrom_media_inserted( void *ref, io_iterator_t iterator )
nkeynes@964
    86
{
nkeynes@964
    87
    osx_cdrom_drive_t drive = (osx_cdrom_drive_t)ref;
nkeynes@964
    88
nkeynes@964
    89
    io_object_t object;
nkeynes@964
    90
    while( (object = IOIteratorNext(iterator)) != 0 ) {
nkeynes@964
    91
        io_string_t iopath = "";
nkeynes@964
    92
        IORegistryEntryGetPath( object, kIOServicePlane, iopath );
nkeynes@964
    93
        if( drive != NULL && g_str_has_prefix(iopath, drive->ioservice_path ) &&
nkeynes@1023
    94
            get_boolean_property(object, CFSTR("Whole"), TRUE) &&
nkeynes@1023
    95
            get_bsdname_for_iomedia(object, drive->media_path, sizeof(drive->media_path)) ) {
nkeynes@964
    96
            // A disc was inserted within the drive of interest
nkeynes@964
    97
            if( drive->media_fh != -1 ) {
nkeynes@964
    98
                close(drive->media_fh);
nkeynes@964
    99
                drive->media_fh = -1;
nkeynes@964
   100
            }
nkeynes@964
   101
nkeynes@964
   102
            if( drive->media_changed != NULL ) {
nkeynes@964
   103
                drive->media_changed(drive, TRUE, drive->media_changed_user_data);
nkeynes@964
   104
            }
nkeynes@964
   105
            // Add a notification listener to get removal events.
nkeynes@964
   106
            IOServiceAddInterestNotification( notify_port, object, kIOGeneralInterest, 
nkeynes@964
   107
                                              osx_cdrom_media_notify, drive, &drive->media_unload_iterator ); 
nkeynes@964
   108
nkeynes@964
   109
        }
nkeynes@964
   110
        IOObjectRelease( object );
nkeynes@964
   111
    }
nkeynes@964
   112
}
nkeynes@964
   113
nkeynes@964
   114
static void osx_drives_changed( void *ref, io_iterator_t iterator )
nkeynes@964
   115
{
nkeynes@964
   116
    io_object_t object;
nkeynes@964
   117
    while( (object = IOIteratorNext(iterator)) != 0 ) {
nkeynes@964
   118
        IOObjectRelease(object);
nkeynes@964
   119
    }
nkeynes@964
   120
nkeynes@964
   121
}
nkeynes@964
   122
nkeynes@964
   123
/******************** Support functions *********************/
nkeynes@964
   124
nkeynes@964
   125
/**
nkeynes@964
   126
 * Determine the BSD device name (ie "/dev/rdisk1") for a given IO object.
nkeynes@964
   127
 * @return TRUE if the device name was retrieved, FALSE if the request failed.
nkeynes@964
   128
 */
nkeynes@964
   129
static gboolean get_bsdname_for_iomedia( io_object_t iomedia, char *buf, int buflen )
nkeynes@964
   130
{
nkeynes@964
   131
    gboolean result = FALSE;
nkeynes@964
   132
    CFTypeRef pathRef = IORegistryEntryCreateCFProperty(iomedia, CFSTR(kIOBSDNameKey),
nkeynes@964
   133
            kCFAllocatorDefault, 0 );
nkeynes@964
   134
    if( pathRef ) {
nkeynes@964
   135
        char pathlen;
nkeynes@964
   136
        strcpy( buf, _PATH_DEV "r" );
nkeynes@964
   137
        pathlen = strlen(buf);
nkeynes@964
   138
        if( CFStringGetCString( pathRef, buf + pathlen, buflen-pathlen,
nkeynes@964
   139
                kCFStringEncodingASCII ) != noErr ) {
nkeynes@964
   140
            result = TRUE;
nkeynes@964
   141
        }
nkeynes@964
   142
        CFRelease(pathRef);
nkeynes@964
   143
    }
nkeynes@964
   144
    return result;
nkeynes@964
   145
}
nkeynes@964
   146
nkeynes@1023
   147
/**
nkeynes@1023
   148
 * Retrieve a boolean property from the io object, and return as a gboolean. If
nkeynes@1023
   149
 * the key is not present, return def instead.
nkeynes@1023
   150
 */
nkeynes@1023
   151
static gboolean get_boolean_property( io_object_t io, CFStringRef key, gboolean def )
nkeynes@1023
   152
{
nkeynes@1023
   153
    gboolean result = def;
nkeynes@1023
   154
    CFTypeRef ref = IORegistryEntryCreateCFProperty(io, key, kCFAllocatorDefault, 0 );
nkeynes@1023
   155
    if( ref ) {
nkeynes@1023
   156
    	result = CFBooleanGetValue(ref);
nkeynes@1023
   157
        CFRelease(ref);
nkeynes@1023
   158
    }
nkeynes@1023
   159
    return result;
nkeynes@1023
   160
 }
nkeynes@1023
   161
nkeynes@964
   162
static gboolean osx_cdrom_drive_get_name( io_object_t object, char *vendor, int vendor_len,
nkeynes@964
   163
                                          char *product, int product_len )
nkeynes@964
   164
{
nkeynes@964
   165
    gboolean result = FALSE;
nkeynes@964
   166
    CFMutableDictionaryRef props = 0;
nkeynes@964
   167
    if( IORegistryEntryCreateCFProperties(object, &props, kCFAllocatorDefault, kNilOptions) == KERN_SUCCESS ) {
nkeynes@964
   168
        CFDictionaryRef dict = 
nkeynes@964
   169
            (CFDictionaryRef)CFDictionaryGetValue(props, CFSTR(kIOPropertyDeviceCharacteristicsKey));
nkeynes@964
   170
        if( dict != NULL ) {
nkeynes@964
   171
            CFTypeRef value = CFDictionaryGetValue(dict, CFSTR(kIOPropertyVendorNameKey));
nkeynes@964
   172
            if( value && CFGetTypeID(value) == CFStringGetTypeID() ) {
nkeynes@964
   173
                CFStringGetCString( (CFStringRef)value, vendor, vendor_len, kCFStringEncodingUTF8 );
nkeynes@964
   174
            } else {
nkeynes@964
   175
                vendor[0] = 0;
nkeynes@964
   176
            }
nkeynes@964
   177
nkeynes@964
   178
            value = CFDictionaryGetValue(dict, CFSTR(kIOPropertyProductNameKey));
nkeynes@964
   179
            if ( value && CFGetTypeID(value) == CFStringGetTypeID() ) {
nkeynes@964
   180
                CFStringGetCString( (CFStringRef)value, product, product_len, kCFStringEncodingUTF8 );
nkeynes@964
   181
            } else {
nkeynes@964
   182
                product[0] = 0;
nkeynes@964
   183
            }
nkeynes@964
   184
            result = TRUE;
nkeynes@964
   185
        }
nkeynes@964
   186
nkeynes@964
   187
        CFRelease(props);
nkeynes@964
   188
    }
nkeynes@964
   189
    return result;
nkeynes@964
   190
}
nkeynes@964
   191
nkeynes@964
   192
/**
nkeynes@964
   193
 * Construct and initialize a new osx_cdrom_drive object, including registering
nkeynes@964
   194
 * it's media inserted notification.
nkeynes@964
   195
 */
nkeynes@964
   196
static osx_cdrom_drive_t osx_cdrom_drive_new( io_object_t device )  
nkeynes@964
   197
{
nkeynes@964
   198
    osx_cdrom_drive_t drive = g_malloc0(sizeof(struct osx_cdrom_drive));
nkeynes@964
   199
nkeynes@964
   200
    IORegistryEntryGetPath( device, kIOServicePlane, drive->ioservice_path );
nkeynes@964
   201
    osx_cdrom_drive_get_name( device, drive->vendor_name, sizeof(drive->vendor_name),
nkeynes@964
   202
                              drive->product_name, sizeof(drive->product_name) );
nkeynes@964
   203
    drive->media_path[0] = '\0';
nkeynes@964
   204
    drive->media_changed = NULL;
nkeynes@964
   205
    drive->media_changed_user_data = NULL;
nkeynes@964
   206
    drive->media_fh = -1;
nkeynes@964
   207
nkeynes@964
   208
    IOServiceAddMatchingNotification( notify_port, kIOFirstPublishNotification, 
nkeynes@964
   209
                                      IOServiceMatching("IOMedia"),
nkeynes@964
   210
                                      osx_cdrom_media_inserted, drive, 
nkeynes@964
   211
                                      &drive->media_load_iterator );
nkeynes@964
   212
    osx_cdrom_media_inserted( drive, drive->media_load_iterator );
nkeynes@964
   213
    return drive;
nkeynes@964
   214
}
nkeynes@964
   215
nkeynes@964
   216
/************************ Exported functions *************************/ 
nkeynes@964
   217
nkeynes@964
   218
osx_cdrom_drive_t osx_cdrom_open_drive( const char *devname )
nkeynes@964
   219
{
nkeynes@964
   220
    io_object_t object = IORegistryEntryFromPath( kIOMasterPortDefault, devname );
nkeynes@964
   221
    if( object == MACH_PORT_NULL ) {
nkeynes@964
   222
        return NULL;
nkeynes@964
   223
    }
nkeynes@964
   224
nkeynes@964
   225
    osx_cdrom_drive_t drive = osx_cdrom_drive_new( object );
nkeynes@964
   226
    IOObjectRelease( object );
nkeynes@964
   227
    return drive;
nkeynes@964
   228
}
nkeynes@964
   229
nkeynes@964
   230
void osx_cdrom_set_media_changed_callback( osx_cdrom_drive_t drive, 
nkeynes@964
   231
                                           media_changed_callback_t callback,
nkeynes@964
   232
                                           void *user_data )
nkeynes@964
   233
{
nkeynes@964
   234
    drive->media_changed = callback;
nkeynes@964
   235
    drive->media_changed_user_data = user_data;
nkeynes@964
   236
}
nkeynes@964
   237
nkeynes@964
   238
void osx_cdrom_close_drive( osx_cdrom_drive_t drive )
nkeynes@964
   239
{
nkeynes@964
   240
    IOObjectRelease( drive->media_load_iterator );
nkeynes@964
   241
    IOObjectRelease( drive->media_unload_iterator );
nkeynes@964
   242
    if( drive->media_fh != -1 ) {
nkeynes@964
   243
        close(drive->media_fh);
nkeynes@964
   244
        drive->media_fh = -1;
nkeynes@964
   245
    }
nkeynes@964
   246
    g_free( drive );
nkeynes@964
   247
}
nkeynes@964
   248
nkeynes@964
   249
int osx_cdrom_get_media_handle( osx_cdrom_drive_t drive )
nkeynes@964
   250
{
nkeynes@964
   251
    if( drive->media_fh == -1 ) {
nkeynes@964
   252
        if( drive->media_path[0] != '\0' ) {
nkeynes@964
   253
            drive->media_fh = open( drive->media_path, O_RDONLY|O_NONBLOCK );
nkeynes@964
   254
        }
nkeynes@964
   255
    }
nkeynes@964
   256
    return drive->media_fh;
nkeynes@964
   257
}
nkeynes@964
   258
nkeynes@964
   259
void osx_cdrom_release_media_handle( osx_cdrom_drive_t drive )
nkeynes@964
   260
{
nkeynes@964
   261
    if( drive->media_fh != -1 ) {
nkeynes@964
   262
        close( drive->media_fh );
nkeynes@964
   263
        drive->media_fh = -1; 
nkeynes@964
   264
    }
nkeynes@964
   265
}
nkeynes@964
   266
nkeynes@964
   267
static io_object_t iterator_find_cdrom( io_object_t iterator, find_drive_callback_t callback, void *user_data )
nkeynes@964
   268
{
nkeynes@964
   269
    io_object_t object;
nkeynes@964
   270
    while( (object = IOIteratorNext(iterator)) != 0 ) {
nkeynes@964
   271
        io_string_t iopath = "";
nkeynes@964
   272
        char product[256], vendor[256];
nkeynes@964
   273
        IORegistryEntryGetPath( object, kIOServicePlane, iopath );
nkeynes@964
   274
        osx_cdrom_drive_get_name( object, vendor, sizeof(vendor), product, sizeof(product) );
nkeynes@964
   275
        if( callback( object, vendor, product, iopath, user_data ) ) {
nkeynes@964
   276
            IOObjectRelease(iterator);
nkeynes@964
   277
            return object;
nkeynes@964
   278
        }
nkeynes@964
   279
        IOObjectRelease(object);
nkeynes@964
   280
    }
nkeynes@964
   281
    IOObjectRelease(iterator);
nkeynes@964
   282
    return 0;
nkeynes@964
   283
}
nkeynes@964
   284
nkeynes@964
   285
nkeynes@964
   286
/**
nkeynes@964
   287
 * Search for a CD or DVD drive (instance of IODVDServices or IOCompactDiscServices).
nkeynes@964
   288
 * The callback will be called repeatedly until either it returns TRUE, or all drives
nkeynes@964
   289
 * have been iterated over.
nkeynes@964
   290
 * 
nkeynes@964
   291
 * @return an IO registry entry for the matched drive, or 0 if no drives matched.
nkeynes@964
   292
 * 
nkeynes@964
   293
 * Note: Use of IOCompactDiscServices is somewhat tentative since I don't have a Mac
nkeynes@964
   294
 * with a CD-Rom drive.
nkeynes@964
   295
 */ 
nkeynes@964
   296
io_object_t find_cdrom_drive( find_drive_callback_t callback, void *user_data )
nkeynes@964
   297
{
nkeynes@964
   298
    mach_port_t master_port;
nkeynes@964
   299
    CFMutableDictionaryRef match;
nkeynes@964
   300
    io_iterator_t services;
nkeynes@964
   301
    io_object_t result;
nkeynes@964
   302
nkeynes@964
   303
    if( IOMasterPort( MACH_PORT_NULL, &master_port ) != KERN_SUCCESS ) {
nkeynes@964
   304
        return 0; // Failed to get the master port?
nkeynes@964
   305
    }
nkeynes@964
   306
nkeynes@964
   307
    match = IOServiceMatching("IODVDServices");
nkeynes@964
   308
    if( IOServiceGetMatchingServices(master_port, match, &services) != kIOReturnSuccess ) {
nkeynes@964
   309
        return 0;
nkeynes@964
   310
    }
nkeynes@964
   311
nkeynes@964
   312
    result = iterator_find_cdrom( services, callback, user_data );
nkeynes@964
   313
    if( result != 0 ) {
nkeynes@964
   314
        return result;
nkeynes@964
   315
    }
nkeynes@964
   316
nkeynes@964
   317
    match = IOServiceMatching("IOCompactDiscServices");
nkeynes@964
   318
    if( IOServiceGetMatchingServices(master_port, match, &services) != kIOReturnSuccess ) {
nkeynes@964
   319
        return 0;
nkeynes@964
   320
    }
nkeynes@964
   321
    return iterator_find_cdrom( services, callback, user_data );
nkeynes@964
   322
}
nkeynes@964
   323
nkeynes@964
   324
nkeynes@964
   325
// *********************** Notification management ************************/
nkeynes@964
   326
nkeynes@964
   327
static void osx_hid_inserted( void *ref, io_iterator_t iterator )
nkeynes@964
   328
{
nkeynes@964
   329
    io_object_t object;
nkeynes@964
   330
    while( (object = IOIteratorNext(iterator)) != 0 ) {
nkeynes@964
   331
        io_string_t iopath = "";
nkeynes@964
   332
        IORegistryEntryGetPath( object, kIOServicePlane, iopath );
nkeynes@964
   333
        IOObjectRelease( object );
nkeynes@964
   334
    }
nkeynes@964
   335
}
nkeynes@964
   336
nkeynes@964
   337
gboolean osx_register_iokit_notifications()
nkeynes@964
   338
{
nkeynes@964
   339
    notify_port = IONotificationPortCreate( kIOMasterPortDefault );
nkeynes@964
   340
    CFRunLoopSourceRef runloop_source = IONotificationPortGetRunLoopSource( notify_port );
nkeynes@964
   341
    CFRunLoopAddSource( CFRunLoopGetCurrent(), runloop_source, kCFRunLoopCommonModes );
nkeynes@964
   342
nkeynes@964
   343
    // Drive notifications
nkeynes@964
   344
    if( IOServiceAddMatchingNotification( notify_port, kIOFirstPublishNotification,
nkeynes@964
   345
            IOServiceMatching("IOCompactDiscServies"),
nkeynes@964
   346
            osx_drives_changed, NULL, &iokit_iterators[0] ) != kIOReturnSuccess ) {
nkeynes@964
   347
        ERROR( "IOServiceAddMatchingNotification failed" );
nkeynes@964
   348
    }
nkeynes@964
   349
    osx_drives_changed(NULL, iokit_iterators[0]);
nkeynes@964
   350
    if( IOServiceAddMatchingNotification( notify_port, kIOFirstPublishNotification,
nkeynes@964
   351
            IOServiceMatching("IODVDServies"),
nkeynes@964
   352
            osx_drives_changed, NULL, &iokit_iterators[1] ) != kIOReturnSuccess ) {
nkeynes@964
   353
        ERROR( "IOServiceAddMatchingNotification failed" );
nkeynes@964
   354
    }
nkeynes@964
   355
    osx_drives_changed(NULL, iokit_iterators[1]);
nkeynes@964
   356
nkeynes@964
   357
    if( IOServiceAddMatchingNotification( notify_port, kIOFirstPublishNotification, 
nkeynes@964
   358
            IOServiceMatching(kIOHIDDeviceKey),
nkeynes@964
   359
            osx_hid_inserted, NULL, &iokit_iterators[2] ) != kIOReturnSuccess ) {
nkeynes@964
   360
        ERROR( "IOServiceAddMatchingNotification failed" );
nkeynes@964
   361
    }
nkeynes@964
   362
    osx_hid_inserted(NULL, iokit_iterators[2]);
nkeynes@964
   363
    return TRUE;
nkeynes@964
   364
}
nkeynes@964
   365
nkeynes@964
   366
void osx_unregister_iokit_notifications()
nkeynes@964
   367
{
nkeynes@964
   368
    CFRunLoopSourceRef runloop_source = IONotificationPortGetRunLoopSource( notify_port );
nkeynes@964
   369
    CFRunLoopRemoveSource( CFRunLoopGetCurrent(), runloop_source, kCFRunLoopCommonModes );
nkeynes@964
   370
    IONotificationPortDestroy( notify_port );
nkeynes@964
   371
    notify_port = 0;
nkeynes@964
   372
}
.