macos_system.m 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. #import "macos_system.h"
  2. #import <Cocoa/Cocoa.h>
  3. #include <stdbool.h>
  4. #include <iron_system.h>
  5. #include <iron_gpu.h>
  6. #include <iron_math.h>
  7. #include <iron_video.h>
  8. #include <objc/runtime.h>
  9. #include <mach/mach_time.h>
  10. struct WindowData {
  11. id handle;
  12. id view;
  13. bool fullscreen;
  14. void (*resizeCallback)(int width, int height, void *data);
  15. void *resizeCallbackData;
  16. bool (*closeCallback)(void *data);
  17. void *closeCallbackData;
  18. };
  19. static struct WindowData windows[1] = {0};
  20. static bool controlKeyMouseButton = false;
  21. static int mouseX, mouseY;
  22. static bool keyboardShown = false;
  23. static const char *videoFormats[] = {"ogv", NULL};
  24. static NSApplication *myapp;
  25. static NSWindow *window;
  26. static BasicMTKView *view;
  27. static char language[3];
  28. static int current_cursor_index = 0;
  29. @implementation BasicMTKView
  30. static bool shift = false;
  31. static bool ctrl = false;
  32. static bool alt = false;
  33. static bool cmd = false;
  34. - (void)flagsChanged:(NSEvent *)theEvent {
  35. if (shift) {
  36. iron_internal_keyboard_trigger_key_up(IRON_KEY_SHIFT);
  37. shift = false;
  38. }
  39. if (ctrl) {
  40. iron_internal_keyboard_trigger_key_up(IRON_KEY_CONTROL);
  41. ctrl = false;
  42. }
  43. if (alt) {
  44. iron_internal_keyboard_trigger_key_up(IRON_KEY_ALT);
  45. alt = false;
  46. }
  47. if (cmd) {
  48. iron_internal_keyboard_trigger_key_up(IRON_KEY_META);
  49. cmd = false;
  50. }
  51. if ([theEvent modifierFlags] & NSShiftKeyMask) {
  52. iron_internal_keyboard_trigger_key_down(IRON_KEY_SHIFT);
  53. shift = true;
  54. }
  55. if ([theEvent modifierFlags] & NSControlKeyMask) {
  56. iron_internal_keyboard_trigger_key_down(IRON_KEY_CONTROL);
  57. ctrl = true;
  58. }
  59. if ([theEvent modifierFlags] & NSAlternateKeyMask) {
  60. iron_internal_keyboard_trigger_key_down(IRON_KEY_ALT);
  61. alt = true;
  62. }
  63. if ([theEvent modifierFlags] & NSCommandKeyMask) {
  64. iron_internal_keyboard_trigger_key_down(IRON_KEY_META);
  65. cmd = true;
  66. }
  67. }
  68. - (void)keyDown:(NSEvent *)theEvent {
  69. if ([theEvent isARepeat])
  70. return;
  71. NSString *characters = [theEvent charactersIgnoringModifiers];
  72. if ([characters length]) {
  73. unichar ch = [characters characterAtIndex:0];
  74. switch (ch) { // keys that exist in keydown and keypress events
  75. case 59:
  76. iron_internal_keyboard_trigger_key_down(IRON_KEY_SEMICOLON);
  77. break;
  78. case 91:
  79. iron_internal_keyboard_trigger_key_down(IRON_KEY_OPEN_BRACKET);
  80. break;
  81. case 93:
  82. iron_internal_keyboard_trigger_key_down(IRON_KEY_CLOSE_BRACKET);
  83. break;
  84. case 39:
  85. iron_internal_keyboard_trigger_key_down(IRON_KEY_QUOTE);
  86. break;
  87. case 92:
  88. iron_internal_keyboard_trigger_key_down(IRON_KEY_BACK_SLASH);
  89. break;
  90. case 44:
  91. iron_internal_keyboard_trigger_key_down(IRON_KEY_COMMA);
  92. break;
  93. case 46:
  94. iron_internal_keyboard_trigger_key_down(IRON_KEY_PERIOD);
  95. break;
  96. case 47:
  97. iron_internal_keyboard_trigger_key_down(IRON_KEY_SLASH);
  98. break;
  99. case 96:
  100. iron_internal_keyboard_trigger_key_down(IRON_KEY_BACK_QUOTE);
  101. break;
  102. case 32:
  103. iron_internal_keyboard_trigger_key_down(IRON_KEY_SPACE);
  104. break;
  105. case 34:
  106. iron_internal_keyboard_trigger_key_down(IRON_KEY_DOUBLE_QUOTE);
  107. break;
  108. case 40:
  109. iron_internal_keyboard_trigger_key_down(IRON_KEY_OPEN_PAREN);
  110. break;
  111. case 41:
  112. iron_internal_keyboard_trigger_key_down(IRON_KEY_CLOSE_PAREN);
  113. break;
  114. case 42:
  115. iron_internal_keyboard_trigger_key_down(IRON_KEY_ASTERISK);
  116. break;
  117. case 43:
  118. iron_internal_keyboard_trigger_key_down(IRON_KEY_PLUS);
  119. break;
  120. case 45:
  121. iron_internal_keyboard_trigger_key_down(IRON_KEY_HYPHEN_MINUS);
  122. break;
  123. case 61:
  124. iron_internal_keyboard_trigger_key_down(IRON_KEY_EQUALS);
  125. break;
  126. case 95:
  127. iron_internal_keyboard_trigger_key_down(IRON_KEY_UNDERSCORE);
  128. break;
  129. }
  130. switch (ch) {
  131. case NSRightArrowFunctionKey:
  132. iron_internal_keyboard_trigger_key_down(IRON_KEY_RIGHT);
  133. break;
  134. case NSLeftArrowFunctionKey:
  135. iron_internal_keyboard_trigger_key_down(IRON_KEY_LEFT);
  136. break;
  137. case NSUpArrowFunctionKey:
  138. iron_internal_keyboard_trigger_key_down(IRON_KEY_UP);
  139. break;
  140. case NSDownArrowFunctionKey:
  141. iron_internal_keyboard_trigger_key_down(IRON_KEY_DOWN);
  142. break;
  143. case 27:
  144. iron_internal_keyboard_trigger_key_down(IRON_KEY_ESCAPE);
  145. break;
  146. case NSEnterCharacter:
  147. case NSNewlineCharacter:
  148. case NSCarriageReturnCharacter:
  149. iron_internal_keyboard_trigger_key_down(IRON_KEY_RETURN);
  150. iron_internal_keyboard_trigger_key_press('\n');
  151. break;
  152. case 0x7f:
  153. iron_internal_keyboard_trigger_key_down(IRON_KEY_BACKSPACE);
  154. iron_internal_keyboard_trigger_key_press('\x08');
  155. break;
  156. case 9:
  157. iron_internal_keyboard_trigger_key_down(IRON_KEY_TAB);
  158. iron_internal_keyboard_trigger_key_press('\t');
  159. break;
  160. default:
  161. if (ch == 'x' && [theEvent modifierFlags] & NSCommandKeyMask) {
  162. char *text = iron_internal_cut_callback();
  163. if (text != NULL) {
  164. NSPasteboard *board = [NSPasteboard generalPasteboard];
  165. [board clearContents];
  166. [board setString:[NSString stringWithUTF8String:text] forType:NSStringPboardType];
  167. }
  168. }
  169. if (ch == 'c' && [theEvent modifierFlags] & NSCommandKeyMask) {
  170. char *text = iron_internal_copy_callback();
  171. if (text != NULL) {
  172. iron_copy_to_clipboard(text);
  173. }
  174. }
  175. if (ch == 'v' && [theEvent modifierFlags] & NSCommandKeyMask) {
  176. NSPasteboard *board = [NSPasteboard generalPasteboard];
  177. NSString *data = [board stringForType:NSStringPboardType];
  178. if (data != nil) {
  179. char charData[4096];
  180. strcpy(charData, [data UTF8String]);
  181. iron_internal_paste_callback(charData);
  182. }
  183. }
  184. if (ch >= L'a' && ch <= L'z') {
  185. iron_internal_keyboard_trigger_key_down(ch - L'a' + IRON_KEY_A);
  186. }
  187. else if (ch >= L'A' && ch <= L'Z') {
  188. iron_internal_keyboard_trigger_key_down(ch - L'A' + IRON_KEY_A);
  189. }
  190. else if (ch >= L'0' && ch <= L'9') {
  191. iron_internal_keyboard_trigger_key_down(ch - L'0' + IRON_KEY_0);
  192. }
  193. iron_internal_keyboard_trigger_key_press(ch);
  194. break;
  195. }
  196. }
  197. }
  198. - (void)keyUp:(NSEvent *)theEvent {
  199. NSString *characters = [theEvent charactersIgnoringModifiers];
  200. if ([characters length]) {
  201. unichar ch = [characters characterAtIndex:0];
  202. switch (ch) {
  203. case 59:
  204. iron_internal_keyboard_trigger_key_up(IRON_KEY_SEMICOLON);
  205. break;
  206. case 91:
  207. iron_internal_keyboard_trigger_key_up(IRON_KEY_OPEN_BRACKET);
  208. break;
  209. case 93:
  210. iron_internal_keyboard_trigger_key_up(IRON_KEY_CLOSE_BRACKET);
  211. break;
  212. case 39:
  213. iron_internal_keyboard_trigger_key_up(IRON_KEY_QUOTE);
  214. break;
  215. case 92:
  216. iron_internal_keyboard_trigger_key_up(IRON_KEY_BACK_SLASH);
  217. break;
  218. case 44:
  219. iron_internal_keyboard_trigger_key_up(IRON_KEY_COMMA);
  220. break;
  221. case 46:
  222. iron_internal_keyboard_trigger_key_up(IRON_KEY_PERIOD);
  223. break;
  224. case 47:
  225. iron_internal_keyboard_trigger_key_up(IRON_KEY_SLASH);
  226. break;
  227. case 96:
  228. iron_internal_keyboard_trigger_key_up(IRON_KEY_BACK_QUOTE);
  229. break;
  230. case 45:
  231. iron_internal_keyboard_trigger_key_up(IRON_KEY_HYPHEN_MINUS);
  232. break;
  233. case 61:
  234. iron_internal_keyboard_trigger_key_up(IRON_KEY_EQUALS);
  235. break;
  236. case NSRightArrowFunctionKey:
  237. iron_internal_keyboard_trigger_key_up(IRON_KEY_RIGHT);
  238. break;
  239. case NSLeftArrowFunctionKey:
  240. iron_internal_keyboard_trigger_key_up(IRON_KEY_LEFT);
  241. break;
  242. case NSUpArrowFunctionKey:
  243. iron_internal_keyboard_trigger_key_up(IRON_KEY_UP);
  244. break;
  245. case NSDownArrowFunctionKey:
  246. iron_internal_keyboard_trigger_key_up(IRON_KEY_DOWN);
  247. break;
  248. case 27:
  249. iron_internal_keyboard_trigger_key_up(IRON_KEY_ESCAPE);
  250. break;
  251. case NSEnterCharacter:
  252. case NSNewlineCharacter:
  253. case NSCarriageReturnCharacter:
  254. iron_internal_keyboard_trigger_key_up(IRON_KEY_RETURN);
  255. break;
  256. case 0x7f:
  257. iron_internal_keyboard_trigger_key_up(IRON_KEY_BACKSPACE);
  258. break;
  259. case 9:
  260. iron_internal_keyboard_trigger_key_up(IRON_KEY_TAB);
  261. break;
  262. case 32:
  263. iron_internal_keyboard_trigger_key_up(IRON_KEY_SPACE);
  264. break;
  265. case 34:
  266. iron_internal_keyboard_trigger_key_up(IRON_KEY_DOUBLE_QUOTE);
  267. break;
  268. case 40:
  269. iron_internal_keyboard_trigger_key_up(IRON_KEY_OPEN_PAREN);
  270. break;
  271. case 41:
  272. iron_internal_keyboard_trigger_key_up(IRON_KEY_CLOSE_PAREN);
  273. break;
  274. case 42:
  275. iron_internal_keyboard_trigger_key_up(IRON_KEY_ASTERISK);
  276. break;
  277. case 43:
  278. iron_internal_keyboard_trigger_key_up(IRON_KEY_PLUS);
  279. break;
  280. case 95:
  281. iron_internal_keyboard_trigger_key_up(IRON_KEY_UNDERSCORE);
  282. break;
  283. default:
  284. if (ch >= L'a' && ch <= L'z') {
  285. iron_internal_keyboard_trigger_key_up(ch - L'a' + IRON_KEY_A);
  286. }
  287. else if (ch >= L'A' && ch <= L'Z') {
  288. iron_internal_keyboard_trigger_key_up(ch - L'A' + IRON_KEY_A);
  289. }
  290. else if (ch >= L'0' && ch <= L'9') {
  291. iron_internal_keyboard_trigger_key_up(ch - L'0' + IRON_KEY_0);
  292. }
  293. break;
  294. }
  295. }
  296. }
  297. static int getMouseX(NSEvent *event) {
  298. NSWindow *window = [[NSApplication sharedApplication] mainWindow];
  299. float scale = [window backingScaleFactor];
  300. return (int)([event locationInWindow].x * scale);
  301. }
  302. static int getMouseY(NSEvent *event) {
  303. NSWindow *window = [[NSApplication sharedApplication] mainWindow];
  304. float scale = [window backingScaleFactor];
  305. return (int)(iron_window_height() - [event locationInWindow].y * scale);
  306. }
  307. - (void)mouseDown:(NSEvent *)theEvent {
  308. if ([theEvent modifierFlags] & NSControlKeyMask) {
  309. controlKeyMouseButton = true;
  310. iron_internal_mouse_trigger_press(1, getMouseX(theEvent), getMouseY(theEvent));
  311. }
  312. else {
  313. controlKeyMouseButton = false;
  314. iron_internal_mouse_trigger_press(0, getMouseX(theEvent), getMouseY(theEvent));
  315. }
  316. if ([theEvent subtype] == NSTabletPointEventSubtype) {
  317. iron_internal_pen_trigger_press(getMouseX(theEvent), getMouseY(theEvent), theEvent.pressure);
  318. }
  319. }
  320. - (void)mouseUp:(NSEvent *)theEvent {
  321. if (controlKeyMouseButton) {
  322. iron_internal_mouse_trigger_release(1, getMouseX(theEvent), getMouseY(theEvent));
  323. }
  324. else {
  325. iron_internal_mouse_trigger_release(0, getMouseX(theEvent), getMouseY(theEvent));
  326. }
  327. controlKeyMouseButton = false;
  328. if ([theEvent subtype] == NSTabletPointEventSubtype) {
  329. iron_internal_pen_trigger_release(getMouseX(theEvent), getMouseY(theEvent), theEvent.pressure);
  330. }
  331. }
  332. - (void)mouseMoved:(NSEvent *)theEvent {
  333. iron_internal_mouse_trigger_move(getMouseX(theEvent), getMouseY(theEvent));
  334. }
  335. - (void)mouseDragged:(NSEvent *)theEvent {
  336. iron_internal_mouse_trigger_move(getMouseX(theEvent), getMouseY(theEvent));
  337. if ([theEvent subtype] == NSTabletPointEventSubtype) {
  338. iron_internal_pen_trigger_move(getMouseX(theEvent), getMouseY(theEvent), theEvent.pressure);
  339. }
  340. }
  341. - (void)rightMouseDown:(NSEvent *)theEvent {
  342. iron_internal_mouse_trigger_press(1, getMouseX(theEvent), getMouseY(theEvent));
  343. }
  344. - (void)rightMouseUp:(NSEvent *)theEvent {
  345. iron_internal_mouse_trigger_release(1, getMouseX(theEvent), getMouseY(theEvent));
  346. }
  347. - (void)rightMouseDragged:(NSEvent *)theEvent {
  348. iron_internal_mouse_trigger_move(getMouseX(theEvent), getMouseY(theEvent));
  349. }
  350. - (void)otherMouseDown:(NSEvent *)theEvent {
  351. iron_internal_mouse_trigger_press(2, getMouseX(theEvent), getMouseY(theEvent));
  352. }
  353. - (void)otherMouseUp:(NSEvent *)theEvent {
  354. iron_internal_mouse_trigger_release(2, getMouseX(theEvent), getMouseY(theEvent));
  355. }
  356. - (void)otherMouseDragged:(NSEvent *)theEvent {
  357. iron_internal_mouse_trigger_move(getMouseX(theEvent), getMouseY(theEvent));
  358. }
  359. - (void)scrollWheel:(NSEvent *)theEvent {
  360. int delta = [theEvent deltaY];
  361. iron_internal_mouse_trigger_scroll(-delta);
  362. }
  363. - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
  364. NSPasteboard *pboard = [sender draggingPasteboard];
  365. NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
  366. if ([[pboard types] containsObject:NSURLPboardType]) {
  367. if (sourceDragMask & NSDragOperationLink) {
  368. return NSDragOperationLink;
  369. }
  370. }
  371. return NSDragOperationNone;
  372. }
  373. - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
  374. NSPasteboard *pboard = [sender draggingPasteboard];
  375. if ([[pboard types] containsObject:NSURLPboardType]) {
  376. NSArray *urls = [pboard readObjectsForClasses:@[[NSURL class]] options:nil];
  377. for (NSURL *fileURL in urls) {
  378. wchar_t *filePath = (wchar_t *)[fileURL.path cStringUsingEncoding:NSUTF32LittleEndianStringEncoding];
  379. iron_internal_drop_files_callback(filePath);
  380. }
  381. }
  382. return YES;
  383. }
  384. - (id)initWithFrame:(NSRect)frameRect {
  385. self = [super initWithFrame:frameRect];
  386. device = MTLCreateSystemDefaultDevice();
  387. commandQueue = [device newCommandQueue];
  388. library = [device newDefaultLibrary];
  389. CAMetalLayer *metalLayer = (CAMetalLayer *)self.layer;
  390. metalLayer.device = device;
  391. metalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm;
  392. metalLayer.framebufferOnly = YES;
  393. metalLayer.opaque = YES;
  394. metalLayer.backgroundColor = nil;
  395. return self;
  396. }
  397. - (BOOL)acceptsFirstResponder {
  398. return YES;
  399. }
  400. - (BOOL)becomeFirstResponder {
  401. return YES;
  402. }
  403. - (BOOL)resignFirstResponder {
  404. return YES;
  405. }
  406. - (void)resize:(NSSize)size {
  407. [self setFrameSize:size];
  408. }
  409. - (CAMetalLayer *)metalLayer {
  410. return (CAMetalLayer *)self.layer;
  411. }
  412. - (id<MTLDevice>)metalDevice {
  413. return device;
  414. }
  415. - (id<MTLCommandQueue>)metalQueue {
  416. return commandQueue;
  417. }
  418. @end
  419. void iron_copy_to_clipboard(const char *text) {
  420. NSPasteboard *board = [NSPasteboard generalPasteboard];
  421. [board clearContents];
  422. [board setString:[NSString stringWithUTF8String:text] forType:NSStringPboardType];
  423. }
  424. int iron_count_displays(void) {
  425. NSArray *screens = [NSScreen screens];
  426. return (int)[screens count];
  427. }
  428. int iron_primary_display(void) {
  429. NSArray *screens = [NSScreen screens];
  430. NSScreen *mainScreen = [NSScreen mainScreen];
  431. int max_displays = 8;
  432. for (int i = 0; i < max_displays; ++i) {
  433. if (mainScreen == screens[i]) {
  434. return i;
  435. }
  436. }
  437. return -1;
  438. }
  439. void iron_display_init(void) {}
  440. iron_display_mode_t iron_display_current_mode(int display) {
  441. NSArray *screens = [NSScreen screens];
  442. NSScreen *screen = screens[display];
  443. NSRect screenRect = [screen frame];
  444. iron_display_mode_t dm;
  445. dm.width = screenRect.size.width;
  446. dm.height = screenRect.size.height;
  447. dm.frequency = 60;
  448. dm.bits_per_pixel = 32;
  449. NSDictionary *description = [screen deviceDescription];
  450. NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue];
  451. NSNumber *screenNumber = [description objectForKey:@"NSScreenNumber"];
  452. CGSize displayPhysicalSize = CGDisplayScreenSize([screenNumber unsignedIntValue]); // in millimeters
  453. double ppi = displayPixelSize.width / (displayPhysicalSize.width * 0.039370); // Convert MM to INCH
  454. dm.pixels_per_inch = round(ppi);
  455. return dm;
  456. }
  457. void iron_internal_mouse_lock() {
  458. iron_mouse_hide();
  459. }
  460. void iron_internal_mouse_unlock(void) {
  461. iron_mouse_show();
  462. }
  463. bool iron_mouse_can_lock(void) {
  464. return true;
  465. }
  466. void iron_mouse_show(void) {
  467. CGDisplayShowCursor(kCGDirectMainDisplay);
  468. }
  469. void iron_mouse_hide(void) {
  470. CGDisplayHideCursor(kCGDirectMainDisplay);
  471. }
  472. void iron_mouse_set_position(int x, int y) {
  473. NSWindow *window = windows[0].handle;
  474. float scale = [window backingScaleFactor];
  475. NSRect rect = [[NSScreen mainScreen] frame];
  476. CGPoint point;
  477. point.x = window.frame.origin.x + (x / scale);
  478. point.y = rect.size.height - (window.frame.origin.y + (y / scale));
  479. CGDisplayMoveCursorToPoint(0, point);
  480. CGAssociateMouseAndMouseCursorPosition(true);
  481. }
  482. void iron_mouse_get_position(int *x, int *y) {
  483. NSWindow *window = windows[0].handle;
  484. NSPoint point = [window mouseLocationOutsideOfEventStream];
  485. *x = (int)point.x;
  486. *y = (int)point.y;
  487. }
  488. void iron_mouse_set_cursor(iron_cursor_t cursor_index) {
  489. if (current_cursor_index == cursor_index) {
  490. return;
  491. }
  492. current_cursor_index = cursor_index;
  493. if (cursor_index == IRON_CURSOR_HAND) {
  494. [[NSCursor pointingHandCursor] set];
  495. }
  496. else if (cursor_index == IRON_CURSOR_IBEAM) {
  497. [[NSCursor IBeamCursor] set];
  498. }
  499. else if (cursor_index == IRON_CURSOR_SIZEWE) {
  500. [[NSCursor resizeLeftRightCursor] set];
  501. }
  502. else if (cursor_index == IRON_CURSOR_SIZENS) {
  503. [[NSCursor resizeUpDownCursor] set];
  504. }
  505. else {
  506. [[NSCursor arrowCursor] set];
  507. }
  508. }
  509. void iron_keyboard_show(void) {
  510. keyboardShown = true;
  511. }
  512. void iron_keyboard_hide(void) {
  513. keyboardShown = false;
  514. }
  515. bool iron_keyboard_active(void) {
  516. return keyboardShown;
  517. }
  518. const char *iron_system_id(void) {
  519. return "macOS";
  520. }
  521. const char **iron_video_formats(void) {
  522. return videoFormats;
  523. }
  524. void iron_set_keep_screen_on(bool on) {}
  525. double iron_frequency(void) {
  526. mach_timebase_info_data_t info;
  527. mach_timebase_info(&info);
  528. return (double)info.denom / (double)info.numer / 1e-9;
  529. }
  530. uint64_t iron_timestamp(void) {
  531. return mach_absolute_time();
  532. }
  533. bool with_autoreleasepool(bool (*f)(void)) {
  534. @autoreleasepool {
  535. return f();
  536. }
  537. }
  538. const char *iron_get_resource_path(void) {
  539. return [[[NSBundle mainBundle] resourcePath] cStringUsingEncoding:NSUTF8StringEncoding];
  540. }
  541. @interface IronApplication : NSApplication {
  542. }
  543. - (void)terminate:(id)sender;
  544. @end
  545. @interface IronAppDelegate : NSObject <NSWindowDelegate> {
  546. }
  547. - (void)windowWillClose:(NSNotification *)notification;
  548. - (void)windowDidResize:(NSNotification *)notification;
  549. - (void)windowWillMiniaturize:(NSNotification *)notification;
  550. - (void)windowDidDeminiaturize:(NSNotification *)notification;
  551. - (void)windowDidResignMain:(NSNotification *)notification;
  552. - (void)windowDidBecomeMain:(NSNotification *)notification;
  553. @end
  554. static IronAppDelegate *delegate;
  555. CAMetalLayer *get_metal_layer(void) {
  556. return [view metalLayer];
  557. }
  558. id get_metal_device(void) {
  559. return [view metalDevice];
  560. }
  561. id get_metal_queue(void) {
  562. return [view metalQueue];
  563. }
  564. bool iron_internal_handle_messages(void) {
  565. NSEvent *event = [myapp nextEventMatchingMask:NSAnyEventMask
  566. untilDate:[NSDate distantPast]
  567. inMode:NSDefaultRunLoopMode
  568. dequeue:YES]; // distantPast: non-blocking
  569. if (event != nil) {
  570. [myapp sendEvent:event];
  571. [myapp updateWindows];
  572. }
  573. // Sleep for a frame to limit the calls when the window is not visible.
  574. if (!window.visible) {
  575. [NSThread sleepForTimeInterval:1.0 / 60];
  576. }
  577. return true;
  578. }
  579. static void createWindow(iron_window_options_t *options) {
  580. int width = options->width / [[NSScreen mainScreen] backingScaleFactor];
  581. int height = options->height / [[NSScreen mainScreen] backingScaleFactor];
  582. int styleMask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable;
  583. if ((options->features & IRON_WINDOW_FEATURE_RESIZEABLE) || (options->features & IRON_WINDOW_FEATURE_MAXIMIZABLE)) {
  584. styleMask |= NSWindowStyleMaskResizable;
  585. }
  586. if (options->features & IRON_WINDOW_FEATURE_MINIMIZABLE) {
  587. styleMask |= NSWindowStyleMaskMiniaturizable;
  588. }
  589. view = [[BasicMTKView alloc] initWithFrame:NSMakeRect(0, 0, width, height)];
  590. [view registerForDraggedTypes:[NSArray arrayWithObjects:NSURLPboardType, nil]];
  591. window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, width, height) styleMask:styleMask backing:NSBackingStoreBuffered defer:TRUE];
  592. delegate = [IronAppDelegate alloc];
  593. [window setDelegate:delegate];
  594. [window setTitle:[NSString stringWithCString:options->title encoding:NSUTF8StringEncoding]];
  595. [window setAcceptsMouseMovedEvents:YES];
  596. [[window contentView] addSubview:view];
  597. [window center];
  598. windows[0].handle = window;
  599. windows[0].view = view;
  600. [window makeKeyAndOrderFront:nil];
  601. if (options->mode == IRON_WINDOW_MODE_FULLSCREEN) {
  602. [window toggleFullScreen:nil];
  603. windows[0].fullscreen = true;
  604. }
  605. }
  606. void iron_window_change_window_mode(iron_window_mode_t mode) {
  607. switch (mode) {
  608. case IRON_WINDOW_MODE_WINDOW:
  609. if (windows[0].fullscreen) {
  610. [window toggleFullScreen:nil];
  611. windows[0].fullscreen = false;
  612. }
  613. break;
  614. case IRON_WINDOW_MODE_FULLSCREEN:
  615. if (!windows[0].fullscreen) {
  616. [window toggleFullScreen:nil];
  617. windows[0].fullscreen = true;
  618. }
  619. break;
  620. }
  621. }
  622. void iron_window_set_close_callback(bool (*callback)(void *), void *data) {
  623. windows[0].closeCallback = callback;
  624. windows[0].closeCallbackData = data;
  625. }
  626. static void add_menubar(void) {
  627. NSString *appName = [[NSProcessInfo processInfo] processName];
  628. NSMenu *appMenu = [NSMenu new];
  629. NSString *quitTitle = [@"Quit " stringByAppendingString:appName];
  630. NSMenuItem *quitMenuItem = [[NSMenuItem alloc] initWithTitle:quitTitle action:@selector(terminate:) keyEquivalent:@"q"];
  631. [appMenu addItem:quitMenuItem];
  632. NSMenuItem *appMenuItem = [NSMenuItem new];
  633. [appMenuItem setSubmenu:appMenu];
  634. NSMenu *menubar = [NSMenu new];
  635. [menubar addItem:appMenuItem];
  636. [NSApp setMainMenu:menubar];
  637. }
  638. void iron_init(iron_window_options_t *win) {
  639. @autoreleasepool {
  640. myapp = [IronApplication sharedApplication];
  641. [myapp finishLaunching];
  642. [[NSRunningApplication currentApplication] activateWithOptions:(NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps)];
  643. NSApp.activationPolicy = NSApplicationActivationPolicyRegular;
  644. add_menubar();
  645. #ifdef WITH_GAMEPAD
  646. hidManager = (struct HIDManager *)malloc(sizeof(struct HIDManager));
  647. HIDManager_init(hidManager);
  648. #endif
  649. }
  650. createWindow(win);
  651. gpu_init(win->depth_bits, true);
  652. }
  653. int iron_window_width() {
  654. NSWindow *window = windows[0].handle;
  655. float scale = [window backingScaleFactor];
  656. return [[window contentView] frame].size.width * scale;
  657. }
  658. int iron_window_height() {
  659. NSWindow *window = windows[0].handle;
  660. float scale = [window backingScaleFactor];
  661. return [[window contentView] frame].size.height * scale;
  662. }
  663. void iron_load_url(const char *url) {
  664. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithUTF8String:url]]];
  665. }
  666. const char *iron_language(void) {
  667. NSString *nsstr = [[NSLocale preferredLanguages] objectAtIndex:0];
  668. const char *lang = [nsstr UTF8String];
  669. language[0] = lang[0];
  670. language[1] = lang[1];
  671. language[2] = 0;
  672. return language;
  673. }
  674. void iron_internal_shutdown(void) {}
  675. const char *iron_internal_save_path(void) {
  676. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  677. NSString *resolvedPath = [paths objectAtIndex:0];
  678. NSString *appName = [NSString stringWithUTF8String:iron_application_name()];
  679. resolvedPath = [resolvedPath stringByAppendingPathComponent:appName];
  680. NSFileManager *fileMgr = [[NSFileManager alloc] init];
  681. NSError *error;
  682. [fileMgr createDirectoryAtPath:resolvedPath withIntermediateDirectories:YES attributes:nil error:&error];
  683. resolvedPath = [resolvedPath stringByAppendingString:@"/"];
  684. return [resolvedPath cStringUsingEncoding:NSUTF8StringEncoding];
  685. }
  686. #ifndef IRON_NO_MAIN
  687. int main(int argc, char **argv) {
  688. return kickstart(argc, argv);
  689. }
  690. #endif
  691. @implementation IronApplication
  692. - (void)terminate:(id)sender {
  693. iron_stop();
  694. }
  695. @end
  696. @implementation IronAppDelegate
  697. - (BOOL)windowShouldClose:(NSWindow *)sender {
  698. if (windows[0].closeCallback != NULL) {
  699. if (windows[0].closeCallback(windows[0].closeCallbackData)) {
  700. return YES;
  701. }
  702. else {
  703. return NO;
  704. }
  705. }
  706. return YES;
  707. }
  708. - (void)windowWillClose:(NSNotification *)notification {
  709. iron_stop();
  710. }
  711. void iron_internal_call_resize_callback(int width, int height) {
  712. if (windows[0].resizeCallback != NULL) {
  713. windows[0].resizeCallback(width, height, windows[0].resizeCallbackData);
  714. }
  715. }
  716. - (void)windowDidResize:(NSNotification *)notification {
  717. NSWindow *window = [notification object];
  718. NSSize size = [[window contentView] frame].size;
  719. [view resize:size];
  720. float scale = [window backingScaleFactor];
  721. int w = size.width * scale;
  722. int h = size.height * scale;
  723. gpu_resize(w, h);
  724. iron_internal_call_resize_callback(w, h);
  725. }
  726. - (void)windowWillMiniaturize:(NSNotification *)notification {
  727. iron_internal_background_callback();
  728. }
  729. - (void)windowDidDeminiaturize:(NSNotification *)notification {
  730. iron_internal_foreground_callback();
  731. }
  732. - (void)windowDidResignMain:(NSNotification *)notification {
  733. iron_internal_pause_callback();
  734. }
  735. - (void)windowDidBecomeMain:(NSNotification *)notification {
  736. iron_internal_resume_callback();
  737. }
  738. @end
  739. int iron_window_x() {
  740. return 0;
  741. }
  742. int iron_window_y() {
  743. return 0;
  744. }
  745. void iron_window_resize(int width, int height) {}
  746. void iron_window_move(int x, int y) {}
  747. void iron_window_change_mode(iron_window_mode_t mode) {}
  748. void iron_window_destroy() {}
  749. void iron_window_show() {}
  750. void iron_window_hide() {}
  751. void iron_window_create(iron_window_options_t *win) {}
  752. void iron_window_set_title(const char *title) {
  753. NSWindow *window = windows[0].handle;
  754. [window setTitle:[NSString stringWithUTF8String:title]];
  755. }
  756. void iron_window_set_resize_callback(void (*callback)(int x, int y, void *data), void *data) {
  757. windows[0].resizeCallback = callback;
  758. windows[0].resizeCallbackData = data;
  759. }
  760. iron_window_mode_t iron_window_get_mode() {
  761. return IRON_WINDOW_MODE_WINDOW;
  762. }
  763. int iron_window_display() {
  764. return 0;
  765. }
  766. #ifdef WITH_GAMEPAD
  767. static struct HIDManager *hidManager;
  768. bool iron_gamepad_connected(int num) {
  769. return true;
  770. }
  771. void iron_gamepad_rumble(int gamepad, float left, float right) {}
  772. static void inputValueCallback(void *inContext, IOReturn inResult, void *inSender, IOHIDValueRef inIOHIDValueRef);
  773. static void valueAvailableCallback(void *inContext, IOReturn inResult, void *inSender);
  774. static void reset(struct HIDGamepad *gamepad);
  775. static void initDeviceElements(struct HIDGamepad *gamepad, CFArrayRef elements);
  776. static void buttonChanged(struct HIDGamepad *gamepad, IOHIDElementRef elementRef, IOHIDValueRef valueRef, int buttonIndex);
  777. static void axisChanged(struct HIDGamepad *gamepad, IOHIDElementRef elementRef, IOHIDValueRef valueRef, int axisIndex);
  778. static void cstringFromCFStringRef(CFStringRef string, char *cstr, size_t clen) {
  779. cstr[0] = '\0';
  780. if (string != NULL) {
  781. char temp[256];
  782. if (CFStringGetCString(string, temp, 256, kCFStringEncodingUTF8)) {
  783. temp[iron_mini(255, (int)(clen - 1))] = '\0';
  784. strncpy(cstr, temp, clen);
  785. }
  786. }
  787. }
  788. void HIDGamepad_init(struct HIDGamepad *gamepad) {
  789. reset(gamepad);
  790. }
  791. void HIDGamepad_destroy(struct HIDGamepad *gamepad) {
  792. HIDGamepad_unbind(gamepad);
  793. }
  794. void HIDGamepad_bind(struct HIDGamepad *gamepad, IOHIDDeviceRef inDeviceRef, int inPadIndex) {
  795. gamepad->hidDeviceRef = inDeviceRef;
  796. gamepad->padIndex = inPadIndex;
  797. IOHIDDeviceOpen(gamepad->hidDeviceRef, kIOHIDOptionsTypeSeizeDevice);
  798. IOHIDDeviceRegisterInputValueCallback(gamepad->hidDeviceRef, inputValueCallback, gamepad);
  799. IOHIDDeviceScheduleWithRunLoop(gamepad->hidDeviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
  800. gamepad->hidQueueRef = IOHIDQueueCreate(kCFAllocatorDefault, gamepad->hidDeviceRef, 32, kIOHIDOptionsTypeNone);
  801. if (CFGetTypeID(gamepad->hidQueueRef) == IOHIDQueueGetTypeID()) {
  802. IOHIDQueueStart(gamepad->hidQueueRef);
  803. IOHIDQueueRegisterValueAvailableCallback(gamepad->hidQueueRef, valueAvailableCallback, gamepad);
  804. IOHIDQueueScheduleWithRunLoop(gamepad->hidQueueRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
  805. }
  806. CFArrayRef elementCFArrayRef = IOHIDDeviceCopyMatchingElements(gamepad->hidDeviceRef, NULL, kIOHIDOptionsTypeNone);
  807. initDeviceElements(gamepad, elementCFArrayRef);
  808. {
  809. CFNumberRef vendorIdRef = (CFNumberRef)IOHIDDeviceGetProperty(gamepad->hidDeviceRef, CFSTR(kIOHIDVendorIDKey));
  810. CFNumberGetValue(vendorIdRef, kCFNumberIntType, &gamepad->hidDeviceVendorID);
  811. CFNumberRef productIdRef = (CFNumberRef)IOHIDDeviceGetProperty(gamepad->hidDeviceRef, CFSTR(kIOHIDProductIDKey));
  812. CFNumberGetValue(productIdRef, kCFNumberIntType, &gamepad->hidDeviceProductID);
  813. CFStringRef vendorRef = (CFStringRef)IOHIDDeviceGetProperty(gamepad->hidDeviceRef, CFSTR(kIOHIDManufacturerKey));
  814. cstringFromCFStringRef(vendorRef, gamepad->hidDeviceVendor, sizeof(gamepad->hidDeviceVendor));
  815. CFStringRef productRef = (CFStringRef)IOHIDDeviceGetProperty(gamepad->hidDeviceRef, CFSTR(kIOHIDProductKey));
  816. cstringFromCFStringRef(productRef, gamepad->hidDeviceProduct, sizeof(gamepad->hidDeviceProduct));
  817. }
  818. }
  819. static void initDeviceElements(struct HIDGamepad *gamepad, CFArrayRef elements) {
  820. for (CFIndex i = 0, count = CFArrayGetCount(elements); i < count; ++i) {
  821. IOHIDElementRef elementRef = (IOHIDElementRef)CFArrayGetValueAtIndex(elements, i);
  822. IOHIDElementType elemType = IOHIDElementGetType(elementRef);
  823. IOHIDElementCookie cookie = IOHIDElementGetCookie(elementRef);
  824. uint32_t usagePage = IOHIDElementGetUsagePage(elementRef);
  825. uint32_t usage = IOHIDElementGetUsage(elementRef);
  826. // Match up items
  827. switch (usagePage) {
  828. case kHIDPage_GenericDesktop:
  829. switch (usage) {
  830. case kHIDUsage_GD_X: // Left stick X
  831. gamepad->axis[0] = cookie;
  832. break;
  833. case kHIDUsage_GD_Y: // Left stick Y
  834. gamepad->axis[1] = cookie;
  835. break;
  836. case kHIDUsage_GD_Z: // Left trigger
  837. gamepad->axis[4] = cookie;
  838. break;
  839. case kHIDUsage_GD_Rx: // Right stick X
  840. gamepad->axis[2] = cookie;
  841. break;
  842. case kHIDUsage_GD_Ry: // Right stick Y
  843. gamepad->axis[3] = cookie;
  844. break;
  845. case kHIDUsage_GD_Rz: // Right trigger
  846. gamepad->axis[5] = cookie;
  847. break;
  848. case kHIDUsage_GD_Hatswitch:
  849. break;
  850. default:
  851. break;
  852. }
  853. break;
  854. case kHIDPage_Button:
  855. if ((usage >= 1) && (usage <= 15)) {
  856. // Button 1-11
  857. gamepad->buttons[usage - 1] = cookie;
  858. }
  859. break;
  860. default:
  861. break;
  862. }
  863. if (elemType == kIOHIDElementTypeInput_Misc || elemType == kIOHIDElementTypeInput_Button || elemType == kIOHIDElementTypeInput_Axis) {
  864. if (!IOHIDQueueContainsElement(gamepad->hidQueueRef, elementRef))
  865. IOHIDQueueAddElement(gamepad->hidQueueRef, elementRef);
  866. }
  867. }
  868. }
  869. void HIDGamepad_unbind(struct HIDGamepad *gamepad) {
  870. if (gamepad->hidQueueRef) {
  871. IOHIDQueueStop(gamepad->hidQueueRef);
  872. IOHIDQueueUnscheduleFromRunLoop(gamepad->hidQueueRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
  873. }
  874. if (gamepad->hidDeviceRef) {
  875. IOHIDDeviceUnscheduleFromRunLoop(gamepad->hidDeviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
  876. IOHIDDeviceClose(gamepad->hidDeviceRef, kIOHIDOptionsTypeSeizeDevice);
  877. }
  878. reset(gamepad);
  879. }
  880. static void reset(struct HIDGamepad *gamepad) {
  881. gamepad->padIndex = -1;
  882. gamepad->hidDeviceRef = NULL;
  883. gamepad->hidQueueRef = NULL;
  884. gamepad->hidDeviceVendor[0] = '\0';
  885. gamepad->hidDeviceProduct[0] = '\0';
  886. gamepad->hidDeviceVendorID = 0;
  887. gamepad->hidDeviceProductID = 0;
  888. memset(gamepad->axis, 0, sizeof(gamepad->axis));
  889. memset(gamepad->buttons, 0, sizeof(gamepad->buttons));
  890. }
  891. static void buttonChanged(struct HIDGamepad *gamepad, IOHIDElementRef elementRef, IOHIDValueRef valueRef, int buttonIndex) {
  892. double rawValue = IOHIDValueGetScaledValue(valueRef, kIOHIDValueScaleTypePhysical);
  893. double min = IOHIDElementGetLogicalMin(elementRef);
  894. double max = IOHIDElementGetLogicalMax(elementRef);
  895. double normalize = (rawValue - min) / (max - min);
  896. iron_internal_gamepad_trigger_button(gamepad->padIndex, buttonIndex, normalize);
  897. }
  898. static void axisChanged(struct HIDGamepad *gamepad, IOHIDElementRef elementRef, IOHIDValueRef valueRef, int axisIndex) {
  899. double rawValue = IOHIDValueGetScaledValue(valueRef, kIOHIDValueScaleTypePhysical);
  900. double min = IOHIDElementGetPhysicalMin(elementRef);
  901. double max = IOHIDElementGetPhysicalMax(elementRef);
  902. double normalize = normalize = (((rawValue - min) / (max - min)) * 2) - 1;
  903. if (axisIndex % 2 == 1) {
  904. normalize = -normalize;
  905. }
  906. iron_internal_gamepad_trigger_axis(gamepad->padIndex, axisIndex, normalize);
  907. }
  908. static void inputValueCallback(void *inContext, IOReturn inResult, void *inSender, IOHIDValueRef inIOHIDValueRef) {}
  909. static void valueAvailableCallback(void *inContext, IOReturn inResult, void *inSender) {
  910. struct HIDGamepad *pad = (struct HIDGamepad *)inContext;
  911. do {
  912. IOHIDValueRef valueRef = IOHIDQueueCopyNextValueWithTimeout((IOHIDQueueRef)inSender, 0.);
  913. if (!valueRef) {
  914. break;
  915. }
  916. IOHIDElementRef elementRef = IOHIDValueGetElement(valueRef);
  917. IOHIDElementCookie cookie = IOHIDElementGetCookie(elementRef);
  918. for (int i = 0, c = sizeof(pad->buttons); i < c; ++i) {
  919. if (cookie == pad->buttons[i]) {
  920. buttonChanged(pad, elementRef, valueRef, i);
  921. break;
  922. }
  923. }
  924. for (int i = 0, c = sizeof(pad->axis); i < c; ++i) {
  925. if (cookie == pad->axis[i]) {
  926. axisChanged(pad, elementRef, valueRef, i);
  927. break;
  928. }
  929. }
  930. CFRelease(valueRef);
  931. } while (1);
  932. }
  933. const char *iron_gamepad_vendor(int gamepad) {
  934. return "unknown";
  935. }
  936. const char *iron_gamepad_product_name(int gamepad) {
  937. return "unknown";
  938. }
  939. static int initHIDManager(struct HIDManager *manager);
  940. static bool addMatchingArray(struct HIDManager *manager, CFMutableArrayRef matchingCFArrayRef, CFDictionaryRef matchingCFDictRef);
  941. static CFMutableDictionaryRef createDeviceMatchingDictionary(struct HIDManager *manager, uint32_t inUsagePage, uint32_t inUsage);
  942. static void deviceConnected(void *inContext, IOReturn inResult, void *inSender, IOHIDDeviceRef inIOHIDDeviceRef);
  943. static void deviceRemoved(void *inContext, IOReturn inResult, void *inSender, IOHIDDeviceRef inIOHIDDeviceRef);
  944. void HIDManager_init(struct HIDManager *manager) {
  945. manager->managerRef = 0x0;
  946. initHIDManager(manager);
  947. }
  948. void HIDManager_destroy(struct HIDManager *manager) {
  949. if (manager->managerRef) {
  950. IOHIDManagerUnscheduleFromRunLoop(manager->managerRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
  951. IOHIDManagerClose(manager->managerRef, kIOHIDOptionsTypeNone);
  952. }
  953. }
  954. static int initHIDManager(struct HIDManager *manager) {
  955. manager->managerRef = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
  956. if (CFGetTypeID(manager->managerRef) == IOHIDManagerGetTypeID()) {
  957. CFMutableArrayRef matchingCFArrayRef = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
  958. if (matchingCFArrayRef) {
  959. CFDictionaryRef matchingCFDictRef = createDeviceMatchingDictionary(manager, kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick);
  960. addMatchingArray(manager, matchingCFArrayRef, matchingCFDictRef);
  961. matchingCFDictRef = createDeviceMatchingDictionary(manager, kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad);
  962. addMatchingArray(manager, matchingCFArrayRef, matchingCFDictRef);
  963. }
  964. else {
  965. iron_error("%s: CFArrayCreateMutable failed.", __PRETTY_FUNCTION__);
  966. return -1;
  967. }
  968. IOHIDManagerSetDeviceMatchingMultiple(manager->managerRef, matchingCFArrayRef);
  969. CFRelease(matchingCFArrayRef);
  970. IOHIDManagerOpen(manager->managerRef, kIOHIDOptionsTypeNone);
  971. IOHIDManagerRegisterDeviceMatchingCallback(manager->managerRef, deviceConnected, manager);
  972. IOHIDManagerRegisterDeviceRemovalCallback(manager->managerRef, deviceRemoved, manager);
  973. IOHIDManagerScheduleWithRunLoop(manager->managerRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
  974. return 0;
  975. }
  976. return -1;
  977. }
  978. bool addMatchingArray(struct HIDManager *manager, CFMutableArrayRef matchingCFArrayRef, CFDictionaryRef matchingCFDictRef) {
  979. if (matchingCFDictRef) {
  980. CFArrayAppendValue(matchingCFArrayRef, matchingCFDictRef);
  981. CFRelease(matchingCFDictRef);
  982. return true;
  983. }
  984. return false;
  985. }
  986. CFMutableDictionaryRef createDeviceMatchingDictionary(struct HIDManager *manager, uint32_t inUsagePage, uint32_t inUsage) {
  987. CFMutableDictionaryRef result = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  988. if (result) {
  989. if (inUsagePage) {
  990. CFNumberRef pageCFNumberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &inUsagePage);
  991. if (pageCFNumberRef) {
  992. CFDictionarySetValue(result, CFSTR(kIOHIDDeviceUsagePageKey), pageCFNumberRef);
  993. CFRelease(pageCFNumberRef);
  994. if (inUsage) {
  995. CFNumberRef usageCFNumberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &inUsage);
  996. if (usageCFNumberRef) {
  997. CFDictionarySetValue(result, CFSTR(kIOHIDDeviceUsageKey), usageCFNumberRef);
  998. CFRelease(usageCFNumberRef);
  999. }
  1000. }
  1001. }
  1002. }
  1003. }
  1004. return result;
  1005. }
  1006. void deviceConnected(void *inContext, IOReturn inResult, void *inSender, IOHIDDeviceRef inIOHIDDeviceRef) {
  1007. struct HIDManager *manager = (struct HIDManager *)inContext;
  1008. struct HIDManagerDeviceRecord *device = &manager->devices[0];
  1009. for (int i = 0; i < IRON_MAX_HID_DEVICES; ++i, ++device) {
  1010. if (!device->connected) {
  1011. device->connected = true;
  1012. device->device = inIOHIDDeviceRef;
  1013. HIDGamepad_bind(&device->pad, inIOHIDDeviceRef, i);
  1014. break;
  1015. }
  1016. }
  1017. }
  1018. void deviceRemoved(void *inContext, IOReturn inResult, void *inSender, IOHIDDeviceRef inIOHIDDeviceRef) {
  1019. struct HIDManager *manager = (struct HIDManager *)inContext;
  1020. struct HIDManagerDeviceRecord *device = &manager->devices[0];
  1021. for (int i = 0; i < IRON_MAX_HID_DEVICES; ++i, ++device) {
  1022. if (device->connected && device->device == inIOHIDDeviceRef) {
  1023. device->connected = false;
  1024. device->device = NULL;
  1025. HIDGamepad_unbind(&device->pad);
  1026. break;
  1027. }
  1028. }
  1029. }
  1030. #endif