| 1 | /*---------------------------------------------------------------------------- |
| 2 | ChucK Strongly-timed Audio Programming Language |
| 3 | Compiler, Virtual Machine, and Synthesis Engine |
| 4 | |
| 5 | Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved. |
| 6 | http://chuck.stanford.edu/ |
| 7 | http://chuck.cs.princeton.edu/ |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the dual-license terms of EITHER the MIT License OR the GNU |
| 11 | General Public License (the latter as published by the Free Software |
| 12 | Foundation; either version 2 of the License or, at your option, any |
| 13 | later version). |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful and/or |
| 16 | interesting, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 17 | of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | MIT Licence and/or the GNU General Public License for details. |
| 19 | |
| 20 | You should have received a copy of the MIT License and the GNU General |
| 21 | Public License (GPL) along with this program; a copy of the GPL can also |
| 22 | be obtained by writing to the Free Software Foundation, Inc., 59 Temple |
| 23 | Place, Suite 330, Boston, MA 02111-1307 U.S.A. |
| 24 | -----------------------------------------------------------------------------*/ |
| 25 | |
| 26 | //----------------------------------------------------------------------------- |
| 27 | // file: util_hid.cpp |
| 28 | // desc: refactored HID joystick, keyboard, mouse support |
| 29 | // |
| 30 | // author: Spencer Salazar ([email protected]) |
| 31 | // date: summer 2006 |
| 32 | //----------------------------------------------------------------------------- |
| 33 | #include "util_hid.h" |
| 34 | #include "util_platforms.h" |
| 35 | #include "chuck_errmsg.h" |
| 36 | #include "hidio_sdl.h" |
| 37 | |
| 38 | #include <limits.h> |
| 39 | #include <vector> |
| 40 | #include <map> |
| 41 | |
| 42 | using namespace std; |
| 43 | |
| 44 | /* device types */ |
| 45 | const t_CKUINT CK_HID_DEV_NONE = 0; |
| 46 | const t_CKUINT CK_HID_DEV_JOYSTICK = 1; |
| 47 | const t_CKUINT CK_HID_DEV_MOUSE = 2; |
| 48 | const t_CKUINT CK_HID_DEV_KEYBOARD = 3; |
| 49 | const t_CKUINT CK_HID_DEV_WIIREMOTE = 4; |
| 50 | const t_CKUINT CK_HID_DEV_TILTSENSOR = 5; |
| 51 | const t_CKUINT CK_HID_DEV_TABLET = 6; |
| 52 | const t_CKUINT CK_HID_DEV_MULTITOUCH = 7; |
| 53 | const t_CKUINT CK_HID_DEV_COUNT = 8; |
| 54 | |
| 55 | /* message types */ |
| 56 | const t_CKUINT CK_HID_JOYSTICK_AXIS = 0; |
| 57 | const t_CKUINT CK_HID_BUTTON_DOWN = 1; |
| 58 | const t_CKUINT CK_HID_BUTTON_UP = 2; |
| 59 | const t_CKUINT CK_HID_JOYSTICK_HAT = 3; |
| 60 | const t_CKUINT CK_HID_JOYSTICK_BALL = 4; |
| 61 | const t_CKUINT CK_HID_MOUSE_MOTION = 5; |
| 62 | const t_CKUINT CK_HID_MOUSE_WHEEL = 6; |
| 63 | const t_CKUINT CK_HID_DEVICE_CONNECTED = 7; |
| 64 | const t_CKUINT CK_HID_DEVICE_DISCONNECTED = 8; |
| 65 | const t_CKUINT CK_HID_ACCELEROMETER = 9; |
| 66 | const t_CKUINT CK_HID_WIIREMOTE_IR = 10; |
| 67 | const t_CKUINT CK_HID_LED = 11; |
| 68 | const t_CKUINT CK_HID_FORCE_FEEDBACK = 12; |
| 69 | const t_CKUINT CK_HID_SPEAKER = 13; |
| 70 | const t_CKUINT CK_HID_TABLET_PRESSURE = 14; |
| 71 | const t_CKUINT CK_HID_TABLET_MOTION = 15; |
| 72 | const t_CKUINT CK_HID_TABLET_ROTATION = 16; |
| 73 | const t_CKUINT CK_HID_MULTITOUCH_TOUCH = 17; |
| 74 | const t_CKUINT CK_HID_MSG_COUNT = 18; |
| 75 | |
| 76 | #if defined(__PLATFORM_APPLE__) && !defined(__CHIP_MODE__) |
| 77 | #pragma mark OS X General HID support |
| 78 | |
| 79 | /* TODO: *********************************************************************** |
| 80 | |
| 81 | Make Joystick/Mouse/Keyboard_open/_close thread safe (i.e., do all ->configure |
| 82 | and ->startQueue calls on the Hid thread). *** DONE |
| 83 | |
| 84 | Make ->disconnect delete the element list *** worked around |
| 85 | |
| 86 | Make Keyboard, Mouse dis/reattachment work *** DONE |
| 87 | |
| 88 | Hid Probe |
| 89 | |
| 90 | *******************************************************************************/ |
| 91 | |
| 92 | #include <mach/mach.h> |
| 93 | #include <mach/mach_error.h> |
| 94 | #include <IOKit/IOKitLib.h> |
| 95 | #include <IOKit/IOCFPlugIn.h> |
| 96 | #include <Kernel/IOKit/hidsystem/IOHIDUsageTables.h> |
| 97 | #include <IOKit/hid/IOHIDLib.h> |
| 98 | #include <IOKit/hid/IOHIDKeys.h> |
| 99 | #include <IOKit/IOMessage.h> |
| 100 | #include <CoreFoundation/CoreFoundation.h> |
| 101 | #include <Carbon/Carbon.h> |
| 102 | |
| 103 | // check for OS X 10.4/CGEvent |
| 104 | #include <AvailabilityMacros.h> |
| 105 | #undef MAC_OS_X_VERSION_MIN_REQUIRED |
| 106 | #undef MAC_OS_X_VERSION_MAX_ALLOWED |
| 107 | #define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_3 |
| 108 | #define MAC_OS_X_VERSION_MAX_ALLOWED MAC_OS_X_VERSION_10_4 |
| 109 | #include <ApplicationServices/ApplicationServices.h> |
| 110 | #ifdef __CGEVENT_H__ |
| 111 | #define __CK_HID_CURSOR_TRACK__ |
| 112 | #endif |
| 113 | |
| 114 | #ifndef __DAN__ |
| 115 | #undef __CK_HID_CURSOR_TRACK__ |
| 116 | #endif |
| 117 | |
| 118 | #ifdef __CK_HID_WIIREMOTE__ |
| 119 | #include <IOBluetooth/IOBluetoothUserLib.h> |
| 120 | #include "util_buffers.h" |
| 121 | #endif // __CK_HID_WIIREMOTE__ |
| 122 | |
| 123 | // 1.5.0.5 (ge) added |
| 124 | #include "util_platforms.h" |
| 125 | |
| 126 | |
| 127 | class lockable |
| 128 | { |
| 129 | public: |
| 130 | void lock() |
| 131 | { |
| 132 | m_mutex.acquire(); |
| 133 | } |
| 134 | |
| 135 | void unlock() |
| 136 | { |
| 137 | m_mutex.release(); |
| 138 | } |
| 139 | |
| 140 | private: |
| 141 | XMutex m_mutex; |
| 142 | }; |
| 143 | |
| 144 | |
| 145 | template< typename _Tp, typename _Alloc = allocator< _Tp > > |
| 146 | class xvector : public vector< _Tp, _Alloc >, public lockable |
| 147 | { |
| 148 | }; |
| 149 | |
| 150 | template <typename _Key, typename _Tp, typename _Compare = less<_Key>, |
| 151 | typename _Alloc = allocator<pair<const _Key, _Tp> > > |
| 152 | class xmultimap : public multimap< _Key, _Tp, _Compare, _Alloc >, public lockable |
| 153 | { |
| 154 | }; |
| 155 | |
| 156 | class OSX_Device : public lockable |
| 157 | { |
| 158 | public: |
| 159 | OSX_Device() |
| 160 | { |
| 161 | refcount = 0; |
| 162 | } |
| 163 | |
| 164 | virtual ~OSX_Device() {} |
| 165 | |
| 166 | virtual void open() {}; |
| 167 | virtual void close() {}; |
| 168 | |
| 169 | t_CKUINT refcount; // incremented on open, decremented on close |
| 170 | }; |
| 171 | |
| 172 | class OSX_Hid_Device_Element |
| 173 | { |
| 174 | public: |
| 175 | OSX_Hid_Device_Element() |
| 176 | { |
| 177 | cookie = 0; |
| 178 | num = 0; |
| 179 | value = 0; |
| 180 | usage = usage_page = 0; |
| 181 | min = 0; |
| 182 | max = 0; |
| 183 | } |
| 184 | |
| 185 | IOHIDElementCookie cookie; |
| 186 | |
| 187 | t_CKINT type; |
| 188 | t_CKINT num; |
| 189 | t_CKINT value; |
| 190 | |
| 191 | t_CKINT usage_page; |
| 192 | t_CKINT usage; |
| 193 | |
| 194 | t_CKINT min; |
| 195 | t_CKINT max; |
| 196 | }; |
| 197 | |
| 198 | class OSX_Hid_Device : public OSX_Device |
| 199 | { |
| 200 | public: |
| 201 | OSX_Hid_Device() |
| 202 | { |
| 203 | configed = preconfiged = FALSE; |
| 204 | plugInInterface = NULL; |
| 205 | interface = NULL; |
| 206 | queue = NULL; |
| 207 | strncpy( name, "Device", 256 ); |
| 208 | elements = NULL; |
| 209 | outputs = NULL; |
| 210 | usage = usage_page = 0; |
| 211 | type = num = -1; |
| 212 | buttons = -1; |
| 213 | axes = -1; |
| 214 | hats = -1; |
| 215 | wheels = -1; |
| 216 | refcount = 0; |
| 217 | hidProperties = NULL; |
| 218 | removal_notification = 0; // NULL; |
| 219 | vendorID = productID = locationID = NULL; |
| 220 | manufacturer = product = NULL; |
| 221 | } |
| 222 | |
| 223 | ~OSX_Hid_Device() |
| 224 | { |
| 225 | cleanup(); |
| 226 | } |
| 227 | |
| 228 | t_CKINT preconfigure( io_object_t ioHIDDeviceObject ); |
| 229 | t_CKINT configure(); |
| 230 | void enumerate_elements( CFArrayRef cfElements ); |
| 231 | t_CKINT start(); |
| 232 | t_CKINT stop(); |
| 233 | t_CKBOOL is_connected() const; |
| 234 | t_CKBOOL is_same_as( io_object_t ioHIDDeviceObject ) const; |
| 235 | void disconnect(); |
| 236 | void cleanup(); |
| 237 | |
| 238 | IOCFPlugInInterface ** plugInInterface; |
| 239 | IOHIDDeviceInterface ** interface; // device interface |
| 240 | IOHIDQueueInterface ** queue; // device event queue |
| 241 | |
| 242 | char name[256]; |
| 243 | |
| 244 | // uniquely identifying information |
| 245 | /* For ChucK's purposes, HID devices are uniquely identified by the tuple of |
| 246 | (USB device location, vendor ID, product ID, |
| 247 | manufacturer name, product name ) */ |
| 248 | CFNumberRef locationID; |
| 249 | CFNumberRef vendorID; |
| 250 | CFNumberRef productID; |
| 251 | CFStringRef manufacturer; |
| 252 | CFStringRef product; |
| 253 | |
| 254 | t_CKBOOL preconfiged, configed; |
| 255 | |
| 256 | t_CKINT type; |
| 257 | t_CKINT num; |
| 258 | t_CKINT usage_page; |
| 259 | t_CKINT usage; |
| 260 | |
| 261 | map< IOHIDElementCookie, OSX_Hid_Device_Element * > * elements; |
| 262 | |
| 263 | /* Note: setting any of these to -1 informs the element enumerating |
| 264 | method that we are not interested in that particular element type */ |
| 265 | t_CKINT buttons; |
| 266 | t_CKINT axes; |
| 267 | t_CKINT hats; |
| 268 | t_CKINT wheels; |
| 269 | |
| 270 | /* outputs are stored in a map of vectors of OSX_Hid_Device_Elements |
| 271 | Thus if ( *outputs )[CK_HID_OUTPUT_TYPE] != NULL && |
| 272 | ( *outputs )[CK_HID_OUTPUT_TYPE]->size() > n, |
| 273 | ( *outputs )[CK_HID_OUTPUT_TYPE]->at( n ) is the nth output of type |
| 274 | CK_HID_OUTPUT_TYPE. */ |
| 275 | map< unsigned int, vector< OSX_Hid_Device_Element * > * > * outputs; |
| 276 | |
| 277 | CFRunLoopSourceRef eventSource; |
| 278 | |
| 279 | CFMutableDictionaryRef hidProperties; |
| 280 | |
| 281 | io_object_t removal_notification; |
| 282 | |
| 283 | const static t_CKINT event_queue_size = 50; |
| 284 | // queues use wired kernel memory so should be set to as small as possible |
| 285 | // but should account for the maximum possible events in the queue |
| 286 | // USB updates will likely occur at 100 Hz so one must account for this rate |
| 287 | // if states change quickly (updates are only posted on state changes) |
| 288 | }; |
| 289 | |
| 290 | struct OSX_Hid_op |
| 291 | { |
| 292 | enum |
| 293 | { |
| 294 | open, |
| 295 | close |
| 296 | } type; |
| 297 | |
| 298 | xvector< OSX_Hid_Device * > * v; |
| 299 | xvector< OSX_Hid_Device * >::size_type index; |
| 300 | }; |
| 301 | |
| 302 | #pragma mark global variables |
| 303 | |
| 304 | // hid run loop (event dispatcher) |
| 305 | static CFRunLoopRef rlHid = NULL; |
| 306 | // special hid run loop mode (limits events to hid device events only) |
| 307 | //static const CFStringRef kCFRunLoopChuckHidMode = CFSTR( "ChucKHid" ); |
| 308 | static const CFStringRef kCFRunLoopChuckHidMode = kCFRunLoopDefaultMode; |
| 309 | |
| 310 | // general hid callback for device events |
| 311 | static void Hid_callback( void * target, IOReturn result, |
| 312 | void * refcon, void * sender ); |
| 313 | |
| 314 | // CFRunLoop source for open/close device operations |
| 315 | static CFRunLoopSourceRef hidOpSource = NULL; |
| 316 | // cbuffer for open/close device operations |
| 317 | static CBufferSimple * hid_operation_buffer = NULL; |
| 318 | // callback for open/close device operations |
| 319 | static void Hid_do_operation( void * info ); |
| 320 | |
| 321 | // IO iterator for new hid devices |
| 322 | // we only keep track of this so we can release it later |
| 323 | io_iterator_t hid_iterator = 0; // NULL; |
| 324 | |
| 325 | // notification port for device add/remove events |
| 326 | static IONotificationPortRef newDeviceNotificationPort = NULL; |
| 327 | // callback for new devices |
| 328 | static void Hid_new_devices( void * refcon, io_iterator_t iterator ); |
| 329 | // callback for device removal |
| 330 | static void Hid_device_removed( void * refcon, io_service_t service, |
| 331 | natural_t messageType, void * messageArgument ); |
| 332 | |
| 333 | /* the mutexes should be acquired whenever making changes to the corresponding |
| 334 | vectors or accessing them from a non-hid thread after the hid thread has |
| 335 | been started */ |
| 336 | static xvector< OSX_Hid_Device * > * joysticks = NULL; |
| 337 | static xvector< OSX_Hid_Device * > * mice = NULL; |
| 338 | static xvector< OSX_Hid_Device * > * keyboards = NULL; |
| 339 | |
| 340 | static xmultimap< string, OSX_Hid_Device * > * joystick_names = NULL; |
| 341 | static xmultimap< string, OSX_Hid_Device * > * mouse_names = NULL; |
| 342 | static xmultimap< string, OSX_Hid_Device * > * keyboard_names = NULL; |
| 343 | |
| 344 | // has hid been initialized? |
| 345 | static t_CKBOOL g_hid_init = FALSE; |
| 346 | // table to translate keys to ASCII |
| 347 | static t_CKBYTE g_hid_key_table[256]; |
| 348 | |
| 349 | // cursor track stuff |
| 350 | #ifdef __CK_HID_CURSOR_TRACK__ |
| 351 | static t_CKINT cursorX = 0; |
| 352 | static t_CKINT cursorY = 0; |
| 353 | static t_CKFLOAT scaledCursorX = 0; |
| 354 | static t_CKFLOAT scaledCursorY = 0; |
| 355 | static CFRunLoopRef rlCursorTrack = NULL; |
| 356 | static t_CKBOOL g_ct_go = FALSE; |
| 357 | #endif // __CK_HID_CURSOR_TRACK__ |
| 358 | |
| 359 | #ifdef __CK_HID_WIIREMOTE__ |
| 360 | // wii remote stuff |
| 361 | static CFRunLoopSourceRef cfrlWiiRemoteSource = NULL; |
| 362 | static void WiiRemote_cfrl_callback( void * info ); |
| 363 | #endif |
| 364 | |
| 365 | t_CKINT OSX_Hid_Device::preconfigure( io_object_t ioHIDDeviceObject ) |
| 366 | { |
| 367 | // can only be called in Hid thread, or if the hid thread hasnt started |
| 368 | assert( rlHid == CFRunLoopGetCurrent() || rlHid == NULL ); |
| 369 | |
| 370 | if( preconfiged ) |
| 371 | return 0; |
| 372 | |
| 373 | switch( type ) |
| 374 | { |
| 375 | case CK_HID_DEV_NONE: |
| 376 | strncpy( name, "Human Interface Device", 256 ); |
| 377 | buttons = 0; |
| 378 | axes = 0; |
| 379 | wheels = 0; |
| 380 | hats = 0; |
| 381 | break; |
| 382 | |
| 383 | case CK_HID_DEV_JOYSTICK: |
| 384 | strncpy( name, "Joystick", 256 ); |
| 385 | buttons = 0; |
| 386 | axes = 0; |
| 387 | wheels = 0; |
| 388 | hats = 0; |
| 389 | break; |
| 390 | |
| 391 | case CK_HID_DEV_MOUSE: |
| 392 | strncpy( name, "Mouse", 256 ); |
| 393 | buttons = 0; |
| 394 | axes = 0; |
| 395 | wheels = 0; |
| 396 | hats = -1; |
| 397 | break; |
| 398 | |
| 399 | case CK_HID_DEV_KEYBOARD: |
| 400 | strncpy( name, "Keyboard", 256 ); |
| 401 | buttons = 0; |
| 402 | axes = -1; |
| 403 | wheels = -1; |
| 404 | hats = -1; |
| 405 | break; |
| 406 | } |
| 407 | |
| 408 | // retrieve a dictionary of device properties |
| 409 | kern_return_t kern_result = IORegistryEntryCreateCFProperties( ioHIDDeviceObject, |
| 410 | &hidProperties, |
| 411 | kCFAllocatorDefault, |
| 412 | kNilOptions); |
| 413 | |
| 414 | if( kern_result != KERN_SUCCESS || hidProperties == NULL ) |
| 415 | { |
| 416 | hidProperties = NULL; |
| 417 | return -1; |
| 418 | } |
| 419 | |
| 420 | // get the device name, and copy it into the device record |
| 421 | CFTypeRef refCF = CFDictionaryGetValue( hidProperties, |
| 422 | CFSTR( kIOHIDProductKey ) ); |
| 423 | if( refCF ) |
| 424 | CFStringGetCString( ( CFStringRef )refCF, name, 256, |
| 425 | kCFStringEncodingASCII ); |
| 426 | |
| 427 | // retrieve uniquely identifying information |
| 428 | if( !locationID ) |
| 429 | { |
| 430 | locationID = ( CFNumberRef ) CFDictionaryGetValue( hidProperties, |
| 431 | CFSTR( kIOHIDLocationIDKey ) ); |
| 432 | if( locationID ) |
| 433 | CFRetain( locationID ); |
| 434 | } |
| 435 | |
| 436 | if( !vendorID ) |
| 437 | { |
| 438 | vendorID = ( CFNumberRef ) CFDictionaryGetValue( hidProperties, |
| 439 | CFSTR( kIOHIDVendorIDKey ) ); |
| 440 | if( vendorID ) |
| 441 | CFRetain( vendorID ); |
| 442 | } |
| 443 | |
| 444 | if( !productID ) |
| 445 | { |
| 446 | productID = ( CFNumberRef ) CFDictionaryGetValue( hidProperties, |
| 447 | CFSTR( kIOHIDProductIDKey ) ); |
| 448 | if( productID ) |
| 449 | CFRetain( productID ); |
| 450 | } |
| 451 | |
| 452 | if( !manufacturer ) |
| 453 | { |
| 454 | manufacturer = ( CFStringRef ) CFDictionaryGetValue( hidProperties, |
| 455 | CFSTR( kIOHIDManufacturerKey ) ); |
| 456 | if( manufacturer ) |
| 457 | CFRetain( manufacturer ); |
| 458 | } |
| 459 | |
| 460 | if( !product ) |
| 461 | { |
| 462 | product = ( CFStringRef ) CFDictionaryGetValue( hidProperties, |
| 463 | CFSTR( kIOHIDProductKey ) ); |
| 464 | if( product ) |
| 465 | CFRetain( product ); |
| 466 | } |
| 467 | |
| 468 | // create plugin interface |
| 469 | IOReturn result; |
| 470 | SInt32 score = 0; |
| 471 | result = IOCreatePlugInInterfaceForService( ioHIDDeviceObject, |
| 472 | kIOHIDDeviceUserClientTypeID, |
| 473 | kIOCFPlugInInterfaceID, |
| 474 | &plugInInterface, |
| 475 | &score); |
| 476 | |
| 477 | if( result != kIOReturnSuccess ) |
| 478 | { |
| 479 | CFRelease( hidProperties ); |
| 480 | hidProperties = NULL; |
| 481 | return -1; |
| 482 | } |
| 483 | |
| 484 | #ifdef __LITTLE_ENDIAN__ |
| 485 | /* MacBooks and MacBook Pros have some sort of "phantom" trackpad, which |
| 486 | shows up in the HID registry and claims to have no wheels. The device |
| 487 | name is usually Trackpad with the manufacturer being Apple. This may |
| 488 | disable legitimate Trackpads, but hopefully not... */ |
| 489 | if( type == CK_HID_DEV_MOUSE && strncmp( "Trackpad", name, 256 ) == 0 ) |
| 490 | { |
| 491 | refCF = CFDictionaryGetValue( hidProperties, |
| 492 | CFSTR( kIOHIDManufacturerKey ) ); |
| 493 | if( refCF != NULL && |
| 494 | CFStringCompare( ( CFStringRef ) refCF, CFSTR( "Apple" ), 0 ) == 0 ) |
| 495 | { |
| 496 | preconfiged = TRUE; |
| 497 | configure(); |
| 498 | |
| 499 | if( wheels == 0 ) |
| 500 | // this is the "phantom" trackpad |
| 501 | { |
| 502 | // iterate through the element map, delete device element records |
| 503 | map< IOHIDElementCookie, OSX_Hid_Device_Element * >::iterator iter, end; |
| 504 | end = elements->end(); |
| 505 | for( iter = elements->begin(); iter != end; iter++ ) |
| 506 | delete iter->second; |
| 507 | delete elements; |
| 508 | elements = NULL; |
| 509 | |
| 510 | if( queue ) |
| 511 | { |
| 512 | ( *queue )->dispose( queue ); |
| 513 | ( *queue )->Release( queue ); |
| 514 | queue = NULL; |
| 515 | } |
| 516 | |
| 517 | if( interface ) |
| 518 | { |
| 519 | ( *interface )->close( interface ); |
| 520 | ( *interface )->Release( interface ); |
| 521 | interface = NULL; |
| 522 | } |
| 523 | |
| 524 | return -1; |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | #endif // __LITTLE_ENDIAN__ |
| 529 | |
| 530 | // register callback for removal notification, among a few other things |
| 531 | result = IOServiceAddInterestNotification( newDeviceNotificationPort, |
| 532 | ioHIDDeviceObject, |
| 533 | kIOGeneralInterest, |
| 534 | Hid_device_removed, |
| 535 | this, |
| 536 | &removal_notification ); |
| 537 | |
| 538 | preconfiged = TRUE; |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | t_CKINT OSX_Hid_Device::configure() |
| 544 | { |
| 545 | // can only be called in Hid thread, or if the hid thread hasnt started |
| 546 | assert( rlHid == NULL || rlHid == CFRunLoopGetCurrent() ); |
| 547 | |
| 548 | if( configed ) |
| 549 | return 0; |
| 550 | |
| 551 | if( !preconfiged ) |
| 552 | return -1; |
| 553 | |
| 554 | EM_log( CK_LOG_INFO, "hid: configuring %s", name ); |
| 555 | |
| 556 | // create the device interface |
| 557 | HRESULT plugInResult = S_OK; |
| 558 | plugInResult = (*plugInInterface)->QueryInterface( plugInInterface, |
| 559 | CFUUIDGetUUIDBytes( kIOHIDDeviceInterfaceID ), |
| 560 | ( LPVOID * ) &interface ); |
| 561 | (*plugInInterface)->Release( plugInInterface ); |
| 562 | plugInInterface = NULL; |
| 563 | if( plugInResult != S_OK ) |
| 564 | { |
| 565 | CFRelease( hidProperties ); |
| 566 | hidProperties = NULL; |
| 567 | return -1; |
| 568 | } |
| 569 | |
| 570 | // open the interface |
| 571 | IOReturn result; |
| 572 | result = (*interface)->open( interface, 0 ); |
| 573 | if( result != kIOReturnSuccess ) |
| 574 | { |
| 575 | CFRelease( hidProperties ); |
| 576 | hidProperties = NULL; |
| 577 | (*interface)->Release( interface ); |
| 578 | interface = NULL; |
| 579 | return -1; |
| 580 | } |
| 581 | |
| 582 | // create an event queue, so we dont have to poll individual elements |
| 583 | queue = (*interface)->allocQueue( interface ); |
| 584 | if( !queue ) |
| 585 | { |
| 586 | CFRelease( hidProperties ); |
| 587 | hidProperties = NULL; |
| 588 | (*interface)->close( interface ); |
| 589 | (*interface)->Release( interface ); |
| 590 | interface = NULL; |
| 591 | return -1; |
| 592 | } |
| 593 | |
| 594 | result = (*queue)->create( queue, 0, event_queue_size ); |
| 595 | if( result != kIOReturnSuccess ) |
| 596 | { |
| 597 | CFRelease( hidProperties ); |
| 598 | hidProperties = NULL; |
| 599 | (*queue)->Release( queue ); |
| 600 | queue = NULL; |
| 601 | (*interface)->close( interface ); |
| 602 | (*interface)->Release( interface ); |
| 603 | interface = NULL; |
| 604 | return -1; |
| 605 | } |
| 606 | |
| 607 | // set up the call back mechanism |
| 608 | result = (*queue)->createAsyncEventSource( queue, &eventSource ); |
| 609 | if( result != kIOReturnSuccess ) |
| 610 | { |
| 611 | CFRelease( hidProperties ); |
| 612 | hidProperties = NULL; |
| 613 | (*queue)->dispose( queue ); |
| 614 | (*queue)->Release( queue ); |
| 615 | queue = NULL; |
| 616 | (*interface)->close( interface ); |
| 617 | (*interface)->Release( interface ); |
| 618 | interface = NULL; |
| 619 | return -1; |
| 620 | } |
| 621 | |
| 622 | result = (*queue)->setEventCallout( queue, Hid_callback, NULL, this ); |
| 623 | if( result != kIOReturnSuccess ) |
| 624 | { |
| 625 | CFRelease( hidProperties ); |
| 626 | hidProperties = NULL; |
| 627 | (*queue)->dispose( queue ); |
| 628 | (*queue)->Release( queue ); |
| 629 | queue = NULL; |
| 630 | (*interface)->close( interface ); |
| 631 | (*interface)->Release( interface ); |
| 632 | interface = NULL; |
| 633 | return -1; |
| 634 | } |
| 635 | |
| 636 | if( hidProperties != NULL ) |
| 637 | { |
| 638 | if( elements == NULL ) |
| 639 | { |
| 640 | // retrieve the array of elements... |
| 641 | CFTypeRef refCF = CFDictionaryGetValue( hidProperties, |
| 642 | CFSTR( kIOHIDElementKey ) ); |
| 643 | if( !refCF ) |
| 644 | { |
| 645 | CFRelease( hidProperties ); |
| 646 | hidProperties = NULL; |
| 647 | (*queue)->dispose( queue ); |
| 648 | (*queue)->Release( queue ); |
| 649 | queue = NULL; |
| 650 | (*interface)->close( interface ); |
| 651 | (*interface)->Release( interface ); |
| 652 | interface = NULL; |
| 653 | return -1; |
| 654 | } |
| 655 | |
| 656 | // ...allocate space for element records... |
| 657 | elements = new map< IOHIDElementCookie, OSX_Hid_Device_Element * >; |
| 658 | // axes = 0; |
| 659 | // buttons = 0; |
| 660 | // hats = 0; |
| 661 | |
| 662 | // ...and parse the element array recursively |
| 663 | enumerate_elements( ( CFArrayRef ) refCF ); |
| 664 | } |
| 665 | |
| 666 | else |
| 667 | { |
| 668 | map< IOHIDElementCookie, OSX_Hid_Device_Element * >::iterator iter, end; |
| 669 | end = elements->end(); |
| 670 | for( iter = elements->begin(); iter != end; iter++ ) |
| 671 | ( *queue )->addElement( queue, iter->second->cookie, 0 ); |
| 672 | } |
| 673 | |
| 674 | CFRelease( hidProperties ); |
| 675 | hidProperties = NULL; |
| 676 | } |
| 677 | |
| 678 | configed = TRUE; |
| 679 | |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | //------------------------------------------------------------------------------ |
| 684 | // name: enumerate_device_elements |
| 685 | // desc: perform depth depth first search on potentially nested arrays of |
| 686 | // elements on the device, add found elements to the vector |
| 687 | //------------------------------------------------------------------------------ |
| 688 | void OSX_Hid_Device::enumerate_elements( CFArrayRef cfElements ) |
| 689 | { |
| 690 | // can only be called in Hid thread, or if the hid thread hasnt started |
| 691 | assert( rlHid == NULL || rlHid == CFRunLoopGetCurrent() ); |
| 692 | |
| 693 | CFTypeRef refCF = 0; |
| 694 | CFDictionaryRef element_dictionary; |
| 695 | OSX_Hid_Device_Element * element; |
| 696 | t_CKINT usage_page, usage, element_type; |
| 697 | IOReturn result; |
| 698 | |
| 699 | CFIndex i, len = CFArrayGetCount( cfElements ); |
| 700 | for( i = 0; i < len; i++ ) |
| 701 | { |
| 702 | // retrieve the dictionary of the ith element's data |
| 703 | refCF = CFArrayGetValueAtIndex( cfElements, i ); |
| 704 | element_dictionary = ( CFDictionaryRef ) refCF; |
| 705 | |
| 706 | // get element type |
| 707 | refCF = CFDictionaryGetValue( element_dictionary, |
| 708 | CFSTR( kIOHIDElementTypeKey ) ); |
| 709 | if( !refCF || |
| 710 | !CFNumberGetValue( ( CFNumberRef ) refCF, kCFNumberLongType, |
| 711 | &element_type ) ) |
| 712 | continue; |
| 713 | |
| 714 | // get element usage page |
| 715 | refCF = CFDictionaryGetValue( element_dictionary, |
| 716 | CFSTR( kIOHIDElementUsagePageKey ) ); |
| 717 | if( !refCF || |
| 718 | !CFNumberGetValue( ( CFNumberRef ) refCF, kCFNumberLongType, |
| 719 | &usage_page ) ) |
| 720 | continue; |
| 721 | |
| 722 | // get element usage |
| 723 | refCF = CFDictionaryGetValue( element_dictionary, |
| 724 | CFSTR( kIOHIDElementUsageKey ) ); |
| 725 | if( !refCF || |
| 726 | !CFNumberGetValue( ( CFNumberRef ) refCF, kCFNumberLongType, |
| 727 | &usage ) ) |
| 728 | continue; |
| 729 | |
| 730 | switch( element_type ) |
| 731 | { |
| 732 | case kIOHIDElementTypeInput_Misc: |
| 733 | case kIOHIDElementTypeInput_Button: |
| 734 | case kIOHIDElementTypeInput_Axis: |
| 735 | case kIOHIDElementTypeInput_ScanCodes: |
| 736 | // an input of some sort |
| 737 | switch( usage_page ) |
| 738 | { |
| 739 | case kHIDPage_GenericDesktop: |
| 740 | switch( usage ) |
| 741 | { |
| 742 | case kHIDUsage_GD_X: |
| 743 | case kHIDUsage_GD_Y: |
| 744 | case kHIDUsage_GD_Z: |
| 745 | case kHIDUsage_GD_Rx: |
| 746 | case kHIDUsage_GD_Ry: |
| 747 | case kHIDUsage_GD_Rz: |
| 748 | case kHIDUsage_GD_Slider: |
| 749 | case kHIDUsage_GD_Dial: |
| 750 | // this is an axis |
| 751 | if( axes == -1 ) |
| 752 | continue; |
| 753 | |
| 754 | element = new OSX_Hid_Device_Element; |
| 755 | |
| 756 | element->num = axes; |
| 757 | if( this->type == CK_HID_DEV_JOYSTICK ) |
| 758 | element->type = CK_HID_JOYSTICK_AXIS; |
| 759 | else |
| 760 | element->type = CK_HID_MOUSE_MOTION; |
| 761 | element->usage = usage; |
| 762 | element->usage_page = usage_page; |
| 763 | |
| 764 | refCF = CFDictionaryGetValue( element_dictionary, |
| 765 | CFSTR( kIOHIDElementCookieKey ) ); |
| 766 | if( !refCF || |
| 767 | !CFNumberGetValue( ( CFNumberRef ) refCF, kCFNumberLongType, &element->cookie ) ) |
| 768 | { |
| 769 | delete element; |
| 770 | continue; |
| 771 | } |
| 772 | |
| 773 | refCF = CFDictionaryGetValue( element_dictionary, |
| 774 | CFSTR( kIOHIDElementMinKey ) ); |
| 775 | if( !refCF || |
| 776 | !CFNumberGetValue( ( CFNumberRef ) refCF, kCFNumberLongType, &element->min ) ) |
| 777 | { |
| 778 | delete element; |
| 779 | continue; |
| 780 | } |
| 781 | |
| 782 | refCF = CFDictionaryGetValue( element_dictionary, |
| 783 | CFSTR( kIOHIDElementMaxKey ) ); |
| 784 | if( !refCF || |
| 785 | !CFNumberGetValue( ( CFNumberRef ) refCF, kCFNumberLongType, &element->max ) ) |
| 786 | { |
| 787 | delete element; |
| 788 | continue; |
| 789 | } |
| 790 | |
| 791 | result = (*queue)->addElement( queue, element->cookie, 0 ); |
| 792 | if( result != kIOReturnSuccess ) |
| 793 | { |
| 794 | delete element; |
| 795 | continue; |
| 796 | } |
| 797 | |
| 798 | EM_log( CK_LOG_FINE, "adding axis %d", axes ); |
| 799 | |
| 800 | (*elements)[element->cookie] = element; |
| 801 | axes++; |
| 802 | |
| 803 | break; |
| 804 | |
| 805 | case kHIDUsage_GD_Wheel: |
| 806 | // this is an wheel |
| 807 | if( wheels == -1 ) |
| 808 | continue; |
| 809 | |
| 810 | element = new OSX_Hid_Device_Element; |
| 811 | |
| 812 | element->num = wheels; |
| 813 | if( this->type == CK_HID_DEV_JOYSTICK ) |
| 814 | element->type = CK_HID_JOYSTICK_AXIS; |
| 815 | else |
| 816 | element->type = CK_HID_MOUSE_WHEEL; |
| 817 | element->usage = usage; |
| 818 | element->usage_page = usage_page; |
| 819 | |
| 820 | refCF = CFDictionaryGetValue( element_dictionary, |
| 821 | CFSTR( kIOHIDElementCookieKey ) ); |
| 822 | if( !refCF || |
| 823 | !CFNumberGetValue( ( CFNumberRef ) refCF, |
| 824 | kCFNumberLongType, |
| 825 | &element->cookie ) ) |
| 826 | { |
| 827 | delete element; |
| 828 | continue; |
| 829 | } |
| 830 | |
| 831 | result = (*queue)->addElement( queue, |
| 832 | element->cookie, |
| 833 | 0 ); |
| 834 | if( result != kIOReturnSuccess ) |
| 835 | { |
| 836 | delete element; |
| 837 | continue; |
| 838 | } |
| 839 | |
| 840 | EM_log( CK_LOG_FINE, "adding wheel %d", wheels ); |
| 841 | |
| 842 | (*elements)[element->cookie] = element; |
| 843 | wheels++; |
| 844 | break; |
| 845 | |
| 846 | case kHIDUsage_GD_Hatswitch: |
| 847 | // this is a hat |
| 848 | if( hats == -1 ) |
| 849 | continue; |
| 850 | |
| 851 | element = new OSX_Hid_Device_Element; |
| 852 | |
| 853 | element->type = CK_HID_JOYSTICK_HAT; |
| 854 | element->num = hats; |
| 855 | element->usage = usage; |
| 856 | element->usage_page = usage_page; |
| 857 | |
| 858 | refCF = CFDictionaryGetValue( element_dictionary, |
| 859 | CFSTR( kIOHIDElementCookieKey ) ); |
| 860 | if( !refCF || |
| 861 | !CFNumberGetValue( ( CFNumberRef ) refCF, kCFNumberLongType, &element->cookie ) ) |
| 862 | { |
| 863 | delete element; |
| 864 | continue; |
| 865 | } |
| 866 | |
| 867 | result = (*queue)->addElement( queue, element->cookie, 0 ); |
| 868 | if( result != kIOReturnSuccess ) |
| 869 | { |
| 870 | delete element; |
| 871 | continue; |
| 872 | } |
| 873 | |
| 874 | EM_log( CK_LOG_FINE, "adding hat %d", hats ); |
| 875 | |
| 876 | (*elements)[element->cookie] = element; |
| 877 | hats++; |
| 878 | |
| 879 | break; |
| 880 | |
| 881 | default: |
| 882 | EM_log( CK_LOG_INFO, "unknown page: %i usage: %i", usage_page, usage ); |
| 883 | } |
| 884 | |
| 885 | break; |
| 886 | |
| 887 | case kHIDPage_Button: |
| 888 | case kHIDPage_KeyboardOrKeypad: |
| 889 | // this is a button |
| 890 | if( buttons == -1 ) |
| 891 | continue; |
| 892 | |
| 893 | /* filter out error and reserved usages*/ |
| 894 | if( usage_page == kHIDPage_KeyboardOrKeypad && |
| 895 | !( usage >= kHIDUsage_KeyboardA && |
| 896 | usage <= kHIDUsage_KeyboardRightGUI ) ) |
| 897 | continue; |
| 898 | |
| 899 | element = new OSX_Hid_Device_Element; |
| 900 | |
| 901 | element->type = CK_HID_BUTTON_DOWN; |
| 902 | element->num = buttons; |
| 903 | element->usage = usage; |
| 904 | element->usage_page = usage_page; |
| 905 | |
| 906 | refCF = CFDictionaryGetValue( element_dictionary, |
| 907 | CFSTR( kIOHIDElementCookieKey ) ); |
| 908 | if( !refCF || |
| 909 | !CFNumberGetValue( ( CFNumberRef ) refCF, kCFNumberLongType, &element->cookie ) ) |
| 910 | { |
| 911 | delete element; |
| 912 | continue; |
| 913 | } |
| 914 | |
| 915 | result = (*queue)->addElement( queue, element->cookie, 0 ); |
| 916 | if( result != kIOReturnSuccess ) |
| 917 | { |
| 918 | delete element; |
| 919 | continue; |
| 920 | } |
| 921 | |
| 922 | EM_log( CK_LOG_FINE, "adding button %d", buttons ); |
| 923 | |
| 924 | (*elements)[element->cookie] = element; |
| 925 | buttons++; |
| 926 | |
| 927 | break; |
| 928 | |
| 929 | default: |
| 930 | EM_log( CK_LOG_FINER, "unknown page: %i usage: %i", usage_page, usage ); |
| 931 | } |
| 932 | |
| 933 | break; |
| 934 | |
| 935 | case kIOHIDElementTypeCollection: |
| 936 | refCF = CFDictionaryGetValue( element_dictionary, CFSTR( kIOHIDElementKey ) ); |
| 937 | if( !refCF ) |
| 938 | continue; |
| 939 | |
| 940 | enumerate_elements( ( CFArrayRef ) refCF ); |
| 941 | break; |
| 942 | |
| 943 | case kIOHIDElementTypeOutput: |
| 944 | switch( usage_page ) |
| 945 | { |
| 946 | case kHIDPage_LEDs: |
| 947 | /* filter out error and reserved usages */ |
| 948 | if( usage > kHIDUsage_LED_ExternalPowerConnected ) |
| 949 | continue; |
| 950 | |
| 951 | element = new OSX_Hid_Device_Element; |
| 952 | |
| 953 | refCF = CFDictionaryGetValue( element_dictionary, |
| 954 | CFSTR( kIOHIDElementCookieKey ) ); |
| 955 | if( !refCF || |
| 956 | !CFNumberGetValue( ( CFNumberRef ) refCF, kCFNumberLongType, &element->cookie ) ) |
| 957 | { |
| 958 | delete element; |
| 959 | continue; |
| 960 | } |
| 961 | |
| 962 | refCF = CFDictionaryGetValue( element_dictionary, |
| 963 | CFSTR( kIOHIDElementMinKey ) ); |
| 964 | if( !refCF || |
| 965 | !CFNumberGetValue( ( CFNumberRef ) refCF, kCFNumberLongType, &element->min ) ) |
| 966 | { |
| 967 | delete element; |
| 968 | continue; |
| 969 | } |
| 970 | |
| 971 | refCF = CFDictionaryGetValue( element_dictionary, |
| 972 | CFSTR( kIOHIDElementMaxKey ) ); |
| 973 | if( !refCF || |
| 974 | !CFNumberGetValue( ( CFNumberRef ) refCF, kCFNumberLongType, &element->max ) ) |
| 975 | { |
| 976 | delete element; |
| 977 | continue; |
| 978 | } |
| 979 | |
| 980 | if( outputs == NULL ) |
| 981 | outputs = new map< unsigned int, vector< OSX_Hid_Device_Element * > * >; |
| 982 | |
| 983 | if( outputs->find( CK_HID_LED ) == outputs->end() ) |
| 984 | ( *outputs )[CK_HID_LED] = new vector< OSX_Hid_Device_Element * >; |
| 985 | |
| 986 | element->type = CK_HID_LED; |
| 987 | element->num = ( *outputs )[CK_HID_LED]->size(); |
| 988 | element->usage = usage; |
| 989 | element->usage_page = usage_page; |
| 990 | |
| 991 | EM_log( CK_LOG_FINE, "adding led %d", element->num ); |
| 992 | |
| 993 | ( *outputs )[CK_HID_LED]->push_back( element ); |
| 994 | |
| 995 | break; |
| 996 | } |
| 997 | |
| 998 | break; |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | t_CKINT OSX_Hid_Device::start() |
| 1004 | { |
| 1005 | // can only be called in Hid thread |
| 1006 | assert( rlHid == CFRunLoopGetCurrent() ); |
| 1007 | |
| 1008 | EM_log( CK_LOG_INFO, "hid: starting %s", name ); |
| 1009 | EM_pushlog(); |
| 1010 | |
| 1011 | // configure if needed |
| 1012 | if( !configed ) |
| 1013 | { |
| 1014 | // lock the device so we can mutate it |
| 1015 | this->lock(); |
| 1016 | |
| 1017 | // configure |
| 1018 | if( configure() ) |
| 1019 | { |
| 1020 | // error during configuration! |
| 1021 | this->unlock(); |
| 1022 | |
| 1023 | EM_log( CK_LOG_HERALD, "hid: error configuring %s", name ); |
| 1024 | EM_poplog(); |
| 1025 | |
| 1026 | return -1; |
| 1027 | } |
| 1028 | |
| 1029 | this->unlock(); |
| 1030 | } |
| 1031 | |
| 1032 | EM_log( CK_LOG_INFO, "hid: adding %s to run loop", name ); |
| 1033 | CFRunLoopAddSource( rlHid, eventSource, kCFRunLoopChuckHidMode ); |
| 1034 | |
| 1035 | IOReturn result = ( *queue )->start( queue ); |
| 1036 | if( result != kIOReturnSuccess ) |
| 1037 | { |
| 1038 | EM_log( CK_LOG_HERALD, "hid: error starting event queue for %s", name ); |
| 1039 | EM_poplog(); |
| 1040 | return -1; |
| 1041 | } |
| 1042 | |
| 1043 | EM_poplog(); |
| 1044 | |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
| 1048 | t_CKINT OSX_Hid_Device::stop() |
| 1049 | { |
| 1050 | // can only be called in Hid thread |
| 1051 | assert( rlHid == CFRunLoopGetCurrent() ); |
| 1052 | |
| 1053 | if( queue ) |
| 1054 | { |
| 1055 | IOReturn result = ( *queue )->stop( queue ); |
| 1056 | if( result != kIOReturnSuccess ) |
| 1057 | EM_log( CK_LOG_INFO, "hid: error stopping event queue for %s", name ); |
| 1058 | } |
| 1059 | |
| 1060 | CFRunLoopRemoveSource( rlHid, eventSource, kCFRunLoopChuckHidMode ); |
| 1061 | |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | t_CKBOOL OSX_Hid_Device::is_connected() const |
| 1066 | { |
| 1067 | if( plugInInterface == NULL && interface == NULL ) |
| 1068 | return FALSE; |
| 1069 | return TRUE; |
| 1070 | } |
| 1071 | |
| 1072 | t_CKBOOL OSX_Hid_Device::is_same_as( io_object_t ioHIDDeviceObject ) const |
| 1073 | { |
| 1074 | CFMutableDictionaryRef hidProperties2 = NULL; |
| 1075 | t_CKBOOL result = FALSE; |
| 1076 | |
| 1077 | // retrieve a dictionary of device properties |
| 1078 | kern_return_t kern_result = IORegistryEntryCreateCFProperties( ioHIDDeviceObject, |
| 1079 | &hidProperties2, |
| 1080 | kCFAllocatorDefault, |
| 1081 | kNilOptions ); |
| 1082 | |
| 1083 | if( kern_result != KERN_SUCCESS || hidProperties2 == NULL ) |
| 1084 | { |
| 1085 | return FALSE; |
| 1086 | } |
| 1087 | |
| 1088 | // gewang: moved these decl's above goto's |
| 1089 | CFNumberRef locationID2 = 0; |
| 1090 | CFNumberRef vendorID2 = 0; |
| 1091 | CFNumberRef productID2 = 0; |
| 1092 | CFStringRef manufacturer2 = 0; |
| 1093 | CFStringRef product2 = 0; |
| 1094 | |
| 1095 | // if no "uniquely identifying" info is available, then assume its new |
| 1096 | if( !locationID && !vendorID && !productID && !manufacturer && !product ) |
| 1097 | goto end; |
| 1098 | |
| 1099 | locationID2 = ( CFNumberRef ) CFDictionaryGetValue( hidProperties2, |
| 1100 | CFSTR( kIOHIDLocationIDKey ) ); |
| 1101 | if( ( locationID && !locationID2 ) || ( !locationID && locationID2 ) ) |
| 1102 | goto end; // not the same |
| 1103 | if( locationID && locationID2 && CFNumberCompare( locationID, locationID2, NULL ) ) |
| 1104 | goto end; // not the same |
| 1105 | |
| 1106 | vendorID2 = ( CFNumberRef ) CFDictionaryGetValue( hidProperties2, |
| 1107 | CFSTR( kIOHIDVendorIDKey ) ); |
| 1108 | if( ( vendorID && !vendorID2 ) || ( !vendorID && vendorID2 ) ) |
| 1109 | goto end; // not the same |
| 1110 | if( vendorID && vendorID2 && CFNumberCompare( vendorID, vendorID2, NULL ) ) |
| 1111 | goto end; // not the same |
| 1112 | |
| 1113 | productID2 = ( CFNumberRef ) CFDictionaryGetValue( hidProperties2, |
| 1114 | CFSTR( kIOHIDProductIDKey ) ); |
| 1115 | if( ( productID && !productID2 ) || ( !productID && productID2 ) ) |
| 1116 | goto end; // not the same |
| 1117 | if( productID && productID2 && CFNumberCompare( productID, productID2, NULL ) ) |
| 1118 | goto end; // not the same |
| 1119 | |
| 1120 | manufacturer2 = ( CFStringRef ) CFDictionaryGetValue( hidProperties2, |
| 1121 | CFSTR( kIOHIDManufacturerKey ) ); |
| 1122 | if( ( manufacturer && !manufacturer2 ) || ( !manufacturer && manufacturer2 ) ) |
| 1123 | goto end; // not the same |
| 1124 | if( manufacturer && manufacturer2 && CFStringCompare( manufacturer, manufacturer2, 0 ) ) |
| 1125 | goto end; // not the same |
| 1126 | |
| 1127 | product2 = ( CFStringRef ) CFDictionaryGetValue( hidProperties2, |
| 1128 | CFSTR( kIOHIDProductKey ) ); |
| 1129 | if( ( product && !product2 ) || ( !product && product2 ) ) |
| 1130 | goto end; // not the same |
| 1131 | if( product && product2 && CFStringCompare( product, product2, 0 ) ) |
| 1132 | goto end; // not the same |
| 1133 | |
| 1134 | // if execution reaches this point, then they are the same |
| 1135 | result = TRUE; |
| 1136 | |
| 1137 | end: |
| 1138 | CFRelease( hidProperties2 ); |
| 1139 | return result; |
| 1140 | } |
| 1141 | |
| 1142 | //------------------------------------------------------------------------------ |
| 1143 | // name: disconnect() |
| 1144 | // desc: called when device is physically disconnected from the machine. |
| 1145 | // deallocates per-connection resources, but leaves stuff that should be the |
| 1146 | // same across connections (element list, name, other general properties, etc) |
| 1147 | //------------------------------------------------------------------------------ |
| 1148 | void OSX_Hid_Device::disconnect() |
| 1149 | { |
| 1150 | // can only be called in Hid thread, or if the hid thread hasnt started |
| 1151 | assert( rlHid == NULL || rlHid == CFRunLoopGetCurrent() ); |
| 1152 | |
| 1153 | if( plugInInterface ) |
| 1154 | { |
| 1155 | ( *plugInInterface )->Release( plugInInterface ); |
| 1156 | plugInInterface = NULL; |
| 1157 | } |
| 1158 | |
| 1159 | if( queue ) |
| 1160 | { |
| 1161 | if( refcount ) |
| 1162 | { |
| 1163 | ( *queue )->stop( queue ); |
| 1164 | |
| 1165 | if( rlHid ) |
| 1166 | CFRunLoopRemoveSource( rlHid, eventSource, |
| 1167 | kCFRunLoopChuckHidMode ); |
| 1168 | } |
| 1169 | |
| 1170 | ( *queue )->dispose( queue ); |
| 1171 | ( *queue )->Release( queue ); |
| 1172 | queue = NULL; |
| 1173 | } |
| 1174 | |
| 1175 | if( interface ) |
| 1176 | { |
| 1177 | ( *interface )->close( interface ); |
| 1178 | ( *interface )->Release( interface ); |
| 1179 | interface = NULL; |
| 1180 | } |
| 1181 | |
| 1182 | if( removal_notification ) |
| 1183 | { |
| 1184 | IOObjectRelease( removal_notification ); |
| 1185 | removal_notification = 0; // NULL; |
| 1186 | } |
| 1187 | |
| 1188 | configed = FALSE; |
| 1189 | preconfiged = FALSE; |
| 1190 | } |
| 1191 | |
| 1192 | //------------------------------------------------------------------------------ |
| 1193 | // name: cleanup() |
| 1194 | // desc: deallocates all resources associated with the device |
| 1195 | //------------------------------------------------------------------------------ |
| 1196 | void OSX_Hid_Device::cleanup() |
| 1197 | { |
| 1198 | // can only be called in Hid thread, or if the hid thread hasnt started |
| 1199 | assert( rlHid == NULL || rlHid == CFRunLoopGetCurrent() ); |
| 1200 | |
| 1201 | disconnect(); |
| 1202 | |
| 1203 | // delete all elements |
| 1204 | if( elements ) |
| 1205 | { |
| 1206 | this->lock(); |
| 1207 | |
| 1208 | // iterate through the axis map, delete device element records |
| 1209 | map< IOHIDElementCookie, OSX_Hid_Device_Element * >::iterator iter, end; |
| 1210 | end = elements->end(); |
| 1211 | for( iter = elements->begin(); iter != end; iter++ ) |
| 1212 | delete iter->second; |
| 1213 | delete elements; |
| 1214 | elements = NULL; |
| 1215 | |
| 1216 | // TODO: delete outputs also |
| 1217 | |
| 1218 | this->unlock(); |
| 1219 | } |
| 1220 | |
| 1221 | if( hidProperties ) |
| 1222 | { |
| 1223 | CFRelease( hidProperties ); |
| 1224 | hidProperties = NULL; |
| 1225 | } |
| 1226 | |
| 1227 | this->lock(); |
| 1228 | |
| 1229 | if( locationID ) |
| 1230 | { |
| 1231 | CFRelease( locationID ); |
| 1232 | locationID = NULL; |
| 1233 | } |
| 1234 | |
| 1235 | if( vendorID ) |
| 1236 | { |
| 1237 | CFRelease( vendorID ); |
| 1238 | vendorID = NULL; |
| 1239 | } |
| 1240 | |
| 1241 | if( productID ) |
| 1242 | { |
| 1243 | CFRelease( productID ); |
| 1244 | productID = NULL; |
| 1245 | } |
| 1246 | |
| 1247 | if( manufacturer ) |
| 1248 | { |
| 1249 | CFRelease( manufacturer ); |
| 1250 | manufacturer = NULL; |
| 1251 | } |
| 1252 | |
| 1253 | if( product ) |
| 1254 | { |
| 1255 | CFRelease( product ); |
| 1256 | product = NULL; |
| 1257 | } |
| 1258 | |
| 1259 | this->unlock(); |
| 1260 | } |
| 1261 | |
| 1262 | static void Hid_key_table_init() |
| 1263 | { |
| 1264 | memset( g_hid_key_table, 0, sizeof( g_hid_key_table ) ); |
| 1265 | |
| 1266 | // ASCII letters |
| 1267 | g_hid_key_table[kHIDUsage_KeyboardA] = 'A'; |
| 1268 | g_hid_key_table[kHIDUsage_KeyboardB] = 'B'; |
| 1269 | g_hid_key_table[kHIDUsage_KeyboardC] = 'C'; |
| 1270 | g_hid_key_table[kHIDUsage_KeyboardD] = 'D'; |
| 1271 | g_hid_key_table[kHIDUsage_KeyboardE] = 'E'; |
| 1272 | g_hid_key_table[kHIDUsage_KeyboardF] = 'F'; |
| 1273 | g_hid_key_table[kHIDUsage_KeyboardG] = 'G'; |
| 1274 | g_hid_key_table[kHIDUsage_KeyboardH] = 'H'; |
| 1275 | g_hid_key_table[kHIDUsage_KeyboardI] = 'I'; |
| 1276 | g_hid_key_table[kHIDUsage_KeyboardJ] = 'J'; |
| 1277 | g_hid_key_table[kHIDUsage_KeyboardK] = 'K'; |
| 1278 | g_hid_key_table[kHIDUsage_KeyboardL] = 'L'; |
| 1279 | g_hid_key_table[kHIDUsage_KeyboardM] = 'M'; |
| 1280 | g_hid_key_table[kHIDUsage_KeyboardN] = 'N'; |
| 1281 | g_hid_key_table[kHIDUsage_KeyboardO] = 'O'; |
| 1282 | g_hid_key_table[kHIDUsage_KeyboardP] = 'P'; |
| 1283 | g_hid_key_table[kHIDUsage_KeyboardQ] = 'Q'; |
| 1284 | g_hid_key_table[kHIDUsage_KeyboardR] = 'R'; |
| 1285 | g_hid_key_table[kHIDUsage_KeyboardS] = 'S'; |
| 1286 | g_hid_key_table[kHIDUsage_KeyboardT] = 'T'; |
| 1287 | g_hid_key_table[kHIDUsage_KeyboardU] = 'U'; |
| 1288 | g_hid_key_table[kHIDUsage_KeyboardV] = 'V'; |
| 1289 | g_hid_key_table[kHIDUsage_KeyboardW] = 'W'; |
| 1290 | g_hid_key_table[kHIDUsage_KeyboardX] = 'X'; |
| 1291 | g_hid_key_table[kHIDUsage_KeyboardY] = 'Y'; |
| 1292 | g_hid_key_table[kHIDUsage_KeyboardZ] = 'Z'; |
| 1293 | |
| 1294 | // ASCII numbers |
| 1295 | g_hid_key_table[kHIDUsage_Keyboard1] = '1'; |
| 1296 | g_hid_key_table[kHIDUsage_Keyboard2] = '2'; |
| 1297 | g_hid_key_table[kHIDUsage_Keyboard3] = '3'; |
| 1298 | g_hid_key_table[kHIDUsage_Keyboard4] = '4'; |
| 1299 | g_hid_key_table[kHIDUsage_Keyboard5] = '5'; |
| 1300 | g_hid_key_table[kHIDUsage_Keyboard6] = '6'; |
| 1301 | g_hid_key_table[kHIDUsage_Keyboard7] = '7'; |
| 1302 | g_hid_key_table[kHIDUsage_Keyboard8] = '8'; |
| 1303 | g_hid_key_table[kHIDUsage_Keyboard9] = '9'; |
| 1304 | g_hid_key_table[kHIDUsage_Keyboard0] = '0'; |
| 1305 | |
| 1306 | // ASCII whitespace/control characters |
| 1307 | g_hid_key_table[kHIDUsage_KeyboardEscape] = '\e'; |
| 1308 | g_hid_key_table[kHIDUsage_KeyboardDeleteOrBackspace] = '\b'; |
| 1309 | g_hid_key_table[kHIDUsage_KeyboardReturnOrEnter] = '\n'; |
| 1310 | g_hid_key_table[kHIDUsage_KeyboardTab] = '\t'; |
| 1311 | g_hid_key_table[kHIDUsage_KeyboardSpacebar] = ' '; |
| 1312 | |
| 1313 | // ASCII symbols |
| 1314 | g_hid_key_table[kHIDUsage_KeyboardHyphen] = '-'; |
| 1315 | g_hid_key_table[kHIDUsage_KeyboardEqualSign] = '='; |
| 1316 | g_hid_key_table[kHIDUsage_KeyboardOpenBracket] = '['; |
| 1317 | g_hid_key_table[kHIDUsage_KeyboardCloseBracket] = ']'; |
| 1318 | g_hid_key_table[kHIDUsage_KeyboardBackslash] = '\\'; |
| 1319 | g_hid_key_table[kHIDUsage_KeyboardNonUSPound] = '#'; |
| 1320 | g_hid_key_table[kHIDUsage_KeyboardSemicolon] = ';'; |
| 1321 | g_hid_key_table[kHIDUsage_KeyboardQuote] = '\''; |
| 1322 | g_hid_key_table[kHIDUsage_KeyboardGraveAccentAndTilde] = '`'; |
| 1323 | g_hid_key_table[kHIDUsage_KeyboardComma] = ','; |
| 1324 | g_hid_key_table[kHIDUsage_KeyboardPeriod] = '.'; |
| 1325 | g_hid_key_table[kHIDUsage_KeyboardSlash] = '/'; |
| 1326 | |
| 1327 | // ASCII keypad symbols/whitespace |
| 1328 | g_hid_key_table[kHIDUsage_KeypadSlash] = '/'; |
| 1329 | g_hid_key_table[kHIDUsage_KeypadAsterisk] = '*'; |
| 1330 | g_hid_key_table[kHIDUsage_KeypadHyphen] = '-'; |
| 1331 | g_hid_key_table[kHIDUsage_KeypadPlus] = '+'; |
| 1332 | g_hid_key_table[kHIDUsage_KeypadEnter] = '\n'; |
| 1333 | |
| 1334 | // ASCII keypad numbers |
| 1335 | g_hid_key_table[kHIDUsage_Keypad1] = '1'; |
| 1336 | g_hid_key_table[kHIDUsage_Keypad2] = '2'; |
| 1337 | g_hid_key_table[kHIDUsage_Keypad3] = '3'; |
| 1338 | g_hid_key_table[kHIDUsage_Keypad4] = '4'; |
| 1339 | g_hid_key_table[kHIDUsage_Keypad5] = '5'; |
| 1340 | g_hid_key_table[kHIDUsage_Keypad6] = '6'; |
| 1341 | g_hid_key_table[kHIDUsage_Keypad7] = '7'; |
| 1342 | g_hid_key_table[kHIDUsage_Keypad8] = '8'; |
| 1343 | g_hid_key_table[kHIDUsage_Keypad9] = '9'; |
| 1344 | g_hid_key_table[kHIDUsage_Keypad0] = '0'; |
| 1345 | |
| 1346 | // ASCII keypad symbols |
| 1347 | g_hid_key_table[kHIDUsage_KeypadPeriod] = '.'; |
| 1348 | g_hid_key_table[kHIDUsage_KeyboardNonUSBackslash] = '\\'; |
| 1349 | g_hid_key_table[kHIDUsage_KeypadEqualSign] = '='; |
| 1350 | g_hid_key_table[kHIDUsage_KeypadComma] = ','; |
| 1351 | g_hid_key_table[kHIDUsage_KeypadEqualSignAS400] = '='; |
| 1352 | } |
| 1353 | |
| 1354 | t_CKINT Hid_hwkey_to_ascii( t_CKINT hwkey ) |
| 1355 | { |
| 1356 | if( hwkey < 0 || hwkey >= sizeof( g_hid_key_table ) ) |
| 1357 | return -1; |
| 1358 | |
| 1359 | return g_hid_key_table[hwkey]; |
| 1360 | } |
| 1361 | |
| 1362 | void Hid_init() |
| 1363 | { |
| 1364 | Hid_key_table_init(); |
| 1365 | |
| 1366 | rlHid = CFRunLoopGetCurrent(); |
| 1367 | |
| 1368 | // set up notification for new added devices/removed devices |
| 1369 | CFRunLoopAddSource( rlHid, |
| 1370 | IONotificationPortGetRunLoopSource( newDeviceNotificationPort ), |
| 1371 | kCFRunLoopChuckHidMode ); |
| 1372 | |
| 1373 | // add open/close operation source |
| 1374 | CFRunLoopSourceContext opSourceContext; |
| 1375 | opSourceContext.version = 0; |
| 1376 | opSourceContext.info = NULL; |
| 1377 | opSourceContext.retain = NULL; |
| 1378 | opSourceContext.release = NULL; |
| 1379 | opSourceContext.copyDescription = NULL; |
| 1380 | opSourceContext.equal = NULL; |
| 1381 | opSourceContext.hash = NULL; |
| 1382 | opSourceContext.schedule = NULL; |
| 1383 | opSourceContext.cancel = NULL; |
| 1384 | opSourceContext.perform = Hid_do_operation; |
| 1385 | CFRunLoopSourceRef _hidOpSource = CFRunLoopSourceCreate( kCFAllocatorDefault, |
| 1386 | 0, |
| 1387 | &opSourceContext ); |
| 1388 | CFRunLoopAddSource( rlHid, _hidOpSource, kCFRunLoopChuckHidMode ); |
| 1389 | hidOpSource = _hidOpSource; |
| 1390 | |
| 1391 | Hid_do_operation( NULL ); |
| 1392 | |
| 1393 | #ifdef __CK_HID_WIIREMOTE__ |
| 1394 | // add wii remote source |
| 1395 | CFRunLoopSourceContext wrSourceContext; |
| 1396 | wrSourceContext.version = 0; |
| 1397 | wrSourceContext.info = NULL; |
| 1398 | wrSourceContext.retain = NULL; |
| 1399 | wrSourceContext.release = NULL; |
| 1400 | wrSourceContext.copyDescription = NULL; |
| 1401 | wrSourceContext.equal = NULL; |
| 1402 | wrSourceContext.hash = NULL; |
| 1403 | wrSourceContext.schedule = NULL; |
| 1404 | wrSourceContext.cancel = NULL; |
| 1405 | wrSourceContext.perform = WiiRemote_cfrl_callback; |
| 1406 | CFRunLoopSourceRef _cfrlWiiRemoteSource = CFRunLoopSourceCreate( kCFAllocatorDefault, |
| 1407 | 0, |
| 1408 | &wrSourceContext ); |
| 1409 | CFRunLoopAddSource( rlHid, _cfrlWiiRemoteSource, kCFRunLoopChuckHidMode ); |
| 1410 | cfrlWiiRemoteSource = _cfrlWiiRemoteSource; |
| 1411 | |
| 1412 | WiiRemote_cfrl_callback( NULL ); |
| 1413 | #endif |
| 1414 | } |
| 1415 | |
| 1416 | void Hid_init2() |
| 1417 | { |
| 1418 | // verify that the joystick system has not already been initialized |
| 1419 | if( g_hid_init == TRUE ) |
| 1420 | return; |
| 1421 | |
| 1422 | g_hid_init = TRUE; |
| 1423 | |
| 1424 | IOReturn result = kIOReturnSuccess; |
| 1425 | io_iterator_t hidObjectIterator = 0; |
| 1426 | // CFTypeRef refCF; |
| 1427 | // t_CKINT filter_usage_page = kHIDPage_GenericDesktop; |
| 1428 | |
| 1429 | // allocate vectors of device records |
| 1430 | joysticks = new xvector< OSX_Hid_Device * >; |
| 1431 | mice = new xvector< OSX_Hid_Device * >; |
| 1432 | keyboards = new xvector< OSX_Hid_Device * >; |
| 1433 | |
| 1434 | joystick_names = new xmultimap< string, OSX_Hid_Device * >; |
| 1435 | mouse_names = new xmultimap< string, OSX_Hid_Device * >; |
| 1436 | keyboard_names = new xmultimap< string, OSX_Hid_Device * >; |
| 1437 | |
| 1438 | hid_operation_buffer = new CBufferSimple; |
| 1439 | hid_operation_buffer->initialize( 20, sizeof( OSX_Hid_op ) ); |
| 1440 | |
| 1441 | CFMutableDictionaryRef hidMatchDictionary = IOServiceMatching( kIOHIDDeviceKey ); |
| 1442 | if( !hidMatchDictionary ) |
| 1443 | { |
| 1444 | EM_log( CK_LOG_HERALD, "hid: error: unable to retrieving hidMatchDictionary, unable to initialize" ); |
| 1445 | return; |
| 1446 | } |
| 1447 | |
| 1448 | /*refCF = ( CFTypeRef ) CFNumberCreate( kCFAllocatorDefault, |
| 1449 | kCFNumberLongType, |
| 1450 | &filter_usage_page ); |
| 1451 | CFDictionarySetValue( hidMatchDictionary, |
| 1452 | CFSTR( kIOHIDPrimaryUsagePageKey ), refCF );*/ |
| 1453 | |
| 1454 | // 1.5.0.7 (ge) updated kIOMasterPortDefault (deprecated as of macOS 12) to kIOMainPortDefault |
| 1455 | #if __MAC_OS_X_VERSION_MIN_REQUIRED < 120000 // before macOS 12 |
| 1456 | newDeviceNotificationPort = IONotificationPortCreate( kIOMasterPortDefault ); |
| 1457 | #else |
| 1458 | newDeviceNotificationPort = IONotificationPortCreate( kIOMainPortDefault ); |
| 1459 | #endif |
| 1460 | |
| 1461 | result = IOServiceAddMatchingNotification( newDeviceNotificationPort, |
| 1462 | kIOFirstMatchNotification, |
| 1463 | hidMatchDictionary, |
| 1464 | Hid_new_devices, |
| 1465 | NULL, |
| 1466 | &hidObjectIterator ); |
| 1467 | |
| 1468 | if( result != kIOReturnSuccess || hidObjectIterator == 0 ) |
| 1469 | { |
| 1470 | EM_log( CK_LOG_HERALD, "hid: error: unable to retrieving matching services, unable to initialize" ); |
| 1471 | return; |
| 1472 | } |
| 1473 | |
| 1474 | //CFRelease( refCF ); |
| 1475 | |
| 1476 | Hid_new_devices( NULL, hidObjectIterator ); |
| 1477 | } |
| 1478 | |
| 1479 | static void Hid_new_devices( void * refcon, io_iterator_t iterator ) |
| 1480 | { |
| 1481 | EM_log( CK_LOG_INFO, "hid: new device(s) found" ); |
| 1482 | |
| 1483 | char __temp[256]; |
| 1484 | |
| 1485 | CFTypeRef refCF = NULL; |
| 1486 | |
| 1487 | io_object_t ioHIDDeviceObject = 0; |
| 1488 | t_CKINT usage, usage_page; |
| 1489 | t_CKINT joysticks_seen = joysticks->size(), |
| 1490 | mice_seen = mice->size(), |
| 1491 | keyboards_seen = keyboards->size(); |
| 1492 | |
| 1493 | while( 1 ) |
| 1494 | { |
| 1495 | if( ioHIDDeviceObject ) |
| 1496 | IOObjectRelease( ioHIDDeviceObject ); |
| 1497 | |
| 1498 | ioHIDDeviceObject = IOIteratorNext( iterator ); |
| 1499 | if( ioHIDDeviceObject == 0 ) |
| 1500 | break; |
| 1501 | |
| 1502 | // ascertain device information |
| 1503 | |
| 1504 | // first, determine the device usage page and usage |
| 1505 | usage = usage_page = -1; |
| 1506 | |
| 1507 | refCF = IORegistryEntryCreateCFProperty( ioHIDDeviceObject, |
| 1508 | CFSTR( kIOHIDPrimaryUsagePageKey ), |
| 1509 | kCFAllocatorDefault, |
| 1510 | kNilOptions ); |
| 1511 | if( !refCF ) |
| 1512 | continue; |
| 1513 | |
| 1514 | CFNumberGetValue( ( CFNumberRef )refCF, kCFNumberLongType, &usage_page ); |
| 1515 | CFRelease( refCF ); |
| 1516 | |
| 1517 | refCF = IORegistryEntryCreateCFProperty( ioHIDDeviceObject, |
| 1518 | CFSTR( kIOHIDPrimaryUsageKey ), |
| 1519 | kCFAllocatorDefault, |
| 1520 | kNilOptions); |
| 1521 | if( !refCF ) |
| 1522 | continue; |
| 1523 | |
| 1524 | CFNumberGetValue( ( CFNumberRef )refCF, kCFNumberLongType, &usage ); |
| 1525 | CFRelease( refCF ); |
| 1526 | |
| 1527 | if( usage_page != kHIDPage_GenericDesktop ) |
| 1528 | { |
| 1529 | // some sort of HID device we dont recognize |
| 1530 | // lets probe its input/output reports and try to categorize it |
| 1531 | EM_log( CK_LOG_INFO, "hid: device not recognized, attempting to determine HID type" ); |
| 1532 | |
| 1533 | // allocate the device record, set usage page and usage |
| 1534 | OSX_Hid_Device * new_device = new OSX_Hid_Device; |
| 1535 | new_device->type = CK_HID_DEV_NONE; |
| 1536 | new_device->num = 0; |
| 1537 | new_device->usage_page = usage_page; |
| 1538 | new_device->usage = usage; |
| 1539 | |
| 1540 | if( !new_device->preconfigure( ioHIDDeviceObject ) && !new_device->configure() ) |
| 1541 | { |
| 1542 | if( new_device->hats > 0 || new_device->axes > 2 ) |
| 1543 | // make it a joystick |
| 1544 | { |
| 1545 | usage_page = kHIDPage_GenericDesktop; |
| 1546 | usage = kHIDUsage_GD_Joystick; |
| 1547 | } |
| 1548 | |
| 1549 | else if( new_device->axes == 2 ) |
| 1550 | // make it a mouse |
| 1551 | { |
| 1552 | usage_page = kHIDPage_GenericDesktop; |
| 1553 | usage = kHIDUsage_GD_Mouse; |
| 1554 | } |
| 1555 | |
| 1556 | else if( new_device->buttons > 0 ) |
| 1557 | // make it a keyboard |
| 1558 | { |
| 1559 | usage_page = kHIDPage_GenericDesktop; |
| 1560 | usage = kHIDUsage_GD_Keyboard; |
| 1561 | } |
| 1562 | |
| 1563 | new_device->cleanup(); |
| 1564 | } |
| 1565 | |
| 1566 | else |
| 1567 | { |
| 1568 | //EM_log( ); |
| 1569 | } |
| 1570 | |
| 1571 | delete new_device; |
| 1572 | } |
| 1573 | |
| 1574 | if ( usage_page == kHIDPage_GenericDesktop ) |
| 1575 | { |
| 1576 | if( usage == kHIDUsage_GD_Joystick || |
| 1577 | usage == kHIDUsage_GD_GamePad ) |
| 1578 | // this is a joystick, create a new item in the joystick array |
| 1579 | { |
| 1580 | // see if this an device that was disconnected being reconnected |
| 1581 | refCF = IORegistryEntryCreateCFProperty( ioHIDDeviceObject, |
| 1582 | CFSTR( kIOHIDProductKey ), |
| 1583 | kCFAllocatorDefault, |
| 1584 | kNilOptions); |
| 1585 | |
| 1586 | if( refCF ) |
| 1587 | { |
| 1588 | CFStringGetCString( ( CFStringRef ) refCF, __temp, 256, |
| 1589 | kCFStringEncodingASCII ); |
| 1590 | CFRelease( refCF ); |
| 1591 | } |
| 1592 | |
| 1593 | else |
| 1594 | strncpy( __temp, "Joystick", 256 ); |
| 1595 | |
| 1596 | pair< xmultimap< string, OSX_Hid_Device * >::const_iterator, |
| 1597 | xmultimap< string, OSX_Hid_Device * >::const_iterator > name_range |
| 1598 | = joystick_names->equal_range( string( __temp ) ); |
| 1599 | |
| 1600 | xmultimap< string, OSX_Hid_Device * >::const_iterator |
| 1601 | start = name_range.first, |
| 1602 | end = name_range.second; |
| 1603 | |
| 1604 | t_CKBOOL match_found = FALSE; |
| 1605 | |
| 1606 | for( ; start != end; start++ ) |
| 1607 | { |
| 1608 | OSX_Hid_Device * old_device = ( *start ).second; |
| 1609 | if( !old_device->is_connected() && |
| 1610 | old_device->is_same_as( ioHIDDeviceObject ) ) |
| 1611 | { |
| 1612 | match_found = TRUE; |
| 1613 | |
| 1614 | EM_log( CK_LOG_INFO, "hid: joystick %s reattached", |
| 1615 | old_device->name ); |
| 1616 | |
| 1617 | if( old_device->preconfigure( ioHIDDeviceObject ) ) |
| 1618 | { |
| 1619 | EM_log( CK_LOG_INFO, "hid: error during reconfiguration of %s", |
| 1620 | old_device->name ); |
| 1621 | break; |
| 1622 | } |
| 1623 | |
| 1624 | if( old_device->refcount && |
| 1625 | !old_device->start() ) |
| 1626 | { |
| 1627 | HidMsg msg; |
| 1628 | msg.device_type = old_device->type; |
| 1629 | msg.device_num = old_device->num; |
| 1630 | msg.type = CK_HID_DEVICE_CONNECTED; |
| 1631 | |
| 1632 | HidInManager::push_message( msg ); |
| 1633 | } |
| 1634 | |
| 1635 | break; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | if( match_found ) |
| 1640 | continue; |
| 1641 | |
| 1642 | // create a new device record for the device |
| 1643 | EM_log( CK_LOG_INFO, "hid: preconfiguring joystick %i", joysticks_seen ); |
| 1644 | EM_pushlog(); |
| 1645 | |
| 1646 | // allocate the device record, set usage page and usage |
| 1647 | OSX_Hid_Device * new_device = new OSX_Hid_Device; |
| 1648 | new_device->type = CK_HID_DEV_JOYSTICK; |
| 1649 | new_device->num = joysticks->size(); |
| 1650 | new_device->usage_page = usage_page; |
| 1651 | new_device->usage = usage; |
| 1652 | |
| 1653 | if( !new_device->preconfigure( ioHIDDeviceObject ) ) |
| 1654 | { |
| 1655 | joysticks->lock(); |
| 1656 | joysticks->push_back( new_device ); |
| 1657 | joysticks->unlock(); |
| 1658 | |
| 1659 | joystick_names->lock(); |
| 1660 | joystick_names->insert( pair< string, OSX_Hid_Device * >( string( new_device->name ), |
| 1661 | new_device ) ); |
| 1662 | joystick_names->unlock(); |
| 1663 | } |
| 1664 | |
| 1665 | else |
| 1666 | { |
| 1667 | EM_log( CK_LOG_INFO, |
| 1668 | "hid: ignoring %i, invalid joystick", |
| 1669 | joysticks_seen ); |
| 1670 | delete new_device; |
| 1671 | } |
| 1672 | |
| 1673 | joysticks_seen++; |
| 1674 | |
| 1675 | EM_poplog(); |
| 1676 | } |
| 1677 | |
| 1678 | if( usage == kHIDUsage_GD_Mouse ) |
| 1679 | // this is a mouse |
| 1680 | { |
| 1681 | // see if this an device that was disconnected being reconnected |
| 1682 | refCF = IORegistryEntryCreateCFProperty( ioHIDDeviceObject, |
| 1683 | CFSTR( kIOHIDProductKey ), |
| 1684 | kCFAllocatorDefault, |
| 1685 | kNilOptions); |
| 1686 | |
| 1687 | if( refCF ) |
| 1688 | { |
| 1689 | CFStringGetCString( ( CFStringRef ) refCF, __temp, 256, |
| 1690 | kCFStringEncodingASCII ); |
| 1691 | CFRelease( refCF ); |
| 1692 | } |
| 1693 | |
| 1694 | else |
| 1695 | strncpy( __temp, "Mouse", 256 ); |
| 1696 | |
| 1697 | pair< xmultimap< string, OSX_Hid_Device * >::const_iterator, |
| 1698 | xmultimap< string, OSX_Hid_Device * >::const_iterator > name_range |
| 1699 | = mouse_names->equal_range( string( __temp ) ); |
| 1700 | |
| 1701 | xmultimap< string, OSX_Hid_Device * >::const_iterator |
| 1702 | start = name_range.first, |
| 1703 | end = name_range.second; |
| 1704 | |
| 1705 | t_CKBOOL match_found = FALSE; |
| 1706 | |
| 1707 | for( ; start != end; start++ ) |
| 1708 | { |
| 1709 | OSX_Hid_Device * old_device = ( *start ).second; |
| 1710 | if( !old_device->is_connected() && |
| 1711 | old_device->is_same_as( ioHIDDeviceObject ) ) |
| 1712 | { |
| 1713 | match_found = TRUE; |
| 1714 | |
| 1715 | EM_log( CK_LOG_INFO, "hid: mouse %s reattached", |
| 1716 | old_device->name ); |
| 1717 | |
| 1718 | if( old_device->preconfigure( ioHIDDeviceObject ) ) |
| 1719 | { |
| 1720 | EM_log( CK_LOG_INFO, "hid: error during reconfiguration of %s", |
| 1721 | old_device->name ); |
| 1722 | break; |
| 1723 | } |
| 1724 | |
| 1725 | if( old_device->refcount && |
| 1726 | !old_device->start() ) |
| 1727 | { |
| 1728 | HidMsg msg; |
| 1729 | msg.device_type = old_device->type; |
| 1730 | msg.device_num = old_device->num; |
| 1731 | msg.type = CK_HID_DEVICE_CONNECTED; |
| 1732 | |
| 1733 | HidInManager::push_message( msg ); |
| 1734 | } |
| 1735 | |
| 1736 | break; |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | if( match_found ) |
| 1741 | continue; |
| 1742 | |
| 1743 | // create a new device record for the device |
| 1744 | EM_log( CK_LOG_INFO, "hid: preconfiguring mouse %i", mice_seen ); |
| 1745 | EM_pushlog(); |
| 1746 | |
| 1747 | // allocate the device record, set usage page and usage |
| 1748 | OSX_Hid_Device * new_device = new OSX_Hid_Device; |
| 1749 | new_device->type = CK_HID_DEV_MOUSE; |
| 1750 | new_device->num = mice->size(); |
| 1751 | new_device->usage_page = usage_page; |
| 1752 | new_device->usage = usage; |
| 1753 | |
| 1754 | if( !new_device->preconfigure( ioHIDDeviceObject ) ) |
| 1755 | { |
| 1756 | mice->lock(); |
| 1757 | mice->push_back( new_device ); |
| 1758 | mice->unlock(); |
| 1759 | |
| 1760 | mouse_names->lock(); |
| 1761 | mouse_names->insert( pair< string, OSX_Hid_Device * >( string( new_device->name ), |
| 1762 | new_device ) ); |
| 1763 | mouse_names->unlock(); |
| 1764 | } |
| 1765 | |
| 1766 | else |
| 1767 | { |
| 1768 | EM_log( CK_LOG_INFO, |
| 1769 | "hid: ignoring %i, invalid mouse", |
| 1770 | mice_seen ); |
| 1771 | delete new_device; |
| 1772 | } |
| 1773 | |
| 1774 | mice_seen++; |
| 1775 | |
| 1776 | EM_poplog(); |
| 1777 | } |
| 1778 | |
| 1779 | if( usage == kHIDUsage_GD_Keyboard || usage == kHIDUsage_GD_Keypad ) |
| 1780 | // this is a keyboard |
| 1781 | { |
| 1782 | // see if this an device that was disconnected being reconnected |
| 1783 | refCF = IORegistryEntryCreateCFProperty( ioHIDDeviceObject, |
| 1784 | CFSTR( kIOHIDProductKey ), |
| 1785 | kCFAllocatorDefault, |
| 1786 | kNilOptions); |
| 1787 | |
| 1788 | if( refCF ) |
| 1789 | { |
| 1790 | CFStringGetCString( ( CFStringRef ) refCF, __temp, 256, |
| 1791 | kCFStringEncodingASCII ); |
| 1792 | CFRelease( refCF ); |
| 1793 | } |
| 1794 | |
| 1795 | else |
| 1796 | strncpy( __temp, "Keyboard", 256 ); |
| 1797 | |
| 1798 | pair< xmultimap< string, OSX_Hid_Device * >::const_iterator, |
| 1799 | xmultimap< string, OSX_Hid_Device * >::const_iterator > name_range |
| 1800 | = keyboard_names->equal_range( string( __temp ) ); |
| 1801 | |
| 1802 | xmultimap< string, OSX_Hid_Device * >::const_iterator |
| 1803 | start = name_range.first, |
| 1804 | end = name_range.second; |
| 1805 | |
| 1806 | t_CKBOOL match_found = FALSE; |
| 1807 | |
| 1808 | for( ; start != end; start++ ) |
| 1809 | { |
| 1810 | OSX_Hid_Device * old_device = ( *start ).second; |
| 1811 | if( !old_device->is_connected() && |
| 1812 | old_device->is_same_as( ioHIDDeviceObject ) ) |
| 1813 | { |
| 1814 | match_found = TRUE; |
| 1815 | |
| 1816 | EM_log( CK_LOG_INFO, "hid: keyboard %s reattached", |
| 1817 | old_device->name ); |
| 1818 | |
| 1819 | if( old_device->preconfigure( ioHIDDeviceObject ) ) |
| 1820 | { |
| 1821 | EM_log( CK_LOG_INFO, "hid: error during reconfiguration of %s", |
| 1822 | old_device->name ); |
| 1823 | break; |
| 1824 | } |
| 1825 | |
| 1826 | if( old_device->refcount && |
| 1827 | !old_device->start() ) |
| 1828 | { |
| 1829 | HidMsg msg; |
| 1830 | msg.device_type = old_device->type; |
| 1831 | msg.device_num = old_device->num; |
| 1832 | msg.type = CK_HID_DEVICE_CONNECTED; |
| 1833 | |
| 1834 | HidInManager::push_message( msg ); |
| 1835 | } |
| 1836 | |
| 1837 | break; |
| 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | if( match_found ) |
| 1842 | continue; |
| 1843 | |
| 1844 | // create a new device record for the device |
| 1845 | EM_log( CK_LOG_INFO, "hid: preconfiguring keyboard %i", |
| 1846 | keyboards_seen ); |
| 1847 | EM_pushlog(); |
| 1848 | |
| 1849 | // allocate the device record, set usage page and usage |
| 1850 | OSX_Hid_Device * new_device = new OSX_Hid_Device; |
| 1851 | new_device->type = CK_HID_DEV_KEYBOARD; |
| 1852 | new_device->num = keyboards->size(); |
| 1853 | new_device->usage_page = usage_page; |
| 1854 | new_device->usage = usage; |
| 1855 | |
| 1856 | if( !new_device->preconfigure( ioHIDDeviceObject ) ) |
| 1857 | { |
| 1858 | keyboards->lock(); |
| 1859 | keyboards->push_back( new_device ); |
| 1860 | keyboards->unlock(); |
| 1861 | |
| 1862 | keyboard_names->lock(); |
| 1863 | keyboard_names->insert( pair< string, OSX_Hid_Device * >( string( new_device->name ), |
| 1864 | new_device ) ); |
| 1865 | keyboard_names->unlock(); |
| 1866 | } |
| 1867 | |
| 1868 | else |
| 1869 | { |
| 1870 | EM_log( CK_LOG_INFO, |
| 1871 | "hid: ignoring %i, invalid keyboard", |
| 1872 | keyboards_seen ); |
| 1873 | delete new_device; |
| 1874 | } |
| 1875 | |
| 1876 | keyboards_seen++; |
| 1877 | |
| 1878 | EM_poplog(); |
| 1879 | } |
| 1880 | } |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | void Hid_device_removed( void * refcon, io_service_t service, |
| 1885 | natural_t messageType, void * messageArgument ) |
| 1886 | { |
| 1887 | if( messageType == kIOMessageServiceIsTerminated && refcon ) |
| 1888 | { |
| 1889 | OSX_Hid_Device * device = ( OSX_Hid_Device * ) refcon; |
| 1890 | |
| 1891 | if( device->is_connected() ) |
| 1892 | { |
| 1893 | EM_log( CK_LOG_INFO, "hid: %s disconnected", device->name ); |
| 1894 | |
| 1895 | device->lock(); |
| 1896 | device->disconnect(); |
| 1897 | device->unlock(); |
| 1898 | |
| 1899 | HidMsg msg; |
| 1900 | msg.device_type = device->type; |
| 1901 | msg.device_num = device->num; |
| 1902 | msg.type = CK_HID_DEVICE_DISCONNECTED; |
| 1903 | |
| 1904 | HidInManager::push_message( msg ); |
| 1905 | } |
| 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | #ifdef __CK_HID_WIIREMOTE__ |
| 1910 | |
| 1911 | #include "objc/objc.h" |
| 1912 | #include "objc/objc-runtime.h" |
| 1913 | #include "objc/objc-class.h" |
| 1914 | |
| 1915 | static id createAutoReleasePool() |
| 1916 | { |
| 1917 | Class NSAutoreleasePoolClass = ( Class ) objc_getClass( "NSAutoreleasePool" ); |
| 1918 | |
| 1919 | id autoReleasePool = class_createInstance( NSAutoreleasePoolClass, 0 ); |
| 1920 | |
| 1921 | SEL initSelector = sel_registerName( "init" ); |
| 1922 | |
| 1923 | autoReleasePool = objc_msgSend( autoReleasePool, initSelector ); |
| 1924 | |
| 1925 | return autoReleasePool; |
| 1926 | } |
| 1927 | |
| 1928 | static void releaseAutoReleasePool() |
| 1929 | { |
| 1930 | |
| 1931 | } |
| 1932 | |
| 1933 | #endif |
| 1934 | |
| 1935 | void Hid_poll() |
| 1936 | { |
| 1937 | #ifdef __CK_HID_WIIREMOTE__ |
| 1938 | id auto_release_pool = createAutoReleasePool(); |
| 1939 | #endif |
| 1940 | |
| 1941 | CFRunLoopRunInMode( kCFRunLoopChuckHidMode, 60 * 60 * 24, false ); |
| 1942 | } |
| 1943 | |
| 1944 | void Hid_callback( void * target, IOReturn result, |
| 1945 | void * refcon, void * sender) |
| 1946 | { |
| 1947 | OSX_Hid_Device * device = ( OSX_Hid_Device * ) refcon; |
| 1948 | OSX_Hid_Device_Element * element; |
| 1949 | AbsoluteTime atZero = { 0, 0 }; |
| 1950 | IOHIDEventStruct event; |
| 1951 | HidMsg msg; |
| 1952 | |
| 1953 | while( result == kIOReturnSuccess ) |
| 1954 | { |
| 1955 | result = ( *( device->queue ) )->getNextEvent( device->queue, |
| 1956 | &event, atZero, 0 ); |
| 1957 | if( result == kIOReturnUnderrun ) |
| 1958 | break; |
| 1959 | if( result != kIOReturnSuccess ) |
| 1960 | { |
| 1961 | EM_log( CK_LOG_INFO, "hid: warning: getNextEvent failed" ); |
| 1962 | break; |
| 1963 | } |
| 1964 | |
| 1965 | element = ( *( device->elements ) )[event.elementCookie]; |
| 1966 | |
| 1967 | msg.clear(); |
| 1968 | msg.device_type = device->type; |
| 1969 | msg.device_num = device->num; |
| 1970 | msg.type = element->type; |
| 1971 | msg.eid = element->num; |
| 1972 | |
| 1973 | switch( msg.type ) |
| 1974 | { |
| 1975 | case CK_HID_JOYSTICK_AXIS: |
| 1976 | // scale the value to [-1.0, 1.0] |
| 1977 | msg.fdata[0] = ((t_CKFLOAT)(event.value - element->min)) * 2.0 / ((t_CKFLOAT)(element->max - element->min)) - 1.0; |
| 1978 | break; |
| 1979 | |
| 1980 | case CK_HID_JOYSTICK_HAT: |
| 1981 | msg.idata[0] = event.value; |
| 1982 | break; |
| 1983 | |
| 1984 | case CK_HID_MOUSE_MOTION: |
| 1985 | { // gewang: added |
| 1986 | msg.eid = 0; |
| 1987 | if( element->usage == kHIDUsage_GD_X ) |
| 1988 | { |
| 1989 | msg.idata[0] = event.value; |
| 1990 | msg.idata[1] = 0; |
| 1991 | } |
| 1992 | |
| 1993 | else |
| 1994 | { |
| 1995 | msg.idata[0] = 0; |
| 1996 | msg.idata[1] = event.value; |
| 1997 | } |
| 1998 | |
| 1999 | #ifndef __CK_HID_CURSOR_TRACK__ |
| 2000 | |
| 2001 | CGEventRef event = CGEventCreate(NULL); |
| 2002 | CGPoint cursor = CGEventGetLocation(event); |
| 2003 | CFRelease(event); |
| 2004 | |
| 2005 | msg.idata[2] = cursor.x; |
| 2006 | msg.idata[3] = cursor.y; |
| 2007 | |
| 2008 | CGDirectDisplayID display; |
| 2009 | CGDisplayCount displayCount; |
| 2010 | |
| 2011 | CGPoint cgp; |
| 2012 | cgp.x = cursor.x; |
| 2013 | cgp.y = cursor.y; |
| 2014 | |
| 2015 | if( CGGetDisplaysWithPoint( cgp, 1, &display, &displayCount ) == |
| 2016 | kCGErrorSuccess ) |
| 2017 | { |
| 2018 | CGRect bounds = CGDisplayBounds( display ); |
| 2019 | |
| 2020 | msg.fdata[0] = ( ( t_CKFLOAT ) ( cursor.x - bounds.origin.x ) ) / |
| 2021 | ( bounds.size.width - 1 ); |
| 2022 | msg.fdata[1] = ( ( t_CKFLOAT ) ( cursor.y - bounds.origin.y ) ) / |
| 2023 | ( bounds.size.height - 1 ); |
| 2024 | } |
| 2025 | #else |
| 2026 | msg.idata[2] = cursorX; |
| 2027 | msg.idata[3] = cursorY; |
| 2028 | msg.fdata[0] = scaledCursorX; |
| 2029 | msg.fdata[1] = scaledCursorY; |
| 2030 | #endif // __CK_HID_CURSOR_TRACK__ |
| 2031 | } // gewang: added |
| 2032 | break; |
| 2033 | |
| 2034 | case CK_HID_MOUSE_WHEEL: |
| 2035 | msg.eid = 0; |
| 2036 | |
| 2037 | if( element->num == 1 ) // "X" wheel motion |
| 2038 | { |
| 2039 | msg.idata[0] = event.value; |
| 2040 | msg.idata[1] = 0; |
| 2041 | } |
| 2042 | |
| 2043 | else // "Y" wheel motion - the default for single wheel systems |
| 2044 | { |
| 2045 | msg.idata[0] = 0; |
| 2046 | msg.idata[1] = event.value; |
| 2047 | } |
| 2048 | |
| 2049 | break; |
| 2050 | |
| 2051 | case CK_HID_BUTTON_DOWN: |
| 2052 | if( event.value == 0 ) |
| 2053 | msg.type = CK_HID_BUTTON_UP; |
| 2054 | |
| 2055 | msg.idata[0] = event.value; |
| 2056 | |
| 2057 | if( msg.device_type == CK_HID_DEV_KEYBOARD ) |
| 2058 | { |
| 2059 | msg.eid = element->usage; |
| 2060 | msg.idata[1] = element->usage; |
| 2061 | msg.idata[2] = Hid_hwkey_to_ascii( element->usage ); |
| 2062 | } |
| 2063 | |
| 2064 | break; |
| 2065 | } |
| 2066 | |
| 2067 | HidInManager::push_message( msg ); |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | static void Hid_do_operation( void * info ) |
| 2072 | { |
| 2073 | OSX_Hid_op op; |
| 2074 | |
| 2075 | while( hid_operation_buffer->get( &op, 1 ) ) |
| 2076 | { |
| 2077 | |
| 2078 | OSX_Hid_Device * device = NULL; |
| 2079 | |
| 2080 | switch( op.type ) |
| 2081 | { |
| 2082 | case OSX_Hid_op::open: |
| 2083 | if( op.index >= op.v->size() ) |
| 2084 | { |
| 2085 | EM_log( CK_LOG_HERALD, "hid: error: no such device %i", op.index ); |
| 2086 | return; |
| 2087 | } |
| 2088 | |
| 2089 | device = op.v->at( op.index ); |
| 2090 | |
| 2091 | if( device->refcount == 0 ) |
| 2092 | device->start(); |
| 2093 | |
| 2094 | device->refcount++; |
| 2095 | |
| 2096 | break; |
| 2097 | |
| 2098 | case OSX_Hid_op::close: |
| 2099 | if( op.index >= op.v->size() ) |
| 2100 | { |
| 2101 | EM_log( CK_LOG_HERALD, "hid: error: no such device %i", op.index ); |
| 2102 | return; |
| 2103 | } |
| 2104 | |
| 2105 | device = op.v->at( op.index ); |
| 2106 | |
| 2107 | device->refcount--; |
| 2108 | |
| 2109 | if( device->refcount == 0 ) |
| 2110 | device->stop(); |
| 2111 | |
| 2112 | break; |
| 2113 | } |
| 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | void Hid_quit() |
| 2118 | { |
| 2119 | // stop the run loop; since thread_going is FALSE, the poll thread will exit |
| 2120 | if( rlHid ) |
| 2121 | CFRunLoopStop( rlHid ); |
| 2122 | |
| 2123 | if( hidOpSource ) |
| 2124 | { |
| 2125 | CFRunLoopRemoveSource( rlHid, hidOpSource, kCFRunLoopChuckHidMode ); |
| 2126 | CFRelease( hidOpSource ); |
| 2127 | hidOpSource = NULL; |
| 2128 | } |
| 2129 | |
| 2130 | if( newDeviceNotificationPort ) |
| 2131 | { |
| 2132 | IONotificationPortDestroy( newDeviceNotificationPort ); |
| 2133 | newDeviceNotificationPort = NULL; |
| 2134 | } |
| 2135 | |
| 2136 | rlHid = NULL; |
| 2137 | |
| 2138 | #ifdef __CK_HID_CURSOR_TRACK__ |
| 2139 | Mouse_stop_cursor_track(); |
| 2140 | #endif // __CK_HID_CURSOR_TRACK__ |
| 2141 | } |
| 2142 | |
| 2143 | void Hid_quit2() |
| 2144 | { |
| 2145 | if( g_hid_init == FALSE ) |
| 2146 | return; |
| 2147 | g_hid_init = FALSE; |
| 2148 | |
| 2149 | delete hid_operation_buffer; |
| 2150 | |
| 2151 | xvector< OSX_Hid_Device * >::size_type i, len; |
| 2152 | |
| 2153 | len = joysticks->size(); |
| 2154 | for( i = 0; i < len; i++ ) |
| 2155 | delete joysticks->at( i ); |
| 2156 | |
| 2157 | delete joysticks; |
| 2158 | joysticks = NULL; |
| 2159 | |
| 2160 | len = mice->size(); |
| 2161 | for( i = 0; i < len; i++ ) |
| 2162 | delete mice->at( i ); |
| 2163 | |
| 2164 | delete mice; |
| 2165 | mice = NULL; |
| 2166 | |
| 2167 | len = keyboards->size(); |
| 2168 | for( i = 0; i < len; i++ ) |
| 2169 | delete keyboards->at( i ); |
| 2170 | |
| 2171 | delete keyboards; |
| 2172 | keyboards = NULL; |
| 2173 | |
| 2174 | // TODO: delete xmultimaps |
| 2175 | } |
| 2176 | |
| 2177 | int Hid_count( xvector< OSX_Hid_Device * > * v ) |
| 2178 | { |
| 2179 | if( v == NULL ) |
| 2180 | return 0; |
| 2181 | |
| 2182 | v->lock(); |
| 2183 | |
| 2184 | int count = v->size(); |
| 2185 | |
| 2186 | v->unlock(); |
| 2187 | |
| 2188 | return count; |
| 2189 | } |
| 2190 | |
| 2191 | int Hid_count_elements( xvector< OSX_Hid_Device * > * v, int i, int type ) |
| 2192 | { |
| 2193 | if( v == NULL || i < 0 ) |
| 2194 | return -1; |
| 2195 | |
| 2196 | int r = 0; |
| 2197 | |
| 2198 | v->lock(); |
| 2199 | |
| 2200 | if( i >= v->size() ) |
| 2201 | { |
| 2202 | v->unlock(); |
| 2203 | return -1; |
| 2204 | } |
| 2205 | |
| 2206 | OSX_Hid_Device * device = v->at( i ); |
| 2207 | |
| 2208 | device->lock(); |
| 2209 | |
| 2210 | v->unlock(); |
| 2211 | |
| 2212 | switch( type ) |
| 2213 | { |
| 2214 | case CK_HID_JOYSTICK_AXIS: |
| 2215 | if( device->type == CK_HID_DEV_JOYSTICK ) |
| 2216 | r = device->axes; |
| 2217 | break; |
| 2218 | |
| 2219 | case CK_HID_BUTTON_DOWN: |
| 2220 | case CK_HID_BUTTON_UP: |
| 2221 | r = device->buttons; |
| 2222 | break; |
| 2223 | |
| 2224 | case CK_HID_JOYSTICK_HAT: |
| 2225 | r = device->hats; |
| 2226 | break; |
| 2227 | |
| 2228 | case CK_HID_JOYSTICK_BALL: |
| 2229 | r = 0; |
| 2230 | break; |
| 2231 | |
| 2232 | case CK_HID_MOUSE_MOTION: |
| 2233 | if( device->type == CK_HID_DEV_MOUSE ) |
| 2234 | r = device->axes; |
| 2235 | break; |
| 2236 | |
| 2237 | case CK_HID_MOUSE_WHEEL: |
| 2238 | if( device->type == CK_HID_DEV_MOUSE ) |
| 2239 | r = device->wheels; |
| 2240 | break; |
| 2241 | |
| 2242 | case CK_HID_LED: |
| 2243 | if( ( *device->outputs )[type] ) |
| 2244 | r = ( *device->outputs )[type]->size(); |
| 2245 | break; |
| 2246 | } |
| 2247 | |
| 2248 | device->unlock(); |
| 2249 | |
| 2250 | return r; |
| 2251 | } |
| 2252 | |
| 2253 | int Hid_open( xvector< OSX_Hid_Device * > * v, int i ) |
| 2254 | { |
| 2255 | if( v == NULL || i < 0 ) |
| 2256 | return -1; |
| 2257 | |
| 2258 | v->lock(); |
| 2259 | |
| 2260 | if( i >= v->size() ) |
| 2261 | { |
| 2262 | v->unlock(); |
| 2263 | return -1; |
| 2264 | } |
| 2265 | |
| 2266 | v->unlock(); |
| 2267 | |
| 2268 | OSX_Hid_op op; |
| 2269 | op.type = OSX_Hid_op::open; |
| 2270 | op.v = v; |
| 2271 | op.index = i; |
| 2272 | |
| 2273 | hid_operation_buffer->put( &op, 1 ); |
| 2274 | |
| 2275 | if( hidOpSource && rlHid ) |
| 2276 | { |
| 2277 | CFRunLoopSourceSignal( hidOpSource ); |
| 2278 | CFRunLoopWakeUp( rlHid ); |
| 2279 | } |
| 2280 | |
| 2281 | return 0; |
| 2282 | } |
| 2283 | |
| 2284 | int Hid_close( xvector< OSX_Hid_Device * > * v, int i ) |
| 2285 | { |
| 2286 | if( v == NULL || i < 0 ) |
| 2287 | return -1; |
| 2288 | |
| 2289 | v->lock(); |
| 2290 | |
| 2291 | if( i >= v->size() ) |
| 2292 | { |
| 2293 | v->unlock(); |
| 2294 | return -1; |
| 2295 | } |
| 2296 | |
| 2297 | v->unlock(); |
| 2298 | |
| 2299 | OSX_Hid_op op; |
| 2300 | op.type = OSX_Hid_op::close; |
| 2301 | op.v = v; |
| 2302 | op.index = i; |
| 2303 | |
| 2304 | hid_operation_buffer->put( &op, 1 ); |
| 2305 | |
| 2306 | if( hidOpSource && rlHid ) |
| 2307 | { |
| 2308 | CFRunLoopSourceSignal( hidOpSource ); |
| 2309 | CFRunLoopWakeUp( rlHid ); |
| 2310 | } |
| 2311 | |
| 2312 | return 0; |
| 2313 | } |
| 2314 | |
| 2315 | const char * Hid_name( xvector< OSX_Hid_Device * > * v, int i ) |
| 2316 | { |
| 2317 | if( v == NULL || i < 0 ) |
| 2318 | return NULL; |
| 2319 | |
| 2320 | v->lock(); |
| 2321 | |
| 2322 | if( i >= v->size() ) |
| 2323 | { |
| 2324 | v->unlock(); |
| 2325 | return NULL; |
| 2326 | } |
| 2327 | |
| 2328 | OSX_Hid_Device * device = v->at( i ); |
| 2329 | |
| 2330 | v->unlock(); |
| 2331 | |
| 2332 | device->lock(); |
| 2333 | |
| 2334 | const char * name = device->name; |
| 2335 | |
| 2336 | device->unlock(); |
| 2337 | |
| 2338 | return name; |
| 2339 | } |
| 2340 | |
| 2341 | #pragma mark OS X Joystick support |
| 2342 | |
| 2343 | void Joystick_init() |
| 2344 | { |
| 2345 | Hid_init2(); |
| 2346 | } |
| 2347 | |
| 2348 | void Joystick_poll() |
| 2349 | { |
| 2350 | } |
| 2351 | |
| 2352 | void Joystick_quit() |
| 2353 | { |
| 2354 | Hid_quit2(); |
| 2355 | } |
| 2356 | |
| 2357 | int Joystick_count() |
| 2358 | { |
| 2359 | return Hid_count( joysticks ); |
| 2360 | } |
| 2361 | |
| 2362 | int Joystick_count_elements( int js, int type ) |
| 2363 | { |
| 2364 | return Hid_count_elements( joysticks, js, type ); |
| 2365 | } |
| 2366 | |
| 2367 | int Joystick_open( int js ) |
| 2368 | { |
| 2369 | return Hid_open( joysticks, js ); |
| 2370 | } |
| 2371 | |
| 2372 | int Joystick_close( int js ) |
| 2373 | { |
| 2374 | return Hid_close( joysticks, js ); |
| 2375 | } |
| 2376 | |
| 2377 | const char * Joystick_name( int js ) |
| 2378 | { |
| 2379 | return Hid_name( joysticks, js ); |
| 2380 | } |
| 2381 | |
| 2382 | #pragma mark OS X Mouse support |
| 2383 | |
| 2384 | void Mouse_init() |
| 2385 | { |
| 2386 | Hid_init2(); |
| 2387 | } |
| 2388 | |
| 2389 | void Mouse_poll() |
| 2390 | { |
| 2391 | } |
| 2392 | |
| 2393 | void Mouse_quit() |
| 2394 | { |
| 2395 | Hid_quit2(); |
| 2396 | } |
| 2397 | |
| 2398 | int Mouse_count() |
| 2399 | { |
| 2400 | return Hid_count( mice ); |
| 2401 | } |
| 2402 | |
| 2403 | int Mouse_count_elements( int m, int type ) |
| 2404 | { |
| 2405 | return Hid_count_elements( mice, m, type ); |
| 2406 | } |
| 2407 | |
| 2408 | int Mouse_open( int m ) |
| 2409 | { |
| 2410 | return Hid_open( mice, m ); |
| 2411 | } |
| 2412 | |
| 2413 | int Mouse_close( int m ) |
| 2414 | { |
| 2415 | return Hid_close( mice, m ); |
| 2416 | } |
| 2417 | |
| 2418 | const char * Mouse_name( int m ) |
| 2419 | { |
| 2420 | return Hid_name( mice, m ); |
| 2421 | } |
| 2422 | |
| 2423 | #ifdef __CK_HID_CURSOR_TRACK__ |
| 2424 | CGPoint CGEventGetLocation(CGEventRef event); |
| 2425 | |
| 2426 | CGEventRef Mouse_cursor_track_cb( CGEventTapProxy proxy, CGEventType type, |
| 2427 | CGEventRef event, void * refcon ) |
| 2428 | { |
| 2429 | CGPoint p = CGEventGetLocation( event ); |
| 2430 | |
| 2431 | cursorX = ( t_CKINT ) p.x; |
| 2432 | cursorY = ( t_CKINT ) p.y; |
| 2433 | |
| 2434 | CGDirectDisplayID display; |
| 2435 | CGDisplayCount displayCount; |
| 2436 | |
| 2437 | if( CGGetDisplaysWithPoint( p, 1, &display, &displayCount ) == |
| 2438 | kCGErrorSuccess ) |
| 2439 | { |
| 2440 | CGRect bounds = CGDisplayBounds( display ); |
| 2441 | |
| 2442 | scaledCursorX = ( ( t_CKFLOAT ) ( p.x - bounds.origin.x ) ) / |
| 2443 | ( bounds.size.width - 1 ); |
| 2444 | scaledCursorY = ( ( t_CKFLOAT ) ( p.y - bounds.origin.y ) ) / |
| 2445 | ( bounds.size.height - 1 ); |
| 2446 | } |
| 2447 | |
| 2448 | return event; |
| 2449 | } |
| 2450 | |
| 2451 | void * Mouse_cursor_track( void * ) |
| 2452 | { |
| 2453 | EM_log( CK_LOG_INFO, "hid: starting cursor track" ); |
| 2454 | |
| 2455 | cursorX = 0; |
| 2456 | cursorY = 0; |
| 2457 | |
| 2458 | rlCursorTrack = CFRunLoopGetCurrent(); |
| 2459 | |
| 2460 | CFMachPortRef machPort = CGEventTapCreate( kCGSessionEventTap, |
| 2461 | kCGHeadInsertEventTap, |
| 2462 | kCGEventTapOptionListenOnly, |
| 2463 | CGEventMaskBit( kCGEventMouseMoved ), |
| 2464 | Mouse_cursor_track_cb, NULL ); |
| 2465 | CFRunLoopSourceRef tapSource; |
| 2466 | if( machPort != NULL ) |
| 2467 | { |
| 2468 | tapSource = CFMachPortCreateRunLoopSource( NULL, machPort, 0 ); |
| 2469 | CFRunLoopAddSource( rlCursorTrack, tapSource, kCFRunLoopChuckHidMode ); |
| 2470 | } |
| 2471 | |
| 2472 | else |
| 2473 | { |
| 2474 | EM_log( CK_LOG_WARNING, "hid: cursor position listener startup failed" ); |
| 2475 | return 0; |
| 2476 | } |
| 2477 | |
| 2478 | while( g_ct_go ) |
| 2479 | CFRunLoopRunInMode( kCFRunLoopChuckHidMode, 60 * 60 * 24, FALSE ); |
| 2480 | |
| 2481 | CFRelease( machPort ); |
| 2482 | CFRelease( tapSource ); |
| 2483 | rlCursorTrack = NULL; |
| 2484 | |
| 2485 | EM_log( CK_LOG_INFO, "hid: stopping cursor track" ); |
| 2486 | |
| 2487 | return 0; |
| 2488 | } |
| 2489 | |
| 2490 | #endif // __CK_HID_CURSOR_TRACK__ |
| 2491 | |
| 2492 | int Mouse_start_cursor_track() |
| 2493 | { |
| 2494 | #ifdef __CK_HID_CURSOR_TRACK__ |
| 2495 | if( &CGEventTapCreate == NULL ) |
| 2496 | return -1; |
| 2497 | |
| 2498 | if( g_ct_go ) |
| 2499 | return 0; |
| 2500 | |
| 2501 | g_ct_go = TRUE; |
| 2502 | pthread_t ct_thread; |
| 2503 | |
| 2504 | if( pthread_create( &ct_thread, NULL, Mouse_cursor_track, NULL ) != 0 ) |
| 2505 | { |
| 2506 | EM_log( CK_LOG_WARNING, "hid: cursor track thread failed to start" ); |
| 2507 | return -1; |
| 2508 | } |
| 2509 | |
| 2510 | return 0; |
| 2511 | #else |
| 2512 | return -1; |
| 2513 | #endif // __CK_HID_CURSOR_TRACK__ |
| 2514 | } |
| 2515 | |
| 2516 | int Mouse_stop_cursor_track() |
| 2517 | { |
| 2518 | #ifdef __CK_HID_CURSOR_TRACK__ |
| 2519 | if( g_ct_go ) |
| 2520 | { |
| 2521 | g_ct_go = FALSE; |
| 2522 | |
| 2523 | if( rlCursorTrack ) |
| 2524 | CFRunLoopStop( rlCursorTrack ); |
| 2525 | |
| 2526 | cursorX = 0; |
| 2527 | cursorY = 0; |
| 2528 | } |
| 2529 | #endif // __CK_HID_CURSOR_TRACK__ |
| 2530 | return 0; |
| 2531 | } |
| 2532 | |
| 2533 | //#endif /* __CK_HID_CURSORTRACK__ */ |
| 2534 | |
| 2535 | #pragma mark OS X Keyboard support |
| 2536 | void Keyboard_init() |
| 2537 | { |
| 2538 | Hid_init2(); |
| 2539 | } |
| 2540 | |
| 2541 | void Keyboard_poll() |
| 2542 | { |
| 2543 | |
| 2544 | } |
| 2545 | |
| 2546 | void Keyboard_quit() |
| 2547 | { |
| 2548 | Hid_quit2(); |
| 2549 | } |
| 2550 | |
| 2551 | int Keyboard_count() |
| 2552 | { |
| 2553 | return Hid_count( keyboards ); |
| 2554 | } |
| 2555 | |
| 2556 | int Keyboard_count_elements( int k, int type ) |
| 2557 | { |
| 2558 | return Hid_count_elements( keyboards, k, type ); |
| 2559 | } |
| 2560 | |
| 2561 | int Keyboard_open( int k ) |
| 2562 | { |
| 2563 | return Hid_open( keyboards, k ); |
| 2564 | } |
| 2565 | |
| 2566 | int Keyboard_close( int k ) |
| 2567 | { |
| 2568 | return Hid_close( keyboards, k ); |
| 2569 | } |
| 2570 | |
| 2571 | int Keyboard_send( int k, const HidMsg * msg ) |
| 2572 | { |
| 2573 | if( keyboards == NULL || k < 0 || k >= keyboards->size() ) |
| 2574 | return -1; |
| 2575 | |
| 2576 | OSX_Hid_Device * keyboard = ( *keyboards )[k]; |
| 2577 | |
| 2578 | if( keyboard->outputs == NULL || // no outputs |
| 2579 | keyboard->outputs->find( msg->type ) == keyboard->outputs->end() ) |
| 2580 | // no outputs of that type |
| 2581 | return -1; |
| 2582 | |
| 2583 | if( msg->eid < 0 || msg->eid >= ( *( keyboard->outputs ) )[msg->type]->size() ) |
| 2584 | // invalid output type element number |
| 2585 | return -1; |
| 2586 | |
| 2587 | OSX_Hid_Device_Element * output = ( *( *( keyboard->outputs ) )[msg->type] )[msg->eid]; |
| 2588 | |
| 2589 | IOHIDEventStruct eventStruct; |
| 2590 | |
| 2591 | eventStruct.type = kIOHIDElementTypeOutput; |
| 2592 | eventStruct.elementCookie = output->cookie; |
| 2593 | // eventStruct.timestamp = UpTime(); // deprecated in macOS |
| 2594 | eventStruct.timestamp = ck_macOS_uptime(); // 1.5.0.5 (ge) updated |
| 2595 | eventStruct.longValueSize = 0; |
| 2596 | eventStruct.longValue = NULL; |
| 2597 | |
| 2598 | if( msg->idata[0] < output->min ) |
| 2599 | eventStruct.value = output->min; |
| 2600 | else if( msg->idata[0] > output->max ) |
| 2601 | eventStruct.value = output->max; |
| 2602 | else |
| 2603 | eventStruct.value = msg->idata[0]; |
| 2604 | |
| 2605 | IOReturn result; |
| 2606 | result = ( *( keyboard->interface ) )->setElementValue( keyboard->interface, |
| 2607 | output->cookie, |
| 2608 | &eventStruct, |
| 2609 | 0, 0, 0, 0 ); |
| 2610 | if( result != kIOReturnSuccess ) |
| 2611 | return -1; |
| 2612 | |
| 2613 | return 0; |
| 2614 | } |
| 2615 | |
| 2616 | const char * Keyboard_name( int k ) |
| 2617 | { |
| 2618 | return Hid_name( keyboards, k ); |
| 2619 | } |
| 2620 | |
| 2621 | #pragma mark OS X Tilt/Sudden Motion Sensor support |
| 2622 | |
| 2623 | enum |
| 2624 | { |
| 2625 | kSMSPowerbookDataType, |
| 2626 | kSMSMacBookProDataType |
| 2627 | }; |
| 2628 | |
| 2629 | static struct t_TiltSensor_data |
| 2630 | { |
| 2631 | union |
| 2632 | { |
| 2633 | struct t_powerbook |
| 2634 | { |
| 2635 | int8_t x; |
| 2636 | int8_t y; |
| 2637 | int8_t z; |
| 2638 | int8_t pad[57]; |
| 2639 | } powerbook; |
| 2640 | |
| 2641 | struct t_macbookpro |
| 2642 | { |
| 2643 | int16_t x; |
| 2644 | int16_t y; |
| 2645 | int16_t z; |
| 2646 | int8_t pad[34]; |
| 2647 | } macbookpro; |
| 2648 | } data; |
| 2649 | |
| 2650 | int kernFunc; |
| 2651 | char * servMatch; |
| 2652 | int dataType; |
| 2653 | io_connect_t dataPort; |
| 2654 | |
| 2655 | int detected; // 0 = detection not yet run, -1 = no sensor found, 1 = sensor found |
| 2656 | int refcount; |
| 2657 | |
| 2658 | t_TiltSensor_data() |
| 2659 | { |
| 2660 | refcount = 0; |
| 2661 | kernFunc = 0; |
| 2662 | servMatch = NULL; |
| 2663 | dataType = -1; |
| 2664 | dataPort = 0; |
| 2665 | detected = 0; |
| 2666 | } |
| 2667 | |
| 2668 | } TiltSensor_data; |
| 2669 | |
| 2670 | // ge: SMS multi-threading |
| 2671 | static int TiltSensor_do_read(); |
| 2672 | |
| 2673 | //----------------------------------------------------------------------------- |
| 2674 | // name: class SMSManager |
| 2675 | // desc: ... |
| 2676 | //----------------------------------------------------------------------------- |
| 2677 | class SMSManager |
| 2678 | { |
| 2679 | public: |
| 2680 | static t_CKBOOL init(); |
| 2681 | static void shutdown(); |
| 2682 | static void on(); |
| 2683 | static void off(); |
| 2684 | |
| 2685 | public: |
| 2686 | static t_CKINT the_onoff; |
| 2687 | static t_CKBOOL the_init; |
| 2688 | static t_CKINT wait_time; |
| 2689 | static XThread * the_thread; |
| 2690 | static t_CKINT x; |
| 2691 | static t_CKINT y; |
| 2692 | static t_CKINT z; |
| 2693 | }; |
| 2694 | |
| 2695 | // designate new poll rate |
| 2696 | t_CKINT TiltSensor_setPollRate( t_CKINT usec ) |
| 2697 | { |
| 2698 | // sanity |
| 2699 | assert( usec >= 0 ); |
| 2700 | |
| 2701 | SMSManager::wait_time = usec; |
| 2702 | |
| 2703 | return SMSManager::wait_time; |
| 2704 | } |
| 2705 | |
| 2706 | // query current poll rate |
| 2707 | t_CKINT TiltSensor_getPollRate( ) |
| 2708 | { |
| 2709 | return SMSManager::wait_time; |
| 2710 | } |
| 2711 | |
| 2712 | // initialize |
| 2713 | t_CKINT SMSManager::the_onoff = 0; |
| 2714 | t_CKBOOL SMSManager::the_init = FALSE; |
| 2715 | t_CKINT SMSManager::wait_time = 3000; |
| 2716 | XThread * SMSManager::the_thread = NULL; |
| 2717 | t_CKINT SMSManager::x = 0; |
| 2718 | t_CKINT SMSManager::y = 0; |
| 2719 | t_CKINT SMSManager::z = 0; |
| 2720 | |
| 2721 | |
| 2722 | #if !defined(__PLATFORM_WINDOWS__) || defined(__WINDOWS_PTHREAD__) |
| 2723 | static void * sms_loop( void * ) |
| 2724 | #else |
| 2725 | static unsigned int __stdcall sms_loop( void * ) |
| 2726 | #endif |
| 2727 | { |
| 2728 | // t_CKINT c; |
| 2729 | EM_log( CK_LOG_INFO, "starting SMS multi-threaded loop..." ); |
| 2730 | |
| 2731 | // go |
| 2732 | while( SMSManager::the_init ) |
| 2733 | { |
| 2734 | // if on |
| 2735 | if( SMSManager::the_onoff ) |
| 2736 | { |
| 2737 | // poll SMS |
| 2738 | TiltSensor_do_read(); |
| 2739 | |
| 2740 | // save data |
| 2741 | if( TiltSensor_data.dataType == kSMSPowerbookDataType ) |
| 2742 | { |
| 2743 | SMSManager::x = TiltSensor_data.data.powerbook.x; |
| 2744 | SMSManager::y = TiltSensor_data.data.powerbook.y; |
| 2745 | SMSManager::z = TiltSensor_data.data.powerbook.z; |
| 2746 | } |
| 2747 | |
| 2748 | else if( TiltSensor_data.dataType == kSMSMacBookProDataType ) |
| 2749 | { |
| 2750 | SMSManager::x = TiltSensor_data.data.macbookpro.x; |
| 2751 | SMSManager::y = TiltSensor_data.data.macbookpro.y; |
| 2752 | SMSManager::z = TiltSensor_data.data.macbookpro.z; |
| 2753 | } |
| 2754 | // wait |
| 2755 | ck_usleep( SMSManager::wait_time ); |
| 2756 | } |
| 2757 | else |
| 2758 | { |
| 2759 | // wait |
| 2760 | ck_usleep( 1000 ); |
| 2761 | } |
| 2762 | } |
| 2763 | |
| 2764 | // TODO: hack! |
| 2765 | CK_SAFE_DELETE( SMSManager::the_thread ); |
| 2766 | |
| 2767 | return 0; |
| 2768 | } |
| 2769 | |
| 2770 | |
| 2771 | // init |
| 2772 | t_CKBOOL SMSManager::init() |
| 2773 | { |
| 2774 | // sanity |
| 2775 | if( the_thread != NULL ) |
| 2776 | return FALSE; |
| 2777 | |
| 2778 | EM_log( CK_LOG_INFO, "initializing SMSManager..." ); |
| 2779 | |
| 2780 | the_onoff = 0; |
| 2781 | the_init = TRUE; |
| 2782 | the_thread = new XThread; |
| 2783 | the_thread->start( sms_loop, NULL ); |
| 2784 | |
| 2785 | return TRUE; |
| 2786 | } |
| 2787 | |
| 2788 | |
| 2789 | // shutdown |
| 2790 | void SMSManager::shutdown() |
| 2791 | { |
| 2792 | EM_log( CK_LOG_INFO, "shutting down SMSManager..." ); |
| 2793 | |
| 2794 | the_onoff = 0; |
| 2795 | the_init = FALSE; |
| 2796 | } |
| 2797 | |
| 2798 | |
| 2799 | // on() |
| 2800 | void SMSManager::on() |
| 2801 | { |
| 2802 | the_onoff++; |
| 2803 | } |
| 2804 | |
| 2805 | |
| 2806 | // off() |
| 2807 | void SMSManager::off() |
| 2808 | { |
| 2809 | the_onoff--; |
| 2810 | } |
| 2811 | |
| 2812 | |
| 2813 | static int TiltSensor_test( int kernFunc, const char * servMatch, int dataType ) |
| 2814 | { |
| 2815 | kern_return_t result; |
| 2816 | io_iterator_t iterator; |
| 2817 | io_object_t aDevice; |
| 2818 | io_connect_t dataPort; |
| 2819 | |
| 2820 | IOItemCount structureInputSize; |
| 2821 | size_t structureOutputSize; |
| 2822 | |
| 2823 | // log |
| 2824 | EM_log( CK_LOG_FINE, "testing for SMS sensor..." ); |
| 2825 | |
| 2826 | CFMutableDictionaryRef matchingDictionary = IOServiceMatching( servMatch ); |
| 2827 | |
| 2828 | // 1.5.0.7 (ge) updated kIOMasterPortDefault to kIOMainPortDefault |
| 2829 | #if __MAC_OS_X_VERSION_MIN_REQUIRED < 120000 // before macOS 12 |
| 2830 | result = IOServiceGetMatchingServices( kIOMasterPortDefault, matchingDictionary, &iterator ); |
| 2831 | #else |
| 2832 | result = IOServiceGetMatchingServices( kIOMainPortDefault, matchingDictionary, &iterator ); |
| 2833 | #endif |
| 2834 | |
| 2835 | if( result != KERN_SUCCESS ) |
| 2836 | return 0; |
| 2837 | |
| 2838 | aDevice = IOIteratorNext( iterator ); |
| 2839 | IOObjectRelease( iterator ); |
| 2840 | |
| 2841 | if( aDevice == 0 ) |
| 2842 | return 0; |
| 2843 | |
| 2844 | result = IOServiceOpen( aDevice, mach_task_self(), 0, &dataPort ); |
| 2845 | IOObjectRelease( aDevice ); |
| 2846 | |
| 2847 | if ( result != KERN_SUCCESS ) |
| 2848 | return 0; |
| 2849 | |
| 2850 | switch( dataType ) |
| 2851 | { |
| 2852 | case kSMSPowerbookDataType: |
| 2853 | structureInputSize = sizeof( TiltSensor_data.data.powerbook ); |
| 2854 | structureOutputSize = sizeof( TiltSensor_data.data.powerbook ); |
| 2855 | break; |
| 2856 | |
| 2857 | case kSMSMacBookProDataType: |
| 2858 | structureInputSize = sizeof( TiltSensor_data.data.macbookpro ); |
| 2859 | structureOutputSize = sizeof( TiltSensor_data.data.macbookpro ); |
| 2860 | break; |
| 2861 | |
| 2862 | default: |
| 2863 | IOServiceClose( dataPort ); |
| 2864 | return 0; |
| 2865 | } |
| 2866 | |
| 2867 | memset( &TiltSensor_data.data, 0, sizeof( TiltSensor_data.data ) ); |
| 2868 | memset( &TiltSensor_data.data, 0, sizeof( TiltSensor_data.data ) ); |
| 2869 | |
| 2870 | // result = IOConnectMethodStructureIStructureO( dataPort, |
| 2871 | // kernFunc, |
| 2872 | // structureInputSize, |
| 2873 | // &structureOutputSize, |
| 2874 | // &TiltSensor_data.data, |
| 2875 | // &TiltSensor_data.data ); |
| 2876 | result = IOConnectCallStructMethod( dataPort, |
| 2877 | kernFunc, |
| 2878 | &TiltSensor_data.data, |
| 2879 | structureInputSize, |
| 2880 | &TiltSensor_data.data, |
| 2881 | &structureOutputSize ); |
| 2882 | |
| 2883 | if ( result != KERN_SUCCESS ) |
| 2884 | { |
| 2885 | IOServiceClose( dataPort ); |
| 2886 | return 0; |
| 2887 | } |
| 2888 | |
| 2889 | // leave dataPort open for future use |
| 2890 | TiltSensor_data.dataPort = dataPort; |
| 2891 | |
| 2892 | return 1; |
| 2893 | } |
| 2894 | |
| 2895 | static int TiltSensor_do_read() |
| 2896 | { |
| 2897 | kern_return_t result; |
| 2898 | IOItemCount structureInputSize; |
| 2899 | size_t structureOutputSize; |
| 2900 | |
| 2901 | // log |
| 2902 | EM_log( CK_LOG_FINE, "reading SMS sensor..." ); |
| 2903 | |
| 2904 | switch( TiltSensor_data.dataType ) |
| 2905 | { |
| 2906 | case kSMSPowerbookDataType: |
| 2907 | structureInputSize = sizeof( TiltSensor_data.data.powerbook ); |
| 2908 | structureOutputSize = sizeof( TiltSensor_data.data.powerbook ); |
| 2909 | break; |
| 2910 | |
| 2911 | case kSMSMacBookProDataType: |
| 2912 | structureInputSize = sizeof( TiltSensor_data.data.macbookpro ); |
| 2913 | structureOutputSize = sizeof( TiltSensor_data.data.macbookpro ); |
| 2914 | break; |
| 2915 | |
| 2916 | default: |
| 2917 | return 0; |
| 2918 | } |
| 2919 | |
| 2920 | memset( &TiltSensor_data.data, 0, sizeof( TiltSensor_data.data ) ); |
| 2921 | memset( &TiltSensor_data.data, 0, sizeof( TiltSensor_data.data ) ); |
| 2922 | |
| 2923 | // result = IOConnectMethodStructureIStructureO( TiltSensor_data.dataPort, |
| 2924 | // TiltSensor_data.kernFunc, |
| 2925 | // structureInputSize, |
| 2926 | // &structureOutputSize, |
| 2927 | // &TiltSensor_data.data, |
| 2928 | // &TiltSensor_data.data ); |
| 2929 | result = IOConnectCallStructMethod( TiltSensor_data.dataPort, |
| 2930 | TiltSensor_data.kernFunc, |
| 2931 | &TiltSensor_data.data, |
| 2932 | structureInputSize, |
| 2933 | &TiltSensor_data.data, |
| 2934 | &structureOutputSize ); |
| 2935 | |
| 2936 | return 1; |
| 2937 | } |
| 2938 | |
| 2939 | static int TiltSensor_detect() |
| 2940 | { |
| 2941 | // log |
| 2942 | EM_log( CK_LOG_FINE, "detecting SMS sensor..." ); |
| 2943 | |
| 2944 | // try different interfaces until we find one that works |
| 2945 | int powerbookKernFunc = 0; |
| 2946 | |
| 2947 | // get macOS release version | 1.5.0.5 (ge) new way |
| 2948 | ck_OSVersion v = ck_macOS_version(); |
| 2949 | // check 10.4.[0,3] |
| 2950 | if( v.major == 10 & v.minor == 4 && v.patch <= 3 ) powerbookKernFunc = 24; |
| 2951 | else powerbookKernFunc = 21; |
| 2952 | |
| 2953 | // old way |
| 2954 | // SInt32 osx_version = 0; |
| 2955 | // Gestalt( gestaltSystemVersion, &osx_version ); |
| 2956 | // if( osx_version == 0x1040 || |
| 2957 | // osx_version == 0x1041 || |
| 2958 | // osx_version == 0x1042 || |
| 2959 | // osx_version == 0x1043 ) |
| 2960 | // powerbookKernFunc = 24; |
| 2961 | // else |
| 2962 | // powerbookKernFunc = 21; |
| 2963 | |
| 2964 | // 1.3.1.0: added cast to t_CKINT |
| 2965 | // CK_FPRINTF_STDOUT( "osx_version = %ld \n", (t_CKINT)osx_version ); |
| 2966 | |
| 2967 | // ibook/powerbook (OS X 10.4.x) tilt sensor interface |
| 2968 | if( TiltSensor_test( powerbookKernFunc, "IOI2CMotionSensor", kSMSPowerbookDataType ) ) |
| 2969 | { |
| 2970 | TiltSensor_data.kernFunc = powerbookKernFunc; |
| 2971 | TiltSensor_data.dataType = kSMSPowerbookDataType; |
| 2972 | TiltSensor_data.detected = 1; |
| 2973 | return 1; |
| 2974 | } |
| 2975 | |
| 2976 | // hi resolution powerbook tilt sensor interface |
| 2977 | if( TiltSensor_test( powerbookKernFunc, "PMUMotionSensor", kSMSPowerbookDataType ) ) |
| 2978 | { |
| 2979 | TiltSensor_data.kernFunc = powerbookKernFunc; |
| 2980 | TiltSensor_data.dataType = kSMSPowerbookDataType; |
| 2981 | TiltSensor_data.detected = 1; |
| 2982 | return 1; |
| 2983 | } |
| 2984 | |
| 2985 | // mac book (pro) tilt sensor interface |
| 2986 | if( TiltSensor_test( 5, "SMCMotionSensor", kSMSMacBookProDataType ) ) |
| 2987 | { |
| 2988 | TiltSensor_data.kernFunc = 5; |
| 2989 | TiltSensor_data.dataType = kSMSMacBookProDataType; |
| 2990 | TiltSensor_data.detected = 1; |
| 2991 | return 1; |
| 2992 | } |
| 2993 | |
| 2994 | TiltSensor_data.detected = -1; |
| 2995 | |
| 2996 | return 0; |
| 2997 | } |
| 2998 | |
| 2999 | void TiltSensor_init() |
| 3000 | { |
| 3001 | |
| 3002 | } |
| 3003 | |
| 3004 | void TiltSensor_quit() |
| 3005 | { |
| 3006 | // log |
| 3007 | EM_log( CK_LOG_FINE, "quiting SMS bridge..." ); |
| 3008 | |
| 3009 | if( TiltSensor_data.dataPort == 0 ) |
| 3010 | IOServiceClose( TiltSensor_data.dataPort ); |
| 3011 | } |
| 3012 | |
| 3013 | void TiltSensor_probe() |
| 3014 | { |
| 3015 | |
| 3016 | } |
| 3017 | |
| 3018 | int TiltSensor_count() |
| 3019 | { |
| 3020 | // log |
| 3021 | EM_log( CK_LOG_FINE, "counting SMS sensors..." ); |
| 3022 | |
| 3023 | if( TiltSensor_data.detected == 0 ) |
| 3024 | TiltSensor_detect(); |
| 3025 | |
| 3026 | if( TiltSensor_data.detected == -1 ) |
| 3027 | return 0; |
| 3028 | |
| 3029 | else if( TiltSensor_data.detected == 1 ) |
| 3030 | return 1; |
| 3031 | |
| 3032 | return 0; |
| 3033 | } |
| 3034 | |
| 3035 | int TiltSensor_open( int ts ) |
| 3036 | { |
| 3037 | // log |
| 3038 | EM_log( CK_LOG_FINE, "opening SMS sensor..." ); |
| 3039 | EM_pushlog(); |
| 3040 | |
| 3041 | if( TiltSensor_data.detected == 0 ) |
| 3042 | TiltSensor_detect(); |
| 3043 | |
| 3044 | if( TiltSensor_data.detected == -1 ) |
| 3045 | return -1; |
| 3046 | |
| 3047 | TiltSensor_data.refcount++; |
| 3048 | |
| 3049 | // ge: init manager |
| 3050 | SMSManager::init(); |
| 3051 | SMSManager::on(); |
| 3052 | |
| 3053 | // log |
| 3054 | EM_poplog(); |
| 3055 | |
| 3056 | return 0; |
| 3057 | } |
| 3058 | |
| 3059 | int TiltSensor_close( int ts ) |
| 3060 | { |
| 3061 | TiltSensor_data.refcount--; |
| 3062 | |
| 3063 | // ge: notify |
| 3064 | SMSManager::off(); |
| 3065 | if( TiltSensor_data.refcount <= 0 ) |
| 3066 | SMSManager::shutdown(); |
| 3067 | |
| 3068 | return 0; |
| 3069 | } |
| 3070 | |
| 3071 | const char * TiltSensor_name( int ts ) |
| 3072 | { |
| 3073 | return "Apple Sudden Motion Sensor"; |
| 3074 | } |
| 3075 | |
| 3076 | int TiltSensor_read( int ts, int type, int num, HidMsg * msg ) |
| 3077 | { |
| 3078 | if( type != CK_HID_ACCELEROMETER ) |
| 3079 | return -1; |
| 3080 | |
| 3081 | if( TiltSensor_data.detected == -1 ) |
| 3082 | return -1; |
| 3083 | |
| 3084 | // ge: no longer need in the multi-threaded case... |
| 3085 | // if( !TiltSensor_do_read() ) |
| 3086 | // return -1; |
| 3087 | |
| 3088 | // ge: use sms multi-threaded |
| 3089 | msg->idata[0] = SMSManager::x; |
| 3090 | msg->idata[1] = SMSManager::y; |
| 3091 | msg->idata[2] = SMSManager::z; |
| 3092 | |
| 3093 | return 0; |
| 3094 | } |
| 3095 | |
| 3096 | |
| 3097 | typedef struct { float x,y; } mtPoint; |
| 3098 | typedef struct { mtPoint pos,vel; } mtReadout; |
| 3099 | |
| 3100 | typedef struct { |
| 3101 | int frame; |
| 3102 | double timestamp; |
| 3103 | int identifier, state, foo3, foo4; |
| 3104 | mtReadout normalized; |
| 3105 | float size; |
| 3106 | int zero1; |
| 3107 | float angle, majorAxis, minorAxis; // ellipsoid |
| 3108 | mtReadout mm; |
| 3109 | int zero2[2]; |
| 3110 | float unk2; |
| 3111 | } Finger; |
| 3112 | |
| 3113 | typedef void *MTDeviceRef; |
| 3114 | typedef int (*MTContactCallbackFunction)(MTDeviceRef,Finger*,int,double,int); |
| 3115 | |
| 3116 | extern "C" |
| 3117 | { |
| 3118 | // MTDeviceRef MTDeviceCreateDefault(); |
| 3119 | CFMutableArrayRef MTDeviceCreateList(void); |
| 3120 | void MTRegisterContactFrameCallback(MTDeviceRef, MTContactCallbackFunction); |
| 3121 | void MTUnregisterContactFrameCallback(MTDeviceRef, MTContactCallbackFunction); |
| 3122 | void MTDeviceStart(MTDeviceRef, int); |
| 3123 | void MTDeviceStop(MTDeviceRef); |
| 3124 | } |
| 3125 | |
| 3126 | |
| 3127 | class MTDManager |
| 3128 | { |
| 3129 | public: |
| 3130 | static bool checkedAvailability; |
| 3131 | static bool available; |
| 3132 | |
| 3133 | static bool inited; |
| 3134 | static int numDevices; |
| 3135 | static CFMutableArrayRef deviceList; |
| 3136 | static map<MTDeviceRef, bool> openDevices; |
| 3137 | }; |
| 3138 | |
| 3139 | |
| 3140 | bool MTDManager::checkedAvailability = false; |
| 3141 | bool MTDManager::available = false; |
| 3142 | bool MTDManager::inited = false; |
| 3143 | int MTDManager::numDevices = -1; |
| 3144 | CFMutableArrayRef MTDManager::deviceList = NULL; |
| 3145 | map<MTDeviceRef, bool> MTDManager::openDevices; |
| 3146 | |
| 3147 | |
| 3148 | int MultiTouchDevice_callback(MTDeviceRef device, Finger *data, int nFingers, double timestamp, int frame) |
| 3149 | { |
| 3150 | int device_num = CFArrayGetFirstIndexOfValue(MTDManager::deviceList, |
| 3151 | CFRangeMake(0, MTDManager::numDevices), |
| 3152 | device); |
| 3153 | |
| 3154 | for (int i=0; i<nFingers; i++) |
| 3155 | { |
| 3156 | Finger *f = &data[i]; |
| 3157 | // printf("Frame %7d: Angle %6.2f, ellipse %6.3f x%6.3f; " |
| 3158 | // "position (%6.3f,%6.3f) vel (%6.3f,%6.3f) " |
| 3159 | // "ID %d, state %d [%d %d?] size %6.3f, %6.3f?\n", |
| 3160 | // f->frame, |
| 3161 | // f->angle * 90 / atan2(1,0), |
| 3162 | // f->majorAxis, |
| 3163 | // f->minorAxis, |
| 3164 | // f->normalized.pos.x, |
| 3165 | // f->normalized.pos.y, |
| 3166 | // f->normalized.vel.x, |
| 3167 | // f->normalized.vel.y, |
| 3168 | // f->identifier, f->state, f->foo3, f->foo4, |
| 3169 | // f->size, f->unk2); |
| 3170 | |
| 3171 | HidMsg msg; |
| 3172 | |
| 3173 | msg.device_type = CK_HID_DEV_MULTITOUCH; |
| 3174 | msg.device_num = device_num; |
| 3175 | msg.type = CK_HID_MULTITOUCH_TOUCH; |
| 3176 | msg.eid = f->identifier; |
| 3177 | msg.fdata[0] = f->normalized.pos.x; |
| 3178 | msg.fdata[1] = f->normalized.pos.y; |
| 3179 | msg.fdata[2] = f->size; |
| 3180 | |
| 3181 | HidInManager::push_message( msg ); |
| 3182 | } |
| 3183 | // |
| 3184 | // printf("\n"); |
| 3185 | // |
| 3186 | return 0; |
| 3187 | } |
| 3188 | |
| 3189 | |
| 3190 | void MultiTouchDevice_init() |
| 3191 | { |
| 3192 | if(!MTDManager::checkedAvailability) |
| 3193 | MTDManager::available = (&MTDeviceCreateList != NULL && |
| 3194 | &MTRegisterContactFrameCallback != NULL && |
| 3195 | &MTUnregisterContactFrameCallback != NULL && |
| 3196 | &MTDeviceStart != NULL && |
| 3197 | &MTDeviceStop != NULL); |
| 3198 | |
| 3199 | if(!MTDManager::available) |
| 3200 | return; |
| 3201 | |
| 3202 | if(MTDManager::inited == false) |
| 3203 | { |
| 3204 | MTDManager::deviceList = MTDeviceCreateList(); |
| 3205 | if(MTDManager::deviceList) |
| 3206 | { |
| 3207 | MTDManager::numDevices = CFArrayGetCount(MTDManager::deviceList); |
| 3208 | |
| 3209 | MTDManager::inited = true; |
| 3210 | } |
| 3211 | } |
| 3212 | } |
| 3213 | |
| 3214 | |
| 3215 | void MultiTouchDevice_quit() |
| 3216 | { |
| 3217 | if(!MTDManager::available) |
| 3218 | return; |
| 3219 | |
| 3220 | if(MTDManager::inited) |
| 3221 | { |
| 3222 | if(MTDManager::deviceList) |
| 3223 | { |
| 3224 | CFRelease(MTDManager::deviceList); |
| 3225 | MTDManager::deviceList = NULL; |
| 3226 | } |
| 3227 | MTDManager::numDevices = -1; |
| 3228 | MTDManager::inited = false; |
| 3229 | } |
| 3230 | } |
| 3231 | |
| 3232 | |
| 3233 | void MultiTouchDevice_probe() |
| 3234 | { |
| 3235 | } |
| 3236 | |
| 3237 | |
| 3238 | int MultiTouchDevice_count() |
| 3239 | { |
| 3240 | if(!MTDManager::available) |
| 3241 | return 0; |
| 3242 | |
| 3243 | return MTDManager::numDevices; |
| 3244 | } |
| 3245 | |
| 3246 | |
| 3247 | int MultiTouchDevice_open( int ts ) |
| 3248 | { |
| 3249 | if(!MTDManager::available) |
| 3250 | return -1; |
| 3251 | |
| 3252 | if(!MTDManager::inited || ts < 0 || ts >= MTDManager::numDevices) |
| 3253 | return -1; |
| 3254 | |
| 3255 | MTDeviceRef device = (MTDeviceRef) CFArrayGetValueAtIndex(MTDManager::deviceList, ts); |
| 3256 | MTRegisterContactFrameCallback(device, MultiTouchDevice_callback); |
| 3257 | MTDeviceStart(device, 0); |
| 3258 | |
| 3259 | MTDManager::openDevices[device] = true; |
| 3260 | |
| 3261 | return 0; |
| 3262 | } |
| 3263 | |
| 3264 | |
| 3265 | int MultiTouchDevice_close( int ts ) |
| 3266 | { |
| 3267 | if(!MTDManager::available) |
| 3268 | return -1; |
| 3269 | |
| 3270 | if(MTDManager::numDevices == -1 || ts < 0 || ts >= MTDManager::numDevices) |
| 3271 | return -1; |
| 3272 | |
| 3273 | MTDeviceRef device = (MTDeviceRef) CFArrayGetValueAtIndex(MTDManager::deviceList, ts); |
| 3274 | if(MTDManager::openDevices.count(device)) |
| 3275 | { |
| 3276 | MTDeviceStop(device); |
| 3277 | //MTUnregisterContactFrameCallback(device, MultiTouchDevice_callback); |
| 3278 | MTDManager::openDevices[device] = false; |
| 3279 | } |
| 3280 | |
| 3281 | return 0; |
| 3282 | } |
| 3283 | |
| 3284 | |
| 3285 | const char * MultiTouchDevice_name( int ts ) |
| 3286 | { |
| 3287 | if(!MTDManager::available) |
| 3288 | return NULL; |
| 3289 | |
| 3290 | return "MultiTouch Device"; |
| 3291 | } |
| 3292 | |
| 3293 | |
| 3294 | class TabletManager |
| 3295 | { |
| 3296 | public: |
| 3297 | |
| 3298 | |
| 3299 | |
| 3300 | CGEventRef TapCallBack(CGEventTapProxy proxy, CGEventType type, |
| 3301 | CGEventRef event) |
| 3302 | { |
| 3303 | HidMsg msg; |
| 3304 | |
| 3305 | // msg.device_type = CK_HID_DEV_MULTITOUCH; |
| 3306 | // msg.device_num = device_num; |
| 3307 | // msg.type = CK_HID_MULTITOUCH_TOUCH; |
| 3308 | // msg.eid = f->identifier; |
| 3309 | // msg.fdata[0] = f->normalized.pos.x; |
| 3310 | // msg.fdata[1] = f->normalized.pos.y; |
| 3311 | // msg.fdata[2] = f->size; |
| 3312 | |
| 3313 | HidInManager::push_message( msg ); |
| 3314 | |
| 3315 | return event; |
| 3316 | } |
| 3317 | |
| 3318 | private: |
| 3319 | |
| 3320 | }; |
| 3321 | |
| 3322 | static TabletManager * g_tabletManager = NULL; |
| 3323 | |
| 3324 | |
| 3325 | |
| 3326 | CGEventRef Tablet_TapCallBack(CGEventTapProxy proxy, CGEventType type, |
| 3327 | CGEventRef event, void *refcon) |
| 3328 | { |
| 3329 | TabletManager * mgr = (TabletManager *) refcon; |
| 3330 | |
| 3331 | return mgr->TapCallBack(proxy, type, event); |
| 3332 | } |
| 3333 | |
| 3334 | |
| 3335 | |
| 3336 | void Tablet_init() |
| 3337 | { |
| 3338 | g_tabletManager = new TabletManager; |
| 3339 | } |
| 3340 | |
| 3341 | void Tablet_quit() |
| 3342 | { |
| 3343 | delete g_tabletManager; |
| 3344 | g_tabletManager = NULL; |
| 3345 | } |
| 3346 | |
| 3347 | void Tablet_probe() |
| 3348 | { |
| 3349 | |
| 3350 | } |
| 3351 | |
| 3352 | int Tablet_count() |
| 3353 | { |
| 3354 | return 0; |
| 3355 | } |
| 3356 | |
| 3357 | int Tablet_open( int ts ) |
| 3358 | { |
| 3359 | return HID_NOERROR; |
| 3360 | } |
| 3361 | |
| 3362 | int Tablet_close( int ts ) |
| 3363 | { |
| 3364 | return HID_NOERROR; |
| 3365 | } |
| 3366 | |
| 3367 | const char * Tablet_name( int ts ) |
| 3368 | { |
| 3369 | return NULL; |
| 3370 | } |
| 3371 | |
| 3372 | |
| 3373 | |
| 3374 | |
| 3375 | |
| 3376 | |
| 3377 | #ifdef __CK_HID_WIIREMOTE__ |
| 3378 | #pragma mark OS X Wii Remote support |
| 3379 | |
| 3380 | class Bluetooth_Device : public lockable |
| 3381 | { |
| 3382 | public: |
| 3383 | Bluetooth_Device() |
| 3384 | { |
| 3385 | device = NULL; |
| 3386 | memset( &address, 0, sizeof( address ) ); |
| 3387 | strncpy( name, "Bluetooth Device", 256 ); |
| 3388 | interrupt_channel = NULL; |
| 3389 | control_channel = NULL; |
| 3390 | disconnect_notification = NULL; |
| 3391 | |
| 3392 | type = 0; |
| 3393 | num = -1; |
| 3394 | |
| 3395 | refcount = 0; |
| 3396 | } |
| 3397 | |
| 3398 | virtual ~Bluetooth_Device() |
| 3399 | { |
| 3400 | close(); |
| 3401 | } |
| 3402 | |
| 3403 | virtual t_CKINT open() { return -1; } |
| 3404 | virtual t_CKINT connect() { return -1; } |
| 3405 | virtual t_CKINT control_init() { return -1; } |
| 3406 | virtual t_CKINT interrupt_init() { return -1; } |
| 3407 | virtual t_CKINT disconnect() { return -1; } |
| 3408 | virtual t_CKINT close() { return -1; } |
| 3409 | virtual t_CKBOOL is_connected() { return FALSE; } |
| 3410 | |
| 3411 | virtual void control_receive( void * data, size_t size ) {}; |
| 3412 | virtual void interrupt_receive( void * data, size_t size ) {}; |
| 3413 | |
| 3414 | virtual void control_send( void * data, size_t size ) {}; |
| 3415 | virtual void interrupt_send( void * data, size_t size ) {}; |
| 3416 | |
| 3417 | IOBluetoothDeviceRef device; |
| 3418 | BluetoothDeviceAddress address; |
| 3419 | IOBluetoothL2CAPChannelRef interrupt_channel; |
| 3420 | IOBluetoothL2CAPChannelRef control_channel; |
| 3421 | IOBluetoothUserNotificationRef disconnect_notification; |
| 3422 | char name[256]; |
| 3423 | |
| 3424 | t_CKUINT type; |
| 3425 | t_CKINT num; |
| 3426 | t_CKUINT refcount; |
| 3427 | }; |
| 3428 | |
| 3429 | class WiiRemote : public Bluetooth_Device |
| 3430 | { |
| 3431 | protected: |
| 3432 | enum Extension |
| 3433 | { |
| 3434 | ExtensionNone, |
| 3435 | ExtensionNunchuk, |
| 3436 | ExtensionClassicController |
| 3437 | }; |
| 3438 | |
| 3439 | public: |
| 3440 | |
| 3441 | WiiRemote() |
| 3442 | { |
| 3443 | force_feedback_enabled = FALSE; |
| 3444 | motion_sensor_enabled = FALSE; |
| 3445 | ir_sensor_enabled = FALSE; |
| 3446 | ir_sensor_initialized = FALSE; |
| 3447 | extension_enabled = FALSE; |
| 3448 | led1 = led2 = led3 = led4 = FALSE; |
| 3449 | speaker_enabled = FALSE; |
| 3450 | connected_extension = ExtensionNone; |
| 3451 | timer = NULL; |
| 3452 | audio_buffer = NULL; |
| 3453 | memset( &buttons, 0, sizeof( buttons ) ); |
| 3454 | memset( &accels, 0, sizeof( accels ) ); |
| 3455 | memset( &ir, 0xff, sizeof( ir ) ); |
| 3456 | memset( &extension, 0, sizeof( extension ) ); |
| 3457 | extension[5] |= 0x03; |
| 3458 | } |
| 3459 | |
| 3460 | virtual t_CKINT open(); |
| 3461 | virtual t_CKINT connect(); |
| 3462 | virtual t_CKINT control_init(); |
| 3463 | virtual t_CKINT disconnect(); |
| 3464 | virtual t_CKINT close(); |
| 3465 | virtual t_CKBOOL is_connected(); |
| 3466 | |
| 3467 | virtual void control_receive( void * data, size_t size ); |
| 3468 | virtual void interrupt_receive( void * data, size_t size ); |
| 3469 | |
| 3470 | virtual void control_send( const void * data, unsigned int size ); |
| 3471 | virtual void write_memory( const void * data, unsigned int size, unsigned int address ); |
| 3472 | virtual void read_memory( unsigned int address, unsigned int size ); |
| 3473 | |
| 3474 | virtual void check_extension(); |
| 3475 | |
| 3476 | virtual void enable_peripherals( t_CKBOOL force_feedback, |
| 3477 | t_CKBOOL motion_sensor, |
| 3478 | t_CKBOOL ir_sensor, |
| 3479 | t_CKBOOL extension ); |
| 3480 | virtual void enable_force_feedback( t_CKBOOL enable ); |
| 3481 | virtual void enable_motion_sensor( t_CKBOOL enable ); |
| 3482 | virtual void enable_ir_sensor( t_CKBOOL enable ); |
| 3483 | virtual void enable_extension( t_CKBOOL enable ); |
| 3484 | virtual void enable_leds( t_CKBOOL l1, t_CKBOOL l2, |
| 3485 | t_CKBOOL l3, t_CKBOOL l4 ); |
| 3486 | virtual void set_led( t_CKUINT led, t_CKBOOL state ); |
| 3487 | virtual void enable_speaker( t_CKBOOL enable ); |
| 3488 | |
| 3489 | CFRunLoopTimerRef timer; |
| 3490 | CBufferSimple * audio_buffer; |
| 3491 | virtual void send_audio_data(); |
| 3492 | |
| 3493 | protected: |
| 3494 | |
| 3495 | virtual void initialize_ir_sensor(); |
| 3496 | |
| 3497 | t_CKBOOL force_feedback_enabled; |
| 3498 | t_CKBOOL motion_sensor_enabled; |
| 3499 | t_CKBOOL ir_sensor_enabled; |
| 3500 | t_CKBOOL ir_sensor_initialized; |
| 3501 | t_CKBOOL extension_enabled; |
| 3502 | t_CKBOOL led1, led2, led3, led4; |
| 3503 | t_CKBOOL speaker_enabled; |
| 3504 | |
| 3505 | t_CKBYTE buttons[2]; |
| 3506 | t_CKBYTE accels[3]; |
| 3507 | t_CKBYTE ir[12]; |
| 3508 | t_CKBYTE extension[6]; |
| 3509 | |
| 3510 | Extension connected_extension; |
| 3511 | |
| 3512 | enum Button |
| 3513 | { |
| 3514 | ButtonHome = 0, |
| 3515 | Button1, |
| 3516 | Button2, |
| 3517 | ButtonPlus, |
| 3518 | ButtonMinus, |
| 3519 | ButtonA, |
| 3520 | ButtonB, |
| 3521 | ButtonUp, |
| 3522 | ButtonRight, |
| 3523 | ButtonDown, |
| 3524 | ButtonLeft, |
| 3525 | ButtonC, |
| 3526 | ButtonZ |
| 3527 | }; |
| 3528 | }; |
| 3529 | |
| 3530 | bool operator< ( BluetoothDeviceAddress a, BluetoothDeviceAddress b ) |
| 3531 | { |
| 3532 | for( int i = 0; i < 6; i++ ) |
| 3533 | { |
| 3534 | if( a.data[i] < b.data[i] ) |
| 3535 | return true; |
| 3536 | if( a.data[i] > b.data[i] ) |
| 3537 | return false; |
| 3538 | } |
| 3539 | |
| 3540 | return false; |
| 3541 | } |
| 3542 | |
| 3543 | static xvector< WiiRemote * > * wiiremotes = NULL; |
| 3544 | |
| 3545 | /* the user can open a wiimote with any id number he chooses without an error, |
| 3546 | and will then be sent the appropriate message when that wiimote is connected. |
| 3547 | The nth wiimote is simply the nth wiimote that chuck hid detects, and since |
| 3548 | there is absolutely no way to determine how many wiimotes will connect in |
| 3549 | advance, this number must be left unbounded. To support this, WiiRemote_open |
| 3550 | will add empty WiiRemotes to wiiremotes with refcount of 1 if it the wiimote |
| 3551 | its opening isnt there yet, and hope it will be opened at some future time. |
| 3552 | Thus when discovering wiimotes we have to distinguish between wiimotes that have |
| 3553 | been discovered and wiimotes that have been opened but have not yet been |
| 3554 | discovered. all wiimotes with indices less than g_wr_next_real_wiimote have |
| 3555 | actually been discovered, and anything at or above that index in wiiremotes has |
| 3556 | not actually been discovered, but (if its non-NULL) has been opened. */ |
| 3557 | static xvector< WiiRemote * >::size_type g_next_real_wiimote = 0; |
| 3558 | |
| 3559 | static map< BluetoothDeviceAddress, WiiRemote * > * wr_addresses = NULL; |
| 3560 | static t_CKBOOL g_bt_query_active = FALSE; // is a query currently active? |
| 3561 | |
| 3562 | void Bluetooth_device_connected( void * userRefCon, |
| 3563 | IOBluetoothDeviceRef deviceRef, |
| 3564 | IOReturn status ) |
| 3565 | { |
| 3566 | if( status == noErr ) |
| 3567 | ( ( Bluetooth_Device * ) userRefCon )->connect(); |
| 3568 | else |
| 3569 | EM_log( CK_LOG_WARNING, "hid: error: opening Wii Remote Controller connection" ); |
| 3570 | } |
| 3571 | |
| 3572 | void Bluetooth_device_control_event( IOBluetoothL2CAPChannelRef l2capChannel, |
| 3573 | void * refCon, |
| 3574 | IOBluetoothL2CAPChannelEvent * event ) |
| 3575 | { |
| 3576 | switch( event->eventType ) |
| 3577 | { |
| 3578 | case kIOBluetoothL2CAPChannelEventTypeOpenComplete: |
| 3579 | ( ( Bluetooth_Device * ) refCon )->control_init(); |
| 3580 | break; |
| 3581 | |
| 3582 | case kIOBluetoothL2CAPChannelEventTypeData: |
| 3583 | ( ( Bluetooth_Device * ) refCon )->control_receive( event->u.data.dataPtr, |
| 3584 | event->u.data.dataSize ); |
| 3585 | break; |
| 3586 | |
| 3587 | case kIOBluetoothL2CAPChannelEventTypeWriteComplete: |
| 3588 | if( event->status != noErr ) |
| 3589 | { |
| 3590 | EM_log( CK_LOG_WARNING, "hid: error: writing data to control L2CAP channel for '%s'", |
| 3591 | ( ( Bluetooth_Device * ) refCon )->name ); |
| 3592 | } |
| 3593 | |
| 3594 | break; |
| 3595 | |
| 3596 | case kIOBluetoothL2CAPChannelEventTypeClosed: |
| 3597 | EM_log( CK_LOG_FINE, "hid: error: control L2CAP channel for '%s' closed", |
| 3598 | ( ( Bluetooth_Device * ) refCon )->name ); |
| 3599 | break; |
| 3600 | } |
| 3601 | } |
| 3602 | |
| 3603 | void Bluetooth_device_interrupt_event( IOBluetoothL2CAPChannelRef l2capChannel, |
| 3604 | void * refCon, |
| 3605 | IOBluetoothL2CAPChannelEvent * event ) |
| 3606 | { |
| 3607 | switch( event->eventType ) |
| 3608 | { |
| 3609 | case kIOBluetoothL2CAPChannelEventTypeData: |
| 3610 | ( ( Bluetooth_Device * ) refCon )->control_receive( event->u.data.dataPtr, |
| 3611 | event->u.data.dataSize ); |
| 3612 | break; |
| 3613 | } |
| 3614 | } |
| 3615 | |
| 3616 | void Bluetooth_device_disconnected( void * userRefCon, |
| 3617 | IOBluetoothUserNotificationRef inRef, |
| 3618 | IOBluetoothObjectRef objectRef ) |
| 3619 | { |
| 3620 | ( ( Bluetooth_Device * ) userRefCon )->disconnect(); |
| 3621 | } |
| 3622 | |
| 3623 | void WiiRemote_send_audio_data( CFRunLoopTimerRef timer, void *info ) |
| 3624 | { |
| 3625 | WiiRemote * wr = ( WiiRemote * ) info; |
| 3626 | wr->send_audio_data(); |
| 3627 | } |
| 3628 | |
| 3629 | t_CKINT WiiRemote::open() |
| 3630 | { |
| 3631 | // see if its already connected, reconnect if so |
| 3632 | if( IOBluetoothDeviceIsConnected( device ) ) |
| 3633 | { |
| 3634 | if( IOBluetoothDeviceCloseConnection( device ) != noErr ) |
| 3635 | { |
| 3636 | EM_log( CK_LOG_WARNING, "hid: error: closing Wii Remote Controller connection" ); |
| 3637 | return -1; |
| 3638 | } |
| 3639 | } |
| 3640 | |
| 3641 | if( IOBluetoothDeviceOpenConnection( device, Bluetooth_device_connected, |
| 3642 | this ) != noErr) |
| 3643 | { |
| 3644 | EM_log( CK_LOG_WARNING, "hid: error: opening Wii Remote Controller connection" ); |
| 3645 | return -1; |
| 3646 | } |
| 3647 | |
| 3648 | return 0; |
| 3649 | } |
| 3650 | |
| 3651 | t_CKINT WiiRemote::connect() |
| 3652 | { |
| 3653 | disconnect_notification = IOBluetoothDeviceRegisterForDisconnectNotification( device, |
| 3654 | Bluetooth_device_disconnected, |
| 3655 | this ); |
| 3656 | |
| 3657 | if( disconnect_notification == NULL ) |
| 3658 | { |
| 3659 | EM_log( CK_LOG_WARNING, "hid: error: registering for Wii Remote Controller disconnection notification" ); |
| 3660 | return -1; |
| 3661 | } |
| 3662 | |
| 3663 | if( IOBluetoothDeviceOpenL2CAPChannelAsync( device, &control_channel, 17, |
| 3664 | Bluetooth_device_control_event, |
| 3665 | this ) != kIOReturnSuccess ) |
| 3666 | { |
| 3667 | EM_log( CK_LOG_WARNING, "hid: error: opening Wii Remote Controller L2CAP connection" ); |
| 3668 | return -1; |
| 3669 | } |
| 3670 | //CK_FPRINTF_STDERR( "l2cap control channel ref: 0x%x\n", control_channel ); |
| 3671 | if( IOBluetoothDeviceOpenL2CAPChannelAsync( device, &interrupt_channel, 19, |
| 3672 | Bluetooth_device_interrupt_event, |
| 3673 | this ) != kIOReturnSuccess ) |
| 3674 | { |
| 3675 | EM_log( CK_LOG_WARNING, "hid: error: opening Wii Remote Controller L2CAP connection" ); |
| 3676 | return -1; |
| 3677 | } |
| 3678 | |
| 3679 | return 0; |
| 3680 | } |
| 3681 | |
| 3682 | t_CKINT WiiRemote::control_init() |
| 3683 | { |
| 3684 | check_extension(); |
| 3685 | |
| 3686 | //enable_motion_sensor( TRUE ); |
| 3687 | //enable_ir_sensor( TRUE ); |
| 3688 | //enable_force_feedback( FALSE ); |
| 3689 | //enable_extension( TRUE ); |
| 3690 | |
| 3691 | enable_peripherals( FALSE, TRUE, TRUE, TRUE ); |
| 3692 | |
| 3693 | ck_usleep( 1000 ); |
| 3694 | enable_leds( ( num % 4 ) == 0, ( ( num - 1 ) % 4 ) == 0, |
| 3695 | ( ( num - 2 ) % 4 ) == 0, ( ( num - 3 ) % 4 ) == 0 ); |
| 3696 | |
| 3697 | ck_usleep( 1000 ); |
| 3698 | enable_speaker( FALSE ); |
| 3699 | |
| 3700 | HidMsg msg; |
| 3701 | |
| 3702 | msg.device_num = num; |
| 3703 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 3704 | msg.type = CK_HID_DEVICE_CONNECTED; |
| 3705 | |
| 3706 | HidInManager::push_message( msg ); |
| 3707 | |
| 3708 | return 0; |
| 3709 | } |
| 3710 | |
| 3711 | t_CKINT WiiRemote::disconnect() |
| 3712 | { |
| 3713 | if( interrupt_channel ) |
| 3714 | { |
| 3715 | IOBluetoothL2CAPChannelCloseChannel( interrupt_channel ); |
| 3716 | IOBluetoothObjectRelease( interrupt_channel ); |
| 3717 | interrupt_channel = NULL; |
| 3718 | } |
| 3719 | |
| 3720 | if( control_channel ) |
| 3721 | { |
| 3722 | IOBluetoothL2CAPChannelCloseChannel( control_channel ); |
| 3723 | IOBluetoothObjectRelease( control_channel ); |
| 3724 | control_channel = NULL; |
| 3725 | } |
| 3726 | |
| 3727 | if( device ) |
| 3728 | { |
| 3729 | IOBluetoothDeviceCloseConnection( device ); |
| 3730 | IOBluetoothObjectRelease( device ); |
| 3731 | device = NULL; |
| 3732 | } |
| 3733 | |
| 3734 | if( disconnect_notification ) |
| 3735 | { |
| 3736 | IOBluetoothUserNotificationUnregister( disconnect_notification ); |
| 3737 | disconnect_notification = NULL; |
| 3738 | } |
| 3739 | |
| 3740 | HidMsg msg; |
| 3741 | |
| 3742 | msg.device_num = num; |
| 3743 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 3744 | msg.type = CK_HID_DEVICE_DISCONNECTED; |
| 3745 | |
| 3746 | HidInManager::push_message( msg ); |
| 3747 | |
| 3748 | return 0; |
| 3749 | } |
| 3750 | |
| 3751 | t_CKINT WiiRemote::close() |
| 3752 | { |
| 3753 | if( is_connected() ) |
| 3754 | disconnect(); |
| 3755 | return 0; |
| 3756 | } |
| 3757 | |
| 3758 | t_CKBOOL WiiRemote::is_connected() |
| 3759 | { |
| 3760 | if( device == NULL ) |
| 3761 | return FALSE; |
| 3762 | return TRUE; |
| 3763 | } |
| 3764 | |
| 3765 | static inline unsigned char wii_remote_extension_decrypt( unsigned char b ) |
| 3766 | { |
| 3767 | return ( ( b ^ 0x17 ) + 0x17 & 0xFF ); |
| 3768 | } |
| 3769 | |
| 3770 | void WiiRemote::control_receive( void * data, size_t size ) |
| 3771 | { |
| 3772 | unsigned char * d = ( unsigned char * ) data; |
| 3773 | HidMsg msg; |
| 3774 | |
| 3775 | if( ( d[1] & 0xf0 ) == 0x30 ) |
| 3776 | { |
| 3777 | /* buttons */ |
| 3778 | |
| 3779 | if( ( d[3] & 0x80 ) ^ ( buttons[1] & 0x80 ) ) |
| 3780 | { |
| 3781 | msg.device_num = num; |
| 3782 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 3783 | msg.type = ( d[3] & 0x80 ) ? CK_HID_BUTTON_DOWN : CK_HID_BUTTON_UP; |
| 3784 | msg.eid = ButtonHome; |
| 3785 | |
| 3786 | HidInManager::push_message( msg ); |
| 3787 | |
| 3788 | msg.clear(); |
| 3789 | } |
| 3790 | |
| 3791 | if( ( d[3] & 0x02 ) ^ ( buttons[1] & 0x02 ) ) |
| 3792 | { |
| 3793 | msg.device_num = num; |
| 3794 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 3795 | msg.type = ( d[3] & 0x02 ) ? CK_HID_BUTTON_DOWN : CK_HID_BUTTON_UP; |
| 3796 | msg.eid = Button1; |
| 3797 | |
| 3798 | HidInManager::push_message( msg ); |
| 3799 | |
| 3800 | msg.clear(); |
| 3801 | } |
| 3802 | |
| 3803 | if( ( d[3] & 0x01 ) ^ ( buttons[1] & 0x01 ) ) |
| 3804 | { |
| 3805 | msg.device_num = num; |
| 3806 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 3807 | msg.type = ( d[3] & 0x01 ) ? CK_HID_BUTTON_DOWN : CK_HID_BUTTON_UP; |
| 3808 | msg.eid = Button2; |
| 3809 | |
| 3810 | HidInManager::push_message( msg ); |
| 3811 | |
| 3812 | msg.clear(); |
| 3813 | } |
| 3814 | |
| 3815 | if( ( d[2] & 0x10 ) ^ ( buttons[0] & 0x10 ) ) |
| 3816 | { |
| 3817 | msg.device_num = num; |
| 3818 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 3819 | msg.type = ( d[2] & 0x10 ) ? CK_HID_BUTTON_DOWN : CK_HID_BUTTON_UP; |
| 3820 | msg.eid = ButtonPlus; |
| 3821 | |
| 3822 | HidInManager::push_message( msg ); |
| 3823 | |
| 3824 | msg.clear(); |
| 3825 | } |
| 3826 | |
| 3827 | if( ( d[3] & 0x10 ) ^ ( buttons[1] & 0x10 ) ) |
| 3828 | { |
| 3829 | msg.device_num = num; |
| 3830 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 3831 | msg.type = ( d[3] & 0x10 ) ? CK_HID_BUTTON_DOWN : CK_HID_BUTTON_UP; |
| 3832 | msg.eid = ButtonMinus; |
| 3833 | |
| 3834 | HidInManager::push_message( msg ); |
| 3835 | |
| 3836 | msg.clear(); |
| 3837 | } |
| 3838 | |
| 3839 | if( ( d[3] & 0x08 ) ^ ( buttons[1] & 0x08 ) ) |
| 3840 | { |
| 3841 | msg.device_num = num; |
| 3842 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 3843 | msg.type = ( d[3] & 0x08 ) ? CK_HID_BUTTON_DOWN : CK_HID_BUTTON_UP; |
| 3844 | msg.eid = ButtonA; |
| 3845 | |
| 3846 | HidInManager::push_message( msg ); |
| 3847 | |
| 3848 | msg.clear(); |
| 3849 | } |
| 3850 | |
| 3851 | if( ( d[3] & 0x04 ) ^ ( buttons[1] & 0x04 ) ) |
| 3852 | { |
| 3853 | msg.device_num = num; |
| 3854 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 3855 | msg.type = ( d[3] & 0x04 ) ? CK_HID_BUTTON_DOWN : CK_HID_BUTTON_UP; |
| 3856 | msg.eid = ButtonB; |
| 3857 | |
| 3858 | HidInManager::push_message( msg ); |
| 3859 | |
| 3860 | msg.clear(); |
| 3861 | } |
| 3862 | |
| 3863 | if( ( d[2] & 0x08 ) ^ ( buttons[0] & 0x08 ) ) |
| 3864 | { |
| 3865 | msg.device_num = num; |
| 3866 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 3867 | msg.type = ( d[2] & 0x08 ) ? CK_HID_BUTTON_DOWN : CK_HID_BUTTON_UP; |
| 3868 | msg.eid = ButtonUp; |
| 3869 | |
| 3870 | HidInManager::push_message( msg ); |
| 3871 | |
| 3872 | msg.clear(); |
| 3873 | } |
| 3874 | |
| 3875 | if( ( d[2] & 0x02 ) ^ ( buttons[0] & 0x02 ) ) |
| 3876 | { |
| 3877 | msg.device_num = num; |
| 3878 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 3879 | msg.type = ( d[2] & 0x02 ) ? CK_HID_BUTTON_DOWN : CK_HID_BUTTON_UP; |
| 3880 | msg.eid = ButtonRight; |
| 3881 | |
| 3882 | HidInManager::push_message( msg ); |
| 3883 | |
| 3884 | msg.clear(); |
| 3885 | } |
| 3886 | |
| 3887 | if( ( d[2] & 0x04 ) ^ ( buttons[0] & 0x04 ) ) |
| 3888 | { |
| 3889 | msg.device_num = num; |
| 3890 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 3891 | msg.type = ( d[2] & 0x04 ) ? CK_HID_BUTTON_DOWN : CK_HID_BUTTON_UP; |
| 3892 | msg.eid = ButtonDown; |
| 3893 | |
| 3894 | HidInManager::push_message( msg ); |
| 3895 | |
| 3896 | msg.clear(); |
| 3897 | } |
| 3898 | |
| 3899 | if( ( d[2] & 0x01 ) ^ ( buttons[0] & 0x01 ) ) |
| 3900 | { |
| 3901 | msg.device_num = num; |
| 3902 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 3903 | msg.type = ( d[2] & 0x01 ) ? CK_HID_BUTTON_DOWN : CK_HID_BUTTON_UP; |
| 3904 | msg.eid = ButtonLeft; |
| 3905 | |
| 3906 | HidInManager::push_message( msg ); |
| 3907 | |
| 3908 | msg.clear(); |
| 3909 | } |
| 3910 | |
| 3911 | memcpy( buttons, d + 2, sizeof( buttons ) ); |
| 3912 | |
| 3913 | /* accelerometers */ |
| 3914 | if( d[1] & 0x01 ) |
| 3915 | { |
| 3916 | if( d[4] ^ accels[0] || |
| 3917 | d[5] ^ accels[1] || |
| 3918 | d[6] ^ accels[2] ) |
| 3919 | { |
| 3920 | msg.device_num = num; |
| 3921 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 3922 | msg.type = CK_HID_ACCELEROMETER; |
| 3923 | msg.eid = 0; |
| 3924 | msg.idata[0] = d[4]; |
| 3925 | msg.idata[1] = d[5]; |
| 3926 | msg.idata[2] = d[6]; |
| 3927 | |
| 3928 | HidInManager::push_message( msg ); |
| 3929 | |
| 3930 | msg.clear(); |
| 3931 | } |
| 3932 | |
| 3933 | memcpy( accels, d + 4, sizeof( accels ) ); |
| 3934 | } |
| 3935 | |
| 3936 | /* ir sensor */ |
| 3937 | if( d[1] & 0x02 ) |
| 3938 | { |
| 3939 | unsigned i; |
| 3940 | |
| 3941 | if( !( d[1] & 0x04 ) ) |
| 3942 | { |
| 3943 | for( i = 0; i < 4; i++ ) |
| 3944 | { |
| 3945 | // 12 byte extended IR data format |
| 3946 | // available when there are no extension bytes |
| 3947 | if( ( d[7 + i * 3] ^ ir[i * 3] || |
| 3948 | d[7 + i * 3 + 1] ^ ir[i * 3 + 1] || |
| 3949 | d[7 + i * 3 + 2] ^ ir[i * 3 + 2] ) && |
| 3950 | ( d[7 + i * 3] != 0xff ) && |
| 3951 | ( d[7 + i * 3 + 1] != 0xff ) && |
| 3952 | ( d[7 + i * 3 + 2] != 0xff ) ) |
| 3953 | { |
| 3954 | msg.device_num = num; |
| 3955 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 3956 | msg.type = CK_HID_WIIREMOTE_IR; |
| 3957 | msg.eid = i; |
| 3958 | // x |
| 3959 | msg.idata[0] = d[7 + 3 * i] + ( ( d[7 + 3 * i + 2] << 4 ) & 0x300 ); |
| 3960 | // y |
| 3961 | msg.idata[1] = d[7 + 3 * i + 1] + ( ( d[7 + 3 * i + 2] << 2 ) & 0x300 ); |
| 3962 | // size |
| 3963 | msg.idata[2] = d[7 + 3 * i + 2] & 0xffff; |
| 3964 | |
| 3965 | HidInManager::push_message( msg ); |
| 3966 | |
| 3967 | msg.clear(); |
| 3968 | } |
| 3969 | } |
| 3970 | } |
| 3971 | |
| 3972 | else |
| 3973 | { |
| 3974 | // 9 byte basic IR data format |
| 3975 | // available when there are no extension bytes |
| 3976 | } |
| 3977 | |
| 3978 | memcpy( ir, d + 7, sizeof( ir ) ); |
| 3979 | } |
| 3980 | |
| 3981 | /* extension */ |
| 3982 | if( d[1] & 0x04 ) |
| 3983 | { |
| 3984 | for( int i = 0; i < 6; i++ ) |
| 3985 | d[17 + i] = wii_remote_extension_decrypt( d[17 + i] ); |
| 3986 | |
| 3987 | switch( connected_extension ) |
| 3988 | { |
| 3989 | case ExtensionNunchuk: |
| 3990 | if( d[17] ^ extension[0] || |
| 3991 | d[18] ^ extension[1] ) |
| 3992 | // joystick axis |
| 3993 | { |
| 3994 | msg.device_num = num; |
| 3995 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 3996 | msg.type = CK_HID_JOYSTICK_AXIS; |
| 3997 | msg.eid = 0; |
| 3998 | |
| 3999 | // x axis |
| 4000 | msg.idata[0] = d[17]; |
| 4001 | // y axis |
| 4002 | msg.idata[1] = d[18]; |
| 4003 | |
| 4004 | HidInManager::push_message( msg ); |
| 4005 | |
| 4006 | msg.clear(); |
| 4007 | } |
| 4008 | |
| 4009 | if( d[19] ^ extension[2] || |
| 4010 | d[20] ^ extension[3] || |
| 4011 | d[21] ^ extension[4] ) |
| 4012 | { |
| 4013 | msg.device_num = num; |
| 4014 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 4015 | msg.type = CK_HID_ACCELEROMETER; |
| 4016 | msg.eid = 1; |
| 4017 | |
| 4018 | // x axis |
| 4019 | msg.idata[0] = d[19]; |
| 4020 | // y axis |
| 4021 | msg.idata[1] = d[20]; |
| 4022 | // z axis |
| 4023 | msg.idata[2] = d[21]; |
| 4024 | |
| 4025 | HidInManager::push_message( msg ); |
| 4026 | |
| 4027 | msg.clear(); |
| 4028 | } |
| 4029 | |
| 4030 | if( ( d[22] & ( 1 << 0 ) ) ^ ( extension[5] & ( 1 << 0 ) ) ) |
| 4031 | { |
| 4032 | msg.device_num = num; |
| 4033 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 4034 | msg.type = ( d[22] & ( 1 << 0 ) ) ? CK_HID_BUTTON_UP : CK_HID_BUTTON_DOWN; |
| 4035 | msg.eid = ButtonZ; |
| 4036 | |
| 4037 | HidInManager::push_message( msg ); |
| 4038 | |
| 4039 | msg.clear(); |
| 4040 | } |
| 4041 | |
| 4042 | if( ( d[22] & ( 1 << 1 ) ) ^ ( extension[5] & ( 1 << 1 ) ) ) |
| 4043 | { |
| 4044 | msg.device_num = num; |
| 4045 | msg.device_type = CK_HID_DEV_WIIREMOTE; |
| 4046 | msg.type = ( d[22] & ( 1 << 1 ) ) ? CK_HID_BUTTON_UP : CK_HID_BUTTON_DOWN; |
| 4047 | msg.eid = ButtonC; |
| 4048 | |
| 4049 | HidInManager::push_message( msg ); |
| 4050 | |
| 4051 | msg.clear(); |
| 4052 | } |
| 4053 | |
| 4054 | break; |
| 4055 | } |
| 4056 | |
| 4057 | memcpy( extension, d + 17, 6 ); |
| 4058 | } |
| 4059 | } |
| 4060 | |
| 4061 | else if( d[1] == 0x21 ) |
| 4062 | { |
| 4063 | int i = 0; |
| 4064 | i++; |
| 4065 | // read memory packet |
| 4066 | if( ( d[4] & 0xf0 ) == 0x10 && // size = 2 |
| 4067 | d[5] == 0x00 && // address = 0x04a400fe (but we only have the first 2 bytes of that here) |
| 4068 | d[6] == 0xfe ) |
| 4069 | { |
| 4070 | unsigned char ext0 = wii_remote_extension_decrypt( d[7] ); |
| 4071 | unsigned char ext1 = wii_remote_extension_decrypt( d[8] ); |
| 4072 | |
| 4073 | if( ext0 == 0x00 && ext1 == 0x00 ) |
| 4074 | connected_extension = ExtensionNunchuk; |
| 4075 | else if( ext0 == 0x01 && ext1 == 0x01 ) |
| 4076 | connected_extension = ExtensionClassicController; |
| 4077 | else |
| 4078 | connected_extension = ExtensionNone; |
| 4079 | } |
| 4080 | } |
| 4081 | } |
| 4082 | |
| 4083 | void WiiRemote::interrupt_receive( void * data, size_t size ) |
| 4084 | { |
| 4085 | |
| 4086 | } |
| 4087 | |
| 4088 | void WiiRemote::control_send( const void * data, unsigned int size ) |
| 4089 | { |
| 4090 | assert( size <= 22 ); |
| 4091 | |
| 4092 | unsigned char buf[23]; |
| 4093 | |
| 4094 | memset( buf, 0, 23 ); |
| 4095 | buf[0] = 0x52; |
| 4096 | memcpy( buf+1, data, size ); |
| 4097 | |
| 4098 | size++; |
| 4099 | |
| 4100 | //printf( "send (%i):", size ); |
| 4101 | //for( int i = 0; i < size; i++ ) |
| 4102 | // printf( " %02x", buf[i] ); |
| 4103 | //printf( "\n" ); |
| 4104 | //CK_FPRINTF_STDERR( "l2cap control channel ref: 0x%x\n", control_channel ); |
| 4105 | |
| 4106 | IOReturn result; |
| 4107 | result = IOBluetoothL2CAPChannelWriteSync( control_channel, buf, size ); |
| 4108 | //IOReturn result = IOBluetoothL2CAPChannelWriteAsync( control_channel, buf, size, NULL ); |
| 4109 | |
| 4110 | if( result != kIOReturnSuccess ) |
| 4111 | EM_log( CK_LOG_WARNING, |
| 4112 | "hid: error: sending data to Wii Remote Controller %i (error 0x%x)", |
| 4113 | num, result ); |
| 4114 | } |
| 4115 | |
| 4116 | void WiiRemote::write_memory( const void * data, unsigned int size, |
| 4117 | unsigned int address ) |
| 4118 | { |
| 4119 | assert( size <= 16 ); |
| 4120 | |
| 4121 | unsigned char cmd[22]; |
| 4122 | |
| 4123 | memset( cmd, 0, 22 ); |
| 4124 | |
| 4125 | cmd[0] = 0x16; |
| 4126 | cmd[1] = ( address >> 24 ) & 0xff; |
| 4127 | cmd[2] = ( address >> 16 ) & 0xff; |
| 4128 | cmd[3] = ( address >> 8 ) & 0xff; |
| 4129 | cmd[4] = address & 0xff; |
| 4130 | cmd[5] = size; |
| 4131 | |
| 4132 | memcpy( cmd + 6, data, size ); |
| 4133 | |
| 4134 | if( force_feedback_enabled ) |
| 4135 | cmd[1] |= 0x01; |
| 4136 | |
| 4137 | control_send( cmd, 22 ); |
| 4138 | } |
| 4139 | |
| 4140 | void WiiRemote::read_memory( unsigned int address, unsigned int size ) |
| 4141 | { |
| 4142 | unsigned char cmd[7]; |
| 4143 | |
| 4144 | cmd[0] = 0x17; |
| 4145 | cmd[1] = ( address >> 24 ) & 0xff; |
| 4146 | cmd[2] = ( address >> 16 ) & 0xff; |
| 4147 | cmd[3] = ( address >> 8 ) & 0xff; |
| 4148 | cmd[4] = address & 0xff; |
| 4149 | cmd[5] = ( size >> 8 ) & 0xff; |
| 4150 | cmd[6] = ( size >> 0 ) & 0xff; |
| 4151 | |
| 4152 | if( force_feedback_enabled ) |
| 4153 | cmd[1] |= 0x01; |
| 4154 | |
| 4155 | control_send( cmd, 7 ); |
| 4156 | } |
| 4157 | |
| 4158 | void WiiRemote::check_extension() |
| 4159 | { |
| 4160 | unsigned char key[] = { 0x00 }; |
| 4161 | |
| 4162 | // write key |
| 4163 | write_memory( key, 1, 0x04a40040 ); |
| 4164 | |
| 4165 | // read extension id |
| 4166 | read_memory( 0x04a400fe, 2 ); |
| 4167 | } |
| 4168 | |
| 4169 | void WiiRemote::enable_peripherals( t_CKBOOL force_feedback, |
| 4170 | t_CKBOOL motion_sensor, |
| 4171 | t_CKBOOL ir_sensor, |
| 4172 | t_CKBOOL extension ) |
| 4173 | { |
| 4174 | force_feedback_enabled = force_feedback; |
| 4175 | motion_sensor_enabled = motion_sensor; |
| 4176 | ir_sensor_enabled = ir_sensor; |
| 4177 | extension_enabled = extension; |
| 4178 | |
| 4179 | unsigned char cmd[] = { 0x12, 0x00, 0x30 }; |
| 4180 | if( motion_sensor_enabled ) |
| 4181 | cmd[2] |= 0x01; |
| 4182 | if( ir_sensor_enabled ) |
| 4183 | cmd[2] |= 0x02; |
| 4184 | if( extension_enabled ) |
| 4185 | cmd[2] |= 0x04; |
| 4186 | if( force_feedback_enabled ) |
| 4187 | cmd[1] |= 0x01; |
| 4188 | |
| 4189 | control_send( cmd, 3 ); |
| 4190 | |
| 4191 | unsigned char cmd2[] = { 0x13, 0x00 }; |
| 4192 | if( force_feedback_enabled ) |
| 4193 | cmd2[1] |= 0x01; |
| 4194 | if( ir_sensor_enabled ) |
| 4195 | cmd2[1] |= 0x04; |
| 4196 | |
| 4197 | control_send( cmd2, 2 ); |
| 4198 | |
| 4199 | if( ir_sensor_enabled && !ir_sensor_initialized ) |
| 4200 | initialize_ir_sensor(); |
| 4201 | } |
| 4202 | |
| 4203 | void WiiRemote::enable_force_feedback( t_CKBOOL enable ) |
| 4204 | { |
| 4205 | force_feedback_enabled = enable; |
| 4206 | |
| 4207 | unsigned char cmd[] = { 0x13, 0x00 }; |
| 4208 | if( enable ) |
| 4209 | cmd[1] |= 0x01; |
| 4210 | if( ir_sensor_enabled ) |
| 4211 | cmd[1] |= 0x04; |
| 4212 | |
| 4213 | control_send( cmd, 2 ); |
| 4214 | } |
| 4215 | |
| 4216 | void WiiRemote::enable_motion_sensor( t_CKBOOL enable ) |
| 4217 | { |
| 4218 | motion_sensor_enabled = enable; |
| 4219 | |
| 4220 | unsigned char cmd[] = { 0x12, 0x00, 0x30 }; |
| 4221 | if( motion_sensor_enabled ) |
| 4222 | cmd[2] |= 0x01; |
| 4223 | if( ir_sensor_enabled ) |
| 4224 | cmd[2] |= 0x02; |
| 4225 | if( extension_enabled ) |
| 4226 | cmd[2] |= 0x04; |
| 4227 | if( force_feedback_enabled ) |
| 4228 | cmd[1] |= 0x01; |
| 4229 | |
| 4230 | control_send( cmd, 3 ); |
| 4231 | } |
| 4232 | |
| 4233 | void WiiRemote::enable_ir_sensor( t_CKBOOL enable ) |
| 4234 | { |
| 4235 | ir_sensor_enabled = enable; |
| 4236 | |
| 4237 | enable_motion_sensor( motion_sensor_enabled ); |
| 4238 | enable_force_feedback( force_feedback_enabled ); |
| 4239 | |
| 4240 | unsigned char cmd[] = { 0x1a, 0x00 }; |
| 4241 | if( ir_sensor_enabled ) |
| 4242 | cmd[1] |= 0x04; |
| 4243 | if( force_feedback_enabled ) |
| 4244 | cmd[1] |= 0x01; |
| 4245 | |
| 4246 | control_send( cmd, 2 ); |
| 4247 | |
| 4248 | if( ir_sensor_enabled && !ir_sensor_initialized ) |
| 4249 | initialize_ir_sensor(); |
| 4250 | } |
| 4251 | |
| 4252 | void WiiRemote::initialize_ir_sensor() |
| 4253 | { |
| 4254 | ir_sensor_initialized = TRUE; |
| 4255 | |
| 4256 | unsigned char en0[] = { 0x01 }; |
| 4257 | write_memory( en0, 1, 0x04b00030 ); |
| 4258 | //ck_usleep(10000); |
| 4259 | |
| 4260 | unsigned char en[] = { 0x08 }; |
| 4261 | write_memory( en, 1, 0x04b00030 ); |
| 4262 | //ck_usleep(10000); |
| 4263 | |
| 4264 | unsigned char sensitivity_block1[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0x00, 0xc0 }; |
| 4265 | write_memory( sensitivity_block1, 9, 0x04b00000 ); |
| 4266 | //ck_usleep(10000); |
| 4267 | |
| 4268 | unsigned char sensitivity_block2[] = { 0x40, 0x00 }; |
| 4269 | write_memory( sensitivity_block2, 2, 0x04b0001a ); |
| 4270 | //ck_usleep(10000); |
| 4271 | |
| 4272 | unsigned char mode[] = { 0x03 }; |
| 4273 | write_memory( mode, 1, 0x04b00033 ); |
| 4274 | //ck_usleep(10000); |
| 4275 | |
| 4276 | unsigned char what[] = { 0x08 }; |
| 4277 | write_memory( what, 1, 0x04b00030 ); |
| 4278 | //ck_usleep(10000); |
| 4279 | } |
| 4280 | |
| 4281 | void WiiRemote::enable_extension( t_CKBOOL enable ) |
| 4282 | { |
| 4283 | extension_enabled = enable; |
| 4284 | |
| 4285 | enable_motion_sensor( motion_sensor_enabled ); |
| 4286 | } |
| 4287 | |
| 4288 | void WiiRemote::set_led( t_CKUINT led, t_CKBOOL state ) |
| 4289 | { |
| 4290 | switch( led ) |
| 4291 | { |
| 4292 | case 0: |
| 4293 | led1 = state; |
| 4294 | break; |
| 4295 | |
| 4296 | case 1: |
| 4297 | led2 = state; |
| 4298 | break; |
| 4299 | |
| 4300 | case 2: |
| 4301 | led3 = state; |
| 4302 | break; |
| 4303 | |
| 4304 | case 3: |
| 4305 | led4 = state; |
| 4306 | break; |
| 4307 | } |
| 4308 | |
| 4309 | enable_leds( led1, led2, led3, led4 ); |
| 4310 | } |
| 4311 | |
| 4312 | void WiiRemote::enable_leds( t_CKBOOL l1, t_CKBOOL l2, |
| 4313 | t_CKBOOL l3, t_CKBOOL l4 ) |
| 4314 | { |
| 4315 | led1 = l1; |
| 4316 | led2 = l2; |
| 4317 | led3 = l3; |
| 4318 | led4 = l4; |
| 4319 | |
| 4320 | unsigned char cmd[] = { 0x11, 0x00 }; |
| 4321 | if( force_feedback_enabled ) |
| 4322 | cmd[1] |= 0x01; |
| 4323 | if( l1 ) |
| 4324 | cmd[1] |= 0x10; |
| 4325 | if( l2 ) |
| 4326 | cmd[1] |= 0x20; |
| 4327 | if( l3 ) |
| 4328 | cmd[1] |= 0x40; |
| 4329 | if( l4 ) |
| 4330 | cmd[1] |= 0x80; |
| 4331 | |
| 4332 | control_send( cmd, 2 ); |
| 4333 | } |
| 4334 | |
| 4335 | void WiiRemote::enable_speaker( t_CKBOOL enable ) |
| 4336 | { |
| 4337 | speaker_enabled = enable; |
| 4338 | |
| 4339 | if( speaker_enabled ) |
| 4340 | { |
| 4341 | // enable speaker |
| 4342 | unsigned char cmd[] = { 0x14, 0x04 }; |
| 4343 | if( force_feedback_enabled ) |
| 4344 | cmd[1] |= 0x01; |
| 4345 | control_send( cmd, 2 ); |
| 4346 | |
| 4347 | // mute |
| 4348 | unsigned char mute[] = { 0x19, 0x04 }; |
| 4349 | if( force_feedback_enabled ) |
| 4350 | mute[1] |= 0x01; |
| 4351 | control_send( mute, 2 ); |
| 4352 | |
| 4353 | unsigned char mem1[] = { 0x01 }; |
| 4354 | write_memory( mem1, 1, 0x04a20009 ); |
| 4355 | |
| 4356 | unsigned char mem2[] = { 0x08 }; |
| 4357 | write_memory( mem2, 1, 0x04a20001 ); |
| 4358 | |
| 4359 | // config |
| 4360 | unsigned char config[] = { 0x00, 0x00, 0x00, 0x0c, 0x40, 0x00, 0x00 }; |
| 4361 | // description |
| 4362 | // byte 0: unknown |
| 4363 | // 1: unknown |
| 4364 | // 2: unknown |
| 4365 | // 3: sample rate -{ 0x0B = 4200 Hz |
| 4366 | // 0x0C = 3920 Hz |
| 4367 | // 0x0D = 3640 Hz |
| 4368 | // 0x0E = 3360 Hz |
| 4369 | // 0x0F = 3080 Hz |
| 4370 | // 4: speaker volume |
| 4371 | // 5: unknown |
| 4372 | // 6: unknown |
| 4373 | |
| 4374 | write_memory( config, 7, 0x04a20001 ); |
| 4375 | |
| 4376 | unsigned char mem3[] = { 0x01 }; |
| 4377 | write_memory( mem3, 1, 0x04a20008 ); |
| 4378 | |
| 4379 | // unmute |
| 4380 | unsigned char unmute[] = { 0x19, 0x00 }; |
| 4381 | if( force_feedback_enabled ) |
| 4382 | unmute[1] |= 0x01; |
| 4383 | control_send( unmute, 2 ); |
| 4384 | |
| 4385 | // initialize audio buffer, if necessary |
| 4386 | if( audio_buffer == NULL ) |
| 4387 | { |
| 4388 | audio_buffer = new CBufferSimple; |
| 4389 | audio_buffer->initialize( 20 * 5, sizeof( SAMPLE ) ); |
| 4390 | } |
| 4391 | |
| 4392 | // set up audio timer callback, if necessary |
| 4393 | if( timer == NULL ) |
| 4394 | { |
| 4395 | CFRunLoopTimerContext timerContext = { 0, this, NULL, NULL, NULL }; |
| 4396 | timer = CFRunLoopTimerCreate( NULL, CFAbsoluteTimeGetCurrent(), |
| 4397 | 1.0 / 3920.0, 0, 0, |
| 4398 | WiiRemote_send_audio_data, |
| 4399 | &timerContext ); |
| 4400 | CFRunLoopAddTimer( rlHid, timer, kCFRunLoopChuckHidMode ); |
| 4401 | } |
| 4402 | } |
| 4403 | } |
| 4404 | |
| 4405 | void WiiRemote::send_audio_data() |
| 4406 | { |
| 4407 | unsigned char cmd[] = { 0x18, ( 20 << 3 ), |
| 4408 | 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 4409 | 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; |
| 4410 | |
| 4411 | if( force_feedback_enabled ) |
| 4412 | cmd[1] |= 0x01; |
| 4413 | |
| 4414 | control_send( cmd, 22 ); |
| 4415 | } |
| 4416 | |
| 4417 | void Bluetooth_inquiry_device_found( void * userRefCon, |
| 4418 | IOBluetoothDeviceInquiryRef inquiryRef, |
| 4419 | IOBluetoothDeviceRef deviceRef ) |
| 4420 | { |
| 4421 | CFStringRef device_name = IOBluetoothDeviceGetName( deviceRef ); |
| 4422 | const BluetoothDeviceAddress * address = IOBluetoothDeviceGetAddress( deviceRef ); |
| 4423 | |
| 4424 | if( device_name == NULL ) |
| 4425 | return; |
| 4426 | |
| 4427 | // is device Nintendo Wii Remote controller? |
| 4428 | if( CFStringCompare( device_name, CFSTR( "Nintendo RVL-CNT-01" ), 0 ) == 0 ) |
| 4429 | { |
| 4430 | // has this already been detected? |
| 4431 | if( wr_addresses->find( *address ) == wr_addresses->end() ) |
| 4432 | { |
| 4433 | WiiRemote * wr; |
| 4434 | |
| 4435 | if( g_next_real_wiimote >= wiiremotes->size() ) |
| 4436 | { |
| 4437 | wr = new WiiRemote; |
| 4438 | wiiremotes->push_back( wr ); |
| 4439 | } |
| 4440 | |
| 4441 | else if( ( *wiiremotes )[g_next_real_wiimote] == NULL ) |
| 4442 | { |
| 4443 | wr = new WiiRemote; |
| 4444 | ( *wiiremotes )[g_next_real_wiimote] = wr; |
| 4445 | } |
| 4446 | |
| 4447 | else |
| 4448 | { |
| 4449 | wr = ( *wiiremotes )[g_next_real_wiimote]; |
| 4450 | } |
| 4451 | |
| 4452 | // set member data |
| 4453 | wr->device = deviceRef; |
| 4454 | memcpy( &wr->address, address, sizeof( BluetoothDeviceAddress ) ); |
| 4455 | strncpy( wr->name, "Nintendo RVL-CNT-01", 256 ); |
| 4456 | wr->num = g_next_real_wiimote; |
| 4457 | wr->type = CK_HID_DEV_WIIREMOTE; |
| 4458 | |
| 4459 | ( *wr_addresses )[wr->address] = wr; |
| 4460 | |
| 4461 | EM_log( CK_LOG_INFO, "hid: found Wii Remote Controller" ); |
| 4462 | |
| 4463 | if( wr->refcount ) |
| 4464 | wr->open(); |
| 4465 | |
| 4466 | g_next_real_wiimote++; |
| 4467 | } |
| 4468 | |
| 4469 | else if( !( *wr_addresses )[*address]->is_connected() ) |
| 4470 | { |
| 4471 | WiiRemote * wr = ( *wr_addresses )[*address]; |
| 4472 | |
| 4473 | if( wr->refcount ) |
| 4474 | wr->open(); |
| 4475 | } |
| 4476 | } |
| 4477 | |
| 4478 | else |
| 4479 | { |
| 4480 | EM_log( CK_LOG_INFO, "hid: found bluetooth device" ); |
| 4481 | } |
| 4482 | } |
| 4483 | |
| 4484 | int WiiRemote_query(); |
| 4485 | |
| 4486 | void Bluetooth_inquiry_complete( void * userRefCon, |
| 4487 | IOBluetoothDeviceInquiryRef inquiryRef, |
| 4488 | IOReturn error, |
| 4489 | Boolean aborted ) |
| 4490 | { |
| 4491 | g_bt_query_active = FALSE; |
| 4492 | |
| 4493 | IOBluetoothDeviceInquiryDelete( inquiryRef ); |
| 4494 | |
| 4495 | t_CKBOOL do_query = FALSE; |
| 4496 | WiiRemote * wiiremote; |
| 4497 | |
| 4498 | for( int i = 0; i < wiiremotes->size(); i++ ) |
| 4499 | { |
| 4500 | wiiremote = ( *wiiremotes )[i]; |
| 4501 | if( wiiremote->refcount > 0 && !wiiremote->is_connected() ) |
| 4502 | { |
| 4503 | do_query = TRUE; |
| 4504 | break; |
| 4505 | } |
| 4506 | } |
| 4507 | |
| 4508 | if( do_query ) |
| 4509 | { |
| 4510 | EM_log( CK_LOG_INFO, "hid: continuing bluetooth query" ); |
| 4511 | WiiRemote_query(); |
| 4512 | } |
| 4513 | else |
| 4514 | EM_log( CK_LOG_INFO, "hid: ending bluetooth query" ); |
| 4515 | } |
| 4516 | |
| 4517 | int WiiRemote_query() |
| 4518 | { |
| 4519 | EM_log( CK_LOG_INFO, "hid: performing bluetooth query" ); |
| 4520 | if( g_bt_query_active ) |
| 4521 | return 0; |
| 4522 | |
| 4523 | if( IOBluetoothLocalDeviceAvailable() == FALSE ) |
| 4524 | { |
| 4525 | EM_log( CK_LOG_WARNING, "hid: error: bluetooth unavailable" ); |
| 4526 | return -1; |
| 4527 | } |
| 4528 | |
| 4529 | IOBluetoothDeviceInquiryRef btDeviceInquiry = IOBluetoothDeviceInquiryCreateWithCallbackRefCon( NULL ); |
| 4530 | |
| 4531 | if( btDeviceInquiry == NULL ) |
| 4532 | { |
| 4533 | EM_log( CK_LOG_WARNING, "hid: error: creating bluetooth device inquiry" ); |
| 4534 | return -1; |
| 4535 | } |
| 4536 | |
| 4537 | if( IOBluetoothDeviceInquirySetDeviceFoundCallback( btDeviceInquiry, |
| 4538 | Bluetooth_inquiry_device_found ) |
| 4539 | != noErr ) |
| 4540 | { |
| 4541 | EM_log( CK_LOG_WARNING, "hid: error: setting bluetooth device inquiry callback" ); |
| 4542 | return -1; |
| 4543 | } |
| 4544 | |
| 4545 | if( IOBluetoothDeviceInquirySetCompleteCallback( btDeviceInquiry, |
| 4546 | Bluetooth_inquiry_complete ) |
| 4547 | != noErr ) |
| 4548 | { |
| 4549 | EM_log( CK_LOG_WARNING, "hid: error: setting bluetooth device inquiry completion callback" ); |
| 4550 | return -1; |
| 4551 | } |
| 4552 | |
| 4553 | if( IOBluetoothDeviceInquirySetUpdateNewDeviceNames( btDeviceInquiry, |
| 4554 | TRUE ) |
| 4555 | != noErr ) |
| 4556 | { |
| 4557 | EM_log( CK_LOG_WARNING, "hid: error: setting bluetooth device inquiry name update flag" ); |
| 4558 | return -1; |
| 4559 | } |
| 4560 | |
| 4561 | if( IOBluetoothDeviceInquirySetInquiryLength( btDeviceInquiry, 20 ) != noErr ) |
| 4562 | { |
| 4563 | EM_log( CK_LOG_WARNING, "hid: error: setting bluetooth inquiry length" ); |
| 4564 | return -1; |
| 4565 | } |
| 4566 | |
| 4567 | if( IOBluetoothDeviceInquiryStart( btDeviceInquiry ) != noErr ) |
| 4568 | { |
| 4569 | EM_log( CK_LOG_WARNING, "hid: error: starting bluetooth device inquiry" ); |
| 4570 | return -1; |
| 4571 | } |
| 4572 | |
| 4573 | g_bt_query_active = TRUE; |
| 4574 | |
| 4575 | return 0; |
| 4576 | } |
| 4577 | |
| 4578 | /* -- WiiRemote inter-thread communication -- |
| 4579 | Every IOBluetooth function needs to be called from the HID thread because none |
| 4580 | of it is thread safe, and it also requires a CFRunLoop to be running at some |
| 4581 | point. So, WiiRemote_open and _close, which are called from the VM thread, put |
| 4582 | open and close messages into a thread-safe circular buffer and then signal the |
| 4583 | hid thread to indicate that new info is available on the cbuf. The hid thread |
| 4584 | will then process any pending open/close messages in the cbuf. |
| 4585 | |
| 4586 | send messages now also are carried out in the hid thread through this mechanism. |
| 4587 | */ |
| 4588 | |
| 4589 | struct WiiRemoteOp |
| 4590 | { |
| 4591 | enum |
| 4592 | { |
| 4593 | open, |
| 4594 | close, |
| 4595 | send |
| 4596 | } op; |
| 4597 | |
| 4598 | int index; |
| 4599 | }; |
| 4600 | |
| 4601 | static CBufferSimple * WiiRemoteOp_cbuf = NULL; |
| 4602 | static CBufferSimple * WiiRemoteOp_msgbuf = NULL; |
| 4603 | |
| 4604 | static void WiiRemote_cfrl_callback( void * info ) |
| 4605 | { |
| 4606 | WiiRemoteOp wro; |
| 4607 | HidMsg msg; |
| 4608 | t_CKBOOL do_query = FALSE; |
| 4609 | |
| 4610 | while( WiiRemoteOp_cbuf->get( &wro, 1 ) ) |
| 4611 | { |
| 4612 | switch( wro.op ) |
| 4613 | { |
| 4614 | case WiiRemoteOp::open: |
| 4615 | // does it exist already? |
| 4616 | if( wro.index < wiiremotes->size() ) |
| 4617 | // yes |
| 4618 | { |
| 4619 | // is the device connected? |
| 4620 | if( !( *wiiremotes )[wro.index]->is_connected() ) |
| 4621 | // no, so do a query |
| 4622 | do_query = TRUE; |
| 4623 | } |
| 4624 | |
| 4625 | else |
| 4626 | // no, so create it |
| 4627 | { |
| 4628 | while( wro.index > wiiremotes->size() ) |
| 4629 | wiiremotes->push_back( NULL ); |
| 4630 | wiiremotes->push_back( new WiiRemote ); |
| 4631 | |
| 4632 | do_query = TRUE; |
| 4633 | } |
| 4634 | |
| 4635 | ( *wiiremotes )[wro.index]->refcount++; |
| 4636 | |
| 4637 | break; |
| 4638 | |
| 4639 | case WiiRemoteOp::close: |
| 4640 | if( wro.index < wiiremotes->size() ) |
| 4641 | { |
| 4642 | if( --( *wiiremotes )[wro.index]->refcount == 0 ) |
| 4643 | ( *wiiremotes )[wro.index]->close(); |
| 4644 | } |
| 4645 | |
| 4646 | break; |
| 4647 | |
| 4648 | case WiiRemoteOp::send: |
| 4649 | if( WiiRemoteOp_msgbuf->get( &msg, 1 ) ) |
| 4650 | { |
| 4651 | // double-check remote number for validity |
| 4652 | if( wro.index < 0 || wro.index >= wiiremotes->size() || |
| 4653 | ( *wiiremotes )[wro.index] == NULL || |
| 4654 | !( *wiiremotes )[wro.index]->is_connected() ) |
| 4655 | { |
| 4656 | // report failure |
| 4657 | HidInManager::push_message( msg ); |
| 4658 | break; |
| 4659 | } |
| 4660 | |
| 4661 | switch( msg.type ) |
| 4662 | { |
| 4663 | case CK_HID_LED: |
| 4664 | if( msg.eid < 0 || msg.eid >= 4 ) |
| 4665 | { |
| 4666 | // report failure |
| 4667 | HidInManager::push_message( msg ); |
| 4668 | break; |
| 4669 | } |
| 4670 | |
| 4671 | ( *wiiremotes )[wro.index]->set_led( msg.eid, msg.idata[0] ); |
| 4672 | |
| 4673 | break; |
| 4674 | |
| 4675 | case CK_HID_FORCE_FEEDBACK: |
| 4676 | ( *wiiremotes )[wro.index]->enable_force_feedback( msg.idata[0] ); |
| 4677 | |
| 4678 | break; |
| 4679 | } |
| 4680 | } |
| 4681 | |
| 4682 | break; |
| 4683 | } |
| 4684 | } |
| 4685 | |
| 4686 | if( do_query ) |
| 4687 | WiiRemote_query(); |
| 4688 | } |
| 4689 | |
| 4690 | static void WiiRemote_signal() |
| 4691 | { |
| 4692 | if( cfrlWiiRemoteSource && rlHid ) |
| 4693 | { |
| 4694 | CFRunLoopSourceSignal( cfrlWiiRemoteSource ); |
| 4695 | CFRunLoopWakeUp( rlHid ); |
| 4696 | } |
| 4697 | } |
| 4698 | |
| 4699 | #endif // __CK_HID_WIIREMOTE |
| 4700 | |
| 4701 | void WiiRemote_init() |
| 4702 | { |
| 4703 | #ifdef __CK_HID_WIIREMOTE__ |
| 4704 | Hid_init2(); |
| 4705 | |
| 4706 | wiiremotes = new xvector< WiiRemote * >; |
| 4707 | wr_addresses = new map< BluetoothDeviceAddress, WiiRemote * >; |
| 4708 | |
| 4709 | WiiRemoteOp_cbuf = new CBufferSimple; |
| 4710 | WiiRemoteOp_cbuf->initialize( 20, sizeof( WiiRemoteOp ) ); |
| 4711 | |
| 4712 | WiiRemoteOp_msgbuf = new CBufferSimple; |
| 4713 | WiiRemoteOp_msgbuf->initialize( 100, sizeof( HidMsg ) ); |
| 4714 | #endif // __CK_HID_WIIREMOTE |
| 4715 | } |
| 4716 | |
| 4717 | void WiiRemote_poll() |
| 4718 | { |
| 4719 | } |
| 4720 | |
| 4721 | void WiiRemote_quit() |
| 4722 | { |
| 4723 | #ifdef __CK_HID_WIIREMOTE__ |
| 4724 | for( xvector< WiiRemote * >::size_type i = 0; i < wiiremotes->size(); i++ ) |
| 4725 | { |
| 4726 | if( ( *wiiremotes )[i] ) |
| 4727 | CK_SAFE_DELETE( ( *wiiremotes )[i] ); |
| 4728 | } |
| 4729 | CK_SAFE_DELETE( wiiremotes ); |
| 4730 | CK_SAFE_DELETE( wr_addresses ); |
| 4731 | CK_SAFE_DELETE( WiiRemoteOp_cbuf ); |
| 4732 | |
| 4733 | Hid_quit2(); |
| 4734 | #endif // __CK_HID_WIIREMOTE__ |
| 4735 | } |
| 4736 | |
| 4737 | int WiiRemote_count() |
| 4738 | { |
| 4739 | #ifdef __CK_HID_WIIREMOTE__ |
| 4740 | return wiiremotes->size(); |
| 4741 | #else |
| 4742 | return 0; |
| 4743 | #endif // __CK_HID_WIIREMOTE__ |
| 4744 | } |
| 4745 | |
| 4746 | int WiiRemote_open( int wr ) |
| 4747 | { |
| 4748 | #ifdef __CK_HID_WIIREMOTE__ |
| 4749 | WiiRemoteOp wro; |
| 4750 | wro.op = WiiRemoteOp::open; |
| 4751 | wro.index = wr; |
| 4752 | |
| 4753 | WiiRemoteOp_cbuf->put( &wro, 1 ); |
| 4754 | |
| 4755 | WiiRemote_signal(); |
| 4756 | |
| 4757 | return 0; |
| 4758 | #else |
| 4759 | return -1; |
| 4760 | #endif // __CK_HID_WIIREMOTE__ |
| 4761 | } |
| 4762 | |
| 4763 | int WiiRemote_close( int wr ) |
| 4764 | { |
| 4765 | #ifdef __CK_HID_WIIREMOTE__ |
| 4766 | WiiRemoteOp wro; |
| 4767 | wro.op = WiiRemoteOp::close; |
| 4768 | wro.index = wr; |
| 4769 | |
| 4770 | WiiRemoteOp_cbuf->put( &wro, 1 ); |
| 4771 | |
| 4772 | WiiRemote_signal(); |
| 4773 | |
| 4774 | return 0; |
| 4775 | #else |
| 4776 | return -1; |
| 4777 | #endif // __CK_HID_WIIREMOTE__ |
| 4778 | } |
| 4779 | |
| 4780 | int WiiRemote_send( int wr, const HidMsg * msg ) |
| 4781 | { |
| 4782 | #ifdef __CK_HID_WIIREMOTE__ |
| 4783 | wiiremotes->lock(); |
| 4784 | |
| 4785 | if( wr < 0 || wr >= wiiremotes->size() ) |
| 4786 | { |
| 4787 | wiiremotes->unlock(); |
| 4788 | return -1; |
| 4789 | } |
| 4790 | |
| 4791 | WiiRemote * wiiremote = ( *wiiremotes )[wr]; |
| 4792 | |
| 4793 | wiiremotes->unlock(); |
| 4794 | |
| 4795 | if( wiiremote == NULL ) |
| 4796 | return -1; |
| 4797 | |
| 4798 | |
| 4799 | wiiremote->lock(); |
| 4800 | |
| 4801 | if( !wiiremote->is_connected() ) |
| 4802 | { |
| 4803 | wiiremote->unlock(); |
| 4804 | return -1; |
| 4805 | } |
| 4806 | |
| 4807 | wiiremote->unlock(); |
| 4808 | |
| 4809 | WiiRemoteOp_msgbuf->put( ( void * )msg, 1 ); |
| 4810 | |
| 4811 | WiiRemoteOp wro; |
| 4812 | wro.op = WiiRemoteOp::send; |
| 4813 | wro.index = wr; |
| 4814 | |
| 4815 | WiiRemoteOp_cbuf->put( &wro, 1 ); |
| 4816 | |
| 4817 | WiiRemote_signal(); |
| 4818 | |
| 4819 | return 0; |
| 4820 | #else |
| 4821 | return -1; |
| 4822 | #endif |
| 4823 | } |
| 4824 | |
| 4825 | const char * WiiRemote_name( int wr ) |
| 4826 | { |
| 4827 | #ifdef __CK_HID_WIIREMOTE__ |
| 4828 | return "Nintendo Wii Remote"; |
| 4829 | #else |
| 4830 | return " "; |
| 4831 | #endif // __CK_HID_WIIREMOTE__ |
| 4832 | } |
| 4833 | |
| 4834 | #elif ( defined(__PLATFORM_WINDOWS__) || defined(__WINDOWS_PTHREAD__) ) && !defined( USE_RAWINPUT ) |
| 4835 | /***************************************************************************** |
| 4836 | Windows general HID support |
| 4837 | *****************************************************************************/ |
| 4838 | #pragma mark Windows general HID support |
| 4839 | |
| 4840 | #include <windows.h> |
| 4841 | #include <winuser.h> |
| 4842 | |
| 4843 | // 1.5.0.7 (ge) make "__WINDOWS_MODERN__" default |
| 4844 | // previously: #ifdef __WINDOWS_MODERN__ |
| 4845 | #ifndef __WINDOWS_OLDSCHOOL__ // REFACTOR-2017 |
| 4846 | #define DIRECTINPUT_VERSION 0x0800 |
| 4847 | #else // earlier windows |
| 4848 | #ifdef __USE_DINPUT8LIB__ |
| 4849 | #define DIRECTINPUT_VERSION 0x0800 |
| 4850 | #else |
| 4851 | #define DIRECTINPUT_VERSION 0x0500 |
| 4852 | #endif |
| 4853 | #endif |
| 4854 | #include <dinput.h> |
| 4855 | |
| 4856 | /* for performance, we use device event notifications to tell us when a device |
| 4857 | has new data. However, serial devices don't seem to support event |
| 4858 | notification, so we have to fall back to the periodic sleeping and polling. |
| 4859 | g_wait_function is usually set to call WaitForSingleObject (wait for event |
| 4860 | notification), but devices for which SetEventNotification fails will replace |
| 4861 | it with a wrapper around usleep. */ |
| 4862 | static HANDLE g_device_event = NULL; |
| 4863 | static void (*g_wait_function)() = NULL; |
| 4864 | |
| 4865 | static void Hid_wait_usleep() |
| 4866 | { |
| 4867 | ck_usleep( 10 ); |
| 4868 | } |
| 4869 | |
| 4870 | static void Hid_wait_event() |
| 4871 | { |
| 4872 | WaitForSingleObject( g_device_event, INFINITE ); |
| 4873 | } |
| 4874 | |
| 4875 | void Hid_init() |
| 4876 | { |
| 4877 | if( g_device_event != NULL ) |
| 4878 | return; |
| 4879 | |
| 4880 | g_device_event = CreateEvent( NULL, FALSE, FALSE, NULL ); |
| 4881 | if( g_device_event == NULL ) |
| 4882 | EM_log( CK_LOG_HERALD, "hid: error: unable to create event (win32 error %i)", GetLastError() ); |
| 4883 | g_wait_function = Hid_wait_event; |
| 4884 | } |
| 4885 | |
| 4886 | void Hid_poll() |
| 4887 | { |
| 4888 | g_wait_function(); |
| 4889 | Joystick_poll(); |
| 4890 | Keyboard_poll(); |
| 4891 | Mouse_poll(); |
| 4892 | } |
| 4893 | |
| 4894 | void Hid_quit() |
| 4895 | { |
| 4896 | SetEvent( g_device_event ); |
| 4897 | /*if( g_device_event != NULL ) |
| 4898 | { |
| 4899 | CloseHandle( g_device_event ); |
| 4900 | g_device_event = NULL; |
| 4901 | }*/ |
| 4902 | } |
| 4903 | |
| 4904 | |
| 4905 | |
| 4906 | |
| 4907 | /***************************************************************************** |
| 4908 | ge: Windows tiltsensor non-support |
| 4909 | *****************************************************************************/ |
| 4910 | // designate new poll rate |
| 4911 | t_CKINT TiltSensor_setPollRate( t_CKINT usec ) |
| 4912 | { |
| 4913 | // sanity |
| 4914 | assert( usec >= 0 ); |
| 4915 | // not supported |
| 4916 | CK_FPRINTF_STDERR( "TiltSensor - setPollRate is not (yet) supported on this platform...\n" ); |
| 4917 | return -1; |
| 4918 | } |
| 4919 | |
| 4920 | // query current poll rate |
| 4921 | t_CKINT TiltSensor_getPollRate( ) |
| 4922 | { |
| 4923 | // not supported |
| 4924 | CK_FPRINTF_STDERR( "TiltSensor - getPollRate is not (yet) supported on this platform...\n" ); |
| 4925 | return -1; |
| 4926 | } |
| 4927 | |
| 4928 | |
| 4929 | |
| 4930 | |
| 4931 | /***************************************************************************** |
| 4932 | Windows joystick support |
| 4933 | *****************************************************************************/ |
| 4934 | #pragma mark Windows joystick support |
| 4935 | |
| 4936 | static LPDIRECTINPUT lpdi = NULL; |
| 4937 | |
| 4938 | struct win32_joystick |
| 4939 | { |
| 4940 | win32_joystick() |
| 4941 | { |
| 4942 | lpdiJoystick = NULL; |
| 4943 | refcount = 0; |
| 4944 | needs_close = FALSE; |
| 4945 | strncpy( name, "Joystick", MAX_PATH ); |
| 4946 | memset( &last_state, 0, sizeof( last_state ) ); |
| 4947 | memset( &caps, 0, sizeof( caps ) ); |
| 4948 | caps.dwSize = sizeof( DIDEVCAPS ); |
| 4949 | } |
| 4950 | |
| 4951 | LPDIRECTINPUTDEVICE2 lpdiJoystick; |
| 4952 | DIJOYSTATE2 last_state; |
| 4953 | DIDEVCAPS caps; |
| 4954 | |
| 4955 | char name[MAX_PATH]; |
| 4956 | |
| 4957 | t_CKUINT refcount; |
| 4958 | t_CKBOOL needs_close; |
| 4959 | |
| 4960 | }; |
| 4961 | |
| 4962 | const static LONG axis_min = -32767; |
| 4963 | const static LONG axis_max = 32767; |
| 4964 | |
| 4965 | |
| 4966 | static vector< win32_joystick * > * joysticks; |
| 4967 | |
| 4968 | static BOOL CALLBACK DIEnumJoystickProc( LPCDIDEVICEINSTANCE lpddi, |
| 4969 | LPVOID pvRef ) |
| 4970 | { |
| 4971 | GUID guid = lpddi->guidInstance; |
| 4972 | win32_joystick * js = new win32_joystick; |
| 4973 | |
| 4974 | EM_log( CK_LOG_INFO, "found %s", lpddi->tszProductName ); |
| 4975 | |
| 4976 | strncpy( js->name, lpddi->tszProductName, MAX_PATH ); |
| 4977 | |
| 4978 | if( lpdi->CreateDevice( guid, ( LPDIRECTINPUTDEVICE * )&js->lpdiJoystick, NULL ) != DI_OK ) |
| 4979 | { |
| 4980 | delete js; |
| 4981 | return DIENUM_CONTINUE; |
| 4982 | } |
| 4983 | |
| 4984 | joysticks->push_back( js ); |
| 4985 | |
| 4986 | return DIENUM_CONTINUE; |
| 4987 | } |
| 4988 | |
| 4989 | static BOOL CALLBACK DIEnumJoystickObjectsProc( LPCDIDEVICEOBJECTINSTANCE lpdidoi, |
| 4990 | LPVOID pvRef ) |
| 4991 | { |
| 4992 | LPDIRECTINPUTDEVICE lpdiJoystick = ( LPDIRECTINPUTDEVICE ) pvRef; |
| 4993 | |
| 4994 | DIPROPRANGE diprg; |
| 4995 | |
| 4996 | // set axis minimum and maximum range |
| 4997 | diprg.diph.dwSize = sizeof(DIPROPRANGE); |
| 4998 | diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER); |
| 4999 | diprg.diph.dwHow = DIPH_BYID; |
| 5000 | diprg.diph.dwObj = lpdidoi->dwType; |
| 5001 | diprg.lMin = axis_min; |
| 5002 | diprg.lMax = axis_max; |
| 5003 | |
| 5004 | if( lpdiJoystick->SetProperty( DIPROP_RANGE, &diprg.diph ) != DI_OK ) |
| 5005 | { |
| 5006 | |
| 5007 | } |
| 5008 | |
| 5009 | return DIENUM_CONTINUE; |
| 5010 | } |
| 5011 | |
| 5012 | void Joystick_init() |
| 5013 | { |
| 5014 | if( joysticks != NULL ) |
| 5015 | return; |
| 5016 | |
| 5017 | EM_log( CK_LOG_INFO, "initializing joystick" ); |
| 5018 | EM_pushlog(); |
| 5019 | |
| 5020 | HINSTANCE hInstance = GetModuleHandle( NULL ); |
| 5021 | |
| 5022 | if( lpdi == NULL ) |
| 5023 | { |
| 5024 | #if DIRECTINPUT_VERSION > 0x0700 // REFACTOR-2017 |
| 5025 | if( DirectInput8Create( hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, |
| 5026 | (LPVOID *) &lpdi, NULL) != DI_OK ) |
| 5027 | #else |
| 5028 | if( DirectInputCreate( hInstance, DIRECTINPUT_VERSION, |
| 5029 | &lpdi, NULL) != DI_OK ) |
| 5030 | #endif |
| 5031 | { |
| 5032 | lpdi = NULL; |
| 5033 | EM_log( CK_LOG_HERALD, "error: unable to initialize DirectInput, initialization failed" ); |
| 5034 | EM_poplog(); |
| 5035 | return; |
| 5036 | } |
| 5037 | } |
| 5038 | |
| 5039 | joysticks = new vector< win32_joystick * >; |
| 5040 | #if DIRECTINPUT_VERSION <= 0x700 // REFACTOR-2017 |
| 5041 | if( lpdi->EnumDevices( DIDEVTYPE_JOYSTICK, DIEnumJoystickProc, |
| 5042 | #else |
| 5043 | // fix 1.5.0.1 (Spencer 2023): use DI8DEVCLASS_GAMECTRL instead of DI8DEVTYPE_JOYSTICK |
| 5044 | // cf. EnumDevices documentation https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ee417804(v=vs.85) |
| 5045 | // this seems to fix joystick usage in general, but Xbox One controller still fails |
| 5046 | // XBox One controller workaround from otherTom#4512 |
| 5047 | // *** open up Device Manager, expand "Human Interface Devices", |
| 5048 | // right click "XINPUT compatible HID device", "Update driver", |
| 5049 | // "Browse my computer", "Let me pick". should be three options. |
| 5050 | // Try "HID-compliant game controller". If you want to undo the |
| 5051 | // change, same thing but update "HID-compliant game controller" |
| 5052 | // and pick the XINPUT *** |
| 5053 | // https://www.snes9x.com/phpbb3/viewtopic.php?t=27510&sid=661fbbc1609037a8ec2e5f10e003f5a0 |
| 5054 | // was: if (lpdi->EnumDevices(DI8DEVTYPE_JOYSTICK, DIEnumJoystickProc, |
| 5055 | if( lpdi->EnumDevices( DI8DEVCLASS_GAMECTRL, DIEnumJoystickProc, |
| 5056 | #endif |
| 5057 | NULL, DIEDFL_ATTACHEDONLY ) != DI_OK ) |
| 5058 | { |
| 5059 | delete joysticks; |
| 5060 | joysticks = NULL; |
| 5061 | lpdi->Release(); |
| 5062 | lpdi = NULL; |
| 5063 | EM_log( CK_LOG_HERALD, "error: unable to enumerate devices, initialization failed" ); |
| 5064 | EM_poplog(); |
| 5065 | return; |
| 5066 | } |
| 5067 | |
| 5068 | EM_poplog(); |
| 5069 | } |
| 5070 | |
| 5071 | t_CKINT Joystick_translate_POV( DWORD v ) |
| 5072 | { |
| 5073 | #define CK_POV_CENTER 0 |
| 5074 | #define CK_POV_UP 1 |
| 5075 | #define CK_POV_RIGHT 2 |
| 5076 | #define CK_POV_DOWN 4 |
| 5077 | #define CK_POV_LEFT 8 |
| 5078 | |
| 5079 | #define CK_POV_UP_LBORDER 29250 |
| 5080 | #define CK_POV_UP_RBORDER 6750 |
| 5081 | #define CK_POV_RIGHT_LBORDER 2250 |
| 5082 | #define CK_POV_RIGHT_RBORDER 15750 |
| 5083 | #define CK_POV_DOWN_LBORDER 11250 |
| 5084 | #define CK_POV_DOWN_RBORDER 24750 |
| 5085 | #define CK_POV_LEFT_LBORDER 20250 |
| 5086 | #define CK_POV_LEFT_RBORDER 33750 |
| 5087 | |
| 5088 | t_CKINT r = 0; |
| 5089 | |
| 5090 | if( LOWORD(v) == 0xffff ) |
| 5091 | return CK_POV_CENTER; |
| 5092 | |
| 5093 | if( v > CK_POV_UP_LBORDER || v < CK_POV_UP_RBORDER ) |
| 5094 | r |= CK_POV_UP; |
| 5095 | if( v > CK_POV_RIGHT_LBORDER && v < CK_POV_RIGHT_RBORDER ) |
| 5096 | r |= CK_POV_RIGHT; |
| 5097 | if( v > CK_POV_DOWN_LBORDER && v < CK_POV_DOWN_RBORDER ) |
| 5098 | r |= CK_POV_DOWN; |
| 5099 | if( v > CK_POV_LEFT_LBORDER && v < CK_POV_LEFT_RBORDER ) |
| 5100 | r |= CK_POV_LEFT; |
| 5101 | |
| 5102 | return r; |
| 5103 | } |
| 5104 | |
| 5105 | void Joystick_poll() |
| 5106 | { |
| 5107 | if( !joysticks ) |
| 5108 | return; |
| 5109 | |
| 5110 | win32_joystick * joystick; |
| 5111 | HidMsg msg; |
| 5112 | vector< win32_joystick * >::size_type i, len = joysticks->size(); |
| 5113 | int j; |
| 5114 | for( i = 0; i < len; i++ ) |
| 5115 | { |
| 5116 | joystick = joysticks->at( i ); |
| 5117 | if( joystick->refcount ) |
| 5118 | { |
| 5119 | // TODO: convert this to buffered input, or maybe notifications |
| 5120 | DIJOYSTATE2 state; |
| 5121 | |
| 5122 | joystick->lpdiJoystick->Poll(); |
| 5123 | |
| 5124 | if( joystick->lpdiJoystick->GetDeviceState( sizeof( DIJOYSTATE2 ), &state ) |
| 5125 | != DI_OK ) |
| 5126 | { |
| 5127 | EM_log( CK_LOG_WARNING, "joystick: GetDeviceState failed for %s", joystick->name ); |
| 5128 | continue; |
| 5129 | } |
| 5130 | |
| 5131 | if( state.lX != joystick->last_state.lX ) |
| 5132 | { |
| 5133 | msg.clear(); |
| 5134 | msg.device_num = i; |
| 5135 | msg.device_type = CK_HID_DEV_JOYSTICK; |
| 5136 | msg.eid = 0; |
| 5137 | msg.type = CK_HID_JOYSTICK_AXIS; |
| 5138 | msg.fdata[0] = ((float)state.lX)/((float)axis_max); |
| 5139 | HidInManager::push_message( msg ); |
| 5140 | } |
| 5141 | |
| 5142 | if( state.lY != joystick->last_state.lY ) |
| 5143 | { |
| 5144 | msg.clear(); |
| 5145 | msg.device_num = i; |
| 5146 | msg.device_type = CK_HID_DEV_JOYSTICK; |
| 5147 | msg.eid = 1; |
| 5148 | msg.type = CK_HID_JOYSTICK_AXIS; |
| 5149 | msg.fdata[0] = ((float)state.lY)/((float)axis_max); |
| 5150 | HidInManager::push_message( msg ); |
| 5151 | } |
| 5152 | |
| 5153 | if( state.lZ != joystick->last_state.lZ ) |
| 5154 | { |
| 5155 | msg.clear(); |
| 5156 | msg.device_num = i; |
| 5157 | msg.device_type = CK_HID_DEV_JOYSTICK; |
| 5158 | msg.eid = 2; |
| 5159 | msg.type = CK_HID_JOYSTICK_AXIS; |
| 5160 | msg.fdata[0] = ((float)state.lZ)/((float)axis_max); |
| 5161 | HidInManager::push_message( msg ); |
| 5162 | } |
| 5163 | |
| 5164 | if( state.lRx != joystick->last_state.lRx ) |
| 5165 | { |
| 5166 | msg.clear(); |
| 5167 | msg.device_num = i; |
| 5168 | msg.device_type = CK_HID_DEV_JOYSTICK; |
| 5169 | msg.eid = 3; |
| 5170 | msg.type = CK_HID_JOYSTICK_AXIS; |
| 5171 | msg.fdata[0] = ((float)state.lRx)/((float)axis_max); |
| 5172 | HidInManager::push_message( msg ); |
| 5173 | } |
| 5174 | |
| 5175 | if( state.lRy != joystick->last_state.lRy ) |
| 5176 | { |
| 5177 | msg.clear(); |
| 5178 | msg.device_num = i; |
| 5179 | msg.device_type = CK_HID_DEV_JOYSTICK; |
| 5180 | msg.eid = 4; |
| 5181 | msg.type = CK_HID_JOYSTICK_AXIS; |
| 5182 | msg.fdata[0] = ((float)state.lRy)/((float)axis_max); |
| 5183 | HidInManager::push_message( msg ); |
| 5184 | } |
| 5185 | |
| 5186 | if( state.lRz != joystick->last_state.lRz ) |
| 5187 | { |
| 5188 | msg.clear(); |
| 5189 | msg.device_num = i; |
| 5190 | msg.device_type = CK_HID_DEV_JOYSTICK; |
| 5191 | msg.eid = 5; |
| 5192 | msg.type = CK_HID_JOYSTICK_AXIS; |
| 5193 | msg.fdata[0] = ((float)state.lRz)/((float)axis_max); |
| 5194 | HidInManager::push_message( msg ); |
| 5195 | } |
| 5196 | |
| 5197 | for( j = 0; j < 2; j++ ) |
| 5198 | { |
| 5199 | if( state.rglSlider[j] != joystick->last_state.rglSlider[j] ) |
| 5200 | { |
| 5201 | msg.clear(); |
| 5202 | msg.device_num = i; |
| 5203 | msg.device_type = CK_HID_DEV_JOYSTICK; |
| 5204 | msg.eid = 5 + j; |
| 5205 | msg.type = CK_HID_JOYSTICK_AXIS; |
| 5206 | msg.fdata[0] = ((float)state.rglSlider[j])/((float)axis_max); |
| 5207 | HidInManager::push_message( msg ); |
| 5208 | } |
| 5209 | } |
| 5210 | |
| 5211 | for( j = 0; j < joystick->caps.dwPOVs && j < 4; j++ ) |
| 5212 | { |
| 5213 | if( state.rgdwPOV[j] != joystick->last_state.rgdwPOV[j] ) |
| 5214 | { |
| 5215 | msg.clear(); |
| 5216 | msg.device_num = i; |
| 5217 | msg.device_type = CK_HID_DEV_JOYSTICK; |
| 5218 | msg.eid = j; |
| 5219 | msg.type = CK_HID_JOYSTICK_HAT; |
| 5220 | msg.idata[0] = Joystick_translate_POV( state.rgdwPOV[j] ); |
| 5221 | msg.fdata[0] = (t_CKFLOAT)state.rgdwPOV[j]; |
| 5222 | HidInManager::push_message( msg ); |
| 5223 | } |
| 5224 | } |
| 5225 | |
| 5226 | for( j = 0; j < joystick->caps.dwButtons && j < 128; j++ ) |
| 5227 | { |
| 5228 | if( ( state.rgbButtons[j] & 0x80 ) ^ |
| 5229 | ( joystick->last_state.rgbButtons[j] & 0x80 ) ) |
| 5230 | { |
| 5231 | msg.clear(); |
| 5232 | msg.device_num = i; |
| 5233 | msg.device_type = CK_HID_DEV_JOYSTICK; |
| 5234 | msg.eid = j; |
| 5235 | msg.type = ( state.rgbButtons[j] & 0x80 ) ? CK_HID_BUTTON_DOWN : |
| 5236 | CK_HID_BUTTON_UP; |
| 5237 | msg.idata[0] = ( state.rgbButtons[j] & 0x80 ) ? 1 : 0; |
| 5238 | HidInManager::push_message( msg ); |
| 5239 | } |
| 5240 | } |
| 5241 | |
| 5242 | joystick->last_state = state; |
| 5243 | } |
| 5244 | |
| 5245 | else if( joystick->needs_close ) |
| 5246 | { |
| 5247 | joystick->needs_close = FALSE; |
| 5248 | joystick->lpdiJoystick->Unacquire(); |
| 5249 | joystick->lpdiJoystick->SetEventNotification( NULL ); |
| 5250 | } |
| 5251 | } |
| 5252 | |
| 5253 | } |
| 5254 | |
| 5255 | void Joystick_quit() |
| 5256 | { |
| 5257 | if( joysticks ) |
| 5258 | { |
| 5259 | win32_joystick * joystick; |
| 5260 | vector< win32_joystick * >::size_type i, len = joysticks->size(); |
| 5261 | for( i = 0; i < len; i++ ) |
| 5262 | { |
| 5263 | joystick = joysticks->at( i ); |
| 5264 | |
| 5265 | if( joystick->refcount > 0 || joystick->needs_close) |
| 5266 | { |
| 5267 | joystick->needs_close = FALSE; |
| 5268 | joystick->refcount = 0; |
| 5269 | joystick->lpdiJoystick->Unacquire(); |
| 5270 | joystick->lpdiJoystick->SetEventNotification( NULL ); |
| 5271 | } |
| 5272 | |
| 5273 | joystick->lpdiJoystick->Release(); |
| 5274 | delete joystick; |
| 5275 | } |
| 5276 | |
| 5277 | delete joysticks; |
| 5278 | joysticks = NULL; |
| 5279 | } |
| 5280 | |
| 5281 | if( lpdi ) |
| 5282 | { |
| 5283 | lpdi->Release(); |
| 5284 | lpdi = NULL; |
| 5285 | } |
| 5286 | } |
| 5287 | |
| 5288 | int Joystick_count() |
| 5289 | { |
| 5290 | if( !joysticks ) |
| 5291 | return 0; |
| 5292 | return (int)joysticks->size(); |
| 5293 | } |
| 5294 | |
| 5295 | int Joystick_open( int js ) |
| 5296 | { |
| 5297 | if( !joysticks || js < 0 || js >= joysticks->size() ) |
| 5298 | return -1; |
| 5299 | |
| 5300 | win32_joystick * joystick = joysticks->at( js ); |
| 5301 | |
| 5302 | if( joystick->refcount == 0 ) |
| 5303 | { |
| 5304 | if( joystick->lpdiJoystick->EnumObjects( DIEnumJoystickObjectsProc, |
| 5305 | joystick->lpdiJoystick, |
| 5306 | DIDFT_AXIS ) != DI_OK ) |
| 5307 | { |
| 5308 | return -1; |
| 5309 | } |
| 5310 | |
| 5311 | if( joystick->lpdiJoystick->GetCapabilities( &joystick->caps ) != DI_OK ) |
| 5312 | { |
| 5313 | return -1; |
| 5314 | } |
| 5315 | |
| 5316 | if( joystick->lpdiJoystick->SetDataFormat( &c_dfDIJoystick2 ) != DI_OK ) |
| 5317 | { |
| 5318 | return -1; |
| 5319 | } |
| 5320 | |
| 5321 | if( joystick->lpdiJoystick->SetEventNotification( g_device_event ) != DI_OK ) |
| 5322 | { |
| 5323 | // fallback to sleep+poll mode |
| 5324 | g_wait_function = Hid_wait_usleep; |
| 5325 | SetEvent( g_device_event ); |
| 5326 | } |
| 5327 | |
| 5328 | if( joystick->lpdiJoystick->Acquire() != DI_OK ) |
| 5329 | { |
| 5330 | joystick->lpdiJoystick->SetEventNotification( NULL ); |
| 5331 | return -1; |
| 5332 | } |
| 5333 | } |
| 5334 | |
| 5335 | joystick->refcount++; |
| 5336 | |
| 5337 | return 0; |
| 5338 | } |
| 5339 | |
| 5340 | int Joystick_close( int js ) |
| 5341 | { |
| 5342 | if( !joysticks || js < 0 || js >= joysticks->size() ) |
| 5343 | return -1; |
| 5344 | |
| 5345 | win32_joystick * joystick = joysticks->at( js ); |
| 5346 | |
| 5347 | joystick->refcount--; |
| 5348 | |
| 5349 | if( joystick->refcount < 1 ) |
| 5350 | joystick->needs_close = TRUE; |
| 5351 | |
| 5352 | return 0; |
| 5353 | } |
| 5354 | |
| 5355 | const char * Joystick_name( int js ) |
| 5356 | { |
| 5357 | if( !joysticks || js < 0 || js >= joysticks->size() ) |
| 5358 | return NULL; |
| 5359 | |
| 5360 | return joysticks->at( js )->name; |
| 5361 | } |
| 5362 | |
| 5363 | /***************************************************************************** |
| 5364 | Windows keyboard support |
| 5365 | *****************************************************************************/ |
| 5366 | #pragma mark Windows keyboards support |
| 5367 | |
| 5368 | #define DINPUT_KEYBUFFER_SIZE 256 |
| 5369 | |
| 5370 | struct win32_keyboard |
| 5371 | { |
| 5372 | win32_keyboard() |
| 5373 | { |
| 5374 | lpdiKeyboard = NULL; |
| 5375 | refcount = 0; |
| 5376 | needs_close = FALSE; |
| 5377 | strncpy( name, "Keyboard", MAX_PATH ); |
| 5378 | memset( &last_state, 0, DINPUT_KEYBUFFER_SIZE ); |
| 5379 | } |
| 5380 | |
| 5381 | LPDIRECTINPUTDEVICE2 lpdiKeyboard; |
| 5382 | char last_state[DINPUT_KEYBUFFER_SIZE]; |
| 5383 | DIDEVCAPS caps; |
| 5384 | |
| 5385 | char name[MAX_PATH]; |
| 5386 | |
| 5387 | t_CKUINT refcount; |
| 5388 | t_CKBOOL needs_close; |
| 5389 | |
| 5390 | }; |
| 5391 | |
| 5392 | static vector< win32_keyboard * > * keyboards; |
| 5393 | |
| 5394 | // table to translate DirectInput keybuffer offsets to ASCII and USB usages |
| 5395 | // first element is ASCII |
| 5396 | // second element is USB usage |
| 5397 | static unsigned short kb_translation_table[DINPUT_KEYBUFFER_SIZE]; |
| 5398 | |
| 5399 | static void Keyboard_init_translation_table() |
| 5400 | { |
| 5401 | memset( &kb_translation_table, 0, sizeof( kb_translation_table ) ); |
| 5402 | |
| 5403 | kb_translation_table[DIK_0] = '0'; |
| 5404 | kb_translation_table[DIK_0] |= 0x27 << 8; |
| 5405 | |
| 5406 | kb_translation_table[DIK_1] = '1'; |
| 5407 | kb_translation_table[DIK_1] |= 0x1e << 8; |
| 5408 | |
| 5409 | kb_translation_table[DIK_2] = '2'; |
| 5410 | kb_translation_table[DIK_2] |= 0x1f << 8; |
| 5411 | |
| 5412 | kb_translation_table[DIK_3] = '3'; |
| 5413 | kb_translation_table[DIK_3] |= 0x20 << 8; |
| 5414 | |
| 5415 | kb_translation_table[DIK_4] = '4'; |
| 5416 | kb_translation_table[DIK_4] |= 0x21 << 8; |
| 5417 | |
| 5418 | kb_translation_table[DIK_5] = '5'; |
| 5419 | kb_translation_table[DIK_5] |= 0x22 << 8; |
| 5420 | |
| 5421 | kb_translation_table[DIK_6] = '6'; |
| 5422 | kb_translation_table[DIK_6] |= 0x23 << 8; |
| 5423 | |
| 5424 | kb_translation_table[DIK_7] = '7'; |
| 5425 | kb_translation_table[DIK_7] |= 0x24 << 8; |
| 5426 | |
| 5427 | kb_translation_table[DIK_8] = '8'; |
| 5428 | kb_translation_table[DIK_8] |= 0x25 << 8; |
| 5429 | |
| 5430 | kb_translation_table[DIK_9] = '9'; |
| 5431 | kb_translation_table[DIK_9] |= 0x26 << 8; |
| 5432 | |
| 5433 | kb_translation_table[DIK_A] = 'A'; |
| 5434 | kb_translation_table[DIK_A] |= 0x04 << 8; |
| 5435 | |
| 5436 | kb_translation_table[DIK_B] = 'B'; |
| 5437 | kb_translation_table[DIK_B] |= 0x05 << 8; |
| 5438 | |
| 5439 | kb_translation_table[DIK_C] = 'C'; |
| 5440 | kb_translation_table[DIK_C] |= 0x06 << 8; |
| 5441 | |
| 5442 | kb_translation_table[DIK_D] = 'D'; |
| 5443 | kb_translation_table[DIK_D] |= 0x07 << 8; |
| 5444 | |
| 5445 | kb_translation_table[DIK_E] = 'E'; |
| 5446 | kb_translation_table[DIK_E] |= 0x08 << 8; |
| 5447 | |
| 5448 | kb_translation_table[DIK_F] = 'F'; |
| 5449 | kb_translation_table[DIK_F] |= 0x09 << 8; |
| 5450 | |
| 5451 | kb_translation_table[DIK_G] = 'G'; |
| 5452 | kb_translation_table[DIK_G] |= 0x0a << 8; |
| 5453 | |
| 5454 | kb_translation_table[DIK_H] = 'H'; |
| 5455 | kb_translation_table[DIK_H] |= 0x0b << 8; |
| 5456 | |
| 5457 | kb_translation_table[DIK_I] = 'I'; |
| 5458 | kb_translation_table[DIK_I] |= 0x0c << 8; |
| 5459 | |
| 5460 | kb_translation_table[DIK_J] = 'J'; |
| 5461 | kb_translation_table[DIK_J] |= 0x0d << 8; |
| 5462 | |
| 5463 | kb_translation_table[DIK_K] = 'K'; |
| 5464 | kb_translation_table[DIK_K] |= 0x0e << 8; |
| 5465 | |
| 5466 | kb_translation_table[DIK_L] = 'L'; |
| 5467 | kb_translation_table[DIK_L] |= 0x0f << 8; |
| 5468 | |
| 5469 | kb_translation_table[DIK_M] = 'M'; |
| 5470 | kb_translation_table[DIK_M] |= 0x10 << 8; |
| 5471 | |
| 5472 | kb_translation_table[DIK_N] = 'N'; |
| 5473 | kb_translation_table[DIK_N] |= 0x11 << 8; |
| 5474 | |
| 5475 | kb_translation_table[DIK_O] = 'O'; |
| 5476 | kb_translation_table[DIK_O] |= 0x12 << 8; |
| 5477 | |
| 5478 | kb_translation_table[DIK_P] = 'P'; |
| 5479 | kb_translation_table[DIK_P] |= 0x13 << 8; |
| 5480 | |
| 5481 | kb_translation_table[DIK_Q] = 'Q'; |
| 5482 | kb_translation_table[DIK_Q] |= 0x14 << 8; |
| 5483 | |
| 5484 | kb_translation_table[DIK_R] = 'R'; |
| 5485 | kb_translation_table[DIK_R] |= 0x15 << 8; |
| 5486 | |
| 5487 | kb_translation_table[DIK_S] = 'S'; |
| 5488 | kb_translation_table[DIK_S] |= 0x16 << 8; |
| 5489 | |
| 5490 | kb_translation_table[DIK_T] = 'T'; |
| 5491 | kb_translation_table[DIK_T] |= 0x17 << 8; |
| 5492 | |
| 5493 | kb_translation_table[DIK_U] = 'U'; |
| 5494 | kb_translation_table[DIK_U] |= 0x18 << 8; |
| 5495 | |
| 5496 | kb_translation_table[DIK_V] = 'V'; |
| 5497 | kb_translation_table[DIK_V] |= 0x19 << 8; |
| 5498 | |
| 5499 | kb_translation_table[DIK_W] = 'W'; |
| 5500 | kb_translation_table[DIK_W] |= 0x1a << 8; |
| 5501 | |
| 5502 | kb_translation_table[DIK_X] = 'X'; |
| 5503 | kb_translation_table[DIK_X] |= 0x1b << 8; |
| 5504 | |
| 5505 | kb_translation_table[DIK_Y] = 'Y'; |
| 5506 | kb_translation_table[DIK_Y] |= 0x1c << 8; |
| 5507 | |
| 5508 | kb_translation_table[DIK_Z] = 'Z'; |
| 5509 | kb_translation_table[DIK_Z] |= 0x1d << 8; |
| 5510 | |
| 5511 | kb_translation_table[DIK_ADD] = '+'; |
| 5512 | kb_translation_table[DIK_ADD] |= 0x57 << 8; |
| 5513 | |
| 5514 | kb_translation_table[DIK_APOSTROPHE] = '\''; |
| 5515 | kb_translation_table[DIK_APOSTROPHE] |= 0x34 << 8; |
| 5516 | |
| 5517 | kb_translation_table[DIK_APPS] = 0; |
| 5518 | kb_translation_table[DIK_APPS] |= 0x65 << 8; |
| 5519 | |
| 5520 | kb_translation_table[DIK_BACK] = '\b'; |
| 5521 | kb_translation_table[DIK_BACK] |= 0x2a << 8; |
| 5522 | |
| 5523 | kb_translation_table[DIK_BACKSLASH] = '\\'; |
| 5524 | kb_translation_table[DIK_BACKSLASH] |= 0x31 << 8; |
| 5525 | |
| 5526 | kb_translation_table[DIK_CAPITAL] = 0; |
| 5527 | kb_translation_table[DIK_CAPITAL] |= 0x39 << 8; |
| 5528 | |
| 5529 | kb_translation_table[DIK_COLON] = ':'; |
| 5530 | kb_translation_table[DIK_COLON] |= 0x00 << 8; |
| 5531 | |
| 5532 | kb_translation_table[DIK_COMMA] = ','; |
| 5533 | kb_translation_table[DIK_COMMA] |= 0x36 << 8; |
| 5534 | |
| 5535 | kb_translation_table[DIK_DECIMAL] = '.'; |
| 5536 | kb_translation_table[DIK_DECIMAL] |= 0x63 << 8; |
| 5537 | |
| 5538 | kb_translation_table[DIK_DELETE] = 0x7f; |
| 5539 | kb_translation_table[DIK_DELETE] |= 0x4c << 8; |
| 5540 | |
| 5541 | kb_translation_table[DIK_DIVIDE] = '/'; |
| 5542 | kb_translation_table[DIK_DIVIDE] |= 0x54 << 8; |
| 5543 | |
| 5544 | kb_translation_table[DIK_DOWN] = 0; |
| 5545 | kb_translation_table[DIK_DOWN] |= 0x51 << 8; |
| 5546 | |
| 5547 | kb_translation_table[DIK_END] = 0; |
| 5548 | kb_translation_table[DIK_END] |= 0x4d << 8; |
| 5549 | |
| 5550 | kb_translation_table[DIK_EQUALS] = '='; |
| 5551 | kb_translation_table[DIK_EQUALS] |= 0x2e << 8; |
| 5552 | |
| 5553 | kb_translation_table[DIK_ESCAPE] = 0x1b; |
| 5554 | kb_translation_table[DIK_ESCAPE] |= 0x29 << 8; |
| 5555 | |
| 5556 | kb_translation_table[DIK_F1] = 0; |
| 5557 | kb_translation_table[DIK_F1] |= 0x3a << 8; |
| 5558 | |
| 5559 | kb_translation_table[DIK_F2] = 0; |
| 5560 | kb_translation_table[DIK_F2] |= 0x3b << 8; |
| 5561 | |
| 5562 | kb_translation_table[DIK_F3] = 0; |
| 5563 | kb_translation_table[DIK_F3] |= 0x3c << 8; |
| 5564 | |
| 5565 | kb_translation_table[DIK_F4] = 0; |
| 5566 | kb_translation_table[DIK_F4] |= 0x3d << 8; |
| 5567 | |
| 5568 | kb_translation_table[DIK_F5] = 0; |
| 5569 | kb_translation_table[DIK_F5] |= 0x3e << 8; |
| 5570 | |
| 5571 | kb_translation_table[DIK_F6] = 0; |
| 5572 | kb_translation_table[DIK_F6] |= 0x3f << 8; |
| 5573 | |
| 5574 | kb_translation_table[DIK_F7] = 0; |
| 5575 | kb_translation_table[DIK_F7] |= 0x40 << 8; |
| 5576 | |
| 5577 | kb_translation_table[DIK_F8] = 0; |
| 5578 | kb_translation_table[DIK_F8] |= 0x41 << 8; |
| 5579 | |
| 5580 | kb_translation_table[DIK_F9] = 0; |
| 5581 | kb_translation_table[DIK_F9] |= 0x42 << 8; |
| 5582 | |
| 5583 | kb_translation_table[DIK_F10] = 0; |
| 5584 | kb_translation_table[DIK_F10] |= 0x43 << 8; |
| 5585 | |
| 5586 | kb_translation_table[DIK_F11] = 0; |
| 5587 | kb_translation_table[DIK_F11] |= 0x44 << 8; |
| 5588 | |
| 5589 | kb_translation_table[DIK_F12] = 0; |
| 5590 | kb_translation_table[DIK_F12] |= 0x45 << 8; |
| 5591 | |
| 5592 | kb_translation_table[DIK_F13] = 0; |
| 5593 | kb_translation_table[DIK_F13] |= 0x68 << 8; |
| 5594 | |
| 5595 | kb_translation_table[DIK_F14] = 0; |
| 5596 | kb_translation_table[DIK_F14] |= 0x69 << 8; |
| 5597 | |
| 5598 | kb_translation_table[DIK_F15] = 0; |
| 5599 | kb_translation_table[DIK_F15] |= 0x6a << 8; |
| 5600 | |
| 5601 | kb_translation_table[DIK_GRAVE] = '`'; |
| 5602 | kb_translation_table[DIK_GRAVE] |= 0x35 << 8; |
| 5603 | |
| 5604 | kb_translation_table[DIK_HOME] = 0; |
| 5605 | kb_translation_table[DIK_HOME] |= 0x4a << 8; |
| 5606 | |
| 5607 | kb_translation_table[DIK_INSERT] = 0; |
| 5608 | kb_translation_table[DIK_INSERT] |= 0x49 << 8; |
| 5609 | |
| 5610 | kb_translation_table[DIK_LBRACKET] = '['; |
| 5611 | kb_translation_table[DIK_LBRACKET] |= 0x2f << 8; |
| 5612 | |
| 5613 | kb_translation_table[DIK_LCONTROL] = 0; |
| 5614 | kb_translation_table[DIK_LCONTROL] |= 0xe0 << 8; |
| 5615 | |
| 5616 | kb_translation_table[DIK_LEFT] = 0; |
| 5617 | kb_translation_table[DIK_LEFT] |= 0x50 << 8; |
| 5618 | |
| 5619 | kb_translation_table[DIK_LMENU] = 0; |
| 5620 | kb_translation_table[DIK_LMENU] |= 0xe2 << 8; |
| 5621 | |
| 5622 | kb_translation_table[DIK_LSHIFT] = 0; |
| 5623 | kb_translation_table[DIK_LSHIFT] |= 0xe1 << 8; |
| 5624 | |
| 5625 | kb_translation_table[DIK_LWIN] = 0; |
| 5626 | kb_translation_table[DIK_LWIN] |= 0xe3 << 8; |
| 5627 | |
| 5628 | kb_translation_table[DIK_MINUS] = '-'; |
| 5629 | kb_translation_table[DIK_MINUS] |= 0x2d << 8; |
| 5630 | |
| 5631 | kb_translation_table[DIK_MULTIPLY] = '*'; |
| 5632 | kb_translation_table[DIK_MULTIPLY] |= 0x55 << 8; |
| 5633 | |
| 5634 | // kb_translation_table[DIK_MUTE] = 0; |
| 5635 | // kb_translation_table[DIK_MUTE] |= 0x7f; |
| 5636 | |
| 5637 | kb_translation_table[DIK_NEXT] = 0; |
| 5638 | kb_translation_table[DIK_NEXT] |= 0x4e << 8; |
| 5639 | |
| 5640 | kb_translation_table[DIK_NUMLOCK] = 0; |
| 5641 | kb_translation_table[DIK_NUMLOCK] |= 0x53 << 8; |
| 5642 | |
| 5643 | kb_translation_table[DIK_NUMPAD0] = '0'; |
| 5644 | kb_translation_table[DIK_NUMPAD0] |= 0x62 << 8; |
| 5645 | |
| 5646 | kb_translation_table[DIK_NUMPAD1] = '1'; |
| 5647 | kb_translation_table[DIK_NUMPAD1] |= 0x59 << 8; |
| 5648 | |
| 5649 | kb_translation_table[DIK_NUMPAD2] = '2'; |
| 5650 | kb_translation_table[DIK_NUMPAD2] |= 0x5a << 8; |
| 5651 | |
| 5652 | kb_translation_table[DIK_NUMPAD3] = '3'; |
| 5653 | kb_translation_table[DIK_NUMPAD3] |= 0x5b << 8; |
| 5654 | |
| 5655 | kb_translation_table[DIK_NUMPAD4] = '4'; |
| 5656 | kb_translation_table[DIK_NUMPAD4] |= 0x5c << 8; |
| 5657 | |
| 5658 | kb_translation_table[DIK_NUMPAD5] = '5'; |
| 5659 | kb_translation_table[DIK_NUMPAD5] |= 0x5d << 8; |
| 5660 | |
| 5661 | kb_translation_table[DIK_NUMPAD6] = '6'; |
| 5662 | kb_translation_table[DIK_NUMPAD6] |= 0x5e << 8; |
| 5663 | |
| 5664 | kb_translation_table[DIK_NUMPAD7] = '7'; |
| 5665 | kb_translation_table[DIK_NUMPAD7] |= 0x5f << 8; |
| 5666 | |
| 5667 | kb_translation_table[DIK_NUMPAD8] = '8'; |
| 5668 | kb_translation_table[DIK_NUMPAD8] |= 0x60 << 8; |
| 5669 | |
| 5670 | kb_translation_table[DIK_NUMPAD9] = '9'; |
| 5671 | kb_translation_table[DIK_NUMPAD9] |= 0x61 << 8; |
| 5672 | |
| 5673 | kb_translation_table[DIK_NUMPADCOMMA] = ','; |
| 5674 | kb_translation_table[DIK_NUMPADCOMMA] |= 0x85 << 8; |
| 5675 | |
| 5676 | kb_translation_table[DIK_NUMPADENTER] = '\n'; |
| 5677 | kb_translation_table[DIK_NUMPADENTER] |= 0x58 << 8; |
| 5678 | |
| 5679 | kb_translation_table[DIK_NUMPADEQUALS] = '='; |
| 5680 | kb_translation_table[DIK_NUMPADEQUALS] |= 0x67 << 8; |
| 5681 | |
| 5682 | kb_translation_table[DIK_PAUSE] = 0; |
| 5683 | kb_translation_table[DIK_PAUSE] |= 0x48 << 8; |
| 5684 | |
| 5685 | kb_translation_table[DIK_PERIOD] = '.'; |
| 5686 | kb_translation_table[DIK_PERIOD] |= 0x37 << 8; |
| 5687 | |
| 5688 | kb_translation_table[DIK_POWER] = 0; |
| 5689 | kb_translation_table[DIK_POWER] |= 0x66 << 8; |
| 5690 | |
| 5691 | kb_translation_table[DIK_PRIOR] = 0; |
| 5692 | kb_translation_table[DIK_PRIOR] |= 0x4b << 8; |
| 5693 | |
| 5694 | kb_translation_table[DIK_RBRACKET] = ']'; |
| 5695 | kb_translation_table[DIK_RBRACKET] |= 0x30 << 8; |
| 5696 | |
| 5697 | kb_translation_table[DIK_RCONTROL] = 0; |
| 5698 | kb_translation_table[DIK_RCONTROL] |= 0xe4 << 8; |
| 5699 | |
| 5700 | kb_translation_table[DIK_RETURN] = '\n'; |
| 5701 | kb_translation_table[DIK_RETURN] |= 0x28 << 8; |
| 5702 | |
| 5703 | kb_translation_table[DIK_RIGHT] = 0; |
| 5704 | kb_translation_table[DIK_RIGHT] |= 0x4f << 8; |
| 5705 | |
| 5706 | kb_translation_table[DIK_RMENU] = 0; |
| 5707 | kb_translation_table[DIK_RMENU] |= 0xe6 << 8; |
| 5708 | |
| 5709 | kb_translation_table[DIK_RSHIFT] = 0; |
| 5710 | kb_translation_table[DIK_RSHIFT] |= 0xe5 << 8; |
| 5711 | |
| 5712 | kb_translation_table[DIK_RWIN] = 0; |
| 5713 | kb_translation_table[DIK_RWIN] |= 0xe7 << 8; |
| 5714 | |
| 5715 | kb_translation_table[DIK_SCROLL] = 0; |
| 5716 | kb_translation_table[DIK_SCROLL] |= 0x47 << 8; |
| 5717 | |
| 5718 | kb_translation_table[DIK_SEMICOLON] = ';'; |
| 5719 | kb_translation_table[DIK_SEMICOLON] |= 0x33 << 8; |
| 5720 | |
| 5721 | kb_translation_table[DIK_SLASH] = '/'; |
| 5722 | kb_translation_table[DIK_SLASH] |= 0x38 << 8; |
| 5723 | |
| 5724 | kb_translation_table[DIK_SPACE] = ' '; |
| 5725 | kb_translation_table[DIK_SPACE] |= 0x2c << 8; |
| 5726 | |
| 5727 | kb_translation_table[DIK_STOP] = 0; |
| 5728 | kb_translation_table[DIK_STOP] |= 0x78 << 8; |
| 5729 | |
| 5730 | kb_translation_table[DIK_SUBTRACT] = '-'; |
| 5731 | kb_translation_table[DIK_SUBTRACT] |= 0x56 << 8; |
| 5732 | |
| 5733 | kb_translation_table[DIK_SYSRQ] = 0; |
| 5734 | kb_translation_table[DIK_SYSRQ] |= 0x46 << 8; |
| 5735 | |
| 5736 | kb_translation_table[DIK_TAB] = '\t'; |
| 5737 | kb_translation_table[DIK_TAB] |= 0x2b << 8; |
| 5738 | |
| 5739 | kb_translation_table[DIK_UP] = 0; |
| 5740 | kb_translation_table[DIK_UP] |= 0x52 << 8; |
| 5741 | |
| 5742 | // kb_translation_table[DIK_VOLUMEDOWN] = 0; |
| 5743 | // kb_translation_table[DIK_VOLUMEDOWN] |= 0x81; |
| 5744 | |
| 5745 | // kb_translation_table[DIK_VOLUMEUP] = 0; |
| 5746 | // kb_translation_table[DIK_VOLUMEUP] |= 0x80; |
| 5747 | } |
| 5748 | |
| 5749 | static void Keyboard_dikey_to_ascii_and_usb( unsigned char dikey, |
| 5750 | long & ascii, |
| 5751 | long & usb ) |
| 5752 | { |
| 5753 | unsigned short tr = kb_translation_table[dikey]; |
| 5754 | |
| 5755 | ascii = tr & 0xff; |
| 5756 | usb = ( tr >> 8 ) & 0xff; |
| 5757 | } |
| 5758 | |
| 5759 | static BOOL CALLBACK DIEnumKeyboardProc( LPCDIDEVICEINSTANCE lpddi, |
| 5760 | LPVOID pvRef ) |
| 5761 | { |
| 5762 | GUID guid = lpddi->guidInstance; |
| 5763 | win32_keyboard * keyboard = new win32_keyboard; |
| 5764 | |
| 5765 | EM_log( CK_LOG_INFO, "found %s", lpddi->tszProductName ); |
| 5766 | |
| 5767 | strncpy( keyboard->name, lpddi->tszProductName, MAX_PATH ); |
| 5768 | |
| 5769 | if( lpdi->CreateDevice( guid, |
| 5770 | ( LPDIRECTINPUTDEVICE * ) &keyboard->lpdiKeyboard, |
| 5771 | NULL ) != DI_OK ) |
| 5772 | { |
| 5773 | delete keyboard; |
| 5774 | EM_log( CK_LOG_WARNING, "error: unable to initialize device %s", |
| 5775 | lpddi->tszProductName ); |
| 5776 | return DIENUM_CONTINUE; |
| 5777 | } |
| 5778 | |
| 5779 | keyboards->push_back( keyboard ); |
| 5780 | |
| 5781 | return DIENUM_CONTINUE; |
| 5782 | } |
| 5783 | |
| 5784 | void Keyboard_init() |
| 5785 | { |
| 5786 | if( keyboards != NULL ) |
| 5787 | return; |
| 5788 | |
| 5789 | EM_log( CK_LOG_INFO, "initializing keyboard" ); |
| 5790 | EM_pushlog(); |
| 5791 | |
| 5792 | HINSTANCE hInstance = GetModuleHandle( NULL ); |
| 5793 | |
| 5794 | if( lpdi == NULL ) |
| 5795 | { |
| 5796 | #if DIRECTINPUT_VERSION > 0x0700 // REFACTOR-2017 |
| 5797 | if( DirectInput8Create( hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, |
| 5798 | (LPVOID *) &lpdi, NULL) != DI_OK ) |
| 5799 | #else |
| 5800 | if( DirectInputCreate( hInstance, DIRECTINPUT_VERSION, |
| 5801 | &lpdi, NULL) != DI_OK ) |
| 5802 | #endif |
| 5803 | { |
| 5804 | lpdi = NULL; |
| 5805 | EM_log( CK_LOG_HERALD, "error: unable to initialize DirectInput, initialization failed" ); |
| 5806 | EM_poplog(); |
| 5807 | return; |
| 5808 | } |
| 5809 | } |
| 5810 | |
| 5811 | keyboards = new vector< win32_keyboard * >; |
| 5812 | #if DIRECTINPUT_VERSION <= 0x700 // REFACTOR-2017 |
| 5813 | if( lpdi->EnumDevices( DIDEVTYPE_KEYBOARD, DIEnumKeyboardProc, |
| 5814 | #else |
| 5815 | if( lpdi->EnumDevices( DI8DEVTYPE_KEYBOARD, DIEnumKeyboardProc, |
| 5816 | #endif |
| 5817 | NULL, DIEDFL_ATTACHEDONLY ) != DI_OK ) |
| 5818 | { |
| 5819 | delete keyboards; |
| 5820 | keyboards = NULL; |
| 5821 | lpdi->Release(); |
| 5822 | lpdi = NULL; |
| 5823 | EM_log( CK_LOG_HERALD, "error: unable to enumerate devices, initialization failed" ); |
| 5824 | EM_poplog(); |
| 5825 | return; |
| 5826 | } |
| 5827 | |
| 5828 | Keyboard_init_translation_table(); |
| 5829 | |
| 5830 | EM_poplog(); |
| 5831 | } |
| 5832 | |
| 5833 | void Keyboard_poll() |
| 5834 | { |
| 5835 | if( !keyboards ) |
| 5836 | return; |
| 5837 | |
| 5838 | win32_keyboard * keyboard; |
| 5839 | HidMsg msg; |
| 5840 | vector< win32_keyboard * >::size_type i, len = keyboards->size(); |
| 5841 | for( i = 0; i < len; i++ ) |
| 5842 | { |
| 5843 | keyboard = keyboards->at( i ); |
| 5844 | if( keyboard->refcount ) |
| 5845 | { |
| 5846 | // TODO: convert this to buffered input, or maybe notifications |
| 5847 | char state[DINPUT_KEYBUFFER_SIZE]; |
| 5848 | |
| 5849 | keyboard->lpdiKeyboard->Poll(); |
| 5850 | |
| 5851 | if( keyboard->lpdiKeyboard->GetDeviceState( DINPUT_KEYBUFFER_SIZE, state ) |
| 5852 | != DI_OK ) |
| 5853 | { |
| 5854 | EM_log( CK_LOG_WARNING, "keyboard: GetDeviceState failed for %s", |
| 5855 | keyboard->name ); |
| 5856 | continue; |
| 5857 | } |
| 5858 | |
| 5859 | for( int j = 0; j < DINPUT_KEYBUFFER_SIZE; j++ ) |
| 5860 | { |
| 5861 | if( ( state[j] & 0x80 ) ^ ( keyboard->last_state[j] & 0x80 ) ) |
| 5862 | { |
| 5863 | msg.clear(); |
| 5864 | msg.device_num = i; |
| 5865 | msg.device_type = CK_HID_DEV_KEYBOARD; |
| 5866 | msg.type = ( state[j] & 0x80 ) ? CK_HID_BUTTON_DOWN : |
| 5867 | CK_HID_BUTTON_UP; |
| 5868 | msg.eid = j; |
| 5869 | msg.idata[0] = ( state[j] & 0x80 ) ? 1 : 0; |
| 5870 | Keyboard_dikey_to_ascii_and_usb( j, msg.idata[2], msg.idata[1] ); |
| 5871 | HidInManager::push_message( msg ); |
| 5872 | } |
| 5873 | } |
| 5874 | |
| 5875 | memcpy( keyboard->last_state, state, DINPUT_KEYBUFFER_SIZE ); |
| 5876 | } |
| 5877 | |
| 5878 | else if( keyboard->needs_close ) |
| 5879 | { |
| 5880 | keyboard->needs_close = FALSE; |
| 5881 | keyboard->lpdiKeyboard->Unacquire(); |
| 5882 | keyboard->lpdiKeyboard->SetEventNotification( NULL ); |
| 5883 | } |
| 5884 | } |
| 5885 | } |
| 5886 | |
| 5887 | void Keyboard_quit() |
| 5888 | { |
| 5889 | if( keyboards ) |
| 5890 | { |
| 5891 | win32_keyboard * keyboard; |
| 5892 | vector< win32_keyboard * >::size_type i, len = keyboards->size(); |
| 5893 | for( i = 0; i < len; i++ ) |
| 5894 | { |
| 5895 | keyboard = keyboards->at( i ); |
| 5896 | |
| 5897 | if( keyboard->refcount > 0 || keyboard->needs_close) |
| 5898 | { |
| 5899 | keyboard->needs_close = FALSE; |
| 5900 | keyboard->refcount = 0; |
| 5901 | keyboard->lpdiKeyboard->Unacquire(); |
| 5902 | keyboard->lpdiKeyboard->SetEventNotification( NULL ); |
| 5903 | } |
| 5904 | |
| 5905 | keyboard->lpdiKeyboard->Release(); |
| 5906 | delete keyboard; |
| 5907 | } |
| 5908 | |
| 5909 | delete keyboards; |
| 5910 | keyboards = NULL; |
| 5911 | } |
| 5912 | |
| 5913 | if( lpdi ) |
| 5914 | { |
| 5915 | lpdi->Release(); |
| 5916 | lpdi = NULL; |
| 5917 | } |
| 5918 | } |
| 5919 | |
| 5920 | int Keyboard_count() |
| 5921 | { |
| 5922 | if( !keyboards ) |
| 5923 | return 0; |
| 5924 | return (int)keyboards->size(); |
| 5925 | } |
| 5926 | |
| 5927 | int Keyboard_open( int k ) |
| 5928 | { |
| 5929 | if( !keyboards || k < 0 || k >= keyboards->size() ) |
| 5930 | return -1; |
| 5931 | |
| 5932 | win32_keyboard * keyboard = keyboards->at( k ); |
| 5933 | |
| 5934 | if( keyboard->refcount == 0 ) |
| 5935 | { |
| 5936 | if( keyboard->lpdiKeyboard->SetDataFormat( &c_dfDIKeyboard ) != DI_OK ) |
| 5937 | { |
| 5938 | return -1; |
| 5939 | } |
| 5940 | |
| 5941 | if( keyboard->lpdiKeyboard->SetEventNotification( g_device_event ) != DI_OK ) |
| 5942 | { |
| 5943 | // fallback to sleep+poll mode |
| 5944 | g_wait_function = Hid_wait_usleep; |
| 5945 | SetEvent( g_device_event ); |
| 5946 | } |
| 5947 | |
| 5948 | if( keyboard->lpdiKeyboard->Acquire() != DI_OK ) |
| 5949 | { |
| 5950 | keyboard->lpdiKeyboard->SetEventNotification( NULL ); |
| 5951 | return -1; |
| 5952 | } |
| 5953 | } |
| 5954 | |
| 5955 | keyboard->refcount++; |
| 5956 | |
| 5957 | return 0; |
| 5958 | } |
| 5959 | |
| 5960 | int Keyboard_close( int k ) |
| 5961 | { |
| 5962 | if( !keyboards || k < 0 || k >= keyboards->size() ) |
| 5963 | return -1; |
| 5964 | |
| 5965 | win32_keyboard * keyboard = keyboards->at( k ); |
| 5966 | |
| 5967 | keyboard->refcount--; |
| 5968 | |
| 5969 | if( keyboard->refcount < 1 ) |
| 5970 | keyboard->needs_close = TRUE; |
| 5971 | |
| 5972 | return 0; |
| 5973 | } |
| 5974 | |
| 5975 | const char * Keyboard_name( int kb ) |
| 5976 | { |
| 5977 | if( !keyboards || kb < 0 || kb >= keyboards->size() ) |
| 5978 | return NULL; |
| 5979 | |
| 5980 | return keyboards->at( kb )->name; |
| 5981 | } |
| 5982 | |
| 5983 | /***************************************************************************** |
| 5984 | Windows mouse support |
| 5985 | *****************************************************************************/ |
| 5986 | #pragma mark Windows mouse support |
| 5987 | |
| 5988 | struct win32_mouse |
| 5989 | { |
| 5990 | win32_mouse() |
| 5991 | { |
| 5992 | refcount = 0; |
| 5993 | needs_close = FALSE; |
| 5994 | |
| 5995 | lpdiMouse = NULL; |
| 5996 | memset( &last_state, 0, sizeof( last_state ) ); |
| 5997 | } |
| 5998 | |
| 5999 | LPDIRECTINPUTDEVICE2 lpdiMouse; |
| 6000 | DIMOUSESTATE last_state; |
| 6001 | |
| 6002 | char name[MAX_PATH]; |
| 6003 | |
| 6004 | t_CKUINT refcount; |
| 6005 | t_CKBOOL needs_close; |
| 6006 | }; |
| 6007 | |
| 6008 | static vector< win32_mouse * > * mice; |
| 6009 | |
| 6010 | static BOOL CALLBACK DIEnumMouseProc( LPCDIDEVICEINSTANCE lpddi, |
| 6011 | LPVOID pvRef ) |
| 6012 | { |
| 6013 | GUID guid = lpddi->guidInstance; |
| 6014 | win32_mouse * mouse = new win32_mouse; |
| 6015 | |
| 6016 | EM_log( CK_LOG_INFO, "found %s", lpddi->tszProductName ); |
| 6017 | |
| 6018 | strncpy( mouse->name, lpddi->tszProductName, MAX_PATH ); |
| 6019 | |
| 6020 | if( lpdi->CreateDevice( guid, ( LPDIRECTINPUTDEVICE * ) &mouse->lpdiMouse, |
| 6021 | NULL ) != DI_OK ) |
| 6022 | { |
| 6023 | delete mouse; |
| 6024 | return DIENUM_CONTINUE; |
| 6025 | } |
| 6026 | |
| 6027 | mice->push_back( mouse ); |
| 6028 | |
| 6029 | return DIENUM_CONTINUE; |
| 6030 | } |
| 6031 | |
| 6032 | void Mouse_init() |
| 6033 | { |
| 6034 | if( mice != NULL ) |
| 6035 | return; |
| 6036 | |
| 6037 | EM_log( CK_LOG_INFO, "initializing mouse" ); |
| 6038 | EM_pushlog(); |
| 6039 | |
| 6040 | HINSTANCE hInstance = GetModuleHandle( NULL ); |
| 6041 | |
| 6042 | if( lpdi == NULL ) |
| 6043 | { |
| 6044 | #if DIRECTINPUT_VERSION > 0x0700 // REFACTOR-2017 |
| 6045 | if( DirectInput8Create( hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, |
| 6046 | (LPVOID *) &lpdi, NULL) != DI_OK ) |
| 6047 | #else |
| 6048 | if( DirectInputCreate( hInstance, DIRECTINPUT_VERSION, |
| 6049 | &lpdi, NULL) != DI_OK ) |
| 6050 | #endif |
| 6051 | { |
| 6052 | lpdi = NULL; |
| 6053 | EM_poplog(); |
| 6054 | return; |
| 6055 | } |
| 6056 | } |
| 6057 | |
| 6058 | mice = new vector< win32_mouse * >; |
| 6059 | |
| 6060 | #if DIRECTINPUT_VERSION <= 0x700 // REFACTOR-2017 |
| 6061 | if( lpdi->EnumDevices( DIDEVTYPE_MOUSE, DIEnumMouseProc, |
| 6062 | #else |
| 6063 | if( lpdi->EnumDevices( DI8DEVTYPE_MOUSE, DIEnumMouseProc, |
| 6064 | #endif |
| 6065 | NULL, DIEDFL_ATTACHEDONLY ) != DI_OK ) |
| 6066 | { |
| 6067 | delete mice; |
| 6068 | mice = NULL; |
| 6069 | lpdi->Release(); |
| 6070 | lpdi = NULL; |
| 6071 | EM_poplog(); |
| 6072 | return; |
| 6073 | } |
| 6074 | |
| 6075 | EM_poplog(); |
| 6076 | } |
| 6077 | |
| 6078 | //----------------------------------------------------------------------------- |
| 6079 | // name: Mouse_poll() |
| 6080 | // desc: Populate mouse polling HID message for windows |
| 6081 | //----------------------------------------------------------------------------- |
| 6082 | void Mouse_poll() |
| 6083 | { |
| 6084 | if( !mice ) |
| 6085 | return; |
| 6086 | |
| 6087 | win32_mouse * mouse; |
| 6088 | HidMsg msg; |
| 6089 | vector< win32_mouse * >::size_type i, len = mice->size(); |
| 6090 | for( i = 0; i < len; i++ ) |
| 6091 | { |
| 6092 | mouse = mice->at( i ); |
| 6093 | if( mouse->refcount ) |
| 6094 | { |
| 6095 | // TODO: convert this to buffered input, or maybe notifications |
| 6096 | DIMOUSESTATE state; |
| 6097 | |
| 6098 | mouse->lpdiMouse->Poll(); |
| 6099 | |
| 6100 | if( mouse->lpdiMouse->GetDeviceState( sizeof( DIMOUSESTATE ), &state ) |
| 6101 | != DI_OK ) |
| 6102 | { |
| 6103 | EM_log( CK_LOG_WARNING, "mouse: GetDeviceState failed for %s", mouse->name ); |
| 6104 | continue; |
| 6105 | } |
| 6106 | |
| 6107 | if( state.lX != 0 || state.lY != 0 ) |
| 6108 | { |
| 6109 | msg.clear(); |
| 6110 | msg.device_num = i; |
| 6111 | msg.device_type = CK_HID_DEV_MOUSE; |
| 6112 | msg.type = CK_HID_MOUSE_MOTION; |
| 6113 | msg.eid = 0; |
| 6114 | msg.idata[0] = state.lX; |
| 6115 | msg.idata[1] = state.lY; |
| 6116 | |
| 6117 | // 1.5.0.0 (nshaheed) add support for absolute and normalized mouse pos |
| 6118 | // Acquire cursor position (in pixels) |
| 6119 | POINT point; |
| 6120 | if (GetCursorPos(&point)) { |
| 6121 | msg.idata[2] = point.x; |
| 6122 | msg.idata[3] = point.y; |
| 6123 | } |
| 6124 | |
| 6125 | // Get primary monitor handle |
| 6126 | const POINT ptZero = { 0, 0 }; |
| 6127 | HMONITOR hMonitor = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY); |
| 6128 | |
| 6129 | MONITORINFO info; |
| 6130 | info.cbSize = sizeof(info); |
| 6131 | |
| 6132 | // Populate scaledCursorX and scaledCursorY |
| 6133 | if (GetMonitorInfo(hMonitor, (LPMONITORINFO)&info)) { |
| 6134 | msg.fdata[0] = ((t_CKFLOAT) (point.x - info.rcMonitor.left)) / (info.rcMonitor.right - 1); |
| 6135 | msg.fdata[1] = ((t_CKFLOAT) (point.y - info.rcMonitor.top)) / (info.rcMonitor.bottom - 1); |
| 6136 | } |
| 6137 | |
| 6138 | HidInManager::push_message(msg); |
| 6139 | } |
| 6140 | |
| 6141 | if( state.lZ != 0 ) |
| 6142 | { |
| 6143 | msg.clear(); |
| 6144 | msg.device_num = i; |
| 6145 | msg.device_type = CK_HID_DEV_MOUSE; |
| 6146 | msg.type = CK_HID_MOUSE_WHEEL; |
| 6147 | msg.eid = 0; |
| 6148 | msg.idata[0] = 0; |
| 6149 | msg.idata[1] = state.lZ; |
| 6150 | HidInManager::push_message( msg ); |
| 6151 | } |
| 6152 | |
| 6153 | for( int j = 0; j < 4; j++ ) |
| 6154 | { |
| 6155 | if( ( state.rgbButtons[j] & 0x80 ) ^ |
| 6156 | ( mouse->last_state.rgbButtons[j] & 0x80 ) ) |
| 6157 | { |
| 6158 | msg.clear(); |
| 6159 | msg.device_num = i; |
| 6160 | msg.device_type = CK_HID_DEV_MOUSE; |
| 6161 | msg.type = ( state.rgbButtons[j] & 0x80 ) ? CK_HID_BUTTON_DOWN : |
| 6162 | CK_HID_BUTTON_UP; |
| 6163 | msg.eid = j; |
| 6164 | msg.idata[0] = ( state.rgbButtons[j] & 0x80 ) ? 1 : 0; |
| 6165 | HidInManager::push_message( msg ); |
| 6166 | } |
| 6167 | } |
| 6168 | |
| 6169 | mouse->last_state = state; |
| 6170 | } |
| 6171 | |
| 6172 | else if( mouse->needs_close ) |
| 6173 | { |
| 6174 | mouse->needs_close = FALSE; |
| 6175 | mouse->lpdiMouse->Unacquire(); |
| 6176 | mouse->lpdiMouse->SetEventNotification( NULL ); |
| 6177 | } |
| 6178 | } |
| 6179 | } |
| 6180 | |
| 6181 | void Mouse_quit() |
| 6182 | { |
| 6183 | if( mice ) |
| 6184 | { |
| 6185 | win32_mouse * mouse; |
| 6186 | vector< win32_mouse * >::size_type i, len = mice->size(); |
| 6187 | for( i = 0; i < len; i++ ) |
| 6188 | { |
| 6189 | mouse = mice->at( i ); |
| 6190 | |
| 6191 | if( mouse->refcount > 0 || mouse->needs_close) |
| 6192 | { |
| 6193 | mouse->needs_close = FALSE; |
| 6194 | mouse->refcount = 0; |
| 6195 | mouse->lpdiMouse->Unacquire(); |
| 6196 | mouse->lpdiMouse->SetEventNotification( NULL ); |
| 6197 | } |
| 6198 | |
| 6199 | mouse->lpdiMouse->Release(); |
| 6200 | delete mouse; |
| 6201 | } |
| 6202 | |
| 6203 | delete mice; |
| 6204 | mice = NULL; |
| 6205 | } |
| 6206 | |
| 6207 | if( lpdi ) |
| 6208 | { |
| 6209 | lpdi->Release(); |
| 6210 | lpdi = NULL; |
| 6211 | } |
| 6212 | } |
| 6213 | |
| 6214 | int Mouse_count() |
| 6215 | { |
| 6216 | if( !mice ) |
| 6217 | return 0; |
| 6218 | return (int)mice->size(); |
| 6219 | } |
| 6220 | |
| 6221 | int Mouse_open( int m ) |
| 6222 | { |
| 6223 | if( !mice || m < 0 || m >= mice->size() ) |
| 6224 | return -1; |
| 6225 | |
| 6226 | win32_mouse * mouse = mice->at( m ); |
| 6227 | |
| 6228 | if( mouse->refcount == 0 ) |
| 6229 | { |
| 6230 | if( mouse->lpdiMouse->SetDataFormat( &c_dfDIMouse ) != DI_OK ) |
| 6231 | { |
| 6232 | return -1; |
| 6233 | } |
| 6234 | |
| 6235 | if( mouse->lpdiMouse->SetEventNotification( g_device_event ) != DI_OK ) |
| 6236 | { |
| 6237 | // fallback to sleep+poll mode |
| 6238 | g_wait_function = Hid_wait_usleep; |
| 6239 | SetEvent( g_device_event ); |
| 6240 | } |
| 6241 | |
| 6242 | if( mouse->lpdiMouse->Acquire() != DI_OK ) |
| 6243 | { |
| 6244 | mouse->lpdiMouse->SetEventNotification( g_device_event ); |
| 6245 | return -1; |
| 6246 | } |
| 6247 | } |
| 6248 | |
| 6249 | mouse->refcount++; |
| 6250 | |
| 6251 | return 0; |
| 6252 | } |
| 6253 | |
| 6254 | int Mouse_close( int m ) |
| 6255 | { |
| 6256 | if( !mice || m < 0 || m >= mice->size() ) |
| 6257 | return -1; |
| 6258 | |
| 6259 | win32_mouse * mouse = mice->at( m ); |
| 6260 | |
| 6261 | mouse->refcount--; |
| 6262 | |
| 6263 | if( mouse->refcount < 1 ) |
| 6264 | mouse->needs_close = TRUE; // let the polling thread take care of it |
| 6265 | |
| 6266 | return 0; |
| 6267 | } |
| 6268 | |
| 6269 | const char * Mouse_name( int m ) |
| 6270 | { |
| 6271 | if( !mice || m < 0 || m >= mice->size() ) |
| 6272 | return NULL; |
| 6273 | |
| 6274 | return mice->at( m )->name; |
| 6275 | } |
| 6276 | |
| 6277 | #elif defined(__PLATFORM_WINDOWS__) || defined(__WINDOWS_PTHREAD__) && defined( USE_RAWINPUT ) |
| 6278 | |
| 6279 | void Joystick_init() |
| 6280 | { |
| 6281 | |
| 6282 | } |
| 6283 | |
| 6284 | void Joystick_poll() |
| 6285 | { |
| 6286 | |
| 6287 | } |
| 6288 | |
| 6289 | void Joystick_quit() |
| 6290 | { |
| 6291 | |
| 6292 | } |
| 6293 | |
| 6294 | int Joystick_count() |
| 6295 | { |
| 6296 | return 0; |
| 6297 | } |
| 6298 | |
| 6299 | int Joystick_open( int js ) |
| 6300 | { |
| 6301 | return -1; |
| 6302 | } |
| 6303 | |
| 6304 | int Joystick_close( int js ) |
| 6305 | { |
| 6306 | return -1; |
| 6307 | } |
| 6308 | |
| 6309 | void Mouse_init() |
| 6310 | { |
| 6311 | |
| 6312 | } |
| 6313 | |
| 6314 | void Mouse_poll() |
| 6315 | { |
| 6316 | |
| 6317 | } |
| 6318 | |
| 6319 | void Mouse_quit() |
| 6320 | { |
| 6321 | |
| 6322 | } |
| 6323 | |
| 6324 | int Mouse_count() |
| 6325 | { |
| 6326 | return 0; |
| 6327 | } |
| 6328 | |
| 6329 | int Mouse_open( int js ) |
| 6330 | { |
| 6331 | return -1; |
| 6332 | } |
| 6333 | |
| 6334 | int Mouse_close( int js ) |
| 6335 | { |
| 6336 | return -1; |
| 6337 | } |
| 6338 | |
| 6339 | void Keyboard_init() |
| 6340 | { |
| 6341 | |
| 6342 | } |
| 6343 | |
| 6344 | void Keyboard_poll() |
| 6345 | { |
| 6346 | |
| 6347 | } |
| 6348 | |
| 6349 | void Keyboard_quit() |
| 6350 | { |
| 6351 | |
| 6352 | } |
| 6353 | |
| 6354 | int Keyboard_count() |
| 6355 | { |
| 6356 | return 0; |
| 6357 | } |
| 6358 | |
| 6359 | int Keyboard_open( int js ) |
| 6360 | { |
| 6361 | return -1; |
| 6362 | } |
| 6363 | |
| 6364 | int Keyboard_close( int js ) |
| 6365 | { |
| 6366 | return -1; |
| 6367 | } |
| 6368 | |
| 6369 | |
| 6370 | |
| 6371 | #elif defined(__PLATFORM_LINUX__) |
| 6372 | /***************************************************************************** |
| 6373 | Linux general HID support |
| 6374 | *****************************************************************************/ |
| 6375 | #pragma mark Linux general HID support |
| 6376 | |
| 6377 | #include <sys/types.h> |
| 6378 | #include <sys/stat.h> |
| 6379 | #include <unistd.h> |
| 6380 | #include <stdint.h> |
| 6381 | #include <dirent.h> |
| 6382 | #include <string.h> |
| 6383 | #include <fcntl.h> |
| 6384 | #include <sys/poll.h> |
| 6385 | #include <sys/ioctl.h> |
| 6386 | #include <errno.h> |
| 6387 | #include <linux/joystick.h> |
| 6388 | #include <linux/input.h> |
| 6389 | |
| 6390 | #if defined(__USE_X11__) |
| 6391 | // for Mouse | 1.5.4.2 (ge) added |
| 6392 | #include <X11/Xlib.h> |
| 6393 | #endif |
| 6394 | |
| 6395 | #define CK_HID_DIR ("/dev/input") |
| 6396 | #define CK_HID_MOUSEFILE ("mouse%d") |
| 6397 | #define CK_HID_JOYSTICKFILE ("js%d") |
| 6398 | #define CK_HID_EVDEVFILE ("event%d") |
| 6399 | #define CK_HID_STRBUFSIZE (1024) |
| 6400 | #define CK_HID_NAMESIZE (128) |
| 6401 | |
| 6402 | class linux_device |
| 6403 | { |
| 6404 | public: |
| 6405 | linux_device() |
| 6406 | { |
| 6407 | fd = -1; |
| 6408 | num = -1; |
| 6409 | refcount = 0; |
| 6410 | pollfds_i = 0; |
| 6411 | filename[0] = '\0'; |
| 6412 | needs_open = needs_close = FALSE; |
| 6413 | strncpy( name, "(name unknown)", CK_HID_NAMESIZE ); |
| 6414 | } |
| 6415 | |
| 6416 | virtual void callback() = 0; |
| 6417 | |
| 6418 | int fd; // file descriptor |
| 6419 | t_CKINT num; // index in the respective device vector {joysticks, mice, keyboards} |
| 6420 | |
| 6421 | t_CKUINT refcount; |
| 6422 | |
| 6423 | size_t pollfds_i; |
| 6424 | |
| 6425 | t_CKBOOL needs_open; |
| 6426 | t_CKBOOL needs_close; |
| 6427 | |
| 6428 | char filename[CK_HID_STRBUFSIZE]; |
| 6429 | char name[CK_HID_NAMESIZE]; |
| 6430 | }; |
| 6431 | |
| 6432 | class linux_joystick : public linux_device |
| 6433 | { |
| 6434 | public: |
| 6435 | linux_joystick() : linux_device() |
| 6436 | { |
| 6437 | js_num = -1; |
| 6438 | } |
| 6439 | |
| 6440 | virtual void callback() |
| 6441 | { |
| 6442 | js_event event; |
| 6443 | HidMsg msg; |
| 6444 | ssize_t len; |
| 6445 | |
| 6446 | while( ( len = read( fd, &event, sizeof( event ) ) ) > 0 ) |
| 6447 | { |
| 6448 | if( len < sizeof( event ) ) |
| 6449 | { |
| 6450 | EM_log( CK_LOG_WARNING, "joystick: read event from %s smaller than expected, ignoring", name ); |
| 6451 | continue; |
| 6452 | } |
| 6453 | |
| 6454 | if( event.type == JS_EVENT_INIT ) |
| 6455 | continue; |
| 6456 | |
| 6457 | msg.device_type = CK_HID_DEV_JOYSTICK; |
| 6458 | msg.device_num = num; |
| 6459 | msg.eid = event.number; |
| 6460 | |
| 6461 | switch( event.type ) |
| 6462 | { |
| 6463 | case JS_EVENT_BUTTON: |
| 6464 | msg.type = event.value ? CK_HID_BUTTON_DOWN : |
| 6465 | CK_HID_BUTTON_UP; |
| 6466 | msg.idata[0] = event.value; |
| 6467 | break; |
| 6468 | case JS_EVENT_AXIS: |
| 6469 | msg.type = CK_HID_JOYSTICK_AXIS; |
| 6470 | msg.fdata[0] = ((t_CKFLOAT)event.value) / ((t_CKFLOAT) SHRT_MAX); |
| 6471 | break; |
| 6472 | default: |
| 6473 | EM_log( CK_LOG_WARNING, "joystick: unknown event type from %s, ignoring", name ); |
| 6474 | continue; |
| 6475 | } |
| 6476 | |
| 6477 | HidInManager::push_message( msg ); |
| 6478 | } |
| 6479 | } |
| 6480 | |
| 6481 | int js_num; // /dev/input/js# <-- the # |
| 6482 | }; |
| 6483 | |
| 6484 | #define __LITTLE_ENDIAN__ |
| 6485 | struct ps2_mouse_event |
| 6486 | { |
| 6487 | #ifdef __BIG_ENDIAN__ |
| 6488 | unsigned unused:2; |
| 6489 | unsigned y_reverse_motion:1; |
| 6490 | unsigned x_reverse_motion:1; |
| 6491 | unsigned always1:1; |
| 6492 | unsigned button3:1; |
| 6493 | unsigned button2:1; |
| 6494 | unsigned button1:1; |
| 6495 | signed dx:8; |
| 6496 | signed dy:8; |
| 6497 | #elif defined( __LITTLE_ENDIAN__ ) |
| 6498 | unsigned button1:1; |
| 6499 | unsigned button2:1; |
| 6500 | unsigned button3:1; |
| 6501 | unsigned always1:1; |
| 6502 | unsigned x_reverse_motion:1; |
| 6503 | unsigned y_reverse_motion:1; |
| 6504 | unsigned unused:2; |
| 6505 | signed dx:8; |
| 6506 | signed dy:8; |
| 6507 | #else |
| 6508 | #error unknown endian mode (both __LITTLE_ENDIAN__ and __BIG_ENDIAN__ undefined) |
| 6509 | #endif |
| 6510 | } __attribute__ ((__packed__)); |
| 6511 | |
| 6512 | class linux_mouse : public linux_device |
| 6513 | { |
| 6514 | public: |
| 6515 | linux_mouse() : linux_device() |
| 6516 | { |
| 6517 | m_num = -1; |
| 6518 | memset( &last_event, 0, sizeof( last_event ) ); |
| 6519 | } |
| 6520 | |
| 6521 | virtual void callback() |
| 6522 | { |
| 6523 | //ps2_mouse_event event; |
| 6524 | input_event event; |
| 6525 | HidMsg msg; |
| 6526 | ssize_t len; |
| 6527 | |
| 6528 | while( ( len = read( fd, &event, sizeof( event ) ) ) > 0 ) |
| 6529 | { |
| 6530 | if( len < sizeof( event ) ) |
| 6531 | { |
| 6532 | EM_log( CK_LOG_WARNING, "mouse: read event from mouse %i smaller than expected (%i), ignoring", num, len ); |
| 6533 | continue; |
| 6534 | } |
| 6535 | |
| 6536 | switch( event.type ) |
| 6537 | { |
| 6538 | case EV_KEY: |
| 6539 | if( event.code & BTN_MOUSE ) |
| 6540 | { |
| 6541 | msg.clear(); |
| 6542 | msg.device_type = CK_HID_DEV_MOUSE; |
| 6543 | msg.device_num = num; |
| 6544 | msg.eid = event.code - BTN_MOUSE; |
| 6545 | msg.type = event.value ? CK_HID_BUTTON_DOWN : CK_HID_BUTTON_UP; |
| 6546 | msg.idata[0] = event.value; |
| 6547 | HidInManager::push_message( msg ); |
| 6548 | } |
| 6549 | |
| 6550 | break; |
| 6551 | |
| 6552 | case EV_REL: |
| 6553 | msg.clear(); |
| 6554 | msg.device_type = CK_HID_DEV_MOUSE; |
| 6555 | msg.device_num = num; |
| 6556 | |
| 6557 | switch( event.code ) |
| 6558 | { |
| 6559 | case REL_X: |
| 6560 | msg.type = CK_HID_MOUSE_MOTION; |
| 6561 | msg.idata[0] = event.value; |
| 6562 | msg.idata[1] = 0; |
| 6563 | break; |
| 6564 | |
| 6565 | case REL_Y: |
| 6566 | msg.type = CK_HID_MOUSE_MOTION; |
| 6567 | msg.idata[0] = 0; |
| 6568 | msg.idata[1] = event.value; |
| 6569 | break; |
| 6570 | |
| 6571 | case REL_HWHEEL: |
| 6572 | msg.type = CK_HID_MOUSE_WHEEL; |
| 6573 | msg.idata[0] = event.value; |
| 6574 | msg.idata[1] = 0; |
| 6575 | break; |
| 6576 | |
| 6577 | case REL_Z: |
| 6578 | case REL_WHEEL: |
| 6579 | msg.type = CK_HID_MOUSE_WHEEL; |
| 6580 | msg.idata[0] = 0; |
| 6581 | msg.idata[1] = event.value; |
| 6582 | break; |
| 6583 | } |
| 6584 | |
| 6585 | HidInManager::push_message( msg ); |
| 6586 | |
| 6587 | break; |
| 6588 | |
| 6589 | } |
| 6590 | |
| 6591 | /* |
| 6592 | if( event.dx || event.dy ) |
| 6593 | { |
| 6594 | msg.device_type = CK_HID_DEV_MOUSE; |
| 6595 | msg.device_num = num; |
| 6596 | msg.eid = 0; |
| 6597 | msg.type = CK_HID_MOUSE_MOTION; |
| 6598 | msg.idata[0] = event.dx; |
| 6599 | msg.idata[1] = event.dy; |
| 6600 | HidInManager::push_message( msg ); |
| 6601 | } |
| 6602 | |
| 6603 | if( event.button1 ^ last_event.button1 ) |
| 6604 | { |
| 6605 | msg.device_type = CK_HID_DEV_MOUSE; |
| 6606 | msg.device_num = num; |
| 6607 | msg.eid = 0; |
| 6608 | msg.type = event.button1 ? CK_HID_BUTTON_DOWN : CK_HID_BUTTON_UP; |
| 6609 | msg.idata[0] = event.button1; |
| 6610 | HidInManager::push_message( msg ); |
| 6611 | } |
| 6612 | |
| 6613 | if( event.button2 ^ last_event.button2 ) |
| 6614 | { |
| 6615 | msg.device_type = CK_HID_DEV_MOUSE; |
| 6616 | msg.device_num = num; |
| 6617 | msg.eid = 2; |
| 6618 | msg.type = event.button2 ? CK_HID_BUTTON_DOWN : CK_HID_BUTTON_UP; |
| 6619 | msg.idata[0] = event.button2; |
| 6620 | HidInManager::push_message( msg ); |
| 6621 | } |
| 6622 | |
| 6623 | if( event.button3 ^ last_event.button3 ) |
| 6624 | { |
| 6625 | msg.device_type = CK_HID_DEV_MOUSE; |
| 6626 | msg.device_num = num; |
| 6627 | msg.eid = 3; |
| 6628 | msg.type = event.button3 ? CK_HID_BUTTON_DOWN : CK_HID_BUTTON_UP; |
| 6629 | msg.idata[0] = event.button3; |
| 6630 | HidInManager::push_message( msg ); |
| 6631 | } |
| 6632 | */ |
| 6633 | memcpy( &last_event, &event, sizeof( last_event ) ); |
| 6634 | } |
| 6635 | } |
| 6636 | |
| 6637 | int m_num; // /dev/input/mouse# <-- the # |
| 6638 | //ps2_mouse_event last_event; |
| 6639 | input_event last_event; |
| 6640 | }; |
| 6641 | |
| 6642 | static unsigned short kb_translation_table[KEY_UNKNOWN]; |
| 6643 | |
| 6644 | static void Keyboard_init_translation_table() |
| 6645 | { |
| 6646 | memset( kb_translation_table, 0, sizeof( kb_translation_table ) ); |
| 6647 | |
| 6648 | kb_translation_table[KEY_ESC] = '\e'; |
| 6649 | kb_translation_table[KEY_ESC] |= 0x29 << 8; |
| 6650 | |
| 6651 | kb_translation_table[KEY_1] = '1'; |
| 6652 | kb_translation_table[KEY_1] |= 0x1e << 8; |
| 6653 | |
| 6654 | kb_translation_table[KEY_2] = '2'; |
| 6655 | kb_translation_table[KEY_2] |= 0x1f << 8; |
| 6656 | |
| 6657 | kb_translation_table[KEY_3] = '3'; |
| 6658 | kb_translation_table[KEY_3] |= 0x20 << 8; |
| 6659 | |
| 6660 | kb_translation_table[KEY_4] = '4'; |
| 6661 | kb_translation_table[KEY_4] |= 0x21 << 8; |
| 6662 | |
| 6663 | kb_translation_table[KEY_5] = '5'; |
| 6664 | kb_translation_table[KEY_5] |= 0x22 << 8; |
| 6665 | |
| 6666 | kb_translation_table[KEY_6] = '6'; |
| 6667 | kb_translation_table[KEY_6] |= 0x23 << 8; |
| 6668 | |
| 6669 | kb_translation_table[KEY_7] = '7'; |
| 6670 | kb_translation_table[KEY_7] |= 0x24 << 8; |
| 6671 | |
| 6672 | kb_translation_table[KEY_8] = '8'; |
| 6673 | kb_translation_table[KEY_8] |= 0x25 << 8; |
| 6674 | |
| 6675 | kb_translation_table[KEY_9] = '9'; |
| 6676 | kb_translation_table[KEY_9] |= 0x26 << 8; |
| 6677 | |
| 6678 | kb_translation_table[KEY_0] = '0'; |
| 6679 | kb_translation_table[KEY_0] |= 0x27 << 8; |
| 6680 | |
| 6681 | kb_translation_table[KEY_MINUS] = '-'; |
| 6682 | kb_translation_table[KEY_MINUS] |= 0x2d << 8; |
| 6683 | |
| 6684 | kb_translation_table[KEY_EQUAL] = '='; |
| 6685 | kb_translation_table[KEY_EQUAL] |= 0x2e << 8; |
| 6686 | |
| 6687 | kb_translation_table[KEY_BACKSPACE] = '\b'; |
| 6688 | kb_translation_table[KEY_BACKSPACE] |= 0x2a << 8; |
| 6689 | |
| 6690 | kb_translation_table[KEY_TAB] = '\t'; |
| 6691 | kb_translation_table[KEY_TAB] |= 0x2b << 8; |
| 6692 | |
| 6693 | kb_translation_table[KEY_Q] = 'Q'; |
| 6694 | kb_translation_table[KEY_Q] |= 0x14 << 8; |
| 6695 | |
| 6696 | kb_translation_table[KEY_W] = 'W'; |
| 6697 | kb_translation_table[KEY_W] |= 0x1a << 8; |
| 6698 | |
| 6699 | kb_translation_table[KEY_E] = 'E'; |
| 6700 | kb_translation_table[KEY_E] |= 0x08 << 8; |
| 6701 | |
| 6702 | kb_translation_table[KEY_R] = 'R'; |
| 6703 | kb_translation_table[KEY_R] |= 0x15 << 8; |
| 6704 | |
| 6705 | kb_translation_table[KEY_T] = 'T'; |
| 6706 | kb_translation_table[KEY_T] |= 0x17 << 8; |
| 6707 | |
| 6708 | kb_translation_table[KEY_Y] = 'Y'; |
| 6709 | kb_translation_table[KEY_Y] |= 0x1c << 8; |
| 6710 | |
| 6711 | kb_translation_table[KEY_U] = 'U'; |
| 6712 | kb_translation_table[KEY_U] |= 0x18 << 8; |
| 6713 | |
| 6714 | kb_translation_table[KEY_I] = 'I'; |
| 6715 | kb_translation_table[KEY_I] |= 0x0c << 8; |
| 6716 | |
| 6717 | kb_translation_table[KEY_O] = 'O'; |
| 6718 | kb_translation_table[KEY_O] |= 0x12 << 8; |
| 6719 | |
| 6720 | kb_translation_table[KEY_P] = 'P'; |
| 6721 | kb_translation_table[KEY_P] |= 0x13 << 8; |
| 6722 | |
| 6723 | kb_translation_table[KEY_LEFTBRACE] = '['; |
| 6724 | kb_translation_table[KEY_LEFTBRACE] |= 0x2f << 8; |
| 6725 | |
| 6726 | kb_translation_table[KEY_RIGHTBRACE] = ']'; |
| 6727 | kb_translation_table[KEY_RIGHTBRACE] |= 0x30 << 8; |
| 6728 | |
| 6729 | kb_translation_table[KEY_ENTER] = '\n'; |
| 6730 | kb_translation_table[KEY_ENTER] |= 0x28 << 8; |
| 6731 | |
| 6732 | kb_translation_table[KEY_LEFTCTRL] = 0; |
| 6733 | kb_translation_table[KEY_LEFTCTRL] |= 0xe0 << 8; |
| 6734 | |
| 6735 | kb_translation_table[KEY_A] = 'A'; |
| 6736 | kb_translation_table[KEY_A] |= 0x04 << 8; |
| 6737 | |
| 6738 | kb_translation_table[KEY_S] = 'S'; |
| 6739 | kb_translation_table[KEY_S] |= 0x16 << 8; |
| 6740 | |
| 6741 | kb_translation_table[KEY_D] = 'D'; |
| 6742 | kb_translation_table[KEY_D] |= 0x07 << 8; |
| 6743 | |
| 6744 | kb_translation_table[KEY_F] = 'F'; |
| 6745 | kb_translation_table[KEY_F] |= 0x09 << 8; |
| 6746 | |
| 6747 | kb_translation_table[KEY_G] = 'G'; |
| 6748 | kb_translation_table[KEY_G] |= 0x0a << 8; |
| 6749 | |
| 6750 | kb_translation_table[KEY_H] = 'H'; |
| 6751 | kb_translation_table[KEY_H] |= 0x0b << 8; |
| 6752 | |
| 6753 | kb_translation_table[KEY_J] = 'J'; |
| 6754 | kb_translation_table[KEY_J] |= 0x0d << 8; |
| 6755 | |
| 6756 | kb_translation_table[KEY_K] = 'K'; |
| 6757 | kb_translation_table[KEY_K] |= 0x0e << 8; |
| 6758 | |
| 6759 | kb_translation_table[KEY_L] = 'L'; |
| 6760 | kb_translation_table[KEY_L] |= 0x0f << 8; |
| 6761 | |
| 6762 | kb_translation_table[KEY_SEMICOLON] = ';'; |
| 6763 | kb_translation_table[KEY_SEMICOLON] |= 0x33 << 8; |
| 6764 | |
| 6765 | kb_translation_table[KEY_APOSTROPHE] = '\''; |
| 6766 | kb_translation_table[KEY_APOSTROPHE] |= 0x34 << 8; |
| 6767 | |
| 6768 | kb_translation_table[KEY_GRAVE] = '`'; |
| 6769 | kb_translation_table[KEY_GRAVE] |= 0x35 << 8; |
| 6770 | |
| 6771 | kb_translation_table[KEY_LEFTSHIFT] = 0; |
| 6772 | kb_translation_table[KEY_LEFTSHIFT] |= 0xe1 << 8; |
| 6773 | |
| 6774 | kb_translation_table[KEY_BACKSLASH] = '\\'; |
| 6775 | kb_translation_table[KEY_BACKSLASH] |= 0x31 << 8; |
| 6776 | |
| 6777 | kb_translation_table[KEY_Z] = 'Z'; |
| 6778 | kb_translation_table[KEY_Z] |= 0x1d << 8; |
| 6779 | |
| 6780 | kb_translation_table[KEY_X] = 'X'; |
| 6781 | kb_translation_table[KEY_X] |= 0x1b << 8; |
| 6782 | |
| 6783 | kb_translation_table[KEY_C] = 'C'; |
| 6784 | kb_translation_table[KEY_C] |= 0x06 << 8; |
| 6785 | |
| 6786 | kb_translation_table[KEY_V] = 'V'; |
| 6787 | kb_translation_table[KEY_V] |= 0x19 << 8; |
| 6788 | |
| 6789 | kb_translation_table[KEY_B] = 'B'; |
| 6790 | kb_translation_table[KEY_B] |= 0x05 << 8; |
| 6791 | |
| 6792 | kb_translation_table[KEY_N] = 'N'; |
| 6793 | kb_translation_table[KEY_N] |= 0x11 << 8; |
| 6794 | |
| 6795 | kb_translation_table[KEY_M] = 'M'; |
| 6796 | kb_translation_table[KEY_M] |= 0x10 << 8; |
| 6797 | |
| 6798 | kb_translation_table[KEY_COMMA] = ','; |
| 6799 | kb_translation_table[KEY_COMMA] |= 0x36 << 8; |
| 6800 | |
| 6801 | kb_translation_table[KEY_DOT] = '.'; |
| 6802 | kb_translation_table[KEY_DOT] |= 0x37 << 8; |
| 6803 | |
| 6804 | kb_translation_table[KEY_SLASH] = '/'; |
| 6805 | kb_translation_table[KEY_SLASH] |= 0x38 << 8; |
| 6806 | |
| 6807 | kb_translation_table[KEY_RIGHTSHIFT] = 0; |
| 6808 | kb_translation_table[KEY_RIGHTSHIFT] |= 0xe5 << 8; |
| 6809 | |
| 6810 | kb_translation_table[KEY_KPASTERISK] = '*'; |
| 6811 | kb_translation_table[KEY_KPASTERISK] |= 0x55 << 8; |
| 6812 | |
| 6813 | kb_translation_table[KEY_LEFTALT] = 0; |
| 6814 | kb_translation_table[KEY_LEFTALT] |= 0xe2 << 8; |
| 6815 | |
| 6816 | kb_translation_table[KEY_SPACE] = ' '; |
| 6817 | kb_translation_table[KEY_SPACE] |= 0x2c << 8; |
| 6818 | |
| 6819 | kb_translation_table[KEY_CAPSLOCK] = 0; |
| 6820 | kb_translation_table[KEY_CAPSLOCK] |= 0x39 << 8; |
| 6821 | |
| 6822 | kb_translation_table[KEY_F1] = 0; |
| 6823 | kb_translation_table[KEY_F1] |= 0x3a << 8; |
| 6824 | |
| 6825 | kb_translation_table[KEY_F2] = 0; |
| 6826 | kb_translation_table[KEY_F2] |= 0x3b << 8; |
| 6827 | |
| 6828 | kb_translation_table[KEY_F3] = 0; |
| 6829 | kb_translation_table[KEY_F3] |= 0x3c << 8; |
| 6830 | |
| 6831 | kb_translation_table[KEY_F4] = 0; |
| 6832 | kb_translation_table[KEY_F4] |= 0x3d << 8; |
| 6833 | |
| 6834 | kb_translation_table[KEY_F5] = 0; |
| 6835 | kb_translation_table[KEY_F5] |= 0x3e << 8; |
| 6836 | |
| 6837 | kb_translation_table[KEY_F6] = 0; |
| 6838 | kb_translation_table[KEY_F6] |= 0x3f << 8; |
| 6839 | |
| 6840 | kb_translation_table[KEY_F7] = 0; |
| 6841 | kb_translation_table[KEY_F7] |= 0x40 << 8; |
| 6842 | |
| 6843 | kb_translation_table[KEY_F8] = 0; |
| 6844 | kb_translation_table[KEY_F8] |= 0x41 << 8; |
| 6845 | |
| 6846 | kb_translation_table[KEY_F9] = 0; |
| 6847 | kb_translation_table[KEY_F9] |= 0x42 << 8; |
| 6848 | |
| 6849 | kb_translation_table[KEY_F10] = 0; |
| 6850 | kb_translation_table[KEY_F10] |= 0x43 << 8; |
| 6851 | |
| 6852 | kb_translation_table[KEY_NUMLOCK] = 0; |
| 6853 | kb_translation_table[KEY_NUMLOCK] |= 0x53 << 8; |
| 6854 | |
| 6855 | kb_translation_table[KEY_SCROLLLOCK] = 0; |
| 6856 | kb_translation_table[KEY_SCROLLLOCK] |= 0x47 << 8; |
| 6857 | |
| 6858 | kb_translation_table[KEY_KP7] = '7'; |
| 6859 | kb_translation_table[KEY_KP7] |= 0x5f << 8; |
| 6860 | |
| 6861 | kb_translation_table[KEY_KP8] = '8'; |
| 6862 | kb_translation_table[KEY_KP8] |= 0x60 << 8; |
| 6863 | |
| 6864 | kb_translation_table[KEY_KP9] = '9'; |
| 6865 | kb_translation_table[KEY_KP9] |= 0x61 << 8; |
| 6866 | |
| 6867 | kb_translation_table[KEY_KPMINUS] = '-'; |
| 6868 | kb_translation_table[KEY_KPMINUS] |= 0x56 << 8; |
| 6869 | |
| 6870 | kb_translation_table[KEY_KP4] = '4'; |
| 6871 | kb_translation_table[KEY_KP4] |= 0x5c << 8; |
| 6872 | |
| 6873 | kb_translation_table[KEY_KP5] = '5'; |
| 6874 | kb_translation_table[KEY_KP5] |= 0x5d << 8; |
| 6875 | |
| 6876 | kb_translation_table[KEY_KP6] = '6'; |
| 6877 | kb_translation_table[KEY_KP6] |= 0x5e << 8; |
| 6878 | |
| 6879 | kb_translation_table[KEY_KPPLUS] = '+'; |
| 6880 | kb_translation_table[KEY_KPPLUS] |= 0x57 << 8; |
| 6881 | |
| 6882 | kb_translation_table[KEY_KP1] = '1'; |
| 6883 | kb_translation_table[KEY_KP1] |= 0x59 << 8; |
| 6884 | |
| 6885 | kb_translation_table[KEY_KP2] = '2'; |
| 6886 | kb_translation_table[KEY_KP2] |= 0x51 << 8; |
| 6887 | |
| 6888 | kb_translation_table[KEY_KP3] = '3'; |
| 6889 | kb_translation_table[KEY_KP3] |= 0x52 << 8; |
| 6890 | |
| 6891 | kb_translation_table[KEY_KP0] = '0'; |
| 6892 | kb_translation_table[KEY_KP0] |= 0x62 << 8; |
| 6893 | |
| 6894 | kb_translation_table[KEY_KPDOT] = '.'; |
| 6895 | kb_translation_table[KEY_KPDOT] |= 0x63 << 8; |
| 6896 | |
| 6897 | kb_translation_table[KEY_F11] = 0; |
| 6898 | kb_translation_table[KEY_F11] |= 0x44 << 8; |
| 6899 | |
| 6900 | kb_translation_table[KEY_F12] = 0; |
| 6901 | kb_translation_table[KEY_F12] |= 0x45 << 8; |
| 6902 | |
| 6903 | // kb_translation_table[KEY_KPJPCOMMA] = ','; |
| 6904 | // kb_translation_table[KEY_KPJPCOMMA] |= 0x85 << 8; |
| 6905 | |
| 6906 | kb_translation_table[KEY_KPENTER] = '\n'; |
| 6907 | kb_translation_table[KEY_KPENTER] |= 0x58 << 8; |
| 6908 | |
| 6909 | kb_translation_table[KEY_RIGHTCTRL] = 0; |
| 6910 | kb_translation_table[KEY_RIGHTCTRL] |= 0xe4 << 8; |
| 6911 | |
| 6912 | kb_translation_table[KEY_KPSLASH] = '/'; |
| 6913 | kb_translation_table[KEY_KPSLASH] |= 0x54 << 8; |
| 6914 | |
| 6915 | kb_translation_table[KEY_SYSRQ] = 0; |
| 6916 | kb_translation_table[KEY_SYSRQ] |= 0x46 << 8; |
| 6917 | |
| 6918 | kb_translation_table[KEY_RIGHTALT] = 0; |
| 6919 | kb_translation_table[KEY_RIGHTALT] |= 0xe6 << 8; |
| 6920 | |
| 6921 | kb_translation_table[KEY_HOME] = 0; |
| 6922 | kb_translation_table[KEY_HOME] |= 0x4a << 8; |
| 6923 | |
| 6924 | kb_translation_table[KEY_UP] = 0; |
| 6925 | kb_translation_table[KEY_UP] |= 0x52 << 8; |
| 6926 | |
| 6927 | kb_translation_table[KEY_PAGEUP] = 0; |
| 6928 | kb_translation_table[KEY_PAGEUP] |= 0x4b << 8; |
| 6929 | |
| 6930 | kb_translation_table[KEY_LEFT] = 0; |
| 6931 | kb_translation_table[KEY_LEFT] |= 0x50 << 8; |
| 6932 | |
| 6933 | kb_translation_table[KEY_RIGHT] = 0; |
| 6934 | kb_translation_table[KEY_RIGHT] |= 0x4f << 8; |
| 6935 | |
| 6936 | kb_translation_table[KEY_END] = 0; |
| 6937 | kb_translation_table[KEY_END] |= 0x4d << 8; |
| 6938 | |
| 6939 | kb_translation_table[KEY_DOWN] = 0; |
| 6940 | kb_translation_table[KEY_DOWN] |= 0x51 << 8; |
| 6941 | |
| 6942 | kb_translation_table[KEY_PAGEDOWN] = 0; |
| 6943 | kb_translation_table[KEY_PAGEDOWN] |= 0x4e << 8; |
| 6944 | |
| 6945 | kb_translation_table[KEY_INSERT] = 0; |
| 6946 | kb_translation_table[KEY_INSERT] |= 0x49 << 8; |
| 6947 | |
| 6948 | kb_translation_table[KEY_DELETE] = 0x7f; |
| 6949 | kb_translation_table[KEY_DELETE] |= 0x4c << 8; |
| 6950 | |
| 6951 | kb_translation_table[KEY_MUTE] = 0; |
| 6952 | kb_translation_table[KEY_MUTE] |= 0x7f << 8; |
| 6953 | |
| 6954 | kb_translation_table[KEY_VOLUMEDOWN] = 0; |
| 6955 | kb_translation_table[KEY_VOLUMEDOWN] |= 0x81 << 8; |
| 6956 | |
| 6957 | kb_translation_table[KEY_VOLUMEUP] = 0; |
| 6958 | kb_translation_table[KEY_VOLUMEUP] |= 0x80 << 8; |
| 6959 | |
| 6960 | kb_translation_table[KEY_POWER] = 0; |
| 6961 | kb_translation_table[KEY_POWER] |= 0x66 << 8; |
| 6962 | |
| 6963 | kb_translation_table[KEY_KPEQUAL] = '='; |
| 6964 | kb_translation_table[KEY_KPEQUAL] |= 0x67 << 8; |
| 6965 | |
| 6966 | kb_translation_table[KEY_PAUSE] = 0; |
| 6967 | kb_translation_table[KEY_PAUSE] |= 0x48 << 8; |
| 6968 | |
| 6969 | kb_translation_table[KEY_KPCOMMA] = ','; |
| 6970 | kb_translation_table[KEY_KPCOMMA] |= 0x85 << 8; |
| 6971 | |
| 6972 | kb_translation_table[KEY_LEFTMETA] = 0; |
| 6973 | kb_translation_table[KEY_LEFTMETA] |= 0xe3 << 8; |
| 6974 | |
| 6975 | kb_translation_table[KEY_RIGHTMETA] = 0; |
| 6976 | kb_translation_table[KEY_RIGHTMETA] |= 0xe7 << 8; |
| 6977 | |
| 6978 | kb_translation_table[KEY_STOP] = 0; |
| 6979 | kb_translation_table[KEY_STOP] |= 0x78 << 8; |
| 6980 | |
| 6981 | kb_translation_table[KEY_AGAIN] = 0; |
| 6982 | kb_translation_table[KEY_AGAIN] |= 0x79 << 8; |
| 6983 | |
| 6984 | kb_translation_table[KEY_UNDO] = 0; |
| 6985 | kb_translation_table[KEY_UNDO] |= 0x7a << 8; |
| 6986 | |
| 6987 | kb_translation_table[KEY_COPY] = 0; |
| 6988 | kb_translation_table[KEY_COPY] |= 0x7c << 8; |
| 6989 | |
| 6990 | kb_translation_table[KEY_PASTE] = 0; |
| 6991 | kb_translation_table[KEY_PASTE] |= 0x7d << 8; |
| 6992 | |
| 6993 | kb_translation_table[KEY_FIND] = 0; |
| 6994 | kb_translation_table[KEY_FIND] |= 0x7e << 8; |
| 6995 | |
| 6996 | kb_translation_table[KEY_CUT] = 0; |
| 6997 | kb_translation_table[KEY_CUT] |= 0x7b << 8; |
| 6998 | |
| 6999 | kb_translation_table[KEY_HELP] = 0; |
| 7000 | kb_translation_table[KEY_HELP] |= 0x75 << 8; |
| 7001 | |
| 7002 | kb_translation_table[KEY_MENU] = 0; |
| 7003 | kb_translation_table[KEY_MENU] |= 0x76 << 8; |
| 7004 | |
| 7005 | kb_translation_table[KEY_F13] = 0; |
| 7006 | kb_translation_table[KEY_F13] |= 0x68 << 8; |
| 7007 | |
| 7008 | kb_translation_table[KEY_F14] = 0; |
| 7009 | kb_translation_table[KEY_F14] |= 0x69 << 8; |
| 7010 | |
| 7011 | kb_translation_table[KEY_F15] = 0; |
| 7012 | kb_translation_table[KEY_F15] |= 0x6a << 8; |
| 7013 | |
| 7014 | kb_translation_table[KEY_F16] = 0; |
| 7015 | kb_translation_table[KEY_F16] |= 0x6b << 8; |
| 7016 | |
| 7017 | kb_translation_table[KEY_F17] = 0; |
| 7018 | kb_translation_table[KEY_F17] |= 0x6c << 8; |
| 7019 | |
| 7020 | kb_translation_table[KEY_F18] = 0; |
| 7021 | kb_translation_table[KEY_F18] |= 0x6d << 8; |
| 7022 | |
| 7023 | kb_translation_table[KEY_F19] = 0; |
| 7024 | kb_translation_table[KEY_F19] |= 0x6e << 8; |
| 7025 | |
| 7026 | kb_translation_table[KEY_F20] = 0; |
| 7027 | kb_translation_table[KEY_F20] |= 0x6f << 8; |
| 7028 | |
| 7029 | kb_translation_table[KEY_F21] = 0; |
| 7030 | kb_translation_table[KEY_F21] |= 0x70 << 8; |
| 7031 | |
| 7032 | kb_translation_table[KEY_F22] = 0; |
| 7033 | kb_translation_table[KEY_F22] |= 0x71 << 8; |
| 7034 | |
| 7035 | kb_translation_table[KEY_F23] = 0; |
| 7036 | kb_translation_table[KEY_F23] |= 0x72 << 8; |
| 7037 | |
| 7038 | kb_translation_table[KEY_F24] = 0; |
| 7039 | kb_translation_table[KEY_F24] |= 0x73 << 8; |
| 7040 | } |
| 7041 | |
| 7042 | static void Keyboard_translate_key( uint16_t evdev_key, long & ascii, long & usb ) |
| 7043 | { |
| 7044 | unsigned short tr = kb_translation_table[evdev_key]; |
| 7045 | |
| 7046 | ascii = tr & 0xff; |
| 7047 | usb = ( tr >> 8 ) & 0xff; |
| 7048 | } |
| 7049 | |
| 7050 | class linux_keyboard : public linux_device |
| 7051 | { |
| 7052 | public: |
| 7053 | linux_keyboard() : linux_device() |
| 7054 | { |
| 7055 | } |
| 7056 | |
| 7057 | virtual void callback() |
| 7058 | { |
| 7059 | input_event event; |
| 7060 | HidMsg msg; |
| 7061 | ssize_t len; |
| 7062 | |
| 7063 | while( ( len = read( fd, &event, sizeof( event ) ) ) > 0 ) |
| 7064 | { |
| 7065 | if( len < sizeof( event ) ) |
| 7066 | { |
| 7067 | EM_log( CK_LOG_WARNING, "keyboard: read event from keyboard %i smaller than expected (%i), ignoring", num, len ); |
| 7068 | continue; |
| 7069 | } |
| 7070 | |
| 7071 | if( event.type != EV_KEY ) |
| 7072 | continue; |
| 7073 | |
| 7074 | if( event.value == 2 ) |
| 7075 | continue; |
| 7076 | |
| 7077 | msg.clear(); |
| 7078 | msg.device_type = CK_HID_DEV_KEYBOARD; |
| 7079 | msg.device_num = num; |
| 7080 | msg.type = event.value ? CK_HID_BUTTON_DOWN : CK_HID_BUTTON_UP; |
| 7081 | msg.eid = event.code; |
| 7082 | msg.idata[0] = event.value; |
| 7083 | Keyboard_translate_key( event.code, msg.idata[2], msg.idata[1] ); |
| 7084 | |
| 7085 | HidInManager::push_message( msg ); |
| 7086 | } |
| 7087 | } |
| 7088 | }; |
| 7089 | |
| 7090 | static vector< linux_joystick * > * joysticks = NULL; |
| 7091 | static vector< linux_mouse * > * mice = NULL; |
| 7092 | static vector< linux_keyboard * > * keyboards = NULL; |
| 7093 | |
| 7094 | static map< int, linux_device * > * device_map = NULL; |
| 7095 | |
| 7096 | static pollfd * pollfds = NULL; |
| 7097 | #define DEFAULT_POLLFDS_SIZE (1) |
| 7098 | static size_t pollfds_size = 0; |
| 7099 | static size_t pollfds_end = 0; |
| 7100 | static int hid_channel_r = -1; // HID communications channel, read fd |
| 7101 | static int hid_channel_w = -1; // HID communications channel, write fd |
| 7102 | |
| 7103 | struct hid_channel_msg |
| 7104 | { |
| 7105 | int action; |
| 7106 | #define HID_CHANNEL_OPEN 1 |
| 7107 | #define HID_CHANNEL_CLOSE 0 |
| 7108 | #define HID_CHANNEL_QUIT -1 |
| 7109 | linux_device * device; |
| 7110 | }; |
| 7111 | |
| 7112 | static t_CKBOOL g_hid_init = FALSE; |
| 7113 | |
| 7114 | void Hid_init() |
| 7115 | { |
| 7116 | if( g_hid_init ) |
| 7117 | return; |
| 7118 | |
| 7119 | pollfds = new pollfd[DEFAULT_POLLFDS_SIZE]; |
| 7120 | pollfds_size = DEFAULT_POLLFDS_SIZE; |
| 7121 | pollfds_end = 0; |
| 7122 | |
| 7123 | int filedes[2]; |
| 7124 | if( pipe( filedes ) ) |
| 7125 | { |
| 7126 | EM_log( CK_LOG_HERALD, "hid: unable to create pipe, initialization failed" ); |
| 7127 | return; |
| 7128 | } |
| 7129 | |
| 7130 | hid_channel_r = filedes[0]; |
| 7131 | hid_channel_w = filedes[1]; |
| 7132 | |
| 7133 | int fd_flags = fcntl( hid_channel_r, F_GETFL ); |
| 7134 | fcntl( hid_channel_r, F_SETFL, fd_flags | O_NONBLOCK ); |
| 7135 | |
| 7136 | /* right now, the hid_channel is just used as a dummy file descriptor |
| 7137 | passed to poll, such that poll will work/block even when there are |
| 7138 | no open devices. In the future hid_channel could also be used to |
| 7139 | communicate between the VM thread and the HID thread */ |
| 7140 | pollfds[0].fd = hid_channel_r; |
| 7141 | pollfds[0].events = POLLIN; |
| 7142 | pollfds[0].revents = 0; |
| 7143 | pollfds_end = 1; |
| 7144 | |
| 7145 | device_map = new map< int, linux_device * >; |
| 7146 | |
| 7147 | Keyboard_init_translation_table(); |
| 7148 | |
| 7149 | g_hid_init = TRUE; |
| 7150 | } |
| 7151 | |
| 7152 | void Hid_poll() |
| 7153 | { |
| 7154 | if( !g_hid_init ) |
| 7155 | return; |
| 7156 | |
| 7157 | hid_channel_msg hcm; |
| 7158 | while( poll( pollfds, pollfds_end, -1 ) > 0 ) |
| 7159 | { |
| 7160 | for( int i = 1; i < pollfds_end; i++ ) |
| 7161 | { |
| 7162 | if( pollfds[i].revents & POLLIN ) |
| 7163 | { |
| 7164 | ( *device_map )[pollfds[i].fd]->callback(); |
| 7165 | pollfds[i].revents = 0; |
| 7166 | } |
| 7167 | } |
| 7168 | |
| 7169 | if( pollfds[0].revents & POLLIN ) |
| 7170 | { |
| 7171 | while( read( hid_channel_r, &hcm, sizeof( hcm ) ) > 0 ) |
| 7172 | { |
| 7173 | if( hcm.action == HID_CHANNEL_OPEN ) |
| 7174 | { |
| 7175 | if( pollfds_end >= pollfds_size ) |
| 7176 | { |
| 7177 | pollfds_size = pollfds_end * 2; |
| 7178 | pollfd * t_pollfds = new pollfd[pollfds_size]; |
| 7179 | memcpy( t_pollfds, pollfds, pollfds_end * sizeof( pollfd ) ); |
| 7180 | delete[] pollfds; |
| 7181 | pollfds = t_pollfds; |
| 7182 | } |
| 7183 | |
| 7184 | pollfds[pollfds_end].fd = hcm.device->fd; |
| 7185 | pollfds[pollfds_end].events = POLLIN; |
| 7186 | pollfds[pollfds_end].revents = 0; |
| 7187 | hcm.device->pollfds_i = pollfds_end; |
| 7188 | ( *device_map )[hcm.device->fd] = hcm.device; |
| 7189 | pollfds_end++; |
| 7190 | } |
| 7191 | |
| 7192 | else if( hcm.action == HID_CHANNEL_CLOSE ) |
| 7193 | { |
| 7194 | // erase the closing entry by copying the last entry into it |
| 7195 | // this is okay even when joystick->pollfds_i == pollfds_end |
| 7196 | // because we decrement pollfds_end |
| 7197 | ( *device_map )[pollfds[pollfds_end - 1].fd]->pollfds_i = hcm.device->pollfds_i; |
| 7198 | pollfds[hcm.device->pollfds_i] = pollfds[pollfds_end - 1]; |
| 7199 | pollfds_end--; |
| 7200 | close( hcm.device->fd ); |
| 7201 | device_map->erase( hcm.device->fd ); |
| 7202 | } |
| 7203 | |
| 7204 | else if( hcm.action == HID_CHANNEL_QUIT ) |
| 7205 | { |
| 7206 | close( hid_channel_r ); |
| 7207 | return; |
| 7208 | } |
| 7209 | |
| 7210 | } |
| 7211 | |
| 7212 | break; |
| 7213 | } |
| 7214 | } |
| 7215 | } |
| 7216 | |
| 7217 | void Hid_quit() |
| 7218 | { |
| 7219 | if( !g_hid_init ) |
| 7220 | return; |
| 7221 | |
| 7222 | hid_channel_msg hcm = { HID_CHANNEL_QUIT, NULL }; |
| 7223 | if (write( hid_channel_w, &hcm, sizeof( hcm ) ) == -1) |
| 7224 | EM_log( CK_LOG_WARNING, "HID_CHANNEL_QUIT message failed: %s" , strerror( errno ) ); |
| 7225 | |
| 7226 | close( hid_channel_w ); |
| 7227 | |
| 7228 | delete[] pollfds; |
| 7229 | delete device_map; |
| 7230 | |
| 7231 | g_hid_init = FALSE; |
| 7232 | } |
| 7233 | |
| 7234 | int TiltSensor_read( t_CKINT * x, t_CKINT * y, t_CKINT * z ) |
| 7235 | { |
| 7236 | return 0; |
| 7237 | } |
| 7238 | |
| 7239 | // designate new poll rate |
| 7240 | t_CKINT TiltSensor_setPollRate( t_CKINT usec ) |
| 7241 | { |
| 7242 | // sanity |
| 7243 | assert( usec >= 0 ); |
| 7244 | // not supported |
| 7245 | CK_FPRINTF_STDERR( "TiltSensor - setPollRate is not (yet) supported on this platform...\n" ); |
| 7246 | return -1; |
| 7247 | } |
| 7248 | |
| 7249 | // query current poll rate |
| 7250 | t_CKINT TiltSensor_getPollRate( ) |
| 7251 | { |
| 7252 | // not supported |
| 7253 | CK_FPRINTF_STDERR( "TiltSensor - getPollRate is not (yet) supported on this platform...\n" ); |
| 7254 | return -1; |
| 7255 | } |
| 7256 | |
| 7257 | |
| 7258 | /***************************************************************************** |
| 7259 | Linux joystick support |
| 7260 | *****************************************************************************/ |
| 7261 | #pragma mark Linux joystick support |
| 7262 | |
| 7263 | void Joystick_init() |
| 7264 | { |
| 7265 | if( joysticks != NULL ) |
| 7266 | return; |
| 7267 | |
| 7268 | EM_log( CK_LOG_INFO, "initializing joysticks" ); |
| 7269 | EM_pushlog(); |
| 7270 | |
| 7271 | joysticks = new vector< linux_joystick * >; |
| 7272 | |
| 7273 | DIR * dir_handle; |
| 7274 | struct dirent * dir_entity; |
| 7275 | struct stat stat_buf; |
| 7276 | int js_num, fd, i; |
| 7277 | uid_t uid = geteuid(); |
| 7278 | gid_t gid = getegid(); |
| 7279 | char buf[CK_HID_STRBUFSIZE]; |
| 7280 | linux_joystick * js; |
| 7281 | |
| 7282 | dir_handle = opendir( CK_HID_DIR ); |
| 7283 | if( dir_handle == NULL ) |
| 7284 | { |
| 7285 | if( errno == EACCES ) |
| 7286 | EM_log( CK_LOG_WARNING, "hid: error opening %s, unable to initialize joystick", CK_HID_DIR ); |
| 7287 | EM_poplog(); |
| 7288 | return; |
| 7289 | } |
| 7290 | |
| 7291 | while( dir_entity = readdir( dir_handle ) ) |
| 7292 | { |
| 7293 | if( sscanf( dir_entity->d_name, CK_HID_JOYSTICKFILE, &js_num ) ) |
| 7294 | { |
| 7295 | snprintf( buf, CK_HID_STRBUFSIZE, "%s/%s", CK_HID_DIR, |
| 7296 | dir_entity->d_name ); |
| 7297 | if( ( fd = open( buf, O_RDONLY | O_NONBLOCK ) ) >= 0 || |
| 7298 | errno == EACCES ) /* wait to report access errors until the |
| 7299 | device is actually opened */ |
| 7300 | { |
| 7301 | js = new linux_joystick; |
| 7302 | js->js_num = js_num; |
| 7303 | js->num = joysticks->size(); |
| 7304 | if( fd >= 0 ) |
| 7305 | { |
| 7306 | ioctl( fd, JSIOCGNAME( CK_HID_NAMESIZE ), js->name ); |
| 7307 | close( fd ); // no need to keep the file open |
| 7308 | } |
| 7309 | strncpy( js->filename, buf, CK_HID_STRBUFSIZE ); |
| 7310 | joysticks->push_back( js ); |
| 7311 | EM_log( CK_LOG_INFO, "joystick: found device %s", js->name ); |
| 7312 | } |
| 7313 | } |
| 7314 | } |
| 7315 | |
| 7316 | closedir( dir_handle ); |
| 7317 | EM_poplog(); |
| 7318 | } |
| 7319 | |
| 7320 | void Joystick_poll() |
| 7321 | { |
| 7322 | |
| 7323 | } |
| 7324 | |
| 7325 | void Joystick_quit() |
| 7326 | { |
| 7327 | if( joysticks == NULL ) |
| 7328 | return; |
| 7329 | |
| 7330 | vector< linux_joystick * >::size_type i, len = joysticks->size(); |
| 7331 | |
| 7332 | for( i = 0; i < len; i++ ) |
| 7333 | delete joysticks->at( i ); |
| 7334 | |
| 7335 | delete joysticks; |
| 7336 | joysticks = NULL; |
| 7337 | } |
| 7338 | |
| 7339 | int Joystick_count() |
| 7340 | { |
| 7341 | if( joysticks == NULL ) |
| 7342 | return 0; |
| 7343 | return joysticks->size(); |
| 7344 | } |
| 7345 | |
| 7346 | int Joystick_open( int js ) |
| 7347 | { |
| 7348 | if( joysticks == NULL || js < 0 || js >= joysticks->size() ) |
| 7349 | return -1; |
| 7350 | |
| 7351 | linux_joystick * joystick = joysticks->at( js ); |
| 7352 | |
| 7353 | if( joystick->refcount == 0 ) |
| 7354 | { |
| 7355 | if( ( joystick->fd = open( joystick->filename, O_RDONLY | O_NONBLOCK ) ) < 0 ) |
| 7356 | { |
| 7357 | EM_log( CK_LOG_HERALD, "joystick: unable to open %s: %s", joystick->filename, strerror( errno ) ); |
| 7358 | return -1; |
| 7359 | } |
| 7360 | |
| 7361 | hid_channel_msg hcm = { HID_CHANNEL_OPEN, joystick }; |
| 7362 | if(write( hid_channel_w, &hcm, sizeof( hcm ) ) == -1) |
| 7363 | { |
| 7364 | EM_log( CK_LOG_HERALD, "joystick: unable to write channel message %s: %s", |
| 7365 | joystick->filename, strerror( errno ) ); |
| 7366 | return -1; |
| 7367 | } |
| 7368 | } |
| 7369 | |
| 7370 | joystick->refcount++; |
| 7371 | |
| 7372 | return 0; |
| 7373 | } |
| 7374 | |
| 7375 | int Joystick_close( int js ) |
| 7376 | { |
| 7377 | if( joysticks == NULL || js < 0 || js >= joysticks->size() ) |
| 7378 | return -1; |
| 7379 | |
| 7380 | linux_joystick * joystick = joysticks->at( js ); |
| 7381 | |
| 7382 | joystick->refcount--; |
| 7383 | |
| 7384 | if( joystick->refcount == 0 ) |
| 7385 | { |
| 7386 | hid_channel_msg hcm = { HID_CHANNEL_CLOSE, joystick }; |
| 7387 | if(write( hid_channel_w, &hcm, sizeof( hcm ) ) == -1) return -1; |
| 7388 | EM_log( CK_LOG_HERALD, "joystick: unable complete close message: %s", |
| 7389 | strerror( errno ) ); |
| 7390 | } |
| 7391 | |
| 7392 | |
| 7393 | return 0; |
| 7394 | } |
| 7395 | |
| 7396 | const char * Joystick_name( int js ) |
| 7397 | { |
| 7398 | if( joysticks == NULL || js < 0 || js >= joysticks->size() ) |
| 7399 | return NULL; |
| 7400 | |
| 7401 | return joysticks->at( js )->name; |
| 7402 | } |
| 7403 | |
| 7404 | #define test_bit( array, bit ) (array[bit/8] & (1<<(bit%8))) |
| 7405 | |
| 7406 | void Mouse_configure( const char * filename ) |
| 7407 | { |
| 7408 | struct stat statbuf; |
| 7409 | int fd, devmajor, devminor, is_mouse = 0; |
| 7410 | linux_mouse * mouse; |
| 7411 | unsigned char relcaps[(REL_MAX / 8) + 1]; |
| 7412 | unsigned char abscaps[(ABS_MAX / 8) + 1]; |
| 7413 | unsigned char keycaps[(KEY_MAX / 8) + 1]; |
| 7414 | |
| 7415 | if( stat( filename, &statbuf ) == -1 ) |
| 7416 | return; |
| 7417 | |
| 7418 | if( S_ISCHR( statbuf.st_mode ) == 0 ) |
| 7419 | return; /* not a character device... */ |
| 7420 | |
| 7421 | #ifndef __FreeBSD__ |
| 7422 | devmajor = ( statbuf.st_rdev & 0xFF00 ) >> 8; |
| 7423 | devminor = ( statbuf.st_rdev & 0x00FF ); |
| 7424 | if ( ( devmajor != 13 ) || ( devminor < 64 ) || ( devminor > 96 ) ) |
| 7425 | return; /* not an evdev. */ |
| 7426 | #endif |
| 7427 | |
| 7428 | if( ( fd = open( filename, O_RDONLY | O_NONBLOCK ) ) < 0 ) |
| 7429 | return; |
| 7430 | |
| 7431 | memset( relcaps, 0, sizeof( relcaps ) ); |
| 7432 | memset( abscaps, 0, sizeof( abscaps ) ); |
| 7433 | memset( keycaps, 0, sizeof( keycaps ) ); |
| 7434 | |
| 7435 | //int num_keys = 0; |
| 7436 | if( ioctl( fd, EVIOCGBIT( EV_KEY, sizeof( keycaps ) ), keycaps ) == -1 ) |
| 7437 | return; |
| 7438 | |
| 7439 | is_mouse = 0; |
| 7440 | if( ioctl( fd, EVIOCGBIT( EV_REL, sizeof( relcaps ) ), relcaps ) != -1 ) |
| 7441 | { |
| 7442 | if( test_bit( relcaps, REL_X ) && test_bit( relcaps, REL_Y ) && |
| 7443 | test_bit( keycaps, BTN_MOUSE ) ) |
| 7444 | { |
| 7445 | is_mouse = 1; |
| 7446 | } |
| 7447 | |
| 7448 | if( test_bit( relcaps, REL_DIAL ) ) |
| 7449 | is_mouse = 1; |
| 7450 | } |
| 7451 | |
| 7452 | if( ioctl( fd, EVIOCGBIT( EV_ABS, sizeof( abscaps ) ), abscaps ) != -1 ) |
| 7453 | { |
| 7454 | if( test_bit( abscaps, ABS_X ) && test_bit( abscaps, ABS_Y ) && |
| 7455 | ( test_bit( keycaps, BTN_MOUSE ) || test_bit( keycaps, BTN_TOUCH ) ) ) |
| 7456 | is_mouse = 1; |
| 7457 | } |
| 7458 | |
| 7459 | if( !is_mouse ) |
| 7460 | return; |
| 7461 | |
| 7462 | mouse = new linux_mouse; |
| 7463 | mouse->num = mice->size(); |
| 7464 | ioctl( fd, EVIOCGNAME( CK_HID_NAMESIZE ), mouse->name ); |
| 7465 | if( fd >= 0 ) |
| 7466 | close( fd ); // no need to keep the file open |
| 7467 | strncpy( mouse->filename, filename, CK_HID_STRBUFSIZE ); |
| 7468 | mice->push_back( mouse ); |
| 7469 | EM_log( CK_LOG_INFO, "mouse: found device %s", mouse->name ); |
| 7470 | } |
| 7471 | |
| 7472 | void Mouse_init() |
| 7473 | { |
| 7474 | if( mice != NULL ) |
| 7475 | return; |
| 7476 | |
| 7477 | EM_log( CK_LOG_INFO, "initializing mice" ); |
| 7478 | EM_pushlog(); |
| 7479 | |
| 7480 | mice = new vector< linux_mouse * >; |
| 7481 | |
| 7482 | DIR * dir_handle; |
| 7483 | struct dirent * dir_entity; |
| 7484 | struct stat stat_buf; |
| 7485 | int m_num, fd, i; |
| 7486 | char buf[CK_HID_STRBUFSIZE]; |
| 7487 | linux_mouse * mouse; |
| 7488 | |
| 7489 | dir_handle = opendir( CK_HID_DIR ); |
| 7490 | if( dir_handle == NULL ) |
| 7491 | { |
| 7492 | EM_log( CK_LOG_WARNING, "hid: error opening %s, unable to initialize mice", CK_HID_DIR ); |
| 7493 | EM_poplog(); |
| 7494 | return; |
| 7495 | } |
| 7496 | |
| 7497 | while( dir_entity = readdir( dir_handle ) ) |
| 7498 | { |
| 7499 | if( sscanf( dir_entity->d_name, CK_HID_EVDEVFILE, &m_num ) ) |
| 7500 | { |
| 7501 | snprintf( buf, CK_HID_STRBUFSIZE, "%s/%s", CK_HID_DIR, |
| 7502 | dir_entity->d_name ); |
| 7503 | Mouse_configure( buf ); |
| 7504 | /* if( ( fd = open( buf, O_RDONLY | O_NONBLOCK ) ) >= 0 || |
| 7505 | errno == EACCES ) *//* wait to report access errors until the |
| 7506 | device is actually opened *//* |
| 7507 | { |
| 7508 | mouse = new linux_mouse; |
| 7509 | mouse->m_num = m_num; |
| 7510 | mouse->num = mice->size(); |
| 7511 | //ioctl( fd, JSIOCGNAME( CK_HID_NAMESIZE ), js->name ); |
| 7512 | if( fd >= 0 ) |
| 7513 | close( fd ); // no need to keep the file open |
| 7514 | strncpy( mouse->filename, buf, CK_HID_STRBUFSIZE ); |
| 7515 | mice->push_back( mouse ); |
| 7516 | //EM_log( CK_LOG_INFO, "mouse: found device %s", mouse->name ); |
| 7517 | }*/ |
| 7518 | } |
| 7519 | } |
| 7520 | |
| 7521 | closedir( dir_handle ); |
| 7522 | EM_poplog(); |
| 7523 | |
| 7524 | } |
| 7525 | |
| 7526 | void Mouse_poll() |
| 7527 | { |
| 7528 | |
| 7529 | } |
| 7530 | |
| 7531 | void Mouse_quit() |
| 7532 | { |
| 7533 | if( mice == NULL ) |
| 7534 | return; |
| 7535 | |
| 7536 | vector< linux_mouse * >::size_type i, len = mice->size(); |
| 7537 | |
| 7538 | for( i = 0; i < len; i++ ) |
| 7539 | delete mice->at( i ); |
| 7540 | |
| 7541 | delete mice; |
| 7542 | mice = NULL; |
| 7543 | } |
| 7544 | |
| 7545 | int Mouse_count() |
| 7546 | { |
| 7547 | if( !mice ) |
| 7548 | return 0; |
| 7549 | return mice->size(); |
| 7550 | } |
| 7551 | |
| 7552 | int Mouse_open( int m ) |
| 7553 | { |
| 7554 | if( mice == NULL || m < 0 || m >= mice->size() ) |
| 7555 | return -1; |
| 7556 | |
| 7557 | linux_mouse * mouse = mice->at( m ); |
| 7558 | |
| 7559 | if( mouse->refcount == 0 ) |
| 7560 | { |
| 7561 | if( ( mouse->fd = open( mouse->filename, O_RDONLY | O_NONBLOCK ) ) < 0 ) |
| 7562 | { |
| 7563 | EM_log( CK_LOG_HERALD, "mouse: unable to open %s: %s", mouse->filename, strerror( errno ) ); |
| 7564 | return -1; |
| 7565 | } |
| 7566 | |
| 7567 | mouse->needs_open = TRUE; |
| 7568 | hid_channel_msg hcm = { HID_CHANNEL_OPEN, mouse }; |
| 7569 | if(write( hid_channel_w, &hcm, sizeof( hcm ) ) == -1) |
| 7570 | { |
| 7571 | EM_log( CK_LOG_HERALD, "mouse: unable to complete open message %: %s", strerror( errno ) ); |
| 7572 | return -1; |
| 7573 | } |
| 7574 | } |
| 7575 | |
| 7576 | mouse->refcount++; |
| 7577 | |
| 7578 | return 0; |
| 7579 | } |
| 7580 | |
| 7581 | int Mouse_close( int m ) |
| 7582 | { |
| 7583 | if( mice == NULL || m < 0 || m >= mice->size() ) |
| 7584 | return -1; |
| 7585 | |
| 7586 | linux_mouse * mouse = mice->at( m ); |
| 7587 | |
| 7588 | mouse->refcount--; |
| 7589 | |
| 7590 | if( mouse->refcount == 0 ) |
| 7591 | { |
| 7592 | mouse->needs_close = TRUE; |
| 7593 | hid_channel_msg hcm = { HID_CHANNEL_CLOSE, mouse }; |
| 7594 | if(write( hid_channel_w, &hcm, sizeof( hcm ) ) == -1) |
| 7595 | EM_log( CK_LOG_WARNING, "mouse: HID_CHANNEL_CLOSE message failed: %s" , strerror( errno ) ); |
| 7596 | } |
| 7597 | |
| 7598 | return 0; |
| 7599 | } |
| 7600 | |
| 7601 | const char * Mouse_name( int m ) |
| 7602 | { |
| 7603 | if( mice == NULL || m < 0 || m >= mice->size() ) |
| 7604 | return NULL; |
| 7605 | return mice->at( m )->name; |
| 7606 | } |
| 7607 | |
| 7608 | /** return codes for Keyboard_configure */ |
| 7609 | enum { |
| 7610 | HID_KB_CONFIG_SUCCESS = 0, |
| 7611 | HID_KB_CONFIG_INVALID, |
| 7612 | HID_KB_CONFIG_ERROR_CANTOPEN, |
| 7613 | }; |
| 7614 | |
| 7615 | int Keyboard_configure( const char * filename ) |
| 7616 | { |
| 7617 | struct stat statbuf; |
| 7618 | int fd, devmajor, devminor; |
| 7619 | linux_keyboard * keyboard; |
| 7620 | unsigned char relcaps[(REL_MAX / 8) + 1]; |
| 7621 | unsigned char abscaps[(ABS_MAX / 8) + 1]; |
| 7622 | unsigned char keycaps[(KEY_MAX / 8) + 1]; |
| 7623 | |
| 7624 | if( stat( filename, &statbuf ) == -1 ) |
| 7625 | return HID_KB_CONFIG_ERROR_CANTOPEN; |
| 7626 | |
| 7627 | if( S_ISCHR( statbuf.st_mode ) == 0 ) |
| 7628 | return HID_KB_CONFIG_INVALID; /* not a character device... */ |
| 7629 | |
| 7630 | #ifndef __FreeBSD__ |
| 7631 | devmajor = ( statbuf.st_rdev & 0xFF00 ) >> 8; |
| 7632 | devminor = ( statbuf.st_rdev & 0x00FF ); |
| 7633 | if ( ( devmajor != 13 ) || ( devminor < 64 ) || ( devminor > 96 ) ) |
| 7634 | return HID_KB_CONFIG_INVALID; /* not an evdev. */ |
| 7635 | #endif |
| 7636 | |
| 7637 | if( ( fd = open( filename, O_RDONLY | O_NONBLOCK ) ) < 0 ) |
| 7638 | return HID_KB_CONFIG_ERROR_CANTOPEN; |
| 7639 | |
| 7640 | memset( relcaps, 0, sizeof( relcaps ) ); |
| 7641 | memset( abscaps, 0, sizeof( abscaps ) ); |
| 7642 | memset( keycaps, 0, sizeof( keycaps ) ); |
| 7643 | |
| 7644 | int num_keys = 0; |
| 7645 | if( ioctl( fd, EVIOCGBIT( EV_KEY, sizeof( keycaps ) ), keycaps ) == -1 ) |
| 7646 | return HID_KB_CONFIG_INVALID; |
| 7647 | |
| 7648 | if( ioctl( fd, EVIOCGBIT( EV_REL, sizeof( relcaps ) ), relcaps ) != -1 ) |
| 7649 | { |
| 7650 | for( int i = 0; i < sizeof( relcaps ); i++ ) |
| 7651 | if( relcaps[i] ) |
| 7652 | return HID_KB_CONFIG_INVALID; |
| 7653 | } |
| 7654 | |
| 7655 | if( ioctl( fd, EVIOCGBIT( EV_ABS, sizeof( abscaps ) ), abscaps ) != -1 ) |
| 7656 | { |
| 7657 | for( int i = 0; i < sizeof( abscaps ); i++ ) |
| 7658 | if( abscaps[i] ) |
| 7659 | return HID_KB_CONFIG_INVALID; |
| 7660 | } |
| 7661 | |
| 7662 | for( int i = 0; i < sizeof( keycaps ); i++ ) |
| 7663 | { |
| 7664 | for( int j = 0; j < 8; j++ ) |
| 7665 | { |
| 7666 | if( keycaps[i] & ( 1 << j ) ) |
| 7667 | num_keys++; |
| 7668 | } |
| 7669 | } |
| 7670 | |
| 7671 | keyboard = new linux_keyboard; |
| 7672 | keyboard->num = keyboards->size(); |
| 7673 | ioctl( fd, EVIOCGNAME( CK_HID_NAMESIZE ), keyboard->name ); |
| 7674 | if( fd >= 0 ) |
| 7675 | close( fd ); // no need to keep the file open |
| 7676 | strncpy( keyboard->filename, filename, CK_HID_STRBUFSIZE ); |
| 7677 | keyboards->push_back( keyboard ); |
| 7678 | EM_log( CK_LOG_INFO, "keyboard: found device %s", keyboard->name ); |
| 7679 | |
| 7680 | return HID_KB_CONFIG_SUCCESS; |
| 7681 | } |
| 7682 | |
| 7683 | void Keyboard_init() |
| 7684 | { |
| 7685 | if( keyboards != NULL ) |
| 7686 | return; |
| 7687 | |
| 7688 | EM_log( CK_LOG_INFO, "initializing keyboards" ); |
| 7689 | EM_pushlog(); |
| 7690 | |
| 7691 | keyboards = new vector< linux_keyboard * >; |
| 7692 | |
| 7693 | DIR * dir_handle; |
| 7694 | struct dirent * dir_entity; |
| 7695 | |
| 7696 | int m_num; |
| 7697 | |
| 7698 | char buf[CK_HID_STRBUFSIZE]; |
| 7699 | |
| 7700 | dir_handle = opendir( CK_HID_DIR ); |
| 7701 | if( dir_handle == NULL ) |
| 7702 | { |
| 7703 | if( errno == EACCES ) |
| 7704 | { |
| 7705 | ck_fprintf_stderr( "hid: error opening %s, unable to initialize keyboards\n", CK_HID_DIR ); |
| 7706 | ck_fprintf_stderr( "hid: please verify you have read permissions to device directory '%s' to use HID keyboard input devices\n", CK_HID_DIR ); |
| 7707 | } |
| 7708 | EM_poplog(); |
| 7709 | return; |
| 7710 | } |
| 7711 | |
| 7712 | while( dir_entity = readdir( dir_handle ) ) |
| 7713 | { |
| 7714 | if( sscanf( dir_entity->d_name, CK_HID_EVDEVFILE, &m_num ) ) |
| 7715 | { |
| 7716 | snprintf( buf, CK_HID_STRBUFSIZE, "%s/%s", CK_HID_DIR, |
| 7717 | dir_entity->d_name ); |
| 7718 | int result = Keyboard_configure( buf ); |
| 7719 | |
| 7720 | if(result == HID_KB_CONFIG_ERROR_CANTOPEN) |
| 7721 | { |
| 7722 | ck_fprintf_stderr( "hid: error opening %s, unable to check for keyboard\n", buf ); |
| 7723 | ck_fprintf_stderr( "hid: please verify you have read permissions to input device '%s' to use as HID keyboard input device\n", buf ); |
| 7724 | } |
| 7725 | } |
| 7726 | } |
| 7727 | |
| 7728 | closedir( dir_handle ); |
| 7729 | EM_poplog(); |
| 7730 | |
| 7731 | } |
| 7732 | |
| 7733 | void Keyboard_poll() |
| 7734 | { |
| 7735 | |
| 7736 | } |
| 7737 | |
| 7738 | void Keyboard_quit() |
| 7739 | { |
| 7740 | |
| 7741 | } |
| 7742 | |
| 7743 | int Keyboard_count() |
| 7744 | { |
| 7745 | if( keyboards == NULL ) |
| 7746 | return 0; |
| 7747 | return keyboards->size(); |
| 7748 | } |
| 7749 | |
| 7750 | int Keyboard_open( int k ) |
| 7751 | { |
| 7752 | if( keyboards == NULL || k < 0 || k >= keyboards->size() ) |
| 7753 | return -1; |
| 7754 | |
| 7755 | linux_keyboard * keyboard = keyboards->at( k ); |
| 7756 | |
| 7757 | if( keyboard->refcount == 0 ) |
| 7758 | { |
| 7759 | if( ( keyboard->fd = open( keyboard->filename, O_RDONLY | O_NONBLOCK ) ) < 0 ) |
| 7760 | { |
| 7761 | EM_log( CK_LOG_HERALD, "keyboard: unable to open %s: %s", keyboard->filename, strerror( errno ) ); |
| 7762 | return -1; |
| 7763 | } |
| 7764 | |
| 7765 | hid_channel_msg hcm = { HID_CHANNEL_OPEN, keyboard }; |
| 7766 | if(write( hid_channel_w, &hcm, sizeof( hcm ) ) == -1) |
| 7767 | { |
| 7768 | EM_log( CK_LOG_HERALD, "keyboard: unable to write open message: %s", strerror( errno ) ); |
| 7769 | return -1; |
| 7770 | } |
| 7771 | } |
| 7772 | |
| 7773 | keyboard->refcount++; |
| 7774 | |
| 7775 | return 0; |
| 7776 | } |
| 7777 | |
| 7778 | int Keyboard_close( int k ) |
| 7779 | { |
| 7780 | if( keyboards == NULL || k < 0 || k >= keyboards->size() ) |
| 7781 | return -1; |
| 7782 | |
| 7783 | linux_keyboard * keyboard = keyboards->at( k ); |
| 7784 | |
| 7785 | keyboard->refcount--; |
| 7786 | |
| 7787 | if( keyboard->refcount == 0 ) |
| 7788 | { |
| 7789 | hid_channel_msg hcm = { HID_CHANNEL_CLOSE, keyboard }; |
| 7790 | if(write( hid_channel_w, &hcm, sizeof( hcm ) ) == -1) |
| 7791 | EM_log( CK_LOG_WARNING, "keyboard: HID_CHANNEL_CLOSE message failed: %s" , strerror( errno ) ); |
| 7792 | } |
| 7793 | |
| 7794 | return 0; |
| 7795 | } |
| 7796 | |
| 7797 | const char * Keyboard_name( int k ) |
| 7798 | { |
| 7799 | if( keyboards == NULL || k < 0 || k >= keyboards->size() ) |
| 7800 | return NULL; |
| 7801 | |
| 7802 | return keyboards->at( k )->name; |
| 7803 | } |
| 7804 | |
| 7805 | #endif |
| 7806 | |
| 7807 | |
| 7808 | #ifdef __PLATFORM_WINDOWS__ |
| 7809 | |
| 7810 | // multiple monitors info: |
| 7811 | // https://stackoverflow.com/questions/4631292/how-to-detect-the-current-screen-resolution |
| 7812 | t_CKBOOL GetMonitorRealResolution( HMONITOR monitor, t_CKFLOAT * pixelsWidth, t_CKFLOAT * pixelsHeight ) |
| 7813 | { |
| 7814 | // zero out |
| 7815 | *pixelsWidth = *pixelsHeight = 0; |
| 7816 | |
| 7817 | // get monitor info |
| 7818 | MONITORINFOEX info; info.cbSize = sizeof( MONITORINFOEX ); |
| 7819 | if( !GetMonitorInfoA( monitor, &info ) ) return FALSE; |
| 7820 | |
| 7821 | // get display setting |
| 7822 | DEVMODE devmode; devmode.dmSize = sizeof( DEVMODE ); |
| 7823 | if( !EnumDisplaySettingsA( info.szDevice, ENUM_CURRENT_SETTINGS, &devmode ) ) return FALSE; |
| 7824 | |
| 7825 | // get monitor pixel dimensions (note does not take DPI scaling into account; see below for alternate method) |
| 7826 | *pixelsWidth = (t_CKFLOAT)devmode.dmPelsWidth; |
| 7827 | *pixelsHeight = (t_CKFLOAT)devmode.dmPelsHeight; |
| 7828 | |
| 7829 | // done |
| 7830 | return TRUE; |
| 7831 | } |
| 7832 | |
| 7833 | #endif |
| 7834 | |
| 7835 | |
| 7836 | |
| 7837 | |
| 7838 | //----------------------------------------------------------------------------- |
| 7839 | // unified easy mouse functions |
| 7840 | //----------------------------------------------------------------------------- |
| 7841 | // name: ck_get_mouse_xy_normalize() | 1.5.4.2 (ge & spencer) added |
| 7842 | // desc: get normalize [0,1] mouse cursor position |
| 7843 | //----------------------------------------------------------------------------- |
| 7844 | t_CKVEC2 ck_get_mouse_xy_normalize() |
| 7845 | { |
| 7846 | t_CKVEC2 retval; retval.x = retval.y = 0; |
| 7847 | |
| 7848 | #ifdef __PLATFORM_APPLE__ |
| 7849 | #if defined(__PLATFORM_MACOS__) // macOS |
| 7850 | // get screen coords |
| 7851 | CGEventRef cg_event = CGEventCreate(NULL); |
| 7852 | CGPoint mouseLocation = CGEventGetLocation(cg_event); |
| 7853 | |
| 7854 | t_CKFLOAT cursorX = (t_CKFLOAT)mouseLocation.x; |
| 7855 | t_CKFLOAT cursorY = (t_CKFLOAT)mouseLocation.y; |
| 7856 | |
| 7857 | CGDirectDisplayID display; |
| 7858 | CGDisplayCount displayCount; |
| 7859 | |
| 7860 | if( CGGetDisplaysWithPoint( mouseLocation, 1, &display, &displayCount ) == kCGErrorSuccess ) |
| 7861 | { |
| 7862 | CGRect bounds = CGDisplayBounds( display ); |
| 7863 | retval.x = ( (t_CKFLOAT)(mouseLocation.x-bounds.origin.x) ) / bounds.size.width; |
| 7864 | retval.y = ( (t_CKFLOAT)(mouseLocation.y-bounds.origin.y) ) / bounds.size.height; |
| 7865 | } |
| 7866 | |
| 7867 | // reclaim |
| 7868 | CFRelease( cg_event ); |
| 7869 | #elif defined(__PLATFORM_IOS__) // iOS |
| 7870 | // TODO: figure out how / whether to support iOS |
| 7871 | // for now, return @(0,0) |
| 7872 | #endif |
| 7873 | #elif defined(__PLATFORM_WINDOWS__) |
| 7874 | |
| 7875 | t_CKINT x = 0, y = 0; |
| 7876 | t_CKFLOAT logicalWidth = 0, logicalHeight = 0; |
| 7877 | POINT pt; |
| 7878 | MONITORINFOEX info; info.cbSize = sizeof( MONITORINFOEX ); |
| 7879 | DEVMODE devmode; devmode.dmSize = sizeof( DEVMODE ); |
| 7880 | HMONITOR monitor = NULL; |
| 7881 | |
| 7882 | // get cursor position; in a mutliple monitor setup, this could return negative values |
| 7883 | // for monitors to the left or above the primary monitor |
| 7884 | if( !GetCursorPos( &pt ) ) goto done; |
| 7885 | // get the monitor that contains the point |
| 7886 | monitor = MonitorFromPoint( pt, MONITOR_DEFAULTTONEAREST ); if( !monitor ) goto done; |
| 7887 | // get monitor info |
| 7888 | if( !GetMonitorInfo( monitor, &info ) ) goto done; |
| 7889 | // get logical width and height, this takes DPI scaling into account |
| 7890 | // e.g., in Settings->Display->Make everything bigger (Windows 10, 11) |
| 7891 | logicalWidth = (t_CKFLOAT)info.rcMonitor.right - info.rcMonitor.left; |
| 7892 | logicalHeight = (t_CKFLOAT)info.rcMonitor.bottom - info.rcMonitor.top; |
| 7893 | // map to monitor coordinate |
| 7894 | x = (t_CKINT)pt.x - info.rcMonitor.left; |
| 7895 | y = (t_CKINT)pt.y - info.rcMonitor.top; |
| 7896 | // normalize |
| 7897 | retval.x = x / logicalWidth; |
| 7898 | retval.y = y / logicalHeight; |
| 7899 | |
| 7900 | // cerr << pt.x << " " << pt.y << " => " << x << " " << y << " " << " offset: " |
| 7901 | // << info.rcMonitor.left << " " << info.rcMonitor.top |
| 7902 | // << " dim: " << logicalWidth << " " << logicalHeight |
| 7903 | // << " return: " << retval.x << " " << retval.y << endl; |
| 7904 | |
| 7905 | // t_CKFLOAT screenWidth = GetSystemMetrics( SM_CXVIRTUALSCREEN ); |
| 7906 | // t_CKFLOAT screenHeight = GetSystemMetrics( SM_CYVIRTUALSCREEN ); |
| 7907 | #elif defined(__PLATFORM_LINUX__) |
| 7908 | |
| 7909 | #if defined(__USE_X11__) |
| 7910 | // stuff to query |
| 7911 | Window root; |
| 7912 | XWindowAttributes attributes; |
| 7913 | int qRootX, qRootY, qChildX, qChildY; |
| 7914 | Window qRoot, qChild; |
| 7915 | unsigned int qMask; |
| 7916 | // open connection to X server |
| 7917 | Display * display = XOpenDisplay( nullptr ); |
| 7918 | if( !display ) |
| 7919 | { |
| 7920 | // print error |
| 7921 | std::cerr << "Mouse cannot open display (X server)..." << std::endl; |
| 7922 | goto done; |
| 7923 | } |
| 7924 | // get root window |
| 7925 | root = DefaultRootWindow( display ); |
| 7926 | // get the point |
| 7927 | XQueryPointer( display, root, &qRoot, &qChild, &qRootX, &qRootY, &qChildX, &qChildY, &qMask ); |
| 7928 | // get window attributes |
| 7929 | XGetWindowAttributes( display, root, &attributes ); |
| 7930 | // copy to return value |
| 7931 | retval.x = (t_CKFLOAT)qRootX / attributes.width; |
| 7932 | retval.y = (t_CKFLOAT)qRootY / attributes.height; |
| 7933 | // close the connection |
| 7934 | XCloseDisplay(display); |
| 7935 | #else // no __USE_X11__ |
| 7936 | cerr << "Mouse needs X11 on Linux; to use, re-compile chuck with USE_X11=1" << endl; |
| 7937 | #endif // if defined(__USE_X11__) |
| 7938 | |
| 7939 | #else |
| 7940 | // unsupported, for now; return 0,0 |
| 7941 | #endif |
| 7942 | |
| 7943 | done: |
| 7944 | // done |
| 7945 | return retval; |
| 7946 | } |
| 7947 | //----------------------------------------------------------------------------- |
| 7948 | // name: ck_get_mouse_xy_absolute() | 1.5.4.2 (ge & spencer) added |
| 7949 | // desc: get absolute mouse cursor position, dependent on screen resolution |
| 7950 | //----------------------------------------------------------------------------- |
| 7951 | t_CKVEC2 ck_get_mouse_xy_absolute() |
| 7952 | { |
| 7953 | t_CKVEC2 retval; retval.x = retval.y = 0; |
| 7954 | #ifdef __PLATFORM_APPLE__ |
| 7955 | #if defined(__PLATFORM_MACOS__) // macOS |
| 7956 | // get screen coords |
| 7957 | CGEventRef cg_event = CGEventCreate(NULL); |
| 7958 | CGPoint mouseLocation = CGEventGetLocation(cg_event); |
| 7959 | // return value |
| 7960 | retval.x = (t_CKFLOAT)mouseLocation.x; |
| 7961 | retval.y = (t_CKFLOAT)mouseLocation.y; |
| 7962 | // reclaim |
| 7963 | CFRelease( cg_event ); |
| 7964 | #elif defined(__PLATFORM_IOS__) // iOS |
| 7965 | // TODO: figure out how / whether to support iOS |
| 7966 | // for now, return @(0,0) |
| 7967 | #endif |
| 7968 | #elif defined(__PLATFORM_WINDOWS__) |
| 7969 | // get screen coords |
| 7970 | POINT pt; |
| 7971 | if( GetCursorPos( &pt ) ) |
| 7972 | { |
| 7973 | retval.x = pt.x; |
| 7974 | retval.y = pt.y; |
| 7975 | } |
| 7976 | #elif defined(__PLATFORM_LINUX__) |
| 7977 | |
| 7978 | #if defined(__USE_X11__) |
| 7979 | // stuff to query |
| 7980 | Window root; |
| 7981 | int qRootX, qRootY, qChildX, qChildY; |
| 7982 | Window qRoot, qChild; |
| 7983 | unsigned int qMask; |
| 7984 | // open connection to X server |
| 7985 | Display * display = XOpenDisplay( nullptr ); |
| 7986 | if( display ) |
| 7987 | { |
| 7988 | // get root window |
| 7989 | root = DefaultRootWindow( display ); |
| 7990 | // get the point |
| 7991 | XQueryPointer( display, root, &qRoot, &qChild, &qRootX, &qRootY, &qChildX, &qChildY, &qMask ); |
| 7992 | // copy to return value |
| 7993 | retval.x = qRootX; |
| 7994 | retval.y = qRootY; |
| 7995 | // close the connection |
| 7996 | XCloseDisplay( display ); |
| 7997 | } |
| 7998 | else |
| 7999 | { |
| 8000 | // print error |
| 8001 | std::cerr << "Mouse cannot open display (X server)..." << std::endl; |
| 8002 | } |
| 8003 | #else // no __USE_X11__ |
| 8004 | cerr << "Mouse needs X11 on Linux; to use, re-compile chuck with USE_X11=1" << endl; |
| 8005 | #endif // if defined(__USE_X11__) |
| 8006 | |
| 8007 | #else |
| 8008 | // unsupported platform; nothing to do for now; return 0,0 |
| 8009 | #endif // platform select |
| 8010 | |
| 8011 | return retval; |
| 8012 | } |
| 8013 | |
| 8014 | |
| 8015 | |
| 8016 | |
| 8017 | //----------------------------------------------------------------------------- |
| 8018 | #pragma mark Hid graveyard |
| 8019 | //----------------------------------------------------------------------------- |
| 8020 | /***** empty functions for stuff that isn't yet cross platform *****/ |
| 8021 | #ifndef __PLATFORM_APPLE__ |
| 8022 | /*** empty functions for Mac-only stuff ***/ |
| 8023 | |
| 8024 | int Joystick_count_elements( int js, int type ) { return -1; } |
| 8025 | |
| 8026 | int Mouse_count_elements( int js, int type ) { return -1; } |
| 8027 | int Mouse_start_cursor_track(){ return -1; } |
| 8028 | int Mouse_stop_cursor_track(){ return -1; } |
| 8029 | |
| 8030 | int Keyboard_count_elements( int js, int type ) { return -1; } |
| 8031 | int Keyboard_send( int k, const HidMsg * msg ){ return -1; } |
| 8032 | |
| 8033 | int WiiRemote_send( int k, const HidMsg * msg ){ return -1; } |
| 8034 | |
| 8035 | void WiiRemote_init(){} |
| 8036 | void WiiRemote_poll(){} |
| 8037 | void WiiRemote_quit(){} |
| 8038 | void WiiRemote_probe(){} |
| 8039 | int WiiRemote_count(){ return 0; } |
| 8040 | int WiiRemote_open( int wr ){ return -1; } |
| 8041 | int WiiRemote_open( const char * name ){ return -1; } |
| 8042 | int WiiRemote_close( int wr ){ return -1; } |
| 8043 | int WiiRemote_send( const HidMsg * msg ){ return -1; } |
| 8044 | const char * WiiRemote_name( int wr ){ return NULL; } |
| 8045 | |
| 8046 | void TiltSensor_init(){} |
| 8047 | void TiltSensor_quit(){} |
| 8048 | void TiltSensor_probe(){} |
| 8049 | int TiltSensor_count(){ return 0; } |
| 8050 | int TiltSensor_open( int ts ){ return -1; } |
| 8051 | int TiltSensor_close( int ts ){ return -1; } |
| 8052 | int TiltSensor_read( int ts, int type, int num, HidMsg * msg ){ return -1; } |
| 8053 | const char * TiltSensor_name( int ts ){ return NULL; } |
| 8054 | |
| 8055 | |
| 8056 | void MultiTouchDevice_init() { } |
| 8057 | void MultiTouchDevice_quit() { } |
| 8058 | void MultiTouchDevice_probe() { } |
| 8059 | int MultiTouchDevice_count() { return 0; } |
| 8060 | int MultiTouchDevice_open( int ts ) { return -1; } |
| 8061 | int MultiTouchDevice_close( int ts ) { return -1; } |
| 8062 | const char * MultiTouchDevice_name( int ts ) { return NULL; } |
| 8063 | |
| 8064 | extern void Tablet_init() { } |
| 8065 | extern void Tablet_quit() { } |
| 8066 | extern void Tablet_probe() { } |
| 8067 | extern int Tablet_count() { return 0; } |
| 8068 | extern int Tablet_open( int ts ) { return -1; } |
| 8069 | extern int Tablet_close( int ts ) { return -1; } |
| 8070 | extern const char * Tablet_name( int ts ) { return NULL; } |
| 8071 | |
| 8072 | #endif |
| 8073 | |
| 8074 | |
| 8075 | #ifdef __CHIP_MODE__ |
| 8076 | |
| 8077 | // #include "util_iphone.h" |
| 8078 | |
| 8079 | int get_tilt_sensor_x() { return 0; } |
| 8080 | int get_tilt_sensor_y() { return 0; } |
| 8081 | int get_tilt_sensor_z() { return 0; } |
| 8082 | |
| 8083 | void start_hid_multi_touch() { } |
| 8084 | void stop_hid_multi_touch() { } |
| 8085 | |
| 8086 | void Hid_init(){} |
| 8087 | void Hid_poll(){} |
| 8088 | void Hid_quit(){} |
| 8089 | |
| 8090 | void Mouse_init(){} |
| 8091 | void Mouse_poll(){} |
| 8092 | void Mouse_quit(){} |
| 8093 | void Mouse_probe(){} |
| 8094 | |
| 8095 | int Mouse_count() |
| 8096 | { |
| 8097 | return 1; |
| 8098 | } |
| 8099 | |
| 8100 | int Mouse_count_elements( int js, int type ) |
| 8101 | { |
| 8102 | return -1; |
| 8103 | } |
| 8104 | |
| 8105 | int Mouse_open( int m ) |
| 8106 | { |
| 8107 | if(m >= 0 && m < 1) |
| 8108 | { |
| 8109 | start_hid_multi_touch(); |
| 8110 | return 0; |
| 8111 | } |
| 8112 | else |
| 8113 | return -1; |
| 8114 | } |
| 8115 | |
| 8116 | int Mouse_open( const char * name ) |
| 8117 | { |
| 8118 | return -1; |
| 8119 | } |
| 8120 | |
| 8121 | int Mouse_close( int m ) |
| 8122 | { |
| 8123 | if(m >= 0 && m < 1) |
| 8124 | { |
| 8125 | stop_hid_multi_touch(); |
| 8126 | return 0; |
| 8127 | } |
| 8128 | else |
| 8129 | return -1; |
| 8130 | } |
| 8131 | |
| 8132 | int Mouse_send( int m, const HidMsg * msg ) |
| 8133 | { |
| 8134 | return -1; |
| 8135 | } |
| 8136 | |
| 8137 | const char * Mouse_name( int m ) |
| 8138 | { |
| 8139 | if(m == 0) |
| 8140 | return "iPhone Multitouch"; |
| 8141 | else |
| 8142 | return NULL; |
| 8143 | } |
| 8144 | |
| 8145 | int Mouse_buttons( int m ) |
| 8146 | { |
| 8147 | if(m == 0) |
| 8148 | return 2; |
| 8149 | else |
| 8150 | return -1; |
| 8151 | } |
| 8152 | |
| 8153 | int Mouse_start_cursor_track() |
| 8154 | { |
| 8155 | return -1; |
| 8156 | } |
| 8157 | |
| 8158 | int Mouse_stop_cursor_track() |
| 8159 | { |
| 8160 | return -1; |
| 8161 | } |
| 8162 | |
| 8163 | void TiltSensor_init(){} |
| 8164 | void TiltSensor_quit(){} |
| 8165 | void TiltSensor_probe(){} |
| 8166 | |
| 8167 | int TiltSensor_count() |
| 8168 | { |
| 8169 | return 1; |
| 8170 | } |
| 8171 | |
| 8172 | int TiltSensor_open( int ts ) |
| 8173 | { |
| 8174 | if(ts == 0) |
| 8175 | return 0; |
| 8176 | return -1; |
| 8177 | } |
| 8178 | |
| 8179 | int TiltSensor_close( int ts ) |
| 8180 | { |
| 8181 | if(ts == 0) |
| 8182 | return 0; |
| 8183 | return -1; |
| 8184 | } |
| 8185 | |
| 8186 | int TiltSensor_read( int ts, int type, int num, HidMsg * msg ) |
| 8187 | { |
| 8188 | if( type != CK_HID_ACCELEROMETER ) |
| 8189 | return -1; |
| 8190 | |
| 8191 | msg->idata[0] = get_tilt_sensor_x(); |
| 8192 | msg->idata[1] = get_tilt_sensor_y(); |
| 8193 | msg->idata[2] = get_tilt_sensor_z(); |
| 8194 | |
| 8195 | return 0; |
| 8196 | } |
| 8197 | |
| 8198 | const char * TiltSensor_name( int ts ) |
| 8199 | { |
| 8200 | if(ts == 0) |
| 8201 | return "iPhone Accelerometer"; |
| 8202 | else |
| 8203 | return NULL; |
| 8204 | } |
| 8205 | |
| 8206 | // ge: SMS multi-thread poll rate |
| 8207 | t_CKINT TiltSensor_setPollRate( t_CKINT usec ) |
| 8208 | { |
| 8209 | return -1; |
| 8210 | } |
| 8211 | |
| 8212 | t_CKINT TiltSensor_getPollRate( ) |
| 8213 | { |
| 8214 | return -1; |
| 8215 | } |
| 8216 | |
| 8217 | void Joystick_init(){} |
| 8218 | |
| 8219 | void Joystick_poll(){} |
| 8220 | |
| 8221 | void Joystick_quit(){} |
| 8222 | |
| 8223 | void Joystick_probe(){} |
| 8224 | |
| 8225 | int Joystick_count() |
| 8226 | { |
| 8227 | return 0; |
| 8228 | } |
| 8229 | |
| 8230 | int Joystick_count_elements( int js, int type ) |
| 8231 | { |
| 8232 | return -1; |
| 8233 | } |
| 8234 | |
| 8235 | int Joystick_open( int js ) |
| 8236 | { |
| 8237 | return -1; |
| 8238 | } |
| 8239 | |
| 8240 | int Joystick_open_async( int js ) |
| 8241 | { |
| 8242 | return -1; |
| 8243 | } |
| 8244 | |
| 8245 | int Joystick_open( const char * name ) |
| 8246 | { |
| 8247 | return -1; |
| 8248 | } |
| 8249 | |
| 8250 | int Joystick_close( int js ) |
| 8251 | { |
| 8252 | return -1; |
| 8253 | } |
| 8254 | |
| 8255 | int Joystick_send( int js, const HidMsg * msg ) |
| 8256 | { |
| 8257 | return -1; |
| 8258 | } |
| 8259 | |
| 8260 | |
| 8261 | const char * Joystick_name( int js ) |
| 8262 | { |
| 8263 | return NULL; |
| 8264 | } |
| 8265 | |
| 8266 | int Joystick_axes( int js ) |
| 8267 | { |
| 8268 | return -1; |
| 8269 | } |
| 8270 | |
| 8271 | int Joystick_buttons( int js ) |
| 8272 | { |
| 8273 | return -1; |
| 8274 | } |
| 8275 | |
| 8276 | int Joystick_hats( int js ) |
| 8277 | { |
| 8278 | return -1; |
| 8279 | } |
| 8280 | |
| 8281 | void Keyboard_init(){} |
| 8282 | void Keyboard_poll(){} |
| 8283 | void Keyboard_quit(){} |
| 8284 | void Keyboard_probe(){} |
| 8285 | |
| 8286 | int Keyboard_count() |
| 8287 | { |
| 8288 | return 0; |
| 8289 | } |
| 8290 | |
| 8291 | int Keyboard_count_elements( int js, int type ) |
| 8292 | { |
| 8293 | return -1; |
| 8294 | } |
| 8295 | |
| 8296 | int Keyboard_open( int kb ) |
| 8297 | { |
| 8298 | return -1; |
| 8299 | } |
| 8300 | |
| 8301 | int Keyboard_open( const char * name ) |
| 8302 | { |
| 8303 | return -1; |
| 8304 | } |
| 8305 | |
| 8306 | int Keyboard_close( int kb ) |
| 8307 | { |
| 8308 | return -1; |
| 8309 | } |
| 8310 | |
| 8311 | int Keyboard_send( int kb, const HidMsg * msg ) |
| 8312 | { |
| 8313 | return -1; |
| 8314 | } |
| 8315 | |
| 8316 | const char * Keyboard_name( int kb ) |
| 8317 | { |
| 8318 | return NULL; |
| 8319 | } |
| 8320 | |
| 8321 | void WiiRemote_init(){} |
| 8322 | void WiiRemote_poll(){} |
| 8323 | void WiiRemote_quit(){} |
| 8324 | void WiiRemote_probe(){} |
| 8325 | |
| 8326 | int WiiRemote_count() |
| 8327 | { |
| 8328 | return 0; |
| 8329 | } |
| 8330 | |
| 8331 | int WiiRemote_open( int wr ) |
| 8332 | { |
| 8333 | return -1; |
| 8334 | } |
| 8335 | |
| 8336 | int WiiRemote_open( const char * name ) |
| 8337 | { |
| 8338 | return -1; |
| 8339 | } |
| 8340 | |
| 8341 | int WiiRemote_close( int wr ) |
| 8342 | { |
| 8343 | return -1; |
| 8344 | } |
| 8345 | |
| 8346 | int WiiRemote_send( int wr, const HidMsg * msg ) |
| 8347 | { |
| 8348 | return -1; |
| 8349 | } |
| 8350 | |
| 8351 | const char * WiiRemote_name( int wr ) |
| 8352 | { |
| 8353 | return NULL; |
| 8354 | } |
| 8355 | |
| 8356 | void MultiTouchDevice_init() { } |
| 8357 | void MultiTouchDevice_quit() { } |
| 8358 | void MultiTouchDevice_probe() { } |
| 8359 | int MultiTouchDevice_count() { return 0; } |
| 8360 | int MultiTouchDevice_open( int ts ) { return -1; } |
| 8361 | int MultiTouchDevice_close( int ts ) { return -1; } |
| 8362 | const char * MultiTouchDevice_name( int ts ) { return NULL; } |
| 8363 | |
| 8364 | |
| 8365 | #endif // __CHIP_MODE__ |
| 8366 |