display_server_macos.mm 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611
  1. /*************************************************************************/
  2. /* display_server_macos.mm */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_macos.h"
  31. #include "godot_content_view.h"
  32. #include "godot_menu_delegate.h"
  33. #include "godot_menu_item.h"
  34. #include "godot_window.h"
  35. #include "godot_window_delegate.h"
  36. #include "key_mapping_macos.h"
  37. #include "os_macos.h"
  38. #include "tts_macos.h"
  39. #include "core/config/project_settings.h"
  40. #include "core/io/marshalls.h"
  41. #include "core/math/geometry_2d.h"
  42. #include "core/os/keyboard.h"
  43. #include "main/main.h"
  44. #include "scene/resources/texture.h"
  45. #import <Carbon/Carbon.h>
  46. #import <Cocoa/Cocoa.h>
  47. #import <IOKit/IOCFPlugIn.h>
  48. #import <IOKit/IOKitLib.h>
  49. #import <IOKit/hid/IOHIDKeys.h>
  50. #import <IOKit/hid/IOHIDLib.h>
  51. #if defined(GLES3_ENABLED)
  52. #include "drivers/gles3/rasterizer_gles3.h"
  53. #endif
  54. #if defined(VULKAN_ENABLED)
  55. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  56. #endif
  57. const NSMenu *DisplayServerMacOS::_get_menu_root(const String &p_menu_root) const {
  58. const NSMenu *menu = nullptr;
  59. if (p_menu_root == "" || p_menu_root.to_lower() == "_main") {
  60. // Main menu.
  61. menu = [NSApp mainMenu];
  62. } else if (p_menu_root.to_lower() == "_dock") {
  63. // macOS dock menu.
  64. menu = dock_menu;
  65. } else {
  66. // Submenu.
  67. if (submenu.has(p_menu_root)) {
  68. menu = submenu[p_menu_root];
  69. }
  70. }
  71. if (menu == apple_menu) {
  72. // Do not allow to change Apple menu.
  73. return nullptr;
  74. }
  75. return menu;
  76. }
  77. NSMenu *DisplayServerMacOS::_get_menu_root(const String &p_menu_root) {
  78. NSMenu *menu = nullptr;
  79. if (p_menu_root == "" || p_menu_root.to_lower() == "_main") {
  80. // Main menu.
  81. menu = [NSApp mainMenu];
  82. } else if (p_menu_root.to_lower() == "_dock") {
  83. // macOS dock menu.
  84. menu = dock_menu;
  85. } else {
  86. // Submenu.
  87. if (!submenu.has(p_menu_root)) {
  88. NSMenu *n_menu = [[NSMenu alloc] initWithTitle:[NSString stringWithUTF8String:p_menu_root.utf8().get_data()]];
  89. [n_menu setAutoenablesItems:NO];
  90. [n_menu setDelegate:menu_delegate];
  91. submenu[p_menu_root] = n_menu;
  92. }
  93. menu = submenu[p_menu_root];
  94. }
  95. if (menu == apple_menu) {
  96. // Do not allow to change Apple menu.
  97. return nullptr;
  98. }
  99. return menu;
  100. }
  101. DisplayServerMacOS::WindowID DisplayServerMacOS::_create_window(WindowMode p_mode, VSyncMode p_vsync_mode, const Rect2i &p_rect) {
  102. WindowID id;
  103. const float scale = screen_get_max_scale();
  104. {
  105. WindowData wd;
  106. wd.window_delegate = [[GodotWindowDelegate alloc] init];
  107. ERR_FAIL_COND_V_MSG(wd.window_delegate == nil, INVALID_WINDOW_ID, "Can't create a window delegate");
  108. [wd.window_delegate setWindowID:window_id_counter];
  109. Point2i position = p_rect.position;
  110. // OS X native y-coordinate relative to _get_screens_origin() is negative,
  111. // Godot passes a positive value.
  112. position.y *= -1;
  113. position += _get_screens_origin();
  114. // initWithContentRect uses bottom-left corner of the window’s frame as origin.
  115. wd.window_object = [[GodotWindow alloc]
  116. initWithContentRect:NSMakeRect(position.x / scale, (position.y - p_rect.size.height) / scale, p_rect.size.width / scale, p_rect.size.height / scale)
  117. styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable
  118. backing:NSBackingStoreBuffered
  119. defer:NO];
  120. ERR_FAIL_COND_V_MSG(wd.window_object == nil, INVALID_WINDOW_ID, "Can't create a window");
  121. [wd.window_object setWindowID:window_id_counter];
  122. wd.window_view = [[GodotContentView alloc] init];
  123. ERR_FAIL_COND_V_MSG(wd.window_view == nil, INVALID_WINDOW_ID, "Can't create a window view");
  124. [wd.window_view setWindowID:window_id_counter];
  125. [wd.window_view setWantsLayer:TRUE];
  126. [wd.window_object setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
  127. [wd.window_object setContentView:wd.window_view];
  128. [wd.window_object setDelegate:wd.window_delegate];
  129. [wd.window_object setAcceptsMouseMovedEvents:YES];
  130. [wd.window_object setRestorable:NO];
  131. [wd.window_object setColorSpace:[NSColorSpace sRGBColorSpace]];
  132. if ([wd.window_object respondsToSelector:@selector(setTabbingMode:)]) {
  133. [wd.window_object setTabbingMode:NSWindowTabbingModeDisallowed];
  134. }
  135. CALayer *layer = [(NSView *)wd.window_view layer];
  136. if (layer) {
  137. layer.contentsScale = scale;
  138. }
  139. #if defined(VULKAN_ENABLED)
  140. if (context_vulkan) {
  141. Error err = context_vulkan->window_create(window_id_counter, p_vsync_mode, wd.window_view, p_rect.size.width, p_rect.size.height);
  142. ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Can't create a Vulkan context");
  143. }
  144. #endif
  145. #if defined(GLES3_ENABLED)
  146. if (gl_manager) {
  147. Error err = gl_manager->window_create(window_id_counter, wd.window_view, p_rect.size.width, p_rect.size.height);
  148. ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Can't create an OpenGL context");
  149. }
  150. #endif
  151. id = window_id_counter++;
  152. windows[id] = wd;
  153. }
  154. WindowData &wd = windows[id];
  155. window_set_mode(p_mode, id);
  156. const NSRect contentRect = [wd.window_view frame];
  157. wd.size.width = contentRect.size.width * scale;
  158. wd.size.height = contentRect.size.height * scale;
  159. CALayer *layer = [(NSView *)wd.window_view layer];
  160. if (layer) {
  161. layer.contentsScale = scale;
  162. }
  163. #if defined(GLES3_ENABLED)
  164. if (gl_manager) {
  165. gl_manager->window_resize(id, wd.size.width, wd.size.height);
  166. }
  167. #endif
  168. #if defined(VULKAN_ENABLED)
  169. if (context_vulkan) {
  170. context_vulkan->window_resize(id, wd.size.width, wd.size.height);
  171. }
  172. #endif
  173. return id;
  174. }
  175. void DisplayServerMacOS::_update_window_style(WindowData p_wd) {
  176. bool borderless_full = false;
  177. if (p_wd.borderless) {
  178. NSRect frameRect = [p_wd.window_object frame];
  179. NSRect screenRect = [[p_wd.window_object screen] frame];
  180. // Check if our window covers up the screen.
  181. if (frameRect.origin.x <= screenRect.origin.x && frameRect.origin.y <= frameRect.origin.y &&
  182. frameRect.size.width >= screenRect.size.width && frameRect.size.height >= screenRect.size.height) {
  183. borderless_full = true;
  184. }
  185. }
  186. if (borderless_full) {
  187. // If the window covers up the screen set the level to above the main menu and hide on deactivate.
  188. [(NSWindow *)p_wd.window_object setLevel:NSMainMenuWindowLevel + 1];
  189. [(NSWindow *)p_wd.window_object setHidesOnDeactivate:YES];
  190. } else {
  191. // Reset these when our window is not a borderless window that covers up the screen.
  192. if (p_wd.on_top && !p_wd.fullscreen) {
  193. [(NSWindow *)p_wd.window_object setLevel:NSFloatingWindowLevel];
  194. } else {
  195. [(NSWindow *)p_wd.window_object setLevel:NSNormalWindowLevel];
  196. }
  197. [(NSWindow *)p_wd.window_object setHidesOnDeactivate:NO];
  198. }
  199. }
  200. void DisplayServerMacOS::_set_window_per_pixel_transparency_enabled(bool p_enabled, WindowID p_window) {
  201. ERR_FAIL_COND(!windows.has(p_window));
  202. WindowData &wd = windows[p_window];
  203. if (!OS::get_singleton()->is_layered_allowed()) {
  204. return;
  205. }
  206. if (wd.layered_window != p_enabled) {
  207. if (p_enabled) {
  208. [wd.window_object setBackgroundColor:[NSColor clearColor]];
  209. [wd.window_object setOpaque:NO];
  210. [wd.window_object setHasShadow:NO];
  211. CALayer *layer = [(NSView *)wd.window_view layer];
  212. if (layer) {
  213. [layer setBackgroundColor:[NSColor clearColor].CGColor];
  214. [layer setOpaque:NO];
  215. }
  216. #if defined(GLES3_ENABLED)
  217. if (gl_manager) {
  218. gl_manager->window_set_per_pixel_transparency_enabled(p_window, true);
  219. }
  220. #endif
  221. wd.layered_window = true;
  222. } else {
  223. [wd.window_object setBackgroundColor:[NSColor colorWithCalibratedWhite:1 alpha:1]];
  224. [wd.window_object setOpaque:YES];
  225. [wd.window_object setHasShadow:YES];
  226. CALayer *layer = [(NSView *)wd.window_view layer];
  227. if (layer) {
  228. [layer setBackgroundColor:[NSColor colorWithCalibratedWhite:1 alpha:1].CGColor];
  229. [layer setOpaque:YES];
  230. }
  231. #if defined(GLES3_ENABLED)
  232. if (gl_manager) {
  233. gl_manager->window_set_per_pixel_transparency_enabled(p_window, false);
  234. }
  235. #endif
  236. wd.layered_window = false;
  237. }
  238. NSRect frameRect = [wd.window_object frame];
  239. [wd.window_object setFrame:NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width + 1, frameRect.size.height) display:YES];
  240. [wd.window_object setFrame:frameRect display:YES];
  241. }
  242. }
  243. void DisplayServerMacOS::_update_displays_arrangement() {
  244. origin = Point2i();
  245. for (int i = 0; i < get_screen_count(); i++) {
  246. Point2i position = _get_native_screen_position(i);
  247. if (position.x < origin.x) {
  248. origin.x = position.x;
  249. }
  250. if (position.y > origin.y) {
  251. origin.y = position.y;
  252. }
  253. }
  254. displays_arrangement_dirty = false;
  255. }
  256. Point2i DisplayServerMacOS::_get_screens_origin() const {
  257. // Returns the native top-left screen coordinate of the smallest rectangle
  258. // that encompasses all screens. Needed in get_screen_position(),
  259. // window_get_position, and window_set_position()
  260. // to convert between OS X native screen coordinates and the ones expected by Godot.
  261. if (displays_arrangement_dirty) {
  262. const_cast<DisplayServerMacOS *>(this)->_update_displays_arrangement();
  263. }
  264. return origin;
  265. }
  266. Point2i DisplayServerMacOS::_get_native_screen_position(int p_screen) const {
  267. NSArray *screenArray = [NSScreen screens];
  268. if ((NSUInteger)p_screen < [screenArray count]) {
  269. NSRect nsrect = [[screenArray objectAtIndex:p_screen] frame];
  270. // Return the top-left corner of the screen, for OS X the y starts at the bottom.
  271. return Point2i(nsrect.origin.x, nsrect.origin.y + nsrect.size.height) * screen_get_max_scale();
  272. }
  273. return Point2i();
  274. }
  275. void DisplayServerMacOS::_displays_arrangement_changed(CGDirectDisplayID display_id, CGDisplayChangeSummaryFlags flags, void *user_info) {
  276. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  277. if (ds) {
  278. ds->displays_arrangement_dirty = true;
  279. }
  280. }
  281. void DisplayServerMacOS::_dispatch_input_events(const Ref<InputEvent> &p_event) {
  282. ((DisplayServerMacOS *)(get_singleton()))->_dispatch_input_event(p_event);
  283. }
  284. void DisplayServerMacOS::_dispatch_input_event(const Ref<InputEvent> &p_event) {
  285. _THREAD_SAFE_METHOD_
  286. if (!in_dispatch_input_event) {
  287. in_dispatch_input_event = true;
  288. Variant ev = p_event;
  289. Variant *evp = &ev;
  290. Variant ret;
  291. Callable::CallError ce;
  292. {
  293. List<WindowID>::Element *E = popup_list.back();
  294. if (E && Object::cast_to<InputEventKey>(*p_event)) {
  295. // Redirect keyboard input to active popup.
  296. if (windows.has(E->get())) {
  297. Callable callable = windows[E->get()].input_event_callback;
  298. if (callable.is_valid()) {
  299. callable.callp((const Variant **)&evp, 1, ret, ce);
  300. }
  301. }
  302. in_dispatch_input_event = false;
  303. return;
  304. }
  305. }
  306. Ref<InputEventFromWindow> event_from_window = p_event;
  307. if (event_from_window.is_valid() && event_from_window->get_window_id() != INVALID_WINDOW_ID) {
  308. // Send to a window.
  309. if (windows.has(event_from_window->get_window_id())) {
  310. Callable callable = windows[event_from_window->get_window_id()].input_event_callback;
  311. if (callable.is_valid()) {
  312. callable.callp((const Variant **)&evp, 1, ret, ce);
  313. }
  314. }
  315. } else {
  316. // Send to all windows.
  317. for (KeyValue<WindowID, WindowData> &E : windows) {
  318. Callable callable = E.value.input_event_callback;
  319. if (callable.is_valid()) {
  320. callable.callp((const Variant **)&evp, 1, ret, ce);
  321. }
  322. }
  323. }
  324. in_dispatch_input_event = false;
  325. }
  326. }
  327. void DisplayServerMacOS::_push_input(const Ref<InputEvent> &p_event) {
  328. Ref<InputEvent> ev = p_event;
  329. Input::get_singleton()->parse_input_event(ev);
  330. }
  331. void DisplayServerMacOS::_process_key_events() {
  332. Ref<InputEventKey> k;
  333. for (int i = 0; i < key_event_pos; i++) {
  334. const KeyEvent &ke = key_event_buffer[i];
  335. if (ke.raw) {
  336. // Non IME input - no composite characters, pass events as is.
  337. k.instantiate();
  338. k->set_window_id(ke.window_id);
  339. get_key_modifier_state(ke.macos_state, k);
  340. k->set_pressed(ke.pressed);
  341. k->set_echo(ke.echo);
  342. k->set_keycode(ke.keycode);
  343. k->set_physical_keycode((Key)ke.physical_keycode);
  344. k->set_unicode(ke.unicode);
  345. _push_input(k);
  346. } else {
  347. // IME input.
  348. if ((i == 0 && ke.keycode == Key::NONE) || (i > 0 && key_event_buffer[i - 1].keycode == Key::NONE)) {
  349. k.instantiate();
  350. k->set_window_id(ke.window_id);
  351. get_key_modifier_state(ke.macos_state, k);
  352. k->set_pressed(ke.pressed);
  353. k->set_echo(ke.echo);
  354. k->set_keycode(Key::NONE);
  355. k->set_physical_keycode(Key::NONE);
  356. k->set_unicode(ke.unicode);
  357. _push_input(k);
  358. }
  359. if (ke.keycode != Key::NONE) {
  360. k.instantiate();
  361. k->set_window_id(ke.window_id);
  362. get_key_modifier_state(ke.macos_state, k);
  363. k->set_pressed(ke.pressed);
  364. k->set_echo(ke.echo);
  365. k->set_keycode(ke.keycode);
  366. k->set_physical_keycode((Key)ke.physical_keycode);
  367. if (i + 1 < key_event_pos && key_event_buffer[i + 1].keycode == Key::NONE) {
  368. k->set_unicode(key_event_buffer[i + 1].unicode);
  369. }
  370. _push_input(k);
  371. }
  372. }
  373. }
  374. key_event_pos = 0;
  375. }
  376. void DisplayServerMacOS::_update_keyboard_layouts() {
  377. kbd_layouts.clear();
  378. current_layout = 0;
  379. TISInputSourceRef cur_source = TISCopyCurrentKeyboardInputSource();
  380. NSString *cur_name = (__bridge NSString *)TISGetInputSourceProperty(cur_source, kTISPropertyLocalizedName);
  381. CFRelease(cur_source);
  382. // Enum IME layouts.
  383. NSDictionary *filter_ime = @{ (NSString *)kTISPropertyInputSourceType : (NSString *)kTISTypeKeyboardInputMode };
  384. NSArray *list_ime = (__bridge NSArray *)TISCreateInputSourceList((__bridge CFDictionaryRef)filter_ime, false);
  385. for (NSUInteger i = 0; i < [list_ime count]; i++) {
  386. LayoutInfo ly;
  387. NSString *name = (__bridge NSString *)TISGetInputSourceProperty((__bridge TISInputSourceRef)[list_ime objectAtIndex:i], kTISPropertyLocalizedName);
  388. ly.name.parse_utf8([name UTF8String]);
  389. NSArray *langs = (__bridge NSArray *)TISGetInputSourceProperty((__bridge TISInputSourceRef)[list_ime objectAtIndex:i], kTISPropertyInputSourceLanguages);
  390. ly.code.parse_utf8([(NSString *)[langs objectAtIndex:0] UTF8String]);
  391. kbd_layouts.push_back(ly);
  392. if ([name isEqualToString:cur_name]) {
  393. current_layout = kbd_layouts.size() - 1;
  394. }
  395. }
  396. // Enum plain keyboard layouts.
  397. NSDictionary *filter_kbd = @{ (NSString *)kTISPropertyInputSourceType : (NSString *)kTISTypeKeyboardLayout };
  398. NSArray *list_kbd = (__bridge NSArray *)TISCreateInputSourceList((__bridge CFDictionaryRef)filter_kbd, false);
  399. for (NSUInteger i = 0; i < [list_kbd count]; i++) {
  400. LayoutInfo ly;
  401. NSString *name = (__bridge NSString *)TISGetInputSourceProperty((__bridge TISInputSourceRef)[list_kbd objectAtIndex:i], kTISPropertyLocalizedName);
  402. ly.name.parse_utf8([name UTF8String]);
  403. NSArray *langs = (__bridge NSArray *)TISGetInputSourceProperty((__bridge TISInputSourceRef)[list_kbd objectAtIndex:i], kTISPropertyInputSourceLanguages);
  404. ly.code.parse_utf8([(NSString *)[langs objectAtIndex:0] UTF8String]);
  405. kbd_layouts.push_back(ly);
  406. if ([name isEqualToString:cur_name]) {
  407. current_layout = kbd_layouts.size() - 1;
  408. }
  409. }
  410. keyboard_layout_dirty = false;
  411. }
  412. void DisplayServerMacOS::_keyboard_layout_changed(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef user_info) {
  413. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  414. if (ds) {
  415. ds->keyboard_layout_dirty = true;
  416. }
  417. }
  418. NSImage *DisplayServerMacOS::_convert_to_nsimg(Ref<Image> &p_image) const {
  419. p_image->convert(Image::FORMAT_RGBA8);
  420. NSBitmapImageRep *imgrep = [[NSBitmapImageRep alloc]
  421. initWithBitmapDataPlanes:NULL
  422. pixelsWide:p_image->get_width()
  423. pixelsHigh:p_image->get_height()
  424. bitsPerSample:8
  425. samplesPerPixel:4
  426. hasAlpha:YES
  427. isPlanar:NO
  428. colorSpaceName:NSDeviceRGBColorSpace
  429. bytesPerRow:int(p_image->get_width()) * 4
  430. bitsPerPixel:32];
  431. ERR_FAIL_COND_V(imgrep == nil, nil);
  432. uint8_t *pixels = [imgrep bitmapData];
  433. int len = p_image->get_width() * p_image->get_height();
  434. const uint8_t *r = p_image->get_data().ptr();
  435. /* Premultiply the alpha channel */
  436. for (int i = 0; i < len; i++) {
  437. uint8_t alpha = r[i * 4 + 3];
  438. pixels[i * 4 + 0] = (uint8_t)(((uint16_t)r[i * 4 + 0] * alpha) / 255);
  439. pixels[i * 4 + 1] = (uint8_t)(((uint16_t)r[i * 4 + 1] * alpha) / 255);
  440. pixels[i * 4 + 2] = (uint8_t)(((uint16_t)r[i * 4 + 2] * alpha) / 255);
  441. pixels[i * 4 + 3] = alpha;
  442. }
  443. NSImage *nsimg = [[NSImage alloc] initWithSize:NSMakeSize(p_image->get_width(), p_image->get_height())];
  444. ERR_FAIL_COND_V(nsimg == nil, nil);
  445. [nsimg addRepresentation:imgrep];
  446. return nsimg;
  447. }
  448. NSCursor *DisplayServerMacOS::_cursor_from_selector(SEL p_selector, SEL p_fallback) {
  449. if ([NSCursor respondsToSelector:p_selector]) {
  450. id object = [NSCursor performSelector:p_selector];
  451. if ([object isKindOfClass:[NSCursor class]]) {
  452. return object;
  453. }
  454. }
  455. if (p_fallback) {
  456. // Fallback should be a reasonable default, no need to check.
  457. return [NSCursor performSelector:p_fallback];
  458. }
  459. return [NSCursor arrowCursor];
  460. }
  461. NSMenu *DisplayServerMacOS::get_dock_menu() const {
  462. return dock_menu;
  463. }
  464. void DisplayServerMacOS::menu_callback(id p_sender) {
  465. if (![p_sender representedObject]) {
  466. return;
  467. }
  468. GodotMenuItem *value = [p_sender representedObject];
  469. if (value) {
  470. if (value->max_states > 0) {
  471. value->state++;
  472. if (value->state >= value->max_states) {
  473. value->state = 0;
  474. }
  475. }
  476. if (value->checkable_type == CHECKABLE_TYPE_CHECK_BOX) {
  477. if ([p_sender state] == NSControlStateValueOff) {
  478. [p_sender setState:NSControlStateValueOn];
  479. } else {
  480. [p_sender setState:NSControlStateValueOff];
  481. }
  482. }
  483. if (value->callback != Callable()) {
  484. Variant tag = value->meta;
  485. Variant *tagp = &tag;
  486. Variant ret;
  487. Callable::CallError ce;
  488. value->callback.callp((const Variant **)&tagp, 1, ret, ce);
  489. }
  490. }
  491. }
  492. bool DisplayServerMacOS::has_window(WindowID p_window) const {
  493. return windows.has(p_window);
  494. }
  495. DisplayServerMacOS::WindowData &DisplayServerMacOS::get_window(WindowID p_window) {
  496. return windows[p_window];
  497. }
  498. void DisplayServerMacOS::send_event(NSEvent *p_event) {
  499. // Special case handling of command-period, which is traditionally a special
  500. // shortcut in macOS and doesn't arrive at our regular keyDown handler.
  501. if ([p_event type] == NSEventTypeKeyDown) {
  502. if (([p_event modifierFlags] & NSEventModifierFlagCommand) && [p_event keyCode] == 0x2f) {
  503. Ref<InputEventKey> k;
  504. k.instantiate();
  505. get_key_modifier_state([p_event modifierFlags], k);
  506. k->set_window_id(DisplayServerMacOS::INVALID_WINDOW_ID);
  507. k->set_pressed(true);
  508. k->set_keycode(Key::PERIOD);
  509. k->set_physical_keycode(Key::PERIOD);
  510. k->set_echo([p_event isARepeat]);
  511. Input::get_singleton()->parse_input_event(k);
  512. }
  513. }
  514. }
  515. void DisplayServerMacOS::send_window_event(const WindowData &wd, WindowEvent p_event) {
  516. _THREAD_SAFE_METHOD_
  517. if (!wd.event_callback.is_null()) {
  518. Variant event = int(p_event);
  519. Variant *eventp = &event;
  520. Variant ret;
  521. Callable::CallError ce;
  522. wd.event_callback.callp((const Variant **)&eventp, 1, ret, ce);
  523. }
  524. }
  525. void DisplayServerMacOS::release_pressed_events() {
  526. _THREAD_SAFE_METHOD_
  527. if (Input::get_singleton()) {
  528. Input::get_singleton()->release_pressed_events();
  529. }
  530. }
  531. void DisplayServerMacOS::get_key_modifier_state(unsigned int p_macos_state, Ref<InputEventWithModifiers> r_state) const {
  532. r_state->set_shift_pressed((p_macos_state & NSEventModifierFlagShift));
  533. r_state->set_ctrl_pressed((p_macos_state & NSEventModifierFlagControl));
  534. r_state->set_alt_pressed((p_macos_state & NSEventModifierFlagOption));
  535. r_state->set_meta_pressed((p_macos_state & NSEventModifierFlagCommand));
  536. }
  537. void DisplayServerMacOS::update_mouse_pos(DisplayServerMacOS::WindowData &p_wd, NSPoint p_location_in_window) {
  538. const NSRect content_rect = [p_wd.window_view frame];
  539. const float scale = screen_get_max_scale();
  540. p_wd.mouse_pos.x = p_location_in_window.x * scale;
  541. p_wd.mouse_pos.y = (content_rect.size.height - p_location_in_window.y) * scale;
  542. Input::get_singleton()->set_mouse_position(p_wd.mouse_pos);
  543. }
  544. void DisplayServerMacOS::push_to_key_event_buffer(const DisplayServerMacOS::KeyEvent &p_event) {
  545. if (key_event_pos >= key_event_buffer.size()) {
  546. key_event_buffer.resize(1 + key_event_pos);
  547. }
  548. key_event_buffer.write[key_event_pos++] = p_event;
  549. }
  550. void DisplayServerMacOS::update_im_text(const Point2i &p_selection, const String &p_text) {
  551. im_selection = p_selection;
  552. im_text = p_text;
  553. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_IME_UPDATE);
  554. }
  555. void DisplayServerMacOS::set_last_focused_window(WindowID p_window) {
  556. last_focused_window = p_window;
  557. }
  558. void DisplayServerMacOS::set_is_resizing(bool p_is_resizing) {
  559. is_resizing = p_is_resizing;
  560. }
  561. bool DisplayServerMacOS::get_is_resizing() const {
  562. return is_resizing;
  563. }
  564. void DisplayServerMacOS::window_update(WindowID p_window) {
  565. #if defined(GLES3_ENABLED)
  566. if (gl_manager) {
  567. gl_manager->window_update(p_window);
  568. }
  569. #endif
  570. }
  571. void DisplayServerMacOS::window_destroy(WindowID p_window) {
  572. #if defined(GLES3_ENABLED)
  573. if (gl_manager) {
  574. gl_manager->window_destroy(p_window);
  575. }
  576. #endif
  577. #ifdef VULKAN_ENABLED
  578. if (context_vulkan) {
  579. context_vulkan->window_destroy(p_window);
  580. }
  581. #endif
  582. windows.erase(p_window);
  583. }
  584. void DisplayServerMacOS::window_resize(WindowID p_window, int p_width, int p_height) {
  585. #if defined(GLES3_ENABLED)
  586. if (gl_manager) {
  587. gl_manager->window_resize(p_window, p_width, p_height);
  588. }
  589. #endif
  590. #if defined(VULKAN_ENABLED)
  591. if (context_vulkan) {
  592. context_vulkan->window_resize(p_window, p_width, p_height);
  593. }
  594. #endif
  595. }
  596. bool DisplayServerMacOS::has_feature(Feature p_feature) const {
  597. switch (p_feature) {
  598. case FEATURE_GLOBAL_MENU:
  599. case FEATURE_SUBWINDOWS:
  600. //case FEATURE_TOUCHSCREEN:
  601. case FEATURE_MOUSE:
  602. case FEATURE_MOUSE_WARP:
  603. case FEATURE_CLIPBOARD:
  604. case FEATURE_CURSOR_SHAPE:
  605. case FEATURE_CUSTOM_CURSOR_SHAPE:
  606. case FEATURE_NATIVE_DIALOG:
  607. case FEATURE_IME:
  608. case FEATURE_WINDOW_TRANSPARENCY:
  609. case FEATURE_HIDPI:
  610. case FEATURE_ICON:
  611. case FEATURE_NATIVE_ICON:
  612. //case FEATURE_KEEP_SCREEN_ON:
  613. case FEATURE_SWAP_BUFFERS:
  614. case FEATURE_TEXT_TO_SPEECH:
  615. case FEATURE_EXTEND_TO_TITLE:
  616. return true;
  617. default: {
  618. }
  619. }
  620. return false;
  621. }
  622. String DisplayServerMacOS::get_name() const {
  623. return "macOS";
  624. }
  625. bool DisplayServerMacOS::_has_help_menu() const {
  626. if ([NSApp helpMenu]) {
  627. return true;
  628. } else {
  629. NSMenu *menu = [NSApp mainMenu];
  630. const NSMenuItem *menu_item = [menu itemAtIndex:[menu numberOfItems] - 1];
  631. if (menu_item) {
  632. String menu_name = String::utf8([[menu_item title] UTF8String]);
  633. if (menu_name == "Help" || menu_name == RTR("Help")) {
  634. return true;
  635. }
  636. }
  637. return false;
  638. }
  639. }
  640. NSMenuItem *DisplayServerMacOS::_menu_add_item(const String &p_menu_root, const String &p_label, Key p_accel, int p_index, int *r_out) {
  641. NSMenu *menu = _get_menu_root(p_menu_root);
  642. if (menu) {
  643. String keycode = KeyMappingMacOS::keycode_get_native_string(p_accel & KeyModifierMask::CODE_MASK);
  644. NSMenuItem *menu_item;
  645. int item_count = ((menu == [NSApp mainMenu]) && _has_help_menu()) ? [menu numberOfItems] - 1 : [menu numberOfItems];
  646. if ((menu == [NSApp mainMenu]) && (p_label == "Help" || p_label == RTR("Help"))) {
  647. p_index = [menu numberOfItems];
  648. } else if (p_index < 0) {
  649. p_index = item_count;
  650. } else {
  651. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  652. p_index++;
  653. }
  654. p_index = CLAMP(p_index, 0, item_count);
  655. }
  656. menu_item = [menu insertItemWithTitle:[NSString stringWithUTF8String:p_label.utf8().get_data()] action:@selector(globalMenuCallback:) keyEquivalent:[NSString stringWithUTF8String:keycode.utf8().get_data()] atIndex:p_index];
  657. *r_out = (menu == [NSApp mainMenu]) ? p_index - 1 : p_index;
  658. return menu_item;
  659. }
  660. return nullptr;
  661. }
  662. int DisplayServerMacOS::global_menu_add_item(const String &p_menu_root, const String &p_label, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
  663. _THREAD_SAFE_METHOD_
  664. int out = -1;
  665. NSMenuItem *menu_item = _menu_add_item(p_menu_root, p_label, p_accel, p_index, &out);
  666. if (menu_item) {
  667. GodotMenuItem *obj = [[GodotMenuItem alloc] init];
  668. obj->callback = p_callback;
  669. obj->key_callback = p_key_callback;
  670. obj->meta = p_tag;
  671. obj->checkable_type = CHECKABLE_TYPE_NONE;
  672. obj->max_states = 0;
  673. obj->state = 0;
  674. [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
  675. [menu_item setRepresentedObject:obj];
  676. }
  677. return out;
  678. }
  679. int DisplayServerMacOS::global_menu_add_check_item(const String &p_menu_root, const String &p_label, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
  680. _THREAD_SAFE_METHOD_
  681. int out = -1;
  682. NSMenuItem *menu_item = _menu_add_item(p_menu_root, p_label, p_accel, p_index, &out);
  683. if (menu_item) {
  684. GodotMenuItem *obj = [[GodotMenuItem alloc] init];
  685. obj->callback = p_callback;
  686. obj->key_callback = p_key_callback;
  687. obj->meta = p_tag;
  688. obj->checkable_type = CHECKABLE_TYPE_CHECK_BOX;
  689. obj->max_states = 0;
  690. obj->state = 0;
  691. [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
  692. [menu_item setRepresentedObject:obj];
  693. }
  694. return out;
  695. }
  696. int DisplayServerMacOS::global_menu_add_icon_item(const String &p_menu_root, const Ref<Texture2D> &p_icon, const String &p_label, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
  697. _THREAD_SAFE_METHOD_
  698. int out = -1;
  699. NSMenuItem *menu_item = _menu_add_item(p_menu_root, p_label, p_accel, p_index, &out);
  700. if (menu_item) {
  701. GodotMenuItem *obj = [[GodotMenuItem alloc] init];
  702. obj->callback = p_callback;
  703. obj->key_callback = p_key_callback;
  704. obj->meta = p_tag;
  705. obj->checkable_type = CHECKABLE_TYPE_NONE;
  706. obj->max_states = 0;
  707. obj->state = 0;
  708. if (p_icon.is_valid()) {
  709. obj->img = p_icon->get_image();
  710. obj->img = obj->img->duplicate();
  711. if (obj->img->is_compressed()) {
  712. obj->img->decompress();
  713. }
  714. obj->img->resize(16, 16, Image::INTERPOLATE_LANCZOS);
  715. [menu_item setImage:_convert_to_nsimg(obj->img)];
  716. }
  717. [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
  718. [menu_item setRepresentedObject:obj];
  719. }
  720. return out;
  721. }
  722. int DisplayServerMacOS::global_menu_add_icon_check_item(const String &p_menu_root, const Ref<Texture2D> &p_icon, const String &p_label, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
  723. _THREAD_SAFE_METHOD_
  724. int out = -1;
  725. NSMenuItem *menu_item = _menu_add_item(p_menu_root, p_label, p_accel, p_index, &out);
  726. if (menu_item) {
  727. GodotMenuItem *obj = [[GodotMenuItem alloc] init];
  728. obj->callback = p_callback;
  729. obj->key_callback = p_key_callback;
  730. obj->meta = p_tag;
  731. obj->checkable_type = CHECKABLE_TYPE_CHECK_BOX;
  732. obj->max_states = 0;
  733. obj->state = 0;
  734. if (p_icon.is_valid()) {
  735. obj->img = p_icon->get_image();
  736. obj->img = obj->img->duplicate();
  737. if (obj->img->is_compressed()) {
  738. obj->img->decompress();
  739. }
  740. obj->img->resize(16, 16, Image::INTERPOLATE_LANCZOS);
  741. [menu_item setImage:_convert_to_nsimg(obj->img)];
  742. }
  743. [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
  744. [menu_item setRepresentedObject:obj];
  745. }
  746. return out;
  747. }
  748. int DisplayServerMacOS::global_menu_add_radio_check_item(const String &p_menu_root, const String &p_label, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
  749. _THREAD_SAFE_METHOD_
  750. int out = -1;
  751. NSMenuItem *menu_item = _menu_add_item(p_menu_root, p_label, p_accel, p_index, &out);
  752. if (menu_item) {
  753. GodotMenuItem *obj = [[GodotMenuItem alloc] init];
  754. obj->callback = p_callback;
  755. obj->key_callback = p_key_callback;
  756. obj->meta = p_tag;
  757. obj->checkable_type = CHECKABLE_TYPE_RADIO_BUTTON;
  758. obj->max_states = 0;
  759. obj->state = 0;
  760. [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
  761. [menu_item setRepresentedObject:obj];
  762. }
  763. return out;
  764. }
  765. int DisplayServerMacOS::global_menu_add_icon_radio_check_item(const String &p_menu_root, const Ref<Texture2D> &p_icon, const String &p_label, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
  766. _THREAD_SAFE_METHOD_
  767. int out = -1;
  768. NSMenuItem *menu_item = _menu_add_item(p_menu_root, p_label, p_accel, p_index, &out);
  769. if (menu_item) {
  770. GodotMenuItem *obj = [[GodotMenuItem alloc] init];
  771. obj->callback = p_callback;
  772. obj->key_callback = p_key_callback;
  773. obj->meta = p_tag;
  774. obj->checkable_type = CHECKABLE_TYPE_RADIO_BUTTON;
  775. obj->max_states = 0;
  776. obj->state = 0;
  777. if (p_icon.is_valid()) {
  778. obj->img = p_icon->get_image();
  779. obj->img = obj->img->duplicate();
  780. if (obj->img->is_compressed()) {
  781. obj->img->decompress();
  782. }
  783. obj->img->resize(16, 16, Image::INTERPOLATE_LANCZOS);
  784. [menu_item setImage:_convert_to_nsimg(obj->img)];
  785. }
  786. [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
  787. [menu_item setRepresentedObject:obj];
  788. }
  789. return out;
  790. }
  791. int DisplayServerMacOS::global_menu_add_multistate_item(const String &p_menu_root, const String &p_label, int p_max_states, int p_default_state, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
  792. _THREAD_SAFE_METHOD_
  793. int out = -1;
  794. NSMenuItem *menu_item = _menu_add_item(p_menu_root, p_label, p_accel, p_index, &out);
  795. if (menu_item) {
  796. GodotMenuItem *obj = [[GodotMenuItem alloc] init];
  797. obj->callback = p_callback;
  798. obj->key_callback = p_key_callback;
  799. obj->meta = p_tag;
  800. obj->checkable_type = CHECKABLE_TYPE_NONE;
  801. obj->max_states = p_max_states;
  802. obj->state = p_default_state;
  803. [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
  804. [menu_item setRepresentedObject:obj];
  805. }
  806. return out;
  807. }
  808. int DisplayServerMacOS::global_menu_add_submenu_item(const String &p_menu_root, const String &p_label, const String &p_submenu, int p_index) {
  809. _THREAD_SAFE_METHOD_
  810. NSMenu *menu = _get_menu_root(p_menu_root);
  811. NSMenu *sub_menu = _get_menu_root(p_submenu);
  812. int out = -1;
  813. if (menu && sub_menu) {
  814. if (sub_menu == menu) {
  815. ERR_PRINT("Can't set submenu to self!");
  816. return -1;
  817. }
  818. if ([sub_menu supermenu]) {
  819. ERR_PRINT("Can't set submenu to menu that is already a submenu of some other menu!");
  820. return -1;
  821. }
  822. NSMenuItem *menu_item;
  823. int item_count = ((menu == [NSApp mainMenu]) && _has_help_menu()) ? [menu numberOfItems] - 1 : [menu numberOfItems];
  824. if ((menu == [NSApp mainMenu]) && (p_label == "Help" || p_label == RTR("Help"))) {
  825. p_index = [menu numberOfItems];
  826. } else if (p_index < 0) {
  827. p_index = item_count;
  828. } else {
  829. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  830. p_index++;
  831. }
  832. p_index = CLAMP(p_index, 0, item_count);
  833. }
  834. menu_item = [menu insertItemWithTitle:[NSString stringWithUTF8String:p_label.utf8().get_data()] action:nil keyEquivalent:@"" atIndex:p_index];
  835. out = (menu == [NSApp mainMenu]) ? p_index - 1 : p_index;
  836. GodotMenuItem *obj = [[GodotMenuItem alloc] init];
  837. obj->callback = Callable();
  838. obj->checkable_type = CHECKABLE_TYPE_NONE;
  839. obj->max_states = 0;
  840. obj->state = 0;
  841. [menu_item setRepresentedObject:obj];
  842. [sub_menu setTitle:[NSString stringWithUTF8String:p_label.utf8().get_data()]];
  843. [menu setSubmenu:sub_menu forItem:menu_item];
  844. }
  845. return out;
  846. }
  847. int DisplayServerMacOS::global_menu_add_separator(const String &p_menu_root, int p_index) {
  848. _THREAD_SAFE_METHOD_
  849. NSMenu *menu = _get_menu_root(p_menu_root);
  850. if (menu) {
  851. if (menu == [NSApp mainMenu]) { // Do not add separators into main menu.
  852. return -1;
  853. }
  854. if (p_index < 0) {
  855. p_index = [menu numberOfItems];
  856. } else {
  857. p_index = CLAMP(p_index, 0, [menu numberOfItems]);
  858. }
  859. [menu insertItem:[NSMenuItem separatorItem] atIndex:p_index];
  860. return p_index;
  861. }
  862. return -1;
  863. }
  864. int DisplayServerMacOS::global_menu_get_item_index_from_text(const String &p_menu_root, const String &p_text) const {
  865. _THREAD_SAFE_METHOD_
  866. const NSMenu *menu = _get_menu_root(p_menu_root);
  867. if (menu) {
  868. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  869. return [menu indexOfItemWithTitle:[NSString stringWithUTF8String:p_text.utf8().get_data()]] - 1;
  870. } else {
  871. return [menu indexOfItemWithTitle:[NSString stringWithUTF8String:p_text.utf8().get_data()]];
  872. }
  873. }
  874. return -1;
  875. }
  876. int DisplayServerMacOS::global_menu_get_item_index_from_tag(const String &p_menu_root, const Variant &p_tag) const {
  877. _THREAD_SAFE_METHOD_
  878. const NSMenu *menu = _get_menu_root(p_menu_root);
  879. if (menu) {
  880. for (NSInteger i = (menu == [NSApp mainMenu]) ? 1 : 0; i < [menu numberOfItems]; i++) {
  881. const NSMenuItem *menu_item = [menu itemAtIndex:i];
  882. if (menu_item) {
  883. const GodotMenuItem *obj = [menu_item representedObject];
  884. if (obj && obj->meta == p_tag) {
  885. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  886. return i - 1;
  887. } else {
  888. return i;
  889. }
  890. }
  891. }
  892. }
  893. }
  894. return -1;
  895. }
  896. bool DisplayServerMacOS::global_menu_is_item_checked(const String &p_menu_root, int p_idx) const {
  897. _THREAD_SAFE_METHOD_
  898. const NSMenu *menu = _get_menu_root(p_menu_root);
  899. if (menu) {
  900. ERR_FAIL_COND_V(p_idx < 0, false);
  901. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  902. p_idx++;
  903. }
  904. ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], false);
  905. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  906. if (menu_item) {
  907. return ([menu_item state] == NSControlStateValueOn);
  908. }
  909. }
  910. return false;
  911. }
  912. bool DisplayServerMacOS::global_menu_is_item_checkable(const String &p_menu_root, int p_idx) const {
  913. _THREAD_SAFE_METHOD_
  914. const NSMenu *menu = _get_menu_root(p_menu_root);
  915. if (menu) {
  916. ERR_FAIL_COND_V(p_idx < 0, false);
  917. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  918. p_idx++;
  919. }
  920. ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], false);
  921. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  922. if (menu_item) {
  923. GodotMenuItem *obj = [menu_item representedObject];
  924. if (obj) {
  925. return obj->checkable_type == CHECKABLE_TYPE_CHECK_BOX;
  926. }
  927. }
  928. }
  929. return false;
  930. }
  931. bool DisplayServerMacOS::global_menu_is_item_radio_checkable(const String &p_menu_root, int p_idx) const {
  932. _THREAD_SAFE_METHOD_
  933. const NSMenu *menu = _get_menu_root(p_menu_root);
  934. if (menu) {
  935. ERR_FAIL_COND_V(p_idx < 0, false);
  936. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  937. p_idx++;
  938. }
  939. ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], false);
  940. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  941. if (menu_item) {
  942. GodotMenuItem *obj = [menu_item representedObject];
  943. if (obj) {
  944. return obj->checkable_type == CHECKABLE_TYPE_RADIO_BUTTON;
  945. }
  946. }
  947. }
  948. return false;
  949. }
  950. Callable DisplayServerMacOS::global_menu_get_item_callback(const String &p_menu_root, int p_idx) const {
  951. _THREAD_SAFE_METHOD_
  952. const NSMenu *menu = _get_menu_root(p_menu_root);
  953. if (menu) {
  954. ERR_FAIL_COND_V(p_idx < 0, Callable());
  955. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  956. p_idx++;
  957. }
  958. ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], Callable());
  959. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  960. if (menu_item) {
  961. GodotMenuItem *obj = [menu_item representedObject];
  962. if (obj) {
  963. return obj->callback;
  964. }
  965. }
  966. }
  967. return Callable();
  968. }
  969. Callable DisplayServerMacOS::global_menu_get_item_key_callback(const String &p_menu_root, int p_idx) const {
  970. _THREAD_SAFE_METHOD_
  971. const NSMenu *menu = _get_menu_root(p_menu_root);
  972. if (menu) {
  973. ERR_FAIL_COND_V(p_idx < 0, Callable());
  974. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  975. p_idx++;
  976. }
  977. ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], Callable());
  978. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  979. if (menu_item) {
  980. GodotMenuItem *obj = [menu_item representedObject];
  981. if (obj) {
  982. return obj->key_callback;
  983. }
  984. }
  985. }
  986. return Callable();
  987. }
  988. Variant DisplayServerMacOS::global_menu_get_item_tag(const String &p_menu_root, int p_idx) const {
  989. _THREAD_SAFE_METHOD_
  990. const NSMenu *menu = _get_menu_root(p_menu_root);
  991. if (menu) {
  992. ERR_FAIL_COND_V(p_idx < 0, Variant());
  993. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  994. p_idx++;
  995. }
  996. ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], Variant());
  997. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  998. if (menu_item) {
  999. GodotMenuItem *obj = [menu_item representedObject];
  1000. if (obj) {
  1001. return obj->meta;
  1002. }
  1003. }
  1004. }
  1005. return Variant();
  1006. }
  1007. String DisplayServerMacOS::global_menu_get_item_text(const String &p_menu_root, int p_idx) const {
  1008. _THREAD_SAFE_METHOD_
  1009. const NSMenu *menu = _get_menu_root(p_menu_root);
  1010. if (menu) {
  1011. ERR_FAIL_COND_V(p_idx < 0, String());
  1012. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1013. p_idx++;
  1014. }
  1015. ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], String());
  1016. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1017. if (menu_item) {
  1018. return String::utf8([[menu_item title] UTF8String]);
  1019. }
  1020. }
  1021. return String();
  1022. }
  1023. String DisplayServerMacOS::global_menu_get_item_submenu(const String &p_menu_root, int p_idx) const {
  1024. _THREAD_SAFE_METHOD_
  1025. const NSMenu *menu = _get_menu_root(p_menu_root);
  1026. if (menu) {
  1027. ERR_FAIL_COND_V(p_idx < 0, String());
  1028. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1029. p_idx++;
  1030. }
  1031. ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], String());
  1032. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1033. if (menu_item) {
  1034. const NSMenu *sub_menu = [menu_item submenu];
  1035. if (sub_menu) {
  1036. for (const KeyValue<String, NSMenu *> &E : submenu) {
  1037. if (E.value == sub_menu) {
  1038. return E.key;
  1039. }
  1040. }
  1041. }
  1042. }
  1043. }
  1044. return String();
  1045. }
  1046. Key DisplayServerMacOS::global_menu_get_item_accelerator(const String &p_menu_root, int p_idx) const {
  1047. _THREAD_SAFE_METHOD_
  1048. const NSMenu *menu = _get_menu_root(p_menu_root);
  1049. if (menu) {
  1050. ERR_FAIL_COND_V(p_idx < 0, Key::NONE);
  1051. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1052. p_idx++;
  1053. }
  1054. ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], Key::NONE);
  1055. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1056. if (menu_item) {
  1057. String ret = String::utf8([[menu_item keyEquivalent] UTF8String]);
  1058. Key keycode = find_keycode(ret);
  1059. NSUInteger mask = [menu_item keyEquivalentModifierMask];
  1060. if (mask & NSEventModifierFlagControl) {
  1061. keycode |= KeyModifierMask::CTRL;
  1062. }
  1063. if (mask & NSEventModifierFlagOption) {
  1064. keycode |= KeyModifierMask::ALT;
  1065. }
  1066. if (mask & NSEventModifierFlagShift) {
  1067. keycode |= KeyModifierMask::SHIFT;
  1068. }
  1069. if (mask & NSEventModifierFlagCommand) {
  1070. keycode |= KeyModifierMask::META;
  1071. }
  1072. if (mask & NSEventModifierFlagNumericPad) {
  1073. keycode |= KeyModifierMask::KPAD;
  1074. }
  1075. return keycode;
  1076. }
  1077. }
  1078. return Key::NONE;
  1079. }
  1080. bool DisplayServerMacOS::global_menu_is_item_disabled(const String &p_menu_root, int p_idx) const {
  1081. _THREAD_SAFE_METHOD_
  1082. const NSMenu *menu = _get_menu_root(p_menu_root);
  1083. if (menu) {
  1084. ERR_FAIL_COND_V(p_idx < 0, false);
  1085. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1086. p_idx++;
  1087. }
  1088. ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], false);
  1089. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1090. if (menu_item) {
  1091. return ![menu_item isEnabled];
  1092. }
  1093. }
  1094. return false;
  1095. }
  1096. String DisplayServerMacOS::global_menu_get_item_tooltip(const String &p_menu_root, int p_idx) const {
  1097. _THREAD_SAFE_METHOD_
  1098. const NSMenu *menu = _get_menu_root(p_menu_root);
  1099. if (menu) {
  1100. ERR_FAIL_COND_V(p_idx < 0, String());
  1101. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1102. p_idx++;
  1103. }
  1104. ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], String());
  1105. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1106. if (menu_item) {
  1107. return String::utf8([[menu_item toolTip] UTF8String]);
  1108. }
  1109. }
  1110. return String();
  1111. }
  1112. int DisplayServerMacOS::global_menu_get_item_state(const String &p_menu_root, int p_idx) const {
  1113. _THREAD_SAFE_METHOD_
  1114. const NSMenu *menu = _get_menu_root(p_menu_root);
  1115. if (menu) {
  1116. ERR_FAIL_COND_V(p_idx < 0, 0);
  1117. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1118. p_idx++;
  1119. }
  1120. ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], 0);
  1121. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1122. if (menu_item) {
  1123. GodotMenuItem *obj = [menu_item representedObject];
  1124. if (obj) {
  1125. return obj->state;
  1126. }
  1127. }
  1128. }
  1129. return 0;
  1130. }
  1131. int DisplayServerMacOS::global_menu_get_item_max_states(const String &p_menu_root, int p_idx) const {
  1132. _THREAD_SAFE_METHOD_
  1133. const NSMenu *menu = _get_menu_root(p_menu_root);
  1134. if (menu) {
  1135. ERR_FAIL_COND_V(p_idx < 0, 0);
  1136. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1137. p_idx++;
  1138. }
  1139. ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], 0);
  1140. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1141. if (menu_item) {
  1142. GodotMenuItem *obj = [menu_item representedObject];
  1143. if (obj) {
  1144. return obj->max_states;
  1145. }
  1146. }
  1147. }
  1148. return 0;
  1149. }
  1150. Ref<Texture2D> DisplayServerMacOS::global_menu_get_item_icon(const String &p_menu_root, int p_idx) const {
  1151. _THREAD_SAFE_METHOD_
  1152. const NSMenu *menu = _get_menu_root(p_menu_root);
  1153. if (menu) {
  1154. ERR_FAIL_COND_V(p_idx < 0, Ref<Texture2D>());
  1155. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1156. p_idx++;
  1157. }
  1158. ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], Ref<Texture2D>());
  1159. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1160. if (menu_item) {
  1161. GodotMenuItem *obj = [menu_item representedObject];
  1162. if (obj) {
  1163. if (obj->img.is_valid()) {
  1164. return ImageTexture::create_from_image(obj->img);
  1165. }
  1166. }
  1167. }
  1168. }
  1169. return Ref<Texture2D>();
  1170. }
  1171. int DisplayServerMacOS::global_menu_get_item_indentation_level(const String &p_menu_root, int p_idx) const {
  1172. _THREAD_SAFE_METHOD_
  1173. const NSMenu *menu = _get_menu_root(p_menu_root);
  1174. if (menu) {
  1175. ERR_FAIL_COND_V(p_idx < 0, 0);
  1176. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1177. p_idx++;
  1178. }
  1179. ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], 0);
  1180. const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1181. if (menu_item) {
  1182. return [menu_item indentationLevel];
  1183. }
  1184. }
  1185. return 0;
  1186. }
  1187. void DisplayServerMacOS::global_menu_set_item_checked(const String &p_menu_root, int p_idx, bool p_checked) {
  1188. _THREAD_SAFE_METHOD_
  1189. NSMenu *menu = _get_menu_root(p_menu_root);
  1190. if (menu) {
  1191. ERR_FAIL_COND(p_idx < 0);
  1192. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1193. p_idx++;
  1194. }
  1195. ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
  1196. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1197. if (menu_item) {
  1198. if (p_checked) {
  1199. [menu_item setState:NSControlStateValueOn];
  1200. } else {
  1201. [menu_item setState:NSControlStateValueOff];
  1202. }
  1203. }
  1204. }
  1205. }
  1206. void DisplayServerMacOS::global_menu_set_item_checkable(const String &p_menu_root, int p_idx, bool p_checkable) {
  1207. _THREAD_SAFE_METHOD_
  1208. NSMenu *menu = _get_menu_root(p_menu_root);
  1209. if (menu) {
  1210. ERR_FAIL_COND(p_idx < 0);
  1211. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1212. p_idx++;
  1213. }
  1214. ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
  1215. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1216. if (menu_item) {
  1217. GodotMenuItem *obj = [menu_item representedObject];
  1218. ERR_FAIL_COND(!obj);
  1219. obj->checkable_type = (p_checkable) ? CHECKABLE_TYPE_CHECK_BOX : CHECKABLE_TYPE_NONE;
  1220. }
  1221. }
  1222. }
  1223. void DisplayServerMacOS::global_menu_set_item_radio_checkable(const String &p_menu_root, int p_idx, bool p_checkable) {
  1224. _THREAD_SAFE_METHOD_
  1225. NSMenu *menu = _get_menu_root(p_menu_root);
  1226. if (menu) {
  1227. ERR_FAIL_COND(p_idx < 0);
  1228. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1229. p_idx++;
  1230. }
  1231. ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
  1232. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1233. if (menu_item) {
  1234. GodotMenuItem *obj = [menu_item representedObject];
  1235. ERR_FAIL_COND(!obj);
  1236. obj->checkable_type = (p_checkable) ? CHECKABLE_TYPE_RADIO_BUTTON : CHECKABLE_TYPE_NONE;
  1237. }
  1238. }
  1239. }
  1240. void DisplayServerMacOS::global_menu_set_item_callback(const String &p_menu_root, int p_idx, const Callable &p_callback) {
  1241. _THREAD_SAFE_METHOD_
  1242. NSMenu *menu = _get_menu_root(p_menu_root);
  1243. if (menu) {
  1244. ERR_FAIL_COND(p_idx < 0);
  1245. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1246. p_idx++;
  1247. }
  1248. ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
  1249. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1250. if (menu_item) {
  1251. GodotMenuItem *obj = [menu_item representedObject];
  1252. ERR_FAIL_COND(!obj);
  1253. obj->callback = p_callback;
  1254. }
  1255. }
  1256. }
  1257. void DisplayServerMacOS::global_menu_set_item_key_callback(const String &p_menu_root, int p_idx, const Callable &p_key_callback) {
  1258. _THREAD_SAFE_METHOD_
  1259. NSMenu *menu = _get_menu_root(p_menu_root);
  1260. if (menu) {
  1261. ERR_FAIL_COND(p_idx < 0);
  1262. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1263. p_idx++;
  1264. }
  1265. ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
  1266. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1267. if (menu_item) {
  1268. GodotMenuItem *obj = [menu_item representedObject];
  1269. ERR_FAIL_COND(!obj);
  1270. obj->key_callback = p_key_callback;
  1271. }
  1272. }
  1273. }
  1274. void DisplayServerMacOS::global_menu_set_item_tag(const String &p_menu_root, int p_idx, const Variant &p_tag) {
  1275. _THREAD_SAFE_METHOD_
  1276. NSMenu *menu = _get_menu_root(p_menu_root);
  1277. if (menu) {
  1278. ERR_FAIL_COND(p_idx < 0);
  1279. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1280. p_idx++;
  1281. }
  1282. ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
  1283. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1284. if (menu_item) {
  1285. GodotMenuItem *obj = [menu_item representedObject];
  1286. ERR_FAIL_COND(!obj);
  1287. obj->meta = p_tag;
  1288. }
  1289. }
  1290. }
  1291. void DisplayServerMacOS::global_menu_set_item_text(const String &p_menu_root, int p_idx, const String &p_text) {
  1292. _THREAD_SAFE_METHOD_
  1293. NSMenu *menu = _get_menu_root(p_menu_root);
  1294. if (menu) {
  1295. ERR_FAIL_COND(p_idx < 0);
  1296. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1297. p_idx++;
  1298. }
  1299. ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
  1300. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1301. if (menu_item) {
  1302. [menu_item setTitle:[NSString stringWithUTF8String:p_text.utf8().get_data()]];
  1303. }
  1304. }
  1305. }
  1306. void DisplayServerMacOS::global_menu_set_item_submenu(const String &p_menu_root, int p_idx, const String &p_submenu) {
  1307. _THREAD_SAFE_METHOD_
  1308. NSMenu *menu = _get_menu_root(p_menu_root);
  1309. NSMenu *sub_menu = _get_menu_root(p_submenu);
  1310. if (menu && sub_menu) {
  1311. if (sub_menu == menu) {
  1312. ERR_PRINT("Can't set submenu to self!");
  1313. return;
  1314. }
  1315. if ([sub_menu supermenu]) {
  1316. ERR_PRINT("Can't set submenu to menu that is already a submenu of some other menu!");
  1317. return;
  1318. }
  1319. ERR_FAIL_COND(p_idx < 0);
  1320. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1321. p_idx++;
  1322. }
  1323. ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
  1324. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1325. if (menu_item) {
  1326. [menu setSubmenu:sub_menu forItem:menu_item];
  1327. }
  1328. }
  1329. }
  1330. void DisplayServerMacOS::global_menu_set_item_accelerator(const String &p_menu_root, int p_idx, Key p_keycode) {
  1331. _THREAD_SAFE_METHOD_
  1332. NSMenu *menu = _get_menu_root(p_menu_root);
  1333. if (menu) {
  1334. ERR_FAIL_COND(p_idx < 0);
  1335. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1336. p_idx++;
  1337. }
  1338. ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
  1339. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1340. if (menu_item) {
  1341. [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_keycode)];
  1342. String keycode = KeyMappingMacOS::keycode_get_native_string(p_keycode & KeyModifierMask::CODE_MASK);
  1343. [menu_item setKeyEquivalent:[NSString stringWithUTF8String:keycode.utf8().get_data()]];
  1344. }
  1345. }
  1346. }
  1347. void DisplayServerMacOS::global_menu_set_item_disabled(const String &p_menu_root, int p_idx, bool p_disabled) {
  1348. _THREAD_SAFE_METHOD_
  1349. NSMenu *menu = _get_menu_root(p_menu_root);
  1350. if (menu) {
  1351. ERR_FAIL_COND(p_idx < 0);
  1352. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1353. p_idx++;
  1354. }
  1355. ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
  1356. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1357. if (menu_item) {
  1358. [menu_item setEnabled:(!p_disabled)];
  1359. }
  1360. }
  1361. }
  1362. void DisplayServerMacOS::global_menu_set_item_tooltip(const String &p_menu_root, int p_idx, const String &p_tooltip) {
  1363. _THREAD_SAFE_METHOD_
  1364. NSMenu *menu = _get_menu_root(p_menu_root);
  1365. if (menu) {
  1366. ERR_FAIL_COND(p_idx < 0);
  1367. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1368. p_idx++;
  1369. }
  1370. ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
  1371. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1372. if (menu_item) {
  1373. [menu_item setToolTip:[NSString stringWithUTF8String:p_tooltip.utf8().get_data()]];
  1374. }
  1375. }
  1376. }
  1377. void DisplayServerMacOS::global_menu_set_item_state(const String &p_menu_root, int p_idx, int p_state) {
  1378. _THREAD_SAFE_METHOD_
  1379. NSMenu *menu = _get_menu_root(p_menu_root);
  1380. if (menu) {
  1381. ERR_FAIL_COND(p_idx < 0);
  1382. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1383. p_idx++;
  1384. }
  1385. ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
  1386. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1387. if (menu_item) {
  1388. GodotMenuItem *obj = [menu_item representedObject];
  1389. ERR_FAIL_COND(!obj);
  1390. obj->state = p_state;
  1391. }
  1392. }
  1393. }
  1394. void DisplayServerMacOS::global_menu_set_item_max_states(const String &p_menu_root, int p_idx, int p_max_states) {
  1395. _THREAD_SAFE_METHOD_
  1396. NSMenu *menu = _get_menu_root(p_menu_root);
  1397. if (menu) {
  1398. ERR_FAIL_COND(p_idx < 0);
  1399. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1400. p_idx++;
  1401. }
  1402. ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
  1403. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1404. if (menu_item) {
  1405. GodotMenuItem *obj = [menu_item representedObject];
  1406. ERR_FAIL_COND(!obj);
  1407. obj->max_states = p_max_states;
  1408. }
  1409. }
  1410. }
  1411. void DisplayServerMacOS::global_menu_set_item_icon(const String &p_menu_root, int p_idx, const Ref<Texture2D> &p_icon) {
  1412. _THREAD_SAFE_METHOD_
  1413. NSMenu *menu = _get_menu_root(p_menu_root);
  1414. if (menu) {
  1415. ERR_FAIL_COND(p_idx < 0);
  1416. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1417. p_idx++;
  1418. }
  1419. ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
  1420. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1421. if (menu_item) {
  1422. GodotMenuItem *obj = [menu_item representedObject];
  1423. ERR_FAIL_COND(!obj);
  1424. if (p_icon.is_valid()) {
  1425. obj->img = p_icon->get_image();
  1426. obj->img = obj->img->duplicate();
  1427. if (obj->img->is_compressed()) {
  1428. obj->img->decompress();
  1429. }
  1430. obj->img->resize(16, 16, Image::INTERPOLATE_LANCZOS);
  1431. [menu_item setImage:_convert_to_nsimg(obj->img)];
  1432. } else {
  1433. obj->img = Ref<Image>();
  1434. [menu_item setImage:nil];
  1435. }
  1436. }
  1437. }
  1438. }
  1439. void DisplayServerMacOS::global_menu_set_item_indentation_level(const String &p_menu_root, int p_idx, int p_level) {
  1440. _THREAD_SAFE_METHOD_
  1441. NSMenu *menu = _get_menu_root(p_menu_root);
  1442. if (menu) {
  1443. ERR_FAIL_COND(p_idx < 0);
  1444. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1445. p_idx++;
  1446. }
  1447. ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
  1448. NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
  1449. if (menu_item) {
  1450. [menu_item setIndentationLevel:p_level];
  1451. }
  1452. }
  1453. }
  1454. int DisplayServerMacOS::global_menu_get_item_count(const String &p_menu_root) const {
  1455. _THREAD_SAFE_METHOD_
  1456. const NSMenu *menu = _get_menu_root(p_menu_root);
  1457. if (menu) {
  1458. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1459. return [menu numberOfItems] - 1;
  1460. } else {
  1461. return [menu numberOfItems];
  1462. }
  1463. } else {
  1464. return 0;
  1465. }
  1466. }
  1467. void DisplayServerMacOS::global_menu_remove_item(const String &p_menu_root, int p_idx) {
  1468. _THREAD_SAFE_METHOD_
  1469. NSMenu *menu = _get_menu_root(p_menu_root);
  1470. if (menu) {
  1471. ERR_FAIL_COND(p_idx < 0);
  1472. if (menu == [NSApp mainMenu]) { // Skip Apple menu.
  1473. p_idx++;
  1474. }
  1475. ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
  1476. [menu removeItemAtIndex:p_idx];
  1477. }
  1478. }
  1479. void DisplayServerMacOS::global_menu_clear(const String &p_menu_root) {
  1480. _THREAD_SAFE_METHOD_
  1481. NSMenu *menu = _get_menu_root(p_menu_root);
  1482. if (menu) {
  1483. [menu removeAllItems];
  1484. // Restore Apple menu.
  1485. if (menu == [NSApp mainMenu]) {
  1486. NSMenuItem *menu_item = [menu addItemWithTitle:@"" action:nil keyEquivalent:@""];
  1487. [menu setSubmenu:apple_menu forItem:menu_item];
  1488. }
  1489. if (submenu.has(p_menu_root)) {
  1490. submenu.erase(p_menu_root);
  1491. }
  1492. }
  1493. }
  1494. bool DisplayServerMacOS::tts_is_speaking() const {
  1495. ERR_FAIL_COND_V(!tts, false);
  1496. return [tts isSpeaking];
  1497. }
  1498. bool DisplayServerMacOS::tts_is_paused() const {
  1499. ERR_FAIL_COND_V(!tts, false);
  1500. return [tts isPaused];
  1501. }
  1502. TypedArray<Dictionary> DisplayServerMacOS::tts_get_voices() const {
  1503. ERR_FAIL_COND_V(!tts, Array());
  1504. return [tts getVoices];
  1505. }
  1506. void DisplayServerMacOS::tts_speak(const String &p_text, const String &p_voice, int p_volume, float p_pitch, float p_rate, int p_utterance_id, bool p_interrupt) {
  1507. ERR_FAIL_COND(!tts);
  1508. [tts speak:p_text voice:p_voice volume:p_volume pitch:p_pitch rate:p_rate utterance_id:p_utterance_id interrupt:p_interrupt];
  1509. }
  1510. void DisplayServerMacOS::tts_pause() {
  1511. ERR_FAIL_COND(!tts);
  1512. [tts pauseSpeaking];
  1513. }
  1514. void DisplayServerMacOS::tts_resume() {
  1515. ERR_FAIL_COND(!tts);
  1516. [tts resumeSpeaking];
  1517. }
  1518. void DisplayServerMacOS::tts_stop() {
  1519. ERR_FAIL_COND(!tts);
  1520. [tts stopSpeaking];
  1521. }
  1522. Error DisplayServerMacOS::dialog_show(String p_title, String p_description, Vector<String> p_buttons, const Callable &p_callback) {
  1523. _THREAD_SAFE_METHOD_
  1524. NSAlert *window = [[NSAlert alloc] init];
  1525. NSString *ns_title = [NSString stringWithUTF8String:p_title.utf8().get_data()];
  1526. NSString *ns_description = [NSString stringWithUTF8String:p_description.utf8().get_data()];
  1527. for (int i = 0; i < p_buttons.size(); i++) {
  1528. NSString *ns_button = [NSString stringWithUTF8String:p_buttons[i].utf8().get_data()];
  1529. [window addButtonWithTitle:ns_button];
  1530. }
  1531. [window setMessageText:ns_title];
  1532. [window setInformativeText:ns_description];
  1533. [window setAlertStyle:NSAlertStyleInformational];
  1534. int button_pressed;
  1535. NSInteger ret = [window runModal];
  1536. if (ret == NSAlertFirstButtonReturn) {
  1537. button_pressed = 0;
  1538. } else if (ret == NSAlertSecondButtonReturn) {
  1539. button_pressed = 1;
  1540. } else if (ret == NSAlertThirdButtonReturn) {
  1541. button_pressed = 2;
  1542. } else {
  1543. button_pressed = 2 + (ret - NSAlertThirdButtonReturn);
  1544. }
  1545. if (!p_callback.is_null()) {
  1546. Variant button = button_pressed;
  1547. Variant *buttonp = &button;
  1548. Variant ret;
  1549. Callable::CallError ce;
  1550. p_callback.callp((const Variant **)&buttonp, 1, ret, ce);
  1551. }
  1552. return OK;
  1553. }
  1554. Error DisplayServerMacOS::dialog_input_text(String p_title, String p_description, String p_partial, const Callable &p_callback) {
  1555. _THREAD_SAFE_METHOD_
  1556. NSAlert *window = [[NSAlert alloc] init];
  1557. NSString *ns_title = [NSString stringWithUTF8String:p_title.utf8().get_data()];
  1558. NSString *ns_description = [NSString stringWithUTF8String:p_description.utf8().get_data()];
  1559. NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 250, 30)];
  1560. [window addButtonWithTitle:@"OK"];
  1561. [window setMessageText:ns_title];
  1562. [window setInformativeText:ns_description];
  1563. [window setAlertStyle:NSAlertStyleInformational];
  1564. [input setStringValue:[NSString stringWithUTF8String:p_partial.utf8().get_data()]];
  1565. [window setAccessoryView:input];
  1566. [window runModal];
  1567. String ret;
  1568. ret.parse_utf8([[input stringValue] UTF8String]);
  1569. if (!p_callback.is_null()) {
  1570. Variant text = ret;
  1571. Variant *textp = &text;
  1572. Variant ret;
  1573. Callable::CallError ce;
  1574. p_callback.callp((const Variant **)&textp, 1, ret, ce);
  1575. }
  1576. return OK;
  1577. }
  1578. void DisplayServerMacOS::mouse_set_mode(MouseMode p_mode) {
  1579. _THREAD_SAFE_METHOD_
  1580. if (p_mode == mouse_mode) {
  1581. return;
  1582. }
  1583. WindowID window_id = windows.has(last_focused_window) ? last_focused_window : MAIN_WINDOW_ID;
  1584. WindowData &wd = windows[window_id];
  1585. if (p_mode == MOUSE_MODE_CAPTURED) {
  1586. // Apple Docs state that the display parameter is not used.
  1587. // "This parameter is not used. By default, you may pass kCGDirectMainDisplay."
  1588. // https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/Quartz_Services_Ref/Reference/reference.html
  1589. if (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED) {
  1590. CGDisplayHideCursor(kCGDirectMainDisplay);
  1591. }
  1592. CGAssociateMouseAndMouseCursorPosition(false);
  1593. [wd.window_object setMovable:NO];
  1594. const NSRect contentRect = [wd.window_view frame];
  1595. NSRect pointInWindowRect = NSMakeRect(contentRect.size.width / 2, contentRect.size.height / 2, 0, 0);
  1596. NSPoint pointOnScreen = [[wd.window_view window] convertRectToScreen:pointInWindowRect].origin;
  1597. CGPoint lMouseWarpPos = { pointOnScreen.x, CGDisplayBounds(CGMainDisplayID()).size.height - pointOnScreen.y };
  1598. CGWarpMouseCursorPosition(lMouseWarpPos);
  1599. } else if (p_mode == MOUSE_MODE_HIDDEN) {
  1600. if (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED) {
  1601. CGDisplayHideCursor(kCGDirectMainDisplay);
  1602. }
  1603. [wd.window_object setMovable:YES];
  1604. CGAssociateMouseAndMouseCursorPosition(true);
  1605. } else if (p_mode == MOUSE_MODE_CONFINED) {
  1606. CGDisplayShowCursor(kCGDirectMainDisplay);
  1607. [wd.window_object setMovable:NO];
  1608. CGAssociateMouseAndMouseCursorPosition(false);
  1609. } else if (p_mode == MOUSE_MODE_CONFINED_HIDDEN) {
  1610. if (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED) {
  1611. CGDisplayHideCursor(kCGDirectMainDisplay);
  1612. }
  1613. [wd.window_object setMovable:NO];
  1614. CGAssociateMouseAndMouseCursorPosition(false);
  1615. } else { // MOUSE_MODE_VISIBLE
  1616. CGDisplayShowCursor(kCGDirectMainDisplay);
  1617. [wd.window_object setMovable:YES];
  1618. CGAssociateMouseAndMouseCursorPosition(true);
  1619. }
  1620. last_warp = [[NSProcessInfo processInfo] systemUptime];
  1621. ignore_warp = true;
  1622. warp_events.clear();
  1623. mouse_mode = p_mode;
  1624. if (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED) {
  1625. cursor_update_shape();
  1626. }
  1627. }
  1628. DisplayServer::MouseMode DisplayServerMacOS::mouse_get_mode() const {
  1629. return mouse_mode;
  1630. }
  1631. bool DisplayServerMacOS::update_mouse_wrap(WindowData &p_wd, NSPoint &r_delta, NSPoint &r_mpos, NSTimeInterval p_timestamp) {
  1632. _THREAD_SAFE_METHOD_
  1633. if (ignore_warp) {
  1634. // Discard late events, before warp.
  1635. if (p_timestamp < last_warp) {
  1636. return true;
  1637. }
  1638. ignore_warp = false;
  1639. return true;
  1640. }
  1641. if (mouse_mode == DisplayServer::MOUSE_MODE_CONFINED || mouse_mode == DisplayServer::MOUSE_MODE_CONFINED_HIDDEN) {
  1642. // Discard late events.
  1643. if (p_timestamp < last_warp) {
  1644. return true;
  1645. }
  1646. // Warp affects next event delta, subtract previous warp deltas.
  1647. List<WarpEvent>::Element *F = warp_events.front();
  1648. while (F) {
  1649. if (F->get().timestamp < p_timestamp) {
  1650. List<DisplayServerMacOS::WarpEvent>::Element *E = F;
  1651. r_delta.x -= E->get().delta.x;
  1652. r_delta.y -= E->get().delta.y;
  1653. F = F->next();
  1654. warp_events.erase(E);
  1655. } else {
  1656. F = F->next();
  1657. }
  1658. }
  1659. // Confine mouse position to the window, and update delta.
  1660. NSRect frame = [p_wd.window_object frame];
  1661. NSPoint conf_pos = r_mpos;
  1662. conf_pos.x = CLAMP(conf_pos.x + r_delta.x, 0.f, frame.size.width);
  1663. conf_pos.y = CLAMP(conf_pos.y - r_delta.y, 0.f, frame.size.height);
  1664. r_delta.x = conf_pos.x - r_mpos.x;
  1665. r_delta.y = r_mpos.y - conf_pos.y;
  1666. r_mpos = conf_pos;
  1667. // Move mouse cursor.
  1668. NSRect point_in_window_rect = NSMakeRect(conf_pos.x, conf_pos.y, 0, 0);
  1669. conf_pos = [[p_wd.window_view window] convertRectToScreen:point_in_window_rect].origin;
  1670. conf_pos.y = CGDisplayBounds(CGMainDisplayID()).size.height - conf_pos.y;
  1671. CGWarpMouseCursorPosition(conf_pos);
  1672. // Save warp data.
  1673. last_warp = [[NSProcessInfo processInfo] systemUptime];
  1674. DisplayServerMacOS::WarpEvent ev;
  1675. ev.timestamp = last_warp;
  1676. ev.delta = r_delta;
  1677. warp_events.push_back(ev);
  1678. }
  1679. return false;
  1680. }
  1681. void DisplayServerMacOS::warp_mouse(const Point2i &p_position) {
  1682. _THREAD_SAFE_METHOD_
  1683. if (mouse_mode != MOUSE_MODE_CAPTURED) {
  1684. WindowID window_id = windows.has(last_focused_window) ? last_focused_window : MAIN_WINDOW_ID;
  1685. WindowData &wd = windows[window_id];
  1686. // Local point in window coords.
  1687. const NSRect contentRect = [wd.window_view frame];
  1688. const float scale = screen_get_max_scale();
  1689. NSRect pointInWindowRect = NSMakeRect(p_position.x / scale, contentRect.size.height - (p_position.y / scale - 1), 0, 0);
  1690. NSPoint pointOnScreen = [[wd.window_view window] convertRectToScreen:pointInWindowRect].origin;
  1691. // Point in scren coords.
  1692. CGPoint lMouseWarpPos = { pointOnScreen.x, CGDisplayBounds(CGMainDisplayID()).size.height - pointOnScreen.y };
  1693. // Do the warping.
  1694. CGEventSourceRef lEventRef = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
  1695. CGEventSourceSetLocalEventsSuppressionInterval(lEventRef, 0.0);
  1696. CGAssociateMouseAndMouseCursorPosition(false);
  1697. CGWarpMouseCursorPosition(lMouseWarpPos);
  1698. if (mouse_mode != MOUSE_MODE_CONFINED && mouse_mode != MOUSE_MODE_CONFINED_HIDDEN) {
  1699. CGAssociateMouseAndMouseCursorPosition(true);
  1700. }
  1701. }
  1702. }
  1703. Point2i DisplayServerMacOS::mouse_get_position() const {
  1704. _THREAD_SAFE_METHOD_
  1705. const NSPoint mouse_pos = [NSEvent mouseLocation];
  1706. const float scale = screen_get_max_scale();
  1707. for (NSScreen *screen in [NSScreen screens]) {
  1708. NSRect frame = [screen frame];
  1709. if (NSMouseInRect(mouse_pos, frame, NO)) {
  1710. Vector2i pos = Vector2i((int)mouse_pos.x, (int)mouse_pos.y);
  1711. pos *= scale;
  1712. pos -= _get_screens_origin();
  1713. pos.y *= -1;
  1714. return pos;
  1715. }
  1716. }
  1717. return Vector2i();
  1718. }
  1719. void DisplayServerMacOS::mouse_set_button_state(MouseButton p_state) {
  1720. last_button_state = p_state;
  1721. }
  1722. MouseButton DisplayServerMacOS::mouse_get_button_state() const {
  1723. return last_button_state;
  1724. }
  1725. void DisplayServerMacOS::clipboard_set(const String &p_text) {
  1726. _THREAD_SAFE_METHOD_
  1727. NSString *copiedString = [NSString stringWithUTF8String:p_text.utf8().get_data()];
  1728. NSArray *copiedStringArray = [NSArray arrayWithObject:copiedString];
  1729. NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
  1730. [pasteboard clearContents];
  1731. [pasteboard writeObjects:copiedStringArray];
  1732. }
  1733. String DisplayServerMacOS::clipboard_get() const {
  1734. _THREAD_SAFE_METHOD_
  1735. NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
  1736. NSArray *classArray = [NSArray arrayWithObject:[NSString class]];
  1737. NSDictionary *options = [NSDictionary dictionary];
  1738. BOOL ok = [pasteboard canReadObjectForClasses:classArray options:options];
  1739. if (!ok) {
  1740. return "";
  1741. }
  1742. NSArray *objectsToPaste = [pasteboard readObjectsForClasses:classArray options:options];
  1743. NSString *string = [objectsToPaste objectAtIndex:0];
  1744. String ret;
  1745. ret.parse_utf8([string UTF8String]);
  1746. return ret;
  1747. }
  1748. int DisplayServerMacOS::get_screen_count() const {
  1749. _THREAD_SAFE_METHOD_
  1750. NSArray *screenArray = [NSScreen screens];
  1751. return [screenArray count];
  1752. }
  1753. Point2i DisplayServerMacOS::screen_get_position(int p_screen) const {
  1754. _THREAD_SAFE_METHOD_
  1755. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  1756. p_screen = window_get_current_screen();
  1757. }
  1758. Point2i position = _get_native_screen_position(p_screen) - _get_screens_origin();
  1759. // OS X native y-coordinate relative to _get_screens_origin() is negative,
  1760. // Godot expects a positive value.
  1761. position.y *= -1;
  1762. return position;
  1763. }
  1764. Size2i DisplayServerMacOS::screen_get_size(int p_screen) const {
  1765. _THREAD_SAFE_METHOD_
  1766. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  1767. p_screen = window_get_current_screen();
  1768. }
  1769. NSArray *screenArray = [NSScreen screens];
  1770. if ((NSUInteger)p_screen < [screenArray count]) {
  1771. // Note: Use frame to get the whole screen size.
  1772. NSRect nsrect = [[screenArray objectAtIndex:p_screen] frame];
  1773. return Size2i(nsrect.size.width, nsrect.size.height) * screen_get_max_scale();
  1774. }
  1775. return Size2i();
  1776. }
  1777. int DisplayServerMacOS::screen_get_dpi(int p_screen) const {
  1778. _THREAD_SAFE_METHOD_
  1779. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  1780. p_screen = window_get_current_screen();
  1781. }
  1782. NSArray *screenArray = [NSScreen screens];
  1783. if ((NSUInteger)p_screen < [screenArray count]) {
  1784. NSDictionary *description = [[screenArray objectAtIndex:p_screen] deviceDescription];
  1785. const NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue];
  1786. const CGSize displayPhysicalSize = CGDisplayScreenSize([[description objectForKey:@"NSScreenNumber"] unsignedIntValue]);
  1787. float scale = [[screenArray objectAtIndex:p_screen] backingScaleFactor];
  1788. float den2 = (displayPhysicalSize.width / 25.4f) * (displayPhysicalSize.width / 25.4f) + (displayPhysicalSize.height / 25.4f) * (displayPhysicalSize.height / 25.4f);
  1789. if (den2 > 0.0f) {
  1790. return ceil(sqrt(displayPixelSize.width * displayPixelSize.width + displayPixelSize.height * displayPixelSize.height) / sqrt(den2) * scale);
  1791. }
  1792. }
  1793. return 72;
  1794. }
  1795. float DisplayServerMacOS::screen_get_scale(int p_screen) const {
  1796. _THREAD_SAFE_METHOD_
  1797. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  1798. p_screen = window_get_current_screen();
  1799. }
  1800. if (OS::get_singleton()->is_hidpi_allowed()) {
  1801. NSArray *screenArray = [NSScreen screens];
  1802. if ((NSUInteger)p_screen < [screenArray count]) {
  1803. if ([[screenArray objectAtIndex:p_screen] respondsToSelector:@selector(backingScaleFactor)]) {
  1804. return fmax(1.0, [[screenArray objectAtIndex:p_screen] backingScaleFactor]);
  1805. }
  1806. }
  1807. }
  1808. return 1.f;
  1809. }
  1810. float DisplayServerMacOS::screen_get_max_scale() const {
  1811. _THREAD_SAFE_METHOD_
  1812. // Note: Do not update max display scale on screen configuration change, existing editor windows can't be rescaled on the fly.
  1813. return display_max_scale;
  1814. }
  1815. Rect2i DisplayServerMacOS::screen_get_usable_rect(int p_screen) const {
  1816. _THREAD_SAFE_METHOD_
  1817. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  1818. p_screen = window_get_current_screen();
  1819. }
  1820. NSArray *screenArray = [NSScreen screens];
  1821. if ((NSUInteger)p_screen < [screenArray count]) {
  1822. const float scale = screen_get_max_scale();
  1823. NSRect nsrect = [[screenArray objectAtIndex:p_screen] visibleFrame];
  1824. Point2i position = Point2i(nsrect.origin.x, nsrect.origin.y + nsrect.size.height) * scale - _get_screens_origin();
  1825. position.y *= -1;
  1826. Size2i size = Size2i(nsrect.size.width, nsrect.size.height) * scale;
  1827. return Rect2i(position, size);
  1828. }
  1829. return Rect2i();
  1830. }
  1831. float DisplayServerMacOS::screen_get_refresh_rate(int p_screen) const {
  1832. _THREAD_SAFE_METHOD_
  1833. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  1834. p_screen = window_get_current_screen();
  1835. }
  1836. NSArray *screenArray = [NSScreen screens];
  1837. if ((NSUInteger)p_screen < [screenArray count]) {
  1838. NSDictionary *description = [[screenArray objectAtIndex:p_screen] deviceDescription];
  1839. const CGDisplayModeRef displayMode = CGDisplayCopyDisplayMode([[description objectForKey:@"NSScreenNumber"] unsignedIntValue]);
  1840. const double displayRefreshRate = CGDisplayModeGetRefreshRate(displayMode);
  1841. return (float)displayRefreshRate;
  1842. }
  1843. ERR_PRINT("An error occurred while trying to get the screen refresh rate.");
  1844. return SCREEN_REFRESH_RATE_FALLBACK;
  1845. }
  1846. bool DisplayServerMacOS::screen_is_kept_on() const {
  1847. return (screen_keep_on_assertion);
  1848. }
  1849. void DisplayServerMacOS::screen_set_keep_on(bool p_enable) {
  1850. if (screen_keep_on_assertion) {
  1851. IOPMAssertionRelease(screen_keep_on_assertion);
  1852. screen_keep_on_assertion = kIOPMNullAssertionID;
  1853. }
  1854. if (p_enable) {
  1855. String app_name_string = ProjectSettings::get_singleton()->get("application/config/name");
  1856. NSString *name = [NSString stringWithUTF8String:(app_name_string.is_empty() ? "Godot Engine" : app_name_string.utf8().get_data())];
  1857. NSString *reason = @"Godot Engine running with display/window/energy_saving/keep_screen_on = true";
  1858. IOPMAssertionCreateWithDescription(kIOPMAssertPreventUserIdleDisplaySleep, (__bridge CFStringRef)name, (__bridge CFStringRef)reason, (__bridge CFStringRef)reason, nullptr, 0, nullptr, &screen_keep_on_assertion);
  1859. }
  1860. }
  1861. Vector<DisplayServer::WindowID> DisplayServerMacOS::get_window_list() const {
  1862. _THREAD_SAFE_METHOD_
  1863. Vector<int> ret;
  1864. for (const KeyValue<WindowID, WindowData> &E : windows) {
  1865. ret.push_back(E.key);
  1866. }
  1867. return ret;
  1868. }
  1869. DisplayServer::WindowID DisplayServerMacOS::create_sub_window(WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Rect2i &p_rect) {
  1870. _THREAD_SAFE_METHOD_
  1871. WindowID id = _create_window(p_mode, p_vsync_mode, p_rect);
  1872. for (int i = 0; i < WINDOW_FLAG_MAX; i++) {
  1873. if (p_flags & (1 << i)) {
  1874. window_set_flag(WindowFlags(i), true, id);
  1875. }
  1876. }
  1877. return id;
  1878. }
  1879. void DisplayServerMacOS::show_window(WindowID p_id) {
  1880. WindowData &wd = windows[p_id];
  1881. popup_open(p_id);
  1882. if (wd.no_focus || wd.is_popup) {
  1883. [wd.window_object orderFront:nil];
  1884. } else {
  1885. [wd.window_object makeKeyAndOrderFront:nil];
  1886. }
  1887. }
  1888. void DisplayServerMacOS::delete_sub_window(WindowID p_id) {
  1889. _THREAD_SAFE_METHOD_
  1890. ERR_FAIL_COND(!windows.has(p_id));
  1891. ERR_FAIL_COND_MSG(p_id == MAIN_WINDOW_ID, "Main window can't be deleted");
  1892. WindowData &wd = windows[p_id];
  1893. [wd.window_object setContentView:nil];
  1894. [wd.window_object close];
  1895. }
  1896. void DisplayServerMacOS::window_set_rect_changed_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.rect_changed_callback = p_callable;
  1901. }
  1902. void DisplayServerMacOS::window_set_window_event_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.event_callback = p_callable;
  1907. }
  1908. void DisplayServerMacOS::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) {
  1909. _THREAD_SAFE_METHOD_
  1910. ERR_FAIL_COND(!windows.has(p_window));
  1911. WindowData &wd = windows[p_window];
  1912. wd.input_event_callback = p_callable;
  1913. }
  1914. void DisplayServerMacOS::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) {
  1915. _THREAD_SAFE_METHOD_
  1916. ERR_FAIL_COND(!windows.has(p_window));
  1917. WindowData &wd = windows[p_window];
  1918. wd.input_text_callback = p_callable;
  1919. }
  1920. void DisplayServerMacOS::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) {
  1921. _THREAD_SAFE_METHOD_
  1922. ERR_FAIL_COND(!windows.has(p_window));
  1923. WindowData &wd = windows[p_window];
  1924. wd.drop_files_callback = p_callable;
  1925. }
  1926. void DisplayServerMacOS::window_set_title(const String &p_title, WindowID p_window) {
  1927. _THREAD_SAFE_METHOD_
  1928. ERR_FAIL_COND(!windows.has(p_window));
  1929. WindowData &wd = windows[p_window];
  1930. [wd.window_object setTitle:[NSString stringWithUTF8String:p_title.utf8().get_data()]];
  1931. }
  1932. void DisplayServerMacOS::window_set_mouse_passthrough(const Vector<Vector2> &p_region, WindowID p_window) {
  1933. _THREAD_SAFE_METHOD_
  1934. ERR_FAIL_COND(!windows.has(p_window));
  1935. WindowData &wd = windows[p_window];
  1936. wd.mpath = p_region;
  1937. }
  1938. int DisplayServerMacOS::window_get_current_screen(WindowID p_window) const {
  1939. _THREAD_SAFE_METHOD_
  1940. ERR_FAIL_COND_V(!windows.has(p_window), -1);
  1941. const WindowData &wd = windows[p_window];
  1942. const NSUInteger index = [[NSScreen screens] indexOfObject:[wd.window_object screen]];
  1943. return (index == NSNotFound) ? 0 : index;
  1944. }
  1945. void DisplayServerMacOS::window_set_current_screen(int p_screen, WindowID p_window) {
  1946. _THREAD_SAFE_METHOD_
  1947. ERR_FAIL_COND(!windows.has(p_window));
  1948. WindowData &wd = windows[p_window];
  1949. if (window_get_current_screen(p_window) == p_screen) {
  1950. return;
  1951. }
  1952. bool was_fullscreen = false;
  1953. if (wd.fullscreen) {
  1954. // Temporary exit fullscreen mode to move window.
  1955. [wd.window_object toggleFullScreen:nil];
  1956. was_fullscreen = true;
  1957. }
  1958. Point2i wpos = window_get_position(p_window) - screen_get_position(window_get_current_screen(p_window));
  1959. window_set_position(wpos + screen_get_position(p_screen), p_window);
  1960. if (was_fullscreen) {
  1961. // Re-enter fullscreen mode.
  1962. [wd.window_object toggleFullScreen:nil];
  1963. }
  1964. }
  1965. void DisplayServerMacOS::window_set_exclusive(WindowID p_window, bool p_exclusive) {
  1966. _THREAD_SAFE_METHOD_
  1967. ERR_FAIL_COND(!windows.has(p_window));
  1968. WindowData &wd = windows[p_window];
  1969. if (wd.exclusive != p_exclusive) {
  1970. wd.exclusive = p_exclusive;
  1971. if (wd.transient_parent != INVALID_WINDOW_ID) {
  1972. WindowData &wd_parent = windows[wd.transient_parent];
  1973. if (wd.exclusive) {
  1974. ERR_FAIL_COND_MSG([[wd_parent.window_object childWindows] count] > 0, "Transient parent has another exclusive child.");
  1975. [wd_parent.window_object addChildWindow:wd.window_object ordered:NSWindowAbove];
  1976. } else {
  1977. [wd_parent.window_object removeChildWindow:wd.window_object];
  1978. }
  1979. }
  1980. }
  1981. }
  1982. Point2i DisplayServerMacOS::window_get_position(WindowID p_window) const {
  1983. _THREAD_SAFE_METHOD_
  1984. ERR_FAIL_COND_V(!windows.has(p_window), Point2i());
  1985. const WindowData &wd = windows[p_window];
  1986. // Use content rect position (without titlebar / window border).
  1987. const NSRect contentRect = [wd.window_view frame];
  1988. const NSRect nsrect = [wd.window_object convertRectToScreen:contentRect];
  1989. Point2i pos;
  1990. // Return the position of the top-left corner, for OS X the y starts at the bottom.
  1991. const float scale = screen_get_max_scale();
  1992. pos.x = nsrect.origin.x;
  1993. pos.y = (nsrect.origin.y + nsrect.size.height);
  1994. pos *= scale;
  1995. pos -= _get_screens_origin();
  1996. // OS X native y-coordinate relative to _get_screens_origin() is negative,
  1997. // Godot expects a positive value.
  1998. pos.y *= -1;
  1999. return pos;
  2000. }
  2001. void DisplayServerMacOS::window_set_position(const Point2i &p_position, WindowID p_window) {
  2002. _THREAD_SAFE_METHOD_
  2003. ERR_FAIL_COND(!windows.has(p_window));
  2004. WindowData &wd = windows[p_window];
  2005. Point2i position = p_position;
  2006. // OS X native y-coordinate relative to _get_screens_origin() is negative,
  2007. // Godot passes a positive value.
  2008. position.y *= -1;
  2009. position += _get_screens_origin();
  2010. position /= screen_get_max_scale();
  2011. // Remove titlebar / window border size.
  2012. const NSRect contentRect = [wd.window_view frame];
  2013. const NSRect windowRect = [wd.window_object frame];
  2014. const NSRect nsrect = [wd.window_object convertRectToScreen:contentRect];
  2015. Point2i offset;
  2016. offset.x = (nsrect.origin.x - windowRect.origin.x);
  2017. offset.y = (nsrect.origin.y + nsrect.size.height);
  2018. offset.y -= (windowRect.origin.y + windowRect.size.height);
  2019. [wd.window_object setFrameTopLeftPoint:NSMakePoint(position.x - offset.x, position.y - offset.y)];
  2020. _update_window_style(wd);
  2021. update_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream]);
  2022. }
  2023. void DisplayServerMacOS::window_set_transient(WindowID p_window, WindowID p_parent) {
  2024. _THREAD_SAFE_METHOD_
  2025. ERR_FAIL_COND(p_window == p_parent);
  2026. ERR_FAIL_COND(!windows.has(p_window));
  2027. WindowData &wd_window = windows[p_window];
  2028. ERR_FAIL_COND(wd_window.transient_parent == p_parent);
  2029. ERR_FAIL_COND_MSG(wd_window.on_top, "Windows with the 'on top' can't become transient.");
  2030. if (p_parent == INVALID_WINDOW_ID) {
  2031. // Remove transient.
  2032. ERR_FAIL_COND(wd_window.transient_parent == INVALID_WINDOW_ID);
  2033. ERR_FAIL_COND(!windows.has(wd_window.transient_parent));
  2034. WindowData &wd_parent = windows[wd_window.transient_parent];
  2035. wd_window.transient_parent = INVALID_WINDOW_ID;
  2036. wd_parent.transient_children.erase(p_window);
  2037. [wd_window.window_object setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
  2038. if (wd_window.exclusive) {
  2039. [wd_parent.window_object removeChildWindow:wd_window.window_object];
  2040. }
  2041. } else {
  2042. ERR_FAIL_COND(!windows.has(p_parent));
  2043. ERR_FAIL_COND_MSG(wd_window.transient_parent != INVALID_WINDOW_ID, "Window already has a transient parent");
  2044. WindowData &wd_parent = windows[p_parent];
  2045. wd_window.transient_parent = p_parent;
  2046. wd_parent.transient_children.insert(p_window);
  2047. [wd_window.window_object setCollectionBehavior:NSWindowCollectionBehaviorFullScreenAuxiliary];
  2048. if (wd_window.exclusive) {
  2049. [wd_parent.window_object addChildWindow:wd_window.window_object ordered:NSWindowAbove];
  2050. }
  2051. }
  2052. }
  2053. void DisplayServerMacOS::window_set_max_size(const Size2i p_size, WindowID p_window) {
  2054. _THREAD_SAFE_METHOD_
  2055. ERR_FAIL_COND(!windows.has(p_window));
  2056. WindowData &wd = windows[p_window];
  2057. if ((p_size != Size2i()) && ((p_size.x < wd.min_size.x) || (p_size.y < wd.min_size.y))) {
  2058. ERR_PRINT("Maximum window size can't be smaller than minimum window size!");
  2059. return;
  2060. }
  2061. wd.max_size = p_size;
  2062. if ((wd.max_size != Size2i()) && !wd.fullscreen) {
  2063. Size2i size = wd.max_size / screen_get_max_scale();
  2064. [wd.window_object setContentMaxSize:NSMakeSize(size.x, size.y)];
  2065. } else {
  2066. [wd.window_object setContentMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
  2067. }
  2068. }
  2069. Size2i DisplayServerMacOS::window_get_max_size(WindowID p_window) const {
  2070. _THREAD_SAFE_METHOD_
  2071. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  2072. const WindowData &wd = windows[p_window];
  2073. return wd.max_size;
  2074. }
  2075. void DisplayServerMacOS::window_set_min_size(const Size2i p_size, WindowID p_window) {
  2076. _THREAD_SAFE_METHOD_
  2077. ERR_FAIL_COND(!windows.has(p_window));
  2078. WindowData &wd = windows[p_window];
  2079. if ((p_size != Size2i()) && (wd.max_size != Size2i()) && ((p_size.x > wd.max_size.x) || (p_size.y > wd.max_size.y))) {
  2080. ERR_PRINT("Minimum window size can't be larger than maximum window size!");
  2081. return;
  2082. }
  2083. wd.min_size = p_size;
  2084. if ((wd.min_size != Size2i()) && !wd.fullscreen) {
  2085. Size2i size = wd.min_size / screen_get_max_scale();
  2086. [wd.window_object setContentMinSize:NSMakeSize(size.x, size.y)];
  2087. } else {
  2088. [wd.window_object setContentMinSize:NSMakeSize(0, 0)];
  2089. }
  2090. }
  2091. Size2i DisplayServerMacOS::window_get_min_size(WindowID p_window) const {
  2092. _THREAD_SAFE_METHOD_
  2093. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  2094. const WindowData &wd = windows[p_window];
  2095. return wd.min_size;
  2096. }
  2097. void DisplayServerMacOS::window_set_size(const Size2i p_size, WindowID p_window) {
  2098. _THREAD_SAFE_METHOD_
  2099. ERR_FAIL_COND(!windows.has(p_window));
  2100. WindowData &wd = windows[p_window];
  2101. Size2i size = p_size / screen_get_max_scale();
  2102. NSPoint top_left;
  2103. NSRect old_frame = [wd.window_object frame];
  2104. top_left.x = old_frame.origin.x;
  2105. top_left.y = NSMaxY(old_frame);
  2106. NSRect new_frame = NSMakeRect(0, 0, size.x, size.y);
  2107. new_frame = [wd.window_object frameRectForContentRect:new_frame];
  2108. new_frame.origin.x = top_left.x;
  2109. new_frame.origin.y = top_left.y - new_frame.size.height;
  2110. [wd.window_object setFrame:new_frame display:YES];
  2111. _update_window_style(wd);
  2112. }
  2113. Size2i DisplayServerMacOS::window_get_size(WindowID p_window) const {
  2114. _THREAD_SAFE_METHOD_
  2115. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  2116. const WindowData &wd = windows[p_window];
  2117. return wd.size;
  2118. }
  2119. Size2i DisplayServerMacOS::window_get_real_size(WindowID p_window) const {
  2120. _THREAD_SAFE_METHOD_
  2121. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  2122. const WindowData &wd = windows[p_window];
  2123. NSRect frame = [wd.window_object frame];
  2124. return Size2i(frame.size.width, frame.size.height) * screen_get_max_scale();
  2125. }
  2126. void DisplayServerMacOS::window_set_mode(WindowMode p_mode, WindowID p_window) {
  2127. _THREAD_SAFE_METHOD_
  2128. ERR_FAIL_COND(!windows.has(p_window));
  2129. WindowData &wd = windows[p_window];
  2130. WindowMode old_mode = window_get_mode(p_window);
  2131. if (old_mode == p_mode) {
  2132. return; // Do nothing.
  2133. }
  2134. switch (old_mode) {
  2135. case WINDOW_MODE_WINDOWED: {
  2136. // Do nothing.
  2137. } break;
  2138. case WINDOW_MODE_MINIMIZED: {
  2139. [wd.window_object deminiaturize:nil];
  2140. } break;
  2141. case WINDOW_MODE_EXCLUSIVE_FULLSCREEN:
  2142. case WINDOW_MODE_FULLSCREEN: {
  2143. [(NSWindow *)wd.window_object setLevel:NSNormalWindowLevel];
  2144. _set_window_per_pixel_transparency_enabled(true, p_window);
  2145. if (wd.resize_disabled) { // Restore resize disabled.
  2146. [wd.window_object setStyleMask:[wd.window_object styleMask] & ~NSWindowStyleMaskResizable];
  2147. }
  2148. if (wd.min_size != Size2i()) {
  2149. Size2i size = wd.min_size / screen_get_max_scale();
  2150. [wd.window_object setContentMinSize:NSMakeSize(size.x, size.y)];
  2151. }
  2152. if (wd.max_size != Size2i()) {
  2153. Size2i size = wd.max_size / screen_get_max_scale();
  2154. [wd.window_object setContentMaxSize:NSMakeSize(size.x, size.y)];
  2155. }
  2156. [wd.window_object toggleFullScreen:nil];
  2157. wd.fullscreen = false;
  2158. } break;
  2159. case WINDOW_MODE_MAXIMIZED: {
  2160. if ([wd.window_object isZoomed]) {
  2161. [wd.window_object zoom:nil];
  2162. }
  2163. } break;
  2164. }
  2165. switch (p_mode) {
  2166. case WINDOW_MODE_WINDOWED: {
  2167. // Do nothing.
  2168. } break;
  2169. case WINDOW_MODE_MINIMIZED: {
  2170. [wd.window_object performMiniaturize:nil];
  2171. } break;
  2172. case WINDOW_MODE_EXCLUSIVE_FULLSCREEN:
  2173. case WINDOW_MODE_FULLSCREEN: {
  2174. _set_window_per_pixel_transparency_enabled(false, p_window);
  2175. if (wd.resize_disabled) { // Fullscreen window should be resizable to work.
  2176. [wd.window_object setStyleMask:[wd.window_object styleMask] | NSWindowStyleMaskResizable];
  2177. }
  2178. [wd.window_object setContentMinSize:NSMakeSize(0, 0)];
  2179. [wd.window_object setContentMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
  2180. [wd.window_object toggleFullScreen:nil];
  2181. wd.fullscreen = true;
  2182. } break;
  2183. case WINDOW_MODE_MAXIMIZED: {
  2184. if (![wd.window_object isZoomed]) {
  2185. [wd.window_object zoom:nil];
  2186. }
  2187. } break;
  2188. }
  2189. }
  2190. DisplayServer::WindowMode DisplayServerMacOS::window_get_mode(WindowID p_window) const {
  2191. _THREAD_SAFE_METHOD_
  2192. ERR_FAIL_COND_V(!windows.has(p_window), WINDOW_MODE_WINDOWED);
  2193. const WindowData &wd = windows[p_window];
  2194. if (wd.fullscreen) { // If fullscreen, it's not in another mode.
  2195. return WINDOW_MODE_FULLSCREEN;
  2196. }
  2197. if ([wd.window_object isZoomed] && !wd.resize_disabled) {
  2198. return WINDOW_MODE_MAXIMIZED;
  2199. }
  2200. if ([wd.window_object respondsToSelector:@selector(isMiniaturized)]) {
  2201. if ([wd.window_object isMiniaturized]) {
  2202. return WINDOW_MODE_MINIMIZED;
  2203. }
  2204. }
  2205. // All other discarded, return windowed.
  2206. return WINDOW_MODE_WINDOWED;
  2207. }
  2208. bool DisplayServerMacOS::window_is_maximize_allowed(WindowID p_window) const {
  2209. return true;
  2210. }
  2211. bool DisplayServerMacOS::window_maximize_on_title_dbl_click() const {
  2212. id value = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleActionOnDoubleClick"];
  2213. if ([value isKindOfClass:[NSString class]]) {
  2214. return [value isEqualToString:@"Maximize"];
  2215. }
  2216. return false;
  2217. }
  2218. bool DisplayServerMacOS::window_minimize_on_title_dbl_click() const {
  2219. id value = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleActionOnDoubleClick"];
  2220. if ([value isKindOfClass:[NSString class]]) {
  2221. return [value isEqualToString:@"Minimize"];
  2222. }
  2223. return false;
  2224. }
  2225. Vector2i DisplayServerMacOS::window_get_safe_title_margins(WindowID p_window) const {
  2226. _THREAD_SAFE_METHOD_
  2227. ERR_FAIL_COND_V(!windows.has(p_window), Vector2i());
  2228. const WindowData &wd = windows[p_window];
  2229. float max_x = 0.f;
  2230. NSButton *cb = [wd.window_object standardWindowButton:NSWindowCloseButton];
  2231. if (cb) {
  2232. max_x = MAX(max_x, [cb frame].origin.x + [cb frame].size.width);
  2233. }
  2234. NSButton *mb = [wd.window_object standardWindowButton:NSWindowMiniaturizeButton];
  2235. if (mb) {
  2236. max_x = MAX(max_x, [mb frame].origin.x + [mb frame].size.width);
  2237. }
  2238. NSButton *zb = [wd.window_object standardWindowButton:NSWindowZoomButton];
  2239. if (zb) {
  2240. max_x = MAX(max_x, [zb frame].origin.x + [zb frame].size.width);
  2241. }
  2242. return Vector2i(max_x * screen_get_max_scale(), 0);
  2243. }
  2244. void DisplayServerMacOS::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) {
  2245. _THREAD_SAFE_METHOD_
  2246. ERR_FAIL_COND(!windows.has(p_window));
  2247. WindowData &wd = windows[p_window];
  2248. switch (p_flag) {
  2249. case WINDOW_FLAG_RESIZE_DISABLED: {
  2250. wd.resize_disabled = p_enabled;
  2251. if (wd.fullscreen) { // Fullscreen window should be resizable, style will be applied on exiting fullscreen.
  2252. return;
  2253. }
  2254. if (p_enabled) {
  2255. [wd.window_object setStyleMask:[wd.window_object styleMask] & ~NSWindowStyleMaskResizable];
  2256. } else {
  2257. [wd.window_object setStyleMask:[wd.window_object styleMask] | NSWindowStyleMaskResizable];
  2258. }
  2259. } break;
  2260. case WINDOW_FLAG_EXTEND_TO_TITLE: {
  2261. NSRect rect = [wd.window_object frame];
  2262. if (p_enabled) {
  2263. [wd.window_object setTitlebarAppearsTransparent:YES];
  2264. [wd.window_object setTitleVisibility:NSWindowTitleHidden];
  2265. [wd.window_object setStyleMask:[wd.window_object styleMask] | NSWindowStyleMaskFullSizeContentView];
  2266. } else {
  2267. [wd.window_object setTitlebarAppearsTransparent:NO];
  2268. [wd.window_object setTitleVisibility:NSWindowTitleVisible];
  2269. [wd.window_object setStyleMask:[wd.window_object styleMask] & ~NSWindowStyleMaskFullSizeContentView];
  2270. }
  2271. [wd.window_object setFrame:rect display:YES];
  2272. } break;
  2273. case WINDOW_FLAG_BORDERLESS: {
  2274. // OrderOut prevents a lose focus bug with the window.
  2275. if ([wd.window_object isVisible]) {
  2276. [wd.window_object orderOut:nil];
  2277. }
  2278. wd.borderless = p_enabled;
  2279. if (p_enabled) {
  2280. [wd.window_object setStyleMask:NSWindowStyleMaskBorderless];
  2281. } else {
  2282. _set_window_per_pixel_transparency_enabled(false, p_window);
  2283. [wd.window_object setStyleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | (wd.resize_disabled ? 0 : NSWindowStyleMaskResizable)];
  2284. // Force update of the window styles.
  2285. NSRect frameRect = [wd.window_object frame];
  2286. [wd.window_object setFrame:NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width + 1, frameRect.size.height) display:NO];
  2287. [wd.window_object setFrame:frameRect display:NO];
  2288. }
  2289. _update_window_style(wd);
  2290. if ([wd.window_object isVisible]) {
  2291. if (wd.no_focus || wd.is_popup) {
  2292. [wd.window_object orderFront:nil];
  2293. } else {
  2294. [wd.window_object makeKeyAndOrderFront:nil];
  2295. }
  2296. }
  2297. } break;
  2298. case WINDOW_FLAG_ALWAYS_ON_TOP: {
  2299. wd.on_top = p_enabled;
  2300. if (wd.fullscreen) {
  2301. return;
  2302. }
  2303. if (p_enabled) {
  2304. [(NSWindow *)wd.window_object setLevel:NSFloatingWindowLevel];
  2305. } else {
  2306. [(NSWindow *)wd.window_object setLevel:NSNormalWindowLevel];
  2307. }
  2308. } break;
  2309. case WINDOW_FLAG_TRANSPARENT: {
  2310. if (p_enabled) {
  2311. [wd.window_object setStyleMask:NSWindowStyleMaskBorderless]; // Force borderless.
  2312. } else if (!wd.borderless) {
  2313. [wd.window_object setStyleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | (wd.resize_disabled ? 0 : NSWindowStyleMaskResizable)];
  2314. }
  2315. _set_window_per_pixel_transparency_enabled(p_enabled, p_window);
  2316. } break;
  2317. case WINDOW_FLAG_NO_FOCUS: {
  2318. wd.no_focus = p_enabled;
  2319. } break;
  2320. case WINDOW_FLAG_POPUP: {
  2321. ERR_FAIL_COND_MSG(p_window == MAIN_WINDOW_ID, "Main window can't be popup.");
  2322. ERR_FAIL_COND_MSG([wd.window_object isVisible] && (wd.is_popup != p_enabled), "Popup flag can't changed while window is opened.");
  2323. wd.is_popup = p_enabled;
  2324. } break;
  2325. default: {
  2326. }
  2327. }
  2328. }
  2329. bool DisplayServerMacOS::window_get_flag(WindowFlags p_flag, WindowID p_window) const {
  2330. _THREAD_SAFE_METHOD_
  2331. ERR_FAIL_COND_V(!windows.has(p_window), false);
  2332. const WindowData &wd = windows[p_window];
  2333. switch (p_flag) {
  2334. case WINDOW_FLAG_RESIZE_DISABLED: {
  2335. return wd.resize_disabled;
  2336. } break;
  2337. case WINDOW_FLAG_EXTEND_TO_TITLE: {
  2338. return [wd.window_object styleMask] & NSWindowStyleMaskFullSizeContentView;
  2339. } break;
  2340. case WINDOW_FLAG_BORDERLESS: {
  2341. return [wd.window_object styleMask] == NSWindowStyleMaskBorderless;
  2342. } break;
  2343. case WINDOW_FLAG_ALWAYS_ON_TOP: {
  2344. if (wd.fullscreen) {
  2345. return wd.on_top;
  2346. } else {
  2347. return [(NSWindow *)wd.window_object level] == NSFloatingWindowLevel;
  2348. }
  2349. } break;
  2350. case WINDOW_FLAG_TRANSPARENT: {
  2351. return wd.layered_window;
  2352. } break;
  2353. case WINDOW_FLAG_NO_FOCUS: {
  2354. return wd.no_focus;
  2355. } break;
  2356. case WINDOW_FLAG_POPUP: {
  2357. return wd.is_popup;
  2358. } break;
  2359. default: {
  2360. }
  2361. }
  2362. return false;
  2363. }
  2364. void DisplayServerMacOS::window_request_attention(WindowID p_window) {
  2365. // It's app global, ignore window id.
  2366. [NSApp requestUserAttention:NSCriticalRequest];
  2367. }
  2368. void DisplayServerMacOS::window_move_to_foreground(WindowID p_window) {
  2369. _THREAD_SAFE_METHOD_
  2370. ERR_FAIL_COND(!windows.has(p_window));
  2371. const WindowData &wd = windows[p_window];
  2372. [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
  2373. if (wd.no_focus || wd.is_popup) {
  2374. [wd.window_object orderFront:nil];
  2375. } else {
  2376. [wd.window_object makeKeyAndOrderFront:nil];
  2377. }
  2378. }
  2379. bool DisplayServerMacOS::window_can_draw(WindowID p_window) const {
  2380. return window_get_mode(p_window) != WINDOW_MODE_MINIMIZED;
  2381. }
  2382. bool DisplayServerMacOS::can_any_window_draw() const {
  2383. _THREAD_SAFE_METHOD_
  2384. for (const KeyValue<WindowID, WindowData> &E : windows) {
  2385. if (window_get_mode(E.key) != WINDOW_MODE_MINIMIZED) {
  2386. return true;
  2387. }
  2388. }
  2389. return false;
  2390. }
  2391. void DisplayServerMacOS::window_set_ime_active(const bool p_active, WindowID p_window) {
  2392. _THREAD_SAFE_METHOD_
  2393. ERR_FAIL_COND(!windows.has(p_window));
  2394. WindowData &wd = windows[p_window];
  2395. wd.im_active = p_active;
  2396. if (!p_active) {
  2397. [wd.window_view cancelComposition];
  2398. }
  2399. }
  2400. void DisplayServerMacOS::window_set_ime_position(const Point2i &p_pos, WindowID p_window) {
  2401. _THREAD_SAFE_METHOD_
  2402. ERR_FAIL_COND(!windows.has(p_window));
  2403. WindowData &wd = windows[p_window];
  2404. wd.im_position = p_pos;
  2405. }
  2406. DisplayServer::WindowID DisplayServerMacOS::get_window_at_screen_position(const Point2i &p_position) const {
  2407. Point2i position = p_position;
  2408. position.y *= -1;
  2409. position += _get_screens_origin();
  2410. position /= screen_get_max_scale();
  2411. NSInteger wnum = [NSWindow windowNumberAtPoint:NSMakePoint(position.x, position.y) belowWindowWithWindowNumber:0 /*topmost*/];
  2412. for (const KeyValue<WindowID, WindowData> &E : windows) {
  2413. if ([E.value.window_object windowNumber] == wnum) {
  2414. return E.key;
  2415. }
  2416. }
  2417. return INVALID_WINDOW_ID;
  2418. }
  2419. int64_t DisplayServerMacOS::window_get_native_handle(HandleType p_handle_type, WindowID p_window) const {
  2420. ERR_FAIL_COND_V(!windows.has(p_window), 0);
  2421. switch (p_handle_type) {
  2422. case DISPLAY_HANDLE: {
  2423. return 0; // Not supported.
  2424. }
  2425. case WINDOW_HANDLE: {
  2426. return (int64_t)windows[p_window].window_object;
  2427. }
  2428. case WINDOW_VIEW: {
  2429. return (int64_t)windows[p_window].window_view;
  2430. }
  2431. default: {
  2432. return 0;
  2433. }
  2434. }
  2435. }
  2436. void DisplayServerMacOS::window_attach_instance_id(ObjectID p_instance, WindowID p_window) {
  2437. _THREAD_SAFE_METHOD_
  2438. ERR_FAIL_COND(!windows.has(p_window));
  2439. windows[p_window].instance_id = p_instance;
  2440. }
  2441. ObjectID DisplayServerMacOS::window_get_attached_instance_id(WindowID p_window) const {
  2442. _THREAD_SAFE_METHOD_
  2443. ERR_FAIL_COND_V(!windows.has(p_window), ObjectID());
  2444. return windows[p_window].instance_id;
  2445. }
  2446. void DisplayServerMacOS::gl_window_make_current(DisplayServer::WindowID p_window_id) {
  2447. #if defined(GLES3_ENABLED)
  2448. gl_manager->window_make_current(p_window_id);
  2449. #endif
  2450. }
  2451. void DisplayServerMacOS::window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window) {
  2452. _THREAD_SAFE_METHOD_
  2453. #if defined(GLES3_ENABLED)
  2454. if (gl_manager) {
  2455. gl_manager->set_use_vsync(p_vsync_mode);
  2456. }
  2457. #endif
  2458. #if defined(VULKAN_ENABLED)
  2459. if (context_vulkan) {
  2460. context_vulkan->set_vsync_mode(p_window, p_vsync_mode);
  2461. }
  2462. #endif
  2463. }
  2464. DisplayServer::VSyncMode DisplayServerMacOS::window_get_vsync_mode(WindowID p_window) const {
  2465. _THREAD_SAFE_METHOD_
  2466. #if defined(GLES3_ENABLED)
  2467. if (gl_manager) {
  2468. return (gl_manager->is_using_vsync() ? DisplayServer::VSyncMode::VSYNC_ENABLED : DisplayServer::VSyncMode::VSYNC_DISABLED);
  2469. }
  2470. #endif
  2471. #if defined(VULKAN_ENABLED)
  2472. if (context_vulkan) {
  2473. return context_vulkan->get_vsync_mode(p_window);
  2474. }
  2475. #endif
  2476. return DisplayServer::VSYNC_ENABLED;
  2477. }
  2478. Point2i DisplayServerMacOS::ime_get_selection() const {
  2479. return im_selection;
  2480. }
  2481. String DisplayServerMacOS::ime_get_text() const {
  2482. return im_text;
  2483. }
  2484. void DisplayServerMacOS::cursor_update_shape() {
  2485. _THREAD_SAFE_METHOD_
  2486. if (cursors[cursor_shape] != nullptr) {
  2487. [cursors[cursor_shape] set];
  2488. } else {
  2489. switch (cursor_shape) {
  2490. case CURSOR_ARROW:
  2491. [[NSCursor arrowCursor] set];
  2492. break;
  2493. case CURSOR_IBEAM:
  2494. [[NSCursor IBeamCursor] set];
  2495. break;
  2496. case CURSOR_POINTING_HAND:
  2497. [[NSCursor pointingHandCursor] set];
  2498. break;
  2499. case CURSOR_CROSS:
  2500. [[NSCursor crosshairCursor] set];
  2501. break;
  2502. case CURSOR_WAIT:
  2503. [[NSCursor arrowCursor] set];
  2504. break;
  2505. case CURSOR_BUSY:
  2506. [[NSCursor arrowCursor] set];
  2507. break;
  2508. case CURSOR_DRAG:
  2509. [[NSCursor closedHandCursor] set];
  2510. break;
  2511. case CURSOR_CAN_DROP:
  2512. [[NSCursor openHandCursor] set];
  2513. break;
  2514. case CURSOR_FORBIDDEN:
  2515. [[NSCursor operationNotAllowedCursor] set];
  2516. break;
  2517. case CURSOR_VSIZE:
  2518. [_cursor_from_selector(@selector(_windowResizeNorthSouthCursor), @selector(resizeUpDownCursor)) set];
  2519. break;
  2520. case CURSOR_HSIZE:
  2521. [_cursor_from_selector(@selector(_windowResizeEastWestCursor), @selector(resizeLeftRightCursor)) set];
  2522. break;
  2523. case CURSOR_BDIAGSIZE:
  2524. [_cursor_from_selector(@selector(_windowResizeNorthEastSouthWestCursor)) set];
  2525. break;
  2526. case CURSOR_FDIAGSIZE:
  2527. [_cursor_from_selector(@selector(_windowResizeNorthWestSouthEastCursor)) set];
  2528. break;
  2529. case CURSOR_MOVE:
  2530. [[NSCursor arrowCursor] set];
  2531. break;
  2532. case CURSOR_VSPLIT:
  2533. [[NSCursor resizeUpDownCursor] set];
  2534. break;
  2535. case CURSOR_HSPLIT:
  2536. [[NSCursor resizeLeftRightCursor] set];
  2537. break;
  2538. case CURSOR_HELP:
  2539. [_cursor_from_selector(@selector(_helpCursor)) set];
  2540. break;
  2541. default: {
  2542. }
  2543. }
  2544. }
  2545. }
  2546. void DisplayServerMacOS::cursor_set_shape(CursorShape p_shape) {
  2547. _THREAD_SAFE_METHOD_
  2548. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  2549. if (cursor_shape == p_shape) {
  2550. return;
  2551. }
  2552. cursor_shape = p_shape;
  2553. if (mouse_mode != MOUSE_MODE_VISIBLE && mouse_mode != MOUSE_MODE_CONFINED) {
  2554. return;
  2555. }
  2556. cursor_update_shape();
  2557. }
  2558. DisplayServerMacOS::CursorShape DisplayServerMacOS::cursor_get_shape() const {
  2559. return cursor_shape;
  2560. }
  2561. void DisplayServerMacOS::cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  2562. _THREAD_SAFE_METHOD_
  2563. if (p_cursor.is_valid()) {
  2564. HashMap<CursorShape, Vector<Variant>>::Iterator cursor_c = cursors_cache.find(p_shape);
  2565. if (cursor_c) {
  2566. if (cursor_c->value[0] == p_cursor && cursor_c->value[1] == p_hotspot) {
  2567. cursor_set_shape(p_shape);
  2568. return;
  2569. }
  2570. cursors_cache.erase(p_shape);
  2571. }
  2572. Ref<Texture2D> texture = p_cursor;
  2573. Ref<AtlasTexture> atlas_texture = p_cursor;
  2574. Ref<Image> image;
  2575. Size2 texture_size;
  2576. Rect2 atlas_rect;
  2577. if (texture.is_valid()) {
  2578. image = texture->get_image();
  2579. }
  2580. if (!image.is_valid() && atlas_texture.is_valid()) {
  2581. texture = atlas_texture->get_atlas();
  2582. atlas_rect.size.width = texture->get_width();
  2583. atlas_rect.size.height = texture->get_height();
  2584. atlas_rect.position.x = atlas_texture->get_region().position.x;
  2585. atlas_rect.position.y = atlas_texture->get_region().position.y;
  2586. texture_size.width = atlas_texture->get_region().size.x;
  2587. texture_size.height = atlas_texture->get_region().size.y;
  2588. } else if (image.is_valid()) {
  2589. texture_size.width = texture->get_width();
  2590. texture_size.height = texture->get_height();
  2591. }
  2592. ERR_FAIL_COND(!texture.is_valid());
  2593. ERR_FAIL_COND(p_hotspot.x < 0 || p_hotspot.y < 0);
  2594. ERR_FAIL_COND(texture_size.width > 256 || texture_size.height > 256);
  2595. ERR_FAIL_COND(p_hotspot.x > texture_size.width || p_hotspot.y > texture_size.height);
  2596. image = texture->get_image();
  2597. ERR_FAIL_COND(!image.is_valid());
  2598. NSBitmapImageRep *imgrep = [[NSBitmapImageRep alloc]
  2599. initWithBitmapDataPlanes:nullptr
  2600. pixelsWide:int(texture_size.width)
  2601. pixelsHigh:int(texture_size.height)
  2602. bitsPerSample:8
  2603. samplesPerPixel:4
  2604. hasAlpha:YES
  2605. isPlanar:NO
  2606. colorSpaceName:NSDeviceRGBColorSpace
  2607. bytesPerRow:int(texture_size.width) * 4
  2608. bitsPerPixel:32];
  2609. ERR_FAIL_COND(imgrep == nil);
  2610. uint8_t *pixels = [imgrep bitmapData];
  2611. int len = int(texture_size.width * texture_size.height);
  2612. for (int i = 0; i < len; i++) {
  2613. int row_index = floor(i / texture_size.width) + atlas_rect.position.y;
  2614. int column_index = (i % int(texture_size.width)) + atlas_rect.position.x;
  2615. if (atlas_texture.is_valid()) {
  2616. column_index = MIN(column_index, atlas_rect.size.width - 1);
  2617. row_index = MIN(row_index, atlas_rect.size.height - 1);
  2618. }
  2619. uint32_t color = image->get_pixel(column_index, row_index).to_argb32();
  2620. uint8_t alpha = (color >> 24) & 0xFF;
  2621. pixels[i * 4 + 0] = ((color >> 16) & 0xFF) * alpha / 255;
  2622. pixels[i * 4 + 1] = ((color >> 8) & 0xFF) * alpha / 255;
  2623. pixels[i * 4 + 2] = ((color)&0xFF) * alpha / 255;
  2624. pixels[i * 4 + 3] = alpha;
  2625. }
  2626. NSImage *nsimage = [[NSImage alloc] initWithSize:NSMakeSize(texture_size.width, texture_size.height)];
  2627. [nsimage addRepresentation:imgrep];
  2628. NSCursor *cursor = [[NSCursor alloc] initWithImage:nsimage hotSpot:NSMakePoint(p_hotspot.x, p_hotspot.y)];
  2629. cursors[p_shape] = cursor;
  2630. Vector<Variant> params;
  2631. params.push_back(p_cursor);
  2632. params.push_back(p_hotspot);
  2633. cursors_cache.insert(p_shape, params);
  2634. if (p_shape == cursor_shape) {
  2635. if (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED) {
  2636. [cursor set];
  2637. }
  2638. }
  2639. } else {
  2640. // Reset to default system cursor.
  2641. if (cursors[p_shape] != nullptr) {
  2642. cursors[p_shape] = nullptr;
  2643. }
  2644. cursor_update_shape();
  2645. cursors_cache.erase(p_shape);
  2646. }
  2647. }
  2648. bool DisplayServerMacOS::get_swap_cancel_ok() {
  2649. return false;
  2650. }
  2651. int DisplayServerMacOS::keyboard_get_layout_count() const {
  2652. if (keyboard_layout_dirty) {
  2653. const_cast<DisplayServerMacOS *>(this)->_update_keyboard_layouts();
  2654. }
  2655. return kbd_layouts.size();
  2656. }
  2657. void DisplayServerMacOS::keyboard_set_current_layout(int p_index) {
  2658. if (keyboard_layout_dirty) {
  2659. const_cast<DisplayServerMacOS *>(this)->_update_keyboard_layouts();
  2660. }
  2661. ERR_FAIL_INDEX(p_index, kbd_layouts.size());
  2662. NSString *cur_name = [NSString stringWithUTF8String:kbd_layouts[p_index].name.utf8().get_data()];
  2663. NSDictionary *filter_kbd = @{ (NSString *)kTISPropertyInputSourceType : (NSString *)kTISTypeKeyboardLayout };
  2664. NSArray *list_kbd = (__bridge NSArray *)TISCreateInputSourceList((__bridge CFDictionaryRef)filter_kbd, false);
  2665. for (NSUInteger i = 0; i < [list_kbd count]; i++) {
  2666. NSString *name = (__bridge NSString *)TISGetInputSourceProperty((__bridge TISInputSourceRef)[list_kbd objectAtIndex:i], kTISPropertyLocalizedName);
  2667. if ([name isEqualToString:cur_name]) {
  2668. TISSelectInputSource((__bridge TISInputSourceRef)[list_kbd objectAtIndex:i]);
  2669. break;
  2670. }
  2671. }
  2672. NSDictionary *filter_ime = @{ (NSString *)kTISPropertyInputSourceType : (NSString *)kTISTypeKeyboardInputMode };
  2673. NSArray *list_ime = (__bridge NSArray *)TISCreateInputSourceList((__bridge CFDictionaryRef)filter_ime, false);
  2674. for (NSUInteger i = 0; i < [list_ime count]; i++) {
  2675. NSString *name = (__bridge NSString *)TISGetInputSourceProperty((__bridge TISInputSourceRef)[list_ime objectAtIndex:i], kTISPropertyLocalizedName);
  2676. if ([name isEqualToString:cur_name]) {
  2677. TISSelectInputSource((__bridge TISInputSourceRef)[list_ime objectAtIndex:i]);
  2678. break;
  2679. }
  2680. }
  2681. }
  2682. int DisplayServerMacOS::keyboard_get_current_layout() const {
  2683. if (keyboard_layout_dirty) {
  2684. const_cast<DisplayServerMacOS *>(this)->_update_keyboard_layouts();
  2685. }
  2686. return current_layout;
  2687. }
  2688. String DisplayServerMacOS::keyboard_get_layout_language(int p_index) const {
  2689. if (keyboard_layout_dirty) {
  2690. const_cast<DisplayServerMacOS *>(this)->_update_keyboard_layouts();
  2691. }
  2692. ERR_FAIL_INDEX_V(p_index, kbd_layouts.size(), "");
  2693. return kbd_layouts[p_index].code;
  2694. }
  2695. String DisplayServerMacOS::keyboard_get_layout_name(int p_index) const {
  2696. if (keyboard_layout_dirty) {
  2697. const_cast<DisplayServerMacOS *>(this)->_update_keyboard_layouts();
  2698. }
  2699. ERR_FAIL_INDEX_V(p_index, kbd_layouts.size(), "");
  2700. return kbd_layouts[p_index].name;
  2701. }
  2702. Key DisplayServerMacOS::keyboard_get_keycode_from_physical(Key p_keycode) const {
  2703. if (p_keycode == Key::PAUSE) {
  2704. return p_keycode;
  2705. }
  2706. Key modifiers = p_keycode & KeyModifierMask::MODIFIER_MASK;
  2707. Key keycode_no_mod = p_keycode & KeyModifierMask::CODE_MASK;
  2708. unsigned int macos_keycode = KeyMappingMacOS::unmap_key((Key)keycode_no_mod);
  2709. return (Key)(KeyMappingMacOS::remap_key(macos_keycode, 0) | modifiers);
  2710. }
  2711. void DisplayServerMacOS::process_events() {
  2712. _THREAD_SAFE_METHOD_
  2713. while (true) {
  2714. NSEvent *event = [NSApp
  2715. nextEventMatchingMask:NSEventMaskAny
  2716. untilDate:[NSDate distantPast]
  2717. inMode:NSDefaultRunLoopMode
  2718. dequeue:YES];
  2719. if (event == nil) {
  2720. break;
  2721. }
  2722. [NSApp sendEvent:event];
  2723. }
  2724. if (!drop_events) {
  2725. _process_key_events();
  2726. Input::get_singleton()->flush_buffered_events();
  2727. }
  2728. for (KeyValue<WindowID, WindowData> &E : windows) {
  2729. WindowData &wd = E.value;
  2730. if (wd.mpath.size() > 0) {
  2731. update_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream]);
  2732. if (Geometry2D::is_point_in_polygon(wd.mouse_pos, wd.mpath)) {
  2733. if ([wd.window_object ignoresMouseEvents]) {
  2734. [wd.window_object setIgnoresMouseEvents:NO];
  2735. }
  2736. } else {
  2737. if (![wd.window_object ignoresMouseEvents]) {
  2738. [wd.window_object setIgnoresMouseEvents:YES];
  2739. }
  2740. }
  2741. } else {
  2742. if ([wd.window_object ignoresMouseEvents]) {
  2743. [wd.window_object setIgnoresMouseEvents:NO];
  2744. }
  2745. }
  2746. }
  2747. }
  2748. void DisplayServerMacOS::force_process_and_drop_events() {
  2749. _THREAD_SAFE_METHOD_
  2750. drop_events = true;
  2751. process_events();
  2752. drop_events = false;
  2753. }
  2754. void DisplayServerMacOS::release_rendering_thread() {
  2755. }
  2756. void DisplayServerMacOS::make_rendering_thread() {
  2757. }
  2758. void DisplayServerMacOS::swap_buffers() {
  2759. #if defined(GLES3_ENABLED)
  2760. if (gl_manager) {
  2761. gl_manager->swap_buffers();
  2762. }
  2763. #endif
  2764. }
  2765. void DisplayServerMacOS::set_native_icon(const String &p_filename) {
  2766. _THREAD_SAFE_METHOD_
  2767. Ref<FileAccess> f = FileAccess::open(p_filename, FileAccess::READ);
  2768. ERR_FAIL_COND(f.is_null());
  2769. Vector<uint8_t> data;
  2770. uint64_t len = f->get_length();
  2771. data.resize(len);
  2772. f->get_buffer((uint8_t *)&data.write[0], len);
  2773. NSData *icon_data = [[NSData alloc] initWithBytes:&data.write[0] length:len];
  2774. ERR_FAIL_COND_MSG(!icon_data, "Error reading icon data.");
  2775. NSImage *icon = [[NSImage alloc] initWithData:icon_data];
  2776. ERR_FAIL_COND_MSG(!icon, "Error loading icon.");
  2777. [NSApp setApplicationIconImage:icon];
  2778. }
  2779. void DisplayServerMacOS::set_icon(const Ref<Image> &p_icon) {
  2780. _THREAD_SAFE_METHOD_
  2781. Ref<Image> img = p_icon;
  2782. img = img->duplicate();
  2783. img->convert(Image::FORMAT_RGBA8);
  2784. NSBitmapImageRep *imgrep = [[NSBitmapImageRep alloc]
  2785. initWithBitmapDataPlanes:nullptr
  2786. pixelsWide:img->get_width()
  2787. pixelsHigh:img->get_height()
  2788. bitsPerSample:8
  2789. samplesPerPixel:4
  2790. hasAlpha:YES
  2791. isPlanar:NO
  2792. colorSpaceName:NSDeviceRGBColorSpace
  2793. bytesPerRow:img->get_width() * 4
  2794. bitsPerPixel:32];
  2795. ERR_FAIL_COND(imgrep == nil);
  2796. uint8_t *pixels = [imgrep bitmapData];
  2797. int len = img->get_width() * img->get_height();
  2798. const uint8_t *r = img->get_data().ptr();
  2799. /* Premultiply the alpha channel */
  2800. for (int i = 0; i < len; i++) {
  2801. uint8_t alpha = r[i * 4 + 3];
  2802. pixels[i * 4 + 0] = (uint8_t)(((uint16_t)r[i * 4 + 0] * alpha) / 255);
  2803. pixels[i * 4 + 1] = (uint8_t)(((uint16_t)r[i * 4 + 1] * alpha) / 255);
  2804. pixels[i * 4 + 2] = (uint8_t)(((uint16_t)r[i * 4 + 2] * alpha) / 255);
  2805. pixels[i * 4 + 3] = alpha;
  2806. }
  2807. NSImage *nsimg = [[NSImage alloc] initWithSize:NSMakeSize(img->get_width(), img->get_height())];
  2808. ERR_FAIL_COND(nsimg == nil);
  2809. [nsimg addRepresentation:imgrep];
  2810. [NSApp setApplicationIconImage:nsimg];
  2811. }
  2812. DisplayServer *DisplayServerMacOS::create_func(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error) {
  2813. DisplayServer *ds = memnew(DisplayServerMacOS(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_resolution, r_error));
  2814. if (r_error != OK) {
  2815. OS::get_singleton()->alert("Your video card driver does not support any of the supported Vulkan or OpenGL versions.", "Unable to initialize Video driver");
  2816. }
  2817. return ds;
  2818. }
  2819. Vector<String> DisplayServerMacOS::get_rendering_drivers_func() {
  2820. Vector<String> drivers;
  2821. #if defined(VULKAN_ENABLED)
  2822. drivers.push_back("vulkan");
  2823. #endif
  2824. #if defined(GLES3_ENABLED)
  2825. drivers.push_back("opengl3");
  2826. #endif
  2827. return drivers;
  2828. }
  2829. void DisplayServerMacOS::register_macos_driver() {
  2830. register_create_function("macos", create_func, get_rendering_drivers_func);
  2831. }
  2832. DisplayServer::WindowID DisplayServerMacOS::window_get_active_popup() const {
  2833. const List<WindowID>::Element *E = popup_list.back();
  2834. if (E) {
  2835. return E->get();
  2836. } else {
  2837. return INVALID_WINDOW_ID;
  2838. }
  2839. }
  2840. void DisplayServerMacOS::window_set_popup_safe_rect(WindowID p_window, const Rect2i &p_rect) {
  2841. _THREAD_SAFE_METHOD_
  2842. ERR_FAIL_COND(!windows.has(p_window));
  2843. WindowData &wd = windows[p_window];
  2844. wd.parent_safe_rect = p_rect;
  2845. }
  2846. Rect2i DisplayServerMacOS::window_get_popup_safe_rect(WindowID p_window) const {
  2847. _THREAD_SAFE_METHOD_
  2848. ERR_FAIL_COND_V(!windows.has(p_window), Rect2i());
  2849. const WindowData &wd = windows[p_window];
  2850. return wd.parent_safe_rect;
  2851. }
  2852. void DisplayServerMacOS::popup_open(WindowID p_window) {
  2853. _THREAD_SAFE_METHOD_
  2854. WindowData &wd = windows[p_window];
  2855. if (wd.is_popup) {
  2856. bool was_empty = popup_list.is_empty();
  2857. // Find current popup parent, or root popup if new window is not transient.
  2858. List<WindowID>::Element *C = nullptr;
  2859. List<WindowID>::Element *E = popup_list.back();
  2860. while (E) {
  2861. if (wd.transient_parent != E->get() || wd.transient_parent == INVALID_WINDOW_ID) {
  2862. C = E;
  2863. E = E->prev();
  2864. } else {
  2865. break;
  2866. }
  2867. }
  2868. if (C) {
  2869. send_window_event(windows[C->get()], DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST);
  2870. }
  2871. if (was_empty && popup_list.is_empty()) {
  2872. // Inform OS that popup was opened, to close other native popups.
  2873. [[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.HIToolbox.beginMenuTrackingNotification" object:@"org.godotengine.godot.popup_window"];
  2874. }
  2875. time_since_popup = OS::get_singleton()->get_ticks_msec();
  2876. popup_list.push_back(p_window);
  2877. }
  2878. }
  2879. void DisplayServerMacOS::popup_close(WindowID p_window) {
  2880. _THREAD_SAFE_METHOD_
  2881. bool was_empty = popup_list.is_empty();
  2882. List<WindowID>::Element *E = popup_list.find(p_window);
  2883. while (E) {
  2884. List<WindowID>::Element *F = E->next();
  2885. WindowID win_id = E->get();
  2886. popup_list.erase(E);
  2887. send_window_event(windows[win_id], DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST);
  2888. E = F;
  2889. }
  2890. if (!was_empty && popup_list.is_empty()) {
  2891. // Inform OS that all popups are closed.
  2892. [[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.HIToolbox.endMenuTrackingNotification" object:@"org.godotengine.godot.popup_window"];
  2893. }
  2894. }
  2895. bool DisplayServerMacOS::mouse_process_popups(bool p_close) {
  2896. _THREAD_SAFE_METHOD_
  2897. bool was_empty = popup_list.is_empty();
  2898. bool closed = false;
  2899. if (p_close) {
  2900. // Close all popups.
  2901. List<WindowID>::Element *E = popup_list.front();
  2902. if (E) {
  2903. send_window_event(windows[E->get()], DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST);
  2904. closed = true;
  2905. }
  2906. if (!was_empty) {
  2907. // Inform OS that all popups are closed.
  2908. [[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.HIToolbox.endMenuTrackingNotification" object:@"org.godotengine.godot.popup_window"];
  2909. }
  2910. } else {
  2911. uint64_t delta = OS::get_singleton()->get_ticks_msec() - time_since_popup;
  2912. if (delta < 250) {
  2913. return false;
  2914. }
  2915. Point2i pos = mouse_get_position();
  2916. List<WindowID>::Element *C = nullptr;
  2917. List<WindowID>::Element *E = popup_list.back();
  2918. // Find top popup to close.
  2919. while (E) {
  2920. // Popup window area.
  2921. Rect2i win_rect = Rect2i(window_get_position(E->get()), window_get_size(E->get()));
  2922. // Area of the parent window, which responsible for opening sub-menu.
  2923. Rect2i safe_rect = window_get_popup_safe_rect(E->get());
  2924. if (win_rect.has_point(pos)) {
  2925. break;
  2926. } else if (safe_rect != Rect2i() && safe_rect.has_point(pos)) {
  2927. break;
  2928. } else {
  2929. C = E;
  2930. E = E->prev();
  2931. }
  2932. }
  2933. if (C) {
  2934. send_window_event(windows[C->get()], DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST);
  2935. closed = true;
  2936. }
  2937. if (!was_empty && popup_list.is_empty()) {
  2938. // Inform OS that all popups are closed.
  2939. [[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.HIToolbox.endMenuTrackingNotification" object:@"org.godotengine.godot.popup_window"];
  2940. }
  2941. }
  2942. return closed;
  2943. }
  2944. DisplayServerMacOS::DisplayServerMacOS(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error) {
  2945. Input::get_singleton()->set_event_dispatch_function(_dispatch_input_events);
  2946. r_error = OK;
  2947. memset(cursors, 0, sizeof(cursors));
  2948. event_source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
  2949. ERR_FAIL_COND(!event_source);
  2950. CGEventSourceSetLocalEventsSuppressionInterval(event_source, 0.0);
  2951. int screen_count = get_screen_count();
  2952. for (int i = 0; i < screen_count; i++) {
  2953. display_max_scale = fmax(display_max_scale, screen_get_scale(i));
  2954. }
  2955. // Register to be notified on keyboard layout changes.
  2956. CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(),
  2957. nullptr, _keyboard_layout_changed,
  2958. kTISNotifySelectedKeyboardInputSourceChanged, nullptr,
  2959. CFNotificationSuspensionBehaviorDeliverImmediately);
  2960. // Register to be notified on displays arrangement changes.
  2961. CGDisplayRegisterReconfigurationCallback(_displays_arrangement_changed, nullptr);
  2962. // Init TTS
  2963. tts = [[TTS_MacOS alloc] init];
  2964. NSMenuItem *menu_item;
  2965. NSString *title;
  2966. NSString *nsappname = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
  2967. if (nsappname == nil) {
  2968. nsappname = [[NSProcessInfo processInfo] processName];
  2969. }
  2970. // Setup Dock menu.
  2971. dock_menu = [[NSMenu alloc] initWithTitle:@"_dock"];
  2972. [dock_menu setAutoenablesItems:NO];
  2973. // Setup Apple menu.
  2974. apple_menu = [[NSMenu alloc] initWithTitle:@""];
  2975. title = [NSString stringWithFormat:NSLocalizedString(@"About %@", nil), nsappname];
  2976. [apple_menu addItemWithTitle:title action:@selector(showAbout:) keyEquivalent:@""];
  2977. [apple_menu setAutoenablesItems:NO];
  2978. [apple_menu addItem:[NSMenuItem separatorItem]];
  2979. NSMenu *services = [[NSMenu alloc] initWithTitle:@""];
  2980. menu_item = [apple_menu addItemWithTitle:NSLocalizedString(@"Services", nil) action:nil keyEquivalent:@""];
  2981. [apple_menu setSubmenu:services forItem:menu_item];
  2982. [NSApp setServicesMenu:services];
  2983. [apple_menu addItem:[NSMenuItem separatorItem]];
  2984. title = [NSString stringWithFormat:NSLocalizedString(@"Hide %@", nil), nsappname];
  2985. [apple_menu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
  2986. menu_item = [apple_menu addItemWithTitle:NSLocalizedString(@"Hide Others", nil) action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
  2987. [menu_item setKeyEquivalentModifierMask:(NSEventModifierFlagOption | NSEventModifierFlagCommand)];
  2988. [apple_menu addItemWithTitle:NSLocalizedString(@"Show all", nil) action:@selector(unhideAllApplications:) keyEquivalent:@""];
  2989. [apple_menu addItem:[NSMenuItem separatorItem]];
  2990. title = [NSString stringWithFormat:NSLocalizedString(@"\t\tQuit %@", nil), nsappname];
  2991. [apple_menu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
  2992. // Add items to the menu bar.
  2993. NSMenu *main_menu = [NSApp mainMenu];
  2994. menu_item = [main_menu addItemWithTitle:@"" action:nil keyEquivalent:@""];
  2995. [main_menu setSubmenu:apple_menu forItem:menu_item];
  2996. [main_menu setAutoenablesItems:NO];
  2997. menu_delegate = [[GodotMenuDelegate alloc] init];
  2998. //!!!!!!!!!!!!!!!!!!!!!!!!!!
  2999. //TODO - do Vulkan and OpenGL support checks, driver selection and fallback
  3000. rendering_driver = p_rendering_driver;
  3001. #if defined(GLES3_ENABLED)
  3002. if (rendering_driver == "opengl3") {
  3003. GLManager_MacOS::ContextType opengl_api_type = GLManager_MacOS::GLES_3_0_COMPATIBLE;
  3004. gl_manager = memnew(GLManager_MacOS(opengl_api_type));
  3005. if (gl_manager->initialize() != OK) {
  3006. memdelete(gl_manager);
  3007. gl_manager = nullptr;
  3008. r_error = ERR_UNAVAILABLE;
  3009. ERR_FAIL_MSG("Could not initialize OpenGL");
  3010. return;
  3011. }
  3012. }
  3013. #endif
  3014. #if defined(VULKAN_ENABLED)
  3015. if (rendering_driver == "vulkan") {
  3016. context_vulkan = memnew(VulkanContextMacOS);
  3017. if (context_vulkan->initialize() != OK) {
  3018. memdelete(context_vulkan);
  3019. context_vulkan = nullptr;
  3020. r_error = ERR_CANT_CREATE;
  3021. ERR_FAIL_MSG("Could not initialize Vulkan");
  3022. }
  3023. }
  3024. #endif
  3025. Point2i window_position(
  3026. screen_get_position(0).x + (screen_get_size(0).width - p_resolution.width) / 2,
  3027. screen_get_position(0).y + (screen_get_size(0).height - p_resolution.height) / 2);
  3028. WindowID main_window = _create_window(p_mode, p_vsync_mode, Rect2i(window_position, p_resolution));
  3029. ERR_FAIL_COND(main_window == INVALID_WINDOW_ID);
  3030. for (int i = 0; i < WINDOW_FLAG_MAX; i++) {
  3031. if (p_flags & (1 << i)) {
  3032. window_set_flag(WindowFlags(i), true, main_window);
  3033. }
  3034. }
  3035. show_window(MAIN_WINDOW_ID);
  3036. #if defined(GLES3_ENABLED)
  3037. if (rendering_driver == "opengl3") {
  3038. RasterizerGLES3::make_current();
  3039. }
  3040. #endif
  3041. #if defined(VULKAN_ENABLED)
  3042. if (rendering_driver == "vulkan") {
  3043. rendering_device_vulkan = memnew(RenderingDeviceVulkan);
  3044. rendering_device_vulkan->initialize(context_vulkan);
  3045. RendererCompositorRD::make_current();
  3046. }
  3047. #endif
  3048. screen_set_keep_on(GLOBAL_GET("display/window/energy_saving/keep_screen_on"));
  3049. }
  3050. DisplayServerMacOS::~DisplayServerMacOS() {
  3051. if (screen_keep_on_assertion) {
  3052. IOPMAssertionRelease(screen_keep_on_assertion);
  3053. screen_keep_on_assertion = kIOPMNullAssertionID;
  3054. }
  3055. // Destroy all windows.
  3056. for (HashMap<WindowID, WindowData>::Iterator E = windows.begin(); E;) {
  3057. HashMap<WindowID, WindowData>::Iterator F = E;
  3058. ++E;
  3059. [F->value.window_object setContentView:nil];
  3060. [F->value.window_object close];
  3061. }
  3062. // Destroy drivers.
  3063. #if defined(GLES3_ENABLED)
  3064. if (gl_manager) {
  3065. memdelete(gl_manager);
  3066. gl_manager = nullptr;
  3067. }
  3068. #endif
  3069. #if defined(VULKAN_ENABLED)
  3070. if (rendering_device_vulkan) {
  3071. rendering_device_vulkan->finalize();
  3072. memdelete(rendering_device_vulkan);
  3073. rendering_device_vulkan = nullptr;
  3074. }
  3075. if (context_vulkan) {
  3076. memdelete(context_vulkan);
  3077. context_vulkan = nullptr;
  3078. }
  3079. #endif
  3080. CFNotificationCenterRemoveObserver(CFNotificationCenterGetDistributedCenter(), nullptr, kTISNotifySelectedKeyboardInputSourceChanged, nullptr);
  3081. CGDisplayRemoveReconfigurationCallback(_displays_arrangement_changed, nullptr);
  3082. cursors_cache.clear();
  3083. }