From 3917941ae1ed902897fd0f0e84be1dfc606dd793 Mon Sep 17 00:00:00 2001 From: tmator Date: Thu, 30 Apr 2009 14:23:45 +0000 Subject: [PATCH] wiimote for osx WIP git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@3109 8ced0084-cf51-0410-be5f-012b33b47a6e --- Externals/WiiUseSrc/Src/SConscript | 14 +- Externals/WiiUseSrc/Src/classic.c | 2 +- Externals/WiiUseSrc/Src/events.c | 10 +- Externals/WiiUseSrc/Src/guitar_hero_3.c | 2 +- Externals/WiiUseSrc/Src/io_osx.c | 212 +++++++++++++++++-- Externals/WiiUseSrc/Src/nunchuk.c | 2 +- Externals/WiiUseSrc/Src/wiiuse.c | 4 +- Source/Plugins/Plugin_Wiimote/Src/SConscript | 16 +- 8 files changed, 222 insertions(+), 40 deletions(-) diff --git a/Externals/WiiUseSrc/Src/SConscript b/Externals/WiiUseSrc/Src/SConscript index 02cb68b2d0..61e3afc354 100644 --- a/Externals/WiiUseSrc/Src/SConscript +++ b/Externals/WiiUseSrc/Src/SConscript @@ -24,10 +24,16 @@ if env['HAVE_BLUEZ']: if sys.platform == 'darwin': files += [ "io_osx.c", ] -env_wiiuse = env.Clone( - CCFLAGS = env.filterWarnings(env['CCFLAGS']) + ' -fvisibility=default', - CXXFLAGS = env.filterWarnings(env['CXXFLAGS']), - ) +if sys.platform == 'darwin': + env_wiiuse = env.Clone( + CCFLAGS = env.filterWarnings(env['CCFLAGS']) + ' -fvisibility=default -x objective-c++', + CXXFLAGS = env.filterWarnings(env['CXXFLAGS']) + ' -x objective-c++', + ) +else: + env_wiiuse = env.Clone( + CCFLAGS = env.filterWarnings(env['CCFLAGS']) + ' -fvisibility=default', + CXXFLAGS = env.filterWarnings(env['CXXFLAGS']), + ) libs = [ 'm', diff --git a/Externals/WiiUseSrc/Src/classic.c b/Externals/WiiUseSrc/Src/classic.c index 1d3abaa33f..683778858c 100644 --- a/Externals/WiiUseSrc/Src/classic.c +++ b/Externals/WiiUseSrc/Src/classic.c @@ -83,7 +83,7 @@ int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte */ if (data[offset + 16] == 0xFF) { /* get the calibration data */ - byte* handshake_buf = malloc(EXP_HANDSHAKE_LEN * sizeof(byte)); + byte* handshake_buf = (byte *)malloc(EXP_HANDSHAKE_LEN * sizeof(byte)); WIIUSE_DEBUG("Classic controller handshake appears invalid, trying again."); wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN); diff --git a/Externals/WiiUseSrc/Src/events.c b/Externals/WiiUseSrc/Src/events.c index 578ee3f08b..3db4ea6f9f 100644 --- a/Externals/WiiUseSrc/Src/events.c +++ b/Externals/WiiUseSrc/Src/events.c @@ -631,7 +631,7 @@ static void handle_expansion(struct wiimote_t* wm, byte* msg) { * a handshake with the expansion. */ void handshake_expansion(struct wiimote_t* wm, byte* data, unsigned short len) { - int id; + int wid; if (!data) { byte* handshake_buf; @@ -649,7 +649,7 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, unsigned short len) { wiiuse_write_data(wm, WM_EXP_MEM_ENABLE, &buf, 1); /* get the calibration data */ - handshake_buf = malloc(EXP_HANDSHAKE_LEN * sizeof(byte)); + handshake_buf = (byte *)malloc(EXP_HANDSHAKE_LEN * sizeof(byte)); wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN); /* tell the wiimote to send expansion data */ @@ -658,10 +658,10 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, unsigned short len) { return; } - id = BIG_ENDIAN_LONG(*(int*)(data + 220)); + wid = BIG_ENDIAN_LONG(*(int*)(data + 220)); /* call the corresponding handshake function for this expansion */ - switch (id) { + switch (wid) { case EXP_ID_CODE_NUNCHUK: { if (nunchuk_handshake(wm, &wm->exp.nunchuk, data, len)) @@ -682,7 +682,7 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, unsigned short len) { } default: { - WIIUSE_WARNING("Unknown expansion type. Code: 0x%x", id); + WIIUSE_WARNING("Unknown expansion type. Code: 0x%x", wid); break; } } diff --git a/Externals/WiiUseSrc/Src/guitar_hero_3.c b/Externals/WiiUseSrc/Src/guitar_hero_3.c index 29c58af10b..8b2f6b0ce5 100644 --- a/Externals/WiiUseSrc/Src/guitar_hero_3.c +++ b/Externals/WiiUseSrc/Src/guitar_hero_3.c @@ -88,7 +88,7 @@ int guitar_hero_3_handshake(struct wiimote_t* wm, struct guitar_hero_3_t* gh3, b */ if (data[offset + 16] == 0xFF) { /* get the calibration data */ - byte* handshake_buf = malloc(EXP_HANDSHAKE_LEN * sizeof(byte)); + byte* handshake_buf = (byte *)malloc(EXP_HANDSHAKE_LEN * sizeof(byte)); WIIUSE_DEBUG("Guitar Hero 3 handshake appears invalid, trying again."); wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN); diff --git a/Externals/WiiUseSrc/Src/io_osx.c b/Externals/WiiUseSrc/Src/io_osx.c index 0b7e69738c..b2f829a715 100644 --- a/Externals/WiiUseSrc/Src/io_osx.c +++ b/Externals/WiiUseSrc/Src/io_osx.c @@ -37,12 +37,155 @@ #include #include +//osx inlcude +#import +#import +#import +#import + +IOBluetoothDeviceInquiry *bti; +IOBluetoothDevice * btd; +IOBluetoothL2CAPChannel * _ichan; +IOBluetoothL2CAPChannel * _cchan; + #include "definitions.h" #include "wiiuse_internal.h" #include "io.h" static int wiiuse_connect_single(struct wiimote_t* wm, char* address); +@interface SearchBT: NSObject {} +-(void) deviceInquiryComplete: (IOBluetoothDeviceInquiry*) sender + error: (IOReturn) error + aborted: (BOOL) aborted; +-(void) deviceInquiryDeviceFound: (IOBluetoothDeviceInquiry*) sender + device: (IOBluetoothDevice*) device; + +@end + +@interface ConnectBT: NSObject {} + +-(void) connectToWiimotes; + +- (IOBluetoothL2CAPChannel *) openL2CAPChannelWithPSM:(BluetoothL2CAPPSM) psm delegate:(id) delegate; +@end + +@implementation ConnectBT + +#pragma mark - +#pragma mark Bluetooth + +- (void) l2capChannelOpenComplete:(IOBluetoothL2CAPChannel*) l2capChannel status:(IOReturn) error +{ + //channel opened + //something to do here ? +} + + +- (void)l2capChannelData:(IOBluetoothL2CAPChannel *) l2capChannel data:(unsigned char *)buffer length:(NSUInteger)length { + //data ? + //here we got data from wiimote + printf("data %d\n",length); +} + + + +- (void) l2capChannelClosed:(IOBluetoothL2CAPChannel*) l2capChannel +{ + //channel closed +} + +#pragma mark - +- (IOBluetoothL2CAPChannel *) openL2CAPChannelWithPSM:(BluetoothL2CAPPSM) psm delegate:(id) delegate +{ + IOBluetoothL2CAPChannel * channel = nil; + IOReturn ret = kIOReturnSuccess; + + printf("Open channel (PSM:%i) ...", psm); + if ((ret = (IOReturn)[btd openL2CAPChannelSync:&channel withPSM:psm delegate:delegate]) != kIOReturnSuccess) { + printf("Could not open L2CAP channel (psm:%i)", psm); + channel = nil; + //TODO : close the connection + } + + return channel; +} + + +-(void) connectToWiimotes +{ + + NSEnumerator * en = [[bti foundDevices] objectEnumerator]; + + id device = nil; + while ((device = [en nextObject]) != nil) { + btd = device; + + [btd openL2CAPChannelSync:&_cchan withPSM:kBluetoothL2CAPPSMHIDControl delegate:self]; + + if (!_cchan) + printf("error when initialised cchan\n"); + + usleep (20000); + + [btd openL2CAPChannelSync:&_ichan withPSM:kBluetoothL2CAPPSMHIDInterrupt delegate:self]; + + if (!_ichan) + printf("error when initialised ichan\n"); + + usleep (20000); + + } + +} + +@end + +@implementation SearchBT +-(void) deviceInquiryComplete: (IOBluetoothDeviceInquiry*) sender + error: (IOReturn) error + aborted: (BOOL) aborted +{ + int founded = [[bti foundDevices] count]; + CFRunLoopStop( CFRunLoopGetCurrent() ); +} +-(void) deviceInquiryDeviceFound: (IOBluetoothDeviceInquiry*) sender + device: (IOBluetoothDevice*) device +{ + printf("discovered one wiimote%s\n", [[device getAddressString] UTF8String]); + [bti stop]; +} + + + +@end + +SearchBT *sbt; +ConnectBT *cbt; + +void detectWiimote() +{ + + [bti setDelegate: sbt]; + [bti setInquiryLength:20]; + [bti setSearchCriteria:kBluetoothServiceClassMajorAny majorDeviceClass:0x05 minorDeviceClass:0x01]; + [bti setUpdateNewDeviceNames:NO]; + + IOReturn ret = [bti start]; + if (ret == kIOReturnSuccess) + { + [bti retain]; + } + else + { + printf("error when detecting wiimote\n"); + [bti setDelegate: nil]; + bti= nil; + } + + +} + /** * @brief Find a wiimote or wiimotes. * @@ -60,20 +203,27 @@ static int wiiuse_connect_single(struct wiimote_t* wm, char* address); * devices. */ int wiiuse_find(struct wiimote_t** wm, int max_wiimotes, int timeout) { - int found_devices; - int found_wiimotes; - /* reset all wiimote bluetooth device addresses */ - /*for (found_wiimotes = 0; found_wiimotes < max_wiimotes; ++found_wiimotes) - wm[found_wiimotes]->bdaddr = *BDADDR_ANY; - */ - found_wiimotes = 0; + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - WIIUSE_INFO("Found %i bluetooth device(s).", found_devices); + sbt = [[SearchBT alloc] init]; + cbt = [[ConnectBT alloc] init]; + bti = [[IOBluetoothDeviceInquiry alloc] init]; + + detectWiimote(); + + CFRunLoopRun(); + + + [bti release]; + [sbt release]; + + [pool release]; + + + return 1; - WIIMOTE_ENABLE_STATE(wm[found_wiimotes], WIIMOTE_STATE_DEV_FOUND); - return found_wiimotes; } @@ -94,19 +244,20 @@ int wiiuse_find(struct wiimote_t** wm, int max_wiimotes, int timeout) { * by the wiiuse_find() function, but can also be set manually. */ int wiiuse_connect(struct wiimote_t** wm, int wiimotes) { - int connected = 0; int i = 0; - for (; i < wiimotes; ++i) { + /*for (; i < wiimotes; ++i) { if (!WIIMOTE_IS_SET(wm[i], WIIMOTE_STATE_DEV_FOUND)) - /* if the device address is not set, skip it */ + // if the device address is not set, skip it continue; if (wiiuse_connect_single(wm[i], NULL)) ++connected; - } + }*/ + wiiuse_connect_single(wm[i], NULL); - return connected; + //return connected; + return 1; } @@ -124,14 +275,22 @@ static int wiiuse_connect_single(struct wiimote_t* wm, char* address) { if (!wm || WIIMOTE_IS_CONNECTED(wm)) return 0; + //connect to the wiimote + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - WIIUSE_INFO("Connected to wiimote [id %i].", wm->unid); + cbt = [[ConnectBT alloc] init]; + //start to connect to the wiimotes + [cbt connectToWiimotes]; - /* do the handshake */ - WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_CONNECTED); - wiiuse_handshake(wm, NULL, 0); + //run the main loop to get bt data + CFRunLoopRun(); + /* do the handshake */ + WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_CONNECTED); + wiiuse_handshake(wm, NULL, 0); - wiiuse_set_report_type(wm); + wiiuse_set_report_type(wm); + + [pool release]; return 1; } @@ -150,18 +309,27 @@ void wiiuse_disconnect(struct wiimote_t* wm) { if (!wm || WIIMOTE_IS_CONNECTED(wm)) return; + //TODO + WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_CONNECTED); WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE); } int wiiuse_io_read(struct wiimote_t* wm) { - /* not used */ - return 0; + + //TODO + return 1; } int wiiuse_io_write(struct wiimote_t* wm, byte* buf, int len) { + + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + [_cchan writeSync:buf length:len]; + + usleep (10000); + [pool release]; return 1; } diff --git a/Externals/WiiUseSrc/Src/nunchuk.c b/Externals/WiiUseSrc/Src/nunchuk.c index fd763bcb9b..40a7b7759a 100644 --- a/Externals/WiiUseSrc/Src/nunchuk.c +++ b/Externals/WiiUseSrc/Src/nunchuk.c @@ -81,7 +81,7 @@ int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, un */ if (data[offset + 16] == 0xFF) { /* get the calibration data */ - byte* handshake_buf = malloc(EXP_HANDSHAKE_LEN * sizeof(byte)); + byte* handshake_buf = (byte *)malloc(EXP_HANDSHAKE_LEN * sizeof(byte)); WIIUSE_DEBUG("Nunchuk handshake appears invalid, trying again."); wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN); diff --git a/Externals/WiiUseSrc/Src/wiiuse.c b/Externals/WiiUseSrc/Src/wiiuse.c index 028862c827..271b4a5059 100644 --- a/Externals/WiiUseSrc/Src/wiiuse.c +++ b/Externals/WiiUseSrc/Src/wiiuse.c @@ -115,10 +115,10 @@ struct wiimote_t** wiiuse_init(int wiimotes) { if (!wiimotes) return NULL; - wm = malloc(sizeof(struct wiimote_t*) * wiimotes); + wm = (wiimote_t **)malloc(sizeof(struct wiimote_t*) * wiimotes); for (i = 0; i < wiimotes; ++i) { - wm[i] = malloc(sizeof(struct wiimote_t)); + wm[i] = (wiimote_t *)malloc(sizeof(struct wiimote_t)); memset(wm[i], 0, sizeof(struct wiimote_t)); wm[i]->unid = i+1; diff --git a/Source/Plugins/Plugin_Wiimote/Src/SConscript b/Source/Plugins/Plugin_Wiimote/Src/SConscript index 0a7203950c..a596bbe597 100644 --- a/Source/Plugins/Plugin_Wiimote/Src/SConscript +++ b/Source/Plugins/Plugin_Wiimote/Src/SConscript @@ -37,9 +37,17 @@ if wmenv['HAVE_WIIUSE']: files += [ "ReadWiimote.cpp" ] cxxflags += ['-DHAVE_WIIUSE'] -wmenv.Append( - CXXFLAGS = cxxflags, - LIBS = libs, - ) +if sys.platform == 'darwin': + wmenv.Append( + CXXFLAGS = cxxflags, + LINKFLAGS = ['-framework' ,'IOBluetooth'], + LIBS = libs, + ) +else: + wmenv.Append( + CXXFLAGS = cxxflags, + LIBS = libs, + ) + wmenv.SharedLibrary(env['plugin_dir']+name, files)