From 19cc31e3fd6942030173981be17ed88ef860ef88 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Thu, 28 Dec 2006 22:57:22 +0000 Subject: ported b3 from Masayuki Akamatsu, looks complete now svn path=/trunk/externals/io/; revision=7091 --- wiiremote/wiiremote.c | 78 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 31 deletions(-) (limited to 'wiiremote/wiiremote.c') diff --git a/wiiremote/wiiremote.c b/wiiremote/wiiremote.c index 35acd03..b379f5e 100644 --- a/wiiremote/wiiremote.c +++ b/wiiremote/wiiremote.c @@ -3,7 +3,6 @@ // Based on "DarwiinRemote" by Hiroaki Kimura #include "wiiremote.h" - #include // this type is used a lot (data array): @@ -53,37 +52,47 @@ void checkDevice(IOBluetoothDeviceRef device) CFStringRef myString; myString = IOBluetoothDeviceGetName(device); - if (CFStringCompare(myString, CFSTR("Nintendo RVL-CNT-01"), 0) == kCFCompareEqualTo) + if (myString != nil) { - gWiiRemote.device = IOBluetoothObjectRetain(device); + if (CFStringCompare(myString, CFSTR("Nintendo RVL-CNT-01"), 0) == kCFCompareEqualTo) + { + gWiiRemote.device = IOBluetoothObjectRetain(device); + if ( !wiiremote_connect()) // add in B3 + wiiremote_disconnect(); // add in B3 + } } } -IOBluetoothDeviceInquiryDeviceFoundCallback myFoundFunc(void *refCon, IOBluetoothDeviceInquiryRef inquiry, IOBluetoothDeviceRef device) +void myFoundFunc(void *refCon, IOBluetoothDeviceInquiryRef inquiry, IOBluetoothDeviceRef device) { checkDevice(device); } -IOBluetoothDeviceInquiryDeviceNameUpdatedCallback myUpdatedFunc(void *refCon, IOBluetoothDeviceInquiryRef inquiry, IOBluetoothDeviceRef device, uint32_t devicesRemaining) +void myUpdatedFunc(void *refCon, IOBluetoothDeviceInquiryRef inquiry, IOBluetoothDeviceRef device, uint32_t devicesRemaining) { checkDevice(device); } -IOBluetoothDeviceInquiryCompleteCallback myCompleteFunc(void *refCon, IOBluetoothDeviceInquiryRef inquiry, IOReturn error, Boolean aborted) +void myCompleteFunc(void *refCon, IOBluetoothDeviceInquiryRef inquiry, IOReturn error, Boolean aborted) { - IOReturn result; - if (aborted) return; // called by stop ;) if (error != kIOReturnSuccess) { wiiremote_stopsearch(); - return; } } //-------------------------------------------------------------------------------------------- +Boolean wiiremote_isconnected(void) +{ + Boolean result; + + result = gWiiRemote.device != nil && IOBluetoothDeviceIsConnected(gWiiRemote.device); + return result; +} + Boolean wiiremote_search(void) { IOReturn ret; @@ -131,7 +140,7 @@ Boolean wiiremote_stopsearch(void) //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- - IOBluetoothL2CAPChannelIncomingDataListener myDataListener(IOBluetoothL2CAPChannelRef channel, void *data, UInt16 length, void *refCon) + void myDataListener(IOBluetoothL2CAPChannelRef channel, void *data, UInt16 length, void *refCon) { unsigned char *dp = (unsigned char*)data; @@ -248,35 +257,33 @@ Boolean wiiremote_stopsearch(void) gWiiRemote.posY = oy; } -IOBluetoothL2CAPChannelIncomingEventListener myEventListener(IOBluetoothL2CAPChannelRef channel, void *refCon, IOBluetoothL2CAPChannelEvent *event) +void myEventListener(IOBluetoothL2CAPChannelRef channel, void *refCon, IOBluetoothL2CAPChannelEvent *event) { - switch (event->eventType) + if (event->eventType == kIOBluetoothL2CAPChannelEventTypeData) { - case kIOBluetoothL2CAPChannelEventTypeData: - // In thise case: - // event->u.newData.dataPtr is a pointer to the block of data received. - // event->u.newData.dataSize is the size of the block of data. - myDataListener(channel, event->u.data.dataPtr, event->u.data.dataSize, refCon); - break; - - case kIOBluetoothL2CAPChannelEventTypeClosed: - // In this case: - // event->u.terminatedChannel is the channel that was terminated. It can be converted in an IOBluetoothL2CAPChannel - // object with [IOBluetoothL2CAPChannel withL2CAPChannelRef:]. (see below). - break; + // In thise case: + // event->u.newData.dataPtr is a pointer to the block of data received. + // event->u.newData.dataSize is the size of the block of data. + myDataListener(channel, event->u.data.dataPtr, event->u.data.dataSize, refCon); + } + else + if (event->eventType == kIOBluetoothL2CAPChannelEventTypeClosed) + { + // In this case: + // event->u.terminatedChannel is the channel that was terminated. It can be converted in an IOBluetoothL2CAPChannel + // object with [IOBluetoothL2CAPChannel withL2CAPChannelRef:]. (see below). } } -IOBluetoothUserNotificationCallback myDisconnectedFunc(void * refCon, IOBluetoothUserNotificationRef inRef, IOBluetoothObjectRef objectRef) +void myDisconnectedFunc(void * refCon, IOBluetoothUserNotificationRef inRef, IOBluetoothObjectRef objectRef) { - wiiremote_disconnect(); + //wiiremote_disconnect(); } //-------------------------------------------------------------------------------------------- Boolean wiiremote_connect(void) { - IOReturn result; short i; if (gWiiRemote.device == nil) @@ -289,7 +296,8 @@ Boolean wiiremote_connect(void) break; usleep(10000); // wait 10ms } - if (i==kTrial) return false; + if (i==kTrial) + return false; gWiiRemote.disconnectNotification = IOBluetoothDeviceRegisterForDisconnectNotification(gWiiRemote.device, myDisconnectedFunc, 0); @@ -300,7 +308,8 @@ Boolean wiiremote_connect(void) break; usleep(10000); // wait 10ms } - if (i==kTrial) return false; + if (i==kTrial) + return false; // open L2CAPChannel : BluetoothL2CAPPSM = 17 for (i=0; i