PolyCocoaCore.mm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyCocoaCore.h"
  20. #include <iostream>
  21. #include <limits.h>
  22. #include <ApplicationServices/ApplicationServices.h>
  23. using namespace Polycode;
  24. long getThreadID() {
  25. return (long)pthread_self();
  26. }
  27. void Core::getScreenInfo(int *width, int *height, int *hz) {
  28. CGDisplayModeRef mode = CGDisplayCopyDisplayMode(CGMainDisplayID());
  29. // Copy the relevant data.
  30. if (width) *width = CGDisplayModeGetWidth(mode);
  31. if (height) *height = CGDisplayModeGetHeight(mode);
  32. if (hz) *hz = CGDisplayModeGetRefreshRate(mode);
  33. CGDisplayModeRelease(mode);
  34. }
  35. CocoaCore::CocoaCore(PolycodeView *view, int _xRes, int _yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate, int monitorIndex) : Core(_xRes, _yRes, fullScreen, vSync, aaLevel, anisotropyLevel, frameRate, monitorIndex) {
  36. hidManager = NULL;
  37. initGamepad();
  38. eventMutex = createMutex();
  39. // NSLog(@"BUNDLE: %@", [[NSBundle mainBundle] bundlePath]);
  40. chdir([[[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/Contents/Resources"] UTF8String]);
  41. defaultWorkingDirectory = String([[[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/Contents/Resources"] UTF8String]);
  42. userHomeDirectory = String([NSHomeDirectory() UTF8String]);
  43. NSOpenGLPixelFormatAttribute attrs[32];
  44. int atindx = 0;
  45. attrs[atindx++] = NSOpenGLPFADoubleBuffer;
  46. attrs[atindx++] = NSOpenGLPFADepthSize;
  47. attrs[atindx++] = 32;
  48. if(aaLevel > 0) {
  49. attrs[atindx++] = NSOpenGLPFASampleBuffers;
  50. attrs[atindx++] = 1;
  51. attrs[atindx++] = NSOpenGLPFASamples;
  52. attrs[atindx++] = aaLevel;
  53. attrs[atindx++] = NSOpenGLPFAMultisample;
  54. }
  55. attrs[atindx++] = NSOpenGLPFANoRecovery;
  56. if(fullScreen) {
  57. // attrs[atindx++] = NSOpenGLPFAFullScreen;
  58. // attrs[atindx++] = NSOpenGLPFAScreenMask;
  59. // attrs[atindx++] = CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay);
  60. }
  61. attrs[atindx++] = NSOpenGLPFAAccelerated;
  62. attrs[atindx++] = nil;
  63. /*
  64. NSOpenGLPixelFormatAttribute attrs[] =
  65. {
  66. NSOpenGLPFADoubleBuffer,
  67. NSOpenGLPFADepthSize, 16,
  68. // NSOpenGLPFAFullScreen,
  69. // NSOpenGLPFAScreenMask,
  70. // CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay),
  71. NSOpenGLPFASampleBuffers, 1,
  72. NSOpenGLPFASamples, aaLevel,
  73. NSOpenGLPFANoRecovery,
  74. // NSOpenGLPFAWindow,
  75. NSOpenGLPFAMultisample,
  76. // NSOpenGLPFAAccelerated,
  77. // NSOpenGLPFAAccumSize, 0,
  78. nil
  79. };
  80. */
  81. // [view lockContext];
  82. [view setCore:this];
  83. NSOpenGLPixelFormat *format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
  84. if(!format) {
  85. NSLog(@"Error creating pixel format!\n");
  86. }
  87. context = [[NSOpenGLContext alloc] initWithFormat: format shareContext:nil];
  88. [format release];
  89. if (context == nil) {
  90. NSLog(@"Failed to create open gl context");
  91. }
  92. if(false) {
  93. // [view enterFullScreenMode:[NSScreen mainScreen] withOptions:0];
  94. // [view removeFromSuperview];
  95. // [[view window] orderOut:nil];
  96. // CGDisplayCapture (kCGDirectMainDisplay ) ;
  97. // CGDisplaySwitchToMode (kCGDirectMainDisplay, CGDisplayBestModeForParameters (kCGDirectMainDisplay, 32, xRes, yRes, NULL) );
  98. // [context makeCurrentContext];
  99. // [context setFullScreen];
  100. // [context flushBuffer];
  101. // CGDisplayCapture (kCGDirectMainDisplay ) ;
  102. // [context clearDrawable];
  103. // [context release];
  104. } else {
  105. [view clearGLContext];
  106. [view setOpenGLContext:context];
  107. [context setView: (NSView*)view];
  108. }
  109. if(fullScreen) {
  110. // [view enterFullScreenMode:[NSScreen mainScreen] withOptions:0];
  111. }
  112. glView = view;
  113. initTime = mach_absolute_time();
  114. // while(![view isContextReady]) {
  115. // }
  116. renderer = new OpenGLRenderer();
  117. services->setRenderer(renderer);
  118. //[view unlockContext];
  119. setVideoMode(xRes,yRes,fullScreen, vSync, aaLevel, anisotropyLevel);
  120. CoreServices::getInstance()->installModule(new GLSLShaderModule());
  121. }
  122. void CocoaCore::copyStringToClipboard(const String& str) {
  123. NSPasteboard *pb = [NSPasteboard generalPasteboard];
  124. NSArray *types = [NSArray arrayWithObjects:NSStringPboardType, nil];
  125. [pb declareTypes:types owner:nil];
  126. NSString *nsstr = [NSString stringWithCString: str.c_str()];
  127. /*
  128. char* data = (char*)str.c_str();
  129. unsigned size = str.size() * sizeof(char);
  130. NSString* nsstr = [[[NSString alloc] initWithBytes:data length:size encoding:NSUTF32LittleEndianStringEncoding] autorelease];
  131. */
  132. [pb setString: nsstr forType:NSStringPboardType];
  133. }
  134. String CocoaCore::getClipboardString() {
  135. NSPasteboard *pb = [NSPasteboard generalPasteboard];
  136. NSString* retString = [pb stringForType:NSStringPboardType];
  137. return [retString UTF8String];
  138. }
  139. void CocoaCore::setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, int aaLeve, int anisotropyLevel) {
  140. this->xRes = xRes;
  141. this->yRes = yRes;
  142. this->fullScreen = fullScreen;
  143. this->aaLevel = aaLevel;
  144. renderer->setAnisotropyAmount(anisotropyLevel);
  145. renderer->Resize(xRes, yRes);
  146. // CoreServices::getInstance()->getMaterialManager()->reloadProgramsAndTextures();
  147. dispatchEvent(new Event(), EVENT_CORE_RESIZE);
  148. // NSRect visibleFrame = [[NSScreen mainScreen] visibleFrame];
  149. // NSRect frame = NSMakeRect([[glView window] frame].origin.x, [[glView window] frame].origin.y, xRes, yRes);
  150. // frame.origin.x = (visibleFrame.size.width - frame.size.width) * 0.5;
  151. // frame.origin.y = (visibleFrame.size.height - frame.size.height) * (9.0/10.0);
  152. // [[glView window] setFrame: frame display: YES animate: NO];
  153. // if(!fullScreen) {
  154. [[glView window] setContentSize: NSMakeSize(xRes, yRes)];
  155. // } else {
  156. // CGDisplaySwitchToMode (kCGDirectMainDisplay, CGDisplayBestModeForParameters (kCGDirectMainDisplay, 32, xRes, yRes, NULL) );
  157. // }
  158. if(fullScreen) {
  159. CGDisplaySwitchToMode (kCGDirectMainDisplay, CGDisplayBestModeForParameters (kCGDirectMainDisplay, 32, xRes, yRes, NULL) );
  160. if(monitorIndex > -1) {
  161. if(monitorIndex > [[NSScreen screens] count]-1) {
  162. Logger::log("Requested monitor index above available screens.\n");
  163. monitorIndex = -1;
  164. }
  165. }
  166. if(monitorIndex == -1) {
  167. [glView enterFullScreenMode:[NSScreen mainScreen] withOptions:[NSDictionary dictionaryWithObjectsAndKeys: nil]];
  168. } else {
  169. [glView enterFullScreenMode:[[NSScreen screens] objectAtIndex:1] withOptions:[NSDictionary dictionaryWithObjectsAndKeys: nil]];
  170. }
  171. }
  172. GLint sync = 0;
  173. if(vSync) {
  174. sync =1 ;
  175. }
  176. [context setValues:&sync forParameter:NSOpenGLCPSwapInterval];
  177. /*
  178. if(aaLevel > 0) {
  179. glEnable( GL_MULTISAMPLE_ARB );
  180. } else {
  181. glDisable( GL_MULTISAMPLE_ARB );
  182. }
  183. */
  184. }
  185. void CocoaCore::openFileWithApplication(String file, String application) {
  186. NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
  187. NSString *filePath = [NSString stringWithCString:file.c_str()];
  188. NSString *appString = [NSString stringWithCString:application.c_str()];
  189. [workspace openFile: filePath withApplication: appString andDeactivate: YES];
  190. }
  191. void CocoaCore::launchApplicationWithFile(String application, String file) {
  192. NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
  193. NSURL *url = [NSURL fileURLWithPath: [NSString stringWithCString:application.c_str()]];
  194. NSError *error = nil;
  195. NSArray *arguments = [NSArray arrayWithObjects: [NSString stringWithCString:file.c_str()], nil];
  196. [workspace launchApplicationAtURL:url options:0 configuration:[NSDictionary dictionaryWithObject:arguments forKey:NSWorkspaceLaunchConfigurationArguments] error:&error];
  197. //Handle error
  198. }
  199. String CocoaCore::executeExternalCommand(String command) {
  200. FILE *fp = popen(command.c_str(), "r");
  201. if(!fp) {
  202. return "Unable to execute command";
  203. }
  204. int fd = fileno(fp);
  205. char path[1024];
  206. String retString;
  207. while (fgets(path, sizeof(path), fp) != NULL) {
  208. retString = retString + String(path);
  209. }
  210. fclose(fp);
  211. pclose(fp);
  212. return retString;
  213. }
  214. void CocoaCore::resizeTo(int xRes, int yRes) {
  215. this->xRes = xRes;
  216. this->yRes = yRes;
  217. renderer->Resize(xRes, yRes);
  218. dispatchEvent(new Event(), EVENT_CORE_RESIZE);
  219. }
  220. vector<Polycode::Rectangle> CocoaCore::getVideoModes() {
  221. vector<Polycode::Rectangle> retVector;
  222. return retVector;
  223. }
  224. CocoaCore::~CocoaCore() {
  225. printf("Shutting down cocoa core\n");
  226. [glView setCore:nil];
  227. shutdownGamepad();
  228. if(fullScreen) {
  229. [glView exitFullScreenModeWithOptions:nil];
  230. }
  231. [glView clearGLContext];
  232. [context release];
  233. }
  234. void *ManagedThreadFunc(void *data) {
  235. Threaded *target = static_cast<Threaded*>(data);
  236. target->runThread();
  237. target->scheduledForRemoval = true;
  238. return NULL;
  239. }
  240. void CocoaCore::createThread(Threaded *target) {
  241. Core::createThread(target);
  242. pthread_t thread;
  243. pthread_create( &thread, NULL, ManagedThreadFunc, (void*)target);
  244. }
  245. void CocoaCore::lockMutex(CoreMutex *mutex) {
  246. PosixMutex *m = (PosixMutex*) mutex;
  247. pthread_mutex_lock(&m->pMutex);
  248. }
  249. void CocoaCore::unlockMutex(CoreMutex *mutex) {
  250. PosixMutex *m = (PosixMutex*) mutex;
  251. pthread_mutex_unlock(&m->pMutex);
  252. }
  253. CoreMutex *CocoaCore::createMutex() {
  254. PosixMutex *mutex = new PosixMutex();
  255. pthread_mutex_init(&mutex->pMutex, NULL);
  256. return mutex;
  257. }
  258. unsigned int CocoaCore::getTicks() {
  259. uint64_t time = mach_absolute_time();
  260. double conversion = 0.0;
  261. mach_timebase_info_data_t info;
  262. mach_timebase_info( &info );
  263. conversion = 1e-9 * (double) info.numer / (double) info.denom;
  264. return (((double)(time - initTime)) * conversion) * 1000.0f;
  265. }
  266. void CocoaCore::enableMouse(bool newval) {
  267. if(newval)
  268. CGDisplayShowCursor(kCGDirectMainDisplay);
  269. else
  270. CGDisplayHideCursor(kCGDirectMainDisplay);
  271. Core::enableMouse(newval);
  272. }
  273. void CocoaCore::setCursor(int cursorType) {
  274. NSCursor *newCursor;
  275. switch(cursorType) {
  276. case CURSOR_TEXT:
  277. newCursor = [NSCursor IBeamCursor];
  278. break;
  279. case CURSOR_POINTER:
  280. newCursor = [NSCursor pointingHandCursor];
  281. break;
  282. case CURSOR_CROSSHAIR:
  283. newCursor = [NSCursor crosshairCursor];
  284. break;
  285. case CURSOR_RESIZE_LEFT_RIGHT:
  286. newCursor = [NSCursor resizeLeftRightCursor];
  287. break;
  288. case CURSOR_RESIZE_UP_DOWN:
  289. newCursor = [NSCursor resizeUpDownCursor];
  290. break;
  291. case CURSOR_OPEN_HAND:
  292. newCursor = [NSCursor openHandCursor];
  293. break;
  294. default:
  295. newCursor = [NSCursor arrowCursor];
  296. break;
  297. }
  298. [glView setCurrentCursor:newCursor];
  299. [glView resetCursorRects];
  300. [[glView window] invalidateCursorRectsForView: (NSView*)glView];
  301. }
  302. void CocoaCore::warpCursor(int x, int y) {
  303. CGSetLocalEventsSuppressionInterval(0);
  304. NSArray *theScreens = [NSScreen screens];
  305. for (NSScreen *theScreen in theScreens) {
  306. CGPoint CenterOfWindow = CGPointMake([glView window].frame.origin.x+x, (-1)*([glView window].frame.origin.y-theScreen.frame.size.height)-yRes+y);
  307. CGDisplayMoveCursorToPoint (kCGDirectMainDisplay, CenterOfWindow);
  308. break;
  309. }
  310. lastMouseX = x;
  311. lastMouseY = y;
  312. }
  313. void CocoaCore::checkEvents() {
  314. lockMutex(eventMutex);
  315. CocoaEvent event;
  316. for(int i=0; i < cocoaEvents.size(); i++) {
  317. event = cocoaEvents[i];
  318. switch(event.eventGroup) {
  319. case CocoaEvent::INPUT_EVENT:
  320. switch(event.eventCode) {
  321. case InputEvent::EVENT_MOUSEMOVE:
  322. input->setDeltaPosition(lastMouseX - event.mouseX, lastMouseY - event.mouseY);
  323. lastMouseX = event.mouseX;
  324. lastMouseY = event.mouseY;
  325. input->setMousePosition(event.mouseX, event.mouseY, getTicks());
  326. break;
  327. case InputEvent::EVENT_MOUSEDOWN:
  328. input->setMouseButtonState(event.mouseButton, true, getTicks());
  329. break;
  330. case InputEvent::EVENT_MOUSEWHEEL_UP:
  331. input->mouseWheelUp(getTicks());
  332. break;
  333. case InputEvent::EVENT_MOUSEWHEEL_DOWN:
  334. input->mouseWheelDown(getTicks());
  335. break;
  336. case InputEvent::EVENT_MOUSEUP:
  337. input->setMouseButtonState(event.mouseButton, false, getTicks());
  338. break;
  339. case InputEvent::EVENT_KEYDOWN:
  340. input->setKeyState(event.keyCode, event.unicodeChar, true, getTicks());
  341. break;
  342. case InputEvent::EVENT_KEYUP:
  343. input->setKeyState(event.keyCode, event.unicodeChar, false, getTicks());
  344. break;
  345. }
  346. break;
  347. case CocoaEvent::FOCUS_EVENT:
  348. switch(event.eventCode) {
  349. case Core::EVENT_LOST_FOCUS:
  350. loseFocus();
  351. break;
  352. case Core::EVENT_GAINED_FOCUS:
  353. gainFocus();
  354. break;
  355. }
  356. break;
  357. }
  358. }
  359. cocoaEvents.clear();
  360. unlockMutex(eventMutex);
  361. }
  362. void CocoaCore::openURL(String url) {
  363. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithUTF8String: url.c_str()]]];
  364. }
  365. void CocoaCore::createFolder(const String& folderPath) {
  366. [[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithUTF8String: folderPath.c_str()] withIntermediateDirectories:YES attributes:nil error:nil];
  367. }
  368. void CocoaCore::copyDiskItem(const String& itemPath, const String& destItemPath) {
  369. [[NSFileManager defaultManager] copyItemAtPath: [NSString stringWithUTF8String: itemPath.c_str()] toPath: [NSString stringWithUTF8String: destItemPath.c_str()] error: nil];
  370. }
  371. void CocoaCore::moveDiskItem(const String& itemPath, const String& destItemPath) {
  372. [[NSFileManager defaultManager] moveItemAtPath: [NSString stringWithUTF8String: itemPath.c_str()] toPath: [NSString stringWithUTF8String: destItemPath.c_str()] error: nil];
  373. }
  374. void CocoaCore::removeDiskItem(const String& itemPath) {
  375. [[NSFileManager defaultManager] removeItemAtPath: [NSString stringWithUTF8String: itemPath.c_str()] error:nil];
  376. }
  377. void CocoaCore::makeApplicationMain() {
  378. [NSApp activateIgnoringOtherApps:YES];
  379. }
  380. String CocoaCore::openFolderPicker() {
  381. unlockMutex(eventMutex);
  382. NSOpenPanel *attachmentPanel = [[NSOpenPanel openPanel] retain];
  383. [attachmentPanel setCanChooseFiles:NO];
  384. [attachmentPanel setCanCreateDirectories: YES];
  385. [attachmentPanel setCanChooseDirectories:YES];
  386. if ( [attachmentPanel runModal] == NSOKButton )
  387. {
  388. // files and directories selected.
  389. NSArray* files = [attachmentPanel filenames];
  390. NSString* fileName = [files objectAtIndex:0];
  391. [attachmentPanel release];
  392. return [fileName UTF8String];
  393. } else {
  394. [attachmentPanel release];
  395. return [@"" UTF8String];
  396. }
  397. }
  398. vector<String> CocoaCore::openFilePicker(vector<CoreFileExtension> extensions, bool allowMultiple) {
  399. unlockMutex(eventMutex);
  400. vector<String> retVector;
  401. NSOpenPanel *attachmentPanel = [NSOpenPanel openPanel];
  402. [attachmentPanel setCanChooseFiles:YES];
  403. [attachmentPanel setCanCreateDirectories: YES];
  404. [attachmentPanel setCanChooseDirectories:NO];
  405. [attachmentPanel setAllowsMultipleSelection: allowMultiple];
  406. NSMutableArray *types = nil;
  407. if(extensions.size() > 0) {
  408. types = [[NSMutableArray alloc] init];
  409. for(int i=0; i < extensions.size(); i++) {
  410. CoreFileExtension extInfo = extensions[i];
  411. [types addObject: [NSString stringWithUTF8String: extInfo.extension.c_str()]];
  412. }
  413. }
  414. if ( [attachmentPanel runModalForDirectory:nil file:nil types:types] == NSOKButton )
  415. {
  416. NSArray* files = [attachmentPanel filenames];
  417. if(files) {
  418. for (int i=0; i < [files count]; i++) {
  419. NSString* fileName = [files objectAtIndex:i];
  420. retVector.push_back([fileName UTF8String]);
  421. }
  422. }
  423. }
  424. return retVector;
  425. }
  426. bool CocoaCore::Update() {
  427. if(!running)
  428. return false;
  429. lockMutex(CoreServices::getRenderMutex());
  430. checkEvents();
  431. renderer->BeginRender();
  432. updateCore();
  433. renderer->EndRender();
  434. [context flushBuffer];
  435. unlockMutex(CoreServices::getRenderMutex());
  436. doSleep();
  437. return running;
  438. }
  439. static void hatValueToXY(CFIndex value, CFIndex range, int * outX, int * outY) {
  440. if (value == range) {
  441. *outX = *outY = 0;
  442. } else {
  443. if (value > 0 && value < range / 2) {
  444. *outX = 1;
  445. } else if (value > range / 2) {
  446. *outX = -1;
  447. } else {
  448. *outX = 0;
  449. }
  450. if (value > range / 4 * 3 || value < range / 4) {
  451. *outY = -1;
  452. } else if (value > range / 4 && value < range / 4 * 3) {
  453. *outY = 1;
  454. } else {
  455. *outY = 0;
  456. }
  457. }
  458. }
  459. static int IOHIDDeviceGetIntProperty(IOHIDDeviceRef deviceRef, CFStringRef key) {
  460. CFTypeRef typeRef;
  461. int value;
  462. typeRef = IOHIDDeviceGetProperty(deviceRef, key);
  463. if (typeRef == NULL || CFGetTypeID(typeRef) != CFNumberGetTypeID()) {
  464. return 0;
  465. }
  466. CFNumberGetValue((CFNumberRef) typeRef, kCFNumberSInt32Type, &value);
  467. return value;
  468. }
  469. static void onDeviceValueChanged(void * context, IOReturn result, void * sender, IOHIDValueRef value) {
  470. IOHIDElementRef element;
  471. IOHIDElementCookie cookie;
  472. unsigned int axisIndex, buttonIndex;
  473. static mach_timebase_info_data_t timebaseInfo;
  474. if (timebaseInfo.denom == 0) {
  475. mach_timebase_info(&timebaseInfo);
  476. }
  477. GamepadDeviceEntry *deviceRecord = (GamepadDeviceEntry*) context;
  478. CoreInput *input = deviceRecord->input;
  479. JoystickInfo *joystickInfo = input->getJoystickInfoByID(deviceRecord->deviceID);
  480. if(!joystickInfo)
  481. return;
  482. element = IOHIDValueGetElement(value);
  483. cookie = IOHIDElementGetCookie(element);
  484. for (axisIndex = 0; axisIndex < deviceRecord->numAxes; axisIndex++) {
  485. if (!deviceRecord->axisElements[axisIndex].isHatSwitchSecondAxis &&
  486. deviceRecord->axisElements[axisIndex].cookie == cookie) {
  487. CFIndex integerValue;
  488. if (IOHIDValueGetLength(value) > 4) {
  489. // Workaround for a strange crash that occurs with PS3 controller; was getting lengths of 39 (!)
  490. continue;
  491. }
  492. integerValue = IOHIDValueGetIntegerValue(value);
  493. if (deviceRecord->axisElements[axisIndex].isHatSwitch) {
  494. int x, y;
  495. // Fix for Saitek X52
  496. deviceRecord->axisElements[axisIndex].hasNullState = false;
  497. if (!deviceRecord->axisElements[axisIndex].hasNullState) {
  498. if (integerValue < deviceRecord->axisElements[axisIndex].logicalMin) {
  499. integerValue = deviceRecord->axisElements[axisIndex].logicalMax - deviceRecord->axisElements[axisIndex].logicalMin + 1;
  500. } else {
  501. integerValue--;
  502. }
  503. }
  504. hatValueToXY(integerValue, deviceRecord->axisElements[axisIndex].logicalMax - deviceRecord->axisElements[axisIndex].logicalMin + 1, &x, &y);
  505. if (x != joystickInfo->joystickAxisState[axisIndex]) {
  506. input->joystickAxisMoved(axisIndex, x, deviceRecord->deviceID);
  507. }
  508. if (y != joystickInfo->joystickAxisState[axisIndex + 1]) {
  509. input->joystickAxisMoved(axisIndex + 1, y, deviceRecord->deviceID);
  510. }
  511. } else {
  512. float floatValue;
  513. if (integerValue < deviceRecord->axisElements[axisIndex].logicalMin) {
  514. deviceRecord->axisElements[axisIndex].logicalMin = integerValue;
  515. }
  516. if (integerValue > deviceRecord->axisElements[axisIndex].logicalMax) {
  517. deviceRecord->axisElements[axisIndex].logicalMax = integerValue;
  518. }
  519. floatValue = (integerValue - deviceRecord->axisElements[axisIndex].logicalMin) / (float) (deviceRecord->axisElements[axisIndex].logicalMax - deviceRecord->axisElements[axisIndex].logicalMin) * 2.0f - 1.0f;
  520. input->joystickAxisMoved(axisIndex, floatValue, deviceRecord->deviceID);
  521. }
  522. return;
  523. }
  524. }
  525. for (buttonIndex = 0; buttonIndex < deviceRecord->numButtons; buttonIndex++) {
  526. if (deviceRecord->buttonElements[buttonIndex].cookie == cookie) {
  527. bool down;
  528. down = IOHIDValueGetIntegerValue(value);
  529. if(down) {
  530. input->joystickButtonDown(buttonIndex, deviceRecord->deviceID);
  531. } else {
  532. input->joystickButtonUp(buttonIndex, deviceRecord->deviceID);
  533. }
  534. return;
  535. }
  536. }
  537. }
  538. static void onDeviceMatched(void * context, IOReturn result, void * sender, IOHIDDeviceRef device) {
  539. CocoaCore *core = (CocoaCore*) context;
  540. CFArrayRef elements;
  541. CFIndex elementIndex;
  542. IOHIDElementRef element;
  543. CFStringRef cfProductName;
  544. IOHIDElementType type;
  545. char * description;
  546. GamepadDeviceEntry *entry = new GamepadDeviceEntry();
  547. entry->device = device;
  548. entry->input = core->getInput();
  549. entry->deviceID = core->nextDeviceID++;
  550. core->gamepads.push_back(entry);
  551. core->getInput()->addJoystick(entry->deviceID);
  552. elements = IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone);
  553. for (elementIndex = 0; elementIndex < CFArrayGetCount(elements); elementIndex++) {
  554. element = (IOHIDElementRef) CFArrayGetValueAtIndex(elements, elementIndex);
  555. type = IOHIDElementGetType(element);
  556. // All of the axis elements I've ever detected have been kIOHIDElementTypeInput_Misc. kIOHIDElementTypeInput_Axis is only included for good faith...
  557. if (type == kIOHIDElementTypeInput_Misc ||
  558. type == kIOHIDElementTypeInput_Axis) {
  559. entry->axisElements.resize(entry->numAxes+1);
  560. entry->axisElements[entry->numAxes].cookie = IOHIDElementGetCookie(element);
  561. entry->axisElements[entry->numAxes].logicalMin = IOHIDElementGetLogicalMin(element);
  562. entry->axisElements[entry->numAxes].logicalMax = IOHIDElementGetLogicalMax(element);
  563. entry->axisElements[entry->numAxes].hasNullState = !!IOHIDElementHasNullState(element);
  564. entry->axisElements[entry->numAxes].isHatSwitch = IOHIDElementGetUsage(element) == kHIDUsage_GD_Hatswitch;
  565. entry->axisElements[entry->numAxes].isHatSwitchSecondAxis = false;
  566. entry->numAxes++;
  567. if (entry->axisElements[entry->numAxes - 1].isHatSwitch) {
  568. entry->axisElements.resize(entry->numAxes+1);
  569. entry->axisElements[entry->numAxes].isHatSwitchSecondAxis = true;
  570. entry->numAxes++;
  571. }
  572. } else if (type == kIOHIDElementTypeInput_Button) {
  573. entry->buttonElements.resize(entry->numButtons+1);
  574. entry->buttonElements[entry->numButtons].cookie = IOHIDElementGetCookie(element);
  575. entry->numButtons++;
  576. }
  577. }
  578. CFRelease(elements);
  579. IOHIDDeviceRegisterInputValueCallback(device, onDeviceValueChanged, entry);
  580. }
  581. static void onDeviceRemoved(void * context, IOReturn result, void * sender, IOHIDDeviceRef device) {
  582. CocoaCore *core = (CocoaCore*) context;
  583. for(int i=0; i < core->gamepads.size();i++) {
  584. if(core->gamepads[i]->device == device) {
  585. core->getInput()->removeJoystick(core->gamepads[i]->deviceID);
  586. delete core->gamepads[i];
  587. core->gamepads.erase(core->gamepads.begin()+i);
  588. IOHIDDeviceRegisterInputValueCallback(device, NULL, NULL);
  589. return;
  590. }
  591. }
  592. }
  593. void CocoaCore::shutdownGamepad() {
  594. if (hidManager != NULL) {
  595. unsigned int deviceIndex;
  596. IOHIDManagerRegisterDeviceMatchingCallback(hidManager, NULL, NULL);
  597. IOHIDManagerRegisterDeviceRemovalCallback(hidManager, NULL, NULL);
  598. IOHIDManagerUnscheduleFromRunLoop(hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
  599. IOHIDManagerClose(hidManager, 0);
  600. CFRelease(hidManager);
  601. hidManager = NULL;
  602. for (int i = 0; i < gamepads.size(); i++) {
  603. IOHIDDeviceRegisterInputValueCallback(gamepads[i]->device, NULL, NULL);
  604. delete gamepads[i];
  605. }
  606. }
  607. }
  608. void CocoaCore::initGamepad() {
  609. if (hidManager == NULL) {
  610. nextDeviceID = 0;
  611. CFStringRef keys[2];
  612. int value;
  613. CFNumberRef values[2];
  614. CFDictionaryRef dictionaries[3];
  615. CFArrayRef array;
  616. hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
  617. IOHIDManagerOpen(hidManager, kIOHIDOptionsTypeNone);
  618. IOHIDManagerScheduleWithRunLoop(hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
  619. keys[0] = CFSTR(kIOHIDDeviceUsagePageKey);
  620. keys[1] = CFSTR(kIOHIDDeviceUsageKey);
  621. value = kHIDPage_GenericDesktop;
  622. values[0] = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value);
  623. value = kHIDUsage_GD_Joystick;
  624. values[1] = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value);
  625. dictionaries[0] = CFDictionaryCreate(kCFAllocatorDefault, (const void **) keys, (const void **) values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  626. CFRelease(values[0]);
  627. CFRelease(values[1]);
  628. value = kHIDPage_GenericDesktop;
  629. values[0] = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value);
  630. value = kHIDUsage_GD_GamePad;
  631. values[1] = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value);
  632. dictionaries[1] = CFDictionaryCreate(kCFAllocatorDefault, (const void **) keys, (const void **) values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  633. CFRelease(values[0]);
  634. CFRelease(values[1]);
  635. value = kHIDPage_GenericDesktop;
  636. values[0] = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value);
  637. value = kHIDUsage_GD_MultiAxisController;
  638. values[1] = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value);
  639. dictionaries[2] = CFDictionaryCreate(kCFAllocatorDefault, (const void **) keys, (const void **) values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  640. CFRelease(values[0]);
  641. CFRelease(values[1]);
  642. array = CFArrayCreate(kCFAllocatorDefault, (const void **) dictionaries, 3, &kCFTypeArrayCallBacks);
  643. CFRelease(dictionaries[0]);
  644. CFRelease(dictionaries[1]);
  645. CFRelease(dictionaries[2]);
  646. IOHIDManagerSetDeviceMatchingMultiple(hidManager, array);
  647. CFRelease(array);
  648. IOHIDManagerRegisterDeviceMatchingCallback(hidManager, onDeviceMatched, this);
  649. IOHIDManagerRegisterDeviceRemovalCallback(hidManager, onDeviceRemoved, this);
  650. }
  651. }