display_server_osx.mm 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604
  1. /*************************************************************************/
  2. /* display_server_osx.mm */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "display_server_osx.h"
  31. #include "os_osx.h"
  32. #include "core/io/marshalls.h"
  33. #include "core/os/keyboard.h"
  34. #include "main/main.h"
  35. #include "scene/resources/texture.h"
  36. #include <Carbon/Carbon.h>
  37. #include <Cocoa/Cocoa.h>
  38. #include <IOKit/IOCFPlugIn.h>
  39. #include <IOKit/IOKitLib.h>
  40. #include <IOKit/hid/IOHIDKeys.h>
  41. #include <IOKit/hid/IOHIDLib.h>
  42. #if defined(OPENGL_ENABLED)
  43. #include "drivers/gles2/rasterizer_gles2.h"
  44. //TODO - reimplement OpenGLES
  45. #endif
  46. #if defined(VULKAN_ENABLED)
  47. #include "servers/rendering/rasterizer_rd/rasterizer_rd.h"
  48. #include <QuartzCore/CAMetalLayer.h>
  49. #endif
  50. #ifndef NSAppKitVersionNumber10_14
  51. #define NSAppKitVersionNumber10_14 1671
  52. #endif
  53. #define DS_OSX ((DisplayServerOSX *)(DisplayServerOSX::get_singleton()))
  54. static void _get_key_modifier_state(unsigned int p_osx_state, Ref<InputEventWithModifiers> r_state) {
  55. r_state->set_shift((p_osx_state & NSEventModifierFlagShift));
  56. r_state->set_control((p_osx_state & NSEventModifierFlagControl));
  57. r_state->set_alt((p_osx_state & NSEventModifierFlagOption));
  58. r_state->set_metakey((p_osx_state & NSEventModifierFlagCommand));
  59. }
  60. static Vector2i _get_mouse_pos(DisplayServerOSX::WindowData &p_wd, NSPoint p_locationInWindow, CGFloat p_backingScaleFactor) {
  61. const NSRect contentRect = [p_wd.window_view frame];
  62. const NSPoint p = p_locationInWindow;
  63. p_wd.mouse_pos.x = p.x * p_backingScaleFactor;
  64. p_wd.mouse_pos.y = (contentRect.size.height - p.y) * p_backingScaleFactor;
  65. DS_OSX->last_mouse_pos = p_wd.mouse_pos;
  66. Input::get_singleton()->set_mouse_position(p_wd.mouse_pos);
  67. return p_wd.mouse_pos;
  68. }
  69. static void _push_to_key_event_buffer(const DisplayServerOSX::KeyEvent &p_event) {
  70. Vector<DisplayServerOSX::KeyEvent> &buffer = DS_OSX->key_event_buffer;
  71. if (DS_OSX->key_event_pos >= buffer.size()) {
  72. buffer.resize(1 + DS_OSX->key_event_pos);
  73. }
  74. buffer.write[DS_OSX->key_event_pos++] = p_event;
  75. }
  76. static NSCursor *_cursorFromSelector(SEL selector, SEL fallback = nil) {
  77. if ([NSCursor respondsToSelector:selector]) {
  78. id object = [NSCursor performSelector:selector];
  79. if ([object isKindOfClass:[NSCursor class]]) {
  80. return object;
  81. }
  82. }
  83. if (fallback) {
  84. // Fallback should be a reasonable default, no need to check.
  85. return [NSCursor performSelector:fallback];
  86. }
  87. return [NSCursor arrowCursor];
  88. }
  89. /*************************************************************************/
  90. /* GodotApplication */
  91. /*************************************************************************/
  92. @interface GodotApplication : NSApplication
  93. @end
  94. @implementation GodotApplication
  95. - (void)sendEvent:(NSEvent *)event {
  96. // special case handling of command-period, which is traditionally a special
  97. // shortcut in macOS and doesn't arrive at our regular keyDown handler.
  98. if ([event type] == NSEventTypeKeyDown) {
  99. if (([event modifierFlags] & NSEventModifierFlagCommand) && [event keyCode] == 0x2f) {
  100. Ref<InputEventKey> k;
  101. k.instance();
  102. _get_key_modifier_state([event modifierFlags], k);
  103. k->set_window_id(DisplayServerOSX::INVALID_WINDOW_ID);
  104. k->set_pressed(true);
  105. k->set_keycode(KEY_PERIOD);
  106. k->set_physical_keycode(KEY_PERIOD);
  107. k->set_echo([event isARepeat]);
  108. Input::get_singleton()->accumulate_input_event(k);
  109. }
  110. }
  111. // From http://cocoadev.com/index.pl?GameKeyboardHandlingAlmost
  112. // This works around an AppKit bug, where key up events while holding
  113. // down the command key don't get sent to the key window.
  114. if ([event type] == NSEventTypeKeyUp && ([event modifierFlags] & NSEventModifierFlagCommand))
  115. [[self keyWindow] sendEvent:event];
  116. else
  117. [super sendEvent:event];
  118. }
  119. @end
  120. /*************************************************************************/
  121. /* GlobalMenuItem */
  122. /*************************************************************************/
  123. @interface GlobalMenuItem : NSObject {
  124. @public
  125. Callable callback;
  126. Variant meta;
  127. bool checkable;
  128. }
  129. @end
  130. @implementation GlobalMenuItem
  131. @end
  132. /*************************************************************************/
  133. /* GodotApplicationDelegate */
  134. /*************************************************************************/
  135. @interface GodotApplicationDelegate : NSObject
  136. - (void)forceUnbundledWindowActivationHackStep1;
  137. - (void)forceUnbundledWindowActivationHackStep2;
  138. - (void)forceUnbundledWindowActivationHackStep3;
  139. @end
  140. @implementation GodotApplicationDelegate
  141. - (void)forceUnbundledWindowActivationHackStep1 {
  142. // Step1: Switch focus to macOS Dock.
  143. // Required to perform step 2, TransformProcessType will fail if app is already the in focus.
  144. for (NSRunningApplication *app in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.dock"]) {
  145. [app activateWithOptions:NSApplicationActivateIgnoringOtherApps];
  146. break;
  147. }
  148. [self performSelector:@selector(forceUnbundledWindowActivationHackStep2) withObject:nil afterDelay:0.02];
  149. }
  150. - (void)forceUnbundledWindowActivationHackStep2 {
  151. // Step 2: Register app as foreground process.
  152. ProcessSerialNumber psn = { 0, kCurrentProcess };
  153. (void)TransformProcessType(&psn, kProcessTransformToForegroundApplication);
  154. [self performSelector:@selector(forceUnbundledWindowActivationHackStep3) withObject:nil afterDelay:0.02];
  155. }
  156. - (void)forceUnbundledWindowActivationHackStep3 {
  157. // Step 3: Switch focus back to app window.
  158. [[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps];
  159. }
  160. - (void)applicationDidFinishLaunching:(NSNotification *)notice {
  161. NSString *nsappname = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
  162. if (nsappname == nil) {
  163. // If executable is not a bundled, macOS WindowServer won't register and activate app window correctly (menu and title bar are grayed out and input ignored).
  164. [self performSelector:@selector(forceUnbundledWindowActivationHackStep1) withObject:nil afterDelay:0.02];
  165. }
  166. }
  167. - (void)globalMenuCallback:(id)sender {
  168. if (![sender representedObject])
  169. return;
  170. GlobalMenuItem *value = [sender representedObject];
  171. if (value) {
  172. if (value->checkable) {
  173. if ([sender state] == NSControlStateValueOff) {
  174. [sender setState:NSControlStateValueOn];
  175. } else {
  176. [sender setState:NSControlStateValueOff];
  177. }
  178. }
  179. if (value->callback != Callable()) {
  180. Variant tag = value->meta;
  181. Variant *tagp = &tag;
  182. Variant ret;
  183. Callable::CallError ce;
  184. value->callback.call((const Variant **)&tagp, 1, ret, ce);
  185. }
  186. }
  187. }
  188. - (NSMenu *)applicationDockMenu:(NSApplication *)sender {
  189. return DS_OSX->dock_menu;
  190. }
  191. - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
  192. // Note: may be called called before main loop init!
  193. char *utfs = strdup([filename UTF8String]);
  194. ((OS_OSX *)(OS_OSX::get_singleton()))->open_with_filename.parse_utf8(utfs);
  195. free(utfs);
  196. #ifdef TOOLS_ENABLED
  197. // Open new instance
  198. if (OS_OSX::get_singleton()->get_main_loop()) {
  199. List<String> args;
  200. args.push_back(((OS_OSX *)(OS_OSX::get_singleton()))->open_with_filename);
  201. String exec = OS::get_singleton()->get_executable_path();
  202. OS::ProcessID pid = 0;
  203. OS::get_singleton()->execute(exec, args, false, &pid);
  204. }
  205. #endif
  206. return YES;
  207. }
  208. - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
  209. DS_OSX->_send_window_event(DS_OSX->windows[DisplayServerOSX::MAIN_WINDOW_ID], DisplayServerOSX::WINDOW_EVENT_CLOSE_REQUEST);
  210. return NSTerminateCancel;
  211. }
  212. - (void)showAbout:(id)sender {
  213. if (OS_OSX::get_singleton()->get_main_loop())
  214. OS_OSX::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_ABOUT);
  215. }
  216. @end
  217. /*************************************************************************/
  218. /* GodotWindowDelegate */
  219. /*************************************************************************/
  220. @interface GodotWindowDelegate : NSObject {
  221. DisplayServerOSX::WindowID window_id;
  222. }
  223. - (void)windowWillClose:(NSNotification *)notification;
  224. - (void)setWindowID:(DisplayServerOSX::WindowID)wid;
  225. @end
  226. @implementation GodotWindowDelegate
  227. - (void)setWindowID:(DisplayServerOSX::WindowID)wid {
  228. window_id = wid;
  229. }
  230. - (BOOL)windowShouldClose:(id)sender {
  231. ERR_FAIL_COND_V(!DS_OSX->windows.has(window_id), YES);
  232. DS_OSX->_send_window_event(DS_OSX->windows[window_id], DisplayServerOSX::WINDOW_EVENT_CLOSE_REQUEST);
  233. return NO;
  234. }
  235. - (void)windowWillClose:(NSNotification *)notification {
  236. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  237. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  238. while (wd.transient_children.size()) {
  239. DS_OSX->window_set_transient(wd.transient_children.front()->get(), DisplayServerOSX::INVALID_WINDOW_ID);
  240. }
  241. DS_OSX->windows.erase(window_id);
  242. if (wd.transient_parent != DisplayServerOSX::INVALID_WINDOW_ID) {
  243. DisplayServerOSX::WindowData &pwd = DS_OSX->windows[wd.transient_parent];
  244. [pwd.window_object makeKeyAndOrderFront:nil]; // Move focus back to parent.
  245. DS_OSX->window_set_transient(window_id, DisplayServerOSX::INVALID_WINDOW_ID);
  246. } else if ((window_id != DisplayServerOSX::MAIN_WINDOW_ID) && (DS_OSX->windows.size() == 1)) {
  247. DisplayServerOSX::WindowData &pwd = DS_OSX->windows[DisplayServerOSX::MAIN_WINDOW_ID];
  248. [pwd.window_object makeKeyAndOrderFront:nil]; // Move focus back to main window if there is no parent or other windows left.
  249. }
  250. #ifdef VULKAN_ENABLED
  251. if (DS_OSX->rendering_driver == "vulkan") {
  252. DS_OSX->context_vulkan->window_destroy(window_id);
  253. }
  254. #endif
  255. }
  256. - (void)windowDidEnterFullScreen:(NSNotification *)notification {
  257. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  258. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  259. wd.fullscreen = true;
  260. [wd.window_object setContentMinSize:NSMakeSize(0, 0)];
  261. [wd.window_object setContentMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
  262. }
  263. - (void)windowDidExitFullScreen:(NSNotification *)notification {
  264. if (!DS_OSX || !DS_OSX->windows.has(window_id))
  265. return;
  266. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  267. wd.fullscreen = false;
  268. if (wd.min_size != Size2i()) {
  269. Size2i size = wd.min_size / DS_OSX->_display_scale([wd.window_object screen]);
  270. [wd.window_object setContentMinSize:NSMakeSize(size.x, size.y)];
  271. }
  272. if (wd.max_size != Size2i()) {
  273. Size2i size = wd.max_size / DS_OSX->_display_scale([wd.window_object screen]);
  274. [wd.window_object setContentMaxSize:NSMakeSize(size.x, size.y)];
  275. }
  276. if (wd.resize_disabled)
  277. [wd.window_object setStyleMask:[wd.window_object styleMask] & ~NSWindowStyleMaskResizable];
  278. }
  279. - (void)windowDidChangeBackingProperties:(NSNotification *)notification {
  280. if (!DisplayServerOSX::get_singleton())
  281. return;
  282. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  283. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  284. CGFloat newBackingScaleFactor = [wd.window_object backingScaleFactor];
  285. CGFloat oldBackingScaleFactor = [[[notification userInfo] objectForKey:@"NSBackingPropertyOldScaleFactorKey"] doubleValue];
  286. #if defined(OPENGL_ENABLED)
  287. if (DS_OSX->rendering_driver == "opengl_es") {
  288. //TODO - reimplement OpenGLES
  289. if (OS_OSX::get_singleton()->is_hidpi_allowed()) {
  290. [wd.window_view setWantsBestResolutionOpenGLSurface:YES];
  291. } else {
  292. [wd.window_view setWantsBestResolutionOpenGLSurface:NO];
  293. }
  294. }
  295. #endif
  296. if (newBackingScaleFactor != oldBackingScaleFactor) {
  297. //Set new display scale and window size
  298. float newDisplayScale = OS_OSX::get_singleton()->is_hidpi_allowed() ? newBackingScaleFactor : 1.0;
  299. const NSRect contentRect = [wd.window_view frame];
  300. wd.size.width = contentRect.size.width * newDisplayScale;
  301. wd.size.height = contentRect.size.height * newDisplayScale;
  302. DS_OSX->_send_window_event(wd, DisplayServerOSX::WINDOW_EVENT_DPI_CHANGE);
  303. #if defined(VULKAN_ENABLED)
  304. if (DS_OSX->rendering_driver == "vulkan") {
  305. CALayer *layer = [wd.window_view layer];
  306. layer.contentsScale = DS_OSX->_display_scale([wd.window_object screen]);
  307. }
  308. #endif
  309. //Force window resize event
  310. [self windowDidResize:notification];
  311. }
  312. }
  313. - (void)windowDidResize:(NSNotification *)notification {
  314. if (!DS_OSX || !DS_OSX->windows.has(window_id))
  315. return;
  316. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  317. #if defined(OPENGL_ENABLED)
  318. if (DS_OSX->rendering_driver == "opengl_es") {
  319. //TODO - reimplement OpenGLES
  320. wd.context_gles2->update();
  321. }
  322. #endif
  323. const NSRect contentRect = [wd.window_view frame];
  324. float displayScale = DS_OSX->_display_scale([wd.window_object screen]);
  325. wd.size.width = contentRect.size.width * displayScale;
  326. wd.size.height = contentRect.size.height * displayScale;
  327. #if defined(VULKAN_ENABLED)
  328. if (DS_OSX->rendering_driver == "vulkan") {
  329. CALayer *layer = [wd.window_view layer];
  330. layer.contentsScale = displayScale;
  331. DS_OSX->context_vulkan->window_resize(window_id, wd.size.width, wd.size.height);
  332. }
  333. #endif
  334. if (!wd.rect_changed_callback.is_null()) {
  335. Variant size = Rect2i(DS_OSX->window_get_position(window_id), DS_OSX->window_get_size(window_id));
  336. Variant *sizep = &size;
  337. Variant ret;
  338. Callable::CallError ce;
  339. wd.rect_changed_callback.call((const Variant **)&sizep, 1, ret, ce);
  340. }
  341. if (OS_OSX::get_singleton()->get_main_loop()) {
  342. Main::force_redraw();
  343. //Event retrieval blocks until resize is over. Call Main::iteration() directly.
  344. if (!Main::is_iterating()) { //avoid cyclic loop
  345. Main::iteration();
  346. }
  347. }
  348. }
  349. - (void)windowDidMove:(NSNotification *)notification {
  350. DS_OSX->_release_pressed_events();
  351. }
  352. - (void)windowDidBecomeKey:(NSNotification *)notification {
  353. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  354. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  355. const CGFloat backingScaleFactor = (OS::get_singleton()->is_hidpi_allowed()) ? [wd.window_view backingScaleFactor] : 1.0;
  356. _get_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream], backingScaleFactor);
  357. Input::get_singleton()->set_mouse_position(wd.mouse_pos);
  358. DS_OSX->window_focused = true;
  359. DS_OSX->_send_window_event(wd, DisplayServerOSX::WINDOW_EVENT_FOCUS_IN);
  360. }
  361. - (void)windowDidResignKey:(NSNotification *)notification {
  362. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  363. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  364. DS_OSX->window_focused = false;
  365. DS_OSX->_release_pressed_events();
  366. DS_OSX->_send_window_event(wd, DisplayServerOSX::WINDOW_EVENT_FOCUS_OUT);
  367. }
  368. - (void)windowDidMiniaturize:(NSNotification *)notification {
  369. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  370. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  371. DS_OSX->window_focused = false;
  372. DS_OSX->_release_pressed_events();
  373. DS_OSX->_send_window_event(wd, DisplayServerOSX::WINDOW_EVENT_FOCUS_OUT);
  374. }
  375. - (void)windowDidDeminiaturize:(NSNotification *)notification {
  376. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  377. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  378. DS_OSX->window_focused = true;
  379. DS_OSX->_send_window_event(wd, DisplayServerOSX::WINDOW_EVENT_FOCUS_IN);
  380. }
  381. @end
  382. /*************************************************************************/
  383. /* GodotContentView */
  384. /*************************************************************************/
  385. @interface GodotContentView : NSView <NSTextInputClient> {
  386. DisplayServerOSX::WindowID window_id;
  387. NSTrackingArea *trackingArea;
  388. NSMutableAttributedString *markedText;
  389. bool imeInputEventInProgress;
  390. }
  391. - (void)cancelComposition;
  392. - (CALayer *)makeBackingLayer;
  393. - (BOOL)wantsUpdateLayer;
  394. - (void)updateLayer;
  395. - (void)setWindowID:(DisplayServerOSX::WindowID)wid;
  396. @end
  397. @implementation GodotContentView
  398. - (void)setWindowID:(DisplayServerOSX::WindowID)wid {
  399. window_id = wid;
  400. }
  401. + (void)initialize {
  402. if (self == [GodotContentView class]) {
  403. // nothing left to do here at the moment..
  404. }
  405. }
  406. - (CALayer *)makeBackingLayer {
  407. #if defined(VULKAN_ENABLED)
  408. if (DS_OSX->rendering_driver == "vulkan") {
  409. CALayer *layer = [[CAMetalLayer class] layer];
  410. return layer;
  411. }
  412. #endif
  413. return [super makeBackingLayer];
  414. }
  415. - (void)updateLayer {
  416. #if defined(VULKAN_ENABLED)
  417. if (DS_OSX->rendering_driver == "vulkan") {
  418. [super updateLayer];
  419. }
  420. #endif
  421. #if defined(OPENGL_ENABLED)
  422. if (DS_OSX->rendering_driver == "opengl_es") {
  423. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  424. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  425. wd.context_gles2->update();
  426. //TODO - reimplement OpenGLES
  427. }
  428. #endif
  429. }
  430. - (BOOL)wantsUpdateLayer {
  431. return YES;
  432. }
  433. - (id)init {
  434. self = [super init];
  435. trackingArea = nil;
  436. imeInputEventInProgress = false;
  437. [self updateTrackingAreas];
  438. [self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
  439. markedText = [[NSMutableAttributedString alloc] init];
  440. return self;
  441. }
  442. - (void)dealloc {
  443. [trackingArea release];
  444. [markedText release];
  445. [super dealloc];
  446. }
  447. static const NSRange kEmptyRange = { NSNotFound, 0 };
  448. - (BOOL)hasMarkedText {
  449. return (markedText.length > 0);
  450. }
  451. - (NSRange)markedRange {
  452. return NSMakeRange(0, markedText.length);
  453. }
  454. - (NSRange)selectedRange {
  455. return kEmptyRange;
  456. }
  457. - (void)setMarkedText:(id)aString selectedRange:(NSRange)selectedRange replacementRange:(NSRange)replacementRange {
  458. if ([aString isKindOfClass:[NSAttributedString class]]) {
  459. [markedText initWithAttributedString:aString];
  460. } else {
  461. [markedText initWithString:aString];
  462. }
  463. if (markedText.length == 0) {
  464. [self unmarkText];
  465. return;
  466. }
  467. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  468. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  469. if (wd.im_active) {
  470. imeInputEventInProgress = true;
  471. DS_OSX->im_text.parse_utf8([[markedText mutableString] UTF8String]);
  472. DS_OSX->im_selection = Point2i(selectedRange.location, selectedRange.length);
  473. OS_OSX::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_IME_UPDATE);
  474. }
  475. }
  476. - (void)doCommandBySelector:(SEL)aSelector {
  477. if ([self respondsToSelector:aSelector])
  478. [self performSelector:aSelector];
  479. }
  480. - (void)unmarkText {
  481. imeInputEventInProgress = false;
  482. [[markedText mutableString] setString:@""];
  483. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  484. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  485. if (wd.im_active) {
  486. DS_OSX->im_text = String();
  487. DS_OSX->im_selection = Point2i();
  488. OS_OSX::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_IME_UPDATE);
  489. }
  490. }
  491. - (NSArray *)validAttributesForMarkedText {
  492. return [NSArray array];
  493. }
  494. - (NSAttributedString *)attributedSubstringForProposedRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange {
  495. return nil;
  496. }
  497. - (NSUInteger)characterIndexForPoint:(NSPoint)aPoint {
  498. return 0;
  499. }
  500. - (NSRect)firstRectForCharacterRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange {
  501. ERR_FAIL_COND_V(!DS_OSX->windows.has(window_id), NSMakeRect(0, 0, 0, 0));
  502. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  503. const NSRect contentRect = [wd.window_view frame];
  504. float displayScale = DS_OSX->_display_scale([wd.window_object screen]);
  505. NSRect pointInWindowRect = NSMakeRect(wd.im_position.x / displayScale, contentRect.size.height - (wd.im_position.y / displayScale) - 1, 0, 0);
  506. NSPoint pointOnScreen = [wd.window_object convertRectToScreen:pointInWindowRect].origin;
  507. return NSMakeRect(pointOnScreen.x, pointOnScreen.y, 0, 0);
  508. }
  509. - (void)cancelComposition {
  510. [self unmarkText];
  511. NSTextInputContext *currentInputContext = [NSTextInputContext currentInputContext];
  512. [currentInputContext discardMarkedText];
  513. }
  514. - (void)insertText:(id)aString {
  515. [self insertText:aString replacementRange:NSMakeRange(0, 0)];
  516. }
  517. - (void)insertText:(id)aString replacementRange:(NSRange)replacementRange {
  518. NSEvent *event = [NSApp currentEvent];
  519. NSString *characters;
  520. if ([aString isKindOfClass:[NSAttributedString class]]) {
  521. characters = [aString string];
  522. } else {
  523. characters = (NSString *)aString;
  524. }
  525. NSUInteger i, length = [characters length];
  526. NSCharacterSet *ctrlChars = [NSCharacterSet controlCharacterSet];
  527. NSCharacterSet *wsnlChars = [NSCharacterSet whitespaceAndNewlineCharacterSet];
  528. if ([characters rangeOfCharacterFromSet:ctrlChars].length && [characters rangeOfCharacterFromSet:wsnlChars].length == 0) {
  529. NSTextInputContext *currentInputContext = [NSTextInputContext currentInputContext];
  530. [currentInputContext discardMarkedText];
  531. [self cancelComposition];
  532. return;
  533. }
  534. for (i = 0; i < length; i++) {
  535. const unichar codepoint = [characters characterAtIndex:i];
  536. if ((codepoint & 0xFF00) == 0xF700)
  537. continue;
  538. DisplayServerOSX::KeyEvent ke;
  539. ke.window_id = window_id;
  540. ke.osx_state = [event modifierFlags];
  541. ke.pressed = true;
  542. ke.echo = false;
  543. ke.raw = false; // IME input event
  544. ke.keycode = 0;
  545. ke.physical_keycode = 0;
  546. ke.unicode = codepoint;
  547. _push_to_key_event_buffer(ke);
  548. }
  549. [self cancelComposition];
  550. }
  551. - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
  552. return NSDragOperationCopy;
  553. }
  554. - (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender {
  555. return NSDragOperationCopy;
  556. }
  557. - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
  558. ERR_FAIL_COND_V(!DS_OSX->windows.has(window_id), NO);
  559. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  560. NSPasteboard *pboard = [sender draggingPasteboard];
  561. NSArray *filenames = [pboard propertyListForType:NSFilenamesPboardType];
  562. Vector<String> files;
  563. for (NSUInteger i = 0; i < filenames.count; i++) {
  564. NSString *ns = [filenames objectAtIndex:i];
  565. char *utfs = strdup([ns UTF8String]);
  566. String ret;
  567. ret.parse_utf8(utfs);
  568. free(utfs);
  569. files.push_back(ret);
  570. }
  571. if (!wd.drop_files_callback.is_null()) {
  572. Variant v = files;
  573. Variant *vp = &v;
  574. Variant ret;
  575. Callable::CallError ce;
  576. wd.drop_files_callback.call((const Variant **)&vp, 1, ret, ce);
  577. }
  578. return NO;
  579. }
  580. - (BOOL)isOpaque {
  581. return YES;
  582. }
  583. - (BOOL)canBecomeKeyView {
  584. return YES;
  585. }
  586. - (BOOL)acceptsFirstResponder {
  587. return YES;
  588. }
  589. - (void)cursorUpdate:(NSEvent *)event {
  590. DisplayServer::CursorShape p_shape = DS_OSX->cursor_shape;
  591. DS_OSX->cursor_shape = DisplayServer::CURSOR_MAX;
  592. DS_OSX->cursor_set_shape(p_shape);
  593. }
  594. static void _mouseDownEvent(DisplayServer::WindowID window_id, NSEvent *event, int index, int mask, bool pressed) {
  595. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  596. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  597. if (pressed) {
  598. DS_OSX->last_button_state |= mask;
  599. } else {
  600. DS_OSX->last_button_state &= ~mask;
  601. }
  602. Ref<InputEventMouseButton> mb;
  603. mb.instance();
  604. mb->set_window_id(window_id);
  605. const CGFloat backingScaleFactor = (OS::get_singleton()->is_hidpi_allowed()) ? [[event window] backingScaleFactor] : 1.0;
  606. const Vector2 pos = _get_mouse_pos(wd, [event locationInWindow], backingScaleFactor);
  607. _get_key_modifier_state([event modifierFlags], mb);
  608. mb->set_button_index(index);
  609. mb->set_pressed(pressed);
  610. mb->set_position(pos);
  611. mb->set_global_position(pos);
  612. mb->set_button_mask(DS_OSX->last_button_state);
  613. if (index == BUTTON_LEFT && pressed) {
  614. mb->set_doubleclick([event clickCount] == 2);
  615. }
  616. Input::get_singleton()->accumulate_input_event(mb);
  617. }
  618. - (void)mouseDown:(NSEvent *)event {
  619. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  620. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  621. if (([event modifierFlags] & NSEventModifierFlagControl)) {
  622. wd.mouse_down_control = true;
  623. _mouseDownEvent(window_id, event, BUTTON_RIGHT, BUTTON_MASK_RIGHT, true);
  624. } else {
  625. wd.mouse_down_control = false;
  626. _mouseDownEvent(window_id, event, BUTTON_LEFT, BUTTON_MASK_LEFT, true);
  627. }
  628. }
  629. - (void)mouseDragged:(NSEvent *)event {
  630. [self mouseMoved:event];
  631. }
  632. - (void)mouseUp:(NSEvent *)event {
  633. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  634. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  635. if (wd.mouse_down_control) {
  636. _mouseDownEvent(window_id, event, BUTTON_RIGHT, BUTTON_MASK_RIGHT, false);
  637. } else {
  638. _mouseDownEvent(window_id, event, BUTTON_LEFT, BUTTON_MASK_LEFT, false);
  639. }
  640. }
  641. - (void)mouseMoved:(NSEvent *)event {
  642. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  643. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  644. Ref<InputEventMouseMotion> mm;
  645. mm.instance();
  646. mm->set_window_id(window_id);
  647. mm->set_button_mask(DS_OSX->last_button_state);
  648. const CGFloat backingScaleFactor = (OS::get_singleton()->is_hidpi_allowed()) ? [[event window] backingScaleFactor] : 1.0;
  649. const Vector2i pos = _get_mouse_pos(wd, [event locationInWindow], backingScaleFactor);
  650. mm->set_position(pos);
  651. mm->set_pressure([event pressure]);
  652. if ([event subtype] == NSEventSubtypeTabletPoint) {
  653. const NSPoint p = [event tilt];
  654. mm->set_tilt(Vector2(p.x, p.y));
  655. }
  656. mm->set_global_position(pos);
  657. mm->set_speed(Input::get_singleton()->get_last_mouse_speed());
  658. Vector2i relativeMotion = Vector2i();
  659. relativeMotion.x = [event deltaX] * backingScaleFactor;
  660. relativeMotion.y = [event deltaY] * backingScaleFactor;
  661. mm->set_relative(relativeMotion);
  662. _get_key_modifier_state([event modifierFlags], mm);
  663. Input::get_singleton()->set_mouse_position(wd.mouse_pos);
  664. Input::get_singleton()->accumulate_input_event(mm);
  665. }
  666. - (void)rightMouseDown:(NSEvent *)event {
  667. _mouseDownEvent(window_id, event, BUTTON_RIGHT, BUTTON_MASK_RIGHT, true);
  668. }
  669. - (void)rightMouseDragged:(NSEvent *)event {
  670. [self mouseMoved:event];
  671. }
  672. - (void)rightMouseUp:(NSEvent *)event {
  673. _mouseDownEvent(window_id, event, BUTTON_RIGHT, BUTTON_MASK_RIGHT, false);
  674. }
  675. - (void)otherMouseDown:(NSEvent *)event {
  676. if ((int)[event buttonNumber] == 2) {
  677. _mouseDownEvent(window_id, event, BUTTON_MIDDLE, BUTTON_MASK_MIDDLE, true);
  678. } else if ((int)[event buttonNumber] == 3) {
  679. _mouseDownEvent(window_id, event, BUTTON_XBUTTON1, BUTTON_MASK_XBUTTON1, true);
  680. } else if ((int)[event buttonNumber] == 4) {
  681. _mouseDownEvent(window_id, event, BUTTON_XBUTTON2, BUTTON_MASK_XBUTTON2, true);
  682. } else {
  683. return;
  684. }
  685. }
  686. - (void)otherMouseDragged:(NSEvent *)event {
  687. [self mouseMoved:event];
  688. }
  689. - (void)otherMouseUp:(NSEvent *)event {
  690. if ((int)[event buttonNumber] == 2) {
  691. _mouseDownEvent(window_id, event, BUTTON_MIDDLE, BUTTON_MASK_MIDDLE, false);
  692. } else if ((int)[event buttonNumber] == 3) {
  693. _mouseDownEvent(window_id, event, BUTTON_XBUTTON1, BUTTON_MASK_XBUTTON1, false);
  694. } else if ((int)[event buttonNumber] == 4) {
  695. _mouseDownEvent(window_id, event, BUTTON_XBUTTON2, BUTTON_MASK_XBUTTON2, false);
  696. } else {
  697. return;
  698. }
  699. }
  700. - (void)mouseExited:(NSEvent *)event {
  701. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  702. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  703. if (DS_OSX->mouse_mode != DisplayServer::MOUSE_MODE_CAPTURED)
  704. DS_OSX->_send_window_event(wd, DisplayServerOSX::WINDOW_EVENT_MOUSE_EXIT);
  705. }
  706. - (void)mouseEntered:(NSEvent *)event {
  707. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  708. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  709. if (DS_OSX->mouse_mode != DisplayServer::MOUSE_MODE_CAPTURED)
  710. DS_OSX->_send_window_event(wd, DisplayServerOSX::WINDOW_EVENT_MOUSE_ENTER);
  711. DisplayServer::CursorShape p_shape = DS_OSX->cursor_shape;
  712. DS_OSX->cursor_shape = DisplayServer::CURSOR_MAX;
  713. DS_OSX->cursor_set_shape(p_shape);
  714. }
  715. - (void)magnifyWithEvent:(NSEvent *)event {
  716. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  717. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  718. Ref<InputEventMagnifyGesture> ev;
  719. ev.instance();
  720. ev->set_window_id(window_id);
  721. _get_key_modifier_state([event modifierFlags], ev);
  722. const CGFloat backingScaleFactor = (OS::get_singleton()->is_hidpi_allowed()) ? [[event window] backingScaleFactor] : 1.0;
  723. ev->set_position(_get_mouse_pos(wd, [event locationInWindow], backingScaleFactor));
  724. ev->set_factor([event magnification] + 1.0);
  725. Input::get_singleton()->accumulate_input_event(ev);
  726. }
  727. - (void)viewDidChangeBackingProperties {
  728. // nothing left to do here
  729. }
  730. - (void)updateTrackingAreas {
  731. if (trackingArea != nil) {
  732. [self removeTrackingArea:trackingArea];
  733. [trackingArea release];
  734. }
  735. NSTrackingAreaOptions options =
  736. NSTrackingMouseEnteredAndExited |
  737. NSTrackingActiveInKeyWindow |
  738. NSTrackingCursorUpdate |
  739. NSTrackingInVisibleRect;
  740. trackingArea = [[NSTrackingArea alloc]
  741. initWithRect:[self bounds]
  742. options:options
  743. owner:self
  744. userInfo:nil];
  745. [self addTrackingArea:trackingArea];
  746. [super updateTrackingAreas];
  747. }
  748. static bool isNumpadKey(unsigned int key) {
  749. static const unsigned int table[] = {
  750. 0x41, /* kVK_ANSI_KeypadDecimal */
  751. 0x43, /* kVK_ANSI_KeypadMultiply */
  752. 0x45, /* kVK_ANSI_KeypadPlus */
  753. 0x47, /* kVK_ANSI_KeypadClear */
  754. 0x4b, /* kVK_ANSI_KeypadDivide */
  755. 0x4c, /* kVK_ANSI_KeypadEnter */
  756. 0x4e, /* kVK_ANSI_KeypadMinus */
  757. 0x51, /* kVK_ANSI_KeypadEquals */
  758. 0x52, /* kVK_ANSI_Keypad0 */
  759. 0x53, /* kVK_ANSI_Keypad1 */
  760. 0x54, /* kVK_ANSI_Keypad2 */
  761. 0x55, /* kVK_ANSI_Keypad3 */
  762. 0x56, /* kVK_ANSI_Keypad4 */
  763. 0x57, /* kVK_ANSI_Keypad5 */
  764. 0x58, /* kVK_ANSI_Keypad6 */
  765. 0x59, /* kVK_ANSI_Keypad7 */
  766. 0x5b, /* kVK_ANSI_Keypad8 */
  767. 0x5c, /* kVK_ANSI_Keypad9 */
  768. 0x5f, /* kVK_JIS_KeypadComma */
  769. 0x00
  770. };
  771. for (int i = 0; table[i] != 0; i++) {
  772. if (key == table[i])
  773. return true;
  774. }
  775. return false;
  776. }
  777. // Translates a OS X keycode to a Godot keycode
  778. //
  779. static int translateKey(unsigned int key) {
  780. // Keyboard symbol translation table
  781. static const unsigned int table[128] = {
  782. /* 00 */ KEY_A,
  783. /* 01 */ KEY_S,
  784. /* 02 */ KEY_D,
  785. /* 03 */ KEY_F,
  786. /* 04 */ KEY_H,
  787. /* 05 */ KEY_G,
  788. /* 06 */ KEY_Z,
  789. /* 07 */ KEY_X,
  790. /* 08 */ KEY_C,
  791. /* 09 */ KEY_V,
  792. /* 0a */ KEY_SECTION, /* ISO Section */
  793. /* 0b */ KEY_B,
  794. /* 0c */ KEY_Q,
  795. /* 0d */ KEY_W,
  796. /* 0e */ KEY_E,
  797. /* 0f */ KEY_R,
  798. /* 10 */ KEY_Y,
  799. /* 11 */ KEY_T,
  800. /* 12 */ KEY_1,
  801. /* 13 */ KEY_2,
  802. /* 14 */ KEY_3,
  803. /* 15 */ KEY_4,
  804. /* 16 */ KEY_6,
  805. /* 17 */ KEY_5,
  806. /* 18 */ KEY_EQUAL,
  807. /* 19 */ KEY_9,
  808. /* 1a */ KEY_7,
  809. /* 1b */ KEY_MINUS,
  810. /* 1c */ KEY_8,
  811. /* 1d */ KEY_0,
  812. /* 1e */ KEY_BRACERIGHT,
  813. /* 1f */ KEY_O,
  814. /* 20 */ KEY_U,
  815. /* 21 */ KEY_BRACELEFT,
  816. /* 22 */ KEY_I,
  817. /* 23 */ KEY_P,
  818. /* 24 */ KEY_ENTER,
  819. /* 25 */ KEY_L,
  820. /* 26 */ KEY_J,
  821. /* 27 */ KEY_APOSTROPHE,
  822. /* 28 */ KEY_K,
  823. /* 29 */ KEY_SEMICOLON,
  824. /* 2a */ KEY_BACKSLASH,
  825. /* 2b */ KEY_COMMA,
  826. /* 2c */ KEY_SLASH,
  827. /* 2d */ KEY_N,
  828. /* 2e */ KEY_M,
  829. /* 2f */ KEY_PERIOD,
  830. /* 30 */ KEY_TAB,
  831. /* 31 */ KEY_SPACE,
  832. /* 32 */ KEY_QUOTELEFT,
  833. /* 33 */ KEY_BACKSPACE,
  834. /* 34 */ KEY_UNKNOWN,
  835. /* 35 */ KEY_ESCAPE,
  836. /* 36 */ KEY_META,
  837. /* 37 */ KEY_META,
  838. /* 38 */ KEY_SHIFT,
  839. /* 39 */ KEY_CAPSLOCK,
  840. /* 3a */ KEY_ALT,
  841. /* 3b */ KEY_CONTROL,
  842. /* 3c */ KEY_SHIFT,
  843. /* 3d */ KEY_ALT,
  844. /* 3e */ KEY_CONTROL,
  845. /* 3f */ KEY_UNKNOWN, /* Function */
  846. /* 40 */ KEY_UNKNOWN, /* F17 */
  847. /* 41 */ KEY_KP_PERIOD,
  848. /* 42 */ KEY_UNKNOWN,
  849. /* 43 */ KEY_KP_MULTIPLY,
  850. /* 44 */ KEY_UNKNOWN,
  851. /* 45 */ KEY_KP_ADD,
  852. /* 46 */ KEY_UNKNOWN,
  853. /* 47 */ KEY_NUMLOCK, /* Really KeypadClear... */
  854. /* 48 */ KEY_VOLUMEUP, /* VolumeUp */
  855. /* 49 */ KEY_VOLUMEDOWN, /* VolumeDown */
  856. /* 4a */ KEY_VOLUMEMUTE, /* Mute */
  857. /* 4b */ KEY_KP_DIVIDE,
  858. /* 4c */ KEY_KP_ENTER,
  859. /* 4d */ KEY_UNKNOWN,
  860. /* 4e */ KEY_KP_SUBTRACT,
  861. /* 4f */ KEY_UNKNOWN, /* F18 */
  862. /* 50 */ KEY_UNKNOWN, /* F19 */
  863. /* 51 */ KEY_EQUAL, /* KeypadEqual */
  864. /* 52 */ KEY_KP_0,
  865. /* 53 */ KEY_KP_1,
  866. /* 54 */ KEY_KP_2,
  867. /* 55 */ KEY_KP_3,
  868. /* 56 */ KEY_KP_4,
  869. /* 57 */ KEY_KP_5,
  870. /* 58 */ KEY_KP_6,
  871. /* 59 */ KEY_KP_7,
  872. /* 5a */ KEY_UNKNOWN, /* F20 */
  873. /* 5b */ KEY_KP_8,
  874. /* 5c */ KEY_KP_9,
  875. /* 5d */ KEY_YEN, /* JIS Yen */
  876. /* 5e */ KEY_UNDERSCORE, /* JIS Underscore */
  877. /* 5f */ KEY_COMMA, /* JIS KeypadComma */
  878. /* 60 */ KEY_F5,
  879. /* 61 */ KEY_F6,
  880. /* 62 */ KEY_F7,
  881. /* 63 */ KEY_F3,
  882. /* 64 */ KEY_F8,
  883. /* 65 */ KEY_F9,
  884. /* 66 */ KEY_UNKNOWN, /* JIS Eisu */
  885. /* 67 */ KEY_F11,
  886. /* 68 */ KEY_UNKNOWN, /* JIS Kana */
  887. /* 69 */ KEY_F13,
  888. /* 6a */ KEY_F16,
  889. /* 6b */ KEY_F14,
  890. /* 6c */ KEY_UNKNOWN,
  891. /* 6d */ KEY_F10,
  892. /* 6e */ KEY_MENU,
  893. /* 6f */ KEY_F12,
  894. /* 70 */ KEY_UNKNOWN,
  895. /* 71 */ KEY_F15,
  896. /* 72 */ KEY_INSERT, /* Really Help... */
  897. /* 73 */ KEY_HOME,
  898. /* 74 */ KEY_PAGEUP,
  899. /* 75 */ KEY_DELETE,
  900. /* 76 */ KEY_F4,
  901. /* 77 */ KEY_END,
  902. /* 78 */ KEY_F2,
  903. /* 79 */ KEY_PAGEDOWN,
  904. /* 7a */ KEY_F1,
  905. /* 7b */ KEY_LEFT,
  906. /* 7c */ KEY_RIGHT,
  907. /* 7d */ KEY_DOWN,
  908. /* 7e */ KEY_UP,
  909. /* 7f */ KEY_UNKNOWN,
  910. };
  911. if (key >= 128)
  912. return KEY_UNKNOWN;
  913. return table[key];
  914. }
  915. struct _KeyCodeMap {
  916. UniChar kchar;
  917. int kcode;
  918. };
  919. static const _KeyCodeMap _keycodes[55] = {
  920. { '`', KEY_QUOTELEFT },
  921. { '~', KEY_ASCIITILDE },
  922. { '0', KEY_0 },
  923. { '1', KEY_1 },
  924. { '2', KEY_2 },
  925. { '3', KEY_3 },
  926. { '4', KEY_4 },
  927. { '5', KEY_5 },
  928. { '6', KEY_6 },
  929. { '7', KEY_7 },
  930. { '8', KEY_8 },
  931. { '9', KEY_9 },
  932. { '-', KEY_MINUS },
  933. { '_', KEY_UNDERSCORE },
  934. { '=', KEY_EQUAL },
  935. { '+', KEY_PLUS },
  936. { 'q', KEY_Q },
  937. { 'w', KEY_W },
  938. { 'e', KEY_E },
  939. { 'r', KEY_R },
  940. { 't', KEY_T },
  941. { 'y', KEY_Y },
  942. { 'u', KEY_U },
  943. { 'i', KEY_I },
  944. { 'o', KEY_O },
  945. { 'p', KEY_P },
  946. { '[', KEY_BRACELEFT },
  947. { ']', KEY_BRACERIGHT },
  948. { '{', KEY_BRACELEFT },
  949. { '}', KEY_BRACERIGHT },
  950. { 'a', KEY_A },
  951. { 's', KEY_S },
  952. { 'd', KEY_D },
  953. { 'f', KEY_F },
  954. { 'g', KEY_G },
  955. { 'h', KEY_H },
  956. { 'j', KEY_J },
  957. { 'k', KEY_K },
  958. { 'l', KEY_L },
  959. { ';', KEY_SEMICOLON },
  960. { ':', KEY_COLON },
  961. { '\'', KEY_APOSTROPHE },
  962. { '\"', KEY_QUOTEDBL },
  963. { '\\', KEY_BACKSLASH },
  964. { '#', KEY_NUMBERSIGN },
  965. { 'z', KEY_Z },
  966. { 'x', KEY_X },
  967. { 'c', KEY_C },
  968. { 'v', KEY_V },
  969. { 'b', KEY_B },
  970. { 'n', KEY_N },
  971. { 'm', KEY_M },
  972. { ',', KEY_COMMA },
  973. { '.', KEY_PERIOD },
  974. { '/', KEY_SLASH }
  975. };
  976. static int remapKey(unsigned int key, unsigned int state) {
  977. if (isNumpadKey(key))
  978. return translateKey(key);
  979. TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();
  980. if (!currentKeyboard)
  981. return translateKey(key);
  982. CFDataRef layoutData = (CFDataRef)TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData);
  983. if (!layoutData)
  984. return translateKey(key);
  985. const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout *)CFDataGetBytePtr(layoutData);
  986. UInt32 keysDown = 0;
  987. UniChar chars[4];
  988. UniCharCount realLength;
  989. OSStatus err = UCKeyTranslate(keyboardLayout,
  990. key,
  991. kUCKeyActionDisplay,
  992. (state >> 8) & 0xFF,
  993. LMGetKbdType(),
  994. kUCKeyTranslateNoDeadKeysBit,
  995. &keysDown,
  996. sizeof(chars) / sizeof(chars[0]),
  997. &realLength,
  998. chars);
  999. if (err != noErr) {
  1000. return translateKey(key);
  1001. }
  1002. for (unsigned int i = 0; i < 55; i++) {
  1003. if (_keycodes[i].kchar == chars[0]) {
  1004. return _keycodes[i].kcode;
  1005. }
  1006. }
  1007. return translateKey(key);
  1008. }
  1009. - (void)keyDown:(NSEvent *)event {
  1010. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  1011. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  1012. // Ignore all input if IME input is in progress
  1013. if (!imeInputEventInProgress) {
  1014. NSString *characters = [event characters];
  1015. NSUInteger length = [characters length];
  1016. if (!wd.im_active && length > 0 && keycode_has_unicode(remapKey([event keyCode], [event modifierFlags]))) {
  1017. // Fallback unicode character handler used if IME is not active
  1018. for (NSUInteger i = 0; i < length; i++) {
  1019. DisplayServerOSX::KeyEvent ke;
  1020. ke.window_id = window_id;
  1021. ke.osx_state = [event modifierFlags];
  1022. ke.pressed = true;
  1023. ke.echo = [event isARepeat];
  1024. ke.keycode = remapKey([event keyCode], [event modifierFlags]);
  1025. ke.physical_keycode = translateKey([event keyCode]);
  1026. ke.raw = true;
  1027. ke.unicode = [characters characterAtIndex:i];
  1028. _push_to_key_event_buffer(ke);
  1029. }
  1030. } else {
  1031. DisplayServerOSX::KeyEvent ke;
  1032. ke.window_id = window_id;
  1033. ke.osx_state = [event modifierFlags];
  1034. ke.pressed = true;
  1035. ke.echo = [event isARepeat];
  1036. ke.keycode = remapKey([event keyCode], [event modifierFlags]);
  1037. ke.physical_keycode = translateKey([event keyCode]);
  1038. ke.raw = false;
  1039. ke.unicode = 0;
  1040. _push_to_key_event_buffer(ke);
  1041. }
  1042. }
  1043. // Pass events to IME handler
  1044. if (wd.im_active)
  1045. [self interpretKeyEvents:[NSArray arrayWithObject:event]];
  1046. }
  1047. - (void)flagsChanged:(NSEvent *)event {
  1048. // Ignore all input if IME input is in progress
  1049. if (!imeInputEventInProgress) {
  1050. DisplayServerOSX::KeyEvent ke;
  1051. ke.window_id = window_id;
  1052. ke.echo = false;
  1053. ke.raw = true;
  1054. int key = [event keyCode];
  1055. int mod = [event modifierFlags];
  1056. if (key == 0x36 || key == 0x37) {
  1057. if (mod & NSEventModifierFlagCommand) {
  1058. mod &= ~NSEventModifierFlagCommand;
  1059. ke.pressed = true;
  1060. } else {
  1061. ke.pressed = false;
  1062. }
  1063. } else if (key == 0x38 || key == 0x3c) {
  1064. if (mod & NSEventModifierFlagShift) {
  1065. mod &= ~NSEventModifierFlagShift;
  1066. ke.pressed = true;
  1067. } else {
  1068. ke.pressed = false;
  1069. }
  1070. } else if (key == 0x3a || key == 0x3d) {
  1071. if (mod & NSEventModifierFlagOption) {
  1072. mod &= ~NSEventModifierFlagOption;
  1073. ke.pressed = true;
  1074. } else {
  1075. ke.pressed = false;
  1076. }
  1077. } else if (key == 0x3b || key == 0x3e) {
  1078. if (mod & NSEventModifierFlagControl) {
  1079. mod &= ~NSEventModifierFlagControl;
  1080. ke.pressed = true;
  1081. } else {
  1082. ke.pressed = false;
  1083. }
  1084. } else {
  1085. return;
  1086. }
  1087. ke.osx_state = mod;
  1088. ke.keycode = remapKey(key, mod);
  1089. ke.physical_keycode = translateKey(key);
  1090. ke.unicode = 0;
  1091. _push_to_key_event_buffer(ke);
  1092. }
  1093. }
  1094. - (void)keyUp:(NSEvent *)event {
  1095. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  1096. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  1097. // Ignore all input if IME input is in progress
  1098. if (!imeInputEventInProgress) {
  1099. NSString *characters = [event characters];
  1100. NSUInteger length = [characters length];
  1101. // Fallback unicode character handler used if IME is not active
  1102. if (!wd.im_active && length > 0 && keycode_has_unicode(remapKey([event keyCode], [event modifierFlags]))) {
  1103. for (NSUInteger i = 0; i < length; i++) {
  1104. DisplayServerOSX::KeyEvent ke;
  1105. ke.window_id = window_id;
  1106. ke.osx_state = [event modifierFlags];
  1107. ke.pressed = false;
  1108. ke.echo = [event isARepeat];
  1109. ke.keycode = remapKey([event keyCode], [event modifierFlags]);
  1110. ke.physical_keycode = translateKey([event keyCode]);
  1111. ke.raw = true;
  1112. ke.unicode = [characters characterAtIndex:i];
  1113. _push_to_key_event_buffer(ke);
  1114. }
  1115. } else {
  1116. DisplayServerOSX::KeyEvent ke;
  1117. ke.window_id = window_id;
  1118. ke.osx_state = [event modifierFlags];
  1119. ke.pressed = false;
  1120. ke.echo = [event isARepeat];
  1121. ke.keycode = remapKey([event keyCode], [event modifierFlags]);
  1122. ke.physical_keycode = translateKey([event keyCode]);
  1123. ke.raw = true;
  1124. ke.unicode = 0;
  1125. _push_to_key_event_buffer(ke);
  1126. }
  1127. }
  1128. }
  1129. inline void sendScrollEvent(DisplayServer::WindowID window_id, int button, double factor, int modifierFlags) {
  1130. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  1131. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  1132. unsigned int mask = 1 << (button - 1);
  1133. Ref<InputEventMouseButton> sc;
  1134. sc.instance();
  1135. sc->set_window_id(window_id);
  1136. _get_key_modifier_state(modifierFlags, sc);
  1137. sc->set_button_index(button);
  1138. sc->set_factor(factor);
  1139. sc->set_pressed(true);
  1140. sc->set_position(wd.mouse_pos);
  1141. sc->set_global_position(wd.mouse_pos);
  1142. DS_OSX->last_button_state |= mask;
  1143. sc->set_button_mask(DS_OSX->last_button_state);
  1144. Input::get_singleton()->accumulate_input_event(sc);
  1145. sc.instance();
  1146. sc->set_window_id(window_id);
  1147. sc->set_button_index(button);
  1148. sc->set_factor(factor);
  1149. sc->set_pressed(false);
  1150. sc->set_position(wd.mouse_pos);
  1151. sc->set_global_position(wd.mouse_pos);
  1152. DS_OSX->last_button_state &= ~mask;
  1153. sc->set_button_mask(DS_OSX->last_button_state);
  1154. Input::get_singleton()->accumulate_input_event(sc);
  1155. }
  1156. inline void sendPanEvent(DisplayServer::WindowID window_id, double dx, double dy, int modifierFlags) {
  1157. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  1158. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  1159. Ref<InputEventPanGesture> pg;
  1160. pg.instance();
  1161. pg->set_window_id(window_id);
  1162. _get_key_modifier_state(modifierFlags, pg);
  1163. pg->set_position(wd.mouse_pos);
  1164. pg->set_delta(Vector2(-dx, -dy));
  1165. Input::get_singleton()->accumulate_input_event(pg);
  1166. }
  1167. - (void)scrollWheel:(NSEvent *)event {
  1168. ERR_FAIL_COND(!DS_OSX->windows.has(window_id));
  1169. DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
  1170. double deltaX, deltaY;
  1171. const CGFloat backingScaleFactor = (OS::get_singleton()->is_hidpi_allowed()) ? [[event window] backingScaleFactor] : 1.0;
  1172. _get_mouse_pos(wd, [event locationInWindow], backingScaleFactor);
  1173. deltaX = [event scrollingDeltaX];
  1174. deltaY = [event scrollingDeltaY];
  1175. if ([event hasPreciseScrollingDeltas]) {
  1176. deltaX *= 0.03;
  1177. deltaY *= 0.03;
  1178. }
  1179. if ([event phase] != NSEventPhaseNone || [event momentumPhase] != NSEventPhaseNone) {
  1180. sendPanEvent(window_id, deltaX, deltaY, [event modifierFlags]);
  1181. } else {
  1182. if (fabs(deltaX)) {
  1183. sendScrollEvent(window_id, 0 > deltaX ? BUTTON_WHEEL_RIGHT : BUTTON_WHEEL_LEFT, fabs(deltaX * 0.3), [event modifierFlags]);
  1184. }
  1185. if (fabs(deltaY)) {
  1186. sendScrollEvent(window_id, 0 < deltaY ? BUTTON_WHEEL_UP : BUTTON_WHEEL_DOWN, fabs(deltaY * 0.3), [event modifierFlags]);
  1187. }
  1188. }
  1189. }
  1190. @end
  1191. /*************************************************************************/
  1192. /* GodotWindow */
  1193. /*************************************************************************/
  1194. @interface GodotWindow : NSWindow {
  1195. }
  1196. @end
  1197. @implementation GodotWindow
  1198. - (BOOL)canBecomeKeyWindow {
  1199. // Required for NSBorderlessWindowMask windows
  1200. return YES;
  1201. }
  1202. @end
  1203. /*************************************************************************/
  1204. /* DisplayServerOSX */
  1205. /*************************************************************************/
  1206. bool DisplayServerOSX::has_feature(Feature p_feature) const {
  1207. switch (p_feature) {
  1208. case FEATURE_GLOBAL_MENU:
  1209. case FEATURE_SUBWINDOWS:
  1210. //case FEATURE_TOUCHSCREEN:
  1211. case FEATURE_MOUSE:
  1212. case FEATURE_MOUSE_WARP:
  1213. case FEATURE_CLIPBOARD:
  1214. case FEATURE_CURSOR_SHAPE:
  1215. case FEATURE_CUSTOM_CURSOR_SHAPE:
  1216. case FEATURE_NATIVE_DIALOG:
  1217. //case FEATURE_CONSOLE_WINDOW:
  1218. case FEATURE_IME:
  1219. case FEATURE_WINDOW_TRANSPARENCY:
  1220. case FEATURE_HIDPI:
  1221. case FEATURE_ICON:
  1222. case FEATURE_NATIVE_ICON:
  1223. case FEATURE_SWAP_BUFFERS:
  1224. return true;
  1225. default: {
  1226. }
  1227. }
  1228. return false;
  1229. }
  1230. String DisplayServerOSX::get_name() const {
  1231. return "OSX";
  1232. }
  1233. const NSMenu *DisplayServerOSX::_get_menu_root(const String &p_menu_root) const {
  1234. const NSMenu *menu = NULL;
  1235. if (p_menu_root == "") {
  1236. // Main menu.x
  1237. menu = [NSApp mainMenu];
  1238. } else if (p_menu_root.to_lower() == "_dock") {
  1239. // macOS dock menu.
  1240. menu = dock_menu;
  1241. } else {
  1242. // Submenu.
  1243. if (submenu.has(p_menu_root)) {
  1244. menu = submenu[p_menu_root];
  1245. }
  1246. }
  1247. if (menu == apple_menu) {
  1248. // Do not allow to change Apple menu.
  1249. return NULL;
  1250. }
  1251. return menu;
  1252. }
  1253. NSMenu *DisplayServerOSX::_get_menu_root(const String &p_menu_root) {
  1254. NSMenu *menu = NULL;
  1255. if (p_menu_root == "") {
  1256. // Main menu.
  1257. menu = [NSApp mainMenu];
  1258. } else if (p_menu_root.to_lower() == "_dock") {
  1259. // macOS dock menu.
  1260. menu = dock_menu;
  1261. } else {
  1262. // Submenu.
  1263. if (!submenu.has(p_menu_root)) {
  1264. NSMenu *n_menu = [[NSMenu alloc] initWithTitle:[NSString stringWithUTF8String:p_menu_root.utf8().get_data()]];
  1265. submenu[p_menu_root] = n_menu;
  1266. }
  1267. menu = submenu[p_menu_root];
  1268. }
  1269. if (menu == apple_menu) {
  1270. // Do not allow to change Apple menu.
  1271. return NULL;
  1272. }
  1273. return menu;
  1274. }
  1275. void DisplayServerOSX::global_menu_add_item(const String &p_menu_root, const String &p_label, const Callable &p_callback, const Variant &p_tag) {
  1276. _THREAD_SAFE_METHOD_
  1277. NSMenu *menu = _get_menu_root(p_menu_root);
  1278. if (menu) {
  1279. NSMenuItem *menu_item = [menu addItemWithTitle:[NSString stringWithUTF8String:p_label.utf8().get_data()] action:@selector(globalMenuCallback:) keyEquivalent:@""];
  1280. GlobalMenuItem *obj = [[[GlobalMenuItem alloc] init] autorelease];
  1281. obj->callback = p_callback;
  1282. obj->meta = p_tag;
  1283. obj->checkable = false;
  1284. [menu_item setRepresentedObject:obj];
  1285. }
  1286. }
  1287. void DisplayServerOSX::global_menu_add_check_item(const String &p_menu_root, const String &p_label, const Callable &p_callback, const Variant &p_tag) {
  1288. _THREAD_SAFE_METHOD_
  1289. NSMenu *menu = _get_menu_root(p_menu_root);
  1290. if (menu) {
  1291. NSMenuItem *menu_item = [menu addItemWithTitle:[NSString stringWithUTF8String:p_label.utf8().get_data()] action:@selector(globalMenuCallback:) keyEquivalent:@""];
  1292. GlobalMenuItem *obj = [[[GlobalMenuItem alloc] init] autorelease];
  1293. obj->callback = p_callback;
  1294. obj->meta = p_tag;
  1295. obj->checkable = true;
  1296. [menu_item setRepresentedObject:obj];
  1297. }
  1298. }
  1299. void DisplayServerOSX::global_menu_add_submenu_item(const String &p_menu_root, const String &p_label, const String &p_submenu) {
  1300. _THREAD_SAFE_METHOD_
  1301. NSMenu *menu = _get_menu_root(p_menu_root);
  1302. NSMenu *sub_menu = _get_menu_root(p_submenu);
  1303. if (menu && sub_menu) {
  1304. if (sub_menu == menu) {
  1305. ERR_PRINT("Can't set submenu to self!");
  1306. return;
  1307. }
  1308. if ([sub_menu supermenu]) {
  1309. ERR_PRINT("Can't set submenu to menu that is already a submenu of some other menu!");
  1310. return;
  1311. }
  1312. NSMenuItem *menu_item = [menu addItemWithTitle:[NSString stringWithUTF8String:p_label.utf8().get_data()] action:nil keyEquivalent:@""];
  1313. [menu setSubmenu:sub_menu forItem:menu_item];
  1314. }
  1315. }
  1316. void DisplayServerOSX::global_menu_add_separator(const String &p_menu_root) {
  1317. _THREAD_SAFE_METHOD_
  1318. NSMenu *menu = _get_menu_root(p_menu_root);
  1319. if (menu) {
  1320. [menu addItem:[NSMenuItem separatorItem]];
  1321. }
  1322. }
  1323. bool DisplayServerOSX::global_menu_is_item_checked(const String &p_menu_root, int p_idx) const {
  1324. _THREAD_SAFE_METHOD_
  1325. const NSMenu *menu = _get_menu_root(p_menu_root);
  1326. if (menu) {
  1327. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1328. if (menu_item) {
  1329. return ([menu_item state] == NSControlStateValueOn);
  1330. }
  1331. }
  1332. return false;
  1333. }
  1334. bool DisplayServerOSX::global_menu_is_item_checkable(const String &p_menu_root, int p_idx) const {
  1335. _THREAD_SAFE_METHOD_
  1336. const NSMenu *menu = _get_menu_root(p_menu_root);
  1337. if (menu) {
  1338. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1339. if (menu_item) {
  1340. GlobalMenuItem *obj = [menu_item representedObject];
  1341. if (obj) {
  1342. return obj->checkable;
  1343. }
  1344. }
  1345. }
  1346. return false;
  1347. }
  1348. Callable DisplayServerOSX::global_menu_get_item_callback(const String &p_menu_root, int p_idx) {
  1349. _THREAD_SAFE_METHOD_
  1350. const NSMenu *menu = _get_menu_root(p_menu_root);
  1351. if (menu) {
  1352. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1353. if (menu_item) {
  1354. GlobalMenuItem *obj = [menu_item representedObject];
  1355. if (obj) {
  1356. return obj->callback;
  1357. }
  1358. }
  1359. }
  1360. return Callable();
  1361. }
  1362. Variant DisplayServerOSX::global_menu_get_item_tag(const String &p_menu_root, int p_idx) {
  1363. _THREAD_SAFE_METHOD_
  1364. const NSMenu *menu = _get_menu_root(p_menu_root);
  1365. if (menu) {
  1366. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1367. if (menu_item) {
  1368. GlobalMenuItem *obj = [menu_item representedObject];
  1369. if (obj) {
  1370. return obj->meta;
  1371. }
  1372. }
  1373. }
  1374. return Variant();
  1375. }
  1376. String DisplayServerOSX::global_menu_get_item_text(const String &p_menu_root, int p_idx) {
  1377. _THREAD_SAFE_METHOD_
  1378. const NSMenu *menu = _get_menu_root(p_menu_root);
  1379. if (menu) {
  1380. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1381. if (menu_item) {
  1382. char *utfs = strdup([[menu_item title] UTF8String]);
  1383. String ret;
  1384. ret.parse_utf8(utfs);
  1385. free(utfs);
  1386. return ret;
  1387. }
  1388. }
  1389. return String();
  1390. }
  1391. String DisplayServerOSX::global_menu_get_item_submenu(const String &p_menu_root, int p_idx) {
  1392. _THREAD_SAFE_METHOD_
  1393. const NSMenu *menu = _get_menu_root(p_menu_root);
  1394. if (menu) {
  1395. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1396. if (menu_item) {
  1397. const NSMenu *sub_menu = [menu_item submenu];
  1398. if (sub_menu) {
  1399. for (Map<String, NSMenu *>::Element *E = submenu.front(); E; E = E->next()) {
  1400. if (E->get() == sub_menu)
  1401. return E->key();
  1402. }
  1403. }
  1404. }
  1405. }
  1406. return String();
  1407. }
  1408. void DisplayServerOSX::global_menu_set_item_checked(const String &p_menu_root, int p_idx, bool p_checked) {
  1409. _THREAD_SAFE_METHOD_
  1410. NSMenu *menu = _get_menu_root(p_menu_root);
  1411. if (menu) {
  1412. if ((menu == [NSApp mainMenu]) && (p_idx == 0)) { // Do not edit Apple menu.
  1413. return;
  1414. }
  1415. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1416. if (menu_item) {
  1417. if (p_checked) {
  1418. [menu_item setState:NSControlStateValueOn];
  1419. } else {
  1420. [menu_item setState:NSControlStateValueOff];
  1421. }
  1422. }
  1423. }
  1424. }
  1425. void DisplayServerOSX::global_menu_set_item_checkable(const String &p_menu_root, int p_idx, bool p_checkable) {
  1426. _THREAD_SAFE_METHOD_
  1427. NSMenu *menu = _get_menu_root(p_menu_root);
  1428. if (menu) {
  1429. if ((menu == [NSApp mainMenu]) && (p_idx == 0)) { // Do not edit Apple menu.
  1430. return;
  1431. }
  1432. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1433. if (menu_item) {
  1434. GlobalMenuItem *obj = [menu_item representedObject];
  1435. obj->checkable = p_checkable;
  1436. }
  1437. }
  1438. }
  1439. void DisplayServerOSX::global_menu_set_item_callback(const String &p_menu_root, int p_idx, const Callable &p_callback) {
  1440. _THREAD_SAFE_METHOD_
  1441. NSMenu *menu = _get_menu_root(p_menu_root);
  1442. if (menu) {
  1443. if ((menu == [NSApp mainMenu]) && (p_idx == 0)) { // Do not edit Apple menu.
  1444. return;
  1445. }
  1446. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1447. if (menu_item) {
  1448. GlobalMenuItem *obj = [menu_item representedObject];
  1449. obj->callback = p_callback;
  1450. }
  1451. }
  1452. }
  1453. void DisplayServerOSX::global_menu_set_item_tag(const String &p_menu_root, int p_idx, const Variant &p_tag) {
  1454. _THREAD_SAFE_METHOD_
  1455. NSMenu *menu = _get_menu_root(p_menu_root);
  1456. if (menu) {
  1457. if ((menu == [NSApp mainMenu]) && (p_idx == 0)) { // Do not edit Apple menu.
  1458. return;
  1459. }
  1460. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1461. if (menu_item) {
  1462. GlobalMenuItem *obj = [menu_item representedObject];
  1463. obj->meta = p_tag;
  1464. }
  1465. }
  1466. }
  1467. void DisplayServerOSX::global_menu_set_item_text(const String &p_menu_root, int p_idx, const String &p_text) {
  1468. _THREAD_SAFE_METHOD_
  1469. NSMenu *menu = _get_menu_root(p_menu_root);
  1470. if (menu) {
  1471. if ((menu == [NSApp mainMenu]) && (p_idx == 0)) { // Do not edit Apple menu.
  1472. return;
  1473. }
  1474. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1475. if (menu_item) {
  1476. [menu_item setTitle:[NSString stringWithUTF8String:p_text.utf8().get_data()]];
  1477. }
  1478. }
  1479. }
  1480. void DisplayServerOSX::global_menu_set_item_submenu(const String &p_menu_root, int p_idx, const String &p_submenu) {
  1481. _THREAD_SAFE_METHOD_
  1482. NSMenu *menu = _get_menu_root(p_menu_root);
  1483. NSMenu *sub_menu = _get_menu_root(p_submenu);
  1484. if (menu && sub_menu) {
  1485. if (sub_menu == menu) {
  1486. ERR_PRINT("Can't set submenu to self!");
  1487. return;
  1488. }
  1489. if ([sub_menu supermenu]) {
  1490. ERR_PRINT("Can't set submenu to menu that is already a submenu of some other menu!");
  1491. return;
  1492. }
  1493. if ((menu == [NSApp mainMenu]) && (p_idx == 0)) { // Do not edit Apple menu.
  1494. return;
  1495. }
  1496. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1497. if (menu_item) {
  1498. [menu setSubmenu:sub_menu forItem:menu_item];
  1499. }
  1500. }
  1501. }
  1502. int DisplayServerOSX::global_menu_get_item_count(const String &p_menu_root) const {
  1503. _THREAD_SAFE_METHOD_
  1504. const NSMenu *menu = _get_menu_root(p_menu_root);
  1505. if (menu) {
  1506. return [menu numberOfItems];
  1507. } else {
  1508. return 0;
  1509. }
  1510. }
  1511. void DisplayServerOSX::global_menu_remove_item(const String &p_menu_root, int p_idx) {
  1512. _THREAD_SAFE_METHOD_
  1513. NSMenu *menu = _get_menu_root(p_menu_root);
  1514. if (menu) {
  1515. if ((menu == [NSApp mainMenu]) && (p_idx == 0)) { // Do not delete Apple menu.
  1516. return;
  1517. }
  1518. [menu removeItemAtIndex:p_idx];
  1519. }
  1520. }
  1521. void DisplayServerOSX::global_menu_clear(const String &p_menu_root) {
  1522. _THREAD_SAFE_METHOD_
  1523. NSMenu *menu = _get_menu_root(p_menu_root);
  1524. if (menu) {
  1525. [menu removeAllItems];
  1526. // Restore Apple menu.
  1527. if (menu == [NSApp mainMenu]) {
  1528. NSMenuItem *menu_item = [menu addItemWithTitle:@"" action:nil keyEquivalent:@""];
  1529. [menu setSubmenu:apple_menu forItem:menu_item];
  1530. }
  1531. }
  1532. }
  1533. void DisplayServerOSX::alert(const String &p_alert, const String &p_title) {
  1534. _THREAD_SAFE_METHOD_
  1535. NSAlert *window = [[NSAlert alloc] init];
  1536. NSString *ns_title = [NSString stringWithUTF8String:p_title.utf8().get_data()];
  1537. NSString *ns_alert = [NSString stringWithUTF8String:p_alert.utf8().get_data()];
  1538. [window addButtonWithTitle:@"OK"];
  1539. [window setMessageText:ns_title];
  1540. [window setInformativeText:ns_alert];
  1541. [window setAlertStyle:NSAlertStyleWarning];
  1542. [window runModal];
  1543. [window release];
  1544. }
  1545. Error DisplayServerOSX::dialog_show(String p_title, String p_description, Vector<String> p_buttons, const Callable &p_callback) {
  1546. _THREAD_SAFE_METHOD_
  1547. NSAlert *window = [[NSAlert alloc] init];
  1548. NSString *ns_title = [NSString stringWithUTF8String:p_title.utf8().get_data()];
  1549. NSString *ns_description = [NSString stringWithUTF8String:p_description.utf8().get_data()];
  1550. for (int i = 0; i < p_buttons.size(); i++) {
  1551. NSString *ns_button = [NSString stringWithUTF8String:p_buttons[i].utf8().get_data()];
  1552. [window addButtonWithTitle:ns_button];
  1553. }
  1554. [window setMessageText:ns_title];
  1555. [window setInformativeText:ns_description];
  1556. [window setAlertStyle:NSAlertStyleInformational];
  1557. int button_pressed;
  1558. NSInteger ret = [window runModal];
  1559. if (ret == NSAlertFirstButtonReturn) {
  1560. button_pressed = 0;
  1561. } else if (ret == NSAlertSecondButtonReturn) {
  1562. button_pressed = 1;
  1563. } else if (ret == NSAlertThirdButtonReturn) {
  1564. button_pressed = 2;
  1565. } else {
  1566. button_pressed = 2 + (ret - NSAlertThirdButtonReturn);
  1567. }
  1568. if (!p_callback.is_null()) {
  1569. Variant button = button_pressed;
  1570. Variant *buttonp = &button;
  1571. Variant ret;
  1572. Callable::CallError ce;
  1573. p_callback.call((const Variant **)&buttonp, 1, ret, ce);
  1574. }
  1575. [window release];
  1576. return OK;
  1577. }
  1578. Error DisplayServerOSX::dialog_input_text(String p_title, String p_description, String p_partial, const Callable &p_callback) {
  1579. _THREAD_SAFE_METHOD_
  1580. NSAlert *window = [[NSAlert alloc] init];
  1581. NSString *ns_title = [NSString stringWithUTF8String:p_title.utf8().get_data()];
  1582. NSString *ns_description = [NSString stringWithUTF8String:p_description.utf8().get_data()];
  1583. NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 250, 30)];
  1584. [window addButtonWithTitle:@"OK"];
  1585. [window setMessageText:ns_title];
  1586. [window setInformativeText:ns_description];
  1587. [window setAlertStyle:NSAlertStyleInformational];
  1588. [input setStringValue:[NSString stringWithUTF8String:p_partial.utf8().get_data()]];
  1589. [window setAccessoryView:input];
  1590. [window runModal];
  1591. char *utfs = strdup([[input stringValue] UTF8String]);
  1592. String ret;
  1593. ret.parse_utf8(utfs);
  1594. free(utfs);
  1595. if (!p_callback.is_null()) {
  1596. Variant text = ret;
  1597. Variant *textp = &text;
  1598. Variant ret;
  1599. Callable::CallError ce;
  1600. p_callback.call((const Variant **)&textp, 1, ret, ce);
  1601. }
  1602. [window release];
  1603. return OK;
  1604. }
  1605. void DisplayServerOSX::mouse_set_mode(MouseMode p_mode) {
  1606. _THREAD_SAFE_METHOD_
  1607. if (p_mode == mouse_mode)
  1608. return;
  1609. if (p_mode == MOUSE_MODE_CAPTURED) {
  1610. // Apple Docs state that the display parameter is not used.
  1611. // "This parameter is not used. By default, you may pass kCGDirectMainDisplay."
  1612. // https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/Quartz_Services_Ref/Reference/reference.html
  1613. CGDisplayHideCursor(kCGDirectMainDisplay);
  1614. CGAssociateMouseAndMouseCursorPosition(false);
  1615. } else if (p_mode == MOUSE_MODE_HIDDEN) {
  1616. CGDisplayHideCursor(kCGDirectMainDisplay);
  1617. CGAssociateMouseAndMouseCursorPosition(true);
  1618. } else {
  1619. CGDisplayShowCursor(kCGDirectMainDisplay);
  1620. CGAssociateMouseAndMouseCursorPosition(true);
  1621. }
  1622. mouse_mode = p_mode;
  1623. }
  1624. DisplayServer::MouseMode DisplayServerOSX::mouse_get_mode() const {
  1625. return mouse_mode;
  1626. }
  1627. void DisplayServerOSX::mouse_warp_to_position(const Point2i &p_to) {
  1628. _THREAD_SAFE_METHOD_
  1629. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  1630. last_mouse_pos = p_to;
  1631. } else {
  1632. WindowData &wd = windows[MAIN_WINDOW_ID];
  1633. //local point in window coords
  1634. const NSRect contentRect = [wd.window_view frame];
  1635. float displayScale = _display_scale([wd.window_object screen]);
  1636. NSRect pointInWindowRect = NSMakeRect(p_to.x / displayScale, contentRect.size.height - (p_to.y / displayScale) - 1, 0, 0);
  1637. NSPoint pointOnScreen = [[wd.window_view window] convertRectToScreen:pointInWindowRect].origin;
  1638. //point in scren coords
  1639. CGPoint lMouseWarpPos = { pointOnScreen.x, CGDisplayBounds(CGMainDisplayID()).size.height - pointOnScreen.y };
  1640. //do the warping
  1641. CGEventSourceRef lEventRef = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
  1642. CGEventSourceSetLocalEventsSuppressionInterval(lEventRef, 0.0);
  1643. CGAssociateMouseAndMouseCursorPosition(false);
  1644. CGWarpMouseCursorPosition(lMouseWarpPos);
  1645. CGAssociateMouseAndMouseCursorPosition(true);
  1646. }
  1647. }
  1648. Point2i DisplayServerOSX::mouse_get_position() const {
  1649. return last_mouse_pos;
  1650. }
  1651. Point2i DisplayServerOSX::mouse_get_absolute_position() const {
  1652. _THREAD_SAFE_METHOD_
  1653. const NSPoint mouse_pos = [NSEvent mouseLocation];
  1654. for (NSScreen *screen in [NSScreen screens]) {
  1655. NSRect frame = [screen frame];
  1656. if (NSMouseInRect(mouse_pos, frame, NO)) {
  1657. return Vector2i((int)mouse_pos.x, (int)-mouse_pos.y) + _get_screens_origin();
  1658. }
  1659. }
  1660. return Vector2i();
  1661. }
  1662. int DisplayServerOSX::mouse_get_button_state() const {
  1663. return last_button_state;
  1664. }
  1665. void DisplayServerOSX::clipboard_set(const String &p_text) {
  1666. _THREAD_SAFE_METHOD_
  1667. NSString *copiedString = [NSString stringWithUTF8String:p_text.utf8().get_data()];
  1668. NSArray *copiedStringArray = [NSArray arrayWithObject:copiedString];
  1669. NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
  1670. [pasteboard clearContents];
  1671. [pasteboard writeObjects:copiedStringArray];
  1672. }
  1673. String DisplayServerOSX::clipboard_get() const {
  1674. _THREAD_SAFE_METHOD_
  1675. NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
  1676. NSArray *classArray = [NSArray arrayWithObject:[NSString class]];
  1677. NSDictionary *options = [NSDictionary dictionary];
  1678. BOOL ok = [pasteboard canReadObjectForClasses:classArray options:options];
  1679. if (!ok) {
  1680. return "";
  1681. }
  1682. NSArray *objectsToPaste = [pasteboard readObjectsForClasses:classArray options:options];
  1683. NSString *string = [objectsToPaste objectAtIndex:0];
  1684. char *utfs = strdup([string UTF8String]);
  1685. String ret;
  1686. ret.parse_utf8(utfs);
  1687. free(utfs);
  1688. return ret;
  1689. }
  1690. int DisplayServerOSX::get_screen_count() const {
  1691. _THREAD_SAFE_METHOD_
  1692. NSArray *screenArray = [NSScreen screens];
  1693. return [screenArray count];
  1694. }
  1695. // Returns the native top-left screen coordinate of the smallest rectangle
  1696. // that encompasses all screens. Needed in get_screen_position(),
  1697. // window_get_position, and window_set_position()
  1698. // to convert between OS X native screen coordinates and the ones expected by Godot
  1699. static bool displays_arrangement_dirty = true;
  1700. static void displays_arrangement_changed(CGDirectDisplayID display_id, CGDisplayChangeSummaryFlags flags, void *user_info) {
  1701. displays_arrangement_dirty = true;
  1702. }
  1703. float DisplayServerOSX::_display_scale(id screen) const {
  1704. if (OS_OSX::get_singleton()->is_hidpi_allowed()) {
  1705. if ([screen respondsToSelector:@selector(backingScaleFactor)]) {
  1706. return fmax(1.0, [screen backingScaleFactor]);
  1707. }
  1708. }
  1709. return 1.0;
  1710. }
  1711. Point2i DisplayServerOSX::_get_screens_origin() const {
  1712. static Point2i origin;
  1713. if (displays_arrangement_dirty) {
  1714. origin = Point2i();
  1715. for (int i = 0; i < get_screen_count(); i++) {
  1716. Point2i position = _get_native_screen_position(i);
  1717. if (position.x < origin.x) {
  1718. origin.x = position.x;
  1719. }
  1720. if (position.y > origin.y) {
  1721. origin.y = position.y;
  1722. }
  1723. }
  1724. displays_arrangement_dirty = false;
  1725. }
  1726. return origin;
  1727. }
  1728. Point2i DisplayServerOSX::_get_native_screen_position(int p_screen) const {
  1729. NSArray *screenArray = [NSScreen screens];
  1730. if ((NSUInteger)p_screen < [screenArray count]) {
  1731. float display_scale = _display_scale([screenArray objectAtIndex:p_screen]);
  1732. NSRect nsrect = [[screenArray objectAtIndex:p_screen] frame];
  1733. // Return the top-left corner of the screen, for OS X the y starts at the bottom
  1734. return Point2i(nsrect.origin.x, nsrect.origin.y + nsrect.size.height) * display_scale;
  1735. }
  1736. return Point2i();
  1737. }
  1738. Point2i DisplayServerOSX::screen_get_position(int p_screen) const {
  1739. _THREAD_SAFE_METHOD_
  1740. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  1741. p_screen = window_get_current_screen();
  1742. }
  1743. Point2i position = _get_native_screen_position(p_screen) - _get_screens_origin();
  1744. // OS X native y-coordinate relative to _get_screens_origin() is negative,
  1745. // Godot expects a positive value
  1746. position.y *= -1;
  1747. return position;
  1748. }
  1749. Size2i DisplayServerOSX::screen_get_size(int p_screen) const {
  1750. _THREAD_SAFE_METHOD_
  1751. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  1752. p_screen = window_get_current_screen();
  1753. }
  1754. NSArray *screenArray = [NSScreen screens];
  1755. if ((NSUInteger)p_screen < [screenArray count]) {
  1756. float displayScale = _display_scale([screenArray objectAtIndex:p_screen]);
  1757. // Note: Use frame to get the whole screen size
  1758. NSRect nsrect = [[screenArray objectAtIndex:p_screen] frame];
  1759. return Size2i(nsrect.size.width, nsrect.size.height) * displayScale;
  1760. }
  1761. return Size2i();
  1762. }
  1763. int DisplayServerOSX::screen_get_dpi(int p_screen) const {
  1764. _THREAD_SAFE_METHOD_
  1765. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  1766. p_screen = window_get_current_screen();
  1767. }
  1768. NSArray *screenArray = [NSScreen screens];
  1769. if ((NSUInteger)p_screen < [screenArray count]) {
  1770. float displayScale = _display_scale([screenArray objectAtIndex:p_screen]);
  1771. NSDictionary *description = [[screenArray objectAtIndex:p_screen] deviceDescription];
  1772. NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue];
  1773. CGSize displayPhysicalSize = CGDisplayScreenSize(
  1774. [[description objectForKey:@"NSScreenNumber"] unsignedIntValue]);
  1775. return (displayPixelSize.width * 25.4f / displayPhysicalSize.width) * displayScale;
  1776. }
  1777. return 96;
  1778. }
  1779. float DisplayServerOSX::screen_get_scale(int p_screen) const {
  1780. _THREAD_SAFE_METHOD_
  1781. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  1782. p_screen = window_get_current_screen();
  1783. }
  1784. NSArray *screenArray = [NSScreen screens];
  1785. if ((NSUInteger)p_screen < [screenArray count]) {
  1786. return _display_scale([screenArray objectAtIndex:p_screen]);
  1787. }
  1788. return 1.f;
  1789. }
  1790. Rect2i DisplayServerOSX::screen_get_usable_rect(int p_screen) const {
  1791. _THREAD_SAFE_METHOD_
  1792. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  1793. p_screen = window_get_current_screen();
  1794. }
  1795. NSArray *screenArray = [NSScreen screens];
  1796. if ((NSUInteger)p_screen < [screenArray count]) {
  1797. float displayScale = _display_scale([screenArray objectAtIndex:p_screen]);
  1798. NSRect nsrect = [[screenArray objectAtIndex:p_screen] visibleFrame];
  1799. Point2i position = Point2i(nsrect.origin.x, nsrect.origin.y + nsrect.size.height) * displayScale - _get_screens_origin();
  1800. position.y *= -1;
  1801. Size2i size = Size2i(nsrect.size.width, nsrect.size.height) / displayScale;
  1802. return Rect2i(position, size);
  1803. }
  1804. return Rect2i();
  1805. }
  1806. Vector<DisplayServer::WindowID> DisplayServerOSX::get_window_list() const {
  1807. _THREAD_SAFE_METHOD_
  1808. Vector<int> ret;
  1809. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  1810. ret.push_back(E->key());
  1811. }
  1812. return ret;
  1813. }
  1814. DisplayServer::WindowID DisplayServerOSX::create_sub_window(WindowMode p_mode, uint32_t p_flags, const Rect2i &p_rect) {
  1815. _THREAD_SAFE_METHOD_
  1816. WindowID id = _create_window(p_mode, p_rect);
  1817. WindowData &wd = windows[id];
  1818. for (int i = 0; i < WINDOW_FLAG_MAX; i++) {
  1819. if (p_flags & (1 << i)) {
  1820. window_set_flag(WindowFlags(i), true, id);
  1821. }
  1822. }
  1823. [wd.window_object makeKeyAndOrderFront:nil];
  1824. return id;
  1825. }
  1826. void DisplayServerOSX::_send_window_event(const WindowData &wd, WindowEvent p_event) {
  1827. _THREAD_SAFE_METHOD_
  1828. if (!wd.event_callback.is_null()) {
  1829. Variant event = int(p_event);
  1830. Variant *eventp = &event;
  1831. Variant ret;
  1832. Callable::CallError ce;
  1833. wd.event_callback.call((const Variant **)&eventp, 1, ret, ce);
  1834. }
  1835. }
  1836. DisplayServerOSX::WindowID DisplayServerOSX::_find_window_id(id p_window) {
  1837. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  1838. if (E->get().window_object == p_window)
  1839. return E->key();
  1840. }
  1841. return INVALID_WINDOW_ID;
  1842. }
  1843. void DisplayServerOSX::_update_window(WindowData p_wd) {
  1844. bool borderless_full = false;
  1845. if (p_wd.borderless) {
  1846. NSRect frameRect = [p_wd.window_object frame];
  1847. NSRect screenRect = [[p_wd.window_object screen] frame];
  1848. // Check if our window covers up the screen
  1849. if (frameRect.origin.x <= screenRect.origin.x && frameRect.origin.y <= frameRect.origin.y &&
  1850. frameRect.size.width >= screenRect.size.width && frameRect.size.height >= screenRect.size.height) {
  1851. borderless_full = true;
  1852. }
  1853. }
  1854. if (borderless_full) {
  1855. // If the window covers up the screen set the level to above the main menu and hide on deactivate
  1856. [p_wd.window_object setLevel:NSMainMenuWindowLevel + 1];
  1857. [p_wd.window_object setHidesOnDeactivate:YES];
  1858. } else {
  1859. // Reset these when our window is not a borderless window that covers up the screen
  1860. [p_wd.window_object setLevel:NSNormalWindowLevel];
  1861. [p_wd.window_object setHidesOnDeactivate:NO];
  1862. }
  1863. }
  1864. void DisplayServerOSX::delete_sub_window(WindowID p_id) {
  1865. _THREAD_SAFE_METHOD_
  1866. ERR_FAIL_COND(!windows.has(p_id));
  1867. ERR_FAIL_COND_MSG(p_id == MAIN_WINDOW_ID, "Main window can't be deleted");
  1868. WindowData &wd = windows[p_id];
  1869. [wd.window_object setContentView:nil];
  1870. [wd.window_object close];
  1871. }
  1872. void DisplayServerOSX::window_set_title(const String &p_title, WindowID p_window) {
  1873. _THREAD_SAFE_METHOD_
  1874. ERR_FAIL_COND(!windows.has(p_window));
  1875. WindowData &wd = windows[p_window];
  1876. [wd.window_object setTitle:[NSString stringWithUTF8String:p_title.utf8().get_data()]];
  1877. }
  1878. void DisplayServerOSX::window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window) {
  1879. _THREAD_SAFE_METHOD_
  1880. ERR_FAIL_COND(!windows.has(p_window));
  1881. WindowData &wd = windows[p_window];
  1882. wd.rect_changed_callback = p_callable;
  1883. }
  1884. void DisplayServerOSX::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) {
  1885. _THREAD_SAFE_METHOD_
  1886. ERR_FAIL_COND(!windows.has(p_window));
  1887. WindowData &wd = windows[p_window];
  1888. wd.event_callback = p_callable;
  1889. }
  1890. void DisplayServerOSX::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) {
  1891. _THREAD_SAFE_METHOD_
  1892. ERR_FAIL_COND(!windows.has(p_window));
  1893. WindowData &wd = windows[p_window];
  1894. wd.input_event_callback = p_callable;
  1895. }
  1896. void DisplayServerOSX::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) {
  1897. _THREAD_SAFE_METHOD_
  1898. ERR_FAIL_COND(!windows.has(p_window));
  1899. WindowData &wd = windows[p_window];
  1900. wd.input_text_callback = p_callable;
  1901. }
  1902. void DisplayServerOSX::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) {
  1903. _THREAD_SAFE_METHOD_
  1904. ERR_FAIL_COND(!windows.has(p_window));
  1905. WindowData &wd = windows[p_window];
  1906. wd.drop_files_callback = p_callable;
  1907. }
  1908. int DisplayServerOSX::window_get_current_screen(WindowID p_window) const {
  1909. _THREAD_SAFE_METHOD_
  1910. ERR_FAIL_COND_V(!windows.has(p_window), -1);
  1911. const WindowData &wd = windows[p_window];
  1912. const NSUInteger index = [[NSScreen screens] indexOfObject:[wd.window_object screen]];
  1913. return (index == NSNotFound) ? 0 : index;
  1914. }
  1915. void DisplayServerOSX::window_set_current_screen(int p_screen, WindowID p_window) {
  1916. _THREAD_SAFE_METHOD_
  1917. Point2i wpos = window_get_position(p_window) - screen_get_position(window_get_current_screen(p_window));
  1918. window_set_position(wpos + screen_get_position(p_screen), p_window);
  1919. }
  1920. void DisplayServerOSX::window_set_transient(WindowID p_window, WindowID p_parent) {
  1921. _THREAD_SAFE_METHOD_
  1922. ERR_FAIL_COND(p_window == p_parent);
  1923. ERR_FAIL_COND(!windows.has(p_window));
  1924. WindowData &wd_window = windows[p_window];
  1925. ERR_FAIL_COND(wd_window.transient_parent == p_parent);
  1926. ERR_FAIL_COND_MSG(wd_window.on_top, "Windows with the 'on top' can't become transient.");
  1927. if (p_parent == INVALID_WINDOW_ID) {
  1928. //remove transient
  1929. ERR_FAIL_COND(wd_window.transient_parent == INVALID_WINDOW_ID);
  1930. ERR_FAIL_COND(!windows.has(wd_window.transient_parent));
  1931. WindowData &wd_parent = windows[wd_window.transient_parent];
  1932. wd_window.transient_parent = INVALID_WINDOW_ID;
  1933. wd_parent.transient_children.erase(p_window);
  1934. [wd_window.window_object setParentWindow:nil];
  1935. } else {
  1936. ERR_FAIL_COND(!windows.has(p_parent));
  1937. ERR_FAIL_COND_MSG(wd_window.transient_parent != INVALID_WINDOW_ID, "Window already has a transient parent");
  1938. WindowData &wd_parent = windows[p_parent];
  1939. wd_window.transient_parent = p_parent;
  1940. wd_parent.transient_children.insert(p_window);
  1941. [wd_window.window_object setParentWindow:wd_parent.window_object];
  1942. }
  1943. }
  1944. Point2i DisplayServerOSX::window_get_position(WindowID p_window) const {
  1945. _THREAD_SAFE_METHOD_
  1946. ERR_FAIL_COND_V(!windows.has(p_window), Point2i());
  1947. const WindowData &wd = windows[p_window];
  1948. NSRect nsrect = [wd.window_object frame];
  1949. Point2i pos;
  1950. float display_scale = _display_scale([wd.window_object screen]);
  1951. // Return the position of the top-left corner, for OS X the y starts at the bottom
  1952. pos.x = nsrect.origin.x * display_scale;
  1953. pos.y = (nsrect.origin.y + nsrect.size.height) * display_scale;
  1954. pos -= _get_screens_origin();
  1955. // OS X native y-coordinate relative to _get_screens_origin() is negative,
  1956. // Godot expects a positive value
  1957. pos.y *= -1;
  1958. return pos;
  1959. }
  1960. void DisplayServerOSX::window_set_position(const Point2i &p_position, WindowID p_window) {
  1961. _THREAD_SAFE_METHOD_
  1962. ERR_FAIL_COND(!windows.has(p_window));
  1963. WindowData &wd = windows[p_window];
  1964. Point2i position = p_position;
  1965. // OS X native y-coordinate relative to _get_screens_origin() is negative,
  1966. // Godot passes a positive value
  1967. position.y *= -1;
  1968. position += _get_screens_origin();
  1969. NSPoint pos;
  1970. float displayScale = _display_scale([wd.window_object screen]);
  1971. pos.x = position.x / displayScale;
  1972. pos.y = position.y / displayScale;
  1973. [wd.window_object setFrameTopLeftPoint:pos];
  1974. _update_window(wd);
  1975. _get_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream], displayScale);
  1976. }
  1977. void DisplayServerOSX::window_set_max_size(const Size2i p_size, WindowID p_window) {
  1978. _THREAD_SAFE_METHOD_
  1979. ERR_FAIL_COND(!windows.has(p_window));
  1980. WindowData &wd = windows[p_window];
  1981. if ((p_size != Size2i()) && ((p_size.x < wd.min_size.x) || (p_size.y < wd.min_size.y))) {
  1982. ERR_PRINT("Maximum window size can't be smaller than minimum window size!");
  1983. return;
  1984. }
  1985. wd.max_size = p_size;
  1986. if ((wd.max_size != Size2i()) && !wd.fullscreen) {
  1987. Size2i size = wd.max_size / _display_scale([wd.window_object screen]);
  1988. [wd.window_object setContentMaxSize:NSMakeSize(size.x, size.y)];
  1989. } else {
  1990. [wd.window_object setContentMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
  1991. }
  1992. }
  1993. Size2i DisplayServerOSX::window_get_max_size(WindowID p_window) const {
  1994. _THREAD_SAFE_METHOD_
  1995. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  1996. const WindowData &wd = windows[p_window];
  1997. return wd.max_size;
  1998. }
  1999. void DisplayServerOSX::window_set_min_size(const Size2i p_size, WindowID p_window) {
  2000. _THREAD_SAFE_METHOD_
  2001. ERR_FAIL_COND(!windows.has(p_window));
  2002. WindowData &wd = windows[p_window];
  2003. if ((p_size != Size2i()) && (wd.max_size != Size2i()) && ((p_size.x > wd.max_size.x) || (p_size.y > wd.max_size.y))) {
  2004. ERR_PRINT("Minimum window size can't be larger than maximum window size!");
  2005. return;
  2006. }
  2007. wd.min_size = p_size;
  2008. if ((wd.min_size != Size2i()) && !wd.fullscreen) {
  2009. Size2i size = wd.min_size / _display_scale([wd.window_object screen]);
  2010. [wd.window_object setContentMinSize:NSMakeSize(size.x, size.y)];
  2011. } else {
  2012. [wd.window_object setContentMinSize:NSMakeSize(0, 0)];
  2013. }
  2014. }
  2015. Size2i DisplayServerOSX::window_get_min_size(WindowID p_window) const {
  2016. _THREAD_SAFE_METHOD_
  2017. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  2018. const WindowData &wd = windows[p_window];
  2019. return wd.min_size;
  2020. }
  2021. void DisplayServerOSX::window_set_size(const Size2i p_size, WindowID p_window) {
  2022. _THREAD_SAFE_METHOD_
  2023. ERR_FAIL_COND(!windows.has(p_window));
  2024. WindowData &wd = windows[p_window];
  2025. Size2i size = p_size / _display_scale([wd.window_object screen]);
  2026. if (!wd.borderless) {
  2027. // NSRect used by setFrame includes the title bar, so add it to our size.y
  2028. CGFloat menuBarHeight = [[[NSApplication sharedApplication] mainMenu] menuBarHeight];
  2029. if (menuBarHeight != 0.f) {
  2030. size.y += menuBarHeight;
  2031. }
  2032. }
  2033. NSRect frame = [wd.window_object frame];
  2034. [wd.window_object setFrame:NSMakeRect(frame.origin.x, frame.origin.y, size.x, size.y) display:YES];
  2035. _update_window(wd);
  2036. }
  2037. Size2i DisplayServerOSX::window_get_size(WindowID p_window) const {
  2038. _THREAD_SAFE_METHOD_
  2039. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  2040. const WindowData &wd = windows[p_window];
  2041. return wd.size;
  2042. }
  2043. Size2i DisplayServerOSX::window_get_real_size(WindowID p_window) const {
  2044. _THREAD_SAFE_METHOD_
  2045. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  2046. const WindowData &wd = windows[p_window];
  2047. NSRect frame = [wd.window_object frame];
  2048. return Size2i(frame.size.width, frame.size.height) * _display_scale([wd.window_object screen]);
  2049. }
  2050. bool DisplayServerOSX::window_is_maximize_allowed(WindowID p_window) const {
  2051. return true;
  2052. }
  2053. void DisplayServerOSX::_set_window_per_pixel_transparency_enabled(bool p_enabled, WindowID p_window) {
  2054. ERR_FAIL_COND(!windows.has(p_window));
  2055. WindowData &wd = windows[p_window];
  2056. if (!OS_OSX::get_singleton()->is_layered_allowed())
  2057. return;
  2058. if (wd.layered_window != p_enabled) {
  2059. if (p_enabled) {
  2060. [wd.window_object setBackgroundColor:[NSColor clearColor]];
  2061. [wd.window_object setOpaque:NO];
  2062. [wd.window_object setHasShadow:NO];
  2063. #if defined(VULKAN_ENABLED)
  2064. if (rendering_driver == "vulkan") {
  2065. CALayer *layer = [wd.window_view layer];
  2066. [layer setOpaque:NO];
  2067. //TODO - implement transparency for Vulkan
  2068. }
  2069. #endif
  2070. #if defined(OPENGL_ENABLED)
  2071. if (rendering_driver == "opengl_es") {
  2072. //TODO - reimplement OpenGLES
  2073. wd.context_gles2->set_opacity(0);
  2074. }
  2075. #endif
  2076. wd.layered_window = true;
  2077. } else {
  2078. [wd.window_object setBackgroundColor:[NSColor colorWithCalibratedWhite:1 alpha:1]];
  2079. [wd.window_object setOpaque:YES];
  2080. [wd.window_object setHasShadow:YES];
  2081. #if defined(VULKAN_ENABLED)
  2082. if (rendering_driver == "vulkan") {
  2083. CALayer *layer = [wd.window_view layer];
  2084. [layer setOpaque:YES];
  2085. //TODO - implement transparency for Vulkan
  2086. }
  2087. #endif
  2088. #if defined(OPENGL_ENABLED)
  2089. if (rendering_driver == "opengl_es") {
  2090. //TODO - reimplement OpenGLES
  2091. wd.context_gles2->set_opacity(1);
  2092. }
  2093. #endif
  2094. wd.layered_window = false;
  2095. }
  2096. #if defined(OPENGL_ENABLED)
  2097. if (rendering_driver == "opengl_es") {
  2098. //TODO - reimplement OpenGLES
  2099. wd.context_gles2->update();
  2100. }
  2101. #endif
  2102. NSRect frameRect = [wd.window_object frame];
  2103. [wd.window_object setFrame:NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width + 1, frameRect.size.height) display:YES];
  2104. [wd.window_object setFrame:frameRect display:YES];
  2105. }
  2106. }
  2107. void DisplayServerOSX::window_set_mode(WindowMode p_mode, WindowID p_window) {
  2108. _THREAD_SAFE_METHOD_
  2109. ERR_FAIL_COND(!windows.has(p_window));
  2110. WindowData &wd = windows[p_window];
  2111. WindowMode old_mode = window_get_mode(p_window);
  2112. if (old_mode == p_mode) {
  2113. return; // do nothing
  2114. }
  2115. switch (old_mode) {
  2116. case WINDOW_MODE_WINDOWED: {
  2117. //do nothing
  2118. } break;
  2119. case WINDOW_MODE_MINIMIZED: {
  2120. [wd.window_object deminiaturize:nil];
  2121. } break;
  2122. case WINDOW_MODE_FULLSCREEN: {
  2123. if (wd.layered_window)
  2124. _set_window_per_pixel_transparency_enabled(true, p_window);
  2125. if (wd.resize_disabled) //restore resize disabled
  2126. [wd.window_object setStyleMask:[wd.window_object styleMask] & ~NSWindowStyleMaskResizable];
  2127. if (wd.min_size != Size2i()) {
  2128. Size2i size = wd.min_size / _display_scale([wd.window_object screen]);
  2129. [wd.window_object setContentMinSize:NSMakeSize(size.x, size.y)];
  2130. }
  2131. if (wd.max_size != Size2i()) {
  2132. Size2i size = wd.max_size / _display_scale([wd.window_object screen]);
  2133. [wd.window_object setContentMaxSize:NSMakeSize(size.x, size.y)];
  2134. }
  2135. [wd.window_object toggleFullScreen:nil];
  2136. wd.fullscreen = false;
  2137. } break;
  2138. case WINDOW_MODE_MAXIMIZED: {
  2139. if ([wd.window_object isZoomed]) {
  2140. [wd.window_object zoom:nil];
  2141. }
  2142. } break;
  2143. }
  2144. switch (p_mode) {
  2145. case WINDOW_MODE_WINDOWED: {
  2146. //do nothing
  2147. } break;
  2148. case WINDOW_MODE_MINIMIZED: {
  2149. [wd.window_object performMiniaturize:nil];
  2150. } break;
  2151. case WINDOW_MODE_FULLSCREEN: {
  2152. if (wd.layered_window)
  2153. _set_window_per_pixel_transparency_enabled(false, p_window);
  2154. if (wd.resize_disabled) //fullscreen window should be resizable to work
  2155. [wd.window_object setStyleMask:[wd.window_object styleMask] | NSWindowStyleMaskResizable];
  2156. [wd.window_object setContentMinSize:NSMakeSize(0, 0)];
  2157. [wd.window_object setContentMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
  2158. [wd.window_object toggleFullScreen:nil];
  2159. wd.fullscreen = true;
  2160. } break;
  2161. case WINDOW_MODE_MAXIMIZED: {
  2162. if (![wd.window_object isZoomed]) {
  2163. [wd.window_object zoom:nil];
  2164. }
  2165. } break;
  2166. }
  2167. }
  2168. DisplayServer::WindowMode DisplayServerOSX::window_get_mode(WindowID p_window) const {
  2169. _THREAD_SAFE_METHOD_
  2170. ERR_FAIL_COND_V(!windows.has(p_window), WINDOW_MODE_WINDOWED);
  2171. const WindowData &wd = windows[p_window];
  2172. if (wd.fullscreen) { //if fullscreen, it's not in another mode
  2173. return WINDOW_MODE_FULLSCREEN;
  2174. }
  2175. if ([wd.window_object isZoomed] && !wd.resize_disabled) {
  2176. return WINDOW_MODE_MAXIMIZED;
  2177. }
  2178. if ([wd.window_object respondsToSelector:@selector(isMiniaturized)]) {
  2179. if ([wd.window_object isMiniaturized]) {
  2180. return WINDOW_MODE_MINIMIZED;
  2181. }
  2182. }
  2183. // all other discarded, return windowed.
  2184. return WINDOW_MODE_WINDOWED;
  2185. }
  2186. void DisplayServerOSX::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) {
  2187. _THREAD_SAFE_METHOD_
  2188. ERR_FAIL_COND(!windows.has(p_window));
  2189. WindowData &wd = windows[p_window];
  2190. switch (p_flag) {
  2191. case WINDOW_FLAG_RESIZE_DISABLED: {
  2192. wd.resize_disabled = p_enabled;
  2193. if (wd.fullscreen) //fullscreen window should be resizable, style will be applyed on exiting fs
  2194. return;
  2195. if (p_enabled) {
  2196. [wd.window_object setStyleMask:[wd.window_object styleMask] & ~NSWindowStyleMaskResizable];
  2197. } else {
  2198. [wd.window_object setStyleMask:[wd.window_object styleMask] | NSWindowStyleMaskResizable];
  2199. }
  2200. } break;
  2201. case WINDOW_FLAG_BORDERLESS: {
  2202. // OrderOut prevents a lose focus bug with the window
  2203. [wd.window_object orderOut:nil];
  2204. wd.borderless = p_enabled;
  2205. if (p_enabled) {
  2206. [wd.window_object setStyleMask:NSWindowStyleMaskBorderless];
  2207. } else {
  2208. if (wd.layered_window)
  2209. _set_window_per_pixel_transparency_enabled(false, p_window);
  2210. [wd.window_object setStyleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | (wd.resize_disabled ? 0 : NSWindowStyleMaskResizable)];
  2211. // Force update of the window styles
  2212. NSRect frameRect = [wd.window_object frame];
  2213. [wd.window_object setFrame:NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width + 1, frameRect.size.height) display:NO];
  2214. [wd.window_object setFrame:frameRect display:NO];
  2215. }
  2216. _update_window(wd);
  2217. [wd.window_object makeKeyAndOrderFront:nil];
  2218. } break;
  2219. case WINDOW_FLAG_ALWAYS_ON_TOP: {
  2220. wd.on_top = p_enabled;
  2221. if (p_enabled) {
  2222. [wd.window_object setLevel:NSFloatingWindowLevel];
  2223. } else {
  2224. [wd.window_object setLevel:NSNormalWindowLevel];
  2225. }
  2226. } break;
  2227. case WINDOW_FLAG_TRANSPARENT: {
  2228. wd.layered_window = p_enabled;
  2229. if (p_enabled) {
  2230. [wd.window_object setStyleMask:NSWindowStyleMaskBorderless]; // force borderless
  2231. } else if (!wd.borderless) {
  2232. [wd.window_object setStyleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | (wd.resize_disabled ? 0 : NSWindowStyleMaskResizable)];
  2233. }
  2234. _set_window_per_pixel_transparency_enabled(p_enabled, p_window);
  2235. } break;
  2236. default: {
  2237. }
  2238. }
  2239. }
  2240. bool DisplayServerOSX::window_get_flag(WindowFlags p_flag, WindowID p_window) const {
  2241. _THREAD_SAFE_METHOD_
  2242. ERR_FAIL_COND_V(!windows.has(p_window), false);
  2243. const WindowData &wd = windows[p_window];
  2244. switch (p_flag) {
  2245. case WINDOW_FLAG_RESIZE_DISABLED: {
  2246. return wd.resize_disabled;
  2247. } break;
  2248. case WINDOW_FLAG_BORDERLESS: {
  2249. return [wd.window_object styleMask] == NSWindowStyleMaskBorderless;
  2250. } break;
  2251. case WINDOW_FLAG_ALWAYS_ON_TOP: {
  2252. return [wd.window_object level] == NSFloatingWindowLevel;
  2253. } break;
  2254. case WINDOW_FLAG_TRANSPARENT: {
  2255. return wd.layered_window;
  2256. } break;
  2257. default: {
  2258. }
  2259. }
  2260. return false;
  2261. }
  2262. void DisplayServerOSX::window_request_attention(WindowID p_window) {
  2263. // It's app global, ignore window id.
  2264. [NSApp requestUserAttention:NSCriticalRequest];
  2265. }
  2266. void DisplayServerOSX::window_move_to_foreground(WindowID p_window) {
  2267. _THREAD_SAFE_METHOD_
  2268. ERR_FAIL_COND(!windows.has(p_window));
  2269. const WindowData &wd = windows[p_window];
  2270. [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
  2271. [wd.window_object makeKeyAndOrderFront:nil];
  2272. }
  2273. bool DisplayServerOSX::window_can_draw(WindowID p_window) const {
  2274. return window_get_mode(p_window) != WINDOW_MODE_MINIMIZED;
  2275. }
  2276. bool DisplayServerOSX::can_any_window_draw() const {
  2277. _THREAD_SAFE_METHOD_
  2278. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  2279. if (window_get_mode(E->key()) != WINDOW_MODE_MINIMIZED) {
  2280. return true;
  2281. }
  2282. }
  2283. return false;
  2284. }
  2285. void DisplayServerOSX::window_set_ime_active(const bool p_active, WindowID p_window) {
  2286. _THREAD_SAFE_METHOD_
  2287. ERR_FAIL_COND(!windows.has(p_window));
  2288. WindowData &wd = windows[p_window];
  2289. wd.im_active = p_active;
  2290. if (!p_active)
  2291. [wd.window_view cancelComposition];
  2292. }
  2293. void DisplayServerOSX::window_set_ime_position(const Point2i &p_pos, WindowID p_window) {
  2294. _THREAD_SAFE_METHOD_
  2295. ERR_FAIL_COND(!windows.has(p_window));
  2296. WindowData &wd = windows[p_window];
  2297. wd.im_position = p_pos;
  2298. }
  2299. bool DisplayServerOSX::get_swap_ok_cancel() {
  2300. return true;
  2301. }
  2302. void DisplayServerOSX::cursor_set_shape(CursorShape p_shape) {
  2303. _THREAD_SAFE_METHOD_
  2304. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  2305. if (cursor_shape == p_shape)
  2306. return;
  2307. if (mouse_mode != MOUSE_MODE_VISIBLE && mouse_mode != MOUSE_MODE_CONFINED) {
  2308. cursor_shape = p_shape;
  2309. return;
  2310. }
  2311. if (cursors[p_shape] != NULL) {
  2312. [cursors[p_shape] set];
  2313. } else {
  2314. switch (p_shape) {
  2315. case CURSOR_ARROW: [[NSCursor arrowCursor] set]; break;
  2316. case CURSOR_IBEAM: [[NSCursor IBeamCursor] set]; break;
  2317. case CURSOR_POINTING_HAND: [[NSCursor pointingHandCursor] set]; break;
  2318. case CURSOR_CROSS: [[NSCursor crosshairCursor] set]; break;
  2319. case CURSOR_WAIT: [[NSCursor arrowCursor] set]; break;
  2320. case CURSOR_BUSY: [[NSCursor arrowCursor] set]; break;
  2321. case CURSOR_DRAG: [[NSCursor closedHandCursor] set]; break;
  2322. case CURSOR_CAN_DROP: [[NSCursor openHandCursor] set]; break;
  2323. case CURSOR_FORBIDDEN: [[NSCursor operationNotAllowedCursor] set]; break;
  2324. case CURSOR_VSIZE: [_cursorFromSelector(@selector(_windowResizeNorthSouthCursor), @selector(resizeUpDownCursor)) set]; break;
  2325. case CURSOR_HSIZE: [_cursorFromSelector(@selector(_windowResizeEastWestCursor), @selector(resizeLeftRightCursor)) set]; break;
  2326. case CURSOR_BDIAGSIZE: [_cursorFromSelector(@selector(_windowResizeNorthEastSouthWestCursor)) set]; break;
  2327. case CURSOR_FDIAGSIZE: [_cursorFromSelector(@selector(_windowResizeNorthWestSouthEastCursor)) set]; break;
  2328. case CURSOR_MOVE: [[NSCursor arrowCursor] set]; break;
  2329. case CURSOR_VSPLIT: [[NSCursor resizeUpDownCursor] set]; break;
  2330. case CURSOR_HSPLIT: [[NSCursor resizeLeftRightCursor] set]; break;
  2331. case CURSOR_HELP: [_cursorFromSelector(@selector(_helpCursor)) set]; break;
  2332. default: {
  2333. }
  2334. }
  2335. }
  2336. cursor_shape = p_shape;
  2337. }
  2338. DisplayServerOSX::CursorShape DisplayServerOSX::cursor_get_shape() const {
  2339. return cursor_shape;
  2340. }
  2341. void DisplayServerOSX::cursor_set_custom_image(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  2342. _THREAD_SAFE_METHOD_
  2343. if (p_cursor.is_valid()) {
  2344. Map<CursorShape, Vector<Variant>>::Element *cursor_c = cursors_cache.find(p_shape);
  2345. if (cursor_c) {
  2346. if (cursor_c->get()[0] == p_cursor && cursor_c->get()[1] == p_hotspot) {
  2347. cursor_set_shape(p_shape);
  2348. return;
  2349. }
  2350. cursors_cache.erase(p_shape);
  2351. }
  2352. Ref<Texture2D> texture = p_cursor;
  2353. Ref<AtlasTexture> atlas_texture = p_cursor;
  2354. Ref<Image> image;
  2355. Size2 texture_size;
  2356. Rect2 atlas_rect;
  2357. if (texture.is_valid()) {
  2358. image = texture->get_data();
  2359. }
  2360. if (!image.is_valid() && atlas_texture.is_valid()) {
  2361. texture = atlas_texture->get_atlas();
  2362. atlas_rect.size.width = texture->get_width();
  2363. atlas_rect.size.height = texture->get_height();
  2364. atlas_rect.position.x = atlas_texture->get_region().position.x;
  2365. atlas_rect.position.y = atlas_texture->get_region().position.y;
  2366. texture_size.width = atlas_texture->get_region().size.x;
  2367. texture_size.height = atlas_texture->get_region().size.y;
  2368. } else if (image.is_valid()) {
  2369. texture_size.width = texture->get_width();
  2370. texture_size.height = texture->get_height();
  2371. }
  2372. ERR_FAIL_COND(!texture.is_valid());
  2373. ERR_FAIL_COND(p_hotspot.x < 0 || p_hotspot.y < 0);
  2374. ERR_FAIL_COND(texture_size.width > 256 || texture_size.height > 256);
  2375. ERR_FAIL_COND(p_hotspot.x > texture_size.width || p_hotspot.y > texture_size.height);
  2376. image = texture->get_data();
  2377. ERR_FAIL_COND(!image.is_valid());
  2378. NSBitmapImageRep *imgrep = [[NSBitmapImageRep alloc]
  2379. initWithBitmapDataPlanes:NULL
  2380. pixelsWide:int(texture_size.width)
  2381. pixelsHigh:int(texture_size.height)
  2382. bitsPerSample:8
  2383. samplesPerPixel:4
  2384. hasAlpha:YES
  2385. isPlanar:NO
  2386. colorSpaceName:NSDeviceRGBColorSpace
  2387. bytesPerRow:int(texture_size.width) * 4
  2388. bitsPerPixel:32];
  2389. ERR_FAIL_COND(imgrep == nil);
  2390. uint8_t *pixels = [imgrep bitmapData];
  2391. int len = int(texture_size.width * texture_size.height);
  2392. for (int i = 0; i < len; i++) {
  2393. int row_index = floor(i / texture_size.width) + atlas_rect.position.y;
  2394. int column_index = (i % int(texture_size.width)) + atlas_rect.position.x;
  2395. if (atlas_texture.is_valid()) {
  2396. column_index = MIN(column_index, atlas_rect.size.width - 1);
  2397. row_index = MIN(row_index, atlas_rect.size.height - 1);
  2398. }
  2399. uint32_t color = image->get_pixel(column_index, row_index).to_argb32();
  2400. uint8_t alpha = (color >> 24) & 0xFF;
  2401. pixels[i * 4 + 0] = ((color >> 16) & 0xFF) * alpha / 255;
  2402. pixels[i * 4 + 1] = ((color >> 8) & 0xFF) * alpha / 255;
  2403. pixels[i * 4 + 2] = ((color)&0xFF) * alpha / 255;
  2404. pixels[i * 4 + 3] = alpha;
  2405. }
  2406. NSImage *nsimage = [[NSImage alloc] initWithSize:NSMakeSize(texture_size.width, texture_size.height)];
  2407. [nsimage addRepresentation:imgrep];
  2408. NSCursor *cursor = [[NSCursor alloc] initWithImage:nsimage hotSpot:NSMakePoint(p_hotspot.x, p_hotspot.y)];
  2409. [cursors[p_shape] release];
  2410. cursors[p_shape] = cursor;
  2411. Vector<Variant> params;
  2412. params.push_back(p_cursor);
  2413. params.push_back(p_hotspot);
  2414. cursors_cache.insert(p_shape, params);
  2415. if (p_shape == cursor_shape) {
  2416. if (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED) {
  2417. [cursor set];
  2418. }
  2419. }
  2420. [imgrep release];
  2421. [nsimage release];
  2422. } else {
  2423. // Reset to default system cursor
  2424. if (cursors[p_shape] != NULL) {
  2425. [cursors[p_shape] release];
  2426. cursors[p_shape] = NULL;
  2427. }
  2428. CursorShape c = cursor_shape;
  2429. cursor_shape = CURSOR_MAX;
  2430. cursor_set_shape(c);
  2431. cursors_cache.erase(p_shape);
  2432. }
  2433. }
  2434. static bool keyboard_layout_dirty = true;
  2435. static void keyboard_layout_changed(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef user_info) {
  2436. keyboard_layout_dirty = true;
  2437. }
  2438. // Returns string representation of keys, if they are printable.
  2439. static NSString *createStringForKeys(const CGKeyCode *keyCode, int length) {
  2440. TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();
  2441. if (!currentKeyboard)
  2442. return nil;
  2443. CFDataRef layoutData = (CFDataRef)TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData);
  2444. if (!layoutData)
  2445. return nil;
  2446. const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout *)CFDataGetBytePtr(layoutData);
  2447. OSStatus err;
  2448. CFMutableStringRef output = CFStringCreateMutable(NULL, 0);
  2449. for (int i = 0; i < length; ++i) {
  2450. UInt32 keysDown = 0;
  2451. UniChar chars[4];
  2452. UniCharCount realLength;
  2453. err = UCKeyTranslate(keyboardLayout,
  2454. keyCode[i],
  2455. kUCKeyActionDisplay,
  2456. 0,
  2457. LMGetKbdType(),
  2458. kUCKeyTranslateNoDeadKeysBit,
  2459. &keysDown,
  2460. sizeof(chars) / sizeof(chars[0]),
  2461. &realLength,
  2462. chars);
  2463. if (err != noErr) {
  2464. CFRelease(output);
  2465. return nil;
  2466. }
  2467. CFStringAppendCharacters(output, chars, 1);
  2468. }
  2469. return (NSString *)output;
  2470. }
  2471. DisplayServerOSX::LatinKeyboardVariant DisplayServerOSX::get_latin_keyboard_variant() const {
  2472. _THREAD_SAFE_METHOD_
  2473. static LatinKeyboardVariant layout = LATIN_KEYBOARD_QWERTY;
  2474. if (keyboard_layout_dirty) {
  2475. layout = LATIN_KEYBOARD_QWERTY;
  2476. CGKeyCode keys[] = { kVK_ANSI_Q, kVK_ANSI_W, kVK_ANSI_E, kVK_ANSI_R, kVK_ANSI_T, kVK_ANSI_Y };
  2477. NSString *test = createStringForKeys(keys, 6);
  2478. if ([test isEqualToString:@"qwertz"]) {
  2479. layout = LATIN_KEYBOARD_QWERTZ;
  2480. } else if ([test isEqualToString:@"azerty"]) {
  2481. layout = LATIN_KEYBOARD_AZERTY;
  2482. } else if ([test isEqualToString:@"qzerty"]) {
  2483. layout = LATIN_KEYBOARD_QZERTY;
  2484. } else if ([test isEqualToString:@"',.pyf"]) {
  2485. layout = LATIN_KEYBOARD_DVORAK;
  2486. } else if ([test isEqualToString:@"xvlcwk"]) {
  2487. layout = LATIN_KEYBOARD_NEO;
  2488. } else if ([test isEqualToString:@"qwfpgj"]) {
  2489. layout = LATIN_KEYBOARD_COLEMAK;
  2490. }
  2491. [test release];
  2492. keyboard_layout_dirty = false;
  2493. return layout;
  2494. }
  2495. return layout;
  2496. }
  2497. void DisplayServerOSX::_push_input(const Ref<InputEvent> &p_event) {
  2498. Ref<InputEvent> ev = p_event;
  2499. Input::get_singleton()->accumulate_input_event(ev);
  2500. }
  2501. void DisplayServerOSX::_release_pressed_events() {
  2502. _THREAD_SAFE_METHOD_
  2503. if (Input::get_singleton()) {
  2504. Input::get_singleton()->release_pressed_events();
  2505. }
  2506. }
  2507. void DisplayServerOSX::_process_key_events() {
  2508. Ref<InputEventKey> k;
  2509. for (int i = 0; i < key_event_pos; i++) {
  2510. const KeyEvent &ke = key_event_buffer[i];
  2511. if (ke.raw) {
  2512. // Non IME input - no composite characters, pass events as is
  2513. k.instance();
  2514. k->set_window_id(ke.window_id);
  2515. _get_key_modifier_state(ke.osx_state, k);
  2516. k->set_pressed(ke.pressed);
  2517. k->set_echo(ke.echo);
  2518. k->set_keycode(ke.keycode);
  2519. k->set_physical_keycode(ke.physical_keycode);
  2520. k->set_unicode(ke.unicode);
  2521. _push_input(k);
  2522. } else {
  2523. // IME input
  2524. if ((i == 0 && ke.keycode == 0) || (i > 0 && key_event_buffer[i - 1].keycode == 0)) {
  2525. k.instance();
  2526. k->set_window_id(ke.window_id);
  2527. _get_key_modifier_state(ke.osx_state, k);
  2528. k->set_pressed(ke.pressed);
  2529. k->set_echo(ke.echo);
  2530. k->set_keycode(0);
  2531. k->set_physical_keycode(0);
  2532. k->set_unicode(ke.unicode);
  2533. _push_input(k);
  2534. }
  2535. if (ke.keycode != 0) {
  2536. k.instance();
  2537. k->set_window_id(ke.window_id);
  2538. _get_key_modifier_state(ke.osx_state, k);
  2539. k->set_pressed(ke.pressed);
  2540. k->set_echo(ke.echo);
  2541. k->set_keycode(ke.keycode);
  2542. k->set_physical_keycode(ke.physical_keycode);
  2543. if (i + 1 < key_event_pos && key_event_buffer[i + 1].keycode == 0) {
  2544. k->set_unicode(key_event_buffer[i + 1].unicode);
  2545. }
  2546. _push_input(k);
  2547. }
  2548. }
  2549. }
  2550. key_event_pos = 0;
  2551. }
  2552. void DisplayServerOSX::process_events() {
  2553. _THREAD_SAFE_METHOD_
  2554. while (true) {
  2555. NSEvent *event = [NSApp
  2556. nextEventMatchingMask:NSEventMaskAny
  2557. untilDate:[NSDate distantPast]
  2558. inMode:NSDefaultRunLoopMode
  2559. dequeue:YES];
  2560. if (event == nil)
  2561. break;
  2562. [NSApp sendEvent:event];
  2563. }
  2564. if (!drop_events) {
  2565. _process_key_events();
  2566. Input::get_singleton()->flush_accumulated_events();
  2567. }
  2568. [autoreleasePool drain];
  2569. autoreleasePool = [[NSAutoreleasePool alloc] init];
  2570. }
  2571. void DisplayServerOSX::force_process_and_drop_events() {
  2572. _THREAD_SAFE_METHOD_
  2573. drop_events = true;
  2574. process_events();
  2575. drop_events = false;
  2576. }
  2577. void DisplayServerOSX::set_native_icon(const String &p_filename) {
  2578. _THREAD_SAFE_METHOD_
  2579. FileAccess *f = FileAccess::open(p_filename, FileAccess::READ);
  2580. ERR_FAIL_COND(!f);
  2581. Vector<uint8_t> data;
  2582. uint32_t len = f->get_len();
  2583. data.resize(len);
  2584. f->get_buffer((uint8_t *)&data.write[0], len);
  2585. memdelete(f);
  2586. NSData *icon_data = [[[NSData alloc] initWithBytes:&data.write[0] length:len] autorelease];
  2587. ERR_FAIL_COND_MSG(!icon_data, "Error reading icon data.");
  2588. NSImage *icon = [[[NSImage alloc] initWithData:icon_data] autorelease];
  2589. ERR_FAIL_COND_MSG(!icon, "Error loading icon.");
  2590. [NSApp setApplicationIconImage:icon];
  2591. }
  2592. void DisplayServerOSX::set_icon(const Ref<Image> &p_icon) {
  2593. _THREAD_SAFE_METHOD_
  2594. Ref<Image> img = p_icon;
  2595. img = img->duplicate();
  2596. img->convert(Image::FORMAT_RGBA8);
  2597. NSBitmapImageRep *imgrep = [[NSBitmapImageRep alloc]
  2598. initWithBitmapDataPlanes:NULL
  2599. pixelsWide:img->get_width()
  2600. pixelsHigh:img->get_height()
  2601. bitsPerSample:8
  2602. samplesPerPixel:4
  2603. hasAlpha:YES
  2604. isPlanar:NO
  2605. colorSpaceName:NSDeviceRGBColorSpace
  2606. bytesPerRow:img->get_width() * 4
  2607. bitsPerPixel:32];
  2608. ERR_FAIL_COND(imgrep == nil);
  2609. uint8_t *pixels = [imgrep bitmapData];
  2610. int len = img->get_width() * img->get_height();
  2611. const uint8_t *r = img->get_data().ptr();
  2612. /* Premultiply the alpha channel */
  2613. for (int i = 0; i < len; i++) {
  2614. uint8_t alpha = r[i * 4 + 3];
  2615. pixels[i * 4 + 0] = (uint8_t)(((uint16_t)r[i * 4 + 0] * alpha) / 255);
  2616. pixels[i * 4 + 1] = (uint8_t)(((uint16_t)r[i * 4 + 1] * alpha) / 255);
  2617. pixels[i * 4 + 2] = (uint8_t)(((uint16_t)r[i * 4 + 2] * alpha) / 255);
  2618. pixels[i * 4 + 3] = alpha;
  2619. }
  2620. NSImage *nsimg = [[NSImage alloc] initWithSize:NSMakeSize(img->get_width(), img->get_height())];
  2621. ERR_FAIL_COND(nsimg == nil);
  2622. [nsimg addRepresentation:imgrep];
  2623. [NSApp setApplicationIconImage:nsimg];
  2624. [imgrep release];
  2625. [nsimg release];
  2626. }
  2627. Vector<String> DisplayServerOSX::get_rendering_drivers_func() {
  2628. Vector<String> drivers;
  2629. #ifdef VULKAN_ENABLED
  2630. drivers.push_back("vulkan");
  2631. #endif
  2632. #ifdef OPENGL_ENABLED
  2633. drivers.push_back("opengl");
  2634. #endif
  2635. return drivers;
  2636. }
  2637. Point2i DisplayServerOSX::ime_get_selection() const {
  2638. return im_selection;
  2639. }
  2640. String DisplayServerOSX::ime_get_text() const {
  2641. return im_text;
  2642. }
  2643. DisplayServer::WindowID DisplayServerOSX::get_window_at_screen_position(const Point2i &p_position) const {
  2644. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  2645. Rect2i win_rect = Rect2i(window_get_position(E->key()), window_get_size(E->key()));
  2646. if (win_rect.has_point(p_position)) {
  2647. return E->key();
  2648. }
  2649. }
  2650. return INVALID_WINDOW_ID;
  2651. }
  2652. void DisplayServerOSX::window_attach_instance_id(ObjectID p_instance, WindowID p_window) {
  2653. _THREAD_SAFE_METHOD_
  2654. ERR_FAIL_COND(!windows.has(p_window));
  2655. windows[p_window].instance_id = p_instance;
  2656. }
  2657. ObjectID DisplayServerOSX::window_get_attached_instance_id(WindowID p_window) const {
  2658. _THREAD_SAFE_METHOD_
  2659. ERR_FAIL_COND_V(!windows.has(p_window), ObjectID());
  2660. return windows[p_window].instance_id;
  2661. }
  2662. DisplayServer *DisplayServerOSX::create_func(const String &p_rendering_driver, WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error) {
  2663. return memnew(DisplayServerOSX(p_rendering_driver, p_mode, p_flags, p_resolution, r_error));
  2664. }
  2665. DisplayServerOSX::WindowID DisplayServerOSX::_create_window(WindowMode p_mode, const Rect2i &p_rect) {
  2666. WindowID id;
  2667. {
  2668. WindowData wd;
  2669. float displayScale = 1.0;
  2670. if (OS_OSX::get_singleton()->is_hidpi_allowed()) {
  2671. // note that mainScreen is not screen #0 but the one with the keyboard focus.
  2672. NSScreen *screen = [NSScreen mainScreen];
  2673. if ([screen respondsToSelector:@selector(backingScaleFactor)]) {
  2674. displayScale = fmax(displayScale, [screen backingScaleFactor]);
  2675. }
  2676. }
  2677. wd.window_delegate = [[GodotWindowDelegate alloc] init];
  2678. ERR_FAIL_COND_V_MSG(wd.window_delegate == nil, INVALID_WINDOW_ID, "Can't create a window delegate");
  2679. [wd.window_delegate setWindowID:window_id_counter];
  2680. Point2i position = p_rect.position;
  2681. // OS X native y-coordinate relative to _get_screens_origin() is negative,
  2682. // Godot passes a positive value
  2683. position.y *= -1;
  2684. position += _get_screens_origin();
  2685. // initWithContentRect uses bottom-left corner of the window’s frame as origin.
  2686. wd.window_object = [[GodotWindow alloc]
  2687. initWithContentRect:NSMakeRect(position.x / displayScale, (position.y - p_rect.size.height) / displayScale, p_rect.size.width / displayScale, p_rect.size.height / displayScale)
  2688. styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable
  2689. backing:NSBackingStoreBuffered
  2690. defer:NO];
  2691. ERR_FAIL_COND_V_MSG(wd.window_object == nil, INVALID_WINDOW_ID, "Can't create a window");
  2692. wd.window_view = [[GodotContentView alloc] init];
  2693. ERR_FAIL_COND_V_MSG(wd.window_view == nil, INVALID_WINDOW_ID, "Can't create a window view");
  2694. [wd.window_view setWindowID:window_id_counter];
  2695. if (NSAppKitVersionNumber >= NSAppKitVersionNumber10_14) {
  2696. [wd.window_view setWantsLayer:TRUE];
  2697. }
  2698. if (displayScale > 1.0) {
  2699. #if defined(OPENGL_ENABLED)
  2700. if (rendering_driver == "opengl_es") {
  2701. [wd.window_view setWantsBestResolutionOpenGLSurface:YES];
  2702. }
  2703. #endif
  2704. [wd.window_object setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
  2705. } else {
  2706. #if defined(OPENGL_ENABLED)
  2707. if (rendering_driver == "opengl_es") {
  2708. [wd.window_view setWantsBestResolutionOpenGLSurface:NO];
  2709. }
  2710. #endif
  2711. }
  2712. [wd.window_object setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
  2713. [wd.window_object setContentView:wd.window_view];
  2714. [wd.window_object setDelegate:wd.window_delegate];
  2715. [wd.window_object setAcceptsMouseMovedEvents:YES];
  2716. [wd.window_object setRestorable:NO];
  2717. if ([wd.window_object respondsToSelector:@selector(setTabbingMode:)])
  2718. [wd.window_object setTabbingMode:NSWindowTabbingModeDisallowed];
  2719. #if defined(VULKAN_ENABLED)
  2720. if (rendering_driver == "vulkan") {
  2721. if (context_vulkan) {
  2722. CALayer *layer = [wd.window_view layer];
  2723. layer.contentsScale = displayScale;
  2724. Error err = context_vulkan->window_create(window_id_counter, wd.window_view, p_rect.size.width, p_rect.size.height);
  2725. ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Can't create a Vulkan context");
  2726. }
  2727. }
  2728. #endif
  2729. #ifdef OPENGL_ENABLED
  2730. if (rendering_driver == "opengl_es") {
  2731. //TODO - reimplement OpenGLES
  2732. wd.context_gles2 = memnew(ContextGL_OSX(wd.window_view, false));
  2733. if (wd.context_gles2->initialize() != OK) {
  2734. memdelete(wd.context_gles2);
  2735. ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Can't create a OpenGL context");
  2736. }
  2737. //if (RasterizerGLES2::is_viable() == OK) {
  2738. // RasterizerGLES2::register_config();
  2739. // RasterizerGLES2::make_current();
  2740. //}
  2741. }
  2742. #endif
  2743. id = window_id_counter++;
  2744. windows[id] = wd;
  2745. }
  2746. WindowData &wd = windows[id];
  2747. window_set_mode(p_mode, id);
  2748. float displayScale = _display_scale([wd.window_object screen]);
  2749. const NSRect contentRect = [wd.window_view frame];
  2750. wd.size.width = contentRect.size.width * displayScale;
  2751. wd.size.height = contentRect.size.height * displayScale;
  2752. #if defined(OPENGL_ENABLED)
  2753. if (rendering_driver == "opengl_es") {
  2754. if (OS_OSX::singleton->is_hidpi_allowed()) {
  2755. [wd.window_view setWantsBestResolutionOpenGLSurface:YES];
  2756. } else {
  2757. [wd.window_view setWantsBestResolutionOpenGLSurface:NO];
  2758. }
  2759. wd.context_gles2->update();
  2760. }
  2761. #endif
  2762. #if defined(VULKAN_ENABLED)
  2763. if (rendering_driver == "vulkan") {
  2764. CALayer *layer = [wd.window_view layer];
  2765. layer.contentsScale = displayScale;
  2766. context_vulkan->window_resize(id, wd.size.width, wd.size.height);
  2767. }
  2768. #endif
  2769. return id;
  2770. }
  2771. void DisplayServerOSX::_dispatch_input_events(const Ref<InputEvent> &p_event) {
  2772. ((DisplayServerOSX *)(get_singleton()))->_dispatch_input_event(p_event);
  2773. }
  2774. void DisplayServerOSX::_dispatch_input_event(const Ref<InputEvent> &p_event) {
  2775. _THREAD_SAFE_METHOD_
  2776. if (!in_dispatch_input_event) {
  2777. in_dispatch_input_event = true;
  2778. Variant ev = p_event;
  2779. Variant *evp = &ev;
  2780. Variant ret;
  2781. Callable::CallError ce;
  2782. Ref<InputEventFromWindow> event_from_window = p_event;
  2783. if (event_from_window.is_valid() && event_from_window->get_window_id() != INVALID_WINDOW_ID) {
  2784. //send to a window
  2785. if (windows.has(event_from_window->get_window_id())) {
  2786. Callable callable = windows[event_from_window->get_window_id()].input_event_callback;
  2787. if (callable.is_null()) {
  2788. return;
  2789. }
  2790. callable.call((const Variant **)&evp, 1, ret, ce);
  2791. }
  2792. } else {
  2793. //send to all windows
  2794. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  2795. Callable callable = E->get().input_event_callback;
  2796. if (callable.is_null()) {
  2797. continue;
  2798. }
  2799. callable.call((const Variant **)&evp, 1, ret, ce);
  2800. }
  2801. }
  2802. in_dispatch_input_event = false;
  2803. }
  2804. }
  2805. void DisplayServerOSX::release_rendering_thread() {
  2806. //TODO - reimplement OpenGLES
  2807. }
  2808. void DisplayServerOSX::make_rendering_thread() {
  2809. //TODO - reimplement OpenGLES
  2810. }
  2811. void DisplayServerOSX::swap_buffers() {
  2812. //TODO - reimplement OpenGLES
  2813. }
  2814. void DisplayServerOSX::console_set_visible(bool p_enabled) {
  2815. //TODO - open terminal and redirect
  2816. }
  2817. bool DisplayServerOSX::is_console_visible() const {
  2818. return isatty(STDIN_FILENO);
  2819. }
  2820. DisplayServerOSX::DisplayServerOSX(const String &p_rendering_driver, WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error) {
  2821. Input::get_singleton()->set_event_dispatch_function(_dispatch_input_events);
  2822. r_error = OK;
  2823. drop_events = false;
  2824. memset(cursors, 0, sizeof(cursors));
  2825. cursor_shape = CURSOR_ARROW;
  2826. key_event_pos = 0;
  2827. mouse_mode = MOUSE_MODE_VISIBLE;
  2828. last_button_state = 0;
  2829. autoreleasePool = [[NSAutoreleasePool alloc] init];
  2830. eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
  2831. ERR_FAIL_COND(!eventSource);
  2832. CGEventSourceSetLocalEventsSuppressionInterval(eventSource, 0.0);
  2833. // Implicitly create shared NSApplication instance
  2834. [GodotApplication sharedApplication];
  2835. // In case we are unbundled, make us a proper UI application
  2836. [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
  2837. keyboard_layout_dirty = true;
  2838. displays_arrangement_dirty = true;
  2839. // Register to be notified on keyboard layout changes
  2840. CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(),
  2841. NULL, keyboard_layout_changed,
  2842. kTISNotifySelectedKeyboardInputSourceChanged, NULL,
  2843. CFNotificationSuspensionBehaviorDeliverImmediately);
  2844. // Register to be notified on displays arrangement changes
  2845. CGDisplayRegisterReconfigurationCallback(displays_arrangement_changed, NULL);
  2846. // Menu bar setup must go between sharedApplication above and
  2847. // finishLaunching below, in order to properly emulate the behavior
  2848. // of NSApplicationMain
  2849. NSMenuItem *menu_item;
  2850. NSString *title;
  2851. NSString *nsappname = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
  2852. if (nsappname == nil)
  2853. nsappname = [[NSProcessInfo processInfo] processName];
  2854. // Setup Dock menu
  2855. dock_menu = [[NSMenu alloc] initWithTitle:@"_dock"];
  2856. // Setup Apple menu
  2857. apple_menu = [[[NSMenu alloc] initWithTitle:@""] autorelease];
  2858. title = [NSString stringWithFormat:NSLocalizedString(@"About %@", nil), nsappname];
  2859. [apple_menu addItemWithTitle:title action:@selector(showAbout:) keyEquivalent:@""];
  2860. [apple_menu addItem:[NSMenuItem separatorItem]];
  2861. NSMenu *services = [[NSMenu alloc] initWithTitle:@""];
  2862. menu_item = [apple_menu addItemWithTitle:NSLocalizedString(@"Services", nil) action:nil keyEquivalent:@""];
  2863. [apple_menu setSubmenu:services forItem:menu_item];
  2864. [NSApp setServicesMenu:services];
  2865. [services release];
  2866. [apple_menu addItem:[NSMenuItem separatorItem]];
  2867. title = [NSString stringWithFormat:NSLocalizedString(@"Hide %@", nil), nsappname];
  2868. [apple_menu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
  2869. menu_item = [apple_menu addItemWithTitle:NSLocalizedString(@"Hide Others", nil) action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
  2870. [menu_item setKeyEquivalentModifierMask:(NSEventModifierFlagOption | NSEventModifierFlagCommand)];
  2871. [apple_menu addItemWithTitle:NSLocalizedString(@"Show all", nil) action:@selector(unhideAllApplications:) keyEquivalent:@""];
  2872. [apple_menu addItem:[NSMenuItem separatorItem]];
  2873. title = [NSString stringWithFormat:NSLocalizedString(@"Quit %@", nil), nsappname];
  2874. [apple_menu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
  2875. // Setup menu bar
  2876. NSMenu *main_menu = [[[NSMenu alloc] initWithTitle:@""] autorelease];
  2877. menu_item = [main_menu addItemWithTitle:@"" action:nil keyEquivalent:@""];
  2878. [main_menu setSubmenu:apple_menu forItem:menu_item];
  2879. [NSApp setMainMenu:main_menu];
  2880. [NSApp finishLaunching];
  2881. delegate = [[GodotApplicationDelegate alloc] init];
  2882. ERR_FAIL_COND(!delegate);
  2883. [NSApp setDelegate:delegate];
  2884. //process application:openFile: event
  2885. while (true) {
  2886. NSEvent *event = [NSApp
  2887. nextEventMatchingMask:NSEventMaskAny
  2888. untilDate:[NSDate distantPast]
  2889. inMode:NSDefaultRunLoopMode
  2890. dequeue:YES];
  2891. if (event == nil)
  2892. break;
  2893. [NSApp sendEvent:event];
  2894. }
  2895. //!!!!!!!!!!!!!!!!!!!!!!!!!!
  2896. //TODO - do Vulkan and GLES2 support checks, driver selection and fallback
  2897. rendering_driver = p_rendering_driver;
  2898. #ifndef _MSC_VER
  2899. #warning Forcing vulkan rendering driver because OpenGL not implemented yet
  2900. #endif
  2901. rendering_driver = "vulkan";
  2902. #if defined(OPENGL_ENABLED)
  2903. if (rendering_driver == "opengl_es") {
  2904. //TODO - reimplement OpenGLES
  2905. }
  2906. #endif
  2907. #if defined(VULKAN_ENABLED)
  2908. if (rendering_driver == "vulkan") {
  2909. context_vulkan = memnew(VulkanContextOSX);
  2910. if (context_vulkan->initialize() != OK) {
  2911. memdelete(context_vulkan);
  2912. context_vulkan = NULL;
  2913. r_error = ERR_CANT_CREATE;
  2914. ERR_FAIL_MSG("Could not initialize Vulkan");
  2915. }
  2916. }
  2917. #endif
  2918. Point2i window_position(
  2919. (screen_get_size(0).width - p_resolution.width) / 2,
  2920. (screen_get_size(0).height - p_resolution.height) / 2);
  2921. WindowID main_window = _create_window(p_mode, Rect2i(window_position, p_resolution));
  2922. for (int i = 0; i < WINDOW_FLAG_MAX; i++) {
  2923. if (p_flags & (1 << i)) {
  2924. window_set_flag(WindowFlags(i), true, main_window);
  2925. }
  2926. }
  2927. [windows[main_window].window_object makeKeyAndOrderFront:nil];
  2928. #if defined(VULKAN_ENABLED)
  2929. if (rendering_driver == "vulkan") {
  2930. rendering_device_vulkan = memnew(RenderingDeviceVulkan);
  2931. rendering_device_vulkan->initialize(context_vulkan);
  2932. RasterizerRD::make_current();
  2933. }
  2934. #endif
  2935. [NSApp activateIgnoringOtherApps:YES];
  2936. /*
  2937. visual_server = memnew(VisualServerRaster);
  2938. if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) {
  2939. visual_server = memnew(VisualServerWrapMT(visual_server, get_render_thread_mode() == RENDER_SEPARATE_THREAD));
  2940. }
  2941. visual_server->init();
  2942. */
  2943. }
  2944. DisplayServerOSX::~DisplayServerOSX() {
  2945. if (dock_menu) {
  2946. [dock_menu release];
  2947. }
  2948. for (Map<String, NSMenu *>::Element *E = submenu.front(); E; E = E->next()) {
  2949. [E->get() release];
  2950. }
  2951. //destroy all windows
  2952. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  2953. [E->get().window_object setContentView:nil];
  2954. [E->get().window_object close];
  2955. }
  2956. //destroy drivers
  2957. #if defined(VULKAN_ENABLED)
  2958. if (rendering_driver == "vulkan") {
  2959. if (rendering_device_vulkan) {
  2960. rendering_device_vulkan->finalize();
  2961. memdelete(rendering_device_vulkan);
  2962. }
  2963. if (context_vulkan)
  2964. memdelete(context_vulkan);
  2965. }
  2966. #endif
  2967. CFNotificationCenterRemoveObserver(CFNotificationCenterGetDistributedCenter(), NULL, kTISNotifySelectedKeyboardInputSourceChanged, NULL);
  2968. CGDisplayRemoveReconfigurationCallback(displays_arrangement_changed, NULL);
  2969. cursors_cache.clear();
  2970. //visual_server->finish();
  2971. //memdelete(visual_server);
  2972. }
  2973. void DisplayServerOSX::register_osx_driver() {
  2974. register_create_function("osx", create_func, get_rendering_drivers_func);
  2975. }