move most of the wiiuse mac implementation into WiiuseWiimote* object
This commit is contained in:
117
src/os_mac.m
117
src/os_mac.m
@@ -47,7 +47,7 @@
|
|||||||
|
|
||||||
|
|
||||||
#pragma mark -
|
#pragma mark -
|
||||||
#pragma mark connect
|
#pragma mark connect, disconnect
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Connect to a wiimote with a known address.
|
* @brief Connect to a wiimote with a known address.
|
||||||
@@ -64,7 +64,7 @@ static short wiiuse_os_connect_single(struct wiimote_t* wm) {
|
|||||||
if(!wm) {
|
if(!wm) {
|
||||||
WIIUSE_ERROR("No Wiimote given.");
|
WIIUSE_ERROR("No Wiimote given.");
|
||||||
return 0;
|
return 0;
|
||||||
} else if(wm && (!WIIMOTE_IS_SET(wm, WIIMOTE_STATE_DEV_FOUND) || wm->device == NULL)) {
|
} else if(wm && (!WIIMOTE_IS_SET(wm, WIIMOTE_STATE_DEV_FOUND) || wm->objc_wm == NULL)) {
|
||||||
WIIUSE_ERROR("Tried to connect Wiimote without an address.");
|
WIIUSE_ERROR("Tried to connect Wiimote without an address.");
|
||||||
return 0;
|
return 0;
|
||||||
} else if(WIIMOTE_IS_CONNECTED(wm)) {
|
} else if(WIIMOTE_IS_CONNECTED(wm)) {
|
||||||
@@ -78,7 +78,7 @@ static short wiiuse_os_connect_single(struct wiimote_t* wm) {
|
|||||||
short result = 0;
|
short result = 0;
|
||||||
|
|
||||||
// connect
|
// connect
|
||||||
WiiuseWiimote* objc_wm = [[WiiuseWiimote alloc] initWithPtr: wm];
|
WiiuseWiimote* objc_wm = (WiiuseWiimote*) wm->objc_wm;
|
||||||
if([objc_wm connect] == kIOReturnSuccess) {
|
if([objc_wm connect] == kIOReturnSuccess) {
|
||||||
WIIUSE_INFO("Connected to Wiimote [id %i].", wm->unid);
|
WIIUSE_INFO("Connected to Wiimote [id %i].", wm->unid);
|
||||||
|
|
||||||
@@ -93,8 +93,6 @@ static short wiiuse_os_connect_single(struct wiimote_t* wm) {
|
|||||||
wiiuse_set_report_type(wm);
|
wiiuse_set_report_type(wm);
|
||||||
|
|
||||||
result = 1;
|
result = 1;
|
||||||
} else {
|
|
||||||
[objc_wm release];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[pool drain];
|
[pool drain];
|
||||||
@@ -111,8 +109,8 @@ int wiiuse_os_connect(struct wiimote_t** wm, int wiimotes) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!WIIMOTE_IS_SET(wm[i], WIIMOTE_STATE_DEV_FOUND) || !wm[i]->device) {
|
if (!WIIMOTE_IS_SET(wm[i], WIIMOTE_STATE_DEV_FOUND) || !wm[i]->objc_wm) {
|
||||||
// If the device is not set, skip it
|
// If the device is not found, skip it
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,56 +121,13 @@ int wiiuse_os_connect(struct wiimote_t** wm, int wiimotes) {
|
|||||||
return connected;
|
return connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark disconnect
|
|
||||||
|
|
||||||
#define WIIUSE_MAC_CLOSE_CHANNEL(wm, channel) \
|
|
||||||
{ \
|
|
||||||
IOBluetoothL2CAPChannel* channel = WIIUSE_IOBluetoothL2CAPChannelRef_to_IOBluetoothL2CAPChannel((wm)->channel##Channel); \
|
|
||||||
if([channel closeChannel] != kIOReturnSuccess) \
|
|
||||||
WIIUSE_ERROR("Unable to close " #channel " channel [id %i].", (wm)->unid); \
|
|
||||||
[channel release]; \
|
|
||||||
(wm)->channel##Channel = NULL; \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define WIIUSE_MAC_CLOSE_DEVICE_CONNECTION(wm) \
|
|
||||||
{ \
|
|
||||||
IOBluetoothDevice* device = WIIUSE_IOBluetoothDeviceRef_to_IOBluetoothDevice((wm)->device); \
|
|
||||||
if([device closeConnection] != kIOReturnSuccess) \
|
|
||||||
WIIUSE_ERROR("Unable to close the device connection [id %i].", (wm)->unid); \
|
|
||||||
[device release]; \
|
|
||||||
(wm)->device = NULL; \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define WIIUSE_MAC_DISCONNECT(wm) \
|
|
||||||
{ \
|
|
||||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; \
|
|
||||||
WIIUSE_MAC_CLOSE_CHANNEL(wm, interrupt); \
|
|
||||||
WIIUSE_MAC_CLOSE_CHANNEL(wm, control); \
|
|
||||||
WIIUSE_MAC_CLOSE_DEVICE_CONNECTION(wm); \
|
|
||||||
[pool drain]; \
|
|
||||||
}
|
|
||||||
|
|
||||||
void wiiuse_os_disconnect(struct wiimote_t* wm) {
|
void wiiuse_os_disconnect(struct wiimote_t* wm) {
|
||||||
if (!wm || !WIIMOTE_IS_CONNECTED(wm))
|
if (!wm || !WIIMOTE_IS_CONNECTED(wm) || !wm->objc_wm)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// disconnect
|
|
||||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||||
{ \
|
[((WiiuseWiimote*)wm->objc_wm) disconnect];
|
||||||
IOBluetoothL2CAPChannel* channel = WIIUSE_IOBluetoothL2CAPChannelRef_to_IOBluetoothL2CAPChannel((wm)->interruptChannel); \
|
|
||||||
if([channel closeChannel] != kIOReturnSuccess) \
|
|
||||||
WIIUSE_ERROR("Unable to close " "interrupt" " channel [id %i].", (wm)->unid); \
|
|
||||||
[channel release]; \
|
|
||||||
(wm)->interruptChannel = NULL; \
|
|
||||||
}
|
|
||||||
WIIUSE_MAC_CLOSE_CHANNEL(wm, control);
|
|
||||||
WIIUSE_MAC_CLOSE_DEVICE_CONNECTION(wm);
|
|
||||||
[pool drain];
|
[pool drain];
|
||||||
|
|
||||||
// clean up C struct
|
|
||||||
wm->event = WIIUSE_NONE;
|
|
||||||
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_CONNECTED);
|
|
||||||
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark -
|
#pragma mark -
|
||||||
@@ -203,25 +158,20 @@ int wiiuse_os_write(struct wiimote_t* wm, byte* buf, int len) {
|
|||||||
#pragma mark platform fields
|
#pragma mark platform fields
|
||||||
|
|
||||||
void wiiuse_init_platform_fields(struct wiimote_t* wm) {
|
void wiiuse_init_platform_fields(struct wiimote_t* wm) {
|
||||||
wm->device = NULL;
|
|
||||||
wm->objc_wm = NULL;
|
wm->objc_wm = NULL;
|
||||||
wm->controlChannel = NULL;
|
|
||||||
wm->interruptChannel = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wiiuse_cleanup_platform_fields(struct wiimote_t* wm) {
|
void wiiuse_cleanup_platform_fields(struct wiimote_t* wm) {
|
||||||
// disconnect & release device and channels
|
if(!wm) return;
|
||||||
// Note: this should already have happened, because this function is called
|
WiiuseWiimote* objc_wm = (WiiuseWiimote*) wm->objc_wm;
|
||||||
// once the device is disconnected. This is just paranoia.
|
|
||||||
WIIUSE_MAC_DISCONNECT(wm);
|
|
||||||
|
|
||||||
// this is also done on disconnect, so it's even more paranoia.
|
// disconnect
|
||||||
wm->device = NULL;
|
// Note: this should already have happened, because this function
|
||||||
wm->controlChannel = NULL;
|
// is called once the device is disconnected. This is just paranoia.
|
||||||
wm->interruptChannel = NULL;
|
[objc_wm disconnect];
|
||||||
|
|
||||||
// release WiiuseWiimote object
|
// release WiiuseWiimote object
|
||||||
[((WiiuseWiimote*) wm->objc_wm) release];
|
[objc_wm release];
|
||||||
wm->objc_wm = NULL;
|
wm->objc_wm = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,10 +180,13 @@ void wiiuse_cleanup_platform_fields(struct wiimote_t* wm) {
|
|||||||
|
|
||||||
@implementation WiiuseWiimote
|
@implementation WiiuseWiimote
|
||||||
|
|
||||||
- (id) initWithPtr: (wiimote*) wm_ {
|
- (id) initWithPtr: (wiimote*) wm_ device:(IOBluetoothDevice *)device_ {
|
||||||
self = [super init];
|
self = [super init];
|
||||||
if(self) {
|
if(self) {
|
||||||
wm = wm_;
|
wm = wm_;
|
||||||
|
device = device_;
|
||||||
|
controlChannel = nil;
|
||||||
|
interruptChannel = nil;
|
||||||
disconnectNotification = nil;
|
disconnectNotification = nil;
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
@@ -242,6 +195,11 @@ void wiiuse_cleanup_platform_fields(struct wiimote_t* wm) {
|
|||||||
- (void) dealloc {
|
- (void) dealloc {
|
||||||
wm = NULL;
|
wm = NULL;
|
||||||
|
|
||||||
|
[interruptChannel release];
|
||||||
|
[controlChannel release];
|
||||||
|
[device release];
|
||||||
|
|
||||||
|
[disconnectNotification unregister];
|
||||||
[disconnectNotification release];
|
[disconnectNotification release];
|
||||||
disconnectNotification = nil;
|
disconnectNotification = nil;
|
||||||
|
|
||||||
@@ -249,14 +207,12 @@ void wiiuse_cleanup_platform_fields(struct wiimote_t* wm) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (IOReturn) connect {
|
- (IOReturn) connect {
|
||||||
IOBluetoothDevice* device = WIIUSE_IOBluetoothDeviceRef_to_IOBluetoothDevice(wm->device);
|
|
||||||
if(!device) {
|
if(!device) {
|
||||||
WIIUSE_ERROR("Missing device.");
|
WIIUSE_ERROR("Missing device.");
|
||||||
return kIOReturnBadArgument;
|
return kIOReturnBadArgument;
|
||||||
}
|
}
|
||||||
|
|
||||||
// open channels
|
// open channels
|
||||||
IOBluetoothL2CAPChannel* controlChannel = nil, *interruptChannel = nil;
|
|
||||||
if ([device openL2CAPChannelSync:&controlChannel withPSM:WM_OUTPUT_CHANNEL delegate:self] != kIOReturnSuccess) {
|
if ([device openL2CAPChannelSync:&controlChannel withPSM:WM_OUTPUT_CHANNEL delegate:self] != kIOReturnSuccess) {
|
||||||
WIIUSE_ERROR("Unable to open L2CAP control channel [id %i].", wm->unid);
|
WIIUSE_ERROR("Unable to open L2CAP control channel [id %i].", wm->unid);
|
||||||
[device closeConnection];
|
[device closeConnection];
|
||||||
@@ -278,14 +234,33 @@ void wiiuse_cleanup_platform_fields(struct wiimote_t* wm) {
|
|||||||
return kIOReturnNotOpen;
|
return kIOReturnNotOpen;
|
||||||
}
|
}
|
||||||
|
|
||||||
// retain channels, and save them to the C struct
|
// retain channels
|
||||||
[controlChannel retain]; [interruptChannel retain];
|
[controlChannel retain];
|
||||||
wm->controlChannel = WIIUSE_IOBluetoothL2CAPChannel_to_IOBluetoothL2CAPChannelRef(controlChannel);
|
[interruptChannel retain];
|
||||||
wm->interruptChannel = WIIUSE_IOBluetoothL2CAPChannel_to_IOBluetoothL2CAPChannelRef(interruptChannel);
|
|
||||||
|
|
||||||
return kIOReturnSuccess;
|
return kIOReturnSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void) disconnect {
|
||||||
|
// interrupt channel
|
||||||
|
if([interruptChannel closeChannel] != kIOReturnSuccess)
|
||||||
|
WIIUSE_ERROR("Unable to close interrupt channel [id %i].", wm ? wm->unid : -1);
|
||||||
|
[interruptChannel release];
|
||||||
|
interruptChannel = nil;
|
||||||
|
|
||||||
|
// control channel
|
||||||
|
if([controlChannel closeChannel] != kIOReturnSuccess)
|
||||||
|
WIIUSE_ERROR("Unable to close control channel [id %i].", wm ? wm->unid : -1);
|
||||||
|
[controlChannel release];
|
||||||
|
controlChannel = nil;
|
||||||
|
|
||||||
|
// device
|
||||||
|
if([device closeConnection] != kIOReturnSuccess)
|
||||||
|
WIIUSE_ERROR("Unable to close the device connection [id %i].", wm ? wm->unid : -1);
|
||||||
|
[device release];
|
||||||
|
device = nil;
|
||||||
|
}
|
||||||
|
|
||||||
#pragma mark IOBluetoothL2CAPChannel delegates
|
#pragma mark IOBluetoothL2CAPChannel delegates
|
||||||
|
|
||||||
- (void) disconnected:(IOBluetoothUserNotification*) notification fromDevice:(IOBluetoothDevice*) device {
|
- (void) disconnected:(IOBluetoothUserNotification*) notification fromDevice:(IOBluetoothDevice*) device {
|
||||||
|
|||||||
@@ -42,57 +42,29 @@
|
|||||||
#import "../wiiuse_internal.h"
|
#import "../wiiuse_internal.h"
|
||||||
|
|
||||||
|
|
||||||
|
#if 0
|
||||||
#if defined(MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7
|
#if defined(MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7
|
||||||
#define WIIUSE_MAC_OS_X_VERSION_10_7_OR_ABOVE 1
|
#define WIIUSE_MAC_OS_X_VERSION_10_7_OR_ABOVE 1
|
||||||
#else
|
#else
|
||||||
#define WIIUSE_MAC_OS_X_VERSION_10_7_OR_ABOVE 0
|
#define WIIUSE_MAC_OS_X_VERSION_10_7_OR_ABOVE 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
// WIIUSE_IOBluetoothDevice_to_IOBluetoothDeviceRef
|
|
||||||
#if WIIUSE_MAC_OS_X_VERSION_10_7_OR_ABOVE
|
|
||||||
#define WIIUSE_IOBluetoothDevice_to_IOBluetoothDeviceRef(device) \
|
|
||||||
((IOBluetoothDeviceRef) (device))
|
|
||||||
#else
|
|
||||||
#define WIIUSE_IOBluetoothDevice_to_IOBluetoothDeviceRef(device) \
|
|
||||||
[(device) getDeviceRef]
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// WIIUSE_IOBluetoothDeviceRef_to_IOBluetoothDevice
|
|
||||||
#if WIIUSE_MAC_OS_X_VERSION_10_7_OR_ABOVE
|
|
||||||
#define WIIUSE_IOBluetoothDeviceRef_to_IOBluetoothDevice(ref) \
|
|
||||||
((IOBluetoothDevice*) (ref))
|
|
||||||
#else
|
|
||||||
#define WIIUSE_IOBluetoothDeviceRef_to_IOBluetoothDevice(ref) \
|
|
||||||
[IOBluetoothDevice withDeviceRef: (ref)]
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// WIIUSE_IOBluetoothL2CAPChannel_to_IOBluetoothL2CAPChannelRef
|
|
||||||
#if WIIUSE_MAC_OS_X_VERSION_10_7_OR_ABOVE
|
|
||||||
#define WIIUSE_IOBluetoothL2CAPChannel_to_IOBluetoothL2CAPChannelRef(channel) \
|
|
||||||
((IOBluetoothL2CAPChannelRef) (channel))
|
|
||||||
#else
|
|
||||||
#define WIIUSE_IOBluetoothL2CAPChannel_to_IOBluetoothL2CAPChannelRef(channel) \
|
|
||||||
[(channel) getL2CAPChannelRef]
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// WIIUSE_IOBluetoothL2CAPChannelRef_to_IOBluetoothL2CAPChannel
|
|
||||||
#if WIIUSE_MAC_OS_X_VERSION_10_7_OR_ABOVE
|
|
||||||
#define WIIUSE_IOBluetoothL2CAPChannelRef_to_IOBluetoothL2CAPChannel(ref) \
|
|
||||||
((IOBluetoothL2CAPChannel*) (ref))
|
|
||||||
#else
|
|
||||||
#define WIIUSE_IOBluetoothL2CAPChannelRef_to_IOBluetoothL2CAPChannel(ref) \
|
|
||||||
[IOBluetoothL2CAPChannel withChanneleRef: (ref)]
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@interface WiiuseWiimote : NSObject<IOBluetoothL2CAPChannelDelegate> {
|
@interface WiiuseWiimote : NSObject<IOBluetoothL2CAPChannelDelegate> {
|
||||||
wiimote* wm; // reference to the C wiimote struct
|
wiimote* wm; // reference to the C wiimote struct
|
||||||
|
|
||||||
|
IOBluetoothDevice* device;
|
||||||
|
IOBluetoothL2CAPChannel* controlChannel;
|
||||||
|
IOBluetoothL2CAPChannel* interruptChannel;
|
||||||
|
|
||||||
IOBluetoothUserNotification* disconnectNotification;
|
IOBluetoothUserNotification* disconnectNotification;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id) initWithPtr: (wiimote*) wm;
|
- (id) initWithPtr: (wiimote*) wm device: (IOBluetoothDevice*) device;
|
||||||
|
|
||||||
- (IOReturn) connect;
|
- (IOReturn) connect;
|
||||||
|
- (void) disconnect;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|||||||
@@ -130,7 +130,7 @@
|
|||||||
IOBluetoothDevice* device = [devices objectAtIndex:i];
|
IOBluetoothDevice* device = [devices objectAtIndex:i];
|
||||||
|
|
||||||
// save the device in the wiimote structure
|
// save the device in the wiimote structure
|
||||||
wiimotes[i]->device = WIIUSE_IOBluetoothDevice_to_IOBluetoothDeviceRef(device);
|
wiimotes[i]->objc_wm = (void*) [[WiiuseWiimote alloc] initWithPtr:wiimotes[i] device: device];
|
||||||
[device retain]; // must retain it for later access through its ref
|
[device retain]; // must retain it for later access through its ref
|
||||||
|
|
||||||
// mark as found
|
// mark as found
|
||||||
|
|||||||
@@ -723,10 +723,7 @@ typedef struct wiimote_t {
|
|||||||
#ifdef WIIUSE_MAC
|
#ifdef WIIUSE_MAC
|
||||||
/** @name Mac OS X-specific members */
|
/** @name Mac OS X-specific members */
|
||||||
/** @{ */
|
/** @{ */
|
||||||
WCONST IOBluetoothDeviceRef device; /** Device reference */
|
|
||||||
WCONST void* objc_wm; /** WiiuseWiimote* as opaque pointer */
|
WCONST void* objc_wm; /** WiiuseWiimote* as opaque pointer */
|
||||||
WCONST IOBluetoothL2CAPChannelRef controlChannel; /** control channel reference */
|
|
||||||
WCONST IOBluetoothL2CAPChannelRef interruptChannel; /** interrupt channel reference */
|
|
||||||
/** @} */
|
/** @} */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user