os_osx.mm 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  1. /*************************************************************************/
  2. /* os_osx.mm */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #import <Cocoa/Cocoa.h>
  30. #include <IOKit/IOKitLib.h>
  31. #include <IOKit/IOCFPlugIn.h>
  32. #include <IOKit/hid/IOHIDLib.h>
  33. #include <IOKit/hid/IOHIDKeys.h>
  34. #include "sem_osx.h"
  35. #include "servers/visual/visual_server_raster.h"
  36. //#include "drivers/opengl/rasterizer_gl.h"
  37. //#include "drivers/gles2/rasterizer_gles2.h"
  38. #include "os_osx.h"
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include "print_string.h"
  42. #include "servers/physics/physics_server_sw.h"
  43. #include "drivers/gles2/rasterizer_instance_gles2.h"
  44. #include "servers/visual/visual_server_wrap_mt.h"
  45. #include "main/main.h"
  46. #include "os/keyboard.h"
  47. #include <sys/types.h>
  48. #include <sys/stat.h>
  49. #include <fcntl.h>
  50. #include <unistd.h>
  51. #include <libproc.h>
  52. //uses portions of glfw
  53. //========================================================================
  54. // GLFW 3.0 - www.glfw.org
  55. //------------------------------------------------------------------------
  56. // Copyright (c) 2002-2006 Marcus Geelnard
  57. // Copyright (c) 2006-2010 Camilla Berglund <[email protected]>
  58. //
  59. // This software is provided 'as-is', without any express or implied
  60. // warranty. In no event will the authors be held liable for any damages
  61. // arising from the use of this software.
  62. //
  63. // Permission is granted to anyone to use this software for any purpose,
  64. // including commercial applications, and to alter it and redistribute it
  65. // freely, subject to the following restrictions:
  66. //
  67. // 1. The origin of this software must not be misrepresented; you must not
  68. // claim that you wrote the original software. If you use this software
  69. // in a product, an acknowledgment in the product documentation would
  70. // be appreciated but is not required.
  71. //
  72. // 2. Altered source versions must be plainly marked as such, and must not
  73. // be misrepresented as being the original software.
  74. //
  75. // 3. This notice may not be removed or altered from any source
  76. // distribution.
  77. //
  78. //========================================================================
  79. static NSRect convertRectToBacking(NSRect contentRect) {
  80. #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  81. if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6)
  82. return [OS_OSX::singleton->window_view convertRectToBacking:contentRect];
  83. else
  84. #endif /*MAC_OS_X_VERSION_MAX_ALLOWED*/
  85. return contentRect;
  86. }
  87. static InputModifierState translateFlags(NSUInteger flags)
  88. {
  89. InputModifierState mod;
  90. mod.shift = (flags & NSShiftKeyMask);
  91. mod.control = (flags & NSControlKeyMask);
  92. mod.alt = (flags & NSAlternateKeyMask);
  93. mod.meta = (flags & NSCommandKeyMask);
  94. return mod;
  95. }
  96. static int mouse_x=0;
  97. static int mouse_y=0;
  98. static int prev_mouse_x=0;
  99. static int prev_mouse_y=0;
  100. static int button_mask=0;
  101. @interface GodotApplication : NSApplication
  102. @end
  103. @implementation GodotApplication
  104. // From http://cocoadev.com/index.pl?GameKeyboardHandlingAlmost
  105. // This works around an AppKit bug, where key up events while holding
  106. // down the command key don't get sent to the key window.
  107. - (void)sendEvent:(NSEvent *)event
  108. {
  109. if ([event type] == NSKeyUp && ([event modifierFlags] & NSCommandKeyMask))
  110. [[self keyWindow] sendEvent:event];
  111. else
  112. [super sendEvent:event];
  113. }
  114. @end
  115. @interface GodotApplicationDelegate : NSObject
  116. @end
  117. @implementation GodotApplicationDelegate
  118. - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
  119. {
  120. /* _Godotwindow* window;
  121. for (window = _Godot.windowListHead; window; window = window->next)
  122. _GodotInputWindowCloseRequest(window);
  123. */
  124. return NSTerminateCancel;
  125. }
  126. - (void)applicationDidHide:(NSNotification *)notification
  127. {
  128. /* _Godotwindow* window;
  129. for (window = _Godot.windowListHead; window; window = window->next)
  130. _GodotInputWindowVisibility(window, GL_FALSE);
  131. */
  132. }
  133. - (void)applicationDidUnhide:(NSNotification *)notification
  134. {
  135. /*
  136. _Godotwindow* window;
  137. for (window = _Godot.windowListHead; window; window = window->next)
  138. {
  139. if ([window_object isVisible])
  140. _GodotInputWindowVisibility(window, GL_TRUE);
  141. }
  142. */
  143. }
  144. - (void)applicationDidChangeScreenParameters:(NSNotification *) notification
  145. {
  146. //_GodotInputMonitorChange();
  147. }
  148. @end
  149. @interface GodotWindowDelegate : NSObject
  150. {
  151. // _Godotwindow* window;
  152. }
  153. @end
  154. @implementation GodotWindowDelegate
  155. - (BOOL)windowShouldClose:(id)sender
  156. {
  157. //_GodotInputWindowCloseRequest(window);
  158. if (OS_OSX::singleton->get_main_loop())
  159. OS_OSX::singleton->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST);
  160. return NO;
  161. }
  162. - (void)windowDidResize:(NSNotification *)notification
  163. {
  164. [OS_OSX::singleton->context update];
  165. const NSRect contentRect = [OS_OSX::singleton->window_view frame];
  166. const NSRect fbRect = convertRectToBacking(contentRect);
  167. OS_OSX::singleton->current_videomode.width=fbRect.size.width;
  168. OS_OSX::singleton->current_videomode.height=fbRect.size.height;
  169. // _GodotInputFramebufferSize(window, fbRect.size.width, fbRect.size.height);
  170. // _GodotInputWindowSize(window, contentRect.size.width, contentRect.size.height);
  171. //_GodotInputWindowDamage(window);
  172. //if (window->cursorMode == Godot_CURSOR_DISABLED)
  173. // centerCursor(window);
  174. }
  175. - (void)windowDidMove:(NSNotification *)notification
  176. {
  177. // [window->nsgl.context update];
  178. // int x, y;
  179. // _GodotPlatformGetWindowPos(window, &x, &y);
  180. // _GodotInputWindowPos(window, x, y);
  181. //if (window->cursorMode == Godot_CURSOR_DISABLED)
  182. // centerCursor(window);
  183. }
  184. - (void)windowDidMiniaturize:(NSNotification *)notification
  185. {
  186. // _GodotInputWindowIconify(window, GL_TRUE);
  187. }
  188. - (void)windowDidDeminiaturize:(NSNotification *)notification
  189. {
  190. //if (window->monitor)
  191. // enterFullscreenMode(window);
  192. // _GodotInputWindowIconify(window, GL_FALSE);
  193. }
  194. - (void)windowDidBecomeKey:(NSNotification *)notification
  195. {
  196. // _GodotInputWindowFocus(window, GL_TRUE);
  197. // _GodotPlatformSetCursorMode(window, window->cursorMode);
  198. if (OS_OSX::singleton->get_main_loop())
  199. OS_OSX::singleton->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
  200. }
  201. - (void)windowDidResignKey:(NSNotification *)notification
  202. {
  203. // _GodotInputWindowFocus(window, GL_FALSE);
  204. // _GodotPlatformSetCursorMode(window, Godot_CURSOR_NORMAL);
  205. if (OS_OSX::singleton->get_main_loop())
  206. OS_OSX::singleton->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
  207. }
  208. @end
  209. @interface GodotContentView : NSView
  210. {
  211. NSTrackingArea* trackingArea;
  212. }
  213. @end
  214. @implementation GodotContentView
  215. + (void)initialize
  216. {
  217. if (self == [GodotContentView class])
  218. {
  219. /* if (_glfw.ns.cursor == nil)
  220. {
  221. NSImage* data = [[NSImage alloc] initWithSize:NSMakeSize(1, 1)];
  222. _glfw.ns.cursor = [[NSCursor alloc] initWithImage:data
  223. hotSpot:NSZeroPoint];
  224. [data release];
  225. }*/
  226. }
  227. }
  228. - (id)init
  229. {
  230. self = [super init];
  231. trackingArea = nil;
  232. [self updateTrackingAreas];
  233. return self;
  234. }
  235. -(void)dealloc
  236. {
  237. [trackingArea release];
  238. [super dealloc];
  239. }
  240. - (BOOL)isOpaque
  241. {
  242. return YES;
  243. }
  244. - (BOOL)canBecomeKeyView
  245. {
  246. return YES;
  247. }
  248. - (BOOL)acceptsFirstResponder
  249. {
  250. return YES;
  251. }
  252. - (void)cursorUpdate:(NSEvent *)event
  253. {
  254. // setModeCursor(window, window->cursorMode);
  255. }
  256. - (void)mouseDown:(NSEvent *)event
  257. {
  258. print_line("mouse down:");
  259. button_mask|=BUTTON_MASK_LEFT;
  260. InputEvent ev;
  261. ev.type=InputEvent::MOUSE_BUTTON;
  262. ev.mouse_button.button_index=BUTTON_LEFT;
  263. ev.mouse_button.pressed=true;
  264. ev.mouse_button.x=mouse_x;
  265. ev.mouse_button.y=mouse_y;
  266. ev.mouse_button.global_x=mouse_x;
  267. ev.mouse_button.global_y=mouse_y;
  268. ev.mouse_button.button_mask=button_mask;
  269. ev.mouse_button.doubleclick = [event clickCount]==2;
  270. ev.mouse_button.mod = translateFlags([event modifierFlags]);
  271. OS_OSX::singleton->push_input(ev);
  272. /* _glfwInputMouseClick(window,
  273. GLFW_MOUSE_BUTTON_LEFT,
  274. GLFW_PRESS,
  275. translateFlags([event modifierFlags]));*/
  276. }
  277. - (void)mouseDragged:(NSEvent *)event
  278. {
  279. [self mouseMoved:event];
  280. }
  281. - (void)mouseUp:(NSEvent *)event
  282. {
  283. button_mask&=~BUTTON_MASK_LEFT;
  284. InputEvent ev;
  285. ev.type=InputEvent::MOUSE_BUTTON;
  286. ev.mouse_button.button_index=BUTTON_LEFT;
  287. ev.mouse_button.pressed=false;
  288. ev.mouse_button.x=mouse_x;
  289. ev.mouse_button.y=mouse_y;
  290. ev.mouse_button.global_x=mouse_x;
  291. ev.mouse_button.global_y=mouse_y;
  292. ev.mouse_button.button_mask=button_mask;
  293. ev.mouse_button.mod = translateFlags([event modifierFlags]);
  294. OS_OSX::singleton->push_input(ev);
  295. /* _glfwInputMouseClick(window,
  296. GLFW_MOUSE_BUTTON_LEFT,
  297. GLFW_RELEASE,
  298. translateFlags([event modifierFlags]));*/
  299. }
  300. - (void)mouseMoved:(NSEvent *)event
  301. {
  302. InputEvent ev;
  303. ev.type=InputEvent::MOUSE_MOTION;
  304. ev.mouse_motion.button_mask=button_mask;
  305. prev_mouse_x=mouse_x;
  306. prev_mouse_y=mouse_y;
  307. const NSRect contentRect = [OS_OSX::singleton->window_view frame];
  308. const NSPoint p = [event locationInWindow];
  309. mouse_x = p.x * [[event window] backingScaleFactor];
  310. mouse_y = (contentRect.size.height - p.y) * [[event window] backingScaleFactor];
  311. ev.mouse_motion.x=mouse_x;
  312. ev.mouse_motion.y=mouse_y;
  313. ev.mouse_motion.global_x=mouse_x;
  314. ev.mouse_motion.global_y=mouse_y;
  315. ev.mouse_motion.relative_x=mouse_x - prev_mouse_x;
  316. ev.mouse_motion.relative_y=mouse_y - prev_mouse_y;
  317. ev.mouse_motion.mod = translateFlags([event modifierFlags]);
  318. // ev.mouse_motion.relative_x=[event deltaX];
  319. // ev.mouse_motion.relative_y=[event deltaY];
  320. OS_OSX::singleton->input->set_mouse_pos(Point2(mouse_x,mouse_y));
  321. OS_OSX::singleton->push_input(ev);
  322. /* if (window->cursorMode == GLFW_CURSOR_DISABLED)
  323. _glfwInputCursorMotion(window, [event deltaX], [event deltaY]);
  324. else
  325. {
  326. const NSRect contentRect = [window->ns.view frame];
  327. const NSPoint p = [event locationInWindow];
  328. _glfwInputCursorMotion(window, p.x, contentRect.size.height - p.y);
  329. }*/
  330. }
  331. - (void)rightMouseDown:(NSEvent *)event
  332. {
  333. button_mask|=BUTTON_MASK_RIGHT;
  334. InputEvent ev;
  335. ev.type=InputEvent::MOUSE_BUTTON;
  336. ev.mouse_button.button_index=BUTTON_RIGHT;
  337. ev.mouse_button.pressed=true;
  338. ev.mouse_button.x=mouse_x;
  339. ev.mouse_button.y=mouse_y;
  340. ev.mouse_button.global_x=mouse_x;
  341. ev.mouse_button.global_y=mouse_y;
  342. ev.mouse_button.button_mask=button_mask;
  343. ev.mouse_button.mod = translateFlags([event modifierFlags]);
  344. OS_OSX::singleton->push_input(ev);
  345. /* _glfwInputMouseClick(window,
  346. GLFW_MOUSE_BUTTON_RIGHT,
  347. GLFW_PRESS,
  348. translateFlags([event modifierFlags]));*/
  349. }
  350. - (void)rightMouseDragged:(NSEvent *)event
  351. {
  352. [self mouseMoved:event];
  353. }
  354. - (void)rightMouseUp:(NSEvent *)event
  355. {
  356. button_mask&=~BUTTON_MASK_RIGHT;
  357. InputEvent ev;
  358. ev.type=InputEvent::MOUSE_BUTTON;
  359. ev.mouse_button.button_index=BUTTON_RIGHT;
  360. ev.mouse_button.pressed=false;
  361. ev.mouse_button.x=mouse_x;
  362. ev.mouse_button.y=mouse_y;
  363. ev.mouse_button.global_x=mouse_x;
  364. ev.mouse_button.global_y=mouse_y;
  365. ev.mouse_button.button_mask=button_mask;
  366. ev.mouse_button.mod = translateFlags([event modifierFlags]);
  367. OS_OSX::singleton->push_input(ev);
  368. /*_glfwInputMouseClick(window,
  369. GLFW_MOUSE_BUTTON_RIGHT,
  370. GLFW_RELEASE,
  371. translateFlags([event modifierFlags]));*/
  372. }
  373. - (void)otherMouseDown:(NSEvent *)event
  374. {
  375. if ((int) [event buttonNumber]!=2)
  376. return;
  377. button_mask|=BUTTON_MASK_MIDDLE;
  378. InputEvent ev;
  379. ev.type=InputEvent::MOUSE_BUTTON;
  380. ev.mouse_button.button_index=BUTTON_MIDDLE;
  381. ev.mouse_button.pressed=true;
  382. ev.mouse_button.x=mouse_x;
  383. ev.mouse_button.y=mouse_y;
  384. ev.mouse_button.global_x=mouse_x;
  385. ev.mouse_button.global_y=mouse_y;
  386. ev.mouse_button.button_mask=button_mask;
  387. ev.mouse_button.mod = translateFlags([event modifierFlags]);
  388. OS_OSX::singleton->push_input(ev);
  389. /*_glfwInputMouseClick(window,
  390. (int) [event buttonNumber],
  391. GLFW_PRESS,
  392. translateFlags([event modifierFlags]));*/
  393. }
  394. - (void)otherMouseDragged:(NSEvent *)event
  395. {
  396. [self mouseMoved:event];
  397. }
  398. - (void)otherMouseUp:(NSEvent *)event
  399. {
  400. if ((int) [event buttonNumber]!=2)
  401. return;
  402. button_mask&=~BUTTON_MASK_MIDDLE;
  403. InputEvent ev;
  404. ev.type=InputEvent::MOUSE_BUTTON;
  405. ev.mouse_button.button_index=BUTTON_MIDDLE;
  406. ev.mouse_button.pressed=false;
  407. ev.mouse_button.x=mouse_x;
  408. ev.mouse_button.y=mouse_y;
  409. ev.mouse_button.global_x=mouse_x;
  410. ev.mouse_button.global_y=mouse_y;
  411. ev.mouse_button.button_mask=button_mask;
  412. ev.mouse_button.mod = translateFlags([event modifierFlags]);
  413. OS_OSX::singleton->push_input(ev);
  414. /* _glfwInputMouseClick(window,
  415. (int) [event buttonNumber],
  416. GLFW_RELEASE,
  417. translateFlags([event modifierFlags]));*/
  418. }
  419. - (void)mouseExited:(NSEvent *)event
  420. {
  421. // _glfwInputCursorEnter(window, GL_FALSE);
  422. }
  423. - (void)mouseEntered:(NSEvent *)event
  424. {
  425. // _glfwInputCursorEnter(window, GL_TRUE);
  426. }
  427. - (void)viewDidChangeBackingProperties
  428. {
  429. /* const NSRect contentRect = [window->ns.view frame];
  430. const NSRect fbRect = convertRectToBacking(window, contentRect);
  431. _glfwInputFramebufferSize(window, fbRect.size.width, fbRect.size.height);*/
  432. }
  433. - (void)updateTrackingAreas
  434. {
  435. if (trackingArea != nil)
  436. {
  437. [self removeTrackingArea:trackingArea];
  438. [trackingArea release];
  439. }
  440. NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited |
  441. NSTrackingActiveInKeyWindow |
  442. NSTrackingCursorUpdate |
  443. NSTrackingInVisibleRect;
  444. trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds]
  445. options:options
  446. owner:self
  447. userInfo:nil];
  448. [self addTrackingArea:trackingArea];
  449. [super updateTrackingAreas];
  450. }
  451. // Translates a OS X keycode to a Godot keycode
  452. //
  453. static int translateKey(unsigned int key)
  454. {
  455. // Keyboard symbol translation table
  456. static const unsigned int table[128] =
  457. {
  458. /* 00 */ KEY_A,
  459. /* 01 */ KEY_S,
  460. /* 02 */ KEY_D,
  461. /* 03 */ KEY_F,
  462. /* 04 */ KEY_H,
  463. /* 05 */ KEY_G,
  464. /* 06 */ KEY_Z,
  465. /* 07 */ KEY_X,
  466. /* 08 */ KEY_C,
  467. /* 09 */ KEY_V,
  468. /* 0a */ KEY_UNKNOWN,
  469. /* 0b */ KEY_B,
  470. /* 0c */ KEY_Q,
  471. /* 0d */ KEY_W,
  472. /* 0e */ KEY_E,
  473. /* 0f */ KEY_R,
  474. /* 10 */ KEY_Y,
  475. /* 11 */ KEY_T,
  476. /* 12 */ KEY_1,
  477. /* 13 */ KEY_2,
  478. /* 14 */ KEY_3,
  479. /* 15 */ KEY_4,
  480. /* 16 */ KEY_6,
  481. /* 17 */ KEY_5,
  482. /* 18 */ KEY_EQUAL,
  483. /* 19 */ KEY_9,
  484. /* 1a */ KEY_7,
  485. /* 1b */ KEY_MINUS,
  486. /* 1c */ KEY_8,
  487. /* 1d */ KEY_0,
  488. /* 1e */ KEY_BRACERIGHT,
  489. /* 1f */ KEY_O,
  490. /* 20 */ KEY_U,
  491. /* 21 */ KEY_BRACELEFT,
  492. /* 22 */ KEY_I,
  493. /* 23 */ KEY_P,
  494. /* 24 */ KEY_RETURN,
  495. /* 25 */ KEY_L,
  496. /* 26 */ KEY_J,
  497. /* 27 */ KEY_APOSTROPHE,
  498. /* 28 */ KEY_K,
  499. /* 29 */ KEY_SEMICOLON,
  500. /* 2a */ KEY_BACKSLASH,
  501. /* 2b */ KEY_COMMA,
  502. /* 2c */ KEY_SLASH,
  503. /* 2d */ KEY_N,
  504. /* 2e */ KEY_M,
  505. /* 2f */ KEY_PERIOD,
  506. /* 30 */ KEY_TAB,
  507. /* 31 */ KEY_SPACE,
  508. /* 32 */ KEY_QUOTELEFT,
  509. /* 33 */ KEY_BACKSPACE,
  510. /* 34 */ KEY_UNKNOWN,
  511. /* 35 */ KEY_ESCAPE,
  512. /* 36 */ KEY_META,
  513. /* 37 */ KEY_META,
  514. /* 38 */ KEY_SHIFT,
  515. /* 39 */ KEY_CAPSLOCK,
  516. /* 3a */ KEY_ALT,
  517. /* 3b */ KEY_CONTROL,
  518. /* 3c */ KEY_SHIFT,
  519. /* 3d */ KEY_ALT,
  520. /* 3e */ KEY_CONTROL,
  521. /* 3f */ KEY_UNKNOWN, /* Function */
  522. /* 40 */ KEY_UNKNOWN,
  523. /* 41 */ KEY_KP_PERIOD,
  524. /* 42 */ KEY_UNKNOWN,
  525. /* 43 */ KEY_KP_MULTIPLY,
  526. /* 44 */ KEY_UNKNOWN,
  527. /* 45 */ KEY_KP_ADD,
  528. /* 46 */ KEY_UNKNOWN,
  529. /* 47 */ KEY_NUMLOCK, /* Really KeypadClear... */
  530. /* 48 */ KEY_UNKNOWN, /* VolumeUp */
  531. /* 49 */ KEY_UNKNOWN, /* VolumeDown */
  532. /* 4a */ KEY_UNKNOWN, /* Mute */
  533. /* 4b */ KEY_KP_DIVIDE,
  534. /* 4c */ KEY_KP_ENTER,
  535. /* 4d */ KEY_UNKNOWN,
  536. /* 4e */ KEY_KP_SUBSTRACT,
  537. /* 4f */ KEY_UNKNOWN,
  538. /* 50 */ KEY_UNKNOWN,
  539. /* 51 */ KEY_EQUAL, //wtf equal?
  540. /* 52 */ KEY_KP_0,
  541. /* 53 */ KEY_KP_1,
  542. /* 54 */ KEY_KP_2,
  543. /* 55 */ KEY_KP_3,
  544. /* 56 */ KEY_KP_4,
  545. /* 57 */ KEY_KP_5,
  546. /* 58 */ KEY_KP_6,
  547. /* 59 */ KEY_KP_7,
  548. /* 5a */ KEY_UNKNOWN,
  549. /* 5b */ KEY_KP_8,
  550. /* 5c */ KEY_KP_9,
  551. /* 5d */ KEY_UNKNOWN,
  552. /* 5e */ KEY_UNKNOWN,
  553. /* 5f */ KEY_UNKNOWN,
  554. /* 60 */ KEY_F5,
  555. /* 61 */ KEY_F6,
  556. /* 62 */ KEY_F7,
  557. /* 63 */ KEY_F3,
  558. /* 64 */ KEY_F8,
  559. /* 65 */ KEY_F9,
  560. /* 66 */ KEY_UNKNOWN,
  561. /* 67 */ KEY_F11,
  562. /* 68 */ KEY_UNKNOWN,
  563. /* 69 */ KEY_F13,
  564. /* 6a */ KEY_F16,
  565. /* 6b */ KEY_F14,
  566. /* 6c */ KEY_UNKNOWN,
  567. /* 6d */ KEY_F10,
  568. /* 6e */ KEY_UNKNOWN,
  569. /* 6f */ KEY_F12,
  570. /* 70 */ KEY_UNKNOWN,
  571. /* 71 */ KEY_F15,
  572. /* 72 */ KEY_INSERT, /* Really Help... */
  573. /* 73 */ KEY_HOME,
  574. /* 74 */ KEY_PAGEUP,
  575. /* 75 */ KEY_DELETE,
  576. /* 76 */ KEY_F4,
  577. /* 77 */ KEY_END,
  578. /* 78 */ KEY_F2,
  579. /* 79 */ KEY_PAGEDOWN,
  580. /* 7a */ KEY_F1,
  581. /* 7b */ KEY_LEFT,
  582. /* 7c */ KEY_RIGHT,
  583. /* 7d */ KEY_DOWN,
  584. /* 7e */ KEY_UP,
  585. /* 7f */ KEY_UNKNOWN,
  586. };
  587. if (key >= 128)
  588. return KEY_UNKNOWN;
  589. return table[key];
  590. }
  591. - (void)keyDown:(NSEvent *)event
  592. {
  593. InputEvent ev;
  594. ev.type=InputEvent::KEY;
  595. ev.key.pressed=true;
  596. ev.key.mod=translateFlags([event modifierFlags]);
  597. ev.key.scancode = translateKey([event keyCode]);
  598. ev.key.echo = [event isARepeat];
  599. NSString* characters = [event characters];
  600. NSUInteger i, length = [characters length];
  601. if (length>0 && keycode_has_unicode(ev.key.scancode)) {
  602. for (i = 0; i < length; i++) {
  603. ev.key.unicode=[characters characterAtIndex:i];
  604. OS_OSX::singleton->push_input(ev);
  605. ev.key.scancode=0;
  606. }
  607. } else {
  608. OS_OSX::singleton->push_input(ev);
  609. }
  610. }
  611. - (void)flagsChanged:(NSEvent *)event
  612. {
  613. /* int action;
  614. unsigned int newModifierFlags =
  615. [event modifierFlags] & NSDeviceIndependentModifierFlagsMask;
  616. if (newModifierFlags > window->ns.modifierFlags)
  617. action = GLFW_PRESS;
  618. else
  619. action = GLFW_RELEASE;
  620. window->ns.modifierFlags = newModifierFlags;
  621. const int key = translateKey([event keyCode]);
  622. const int mods = translateFlags([event modifierFlags]);
  623. _glfwInputKey(window, key, [event keyCode], action, mods);*/
  624. }
  625. - (void)keyUp:(NSEvent *)event
  626. {
  627. InputEvent ev;
  628. ev.type=InputEvent::KEY;
  629. ev.key.pressed=false;
  630. ev.key.mod=translateFlags([event modifierFlags]);
  631. ev.key.scancode = translateKey([event keyCode]);
  632. OS_OSX::singleton->push_input(ev);
  633. /* const int key = translateKey([event keyCode]);
  634. const int mods = translateFlags([event modifierFlags]);
  635. _glfwInputKey(window, key, [event keyCode], GLFW_RELEASE, mods);*/
  636. }
  637. - (void)scrollWheel:(NSEvent *)event
  638. {
  639. double deltaX, deltaY;
  640. #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  641. if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6)
  642. {
  643. deltaX = [event scrollingDeltaX];
  644. deltaY = [event scrollingDeltaY];
  645. if ([event hasPreciseScrollingDeltas])
  646. {
  647. deltaX *= 0.1;
  648. deltaY *= 0.1;
  649. }
  650. }
  651. else
  652. #endif /*MAC_OS_X_VERSION_MAX_ALLOWED*/
  653. {
  654. deltaX = [event deltaX];
  655. deltaY = [event deltaY];
  656. }
  657. if (fabs(deltaY)) {
  658. InputEvent ev;
  659. ev.type=InputEvent::MOUSE_BUTTON;
  660. ev.mouse_button.button_index=deltaY >0 ? BUTTON_WHEEL_UP : BUTTON_WHEEL_DOWN;
  661. ev.mouse_button.pressed=true;
  662. ev.mouse_button.x=mouse_x;
  663. ev.mouse_button.y=mouse_y;
  664. ev.mouse_button.global_x=mouse_x;
  665. ev.mouse_button.global_y=mouse_y;
  666. ev.mouse_button.button_mask=button_mask;
  667. OS_OSX::singleton->push_input(ev);
  668. ev.mouse_button.pressed=false;
  669. OS_OSX::singleton->push_input(ev);
  670. }
  671. }
  672. @end
  673. @interface GodotWindow : NSWindow {}
  674. @end
  675. @implementation GodotWindow
  676. - (BOOL)canBecomeKeyWindow
  677. {
  678. // Required for NSBorderlessWindowMask windows
  679. return YES;
  680. }
  681. @end
  682. int OS_OSX::get_video_driver_count() const {
  683. return 1;
  684. }
  685. const char * OS_OSX::get_video_driver_name(int p_driver) const {
  686. return "GLES2";
  687. }
  688. OS::VideoMode OS_OSX::get_default_video_mode() const {
  689. VideoMode vm;
  690. vm.width=800;
  691. vm.height=600;
  692. vm.fullscreen=false;
  693. vm.resizable=true;
  694. return vm;
  695. }
  696. void OS_OSX::initialize_core() {
  697. OS_Unix::initialize_core();
  698. SemaphoreOSX::make_default();
  699. }
  700. void OS_OSX::initialize(const VideoMode& p_desired,int p_video_driver,int p_audio_driver) {
  701. /*** OSX INITIALIZATION ***/
  702. /*** OSX INITIALIZATION ***/
  703. /*** OSX INITIALIZATION ***/
  704. current_videomode=p_desired;
  705. window_delegate = [[GodotWindowDelegate alloc] init];
  706. // Don't use accumulation buffer support; it's not accelerated
  707. // Aux buffers probably aren't accelerated either
  708. unsigned int styleMask = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | (current_videomode.resizable?NSResizableWindowMask:0);
  709. window_object = [[GodotWindow alloc]
  710. initWithContentRect:NSMakeRect(0, 0, current_videomode.width,current_videomode.height)
  711. styleMask:styleMask
  712. backing:NSBackingStoreBuffered
  713. defer:NO];
  714. ERR_FAIL_COND( window_object==nil );
  715. window_view = [[GodotContentView alloc] init];
  716. #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  717. if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6) {
  718. [window_view setWantsBestResolutionOpenGLSurface:YES];
  719. if (current_videomode.resizable)
  720. [window_object setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
  721. }
  722. #endif /*MAC_OS_X_VERSION_MAX_ALLOWED*/
  723. // [window_object setTitle:[NSString stringWithUTF8String:"GodotEnginies"]];
  724. [window_object setContentView:window_view];
  725. [window_object setDelegate:window_delegate];
  726. [window_object setAcceptsMouseMovedEvents:YES];
  727. [window_object center];
  728. #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  729. if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6)
  730. [window_object setRestorable:NO];
  731. #endif /*MAC_OS_X_VERSION_MAX_ALLOWED*/
  732. unsigned int attributeCount = 0;
  733. // OS X needs non-zero color size, so set resonable values
  734. int colorBits = 24;
  735. // Fail if a robustness strategy was requested
  736. #define ADD_ATTR(x) { attributes[attributeCount++] = x; }
  737. #define ADD_ATTR2(x, y) { ADD_ATTR(x); ADD_ATTR(y); }
  738. // Arbitrary array size here
  739. NSOpenGLPixelFormatAttribute attributes[40];
  740. ADD_ATTR(NSOpenGLPFADoubleBuffer);
  741. ADD_ATTR(NSOpenGLPFAClosestPolicy);
  742. #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  743. if (false/* use gl3*/)
  744. ADD_ATTR2(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
  745. #endif /*MAC_OS_X_VERSION_MAX_ALLOWED*/
  746. ADD_ATTR2(NSOpenGLPFAColorSize, colorBits);
  747. /* if (fbconfig->alphaBits > 0)
  748. ADD_ATTR2(NSOpenGLPFAAlphaSize, fbconfig->alphaBits);*/
  749. ADD_ATTR2(NSOpenGLPFADepthSize, 24);
  750. ADD_ATTR2(NSOpenGLPFAStencilSize, 8);
  751. /*if (fbconfig->stereo)
  752. ADD_ATTR(NSOpenGLPFAStereo);*/
  753. /* if (fbconfig->samples > 0)
  754. {
  755. ADD_ATTR2(NSOpenGLPFASampleBuffers, 1);
  756. ADD_ATTR2(NSOpenGLPFASamples, fbconfig->samples);
  757. }*/
  758. // NOTE: All NSOpenGLPixelFormats on the relevant cards support sRGB
  759. // frambuffer, so there's no need (and no way) to request it
  760. ADD_ATTR(0);
  761. #undef ADD_ATTR
  762. #undef ADD_ATTR2
  763. pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
  764. ERR_FAIL_COND( pixelFormat == nil);
  765. context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat
  766. shareContext:nil];
  767. ERR_FAIL_COND(context==nil);
  768. [context setView:window_view];
  769. [context makeCurrentContext];
  770. [NSApp activateIgnoringOtherApps:YES];
  771. [window_object makeKeyAndOrderFront:nil];
  772. /*** END OSX INITIALIZATION ***/
  773. /*** END OSX INITIALIZATION ***/
  774. /*** END OSX INITIALIZATION ***/
  775. bool use_gl2=p_video_driver!=1;
  776. AudioDriverManagerSW::add_driver(&audio_driver_osx);
  777. rasterizer = instance_RasterizerGLES2();
  778. visual_server = memnew( VisualServerRaster(rasterizer) );
  779. if (get_render_thread_mode()!=RENDER_THREAD_UNSAFE) {
  780. visual_server =memnew(VisualServerWrapMT(visual_server,get_render_thread_mode()==RENDER_SEPARATE_THREAD));
  781. }
  782. visual_server->init();
  783. visual_server->cursor_set_visible(false, 0);
  784. AudioDriverManagerSW::get_driver(p_audio_driver)->set_singleton();
  785. if (AudioDriverManagerSW::get_driver(p_audio_driver)->init()!=OK) {
  786. ERR_PRINT("Initializing audio failed.");
  787. }
  788. sample_manager = memnew( SampleManagerMallocSW );
  789. audio_server = memnew( AudioServerSW(sample_manager) );
  790. audio_server->set_mixer_params(AudioMixerSW::INTERPOLATION_LINEAR,false);
  791. audio_server->init();
  792. spatial_sound_server = memnew( SpatialSoundServerSW );
  793. spatial_sound_server->init();
  794. spatial_sound_2d_server = memnew( SpatialSound2DServerSW );
  795. spatial_sound_2d_server->init();
  796. //
  797. physics_server = memnew( PhysicsServerSW );
  798. physics_server->init();
  799. physics_2d_server = memnew( Physics2DServerSW );
  800. physics_2d_server->init();
  801. input = memnew( InputDefault );
  802. _ensure_data_dir();
  803. }
  804. void OS_OSX::finalize() {
  805. }
  806. void OS_OSX::set_main_loop( MainLoop * p_main_loop ) {
  807. main_loop=p_main_loop;
  808. input->set_main_loop(p_main_loop);
  809. }
  810. void OS_OSX::delete_main_loop() {
  811. memdelete(main_loop);
  812. main_loop=NULL;
  813. }
  814. String OS_OSX::get_name() {
  815. return "OSX";
  816. }
  817. void OS_OSX::set_cursor_shape(CursorShape p_shape) {
  818. if (cursor_shape==p_shape)
  819. return;
  820. switch(p_shape) {
  821. case CURSOR_ARROW: [[NSCursor arrowCursor] set]; break;
  822. case CURSOR_IBEAM: [[NSCursor IBeamCursor] set]; break;
  823. case CURSOR_POINTING_HAND: [[NSCursor pointingHandCursor] set]; break;
  824. case CURSOR_CROSS: [[NSCursor crosshairCursor] set]; break;
  825. case CURSOR_WAIT: [[NSCursor arrowCursor] set]; break;
  826. case CURSOR_BUSY: [[NSCursor arrowCursor] set]; break;
  827. case CURSOR_DRAG: [[NSCursor closedHandCursor] set]; break;
  828. case CURSOR_CAN_DROP: [[NSCursor openHandCursor] set]; break;
  829. case CURSOR_FORBIDDEN: [[NSCursor arrowCursor] set]; break;
  830. case CURSOR_VSIZE: [[NSCursor resizeUpDownCursor] set]; break;
  831. case CURSOR_HSIZE: [[NSCursor resizeLeftRightCursor] set]; break;
  832. case CURSOR_BDIAGSIZE: [[NSCursor arrowCursor] set]; break;
  833. case CURSOR_FDIAGSIZE: [[NSCursor arrowCursor] set]; break;
  834. case CURSOR_MOVE: [[NSCursor arrowCursor] set]; break;
  835. case CURSOR_VSPLIT: [[NSCursor resizeUpDownCursor] set]; break;
  836. case CURSOR_HSPLIT: [[NSCursor resizeLeftRightCursor] set]; break;
  837. case CURSOR_HELP: [[NSCursor arrowCursor] set]; break;
  838. default: {};
  839. }
  840. cursor_shape=p_shape;
  841. }
  842. void OS_OSX::set_mouse_show(bool p_show) {
  843. }
  844. void OS_OSX::set_mouse_grab(bool p_grab) {
  845. }
  846. bool OS_OSX::is_mouse_grab_enabled() const {
  847. return mouse_grab;
  848. }
  849. Point2 OS_OSX::get_mouse_pos() const {
  850. return Vector2(mouse_x,mouse_y);
  851. }
  852. int OS_OSX::get_mouse_button_state() const {
  853. return button_mask;
  854. }
  855. void OS_OSX::set_window_title(const String& p_title) {
  856. [window_object setTitle:[NSString stringWithUTF8String:p_title.utf8().get_data()]];
  857. }
  858. void OS_OSX::set_icon(const Image& p_icon) {
  859. Image img=p_icon;
  860. img.convert(Image::FORMAT_RGBA);
  861. NSBitmapImageRep *imgrep= [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
  862. pixelsWide: p_icon.get_width()
  863. pixelsHigh: p_icon.get_height()
  864. bitsPerSample: 8
  865. samplesPerPixel: 4
  866. hasAlpha: YES
  867. isPlanar: NO
  868. colorSpaceName: NSDeviceRGBColorSpace
  869. bytesPerRow: p_icon.get_width()*4
  870. bitsPerPixel: 32] autorelease];
  871. ERR_FAIL_COND(imgrep==nil);
  872. uint8_t *pixels = [imgrep bitmapData];
  873. int len = img.get_width()*img.get_height();
  874. DVector<uint8_t> data = img.get_data();
  875. DVector<uint8_t>::Read r = data.read();
  876. /* Premultiply the alpha channel */
  877. for (int i = 0; i<len ; i++) {
  878. uint8_t alpha = r[i*4+3];
  879. pixels[i*4+0] = (uint8_t)(((uint16_t)r[i*4+0] * alpha) / 255);
  880. pixels[i*4+1] = (uint8_t)(((uint16_t)r[i*4+1] * alpha) / 255);
  881. pixels[i*4+2] = (uint8_t)(((uint16_t)r[i*4+2] * alpha) / 255);
  882. pixels[i*4+3] = alpha;
  883. }
  884. NSImage *nsimg = [[[NSImage alloc] initWithSize: NSMakeSize(img.get_width(),img.get_height())] autorelease];
  885. ERR_FAIL_COND(nsimg == nil);
  886. [nsimg addRepresentation: imgrep];
  887. [NSApp setApplicationIconImage:nsimg];
  888. }
  889. MainLoop *OS_OSX::get_main_loop() const {
  890. return main_loop;
  891. }
  892. bool OS_OSX::can_draw() const {
  893. return true;
  894. }
  895. void OS_OSX::set_clipboard(const String& p_text) {
  896. NSArray* types = [NSArray arrayWithObjects:NSStringPboardType, nil];
  897. NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
  898. [pasteboard declareTypes:types owner:nil];
  899. [pasteboard setString:[NSString stringWithUTF8String:p_text.utf8().get_data()]
  900. forType:NSStringPboardType];
  901. }
  902. String OS_OSX::get_clipboard() const {
  903. NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
  904. if (![[pasteboard types] containsObject:NSStringPboardType])
  905. {
  906. return "";
  907. }
  908. NSString* object = [pasteboard stringForType:NSStringPboardType];
  909. if (!object)
  910. {
  911. return "";
  912. }
  913. char *utfs = strdup([object UTF8String]);
  914. String ret;
  915. ret.parse_utf8(utfs);
  916. free(utfs);
  917. return ret;
  918. }
  919. void OS_OSX::release_rendering_thread() {
  920. [NSOpenGLContext clearCurrentContext];
  921. }
  922. void OS_OSX::make_rendering_thread() {
  923. [context makeCurrentContext];
  924. }
  925. Error OS_OSX::shell_open(String p_uri) {
  926. [[NSWorkspace sharedWorkspace] openURL:[[NSURL alloc] initWithString:[NSString stringWithUTF8String:p_uri.utf8().get_data()]]];
  927. return OK;
  928. }
  929. void OS_OSX::swap_buffers() {
  930. [context flushBuffer];
  931. }
  932. void OS_OSX::set_video_mode(const VideoMode& p_video_mode,int p_screen) {
  933. }
  934. OS::VideoMode OS_OSX::get_video_mode(int p_screen) const {
  935. return current_videomode;
  936. }
  937. void OS_OSX::get_fullscreen_mode_list(List<VideoMode> *p_list,int p_screen) const {
  938. }
  939. void OS_OSX::move_window_to_foreground() {
  940. [window_object orderFrontRegardless];
  941. }
  942. String OS_OSX::get_executable_path() const {
  943. int ret;
  944. pid_t pid;
  945. char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
  946. pid = getpid();
  947. ret = proc_pidpath (pid, pathbuf, sizeof(pathbuf));
  948. if ( ret <= 0 ) {
  949. return OS::get_executable_path();
  950. } else {
  951. String path;
  952. path.parse_utf8(pathbuf);
  953. return path;
  954. }
  955. }
  956. void OS_OSX::process_events() {
  957. while (true) {
  958. NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
  959. untilDate:[NSDate distantPast]
  960. inMode:NSDefaultRunLoopMode
  961. dequeue:YES];
  962. if (event == nil)
  963. break;
  964. [NSApp sendEvent:event];
  965. }
  966. [autoreleasePool drain];
  967. autoreleasePool = [[NSAutoreleasePool alloc] init];
  968. }
  969. void OS_OSX::push_input(const InputEvent& p_event) {
  970. InputEvent ev=p_event;
  971. ev.ID=last_id++;
  972. //print_line("EV: "+String(ev));
  973. input->parse_input_event(ev);
  974. }
  975. void OS_OSX::run() {
  976. force_quit = false;
  977. if (!main_loop)
  978. return;
  979. main_loop->init();
  980. // uint64_t last_ticks=get_ticks_usec();
  981. // int frames=0;
  982. // uint64_t frame=0;
  983. while (!force_quit) {
  984. process_events(); // get rid of pending events
  985. // process_joysticks();
  986. if (Main::iteration()==true)
  987. break;
  988. };
  989. main_loop->finish();
  990. }
  991. OS_OSX* OS_OSX::singleton=NULL;
  992. OS_OSX::OS_OSX() {
  993. main_loop=NULL;
  994. singleton=this;
  995. autoreleasePool = [[NSAutoreleasePool alloc] init];
  996. eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
  997. ERR_FAIL_COND(!eventSource);
  998. CGEventSourceSetLocalEventsSuppressionInterval(eventSource, 0.0);
  999. /*if (pthread_key_create(&_Godot.nsgl.current, NULL) != 0)
  1000. {
  1001. _GodotInputError(Godot_PLATFORM_ERROR,
  1002. "NSGL: Failed to create context TLS");
  1003. return GL_FALSE;
  1004. }*/
  1005. framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl"));
  1006. ERR_FAIL_COND(!framework);
  1007. // Implicitly create shared NSApplication instance
  1008. [GodotApplication sharedApplication];
  1009. // In case we are unbundled, make us a proper UI application
  1010. [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
  1011. #if 0
  1012. // Menu bar setup must go between sharedApplication above and
  1013. // finishLaunching below, in order to properly emulate the behavior
  1014. // of NSApplicationMain
  1015. createMenuBar();
  1016. #endif
  1017. [NSApp finishLaunching];
  1018. delegate = [[GodotApplicationDelegate alloc] init];
  1019. ERR_FAIL_COND(!delegate);
  1020. [NSApp setDelegate:delegate];
  1021. last_id=1;
  1022. cursor_shape=CURSOR_ARROW;
  1023. }