2009-02-16 21:12:37 +00:00
|
|
|
/*
|
|
|
|
* wiiuse
|
|
|
|
*
|
|
|
|
* Written By:
|
|
|
|
* Michael Laforest < para >
|
|
|
|
* Email: < thepara (--AT--) g m a i l [--DOT--] com >
|
|
|
|
*
|
|
|
|
* Copyright 2006-2007
|
|
|
|
*
|
|
|
|
* This file is part of wiiuse.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
2010-06-09 19:36:08 +00:00
|
|
|
* @brief Handles device I/O for OS X.
|
2009-02-16 21:12:37 +00:00
|
|
|
*/
|
|
|
|
|
2010-07-29 13:29:15 +00:00
|
|
|
#import <CoreServices/CoreServices.h>
|
2010-08-11 20:53:19 +00:00
|
|
|
extern OSErr UpdateSystemActivity(UInt8 activity);
|
2010-06-09 19:36:08 +00:00
|
|
|
#define BLUETOOTH_VERSION_USE_CURRENT
|
2009-04-30 14:23:45 +00:00
|
|
|
#import <IOBluetooth/objc/IOBluetoothDevice.h>
|
|
|
|
#import <IOBluetooth/objc/IOBluetoothDeviceInquiry.h>
|
2010-02-13 20:54:05 +00:00
|
|
|
#import <IOBluetooth/objc/IOBluetoothHostController.h>
|
2010-06-09 19:36:08 +00:00
|
|
|
#import <IOBluetooth/objc/IOBluetoothL2CAPChannel.h>
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2009-02-16 21:12:37 +00:00
|
|
|
#include "definitions.h"
|
|
|
|
#include "wiiuse_internal.h"
|
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
static int wiiuse_connect_single(struct wiimote_t *wm, char *address);
|
2009-05-05 20:25:55 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
IOBluetoothDevice *btd;
|
|
|
|
IOBluetoothL2CAPChannel *ichan;
|
|
|
|
IOBluetoothL2CAPChannel *cchan;
|
2009-02-16 21:12:37 +00:00
|
|
|
|
2010-07-22 06:05:08 +00:00
|
|
|
#define QUEUE_SIZE 64
|
2010-06-09 19:36:08 +00:00
|
|
|
volatile struct buffer {
|
|
|
|
char data[MAX_PAYLOAD];
|
|
|
|
int len;
|
|
|
|
} queue[QUEUE_SIZE];
|
2010-07-22 06:05:08 +00:00
|
|
|
volatile int reader, writer, outstanding, watermark;
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
@interface SearchBT: NSObject {
|
|
|
|
@public
|
2010-07-31 19:06:44 +00:00
|
|
|
unsigned int maxDevices;
|
2010-06-09 19:36:08 +00:00
|
|
|
}
|
2009-04-30 14:23:45 +00:00
|
|
|
@end
|
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
@implementation SearchBT
|
|
|
|
- (void) deviceInquiryComplete: (IOBluetoothDeviceInquiry *) sender
|
|
|
|
error: (IOReturn) error
|
|
|
|
aborted: (BOOL) aborted
|
2009-05-05 20:25:55 +00:00
|
|
|
{
|
2010-06-09 19:36:08 +00:00
|
|
|
CFRunLoopStop(CFRunLoopGetCurrent());
|
2009-04-30 14:23:45 +00:00
|
|
|
}
|
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
- (void) deviceInquiryDeviceFound: (IOBluetoothDeviceInquiry *) sender
|
|
|
|
device: (IOBluetoothDevice *) device
|
|
|
|
{
|
|
|
|
WIIUSE_INFO("Discovered bluetooth device at %s: %s",
|
|
|
|
[[device getAddressString] UTF8String],
|
|
|
|
[[device getName] UTF8String]);
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
if ([[sender foundDevices] count] == maxDevices)
|
|
|
|
[sender stop];
|
2009-04-30 14:23:45 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
@interface ConnectBT: NSObject {}
|
|
|
|
@end
|
2009-05-05 20:25:55 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
@implementation ConnectBT
|
|
|
|
- (void) l2capChannelData: (IOBluetoothL2CAPChannel *) l2capChannel
|
|
|
|
data: (byte *) data
|
|
|
|
length: (NSUInteger) length
|
2009-04-30 14:23:45 +00:00
|
|
|
{
|
2010-06-09 19:36:08 +00:00
|
|
|
// IOBluetoothDevice *device = [l2capChannel getDevice];
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
if (length > MAX_PAYLOAD) {
|
|
|
|
WIIUSE_WARNING("Dropping wiimote packet - too large");
|
|
|
|
return;
|
|
|
|
}
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
if (queue[writer].len != 0) {
|
|
|
|
WIIUSE_WARNING("Dropping wiimote packet - queue full");
|
|
|
|
return;
|
|
|
|
}
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
memcpy(queue[writer].data, data, length);
|
|
|
|
queue[writer].len = length;
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
writer++;
|
|
|
|
outstanding++;
|
|
|
|
if (writer == QUEUE_SIZE)
|
|
|
|
writer = 0;
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2010-07-22 06:05:08 +00:00
|
|
|
if (outstanding > watermark) {
|
|
|
|
watermark = outstanding;
|
|
|
|
WIIUSE_WARNING("New wiimote queue watermark %d", watermark);
|
|
|
|
}
|
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
CFRunLoopStop(CFRunLoopGetCurrent());
|
2010-07-29 13:29:15 +00:00
|
|
|
|
2010-08-11 20:53:19 +00:00
|
|
|
UpdateSystemActivity(1);
|
2010-06-09 19:36:08 +00:00
|
|
|
}
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
- (void) l2capChannelClosed: (IOBluetoothL2CAPChannel *) l2capChannel
|
|
|
|
{
|
|
|
|
// IOBluetoothDevice *device = [l2capChannel getDevice];
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
WIIUSE_WARNING("L2CAP channel was closed");
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
if (l2capChannel == cchan)
|
|
|
|
cchan = nil;
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
if (l2capChannel == ichan)
|
|
|
|
ichan = nil;
|
2009-04-30 14:23:45 +00:00
|
|
|
}
|
2010-06-09 19:36:08 +00:00
|
|
|
@end
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2009-02-16 21:12:37 +00:00
|
|
|
/**
|
|
|
|
* @brief Find a wiimote or wiimotes.
|
|
|
|
*
|
2010-06-09 19:36:08 +00:00
|
|
|
* @param wm An array of wiimote_t structures.
|
2009-02-16 21:12:37 +00:00
|
|
|
* @param max_wiimotes The number of wiimote structures in \a wm.
|
2010-06-09 19:36:08 +00:00
|
|
|
* @param timeout The number of seconds before timing out.
|
2009-02-16 21:12:37 +00:00
|
|
|
*
|
|
|
|
* @return The number of wiimotes found.
|
|
|
|
*
|
2010-06-09 19:36:08 +00:00
|
|
|
* @see wiiuse_connect()
|
2009-02-16 21:12:37 +00:00
|
|
|
*
|
2010-06-09 19:36:08 +00:00
|
|
|
* This function will only look for wiimote devices.
|
|
|
|
* When a device is found the address in the structures will be set.
|
|
|
|
* You can then call wiiuse_connect() to connect to the found
|
2009-02-16 21:12:37 +00:00
|
|
|
* devices.
|
|
|
|
*/
|
2010-06-09 19:36:08 +00:00
|
|
|
int wiiuse_find(struct wiimote_t **wm, int max_wiimotes, int timeout)
|
|
|
|
{
|
2010-02-14 14:06:33 +00:00
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
2010-06-09 19:36:08 +00:00
|
|
|
IOBluetoothHostController *bth;
|
|
|
|
IOBluetoothDeviceInquiry *bti;
|
|
|
|
SearchBT *sbt;
|
|
|
|
NSEnumerator *en;
|
|
|
|
int i, found_devices = 0;
|
|
|
|
|
|
|
|
bth = [[IOBluetoothHostController alloc] init];
|
|
|
|
if ([bth addressAsString] == nil)
|
2010-02-13 20:54:05 +00:00
|
|
|
{
|
2010-06-09 19:36:08 +00:00
|
|
|
WIIUSE_WARNING("No bluetooth host controller");
|
2010-02-13 20:54:05 +00:00
|
|
|
[bth release];
|
2010-06-09 19:36:08 +00:00
|
|
|
return 0;
|
2010-02-13 20:54:05 +00:00
|
|
|
}
|
2010-06-09 19:36:08 +00:00
|
|
|
|
2010-02-13 20:54:05 +00:00
|
|
|
sbt = [[SearchBT alloc] init];
|
2010-06-09 19:36:08 +00:00
|
|
|
sbt->maxDevices = max_wiimotes;
|
|
|
|
/*XXX*/ sbt->maxDevices = 1;
|
2010-02-13 20:54:05 +00:00
|
|
|
bti = [[IOBluetoothDeviceInquiry alloc] init];
|
2010-06-09 19:36:08 +00:00
|
|
|
[bti setDelegate: sbt];
|
|
|
|
[bti setInquiryLength: timeout];
|
|
|
|
[bti setSearchCriteria: kBluetoothServiceClassMajorAny
|
2010-07-22 06:05:08 +00:00
|
|
|
majorDeviceClass: kBluetoothDeviceClassMajorPeripheral
|
|
|
|
minorDeviceClass: kBluetoothDeviceClassMinorPeripheral2Joystick
|
|
|
|
];
|
2010-06-09 19:36:08 +00:00
|
|
|
|
|
|
|
IOReturn ret = [bti start];
|
|
|
|
if (ret == kIOReturnSuccess)
|
|
|
|
[bti retain];
|
|
|
|
else
|
|
|
|
WIIUSE_ERROR("Unable to do bluetooth discovery");
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2010-02-13 20:54:05 +00:00
|
|
|
CFRunLoopRun();
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
[bti stop];
|
|
|
|
found_devices = [[bti foundDevices] count];
|
|
|
|
|
2009-05-05 20:25:55 +00:00
|
|
|
WIIUSE_INFO("Found %i bluetooth device(s).", found_devices);
|
2010-06-09 19:36:08 +00:00
|
|
|
|
|
|
|
en = [[bti foundDevices] objectEnumerator];
|
|
|
|
for (i = 0; i < found_devices; i++) {
|
|
|
|
btd = [en nextObject];
|
|
|
|
WIIMOTE_ENABLE_STATE(wm[i], WIIMOTE_STATE_DEV_FOUND);
|
|
|
|
}
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2010-02-14 14:06:33 +00:00
|
|
|
[bth release];
|
2010-01-20 11:49:11 +00:00
|
|
|
[bti release];
|
2009-04-30 14:23:45 +00:00
|
|
|
[sbt release];
|
2010-01-20 11:49:11 +00:00
|
|
|
[pool release];
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
return found_devices;
|
2009-02-16 21:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Connect to a wiimote or wiimotes once an address is known.
|
|
|
|
*
|
2010-06-09 19:36:08 +00:00
|
|
|
* @param wm An array of wiimote_t structures.
|
2009-02-16 21:12:37 +00:00
|
|
|
* @param wiimotes The number of wiimote structures in \a wm.
|
|
|
|
*
|
|
|
|
* @return The number of wiimotes that successfully connected.
|
|
|
|
*
|
|
|
|
* @see wiiuse_find()
|
|
|
|
* @see wiiuse_connect_single()
|
|
|
|
* @see wiiuse_disconnect()
|
|
|
|
*
|
|
|
|
* Connect to a number of wiimotes when the address is already set
|
|
|
|
* in the wiimote_t structures. These addresses are normally set
|
|
|
|
* by the wiiuse_find() function, but can also be set manually.
|
|
|
|
*/
|
2010-06-09 19:36:08 +00:00
|
|
|
int wiiuse_connect(struct wiimote_t **wm, int wiimotes)
|
|
|
|
{
|
|
|
|
int i, connected = 0;
|
2009-02-16 21:12:37 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
for (i = 0; i < wiimotes; ++i)
|
|
|
|
{
|
2009-02-16 21:12:37 +00:00
|
|
|
if (!WIIMOTE_IS_SET(wm[i], WIIMOTE_STATE_DEV_FOUND))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (wiiuse_connect_single(wm[i], NULL))
|
2010-06-09 19:36:08 +00:00
|
|
|
connected++;
|
|
|
|
}
|
2009-02-16 21:12:37 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
return connected;
|
2009-02-16 21:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Connect to a wiimote with a known address.
|
|
|
|
*
|
2010-06-09 19:36:08 +00:00
|
|
|
* @param wm Pointer to a wiimote_t structure.
|
|
|
|
* @param address The address of the device to connect to. If NULL,
|
|
|
|
* use the address in the struct set by wiiuse_find().
|
2009-02-16 21:12:37 +00:00
|
|
|
*
|
|
|
|
* @return 1 on success, 0 on failure
|
|
|
|
*/
|
2010-06-09 19:36:08 +00:00
|
|
|
static int wiiuse_connect_single(struct wiimote_t *wm, char *address)
|
|
|
|
{
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
ConnectBT *cbt = [[ConnectBT alloc] init];
|
2009-02-16 21:12:37 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
if (wm == NULL || WIIMOTE_IS_CONNECTED(wm))
|
2009-02-16 21:12:37 +00:00
|
|
|
return 0;
|
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
[btd openL2CAPChannelSync: &cchan
|
|
|
|
withPSM: kBluetoothL2CAPPSMHIDControl delegate: cbt];
|
|
|
|
[btd openL2CAPChannelSync: &ichan
|
|
|
|
withPSM: kBluetoothL2CAPPSMHIDInterrupt delegate: cbt];
|
|
|
|
if (ichan == NULL || cchan == NULL) {
|
|
|
|
WIIUSE_ERROR("Unable to open L2CAP channels");
|
|
|
|
wiiuse_disconnect(wm);
|
|
|
|
}
|
2010-01-20 11:49:11 +00:00
|
|
|
|
2009-05-05 20:25:55 +00:00
|
|
|
WIIUSE_INFO("Connected to wiimote [id %i].", wm->unid);
|
2009-02-16 21:12:37 +00:00
|
|
|
|
2010-01-20 11:49:11 +00:00
|
|
|
WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_CONNECTED);
|
|
|
|
wiiuse_set_report_type(wm);
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
[cbt release];
|
2009-04-30 14:23:45 +00:00
|
|
|
[pool release];
|
2009-02-16 21:12:37 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Disconnect a wiimote.
|
|
|
|
*
|
|
|
|
* @param wm Pointer to a wiimote_t structure.
|
|
|
|
*
|
|
|
|
* @see wiiuse_connect()
|
|
|
|
*
|
|
|
|
* Note that this will not free the wiimote structure.
|
|
|
|
*/
|
2010-06-09 19:36:08 +00:00
|
|
|
void wiiuse_disconnect(struct wiimote_t *wm)
|
|
|
|
{
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
|
|
|
if (wm == NULL)
|
2009-02-16 21:12:37 +00:00
|
|
|
return;
|
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
WIIUSE_INFO("Disconnecting wiimote [id %i]", wm->unid);
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2009-02-16 21:12:37 +00:00
|
|
|
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_CONNECTED);
|
|
|
|
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE);
|
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
[cchan closeChannel];
|
|
|
|
[ichan closeChannel];
|
|
|
|
[btd closeConnection];
|
|
|
|
[pool release];
|
|
|
|
}
|
2009-08-01 19:35:45 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
int wiiuse_io_read(struct wiimote_t *wm)
|
|
|
|
{
|
|
|
|
int bytes;
|
2009-08-01 19:35:45 +00:00
|
|
|
|
2010-01-20 11:49:11 +00:00
|
|
|
if (!WIIMOTE_IS_CONNECTED(wm))
|
2010-06-09 19:36:08 +00:00
|
|
|
return 0;
|
2009-04-30 14:23:45 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
if (outstanding == 0)
|
|
|
|
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1, true);
|
2009-05-05 20:25:55 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
if (queue[reader].len == 0)
|
2010-03-05 07:03:44 +00:00
|
|
|
return 0;
|
2009-05-05 20:25:55 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
bytes = queue[reader].len;
|
|
|
|
memcpy(wm->event_buf, queue[reader].data, bytes);
|
|
|
|
queue[reader].len = 0;
|
2009-02-16 21:12:37 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
reader++;
|
|
|
|
outstanding--;
|
|
|
|
if (reader == QUEUE_SIZE)
|
|
|
|
reader = 0;
|
2009-02-16 21:12:37 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
if (wm->event_buf[0] == '\0')
|
|
|
|
bytes = 0;
|
|
|
|
|
|
|
|
return bytes;
|
2009-02-16 21:12:37 +00:00
|
|
|
}
|
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
int wiiuse_io_write(struct wiimote_t *wm, byte *buf, int len)
|
|
|
|
{
|
|
|
|
[cchan writeSync: buf length: len];
|
2009-02-16 21:12:37 +00:00
|
|
|
|
2010-06-09 19:36:08 +00:00
|
|
|
return len;
|
|
|
|
}
|